Exemplo n.º 1
0
        #        df = df.loc[df['cc'] * df['nchannel'] >= 0.1 * maxc]
        df = df.loc[df['cc'] *
                    df['nchannel'] >= threshold['threshold_perm'].iloc[i]]

        time = np.arange(xmin, xmax, 1.0 / 365.5)
        nbLFEs = np.zeros(np.shape(time)[0])
        # Loop on LFEs
        for j in range(0, len(df)):
            myYear = df['year'].iloc[j]
            myMonth = df['month'].iloc[j]
            myDay = df['day'].iloc[j]
            myHour = df['hour'].iloc[j]
            myMinute = df['minute'].iloc[j]
            mySecond = int(floor(df['second'].iloc[j]))
            myMicrosecond = int(1000000.0 * (df['second'].iloc[j] - mySecond))
            t = ymdhms2day(myYear, myMonth, myDay, myHour, myMinute, mySecond)
            index = int((t - xmin) * 365.5)
            if (index >= 0) and (index < len(nbLFEs)):
                nbLFEs[index] = nbLFEs[index] + 1

        # Plot LFEs
        time = time[nbLFEs > 0]
        nbLFEs = nbLFEs[nbLFEs > 0]
        plt.scatter(time,
                    np.repeat(latitude,
                              np.shape(time)[0]),
                    s=1 + nbLFEs * 5,
                    c='k')
#        plt.scatter(time, np.repeat(latitude, np.shape(time)[0]), c=nbLFEs, cmap='autumn')

