anemoi-utils 0.1.8__py3-none-any.whl → 0.1.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of anemoi-utils might be problematic. Click here for more details.

anemoi/utils/_version.py CHANGED
@@ -12,5 +12,5 @@ __version__: str
12
12
  __version_tuple__: VERSION_TUPLE
13
13
  version_tuple: VERSION_TUPLE
14
14
 
15
- __version__ = version = '0.1.8'
16
- __version_tuple__ = version_tuple = (0, 1, 8)
15
+ __version__ = version = '0.1.9'
16
+ __version_tuple__ = version_tuple = (0, 1, 9)
anemoi/utils/dates.py CHANGED
@@ -10,6 +10,16 @@ import calendar
10
10
  import datetime
11
11
 
12
12
 
13
+ def normalise_frequency(frequency):
14
+ if isinstance(frequency, int):
15
+ return frequency
16
+ assert isinstance(frequency, str), (type(frequency), frequency)
17
+
18
+ unit = frequency[-1].lower()
19
+ v = int(frequency[:-1])
20
+ return {"h": v, "d": v * 24}[unit]
21
+
22
+
13
23
  def no_time_zone(date):
14
24
  """Remove time zone information from a date.
15
25
 
@@ -27,6 +37,7 @@ def no_time_zone(date):
27
37
  return date.replace(tzinfo=None)
28
38
 
29
39
 
40
+ # this function is use in anemoi-datasets
30
41
  def as_datetime(date):
31
42
  """Convert a date to a datetime object, removing any time zone information.
32
43
 
@@ -162,11 +173,15 @@ class HindcastDatesTimes:
162
173
  """
163
174
 
164
175
  self.reference_dates = reference_dates
165
- self.years = (1, years + 1)
176
+
177
+ if isinstance(years, list):
178
+ self.years = years
179
+ else:
180
+ self.years = range(1, years + 1)
166
181
 
167
182
  def __iter__(self):
168
183
  for reference_date in self.reference_dates:
169
- for year in range(*self.years):
184
+ for year in self.years:
170
185
  if reference_date.month == 2 and reference_date.day == 29:
171
186
  date = datetime.datetime(reference_date.year - year, 2, 28)
172
187
  else:
@@ -246,3 +261,61 @@ class Autumn(DateTimes):
246
261
  _description_
247
262
  """
248
263
  super().__init__(datetime.datetime(year, 9, 1), datetime.datetime(year, 11, 30), **kwargs)
264
+
265
+
266
+ class ConcatDateTimes:
267
+ def __init__(self, *dates):
268
+ if len(dates) == 1 and isinstance(dates[0], list):
269
+ dates = dates[0]
270
+
271
+ self.dates = dates
272
+
273
+ def __iter__(self):
274
+ for date in self.dates:
275
+ yield from date
276
+
277
+
278
+ class EnumDateTimes:
279
+ def __init__(self, dates):
280
+ self.dates = dates
281
+
282
+ def __iter__(self):
283
+ for date in self.dates:
284
+ yield as_datetime(date)
285
+
286
+
287
+ def datetimes_factory(*args, **kwargs):
288
+ if args and kwargs:
289
+ raise ValueError("Cannot provide both args and kwargs for a list of dates")
290
+
291
+ if not args and not kwargs:
292
+ raise ValueError("No dates provided")
293
+
294
+ if kwargs:
295
+ name = kwargs.get("name")
296
+
297
+ if name == "hindcast":
298
+ reference_dates = kwargs["reference_dates"]
299
+ reference_dates = datetimes_factory(reference_dates)
300
+ years = kwargs["years"]
301
+ return HindcastDatesTimes(reference_dates=reference_dates, years=years)
302
+
303
+ kwargs = kwargs.copy()
304
+ if "frequency" in kwargs:
305
+ freq = kwargs.pop("frequency")
306
+ kwargs["increment"] = normalise_frequency(freq)
307
+ return DateTimes(**kwargs)
308
+
309
+ if not any((isinstance(x, dict) or isinstance(x, list)) for x in args):
310
+ return EnumDateTimes(args)
311
+
312
+ if len(args) == 1:
313
+ a = args[0]
314
+
315
+ if isinstance(a, dict):
316
+ return datetimes_factory(**a)
317
+
318
+ if isinstance(a, list):
319
+ return datetimes_factory(*a)
320
+
321
+ return ConcatDateTimes(*[datetimes_factory(a) for a in args])
@@ -0,0 +1,76 @@
1
+ # (C) Copyright 2024 European Centre for Medium-Range Weather Forecasts.
2
+ # This software is licensed under the terms of the Apache Licence Version 2.0
3
+ # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
4
+ # In applying this licence, ECMWF does not waive the privileges and immunities
5
+ # granted to it by virtue of its status as an intergovernmental organisation
6
+ # nor does it submit to any jurisdiction.
7
+
8
+
9
+ """Utilities for working with Mars requests.
10
+
11
+ Has some konwledge of how certain streams are organised in Mars.
12
+
13
+ """
14
+
15
+ import datetime
16
+ import logging
17
+ import os
18
+
19
+ import yaml
20
+
21
+ LOG = logging.getLogger(__name__)
22
+
23
+ DEFAULT_MARS_LABELLING = {
24
+ "class": "od",
25
+ "type": "an",
26
+ "stream": "oper",
27
+ "expver": "0001",
28
+ }
29
+
30
+
31
+ def _expand_mars_labelling(request):
32
+ """Expand the request with the default Mars labelling.
33
+
34
+ The default Mars labelling is:
35
+
36
+ {'class': 'od',
37
+ 'type': 'an',
38
+ 'stream': 'oper',
39
+ 'expver': '0001'}
40
+
41
+ """
42
+ result = DEFAULT_MARS_LABELLING.copy()
43
+ result.update(request)
44
+ return result
45
+
46
+
47
+ STREAMS = None
48
+
49
+
50
+ def _lookup_mars_stream(request):
51
+ global STREAMS
52
+
53
+ if STREAMS is None:
54
+
55
+ with open(os.path.join(os.path.dirname(__file__), "mars.yaml")) as f:
56
+ STREAMS = yaml.safe_load(f)
57
+
58
+ request = _expand_mars_labelling(request)
59
+ for s in STREAMS:
60
+ match = s["match"]
61
+ if all(request.get(k) == v for k, v in match.items()):
62
+ return s["info"]
63
+
64
+
65
+ def recenter(date, center, members):
66
+
67
+ center = _lookup_mars_stream(center)
68
+ members = _lookup_mars_stream(members)
69
+
70
+ return (center, members)
71
+
72
+
73
+ if __name__ == "__main__":
74
+ date = datetime.datetime(2024, 5, 9, 0)
75
+
76
+ print(recenter(date, {"type": "an"}, {"stream": "elda"}))
@@ -0,0 +1,5 @@
1
+ - match:
2
+ class: od
3
+ stream: elda
4
+ info:
5
+ runs: [6, 18]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: anemoi-utils
3
- Version: 0.1.8
3
+ Version: 0.1.9
4
4
  Summary: A package to hold various functions to support training of ML models on ECMWF data.
