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)
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)
Example #4
0
def run_LSP(model_data, x):

    print obs_refs[x]

    vals = model_data

    #check obs vals are valid
    valid = vals >= 0
    vals = vals[valid]
    model_time_val = model_time[valid]
    model_date_val = model_date[valid]

    full_times = modules.date_process(model_date, model_time, start_year)
    if timeres == 'M':
        full_times_year = full_times[:12]
    else:
        full_times_year = full_times[:8766]
    full_times_day = full_times[:24]

    valid_times = modules.date_process(model_date_val, model_time_val,
                                       start_year)

    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

    #make time start from 0
    valid_times_from0 = modules.phase_start_correct(valid_times)

    periodic_periods = [
        1. / 4., 1. / 3., 1. / 2., 1., 365.25 / 4., 365.25 / 3., 365.25 / 2.,
        365.25
    ]
    periods, mag, ph, fr, fi = modules.take_lomb_spec(
        valid_times_from0, vals, w=True, key_periods=periodic_periods)

    #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(periodic_periods, ph,
                                               valid_times)

    key_diurnal_periods = [1. / 4., 1. / 3., 1. / 2., 1.]
    key_seasonal_periods = [365.25 / 4., 365.25 / 3., 365.25 / 2., 365.25]

    diurnal_mags = mag[:4]
    seasonal_mags = mag[4:]
    seasonal_phs = ph[4:]

    #get individual mags and phases
    daily_h3_mag = mag[0]
    daily_h2_mag = mag[1]
    daily_h1_mag = mag[2]
    orig_daily_mag = mag[3]
    daily_h3_ph = ph[0]
    daily_h2_ph = ph[1]
    daily_h1_ph = ph[2]
    orig_daily_ph = ph[3]

    seasonal_h3_mag = mag[4]
    seasonal_h2_mag = mag[5]
    seasonal_h1_mag = mag[6]
    annual_mag = mag[7]
    seasonal_h3_ph = ph[4]
    seasonal_h2_ph = ph[5]
    seasonal_h1_ph = ph[6]
    annual_ph = ph[7]

    #convert sub diurnal phases from UTC to solar time
    daily_h3_ph = modules.solar_time_phase_corrector(daily_h3_ph, 6, time_diff)
    daily_h2_ph = modules.solar_time_phase_corrector(daily_h2_ph, 24. / 3.,
                                                     time_diff)
    daily_h1_ph = modules.solar_time_phase_corrector(daily_h1_ph, 12,
                                                     time_diff)
    orig_daily_ph = modules.solar_time_phase_corrector(orig_daily_ph, 24,
                                                       time_diff)
    diurnal_phs = [daily_h3_ph, daily_h2_ph, daily_h1_ph, orig_daily_ph]

    #convolve annual cycle and harmonics to seasonal waveform for 1 year
    seasonal_mag, seasonal_min_ph, seasonal_max_ph, seasonal_waveform, seasonal_ff = modules.period_convolution(
        key_seasonal_periods, full_times_year, seasonal_mags, seasonal_phs,
        mean_array)

    #convolve diurnal cycle and harmonics to diurnal waveform for 1 day
    diurnal_mag, diurnal_min_ph, diurnal_max_ph, diurnal_waveform, diurnal_ff = modules.period_convolution(
        key_diurnal_periods, full_times_day, diurnal_mags, diurnal_phs,
        mean_array)

    #convolve all
    full_mag, full_min_ph, full_max_ph, full_waveform, full_ff = modules.period_convolution(
        periodic_periods, full_times, mag, ph, mean_array)

    #convert phase to time
    daily_h3_ph = modules.convert_phase_units_actual_single(daily_h3_ph, 6.)
    daily_h2_ph = modules.convert_phase_units_actual_single(
        daily_h2_ph, 24. / 3.)
    daily_h1_ph = modules.convert_phase_units_actual_single(daily_h1_ph, 12.)
    orig_daily_ph = modules.convert_phase_units_actual_single(
        orig_daily_ph, 24.)
    diurnal_min_ph = modules.convert_phase_units_actual_single(
        diurnal_min_ph, 24.)
    diurnal_max_ph = modules.convert_phase_units_actual_single(
        diurnal_max_ph, 24.)
    seasonal_h3_ph = modules.convert_phase_units_actual_single(
        seasonal_h3_ph, 3.)
    seasonal_h2_ph = modules.convert_phase_units_actual_single(
        seasonal_h2_ph, 4.)
    seasonal_h1_ph = modules.convert_phase_units_actual_single(
        seasonal_h1_ph, 6.)
    annual_ph = modules.convert_phase_units_actual_single(annual_ph, 12.)
    seasonal_min_ph = modules.convert_phase_units_actual_single(
        seasonal_min_ph, 12.)
    seasonal_max_ph = modules.convert_phase_units_actual_single(
        seasonal_max_ph, 12.)

    return (x, daily_h3_mag, daily_h3_ph, daily_h2_mag, daily_h2_ph,
            daily_h1_mag, daily_h1_ph, orig_daily_mag, orig_daily_ph,
            diurnal_mag, diurnal_min_ph, diurnal_max_ph, seasonal_h3_mag,
            seasonal_h3_ph, seasonal_h2_mag, seasonal_h2_ph, seasonal_h1_mag,
            seasonal_h1_ph, annual_mag, annual_ph, seasonal_mag,
            seasonal_min_ph, seasonal_max_ph, mean_array, diurnal_waveform,
            seasonal_waveform, full_waveform)
