Repository navigation
Expand file tree
/
Copy pathpnetcn.R
More file actions
132 lines (122 loc) · 4.36 KB
/
Copy pathpnetcn.R
File metadata and controls
132 lines (122 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
source('initvars.R')
source('AllocateMo.R')
source('AllocateYr.R')
source('AtmEnviron.R')
source('CNTrans.R')
source('Decomp.R')
source('Leach.R')
source('Phenology.R')
source('Photosyn.R')
source('SoilResp.R')
source('storeoutput.R')
source('Waterbal.R')
source('YearInit.R')
pnetcn <-function( climIn,siteIn,vegIn) {
#
# [out]<-pnetcn(climIn,siteIn,vegIn)
#
## Initialize
#clear global
#
## Vegetation and site constants
#global veg # Vegetation parameters (input)
#global site # Site specif (ic parameters (input)) {
#global clim # Climatological time series (input)
#global share # Internal variables shared between functions
#global out # Calculated variables int}#ended to be returned to the user
#global rstep # The current running timestep of the execution (day or month)
#global ystep # The yearstep (only used in constructing the output array)
#global CN_Mode # 1 if ( running PnET-CN) {
# Assign input structures to those used within the model
veg <- vegIn
site <- siteIn
clim <- climIn
ystep=1
rstep=1
# Initialize variables
share=initvars(clim,veg)
#print(paste("share$PlantN (0) (pnetcn, rstep=",rstep,") =",share$PlantN))
share$FolMassMax=NA
share$FolMassMin=NA
share$FolNCon=NA
share$year=clim$year[1]
share$doy=clim$doy[1]
share$time.POSIXt=strptime(paste(share$year,share$doy,sep="-"),format="%Y-%j")
outyearly=share
#outyearly$NetPsnMoNoO3=NA
##outyearly$SnowPack=NA
outmonthly=share
#outmonthly$NetPsnMoNoO3=NA
##outmonthly$SnowPack=NA #APPARENTLY this modifies share... don't want
#print(paste("share$GDDTot=",share$GDDTot))
clim$dayspan=clim$doy
#clim$dayspan=30
for ( i in 1 : length(clim$doy) ) {
clim$dayspan[i]<-30
}#end
CN_Mode<-1
## Main run loop
# Loop through all climate records
#print(paste("rstep(1)=",rstep))
#print(paste("clim$doy[rstep](1)=",clim$doy[rstep]))
for ( rstep in 1 : length(clim$doy) ) {
#print(paste("rstep(2)=",rstep))
#print(paste("clim$doy[rstep](2)=",clim$doy[rstep]))
#print(paste("(rstep!=1) =",rstep!=1))
# End-of-year activity
if ( (rstep != 1) && (clim$doy[rstep] < clim$doy[rstep-1])) {
share$FolMassMax=veg$FolMassMax #to be able to change
share$FolMassMin=veg$FolMassMin #to be able to change
share$FolNCon=veg$FolNCon #to be able to change
share=AllocateYr(share,clim,site,veg,rstep,1)
#print("I am here?")
#print(paste("veg$FolMassMax=", veg$FolMassMax))
#print(paste("share$FolMassMax=", share$FolMassMax))
veg$FolMassMax=share$FolMassMax #changing ************
veg$FolMassMin=share$FolMassMin #changing
veg$FolNCon=share$FolNCon #changing
#out=storeoutput(1)
outyearly=rbind(outyearly,share)
ystep=ystep+1
print(paste("Year ",clim$year[rstep], "starting."))
share=YearInit(share)
#print(paste("rstep(3)=",rstep))
#print(paste("clim$doy[rstep](3)=",clim$doy[rstep]))
}#end
# Call subroutines
#print(paste("share$PlantN (1) (pnetcn, rstep=",rstep,") =",share$PlantN))
share=AtmEnviron(share,clim,rstep)
share=Phenology(share,clim,veg,rstep,1)
share=Photosyn(share,clim,site,veg,rstep)
share$SnowPack=site$SnowPack #so can be modified by Waterbal routine
#print(paste("share$SnowPack (before, rstep=",rstep,") =",share$SnowPack))
#print(paste("site$SnowPack (before, rstep=",rstep,") =",site$SnowPack))
share=Waterbal(share,clim,site,veg,rstep)
site$SnowPack=share$SnowPack #incorporates change to SnowPack
#print(paste("share$SnowPack (after, rstep=",rstep,") =",share$SnowPack))
#print(paste("site$SnowPack (after, rstep=",rstep,") =",site$SnowPack))
#AllocateMo_NoTMult
#print(paste("share$PlantN (2) (pnetcn, rstep=",rstep,") =",share$PlantN))
share=AllocateMo(share,clim,veg,rstep,CN_Mode)
share=Phenology(share,clim,veg,rstep,2)
#print(paste("share$PlantN (2.5) (pnetcn, rstep=",rstep,") =",share$PlantN))
share=CNTrans(share,clim,site,veg,rstep)
#print(paste("share$PlantN (3) (pnetcn, rstep=",rstep,") =",share$PlantN))
share=Decomp(share,clim,veg,rstep)
share=Leach(share)
#out=storeoutput(outmonthly,share,rstep,0)
#print(names(outmonthly))
#print(names(share))
#add dates
share$year=clim$year[rstep]
share$doy=clim$doy[rstep]
share$time.POSIXt=strptime(paste(share$year,share$doy,sep="-"),format="%Y-%j")
outmonthly=rbind(outmonthly,share)
}#end
# Calculate final year
#AllocateYr
share=AllocateYr(share,clim,site,veg,rstep,1)
#storeoutput(1)
outyearly=rbind(outyearly,share)
return(c(outmonthly,outyearly));
}