示例#1
0
def test_SNR(network,
             station,
             phase,
             preproloc='ouput/waveforms/preprocessed'):
    """Test the automatic QC scripts for a certain station and writes ratings
    in the rating file."""
    noisematls = []
    critls = []
    loc = os.path.join(preproloc, phase, '/by_station/', network, station)
    for file in os.listdir(loc):
        try:
            st = read(loc + '/' + file)
        except IsADirectoryError:
            continue
        dt = st[0].stats.delta
        sampling_rate = st[0].stats.sampling_rate
        if phase == "S":
            _, crit, _, noisemat = qcs(st, dt, sampling_rate)
        elif phase == "P":
            _, crit, _, noisemat = qcp(st, dt, sampling_rate)
        noisematls.append(noisemat)
        critls.append(crit)
    return noisematls, critls
示例#2
0
def automatic_rate(network, station, phase, preproloc):
    """
    Checks the automatic QC criteria for SRF waveforms.

    Parameters
    ----------
    network : STRING
        Network code (2 letters).
    station : STRING
        Station code (3 letters).
    phase : STRING, optional
        "P" or "S".
    preproloc : string
        Directory that contains the preprocessed files (not quality controlled)

    Returns
    -------
    diff : INTEGER
        Number of waveforms that were not rated 3 or 4.
    ret : INTEGER
        Number of automatically retained waveforms.
    sts : LIST
        List containing all retained + filtered streams.
    crits : LIST
        List containing bools (retained or not) corresponding to streams in
        sts.

    """

    inloc = os.path.join(preproloc, phase, '/by_station/', network, station)
    diff = 0
    ret = 0
    sts = []
    crits = []
    for file in os.listdir(inloc):
        if file[:4] == "info":  # Skip the info files
            continue
        try:
            st = read(inloc + file)
        except IsADirectoryError as e:
            print(e)
            continue
        starttime = str(st[0].stats.starttime)
        if phase == "S":
            st, crit, hf, noisemat = qcs(st, st[0].stats.delta,
                                         st[0].stats.sampling_rate)
        elif phase == "P":
            st, crit, lf, noisemat = qcp(st, st[0].stats.delta,
                                         st[0].stats.sampling_rate)

        with shelve.open(
                os.path.join(finddir(), 'ratings') + network + "." + station +
                "rating") as f:
            f[starttime + "_auto"] = crit
            if starttime in f and int(f[starttime]) < 3 and crit:
                diff = diff + 1
            if crit:
                ret = ret + 1
        sts.append(st)
        crits.append(crit)
    return diff, ret, sts, crits