# Plot time line
def compare_tremor_GPS(station_file, lats, lons, dataset, direction, \
    radius_GPS, radius_tremor, slowness, wavelet, J, J0):
    """
    """
    # Read station file
    stations = pd.read_csv(station_file,
                           sep=r'\s{1,}',
                           header=None,
                           engine='python')
    stations.columns = ['name', 'longitude', 'latitude']

    # Read tremor files (A. Wech)
    data_2009 = pd.read_csv(
        '../data/tremor/tremor_events-2009-08-06T00 00 00-2009-12-31T23 59 59.csv'
    )
    data_2009['time '] = pd.to_datetime(data_2009['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2010 = pd.read_csv(
        '../data/tremor/tremor_events-2010-01-01T00 00 00-2010-12-31T23 59 59.csv'
    )
    data_2010['time '] = pd.to_datetime(data_2010['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2011 = pd.read_csv(
        '../data/tremor/tremor_events-2011-01-01T00 00 00-2011-12-31T23 59 59.csv'
    )
    data_2011['time '] = pd.to_datetime(data_2011['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2012 = pd.read_csv(
        '../data/tremor/tremor_events-2012-01-01T00 00 00-2012-12-31T23 59 59.csv'
    )
    data_2012['time '] = pd.to_datetime(data_2012['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2013 = pd.read_csv(
        '../data/tremor/tremor_events-2013-01-01T00 00 00-2013-12-31T23 59 59.csv'
    )
    data_2013['time '] = pd.to_datetime(data_2013['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2014 = pd.read_csv(
        '../data/tremor/tremor_events-2014-01-01T00 00 00-2014-12-31T23 59 59.csv'
    )
    data_2014['time '] = pd.to_datetime(data_2014['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2015 = pd.read_csv(
        '../data/tremor/tremor_events-2015-01-01T00 00 00-2015-12-31T23 59 59.csv'
    )
    data_2015['time '] = pd.to_datetime(data_2015['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2016 = pd.read_csv(
        '../data/tremor/tremor_events-2016-01-01T00 00 00-2016-12-31T23 59 59.csv'
    )
    data_2016['time '] = pd.to_datetime(data_2016['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2017 = pd.read_csv(
        '../data/tremor/tremor_events-2017-01-01T00 00 00-2017-12-31T23 59 59.csv'
    )
    data_2017['time '] = pd.to_datetime(data_2017['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2018 = pd.read_csv(
        '../data/tremor/tremor_events-2018-01-01T00 00 00-2018-12-31T23 59 59.csv'
    )
    data_2018['time '] = pd.to_datetime(data_2018['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2019 = pd.read_csv(
        '../data/tremor/tremor_events-2019-01-01T00 00 00-2019-12-31T23 59 59.csv'
    )
    data_2019['time '] = pd.to_datetime(data_2019['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2020 = pd.read_csv(
        '../data/tremor/tremor_events-2020-01-01T00 00 00-2020-12-31T23 59 59.csv'
    )
    data_2020['time '] = pd.to_datetime(data_2020['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2021 = pd.read_csv(
        '../data/tremor/tremor_events-2021-01-01T00 00 00-2021-04-29T23 59 59.csv'
    )
    data_2021['time '] = pd.to_datetime(data_2020['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data = pd.concat([data_2009, data_2010, data_2011, data_2012, data_2013, \
        data_2014, data_2015, data_2016, data_2017, data_2018, data_2019, \
        data_2020, data_2021])
    data.reset_index(drop=True, inplace=True)

    a = 6378.136
    e = 0.006694470

    # Start figure
    plt.style.use('bmh')
    fig = plt.figure(figsize=(5, 16))

    # Loop on latitude and longitude
    for index, (lat, lon) in enumerate(zip(lats, lons)):
        ax1 = plt.subplot2grid((len(lats), 1), (len(lats) - index - 1, 0))

        # Keep only stations in a given radius
        dx = (pi / 180.0) * a * cos(lat * pi / 180.0) / sqrt(1.0 - e * e * \
            sin(lat * pi / 180.0) * sin(lat * pi / 180.0))
        dy = (3.6 * pi / 648.0) * a * (1.0 - e * e) / ((1.0 - e * e * \
            sin(lat * pi / 180.0) * sin(lat * pi / 180.0)) ** 1.5)
        x = dx * (stations['longitude'] - lon)
        y = dy * (stations['latitude'] - lat)
        stations['distance'] = np.sqrt(np.power(x, 2.0) + np.power(y, 2.0))
        mask = stations['distance'] <= radius_GPS
        sub_stations = stations.loc[mask].copy()
        sub_stations.reset_index(drop=True, inplace=True)

        # Wavelet vectors initialization
        times = []
        disps = []
        Ws = []
        Vs = []
        Ds = []
        Ss = []

        # Read output files from wavelet transform
        for (station, lon_sta, lat_sta) in zip(sub_stations['name'],
                                               sub_stations['longitude'],
                                               sub_stations['latitude']):
            filename = 'tmp/' + dataset + '_' + station + '_' + direction + '.pkl'
            (time, disp, W, V, D, S) = pickle.load(open(filename, 'rb'))
            if ((np.min(time) <= 2021.25) and (np.max(time) >= 2009.25)):
                times.append(time)
                disps.append(disp)
                Ws.append(W)
                Vs.append(V)
                Ds.append(D)
                Ss.append(S)

        # Divide into time blocks
        tblocks = []
        for time in times:
            tblocks.append(np.min(time))
            tblocks.append(np.max(time))
        tblocks = sorted(set(tblocks))
        tbegin = tblocks[0:-1]
        tend = tblocks[1:]

        # Initializations
        times_stacked = []
        disps_stacked = []
        vesps = []

        # Loop on time blocks
        for t in range(0, len(tblocks) - 1):

            # Find time period
            for time in times:
                indices = np.where((time >= tbegin[t]) & (time < tend[t]))[0]
                if (len(indices) > 0):
                    time_subset = time[indices]
                    break
            times_stacked.append(time_subset)

            # Initialize vespagram
            vespagram = np.zeros((len(slowness), len(time_subset)))

            # Loop on stations
            nsta = 0
            Dj_stacked = np.zeros(len(time_subset))
            for (time, D, lat_sta) in zip(times, Ds, sub_stations['latitude']):
                indices = np.where((time >= tbegin[t]) & (time < tend[t]))[0]
                if (len(indices) > 0):
                    nsta = nsta + 1
                    tj = time[indices]
                    Dj = D[J][indices]
                    Dj_interp = np.interp(time_subset, tj, Dj)
                    Dj_stacked = Dj_stacked + Dj_interp
                    for i in range(0, len(slowness)):
                        Dj_interp = np.interp(time_subset + \
                            slowness[i] * (lat_sta - lat), tj, Dj)
                        vespagram[i, :] = vespagram[i, :] + Dj_interp
            Dj_stacked = Dj_stacked / nsta
            vespagram[:, :] = vespagram[:, :] / nsta
            disps_stacked.append(Dj_stacked)
            vesps.append(vespagram)

        # Concatenate times and disps
        times_stacked = np.concatenate(times_stacked)
        disps_stacked = np.concatenate(disps_stacked)
        vespagram = np.concatenate(vesps, axis=1)

        # Filter displacement
        disps_stacked = disps_stacked[(times_stacked >= 2009.25)
                                      & (times_stacked <= 2021.25)]
        vespagram = vespagram[:, (times_stacked >= 2009.25) &
                              (times_stacked <= 2021.25)]
        times_stacked = times_stacked[(times_stacked >= 2009.25)
                                      & (times_stacked <= 2021.25)]

        # Keep only tremor in a given radius
        dx = (pi / 180.0) * a * cos(lat * pi / 180.0) / sqrt(1.0 - e * e * \
            sin(lat * pi / 180.0) * sin(lat * pi / 180.0))
        dy = (3.6 * pi / 648.0) * a * (1.0 - e * e) / ((1.0 - e * e * \
            sin(lat * pi / 180.0) * sin(lat * pi / 180.0)) ** 1.5)
        x = dx * (data['lon'] - lon)
        y = dy * (data['lat'] - lat)
        distance = np.sqrt(np.power(x, 2.0) + np.power(y, 2.0))
        data['distance'] = distance
        tremor = data.loc[data['distance'] <= radius_tremor].copy()
        tremor.reset_index(drop=True, inplace=True)

        # Convert tremor time
        nt = len(tremor)
        time_tremor = np.zeros(nt)
        for i in range(0, nt):
            year = tremor['time '].loc[i].year
            month = tremor['time '].loc[i].month
            day = tremor['time '].loc[i].day
            hour = tremor['time '].loc[i].hour
            minute = tremor['time '].loc[i].minute
            second = tremor['time '].loc[i].second
            time_tremor[i] = date.ymdhms2day(year, month, day, hour, minute,
                                             second)

        # Interpolate
        tremor = np.interp(times_stacked, np.sort(time_tremor),
                           (1.0 / nt) * np.arange(0, len(time_tremor)))

        # Detrend
        coeffs = np.polyfit(times_stacked, tremor, 1)
        p = np.poly1d(coeffs)
        tremor_detrend = tremor - p(times_stacked)

        # MODWT
        (W, V) = pyramid(tremor_detrend, wavelet, J0)
        (D, S) = get_DS(tremor_detrend, W, wavelet, J0)

        # Save tremor MODWT
        filename = 'tremor/loc' + str(index) + '.pkl'
        pickle.dump([times_stacked, tremor, tremor_detrend, W, V, D, S], \
            open(filename, 'wb'))

        cc = np.zeros(len(D))
        # Cross-correlation
        for (j, Dj) in enumerate(D):
            cc[j] = np.max(np.abs(correlate(disps_stacked, Dj, 20)))
        best = np.argmax(cc)

        # Figure
        if len(times_stacked) > 0:
            plt.contourf(times_stacked, slowness * 365.25 / dy, \
                vespagram, cmap=plt.get_cmap('seismic'), \
                norm=Normalize(vmin=-1.5, vmax=1.5))
            norm_min = np.min(slowness * 365.25 / dy) / np.min(D[best])
            norm_max = np.max(slowness * 365.25 / dy) / np.max(D[best])
            plt.plot(times_stacked,
                     min(norm_min, norm_max) * D[best],
                     color='grey',
                     linewidth=1)
            plt.annotate('{:d}th level: cc = {:0.2f}'.format(best + 1, np.max(cc)), \
                (2009.25 + 0.8 * (2021.25 - 2009.25), 0.06 * 365.25 / dy), fontsize=5)
        plt.xlim([2009.25, 2021.25])
        plt.grid(b=None)
        if index == 0:
            plt.xlabel('Time (year)')
        if index != 0:
            ax1.axes.xaxis.set_ticks([])
        ax1.axes.yaxis.set_ticks([])

    plt.suptitle('Slow slip and tremor from {} to {}'.format(2009.25, 2021.25))
    plt.savefig('comparison_' + str(J + 1) + '.pdf', format='pdf')
    plt.close(1)
Exemplo n.º 3
0
def compute_wavelets_tremor(lats, lons, radius_tremor, wavelet, J):
    """
    """
    # Read tremor files (A. Wech)
    data_2009 = pd.read_csv('../data/tremor/tremor_events-2009-08-06T00 00 00-2009-12-31T23 59 59.csv')
    data_2009['time '] = pd.to_datetime(data_2009['time '], format='%Y-%m-%d %H:%M:%S')
    data_2010 = pd.read_csv('../data/tremor/tremor_events-2010-01-01T00 00 00-2010-12-31T23 59 59.csv')
    data_2010['time '] = pd.to_datetime(data_2010['time '], format='%Y-%m-%d %H:%M:%S')
    data_2011 = pd.read_csv('../data/tremor/tremor_events-2011-01-01T00 00 00-2011-12-31T23 59 59.csv')
    data_2011['time '] = pd.to_datetime(data_2011['time '], format='%Y-%m-%d %H:%M:%S')
    data_2012 = pd.read_csv('../data/tremor/tremor_events-2012-01-01T00 00 00-2012-12-31T23 59 59.csv')
    data_2012['time '] = pd.to_datetime(data_2012['time '], format='%Y-%m-%d %H:%M:%S')
    data_2013 = pd.read_csv('../data/tremor/tremor_events-2013-01-01T00 00 00-2013-12-31T23 59 59.csv')
    data_2013['time '] = pd.to_datetime(data_2013['time '], format='%Y-%m-%d %H:%M:%S')
    data_2014 = pd.read_csv('../data/tremor/tremor_events-2014-01-01T00 00 00-2014-12-31T23 59 59.csv')
    data_2014['time '] = pd.to_datetime(data_2014['time '], format='%Y-%m-%d %H:%M:%S')
    data_2015 = pd.read_csv('../data/tremor/tremor_events-2015-01-01T00 00 00-2015-12-31T23 59 59.csv')
    data_2015['time '] = pd.to_datetime(data_2015['time '], format='%Y-%m-%d %H:%M:%S')
    data_2016 = pd.read_csv('../data/tremor/tremor_events-2016-01-01T00 00 00-2016-12-31T23 59 59.csv')
    data_2016['time '] = pd.to_datetime(data_2016['time '], format='%Y-%m-%d %H:%M:%S')
    data_2017 = pd.read_csv('../data/tremor/tremor_events-2017-01-01T00 00 00-2017-12-31T23 59 59.csv')
    data_2017['time '] = pd.to_datetime(data_2017['time '], format='%Y-%m-%d %H:%M:%S')
    data_2018 = pd.read_csv('../data/tremor/tremor_events-2018-01-01T00 00 00-2018-12-31T23 59 59.csv')
    data_2018['time '] = pd.to_datetime(data_2018['time '], format='%Y-%m-%d %H:%M:%S')
    data_2019 = pd.read_csv('../data/tremor/tremor_events-2019-01-01T00 00 00-2019-12-31T23 59 59.csv')
    data_2019['time '] = pd.to_datetime(data_2019['time '], format='%Y-%m-%d %H:%M:%S')
    data_2020 = pd.read_csv('../data/tremor/tremor_events-2020-01-01T00 00 00-2020-12-31T23 59 59.csv')
    data_2020['time '] = pd.to_datetime(data_2020['time '], format='%Y-%m-%d %H:%M:%S')
    data_2021 = pd.read_csv('../data/tremor/tremor_events-2021-01-01T00 00 00-2021-04-29T23 59 59.csv')
    data_2021['time '] = pd.to_datetime(data_2020['time '], format='%Y-%m-%d %H:%M:%S')
    data = pd.concat([data_2009, data_2010, data_2011, data_2012, data_2013, \
        data_2014, data_2015, data_2016, data_2017, data_2018, data_2019, \
        data_2020, data_2021])
    data.reset_index(drop=True, inplace=True)

    # To convert lat/lon into kilometers
    a = 6378.136
    e = 0.006694470

    # Time vector
    time = pickle.load(open('tmp/time.pkl', 'rb'))

    # Loop on latitude and longitude
    for index, (lat, lon) in enumerate(zip(lats, lons)):

        # Keep only tremor in a given radius
        dx = (pi / 180.0) * a * cos(lat * pi / 180.0) / sqrt(1.0 - e * e * \
            sin(lat * pi / 180.0) * sin(lat * pi / 180.0))
        dy = (3.6 * pi / 648.0) * a * (1.0 - e * e) / ((1.0 - e * e * \
            sin(lat * pi / 180.0) * sin(lat * pi / 180.0)) ** 1.5)
        x = dx * (data['lon'] - lon)
        y = dy * (data['lat'] - lat)
        distance = np.sqrt(np.power(x, 2.0) + np.power(y, 2.0))
        data['distance'] = distance
        tremor = data.loc[data['distance'] <= radius_tremor].copy()
        tremor.reset_index(drop=True, inplace=True)
 
        # Convert tremor time
        nt = len(tremor)
        time_tremor = np.zeros(nt)
        for i in range(0, nt):
            year = tremor['time '].loc[i].year
            month = tremor['time '].loc[i].month
            day = tremor['time '].loc[i].day
            hour = tremor['time '].loc[i].hour
            minute = tremor['time '].loc[i].minute
            second = tremor['time '].loc[i].second
            time_tremor[i] = date.ymdhms2day(year, month, day, hour, minute, second)

        # Interpolate
        tremor = np.interp(time, np.sort(time_tremor), (1.0 / nt) * np.arange(0, len(time_tremor)))

        # Start figure
        params = {'xtick.labelsize':24,
                  'ytick.labelsize':24}
        pylab.rcParams.update(params)   
        fig = plt.figure(1, figsize=(60, 5 * (J + 3)))

        # First method: Detrending
        tremor_detrend = detrend(tremor)

        # MODWT
        (W, V) = pyramid(tremor_detrend, wavelet, J)
        (D, S) = get_DS(tremor_detrend, W, wavelet, J)

        # Save wavelets
#        pickle.dump([time, tremor_detrend, W, V, D, S], \
#            open('tmp/tremor_' + str(index) + '.pkl', 'wb'))
        
        maxD = max([np.max(Dj) for Dj in D])
        minD = min([np.min(Dj) for Dj in D])

        # Plot data
        plt.subplot2grid((J + 2, 4), (J + 1, 0))
        plt.plot(time, tremor_detrend, 'k', label='Data')
        plt.legend(loc=3, fontsize=14)
        # Plot details
        for j in range(0, J):
            plt.subplot2grid((J + 2, 4), (J - j, 0))
            plt.plot(time, D[j], 'k', label='D' + str(j + 1))
            plt.ylim(minD, maxD)
            plt.legend(loc=3, fontsize=14)
        # Plot smooth
        plt.subplot2grid((J + 2, 4), (0, 0))
        plt.plot(time, S[J], 'k', label='S' + str(J))
        plt.ylim(minD, maxD)
        plt.legend(loc=3, fontsize=14)
        plt.title('Detrended data', fontsize=24)

        # Second method: Moving average (5 days)
        ma_filter = np.repeat(1, 5) / 5
        tremor_averaged_5 = tremor[:-4] - np.convolve(tremor, ma_filter, 'valid')

        # MODWT
        (W, V) = pyramid(tremor_averaged_5, wavelet, J)
        (D, S) = get_DS(tremor_averaged_5, W, wavelet, J)
        
        maxD = max([np.max(Dj) for Dj in D])
        minD = min([np.min(Dj) for Dj in D])

        # Plot data
        plt.subplot2grid((J + 2, 4), (J + 1, 1))
        plt.plot(time[:-4], tremor_averaged_5, 'k', label='Data')
        plt.legend(loc=3, fontsize=14)
        # Plot details
        for j in range(0, J):
            plt.subplot2grid((J + 2, 4), (J - j, 1))
            plt.plot(time[:-4], D[j], 'k', label='D' + str(j + 1))
            plt.ylim(minD, maxD)
            plt.legend(loc=3, fontsize=14)
        # Plot smooth
        plt.subplot2grid((J + 2, 4), (0, 1))
        plt.plot(time[:-4], S[J], 'k', label='S' + str(J))
        plt.ylim(minD, maxD)
        plt.legend(loc=3, fontsize=14)
        plt.title('Moving average (5 days)', fontsize=24)

        # Second method: Moving average (15 days)
        ma_filter = np.repeat(1, 15) / 15
        tremor_averaged_15 = tremor[:-14] - np.convolve(tremor, ma_filter, 'valid')

        # MODWT
        (W, V) = pyramid(tremor_averaged_15, wavelet, J)
        (D, S) = get_DS(tremor_averaged_15, W, wavelet, J)
        
        maxD = max([np.max(Dj) for Dj in D])
        minD = min([np.min(Dj) for Dj in D])

        # Plot data
        plt.subplot2grid((J + 2, 4), (J + 1, 2))
        plt.plot(time[:-14], tremor_averaged_15, 'k', label='Data')
        plt.legend(loc=3, fontsize=14)
        # Plot details
        for j in range(0, J):
            plt.subplot2grid((J + 2, 4), (J - j, 2))
            plt.plot(time[:-14], D[j], 'k', label='D' + str(j + 1))
            plt.ylim(minD, maxD)
            plt.legend(loc=3, fontsize=14)
        # Plot smooth
        plt.subplot2grid((J + 2, 4), (0, 2))
        plt.plot(time[:-14], S[J], 'k', label='S' + str(J))
        plt.ylim(minD, maxD)
        plt.legend(loc=3, fontsize=14)
        plt.title('Moving average (15 days)', fontsize=24)

        # Third method: Low-pass filter
        param1, param2 = butter(4, 0.1, btype='low')
        tremor_filtered = tremor - lfilter(param1, param2, tremor)

        # MODWT
        (W, V) = pyramid(tremor_filtered, wavelet, J)
        (D, S) = get_DS(tremor_filtered, W, wavelet, J)
        
        maxD = max([np.max(Dj) for Dj in D])
        minD = min([np.min(Dj) for Dj in D])

        # Plot data
        plt.subplot2grid((J + 2, 4), (J + 1, 3))
        plt.plot(time, tremor_filtered, 'k', label='Data')
        plt.legend(loc=3, fontsize=14)
        # Plot details
        for j in range(0, J):
            plt.subplot2grid((J + 2, 4), (J - j, 3))
            plt.plot(time, D[j], 'k', label='D' + str(j + 1))
            plt.ylim(minD, maxD)
            plt.legend(loc=3, fontsize=14)
        # Plot smooth
        plt.subplot2grid((J + 2, 4), (0, 3))
        plt.plot(time, S[J], 'k', label='S' + str(J))
        plt.ylim(minD, maxD)
        plt.legend(loc=3, fontsize=14)
        plt.title('Low-pass filter', fontsize=24)

        # Save figure
        plt.savefig('tremor_' + str(index) + '.pdf', format='pdf')
        plt.close(1)

        if __name__ == '__main__':

    station_file = '../data/PANGA/stations.txt'
    tremor_file = '../data/tremor/mbbp_cat_d_forHeidi'
    radius_GPS = 50
    radius_tremor = 50
    direction = 'lon'
    dataset = 'cleaned'
    wavelet = 'LA8'
    J = 8
    slowness = np.arange(-0.1, 0.105, 0.005)
    lats = [47.20000, 47.30000, 47.40000, 47.50000, 47.60000, 47.70000, \
        47.80000, 47.90000, 48.00000, 48.10000, 48.20000, 48.30000, 48.40000, \
        48.50000, 48.60000, 48.70000]
    lons = [-122.74294, -122.73912, -122.75036, -122.77612, -122.81591, \
        -122.86920, -122.93549, -123.01425, -123.10498, -123.20716, \
        -123.32028, -123.44381, -123.57726, -123.72011, -123.87183, \
        -124.03193]
    tmin_GPS = 2017.28
    tmax_GPS = 2018.28
    tmin_tremor = 2017.75
    tmax_tremor = 2017.81
    lonmin = -125.4
    lonmax = -121.4
    latmin = 46.3
    latmax = 49.6
    j = 4

    compute_wavelets_tremor(lats, lons, radius_tremor, wavelet, J)
Exemplo n.º 4
0
def vesp_map(station_file, tremor_file, tmin_tremor, tmax_tremor, lats, lons, \
    dataset, direction, radius_GPS, tmin_GPS, tmax_GPS, latmin, latmax, lonmin, lonmax, \
    J, slowness):
    """
    """
    # Read station file
    stations = pd.read_csv(station_file,
                           sep=r'\s{1,}',
                           header=None,
                           engine='python')
    stations.columns = ['name', 'longitude', 'latitude']

    # Read tremor file (A. Ghosh)
    #    data = loadmat(tremor_file)
    #    mbbp = data['mbbp_cat_d']

    # Read tremor files (A. Wech)
    data_2009 = pd.read_csv(
        '../data/tremor/tremor_events-2009-08-06T00 00 00-2009-12-31T23 59 59.csv'
    )
    data_2009['time '] = pd.to_datetime(data_2009['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2010 = pd.read_csv(
        '../data/tremor/tremor_events-2010-01-01T00 00 00-2010-12-31T23 59 59.csv'
    )
    data_2010['time '] = pd.to_datetime(data_2010['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2011 = pd.read_csv(
        '../data/tremor/tremor_events-2011-01-01T00 00 00-2011-12-31T23 59 59.csv'
    )
    data_2011['time '] = pd.to_datetime(data_2011['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2012 = pd.read_csv(
        '../data/tremor/tremor_events-2012-01-01T00 00 00-2012-12-31T23 59 59.csv'
    )
    data_2012['time '] = pd.to_datetime(data_2012['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2013 = pd.read_csv(
        '../data/tremor/tremor_events-2013-01-01T00 00 00-2013-12-31T23 59 59.csv'
    )
    data_2013['time '] = pd.to_datetime(data_2013['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2014 = pd.read_csv(
        '../data/tremor/tremor_events-2014-01-01T00 00 00-2014-12-31T23 59 59.csv'
    )
    data_2014['time '] = pd.to_datetime(data_2014['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2015 = pd.read_csv(
        '../data/tremor/tremor_events-2015-01-01T00 00 00-2015-12-31T23 59 59.csv'
    )
    data_2015['time '] = pd.to_datetime(data_2015['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2016 = pd.read_csv(
        '../data/tremor/tremor_events-2016-01-01T00 00 00-2016-12-31T23 59 59.csv'
    )
    data_2016['time '] = pd.to_datetime(data_2016['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2017 = pd.read_csv(
        '../data/tremor/tremor_events-2017-01-01T00 00 00-2017-12-31T23 59 59.csv'
    )
    data_2017['time '] = pd.to_datetime(data_2017['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2018 = pd.read_csv(
        '../data/tremor/tremor_events-2018-01-01T00 00 00-2018-12-31T23 59 59.csv'
    )
    data_2018['time '] = pd.to_datetime(data_2018['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2019 = pd.read_csv(
        '../data/tremor/tremor_events-2019-01-01T00 00 00-2019-12-31T23 59 59.csv'
    )
    data_2019['time '] = pd.to_datetime(data_2019['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2020 = pd.read_csv(
        '../data/tremor/tremor_events-2020-01-01T00 00 00-2020-12-31T23 59 59.csv'
    )
    data_2020['time '] = pd.to_datetime(data_2020['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data_2021 = pd.read_csv(
        '../data/tremor/tremor_events-2021-01-01T00 00 00-2021-04-29T23 59 59.csv'
    )
    data_2021['time '] = pd.to_datetime(data_2020['time '],
                                        format='%Y-%m-%d %H:%M:%S')
    data = pd.concat([data_2009, data_2010, data_2011, data_2012, data_2013, \
        data_2014, data_2015, data_2016, data_2017, data_2018, data_2019, \
        data_2020, data_2021])
    data.reset_index(drop=True, inplace=True)

    # Convert begin and end times for tremor
    (year1, month1, day1, hour1, minute1,
     second1) = date.day2ymdhms(tmin_tremor)
    day_begin = int(
        floor(date.ymdhms2matlab(year1, month1, day1, hour1, minute1,
                                 second1)))
    (year2, month2, day2, hour2, minute2,
     second2) = date.day2ymdhms(tmax_tremor)
    day_end = int(
        floor(date.ymdhms2matlab(year2, month2, day2, hour2, minute2,
                                 second2)))

    # Start figure
    plt.style.use('bmh')
    fig = plt.figure()
    a = 6378.136
    e = 0.006694470

    # Part 1: vespagrams

    # Stations used for figure
    lon_stas = []
    lat_stas = []

    # Loop on latitude and longitude
    for index, (lat, lon) in enumerate(zip(lats, lons)):
        ax1 = plt.subplot2grid((len(lats), 3), (len(lats) - index - 1, 0))

        # Keep only stations in a given radius
        dx = (pi / 180.0) * a * cos(lat * pi / 180.0) / sqrt(1.0 - e * e * \
            sin(lat * pi / 180.0) * sin(lat * pi / 180.0))
        dy = (3.6 * pi / 648.0) * a * (1.0 - e * e) / ((1.0 - e * e * \
            sin(lat * pi / 180.0) * sin(lat * pi / 180.0)) ** 1.5)
        x = dx * (stations['longitude'] - lon)
        y = dy * (stations['latitude'] - lat)
        stations['distance'] = np.sqrt(np.power(x, 2.0) + np.power(y, 2.0))
        mask = stations['distance'] <= radius_GPS
        sub_stations = stations.loc[mask].copy()
        sub_stations.reset_index(drop=True, inplace=True)

        # Wavelet vectors initialization
        times = []
        disps = []
        Ws = []
        Vs = []
        Ds = []
        Ss = []

        # Read output files from wavelet transform
        for (station, lon_sta, lat_sta) in zip(sub_stations['name'],
                                               sub_stations['longitude'],
                                               sub_stations['latitude']):
            filename = 'tmp/' + dataset + '_' + station + '_' + direction + '.pkl'
            (time, disp, W, V, D, S) = pickle.load(open(filename, 'rb'))
            if ((np.min(time) < tmax_GPS) and (np.max(time) > tmin_GPS)):
                lon_stas.append(lon_sta)
                lat_stas.append(lat_sta)
                times.append(time)
                disps.append(disp)
                Ws.append(W)
                Vs.append(V)
                Ds.append(D)
                Ss.append(S)

        # Divide into time blocks
        tblocks = []
        for time in times:
            tblocks.append(np.min(time))
            tblocks.append(np.max(time))
        tblocks = sorted(set(tblocks))
        tbegin = tblocks[0:-1]
        tend = tblocks[1:]

        # Initializations
        time_vesps = []
        vesps = []
        if len(tblocks) > 0:
            time_sta = np.zeros(2 * (len(tblocks) - 1))
            nb_sta = np.zeros(2 * (len(tblocks) - 1))

        # Loop on time blocks
        t0 = 0
        for t in range(0, len(tblocks) - 1):

            # Find time where we look at number of stations
            if ((tbegin[t] <= 0.5 * (tmin_GPS + tmax_GPS))
                    and (tend[t] >= 0.5 * (tmin_GPS + tmax_GPS))):
                t0 = t

            # Find time period
            for time in times:
                indices = np.where((time >= tbegin[t]) & (time < tend[t]))[0]
                if (len(indices) > 0):
                    time_subset = time[indices]
                    break
            time_vesps.append(time_subset)

            # Initialize vespagram
            vespagram = np.zeros((len(slowness), len(time_subset)))

            # Loop on time scales
            nsta = 0
            for (time, D, lat_sta) in zip(times, Ds, sub_stations['latitude']):
                indices = np.where((time >= tbegin[t]) & (time < tend[t]))[0]
                if (len(indices) > 0):
                    nsta = nsta + 1
                    tj = time[indices]
                    Dj = D[J][indices]
                    for i in range(0, len(slowness)):
                        Dj_interp = np.interp(time_subset + \
                            slowness[i] * (lat_sta - lat), tj, Dj)
                        vespagram[i, :] = vespagram[i, :] + Dj_interp
            vespagram[:, :] = vespagram[:, :] / nsta
            time_sta[2 * t] = tbegin[t]
            time_sta[2 * t + 1] = tend[t]
            nb_sta[2 * t] = nsta
            nb_sta[2 * t + 1] = nsta
            vesps.append(vespagram)

        # Figure
        if len(time_vesps) > 0:
            time_subset = np.concatenate(time_vesps)
            vespagram = np.concatenate(vesps, axis=1)

            max_value = np.max(vespagram[:, (time_subset >= tmin_GPS) &
                                         (time_subset <= tmax_GPS)])
            min_value = np.min(vespagram[:, (time_subset >= tmin_GPS) &
                                         (time_subset <= tmax_GPS)])
            max_index = np.argmax(vespagram[:, (time_subset >= tmin_GPS) &
                                            (time_subset <= tmax_GPS)])
            min_index = np.argmin(vespagram[:, (time_subset >= tmin_GPS) &
                                            (time_subset <= tmax_GPS)])
            (imax, jmax) = np.unravel_index(
                max_index,
                np.array(vespagram[:, (time_subset >= tmin_GPS) &
                                   (time_subset <= tmax_GPS)]).shape)
            (imin, jmin) = np.unravel_index(
                min_index,
                np.array(vespagram[:, (time_subset >= tmin_GPS) &
                                   (time_subset <= tmax_GPS)]).shape)
            t1 = time_subset[(time_subset >= tmin_GPS)
                             & (time_subset <= tmax_GPS)][jmax]
            t2 = time_subset[(time_subset >= tmin_GPS)
                             & (time_subset <= tmax_GPS)][jmin]
            print('{:d} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f}'. \
                  format(index, max_value, min_value, t1, t2, 0.5 * (t1 + t2)))

            plt.contourf(time_subset[(time_subset >= 2009.25) & (time_subset <= 2021.25)], slowness * 365.25 / dy, \
                vespagram[:, (time_subset >= 2009.25) & (time_subset <= 2021.25)], cmap=plt.get_cmap('seismic'), \
                norm=Normalize(vmin=-1.5, vmax=1.5))
            #                levels = np.linspace(-1.2, 1.2, 25))
            plt.axvline(0.5 * (tmin_GPS + tmax_GPS), color='grey', linewidth=1)
            plt.annotate('{:d} stations'.format(int(nb_sta[2 * t0])), \
                (tmin_GPS + 0.7 * (tmax_GPS - tmin_GPS), 0), fontsize=5)
        plt.xlim([tmin_GPS, tmax_GPS])
        plt.grid(b=None)
        if index == 0:
            plt.xlabel('Time (year)')
        if index != 0:
            ax1.axes.xaxis.set_ticks([])
        ax1.axes.yaxis.set_ticks([])

    # Part 2: map of tremor
    ax2 = plt.subplot2grid((len(lats), 3), (0, 1),
                           rowspan=len(lats),
                           colspan=2,
                           projection=ccrs.Mercator())
    shapename = 'ocean'
    ocean_shp = shapereader.natural_earth(resolution='10m',
                                          category='physical',
                                          name=shapename)
    shapename = 'land'
    land_shp = shapereader.natural_earth(resolution='10m',
                                         category='physical',
                                         name=shapename)
    ax2.set_extent([lonmin, lonmax, latmin, latmax], ccrs.Geodetic())
    ax2.gridlines(linestyle=":")
    for myfeature in shapereader.Reader(ocean_shp).geometries():
        ax2.add_geometries([myfeature],
                           ccrs.PlateCarree(),
                           facecolor='#E0FFFF',
                           edgecolor='black',
                           alpha=0.5)
    for myfeature in shapereader.Reader(land_shp).geometries():
        ax2.add_geometries([myfeature],
                           ccrs.PlateCarree(),
                           facecolor='#FFFFE0',
                           edgecolor='black',
                           alpha=0.5)

    # Keep only tremor on map (A. Ghosh)
#    lat_tremor = mbbp[:, 2]
#    lon_tremor = mbbp[:, 3]
#    find = np.where((lat_tremor >= latmin) & (lat_tremor <= latmax) \
#                  & (lon_tremor >= lonmin) & (lon_tremor <= lonmax))
#    tremor = mbbp[find, :][0, :, :]

# Keep only tremor on map (A. Wech)
    mask = ((data['lat'] >= latmin) & (data['lat'] <= latmax) \
          & (data['lon'] >= lonmin) & (data['lon'] <= lonmax))
    tremor = data.loc[mask].copy()
    tremor.reset_index(drop=True, inplace=True)

    # Keep only tremor in time interval (A. Ghosh)
    #    find = np.where((tremor[:, 0] >= day_begin) & (tremor[:, 0] < day_end))
    #    tremor_sub = tremor[find, :][0, :, :]

    # Keep only tremor in time interval (A. Wech)
    mask = ((tremor['time '] >= datetime(year1, month1, day1, hour1, minute1, second1)) \
          & (tremor['time '] <= datetime(year2, month2, day2, hour2, minute2, second2)))
    tremor_sub = tremor.loc[mask].copy()
    tremor_sub.reset_index(drop=True, inplace=True)

    # Convert tremor time (A. Ghosh)
    #    nt = np.shape(tremor_sub)[0]
    #    time_tremor = np.zeros(nt)
    #    for i in range(0, nt):
    #        (year, month, day, hour, minute, second) = date.matlab2ymdhms(tremor_sub[i, 0])
    #        time_tremor[i] = date.ymdhms2day(year, month, day, hour, minute, second)

    # Convert tremor time (A. Wech)
    nt = len(tremor_sub)
    time_tremor = np.zeros(nt)
    for i in range(0, nt):
        year = tremor_sub['time '].loc[i].year
        month = tremor_sub['time '].loc[i].month
        day = tremor_sub['time '].loc[i].day
        hour = tremor_sub['time '].loc[i].hour
        minute = tremor_sub['time '].loc[i].minute
        second = tremor_sub['time '].loc[i].second
        time_tremor[i] = date.ymdhms2day(year, month, day, hour, minute,
                                         second)

    # Plot tremor on map (A. Ghosh)


#    ax2.scatter(tremor_sub[:, 3], tremor_sub[:, 2], c=time_tremor, s=2, transform=ccrs.PlateCarree())

# Plot tremor on map (A. Wech)
    ax2.scatter(tremor_sub['lon'],
                tremor_sub['lat'],
                c=time_tremor,
                s=2,
                transform=ccrs.PlateCarree())

    # Plot GPS stations on map
    ax2.scatter(np.array(lon_stas),
                np.array(lat_stas),
                c='r',
                marker='^',
                s=20,
                transform=ccrs.PlateCarree())

    # Plot centers at which we look for GPS data on map
    ax2.scatter(np.array(lons),
                np.array(lats),
                c='k',
                marker='x',
                s=20,
                transform=ccrs.PlateCarree())

    ax2.set_title('Tremor from {:.2f} to {:.2f}: {:d} in {:d} days'.format( \
        tmin_tremor, tmax_tremor, np.shape(tremor_sub)[0], \
        int(floor(365.25 * (tmax_tremor - tmin_tremor)))), fontsize=10)

    plt.suptitle('Slow slip and tremor at {:.2f}'.format(
        0.5 * (tmin_GPS + tmax_GPS)))
    plt.savefig('vespagram_' + str(J + 1) + '.pdf', format='pdf')
    plt.close(1)