@@ -83,6 +83,7 @@ def convert_input_to_xarray_dataset(
83
83
precip : np .ndarray ,
84
84
quality : np .ndarray | None ,
85
85
metadata : dict [str , str | float | None ],
86
+ startdate : datetime | None = None ,
86
87
) -> xr .Dataset :
87
88
"""
88
89
Read a precip, quality, metadata tuple as returned by the importers
@@ -99,6 +100,8 @@ def convert_input_to_xarray_dataset(
99
100
metadata: dict
100
101
Metadata dictionary containing the attributes described in the
101
102
documentation of :py:mod:`pysteps.io.importers`.
103
+ startdate: datetime, None
104
+ Datetime object containing the start date and time for the nowcast
102
105
103
106
Returns
104
107
-------
@@ -107,7 +110,31 @@ def convert_input_to_xarray_dataset(
107
110
108
111
"""
109
112
var_name , attrs = cf_parameters_from_unit (metadata ["unit" ])
110
- h , w = precip .shape
113
+
114
+ dims = None
115
+ timesteps = None
116
+ ens_number = None
117
+
118
+ if precip .ndim == 4 :
119
+ ens_number , timesteps , h , w = precip .shape
120
+ dims = ["ens_number" , "time" , "y" , "x" ]
121
+
122
+ if startdate is None :
123
+ raise Exception ("startdate missing" )
124
+
125
+ elif precip .ndim == 3 :
126
+ timesteps , h , w = precip .shape
127
+ dims = ["time" , "y" , "x" ]
128
+
129
+ if startdate is None :
130
+ raise Exception ("startdate missing" )
131
+
132
+ elif precip .ndim == 2 :
133
+ h , w = precip .shape
134
+ dims = ["y" , "x" ]
135
+ else :
136
+ raise Exception (f"Precip field shape: { precip .shape } not supported" )
137
+
111
138
x_r = np .linspace (metadata ["x1" ], metadata ["x2" ], w + 1 )[:- 1 ]
112
139
x_r += 0.5 * (x_r [1 ] - x_r [0 ])
113
140
y_r = np .linspace (metadata ["y1" ], metadata ["y2" ], h + 1 )[:- 1 ]
@@ -142,25 +169,33 @@ def convert_input_to_xarray_dataset(
142
169
143
170
data_vars = {
144
171
var_name : (
145
- [ "y" , "x" ] ,
172
+ dims ,
146
173
precip ,
147
174
{
148
175
"units" : attrs ["units" ],
149
176
"standard_name" : attrs ["standard_name" ],
150
177
"long_name" : attrs ["long_name" ],
151
178
"grid_mapping" : "projection" ,
152
- "transform" : metadata ["transform" ],
153
- "accutime" : metadata ["accutime" ],
154
- "threshold" : metadata ["threshold" ],
155
- "zerovalue" : metadata ["zerovalue" ],
156
- "zr_a" : metadata ["zr_a" ],
157
- "zr_b" : metadata ["zr_b" ],
158
179
},
159
180
)
160
181
}
182
+
183
+ metadata_keys = [
184
+ "transform" ,
185
+ "accutime" ,
186
+ "threshold" ,
187
+ "zerovalue" ,
188
+ "zr_a" ,
189
+ "zr_b" ,
190
+ ]
191
+
192
+ for metadata_field in metadata_keys :
193
+ if metadata_field in metadata :
194
+ data_vars [var_name ][2 ][metadata_field ] = metadata [metadata_field ]
195
+
161
196
if quality is not None :
162
197
data_vars ["quality" ] = (
163
- [ "y" , "x" ] ,
198
+ dims ,
164
199
quality ,
165
200
{
166
201
"units" : "1" ,
@@ -210,6 +245,26 @@ def convert_input_to_xarray_dataset(
210
245
},
211
246
),
212
247
}
248
+
249
+ if ens_number is not None :
250
+ coords ["ens_number" ] = (
251
+ ["ens_number" ],
252
+ list (range (1 , ens_number + 1 , 1 )),
253
+ {
254
+ "long_name" : "ensemble member" ,
255
+ "standard_name" : "realization" ,
256
+ "units" : "" ,
257
+ },
258
+ )
259
+
260
+ if timesteps is not None :
261
+ startdate_str = datetime .strftime (startdate , "%Y-%m-%d %H:%M:%S" )
262
+
263
+ coords ["time" ] = (
264
+ ["time" ],
265
+ list (range (1 , timesteps + 1 , 1 )),
266
+ {"long_name" : "forecast time" , "units" : "seconds since %s" % startdate_str },
267
+ )
213
268
if grid_mapping_var_name is not None :
214
269
coords [grid_mapping_name ] = (
215
270
[],
@@ -223,7 +278,7 @@ def convert_input_to_xarray_dataset(
223
278
"precip_var" : var_name ,
224
279
}
225
280
dataset = xr .Dataset (data_vars = data_vars , coords = coords , attrs = attrs )
226
- return dataset .sortby ([ "y" , "x" ] )
281
+ return dataset .sortby (dims )
227
282
228
283
229
284
def convert_output_to_xarray_dataset (
0 commit comments