class TimeFrequencyFrame(BaseFrame, UiTimeFrequencyFrame):
    def __init__(self):

        super(TimeFrequencyFrame, self).__init__()
        self.setupUi(self)
        self.__stations_dir = None
        self.__metadata_manager = None
        self.inventory = {}
        self._stations_info = {}
        self.tr1 = []
        self.tr2 = []
        self.canvas_plot1 = MatplotlibCanvas(self.widget_plot_up, nrows=2)
        # self.canvas_plot1.set_xlabel(1, "Time (s)")
        # self.canvas_plot1.set_ylabel(0, "Amplitude ")
        # self.canvas_plot1.set_ylabel(1, "Frequency (Hz)")
        self.canvas_plot2 = MatplotlibCanvas(self.widget_plot_down, nrows=2)
        # self.canvas_plot2.set_xlabel(1, "Time (s)")
        # self.canvas_plot2.set_ylabel(0, "Amplitude ")
        # self.canvas_plot2.set_ylabel(1, "Frequency (Hz)")
        # Binding
        self.canvas_plot1.mpl_connect('key_press_event', self.key_pressed)
        self.canvas_plot2.mpl_connect('key_press_event', self.key_pressed)
        self.root_path_bind = BindPyqtObject(self.rootPathForm,
                                             self.onChange_root_path)
        self.dataless_path_bind = BindPyqtObject(self.datalessPathForm)
        self.metadata_path_bind = BindPyqtObject(self.datalessPathForm,
                                                 self.onChange_metadata_path)
        # Add file selector to the widget
        self.file_selector = FilesView(
            self.root_path_bind.value,
            parent=self.fileSelectorWidget,
            on_change_file_callback=lambda file_path: self.onChange_file(
                file_path))
        # Binds
        self.selectDirBtn.clicked.connect(
            lambda: self.on_click_select_directory(self.root_path_bind))
        self.datalessBtn.clicked.connect(
            lambda: self.on_click_select_file(self.dataless_path_bind))
        # Action Buttons
        self.actionSettings.triggered.connect(
            lambda: self.open_parameters_settings())
        self.actionOpen_Help.triggered.connect(lambda: self.open_help())
        self.actionOpen_Spectral_Analysis.triggered.connect(
            self.time_frequency_advance)
        self.plotBtn.clicked.connect(self.plot_seismogram)
        self.stationsBtn.clicked.connect(self.stations_info)

        # help Documentation
        self.help = HelpDoc()

        # Parameters settings
        self.parameters = ParametersSettings()

        # Time Frequency Advance
        #self.time_frequency_advance = TimeFrequencyAdvance()

    def filter_error_message(self, msg):
        md = MessageDialog(self)
        md.set_info_message(msg)

    def message_dataless_not_found(self):
        if len(self.dataless_not_found) > 1:
            md = MessageDialog(self)
            md.set_info_message("Metadata not found.")
        else:
            for file in self.dataless_not_found:
                md = MessageDialog(self)
                md.set_info_message("Metadata for {} not found.".format(file))

        self.dataless_not_found.clear()

    def open_parameters_settings(self):
        self.parameters.show()

    def time_frequency_advance(self):
        self._time_frequency_advance = TimeFrequencyAdvance(self.tr1, self.tr2)
        self._time_frequency_advance.show()

    def validate_file(self):
        if not MseedUtil.is_valid_mseed(self.file_selector.file_path):
            msg = "The file {} is not a valid mseed. Please, choose a valid format". \
                format(self.file_selector.file_name)
            raise InvalidFile(msg)

    def onChange_root_path(self, value):
        """
        Fired every time the root_path is changed

        :param value: The path of the new directory.

        :return:
        """
        self.file_selector.set_new_rootPath(value)

    def onChange_file(self, file_path):
        # Called every time user select a different file
        pass

    def on_click_select_directory(self, bind: BindPyqtObject):
        if "darwin" == platform:
            dir_path = pw.QFileDialog.getExistingDirectory(
                self, 'Select Directory', bind.value)
        else:
            dir_path = pw.QFileDialog.getExistingDirectory(
                self, 'Select Directory', bind.value,
                pw.QFileDialog.DontUseNativeDialog)
        if dir_path:
            bind.value = dir_path

    def on_click_select_file(self, bind: BindPyqtObject):
        selected = pw.QFileDialog.getOpenFileName(self, "Select metadata file")
        if isinstance(selected[0], str) and os.path.isfile(selected[0]):
            bind.value = selected[0]

    def onChange_metadata_path(self, value):
        md = MessageDialog(self)
        try:
            self.__metadata_manager = MetadataManager(value)
            self.inventory = self.__metadata_manager.get_inventory()
            md.set_info_message(
                "Loaded Metadata, please check your terminal for further details"
            )
        except:
            md.set_error_message(
                "Something went wrong. Please check your metada file is a correct one"
            )

    @property
    def trace(self):
        return ObspyUtil.get_tracer_from_file(self.file_selector.file_path)

    def get_data(self):
        file = self.file_selector.file_path
        starttime = convert_qdatetime_utcdatetime(self.starttime_date)
        endtime = convert_qdatetime_utcdatetime(self.endtime_date)
        diff = endtime - starttime
        parameters = self.parameters.getParameters()
        sd = SeismogramDataAdvanced(file)

        if self.trimCB.isChecked() and diff >= 0:
            tr = sd.get_waveform_advanced(
                parameters,
                self.inventory,
                filter_error_callback=self.filter_error_message,
                start_time=starttime,
                end_time=endtime)
        else:
            tr = sd.get_waveform_advanced(
                parameters,
                self.inventory,
                filter_error_callback=self.filter_error_message)

        t = tr.times()
        return tr, t

    def get_time_window(self):

        t1 = convert_qdatetime_utcdatetime(self.starttime_date)
        t2 = convert_qdatetime_utcdatetime(self.endtime_date)

        return t1, t2

    def stations_info(self):
        obsfiles = MseedUtil.get_mseed_files(self.root_path_bind.value)
        obsfiles.sort()
        sd = []
        for file in obsfiles:
            st = SeismogramDataAdvanced(file)
            station = [
                st.stats.Network, st.stats.Station, st.stats.Location,
                st.stats.Channel, st.stats.StartTime, st.stats.EndTime,
                st.stats.Sampling_rate, st.stats.Npts
            ]
            sd.append(station)
        self._stations_info = StationsInfo(sd, check=False)
        self._stations_info.show()

    def plot_seismogram(self):
        selection = self.selectCB.currentText()

        if selection == "Seismogram 1":
            #self.validate_file()
            [self.tr1, t] = self.get_data()
            self.canvas_plot1.plot(t,
                                   self.tr1.data,
                                   0,
                                   clear_plot=True,
                                   color="black",
                                   linewidth=0.5)
            self.canvas_plot1.set_xlabel(1, "Time (s)")
            self.canvas_plot1.set_ylabel(0, "Amplitude ")
            self.canvas_plot1.set_ylabel(1, "Frequency (Hz)")
            info = "{}.{}.{}".format(self.tr1.stats.network,
                                     self.tr1.stats.station,
                                     self.tr1.stats.channel)
            self.canvas_plot1.set_plot_label(0, info)

            if self.time_frequencyChB.isChecked():
                self.time_frequency(self.tr1, selection)

        if selection == "Seismogram 2":
            #self.validate_file()
            [self.tr2, t] = self.get_data()
            self.canvas_plot2.plot(t,
                                   self.tr2.data,
                                   0,
                                   clear_plot=True,
                                   color="black",
                                   linewidth=0.5)
            self.canvas_plot2.set_xlabel(1, "Time (s)")
            self.canvas_plot2.set_ylabel(0, "Amplitude ")
            self.canvas_plot2.set_ylabel(1, "Frequency (Hz)")
            info = "{}.{}.{}".format(self.tr2.stats.network,
                                     self.tr2.stats.station,
                                     self.tr2.stats.channel)
            self.canvas_plot2.set_plot_label(0, info)

            if self.time_frequencyChB.isChecked():
                self.time_frequency(self.tr2, selection)

    @AsycTime.run_async()
    def time_frequency(self, tr, order):
        selection = self.time_frequencyCB.currentText()
        ts, te = self.get_time_window()
        diff = te - ts
        if selection == "Multitaper Spectrogram":

            win = int(self.mt_window_lengthDB.value() * tr.stats.sampling_rate)
            tbp = self.time_bandwidth_DB.value()
            ntapers = self.number_tapers_mtSB.value()
            f_min = self.freq_min_mtDB.value()
            f_max = self.freq_max_mtDB.value()
            mtspectrogram = MTspectrogram(self.file_selector.file_path, win,
                                          tbp, ntapers, f_min, f_max)

            if self.trimCB.isChecked() and diff >= 0:
                x, y, log_spectrogram = mtspectrogram.compute_spectrogram(
                    tr, start_time=ts, end_time=te)
            else:
                x, y, log_spectrogram = mtspectrogram.compute_spectrogram(tr)

            log_spectrogram = np.clip(log_spectrogram,
                                      a_min=self.minlevelCB.value(),
                                      a_max=0)
            min_log_spectrogram = self.minlevelCB.value()
            max_log_spectrogram = 0

            if order == "Seismogram 1":
                if self.typeCB.currentText() == 'contourf':

                    self.canvas_plot1.plot_contour(x,
                                                   y,
                                                   log_spectrogram,
                                                   axes_index=1,
                                                   clabel="Power [dB]",
                                                   cmap=plt.get_cmap("jet"),
                                                   vmin=min_log_spectrogram,
                                                   vmax=max_log_spectrogram)
                elif self.typeCB.currentText() == 'pcolormesh':

                    print("plotting pcolormesh")
                    self.canvas_plot1.pcolormesh(x,
                                                 y,
                                                 log_spectrogram,
                                                 axes_index=1,
                                                 clabel="Power [dB]",
                                                 cmap=plt.get_cmap("jet"),
                                                 vmin=min_log_spectrogram,
                                                 vmax=max_log_spectrogram)
                self.canvas_plot1.set_xlabel(1, "Time (s)")
                self.canvas_plot1.set_ylabel(0, "Amplitude ")
                self.canvas_plot1.set_ylabel(1, "Frequency (Hz)")

            elif order == "Seismogram 2":
                if self.typeCB.currentText() == 'contourf':

                    self.canvas_plot2.plot_contour(x,
                                                   y,
                                                   log_spectrogram,
                                                   axes_index=1,
                                                   clear_plot=True,
                                                   clabel="Power [dB]",
                                                   cmap=plt.get_cmap("jet"),
                                                   vmin=min_log_spectrogram,
                                                   vmax=max_log_spectrogram)
                elif self.typeCB.currentText() == 'pcolormesh':

                    self.canvas_plot2.pcolormesh(x,
                                                 y,
                                                 log_spectrogram,
                                                 axes_index=1,
                                                 clear_plot=True,
                                                 clabel="Power [dB]",
                                                 cmap=plt.get_cmap("jet"),
                                                 vmin=min_log_spectrogram,
                                                 vmax=max_log_spectrogram)

                self.canvas_plot2.set_xlabel(1, "Time (s)")
                self.canvas_plot2.set_ylabel(0, "Amplitude ")
                self.canvas_plot2.set_ylabel(1, "Frequency (Hz)")

        elif selection == "Wigner Spectrogram":

            win = int(self.mt_window_lengthDB.value() * tr.stats.sampling_rate)
            tbp = self.time_bandwidth_DB.value()
            ntapers = self.number_tapers_mtSB.value()
            f_min = self.freq_min_mtDB.value()
            f_max = self.freq_max_mtDB.value()
            wignerspec = WignerVille(self.file_selector.file_path, win, tbp,
                                     ntapers, f_min, f_max)

            if self.trimCB.isChecked() and diff >= 0:
                x, y, log_spectrogram = wignerspec.compute_wigner_spectrogram(
                    tr, start_time=ts, end_time=te)
            else:
                x, y, log_spectrogram = wignerspec.compute_wigner_spectrogram(
                    tr)

            if order == "Seismogram 1":
                if self.typeCB.currentText() == 'contourf':
                    self.canvas_plot1.plot_contour(x,
                                                   y,
                                                   log_spectrogram,
                                                   axes_index=1,
                                                   clear_plot=True,
                                                   clabel="Rel Power ",
                                                   cmap=plt.get_cmap("jet"))
                elif self.typeCB.currentText() == 'pcolormesh':
                    self.canvas_plot1.pcolormesh(x,
                                                 y,
                                                 log_spectrogram,
                                                 axes_index=1,
                                                 clear_plot=True,
                                                 clabel="Rel Power ",
                                                 cmap=plt.get_cmap("jet"))
                self.canvas_plot1.set_xlabel(1, "Time (s)")
                self.canvas_plot1.set_ylabel(0, "Amplitude ")
                self.canvas_plot1.set_ylabel(1, "Frequency (Hz)")

            elif order == "Seismogram 2":
                if self.typeCB.currentText() == 'contourf':
                    self.canvas_plot2.plot_contour(x,
                                                   y,
                                                   log_spectrogram,
                                                   axes_index=1,
                                                   clear_plot=True,
                                                   clabel="Power [dB]",
                                                   cmap=plt.get_cmap("jet"))
                elif self.typeCB.currentText() == 'pcolormesh':
                    self.canvas_plot2.pcolormesh(x,
                                                 y,
                                                 log_spectrogram,
                                                 axes_index=1,
                                                 clear_plot=True,
                                                 clabel="Power [dB]",
                                                 cmap=plt.get_cmap("jet"))
                self.canvas_plot2.set_xlabel(1, "Time (s)")
                self.canvas_plot2.set_ylabel(0, "Amplitude ")
                self.canvas_plot2.set_ylabel(1, "Frequency (Hz)")

        elif selection == "Continuous Wavelet Transform":

            fs = tr.stats.sampling_rate
            nf = self.atomsSB.value()
            f_min = self.freq_min_cwtDB.value()
            f_max = self.freq_max_cwtDB.value()
            wmin = self.wminSB.value()
            wmax = self.wminSB.value()
            #tt = int( self.wavelet_lenghtDB.value()*fs)
            npts = len(tr.data)
            t = np.linspace(0, tr.stats.delta * npts, npts)
            #cw = ConvolveWaveletScipy(self.file_selector.file_path)
            cw = ConvolveWaveletScipy(tr)
            wavelet = self.wavelet_typeCB.currentText()

            m = self.wavelets_param.value()
            if self.trimCB.isChecked() and diff >= 0:

                cw.setup_wavelet(ts,
                                 te,
                                 wmin=wmin,
                                 wmax=wmax,
                                 tt=int(fs / f_min),
                                 fmin=f_min,
                                 fmax=f_max,
                                 nf=nf,
                                 use_wavelet=wavelet,
                                 m=m,
                                 decimate=False)
            else:
                cw.setup_wavelet(wmin=wmin,
                                 wmax=wmax,
                                 tt=int(fs / f_min),
                                 fmin=f_min,
                                 fmax=f_max,
                                 nf=nf,
                                 use_wavelet=wavelet,
                                 m=m,
                                 decimate=False)

            scalogram2 = cw.scalogram_in_dbs()
            scalogram2 = np.clip(scalogram2,
                                 a_min=self.minlevelCB.value(),
                                 a_max=0)
            cf = cw.cf_lowpass()
            freq = np.logspace(np.log10(f_min), np.log10(f_max))
            k = wmin / (2 * np.pi * freq)
            delay = int(fs * np.mean(k))
            x, y = np.meshgrid(
                t,
                np.logspace(np.log10(f_min), np.log10(f_max),
                            scalogram2.shape[0]))
            c_f = wmin / 2 * math.pi
            f = np.linspace((f_min), (f_max), scalogram2.shape[0])
            pred = (math.sqrt(2) * c_f / f) - (math.sqrt(2) * c_f / f_max)

            pred_comp = t[len(t) - 1] - pred
            min_cwt = self.minlevelCB.value()
            max_cwt = 0

            norm = Normalize(vmin=min_cwt, vmax=max_cwt)

            tf = t[delay:len(t)]
            cf = cf[0:len(tf)]
            if order == "Seismogram 1":

                #self.canvas_plot1.plot(tf, cf, 0, clear_plot=True, is_twinx=True, color="red",
                #                       linewidth=0.5)
                if self.typeCB.currentText() == 'pcolormesh':
                    self.canvas_plot1.pcolormesh(x,
                                                 y,
                                                 scalogram2,
                                                 axes_index=1,
                                                 clear_plot=True,
                                                 clabel="Power [dB]",
                                                 cmap=plt.get_cmap("jet"),
                                                 vmin=min_cwt,
                                                 vmax=max_cwt)
                elif self.typeCB.currentText() == 'contourf':
                    self.canvas_plot1.plot_contour(x,
                                                   y,
                                                   scalogram2,
                                                   axes_index=1,
                                                   clear_plot=True,
                                                   clabel="Power [dB]",
                                                   cmap=plt.get_cmap("jet"),
                                                   vmin=min_cwt,
                                                   vmax=max_cwt)

                ax_cone = self.canvas_plot1.get_axe(1)
                ax_cone.fill_between(pred,
                                     f,
                                     0,
                                     color="black",
                                     edgecolor="red",
                                     alpha=0.3)
                ax_cone.fill_between(pred_comp,
                                     f,
                                     0,
                                     color="black",
                                     edgecolor="red",
                                     alpha=0.3)
                self.canvas_plot1.set_xlabel(1, "Time (s)")
                self.canvas_plot1.set_ylabel(0, "Amplitude ")
                self.canvas_plot1.set_ylabel(1, "Frequency (Hz)")

            if order == "Seismogram 2":

                #self.canvas_plot2.plot(tf, cf, 0, clear_plot=True, is_twinx=True, color="red",
                #                       linewidth=0.5)

                if self.typeCB.currentText() == 'pcolormesh':
                    self.canvas_plot2.pcolormesh(x,
                                                 y,
                                                 scalogram2,
                                                 axes_index=1,
                                                 clear_plot=True,
                                                 clabel="Power [dB]",
                                                 cmap=plt.get_cmap("jet"),
                                                 vmin=min_cwt,
                                                 vmax=max_cwt)
                elif self.typeCB.currentText() == 'contourf':
                    self.canvas_plot2.plot_contour(x,
                                                   y,
                                                   scalogram2,
                                                   axes_index=1,
                                                   clear_plot=True,
                                                   clabel="Power [dB]",
                                                   cmap=plt.get_cmap("jet"),
                                                   vmin=min_cwt,
                                                   vmax=max_cwt)

                ax_cone2 = self.canvas_plot2.get_axe(1)
                ax_cone2.fill_between(pred,
                                      f,
                                      0,
                                      color="black",
                                      edgecolor="red",
                                      alpha=0.3)
                ax_cone2.fill_between(pred_comp,
                                      f,
                                      0,
                                      color="black",
                                      edgecolor="red",
                                      alpha=0.3)
                self.canvas_plot2.set_xlabel(1, "Time (s)")
                self.canvas_plot2.set_ylabel(0, "Amplitude ")
                self.canvas_plot2.set_ylabel(1, "Frequency (Hz)")

        else:
            pass

    def key_pressed(self, event):
        selection = self.selectCB.currentText()

        if event.key == 'w':
            self.plot_seismogram()

        if event.key == 'q':
            if selection == "Seismogram 1":
                [tr, t] = self.get_data()
                x1, y1 = event.xdata, event.ydata
                tt = tr.stats.starttime + x1
                set_qdatetime(tt, self.starttime_date)
                self.canvas_plot1.draw_arrow(x1,
                                             0,
                                             arrow_label="st",
                                             color="purple",
                                             linestyles='--',
                                             picker=False)
            elif selection == "Seismogram 2":
                [tr, t] = self.get_data()
                x1, y1 = event.xdata, event.ydata
                tt = tr.stats.starttime + x1
                set_qdatetime(tt, self.starttime_date)
                self.canvas_plot2.draw_arrow(x1,
                                             0,
                                             arrow_label="st",
                                             color="purple",
                                             linestyles='--',
                                             picker=False)

        if event.key == 'e':

            if selection == "Seismogram 1":
                [tr, t] = self.get_data()
                x1, y1 = event.xdata, event.ydata
                tt = tr.stats.starttime + x1
                set_qdatetime(tt, self.endtime_date)
                self.canvas_plot1.draw_arrow(x1,
                                             0,
                                             arrow_label="et",
                                             color="purple",
                                             linestyles='--',
                                             picker=False)
            elif selection == "Seismogram 2":
                [tr, t] = self.get_data()
                x1, y1 = event.xdata, event.ydata
                tt = tr.stats.starttime + x1
                set_qdatetime(tt, self.endtime_date)
                self.canvas_plot2.draw_arrow(x1,
                                             0,
                                             arrow_label="et",
                                             color="purple",
                                             linestyles='--',
                                             picker=False)

    def open_help(self):
        self.help.show()
