Skip to content

Commit

Permalink
Update NSIDC_G02202_sh CMORiser to add bounds for lat,lon and time (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
flicj191 authored Sep 6, 2024
1 parent 45e5217 commit 60938a6
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions esmvaltool/cmorizers/data/formatters/datasets/nsidc_g02202_sh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)


Expand Down Expand Up @@ -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

Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)

0 comments on commit 60938a6

Please sign in to comment.