forked from orcestra-campaign/dropsondes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable_tables.py
More file actions
130 lines (118 loc) · 3.84 KB
/
variable_tables.py
File metadata and controls
130 lines (118 loc) · 3.84 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
# %%
import xarray as xr
import pandas as pd
from plots import settings
# %%
l3_path = f"{settings.root}/products/HALO/dropsondes/Level_3"
l4_path = f"{settings.root}/products/HALO/dropsondes/Level_4"
l3 = xr.open_dataset(f"{l3_path}/PERCUSION_Level_3.zarr", engine="zarr")
l4 = xr.open_dataset(f"{l4_path}/PERCUSION_Level_4.zarr", engine="zarr")
# %%
def get_ds_vars(ds, other_ds=None):
if other_ds is not None:
other_ds = other_ds.variables
else:
other_ds = []
return {
var: {
"variable": var.replace("_", "\_"),
"units": ds[var].attrs.get("units", "").replace("_", "\_"),
"standard\_name": ds[var].attrs.get("standard_name", "").replace("_", "\_"),
"dimensions": " ".join(ds[var].dims),
}
for var in ds.data_vars
if (var not in other_ds)
and (not var.endswith("dx"))
and (not var.endswith("dy"))
and (not var.endswith("_mean"))
and (not var.endswith("dx_std_error"))
and (not var.endswith("dy_std_error"))
}
def get_coords(ds):
return {
var: {
"variable": var.replace("_", "\_"),
"units": ds[var].attrs.get("units", "").replace("_", "\_"),
"standard\_name": ds[var].attrs.get("standard_name", "").replace("_", "\_"),
"dimensions": " ".join(ds[var].dims),
}
for var in ds.coords
}
# %%
l3_records = get_ds_vars(l3)
l4_records = get_ds_vars(l4, l3)
# %%
l4_records.update(
{
"*\_mean": {
"variable": "*\_mean",
"units": "*",
"standard\_name": "",
"dimensions": "circle altitude",
},
"*\_d*dx": {
"variable": "*\_d*dx",
"units": "* m-1",
"standard\_name": l4["u_dudx"]
.attrs.get("standard_name", "")
.replace(l4["u"].attrs.get("standard_name", ""), "*")
.replace("_", "\_"),
"dimensions": "circle altitude",
},
"*\_d*dy": {
"variable": "*\_d*dy",
"units": "* m-1",
"standard\_name": l4["u_dudy"]
.attrs.get("standard_name", "")
.replace(l4["u"].attrs.get("standard_name", ""), "*")
.replace("_", "\_"),
"dimensions": "circle altitude",
},
"*\_d*dx_std\_error": {
"variable": "*\_d*dx\_std\_error",
"units": "* m-1",
"standard\_name": l4["u_dudx_std_error"]
.attrs.get("standard_name", "")
.replace(l4["u"].attrs.get("standard_name", ""), "*")
.replace("_", "\_"),
"dimensions": "circle altitude",
},
"*\_d*dy_std\_error": {
"variable": "*\_d*dy\_std\_error",
"units": "* m-1",
"standard\_name": l4["u_dudy_std_error"]
.attrs.get("standard_name", "")
.replace(l4["u"].attrs.get("standard_name", ""), "*")
.replace("_", "\_"),
"dimensions": "circle altitude",
},
}
)
for ds, name, records in [(l3, "l3", l3_records), (l4, "l4", l4_records)]:
if name == "l4":
other_ds = l3
else:
other_ds = None
df = (
pd.DataFrame.from_records(records)
.transpose()
.reset_index(drop=True)
.sort_values("variable")
)
df_coord = (
pd.DataFrame.from_records(get_coords(ds))
.transpose()
.reset_index(drop=True)
.sort_values("variable")
)
df["object"] = "Variables"
df_coord["object"] = "Coordinates"
df_coord = df_coord.set_index(["object", "variable"])
df = df.set_index(["object", "variable"])
final = pd.concat([df, df_coord]).sort_index()
final.to_latex(
f"{name}_vars.tex",
index=True,
caption=f"BEACH Level {name[-1]} Variables",
label=f"tab:l{name[-1]}vars",
)