コード例 #1
0
ファイル: seismogram_analysis.py プロジェクト: ProjectISP/ISP
    def get_waveform(self, filter_error_callback=None, **kwargs):
        filter_value = kwargs.get("filter_value", Filters.Default)
        f_min = kwargs.get("f_min", 0.)
        f_max = kwargs.get("f_max", 0.)
        start_time = kwargs.get("start_time", self.stats.StartTime)
        end_time = kwargs.get("end_time", self.stats.EndTime)

        tr = self.tracer

        tr.detrend(type="demean")

        try:
            if not ObspyUtil.filter_trace(tr, filter_value, f_min, f_max):
                self.__send_filter_error_callback(
                    filter_error_callback, "Lower frequency {} must be "
                    "smaller than Upper frequency {}".format(f_min, f_max))
        except ValueError as e:
            print(e)
            self.__send_filter_error_callback(filter_error_callback, str(e))

        try:
            tr.trim(starttime=start_time, endtime=end_time)
        except:
            print("Please Check Starttime and Endtime")

        return tr
コード例 #2
0
    def plot_cwt_spectrogram(self, canvas: MatplotlibCanvas):
        tr = ObspyUtil.get_tracer_from_file(self.file_selector.file_path)
        ts, te = self.get_time_window()
        tr.trim(starttime=ts, endtime=te)
        tr.detrend(type="demean")
        fs = tr.stats.sampling_rate
        f_min = 1. / self.spectrum_box.win_bind.value if self.filter.min_freq == 0 else self.filter.min_freq
        f_max = self.filter.max_freq
        ObspyUtil.filter_trace(tr, self.filter.filter_value, f_min, f_max)
        nf = 40
        tt = int(self.spectrum_box.win_bind.value *
                 self.tracer_stats.Sampling_rate)
        wmin = self.spectrum_box.w1_bind.value
        wmax = self.spectrum_box.w2_bind.value
        npts = len(tr.data)
        [ba, nConv, frex,
         half_wave] = ccwt_ba_fast(npts, self.tracer_stats.Sampling_rate,
                                   f_min, f_max, wmin, wmax, tt, nf)
        cf, sc, scalogram = cwt_fast(tr.data, ba, nConv, frex, half_wave, fs)
        #scalogram = ccwt(tr.data, self.tracer_stats.Sampling_rate, f_min, f_max, wmin, wmax, tt, nf)

        scalogram = np.abs(scalogram)**2

        t = np.linspace(0, self.tracer_stats.Delta * npts, npts)
        scalogram2 = 10 * (np.log10(scalogram / np.max(scalogram)))
        x, y = np.meshgrid(t, np.linspace(f_min, f_max, scalogram2.shape[0]))

        max_cwt = np.max(scalogram2)
        min_cwt = np.min(scalogram2)
        canvas.plot(t[0:len(t) - 1],
                    cf,
                    0,
                    clear_plot=False,
                    is_twinx=True,
                    color="red",
                    linewidth=0.5)

        norm = Normalize(vmin=min_cwt, vmax=max_cwt)
        canvas.plot_contour(x,
                            y,
                            scalogram2,
                            axes_index=1,
                            clabel="Power [dB]",
                            levels=100,
                            cmap=plt.get_cmap("jet"),
                            norm=norm)
        canvas.set_xlabel(1, "Time (s)")
