From 60938a68685420edc0c25ce9bc985dbef3252527 Mon Sep 17 00:00:00 2001 From: Felicity Chun <32269066+flicj191@users.noreply.github.com> Date: Sat, 7 Sep 2024 05:13:53 +1000 Subject: [PATCH] Update NSIDC_G02202_sh CMORiser to add bounds for lat,lon and time (#3744) --- .../formatters/datasets/nsidc_g02202_sh.py | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/esmvaltool/cmorizers/data/formatters/datasets/nsidc_g02202_sh.py b/esmvaltool/cmorizers/data/formatters/datasets/nsidc_g02202_sh.py index c206f817cb..202e370043 100644 --- a/esmvaltool/cmorizers/data/formatters/datasets/nsidc_g02202_sh.py +++ b/esmvaltool/cmorizers/data/formatters/datasets/nsidc_g02202_sh.py @@ -27,13 +27,15 @@ import re import numpy as np - import iris from cf_units import Unit from iris.coords import AuxCoord +from esmvalcore.cmor._fixes.common import OceanFixGrid +from esmvalcore.cmor.fixes import get_time_bounds from esmvaltool.cmorizers.data import utilities as utils + logger = logging.getLogger(__name__) @@ -71,7 +73,7 @@ def _create_coord(cubes, var_name, standard_name): standard_name=standard_name, long_name=cube.long_name, var_name=var_name, - units='degrees' # cube.units, + units='degrees' ) return coord @@ -85,24 +87,27 @@ def _extract_variable(raw_var, cmor_info, attrs, filepath, out_dir, latlon): cube = cubes.concatenate_cube() iris.util.promote_aux_coord_to_dim_coord(cube, 'projection_y_coordinate') iris.util.promote_aux_coord_to_dim_coord(cube, 'projection_x_coordinate') - cube.coord('projection_y_coordinate').rename('y') - cube.coord('projection_x_coordinate').rename('x') cube.add_aux_coord(latlon[0], (1, 2)) cube.add_aux_coord(latlon[1], (1, 2)) + # add coord typesi area_type = AuxCoord([1.0], standard_name='area_type', var_name='type', long_name='Sea Ice area type') cube.add_aux_coord(area_type) - # cube.convert_units(cmor_info.units) cube.units = '%' cube.data[cube.data > 100] = np.nan cube = cube * 100 - # utils.fix_coords(cube) #latlon multidimensional utils.fix_var_metadata(cube, cmor_info) utils.set_global_atts(cube, attrs) + # latlon are multidimensional, create bounds + siconc = OceanFixGrid(cmor_info) + cube = siconc.fix_metadata(cubes=[cube])[0] + # time bounds + cube.coord('time').bounds = get_time_bounds(cube.coord('time'), + cmor_info.frequency) utils.save_variable(cube, var, @@ -133,8 +138,9 @@ def _create_areacello(cfg, in_dir, sample_cube, glob_attrs, out_dir): long_name=var_info.long_name, var_name=var_info.short_name, units='m2', - dim_coords_and_dims=[(sample_cube.coord('y'), 0), - (sample_cube.coord('x'), 1)]) + # time is index 0, add cell index dim + dim_coords_and_dims=[(sample_cube.coords()[1], 0), + (sample_cube.coords()[2], 1)]) cube.add_aux_coord(lat_coord, (0, 1)) cube.add_aux_coord(sample_cube.coord('longitude'), (0, 1)) utils.fix_var_metadata(cube, var_info) @@ -152,15 +158,17 @@ def cmorization(in_dir, out_dir, cfg, cfg_user, start_date, end_date): cubesaux = iris.load(os.path.join(in_dir, 'G02202-cdr-ancillary-sh.nc')) lat_coord = _create_coord(cubesaux, 'lat', 'latitude') lon_coord = _create_coord(cubesaux, 'lon', 'longitude') + year = 1978 # split by year.. sample_cube = None - while year <= 2022: + for year in range(1979, 2022, 1): filepaths = _get_filepaths(in_dir, cfg['filename'], year) if len(filepaths) > 0: - logger.info("Found %d files in '%s'", len(filepaths), in_dir) + logger.info("Year %d: Found %d files in '%s'", + year, len(filepaths), in_dir) for (var, var_info) in cfg['variables'].items(): logger.info("CMORizing variable '%s'", var) @@ -173,10 +181,8 @@ def cmorization(in_dir, out_dir, cfg, cfg_user, start_date, end_date): lon_coord]) else: - logger.info("No files found ") - logger.info("year: %d basename: %s", year, cfg['filename']) - - year += 1 + logger.info("No files found year: %d basename: %s", + year, cfg['filename']) - if sample_cube is not None: - _create_areacello(cfg, in_dir, sample_cube, glob_attrs, out_dir) + if sample_cube is not None: + _create_areacello(cfg, in_dir, sample_cube, glob_attrs, out_dir)