-
Notifications
You must be signed in to change notification settings - Fork 12
Add TCRE metric #208
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
Merged
Merged
Add TCRE metric #208
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Added Transient Climate Response to Cumulative CO2 Emissions (TCRE) metric to the ESMValTool metrics package. |
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
125 changes: 125 additions & 0 deletions
125
packages/ref-metrics-esmvaltool/src/cmip_ref_metrics_esmvaltool/metrics/tcre.py
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,125 @@ | ||
| from pathlib import Path | ||
|
|
||
| import pandas | ||
| import xarray | ||
|
|
||
| from cmip_ref_core.constraints import ( | ||
| AddSupplementaryDataset, | ||
| RequireContiguousTimerange, | ||
| RequireFacets, | ||
| RequireOverlappingTimerange, | ||
| ) | ||
| from cmip_ref_core.datasets import FacetFilter, SourceDatasetType | ||
| from cmip_ref_core.metrics import DataRequirement | ||
| from cmip_ref_metrics_esmvaltool.metrics.base import ESMValToolMetric | ||
| from cmip_ref_metrics_esmvaltool.recipe import dataframe_to_recipe | ||
| from cmip_ref_metrics_esmvaltool.types import OutputBundle, Recipe | ||
|
|
||
|
|
||
| class TransientClimateResponseEmissions(ESMValToolMetric): | ||
| """ | ||
| Calculate the global mean Transient Climate Response to Cumulative CO2 Emissions. | ||
| """ | ||
|
|
||
| name = "Transient Climate Response to Cumulative CO2 Emissions" | ||
| slug = "esmvaltool-transient-climate-response-emissions" | ||
| base_recipe = "recipe_tcre.yml" | ||
|
|
||
| experiments = ( | ||
| "esm-1pctCO2", | ||
| "esm-piControl", | ||
| ) | ||
| variables = ( | ||
| "tas", | ||
| "fco2antt", | ||
| ) | ||
| data_requirements = ( | ||
| DataRequirement( | ||
| source_type=SourceDatasetType.CMIP6, | ||
| filters=( | ||
| FacetFilter( | ||
| facets={ | ||
| "variable_id": variables, | ||
| "frequency": "mon", | ||
| "experiment_id": experiments, | ||
| }, | ||
| ), | ||
| FacetFilter( | ||
| facets={ | ||
| "variable_id": "fco2antt", | ||
| "experiment_id": "esm-piControl", | ||
| }, | ||
| keep=False, | ||
| ), | ||
| ), | ||
| group_by=("source_id", "member_id", "grid_label"), | ||
| constraints=( | ||
| RequireFacets("experiment_id", experiments), | ||
| RequireFacets("variable_id", variables), | ||
| RequireContiguousTimerange(group_by=("instance_id",)), | ||
| RequireOverlappingTimerange(group_by=("instance_id",)), | ||
| AddSupplementaryDataset.from_defaults("areacella", SourceDatasetType.CMIP6), | ||
| ), | ||
| ), | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def update_recipe(recipe: Recipe, input_files: pandas.DataFrame) -> None: | ||
| """Update the recipe.""" | ||
| # Prepare updated datasets section in recipe. It contains three | ||
| # datasets, "tas" and "fco2antt" for the "esm-1pctCO2" and just "tas" | ||
| # for the "esm-piControl" experiment. | ||
| recipe_variables = dataframe_to_recipe(input_files) | ||
| tas_esm_1pctCO2 = next( | ||
| ds for ds in recipe_variables["tas"]["additional_datasets"] if ds["exp"] == "esm-1pctCO2" | ||
| ) | ||
| fco2antt_esm_1pctCO2 = next( | ||
| ds for ds in recipe_variables["fco2antt"]["additional_datasets"] if ds["exp"] == "esm-1pctCO2" | ||
| ) | ||
| tas_esm_piControl = next( | ||
| ds for ds in recipe_variables["tas"]["additional_datasets"] if ds["exp"] == "esm-piControl" | ||
| ) | ||
| tas_esm_piControl["timerange"] = tas_esm_1pctCO2["timerange"] | ||
|
|
||
| recipe["diagnostics"]["tcre"]["variables"] = { | ||
| "tas_esm-1pctCO2": { | ||
| "short_name": "tas", | ||
| "preprocessor": "global_annual_mean_anomaly", | ||
| "additional_datasets": [tas_esm_1pctCO2], | ||
| }, | ||
| "tas_esm-piControl": { | ||
| "short_name": "tas", | ||
| "preprocessor": "global_annual_mean_anomaly", | ||
| "additional_datasets": [tas_esm_piControl], | ||
| }, | ||
| "fco2antt": { | ||
| "preprocessor": "global_cumulative_sum", | ||
| "additional_datasets": [fco2antt_esm_1pctCO2], | ||
| }, | ||
| } | ||
| recipe["diagnostics"].pop("barplot") | ||
|
|
||
| @staticmethod | ||
| def format_result(result_dir: Path) -> OutputBundle: | ||
| """Format the result.""" | ||
| tcre_file = result_dir / "work/tcre/calculate_tcre/tcre.nc" | ||
| tcre = xarray.open_dataset(tcre_file) | ||
|
|
||
| source_id = tcre.dataset.values[0].decode("utf-8") | ||
| cmec_output = { | ||
| "DIMENSIONS": { | ||
| "model": {source_id: {}}, | ||
| "region": {"global": {}}, | ||
| "metric": {"tcre": {}}, | ||
| "json_structure": [ | ||
| "model", | ||
| "region", | ||
| "metric", | ||
| ], | ||
| }, | ||
| "RESULTS": { | ||
| source_id: {"global": {"tcre": float(tcre.tcre.values[0])}}, | ||
| }, | ||
| } | ||
|
|
||
| return cmec_output | ||
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
1 change: 1 addition & 0 deletions
1
packages/ref-metrics-esmvaltool/src/cmip_ref_metrics_esmvaltool/recipes.txt
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| examples/recipe_python.yml ab3f06d269bb2c1368f4dc39da9bcb232fb2adb1fa556ba769e6c16294ffb4a3 | ||
| recipe_ecs.yml 0cc57034fcb64e32015b4ff949ece5df8cdb8c6f493618b50ceded119fb37918 | ||
| recipe_tcr.yml 35f9ef035a4e71aff5cac5dd26c49da2162fc00291bf3b0bd16b661b7b2f606b | ||
| recipe_tcre.yml 4668e357e00c515a8264ac75cb319ce558289689e10189e6f9e982886c414c94 |
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.
We now have concrete types
cmip_ref_core.pycmec.output.CMECOutputandcmip_ref_core.pycmec.output.CMECMetricinstead of this loose type hint.The content here is also representative of a metrics bundle, not an output bundle.
The output bundle contains references to plots and datafiles, where as the metric bundle contains the calculated scalars. The output bundle content is not currently implemented for ESMValTool (see
packages/ref-metrics-pmp/src/cmip_ref_metrics_pmp/pmp_driver.pyfor an example of including data and plots in an output bundle).I'd recommend fixing this up in a follow-up PR as is will impact all of the ESMValTool metrics