pycontrails 0.54.0__cp312-cp312-macosx_10_13_x86_64.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 pycontrails might be problematic. Click here for more details.

Files changed (109) hide show
  1. pycontrails/__init__.py +70 -0
  2. pycontrails/_version.py +16 -0
  3. pycontrails/core/__init__.py +30 -0
  4. pycontrails/core/aircraft_performance.py +641 -0
  5. pycontrails/core/airports.py +226 -0
  6. pycontrails/core/cache.py +881 -0
  7. pycontrails/core/coordinates.py +174 -0
  8. pycontrails/core/fleet.py +470 -0
  9. pycontrails/core/flight.py +2314 -0
  10. pycontrails/core/flightplan.py +220 -0
  11. pycontrails/core/fuel.py +140 -0
  12. pycontrails/core/interpolation.py +721 -0
  13. pycontrails/core/met.py +2833 -0
  14. pycontrails/core/met_var.py +307 -0
  15. pycontrails/core/models.py +1181 -0
  16. pycontrails/core/polygon.py +549 -0
  17. pycontrails/core/rgi_cython.cpython-312-darwin.so +0 -0
  18. pycontrails/core/vector.py +2190 -0
  19. pycontrails/datalib/__init__.py +12 -0
  20. pycontrails/datalib/_leo_utils/search.py +250 -0
  21. pycontrails/datalib/_leo_utils/static/bq_roi_query.sql +6 -0
  22. pycontrails/datalib/_leo_utils/vis.py +59 -0
  23. pycontrails/datalib/_met_utils/metsource.py +746 -0
  24. pycontrails/datalib/ecmwf/__init__.py +73 -0
  25. pycontrails/datalib/ecmwf/arco_era5.py +340 -0
  26. pycontrails/datalib/ecmwf/common.py +109 -0
  27. pycontrails/datalib/ecmwf/era5.py +550 -0
  28. pycontrails/datalib/ecmwf/era5_model_level.py +487 -0
  29. pycontrails/datalib/ecmwf/hres.py +782 -0
  30. pycontrails/datalib/ecmwf/hres_model_level.py +459 -0
  31. pycontrails/datalib/ecmwf/ifs.py +284 -0
  32. pycontrails/datalib/ecmwf/model_levels.py +434 -0
  33. pycontrails/datalib/ecmwf/static/model_level_dataframe_v20240418.csv +139 -0
  34. pycontrails/datalib/ecmwf/variables.py +267 -0
  35. pycontrails/datalib/gfs/__init__.py +28 -0
  36. pycontrails/datalib/gfs/gfs.py +646 -0
  37. pycontrails/datalib/gfs/variables.py +100 -0
  38. pycontrails/datalib/goes.py +772 -0
  39. pycontrails/datalib/landsat.py +569 -0
  40. pycontrails/datalib/sentinel.py +511 -0
  41. pycontrails/datalib/spire.py +739 -0
  42. pycontrails/ext/bada.py +41 -0
  43. pycontrails/ext/cirium.py +14 -0
  44. pycontrails/ext/empirical_grid.py +140 -0
  45. pycontrails/ext/synthetic_flight.py +430 -0
  46. pycontrails/models/__init__.py +1 -0
  47. pycontrails/models/accf.py +406 -0
  48. pycontrails/models/apcemm/__init__.py +8 -0
  49. pycontrails/models/apcemm/apcemm.py +982 -0
  50. pycontrails/models/apcemm/inputs.py +226 -0
  51. pycontrails/models/apcemm/static/apcemm_yaml_template.yaml +183 -0
  52. pycontrails/models/apcemm/utils.py +437 -0
  53. pycontrails/models/cocip/__init__.py +29 -0
  54. pycontrails/models/cocip/cocip.py +2616 -0
  55. pycontrails/models/cocip/cocip_params.py +299 -0
  56. pycontrails/models/cocip/cocip_uncertainty.py +285 -0
  57. pycontrails/models/cocip/contrail_properties.py +1517 -0
  58. pycontrails/models/cocip/output_formats.py +2261 -0
  59. pycontrails/models/cocip/radiative_forcing.py +1262 -0
  60. pycontrails/models/cocip/radiative_heating.py +520 -0
  61. pycontrails/models/cocip/unterstrasser_wake_vortex.py +403 -0
  62. pycontrails/models/cocip/wake_vortex.py +396 -0
  63. pycontrails/models/cocip/wind_shear.py +120 -0
  64. pycontrails/models/cocipgrid/__init__.py +9 -0
  65. pycontrails/models/cocipgrid/cocip_grid.py +2573 -0
  66. pycontrails/models/cocipgrid/cocip_grid_params.py +138 -0
  67. pycontrails/models/dry_advection.py +494 -0
  68. pycontrails/models/emissions/__init__.py +21 -0
  69. pycontrails/models/emissions/black_carbon.py +594 -0
  70. pycontrails/models/emissions/emissions.py +1353 -0
  71. pycontrails/models/emissions/ffm2.py +336 -0
  72. pycontrails/models/emissions/static/default-engine-uids.csv +239 -0
  73. pycontrails/models/emissions/static/edb-gaseous-v29b-engines.csv +596 -0
  74. pycontrails/models/emissions/static/edb-nvpm-v29b-engines.csv +215 -0
  75. pycontrails/models/humidity_scaling/__init__.py +37 -0
  76. pycontrails/models/humidity_scaling/humidity_scaling.py +1025 -0
  77. pycontrails/models/humidity_scaling/quantiles/era5-model-level-quantiles.pq +0 -0
  78. pycontrails/models/humidity_scaling/quantiles/era5-pressure-level-quantiles.pq +0 -0
  79. pycontrails/models/issr.py +210 -0
  80. pycontrails/models/pcc.py +327 -0
  81. pycontrails/models/pcr.py +154 -0
  82. pycontrails/models/ps_model/__init__.py +17 -0
  83. pycontrails/models/ps_model/ps_aircraft_params.py +376 -0
  84. pycontrails/models/ps_model/ps_grid.py +505 -0
  85. pycontrails/models/ps_model/ps_model.py +1017 -0
  86. pycontrails/models/ps_model/ps_operational_limits.py +540 -0
  87. pycontrails/models/ps_model/static/ps-aircraft-params-20240524.csv +68 -0
  88. pycontrails/models/ps_model/static/ps-synonym-list-20240524.csv +103 -0
  89. pycontrails/models/sac.py +459 -0
  90. pycontrails/models/tau_cirrus.py +168 -0
  91. pycontrails/physics/__init__.py +1 -0
  92. pycontrails/physics/constants.py +116 -0
  93. pycontrails/physics/geo.py +989 -0
  94. pycontrails/physics/jet.py +837 -0
  95. pycontrails/physics/thermo.py +451 -0
  96. pycontrails/physics/units.py +472 -0
  97. pycontrails/py.typed +0 -0
  98. pycontrails/utils/__init__.py +1 -0
  99. pycontrails/utils/dependencies.py +66 -0
  100. pycontrails/utils/iteration.py +13 -0
  101. pycontrails/utils/json.py +188 -0
  102. pycontrails/utils/temp.py +50 -0
  103. pycontrails/utils/types.py +165 -0
  104. pycontrails-0.54.0.dist-info/LICENSE +178 -0
  105. pycontrails-0.54.0.dist-info/METADATA +179 -0
  106. pycontrails-0.54.0.dist-info/NOTICE +43 -0
  107. pycontrails-0.54.0.dist-info/RECORD +109 -0
  108. pycontrails-0.54.0.dist-info/WHEEL +5 -0
  109. pycontrails-0.54.0.dist-info/top_level.txt +3 -0
