@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
>... 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)]
@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.
...
nc_var[[1]] <- PEcAn.utils::to_ncvar("NPP", dims) nc_var[[2]] <- PEcAn.utils::to_ncvar("SWE", dims)
...
Originally posted by @Aariq in https://github.com/PecanProject/pecan/issues/3103#issuecomment-1428597383