示例#1
0
def welch_lomb(t,x,ofac,n50,SAMP_R,detrend=True):        
    ave_seg_len = 2. * len(t) / (n50 + 1)
    if ave_seg_len%1 != 0:
        i = 1
        while ave_seg_len%1 != 0:
            ave_seg_len = 2. * (len(t)-i) / (n50 + 1)
            i+=1
        
    half_ave_seg_len = ave_seg_len/2
    
    start = 0
    end = ave_seg_len

    for i in range(n50):
    
    #split data into 50% overlapping segments
        print 'WOSA segment start =', start
        print 'WOSA segment end = ',end
        twk = t[start:end]
        xwk = x[start:end]
        start = start+half_ave_seg_len
        end = end+half_ave_seg_len

        #if detrend == True:
        #    xwk = scipy.signal.detrend(xwk, type='linear')

        #take lomb
        model_periods,model_mag,model_ph,fr,fi,amp_corr = modules.take_lomb(twk,xwk,ofac,SAMP_R)
        full_n = len(model_periods)

        #sum raw spectra
        try:
            gxx = gxx + model_mag
            fr_ave = fr_ave + fr
            fi_ave = fi_ave + fi
        except:
            gxx = model_mag   
            fr_ave = fr
            fi_ave = fi

    model_freq = 1./model_periods 
    df = model_freq[1] - model_freq[0]
    gxx  = gxx/n50
    
    #trend_test = model_periods < 10000
    #corr_periods = model_periods[trend_test]
    #corr_freq = model_freq[trend_test]
    #corr_gxx = gxx[trend_test]
    #corr_fr = fr_ave[trend_test]
    #corr_fi = fi_ave[trend_test]
    
    return model_periods,gxx,full_n,fr_ave,fi_ave
示例#2
0
def main_arg(x, y, grid_count):
    print grid_count
    ofac = 4

    #average dt of entire time series
    diffs = [x[i + 1] - x[i] for i in range(len(x) - 1)]
    avgdt = np.average(diffs)

    #make time start from 0
    x_from0 = modules.phase_start_correct(x)

    periods, mag, ph, fr, fi, amp_corr = modules.take_lomb(
        x_from0, y, ofac, avgdt)

    #get mean of values
    mean_array = np.average(y)

    #correct magnitude and phase for spectral leakage
    zoomfact = 1000
    half_annual_mag, half_annual_phase = modules.periodic_interp(
        fr, fi, zoomfact, periods, 365.25 / 2., len(y), amp_corr)
    annual_mag, annual_phase = modules.periodic_interp(fr, fi, zoomfact,
                                                       periods, 365.25, len(y),
                                                       amp_corr)

    #correct for phase shift as data starts in Oct 2004
    n_off = 273.25

    if n_off > 365.25 / 2:
        n_off = n_off - (365.25 / 2)
    offset = ((np.pi * 2) / (365.25 / 2)) * n_off
    half_annual_phase = half_annual_phase + offset
    if half_annual_phase > np.pi:
        half_annual_phase = -np.pi + (half_annual_phase - np.pi)

    n_off = 273.25
    offset = ((np.pi * 2) / (365.25)) * n_off
    annual_phase = annual_phase + offset
    if annual_phase > np.pi:
        annual_phase = -np.pi + (annual_phase - np.pi)

    #convert phase to time
    half_annual_phase = modules.convert_phase_units_actual_single(
        half_annual_phase, 6)
    annual_phase = modules.convert_phase_units_actual_single(annual_phase, 12)

    #np.save('mags_phases/mag_spectrums/%i'%(grid_count),mag)
    #np.save('mags_phases/phase_spectrums/%i'%(grid_count),ph)
    #np.save('mags_phases/periods',periods)
    return (half_annual_mag, half_annual_phase, annual_mag, annual_phase,
            mean_array)
