|
| 1 | +"""ESMValTool CMORizer for WAD2M data. |
| 2 | +
|
| 3 | +Tier |
| 4 | + Tier 2: other freely-available dataset. |
| 5 | +
|
| 6 | +Source |
| 7 | + https://zenodo.org/record/5553187 |
| 8 | +
|
| 9 | +Last access |
| 10 | + 20250701 |
| 11 | +
|
| 12 | +Download and processing instructions |
| 13 | + Download the file WAD2M_wetlands_2000-2020_025deg_Ver2.0.nc. |
| 14 | +
|
| 15 | +""" |
| 16 | + |
| 17 | +import logging |
| 18 | +import warnings |
| 19 | +import zipfile |
| 20 | +from datetime import datetime |
| 21 | +from pathlib import Path |
| 22 | + |
| 23 | +import iris |
| 24 | +from cf_units import Unit |
| 25 | +from iris import NameConstraint |
| 26 | +from iris.coords import AuxCoord |
| 27 | + |
| 28 | +from esmvaltool.cmorizers.data import utilities as utils |
| 29 | +from esmvaltool.diag_scripts.shared.iris_helpers import unify_time_coord |
| 30 | + |
| 31 | +logger = logging.getLogger(__name__) |
| 32 | + |
| 33 | + |
| 34 | +def _fix_coords(cube, cmor_info): |
| 35 | + """Fix coordinates.""" |
| 36 | + # Dimensional coordinates |
| 37 | + if "time" in cmor_info.dimensions: |
| 38 | + unify_time_coord(cube, "days since 1950-01-01 00:00:00") |
| 39 | + |
| 40 | + # Move time points to center of month |
| 41 | + time_coord = cube.coord("time") |
| 42 | + old_dates = time_coord.units.num2date(time_coord.points) |
| 43 | + new_dates = [datetime(t.year, t.month, 15) for t in old_dates] |
| 44 | + time_coord.points = time_coord.units.date2num(new_dates) |
| 45 | + |
| 46 | + # Add typewetla coordinate |
| 47 | + typewetla_coord = AuxCoord( |
| 48 | + "wetland", |
| 49 | + var_name="typewetla", |
| 50 | + standard_name="area_type", |
| 51 | + long_name="Wetland", |
| 52 | + units=Unit("no unit"), |
| 53 | + ) |
| 54 | + cube.add_aux_coord(typewetla_coord, ()) |
| 55 | + |
| 56 | + cube = utils.fix_coords(cube) |
| 57 | + |
| 58 | + return cube |
| 59 | + |
| 60 | + |
| 61 | +def _fix_var_metadata(var_info, cmor_info, cube): |
| 62 | + """Fix variable metadata.""" |
| 63 | + if "raw_units" in var_info: |
| 64 | + cube.units = var_info["raw_units"] |
| 65 | + |
| 66 | + # wetlandFrac: |
| 67 | + # Convert from fraction to percentage |
| 68 | + cube.convert_units("%") |
| 69 | + |
| 70 | + utils.fix_var_metadata(cube, cmor_info) |
| 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 | + # Load data |
| 79 | + with warnings.catch_warnings(): |
| 80 | + warnings.filterwarnings( |
| 81 | + action="ignore", |
| 82 | + message="Ignoring netCDF variable .* invalid units .*", |
| 83 | + category=UserWarning, |
| 84 | + module="iris", |
| 85 | + ) |
| 86 | + cube = iris.load_cube(filepath, NameConstraint(var_name=raw_var)) |
| 87 | + |
| 88 | + # Fix variable metadata |
| 89 | + _fix_var_metadata(var_info, cmor_info, cube) |
| 90 | + |
| 91 | + # Fix coordinates |
| 92 | + cube = _fix_coords(cube, cmor_info) |
| 93 | + |
| 94 | + # Fix global metadata |
| 95 | + utils.set_global_atts(cube, attrs) |
| 96 | + |
| 97 | + # Save variable |
| 98 | + utils.save_variable( |
| 99 | + cube, |
| 100 | + var, |
| 101 | + out_dir, |
| 102 | + attrs, |
| 103 | + unlimited_dimensions=["time"], |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +def _unzip(filepath, out_dir): |
| 108 | + """Unzip `*.zip` file.""" |
| 109 | + logger.info("Starting extraction of %s to %s", filepath, out_dir) |
| 110 | + with zipfile.ZipFile(filepath, "r") as zip_ref: |
| 111 | + zip_ref.extract(Path(filepath).stem, out_dir) |
| 112 | + extracted_file = Path(out_dir) / Path(filepath).stem |
| 113 | + logger.info("Succefully extracted file to %s", extracted_file) |
| 114 | + return extracted_file |
| 115 | + |
| 116 | + |
| 117 | +def cmorization(in_dir, out_dir, cfg, cfg_user, start_date, end_date): |
| 118 | + """Cmorization func call.""" |
| 119 | + cmor_table = cfg["cmor_table"] |
| 120 | + glob_attrs = cfg["attributes"] |
| 121 | + |
| 122 | + # Run the cmorization |
| 123 | + for var, var_info in cfg["variables"].items(): |
| 124 | + logger.info("CMORizing variable '%s'", var) |
| 125 | + glob_attrs["comment"] = var_info.get("comment", "") |
| 126 | + glob_attrs["mip"] = var_info["mip"] |
| 127 | + cmor_info = cmor_table.get_variable(var_info["mip"], var) |
| 128 | + |
| 129 | + # Extract file from ZIP archive |
| 130 | + zip_file = Path(in_dir) / cfg["filename"] |
| 131 | + if not Path(zip_file).is_file(): |
| 132 | + logger.debug("Skipping '%s', file '%s' not found", var, zip_file) |
| 133 | + continue |
| 134 | + logger.info("Found input file '%s'", zip_file) |
| 135 | + filepath = _unzip(zip_file, out_dir) |
| 136 | + |
| 137 | + # Extract and save variable file |
| 138 | + _extract_variable(var_info, cmor_info, glob_attrs, filepath, out_dir) |
| 139 | + |
| 140 | + # Remove extracted file |
| 141 | + Path(filepath).unlink() |
| 142 | + logger.info("Removed cached input file %s", filepath) |
0 commit comments