Skip to content

Commit

Permalink
Merge pull request #3442 from harshagr70/GH-2758-fix-tidyverse
Browse files Browse the repository at this point in the history
Fix : Linkages for no visible bindings warnings
  • Loading branch information
infotroph authored Feb 20, 2025
2 parents 284bd62 + 31f5229 commit 9832559
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 55 deletions.
36 changes: 19 additions & 17 deletions models/linkages/R/model2netcdf.LINKAGES.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
model2netcdf.LINKAGES <- function(outdir, sitelat, sitelon, start_date = NULL,
end_date = NULL, pft_names = NULL) {
# , PFTs) { logger.severe('NOT IMPLEMENTED')


## Create a new enviroment
linkages_env <- new.env()
### Read in model output in linkages format
load(file.path(outdir, "linkages.out.Rdata"))
load(file.path(outdir, "linkages.out.Rdata"),envir = linkages_env)
# linkages.output.dims <- dim(linkages.output)

### Determine number of years and output timestep
Expand Down Expand Up @@ -47,21 +49,21 @@ model2netcdf.LINKAGES <- function(outdir, sitelat, sitelon, start_date = NULL,
## Setup outputs for netCDF file in appropriate units
output <- list()
## standard variables: Carbon Pools
output[[1]] <- ag.biomass[y, ] # Above Ground Biomass in kgC/m2
output[[2]] <- ag.biomass[y, ] # Total Live Biomass in kgC/m2 (no distinction from AGB in linkages)
output[[3]] <- total.soil.carbon[y, ] # TotSoilCarb in kgC/m2
output[[4]] <- c(ag.biomass[y, ], total.soil.carbon[y, ], leaf.litter[y, ], area[y, ]) # Carb Pools in kgC/m2
output[[1]] <- linkages_env$ag.biomass[y, ] # Above Ground Biomass in kgC/m2
output[[2]] <- linkages_env$ag.biomass[y, ] # Total Live Biomass in kgC/m2 (no distinction from AGB in linkages)
output[[3]] <- linkages_env$total.soil.carbon[y, ] # TotSoilCarb in kgC/m2
output[[4]] <- c(linkages_env$ag.biomass[y, ], linkages_env$total.soil.carbon[y, ], linkages_env$leaf.litter[y, ], linkages_env$area[y, ]) # Carb Pools in kgC/m2
output[[5]] <- c("AGB", "Soil Organic Matter", "Leaf Litter", "LAI") # poolname
output[[6]] <- ag.npp[y, ] # GWBI = NPP in linkages
output[[7]] <- hetero.resp[y, ] # HeteroResp in kgC/m^2/s
output[[8]] <- ag.npp[y, ] # NPP = GWBI in linkages
output[[9]] <- nee[y, ] # NEE #possibly questionable
output[[10]] <- et[y, ] # Evap in kg/m^2/s
output[[11]] <- agb.pft[, y, ]
output[[12]] <- f.comp[, y]
output[[13]] <- area[y, ] #LAI
output[[14]] <- water[y, ] #soil moisture
output[[15]] <- abvgroundwood.biomass[y,] #AbvGroundWood just wood no leaves
output[[6]] <- linkages_env$ag.npp[y, ] # GWBI = NPP in linkages
output[[7]] <- linkages_env$hetero.resp[y, ] # HeteroResp in kgC/m^2/s
output[[8]] <- linkages_env$ag.npp[y, ] # NPP = GWBI in linkages
output[[9]] <- linkages_env$nee[y, ] # NEE #possibly questionable
output[[10]] <- linkages_env$et[y, ] # Evap in kg/m^2/s
output[[11]] <- linkages_env$agb.pft[, y, ]
output[[12]] <- linkages_env$f.comp[, y]
output[[13]] <- linkages_env$area[y, ] #LAI
output[[14]] <- linkages_env$water[y, ] #soil moisture
output[[15]] <- linkages_env$abvgroundwood.biomass[y,] #AbvGroundWood just wood no leaves
output[[16]] <- seq_along(pft_names)

# ******************** Declare netCDF variables ********************#
Expand All @@ -75,7 +77,7 @@ model2netcdf.LINKAGES <- function(outdir, sitelat, sitelon, start_date = NULL,
dim.cpools <- ncdf4::ncdim_def("cpools", "", vals = 1:4, longname = "Carbon Pools")
dim.cpools1 <- ncdf4::ncdim_def("cpools", "", vals = 1:4, longname = "Carbon Pools", create_dimvar = FALSE)
#dim.pfts <- ncdim_def("pfts", "", vals = 1:nrow(agb.pft), longname = "PFTs", create_dimvar = FALSE)
dim.pfts <- ncdf4::ncdim_def(name = "pft", units = "unitless", vals = 1:length(agb.pft[, 1, 1]), longname = "Plant Functional Type", unlim = TRUE)
dim.pfts <- ncdf4::ncdim_def(name = "pft", units = "unitless", vals = 1:length(linkages_env$agb.pft[, 1, 1]), longname = "Plant Functional Type", unlim = TRUE)


for (i in seq_along(output)) {
Expand Down
19 changes: 12 additions & 7 deletions models/linkages/R/write_restart.LINKAGES.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,21 @@ write_restart.LINKAGES <- function(outdir, runid, start.time, stop.time,
}
print(paste0("runid = ", runid))

# Create a new enviroment
linkages_env <- new.env()
# load output
load(outfile)
load(outfile, envir = linkages_env)

ntrees <- ntrees.kill[, ncol(ntrees.kill), 1] # number of trees
ntrees <- linkages_env$ntrees.kill[, ncol(linkages_env$ntrees.kill), 1] # number of trees

if(sum(ntrees)==0) {
#reloads spin up if theres nothing in the output file
print('No survivors. Reusing spinup.')
load(file.path(outdir, runid,list.files(file.path(outdir, runid))[grep(list.files(file.path(outdir, runid)),pattern='linkages')][1]))
ntrees <- ntrees.kill[, ncol(ntrees.kill), 1] # number of trees
# new enviroment for spin up data
linkages_env_spinup <- new.env()
spinup_file <- file.path(outdir, runid,list.files(file.path(outdir, runid))[grep(list.files(file.path(outdir, runid)),pattern='linkages')][1])
load(spinup_file, envir = linkages_env_spinup)
ntrees <- linkages_env_spinup$ntrees.kill[, ncol(linkages_env_spinup$ntrees.kill), 1] # number of trees

}

Expand All @@ -152,11 +157,11 @@ write_restart.LINKAGES <- function(outdir, runid, start.time, stop.time,
tyl <- tyl
C.mat <- C.mat

nogro <- as.vector(nogro.save[, ncol(nogro.save), 1]) ## no growth indicator
nogro <- as.vector(linkages_env$nogro.save[, ncol(linkages_env$nogro.save), 1]) ## no growth indicator
ksprt <- matrix(0, 1, nspec) ## kill sprout indicator ## LOOK INTO THIS
iage <- as.vector(iage.save[, ncol(iage.save), 1]) # individual age
iage <- as.vector(linkages_env$iage.save[, ncol(linkages_env$iage.save), 1]) # individual age

dbh <- as.vector(dbh.save[, ncol(dbh.save), 1])
dbh <- as.vector(linkages_env$dbh.save[, ncol(linkages_env$dbh.save), 1])

n.index <- c(rep(1, ntrees[1]))
for (i in 2:length(settings$pfts)) {
Expand Down
32 changes: 1 addition & 31 deletions models/linkages/tests/Rcheck_reference.log
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,7 @@ Suggests or Enhances not in mainstream repositories:
* checking S3 generic/method consistency ... OK
* checking replacement functions ... OK
* checking foreign function calls ... OK
* checking R code for possible problems ... NOTE
model2netcdf.LINKAGES: no visible binding for global variable
‘ag.biomass’
model2netcdf.LINKAGES: no visible binding for global variable
‘total.soil.carbon’
model2netcdf.LINKAGES: no visible binding for global variable
‘leaf.litter’
model2netcdf.LINKAGES: no visible binding for global variable ‘area’
model2netcdf.LINKAGES: no visible binding for global variable ‘ag.npp’
model2netcdf.LINKAGES: no visible binding for global variable
‘hetero.resp’
model2netcdf.LINKAGES: no visible binding for global variable ‘nee’
model2netcdf.LINKAGES: no visible binding for global variable ‘et’
model2netcdf.LINKAGES: no visible binding for global variable ‘agb.pft’
model2netcdf.LINKAGES: no visible binding for global variable ‘f.comp’
model2netcdf.LINKAGES: no visible binding for global variable ‘water’
model2netcdf.LINKAGES: no visible binding for global variable
‘abvgroundwood.biomass’
write_restart.LINKAGES: no visible binding for global variable
‘ntrees.kill’
write_restart.LINKAGES: no visible binding for global variable
‘nogro.save’
write_restart.LINKAGES: no visible binding for global variable
‘iage.save’
write_restart.LINKAGES: no visible binding for global variable
‘dbh.save’
Undefined global functions or variables:
abvgroundwood.biomass ag.biomass ag.npp agb.pft area
db.close db.open db.query dbh.save et f.comp
hetero.resp iage.save leaf.litter nee nogro.save
ntrees.kill total.soil.carbon water
* checking R code for possible problems ... OK
* checking Rd files ... OK
* checking Rd metadata ... OK
* checking Rd line widths ... OK
Expand Down

0 comments on commit 9832559

Please sign in to comment.