Skip to content

Commit 569b8bd

Browse files
make list of sites from station_config, switched print to logger.info
1 parent a847958 commit 569b8bd

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

src/pypromice/process/L0toL1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def getPressDepth(z_pt, p, pt_antifreeze, pt_z_factor, pt_z_coef, pt_z_p_coef):
248248
rho_af = 1145
249249
else:
250250
rho_af = np.nan
251-
print('ERROR: Incorrect metadata: "pt_antifreeze" = ' +
251+
logger.info('ERROR: Incorrect metadata: "pt_antifreeze" = ' +
252252
f'{pt_antifreeze}. Antifreeze mix only supported at 50% or 100%')
253253
# assert(False)
254254

src/pypromice/process/join_l2.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@ def loadArr(infile):
4343
if 'number_of_booms' in ds.attrs.keys():
4444
ds.attrs['number_of_booms'] = int(ds.attrs['number_of_booms'])
4545

46-
print(f'{name} array loaded from {infile}')
46+
logger.info(f'{name} array loaded from {infile}')
4747
return ds, name
4848

4949

5050
def join_l2():
5151
args = parse_arguments_join()
52-
52+
logging.basicConfig(
53+
format="%(asctime)s; %(levelname)s; %(name)s; %(message)s",
54+
level=logging.INFO,
55+
stream=sys.stdout,
56+
)
5357
# Check files
5458
if os.path.isfile(args.file1) and os.path.isfile(args.file2):
5559

@@ -61,7 +65,7 @@ def join_l2():
6165
if n1.lower() == n2.lower():
6266

6367
# Merge arrays
64-
print(f'Combining {args.file1} with {args.file2}...')
68+
logger.info(f'Combining {args.file1} with {args.file2}...')
6569
name = n1
6670
all_ds = ds1.combine_first(ds2)
6771

@@ -75,28 +79,28 @@ def join_l2():
7579
all_ds['precip_l_cor'], _ = correctPrecip(all_ds['precip_l'],
7680
all_ds['wspd_l'])
7781
else:
78-
print(f'Mismatched station names {n1}, {n2}')
82+
logger.info(f'Mismatched station names {n1}, {n2}')
7983
exit()
8084

8185
elif os.path.isfile(args.file1):
8286
ds1, name = loadArr(args.file1)
83-
print(f'Only one file found {args.file1}...')
87+
logger.info(f'Only one file found {args.file1}...')
8488
all_ds = ds1
8589

8690
elif os.path.isfile(args.file2):
8791
ds2, name = loadArr(args.file2)
88-
print(f'Only one file found {args.file2}...')
92+
logger.info(f'Only one file found {args.file2}...')
8993
all_ds = ds2
9094

9195
else:
92-
print(f'Invalid files {args.file1}, {args.file2}')
96+
logger.info(f'Invalid files {args.file1}, {args.file2}')
9397
exit()
9498

9599

96100
# Resample to hourly, daily and monthly datasets and write to file
97101
prepare_and_write(all_ds, args.outpath, args.variables, args.metadata, resample = False)
98102

99-
print(f'Files saved to {os.path.join(args.outpath, name)}...')
103+
logger.info(f'Files saved to {os.path.join(args.outpath, name)}...')
100104

101105
if __name__ == "__main__":
102106
join_l2()

src/pypromice/process/join_l3.py

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,43 @@ def gcnet_postprocessing(l3):
160160
# df_in.loc[[df_in.z_surf_combined.first_valid_index()],:].index.astype('int64')[0]
161161
# ) + df_in.loc[df_in.z_surf_combined.first_valid_index(), 'z_surf_combined']
162162
# return l3m
163-
163+
164+
def build_station_dict(config_folder):
165+
station_dict = {}
166+
167+
# Iterate through all files in the given folder
168+
for filename in os.listdir(config_folder):
169+
if filename.endswith(".toml"):
170+
# Construct full file path
171+
file_path = os.path.join(config_folder, filename)
172+
173+
# Load TOML file
174+
with open(file_path, 'r') as file:
175+
data = toml.load(file)
176+
station_key = next(iter(data))
177+
station_data = data[station_key]
178+
179+
if station_data["station_site"] in station_dict:
180+
station_dict[station_data["station_site"]].append(station_key)
181+
else:
182+
station_dict[station_data["station_site"]] = [station_key]
183+
184+
return station_dict
185+
164186
def join_l3():
165187
args = parse_arguments_joinl3()
166-
167-
168-
config_file = os.path.join(args.config_folder, args.site+'.toml')
169-
conf = toml.load(config_file)
188+
logging.basicConfig(
189+
format="%(asctime)s; %(levelname)s; %(name)s; %(message)s",
190+
level=logging.INFO,
191+
stream=sys.stdout,
192+
)
193+
194+
station_dict = build_station_dict(args.config_folder)
170195

171196
l3m = xr.Dataset()
172197
l3m.attrs['level'] = 'L3'
173-
for stid in conf['stations']:
174-
print(stid)
198+
for stid in station_dict[args.site]:
199+
logger.info(stid)
175200

176201
is_promice = False
177202
is_gcnet = False
@@ -183,7 +208,7 @@ def join_l3():
183208
if os.path.isfile(filepath):
184209
is_gcnet = True
185210
if not is_promice and not is_gcnet:
186-
print(stid, 'not found either in', args.folder_l3, 'or', args.folder_gcnet)
211+
logger.info(stid, 'not found either in', args.folder_l3, 'or', args.folder_gcnet)
187212
continue
188213

189214
l3, _ = loadArr(filepath)
@@ -213,14 +238,15 @@ def join_l3():
213238

214239
# if l3 (older data) has variables that does not have l3m (newer data)
215240
# then they are removed from l3
216-
print('dropping')
241+
list_dropped = []
217242
for v in l3.data_vars:
218243
if v not in l3m.data_vars:
219244
if v != 'z_stake':
220-
print(v)
245+
list_dropped.append(v)
221246
l3 = l3.drop(v)
222247
else:
223248
l3m[v] = ('time', l3m.t_u.data*np.nan)
249+
logger.info('Unused variables in older dataset:',list_dropped)
224250

225251
# saving attributes of station under an attribute called $stid
226252
l3m = l3m.assign_attrs({stid : l3.attrs.copy()})
@@ -241,7 +267,7 @@ def join_l3():
241267

242268
# Assign site id
243269
l3m.attrs['site_id'] = args.site
244-
l3m.attrs['stations'] = conf['stations']
270+
l3m.attrs['stations'] = station_dict[args.site]
245271

246272
if args.outpath is not None:
247273
prepare_and_write(l3m, args.outpath, args.variables, args.metadata, '60min')

0 commit comments

Comments
 (0)