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)