|
| 1 | +"""ESMValTool CMORizer for ESACCI-BIOMASS above-ground biomass (agb) data. |
| 2 | +
|
| 3 | +Tier |
| 4 | + Tier 2: other freely-available dataset. |
| 5 | +
|
| 6 | +Source |
| 7 | + ftp://anon-ftp.ceda.ac.uk/neodc/esacci/biomass/data/agb/maps |
| 8 | +
|
| 9 | +Last access |
| 10 | + 20250716 |
| 11 | +
|
| 12 | +Download and processing instructions |
| 13 | + Download 10 km file: |
| 14 | + v6.0/netcd/ESACCI-BIOMASS-L4-AGB-MERGED-10000m-fv6.0.nc |
| 15 | + Put file in ${RAWOBS}/Tier2/ESACCI-BIOMASS |
| 16 | + Or use automatic download script: |
| 17 | + esmvaltool data download ESACCI-BIOMASS |
| 18 | +""" |
| 19 | + |
| 20 | +import datetime |
| 21 | +import glob |
| 22 | +import logging |
| 23 | +import os |
| 24 | +from copy import deepcopy |
| 25 | + |
| 26 | +import iris |
| 27 | +import numpy as np |
| 28 | +from dask import array as da |
| 29 | +from esmvalcore.cmor.table import CMOR_TABLES |
| 30 | +from esmvalcore.preprocessor import extract_time |
| 31 | + |
| 32 | +from esmvaltool.cmorizers.data.utilities import ( |
| 33 | + flip_dim_coord, |
| 34 | + save_variable, |
| 35 | + set_global_atts, |
| 36 | +) |
| 37 | + |
| 38 | +# Configure logging |
| 39 | +logger = logging.getLogger(__name__) |
| 40 | + |
| 41 | + |
| 42 | +def _extract_variable(in_files, var, cfg, out_dir): |
| 43 | + logger.info( |
| 44 | + "CMORizing variable '%s' from input files '%s'", |
| 45 | + var["short_name"], |
| 46 | + ", ".join(in_files), |
| 47 | + ) |
| 48 | + attributes = deepcopy(cfg["attributes"]) |
| 49 | + attributes["mip"] = var["mip"] |
| 50 | + attributes["raw"] = var["raw"] |
| 51 | + attributes["frequency"] = var["frequency"] |
| 52 | + cmor_table = CMOR_TABLES[attributes["project_id"]] |
| 53 | + definition = cmor_table.get_variable(var["mip"], var["short_name"]) |
| 54 | + |
| 55 | + # load all input files (1 year) into 1 cube |
| 56 | + cube_list = iris.load(in_files, var["raw"]) |
| 57 | + |
| 58 | + drop_attrs = ["valid_max", "valid_min"] |
| 59 | + |
| 60 | + for cube in cube_list: |
| 61 | + # set correct names |
| 62 | + cube.var_name = definition.short_name |
| 63 | + cube.standard_name = definition.standard_name |
| 64 | + cube.long_name = definition.long_name |
| 65 | + |
| 66 | + for attr in drop_attrs: |
| 67 | + if attr in cube.attributes: |
| 68 | + cube.attributes.pop(attr) |
| 69 | + |
| 70 | + cube.coord("time").points = ( |
| 71 | + cube.coord("time").core_points().astype("float64") |
| 72 | + ) |
| 73 | + |
| 74 | + # fix units |
| 75 | + cube.convert_units(definition.units) |
| 76 | + |
| 77 | + # set global attributes |
| 78 | + set_global_atts(cube, attributes) |
| 79 | + |
| 80 | + # roll longitude (-180...180 --> 0...360) |
| 81 | + cube.coord("longitude").points = cube.coord("longitude").points + 180.0 |
| 82 | + nlon = len(cube.coord("longitude").points) |
| 83 | + cube.data = da.roll(cube.core_data(), int(nlon / 2), axis=2) |
| 84 | + |
| 85 | + # remove rouding errors introduced by da.roll |
| 86 | + loncoord = cube.coord("longitude") |
| 87 | + latcoord = cube.coord("latitude") |
| 88 | + loncoord.points = np.round(loncoord.core_points(), 3) |
| 89 | + latcoord.points = np.round(latcoord.core_points(), 3) |
| 90 | + |
| 91 | + # flip latitudes |
| 92 | + flip_dim_coord(cube, "latitude") |
| 93 | + |
| 94 | + # fix coordinates |
| 95 | + cube = _fix_coordinates(cube, definition) |
| 96 | + cube.coord("latitude").attributes = None |
| 97 | + cube.coord("longitude").attributes = None |
| 98 | + |
| 99 | + # save each year to a separate output file |
| 100 | + timecoord = cube.coord("time") |
| 101 | + for time in timecoord.units.num2date(timecoord.points): |
| 102 | + # extract current year |
| 103 | + outcube = extract_time(cube, time.year, 1, 1, time.year, 12, 31) |
| 104 | + # adjust time bounds to (year-01-01 00:00, year+1-01-01 00:00) |
| 105 | + out_timecoord = outcube.coord("time") |
| 106 | + start_date = datetime.datetime(time.year, 1, 1) |
| 107 | + end_date = datetime.datetime(time.year + 1, 1, 1) |
| 108 | + out_timecoord.bounds = np.array( |
| 109 | + [ |
| 110 | + out_timecoord.units.date2num(start_date), |
| 111 | + out_timecoord.units.date2num(end_date), |
| 112 | + ] |
| 113 | + ) |
| 114 | + # write output to file |
| 115 | + logger.debug("Saving cube\n%s", outcube) |
| 116 | + logger.debug("Setting time dimension to UNLIMITED while saving!") |
| 117 | + save_variable( |
| 118 | + outcube, |
| 119 | + var["short_name"], |
| 120 | + out_dir, |
| 121 | + attributes, |
| 122 | + unlimited_dimensions=["time"], |
| 123 | + ) |
| 124 | + |
| 125 | + logger.info("Finished CMORizing %s", ", ".join(in_files)) |
| 126 | + |
| 127 | + |
| 128 | +def _fix_coordinates(cube, definition): |
| 129 | + """Fix coordinates.""" |
| 130 | + axis2def = {"T": "time", "X": "longitude", "Y": "latitude"} |
| 131 | + axes = ["T", "X", "Y"] |
| 132 | + |
| 133 | + for axis in axes: |
| 134 | + coord_def = definition.coordinates.get(axis2def[axis]) |
| 135 | + if coord_def: |
| 136 | + coord = cube.coord(axis=axis) |
| 137 | + if axis == "T": |
| 138 | + coord.convert_units("days since 1850-1-1 00:00:00.0") |
| 139 | + coord.points = coord.core_points().astype("float64") |
| 140 | + if coord.bounds is not None: |
| 141 | + coord.bounds = None |
| 142 | + |
| 143 | + if len(coord.points) > 1: |
| 144 | + if coord.bounds is not None: |
| 145 | + coord.bounds = None |
| 146 | + coord.guess_bounds() |
| 147 | + coord.standard_name = coord_def.standard_name |
| 148 | + coord.var_name = coord_def.out_name |
| 149 | + coord.long_name = coord_def.long_name |
| 150 | + |
| 151 | + return cube |
| 152 | + |
| 153 | + |
| 154 | +def cmorization(in_dir, out_dir, cfg, cfg_user, start_date, end_date): |
| 155 | + """Cmorize data.""" |
| 156 | + glob_attrs = cfg["attributes"] |
| 157 | + |
| 158 | + logger.info( |
| 159 | + "Starting cmorization for tier%s OBS files: %s", |
| 160 | + glob_attrs["tier"], |
| 161 | + glob_attrs["dataset_id"], |
| 162 | + ) |
| 163 | + logger.info("Input data from: %s", in_dir) |
| 164 | + logger.info("Output will be written to: %s", out_dir) |
| 165 | + logger.info("CMORizing ESACCI-BIOMASS version %s", glob_attrs["version"]) |
| 166 | + |
| 167 | + for short_name, var in cfg["variables"].items(): |
| 168 | + filepattern = os.path.join(in_dir, var["filename"]) |
| 169 | + in_files = glob.glob(filepattern) |
| 170 | + if "short_name" not in var: |
| 171 | + var["short_name"] = short_name |
| 172 | + if not in_files: |
| 173 | + msg = f"no data not found for variable {short_name}" |
| 174 | + raise ValueError(msg) |
| 175 | + _extract_variable(in_files, var, cfg, out_dir) |
0 commit comments