-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesowest.py
43 lines (40 loc) · 1.21 KB
/
mesowest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import numpy as np
import pandas as pd
import xarray as xr
def mesowest_to_xarray(data):
ds = xr.Dataset()
ds["time"] = pd.to_datetime(data["STATION"][0]["OBSERVATIONS"]["date_time"])
ds["time"] = pd.DatetimeIndex(ds["time"].values, tz=None)
for k in data["STATION"][0]["OBSERVATIONS"].keys():
if k == "date_time":
continue
var = k.split("_set_")
try:
ds[var[0]] = xr.DataArray(
np.array(data["STATION"][0]["OBSERVATIONS"][k]).astype(float),
dims="time",
)
except ValueError: # for variables which can't be converted to float
ds[var[0]] = xr.DataArray(
np.array(data["STATION"][0]["OBSERVATIONS"][k]),
dims="time",
)
ds[var[0]].attrs["units"] = data["UNITS"][var[0]]
for k in [
"STATUS",
"MNET_ID",
"ELEVATION",
"NAME",
"STID",
"ELEV_DEM",
"LONGITUDE",
"STATE",
"LATITUDE",
"TIMEZONE",
"ID",
]:
if k == "NAME":
ds.attrs["STATION_NAME"] = data["STATION"][0][k]
else:
ds.attrs[k] = data["STATION"][0][k]
return ds