|
| 1 | +"""CMORize Yang2020 data.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from datetime import datetime |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import iris |
| 8 | +from cf_units import Unit |
| 9 | +from iris import NameConstraint |
| 10 | +from iris.coords import CellMethod, DimCoord |
| 11 | + |
| 12 | +from esmvaltool.cmorizers.data import utilities as utils |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +TIME_UNITS = Unit("days since 1950-01-01 00:00:00", calendar="standard") |
| 18 | + |
| 19 | + |
| 20 | +def _fix_climatological_time(cube): |
| 21 | + """Fix climatology coordinate.""" |
| 22 | + # Time bounds of the climatology are set to 1971-2018 since measurements |
| 23 | + # that serve as input for this data product have been conducted between |
| 24 | + # 1971 and 2018 (note that the wind products from ERA5 and CCMP, which are |
| 25 | + # necessary to derive the N2O flux, are only available from 1988-2017 |
| 26 | + # though). This has been recommended by a co-author of the original |
| 27 | + # publication (https://doi.org/10.1073/pnas.1921914117) via email, but is |
| 28 | + # NOT explicitly stated in the original publication. Thus, the period given |
| 29 | + # here needs to be treated with care. |
| 30 | + time_points = TIME_UNITS.date2num( |
| 31 | + [datetime(1994, m, 15) for m in range(1, 13)] |
| 32 | + ) |
| 33 | + time_bounds = [ |
| 34 | + [datetime(1971, m, 1), datetime(2018, m + 1, 1)] for m in range(1, 12) |
| 35 | + ] |
| 36 | + time_bounds.append([datetime(1971, 12, 1), datetime(2019, 1, 1)]) |
| 37 | + time_bounds = TIME_UNITS.date2num(time_bounds) |
| 38 | + |
| 39 | + # Add new time coordinate to cube |
| 40 | + time_coord = DimCoord( |
| 41 | + time_points, |
| 42 | + bounds=time_bounds, |
| 43 | + standard_name="time", |
| 44 | + long_name="time", |
| 45 | + var_name="time", |
| 46 | + units=TIME_UNITS, |
| 47 | + climatological=True, |
| 48 | + ) |
| 49 | + cube.remove_coord("time") |
| 50 | + cube.add_dim_coord(time_coord, 0) |
| 51 | + |
| 52 | + # Fix cell methods |
| 53 | + cube.add_cell_method(CellMethod("mean within years", coords=time_coord)) |
| 54 | + cube.add_cell_method(CellMethod("mean over years", coords=time_coord)) |
| 55 | + |
| 56 | + |
| 57 | +def _fix_var_metadata(var_info, cmor_info, cube): |
| 58 | + """Fix variable metadata.""" |
| 59 | + if "raw_units" in var_info: |
| 60 | + cube.units = var_info["raw_units"] |
| 61 | + if ( |
| 62 | + "g" in str(cube.units) |
| 63 | + and "mol" in cmor_info.units |
| 64 | + and "molar_mass" in var_info |
| 65 | + ): |
| 66 | + cube = cube / var_info["molar_mass"] |
| 67 | + cube.units = cube.units / "g mol-1" |
| 68 | + cube.convert_units(cmor_info.units) |
| 69 | + utils.fix_var_metadata(cube, cmor_info) |
| 70 | + return cube |
| 71 | + |
| 72 | + |
| 73 | +def _extract_variable(var_info, cmor_info, attrs, filepath, out_dir): |
| 74 | + """Extract variable.""" |
| 75 | + var = cmor_info.short_name |
| 76 | + raw_var = var_info.get("raw_name", var) |
| 77 | + |
| 78 | + cube = iris.load_cube(filepath, NameConstraint(var_name=raw_var)) |
| 79 | + |
| 80 | + # Fix variable metadata |
| 81 | + cube = _fix_var_metadata(var_info, cmor_info, cube) |
| 82 | + |
| 83 | + # Fix coordinates |
| 84 | + if "time" in cmor_info.coordinates: |
| 85 | + _fix_climatological_time(cube) |
| 86 | + cube = utils.fix_coords(cube, overwrite_time_bounds=False) |
| 87 | + |
| 88 | + # Fix global metadata |
| 89 | + utils.set_global_atts(cube, attrs) |
| 90 | + if cmor_info.positive: |
| 91 | + cube.attributes.locals["positive"] = cmor_info.positive |
| 92 | + |
| 93 | + # Save variable |
| 94 | + utils.save_variable( |
| 95 | + cube, |
| 96 | + var, |
| 97 | + out_dir, |
| 98 | + attrs, |
| 99 | + local_keys=["comment", "positive"], |
| 100 | + unlimited_dimensions=["time"], |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +def cmorization(in_dir, out_dir, cfg, cfg_user, start_date, end_date): |
| 105 | + """Cmorization func call.""" |
| 106 | + cmor_table = cfg["cmor_table"] |
| 107 | + glob_attrs = cfg["attributes"] |
| 108 | + |
| 109 | + # Run the cmorization |
| 110 | + for var, var_info in cfg["variables"].items(): |
| 111 | + filepath = Path(in_dir) / var_info["filename"] |
| 112 | + logger.info("CMORizing variable '%s' from file %s", var, filepath) |
| 113 | + glob_attrs["mip"] = var_info["mip"] |
| 114 | + if "comment" in var_info: |
| 115 | + glob_attrs["comment"] = var_info["comment"] |
| 116 | + cmor_info = cmor_table.get_variable(var_info["mip"], var) |
| 117 | + _extract_variable(var_info, cmor_info, glob_attrs, filepath, out_dir) |
0 commit comments