def run_LSP(vals,x):

    lat_i = lat_indices[x]
    lon_i = lon_indices[x]

    print lat_i,lon_i

    current_lat = lat_c[lat_i]
    current_lon = lon_c[lon_i]
    site_lon = lon_c[lon_i]
 
    #check model vals are valid
    valid = vals >= 0
    vals = vals[valid]
    valid_times = model_ref_time[valid]
    
    full_times_year = model_ref_time[:8766]
    full_times_day = model_ref_time[:24] 
    
    #convert site_lon to 0 to 360 degs
    if site_lon < 0:
        site_lon = 360-np.abs(site_lon)
    
    #transform from UTC time to solar time 
    sun_time = lon_step_time*site_lon
    time_diff = sun_time - 0
    if time_diff > 12:
        time_diff = time_diff-24
             
    #make time start from 0    
    valid_times_from0 = modules.phase_start_correct(valid_times)

    ofac = 4
    samp_step = 1./24
    periods,mag,ph,fr,fi,amp_corr = modules.take_lomb(valid_times_from0,vals,ofac,samp_step,w=True,kp=[1./4.,1./3.,1./2.,1.,365.25/4.,365.25/3.,365.25/2.,365.25])
    
    #get info of weather regimes through model fit.
    grad1,grad2,grad3,bp1,bp2,bp_periods,bp_mag = modules.spectra_fit(periods,mag,ofac)
    
    #get mean of values
    mean_array = np.average(vals)

    #correct all phases for start point
    ph = modules.phase_start_point_correct_all(periods,ph,valid_times)

    #convert phase to time(days)
    ph = modules.convert_phase_units_actual_all(ph,periods)
        
    return (x,periods,mag,ph,grad1,grad2,grad3,bp1,bp2,bp_mag)
def main_arg(x,y,grid_count):  
    print grid_count
    ofac = 4

    #average dt of entire time series
    diffs = [x[i+1]-x[i] for i in range(len(x)-1)]  
    avgdt = np.average(diffs)

    #make time start from 0    
    x_from0 = modules.phase_start_correct(x)

    periods,mag,ph,fr,fi,amp_corr = modules.take_lomb(x_from0,y,ofac,avgdt)

    #get mean of values
    mean_array = np.average(y)

    #correct magnitude and phase for spectral leakage 
    zoomfact = 1000
    half_annual_mag,half_annual_phase = modules.periodic_interp(fr,fi,zoomfact,periods,365.25/2.,len(y),amp_corr)
    annual_mag,annual_phase = modules.periodic_interp(fr,fi,zoomfact,periods,365.25,len(y),amp_corr)

    #correct for phase shift as data starts in Oct 2004 
    n_off = 273.25
    
    if n_off > 365.25/2:
        n_off = n_off-(365.25/2)
    offset = ((np.pi*2)/(365.25/2))*n_off
    half_annual_phase = half_annual_phase + offset
    if half_annual_phase > np.pi:
        half_annual_phase = -np.pi + (half_annual_phase - np.pi)

    n_off = 273.25
    offset = ((np.pi*2)/(365.25))*n_off
    annual_phase = annual_phase + offset
    if annual_phase > np.pi:
        annual_phase = -np.pi + (annual_phase - np.pi)

    #convert phase to time
    half_annual_phase = modules.convert_phase_units_actual_single(half_annual_phase,6)
    annual_phase = modules.convert_phase_units_actual_single(annual_phase,12)
    
    #np.save('mags_phases/mag_spectrums/%i'%(grid_count),mag)
    #np.save('mags_phases/phase_spectrums/%i'%(grid_count),ph)
    #np.save('mags_phases/periods',periods)
    return (half_annual_mag,half_annual_phase,annual_mag,annual_phase,mean_array)
示例#5
0
model_var_60 = model_var[inds_60]
model_var_40 = model_var[inds_40]
model_var_20 = model_var[inds_20]
model_var_5 = model_var[inds_5]
model_var_1 = model_var[inds_1]
model_var_yg = model_var[year_inds]
model_var_2yg = model_var[year_inds_b]

previous = -1
for i in inds_99:
    if i - previous == 2:
        print 'yes', i - 1
    previous += 1

#model lomb
model_periods, model_mag = modules.take_lomb(model_time, model_var, ofac)
model_periods_99_9, model_mag_99_9 = modules.take_lomb(model_time_99_9,
                                                       model_var_99_9, ofac)
model_periods_99, model_mag_99 = modules.take_lomb(model_time_99, model_var_99,
                                                   ofac)
model_periods_95, model_mag_95 = modules.take_lomb(model_time_95, model_var_95,
                                                   ofac)
model_periods_90, model_mag_90 = modules.take_lomb(model_time_90, model_var_90,
                                                   ofac)
