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 initial_window_selection(self): """ Find all possible windows. This is equivalent to the setup_M_L_R() function in flexwin. """ for peak in self.peaks: # only continue if there are available minima on either side if peak <= self.troughs[0] or peak >= self.troughs[-1]: continue # only continue if this maximum is above the water level if self.stalta[peak] <= self.config.stalta_waterlevel[peak]: continue smaller_troughs = self.troughs[self.troughs < peak] larger_troughs = self.troughs[self.troughs > peak] for left, right in itertools.product(smaller_troughs, larger_troughs): self.windows.append( Window(left=left, right=right, center=peak, channel_id=self.observed.id, time_of_first_sample=self.synthetic.stats.starttime, dt=self.observed.stats.delta, min_period=self.config.min_period, weight_function=self.config.window_weight_fct)) logger.info("Initial window selection yielded %i possible windows." % len(self.windows))
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 dataset_windows_to_pyflex_windows(windows, network, station): """ Convert the parameter dictionary of an ASDFDataSet MisfitWindow into a dictionary of Pyflex Window objects, in the same format as Manager.windows Returns empty dict and 0 if no windows are found :type windows: pyasdf.utils.AuxiliaryDataAccessor :param windows: ds.auxiliary_data.MisfitWindows[iter][step] :type network: str :param network: network of the station related to the windows :type station: str :param station: station related to the windows :rtype: dict :return: dictionary of window attributes in the same format that Pyflex outputs """ window_dict, _num_windows = {}, 0 for window_name in windows.list(): net, sta, comp, n = window_name.split("_") # Check the title of the misfit window to see if applicable if (net == network) and (sta == station): par = windows[window_name].parameters # Create a Pyflex Window object window = Window(left=par["left_index"], right=par["right_index"], center=par["center_index"], dt=par["dt"], time_of_first_sample=UTCDateTime( par["time_of_first_sample"]), min_period=par["min_period"], channel_id=par["channel_id"]) # We cant initiate these parameters so set them after the fact # If data changed, should recalculate with Window._calc_criteria() setattr(window, "dlnA", par["dlnA"]) setattr(window, "cc_shift", par["cc_shift_in_samples"]) setattr(window, "max_cc_value", par["max_cc_value"]) # Save windows into the dictionary labelled by component if comp in window_dict.keys(): # Either append to existing entry window_dict[comp] += [window] else: # Or create the first entry window_dict[comp] = [window] _num_windows += 1 logger.debug(f"{_num_windows} window(s) found in dataset for " f"{network}.{station}") return window_dict
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