Beispiel #2
0
class Earthquake3CFrame(pw.QFrame, UiEarthquake3CFrame):
    def __init__(self, parent: pw.QWidget):

        super(Earthquake3CFrame, self).__init__(parent)

        self.setupUi(self)
        ParentWidget.set_parent(parent, self)
        #Initialize parametrs for plot rotation
        self._z = {}
        self._r = {}
        self._t = {}
        self._st = {}
        self.inventory = {}
        #self.filter_3ca = FilterBox(self.toolQFrame, 1)  # add filter box component.
        self.parameters = ParametersSettings()

        # 3C_Component
        self.canvas = MatplotlibCanvas(self.plotMatWidget_3C)
        self.canvas.set_new_subplot(3, ncols=1)
        self.canvas_pol = MatplotlibCanvas(self.Widget_polarization)
        self.canvas.mpl_connect('key_press_event', self.key_pressed)
        # binds
        self.root_path_bind_3C = BindPyqtObject(self.rootPathForm_3C,
                                                self.onChange_root_path_3C)
        self.degreeSB_bind = BindPyqtObject(self.degreeSB)
        self.vertical_form_bind = BindPyqtObject(self.verticalQLineEdit)
        self.north_form_bind = BindPyqtObject(self.northQLineEdit)
        self.east_form_bind = BindPyqtObject(self.eastQLineEdit)

        # accept drops
        self.vertical_form_bind.accept_dragFile(
            drop_event_callback=self.drop_event)
        self.north_form_bind.accept_dragFile(
            drop_event_callback=self.drop_event)
        self.east_form_bind.accept_dragFile(
            drop_event_callback=self.drop_event)

        # Add file selector to the widget
        self.file_selector = FilesView(self.root_path_bind_3C.value,
                                       parent=self.fileSelectorWidget)
        self.file_selector.setDragEnabled(True)

        self.selectDirBtn_3C.clicked.connect(self.on_click_select_directory_3C)
        self.rotateplotBtn.clicked.connect(
            lambda: self.on_click_rotate(self.canvas))
        self.rot_macroBtn.clicked.connect(
            lambda: self.open_parameters_settings())
        self.polarizationBtn.clicked.connect(self.on_click_polarization)
        ###
        self.plotpolBtn.clicked.connect(self.plot_particle_motion)
        self.stationsBtn.clicked.connect(self.stationsInfo)
        self.save_rotatedBtn.clicked.connect(self.save_rotated)
        ###

    def open_parameters_settings(self):
        self.parameters.show()

    def info_message(self, msg):
        md = MessageDialog(self)
        md.set_info_message(msg)

    @staticmethod
    def drop_event(event: pqg.QDropEvent, bind_object: BindPyqtObject):
        data = event.mimeData()
        url = data.urls()[0]
        bind_object.value = url.fileName()

    @property
    def north_component_file(self):
        return os.path.join(self.root_path_bind_3C.value,
                            self.north_form_bind.value)

    @property
    def vertical_component_file(self):
        return os.path.join(self.root_path_bind_3C.value,
                            self.vertical_form_bind.value)

    @property
    def east_component_file(self):
        return os.path.join(self.root_path_bind_3C.value,
                            self.east_form_bind.value)

    def onChange_root_path_3C(self, value):
        """
        Fired every time the root_path is changed

        :param value: The path of the new directory.

        :return:
        """
        self.file_selector.set_new_rootPath(value)

    # Function added for 3C Components
    def on_click_select_directory_3C(self):

        if "darwin" == platform:
            dir_path = pw.QFileDialog.getExistingDirectory(
                self, 'Select Directory', self.root_path_bind_3C.value)
        else:
            dir_path = pw.QFileDialog.getExistingDirectory(
                self, 'Select Directory', self.root_path_bind_3C.value,
                pw.QFileDialog.DontUseNativeDialog)

        if dir_path:
            self.root_path_bind_3C.value = dir_path

    def set_times(self, st):

        max_start = np.max([tr.stats.starttime for tr in st])
        min_end = np.min([tr.stats.endtime for tr in st])

        return min_end, max_start

    def on_click_rotate(self, canvas):

        time1 = convert_qdatetime_utcdatetime(self.dateTimeEdit_4)
        time2 = convert_qdatetime_utcdatetime(self.dateTimeEdit_5)
        angle = self.degreeSB.value()
        incidence_angle = self.incidenceSB.value()
        method = self.methodCB.currentText()
        parameters = self.parameters.getParameters()

        try:
            sd = PolarizationAnalyis(self.vertical_component_file,
                                     self.north_component_file,
                                     self.east_component_file)

            time, z, r, t, st = sd.rotate(self.inventory,
                                          time1,
                                          time2,
                                          angle,
                                          incidence_angle,
                                          method=method,
                                          parameters=parameters,
                                          trim=True)
            self._z = z
            self._r = r
            self._t = t
            self._st = st
            rotated_seismograms = [z, r, t]
            for index, data in enumerate(rotated_seismograms):
                self.canvas.plot(time,
                                 data,
                                 index,
                                 color="black",
                                 linewidth=0.5)
                info = "{}.{}.{}".format(self._st[index].stats.network,
                                         self._st[index].stats.station,
                                         self._st[index].stats.channel)
                ax = self.canvas.get_axe(0)
                ax.set_xlim(sd.t1.matplotlib_date, sd.t2.matplotlib_date)
                formatter = mdt.DateFormatter('%Y/%m/%d/%H:%M:%S')
                ax.xaxis.set_major_formatter(formatter)
                self.canvas.set_plot_label(index, info)

            canvas.set_xlabel(2, "Time (s)")

        except InvalidFile:
            self.info_message(
                "Invalid mseed files. Please, make sure to select all the three components (Z, N, E) "
                "for rotate.")
        except ValueError as error:
            self.info_message(str(error))

    def on_click_polarization(self):
        time1 = convert_qdatetime_utcdatetime(self.dateTimeEdit_4)
        time2 = convert_qdatetime_utcdatetime(self.dateTimeEdit_5)
        sd = PolarizationAnalyis(self.vertical_component_file,
                                 self.north_component_file,
                                 self.east_component_file)
        try:
            var = sd.polarize(time1, time2, self.doubleSpinBox_winlen.value(),
                              self.freq_minDB.value(), self.freq_maxDB.value())

            artist = self.canvas_pol.plot(
                var['time'],
                var[self.comboBox_yaxis.currentText()],
                0,
                clear_plot=True,
                linewidth=0.5)
            self.canvas_pol.set_xlabel(0, "Time [s]")
            self.canvas_pol.set_ylabel(0, self.comboBox_yaxis.currentText())
            self.canvas_pol.set_yaxis_color(self.canvas_pol.get_axe(0),
                                            artist.get_color(),
                                            is_left=True)
            self.canvas_pol.plot(var['time'],
                                 var[self.comboBox_polarity.currentText()],
                                 0,
                                 is_twinx=True,
                                 color="red",
                                 linewidth=0.5)
            self.canvas_pol.set_ylabel_twinx(
                0, self.comboBox_polarity.currentText())
        except InvalidFile:
            self.info_message(
                "Invalid mseed files. Please, make sure to select all the three components (Z, N, E) "
                "for polarization.")
        except ValueError as error:
            self.info_message(str(error))

    def plot_particle_motion(self):
        self._plot_polarization = PlotPolarization(self._z, self._r, self._t)
        self._plot_polarization.show()

    def stationsInfo(self):
        files = []
        try:
            if self.vertical_component_file and self.north_component_file and self.east_component_file:
                files = [
                    self.vertical_component_file, self.north_component_file,
                    self.east_component_file
                ]
        except:
            pass

        sd = []
        if len(files) == 3:
            for file in files:
                try:
                    st = SeismogramDataAdvanced(file)

                    station = [
                        st.stats.Network, st.stats.Station, st.stats.Location,
                        st.stats.Channel, st.stats.StartTime, st.stats.EndTime,
                        st.stats.Sampling_rate, st.stats.Npts
                    ]

                    sd.append(station)
                except:
                    pass

            self._stations_info = StationsInfo(sd)
            self._stations_info.show()

    def save_rotated(self):
        import os
        root_path = os.path.dirname(os.path.abspath(__file__))

        if "darwin" == platform:
            dir_path = pw.QFileDialog.getExistingDirectory(
                self, 'Select Directory', root_path)
        else:
            dir_path = pw.QFileDialog.getExistingDirectory(
                self, 'Select Directory', root_path,
                pw.QFileDialog.DontUseNativeDialog)
        if self._st:
            n = len(self._st)
            for j in range(n):
                tr = self._st[j]
                t1 = tr.stats.starttime
                id = tr.id + "." + "D" + "." + str(t1.year) + "." + str(
                    t1.julday)
                print(tr.id, "Writing data processed")
                path_output = os.path.join(dir_path, id)
                tr.write(path_output, format="MSEED")

    def key_pressed(self, event):

        if event.key == 'q':
            x1, y1 = event.xdata, event.ydata
            tt = UTCDateTime(mdt.num2date(x1))
            set_qdatetime(tt, self.dateTimeEdit_4)
            self.canvas.draw_arrow(x1,
                                   0,
                                   arrow_label="st",
                                   color="purple",
                                   linestyles='--',
                                   picker=False)
            self.canvas.draw_arrow(x1,
                                   1,
                                   arrow_label="st",
                                   color="purple",
                                   linestyles='--',
                                   picker=False)
            self.canvas.draw_arrow(x1,
                                   2,
                                   arrow_label="st",
                                   color="purple",
                                   linestyles='--',
                                   picker=False)

        if event.key == 'e':
            x1, y1 = event.xdata, event.ydata
            tt = UTCDateTime(mdt.num2date(x1))
            set_qdatetime(tt, self.dateTimeEdit_5)
            self.canvas.draw_arrow(x1,
                                   0,
                                   arrow_label="et",
                                   color="purple",
                                   linestyles='--',
                                   picker=False)
            self.canvas.draw_arrow(x1,
                                   1,
                                   arrow_label="st",
                                   color="purple",
                                   linestyles='--',
                                   picker=False)
            self.canvas.draw_arrow(x1,
                                   2,
                                   arrow_label="st",
                                   color="purple",
                                   linestyles='--',
                                   picker=False)
