Skip to content

Commit 4f7ad4d

Browse files
Rename constants to forcings (#408)
1 parent 721fe99 commit 4f7ad4d

9 files changed

+66
-69
lines changed

src/earthkit/data/sources/constants.py renamed to src/earthkit/data/sources/forcings.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
LOG = logging.getLogger(__name__)
2525

2626

27-
class ConstantMetadata(RawMetadata):
27+
class ForcingMetadata(RawMetadata):
2828
LS_KEYS = ["valid_datetime", "param"]
2929

3030
def __init__(self, d, geography):
@@ -42,7 +42,7 @@ def _valid_datetime(self):
4242
return datetime.datetime.fromisoformat(self["valid_datetime"])
4343

4444

45-
class ConstantMaker:
45+
class ForcingMaker:
4646
def __init__(self, field):
4747
self.field = field
4848
self.shape = self.field.shape
@@ -155,9 +155,6 @@ def sin_local_time(self, date):
155155
return np.sin(radians)
156156

157157
def insolation(self, date):
158-
# warn(
159-
# "The function `insolation` is deprecated, please use `cos_solar_zenith_angle` instead"
160-
# )
161158
return self.cos_solar_zenith_angle(date)
162159

163160
def toa_incident_solar_radiation(self, date):
@@ -216,7 +213,7 @@ def wrapper(date):
216213
return wrapper
217214

218215

219-
class ConstantField(Field):
216+
class ForcingField(Field):
220217
def __init__(self, maker, date, param, proc, number=None, array_backend=None):
221218
self.maker = maker
222219
self.date = date
@@ -235,7 +232,7 @@ def __init__(self, maker, date, param, proc, number=None, array_backend=None):
235232
)
236233
super().__init__(
237234
array_backend,
238-
metadata=ConstantMetadata(d, self.maker.field.metadata().geography),
235+
metadata=ForcingMetadata(d, self.maker.field.metadata().geography),
239236
)
240237

241238
def _make_metadata(self):
@@ -248,7 +245,7 @@ def _values(self, dtype=None):
248245
return values
249246

250247
def __repr__(self):
251-
return "ConstantField(%s,%s,%s)" % (self.param, self.date, self.number)
248+
return "ForcingField(%s,%s,%s)" % (self.param, self.date, self.number)
252249

253250

254251
def make_datetime(date, time):
@@ -287,7 +284,7 @@ def index_to_coords(index: int, shape):
287284
return result
288285

289286

290-
class ConstantsFieldListCore(FieldList):
287+
class ForcingsFieldListCore(FieldList):
291288
def __init__(self, source_or_dataset, request={}, **kwargs):
292289
request = dict(**request)
293290
request.update(kwargs)
@@ -317,7 +314,7 @@ def find_dates(source_or_dataset):
317314
make_datetime(date, time)
318315
for date, time in itertools.product(self.request["date"], self.request["time"])
319316
]
320-
assert len(set(dates)) == len(dates), "Duplicates dates in constants."
317+
assert len(set(dates)) == len(dates), "Duplicates dates in forcings."
321318
return dates
322319

323320
assert "date" not in self.request and "time" not in self.request
@@ -339,7 +336,7 @@ def find_dates(source_or_dataset):
339336
if not isinstance(self.numbers, (tuple, list)):
340337
self.numbers = [self.numbers]
341338

342-
self.maker = ConstantMaker(field=source_or_dataset[0])
339+
self.maker = ForcingMaker(field=source_or_dataset[0])
343340
self.procs = {param: getattr(self.maker, param) for param in self.params}
344341
self._len = len(self.dates) * len(self.params) * len(self.numbers)
345342

@@ -353,10 +350,10 @@ def _request(self, **request):
353350

354351
@classmethod
355352
def new_mask_index(self, *args, **kwargs):
356-
return ConstantsMaskFieldList(*args, **kwargs)
353+
return ForcingsMaskFieldList(*args, **kwargs)
357354

358355

359-
class ConstantsFieldList(ConstantsFieldListCore):
356+
class ForcingsFieldList(ForcingsFieldListCore):
360357
def __len__(self):
361358
return self._len
362359

@@ -375,7 +372,7 @@ def _getitem(self, n):
375372
param = self.params[param]
376373
number = self.numbers[number]
377374

378-
return ConstantField(
375+
return ForcingField(
379376
self.maker,
380377
date,
381378
param,
@@ -385,10 +382,10 @@ def _getitem(self, n):
385382
)
386383

387384

388-
class ConstantsMaskFieldList(ConstantsFieldListCore, MaskIndex):
385+
class ForcingsMaskFieldList(ForcingsFieldListCore, MaskIndex):
389386
def __init__(self, *args, **kwargs):
390387
MaskIndex.__init__(self, *args, **kwargs)
391388
FieldList._init_from_mask(self, self)
392389

393390

394-
source = ConstantsFieldList
391+
source = ForcingsFieldList
File renamed without changes.

tests/constants/constants_fixtures.py renamed to tests/forcings/forcings_fixtures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
]
3737