Example #5
0
def run_LSP(obs_time,site_lon,x):

    data_valid = True

    #print 'lev %s'%(x)
    
    full_times_year = np.arange(0,365,1.)
    
    #check obs vals are valid
    valid = vals >= 0
    vals = vals[valid]
    valid_times = obs_time[valid]

    #if length of vals is zero then class as invalid immediately
    if len(vals) == 0:
        data_valid = False
    else:
        #test if there if data is valid to process at each height for each site
        #data should not have gaps > 1 year or 
        time_gaps = np.diff(valid_times)                
        inv_count = 0
        max_count = round(n_years/2.)
        for i in time_gaps:
            if i > 90:
                inv_count+=1
                if inv_count >= max_count:
                    data_valid = False
                    print 'Persisent Data gap > 3 months'
                    break
            if i > 365:
                    data_valid = False
                    print 'Data gap > 1 Year'
                    break

    if data_valid == True:
    
        #convert site_lon to 0 to 360 degs
        if site_lon < 0:
            site_lon = 360-np.abs(site_lon)

        #make time start from 0    
        valid_times_from0 = modules.phase_start_correct(valid_times)

        periodic_periods = [365.25/4.,365.25/3.,365.25/2.,365.25]
        periods,mag,ph,fr,fi = modules.take_lomb_spec(valid_times_from0,vals,w=True,key_periods=periodic_periods)

        #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(periodic_periods,ph,valid_times)

        key_seasonal_periods = [365.25/4.,365.25/3.,365.25/2.,365.25]

        seasonal_mags = mag[:]
        seasonal_phs = ph[:]

        seasonal_h3_mag = mag[0]
        seasonal_h2_mag = mag[1]
        seasonal_h1_mag = mag[2]
        annual_mag = mag[3]
        seasonal_h3_ph = ph[0]
        seasonal_h2_ph = ph[1]
        seasonal_h1_ph = ph[2]
        annual_ph = ph[3]

        #convolve annual cycle and harmonics to seasonal waveform for 1 year
        seasonal_mag,seasonal_min_ph,seasonal_max_ph,seasonal_waveform,seasonal_ff = modules.period_convolution(key_seasonal_periods,full_times_year,seasonal_mags,seasonal_phs,mean_array)
        
        #convert phase to time
        seasonal_h3_ph = modules.convert_phase_units_actual_single(seasonal_h3_ph,3.)
        seasonal_h2_ph = modules.convert_phase_units_actual_single(seasonal_h2_ph,4.)
        seasonal_h1_ph = modules.convert_phase_units_actual_single(seasonal_h1_ph,6.)
        annual_ph = modules.convert_phase_units_actual_single(annual_ph,12.)
        seasonal_min_ph = modules.convert_phase_units_actual_single(seasonal_min_ph,12.)
        seasonal_max_ph = modules.convert_phase_units_actual_single(seasonal_max_ph,12.)

    else:
        seasonal_h3_mag = -99999
        seasonal_h2_mag = -99999
        seasonal_h1_mag = -99999
        annual_mag = -99999
        seasonal_mag = -99999
        
        seasonal_h3_ph = -99999
        seasonal_h2_ph = -99999
        seasonal_h1_ph = -99999
        annual_ph = -99999
        seasonal_max_ph = -99999
        seasonal_min_ph = -99999

        seasonal_waveform = np.array([-99999]*len(full_times_year))

        mean_array = -99999
    
    return x,seasonal_h3_mag,seasonal_h3_ph,seasonal_h2_mag,seasonal_h2_ph,seasonal_h1_mag,seasonal_h1_ph,annual_mag,annual_ph,seasonal_mag,seasonal_max_ph,seasonal_min_ph,seasonal_waveform,mean_array
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]

    if timeres == 'H':
        full_times_year = obs_ref_time[:8766]
    elif timeres == 'D':
        full_times_year = obs_ref_time[:365]
    elif timeres == 'M':
        full_times_year = obs_ref_time[:12]
    full_times_day = obs_ref_time[:24] 
      
    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

    #make time start from 0    
    valid_times_from0 = modules.phase_start_correct(valid_times)

    print valid_times_from0

    periodic_periods = [1./4.,1./3.,1./2.,1.,365.25/4.,365.25/3.,365.25/2.,365.25]
    periods,mag,ph,fr,fi = modules.take_lomb_spec(valid_times_from0,vals,w=True,key_periods=periodic_periods)
    
    #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(periodic_periods,ph,valid_times)

    key_diurnal_periods = [1./4.,1./3.,1./2.,1.]
    key_seasonal_periods = [365.25/4.,365.25/3.,365.25/2.,365.25]

    diurnal_mags = mag[:4]
    seasonal_mags = mag[4:]
    seasonal_phs = ph[4:]

    #get individual mags and phases
    daily_h3_mag = mag[0]
    daily_h2_mag = mag[1]
    daily_h1_mag = mag[2]
    orig_daily_mag = mag[3]
    daily_h3_ph = ph[0]
    daily_h2_ph = ph[1]
    daily_h1_ph = ph[2]
    orig_daily_ph = ph[3]
    
    seasonal_h3_mag = mag[4]
    seasonal_h2_mag = mag[5]
    seasonal_h1_mag = mag[6]
    annual_mag = mag[7]
    seasonal_h3_ph = ph[4]
    seasonal_h2_ph = ph[5]
    seasonal_h1_ph = ph[6]
    annual_ph = ph[7]

    #convert sub diurnal phases from UTC to solar time
    daily_h3_ph = modules.solar_time_phase_corrector(daily_h3_ph,6,time_diff)
    daily_h2_ph = modules.solar_time_phase_corrector(daily_h2_ph,24./3.,time_diff)
    daily_h1_ph = modules.solar_time_phase_corrector(daily_h1_ph,12,time_diff)
    orig_daily_ph = modules.solar_time_phase_corrector(orig_daily_ph,24,time_diff)
    diurnal_phs = [daily_h3_ph,daily_h2_ph,daily_h1_ph,orig_daily_ph]

    #convolve annual cycle and harmonics to seasonal waveform for 1 year
    seasonal_mag,seasonal_min_ph,seasonal_max_ph,seasonal_waveform,seasonal_ff = modules.period_convolution(key_seasonal_periods,full_times_year,seasonal_mags,seasonal_phs,mean_array)

    #convolve diurnal cycle and harmonics to diurnal waveform for 1 day
    diurnal_mag,diurnal_min_ph,diurnal_max_ph,diurnal_waveform,diurnal_ff = modules.period_convolution(key_diurnal_periods,full_times_day,diurnal_mags,diurnal_phs,mean_array)
    
    #convolve all 
    full_mag,full_min_ph,full_max_ph,full_waveform,full_ff = modules.period_convolution(periodic_periods,obs_ref_time,mag,ph,mean_array)

    #convert phase to time
    daily_h3_ph = modules.convert_phase_units_actual_single(daily_h3_ph,6.)
    daily_h2_ph = modules.convert_phase_units_actual_single(daily_h2_ph,24./3.)
    daily_h1_ph = modules.convert_phase_units_actual_single(daily_h1_ph,12.)
    orig_daily_ph = modules.convert_phase_units_actual_single(orig_daily_ph,24.)
    diurnal_min_ph = modules.convert_phase_units_actual_single(diurnal_min_ph,24.)
    diurnal_max_ph = modules.convert_phase_units_actual_single(diurnal_max_ph,24.)
    seasonal_h3_ph = modules.convert_phase_units_actual_single(seasonal_h3_ph,3.)
    seasonal_h2_ph = modules.convert_phase_units_actual_single(seasonal_h2_ph,4.)
    seasonal_h1_ph = modules.convert_phase_units_actual_single(seasonal_h1_ph,6.)
    annual_ph = modules.convert_phase_units_actual_single(annual_ph,12.)
    seasonal_min_ph = modules.convert_phase_units_actual_single(seasonal_min_ph,12.)
    seasonal_max_ph = modules.convert_phase_units_actual_single(seasonal_max_ph,12.)

    return (x,daily_h3_mag,daily_h3_ph,daily_h2_mag,daily_h2_ph,daily_h1_mag,daily_h1_ph,orig_daily_mag,orig_daily_ph,diurnal_mag,diurnal_min_ph,diurnal_max_ph,seasonal_h3_mag,seasonal_h3_ph,seasonal_h2_mag,seasonal_h2_ph,seasonal_h1_mag,seasonal_h1_ph,annual_mag,annual_ph,seasonal_mag,seasonal_min_ph,seasonal_max_ph,mean_array,diurnal_waveform,seasonal_waveform,full_waveform,diurnal_ff,seasonal_ff,full_ff)