Skip to content

Commit fe25e95

Browse files
committed
arts calibration with new dropsonde data, attributes finalized
1 parent 83b79d3 commit fe25e95

File tree

7 files changed

+199
-151
lines changed

7 files changed

+199
-151
lines changed

scripts/arts_calibration/analyse_bt_diffs.py

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import pandas as pd
33
import matplotlib.pyplot as plt
44
import xarray as xr
5-
from src import readwrite_functions as rwfuncs
5+
import yaml
6+
import numpy as np
67

78
# %% define frequencies
89
freq_k = [22.24, 23.04, 23.84, 25.44, 26.24, 27.84, 31.40]
@@ -43,35 +44,14 @@
4344

4445

4546
# %% Read csv BTs
46-
dates = [
47-
"20240811",
48-
"20240813",
49-
"20240816",
50-
"20240818",
51-
"20240821",
52-
"20240822",
53-
"20240825",
54-
"20240827",
55-
"20240829",
56-
"20240831",
57-
"20240903",
58-
"20240906",
59-
"20240907",
60-
"20240909",
61-
"20240912",
62-
"20240914",
63-
"20240916",
64-
"20240919",
65-
"20240921",
66-
"20240923",
67-
"20240924",
68-
"20240926",
69-
"20240928",
47+
flights = pd.read_csv("flights.csv", index_col=0)
48+
flights_processed = flights[
49+
(flights["location"] == "sal") | (flights["location"] == "barbados")
7050
]
7151

7252
TB_arts_list = []
7353
TB_hamp_list = []
74-
for date in dates:
54+
for date in flights_processed.index:
7555
TB_arts_list.append(
7656
pd.read_csv(f"Data/arts_calibration/HALO-{date}a/TBs_arts.csv", index_col=0)
7757
)
@@ -80,18 +60,25 @@
8060
)
8161

8262
# load dropsonde data
83-
configfile = "config_ipns.yaml"
84-
cfg = rwfuncs.extract_config_params(configfile)
85-
ds_dropsonde = xr.open_dataset(cfg["path_dropsondes"], engine="zarr")
63+
with open("process_config.yaml") as f:
64+
cfg = yaml.safe_load(f)
65+
66+
ds_dropsonde = xr.open_dataset(
67+
"ipns://latest.orcestra-campaign.org/products/HALO/dropsondes/Level_3/PERCUSION_Level_3.zarr",
68+
engine="zarr",
69+
)
70+
ds_dropsonde = ds_dropsonde.assign_coords(sonde=np.arange(ds_dropsonde.sonde.size))
8671

87-
# %% restructure data
72+
# restructure data
8873
TB_arts = pd.concat(TB_arts_list, axis=1)
8974
TB_hamp = pd.concat(TB_hamp_list, axis=1)
90-
launch_time = ds_dropsonde.sel(sonde_id=TB_arts.columns).launch_time.values
75+
launch_time = ds_dropsonde.sel(
76+
sonde=[int(float(x)) for x in TB_arts.columns.values]
77+
).sonde_time.values
9178
TB_arts.columns = launch_time
9279
TB_hamp.columns = launch_time
93-
TB_arts = TB_arts.T
94-
TB_hamp = TB_hamp.T
80+
TB_arts = TB_arts.T.dropna()
81+
TB_hamp = TB_hamp.T.dropna()
9582

9683
# %% calculate statistics for each flight
9784
diffs = TB_arts - TB_hamp
Lines changed: 49 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
# %%
22
import os
33
import xarray as xr
4-
import src.readwrite_functions as rwfuncs
5-
from src.post_processed_hamp_data import PostProcessedHAMPData
64
import yaml
75
import pandas as pd
86
import typhon
97
import pyarts
108
from tqdm import tqdm
119
import FluxSimulator as fsm
10+
import numpy as np
1211
from src.arts_functions import (
1312
Hamp_channels,
1413
basic_setup,
@@ -20,9 +19,10 @@
2019
is_complete,
2120
)
2221

22+
# from src.plots_functions import plot_dropsonde, plot_TB_comparison
23+
2324

2425
# %% get ARTS data
25-
print("Download ARTS data")
2626
pyarts.cat.download.retrieve(verbose=True)
2727

2828
# %% setup sensor
@@ -34,9 +34,20 @@
3434
# %% setup workspace
3535
ws = basic_setup([], sensor_description=sensor_description)
3636