示例#3
0
def rate(network,
         phase,
         preproloc,
         station=None,
         review=False,
         retained=False,
         decon_meth='it',
         test_tt_calculation=False):
    """
    Module to rate and review the quality of Sp or Ps waveforms from the given
    station. Shows the automatic rating from qc. Tapers can be controlled with
    the mouse. Rating is done with the keyboard. Makes only sense if the
    files in preproloc have not been quality controlled (the script is a
    reminant from an earlier alpha version).

    Parameters
    ----------
    network : STRING
        Network code (two letters).
    phase : STRING
        Either "P" for Ps or "S" Sp. The default is "S".
    preproloc : string
        Directory that contains the preprocessed files (not quality controlled)
    station : str or list, optional
        Station code (three letters).
    review : INTEGER, optional
        If true, already rated waveforms are shown.
        Can also be an integer between 1 and 4. Then, only waveforms rated
        with the respected rating are shown. The default is False.
    retained : Bool, optional
        Show only waveforms retained by qcp or qcs. The default is False.
    decon_meth : STRING, optional
        Deconvolution method, "waterlevel", 'dampedf' for constant
        damping level, 'it' for iterative time domain deconvoltuion, 'multit'
        for multitaper or 'fqd' for frequency dependently damped spectral
        division. The default is 'it'

    Raises
    ------
    Exception
        For Typing mistakes.

    Returns
    -------
    None.

    """
    if phase == 'P':
        onset = 30
    elif phase[-1] == 'S':
        onset = 120

    inloc = os.path.join(preproloc, phase, 'by_station', network)

    infiles = []  # List of all files in folder
    pattern = []  # List of input constraints
    streams = []  # List of files filtered for input criteria

    for root, dirs, files in os.walk(inloc):
        for name in files:
            infiles.append(os.path.join(root, name))

    # Set filter patterns

    if station:
        if type(station) == str:
            pattern.append('*%s*%s*.mseed' % (network, station))
        elif type(station) == list:
            for stat in station:
                pattern.append('*%s*%s*.mseed' % (network, stat))
    else:
        pattern.append('*%s*.mseed' % (network))

    # Do filtering
    for pat in pattern:
        streams.extend(fnmatch.filter(infiles, pat))

    # clear memory
    del pattern, infiles

    # For TauPy lookup
    model = TauPyModel()

    if test_tt_calculation:
        client = Client()

    for f in streams:
        # if file[:4] == "info":  # Skip the info files
        #     continue
        # try:
        #     st = read(inloc + file)
        # except IsADirectoryError as e:
        #     print(e)
        #     continue

        st = read(f)
        # List for taper coordinates
        if "taper" in rating:
            del rating["taper"]
        rating["taper"] = []
        st.normalize()

        # Additional filter "test"
        if phase == "S":
            st.filter("lowpass", freq=1.0, zerophase=True, corners=2)
        elif phase == "P":
            st.filter("lowpass", freq=1.0, zerophase=True, corners=2)

        y = []
        ch = []
        starttime = str(st[0].stats.starttime)
        dt = st[0].stats.delta
        sampling_f = st[0].stats.sampling_rate
        old = __r_file(network, st[0].stats.station, starttime)  # old

        # read info file
        # location info file
        infof = os.path.join(inloc, st[0].stats.station, 'info')
        with shelve.open(infof) as info:
            ii = info["starttime"].index(st[0].stats.starttime)
            rdelta = info["rdelta"][ii]  # epicentral distance
            mag = info["magnitude"][ii]
            statlat = info["statlat"]
            statlon = info["statlon"]
            rayp = info["rayp_s_deg"][ii] / 111319.9
            evt_depth = info["evt_depth"][ii] / 1000
            ot = info["ot_ret"][ii]
            evtlat = info['evtlat'][ii]
            evtlon = info['evtlon'][ii]

        if old and not review:  # skip already rated files
            continue
        if type(review) == int:
            if review > 4:
                raise Exception("review has to be between 0 and 4")
            if review != int(old):
                continue

        # check automatic rating
        if phase == "S":
            st_f, crit, hf, noisemat = qcs(st, dt, sampling_f, onset=onset)
        elif phase == "P":
            st_f, crit, hf, noisemat = qcp(st, dt, sampling_f, onset=onset)

        # skip files that have not been retained
        if retained and not crit:
            continue

        # create RF
        if not test_tt_calculation:
            _, _, PSV = rotate_PSV(statlat, statlon, rayp, st_f)
        else:
            PSV = st_f  # to see correlation at theoretical arrival

        try:
            RF = createRF(PSV, phase, method=decon_meth, shift=onset)
        except ValueError as e:  # There were some problematic events
            print(e)
            continue

        # TauPy lookup
        ph_name = []
        ph_time = []

        if not test_tt_calculation:
            arrivals = model.get_travel_times(evt_depth,
                                              distance_in_degree=rdelta)

            primary_time = model.get_travel_times(evt_depth,
                                                  distance_in_degree=rdelta,
                                                  phase_list=phase)[0].time

            for arr in arrivals:
                if arr.time < primary_time - onset or arr.time > \
                        primary_time + 120 or arr.name == phase:
                    continue
                ph_name.append(arr.name)
                ph_time.append(arr.time - primary_time)

        else:
            # Caluclate travel times with different methods
            ph_time.append(
                model.get_travel_times_geo(evt_depth,
                                           evtlat,
                                           evtlon,
                                           statlat,
                                           statlon,
                                           phase_list=phase)[0].time)

            d = []
            d.append(
                client.distaz(statlat, statlon, evtlat, evtlon)['distance'])

            d.append(
                kilometer2degrees(
                    gps2dist_azimuth(statlat, statlon, evtlat, evtlon)[0] /
                    1000))

            for dis in d:
                ph_time.append(
                    model.get_travel_times(evt_depth, dis,
                                           phase_list=phase)[0].time)
            ph_name = ['taup', 'iris', 'geodetics']
            ph_time = np.array(ph_time) - (st[0].stats.starttime + onset -
                                           UTCDateTime(ot))

        # waveform data
        st.sort()
        for tr in st:
            y.append(tr.data)
            ch.append(tr.stats.channel[2])

        # create time vector
        t = np.linspace(0 - onset, tr.stats.npts * tr.stats.delta - onset,
                        len(y[0]))

        # plot
        fig, ax = __draw_plot(starttime, t, y, ph_time, ph_name, ch, noisemat,
                              RF, old, rdelta, mag, crit, ot, evt_depth, phase,
                              test_tt_calculation)
        while not plt.waitforbuttonpress(30):
            # Taper when input is there
            if len(rating["taper"]) == 2:
                if rating["taper"][0] < rating["taper"][1]:
                    trim = [rating["taper"][0], rating["taper"][1]]
                else:
                    trim = [rating["taper"][1], rating["taper"][0]]
                trim[0] = trim[0] - t[0]
                trim[1] = t[-1] - trim[1]

                RF = createRF(PSV,
                              phase,
                              method=decon_meth,
                              shift=onset,
                              trim=trim)

                __draw_plot(starttime, t, y, ph_time, ph_name, ch, noisemat,
                            RF, old, rdelta, mag, crit, ot, evt_depth, phase,
                            test_tt_calculation)
                rating["taper"].clear()
        # fig.canvas.mpl_connect('key_press_event', ontype)
        if rating["k"] == "q":
            break
        elif "k" in rating:
            __w_file(network, st[0].stats.station, starttime)