140 likes | 285 Views
Running ORGANON in R. Peter Gould and David Marshall Growth Model User Group December 9, 2011. Why run ORGANON in R?. R is an excellent environment for analysis. You can do just about any computing task in R.
E N D
Running ORGANON in R Peter Gould and David Marshall Growth Model User Group December 9, 2011
Why run ORGANON in R? R is an excellent environment for analysis. You can do just about any computing task in R. Connects with different data formats (text files, Access databases, SQL databases). Produced excellent graphics, also tables and other types of text output. Many tasks can be automated. If you can do it in R, why do it any other way?
What do we need to get started? Computer with Windows OS R (free download from website). ORGANON source code (download from website). Freeware FORTRAN compiler (gfortran, installed as part of MinGW). A little patience…
Everything’s installed, what do we do now? Make a few changes to the ORGANON source code. Recompile the dlls* using gfortran. Write R scripts to access the dlls. *Dynamic link library: a library of subroutines that can be called by a Windows application.
Changes to the Source Code Compiling with gfortran will make “R-compatible” dlls but a few things need to be changed: Remove or comment out all lines that contain “DLL EXPORT” in these files: PREPARE.FOR EXECUTE2.FOR ORGVOL.FOR WOODQUAL.FOR Change the functions JIFIX and INT4 to LONG in: ORGVOL.FOR VOLEQNS.FOR
Example: add a subroutine to identify the new dll: You can also make other changes. C RECORD CHANGES WHEN THE DLL IS RECOMPILED SUBROUTINE REVISION(HISTORY) CHARACTER*500 HISTORY HISTORY='Recompiled by Peter Gould Nov 13,2011. Minor changes' 1 //'were made for compatibility with gfortran. No substantive' 2 //' changes were made. Contact: pgould@fs.fed.us' END
Compile the dlls using gfortran Compiler is called from the command prompt. gfortran -shared -static-libgcc -o ORGEDIT.dll DIAMCAL.FOR COMFILES.FOR START2.FOR PREPARE.FOR Edit the PATH variable to make the call to the compiler easier.
Download Unzip files We can also do all these things with R … Make needed changes ###Download and compile ORGANON dlls ##Peter Gould Dec 6, 2011 ###set a working directory setwd("C:/AdataFolder/ORGANON/R_EXAMPLE") ###download ORGANON download.file("http://www.cof.orst.edu/cof/fr/research/organon/ORGANON%20DLLS%20SOURCE%20CODE.ZIP", "ORGSOURCE.ZIP",mode="wb") ##unzip file unzip("ORGSOURCE.ZIP") unzip("EDITDLL SOURCE CODE.ZIP") unzip("RUNDLL SOURCE CODE.ZIP") unzip("VOLDLL SOURCE CODE.ZIP") unzip("WQDLL SOURCE CODE.ZIP") ###edit files allfiles = dir(pattern=".FOR",recursive = T,full.name=T) for(getfile in allfiles){ read1 = read.table(getfile,sep="~",as.is=T,quote="") read1 = read1$V1 ##remove DLL statements read1 = read1[!grepl("DLL_EXPORT",read1)] ##replace JIFIX statements read1 =sub("JIFIX","LONG",read1) ##replace INT4 statements read1 =sub("INT4","LONG",read1) ##re-write the file write(read1,getfile,ncolumns=1) } ##write a batch file to run the compiler send = rep("",5) send[1] = getwd() send[1] = gsub("/","\\\\",send[1]) send[1] = paste("cd",send[1]) send[2] = "gfortran -shared -static-libgcc -o ORGEDIT.dll DIAMCAL.FOR COMFILES.FOR START2.FOR PREPARE.FOR" send[3] = "gfortran -shared -static-libgcc -static-libgfortran -o ORGRUN.dll CRNGROW.FOR DIAGRO.FOR EXECUTE2.FOR GROW.FOR GROWTH_MODS.FOR HTGROWTH.FOR MORTALITY.FOR STATS.FOR SUBMAX.FOR TRIPLE.FOR WHPHG.FOR" send[4] = "gfortran -shared -static-libgcc -o ORGVOL.dll ORGVOL.FOR VOLEQNS.FOR" send[5] = "gfortran -shared -static-libgcc -o ORGWQ.dll COMFILES2.FOR WOODQ2.FOR WOODQUAL.FOR" write.table(send,"COMPILE.BAT",row.names=F,col.names=F,quote=F) system("COMPILE.BAT") Compile
Load data into R. Format data to get it ready to run. Load the dlls. Call the subroutine “prepare” to fill-in heights and crown ratios. Project the stand 1 cycle (5 yrs) by calling “execute”. Load the projected values into the initial values. Repeat steps 4 and 5 until the projection is completed. Making a model run
Loading the dlls dyn.load() ###load dll ##DEFINE THE DIRECTORY WHERE THE DLLS WERE PLACED setwd("C:/AdataFolder/ORGANON/COMPILE") dyn.load("ORGEDIT.dll", type="Fortran") dyn.load("ORGRUN.dll", type="Fortran") dyn.load("ORGVOL.dll", type="Fortran")
All variables must be initialized first: ## initialize variables VERSION = 1 NPTS = 2 NTREES = 10 STAGE = 40 BHAGE = 37 Calling a dll from R: • Variables must be “cast” within a call: grow = .Fortran("execute", as.integer(CYCLEG), as.integer(VERSION), as.integer(NPTS), as.integer(NTREES1), as.integer(STAGE), as.integer(BHAGE), as.integer(TREENO), as.integer(PTNO), as.integer(SPECIES), as.integer(USER), as.integer(INDS), as.single(DBH1), as.single(HT1), as.single(CR1), as.single(SCR1), as.single(EXPAN1), as.single(MGEXP),
Double-click icon to open. Doesn’t work in “slide show” mode. Example Script
Create an R package for ORGANON. • Can also make individual functions available such as “volcal” and “prepare” to impute heights and crown ratios. • Create functions to “seamlessly” hand off projections from CONIFERS to ORGANON. • Create a working custom version of the main ORGANON growth model. • Do simple or elaborate projections/analyses within R. What can we do now?