-
Notifications
You must be signed in to change notification settings - Fork 313
feat: add ERA5 CDS ↔ CF variable name mapping utility #3922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Akash-paluvai
wants to merge
2
commits into
PecanProject:develop
Choose a base branch
from
Akash-paluvai:GH-3605-era5-cf-varname-map
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| #' ERA5 CDS-to-CF variable name map | ||
| #' | ||
| #' Internal mapping between ERA5 CDS variable names and CF standard names | ||
| #' produced by extract.nc.ERA5(). | ||
| #' | ||
| #' Only variables relevant to the AmeriFlux gap-fill fallback pipeline are | ||
| #' included. Variables not listed here are not handled by this pipeline. | ||
| #' | ||
| #' @noRd | ||
| era5_cds_to_cf_varnames <- c( | ||
| "surface_solar_radiation_downwards" = | ||
| "surface_downwelling_shortwave_flux_in_air", | ||
| "volumetric_soil_water_layer_1" = | ||
| "volume_fraction_of_condensed_water_in_soil" | ||
| ) | ||
|
|
||
| #' Translate ERA5 CDS variable names to CF standard names | ||
| #' | ||
| #' @description | ||
| #' Translate ERA5 CDS variable names to CF standard names. | ||
| #' | ||
| #' @param cds_vars Character vector of ERA5 CDS API variable names. | ||
| #' | ||
| #' @return Named character vector the same length as `cds_vars`. Names are | ||
| #' the input CDS names; values are the corresponding CF standard names as | ||
| #' written by [extract.nc.ERA5()]. Any input name absent from the internal | ||
| #' map produces a [PEcAn.logger::logger.warn()] and an `NA` at that | ||
| #' position. Returns `character(0)` when `cds_vars` is empty. | ||
| #' | ||
| #' @details | ||
| #' Covers only the ERA5 variables used in the AmeriFlux gap-fill fallback | ||
| #' pipeline. Unknown names are warned and returned as `NA` rather than | ||
| #' silently dropped so that callers encounter the failure immediately instead | ||
| #' of producing a silent no-fill condition downstream. | ||
| #' | ||
| #' @seealso [cf_to_cds_varnames()] | ||
| #' @noRd | ||
| cds_to_cf_varnames <- function(cds_vars) { | ||
| if (length(cds_vars) == 0L) { | ||
| return(character(0)) | ||
| } | ||
|
|
||
| result <- era5_cds_to_cf_varnames[cds_vars] | ||
| unknown <- cds_vars[is.na(result)] | ||
|
|
||
| if (length(unknown) > 0L) { | ||
| msg <- paste( | ||
| "cds_to_cf_varnames: no CF mapping for CDS variable(s):", | ||
| paste(unknown, collapse = ", "), | ||
| "returning NA for those entries" | ||
| ) | ||
| warning(msg, call. = FALSE) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. only the PEcAn.logger call is required, it will pass to the appropriate function (warning in this case). and it handles the outer |
||
| PEcAn.logger::logger.warn(msg) | ||
|
|
||
| } | ||
|
|
||
| result | ||
| } | ||
|
|
||
|
|
||
| #' Translate CF standard variable names to ERA5 CDS names | ||
| #' | ||
| #' @param cf_vars Character vector of CF standard variable names. | ||
| #' | ||
| #' @return Named character vector the same length as `cf_vars`. Names are | ||
| #' the input CF names; values are the corresponding CDS API names. Any | ||
| #' input name absent from the internal map produces a | ||
| #' [PEcAn.logger::logger.warn()] and an `NA` at that position. Returns | ||
| #' `character(0)` when `cf_vars` is empty. | ||
| #' | ||
| #' @details | ||
| #' Reverse of [cds_to_cf_varnames()]. Covers only the ERA5 variables used in | ||
| #' the AmeriFlux gap-fill fallback pipeline. Unknown names warn and return | ||
| #' `NA` rather than being silently dropped. | ||
| #' | ||
| #' @seealso [cds_to_cf_varnames()] | ||
| #' @noRd | ||
| cf_to_cds_varnames <- function(cf_vars) { | ||
| if (length(cf_vars) == 0L) { | ||
| return(character(0)) | ||
| } | ||
|
|
||
| # Reverse map built at call time — one source of truth in | ||
| # era5_cds_to_cf_varnames; no second hardcoded vector to keep in sync. | ||
| reverse_map <- stats::setNames( | ||
| names(era5_cds_to_cf_varnames), | ||
| unname(era5_cds_to_cf_varnames) | ||
| ) | ||
|
|
||
| result <- reverse_map[cf_vars] | ||
| unknown <- cf_vars[is.na(result)] | ||
|
|
||
| if (length(unknown) > 0L) { | ||
| msg <- paste( | ||
| "cf_to_cds_varnames: no CDS mapping for CF variable(s):", | ||
| paste(unknown, collapse = ", "), | ||
| "- returning NA for those entries" | ||
| ) | ||
| warning(msg, call. = FALSE) | ||
| PEcAn.logger::logger.warn(msg) | ||
| } | ||
|
|
||
| result | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please let me know if this doesn't make sense, but it seems like it would be simpler and more generally useful to
era5_cdsto https://github.com/PecanProject/pecan/blob/develop/modules/data.atmosphere/R/pecan_standard_met_table.R andthen if it is useful (not clear if it is, but simple enough), developers can create
<from>_to_<to>_varnames. functions as needed.