|
8 | 8 | import logging |
9 | 9 | from functools import cached_property |
10 | 10 |
|
11 | | -import climetlab as cml |
| 11 | +import earthkit.data as ekd |
| 12 | +import earthkit.regrid as ekr |
12 | 13 | import entrypoints |
| 14 | +from earthkit.data.indexing.fieldlist import FieldArray |
13 | 15 |
|
14 | 16 | LOG = logging.getLogger(__name__) |
15 | 17 |
|
16 | 18 |
|
17 | | -class RequestBasedInput: |
18 | | - def __init__(self, owner, **kwargs): |
19 | | - self.owner = owner |
20 | | - |
21 | | - def _patch(self, **kargs): |
22 | | - r = dict(**kargs) |
23 | | - self.owner.patch_retrieve_request(r) |
24 | | - return r |
25 | | - |
26 | | - @cached_property |
27 | | - def fields_sfc(self): |
28 | | - param = self.owner.param_sfc |
29 | | - if not param: |
30 | | - return cml.load_source("empty") |
31 | | - |
32 | | - LOG.info(f"Loading surface fields from {self.WHERE}") |
33 | | - return cml.load_source( |
34 | | - "multi", |
35 | | - [ |
36 | | - self.sfc_load_source( |
37 | | - **self._patch( |
38 | | - date=date, |
39 | | - time=time, |
40 | | - param=param, |
41 | | - grid=self.owner.grid, |
42 | | - area=self.owner.area, |
43 | | - **self.owner.retrieve, |
44 | | - ) |
45 | | - ) |
46 | | - for date, time in self.owner.datetimes() |
47 | | - ], |
48 | | - ) |
49 | | - |
50 | | - @cached_property |
51 | | - def fields_pl(self): |
52 | | - param, level = self.owner.param_level_pl |
53 | | - if not (param and level): |
54 | | - return cml.load_source("empty") |
55 | | - |
56 | | - LOG.info(f"Loading pressure fields from {self.WHERE}") |
57 | | - return cml.load_source( |
58 | | - "multi", |
59 | | - [ |
60 | | - self.pl_load_source( |
61 | | - **self._patch( |
62 | | - date=date, |
63 | | - time=time, |
64 | | - param=param, |
65 | | - level=level, |
66 | | - grid=self.owner.grid, |
67 | | - area=self.owner.area, |
68 | | - ) |
69 | | - ) |
70 | | - for date, time in self.owner.datetimes() |
71 | | - ], |
72 | | - ) |
73 | | - |
74 | | - @cached_property |
75 | | - def fields_ml(self): |
76 | | - param, level = self.owner.param_level_ml |
77 | | - if not (param and level): |
78 | | - return cml.load_source("empty") |
79 | | - |
80 | | - LOG.info(f"Loading model fields from {self.WHERE}") |
81 | | - return cml.load_source( |
82 | | - "multi", |
83 | | - [ |
84 | | - self.ml_load_source( |
85 | | - **self._patch( |
86 | | - date=date, |
87 | | - time=time, |
88 | | - param=param, |
89 | | - level=level, |
90 | | - grid=self.owner.grid, |
91 | | - area=self.owner.area, |
92 | | - ) |
93 | | - ) |
94 | | - for date, time in self.owner.datetimes() |
95 | | - ], |
96 | | - ) |
97 | | - |
98 | | - @cached_property |
99 | | - def all_fields(self): |
100 | | - return self.fields_sfc + self.fields_pl + self.fields_ml |
101 | | - |
102 | | - |
103 | | -class MarsInput(RequestBasedInput): |
104 | | - WHERE = "MARS" |
105 | | - |
106 | | - def __init__(self, owner, **kwargs): |
107 | | - self.owner = owner |
108 | | - |
109 | | - def pl_load_source(self, **kwargs): |
110 | | - kwargs["levtype"] = "pl" |
111 | | - logging.debug("load source mars %s", kwargs) |
112 | | - return cml.load_source("mars", kwargs) |
113 | | - |
114 | | - def sfc_load_source(self, **kwargs): |
115 | | - kwargs["levtype"] = "sfc" |
116 | | - logging.debug("load source mars %s", kwargs) |
117 | | - return cml.load_source("mars", kwargs) |
118 | | - |
119 | | - def ml_load_source(self, **kwargs): |
120 | | - kwargs["levtype"] = "ml" |
121 | | - logging.debug("load source mars %s", kwargs) |
122 | | - return cml.load_source("mars", kwargs) |
123 | | - |
124 | | - |
125 | | -class CdsInput(RequestBasedInput): |
126 | | - WHERE = "CDS" |
127 | | - |
128 | | - def pl_load_source(self, **kwargs): |
129 | | - kwargs["product_type"] = "reanalysis" |
130 | | - return cml.load_source("cds", "reanalysis-era5-pressure-levels", kwargs) |
131 | | - |
132 | | - def sfc_load_source(self, **kwargs): |
133 | | - kwargs["product_type"] = "reanalysis" |
134 | | - return cml.load_source("cds", "reanalysis-era5-single-levels", kwargs) |
135 | | - |
136 | | - def ml_load_source(self, **kwargs): |
137 | | - raise NotImplementedError("CDS does not support model levels") |
138 | | - |
139 | | - |
140 | | -class OpenDataInput(RequestBasedInput): |
141 | | - WHERE = "OPENDATA" |
142 | | - |
143 | | - RESOLS = {(0.25, 0.25): "0p25"} |
144 | | - |
145 | | - def __init__(self, owner, **kwargs): |
146 | | - self.owner = owner |
147 | | - |
148 | | - def _adjust(self, kwargs): |
149 | | - if "level" in kwargs: |
150 | | - # OpenData uses levelist instead of level |
151 | | - kwargs["levelist"] = kwargs.pop("level") |
152 | | - |
153 | | - grid = kwargs.pop("grid") |
154 | | - if isinstance(grid, list): |
155 | | - grid = tuple(grid) |
156 | | - |
157 | | - kwargs["resol"] = self.RESOLS[grid] |
158 | | - r = dict(**kwargs) |
159 | | - r.update(self.owner.retrieve) |
160 | | - return r |
161 | | - |
162 | | - def pl_load_source(self, **kwargs): |
163 | | - self._adjust(kwargs) |
164 | | - kwargs["levtype"] = "pl" |
165 | | - logging.debug("load source ecmwf-open-data %s", kwargs) |
166 | | - return cml.load_source("ecmwf-open-data", **kwargs) |
167 | | - |
168 | | - def sfc_load_source(self, **kwargs): |
169 | | - self._adjust(kwargs) |
170 | | - kwargs["levtype"] = "sfc" |
171 | | - logging.debug("load source ecmwf-open-data %s", kwargs) |
172 | | - return cml.load_source("ecmwf-open-data", **kwargs) |
173 | | - |
174 | | - def ml_load_source(self, **kwargs): |
175 | | - self._adjust(kwargs) |
176 | | - kwargs["levtype"] = "ml" |
177 | | - logging.debug("load source ecmwf-open-data %s", kwargs) |
178 | | - return cml.load_source("ecmwf-open-data", **kwargs) |
179 | | - |
180 | | - |
181 | | -class FileInput: |
182 | | - def __init__(self, owner, file, **kwargs): |
183 | | - self.file = file |
184 | | - self.owner = owner |
185 | | - |
186 | | - @cached_property |
187 | | - def fields_sfc(self): |
188 | | - return self.all_fields.sel(levtype="sfc") |
189 | | - |
190 | | - @cached_property |
191 | | - def fields_pl(self): |
192 | | - return self.all_fields.sel(levtype="pl") |
193 | | - |
194 | | - @cached_property |
195 | | - def fields_ml(self): |
196 | | - return self.all_fields.sel(levtype="ml") |
197 | | - |
198 | | - @cached_property |
199 | | - def all_fields(self): |
200 | | - return cml.load_source("file", self.file) |
201 | | - |
202 | | - |
203 | 19 | def get_input(name, *args, **kwargs): |
204 | 20 | return available_inputs()[name].load()(*args, **kwargs) |
205 | 21 |
|
|
0 commit comments