def test_read_window_json(): winfile_bm = os.path.join(DATA_DIR, "window", "IU.KBL..BHR.window.json") with open(winfile_bm) as fh: windows_json = json.load(fh) for _win_json_bm in windows_json: Window._load_from_json_content(_win_json_bm)
def test_window_on_trace_with_none_user_levels(): obs_tr = read(obsfile).select(channel="*R")[0] syn_tr = read(synfile).select(channel="*R")[0] config_file = os.path.join(DATA_DIR, "window", "27_60.BHZ.config.yaml") config = wio.load_window_config_yaml(config_file) cat = read_events(quakeml) inv = read_inventory(staxml) windows = win.window_on_trace(obs_tr, syn_tr, config, station=inv, event=cat, user_module="None", _verbose=False, figure_mode=False) winfile_bm = os.path.join(DATA_DIR, "window", "IU.KBL..BHR.window.json") parameters = ["left", "right", "center", "time_of_first_sample", "max_cc_value", "cc_shift", "dlnA", "dt", "min_period", "channel_id", "phase_arrivals", "weight_function"] with open(winfile_bm) as fh: windows_json = json.load(fh) for _win, _win_json_bm in zip(windows, windows_json): _win_bm = Window._load_from_json_content(_win_json_bm) assertWinAlmostEqual(_win, _win_bm, parameters)
def test_waveform_adjoint(): obs = read(obsfile).select(channel="*R")[0] syn = read(synfile).select(channel="*R")[0] with open(winfile) as fh: wins_json = json.load(fh) windows = [] for _win in wins_json: windows.append(Window._load_from_json_content(_win)) config_file = os.path.join(DATA_DIR, "adjoint", "waveform.adjoint.config.yaml") config = adj.load_adjoint_config_yaml(config_file) win_time, _ = adj._extract_window_time(windows) adjsrc = adj.calculate_adjsrc_on_trace( obs, syn, win_time, config, adj_src_type="waveform_misfit", adjoint_src_flag=True, figure_mode=False) # tr_adj = adj._convert_adj_to_trace(adjsrc, syn.stats.starttime, syn.id) # tr.write("%s.sac" % syn.id, format="SAC") # plot_adjoint_source(adjsrc, win_time, obs, syn) assert adjsrc
def test_window_on_trace(): obs_tr = read(obsfile).select(channel="*R")[0] syn_tr = read(synfile).select(channel="*R")[0] config_file = os.path.join(DATA_DIR, "window", "27_60.BHZ.config.yaml") config = wio.load_window_config_yaml(config_file) cat = readEvents(quakeml) inv = read_inventory(staxml) windows = win.window_on_trace(obs_tr, syn_tr, config, station=inv, event=cat, _verbose=False, figure_mode=False) assert len(windows) == 5 winfile_bm = os.path.join(DATA_DIR, "window", "IU.KBL..BHR.window.json") with open(winfile_bm) as fh: windows_json = json.load(fh) for _win, _win_json_bm in zip(windows, windows_json): _win_bm = Window._load_from_json_content(_win_json_bm) assert _win == _win_bm
def load(self, filename): """ Load windows from a JSON file and attach them to the current window selector object. :param filename: The filename or file-like object to load. :type filename: str or file-like object """ if hasattr(filename, "read"): obj = json.load(filename) else: if os.path.exists(filename): with open(filename, "r") as fh: obj = json.load(fh) else: obj = json.loads(filename) if "windows" not in obj: raise ValueError("Not a valid Windows JSON file.") windows = obj["windows"] window_objects = [] for win in windows: win_obj = Window._load_from_json_content(win) # Perform a number of checks. if win_obj.channel_id != self.observed.id: raise PyflexError( "The window has channel id '%s' whereas the observed " "data has channel id '%s'." % (win_obj.channel_id, self.observed.id)) if abs(win_obj.dt - self.observed.stats.delta) / \ self.observed.stats.delta >= 0.001: raise PyflexError( "The sample interval specified in the window is %g whereas" " the sample interval in the observed data is %g." % (win_obj.delta, self.observed_stats.delta)) if abs(win_obj.time_of_first_sample - self.observed.stats.starttime) > \ 0.5 * self.observed.stats.delta: raise PyflexError( "The window expects the data to start with at %s whereas " "the observed data starts at %s." % (win.time_of_first_sample, self.observed.stats.starttime)) # Collect in temporary list and not directly attach to not # modify the window object in case a later window raises an # exception. Either all or nothing. window_objects.append(win_obj) self.windows.extend(window_objects) # Recalculate window criteria. for win in self.windows: win._calc_criteria(self.observed.data, self.synthetic.data)
def setup_calculate_adjsrc_on_trace_args(): obs = read(obsfile).select(channel="*R")[0] syn = read(synfile).select(channel="*R")[0] with open(winfile) as fh: wins_json = json.load(fh) windows = [] for _win in wins_json: windows.append(Window._load_from_json_content(_win)) return obs, syn, windows
def setup_calculate_adjsrc_on_trace_args(): obs = read(obsfile).select(channel="*R")[0] syn = read(synfile).select(channel="*R")[0] with open(winfile) as fh: wins_json = json.load(fh) windows = [] for _win in wins_json: windows.append(Window._load_from_json_content(_win)) return obs, syn, windows
def test_window_on_trace(): obs_tr = read(obsfile).select(channel="*Z")[0] syn_tr = read(synfile).select(channel="*Z")[0] config_file = os.path.join(DATA_DIR, "27_60.BHZ.config.yaml") config = win.load_window_config_yaml(config_file) quakeml = os.path.join(DATA_DIR, "C201009031635A.xml") cat = readEvents(quakeml) windows = win.window_on_trace(obs_tr, syn_tr, config, station=inv, event=cat, _verbose=False, figure_mode=False) winfile_bm = os.path.join(DATA_DIR, "benchmark", "IU.KBL..BHZ.window.json") with open(winfile_bm) as fh: windows_json = json.load(fh) for _win, _win_json_bm in zip(windows, windows_json): _win_bm = Window._load_from_json_content(_win_json_bm) assert _win == _win_bm
def test_window_on_trace_with_none_user_levels(): obs_tr = read(obsfile).select(channel="*R")[0] syn_tr = read(synfile).select(channel="*R")[0] config_file = os.path.join(DATA_DIR, "window", "27_60.BHZ.config.yaml") config = wio.load_window_config_yaml(config_file) cat = readEvents(quakeml) inv = read_inventory(staxml) windows = win.window_on_trace(obs_tr, syn_tr, config, station=inv, event=cat, user_module="None", _verbose=False, figure_mode=False) winfile_bm = os.path.join(DATA_DIR, "window", "IU.KBL..BHR.window.json") with open(winfile_bm) as fh: windows_json = json.load(fh) for _win, _win_json_bm in zip(windows, windows_json): _win_bm = Window._load_from_json_content(_win_json_bm) assert _win == _win_bm
def test_read_window_json(): winfile_bm = os.path.join(DATA_DIR, "window", "IU.KBL..BHR.window.json") with open(winfile_bm) as fh: windows_json = json.load(fh) for _win_json_bm in windows_json: Window._load_from_json_content(_win_json_bm)