Skip to content

Commit

Permalink
Bugfix wct (#365)
Browse files Browse the repository at this point in the history
* Update api.py: xr_create_or_open for wct

* Update wct_dvv.py: handle None dataset and figure

* Update api.py: xr_save_wct not always overwriting
  • Loading branch information
LaureBrenot authored Sep 18, 2024
1 parent 6fb51cd commit 3b331b5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
19 changes: 17 additions & 2 deletions msnoise/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2169,6 +2169,19 @@ def xr_create_or_open(fn, taxis=[], name="CCF"):
data = np.random.random((len(times), len(level0), len(level1)))
dr = xr.DataArray(data, coords=[times, level0, level1],
dims=["times", "level0", "level1"])
elif name == "WCT":
dvv_data = np.random.random((len(times), len(taxis)))
err_data = np.random.random((len(times), len(taxis)))
coh_data = np.random.random((len(times), len(taxis)))

dvv_da = xr.DataArray(dvv_data, coords=[times, taxis], dims=['times', 'frequency'])
err_da = xr.DataArray(err_data, coords=[times, taxis], dims=['times', 'frequency'])
coh_da = xr.DataArray(coh_data, coords=[times, taxis], dims=['times', 'frequency'])

# Combine into a Dataset
ds = xr.Dataset({'dvv': dvv_da,'err': err_da,'coh': coh_da})
return ds

else:
logging.error("Not implemented, name=%s invalid." % name)
sys.exit(1)
Expand Down Expand Up @@ -2608,8 +2621,10 @@ def xr_save_wct(station1, station2, components, filterid, mov_stack, taxis, dvv_
'coh': coh_da
})

# Save the dataset to a NetCDF file
ds.to_netcdf(fn)
existing_ds = xr_create_or_open(fn, name="WCT")
updated_ds = xr_insert_or_update(existing_ds, ds)
xr_save_and_close(updated_ds, fn)


logging.debug(f"Saved WCT data to {fn}")
# Clean up
Expand Down
25 changes: 17 additions & 8 deletions msnoise/plots/wct_dvv.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
def plot_dvv_heatmap(data_type, dvv_df, pair, rolling, start, end, low, high, logger, mincoh=0.5):
# Extracting relevant data from dvv_df
dvv_df = dvv_df.loc[start:end]
if dvv_df.empty:
if dvv_df is None or dvv_df.empty:
logger.error(f"No data available for {pair} between {start} and {end}. Exiting function.")
return
return None, None
rolling_window = int(rolling)

dvv_freq = dvv_df['dvv']
Expand Down Expand Up @@ -50,7 +50,7 @@ def plot_dvv_heatmap(data_type, dvv_df, pair, rolling, start, end, low, high, lo
color_bar_label = 'Coherence value'
else:
logger.error("Unknown data type: %s, write 'dvv' or 'coh'? " % data_type)
return None, None, None
return None, None

#if current_config.get('plot_event', False):
# plot_events(ax, current_config['event_list'], start, end)
Expand All @@ -77,9 +77,9 @@ def plot_dvv_heatmap(data_type, dvv_df, pair, rolling, start, end, low, high, lo
def plot_dvv_scatter(dvv_df, pair, rolling, start, end, ranges, logger):
# Extracting relevant data from dvv_df
dvv_df = dvv_df.loc[start:end]
if dvv_df.empty:
if dvv_df is None or dvv_df.empty:
logger.error(f"No data available for {pair} between {start} and {end}. Exiting function.")
return
return None, None
rolling_window = int(rolling)

color = ['Blues', 'Reds','Greens','Greys'] #'Purples'
Expand Down Expand Up @@ -178,6 +178,9 @@ def xr_get_wct_pair(pair, components, filterid, mov_stack, logger):
logger.error("FILE DOES NOT EXIST: %s, skipping" % fn)

data = xr_create_or_open(fn, name="WCT")
if data is None:
logger.error(f"Empty file for pair {pair}.")
return None
data = data.to_dataframe().unstack(level='frequency')
return data

Expand Down Expand Up @@ -268,6 +271,9 @@ def main(mov_stackid=None, components='ZZ', filterid=1,
logger.error("FILE DOES NOT EXIST: %s, skipping" % fullpath)
continue
# Plotting
if dvv is None:
logger.error(f"No data available for {pairs}. Skipping plot.")
continue
if visualize == 'dvv':
fig, savename = plot_dvv_heatmap('dvv', dvv, pairs, rolling, start, end, low, high, logger, mincoh)
elif visualize == 'coh':
Expand All @@ -277,9 +283,12 @@ def main(mov_stackid=None, components='ZZ', filterid=1,
else:
logger.error("PLOT TYPE DOES NOT EXIST: %s" % visualize)
# Save and show the figure
save_figure(fig, savename, logger, mov_stacks, comps, filterid, visualize, plot_all_period=False, start=start, end=end, outfile=outfile)
if show:
plt.show()
if fig is not None :
save_figure(fig, savename, logger, mov_stacks, comps, filterid, visualize, plot_all_period=False, start=start, end=end, outfile=outfile)
if show:
plt.show()
else:
logger.error("Figure was not created. Skipping save.")

if __name__ == "__main__":
main()
Expand Down

0 comments on commit 3b331b5

Please sign in to comment.