1-
21# cmip7_prep/mapping_compat.py
32r"""Mapping loader/evaluator compatible with CMIP6-style lists and CMIP7-style dicts.
43
@@ -48,6 +47,7 @@ def _normalize_table_name(value: Optional[str]) -> Optional[str]:
4847@dataclass (frozen = True )
4948class VarConfig :
5049 """Normalized mapping entry for a single CMIP variable."""
50+
5151 name : str
5252 table : Optional [str ] = None
5353 units : Optional [str ] = None
@@ -110,6 +110,7 @@ class Mapping:
110110 The loader accepts both dict- and list-based YAML styles. All table names
111111 are normalized to a short form (e.g., 'Amon').
112112 """
113+
113114 def __init__ (self , path : str | Path ) -> None :
114115 self .path = Path (path )
115116 self ._vars : Dict [str , VarConfig ] = self ._load_yaml (self .path )
@@ -138,7 +139,9 @@ def _load_yaml(path: Path) -> Dict[str, VarConfig]:
138139 name = str (item ["name" ])
139140 result [name ] = _to_varconfig (name , item )
140141 else :
141- raise TypeError ("Unsupported YAML structure: expected dict or list at top level." )
142+ raise TypeError (
143+ "Unsupported YAML structure: expected dict or list at top level."
144+ )
142145
143146 return result
144147
@@ -208,7 +211,9 @@ def _to_varconfig(name: str, cfg: TMapping[str, Any]) -> VarConfig:
208211 return vc
209212
210213
211- def _require_vars (ds : xr .Dataset , names : List [str ], context : str ) -> Dict [str , xr .DataArray ]:
214+ def _require_vars (
215+ ds : xr .Dataset , names : List [str ], context : str
216+ ) -> Dict [str , xr .DataArray ]:
212217 missing = [n for n in names if n not in ds ]
213218 if missing :
214219 raise KeyError (f"{ context } : missing variables { missing } " )
@@ -224,7 +229,11 @@ def _realize_core(ds: xr.Dataset, vc: VarConfig) -> xr.DataArray:
224229 return ds [vc .source ]
225230
226231 # 2) identity mapping from a single raw variable
227- if vc .raw_variables and vc .formula in (None , "" , "null" ) and len (vc .raw_variables ) == 1 :
232+ if (
233+ vc .raw_variables
234+ and vc .formula in (None , "" , "null" )
235+ and len (vc .raw_variables ) == 1
236+ ):
228237 var = vc .raw_variables [0 ]
229238 if var not in ds :
230239 raise KeyError (f"raw variable { var !r} not found in dataset" )
@@ -263,7 +272,9 @@ def _apply_unit_conversion(da: xr.DataArray, rule: Any) -> xr.DataArray:
263272 try :
264273 out = _safe_eval (rule , {"x" : da })
265274 except Exception as exc :
266- raise ValueError (f"Error evaluating unit_conversion expression: { exc } " ) from exc
275+ raise ValueError (
276+ f"Error evaluating unit_conversion expression: { exc } "
277+ ) from exc
267278 if not isinstance (out , xr .DataArray ):
268279 raise ValueError ("unit_conversion expression did not return a DataArray" )
269280 return out
@@ -273,4 +284,6 @@ def _apply_unit_conversion(da: xr.DataArray, rule: Any) -> xr.DataArray:
273284 offset = rule .get ("offset" , 0.0 )
274285 return da * float (scale ) + float (offset )
275286
276- raise TypeError ("unit_conversion must be a string expression or a dict with 'scale'/'offset'" )
287+ raise TypeError (
288+ "unit_conversion must be a string expression or a dict with 'scale'/'offset'"
289+ )
0 commit comments