Exemple #1
0
    def waveletTransform(self):
        # Wavelet transform:
        self.wave, self.period, self.scale, self.coi = \
            wavelet(self.data, self.cadence, self.pad, self.dj, self.s0, self.j1, self.mother)

        if len(self.time) != len(self.coi):
            self.coi = self.coi[1:]

        self.power = (np.abs(self.wave))**2  # compute wavelet power spectrum

        # Significance levels: (variance=1 for the normalized data)
        signif = wave_signif(([1.0]), dt=self.cadence, sigtest=0, scale=self.scale, \
            lag1=self.lag1, mother=self.mother, siglvl = self.siglvl)
        self.sig95 = signif[:, np.newaxis].dot(np.ones(
            self.n)[np.newaxis, :])  # expand signif --> (J+1)x(N) array
        self.sig95 = self.power / self.sig95  # where ratio > 1, power is significant
        return
Exemple #2
0
                    sst = sst / np.std(sst, ddof=1)
                n = len(sst)
                dt = 0.01
                #                time = np.arange(len(sst)) * dt + 1871.0  # construct time array
                time = wl_fall_time  # construct time array
                xlim = ([min(time), max(time)])  # plotting range
                pad = 1  # pad the time series with zeroes (recommended)
                dj = 0.25  # this will do 4 sub-octaves per octave
                s0 = 2 * dt  # this says start at a scale of 6 months
                j1 = 7 / dj  # this says do 7 powers-of-two with dj sub-octaves each
                lag1 = 0.72  # lag-1 autocorrelation for red noise background
                print("lag1 = ", lag1)
                mother = 'MORLET'

                # Wavelet transform:
                wave, period, scale, coi = wavelet(sst, dt, pad, dj, s0, j1,
                                                   mother)
                power = (np.abs(wave))**2  # compute wavelet power spectrum
                global_ws = (np.sum(power, axis=1) / n
                             )  # time-average over all times

                # Significance levels:
                signif = wave_signif(([variance]),
                                     dt=dt,
                                     sigtest=0,
                                     scale=scale,
                                     lag1=lag1,
                                     mother=mother)
                sig95 = signif[:, np.newaxis].dot(np.ones(n)[
                    np.newaxis, :])  # expand signif --> (J+1)x(N) array
                sig95 = power / sig95  # where ratio > 1, power is significant
# "http://paos.colorado.edu/research/wavelets/plot/"
variance = np.std(sst1, ddof=1) ** 2
sst = (sst1 - np.mean(sst1)) / np.std(sst1, ddof=1)
n = len(sst)
dt = time[1] - time[0]
time = np.arange(len(sst)) * dt  + 28800.0 # construct time array 
xlim = ([28800.0 , 36600.0])  # plotting range
pad = 1  # pad the time series with zeroes (recommended)
dj = 0.25  # this will do 4 sub-octaves per octave
s0 = 2 * dt  # this says start at a scale of 6 months
j1 = 7 / dj  # this says do 7 powers-of-two with dj sub-octaves each
lag1 = 0.72  # lag-1 autocorrelation for red noise background
mother = 'PAUL'  # 'PAUL'

# Wavelet transform:
wave, period, scale, coi = wavelet(sst, dt, pad, dj, s0, j1, mother)
power = (np.abs(wave)) ** 2  # compute wavelet power spectrum

# Significance levels: (variance=1 for the normalized SST)
signif = wave_signif(([1.0]), dt=dt, sigtest=0, scale=scale, lag1=lag1, mother=mother)
sig95 = signif[:, np.newaxis].dot(np.ones(n)[np.newaxis, :])  # expand signif --> (J+1)x(N) array
sig95 = power / sig95  # where ratio > 1, power is significant

# Global wavelet spectrum & significance levels:
global_ws = variance * (np.sum(power, axis=1) / n)  # time-average over all times
dof = n - scale  # the -scale corrects for padding at edges
global_signif = wave_signif(variance, dt=dt, scale=scale, sigtest=1, lag1=lag1, dof=dof, mother=mother)

# Scale-average between El Nino periods of 2--8 years
avg = np.logical_and(scale >= 2, scale < 8)
Cdelta = 0.776  # this is for the MORLET wavelet
#------------ LOMB-SCARGLE APPROACH ----------------
pers, pows = lombscargle(time,
                         data,
                         err,
                         Pmin=s0,
                         Pmax=np.max(time) - np.min(time))
plot_lcper(time, data, pers, pows, lctype=lctype)
peakpows_LS = peakutils.indexes(pows, thres=0.2, min_dist=10)
peakpers_LS = pers[peakpows_LS]

#------------ WAVELET TRANSFORM ----------------
# wave, period, scale, coi = wavelet(data, dt, pad, dj, s0, j1, mother='MORLET')
wave, period, scale, coi = wavelet(data,
                                   dt,
                                   pad,
                                   dj=0.05,
                                   mother=mother,
                                   param=6)
power = (np.abs(wave))**2  # compute wavelet power spectrum
global_ws = (np.sum(power, axis=1) / n)  # time-average over all times

