11# %%
22import os
33import xarray as xr
4+ import src .readwrite_functions as rwfuncs
5+ from src .post_processed_hamp_data import PostProcessedHAMPData
6+ import yaml
7+ import pandas as pd
8+ import typhon
9+ import pyarts
10+ from tqdm import tqdm
11+ import FluxSimulator as fsm
412from src .arts_functions import (
513 Hamp_channels ,
614 basic_setup ,
917 get_surface_temperature ,
1018 get_surface_windspeed ,
1119 forward_model ,
12- check_data ,
20+ is_complete ,
1321)
14- from src .ipfs_helpers import read_nc
15- from orcestra .postprocess .level0 import bahamas
16- import src .readwrite_functions as rwfuncs
17- from src .post_processed_hamp_data import PostProcessedHAMPData
18- import yaml
19- import pandas as pd
20- import typhon
21- import pyarts
22- from tqdm import tqdm
23- import FluxSimulator as fsm
24- from src .plots_functions import plot_dropsonde
2522
2623
2724# %% get ARTS data
3936
4037
4138# %% define function
42- def calc_arts_bts (date ):
39+ def calc_arts_bts (date , flightletter ):
4340 """
4441 Calculates brightness temperatures for the radiometer frequencies with
4542 ARTS based on the dropsonde profiles for the flight on date.
@@ -54,27 +51,18 @@ def calc_arts_bts(date):
5451 """
5552
5653 print ("Read Config" )
57- # change the date in config.yaml to date
5854 configfile = "config_ipns.yaml"
5955 with open (configfile , "r" ) as file :
6056 config_yml = yaml .safe_load (file )
6157 config_yml ["date" ] = date
58+ config_yml ["flightletter" ] = flightletter
6259 with open (configfile , "w" ) as file :
6360 yaml .dump (config_yml , file )
64- # read config
6561 cfg = rwfuncs .extract_config_params (configfile )
6662
67- # load bahamas data from ipfs
6863 print ("Load Bahamas Data" )
69- ds_bahamas = (
70- read_nc (
71- f"ipns://latest.orcestra-campaign.org/raw/HALO/bahamas/{ cfg ['flightname' ]} /QL_*.nc"
72- )
73- .pipe (bahamas )
74- .interpolate_na ("time" )
75- )
64+ ds_bahamas = xr .open_dataset (cfg ["path_position_attitude" ], engine = "zarr" )
7665
77- # read dropsonde data
7866 print ("Load Dropsonde Data" )
7967 ds_dropsonde = xr .open_dataset (cfg ["path_dropsondes" ], engine = "zarr" )
8068 ds_dropsonde = ds_dropsonde .where (
@@ -85,15 +73,13 @@ def calc_arts_bts(date):
8573 )
8674 ).dropna (dim = "sonde_id" , how = "all" )
8775
88- # read HAMP post-processed data
8976 print ("Load HAMP Data" )
9077 hampdata = PostProcessedHAMPData (
9178 xr .open_dataset (cfg ["path_radar" ], engine = "zarr" ),
9279 xr .open_dataset (cfg ["path_radiometers" ], engine = "zarr" ),
9380 xr .open_dataset (cfg ["path_iwv" ], engine = "zarr" ),
9481 )
9582
96- # cloud mask from radar
9783 print ("Calculate Cloud Mask" )
9884 ds_dropsonde = ds_dropsonde .assign (
9985 radar_cloud_flag = (
@@ -110,48 +96,38 @@ def calc_arts_bts(date):
11096 .values
11197 )
11298
113- # initialize result arrays
11499 freqs_hamp = hampdata .radiometers .frequency .values
115- TBs_arts = pd .DataFrame (index = freqs_hamp , columns = cloud_free_idxs )
116- TBs_hamp = TBs_arts . copy ( )
100+ TBs_arts = pd .DataFrame (index = freqs / 1e9 , columns = cloud_free_idxs )
101+ TBs_hamp = pd . DataFrame ( index = freqs_hamp , columns = cloud_free_idxs )
117102
118- # setup folders
119103 print ("Setup Folders" )
120104 if not os .path .exists (f"Data/arts_calibration/{ cfg ['flightname' ]} " ):
121105 os .makedirs (f"Data/arts_calibration/{ cfg ['flightname' ]} " )
122106 if not os .path .exists (f"Data/arts_calibration/{ cfg ['flightname' ]} /plots" ):
123107 os .makedirs (f"Data/arts_calibration/{ cfg ['flightname' ]} /plots" )
124108
125- # loop over cloud free sondes
126109 print (f"Running { cloud_free_idxs .size } dropsondes for { cfg ['flightname' ]} " )
127110 for sonde_id in tqdm (cloud_free_idxs ):
128- # get profiles
129111 ds_dropsonde_loc , hampdata_loc , height , drop_time = get_profiles (
130112 sonde_id , ds_dropsonde , hampdata
131113 )
132114
133- # check if dropsonde is broken (contains only nan values)
134- if not check_data (
135- ds_dropsonde_loc , ds_bahamas , hampdata_loc , drop_time , height , sonde_id
136- ):
115+ if not is_complete (ds_dropsonde_loc , hampdata_loc , drop_time , height , sonde_id ):
137116 continue
138117
139- # get surface values
140118 surface_temp = get_surface_temperature (ds_dropsonde_loc )
141119 surface_ws = get_surface_windspeed (ds_dropsonde_loc )
142120
143- # extrapolate dropsonde profiles
144121 ds_dropsonde_extrap = extrapolate_dropsonde (
145122 ds_dropsonde_loc , height , ds_bahamas
146123 )
147124
148- plot_dropsonde (ds_dropsonde_extrap )
125+ # plot_dropsonde(ds_dropsonde_extrap, ds_dropsonde_loc )
149126
150- # convert xarray to ARTS gridded field
151127 profile_grd = fsm .generate_gridded_field_from_profiles (
152128 pyarts .arts .Vector (ds_dropsonde_extrap ["p" ].values ),
153129 ds_dropsonde_extrap ["ta" ].values ,
154- ds_dropsonde_extrap ["gpsalt " ].values ,
130+ ds_dropsonde_extrap ["alt " ].values ,
155131 gases = {
156132 "H2O" : typhon .physics .specific_humidity2vmr (
157133 ds_dropsonde_extrap ["q" ].values
@@ -161,7 +137,6 @@ def calc_arts_bts(date):
161137 },
162138 )
163139
164- # run arts
165140 BTs , _ = forward_model (
166141 ws ,
167142 profile_grd ,
@@ -170,55 +145,50 @@ def calc_arts_bts(date):
170145 height ,
171146 )
172147
173- # except (ValueError, KeyError, RuntimeError) as e:
174- # print(
175- # f"ARTS or extrapolation failed for dropsonde {sonde_id} with error: {e}, skipping"
176- # )
177- # pass
178-
179- # Store arts BTs
180148 TBs_arts [sonde_id ] = pd .DataFrame (data = BTs , index = freqs / 1e9 )
181- # get according hamp data
182- TBs_hamp [sonde_id ] = hampdata_loc .radiometers .TBs .values
149+ TBs_hamp [sonde_id ] = hampdata_loc ["TBs" ]
150+
151+ # plot_TB_comparison(TBs_arts[sonde_id], TBs_hamp[sonde_id], sonde_id)
183152
184- # save results
185153 TBs_arts .to_csv (f"Data/arts_calibration/{ cfg ['flightname' ]} /TBs_arts.csv" )
186154 TBs_hamp .to_csv (f"Data/arts_calibration/{ cfg ['flightname' ]} /TBs_hamp.csv" )
187155
188156
189157# %% call function
190- # date = str(sys.argv[1])
191- calc_arts_bts ("20240811" )
192-
193- # %% test turning extrapolated dropsonde profiles to ARTS input
194- # read config
195- cfg = rwfuncs .extract_config_params ("config_ipns.yaml" )
196- ds_dropsonde = xr .open_dataset (cfg ["path_dropsondes" ], engine = "zarr" )
197- profile = ds_dropsonde .isel (sonde_id = 100 )
198- profile = profile .dropna ("gpsalt" )
199-
200- # %% convert xarray to ARTS gridded field
201- profile_grd = fsm .generate_gridded_field_from_profiles (
202- pyarts .arts .Vector (profile ["p" ].values ),
203- profile ["ta" ].values ,
204- profile ["alt" ].values ,
205- gases = {
206- "H2O" : typhon .physics .specific_humidity2vmr (profile ["q" ].values ),
207- "N2" : 0.78 ,
208- "O2" : 0.21 ,
209- },
210- )
158+ dates = [
159+ "20240811" ,
160+ "20240813" ,
161+ "20240816" ,
162+ "20240818" ,
163+ "20240821" ,
164+ "20240822" ,
165+ "20240825" ,
166+ "20240827" ,
167+ "20240829" ,
168+ "20240831" ,
169+ "20240903" ,
170+ "20240906" ,
171+ "20240907" ,
172+ "20240909" ,
173+ "20240912" ,
174+ "20240914" ,
175+ "20240916" ,
176+ "20240919" ,
177+ "20240921" ,
178+ "20240923" ,
179+ "20240924" ,
180+ "20240926" ,
181+ "20240928" ,
182+ "20240929" ,
183+ ]
184+
185+ flightletters = {}
186+ for date in dates :
187+ flightletters [date ] = "a"
211188
212189# %%
190+ for date in dates :
191+ calc_arts_bts (date , flightletters [date ])
213192
214- BTs , _ = forward_model (
215- ws ,
216- profile_grd ,
217- 10 ,
218- 300 ,
219- 1e4 ,
220- )
221- # %%
222- freqs = sensor_description [:, 0 ] + sensor_description [:, 1 ] + sensor_description [:, 2 ]
223193
224194# %%
0 commit comments