From 25945e0ac6295033c3910a2891f4a7f6f8a3a1f5 Mon Sep 17 00:00:00 2001 From: Pingu Carsti Date: Tue, 19 Nov 2024 15:52:46 +0100 Subject: [PATCH] used tempfile for fixed output --- src/rook/utils/concat_utils.py | 4 +++- src/rook/utils/decadal_fixes.py | 15 ++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/rook/utils/concat_utils.py b/src/rook/utils/concat_utils.py index 1585da5..65e3357 100644 --- a/src/rook/utils/concat_utils.py +++ b/src/rook/utils/concat_utils.py @@ -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( diff --git a/src/rook/utils/decadal_fixes.py b/src/rook/utils/decadal_fixes.py index 5dc6c5e..686f325 100644 --- a/src/rook/utils/decadal_fixes.py +++ b/src/rook/utils/decadal_fixes.py @@ -1,4 +1,6 @@ import xarray as xr +import os +import tempfile from roocs_utils.xarray_utils.xarray_utils import open_xr_dataset @@ -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) @@ -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