Skip to content

Commit

Permalink
Update examples, arguments of var_update() and names of dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Hhh-hyc authored Jul 10, 2024
1 parent f1825d2 commit b419a4c
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions models/fates/R/model2netcdf.FATES.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
##' @examples
##' \dontrun{
##' example.output <- system.file("case.clm2.h0.2004-01-01-00000.nc",package="PEcAn.FATES")
##' model2netcdf.FATES(outdir="~/")
##' model2netcdf.FATES(outdir="~/",sitelat, sitelon, start_date, end_date, pfts)
##' }
##'
##' @author Michael Dietze, Shawn Serbin
Expand All @@ -41,7 +41,7 @@ model2netcdf.FATES <- function(outdir, sitelat, sitelon, start_date, end_date, p
c("Qle","Evap","Evap","kgC m-2 s-1","Total evaporation"),
c("QVEGT","Transp","kg m-2 s-1","Canopy transpiration"))

var_update <- function(out,oldname,newname,newunits=NULL,long_name=NULL){
var_update <- function(out,oldname,newname,nc_month,nc_month_names,newunits=NULL,long_name=NULL){
if (oldname %in% nc_month_names) {
## define units of variables
oldunits <- ncdf4::ncatt_get(nc_month,oldname,"units")$value
Expand All @@ -50,14 +50,18 @@ model2netcdf.FATES <- function(outdir, sitelat, sitelon, start_date, end_date, p
if (is.null(newunits)) newunits = oldunits

## check pft dimensions
if (any(grepl('pft',easyNCDF::NcReadDims(nc_month, oldname)))){
d_name <- c()
for (i in (nc_month$var[[oldname]]$dim)){
d_name <- append(d_name, i$name)
}
if (any(grepl('pft',d_name))){
dimension <- xypt # include fates_levpft
}else{
dimension <- xyt
} # only xyt

## transpose dimensions into (,t)
if (easyNCDF::NcReadDims(nc_month, oldname)[length(easyNCDF::NcReadDims(nc_month, oldname))]=='time'){
if (d_name[length(d_name)]=='time'){
dat_0 <- ncdf4::ncvar_get(nc_month,oldname) # time at the tail of dims
dat.new <- PEcAn.utils::misc.convert(dat_0,oldunits,newunits) # convert data units
}
Expand Down Expand Up @@ -112,7 +116,7 @@ model2netcdf.FATES <- function(outdir, sitelat, sitelon, start_date, end_date, p

## write monthly files with start(1,1,i)
for (name_param in matched_var){
out <- var_update(out,name_param[1],name_param[2],name_param[3],name_param[4]) # convert monthly fates output into one variable
out <- var_update(out,name_param[1],name_param[2],name_param[3],name_param[4],nc_month,nc_month_names) # convert monthly fates output into one variable
}
out$var[[length(out$var) + 1]] <- ncdf4::ncvar_def(name="time_bounds", units='',
longname = "history time interval endpoints", dim=list(time_interval,t), prec = "double")
Expand Down

0 comments on commit b419a4c

Please sign in to comment.