74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483 | class LamahIce(HydroDataset):
"""LamaHICE dataset class extending HydroDataset.
This class uses a custom data reading implementation to support a newer
dataset version than the one supported by the underlying aquafetch library.
It overrides the download URLs and provides updated methods.
Attributes:
region: Geographic region identifier
download: Whether to download data automatically
"""
def __init__(
self, uri: str, region: Optional[str] = None, download: bool = False
) -> None:
"""Initialize LamaHICE dataset.
Args:
uri: Path to the data directory
region: Geographic region identifier (optional)
download: Whether to download data automatically (default: False)
"""
super().__init__(uri)
self.region = region
self.download = download
# cloud path: aqua_fetch cannot read S3, use cache_*_to_zarr instead
if str(uri).startswith("s3://"):
return
# Use the custom LamaHIce class defined at module level
self.aqua_fetch = LamaHIce(uri)
# OSS relative paths (timestep=D, data_type=total_upstrm)
_P = "LamaHIce/lamah_ice/lamah_ice"
_BASINS_REL = f"{_P}/A_basins_total_upstrm"
_CATCH_ATTR_REL = f"{_P}/A_basins_total_upstrm/1_attributes"
_METEO_REL = f"{_P}/A_basins_total_upstrm/2_timeseries/daily/meteorological_data"
_GAUGE_ATTR_REL = f"{_P}/D_gauges/1_attributes"
_Q_REL = f"{_P}/D_gauges/2_timeseries/daily"
# AquaFetch LamaHIce.static_map
_STATIC_RENAME = {
"area_calc_basin": "area_km2",
"lat_gauge": "lat",
"slope_mean_basin": "slope_mkm-1",
"lon_gauge": "long",
}
# AquaFetch LamaHIce.dyn_map['D'] resolved to cleaned names
_DYN_RENAME = {
"qobs": "q_cms_obs",
"2m_temp_min": "airtemp_c_2m_min",
"2m_temp_max": "airtemp_c_2m_max",
"2m_temp_mean": "airtemp_c_mean_2m",
"prec": "pcp_mm",
"pet": "pet_mm",
"ref_et_rav": "ref_et_mm",
}
def read_object_ids(self) -> np.ndarray:
if self._is_cloud():
fs = self._make_s3fs()
uri = str(self.data_source_dir).rstrip("/")
names = [p.split("/")[-1] for p in fs.ls(f"{uri}/{self._METEO_REL}".removeprefix("s3://"))]
ids = sorted(
(n.split(".")[0].split("_")[1] for n in names if n.startswith("ID_")),
key=lambda x: int(x),
)
return np.array(ids)
return super().read_object_ids()
def cache_attributes_to_zarr(self) -> None:
import zarr
fs = self._make_s3fs()
uri = str(self.data_source_dir).rstrip("/")
def _read(rel, fname):
with fs.open(f"{uri}/{rel}/{fname}".removeprefix("s3://")) as fh:
df = pd.read_csv(fh, sep=";", index_col="id")
df.index = df.index.astype(str)
return df
# basin attributes = catchment + water_balance(_all) + unfiltered(_unfiltered),
# all then suffixed with _basin
cat = _read(self._CATCH_ATTR_REL, "Catchment_attributes.csv")
wb = _read(self._CATCH_ATTR_REL, "water_balance.csv")
wb.columns = [c + "_all" for c in wb.columns]
wbu = _read(self._CATCH_ATTR_REL, "water_balance_unfiltered.csv")
wbu.columns = [c + "_unfiltered" for c in wbu.columns]
basin = pd.concat([cat, wb, wbu], axis=1)
basin.columns = [c + "_basin" for c in basin.columns]
# gauge attributes = Gauge_attributes + hydro_indices
g = _read(self._GAUGE_ATTR_REL, "Gauge_attributes.csv")
hidx = _read(self._GAUGE_ATTR_REL, "hydro_indices_1981_2018.csv")
gauge = pd.concat([g, hidx], axis=1)
static = pd.concat([basin, gauge], axis=1)
static = static.loc[~static.index.duplicated(keep="first")]
static = static.rename(columns=self._STATIC_RENAME)
static.columns = self._clean_feature_names(list(static.columns))
static = static.loc[:, ~static.columns.duplicated(keep="first")]
zarr_name = self._attributes_cache_filename.replace(".nc", ".zarr")
out, opts = self._zarr_path_and_opts(zarr_name)
ids = static.index.tolist()
n = len(ids)
root = zarr.open_group(out, mode="w", storage_options=opts, zarr_format=2)
for col in static.columns:
vals = static[col].values.astype(str) if static[col].dtype == object else static[col].values
arr = root.create_array(col, shape=(n,), chunks=(n,), dtype=vals.dtype)
arr[:] = vals
arr.attrs["_ARRAY_DIMENSIONS"] = ["basin"]
basin_arr = root.create_array("basin", shape=(n,), chunks=(n,), dtype=str)
basin_arr[:] = ids
basin_arr.attrs["_ARRAY_DIMENSIONS"] = ["basin"]
root.attrs["coordinates"] = "basin"
self._write_zarr_units(root, "static")
print(f"Attributes zarr written to: {out}")
def cache_timeseries_to_zarr(self) -> None:
import zarr
fs = self._make_s3fs()
uri = str(self.data_source_dir).rstrip("/")
meteo_base = f"{uri}/{self._METEO_REL}"
q_base = f"{uri}/{self._Q_REL}"
stations = self.read_object_ids().tolist()
all_times = pd.date_range(self.default_t_range[0], self.default_t_range[1], freq="D")
n, nt = len(stations), len(all_times)
times_ns = all_times.asi8
cleaned_var_lst = []
for info in self._dynamic_variable_mapping.values():
for s in info["sources"].values():
if s["specific_name"] not in cleaned_var_lst:
cleaned_var_lst.append(s["specific_name"])
def _read_dated(path):
with fs.open(path.removeprefix("s3://")) as fh:
df = pd.read_csv(fh, sep=";")
idx = pd.to_datetime(dict(year=df["YYYY"], month=df["MM"], day=df["DD"]))
df = df.drop(columns=[c for c in ("YYYY", "MM", "DD", "DOY") if c in df.columns])
df.index = idx
return df
data = {vn: np.full((n, nt), np.nan) for vn in cleaned_var_lst}
for i, stn in enumerate(tqdm(stations, desc="lamah_ice")):
parts = []
try:
parts.append(_read_dated(f"{meteo_base}/ID_{stn}.csv"))
except Exception as e:
print(f" WARN meteo {stn}: {e}")
try:
parts.append(_read_dated(f"{q_base}/ID_{stn}.csv"))
except Exception:
pass
if not parts:
continue
df = pd.concat(parts, axis=1)
df = df.loc[~df.index.duplicated(keep="first")]
df.columns = self._clean_feature_names(
[self._DYN_RENAME.get(c, c) for c in df.columns]
)
df = df.reindex(all_times)
for vn in cleaned_var_lst:
if vn in df.columns:
data[vn][i] = pd.to_numeric(df[vn], errors="coerce").values
zarr_name = self._timeseries_cache_filename.replace(".nc", ".zarr")
out, opts = self._zarr_path_and_opts(zarr_name)
chunk_t = min(nt, 365)
root = zarr.open_group(out, mode="w", storage_options=opts, zarr_format=2)
for vn in cleaned_var_lst:
arr = root.create_array(vn, shape=(n, nt), chunks=(min(n, 100), chunk_t),
dtype="float64", fill_value=np.nan)
arr[:] = data[vn]
arr.attrs["_ARRAY_DIMENSIONS"] = ["basin", "time"]
time_arr = root.create_array("time", shape=(nt,), chunks=(chunk_t,), dtype="int64")
time_arr[:] = times_ns
time_arr.attrs["_ARRAY_DIMENSIONS"] = ["time"]
time_arr.attrs["units"] = "nanoseconds since 1970-01-01"
time_arr.attrs["calendar"] = "proleptic_gregorian"
basin_arr = root.create_array("basin", shape=(n,), chunks=(n,), dtype=str)
basin_arr[:] = stations
basin_arr.attrs["_ARRAY_DIMENSIONS"] = ["basin"]
root.attrs["coordinates"] = "basin time"
self._write_zarr_units(root, "dynamic")
print(f"Timeseries zarr written to: {out}")
@property
def _attributes_cache_filename(self):
return "lamahice_attributes.nc"
@property
def _timeseries_cache_filename(self):
return "lamahice_timeseries.nc"
@property
def default_t_range(self):
return ["1950-01-01", "2021-12-31"]
# Define standardized static variable mappings
# Based on aqua_fetch LamaHIce static_map
# information of features get from pdf https://www.hydroshare.org/resource/705d69c0f77c48538d83cf383f8c63d6/
_subclass_static_definitions = {
"p_mean": {"specific_name": "p_mean_basin", "unit": "mm"},
"area": {"specific_name": "area_km2", "unit": "km^2"},
}
# Define standardized dynamic variable mappings
# Based on aqua_fetch LamaHIce dyn_map
_dynamic_variable_mapping = {
StandardVariable.STREAMFLOW: {
"default_source": "lamah_ice",
"sources": {
"lamah_ice": {"specific_name": "q_cms_obs", "unit": "m^3/s"},
"carra": {"specific_name": "runoff_carra", "unit": "mm"},
},
},
StandardVariable.PRECIPITATION: {
"default_source": "lamah_ice",
"sources": {
"lamah_ice": {"specific_name": "pcp_mm", "unit": "mm"},
"carra": {"specific_name": "prec_carra", "unit": "mm"},
"rav": {"specific_name": "prec_rav", "unit": "mm"},
},
},
StandardVariable.TEMPERATURE_MIN: {
"default_source": "lamah_ice",
"sources": {
"lamah_ice": {"specific_name": "airtemp_c_2m_min", "unit": "°C"},
"dp": {"specific_name": "2m_dp_temp_min", "unit": "°C"},
"carra": {"specific_name": "2m_temp_min_carra", "unit": "°C"},
},
},
StandardVariable.TEMPERATURE_MAX: {
"default_source": "lamah_ice",
"sources": {
"lamah_ice": {"specific_name": "airtemp_c_2m_max", "unit": "°C"},
"dp": {"specific_name": "2m_dp_temp_max", "unit": "°C"},
"carra": {"specific_name": "2m_temp_max_carra", "unit": "°C"},
},
},
StandardVariable.TEMPERATURE_MEAN: {
"default_source": "lamah_ice",
"sources": {
"lamah_ice": {"specific_name": "airtemp_c_mean_2m", "unit": "°C"},
"dp": {"specific_name": "2m_dp_temp_mean", "unit": "°C"},
"rav": {"specific_name": "2m_temp_rav", "unit": "°C"},
"carra": {"specific_name": "2m_temp_carra", "unit": "°C"},
},
},
StandardVariable.POTENTIAL_EVAPOTRANSPIRATION: {
"default_source": "lamah_ice",
"sources": {
"lamah_ice": {"specific_name": "pet_mm", "unit": "mm/day"},
"caravan": {
"specific_name": "potential_evaporation_sum_fao_penman_monteith_from_caravan",
"unit": "mm/day",
},
},
},
StandardVariable.EVAPOTRANSPIRATION: {
"default_source": "rav",
"sources": {
"ref": {"specific_name": "ref_et_mm", "unit": "mm/day"},
"rav": {"specific_name": "total_et_rav", "unit": "mm/day"},
"carra": {"specific_name": "total_et_carra", "unit": "mm/day"},
},
},
StandardVariable.U_WIND_SPEED: {
"default_source": "lamah_ice",
"sources": {
"lamah_ice": {"specific_name": "10m_wind_u", "unit": "m/s"},
"rav": {"specific_name": "10m_wind_u_rav", "unit": "m/s"},
},
},
StandardVariable.V_WIND_SPEED: {
"default_source": "lamah_ice",
"sources": {
"lamah_ice": {"specific_name": "10m_wind_v", "unit": "m/s"},
"rav": {"specific_name": "10m_wind_v_rav", "unit": "m/s"},
},
},
StandardVariable.WIND_SPEED: {
"default_source": "carra",
"sources": {
"carra": {"specific_name": "10m_wind_speed_carra", "unit": "m/s"}
},
},
StandardVariable.WIND_DIR: {
"default_source": "carra",
"sources": {
"carra": {"specific_name": "10m_wind_dir_carra", "unit": "degree"}
},
},
StandardVariable.SNOW_WATER_EQUIVALENT: {
"default_source": "lamah_ice",
"sources": {
"lamah_ice": {"specific_name": "swe", "unit": "mm"},
"carra": {"specific_name": "swe_carra", "unit": "mm"},
},
},
StandardVariable.SOLAR_RADIATION: {
"default_source": "lamah_ice",
"sources": {
"lamah_ice": {
"specific_name": "surf_net_solar_rad_mean",
"unit": "W/m^2",
},
"rav": {"specific_name": "surf_dwn_solar_rad_rav", "unit": "W/m^2"},
"carra": {
"specific_name": "surf_net_solar_rad_carra",
"unit": "W/m^2",
},
"dwn_carra": {
"specific_name": "surf_dwn_solar_rad_carra",
"unit": "W/m^2",
},
},
},
StandardVariable.SOLAR_RADIATION_MAX: {
"default_source": "lamah_ice",
"sources": {
"lamah_ice": {
"specific_name": "surf_net_solar_rad_max",
"unit": "W/m^2",
}
},
},
StandardVariable.THERMAL_RADIATION: {
"default_source": "lamah_ice",
"sources": {
"lamah_ice": {
"specific_name": "surf_net_therm_rad_mean",
"unit": "W/m^2",
},
"outg": {"specific_name": "surf_outg_therm_rad_rav", "unit": "W/m^2"},
"dwn": {"specific_name": "surf_dwn_therm_rad_rav", "unit": "W/m^2"},
"carra": {
"specific_name": "surf_net_therm_rad_carra",
"unit": "W/m^2",
},
"dwn_carra": {
"specific_name": "surf_dwn_therm_rad_carra",
"unit": "W/m^2",
},
},
},
StandardVariable.THERMAL_RADIATION_MAX: {
"default_source": "lamah_ice",
"sources": {
"lamah_ice": {
"specific_name": "surf_net_therm_rad_max",
"unit": "W/m^2",
}
},
},
StandardVariable.SURFACE_PRESSURE: {
"default_source": "lamah_ice",
"sources": {
"lamah_ice": {"specific_name": "surf_press", "unit": "Pa"},
"rav": {"specific_name": "surf_press_rav", "unit": "Pa"},
},
},
StandardVariable.VOLUMETRIC_SOIL_WATER_LAYER1: {
"default_source": "rav",
"sources": {"rav": {"specific_name": "volsw_123", "unit": "mm"}},
},
StandardVariable.VOLUMETRIC_SOIL_WATER_LAYER4: {
"default_source": "rav",
"sources": {"rav": {"specific_name": "volsw_4", "unit": "mm"}},
},
StandardVariable.RELATIVE_HUMIDITY: {
"default_source": "rav",
"sources": {
"rav": {"specific_name": "2m_qv_rav", "unit": "m/s"},
"carra": {"specific_name": "2m_rel_hum_carra", "unit": "m/s"},
},
},
StandardVariable.SPECIFIC_HUMIDITY: {
"default_source": "carra",
"sources": {"carra": {"specific_name": "2m_spec_hum_carra", "unit": "m/s"}},
},
StandardVariable.GROUND_HEAT_FLUX: {
"default_source": "rav",
"sources": {
"rav": {"specific_name": "grdflx_rav", "unit": "W/m^2"},
"sens": {
"specific_name": "surf_dwn_sens_heat_flux_carra",
"unit": "W/m^2",
},
"lat": {
"specific_name": "surf_dwn_lat_heat_flux_carra",
"unit": "W/m^2",
},
},
},
StandardVariable.SNOW_SUBLIMATION: {
"default_source": "carra",
"sources": {
"carra": {"specific_name": "snow_sublimation_carra", "unit": "mm"}
},
},
StandardVariable.SOIL_MOISTURE: {
"default_source": "carra",
"sources": {"carra": {"specific_name": "percolation_carra", "unit": "mm"}},
},
}
|