Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rbeucher committed Jan 29, 2025
1 parent a7ebe03 commit 11cf6f2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ def download_dataset(config, dataset, dataset_info, start_date, end_date,
downloader.download_folder(
base_path.format(year=loop_date.year, month=loop_date.month,
other=suffix), [])
loop_date += relativedelta.relativedelta(months=1)
loop_date += relativedelta.relativedelta(months=1)
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ def download_dataset(config, dataset, dataset_info, start_date, end_date,
downloader.download_folder(
base_path.format(year=loop_date.year, month=loop_date.month,
other=suffix), [])
loop_date += relativedelta.relativedelta(months=1)
loop_date += relativedelta.relativedelta(months=1)
108 changes: 57 additions & 51 deletions esmvaltool/cmorizers/data/formatters/datasets/nsidc_g02202_nh.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def _get_filepaths(in_dir, basename, yyyy):
regex = re.compile(f_name)
return_files = []
for files in os.listdir(in_dir):

if regex.match(files):
return_files.append(os.path.join(in_dir, files))

Expand All @@ -52,16 +51,16 @@ def _get_filepaths(in_dir, basename, yyyy):

def _fix_time_coord(cube, _field, _filename):
"""Set time points to central day of month."""
time_coord = cube.coord('time')
new_unit = Unit('days since 1850-01-01 00:00:00', calendar='standard')
time_coord = cube.coord("time")
new_unit = Unit("days since 1850-01-01 00:00:00", calendar="standard")
time_coord.convert_units(new_unit)
old_time = new_unit.num2date(time_coord.points)
new_time = [d.replace(day=15) for d in old_time]
time_coord.points = new_unit.date2num(new_time)


def _prom_dim_coord(cube, _field, _filename):
iris.util.promote_aux_coord_to_dim_coord(cube, 'time')
iris.util.promote_aux_coord_to_dim_coord(cube, "time")


def _create_coord(cubes, var_name, standard_name):
Expand All @@ -71,7 +70,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", # cube.units,
)
return coord

Expand All @@ -83,98 +82,105 @@ def _extract_variable(raw_var, cmor_info, attrs, filepath, out_dir, latlon):
iris.util.equalise_attributes(cubes)

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')
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')
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.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)

utils.save_variable(cube,
var,
out_dir,
attrs,
unlimited_dimensions=['time'])
utils.save_variable(cube, var, out_dir,
attrs, unlimited_dimensions=["time"])

return cube


def _create_areacello(cfg, in_dir, sample_cube, glob_attrs, out_dir):
if not cfg['custom'].get('create_areacello', False):
if not cfg["custom"].get("create_areacello", False):
return
var_info = cfg['cmor_table'].get_variable('Ofx', 'areacello')
glob_attrs['mip'] = 'Ofx'
lat_coord = sample_cube.coord('latitude')
var_info = cfg["cmor_table"].get_variable("Ofx", "areacello")
glob_attrs["mip"] = "Ofx"
lat_coord = sample_cube.coord("latitude")

area_file = os.path.join(in_dir, cfg['custom']['area_file'])
with open(area_file, 'rb') as datfile:
areasdmnd = np.fromfile(datfile,
dtype=np.int32).reshape(lat_coord.shape)
area_file = os.path.join(in_dir, cfg["custom"]["area_file"])
with open(area_file, "rb") as datfile:
areasdmnd = np.fromfile(
datfile,
dtype=np.int32).reshape(lat_coord.shape)

# Divide by 1000 to get km2 then multiply by 1e6 to m2 ...*1000
ardata = areasdmnd * 1000

cube = iris.cube.Cube(ardata,
standard_name=var_info.standard_name,
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)])
cube = iris.cube.Cube(
ardata,
standard_name=var_info.standard_name,
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)],
)
cube.add_aux_coord(lat_coord, (0, 1))
cube.add_aux_coord(sample_cube.coord('longitude'), (0, 1))
cube.add_aux_coord(sample_cube.coord("longitude"), (0, 1))
utils.fix_var_metadata(cube, var_info)
utils.set_global_atts(cube, glob_attrs)
utils.save_variable(cube, var_info.short_name, out_dir, glob_attrs,
zlib=True)
utils.save_variable(
cube, var_info.short_name, out_dir,
glob_attrs, zlib=True)


def cmorization(in_dir, out_dir, cfg, cfg_user, start_date, end_date):
"""Cmorization func call."""
glob_attrs = cfg['attributes']
cmor_table = cfg['cmor_table']
glob_attrs = cfg["attributes"]
cmor_table = cfg["cmor_table"]

# get aux nc file
cubesaux = iris.load(os.path.join(in_dir, 'G02202-ancillary-psn25-v05r00.nc'))
lat_coord = _create_coord(cubesaux, 'lat', 'latitude')
lon_coord = _create_coord(cubesaux, 'lon', 'longitude')
cubesaux = iris.load(
os.path.join(in_dir, "G02202-ancillary-psn25-v05r00.nc"))
lat_coord = _create_coord(cubesaux, "lat", "latitude")
lon_coord = _create_coord(cubesaux, "lon", "longitude")
year = 1978
# split by year..
sample_cube = None
for year in range(1979, 2025, 1):

filepaths = _get_filepaths(in_dir, cfg['filename'], year)
filepaths = _get_filepaths(in_dir, cfg["filename"], year)

if len(filepaths) > 0:
logger.info("Found %d files in '%s'", len(filepaths), in_dir)

for (var, var_info) in cfg['variables'].items():
for var, var_info in cfg["variables"].items():
logger.info("CMORizing variable '%s'", var)
glob_attrs['mip'] = var_info['mip']
cmor_info = cmor_table.get_variable(var_info['mip'], var)
raw_var = var_info.get('raw', var)
sample_cube = _extract_variable(raw_var, cmor_info,
glob_attrs, filepaths,
out_dir, [lat_coord,
lon_coord])
glob_attrs["mip"] = var_info["mip"]
cmor_info = cmor_table.get_variable(var_info["mip"], var)
raw_var = var_info.get("raw", var)
sample_cube = _extract_variable(
raw_var,
cmor_info,
glob_attrs,
filepaths,
out_dir,
[lat_coord, lon_coord],
)

else:
logger.info("No files found ")
logger.info("year: %d basename: %s", year, cfg['filename'])
logger.info("year: %d basename: %s", year, cfg["filename"])

year += 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ def cmorization(in_dir, out_dir, cfg, cfg_user, start_date, end_date):
cmor_table = cfg['cmor_table']

# get aux nc file
cubesaux = iris.load(os.path.join(in_dir, 'G02202-ancillary-pss25-v05r00.nc'))
cubesaux = iris.load(
os.path.join(in_dir, 'G02202-ancillary-pss25-v05r00.nc'))
lat_coord = _create_coord(cubesaux, 'lat', 'latitude')
lon_coord = _create_coord(cubesaux, 'lon', 'longitude')

Expand Down

0 comments on commit 11cf6f2

Please sign in to comment.