...
| Code Block | ||||||
|---|---|---|---|---|---|---|
| ||||||
import cdsapi
c = cdsapi.Client()
c.retrieve(
'cems-glofas-historical',
{
'variable': 'river_discharge_in_the_last_24_hours',
'format': 'grib',
'hydrological_model': 'lisflood',
'product_type': 'intermediate',
'hyear': '2021',
'hmonth': 'january',
'hday': [
'01', '02', '03',
'04', '05', '06',
'07', '08', '09',
'10', '11', '12',
'13', '14', '15',
'16', '17', '18',
'19', '20', '21',
'22', '23', '24',
'25', '26', '27',
'28', '29', '30',
'31',
],
'system_version': 'version_3_1',
},
'glofas_historical.grib') |
Time series extraction:
...
| Code Block | ||||||
|---|---|---|---|---|---|---|
| ||||||
import xarray as xr
import pandas as pd
parameter = "dis24"
ds = xr.open_dataset("glofas_historical.grib", engine="cfgrib",backend_kwargs={'time_dims':['time']})
df = pd.read_csv("GRDC.csv")
total = len(df)
rows = []
count = 0
for lon, lat, id in zip(df.long, df.lat, df.grdc_no):
extracted = ds.sel(longitude=lon, latitude=lat, method="nearest")[parameter]
df_temp = extracted.drop_vars(["surface"]).to_dataframe().reset_index()
df_temp["grdc"] = str(id)
df_temp = df_temp.set_index(["grdc", "time"])
rows.append(df_temp)
count += 1
print(f"progress: {count/total*100} %")
out = pd.concat(rows)
out.to_csv("extracted.csv", index="grdc") |
Area cropping:
| Code Block | ||||||
|---|---|---|---|---|---|---|
| ||||||
import xarray as xr
# Rhine's basin bounding box
bbox = [50.972204, 46.296530, 5.450796, 11.871059] # N,S,W,E
ds = xr.open_dataset("glofas_historical.grib", engine="cfgrib")
ds_cropped = ds.sel(
longitude=slice(bbox[2], bbox[3]), latitude=slice(bbox[0], bbox[1])
)
ds_cropped.to_netcdf("glofas_historical_cropped.nc") |
EFAS
| Warning | ||
|---|---|---|
| ||
When transforming from lat/lon (source coordinates) to projected LAEA (target coordinates), you need to consider that the number of decimal places of the source coordinates affects the target coordinates precision: An interval of 0.001 degrees corresponds to about 100 metres in LAEA. An interval of 0.00001 degrees corresponds to about 1 metre in LAEA. |
...