Skip to content

Commit

Permalink
used tempfile for fixed output
Browse files Browse the repository at this point in the history
  • Loading branch information
cehbrecht committed Nov 19, 2024
1 parent 7e2a3fd commit 25945e0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/rook/utils/concat_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ def _calculate(self):
datasets = []
for ds_id in norm_collection.keys():
ds = norm_collection[ds_id]
ds_mod = apply_decadal_fixes(ds_id, ds)
ds_mod = apply_decadal_fixes(
ds_id, ds, output_dir=self.params.get("output_dir", ".")
)
datasets.append(ds_mod)

dims = dimension_parameter.DimensionParameter(
Expand Down
15 changes: 10 additions & 5 deletions src/rook/utils/decadal_fixes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import xarray as xr
import os
import tempfile

from roocs_utils.xarray_utils.xarray_utils import open_xr_dataset

Expand Down Expand Up @@ -47,8 +49,8 @@ def get_decadal_model_attr_from_dict(ds_id, ds, attr):
return value


def apply_decadal_fixes(ds_id, ds):
ds_mod = decadal_fix_calendar(ds_id, ds)
def apply_decadal_fixes(ds_id, ds, output_dir=None):
ds_mod = decadal_fix_calendar(ds_id, ds, output_dir=output_dir)
ds_mod = decadal_fix_1(ds_id, ds_mod)
ds_mod = decadal_fix_2(ds_id, ds_mod)
ds_mod = decadal_fix_3(ds_id, ds_mod)
Expand Down Expand Up @@ -137,13 +139,16 @@ def decadal_fix_5(ds_id, ds):
return ds_mod


def decadal_fix_calendar(ds_id, ds):
def decadal_fix_calendar(ds_id, ds, output_dir=None):
# set proleptic_gregorian calendar to gregorian (standard).
# the proleptic gregorian calendar extends the gregorin backward in time before 1582.
calendar = ds.time.encoding.get("calendar", "standard")
if calendar == "proleptic_gregorian":
ds.time.encoding["calendar"] = "standard"
# need to write and read file to rewrite time dimension for the standard calendar!
ds.to_netcdf("fixed_calendar.nc")
ds = open_xr_dataset("fixed_calendar.nc")
tmp_dir = tempfile.TemporaryDirectory(dir=output_dir)
fixed_nc = os.path.join(tmp_dir, "fixed_calendar.nc")
ds.to_netcdf(fixed_nc)
ds = open_xr_dataset(fixed_nc)
tmp_dir.cleanup()
return ds

0 comments on commit 25945e0

Please sign in to comment.