Skip to content

Commit 8556b19

Browse files
committed
more cleanup
1 parent f5a0a2f commit 8556b19

File tree

7 files changed

+61
-24
lines changed

7 files changed

+61
-24
lines changed

cmip7_prep/cmor_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ def filled_for_cmor(
5757
# keep attrs helpful for downstream
5858
da2.attrs["_FillValue"] = f
5959
da2.attrs["missing_value"] = f
60+
if da2.dtype != np.float32:
61+
da2 = da2.astype(np.float32)
6062
return da2, f
6163

6264

cmip7_prep/cmor_writer.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,15 @@ def _get_1d_with_bounds(dsi: xr.Dataset, name: str, units_default: str):
292292
lon_id = None
293293
sdepth_id = None
294294

295-
print(f"[CMOR axis debug] var_dims: {var_dims}")
295+
logger.debug("[CMOR axis debug] var_dims: %s", var_dims)
296296
if "xh" in var_dims and "yh" in var_dims:
297297
# MOM6/curvilinear grid: register xh/yh as generic axes (i/j), not as lat/lon
298298
# Define the native grid using the coordinate arrays
299-
print(
300-
f"[CMOR axis debug] Defining unstructured grid for variable {var_name}."
299+
logger.debug(
300+
"[CMOR axis debug] Defining unstructured grid for variable %s.",
301+
var_name,
301302
)
303+
302304
i_id = cmor.axis(
303305
table_entry="i",
304306
units="1",
@@ -309,7 +311,7 @@ def _get_1d_with_bounds(dsi: xr.Dataset, name: str, units_default: str):
309311
units="1",
310312
length=ds["yh"].size,
311313
)
312-
print("[CMOR axis debug] Defining unstructured grid_id.")
314+
logger.debug("[CMOR axis debug] Defining unstructured grid_id.")
313315
grid_id = cmor.grid(
314316
axis_ids=[j_id, i_id], # note CMOR wants fastest varying last
315317
longitude=ds["geolon"].values,
@@ -331,9 +333,9 @@ def _get_1d_with_bounds(dsi: xr.Dataset, name: str, units_default: str):
331333
f" in variable '{var_name}' (curvilinear)"
332334
)
333335
axes_ids.append(axis_id)
334-
print(f"[CMOR axis debug] Appending grid_id: {grid_id}")
336+
logger.debug("[CMOR axis debug] Appending grid_id: %s", grid_id)
335337
axes_ids.append(grid_id)
336-
print(f"[CMOR axis debug] axes_ids: {axes_ids}")
338+
logger.debug("[CMOR axis debug] axes_ids: %s", axes_ids)
337339
return axes_ids
338340

339341
# --- horizontal axes (use CMOR names) ----
@@ -482,6 +484,8 @@ def _get_1d_with_bounds(dsi: xr.Dataset, name: str, units_default: str):
482484
values = ds["sdepth"].values
483485
logger.info("write sdepth axis")
484486
bnds = bounds_from_centers_1d(values, "sdepth")
487+
if bnds[0, 0] < 0:
488+
bnds[0, 0] = 0.0 # no negative soil depth bounds
485489
sdepth_id = cmor.axis(
486490
table_entry="sdepth",
487491
units="m",
@@ -641,8 +645,9 @@ def write_variable(
641645
cmor.load_table(table_filename)
642646

643647
data = ds[vdef.name]
648+
logger.info("Prepare data for CMOR %s", data.dtype) # debug
644649
data_filled, fillv = filled_for_cmor(data)
645-
logger.info("Define axes")
650+
logger.info("Define axes data_filled dtype: %s", data_filled.dtype) # debug
646651
axes_ids = self._define_axes(ds, vdef)
647652
units = getattr(vdef, "units", "") or ""
648653
# Debug logging for axis mapping
@@ -651,7 +656,7 @@ def write_variable(
651656
try:
652657
for i, aid in enumerate(axes_ids):
653658
entry = cmor.axis_entry(aid) if hasattr(cmor, "axis_entry") else None
654-
logger.info(
659+
logger.debug(
655660
"[CMOR DEBUG] axis %d: id=%s, table_entry=%s", i, aid, entry
656661
)
657662
# pylint: disable=broad-exception-caught
@@ -664,7 +669,6 @@ def write_variable(
664669
positive=getattr(vdef, "positive", None),
665670
missing_value=fillv,
666671
)
667-
data = ds[varname]
668672

669673
# ---- Prepare time info for this write (local, not cached) ----
670674
time_da = ds.coords.get("time")

cmip7_prep/mapping_compat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def realize(self, ds: xr.Dataset, cmip_name: str) -> xr.DataArray:
217217
vc = self._vars[cmip_name]
218218

219219
da = _realize_core(ds, vc)
220-
print(f"da is {da}")
220+
221221
if da is not None:
222222
if vc.unit_conversion is not None:
223223
da = _apply_unit_conversion(da, vc.unit_conversion)

cmip7_prep/pipeline.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,18 @@ def realize_regrid_prepare(
278278
names_to_regrid.append("PS")
279279

280280
# 7) Rename levgrnd if present to sdepth
281-
if "levgrnd" in ds_native.dims and "levgrnd" in ds_vert.coords:
281+
282+
# Check if 'levgrnd' is a dimension of any variable in names_to_regrid
283+
needs_levgrnd_rename = any(
284+
(v in ds_vert and "levgrnd" in getattr(ds_vert[v], "dims", []))
285+
for v in names_to_regrid
286+
)
287+
if (
288+
needs_levgrnd_rename
289+
and "levgrnd" in ds_native.dims
290+
and "levgrnd" in ds_native.coords
291+
):
292+
logger.info("Renaming 'levgrnd' dimension to 'sdepth'")
282293
ds_vert = ds_vert.rename_dims({"levgrnd": "sdepth"})
283294
# Ensure the coordinate variable is also copied
284295
ds_vert = ds_vert.assign_coords(sdepth=ds_native["levgrnd"].values)

cmip7_prep/regrid.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,14 +498,13 @@ def regrid_to_1deg(
498498
*non_spatial, "lat", "lon"
499499
) # ensure last two dims are ('lat','lon')
500500
)
501-
logger.info("here %s, %s", hdim, da2_2d.dims)
502501
else:
503502
da2_2d = da2.rename({"xh": "lon", "yh": "lat"}).transpose(
504503
*non_spatial, "lat", "lon"
505504
)
506505

507506
da2_2d = da2_2d.assign_coords(lon=((da2_2d.lon % 360)))
508-
logger.info(
507+
logger.debug(
509508
"da2_2d range: %f to %f lat, %f to %f lon",
510509
da2_2d["lat"].min().item(),
511510
da2_2d["lat"].max().item(),
@@ -666,7 +665,6 @@ def _build_fx_native(ds_native: xr.Dataset) -> xr.Dataset:
666665
pieces["sftlf"] = sftlf
667666
# Also extract sftof (sea fraction) if present
668667
sftof = None
669-
print("ds_native contains:", list(ds_native.data_vars))
670668
for name in ["sftof", "ocnfrac", "wet"]:
671669
if name in ds_native:
672670
logger.info("Extracting sftof from native variable %s", name)

scripts/fullLmon.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
module load conda
1010
conda activate CMORDEV
1111
NCPUS=$(cat $PBS_NODEFILE | wc -l)
12-
poetry run python ./scripts/monthly_cmor.py --realm lnd --test --workers $NCPUS --skip-timeseries --cmip-vars tsl
12+
poetry run python ./scripts/monthly_cmor.py --realm lnd --test --workers $NCPUS --skip-timeseries --cmip-vars cProduct
1313
#/glade/u/home/cmip7/cases/b.e30_beta06.B1850C_LTso.ne30_t232_wgx3.192.wrkflw.1 /glade/work/hannay/cesm_tags/cesm3_0_beta06/cime

scripts/monthly_cmor.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,15 @@ def process_one_var(
182182
parallel=True,
183183
open_kwargs=open_kwargs,
184184
)
185+
185186
# Append ocn_fx_fields to ds_native if available
186187
if realm == "ocn" and ocn_fx_fields is not None:
187188
ds_native = ds_native.merge(ocn_fx_fields)
188-
logger.info(
189-
f"ds_native keys: {list(ds_native.keys())} for var {varname} with dims {dims}"
189+
logger.debug(
190+
"ds_native keys: %s for var %s with dims %s",
191+
list(ds_native.keys()),
192+
varname,
193+
dims,
190194
)
191195
if var is None:
192196
logger.warning(f"Source variable(s) not found for {varname}")
@@ -215,8 +219,9 @@ def _extract_array(val):
215219
)
216220
else:
217221
# For lnd/atm or any other dims, use existing logic
218-
logger.info(f"Processing {varname} for dims {dims} (atm/lnd or other)")
219-
sftlf_path = next(Path(outdir).rglob("sftlf_fx_*.nc"), None)
222+
logger.debug(
223+
"Processing %s for dims %s (atm/lnd or other)", varname, dims
224+
)
220225
ds_cmor = realize_regrid_prepare(
221226
mapping,
222227
ds_native,
@@ -232,12 +237,14 @@ def _extract_array(val):
232237
)
233238
except Exception as e:
234239
logger.error(
235-
f"Exception during regridding of {varname} with dims {dims}: {e!r}"
240+
"Exception during regridding of %s with dims %s: %r",
241+
varname,
242+
dims,
243+
e,
236244
)
237245
continue
238246
try:
239247
# CMORize
240-
logger.info(f"CMOR writing for {varname} with dims {dims}")
241248
log_dir = outdir + "/logs"
242249
with CmorSession(
243250
tables_path=tables_path,
@@ -262,7 +269,9 @@ def _extract_array(val):
262269
"levels": cfg.get("levels", None),
263270
},
264271
)()
265-
logger.info(f"Writing variable {varname} with dims {dims}")
272+
logger.info(
273+
f"Writing variable {varname} with dims {dims} and type {ds_cmor[varname].dtype}"
274+
)
266275
cm.write_variable(ds_cmor, varname, vdef)
267276
logger.info(f"Finished processing for {varname} with dims {dims}")
268277
results.append((varname, "ok"))
@@ -428,7 +437,7 @@ def main():
428437
sys.exit(0)
429438
hf_collection = HFCollection(input_head_dir, dask_client=client)
430439
for include_pattern in include_patterns:
431-
logger.info(f"Processing files with pattern: {include_pattern}")
440+
logger.info("Processing files with pattern: %s", include_pattern)
432441
hfp_collection = hf_collection.include_patterns([include_pattern])
433442
hfp_collection.pull_metadata()
434443
ts_collection = TSCollection(
@@ -491,13 +500,26 @@ def main():
491500
results = []
492501
for _, result in as_completed(futures, with_results=True):
493502
try:
494-
results.append(result) # (v, status)
503+
# Handle result types: list of tuples, tuple, or other
504+
if isinstance(result, list):
505+
# If it's a list, check if it's a list of tuples
506+
if all(isinstance(x, tuple) and len(x) == 2 for x in result):
507+
results.extend(result)
508+
else:
509+
# Not a list of tuples, wrap as unknown
510+
results.append((str(result), "unknown"))
511+
elif isinstance(result, tuple) and len(result) == 2:
512+
results.append(result)
513+
else:
514+
# Not a tuple/list, wrap as unknown
515+
results.append((str(result), "unknown"))
495516
except Exception as e:
496517
logger.error("Task error:", e)
497518
raise
498519

499520
for v, status in results:
500521
logger.info(f"Variable {v} processed with status: {status}")
522+
501523
else:
502524
logger.info("No results to process.")
503525
if client:

0 commit comments

Comments
 (0)