@@ -0,0 +1,487 @@
1
+ """Model-level ERA5 data access.
2
+
3
+ This module supports
4
+
5
+ - Retrieving model-level ERA5 data by submitting MARS requests through the Copernicus CDS.
6
+ - Processing retrieved model-level files to produce netCDF files on target pressure levels.
7
+ - Local caching of processed netCDF files.
8
+ - Opening processed and cached files as a :class:`pycontrails.MetDataset` object.
9
+
10
+ Consider using :class:`pycontrails.datalib.ecmwf.ARCOERA5`
11
+ to access model-level data from the nominal ERA5 reanalysis between 1959 and 2022.
12
+ :class:`pycontrails.datalib.ecmwf.ARCOERA5` accesses data through Google's
13
+ `Analysis-Ready, Cloud Optimized ERA5 dataset <https://cloud.google.com/storage/docs/public-datasets/era5>`_
14
+ and has lower latency than this module, which retrieves data from the
15
+ `Copernicus Climate Data Store <https://cds.climate.copernicus.eu/#!/home>`_.
16
+ This module must be used to retrieve model-level data from ERA5 ensemble members
17
+ or for more recent dates.
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ import collections
23
+ import concurrent.futures
24
+ import contextlib
25
+ import hashlib
26
+ import logging
27
+ import os
28
+ import threading
29
+ import warnings
30
+ from datetime import datetime
31
+ from typing import Any
32
+
33
+ from overrides import overrides
34
+
35
+ LOG = logging.getLogger(__name__)
36
+
37
+ import pandas as pd
38
+ import xarray as xr
39
+
40
+ import pycontrails
41
+ from pycontrails.core import cache
42
+ from pycontrails.core.met import MetDataset, MetVariable
43
+ from pycontrails.datalib._met_utils import metsource
44
+ from pycontrails.datalib.ecmwf import model_levels as mlmod
45
+ from pycontrails.datalib.ecmwf.common import ECMWFAPI, CDSCredentialsNotFound
46
+ from pycontrails.datalib.ecmwf.variables import MODEL_LEVEL_VARIABLES
47
+ from pycontrails.utils import dependencies, temp
48
+
49
+ ALL_ENSEMBLE_MEMBERS = list(range(10))
50
+
51
+
52
+ class ERA5ModelLevel(ECMWFAPI):
53
+ """Class to support model-level ERA5 data access, download, and organization.
54
+
55
+ The interface is similar to :class:`pycontrails.datalib.ecmwf.ERA5`, which downloads
56
+ pressure-level with much lower vertical resolution.
57
+
58
+ Requires account with
59
+ `Copernicus Data Portal <https://cds.climate.copernicus.eu/cdsapp#!/home>`_
60
+ and local credentials.
61
+
62
+ API credentials can be stored in a ``~/.cdsapirc`` file
63
+ or as ``CDSAPI_URL`` and ``CDSAPI_KEY`` environment variables.
64
+
65
+ export CDSAPI_URL=...
66
+
67
+ export CDSAPI_KEY=...
68
+
69
+ Credentials can also be provided directly ``url`` and ``key`` keyword args.
70
+
71
+ See `cdsapi <https://github.com/ecmwf/cdsapi>`_ documentation
72
+ for more information.
73
+
74
+ Parameters
75
+ ----------
76
+ time : metsource.TimeInput | None
77
+ The time range for data retrieval, either a single datetime or (start, end) datetime range.
78
+ Input must be datetime-like or tuple of datetime-like
79
+ (:py:class:`datetime.datetime`, :class:`pandas.Timestamp`, :class:`numpy.datetime64`)
80
+ specifying the (start, end) of the date range, inclusive.
81
+ NetCDF files will be downloaded from CDS in chunks no larger than 1 month
82
+ for the nominal reanalysis and no larger than 1 day for ensemble members.
83
+ This ensures that exactly one request is submitted per file on tape accessed.
84
+ If None, ``paths`` must be defined and all time coordinates will be loaded from files.
85
+ variables : metsource.VariableInput
86
+ Variable name (i.e. "t", "air_temperature", ["air_temperature, specific_humidity"])
87
+ pressure_levels : metsource.PressureLevelInput, optional
88
+ Pressure levels for data, in hPa (mbar).
89
+ To download surface-level parameters, use :class:`pycontrails.datalib.ecmwf.ERA5`.
90
+ Defaults to pressure levels that match model levels at a nominal surface pressure.
91
+ timestep_freq : str, optional
92
+ Manually set the timestep interval within the bounds defined by :attr:`time`.
93
+ Supports any string that can be passed to ``pd.date_range(freq=...)``.
94
+ By default, this is set to "1h" for reanalysis products and "3h" for ensemble products.
95
+ product_type : str, optional
96
+ Product type, one of "reanalysis" and "ensemble_members". Unlike
97
+ :class:`pycontrails.datalib.ecmwf.ERA5`, this class does not support direct access to the
98
+ ensemble mean and spread, which are not available on model levels.
99
+ grid : float, optional
100
+ Specify latitude/longitude grid spacing in data.
101
+ By default, this is set to 0.25 for reanalysis products and 0.5 for ensemble products.
102
+ model_levels : list[int], optional
103
+ Specify ECMWF model levels to include in MARS requests.
104
+ By default, this is set to include all model levels.
105
+ ensemble_members : list[int], optional
106
+ Specify ensemble members to include.
107
+ Valid only when the product type is "ensemble_members".
108
+ By default, includes every available ensemble member.
109
+ cachestore : cache.CacheStore | None, optional
110
+ Cache data store for staging processed netCDF files.
111
+ Defaults to :class:`pycontrails.core.cache.DiskCacheStore`.
112
+ If None, cache is turned off.
113
+ cache_download: bool, optional
114
+ If True, cache downloaded model-level files rather than storing them in a temporary file.
115
+ By default, False.
116
+ url : str | None
117
+ Override the default `cdsapi <https://github.com/ecmwf/cdsapi>`_ url.
118
+ As of August 2024, the url for the `CDS-Beta <https://cds-beta.climate.copernicus.eu>`_
119
+ is "https://cds-beta.climate.copernicus.eu/api", and the url for the legacy server is
120
+ "https://cds.climate.copernicus.eu/api/v2". If None, the url is set
121
+ by the ``CDSAPI_URL`` environment variable. If this is not defined, the
122
+ ``cdsapi`` package will determine the url.
123
+ key : str | None
124
+ Override default `cdsapi <https://github.com/ecmwf/cdsapi>`_ key. If None,
125
+ the key is set by the ``CDSAPI_KEY`` environment variable. If this is not defined,
126
+ the ``cdsapi`` package will determine the key.
127
+ """
128
+
129
+ __marker = object()
130
+
131
+ def __init__(
132
+ self,
133
+ time: metsource.TimeInput,
134
+ variables: metsource.VariableInput,
135
+ *,
136
+ pressure_levels: metsource.PressureLevelInput | None = None,
137
+ timestep_freq: str | None = None,
138
+ product_type: str = "reanalysis",
139
+ grid: float | None = None,
140
+ model_levels: list[int] | None = None,
141
+ ensemble_members: list[int] | None = None,
142
+ cachestore: cache.CacheStore = __marker, # type: ignore[assignment]
143
+ cache_download: bool = False,
144
+ url: str | None = None,
145
+ key: str | None = None,
146
+ ) -> None:
147
+ self.cachestore = cache.DiskCacheStore() if cachestore is self.__marker else cachestore
148
+ self.cache_download = cache_download
149
+
150
+ self.paths = None
151
+
152
+ self.url = url or os.getenv("CDSAPI_URL")
153
+ self.key = key or os.getenv("CDSAPI_KEY")
154
+
155
+ supported = ("reanalysis", "ensemble_members")
156
+ if product_type not in supported:
157
+ msg = (
158
+ f"Unknown product_type {product_type}. "
159
+ f"Currently support product types: {', '.join(supported)}"
160
+ )
161
+ raise ValueError(msg)
162
+ self.product_type = product_type
163
+
164
+ if product_type != "ensemble_members" and ensemble_members:
165
+ msg = "No ensemble members available for reanalysis product type."
166
+ raise ValueError(msg)
167
+ if product_type == "ensemble_members" and not ensemble_members:
168
+ ensemble_members = ALL_ENSEMBLE_MEMBERS
169
+ self.ensemble_members = ensemble_members
170
+
171
+ if grid is None:
172
+ grid = 0.25 if product_type == "reanalysis" else 0.5
173
+ else:
174
+ grid_min = 0.25 if product_type == "reanalysis" else 0.5
175
+ if grid < grid_min:
176
+ msg = (
177
+ f"The highest resolution available is {grid_min} degrees. "
178
+ f"Your downloaded data will have resolution {grid}, but it is a "
179
+ f"reinterpolation of the {grid_min} degree data. The same interpolation can be "
180
+ "achieved directly with xarray."
181
+ )
182
+ warnings.warn(msg)
183
+ self.grid = grid
184
+
185
+ if model_levels is None:
186
+ model_levels = list(range(1, 138))
187
+ elif min(model_levels) < 1 or max(model_levels) > 137:
188
+ msg = "Retrieval model_levels must be between 1 and 137, inclusive."
189
+ raise ValueError(msg)
190
+ self.model_levels = model_levels
191
+
192
+ datasource_timestep_freq = "1h" if product_type == "reanalysis" else "3h"
193
+ if timestep_freq is None:
194
+ timestep_freq = datasource_timestep_freq
195
+ if not metsource.validate_timestep_freq(timestep_freq, datasource_timestep_freq):
196
+ msg = (
197
+ f"Product {self.product_type} has timestep frequency of {datasource_timestep_freq} "
198
+ f"and cannot support requested timestep frequency of {timestep_freq}."
199
+ )
200
+ raise ValueError(msg)
201
+
202
+ self.timesteps = metsource.parse_timesteps(time, freq=timestep_freq)
203
+ if pressure_levels is None:
204
+ pressure_levels = mlmod.model_level_reference_pressure(20_000.0, 50_000.0)
205
+ self.pressure_levels = metsource.parse_pressure_levels(pressure_levels)
206
+ self.variables = metsource.parse_variables(variables, self.pressure_level_variables)
207
+
208
+ def __repr__(self) -> str:
209
+ base = super().__repr__()
210
+ return f"{base}\n\tDataset: {self.dataset}\n\tProduct type: {self.product_type}"
211
+
212
+ @property
213
+ def pressure_level_variables(self) -> list[MetVariable]:
214
+ """ECMWF pressure level parameters available on model levels.
215
+
216
+ Returns
217
+ -------
218
+ list[MetVariable]
219
+ List of MetVariable available in datasource
220
+ """
221
+ return MODEL_LEVEL_VARIABLES
222
+
223
+ @property
224
+ def single_level_variables(self) -> list[MetVariable]:
225
+ """ECMWF single-level parameters available on model levels.
226
+
227
+ Returns
228
+ -------
229
+ list[MetVariable]
230
+ Always returns an empty list.
231
+ To access single-level variables, used :class:`pycontrails.datalib.ecmwf.ERA5`.
232
+ """
233
+ return []
234
+
235
+ @property
236
+ def dataset(self) -> str:
237
+ """Select dataset for downloading model-level data.
238
+
239
+ Always returns "reanalysis-era5-complete".
240
+
241
+ Returns
242
+ -------
243
+ str
244
+ Model-level ERA5 dataset name in CDS
245
+ """
246
+ return "reanalysis-era5-complete"
247
+
248
+ @overrides
249
+ def create_cachepath(self, t: datetime | pd.Timestamp) -> str:
250
+ """Return cachepath to local ERA5 data file based on datetime.
251
+
252
+ This uniquely defines a cached data file with class parameters.
253
+
254
+ Parameters
255
+ ----------
256
+ t : datetime | pd.Timestamp
257
+ Datetime of datafile
258
+
259
+ Returns
260
+ -------
261
+ str
262
+ Path to local ERA5 data file
263
+ """
264
+ if self.cachestore is None:
265
+ msg = "Cachestore is required to create cache path"
266
+ raise ValueError(msg)
267
+
268
+ string = (
269
+ f"{t:%Y%m%d%H}-"
270
+ f"{'.'.join(str(p) for p in self.pressure_levels)}-"
271
+ f"{'.'.join(sorted(self.variable_shortnames))}-"
272
+ f"{self.grid}"
273
+ )
274
+
275
+ name = hashlib.md5(string.encode()).hexdigest()
276
+ cache_path = f"era5ml-{name}.nc"
277
+
278
+ return self.cachestore.path(cache_path)
279
+
280
+ @overrides
281
+ def download_dataset(self, times: list[datetime]) -> None:
282
+ # group data to request by month (nominal) or by day (ensemble)
283
+ requests: dict[datetime, list[datetime]] = collections.defaultdict(list)
284
+ for t in times:
285
+ request = (
286
+ datetime(t.year, t.month, 1)
287
+ if self.product_type == "reanalysis"
288
+ else datetime(t.year, t.month, t.day)
289
+ )
290
+ requests[request].append(t)
291
+
292
+ # retrieve and process data for each request
293
+ LOG.debug(f"Retrieving ERA5 ML data for times {times} in {len(requests)} request(s)")
294
+ for times_in_request in requests.values():
295
+ self._download_convert_cache_handler(times_in_request)
296
+
297
+ @overrides
298
+ def open_metdataset(
299
+ self,
300
+ dataset: xr.Dataset | None = None,
301
+ xr_kwargs: dict[str, Any] | None = None,
302
+ **kwargs: Any,
303
+ ) -> MetDataset:
304
+ if dataset:
305
+ msg = "Parameter 'dataset' is not supported for Model-level ERA5 data"
306
+ raise ValueError(msg)
307
+
308
+ if self.cachestore is None:
309
+ msg = "Cachestore is required to download data"
310
+ raise ValueError(msg)
311
+
312
+ xr_kwargs = xr_kwargs or {}
313
+ self.download(**xr_kwargs)
314
+
315
+ disk_cachepaths = [self.cachestore.get(f) for f in self._cachepaths]
316
+ ds = self.open_dataset(disk_cachepaths, **xr_kwargs)
317
+
318
+ mds = self._process_dataset(ds, **kwargs)
319
+
320
+ self.set_metadata(mds)
321
+ return mds
322
+
323
+ @overrides
324
+ def set_metadata(self, ds: xr.Dataset | MetDataset) -> None:
325
+ if self.product_type == "reanalysis":
326
+ product = "reanalysis"
327
+ elif self.product_type == "ensemble_members":
328
+ product = "ensemble"
329
+ else:
330
+ msg = f"Unknown product type {self.product_type}"
331
+ raise ValueError(msg)
332
+
333
+ ds.attrs.update(
334
+ provider="ECMWF",
335
+ dataset="ERA5",
336
+ product=product,
337
+ )
338
+
339
+ def _mars_request_base(self, times: list[datetime]) -> dict[str, str]:
340
+ unique_dates = {t.strftime("%Y-%m-%d") for t in times}
341
+ unique_times = {t.strftime("%H:%M:%S") for t in times}
342
+
343
+ common = {
344
+ "class": "ea",
345
+ "date": "/".join(sorted(unique_dates)),
346
+ "expver": "1",
347
+ "levtype": "ml",
348
+ "time": "/".join(sorted(unique_times)),
349
+ "type": "an",
350
+ "grid": f"{self.grid}/{self.grid}",
351
+ "format": "netcdf",
352
+ }
353
+
354
+ if self.product_type == "reanalysis":
355
+ specific = {"stream": "oper"}
356
+ elif self.product_type == "ensemble_members":
357
+ if self.ensemble_members is None:
358
+ msg = "No ensemble members specified for ensemble product type."
359
+ raise ValueError(msg)
360
+ specific = {"stream": "enda", "number": "/".join(str(n) for n in self.ensemble_members)}
361
+
362
+ return common | specific
363
+
364
+ def _mars_request_lnsp(self, times: list[datetime]) -> dict[str, str]:
365
+ out = self._mars_request_base(times)
366
+ out["param"] = "152" # lnsp, needed for model level -> pressure level conversion
367
+ out["levelist"] = "1"
368
+ return out
369
+
370
+ def mars_request(self, times: list[datetime]) -> dict[str, str]:
371
+ """Generate MARS request for specific list of times.
372
+
373
+ Parameters
374
+ ----------
375
+ times : list[datetime]
376
+ Times included in MARS request.
377
+
378
+ Returns
379
+ -------
380
+ dict[str, str]:
381
+ MARS request for submission to Copernicus CDS.
382
+ """
383
+
384
+ out = self._mars_request_base(times)
385
+ out["param"] = "/".join(str(p) for p in sorted(set(self.variable_ecmwfids)))
386
+ out["levelist"] = "/".join(str(lev) for lev in sorted(self.model_levels))
387
+ return out
388
+
389
+ def _set_cds(self) -> None:
390
+ """Set the cdsapi.Client instance."""
391
+ try:
392
+ import cdsapi
393
+ except ModuleNotFoundError as e:
394
+ dependencies.raise_module_not_found_error(
395
+ name="ERA5ModelLevel._set_cds method",
396
+ package_name="cdsapi",
397
+ module_not_found_error=e,
398
+ pycontrails_optional_package="ecmwf",
399
+ )
400
+
401
+ try:
402
+ self.cds = cdsapi.Client(url=self.url, key=self.key)
403
+ # cdsapi throws base-level Exception
404
+ except Exception as err:
405
+ raise CDSCredentialsNotFound from err
406
+
407
+ def _download_convert_cache_handler(self, times: list[datetime]) -> None:
408
+ """Download, convert, and cache ERA5 model level data.
409
+
410
+ This function builds a MARS request and retrieves a single NetCDF file.
411
+ The calling function should ensure that all times will be contained
412
+ in a single file on tape in the MARS archive.
413
+
414
+ Because MARS requests treat dates and times as separate dimensions,
415
+ retrieved data will include the Cartesian product of all unique
416
+ dates and times in the list of specified times.
417
+
418
+ After retrieval, this function processes the NetCDF file
419
+ to produce the dataset specified by class attributes.
420
+
421
+ Parameters
422
+ ----------
423
+ times : list[datetime]
424
+ Times to download in a single MARS request.
425
+ """
426
+ if self.cachestore is None:
427
+ msg = "Cachestore is required to download and cache data"
428
+ raise ValueError(msg)
429
+
430
+ ml_request = self.mars_request(times)
431
+ lnsp_request = self._mars_request_lnsp(times)
432
+
433
+ stack = contextlib.ExitStack()
434
+ if not self.cache_download:
435
+ ml_target = stack.enter_context(temp.temp_file())
436
+ lnsp_target = stack.enter_context(temp.temp_file())
437
+ else:
438
+ ml_target = _target_path(ml_request, self.cachestore)
439
+ lnsp_target = _target_path(lnsp_request, self.cachestore)
440
+
441
+ with stack:
442
+ threads = []
443
+ for request, target in ((ml_request, ml_target), (lnsp_request, lnsp_target)):
444
+ if not self.cache_download or not self.cachestore.exists(target):
445
+ if not hasattr(self, "cds"):
446
+ self._set_cds()
447
+ threads.append(
448
+ threading.Thread(
449
+ target=self.cds.retrieve,
450
+ args=("reanalysis-era5-complete", request, target),
451
+ )
452
+ )
453
+
454
+ # Download across two threads
455
+ with concurrent.futures.ThreadPoolExecutor() as executor:
456
+ for thread in threads:
457
+ executor.submit(thread.run)
458
+
459
+ LOG.debug("Opening model level data file")
460
+
461
+ ds_ml = xr.open_dataset(ml_target)
462
+ lnsp = xr.open_dataarray(lnsp_target)
463
+
464
+ # New CDS-Beta gives "valid_time" instead of "time"
465
+ if "valid_time" in ds_ml:
466
+ ds_ml = ds_ml.rename(valid_time="time")
467
+ if "valid_time" in lnsp.dims:
468
+ lnsp = lnsp.rename(valid_time="time")
469
+
470
+ # The legacy CDS gives "level" instead of "model_level"
471
+ if "level" in ds_ml.dims:
472
+ ds_ml = ds_ml.rename(level="model_level")
473
+
474
+ # Use a chunking scheme harmonious with self.cache_dataset, which groups by time
475
+ # Because ds_ml is dask-backed, nothing gets computed until cache_dataset is called
476
+ ds_ml = ds_ml.chunk(time=1)
477
+ lnsp = lnsp.chunk(time=1)
478
+
479
+ ds = mlmod.ml_to_pl(ds_ml, target_pl=self.pressure_levels, lnsp=lnsp)
480
+ ds.attrs["pycontrails_version"] = pycontrails.__version__
481
+ self.cache_dataset(ds)
482
+
483
+
484
+ def _target_path(request: dict[str, str], cachestore: cache.CacheStore) -> str:
485
+ request_str = ";".join(f"{p}:{request[p]}" for p in sorted(request))
486
+ name = hashlib.md5(request_str.encode()).hexdigest()
487
+ return cachestore.path(f"era5ml-{name}-raw.nc")