Skip to content

Commit bd65cf4

Browse files
committed
updated attribute-adding for axis
should be a bit more generalized now
1 parent 59a9466 commit bd65cf4

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

model_catalogs/process.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -320,26 +320,26 @@ def add_attributes(ds, metadata: Optional[dict] = None):
320320
if metadata is not None and "coords" in metadata:
321321
ds = ds.assign_coords({k: ds[k] for k in metadata["coords"]})
322322

323-
# set axis attributes (time, lon, lat, z potentially)
323+
# set axis attributes (T, X, Y, Z potentially)
324324
if metadata is not None and "axis" in metadata:
325325
for ax_name, var_names in metadata["axis"].items():
326-
if not isinstance(var_names, list):
327-
var_names = [var_names]
326+
var_names = mc.astype(var_names, list)
328327
for var_name in var_names:
329-
# var_name needs to exist
330-
# if ax_name == 'X':
331-
# import pdb; pdb.set_trace()
332328

333-
if var_name in ds.dims:
329+
# Check dims, coords, and data_vars:
330+
if var_name in ds.dims or var_name in ds.data_vars.keys() or var_name in ds.coords:
334331
# var_name needs to be a coord to have attributes
335332
if var_name not in ds.coords:
336-
ds[var_name] = (
337-
var_name,
338-
np.arange(ds.sizes[var_name]),
339-
{"axis": ax_name},
340-
)
333+
ds = ds.assign_coords({var_name: (var_name,np.arange(ds[var_name].size), {"axis": ax_name},)})
341334
else:
342335
ds[var_name].attrs["axis"] = ax_name
336+
337+
else:
338+
warnings.warn(
339+
f"The variable {var_name} input in a catalog file is not present in the Dataset.",
340+
UserWarning,
341+
)
342+
343343

344344
# this won't run for e.g. GFS which has multiple time variables
345345
# but also doesn't need to have the calendar updated

0 commit comments

Comments
 (0)