Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues in cubed-sphere regridding #311

Merged
merged 6 commits into from
Apr 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions gcpy/regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def create_regridders(
"Warning: zonal mean comparison must be lat-lon. Defaulting to 1x1.25")
cmpres = '1x1.25'
cmpgridtype = "ll"
elif sg_ref_params != [] or sg_dev_params != []:
elif sg_ref_params != [1, 170, -90] or sg_dev_params != [1, 170, -90]:
# pick ref grid when a stretched-grid and non-stretched-grid
# are passed
cmpres = refres
Expand Down Expand Up @@ -758,6 +758,12 @@ def ravel_checkpoint_lat(ds_out):
})
return ds_out

# Filter non-existent coordinates/dimensions
def rename_existing(ds, rename_dict):
existing_keys = set(ds.coords) | set(ds.dims)
filtered_rename_dict = {key: value for key, value in rename_dict.items() if key in existing_keys}
return ds.rename(filtered_rename_dict)

dim_formats = {
'checkpoint': {
'unravel': [unravel_checkpoint_lat],
Expand All @@ -779,7 +785,8 @@ def ravel_checkpoint_lat(ds_out):
'Ydim': 'Y',
'time': 'T',
},
'transpose': ('time', 'lev', 'nf', 'Xdim', 'Ydim')
# match format of GCHP output
'transpose': ('time', 'lev', 'nf', 'Ydim', 'Xdim')
}
}

Expand All @@ -790,30 +797,21 @@ def ravel_checkpoint_lat(ds_out):
ds = unravel_callback(ds)

# Rename dimensions
ds = ds.rename(dim_formats[format].get('rename', {}))
ds = rename_existing(ds, dim_formats[format].get('rename', {}))
return ds


# %%%% Renaming from the common format %%%%
# Reverse rename
ds = ds.rename(
ds = rename_existing(ds,
{v: k for k, v in dim_formats[format].get('rename', {}).items()})

# Ravel dimensions
for ravel_callback in dim_formats[format].get('ravel', []):
ds = ravel_callback(ds)

# Transpose
if len(ds.dims) == 5 or (len(ds.dims) == 4 and 'lev' in list(
ds.dims) and 'time' in list(ds.dims)):
# full dim dataset
ds = ds.transpose(*dim_formats[format].get('transpose', []))
elif len(ds.dims) == 4:
# single time
ds = ds.transpose(*dim_formats[format].get('transpose', [])[1:])
elif len(ds.dims) == 3:
# single level / time
ds = ds.transpose(*dim_formats[format].get('transpose', [])[2:])
ds = ds.transpose(*[x for x in dim_formats[format].get('transpose', []) if x in list(ds.dims)])
return ds


Expand Down