37+
# %% load data
38+
configfile = "process_config.yaml"
39+
with open(configfile, "r") as file:
40+
cfg = yaml.safe_load(file)
41+
42+
ds_dropsonde = xr.open_dataset(
43+
"ipns://latest.orcestra-campaign.org/products/HALO/dropsondes/Level_3/PERCUSION_Level_3.zarr",
44+
engine="zarr",
45+
)
46+
ds_dropsonde = ds_dropsonde.assign_coords(sonde=np.arange(ds_dropsonde.sonde.size))
47+
3748

3849
# %% define function
39-
def calc_arts_bts(date, flightletter):
50+
def calc_arts_bts(date, flightletter, ds_dropsonde, cfg):
4051
"""
4152
Calculates brightness temperatures for the radiometer frequencies with
4253
ARTS based on the dropsonde profiles for the flight on date.
@@ -50,84 +61,64 @@ def calc_arts_bts(date, flightletter):
5061
None. Data is saved in arts_comparison folder.
5162
"""
5263

53-
print("Read Config")
54-
configfile = "config_ipns.yaml"
55-
with open(configfile, "r") as file:
56-
config_yml = yaml.safe_load(file)
57-
config_yml["date"] = date
58-
config_yml["flightletter"] = flightletter
59-
with open(configfile, "w") as file:
60-
yaml.dump(config_yml, file)
61-
cfg = rwfuncs.extract_config_params(configfile)
62-
63-
print("Load Bahamas Data")
64-
ds_bahamas = xr.open_dataset(cfg["path_position_attitude"], engine="zarr")
65-
66-
print("Load Dropsonde Data")
67-
ds_dropsonde = xr.open_dataset(cfg["path_dropsondes"], engine="zarr")
68-
ds_dropsonde = ds_dropsonde.where(
69-
(ds_dropsonde["interp_time"] > pd.to_datetime(cfg["date"]))
70-
& (
71-
ds_dropsonde["interp_time"]
72-
< pd.to_datetime(cfg["date"]) + pd.DateOffset(hour=23)
73-
)
74-
).dropna(dim="sonde_id", how="all")
75-
76-
print("Load HAMP Data")
77-
hampdata = PostProcessedHAMPData(
78-
xr.open_dataset(cfg["path_radar"], engine="zarr"),
79-
xr.open_dataset(cfg["path_radiometers"], engine="zarr"),
80-
xr.open_dataset(cfg["path_iwv"], engine="zarr"),
64+
flightname = f"HALO-{date}{flightletter}"
65+
ds_radar = xr.open_dataset(
66+
f"{cfg['save_dir']}/radar/{flightname}_radar.zarr", engine="zarr"
8167
)
68+
ds_radiometers = xr.open_dataset(
69+
f"{cfg['save_dir']}/radiometer/{flightname}_radio.zarr", engine="zarr"
70+
)
71+
ds_dropsonde = ds_dropsonde.where(
72+
(ds_dropsonde["interp_time"] > pd.to_datetime(date))
73+
& (ds_dropsonde["interp_time"] < pd.to_datetime(date) + pd.DateOffset(hour=23))
74+
).dropna(dim="sonde", how="all")
8275

8376
print("Calculate Cloud Mask")
8477
ds_dropsonde = ds_dropsonde.assign(
8578
radar_cloud_flag=(
86-
hampdata.radar.sel(time=ds_dropsonde.launch_time, method="nearest").sel(
79+
ds_radar.sel(time=ds_dropsonde.sonde_time, method="nearest").sel(
8780
height=slice(200, None)
8881
)["dBZe"]
8982
> -30
9083
).max("height")
9184
* 1
9285
)
9386
cloud_free_idxs = (
94-
ds_dropsonde["sonde_id"]
95-
.where(ds_dropsonde["radar_cloud_flag"] == 0, drop=True)
87+
ds_dropsonde["sonde"]
88+
.where((ds_dropsonde["radar_cloud_flag"] == 0).compute(), drop=True)
9689
.values
9790
)
9891

99-
freqs_hamp = hampdata.radiometers.frequency.values
92+
freqs_hamp = ds_radiometers.frequency.values
10093
TBs_arts = pd.DataFrame(index=freqs / 1e9, columns=cloud_free_idxs)
10194
TBs_hamp = pd.DataFrame(index=freqs_hamp, columns=cloud_free_idxs)
10295

10396
print("Setup Folders")
104-
if not os.path.exists(f"Data/arts_calibration/{cfg['flightname']}"):
105-
os.makedirs(f"Data/arts_calibration/{cfg['flightname']}")
106-
if not os.path.exists(f"Data/arts_calibration/{cfg['flightname']}/plots"):
107-
os.makedirs(f"Data/arts_calibration/{cfg['flightname']}/plots")
108-
109-
print(f"Running {cloud_free_idxs.size} dropsondes for {cfg['flightname']}")
110-
for sonde_id in tqdm(cloud_free_idxs):
111-
ds_dropsonde_loc, hampdata_loc, height, drop_time = get_profiles(
112-
sonde_id, ds_dropsonde, hampdata
97+
if not os.path.exists(f"Data/arts_calibration/{flightname}"):
98+
os.makedirs(f"Data/arts_calibration/{flightname}")
99+
if not os.path.exists(f"Data/arts_calibration/{flightname}/plots"):
100+
os.makedirs(f"Data/arts_calibration/{flightname}/plots")
101+
102+
print(f"Running {cloud_free_idxs.size} dropsondes for {date}")
103+
for sonde in tqdm(cloud_free_idxs):
104+
ds_dropsonde_loc, radiometers_loc, height, drop_time = get_profiles(
105+
sonde, ds_dropsonde, ds_radiometers
113106
)
114107

115-
if not is_complete(ds_dropsonde_loc, hampdata_loc, drop_time, height, sonde_id):
108+
if not is_complete(ds_dropsonde_loc, radiometers_loc, drop_time, height, sonde):
116109
continue
117110

118111
surface_temp = get_surface_temperature(ds_dropsonde_loc)
119112
surface_ws = get_surface_windspeed(ds_dropsonde_loc)
120113

121-
ds_dropsonde_extrap = extrapolate_dropsonde(
122-
ds_dropsonde_loc, height, ds_bahamas
123-
)
114+
ds_dropsonde_extrap = extrapolate_dropsonde(ds_dropsonde_loc, height)
124115

125116
# plot_dropsonde(ds_dropsonde_extrap, ds_dropsonde_loc)
126117

127118
profile_grd = fsm.generate_gridded_field_from_profiles(
128119
pyarts.arts.Vector(ds_dropsonde_extrap["p"].values),
129120
ds_dropsonde_extrap["ta"].values,
130-
ds_dropsonde_extrap["alt"].values,
121+
ds_dropsonde_extrap["altitude"].values,
131122
gases={
132123
"H2O": typhon.physics.specific_humidity2vmr(
133124
ds_dropsonde_extrap["q"].values
@@ -145,24 +136,23 @@ def calc_arts_bts(date, flightletter):
145136
height,
146137
)
147138

148-
TBs_arts[sonde_id] = pd.DataFrame(data=BTs, index=freqs / 1e9)
149-
TBs_hamp[sonde_id] = hampdata_loc["TBs"]
139+
TBs_arts[sonde] = pd.DataFrame(data=BTs, index=freqs / 1e9)
140+
TBs_hamp[sonde] = radiometers_loc["TBs"]
150141

151-
# plot_TB_comparison(TBs_arts[sonde_id], TBs_hamp[sonde_id], sonde_id)
142+
# plot_TB_comparison(TBs_arts[sonde], TBs_hamp[sonde], sonde)
152143

153-
TBs_arts.to_csv(f"Data/arts_calibration/{cfg['flightname']}/TBs_arts.csv")
154-
TBs_hamp.to_csv(f"Data/arts_calibration/{cfg['flightname']}/TBs_hamp.csv")
144+
TBs_arts.to_csv(f"Data/arts_calibration/{flightname}/TBs_arts.csv")
145+
TBs_hamp.to_csv(f"Data/arts_calibration/{flightname}/TBs_hamp.csv")
155146

156147

157148
# %% loop over flights
158-
159149
flights = pd.read_csv("flights.csv", index_col=0)
160150
flights_processed = flights[
161151
(flights["location"] == "sal") | (flights["location"] == "barbados")
162152
]
163153

164154
for date in flights_processed.index:
165-
calc_arts_bts(str(date), flights_processed.loc[date]["flightletter"])
166-
167-
155+
calc_arts_bts(
156+
str(date), flights_processed.loc[date]["flightletter"], ds_dropsonde, cfg
157+
)
168158
# %%

0 commit comments

Comments
 (0)