Beispiel #3
0
class RealTimeFrame(BaseFrame, UiRealTimeFrame):
    def __init__(self):
        super(RealTimeFrame, self).__init__()
        self.setupUi(self)
        self.setWindowIcon(pqg.QIcon(':\\icons\\map-icon.png'))
        self.widget_map = None
        self.settings_dialog = SettingsDialog(self)
        self.inventory = {}
        self.files = []
        self.events_times = []
        self.total_items = 0
        self.items_per_page = 1
        self.__dataless_manager = None
        self.__metadata_manager = None
        self.st = None
        self.client = None
        self.stations_available = []
        self.data_dict = {}
        self.dataless_not_found = set(
        )  # a set of mseed files that the dataless couldn't find.
        self.metadata_path_bind = BindPyqtObject(self.datalessPathForm,
                                                 self.onChange_metadata_path)
        self.canvas = MatplotlibCanvas(self.plotMatWidget,
                                       nrows=self.numTracesCB.value(),
                                       constrained_layout=False)
        self.canvas.figure.tight_layout()
        self.timer_outdated = pyc.QTimer()
        self.timer_outdated.setInterval(1000)  # 1 second
        self.timer_outdated.timeout.connect(self.outdated_stations)

        # Binding
        self.root_path_bind = BindPyqtObject(self.rootPathForm)
        self.dataless_path_bind = BindPyqtObject(self.datalessPathForm)

        # Bind

        self.selectDirBtn.clicked.connect(
            lambda: self.on_click_select_directory(self.root_path_bind))

        #self.selectDatalessDirBtn.clicked.connect(lambda: self.on_click_select_directory(self.dataless_path_bind))

        self.metadata_path_bind = BindPyqtObject(self.datalessPathForm,
                                                 self.onChange_metadata_path)
        self.selectDatalessDirBtn.clicked.connect(
            lambda: self.on_click_select_metadata_file(self.metadata_path_bind
                                                       ))

        self.actionSet_Parameters.triggered.connect(
            lambda: self.open_parameters_settings())
        self.mapBtn.clicked.connect(self.show_map)
        # self.__metadata_manager = MetadataManager(self.dataless_path_bind.value)
        self.actionSet_Parameters.triggered.connect(
            lambda: self.open_parameters_settings())
        self.actionArray_Anlysis.triggered.connect(self.open_array_analysis)
        self.actionMoment_Tensor_Inversion.triggered.connect(
            self.open_moment_tensor)
        self.actionTime_Frequency_Analysis.triggered.connect(
            self.time_frequency_analysis)
        self.actionReceiver_Functions.triggered.connect(
            self.open_receiver_functions)
        self.actionOpen_Settings.triggered.connect(
            lambda: self.settings_dialog.show())
        self.actionOpen_Help.triggered.connect(lambda: self.open_help())
        self.RetrieveBtn.clicked.connect(self.retrieve_data)
        self.stopBtn.clicked.connect(self.stop)
        # Parameters settings
        self.parameters = ParametersSettings()
        # Earth Model Viewer
        self.earthmodel = EarthModelViewer()
        # help Documentation
        self.help = HelpDoc()

        # shortcuts

    @property
    def dataless_manager(self):
        if not self.__dataless_manager:
            self.__dataless_manager = DatalessManager(
                self.dataless_path_bind.value)
        return self.__dataless_manager

    def message_dataless_not_found(self):
        if len(self.dataless_not_found) > 1:
            md = MessageDialog(self)
            md.set_info_message("Metadata not found.")
        else:
            for file in self.dataless_not_found:
                md = MessageDialog(self)
                md.set_info_message("Metadata for {} not found.".format(file))

        self.dataless_not_found.clear()

    def subprocess_feedback(self, err_msg: str, set_default_complete=True):
        """
        This method is used as a subprocess feedback. It runs when a raise expect is detected.

        :param err_msg: The error message from the except.
        :param set_default_complete: If True it will set a completed successfully message. Otherwise nothing will
            be displayed.
        :return:
        """
        if err_msg:
            md = MessageDialog(self)
            if "Error code" in err_msg:
                md.set_error_message(
                    "Click in show details detail for more info.", err_msg)
            else:
                md.set_warning_message("Click in show details for more info.",
                                       err_msg)
        else:
            if set_default_complete:
                md = MessageDialog(self)
                md.set_info_message("Loaded Metadata Successfully.")

    def open_parameters_settings(self):
        self.parameters.show()

    def onChange_metadata_path(self, value):

        md = MessageDialog(self)
        try:

            self.__metadata_manager = MetadataManager(value)
            self.inventory = self.__metadata_manager.get_inventory()
            print(self.inventory)
            md.set_info_message(
                "Loaded Metadata, please check your terminal for further details"
            )

        except:

            md.set_error_message(
                "Something went wrong. Please check your metada file is a correct one"
            )

    def on_click_select_directory(self, bind: BindPyqtObject):
        if "darwin" == platform:
            dir_path = pw.QFileDialog.getExistingDirectory(
                self, 'Select Directory', bind.value)
        else:
            dir_path = pw.QFileDialog.getExistingDirectory(
                self, 'Select Directory', bind.value,
                pw.QFileDialog.DontUseNativeDialog)

        if dir_path:
            bind.value = dir_path

    def on_click_select_metadata_file(self, bind: BindPyqtObject):
        selected = pw.QFileDialog.getOpenFileName(self, "Select metadata file")
        if isinstance(selected[0], str) and os.path.isfile(selected[0]):
            bind.value = selected[0]

    def handle_data(self, tr):
        # If tr is not valid, discard it
        if not tr:
            return

        s = [tr.stats.network, tr.stats.station, tr.stats.channel]
        key = ".".join(s)
        outdated_tr = None

        if key in self.data_dict.keys():
            tr.data = np.float64(tr.data)
            current_tr = self.data_dict[key]
            # If received trace is in the same day as current, append received to current
            if current_tr.stats.endtime.julday == tr.stats.endtime.julday:
                self.data_dict[key] = self.data_dict[key] + tr

            else:
                # TODO: maybe the trace should have all the info and the save file only saves current day
                # TODO: so that way data is not erased on day change
                # If received trace has a part within current day, add it to current trace and save as outdated and
                # trim the trace to contain only next day's data
                if current_tr.stats.endtime.julday == tr.stats.starttime.julday:
                    day_start = datetime.datetime.combine(
                        tr.stats.endtime.date, datetime.time())
                    last_day = day_start - datetime.timedelta(microseconds=1)
                    outdated_tr = current_tr + tr.slice(
                        tr.stats.starttime, UTCDateTime(last_day), False)
                    tr.trim(UTCDateTime(day_start), tr.stats.endtime)
                # Set new day's trace as current
                self.data_dict[key] = tr

        else:
            # insert New Key
            tr.data = np.float64(tr.data)
            self.data_dict[key] = tr

        self.plot_seismogram()

        if self.widget_map is not None:
            station_list = self.get_station_info(tr)
            self.widget_map.plot_set_stations(station_list)

        if self.saveDataCB.isChecked():
            if outdated_tr is not None:
                self.write_trace(outdated_tr)
            self.write_trace(tr)

    def outdated_stations(self):
        # this method is run every t seconds to check and plot which stations do not send data
        outdated_stations = []
        outdated_traces = []
        # Outdated stations' traces will be erased from current list
        for key in list(self.data_dict.keys()):
            if self.data_dict[key].stats.endtime + 60 < UTCDateTime.now():
                outdated_traces.append(self.data_dict[key])
                outdated_stations.append(
                    self.get_station_info(outdated_traces[-1]))
                del self.data_dict[key]

        if outdated_stations:
            self.widget_map.plot_unset_stations(outdated_stations)

        if self.saveDataCB.isChecked():
            for tr in outdated_traces:
                self.write_trace(tr)

    def seedlink_error(self, tr):

        print("seedlink_error")

    def terminate_data(self, tr):

        print("terminate_data")

    @AsycTime.run_async()
    def retrieve_data(self, e):

        self.client = create_client(self.serverAddressForm.text(),
                                    on_data=self.handle_data,
                                    on_seedlink_error=self.seedlink_error,
                                    on_terminate=self.terminate_data)

        pyc.QMetaObject.invokeMethod(self.timer_outdated, 'start')

        for net in self.netForm.text().split(","):
            for sta in self.stationForm.text().split(","):
                for chn in self.channelForm.text().split(","):
                    self.client.select_stream(net, sta, chn)

        # self.client.on_data()
        self.client.run()
        # self.client.get_info(level="ALL")

    def plot_seismogram(self):
        # TODO: y axis should be independent for each subplot

        now = UTCDateTime.now()
        start_time = now - self.timewindowSB.value() * 60
        end_time = now + 30
        self.canvas.set_new_subplot(nrows=self.numTracesCB.value(),
                                    ncols=1,
                                    update=False)
        self.canvas.set_xlabel(self.numTracesCB.value() - 1,
                               "Date",
                               update=False)
        index = 0
        parameters = self.parameters.getParameters()
        for key, tr in self.data_dict.items():
            # sd = SeismogramDataAdvanced(file_path=None, stream=Stream(traces=tr), realtime=True)
            sd = SeismogramDataAdvanced(file_path=None,
                                        stream=tr,
                                        realtime=True)
            tr = sd.get_waveform_advanced(parameters, self.inventory)

            t = tr.times("matplotlib")
            s = tr.data
            info = "{}.{}.{}".format(tr.stats.network, tr.stats.station,
                                     tr.stats.channel)
            self.canvas.plot_date(t,
                                  s,
                                  index,
                                  clear_plot=False,
                                  update=False,
                                  color="black",
                                  fmt='-',
                                  linewidth=0.5)
            self.canvas.set_plot_label(index, info)
            ax = self.canvas.get_axe(index)

            if index == self.numTracesCB.value() - 1:
                ax.set_xlim(mdt.num2date(start_time.matplotlib_date),
                            mdt.num2date(end_time.matplotlib_date))

            index = index + 1

            formatter = mdt.DateFormatter('%y/%m/%d/%H:%M:%S')
            ax.xaxis.set_major_formatter(formatter)
        self.canvas.draw()
        # pyc.QCoreApplication.instance().processEvents()

    def stop(self):
        # TODO: not working. Maybe subclassing is necessary
        self.client.close()
        pyc.QMetaObject.invokeMethod(self.timer_outdated, 'stop')

    def write_trace(self, tr):
        t1 = tr.stats.starttime
        file_name = tr.id + "." + "D" + "." + str(t1.year) + "." + str(
            t1.julday)
        path_output = os.path.join(self.rootPathForm.text(), file_name)
        if os.path.exists(path_output):
            temp = tempfile.mkstemp()
            tr.write(temp[1], format="MSEED")
            with open(path_output, 'ab') as current:
                with open(temp[1], 'rb') as temp_bin:
                    current.write(temp_bin.read())
            os.remove(temp[1])
        else:
            tr.write(path_output, format="MSEED")

    def show_map(self):
        if self.inventory:
            self.widget_map = MapRealTime(self.inventory)
            try:
                self.widget_map.show()
            except:
                pass

    def get_station_info(self, tr):
        coordinates = {}
        net_ids = []
        sta_ids = []
        latitude = []
        longitude = []
        net_ids.append(tr.stats.network)
        sta_ids.append(tr.stats.station)
        coords = self.inventory.get_coordinates(tr.id)
        latitude.append(coords['latitude'])
        longitude.append(coords['longitude'])
        net_content = [net_ids, sta_ids, latitude, longitude]
        coordinates[tr.stats.network] = net_content

        return coordinates

    # TODO: this should be generic to be invoked from other windows
    def open_array_analysis(self):
        self.controller().open_array_window()

    def open_moment_tensor(self):
        self.controller().open_momentTensor_window()

    def time_frequency_analysis(self):
        self.controller().open_seismogram_window()

    def open_receiver_functions(self):
        self.controller().open_receiverFunctions()

    def controller(self):
        from isp.Gui.controllers import Controller
        return Controller()
