running organon in r peter gould and david marshall growth model user group december 9, 2011

14
Running ORGANON in R Peter Gould and David Marshall Growth Model User Group December 9, 2011

Upload: charla-allison

Post on 01-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Running ORGANON in R

Peter Gould and David MarshallGrowth 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

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: [email protected]'

END

Example: add a subroutine to identify the new dll:

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.

We can also do all these things with R …###Download and compile ORGANON dlls##Peter Gould Dec 6, 2011###set a working directorysetwd("C:/AdataFolder/ORGANON/R_EXAMPLE")###download ORGANONdownload.file("http://www.cof.orst.edu/cof/fr/research/organon/ORGANON%20DLLS%20SOURCE%20CODE.ZIP", "ORGSOURCE.ZIP",mode="wb")##unzip fileunzip("ORGSOURCE.ZIP")unzip("EDITDLL SOURCE CODE.ZIP")unzip("RUNDLL SOURCE CODE.ZIP")unzip("VOLDLL SOURCE CODE.ZIP")unzip("WQDLL SOURCE CODE.ZIP")###edit filesallfiles = 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 compilersend = 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")

Download

Unzip files

Make neededchanges

Compile

Making a model run1. Load data into R.2. Format data to get it ready to run.3. Load the dlls.4. Call the subroutine “prepare” to fill-in

heights and crown ratios.5. Project the stand 1 cycle (5 yrs) by calling

“execute”.6. Load the projected values into the initial

values.7. Repeat steps 4 and 5 until the projection is

completed.

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")

Calling a dll from R:

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),

• All variables must be initialized first:

• Variables must be “cast” within a call:

## initialize variablesVERSION = 1NPTS = 2NTREES = 10STAGE = 40BHAGE = 37

Example Script

WordPad Document

Double-click icon to open. Doesn’t work in “slide show” mode.

Speed Test100 stands200 trees/stand20 periods56.5 seconds

What can we do now?• 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.