model_periods_80, model_mag_80 = modules.take_lomb(model_time_80, model_var_80,
                                                   ofac)
model_periods_60, model_mag_60 = modules.take_lomb(model_time_60, model_var_60,
                                                   ofac)
model_periods_40, model_mag_40 = modules.take_lomb(model_time_40, model_var_40,
                                                   ofac)
model_periods_20, model_mag_20 = modules.take_lomb(model_time_20, model_var_20,
示例#6
0
#get model gridbox for obs site
gridbox_n = modules.obs_model_gridbox(lat_e,lon_e,obs_lat,obs_lon)

model_var = model_data[gridbox_n::gridbox_count]
model_var = model_var*1e9

#----------------------------------------

#align obs and model periods
#obs_time,model_time,obs_var,model_var = modules.align_periods(obs_time,model_time,obs_var,model_var)

#take lomb for obs and model
ofac = raw_input('\nChoose oversampling factor. (Typically 4.)\n')

#obs lomb
obs_periods,obs_mag,obs_ph,obs_sig,obs_i_freq = modules.take_lomb(obs_time,obs_var,ofac)

obs_noise_count = [0]*len(obs_mag)

for i in range(10):
    #randomly mix arrays 
    obs_rand_i = random.sample(range(len(obs_var)), len(obs_var))
    model_rand_i = random.sample(range(len(model_var)), len(model_var))

    obs_var_rand = obs_var[obs_rand_i]
    model_var_rand = model_var[model_rand_i]

    #obs random lomb
    obs_periods_rand,obs_mag_rand,obs_ph_rand,obs_sig_rand,obs_i_freq_rand = modules.take_lomb(obs_time,obs_var_rand,ofac)
    for i in range(len(obs_mag)):
        if obs_mag_rand[i] > obs_mag[i]:
示例#7
0
def main_arg(x,y,grid_count):  
    #set key parameters
    #mctest = [True/False] Calc. MC-based false-alarm levels (Default False)
    #nsim = Number of simulations
    #n50 = number of WOSA segments (50 % overlap)

    mctest = True
    nsim = 50
    n50 = 3
    ofac = 4

    print x
    print y
    #synthetic data
    #pi2 = np.pi*2
    #x = np.arange(0,1001,1)
    #y = 50 + (10*(np.sin((pi2*x/10)-(0))))

    var_mean = np.mean(y)
    #subtract mean from data
    y = y - var_mean

    #average dt of entire time series
    diffs = [x[i+1]-x[i] for i in range(len(x)-1)]  
    avgdt = np.average(diffs)

    #take Lomb-Scargle
    var_mean = np.mean(y)
    #periods,freqs,mag,full_n,fr,fi,ave_seg_len = redfit.welch_lomb(x,y,ofac,n50,avgdt,detrend=False)
    periods,mag,ph,sig,i_freq,fr,fi = modules.take_lomb(x,y,ofac,avgdt)
    freqs = 1./periods

    #find indices of strong periodicities					    
    closest_annual = min(range(len(periods)), key=lambda i: abs(periods[i]-365))
    closest_hannual = min(range(len(periods)), key=lambda i: abs(periods[i]-365./2))
    closest_third = min(range(len(periods)), key=lambda i: abs(periods[i]-365./3))
    closest_quarter = min(range(len(periods)), key=lambda i: abs(periods[i]-365./4))
    closest_daily = min(range(len(periods)), key=lambda i: abs(periods[i]-1))
	
    #remove significant periods

    annual_test = (periods >= 350) & (periods <= 380)
    hannual_test = (periods >= 180) & (periods <= 185)
    third_test = (periods >= 120) & (periods <= 123)
    quarter_test = (periods >= 90) & (periods <= 92)

    corr_mag = np.copy(mag)
    corr_fr = np.copy(fr)
    corr_fi = np.copy(fi)

    set_annual_i = (np.argmax(annual_test==True))-1
    set_hannual_i = (np.argmax(hannual_test==True))-1
    set_third_i = (np.argmax(third_test==True))-1
    set_quarter_i = (np.argmax(quarter_test==True))-1

    set_annual_i = np.arange(set_annual_i-len(corr_mag[annual_test]),set_annual_i)
    set_hannual_i = np.arange(set_hannual_i-len(corr_mag[hannual_test]),set_hannual_i)
    set_third_i = np.arange(set_third_i-len(corr_mag[third_test]),set_third_i)
    set_quarter_i = np.arange(set_quarter_i-len(corr_mag[quarter_test]),set_quarter_i)

    annual_mag_set_val = corr_mag[set_annual_i]
    hannual_mag_set_val = corr_mag[set_hannual_i]
    third_mag_set_val = corr_mag[set_third_i]
    quarter_mag_set_val = corr_mag[set_quarter_i]

    annual_fr= corr_fr[set_annual_i]
    annual_fi= corr_fi[set_annual_i]
    hannual_fr= corr_fr[set_hannual_i]
    hannual_fi= corr_fi[set_hannual_i]
    third_fr= corr_fr[set_third_i]
    third_fi= corr_fi[set_third_i]
    quarter_fr= corr_fr[set_quarter_i]
    quarter_fi= corr_fi[set_quarter_i]

    corr_mag[annual_test] = annual_mag_set_val
    corr_fr[annual_test] = annual_fr
    corr_fi[annual_test] = annual_fi

    corr_mag[hannual_test] = hannual_mag_set_val
    corr_fr[hannual_test] = hannual_fr
    corr_fi[hannual_test] = hannual_fr

    corr_mag[third_test] = third_mag_set_val
    corr_fr[third_test] = third_fr
    corr_fi[third_test] = third_fi

    corr_mag[quarter_test] = quarter_mag_set_val
    corr_fr[quarter_test] = quarter_fr
    corr_fi[quarter_test] = quarter_fi

#------------------------------------------------------
    #Section uses ifft to generate time series of detrended, key period treated data
    # make complex Fourier spectrum which corresponds to the Lomb-Scargle periodogram:
    F = [0]*((len(corr_fr)*2)+1)
    #set first real value to average 
    F[0] = complex(var_mean,0)

    #Get reverse real and imaginary values
    rev_fr=corr_fr[::-1] 
    rev_fi=corr_fi[::-1]

    #Fill Fourier Spectrum real and imaginary values
    for i in range(len(corr_fr)):
        F[i+1] = complex(corr_fr[i],corr_fi[i])
    for i in range(len(corr_fr),len(corr_fr)*2):
        F[i+1] = complex(rev_fr[i-len(corr_fr)],-rev_fi[i-len(corr_fr)])
    F = np.array(F)

    #Take ifft and just take real values
    output_ts = numpy.fft.ifft(F)
    output_ts = output_ts.astype('float64')

    time = []

    #generate time points
    #	for i in range(len(F)):
    #    time= np.append(time,x[0] +(i/(freqs[1]-freqs[0])/len(F)))

    time = x

    time = time[-len(y):]
    output_ts = output_ts[-len(y):]

    #gap = int((len(output_ts) - ave_seg_len)/2)
    #output_ts = output_ts[
    output_ts = output_ts/np.hanning(len(output_ts))
    time = time[350:-350]
    output_ts = output_ts[350:-350]
    #time = time[gap:-gap]
    #output_ts = output_ts[gap:-gap]
    output_ts = output_ts+var_mean

    #save out modified time series
    np.save('gfdl_sig_all_peaks/modified_GFDL_ts/%i'%(grid_count),output_ts)
    
    #------------------------------------------------------
    #Section uses ifft to generate time series of detrended, key period treated data
    # make complex Fourier spectrum which corresponds to the Lomb-Scargle periodogram:
    F = [0]*((len(fr)*2)+1)
    #set first real value to average 
    F[0] = complex(var_mean,0)

    #Get reverse real and imaginary values
    rev_fr=fr[::-1] 
    rev_fi=fi[::-1]

    #Fill Fourier Spectrum real and imaginary values
    for i in range(len(fr)):
        F[i+1] = complex(fr[i],fi[i])
    for i in range(len(fr),len(fr)*2):
        F[i+1] = complex(rev_fr[i-len(fr)],-rev_fi[i-len(fr)])
    F = np.array(F)

    #Take ifft and just take real values
    other_output_ts = numpy.fft.ifft(F)
    other_output_ts = other_output_ts.astype('float64')

    other_time = []

    #generate time points
    #	for i in range(len(F)):
    #    time= np.append(time,x[0] +(i/(freqs[1]-freqs[0])/len(F)))

    other_time = x

    other_time = other_time[-len(y):]
    other_output_ts = other_output_ts[-len(y):]

    #gap = int((len(output_ts) - ave_seg_len)/2)
    #output_ts = output_ts[
    other_output_ts = other_output_ts/np.hanning(len(other_output_ts))
    other_time = other_time[350:-350]
    other_output_ts = other_output_ts[350:-350]
    #time = time[gap:-gap]
    #output_ts = output_ts[gap:-gap]
    other_output_ts = other_output_ts+var_mean
    model_periods,model_freq,model_mag,full_n,fr,fi,ave_seg_len = redfit.welch_lomb(other_time,other_output_ts,ofac,n50,avgdt,detrend=True)

#-------------------------------------------------------
    alt_periods,alt_mag,red_periods,red_mag,gredth,fac80,fac85,fac90,fac95,fac99,fac99_9,faccrit,tau,corr = redfit.redcore(nsim,n50,mctest,time,output_ts)
    
    #correct unmodified spectrum
    model_mag = model_mag/corr
    if tau == 'invalid':
        print 'Tau Invalid.'
        np.save('gfdl_sig_all_peaks/invalid_gridboxes/%i'%(grid_count),output_ts)
        mag_80,mag_85,mag_90,mag_95,mag_99,mag_99_9,periods_80,periods_85,periods_90,periods_95,periods_99,periods_99_9 = [],[],[],[],[],[],[],[],[],[],[],[]
        return mag_80,mag_85,mag_90,mag_95,mag_99,mag_99_9,periods_80,periods_85,periods_90,periods_95,periods_99,periods_99_9
    
    #save out output spectrums
    np.save('gfdl_sig_all_peaks/modified_GFDL_spectra/%i'%(grid_count),model_mag)
    np.save('gfdl_sig_all_peaks/periods',model_periods)

    #fig=plt.figure(figsize=(16,8))
    #fig.patch.set_facecolor('white')
    #ax = fig.add_subplot(1,1,1)

    #def form2(x, pos):
    #	""" This function returns a string with 3 decimal places, given the input x"""
    #	return '%.2f' % x	

    #def form5(x, pos):
    #	""" This function returns a string with 3 decimal places, given the input x"""
    #	return '%.5f' % x

    #ax.grid(True)
    #plt.loglog(model_periods,model_mag,color='black', label = 'Obs. Spectrum')
    #plt.loglog(red_periods,red_mag,color='red', label = 'Red Noise')
    #plt.loglog(red_periods,gredth,color='red', label = 'Theoretical Red Noise Spectrum')
    #plt.loglog(red_periods,gredth*fac80,color='orange', label = '80% Significance')
    #plt.loglog(red_periods,gredth*fac85,color='blue',label = '85% Significance')
    #plt.loglog(red_periods,gredth*fac90,color='purple', label = '90% Significance')
    #plt.loglog(red_periods,gredth*fac95,color='orange', label = '95% Significance')
    #plt.loglog(red_periods,gredth*fac99,color='blue',label = '99% Significance')
    #plt.loglog(red_periods,gredth*fac99_9,color='purple', label = '99.9% Significance')
    #leg=ax.legend(loc=0, prop={'size':15})
    #leg.get_frame().set_alpha(0.4)

    #xformatter = FuncFormatter(form2)
    #yformatter = FuncFormatter(form2)
    #ax.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
    #ax.yaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
    #plt.gca().xaxis.set_major_formatter(FuncFormatter(xformatter))
    #plt.gca().yaxis.set_major_formatter(FuncFormatter(yformatter))

    #plt.show()

#------------------------
    #Determine if spectal values are significant at various significance levels

    test_80 = model_mag >= gredth*fac80
    test_85 = model_mag >= gredth*fac85
    test_90 = model_mag >= gredth*fac90
    test_95 = model_mag >= gredth*fac95
    test_99 = model_mag >= gredth*fac99
    test_99_9 = model_mag >= gredth*fac99_9

    periods_80 = model_periods[test_80]
    periods_85 = model_periods[test_85]
    periods_90 = model_periods[test_90]
    periods_95 = model_periods[test_95]
    periods_99 = model_periods[test_99]
    periods_99_9 = model_periods[test_99_9]

    mag_80 = model_mag[test_80]
    mag_85 = model_mag[test_85]
    mag_90 = model_mag[test_90]
    mag_95 = model_mag[test_95]
    mag_99 = model_mag[test_99]
    mag_99_9 = model_mag[test_99_9]
        
    grid_count+=1
    
    return mag_80,mag_85,mag_90,mag_95,mag_99,mag_99_9,periods_80,periods_85,periods_90,periods_95,periods_99,periods_99_9
示例#8
0
     fft2_diff_p2 = []
     
     fft3_diff_a1 = []
     fft3_diff_a2 = []
     fft3_diff_p1 = []
     fft3_diff_p2 = []
     
     fft4_diff_a1 = []
     fft4_diff_a2 = []
     fft4_diff_p1 = []
     fft4_diff_p2 = []
 
     #Lomb OUT OF BOX
     #-----------------------------------------------
     ofac = 1
     periods,mag,ph,fr,fi,amp_corr = modules.take_lomb(b,vals,ofac,hourly_step,w=False)
     
     closest_period_1 = min(range(len(periods)), key=lambda i: abs(periods[i]-p1))
     closest_period_2 = min(range(len(periods)), key=lambda i: abs(periods[i]-p2))
     mag1 = mag[closest_period_1]
     phase1 = ph[closest_period_1]
     mag2 = mag[closest_period_2]
     phase2 = ph[closest_period_2]
     
     diff_a1 = (mag1/amp1)*100.
     diff_a2 = (mag2/amp2)*100.
     diff_p1 = phase_1 - phase1
     if diff_p1 > np.pi:
         diff_p1 = np.abs(-np.pi + (diff_p1 - np.pi))
     elif diff_p1 < -np.pi:
         diff_p1 = np.abs(np.pi - (np.abs(diff_p1) - np.pi)) 
示例#9
0
lat_n, lon_n = modules.obs_model_gridbox(lat_e, lon_e, 1., 114.)

y = model_var[:, lat_n, lon_n]

y = y * 1e9

#set up plot
fig = plt.figure(figsize=(23, 12.3))
fig.patch.set_facecolor('white')
ax = fig.add_subplot(1, 1, 1)

x = modules.date_process(model_date, model_time, 2005)

ofac = 4

model_periods, model_mag, model_ph, model_fr, model_fi, amp_corr = modules.take_lomb(
    x, y, ofac, 1. / 24)


def form2(x, pos):
    """ This function returns a string with 3 decimal places, given the input x"""
    return '%.2f' % x


def form5(x, pos):
    """ This function returns a string with 3 decimal places, given the input x"""
    return '%.6f' % x


ax.loglog(model_periods, model_mag, color='red', markersize=20)
plt.grid(True)
xformatter = FuncFormatter(form2)
示例#10
0
print len(valids)
print len(model_time)
model_time_gap = model_time[valids]
model_var_gap = model_var[valids]

#----------------------------------------

#align obs and model periods
obs_time, model_time, obs_var, model_var = modules.align_periods(
    obs_time, model_time, obs_var, model_var)

#take lomb for obs and model
ofac = raw_input('\nChoose oversampling factor. (Typically 4.)\n')

#obs lomb
obs_periods, obs_mag, obs_ph, obs_sig, obs_i_freq = modules.take_lomb(
    obs_time, obs_var, ofac)

#model lomb
model_periods, model_mag, model_ph, model_sig, model_i_freq = modules.take_lomb(
    model_time, model_var, ofac)

#calculate high freq. noise due to gaps - apply obs. gaps to model array
model_periods_gap, model_mag_gap, model_ph_gap, model_sig_gap, model_i_freq_gap = modules.take_lomb(
    model_time_gap, model_var_gap, ofac)

#calculate lomb with instrumental error for O3 implied on model sims which are gapped
err_model_var = []
for i in range(len(model_var_gap)):
    #value of 0.0166 uncertainty for each point determined by standard error of (1 ppbv / sqrt(60*60))= 1/60. = 0.0166
    err_model_var = np.append(
        err_model_var, model_var_gap[i] + random.normalvariate(0, 0.0166))
示例#11
0
    if n_points == 100:
        b = orig_b[:]
        vals = orig_vals[:]
    else:
        frac_points = random.sample(xrange(0, len(orig_vals)), n_points)
        frac_points = np.sort(frac_points)
        all_frac_points.append(n)
        b = orig_b[frac_points]
        vals = orig_vals[frac_points]

    #Lomb OUT OF BOX
    #-----------------------------------------------
    ofac = 1
    periods, mag, ph, fr, fi, amp_corr = modules.take_lomb(b,
                                                           vals,
                                                           ofac,
                                                           hourly_step,
                                                           w=False)

    closest_period_1 = min(range(len(periods)),
                           key=lambda i: abs(periods[i] - p1))
    closest_period_2 = min(range(len(periods)),
                           key=lambda i: abs(periods[i] - p2))
    daily_mag1 = mag[closest_period_1]
    daily_phase1 = ph[closest_period_1]
    daily_mag2 = mag[closest_period_2]
    daily_phase2 = ph[closest_period_2]

    print '\nLOMB OUT OF BOX'
    print 'Est. Amplitude 1 = ', daily_mag1
    print 'Est. Phase 1 = ', daily_phase1
示例#12
0
    datetime.datetime(year=year_val[i],
                      month=month_val[i],
                      day=day_val[i],
                      hour=hour_val[i],
                      minute=minute_val[i]) for i in range(len(year_val))
]
full_obs_datetimes = np.array(full_obs_datetimes)
obs_datetimes = full_obs_datetimes[valids]

