99
1010from contextlib import AbstractContextManager , contextmanager
1111from pathlib import Path
12-
1312import re
1413import types
1514import 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
0 commit comments