5
5
  Author-email: "European Centre for Medium-Range Weather Forecasts (ECMWF)" <software.support@ecmwf.int>
6
6
  License: Apache License
@@ -223,6 +223,7 @@ Classifier: Operating System :: OS Independent
223
223
  Requires-Python: >=3.9
224
224
  License-File: LICENSE
225
225
  Requires-Dist: tomli
226
+ Requires-Dist: pyyaml
226
227
  Provides-Extra: all
227
228
  Requires-Dist: tomli ; extra == 'all'
228
229
  Requires-Dist: GitPython ; extra == 'all'
@@ -1,15 +1,17 @@
1
1
  anemoi/utils/__init__.py,sha256=zZZpbKIoGWwdCOuo6YSruLR7C0GzvzI1Wzhyqaa0K7M,456
2
- anemoi/utils/_version.py,sha256=PdJ7dZoz_SyEgX0MdrMfQYBFlGcwpemv6ibF8NKALBY,411
2
+ anemoi/utils/_version.py,sha256=NWmu2cvzOcqY9v-ee-qFLmtXRczssdN-cFGZ9qMNSmY,411
3
3
  anemoi/utils/caching.py,sha256=HrC9aFHlcCTaM2Z5u0ivGIXz7eFu35UQQhUuwwuG2pk,1743
4
4
  anemoi/utils/checkpoints.py,sha256=IR86FFNh5JR_uQVlgybnZG74PyU0CNLhyocqARwZIrs,2069
5
5
  anemoi/utils/config.py,sha256=XEesqODvkuE3ZA7dnEnZ-ooBRtU6ecPmkfP65FtialA,2147
6
- anemoi/utils/dates.py,sha256=ZabXLecHMio9ecIJbUxNcfOFoGfkcotSwy_xw1kNQ9w,6474
6
+ anemoi/utils/dates.py,sha256=Ot9OTY1uFvHxW1EU4DPv3oUqmzvkXTwKuwhlfVlY788,8426
7
7
  anemoi/utils/grib.py,sha256=gVfo4KYQv31iRyoqRDwk5tiqZDUgOIvhag_kO0qjYD0,3067
8
8
  anemoi/utils/humanize.py,sha256=LD6dGnqChxA5j3tMhSybsAGRQzi33d_qS9pUoUHubkc,10330
9
9
  anemoi/utils/provenance.py,sha256=v54L9jF1JgYcclOhg3iojRl1v3ajbiWz_oc289xTgO4,9574
10
10
  anemoi/utils/text.py,sha256=pGWtDvRFoDxAnSuZJiA-GOGJOJLHsw2dAm0tfVvPKno,8599
11
- anemoi_utils-0.1.8.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
12
- anemoi_utils-0.1.8.dist-info/METADATA,sha256=bEA_bMbWJubncfBmc6tMi6cJLtFgZrgth899Z60i5Yg,15079
13
- anemoi_utils-0.1.8.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
14
- anemoi_utils-0.1.8.dist-info/top_level.txt,sha256=DYn8VPs-fNwr7fNH9XIBqeXIwiYYd2E2k5-dUFFqUz0,7
15
- anemoi_utils-0.1.8.dist-info/RECORD,,
11
+ anemoi/utils/mars/__init__.py,sha256=RAeY8gJ7ZvsPlcIvrQ4fy9xVHs3SphTAPw_XJDtNIKo,1750
12
+ anemoi/utils/mars/mars.yaml,sha256=R0dujp75lLA4wCWhPeOQnzJ45WZAYLT8gpx509cBFlc,66
13
+ anemoi_utils-0.1.9.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
14
+ anemoi_utils-0.1.9.dist-info/METADATA,sha256=wVXW4E6hpkTjCEHUCWDiQXm-pZfJl81Mrzdy6kwKbfA,15101
15
+ anemoi_utils-0.1.9.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
16
+ anemoi_utils-0.1.9.dist-info/top_level.txt,sha256=DYn8VPs-fNwr7fNH9XIBqeXIwiYYd2E2k5-dUFFqUz0,7
17
+ anemoi_utils-0.1.9.dist-info/RECORD,,