# Significance levels:
signif = wave_signif(([variance]),
                     dt=dt,
                     sigtest=0,
                     scale=scale,
                     lag1=lag1,
                     mother=mother)
sig95 = signif[:, np.newaxis].dot(
    np.ones(n)[np.newaxis, :])  # expand signif --> (J+1)x(N) array
sig95 = power / sig95  # where ratio > 1, power is significant
def plot_according_to_library(field):
    cumulative_differences, interpolated_data = get_interpolated_data(field)

    sst = interpolated_data

    sst = sst - np.mean(sst)

    variance = np.std(sst, ddof=1) ** 2

    n = len(sst)
    dt = 11
    time = np.arange(len(sst)) * dt + 1  # construct time array

    xlim = ([time[0], time[-1]])  # plotting range

    pad = 1  # pad the time series with zeroes (recommended)
    dj = 0.25 / 4  # this will do 4 sub-octaves per octave
    s0 = 2 * dt  # this says start at a scale of 6 months
    j1 = 7 / dj  # this says do 7 powers-of-two with dj sub-octaves each
    lag1 = 0.72  # lag-1 autocorrelation for red noise background
    mother = 'MORLET'

    # Wavelet transform:
    wave, period, scale, coi = wavelet(sst, dt, pad, dj, s0, j1, mother)
    power = (np.abs(wave)) ** 2  # compute wavelet power spectrum
    global_ws = (np.sum(power, axis=1) / n)  # time-average over all times

    # Significance levels:
    signif = wave_signif(
        (
            [variance]
        ),
        dt=dt,
        sigtest=0, scale=scale,
        lag1=lag1, mother=mother
    )
    sig95 = signif[:, np.newaxis].dot(
        np.ones(n)[np.newaxis, :]
    )  # expand signif --> (J+1)x(N) array
    sig95 = power / sig95  # where ratio > 1, power is significant

    # Global wavelet spectrum & significance levels:
    dof = n - scale  # the -scale corrects for padding at edges
    global_signif = wave_signif(
        variance, dt=dt, scale=scale, sigtest=1,
        lag1=lag1, dof=dof, mother=mother
    )

    fig = plt.figure(figsize=(9, 10))

    gs = GridSpec(3, 4, hspace=0.4, wspace=0.75)

    plt.subplots_adjust(
        left=0.1, bottom=0.05,
        right=0.9, top=0.95, wspace=0, hspace=0
    )
    plt.subplot(gs[0, 0:3])
    plt.plot(time, sst, 'k')
    plt.xlim(xlim[:])
    plt.xlabel('Time (Seconds)')
    plt.ylabel('{}'.format(snake_to_camel(field)))
    plt.title('a) {} vs Time'.format(snake_to_camel(field)))

    # --- Contour plot wavelet power spectrum
    # plt3 = plt.subplot(3, 1, 2)
    plt3 = plt.subplot(gs[1, 0:3])
    levels = [0, 0.5, 1, 2, 4, 999]
    CS = plt.contourf(
        time, period, power, len(levels)
    )  # *** or use 'contour'
    im = plt.contourf(
        CS, levels=levels,
        colors=['white', 'bisque', 'orange', 'orangered', 'darkred']
    )
    plt.xlabel('Time (Seconds)')
    plt.ylabel('Period (Seconds)')
    plt.title('b) Wavelet Power Spectrum (contours at 0.5,1,2,4\u00B0C$^2$)')
    plt.xlim(xlim[:])
    # 95# significance contour, levels at -99 (fake) and 1 (95# signif)
    plt.contour(time, period, sig95, [-99, 1], colors='k')
    # cone-of-influence, anything "below" is dubious
    plt.plot(time, coi, 'k')
    # format y-scale
    plt3.set_yscale('log', basey=2, subsy=None)
    plt.ylim([np.min(period), np.max(period)])
    ax = plt.gca().yaxis
    ax.set_major_formatter(ticker.ScalarFormatter())
    plt3.ticklabel_format(axis='y', style='plain')
    plt3.invert_yaxis()
    # set up the size and location of the colorbar

    # position = fig.add_axes([0.5, 0.36, 0.2, 0.01])

    # plt.colorbar(
    #     im, cax=position, orientation='horizontal'
    # )  # fraction=0.05, pad=0.5)

    plt.subplots_adjust(right=0.7, top=0.9)

    # --- Plot global wavelet spectrum
    plt4 = plt.subplot(gs[1, -1])
    plt.plot(global_ws, period)
    plt.plot(global_signif, period, '--')
    plt.xlabel('Power (\u00B0C$^2$)')
    plt.title('c) Global Wavelet Spectrum')
    plt.xlim([0, 1.25 * np.max(global_ws)])
    # format y-scale
    plt4.set_yscale('log', basey=2, subsy=None)
    plt.ylim([np.min(period), np.max(period)])
    ax = plt.gca().yaxis
    ax.set_major_formatter(ticker.ScalarFormatter())
    plt4.ticklabel_format(axis='y', style='plain')
    plt4.invert_yaxis()

    # fig.tight_layout()

    plt.show()