Skip to content

Commit 8ea76f6

Browse files
cleaning some code duplication (#256)
* cleaning somd code duplication * update whats new
1 parent f8f93fc commit 8ea76f6

File tree

5 files changed

+69
-126
lines changed

5 files changed

+69
-126
lines changed

docs/whats_new.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ What's New
1111
v0.8.3 (22 October 2025)
1212
------------------------
1313

14-
Bugfix release.
14+
Maintenance and bugfix release.
15+
16+
Internal Changes
17+
~~~~~~~~~~~~~~~~
18+
19+
- Cleaning of unused imports (:pull:`256`)
20+
21+
Bugfixes
22+
~~~~~~~~
1523

1624
- Fix fetching of vertical coordinates from CF models (:pull:`255`)
1725

pyremo/preproc/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
CFPreprocessor,
1010
RemoPreprocessor,
1111
)
12-
from .remapping import remap, remap_remo, to_netcdf, to_tar
12+
from .remapping import remap, remap_remo
13+
from .utils import to_netcdf, to_tar
1314

1415
# from .remap_new import Remapper
1516
from .utils import write_forcing_file

pyremo/preproc/core.py

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -222,56 +222,56 @@ def open_datasets(datasets, ref_ds=None, time_range=None):
222222
return output
223223

224224

225-
def gfile(ds, ref_ds=None, tos=None, time_range=None, attrs=None):
226-
"""Creates a global dataset ready for preprocessing.
227-
228-
This function creates a homogenized global dataset. If neccessary,
229-
units are converted and the sea surface temperature ``tos`` is
230-
interpolated spatially and temporally to the atmospheric grid.
231-
232-
Parameters
233-
----------
234-
ds : xarray.Dataset or dict of filenames
235-
Input dataset from a global model according to CF conventions.
236-
237-
ref_ds : xarray.Dataset
238-
Reference datasets that is used for determining the grid and vertical
239-
coordinates and the global attributes. If ``ref_ds=None``, ``ta`` from
240-
the input dataset is used as a reference.
241-
242-
tos : xarray.Dataset
243-
Sea surface dataset.
244-
245-
time_range :
246-
The common time range from the input and sst that should be used.
247-
248-
attrs:
249-
Global attributes for the output dataset. If ``attrs=None``, the global
250-
attributes from ``ref_ds`` are used.
251-
252-
Returns
253-
-------
254-
gfile : xarray.Dataset
255-
Global dataset ready for preprocessing.
256-
257-
"""
258-
if isinstance(ds, dict):
259-
ds = open_datasets(ds, ref_ds, time_range)
260-
else:
261-
ds = ds.copy()
262-
ds["akgm"], ds["bkgm"] = get_vc(ds)
263-
ds = check_lev(ds)
264-
265-
if time_range is None:
266-
time_range = ds.time
267-
ds = ds.sel(time=time_range)
268-
269-
if tos is not None:
270-
ds["tos"] = map_sst(tos, ds.sel(time=time_range))
271-
# ds = ds.rename({"lev": lev_gm})
272-
ds = convert_units(ds)
273-
# if "sftlf" in ds:
274-
# ds["sftlf"] = np.around(ds.sftlf)
275-
if attrs is None:
276-
attrs = ds.attrs
277-
return ds
225+
# def gfile(ds, ref_ds=None, tos=None, time_range=None, attrs=None):
226+
# """Creates a global dataset ready for preprocessing.
227+
228+
# This function creates a homogenized global dataset. If neccessary,
229+
# units are converted and the sea surface temperature ``tos`` is
230+
# interpolated spatially and temporally to the atmospheric grid.
231+
232+
# Parameters
233+
# ----------
234+
# ds : xarray.Dataset or dict of filenames
235+
# Input dataset from a global model according to CF conventions.
236+
237+
# ref_ds : xarray.Dataset
238+
# Reference datasets that is used for determining the grid and vertical
239+
# coordinates and the global attributes. If ``ref_ds=None``, ``ta`` from
240+
# the input dataset is used as a reference.
241+
242+
# tos : xarray.Dataset
243+
# Sea surface dataset.
244+
245+
# time_range :
246+
# The common time range from the input and sst that should be used.
247+
248+
# attrs:
249+
# Global attributes for the output dataset. If ``attrs=None``, the global
250+
# attributes from ``ref_ds`` are used.
251+
252+
# Returns
253+
# -------
254+
# gfile : xarray.Dataset
255+
# Global dataset ready for preprocessing.
256+
257+
# """
258+
# if isinstance(ds, dict):
259+
# ds = open_datasets(ds, ref_ds, time_range)
260+
# else:
261+
# ds = ds.copy()
262+
# ds["akgm"], ds["bkgm"] = get_vc(ds)
263+
# ds = check_lev(ds)
264+
265+
# if time_range is None:
266+
# time_range = ds.time
267+
# ds = ds.sel(time=time_range)
268+
269+
# if tos is not None:
270+
# ds["tos"] = map_sst(tos, ds.sel(time=time_range))
271+
# # ds = ds.rename({"lev": lev_gm})
272+
# ds = convert_units(ds)
273+
# # if "sftlf" in ds:
274+
# # ds["sftlf"] = np.around(ds.sftlf)
275+
# if attrs is None:
276+
# attrs = ds.attrs
277+
# return ds