3838

39-
def load_constants_fs(params=None, first_step=6, last_step=72):
39+
def load_forcings_fs(params=None, first_step=6, last_step=72):
4040
sample = from_source("file", earthkit_examples_file("test.grib"))
4141

4242
if params is None:
@@ -58,7 +58,7 @@ def load_constants_fs(params=None, first_step=6, last_step=72):
5858
dates.append(start + datetime.timedelta(hours=step))
5959

6060
ds = from_source(
61-
"constants",
61+
"forcings",
6262
sample,
6363
date=dates,
6464
param=params,

tests/constants/test_constants_metadata.py renamed to tests/forcings/test_forcings_metadata.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
here = os.path.dirname(__file__)
1717
sys.path.insert(0, here)
18-
from constants_fixtures import load_constants_fs # noqa: E402
18+
from forcings_fixtures import load_forcings_fs # noqa: E402
1919

2020

21-
def test_constants_datetime():
22-
ds, _ = load_constants_fs(last_step=12)
21+
def test_forcings_datetime():
22+
ds, _ = load_forcings_fs(last_step=12)
2323

2424
ref = {
2525
"base_time": [None],
@@ -32,8 +32,8 @@ def test_constants_datetime():
3232
assert ds.datetime() == ref
3333

3434

35-
def test_constants_valid_datetime():
36-
ds, _ = load_constants_fs(last_step=12)
35+
def test_forcings_valid_datetime():
36+
ds, _ = load_forcings_fs(last_step=12)
3737
f = ds[4]
3838

3939
assert f.metadata("valid_datetime") == "2020-05-13T18:00:00"

tests/constants/test_contants_proc.py renamed to tests/forcings/test_forcings_proc.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020

2121
here = os.path.dirname(__file__)
2222
sys.path.insert(0, here)
23-
from constants_fixtures import all_params # noqa: E402
24-
from constants_fixtures import load_constants_fs # noqa: E402
23+
from forcings_fixtures import all_params # noqa: E402
24+
from forcings_fixtures import load_forcings_fs # noqa: E402
2525

2626

2727
def _build_proc_ref():
2828
import yaml
2929

30-
ds, _ = load_constants_fs(params=all_params, last_step=12)
30+
ds, _ = load_forcings_fs(params=all_params, last_step=12)
3131
d = {}
3232
for p in all_params:
3333
# print(f"p={p}")
@@ -44,11 +44,11 @@ def _build_proc_ref():
4444
yaml.dump(d, outfile, sort_keys=True)
4545

4646

47-
def test_constants_proc():
48-
with open(earthkit_test_data_file(os.path.join("constants", "proc.yaml")), "r") as f:
47+
def test_forcings_proc():
48+
with open(earthkit_test_data_file(os.path.join("forcings", "proc.yaml")), "r") as f:
4949
ref = yaml.safe_load(f)
5050

51-
ds, _ = load_constants_fs(params=all_params, last_step=12)
51+
ds, _ = load_forcings_fs(params=all_params, last_step=12)
5252

5353
for p in all_params:
5454
f = ds.sel(param=p, valid_datetime="2020-05-13T18:00:00")
@@ -61,8 +61,8 @@ def test_constants_proc():
6161

6262

6363
@pytest.mark.parametrize("param,coord", [("latitude", "lat"), ("longitude", "lon")])
64-
def test_constants_proc_latlon(param, coord):
65-
ds, _ = load_constants_fs(params=all_params, last_step=12)
64+
def test_forcings_proc_latlon(param, coord):
65+
ds, _ = load_forcings_fs(params=all_params, last_step=12)
6666

6767
latlon = ds[0].to_latlon(flatten=True)
6868
coord = latlon[coord]

tests/constants/test_constants_sel.py renamed to tests/forcings/test_forcings_sel.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
here = os.path.dirname(__file__)
1919
sys.path.insert(0, here)
20-
from constants_fixtures import load_constants_fs # noqa: E402
20+
from forcings_fixtures import load_forcings_fs # noqa: E402
2121

2222

2323
@pytest.mark.parametrize(
@@ -43,8 +43,8 @@
4343
(dict(INVALIDKEY="sin_logitude"), []),
4444
],
4545
)
46-
def test_constants_sel_single_file_1(params, expected_meta):
47-
ds, _ = load_constants_fs()
46+
def test_forcings_sel_single_file_1(params, expected_meta):
47+
ds, _ = load_forcings_fs()
4848

4949
g = ds.sel(**params)
5050
assert len(g) == len(expected_meta)
@@ -54,8 +54,8 @@ def test_constants_sel_single_file_1(params, expected_meta):
5454
return
5555

5656

57-
def test_constants_sel_single_file_as_dict():
58-
ds, _ = load_constants_fs()
57+
def test_forcings_sel_single_file_as_dict():
58+
ds, _ = load_forcings_fs()
5959

6060
g = ds.sel(
6161
{

tests/constants/test_constants_slice.py renamed to tests/forcings/test_forcings_slice.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@
1717

1818
here = os.path.dirname(__file__)
1919
sys.path.insert(0, here)
20-
from constants_fixtures import load_constants_fs # noqa: E402
20+
from forcings_fixtures import load_forcings_fs # noqa: E402
2121

2222

23-
def test_constants_single_index_bad():
24-
ds, _ = load_constants_fs()
23+
def test_forcings_single_index_bad():
24+
ds, _ = load_forcings_fs()
2525
idx = len(ds) + 10
2626
with pytest.raises(IndexError):
2727
ds[idx]
2828

2929

3030
@pytest.mark.parametrize("index", [0, 2, 95, -1, -96])
31-
def test_constants_single_index(index):
32-
ds, md = load_constants_fs()
31+
def test_forcings_single_index(index):
32+
ds, md = load_forcings_fs()
3333
num = len(ds)
3434
r = ds[index]
3535

@@ -54,8 +54,8 @@ def test_constants_single_index(index):
5454
slice(91, None),
5555
],
5656
)
57-
def test_constants_slice(indexes):
58-
ds, md = load_constants_fs()
57+
def test_forcings_slice(indexes):
58+
ds, md = load_forcings_fs()
5959
num = len(ds)
6060
r = ds[indexes]
6161

@@ -79,8 +79,8 @@ def test_constants_slice(indexes):
7979
((1, 16, 5, 9), (1, 3)),
8080
],
8181
)
82-
def test_constants_array_indexing(indexes1, indexes2):
83-
ds, md = load_constants_fs()
82+
def test_forcings_array_indexing(indexes1, indexes2):
83+
ds, md = load_forcings_fs()
8484

