|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import pandas |
| 4 | +import xarray |
| 5 | + |
| 6 | +from cmip_ref_core.constraints import ( |
| 7 | + AddSupplementaryDataset, |
| 8 | + RequireContiguousTimerange, |
| 9 | + RequireFacets, |
| 10 | + RequireOverlappingTimerange, |
| 11 | +) |
| 12 | +from cmip_ref_core.datasets import FacetFilter, SourceDatasetType |
| 13 | +from cmip_ref_core.metrics import DataRequirement |
| 14 | +from cmip_ref_metrics_esmvaltool.metrics.base import ESMValToolMetric |
| 15 | +from cmip_ref_metrics_esmvaltool.recipe import dataframe_to_recipe |
| 16 | +from cmip_ref_metrics_esmvaltool.types import OutputBundle, Recipe |
| 17 | + |
| 18 | + |
| 19 | +class TransientClimateResponseEmissions(ESMValToolMetric): |
| 20 | + """ |
| 21 | + Calculate the global mean Transient Climate Response to Cumulative CO2 Emissions. |
| 22 | + """ |
| 23 | + |
| 24 | + name = "Transient Climate Response to Cumulative CO2 Emissions" |
| 25 | + slug = "esmvaltool-transient-climate-response-emissions" |
| 26 | + base_recipe = "recipe_tcre.yml" |
| 27 | + |
| 28 | + experiments = ( |
| 29 | + "esm-1pctCO2", |
| 30 | + "esm-piControl", |
| 31 | + ) |
| 32 | + variables = ( |
| 33 | + "tas", |
| 34 | + "fco2antt", |
| 35 | + ) |
| 36 | + data_requirements = ( |
| 37 | + DataRequirement( |
| 38 | + source_type=SourceDatasetType.CMIP6, |
| 39 | + filters=( |
| 40 | + FacetFilter( |
| 41 | + facets={ |
| 42 | + "variable_id": variables, |
| 43 | + "frequency": "mon", |
| 44 | + "experiment_id": experiments, |
| 45 | + }, |
| 46 | + ), |
| 47 | + FacetFilter( |
| 48 | + facets={ |
| 49 | + "variable_id": "fco2antt", |
| 50 | + "experiment_id": "esm-piControl", |
| 51 | + }, |
| 52 | + keep=False, |
| 53 | + ), |
| 54 | + ), |
| 55 | + group_by=("source_id", "member_id", "grid_label"), |
| 56 | + constraints=( |
| 57 | + RequireFacets("experiment_id", experiments), |
| 58 | + RequireFacets("variable_id", variables), |
| 59 | + RequireContiguousTimerange(group_by=("instance_id",)), |
| 60 | + RequireOverlappingTimerange(group_by=("instance_id",)), |
| 61 | + AddSupplementaryDataset.from_defaults("areacella", SourceDatasetType.CMIP6), |
| 62 | + ), |
| 63 | + ), |
| 64 | + ) |
| 65 | + |
| 66 | + @staticmethod |
| 67 | + def update_recipe(recipe: Recipe, input_files: pandas.DataFrame) -> None: |
| 68 | + """Update the recipe.""" |
| 69 | + # Prepare updated datasets section in recipe. It contains three |
| 70 | + # datasets, "tas" and "fco2antt" for the "esm-1pctCO2" and just "tas" |
| 71 | + # for the "esm-piControl" experiment. |
| 72 | + recipe_variables = dataframe_to_recipe(input_files) |
| 73 | + tas_esm_1pctCO2 = next( |
| 74 | + ds for ds in recipe_variables["tas"]["additional_datasets"] if ds["exp"] == "esm-1pctCO2" |
| 75 | + ) |
| 76 | + fco2antt_esm_1pctCO2 = next( |
| 77 | + ds for ds in recipe_variables["fco2antt"]["additional_datasets"] if ds["exp"] == "esm-1pctCO2" |
| 78 | + ) |
| 79 | + tas_esm_piControl = next( |
| 80 | + ds for ds in recipe_variables["tas"]["additional_datasets"] if ds["exp"] == "esm-piControl" |
| 81 | + ) |
| 82 | + tas_esm_piControl["timerange"] = tas_esm_1pctCO2["timerange"] |
| 83 | + |
| 84 | + recipe["diagnostics"]["tcre"]["variables"] = { |
| 85 | + "tas_esm-1pctCO2": { |
| 86 | + "short_name": "tas", |
| 87 | + "preprocessor": "global_annual_mean_anomaly", |
| 88 | + "additional_datasets": [tas_esm_1pctCO2], |
| 89 | + }, |
| 90 | + "tas_esm-piControl": { |
| 91 | + "short_name": "tas", |
| 92 | + "preprocessor": "global_annual_mean_anomaly", |
| 93 | + "additional_datasets": [tas_esm_piControl], |
| 94 | + }, |
| 95 | + "fco2antt": { |
| 96 | + "preprocessor": "global_cumulative_sum", |
| 97 | + "additional_datasets": [fco2antt_esm_1pctCO2], |
| 98 | + }, |
| 99 | + } |
| 100 | + recipe["diagnostics"].pop("barplot") |
| 101 | + |
| 102 | + @staticmethod |
| 103 | + def format_result(result_dir: Path) -> OutputBundle: |
| 104 | + """Format the result.""" |
| 105 | + tcre_file = result_dir / "work/tcre/calculate_tcre/tcre.nc" |
| 106 | + tcre = xarray.open_dataset(tcre_file) |
| 107 | + |
| 108 | + source_id = tcre.dataset.values[0].decode("utf-8") |
| 109 | + cmec_output = { |
| 110 | + "DIMENSIONS": { |
| 111 | + "model": {source_id: {}}, |
| 112 | + "region": {"global": {}}, |
| 113 | + "metric": {"tcre": {}}, |
| 114 | + "json_structure": [ |
| 115 | + "model", |
| 116 | + "region", |
| 117 | + "metric", |
| 118 | + ], |
| 119 | + }, |
| 120 | + "RESULTS": { |
| 121 | + source_id: {"global": {"tcre": float(tcre.tcre.values[0])}}, |
| 122 | + }, |
| 123 | + } |
| 124 | + |
| 125 | + return cmec_output |
0 commit comments