PecanProject / pecan

The Predictive Ecosystem Analyzer (PEcAn) is an integrated ecological bioinformatics toolbox.
www.pecanproject.org
Other
202 stars 235 forks source link

Refactor model2netcdf.SIPNET variable lists #3117

Closed mdietze closed 1 year ago

mdietze commented 1 year ago

@Aariq suggested the following in the discussion for PR #3103. I agree that its a good idea and also that it should be its own issue.

This is probably a separate PR, but if for some reason you feel like doing some refactoring, a more robust alternative to the way model2netcdf works might be to use named lists. Right now it relies a lot on indexing by position which is easy to screw up by accident when adding or removing variables.

For example, instead of this ...


out <- list()
out[[1]] <- sipnet.output$npp * conversion_factor # NPP
out[[2]] <- sipnet.output$snow * 10 # SWE

...

nc_var[[1]] <- PEcAn.utils::to_ncvar("NPP", dims) nc_var[[2]] <- PEcAn.utils::to_ncvar("SWE", dims)

...


>... do this ...

``` r
out <- list(
  "NPP" = sipnet.output$npp * conversion_factor,
  "SWE" = sipnet.output$snow * 10,
...
)

nc_var <- list(
  "SWE" = PEcAn.utils::to_ncvar("SWE", dims),
  "NPP" = PEcAn.utils::to_ncvar("NPP", dims),
...
)
#order doesn't matter anymore, because you can sort them by name if you need the two lists to be in the same order
nc_var[names(out)]

Originally posted by @Aariq in https://github.com/PecanProject/pecan/issues/3103#issuecomment-1428597383

meetagrawal09 commented 1 year ago

I would like to work on this issue.