Beispiel #4
0
class TimeFrequencyAdvance(pw.QFrame, UiTimeFrequencyWidget):
    def __init__(self, tr1, tr2):
        super(TimeFrequencyAdvance, self).__init__()
        self.setupUi(self)
        self.spectrum_Widget_Canvas = MatplotlibCanvas(self.spectrumWidget, nrows=2, ncols=2,
                                                      sharex=False, constrained_layout=True)

        self.spectrum_Widget_Canvas2 = MatplotlibCanvas(self.spectrumWidget2, nrows=1, ncols=1,
                                                      sharex=False, constrained_layout=True)

        self.coherence_Widget_Canvas = MatplotlibCanvas(self.coherenceWidget, nrows=2, ncols=1, sharex=False,
                                                       constrained_layout=True)
        self.cross_correlation_Widget_Canvas = MatplotlibCanvas(self.cross_correlationWidget, nrows = 3, ncols = 1)

        self.cross_spectrumWidget_Widget_Canvas = MatplotlibCanvas(self.cross_spectrumWidget,nrows = 2, ncols = 1,
                                sharex=True, constrained_layout=True)


        self.plot_spectrumBtn.clicked.connect(self.plot_spectrum)
        self.coherenceBtn.clicked.connect(self.coherence)
        self.cross_correlationsBtn.clicked.connect(self.plot_correlation)
        #self.Cross_spectrumBtn.clicked.connect(self.plot_cross_spectrogram)
        self.Cross_scalogramBtn.clicked.connect(self.plot_cross_scalogram)
        self.tr1 = tr1
        self.tr2 = tr2

    def plot_spectrum(self):
        if len(self.tr1) >0:
            [spec1, freq1, jackknife_errors] = spectrumelement(self.tr1.data, self.tr1.stats.delta, self.tr1.id)
            self.spectrum_Widget_Canvas.plot(freq1, spec1, 0)
            ax1 = self.spectrum_Widget_Canvas.get_axe(0)
            ax1.cla()
            ax1.loglog(freq1, spec1, '0.1', linewidth=0.5, color='steelblue', label=self.tr1.id)
            ax1.fill_between(freq1, jackknife_errors[:, 0], jackknife_errors[:, 1], facecolor="0.75",
                             alpha=0.5, edgecolor="0.5")
            ax1.set_ylim(spec1.min() / 10.0, spec1.max() * 100.0)
            ax1.set_xlim(freq1[1], freq1[len(freq1)-1])
            ax1.set_ylabel('Amplitude')
            ax1.set_xlabel('Frequency [Hz]')
            ax1.grid(True, which="both", ls="-", color='grey')
            ax1.legend()

            #plot the phase
            N = len(self.tr1.data)
            D = 2 ** math.ceil(math.log2(N))
            z = np.zeros(D - N)
            data = np.concatenate((self.tr1.data, z), axis=0)
            spectrum = np.fft.rfft(data, D)
            phase = np.angle(spectrum)
            ax3 = self.spectrum_Widget_Canvas.get_axe(2)
            ax3.semilogx(freq1, phase*180/math.pi, color = 'orangered', linewidth=0.5)
            ax3.set_xlim(freq1[1], freq1[len(freq1) - 1])
            ax3.grid(True, which="both", ls="-", color='grey')
            ax3.set_ylabel('Phase')
            ax3.set_xlabel('Frequency [Hz]')

        else:
            pass

        if len(self.tr2) > 0:
            [spec2, freq2, jackknife_errors] = spectrumelement(self.tr2.data, self.tr2.stats.delta, self.tr2.id)
            self.spectrum_Widget_Canvas.plot(freq2, spec2, 1)
            ax2 = self.spectrum_Widget_Canvas.get_axe(1)
            ax2.cla()
            ax2.loglog(freq2, spec2, '0.1', linewidth=0.5, color='steelblue', label=self.tr2.id)
            ax2.fill_between(freq2, jackknife_errors[:, 0], jackknife_errors[:, 1], facecolor="0.75",
                             alpha=0.5, edgecolor="0.5")
            ax2.set_ylim(spec2.min() / 10.0, spec2.max() * 100.0)
            ax2.set_xlim(freq2[1], freq2[len(freq2) - 1])
            ax2.set_ylabel('Amplitude')
            ax2.set_xlabel('Frequency [Hz]')
            ax2.grid(True, which="both", ls="-", color='grey')
            ax2.legend()

            # plot the phase
            N = len(self.tr2.data)
            D = 2 ** math.ceil(math.log2(N))
            z = np.zeros(D - N)
            data = np.concatenate((self.tr2.data, z), axis=0)
            spectrum = np.fft.rfft(data, D)
            phase = np.angle(spectrum)
            ax4 = self.spectrum_Widget_Canvas.get_axe(3)
            ax4.semilogx(freq2, phase * 180 / math.pi, color = 'orangered', linewidth=0.5)
            ax4.set_xlim(freq2[1], freq2[len(freq2) - 1])
            ax4.grid(True, which="both", ls="-", color='grey')
            ax4.set_ylabel('Phase')
            ax4.set_xlabel('Frequency [Hz]')
        else:
            pass

        # Power

        try:
            self.spectrum_Widget_Canvas2.plot(freq1, spec1, 0)
            ax5 = self.spectrum_Widget_Canvas2.get_axe(0)
            ax5.cla()
            spec1=spec1**2
            spec2=spec2**2
            ax5.loglog(freq1, spec1, '0.1', linewidth=0.5, color='steelblue', label=self.tr1.id)
            ax5.loglog(freq2, spec2, '0.1', linewidth=0.5, color='orangered', label=self.tr2.id)
            ax5.set_ylabel('Amplitude')
            ax5.set_xlabel('Frequency [Hz]')
            ax5.grid(True, which="both", ls="-", color='grey')
            ax5.legend()

        except:
            raise("Some data is missing")




    def coherence(self):
        if len(self.tr1) > 0 and len(self.tr2) > 0:
            sampling_rates = []
            fs1=self.tr1.stats.sampling_rate
            fs2=self.tr2.stats.sampling_rate
            sampling_rates.append(fs1)
            sampling_rates.append(fs2)
            max_sampling_rates = np.max(sampling_rates)
            overlap = self.cohe_overlapSB.value()
            time_window = self.time_window_coheSB.value()
            nfft = time_window*max_sampling_rates
            if fs1 != fs2:
                self.tr1.resample(max_sampling_rates)
                self.tr1.resample(max_sampling_rates)

            # Plot Amplitude
            [A,f, phase] = cohe(self.tr1.data, self.tr2.data, max_sampling_rates, nfft, overlap)

            self.coherence_Widget_Canvas.plot(f, A, 0)

            ax1 = self.coherence_Widget_Canvas.get_axe(0)
            ax1.cla()
            ax1.loglog(f, A, '0.1', linewidth=0.5, color='steelblue', label="Amplitude Coherence "+
                                                                            self.tr1.id+" "+ self.tr2.id)

            ax1.set_ylim(A.min() / 10.0, A.max() * 100.0)
            ax1.set_xlim(f[1], f[len(f) - 1])
            ax1.set_ylabel('Magnitude Coherence')
            ax1.set_xlabel('Frequency [Hz]')
            ax1.grid(True, which="both", ls="-", color='grey')
            ax1.legend()

            # Plot Phase
            ax2 = self.coherence_Widget_Canvas.get_axe(1)
            ax2.cla()
            ax2.semilogx(f, phase * 180 / math.pi, color='orangered', linewidth=0.5, label="Phase Coherence "+
                                                                            self.tr1.id+" "+ self.tr2.id)
            ax2.set_xlim(f[1], f[len(f) - 1])
            ax2.set_ylabel('Phase')
            ax2.set_xlabel('Frequency [Hz]')
            ax2.grid(True, which="both", ls="-", color='grey')
            ax2.legend()

    def plot_correlation(self):

        if len(self.tr1) > 0 and len(self.tr2) > 0:
            sampling_rates = []
            fs1=self.tr1.stats.sampling_rate
            fs2=self.tr2.stats.sampling_rate
            sampling_rates.append(fs1)
            sampling_rates.append(fs2)
            max_sampling_rates = np.max(sampling_rates)

            if fs1 != fs2:
                self.tr1.resample(max_sampling_rates)
                self.tr2.resample(max_sampling_rates)

            cc1 = correlate_maxlag(self.tr1.data, self.tr1.data, maxlag = max([len(self.tr1.data),len(self.tr2.data)]))
            cc2 = correlate_maxlag(self.tr2.data, self.tr2.data, maxlag = max([len(self.tr1.data),len(self.tr2.data)]))
            cc3 = correlate_maxlag(self.tr1.data, self.tr2.data, maxlag = max([len(self.tr1.data),len(self.tr2.data)]))
            N1 = len(cc1)
            N2 = len(cc2)
            N3 = len(cc3)
            self.cross_correlation_Widget_Canvas.plot(get_lags(cc1)/max_sampling_rates, cc1, 0,
                                                      clear_plot=True, linewidth=0.5,color='black')
            self.cross_correlation_Widget_Canvas.plot(get_lags(cc2)/max_sampling_rates, cc2, 1,
                                                      clear_plot=True, linewidth=0.5, color = 'black')
            self.cross_correlation_Widget_Canvas.plot(get_lags(cc3)/max_sampling_rates, cc3, 2,
                                                      clear_plot=True, linewidth=0.5, color = 'red')

    def plot_cross_spectrogram(self):
        if len(self.tr1) > 0 and len(self.tr2) > 0:
            csp = cross_spectrogram(self.tr1, self.tr2, win=self.time_windowSB.value(),
                                    tbp=self.time_bandwidthSB.value(), ntapers = self.num_tapersSB.value())

            [coherence_cross, freq, t] = csp.compute_coherence_crosspectrogram()
            f_min = 0
            f_max = 0.5*(1/max(freq))
            f_max=50
            x, y = np.meshgrid(t, np.linspace(f_min, f_max, coherence_cross.shape[0]))

            self.cross_spectrumWidget_Widget_Canvas.plot_contour(x, y, coherence_cross, axes_index=0,
                     clabel="Coherence", cmap=plt.get_cmap("jet"), vmin=0,vmax=1)
            self.cross_spectrumWidget_Widget_Canvas.set_xlabel(0, "Time (s)")
            self.cross_spectrumWidget_Widget_Canvas.set_ylabel(0, "Frequency (Hz)")


    def plot_cross_scalogram(self):

        base_line = self.base_lineDB.value()
        colour = self.colourCB.currentText()

        if len(self.tr1) > 0 and len(self.tr2) > 0:
            #
            fs1 = self.tr1.stats.sampling_rate
            fs2 = self.tr2.stats.sampling_rate
            fs = max(fs1, fs2)
            if fs1 < fs:
                self.tr1.resample(fs)
            elif fs2 < fs:
                self.tr2.resample(fs)
            all_traces = [self.tr1, self.tr2]
            st = Stream(traces=all_traces)
            maxstart = np.max([tr.stats.starttime for tr in st])
            minend = np.min([tr.stats.endtime for tr in st])
            st.trim(maxstart, minend)
            tr1 = st[0]
            tr2 = st[1]
            tr1.detrend(type='demean')
            tr1.taper(max_percentage=0.05)
            tr2.detrend(type='demean')
            tr2.taper(max_percentage=0.05)

            npts =len(tr1.data)
            t = np.linspace(0,  npts/fs, npts)
            f_max = fs/2
            nf = 40
            wmin=wmax=self.num_cyclesSB.value()
            f_min = self.freq_min_waveletSB.value()
            tt = int(fs / f_min)
            [ba, nConv, frex, half_wave] = ccwt_ba_fast(npts, fs, f_min, f_max, wmin, wmax, tt, nf)

            cf, sc, scalogram1 = cwt_fast(tr1.data, ba, nConv, frex, half_wave, fs)

            cf, sc, scalogram2 = cwt_fast(tr2.data, ba, nConv, frex, half_wave, fs)

            crossCFS = scalogram1 * np.conj(scalogram2)

            cross_scalogram = np.abs(crossCFS)**2
            cross_scalogram = 10*np.log(cross_scalogram/np.max(cross_scalogram))
            cross_scalogram = np.clip(cross_scalogram, a_min=base_line, a_max=0)
            min_cwt = base_line
            max_cwt = 0


            x, y = np.meshgrid(t, np.logspace(np.log10(f_min), np.log10(f_max), cross_scalogram.shape[0]))
            c_f = wmin / 2 * math.pi
            f = np.linspace((f_min), (f_max), cross_scalogram.shape[0])
            pred = (math.sqrt(2) * c_f / f) - (math.sqrt(2) * c_f / f_max)

            pred_comp = t[len(t) - 1] - pred


            #Plot Seismograms

            self.cross_spectrumWidget_Widget_Canvas.plot(tr1.times(), self.tr1.data, 0, color="black",
                    linewidth=0.5,alpha = 0.75)

            self.cross_spectrumWidget_Widget_Canvas.plot(tr2.times(), tr2.data, 0, clear_plot=False, is_twinx=True,
                                                         color="orangered", linewidth=0.5, alpha=0.75)
            info = tr1.id + " " + tr2.id
            self.cross_spectrumWidget_Widget_Canvas.set_plot_label(0, info)
            self.cross_spectrumWidget_Widget_Canvas.set_ylabel(0, "Amplitude")
            #info = "{}.{}.{}".format(tr1.stats.network, tr1.stats.station, tr1.stats.channel)
            #self.cross_spectrumWidget_Widget_Canvas.set_plot_label(0, info)
            #info = "{}.{}.{}".format(tr2.stats.network, tr2.stats.station, tr1.stats.channel)
            #self.cross_spectrumWidget_Widget_Canvas.set_plot_label(0, info)


            # Plot Cross Scalogram
            self.cross_spectrumWidget_Widget_Canvas.plot_contour(x, y, cross_scalogram, axes_index=1, clabel="Cross Power [dB]",
                                           cmap=plt.get_cmap(colour))
            # Plot Cone
            ax_cone = self.cross_spectrumWidget_Widget_Canvas.get_axe(1)
            ax_cone.fill_between(pred, f, 0, color="black", edgecolor="red", alpha=0.3)
            ax_cone.fill_between(pred_comp, f, 0, color="black", edgecolor="red", alpha=0.3)
            self.cross_spectrumWidget_Widget_Canvas.set_xlabel(1, "Time (s)")
            self.cross_spectrumWidget_Widget_Canvas.set_ylabel(1, "Frequency (Hz)")
