def run_LSP(vals,x): print obs_refs[x] #check obs vals are valid valid = vals >= 0 vals = vals[valid] valid_times = obs_ref_time[valid] #full_times = modules.date_process(obs_dates,obs_times,start_year) full_times_year = obs_ref_time[:8766] full_times_day = obs_ref_time[:24] #make time start from 0 valid_times_from0 = modules.phase_start_correct(valid_times) samp_step = 1./24 f = interpolate.interp1d(valid_times_from0, vals) valid_times_from0 = np.arange(np.min(valid_times_from0),np.max(valid_times_from0),samp_step) vals = f(valid_times_from0) site_lon = obs_lons[x] #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 #take obs lsp ofac = 1 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] #periods,mag,ph,fr,fi,amp_corr = modules.take_lomb(valid_times_from0,vals,ofac,samp_step,w=True,kp=periodic_periods) periods,mag,ph,fr,fi,fft_array,amp_corr = modules.take_fft(valid_times_from0,vals,ofac,samp_step,w=True,kp=periodic_periods) #convert mag to normalised psd psd_mag = mag**2 freq = 1./periods diff = freq[1] - freq[0] psd_mag = psd_mag/diff #get info of weather regimes through model fit. grad1,grad2,bp1,line1_periods,line1_mag,line2_periods,line2_mag,ave1,ave2,med1,med2,sum1,sum2,line1_s,line1_e,line2_s,line2_e = modules.spectra_fit_fixed_piecewise(periods,psd_mag,ofac,3.0,100.0,10.0) #get mean of values mean_array = np.average(vals) #correct all phases for start point (not actually being from 0 - just corrected to be) 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,psd_mag,ph,grad1,grad2,bp1,line1_periods,line1_mag,line2_periods,line2_mag,ave1,ave2,med1,med2,sum1,sum2,line1_s,line1_e,line2_s,line2_e)
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] 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 = 1 samp_step = 1./24 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] #periods,mag,ph,fr,fi,amp_corr = modules.take_lomb(valid_times_from0,vals,ofac,samp_step,w=True,kp=periodic_periods) periods,mag,ph,fr,fi,fft_array,amp_corr = modules.take_fft(valid_times_from0,vals,ofac,samp_step,w=True,kp=periodic_periods) #convert mag to normalised psd psd_mag = mag**2 freq = 1./periods diff = freq[1] - freq[0] psd_mag = psd_mag/diff #get info of weather regimes through model fit. grad1,grad2,bp1,line1_periods,line1_mag,line2_periods,line2_mag,ave1,ave2,med1,med2,sum1,sum2,line1_s,line1_e,line2_s,line2_e = modules.spectra_fit_fixed_piecewise(periods,psd_mag,ofac,3.0,100.0,10.0) #get mean of values mean_array = np.average(vals) #correct all phases for start point (not actually being from 0 - just corrected to be) 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,psd_mag,ph,grad1,grad2,bp1,line1_periods,line1_mag,line2_periods,line2_mag,ave1,ave2,med1,med2,sum1,sum2,line1_s,line1_e,line2_s,line2_e)
lsp5_diff_p2.append(diff_p2) #print '\nLOMB WINDOWED, SPECIFIC FREQUENCIES' #print 'Est. Amplitude 1 = ',mag1 #print 'Est. Phase 1 = ',phase1 #print 'Est. Amplitude 2 = ',mag2 #print 'Est. Phase 2 = ',phase2 #print '------------------------------------------\n' #FFT - OUT OF BOX #---------------------------------------------------------------- ofac=1 periods,mag,ph,fr,fi,fft_array,amp_corr = modules.take_fft(b,vals,hourly_step,ofac,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))
daily_phase1 = ph[closest_period_1] daily_mag2 = mag[closest_period_2] daily_phase2 = ph[closest_period_2] print '\nLOMB WINDOWED, SPECIFIC FREQUENCIES' print 'Est. Amplitude 1 = ', daily_mag1 print 'Est. Phase 1 = ', daily_phase1 print 'Est. Amplitude 2 = ', daily_mag2 print 'Est. Phase 2 = ', daily_phase2 print '------------------------------------------\n' #FFT - OUT OF BOX #---------------------------------------------------------------- ofac = 1 periods, mag, ph, fr, fi, fft_array, amp_corr = modules.take_fft( b, vals, hourly_step, ofac, 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 '\nFFT OUT OF BOX' print 'Est. Amplitude 1 = ', daily_mag1 print 'Est. Phase 1 = ', daily_phase1 print 'Est. Amplitude 2 = ', daily_mag2 print 'Est. Phase 2 = ', daily_phase2
print vals valid = vals > 0 vals = vals[valid] current_time = current_time[valid] print current_time all_hours = np.arange(np.min(current_time),np.max(current_time)+1./48.,1./24.) f = interpolate.interp1d(current_time, vals) vals = f(all_hours) ofac=4 periods,mag,ph,fr,fi,fft_array,amp_corr = modules.take_fft(all_hours,vals,1./24.,ofac,w=False) print len(fft_array) print len(mag) print len(fr) print len(fi) print fr plt.loglog(periods,mag) #remove points n around fifth diurnal peak diurnal_i = min(range(len(periods)), key=lambda i: abs(periods[i]-0.2)) n_points = int((170*ofac)+np.floor(ofac/2.)) rm_points = range(diurnal_i-n_points,(diurnal_i+n_points)+1) mag[rm_points] = 0 fr[rm_points] = 0
#print 'Est. Amplitude 1 = ',daily_mag1 #print 'Est. Phase 1 = ',daily_phase1 #print 'Est. Amplitude 2 = ',daily_mag2 #print 'Est. Phase 2 = ',daily_phase2 #print '------------------------------------------\n' #FFT - windowed #---------------------------------------------------------------- f = interpolate.interp1d(b, vals) new_b = np.arange(np.min(b), np.max(b), hourly_step) interp_vals = f(new_b) periods, mag, ph, fr, fi, fft_array = modules.take_fft(new_b, interp_vals, hourly_step, w=True) 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] fft_diff = percent * np.abs(daily_mag1 - amp1) all_fft_diff.append(fft_diff)