コード例 #3
0
ファイル: seismogram_analysis.py プロジェクト: ProjectISP/ISP
    def get_waveform_advanced(self,
                              parameters,
                              inventory,
                              filter_error_callback=None,
                              **kwargs):

        start_time = kwargs.get("start_time", self.stats.StartTime)
        end_time = kwargs.get("end_time", self.stats.EndTime)
        trace_number = kwargs.get("trace_number", 0)
        tr = self.tracer

        tr.trim(starttime=start_time, endtime=end_time)
        N = len(parameters)

        for j in range(N):

            if parameters[j][0] == 'rmean':

                tr.detrend(type=parameters[j][1])

            if parameters[j][0] == 'taper':

                tr.taper(max_percentage=parameters[j][2],
                         type=parameters[j][1])

            if parameters[j][0] == 'normalize':

                if parameters[j][1] == 0:
                    tr.normalize(norm=None)
                else:
                    tr.normalize(norm=parameters[j][1])

            if parameters[j][0] == "differentiate":
                tr.differentiate(method=parameters[j][1])

            if parameters[j][0] == "integrate":
                tr.integrate(method=parameters[j][1])

            if parameters[j][0] == 'filter':
                filter_value = parameters[j][1]
                f_min = parameters[j][2]
                f_max = parameters[j][3]
                zero_phase = parameters[j][4]
                poles = parameters[j][5]
                try:
                    if not ObspyUtil.filter_trace(tr,
                                                  filter_value,
                                                  f_min,
                                                  f_max,
                                                  corners=poles,
                                                  zerophase=zero_phase):
                        self.__send_filter_error_callback(
                            filter_error_callback,
                            "Lower frequency {} must be "
                            "smaller than Upper frequency {}".format(
                                f_min, f_max))
                except ValueError as e:
                    self.__send_filter_error_callback(filter_error_callback,
                                                      str(e))

            if parameters[j][0] == "wiener filter":
                print("applying wiener filter")
                time_window = parameters[j][1]
                noise_power = parameters[j][2]
                print(time_window, noise_power)
                tr = wiener_filter(tr,
                                   time_window=time_window,
                                   noise_power=noise_power)

            if parameters[j][0] == 'shift':
                shifts = parameters[j][1]
                for c, value in enumerate(shifts, 1):
                    if value[0] == trace_number:
                        tr.stats.starttime = tr.stats.starttime + value[1]

            if parameters[j][0] == 'remove response':

                f1 = parameters[j][1]
                f2 = parameters[j][2]
                f3 = parameters[j][3]
                f4 = parameters[j][4]
                water_level = parameters[j][5]
                units = parameters[j][6]
                pre_filt = (f1, f2, f3, f4)

                if inventory and units != "Wood Anderson":
                    #print("Deconvolving")
                    try:
                        tr.remove_response(inventory=inventory,
                                           pre_filt=pre_filt,
                                           output=units,
                                           water_level=water_level)
                    except:
                        print("Coudn't deconvolve", tr.stats)
                        tr.data = np.array([])

                elif inventory and units == "Wood Anderson":
                    #print("Simulating Wood Anderson Seismograph")
                    resp = inventory.get_response(tr.id, tr.stats.starttime)
                    resp = resp.response_stages[0]
                    paz_wa = {
                        'sensitivity': 2800,
                        'zeros': [0j],
                        'gain': 1,
                        'poles': [-6.2832 - 4.7124j, -6.2832 + 4.7124j]
                    }

                    paz_mine = {
                        'sensitivity':
                        resp.stage_gain * resp.normalization_factor,
                        'zeros': resp.zeros,
                        'gain': resp.stage_gain,
                        'poles': resp.poles
                    }

                    try:
                        tr.simulate(paz_remove=paz_mine,
                                    paz_simulate=paz_wa,
                                    water_level=water_level)
                    except:
                        print("Coudn't deconvolve", tr.stats)
                        tr.data = np.array([])

            if parameters[j][0] == 'add white noise':
                tr = add_white_noise(tr, parameters[j][1])

            if parameters[j][0] == 'whitening':
                tr = whiten(tr, parameters[j][1], taper_edge=parameters[j][2])

            if parameters[j][0] == 'remove spikes':
                tr = hampel(tr, parameters[j][1], parameters[j][2])

            if parameters[j][0] == 'time normalization':
                tr = normalize(tr,
                               norm_win=parameters[j][1],
                               norm_method=parameters[j][2])

            if parameters[j][0] == 'wavelet denoise':
                tr = wavelet_denoise(tr,
                                     dwt=parameters[j][1],
                                     threshold=parameters[j][2])

            if parameters[j][0] == 'resample':
                tr.resample(sampling_rate=parameters[j][1],
                            window='hanning',
                            no_filter=parameters[j][2])

            if parameters[j][0] == 'fill gaps':
                st = Stream(tr)
                st.merge(fill_value=parameters[j][1])
                tr = st[0]

            if parameters[j][0] == 'smoothing':

                tr = smoothing(tr,
                               type=parameters[j][1],
                               k=parameters[j][2],
                               fwhm=parameters[j][3])

        return tr