Skip to content

Commit f4d5d41

Browse files
committed
testone is working in cmip7 account
1 parent b0454e7 commit f4d5d41

File tree

4 files changed

+19
-26
lines changed

4 files changed

+19
-26
lines changed

cmip7_prep/cmor_writer.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
from contextlib import AbstractContextManager, contextmanager
1111
from pathlib import Path
12-
1312
import re
1413
import types
1514
import warnings
@@ -407,20 +406,20 @@ def __init__(
407406
# NEW: one log per run (session)
408407
log_dir: Path | str | None = None,
409408
log_name: str | None = None,
410-
outdir: Path,
409+
outdir: Path | str | None = None,
411410
) -> None:
412411
self.tables_path = Path(tables_path)
413412
self.dataset_attrs = dict(dataset_attrs or {})
414413
self.dataset_json = dataset_json
415-
self._dataset_json_path = None
416414
self._dataset_json_cm = None
417415
self.tracking_prefix = tracking_prefix
418416
# logging config
419417
self._log_dir = Path(log_dir) if log_dir is not None else None
420418
self._log_name = log_name
421419
self._log_path: Path | None = None
422420
self._pending_ps = None
423-
self._outdir = outdir
421+
self._outdir = Path(outdir) if outdir is not None else Path.cwd() / "CMIP7"
422+
self._outdir.mkdir(parents=True, exist_ok=True)
424423

425424
def __enter__(self) -> "CmorSession":
426425
# Resolve logfile path if requested
@@ -429,7 +428,7 @@ def __enter__(self) -> "CmorSession":
429428
fname = self._log_name or f"cmor_{ts}.log"
430429
self._log_dir.mkdir(parents=True, exist_ok=True)
431430
self._log_path = (self._log_dir / fname).resolve()
432-
print(f"Creating CMOR data with logs to {self._log_path}")
431+
print(f"Creating CMOR data with Tables path {self.tables_path}")
433432

434433
# Setup CMOR; pass logfile if CMOR supports it, else fall back
435434
try:
@@ -478,9 +477,9 @@ def __enter__(self) -> "CmorSession":
478477
# caller passed a context manager directly
479478
self._dataset_json_cm = dj
480479
p = dj.__enter__() # ← ENTER the CM, get a Path
481-
self._dataset_json_path = str(p)
482-
cmor.set_cur_dataset_attribute("outpath", self._outdir)
483-
480+
cmor.dataset_json(str(p))
481+
cmor.set_cur_dataset_attribute("outpath", str(self._outdir))
482+
print(f"outpath set to {self._outdir}")
484483
try:
485484
prod = cmor.get_cur_dataset_attribute("product") # type: ignore[attr-defined]
486485
except Exception: # pylint: disable=broad-except
@@ -772,7 +771,7 @@ def write_variable(
772771
)
773772

774773
table_filename = _resolve_table_filename(self.tables_path, table_key)
775-
774+
print(f"table_filename is {table_filename} table_key {table_key}")
776775
cmor.load_table(table_filename)
777776

778777
data = ds[vdef.name]
@@ -786,13 +785,6 @@ def write_variable(
786785
positive=getattr(vdef, "positive", None),
787786
missing_value=fillv,
788787
)
789-
# Optional variable attributes (e.g., cell_methods, long_name, standard_name)
790-
if getattr(vdef, "cell_methods", None):
791-
cmor.set_variable_attribute(var_id, "cell_methods", vdef.cell_methods)
792-
if getattr(vdef, "long_name", None):
793-
cmor.set_variable_attribute(var_id, "long_name", vdef.long_name)
794-
if getattr(vdef, "standard_name", None):
795-
cmor.set_variable_attribute(var_id, "standard_name", vdef.standard_name)
796788
data = ds[varname]
797789

798790
# ---- Prepare time info for this write (local, not cached) ----
@@ -827,7 +819,6 @@ def write_variable(
827819
cmor.write(ps_id, np.asarray(ps_filled), store_with=var_id)
828820
self._pending_ps = None
829821
outdir = Path(cmor.get_cur_dataset_attribute("outpath"))
830-
outdir.mkdir(parents=True, exist_ok=True)
831822
outfile = outdir / f"{getattr(vdef, 'name', varname)}.nc"
832823
cmor.close(var_id, file_name=str(outfile))
833824

cmip7_prep/data/cmor_dataset.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"mip_era": "CMIP6",
2+
"mip_era": "CMIP7",
33
"activity_id": "CMIP",
44
"institution_id": "NCAR",
55
"institution": "National Center for Atmospheric Research, Climate and Global Dynamics Laboratory, 1850 Table Mesa Drive, Boulder, CO 80305, USA",
66
"contact": "[email protected]",
77
"product": "model-output",
88

99
"source_id": "CESM2",
10-
"source": "CESM2 (2025): atmosphere: CAM; ocean: MOM; sea ice: CICE5; land: CTSM; aerosol: MAM4; coupler: CMEPS.",
10+
"source": "CESM3 (2025): atmosphere: CAM; ocean: MOM; sea ice: CICE6; land: CTSM; aerosol: MAM4; coupler: CMEPS.",
1111
"source_type": "AOGCM",
1212
"nominal_resolution": "100 km",
1313

@@ -28,8 +28,9 @@
2828

2929
"grid_label": "gr",
3030

31-
"title": "CESM2 piControl on 1x1 degree grid (regridded)",
32-
"references": "See CESM2 documentation and CMIP6 references for NCAR/CGD.",
31+
"title": "CESM3 piControl on 1x1 degree grid (regridded from ne30np3 se grid)",
32+
"references": "See CESM3 documentation and CMIP7 references for NCAR/CGD.",
33+
"outpath": ".",
3334
"output_path_template": "<mip_era><activity_id><institution_id><source_id><experiment_id><_member_id><table><variable_id><grid_label>",
3435
"output_file_template": "<variable_id><table><source_id><experiment_id><run_variant><grid_label>"
3536
}

scripts/testone.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
scratch
1212
+ "/archive/timeseries/b.e30_beta06.B1850C_LTso.ne30_t232_wgx3.192.wrkflw.1/atm/hist/"
1313
)
14-
TABLES = "/glade/work/cmip7/e3sm_to_cmip/cmip6-cmor-tables/Tables"
14+
TABLES = Path("/glade/work/cmip7/e3sm_to_cmip/cmip6-cmor-tables/Tables")
1515

1616
# 0) Load mapping (uses packaged data/cesm_to_cmip7.yaml by default)
1717
mapping = Mapping.from_packaged_default()
@@ -76,8 +76,7 @@
7676
"levels": cfg.get("levels", None),
7777
},
7878
)()
79-
8079
# Your writer expects a dataset with varname present:
81-
cm.write_variable(ds_cmor, varname, vdef, outdir=OUTDIR)
80+
cm.write_variable(ds_cmor, varname, vdef)
8281

8382
print("ok")

tests/test_cmor_writer_load_table.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ def test_write_variable_loads_basename_table_and_defines_axes(fake_cmor, tmp_pat
3737
cw.CmurSession
3838
if False
3939
else cw.CmorSession(
40-
tables_path=tables_path, dataset_attrs={"institution_id": "NCAR"}
40+
tables_path=tables_path,
41+
dataset_attrs={"institution_id": "NCAR"},
42+
outdir=outdir,
4143
)
4244
) as cm: # noqa: E701
43-
cm.write_variable(ds, "tas", vdef, outdir=outdir)
45+
cm.write_variable(ds, "tas", vdef)
4446

4547
# Table basename should be used (resolved by inpath)
4648
assert fake.last_table == "Amon.json"

0 commit comments

Comments
 (0)