obs_ave = np.mean(obs_var)

#make time start from 0
obs_times_from0 = modules.phase_start_correct(obs_times)

#remove invalid data
periods, mag, ph, fr, fi, amp_corr = modules.take_lomb(obs_times_from0,
                                                       obs_var, 4, 1. / 24)

zoomfact = 1000

daily_amp, daily_phase = modules.periodic_interp(fr, fi, zoomfact, periods, 1.,
                                                 len(obs_var), amp_corr)
ha_amp, ha_phase = modules.periodic_interp(fr, fi, zoomfact, periods,
                                           365.25 / 2., len(obs_var), amp_corr)
annual_amp, annual_phase = modules.periodic_interp(fr, fi,
                                                   zoomfact, periods, 365.25,
                                                   len(obs_var), amp_corr)

#correct for phase shift from sites where raw times do not start from 0
daily_phase = modules.phase_start_point_correct(1., daily_phase, obs_times)
ha_phase = modules.phase_start_point_correct(365.25 / 2., ha_phase, obs_times)
annual_phase = modules.phase_start_point_correct(365.25, annual_phase,
示例#13
0
#windowing?
wind_set = raw_input('Windowing? Y or N?\n')
if wind_set == 'Y':
    wind_set = True
if wind_set == 'N':
    wind_set = False

#take lomb for obs and model
ofac = int(raw_input('\nChoose oversampling factor. (Typically 4.)\n'))

periodic_periods = [1./10.,1./9.,1./8.,1./7.,1./6.,1./5.,1./4.,1./3.,1./2.,1.,365.25/4.,365.25/3.,365.25/2.,365.25]

samp_step = 1./24

#obs lomb
obs_periods,obs_mag,obs_ph,obs_fr,obs_fi,amp_corr = modules.take_lomb(obs_time,obs_var,ofac,samp_step,w=wind_set,kp=periodic_periods)

#model lomb
model_periods,model_mag,model_ph,model_fr,model_fi,amp_corr = modules.take_lomb(model_time,model_var,ofac,samp_step,w=wind_set,kp=periodic_periods)

obs_time = np.array(obs_time)
obs_var = np.array(obs_var)

#set plotting area & background to white
fig=plt.figure(figsize=(20,12))
fig.patch.set_facecolor('white')
ax = fig.add_subplot(1,1,1)

plt.loglog(obs_periods,obs_mag, color='black',alpha = 1, label = 'Observations')
plt.loglog(model_periods,model_mag, color='red', alpha= 0.6, label = '%s %s %s %s %s'%(model_chose,version,hres,met,timeres))
示例#14
0
#gap data
remove_ints = []
for x in range(500):
    remove_ints.append(random.randint(0, len(vals)))
    remove_ints.append(x)

#print remove_ints
vals = np.delete(vals, remove_ints)
b = np.delete(b, remove_ints)

print '%s percent of dataset complete' % ((100. / total_len) * len(vals))

NOUT = 0.5 * 4 * 1 * len(vals)
NOUT = int(NOUT)

periods, mag, ph, fr, fi, amp_corr = modules.take_lomb(b, vals, 4, hourly_step)

closest_period_index = min(range(len(periods)),
                           key=lambda i: abs(periods[i] - 1.1))

print 'closest period = ', periods[closest_period_index]
period_closest = periods[closest_period_index]

#peak_raw = fft_raw[closest_period_index-10:closest_period_index+11]

peak_fr = fr[closest_period_index - 5:closest_period_index + 6]
peak_fi = fi[closest_period_index - 5:closest_period_index + 6]
peak_periods = periods[closest_period_index - 5:closest_period_index + 6]
peak_mag = mag[closest_period_index - 5:closest_period_index + 6]
peak_phase = ph[closest_period_index - 5:closest_period_index + 6]
示例#15
0
#----------------------------------------
model_time = model_time[valids]
model_var = model_var[valids]


#take lomb for obs and model
ofac = raw_input('\nChoose oversampling factor. (Typically 4.)\n')

#shift times to start from 0
obs_time = np.array(obs_time)
model_time = np.array(model_time)
obs_time_from0 = modules.phase_start_correct(obs_time)
model_time_from0 = modules.phase_start_correct(model_time)

#obs lomb
obs_periods,obs_mag,obs_ph,obs_fr,obs_fi,obs_amp_corr = modules.take_lomb(obs_time_from0,obs_var,ofac,1./24)

#model lomb
model_periods,model_mag,model_ph,model_fr,model_fi,model_amp_corr = modules.take_lomb(model_time_from0,model_var,ofac,1./24)

obs_time = np.array(obs_time)
obs_var = np.array(obs_var)

lon_step_time  = 24./360.

#convert site_lon to 0 to 360 degs
if obs_lon < 0:
    obs_lon = 360-np.abs(obs_lon)
    
#transform from UTC time to solar time 
sun_time = lon_step_time*obs_lon
示例#16
0
    wind_set = True
if wind_set == 'N':
    wind_set = False

#take lomb for obs and model
ofac = int(raw_input('\nChoose oversampling factor. (Typically 4.)\n'))

periodic_periods = [
    1. / 10., 1. / 9., 1. / 8., 1. / 7., 1. / 6., 1. / 5., 1. / 4., 1. / 3.,
    1. / 2., 1., 365.25 / 4., 365.25 / 3., 365.25 / 2., 365.25
]

samp_step = 1. / 24

#obs lomb
obs_periods, obs_mag, obs_ph, obs_fr, obs_fi, amp_corr = modules.take_lomb(
    obs_time, obs_var, ofac, samp_step, w=wind_set, kp=periodic_periods)

#model lomb
model_periods, model_mag, model_ph, model_fr, model_fi, amp_corr = modules.take_lomb(
    model_time, model_var, ofac, samp_step, w=wind_set, kp=periodic_periods)

obs_time = np.array(obs_time)
obs_var = np.array(obs_var)

#set plotting area & background to white
fig = plt.figure(figsize=(20, 12))
fig.patch.set_facecolor('white')
ax = fig.add_subplot(1, 1, 1)

plt.loglog(obs_periods, obs_mag, color='black', alpha=1, label='Observations')
plt.loglog(model_periods,
示例#17
0
    obs_time,model_time,obs_var,model_var = modules.align_periods(obs_time,model_time,obs_var,model_var)
else:
    if model_version == '4x5':
        valids = valids[24:]
        model_time = model_time[valids]
        model_var = model_var[valids]
    else:
        model_time = model_time[valids]
        model_var = model_var[valids]


#take lomb for obs and model
ofac = raw_input('\nChoose oversampling factor. (Typically 4.)\n')

#obs lomb
obs_periods,obs_mag,obs_ph,obs_fr,obs_fi = modules.take_lomb(obs_time,obs_var,ofac,1./24)

#model lomb
model_periods,model_mag,model_ph,model_fr,model_fi = modules.take_lomb(model_time,model_var,ofac,1./24)

obs_time = np.array(obs_time)
obs_var = np.array(obs_var)

#set plotting area & background to white
fig=plt.figure(figsize=(20,12))
fig.patch.set_facecolor('white')
ax = fig.add_subplot(1,1,1)

obs_periods,obs_mag, obs_breakpoint = modules.find_breakpoint(obs_periods,obs_mag)
model_periods,model_mag, model_breakpoint = modules.find_breakpoint(model_periods,model_mag)