pyremo/preproc/remapping.py

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import os
2-
31
import cf_xarray as cfxr # noqa
42
import cordex as cx
53
import numpy as np
@@ -8,7 +6,7 @@
86
import pyremo as pr
97

108
from . import physics
11-
from .constants import fillvars, lev_input
9+
from .constants import lev_input
1210
from .core import const, get_akbkem
1311
from .utils import update_attrs
1412
from .xpyintorg import (
@@ -40,65 +38,6 @@
4038
xr.set_options(keep_attrs=True)
4139

4240

43-
def get_filename(date, expid="000000", template=None):
44-
if template is None:
45-
template = "x{}x{}.nc"
46-
return template.format(expid, date.strftime(format="%Y%m%d%H"))
47-
48-
49-
def to_netcdf(
50-
ads,
51-
path="",
52-
expid="000000",
53-
template=None,
54-
tempfiles=None,
55-
missval=1.0e20,
56-
**kwargs,
57-
):
58-
"""write dataset to netcdf
59-
60-
by default, each timestep goes into a separate output file
61-
62-
"""
63-
if not os.path.isdir(path):
64-
os.makedirs(path)
65-
expand_time = [var for var, da in ads.items() if "time" in da.dims]
66-
if template is None:
67-
template = "a{}a{}.nc"
68-
dates, datasets = zip(*ads.groupby("time"))
69-
paths = [os.path.join(path, get_filename(date, expid, template)) for date in dates]
70-
dsets = []
71-
# expand time dimension only for variables not coordinates.
72-
for ds in datasets:
73-
for var, da in ds.items():
74-
if var in expand_time:
75-
ds[var] = da.expand_dims("time")
76-
if var in fillvars:
77-
ds[var].encoding["_FillValue"] = missval
78-
else:
79-
ds[var].encoding["_FillValue"] = None
80-
dsets.append(ds)
81-
# dsets = [dset.expand_dims('time') for dset in datasets]
82-
return xr.save_mfdataset(dsets, paths, **kwargs)
83-
84-
85-
def to_tar(files, tar_file, mode="w"):
86-
import tarfile
87-
88-
try:
89-
from tqdm import tqdm
90-
except Exception:
91-
92-
def tqdm(x):
93-
return x
94-
95-
tf = tarfile.open(tar_file, mode=mode)
96-
for f in tqdm(files, desc="creating tarfile"):
97-
tf.add(f, arcname=os.path.basename(f), recursive=False)
98-
tf.close()
99-
return tar_file
100-
101-
10241
def broadcast_coords(ds, coords=("lon", "lat")):
10342
"""broadcast 1d global coordinates"""
10443
lat2d, lon2d = xr.broadcast(ds[coords[1]], ds[coords[0]])

pyremo/preproc/utils.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ def to_netcdf(
207207
path="",
208208
expid="000000",
209209
template=None,
210-
tempfiles=None,
211210
missval=1.0e20,
212211
**kwargs,
213212
):
@@ -242,11 +241,10 @@ def to_netcdf(
242241
DeprecationWarning,
243242
stacklevel=2,
244243
)
245-
if not os.path.isdir(path):
246-
os.makedirs(path)
247-
expand_time = [var for var, da in ads.items() if "time" in da.dims]
248-
if template is None:
249-
template = "a{}a{}.nc"
244+
os.makedirs(path, exist_ok=True)
245+
expand_time = [
246+
var for var, da in ads.items() if "time" in da.dims and "time" not in da.coords
247+
]
250248
dates, datasets = zip(*ads.groupby("time"))
251249
paths = [os.path.join(path, get_filename(date, expid, template)) for date in dates]
252250
dsets = []
@@ -262,9 +260,6 @@ def to_netcdf(
262260
dsets.append(ds)
263261
# dsets = [dset.expand_dims('time') for dset in datasets]
264262
xr.save_mfdataset(dsets, paths, **kwargs)
265-
if tempfiles is not None:
266-
for f in tempfiles:
267-
os.remove(f)
268263
return paths
269264

270265

0 commit comments

Comments
 (0)