You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
GPPbyYear<-matrix(GPPx,365,length(GPPx)/365) # Divide GPP data into a matrix so that rows are days and columns are years
103
+
GPPm= unlist(apply(GPPbyYear, 2, function(x) # Applies aggregation on the columns of input matrix of GPPbyYear.
104
+
aggregate(x,by=list(monthsDays),FUN=func)$x)) # Aggregation is done by groups in the variable monthsDays, as there is no timestamp on datapoints of GPP
105
+
GPPm<- as.vector(GPPm)
106
+
return(GPPm)
107
+
}
108
+
109
+
monthlyWeather<-function(varx,func){ # This function works similarly as the monthlyGPP above but with different data set
### A post-process function for modifying prebas model output for meeting the Digital Twin Earth project requirements
120
+
monthlyFluxes<-function(modOut,weatherOption=1){
121
+
cueGV<-0.5###carbon use efficiency (NPP/GPP) of ground vegetation
122
+
123
+
# if(is.matrix(mGPP)){
124
+
##aggregating total GPP (tree layers + GV) at monthly time step
125
+
mGPPtot<- t(apply(modOut$dailyPRELES[,,1],1,monthlyGPP,"sum")) # Daily GPP is converted into daily GPP using the function monthlyGPP, which is defined above
126
+
# $dailyPRELES is a output matrix from the prebas model
127
+
### Calculate CUE for different layers
128
+
cueTrees<-modOut$multiOut[,,18,,1]/modOut$multiOut[,,44,,1] # CUE = calculate npp / GPPspecies, but include data only from the standing trees
129
+
cueTrees[which(is.na(cueTrees))] <-0.# Replace NAs with a value
130
+
cueAll<-pGPP<-array(NA,dim=c(modOut$nSites,modOut$maxYears,(modOut$maxNlayers+1))) # Create a pair of 3D arrays: x=1:7, y=1:150, z=1:4
131
+
cueAll[,,1] <-cueGV# When layer is 1, then CUE is cueGV?
132
+
cueAll[,,2:(modOut$maxNlayers+1)] <-cueTrees# ... and otherwise its cueTrees?
133
+
134
+
### Calculate total GPP (totGPP) and ratio of GPP at a layer/total GPP (pGPP)
135
+
totGPP<- apply(modOut$multiOut[,,44,,1],1:2,sum) +modOut$GVout[,,3] # Sum of GPP for each year and site in a 2d array
136
+
pGPP[,,1] <-modOut$GVout[,,3]/totGPP# Ratio between CUE of vegetation and CUE total?
137
+
138
+
for(iin1:modOut$maxNlayers){ # Ratio between CUE of vegetation and pine or spruce or mixed
139
+
pGPP[,,(i+1)] <-modOut$multiOut[,,44,i,1] /totGPP
140
+
}
141
+
### Create arrays for monthly GPP and NPP values
142
+
mGPP<-mNPP<-array(NA,dim=c(modOut$nSites,modOut$maxYears*12,(modOut$maxNlayers+1))) # Create another 3D array, similar as before.
143
+
for(iin1:dim(pGPP)[3]){ # Loop goes through forest layers 1-4 with i
144
+
for(ijin1:modOut$nSites){ # ... and for every layers it goes through different sites ij
145
+
mGPP[ij,,i] <- rep(pGPP[ij,,i],each=12) *mGPPtot[ij,] # Calculate monthly GPP for different layers by multiplying layer's share of GPP with monthly total GPP
146
+
mNPP[ij,,i] <-mGPP[ij,,i] * rep(cueAll[ij,,i],each=12) # Calculate monthly NPP for different layers by multiplying monthly total GPP with layers yearly CUE
147
+
}
148
+
}
149
+
150
+
151
+
###litterfall calculations lit is splitted to August and September
152
+
# Woody litter
153
+
# These 5 variables below are different litter categories, have 3d structure: site,litter variable,layer
154
+
Lf<- aperm(apply(modOut$multiOut[,,26,,1],c(1,3),mLit,months=8:9),c(2,1,3)) # Runs model output variable Litter_fol through the 'mLit' function and transforms the containing array by switching places between rows and columns
155
+
Lfr<- aperm(apply(modOut$multiOut[,,27,,1],c(1,3),mLit,months=8:9),c(2,1,3)) # The mLit function moves litter fall to the August and September time period in the model output
156
+
Lnw<-Lf+Lfr# Non-woody litter = The sum of Foliage litter (var. Litter_fol) and Fine root litter (var. Litter_fr) prebas output variables
157
+
Lfw<- aperm(apply(modOut$multiOut[,,28,,1],c(1,3),mLit,months=8:9),c(2,1,3)) # fine woody litter
### This function is used for adding yearling litter fall to the fall period on a timeseries data set, used in the monthlyFluxes function
257
+
mLit<-function(aLit,months=8:9){
258
+
nYears<- length(aLit) # Gets the length of the array
259
+
nMonths<- length(months)
260
+
lit<-matrix(0,12,nYears) # Creates a matrix by 12 rows, nYears columns and fills it with zeros
261
+
lit[months,] <-matrix(aLit/nMonths,nMonths,nYears,byrow=T) # This appears to create a new matrix within a matrix, on rows 8-9. Is this just used to fill these rows with data?
262
+
lit<- as.vector(lit) # Matrix is converted intoa vector, which removes the table structure and appears to create a timeseries data set
263
+
return(lit)
264
+
}
265
+
266
+
267
+
###Function to calculate basal weighted mean of a PREBAS output
268
+
### modOut = multisite PREBAS output
269
+
### varX = index of the variable for which the basal area weighted mean needs to be calculated
270
+
baWmean<-function(modOut,varX){
271
+
###calculates basal area weighted mean for single site runs
0 commit comments