Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit fd12122

Browse files
committed
ensemble perturbations ok
1 parent e76b13f commit fd12122

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

ecml_tools/create/functions/ensemble_perturbations.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
#
99

1010
import warnings
11-
import climetlab as cml
11+
1212
import numpy as np
1313
import tqdm
14+
from climetlab import load_source
1415
from climetlab.core.temporary import temp_file
1516
from climetlab.readers.grib.output import new_grib_output
1617

@@ -44,11 +45,11 @@ def ensembles_perturbations(ensembles, center, mean, remapping={}, patches={}):
4445
n_ensembles = len(normalise_number(ensembles["number"]))
4546

4647
print(f"Retrieving ensemble data with {ensembles}")
47-
ensembles = cml.load_source(**ensembles)
48+
ensembles = load_source(**ensembles)
4849
print(f"Retrieving center data with {center}")
49-
center = cml.load_source(**center)
50+
center = load_source(**center)
5051
print(f"Retrieving mean data with {mean}")
51-
mean = cml.load_source(**mean)
52+
mean = load_source(**mean)
5253

5354
assert len(mean) * n_ensembles == len(ensembles), (
5455
len(mean),
@@ -82,7 +83,11 @@ def ensembles_perturbations(ensembles, center, mean, remapping={}, patches={}):
8283
center_coords[k],
8384
ensembles_coords[k],
8485
)
85-
assert set(center_coords[k]) == set(mean_coords[k]), (k, center_coords[k], mean_coords[k])
86+
assert set(center_coords[k]) == set(mean_coords[k]), (
87+
k,
88+
center_coords[k],
89+
mean_coords[k],
90+
)
8691

8792
for field in tqdm.tqdm(center):
8893
param = field.metadata("param")
@@ -105,7 +110,10 @@ def ensembles_perturbations(ensembles, center, mean, remapping={}, patches={}):
105110

106111
for number in ensembles_coords["number"]:
107112
ensembles_field = get_unique_field(ensembles.sel(number=number), selection)
108-
assert ensembles_field.metadata("grid") == grid, (ensembles_field.metadata("grid"), grid)
113+
assert ensembles_field.metadata("grid") == grid, (
114+
ensembles_field.metadata("grid"),
115+
grid,
116+
)
109117

110118
e = ensembles_field.to_numpy()
111119
assert c.shape == e.shape, (c.shape, e.shape)
@@ -122,7 +130,7 @@ def ensembles_perturbations(ensembles, center, mean, remapping={}, patches={}):
122130

123131
out.close()
124132

125-
ds = cml.load_source("file", path)
133+
ds = load_source("file", path)
126134
assert len(ds) == len(ensembles), (len(ds), len(ensembles))
127135
ds._tmp = tmp
128136

ecml_tools/create/input.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
# granted to it by virtue of its status as an intergovernmental organisation
77
# nor does it submit to any jurisdiction.
88
#
9-
import os
10-
from importlib import import_module
119
import datetime
10+
import importlib
1211
import logging
12+
import os
1313
import time
1414
from collections import defaultdict
1515
from copy import deepcopy
@@ -339,6 +339,7 @@ def __getitem__(self, key):
339339
class FunctionResult(Result):
340340
def __init__(self, context, dates, action, previous_sibling=None):
341341
super().__init__(context, dates)
342+
assert isinstance(action, Action), type(action)
342343
self.action = action
343344

344345
_args = self.action.args
@@ -429,16 +430,14 @@ def __init__(self, context, name, **kwargs):
429430

430431
@property
431432
def function(self):
432-
from .functions import ensemble_perturbations
433-
434-
return ensemble_perturbations.execute
435-
import os
436-
437-
here = os.path.dirname(os.path.dirname(__file__))
438-
name = self.action.kwargs["name"]
439-
proc = import_module(f".functions.{name}", package=__name__).execute
440-
441-
return proc
433+
here = os.path.dirname(__file__)
434+
path = os.path.join(here, "functions", f"{self.name}.py")
435+
spec = importlib.util.spec_from_file_location(self.name, path)
436+
module = spec.loader.load_module()
437+
# TODO: this fails here, fix this.
438+
# getattr(module, self.name)
439+
# self.action.kwargs
440+
return module.execute
442441

443442

444443
class ConcatResult(Result):

0 commit comments

Comments
 (0)