Beispiel #5
0
class EGFFrame(pw.QWidget, UiEGFFrame):
    def __init__(self, parameters, settings):
        super(EGFFrame, self).__init__()
        self.setupUi(self)

        self.parameters = parameters
        self.settings_dialog = settings

        self.progressbar = pw.QProgressDialog(self)
        self.progressbar.setWindowTitle('Ambient Noise Tomography')
        self.progressbar.setLabelText(" Computing ")
        self.progressbar.setWindowIcon(pqg.QIcon(':\icons\map-icon.png'))
        self.progressbar.close()

        self.inventory = {}
        self.files = []
        self.total_items = 0
        self.items_per_page = 1
        self.__dataless_manager = None
        self.__metadata_manager = None
        self.st = None
        self.output = None
        self.root_path_bind = BindPyqtObject(self.rootPathForm,
                                             self.onChange_root_path)
        self.root_path_bind2 = BindPyqtObject(self.rootPathForm2,
                                              self.onChange_root_path)
        self.metadata_path_bind = BindPyqtObject(self.datalessPathForm,
                                                 self.onChange_metadata_path)
        self.updateBtn.clicked.connect(self.plot_egfs)
        self.output_bind = BindPyqtObject(self.outPathForm,
                                          self.onChange_root_path)
        self.pagination = Pagination(self.pagination_widget, self.total_items,
                                     self.items_per_page)
        self.pagination.set_total_items(0)

        self.canvas = MatplotlibCanvas(self.plotMatWidget,
                                       nrows=self.items_per_page,
                                       constrained_layout=False)
        self.canvas.set_xlabel(0, "Time (s)")
        self.canvas.figure.tight_layout()

        # Bind buttons

        self.readFilesBtn.clicked.connect(lambda: self.get_now_files())
        self.selectDirBtn.clicked.connect(
            lambda: self.on_click_select_directory(self.root_path_bind))
        self.selectDirBtn2.clicked.connect(
            lambda: self.on_click_select_directory(self.root_path_bind2))
        self.metadata_path_bind = BindPyqtObject(self.datalessPathForm,
                                                 self.onChange_metadata_path)
        self.selectDatalessDirBtn.clicked.connect(
            lambda: self.on_click_select_file(self.metadata_path_bind))
        self.outputBtn.clicked.connect(
            lambda: self.on_click_select_directory(self.output_bind))
        self.preprocessBtn.clicked.connect(self.run_preprocess)
        self.cross_stackBtn.clicked.connect(self.stack)
        self.mapBtn.clicked.connect(self.map)

    @pyc.Slot()
    def _increase_progress(self):
        self.progressbar.setValue(self.progressbar.value() + 1)

    def filter_error_message(self, msg):
        md = MessageDialog(self)
        md.set_info_message(msg)

    def onChange_root_path(self, value):
        """
        Fired every time the root_path is changed
        :param value: The path of the new directory.
        :return:
        """
        # self.read_files(value)
        pass

    def on_click_select_file(self, bind: BindPyqtObject):
        selected = pw.QFileDialog.getOpenFileName(self, "Select metadata file")
        if isinstance(selected[0], str) and os.path.isfile(selected[0]):
            bind.value = selected[0]

    @parse_excepts(lambda self, msg: self.subprocess_feedback(msg))
    @AsycTime.run_async()
    def onChange_metadata_path(self, value):

        try:
            self.__metadata_manager = MetadataManager(value)
            self.inventory = self.__metadata_manager.get_inventory()
            print(self.inventory)
        except:
            raise FileNotFoundError("The metadata is not valid")

    def subprocess_feedback(self, err_msg: str, set_default_complete=True):
        """
        This method is used as a subprocess feedback. It runs when a raise expect is detected.
        :param err_msg: The error message from the except.
        :param set_default_complete: If True it will set a completed successfully message. Otherwise nothing will
            be displayed.
        :return:
        """
        if err_msg:
            md = MessageDialog(self)
            if "Error code" in err_msg:
                md.set_error_message(
                    "Click in show details detail for more info.", err_msg)
            else:
                md.set_warning_message("Click in show details for more info.",
                                       err_msg)
        else:
            if set_default_complete:
                md = MessageDialog(self)
                md.set_info_message(
                    "Loaded Metadata, please check your terminal for further details"
                )

    def on_click_select_directory(self, bind: BindPyqtObject):
        if "darwin" == platform:
            dir_path = pw.QFileDialog.getExistingDirectory(
                self, 'Select Directory', bind.value)
        else:
            dir_path = pw.QFileDialog.getExistingDirectory(
                self, 'Select Directory', bind.value,
                pw.QFileDialog.DontUseNativeDialog)
        if dir_path:
            bind.value = dir_path

    def read_files(self, dir_path):
        md = MessageDialog(self)
        md.hide()
        try:
            self.progressbar.reset()
            self.progressbar.setLabelText(" Reading Files ")
            self.progressbar.setRange(0, 0)
            with ThreadPoolExecutor(1) as executor:
                self.ant = noise_organize(dir_path, self.inventory)
                self.ant.send_message.connect(self.receive_messages)

                def read_files_callback():
                    data_map, size, channels = self.ant.create_dict()
                    print(channels)
                    pyc.QMetaObject.invokeMethod(self.progressbar, 'accept')
                    return data_map, size, channels

                f = executor.submit(read_files_callback)
                self.progressbar.exec()
                self.data_map, self.size, self.channels = f.result()
                f.cancel()

            # self.ant.test()
            md.set_info_message("Readed data files Successfully")
        except:
            md.set_error_message(
                "Something went wrong. Please check your data files are correct mseed files"
            )

        md.show()

    def run_preprocess(self):
        self.params = self.settings_dialog.getParameters()
        self.read_files(self.root_path_bind.value)
        self.process()

    ####################################################################################################################

    def process(self):
        #
        self.process_ant = process_ant(self.output_bind.value, self.params,
                                       self.inventory)
        list_raw = self.process_ant.get_all_values(self.data_map)
        self.process_ant.create_all_dict_matrix(list_raw, self.channels)

    def stack(self):
        stack = noisestack(self.output_bind.value)
        stack.run_cross_stack()
        stack.rotate_horizontals()

    @pyc.pyqtSlot(str)
    def receive_messages(self, message):
        self.listWidget.addItem(message)

    def get_files_at_page(self):
        n_0 = (self.pagination.current_page -
               1) * self.pagination.items_per_page
        n_f = n_0 + self.pagination.items_per_page
        return self.files[n_0:n_f]

    def get_file_at_index(self, index):
        files_at_page = self.get_files_at_page()
        return files_at_page[index]

    def onChange_items_per_page(self, items_per_page):
        self.items_per_page = items_per_page

    def filter_error_message(self, msg):
        md = MessageDialog(self)
        md.set_info_message(msg)

    def set_pagination_files(self, files_path):
        self.files = files_path
        self.total_items = len(self.files)
        self.pagination.set_total_items(self.total_items)

    def get_files(self, dir_path):
        files_path = MseedUtil.get_tree_hd5_files(self, dir_path, robust=False)
        self.set_pagination_files(files_path)
        # print(files_path)
        pyc.QMetaObject.invokeMethod(self.progressbar, 'accept',
                                     qt.AutoConnection)

        return files_path

    def get_now_files(self):

        md = MessageDialog(self)
        md.hide()
        try:

            self.progressbar.reset()
            self.progressbar.setLabelText(" Reading Files ")
            self.progressbar.setRange(0, 0)
            with ThreadPoolExecutor(1) as executor:
                f = executor.submit(
                    lambda: self.get_files(self.root_path_bind2.value))
                self.progressbar.exec()
                self.files_path = f.result()
                f.cancel()

            # self.files_path = self.get_files(self.root_path_bind.value)

            md.set_info_message("Readed data files Successfully")

        except:

            md.set_error_message(
                "Something went wrong. Please check your data files are correct mseed files"
            )

        md.show()

    def plot_egfs(self):
        if self.st:
            del self.st

        self.canvas.clear()
        ##
        self.nums_clicks = 0
        all_traces = []
        if self.sortCB.isChecked():
            if self.comboBox_sort.currentText() == "Distance":
                self.files_path.sort(key=self.sort_by_distance_advance)

        elif self.comboBox_sort.currentText() == "Back Azimuth":
            self.files_path.sort(key=self.sort_by_baz_advance)

        self.set_pagination_files(self.files_path)
        files_at_page = self.get_files_at_page()
        ##
        if len(self.canvas.axes) != len(files_at_page):
            self.canvas.set_new_subplot(nrows=len(files_at_page), ncols=1)
        last_index = 0
        min_starttime = []
        max_endtime = []
        parameters = self.parameters.getParameters()

        for index, file_path in enumerate(files_at_page):
            if os.path.basename(file_path) != ".DS_Store":
                sd = SeismogramDataAdvanced(file_path)

                tr = sd.get_waveform_advanced(
                    parameters,
                    self.inventory,
                    filter_error_callback=self.filter_error_message,
                    trace_number=index)
                print(tr.data)
                if len(tr) > 0:
                    t = tr.times("matplotlib")
                    s = tr.data
                    self.canvas.plot_date(t,
                                          s,
                                          index,
                                          color="black",
                                          fmt='-',
                                          linewidth=0.5)
                    if self.pagination.items_per_page >= 16:
                        ax = self.canvas.get_axe(index)
                        ax.spines["top"].set_visible(False)
                        ax.spines["bottom"].set_visible(False)
                        ax.tick_params(top=False)
                        ax.tick_params(labeltop=False)
                        if index != (self.pagination.items_per_page - 1):
                            ax.tick_params(bottom=False)

                    last_index = index

                    st_stats = ObspyUtil.get_stats(file_path)

                    if st_stats and self.sortCB.isChecked() == False:
                        info = "{}.{}.{}".format(st_stats.Network,
                                                 st_stats.Station,
                                                 st_stats.Channel)
                        self.canvas.set_plot_label(index, info)

                    elif st_stats and self.sortCB.isChecked(
                    ) and self.comboBox_sort.currentText() == "Distance":

                        dist = self.sort_by_distance_advance(file_path)
                        dist = "{:.1f}".format(dist / 1000.0)
                        info = "{}.{}.{} Distance {} km".format(
                            st_stats.Network, st_stats.Station,
                            st_stats.Channel, str(dist))
                        self.canvas.set_plot_label(index, info)

                    elif st_stats and self.sortCB.isChecked(
                    ) and self.comboBox_sort.currentText() == "Back Azimuth":

                        back = self.sort_by_baz_advance(file_path)
                        back = "{:.1f}".format(back)
                        info = "{}.{}.{} Back Azimuth {}".format(
                            st_stats.Network, st_stats.Station,
                            st_stats.Channel, str(back))
                        self.canvas.set_plot_label(index, info)

                    try:
                        min_starttime.append(min(t))
                        max_endtime.append(max(t))
                    except:
                        print("Empty traces")

                all_traces.append(tr)

        self.st = Stream(traces=all_traces)
        try:
            if min_starttime and max_endtime is not None:
                auto_start = min(min_starttime)
                auto_end = max(max_endtime)
                self.auto_start = auto_start
                self.auto_end = auto_end

            ax = self.canvas.get_axe(last_index)
            ax.set_xlim(mdt.num2date(auto_start), mdt.num2date(auto_end))
            formatter = mdt.DateFormatter('%y/%m/%d/%H:%M:%S.%f')
            ax.xaxis.set_major_formatter(formatter)
            self.canvas.set_xlabel(last_index, "Date")
        except:
            pass

    def sort_by_distance_advance(self, file):

        geodetic = MseedUtil.get_geodetic(file)

        if geodetic[0] is not None:

            return geodetic[0]
        else:
            return 0.

    def sort_by_baz_advance(self, file):

        geodetic = MseedUtil.get_geodetic(file)

        if geodetic[1] is not None:

            return geodetic[0]
        else:
            return 0.

    def map(self):

        try:

            map_dict = {}
            sd = []

            for tr in self.st:

                station = tr.stats.station.split("_")[0]
                name = tr.stats.network + "." + station
                sd.append(name)
                map_dict[name] = [
                    tr.stats.mseed['coordinates'][0],
                    tr.stats.mseed['coordinates'][1]
                ]

            self.map_stations = StationsMap(map_dict)
            self.map_stations.plot_stations_map(latitude=0, longitude=0)

        except:
            md = MessageDialog(self)
            md.set_error_message(
                "couldn't plot stations map, please check your metadata and the trace headers"
            )