8585
# first subset
8686
r = ds[indexes1]
@@ -106,14 +106,14 @@ def test_constants_array_indexing(indexes1, indexes2):
106106
((1, 16, 5, 9), (1, 3)),
107107
],
108108
)
109-
def test_constants_array_indexing_bad(indexes):
110-
ds, _ = load_constants_fs()
109+
def test_forcings_array_indexing_bad(indexes):
110+
ds, _ = load_forcings_fs()
111111
with pytest.raises(IndexError):
112112
ds[indexes]
113113

114114

115-
def test_constants_fieldlist_iterator():
116-
ds, md = load_constants_fs()
115+
def test_forcings_fieldlist_iterator():
116+
ds, md = load_forcings_fs()
117117
# sn = ds.metadata(["valid_datetime", "param"])
118118
sn = md
119119
assert len(sn) == len(ds)

tests/constants/test_constants_source.py renamed to tests/forcings/test_forcings_source.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020

2121
here = os.path.dirname(__file__)
2222
sys.path.insert(0, here)
23-
from constants_fixtures import all_params # noqa: E402
23+
from forcings_fixtures import all_params # noqa: E402
2424

2525

26-
def test_constant_source_1():
26+
def test_forcings_source_1():
2727
sample = from_source("file", earthkit_examples_file("test.grib"))
2828

2929
start = sample[0].datetime()["valid_time"]
@@ -37,7 +37,7 @@ def test_constant_source_1():
3737
params = all_params
3838

3939
ds = from_source(
40-
"constants",
40+
"forcings",
4141
sample,
4242
date=dates,
4343
param=all_params,
@@ -53,7 +53,7 @@ def test_constant_source_1():
5353
assert f.metadata("param") == r[1]
5454

5555

56-
def test_constant_2():
56+
def test_forcings_2():
5757
sample = from_source("file", earthkit_examples_file("test.grib"))
5858

5959
start = sample[0].datetime()["valid_time"]
@@ -69,7 +69,7 @@ def test_constant_2():
6969

7070
ntimes = 4
7171
ds = from_source(
72-
"constants",
72+
"forcings",
7373
sample,
7474
date=dates,
7575
time=f"0/to/18/by/{24//ntimes}",
@@ -86,7 +86,7 @@ def test_constant_2():
8686
assert f.metadata("param") == r[1]
8787

8888

89-
def test_constant_3():
89+
def test_forcings_3():
9090
sample = from_source("file", earthkit_test_data_file("t_time_series.grib"))
9191

9292
dates = [
@@ -100,7 +100,7 @@ def test_constant_3():
100100
params = all_params
101101

102102
ds = from_source(
103-
"constants",
103+
"forcings",
104104
sample,
105105
param=params,
106106
)

0 commit comments

Comments
 (0)