Beispiel #6
0
class SyntheticsAnalisysFrame(pw.QMainWindow, UiSyntheticsAnalisysFrame):

    def __init__(self, parent: pw.QWidget = None):

        super(SyntheticsAnalisysFrame, self).__init__(parent)

        self.setupUi(self)
        ParentWidget.set_parent(parent, self)
        self.setWindowTitle('Synthetics Analysis Frame')
        self.setWindowIcon(pqg.QIcon(':\icons\pen-icon.png'))

        #Initialize parametrs for plot rotation
        self._z = {}
        self._r = {}
        self._t = {}
        self._st = {}
        self.inventory = {}
        parameters = {}

        self._generator = SyntheticsGeneratorDialog(self)

        # 3C_Component
        self.focmec_canvas = FocCanvas(self.widget_fp)
        self.canvas = MatplotlibCanvas(self.plotMatWidget_3C)
        self.canvas.set_new_subplot(3, ncols=1)

        # Map
        self.cartopy_canvas = CartopyCanvas(self.map_widget)

        # binds
        self.root_path_bind_3C = BindPyqtObject(self.rootPathForm_3C, self.onChange_root_path_3C)
        self.vertical_form_bind = BindPyqtObject(self.verticalQLineEdit)
        self.north_form_bind = BindPyqtObject(self.northQLineEdit)
        self.east_form_bind = BindPyqtObject(self.eastQLineEdit)
        self.generation_params_bind = BindPyqtObject(self.paramsPathLineEdit)

        # accept drops
        self.vertical_form_bind.accept_dragFile(drop_event_callback=self.drop_event)
        self.north_form_bind.accept_dragFile(drop_event_callback=self.drop_event)
        self.east_form_bind.accept_dragFile(drop_event_callback=self.drop_event)
        self.generation_params_bind.accept_dragFile(drop_event_callback=self.drop_event)
        self.paramsPathLineEdit.textChanged.connect(self._generationParamsChanged)

        # Add file selector to the widget
        self.file_selector = FilesView(self.root_path_bind_3C.value, parent=self.fileSelectorWidget)
        self.file_selector.setDragEnabled(True)

        self.selectDirBtn_3C.clicked.connect(self.on_click_select_directory_3C)
        self.plotBtn.clicked.connect(self.on_click_rotate)
        ###
        self.stationsBtn.clicked.connect(self.stationsInfo)
        ###

        self.actionGenerate_synthetics.triggered.connect(lambda : self._generator.show())

    def info_message(self, msg):
        md = MessageDialog(self)
        md.set_info_message(msg)

    def _generationParamsChanged(self):
        with open(self.generation_params_file, 'rb') as f:
            self.params = pickle.load(f)
            depth_est =self.params['sourcedepthinmeters']
            depth_est =(float(depth_est))/1000
           #self.paramsTextEdit.setPlainText(str(params))
            self.paramsTextEdit.setPlainText("Earth Model: {model}".format(model=self.params['model']))
            self.paramsTextEdit.appendPlainText("Event Coords: {lat} {lon} {depth}".format(
                lat=self.params['sourcelatitude'],lon=self.params['sourcelongitude'],
                              depth=str(depth_est)))
            self.paramsTextEdit.appendPlainText("Time: {time}".format(time=self.params['origintime']))
            self.paramsTextEdit.appendPlainText("Units: {units}".format(units=self.params['units']))

            if 'sourcedoublecouple' in self.params:
                self.paramsTextEdit.appendPlainText("Source: {source}".format(source= self.params['sourcedoublecouple']))
            if 'sourcemomenttensor' in self.params:
                self.paramsTextEdit.appendPlainText("Source: {source}".format(source= self.params['sourcemomenttensor']))


    @staticmethod
    def drop_event(event: pqg.QDropEvent, bind_object: BindPyqtObject):
        data = event.mimeData()
        url = data.urls()[0]
        bind_object.value = url.fileName()

    @property
    def north_component_file(self):
        return os.path.join(self.root_path_bind_3C.value, self.north_form_bind.value)

    @property
    def vertical_component_file(self):
        return os.path.join(self.root_path_bind_3C.value, self.vertical_form_bind.value)

    @property
    def east_component_file(self):
        return os.path.join(self.root_path_bind_3C.value, self.east_form_bind.value)

    @property
    def generation_params_file(self):
        return os.path.join(self.root_path_bind_3C.value, self.generation_params_bind.value)

    def onChange_root_path_3C(self, value):
        """
        Fired every time the root_path is changed

        :param value: The path of the new directory.

        :return:
        """
        self.file_selector.set_new_rootPath(value)

    # Function added for 3C Components
    def on_click_select_directory_3C(self):

        if "darwin" == platform:
            dir_path = pw.QFileDialog.getExistingDirectory(self, 'Select Directory', self.root_path_bind_3C.value)
        else:
            dir_path = pw.QFileDialog.getExistingDirectory(self, 'Select Directory', self.root_path_bind_3C.value,
                                                           pw.QFileDialog.DontUseNativeDialog)

        if dir_path:
            self.root_path_bind_3C.value = dir_path

    def on_click_rotate(self, canvas):
        time1 = convert_qdatetime_utcdatetime(self.dateTimeEdit_4)
        time2 = convert_qdatetime_utcdatetime(self.dateTimeEdit_5)

        try:
            sd = SeismogramData(self.vertical_component_file)
            z = sd.get_waveform()
            sd = SeismogramData(self.north_component_file)
            n = sd.get_waveform()
            sd = SeismogramData(self.east_component_file)
            e = sd.get_waveform()
            seismograms = [z, n, e]
            time = z.times("matplotlib")
            self._st = Stream(traces=seismograms)
            for index, data in enumerate(seismograms):
                self.canvas.plot(time, data, index, color="black", linewidth=0.5)
                info = "{}.{}.{}".format(self._st[index].stats.network, self._st[index].stats.station,
                                         self._st[index].stats.channel)
                ax = self.canvas.get_axe(0)
                ax.set_xlim(time1.matplotlib_date, time2.matplotlib_date)
                formatter = mdt.DateFormatter('%Y/%m/%d/%H:%M:%S')
                ax.xaxis.set_major_formatter(formatter)
                self.canvas.set_plot_label(index, info)

            self.canvas.set_xlabel(2, "Time (s)")

            if 'sourcedoublecouple' in self.params:
                self.focmec_canvas.drawSynthFocMec(0, first_polarity = self.params['sourcedoublecouple'], mti = [])
            if 'sourcemomenttensor' in self.params:
                self.focmec_canvas.drawSynthFocMec(0, first_polarity= [], mti=self.params['sourcemomenttensor'])

        except InvalidFile:
            self.info_message("Invalid mseed files. Please, make sure you have generated correctly the synthetics")

        self.__map_coords()


    def stationsInfo(self):
        files = []
        try:
            if self.vertical_component_file and self.north_component_file and self.east_component_file:
                files = [self.vertical_component_file, self.north_component_file, self.east_component_file]
        except:
            pass

        sd = []
        if len(files)==3:
            for file in files:
                try:
                    st = SeismogramDataAdvanced(file)

                    station = [st.stats.Network,st.stats.Station,st.stats.Location,st.stats.Channel,st.stats.StartTime,
                           st.stats.EndTime, st.stats.Sampling_rate, st.stats.Npts]

                    sd.append(station)
                except:
                    pass

            self._stations_info = StationsInfo(sd)
            self._stations_info.show()

    def __map_coords(self):
        map_dict = {}
        sd = []
        with open(self.generation_params_file, 'rb') as f:
            params = pickle.load(f)

        n = len(params["bulk"])
        for j in range(n):

            for key in params["bulk"][j]:
                if key == "latitude":
                   lat = params["bulk"][j][key]

                elif key == "longitude":
                    lon = params["bulk"][j][key]

                #elif key == "networkcode":
                #    net = params["bulk"][j][key]

                elif key == "stationcode":
                    sta = params["bulk"][j][key]

                    sd.append(sta)
                    map_dict[sta] = [lon, lat]


        self.cartopy_canvas.plot_map(params['sourcelongitude'], params['sourcelatitude'], 0, 0, 0, 0,
                                     resolution='low', stations=map_dict)