def main(input_file,limits,save=False,plot=False,dofit=False):
    data = import_local_csv(input_file,limits[0],limits[1])
    integrated = sum(data)
    if save:
        np.savetxt('output.dat',integrated, delimiter='\n')
    if plot:
        fig = plt.figure()
        fig.patch.set_facecolor('white')
        plt.title('Spectra')
        plt.xlabel('channels')
        plt.ylabel('counts')
        plt.xlim(limits[0],limits[1])
        x = list(range(limits[0],len(integrated)+limits[0]))
        plt.plot(x,integrated,'b:',label='data')
        plt.yscale('log')
        plt.show()

    if dofit:
        mean,sigma,amp = fitter.single_peak_fit(integrated,limits[0],limits[1],20.0,50,True)
        print('mean = {}, sigma = {}, amp = {}'.format(mean,sigma,amp))
        peak_counts = fitter.get_peak_counts(mean[0],sigma[0],amp[0])
        raw_counts = fitter.get_gross_counts(integrated,0,5000)
        print('peak counts = {}'.format(peak_counts))
        print('gross counts = {}'.format(raw_counts))
def main(input_file,limits,save=False,plot=False,dofit=False):
    data = import_local_csv(input_file,limits[0],limits[1])
    integrated = sum(data)
    if save:
        np.savetxt('output.dat',integrated, delimiter='\n')
    if plot:
        fig = plt.figure()
        fig.patch.set_facecolor('white')
        plt.title('Spectra')
        plt.xlabel('channels')
        plt.ylabel('counts')
        plt.xlim(limits[0],limits[1])
        x = list(range(limits[0],len(integrated)+limits[0]))
        plt.plot(x,integrated,'b:',label='data')
        plt.yscale('log')
        plt.show()

    if dofit:
        mean,sigma,amp = fitter.single_peak_fit(integrated,limits[0],limits[1],20.0,50,True)
        print('mean = {}, sigma = {}, amp = {}'.format(mean,sigma,amp))
        peak_counts = fitter.get_peak_counts(mean[0],sigma[0],amp[0])
        raw_counts = fitter.get_gross_counts(integrated,0,5000)
        print('peak counts = {}'.format(peak_counts))
        print('gross counts = {}'.format(raw_counts))
def main(times, spectra, nhours, stationID=0, wtimes=[], temps=[]):
    #---------------------------------------------------------------------#
    # Get fit results for ndays integrating over nhours for Potassium
    #---------------------------------------------------------------------#
    # single_peak_fit args: channel lims, expo offset, plot flag
    #args = [210,310,100,False]
    #args = [180,280,100,True]
    args = [360, 780, 7.0, 100, False, 'K']
    calibs, calib_err = get_calibrations(spectra, fitter.single_peak_fit, args)
    E_spectra, bin_times = calibrate_spectra(spectra, calibs, times, nhours)

    args = [180, 390, 7.0, 100, False, 'K']
    K_peaks, K_sigmas, K_amps = get_peak_fits(E_spectra, \
                                              fitter.single_peak_fit,args)
    #-------------------------------------------------------------------------#
    # Varify and break apart mean,sigma,amp values and uncertainties
    #-------------------------------------------------------------------------#
    K_ch, K_ch_errs = get_arrays(K_peaks)
    K_sig = [i[0] for i in K_sigmas]
    K_A = [i[0] for i in K_amps]

    K_ch_ave, K_ch_var = get_stats(K_ch)
    K_counts = fitter.get_peak_counts(K_ch, K_sig, K_A)

    K_count = cut_outliers(K_counts)
    K_mean, K_var = get_stats(np.asarray(K_counts))

    for i in range(len(K_ch)):
        if abs(K_ch[i] - K_ch_ave) > 3 * K_ch_var:
            print('Bad K-40 fit: peak channel = {}'.format(K_ch[i]))

    #---------------------------------------------------------------------#
    # Do the same for Bizmuth-214
    #---------------------------------------------------------------------#
    # double_peak_fit args: channel lims, gaus index, expo offset, plot flag
    #args = [50,130,1,1,True]

    if stationID == 0:
        args = [50, 130, 1, 1, False, 'Bi']
        Bi_peaks,Bi_sigmas,Bi_amps = get_peak_fits(E_spectra, \
                                                   fitter.double_peak_fit,args)
    if stationID == 1:
        args = [90, 150, 5.0, 1, False, 'Bi']
        Bi_peaks,Bi_sigmas,Bi_amps = get_peak_fits(E_spectra, \
                                                   fitter.single_peak_fit,args)
    Bi_ch, Bi_ch_errs = get_arrays(Bi_peaks)
    Bi_sig = [i[0] for i in Bi_sigmas]
    Bi_A = [i[0] for i in Bi_amps]
    B_ch_ave, B_ch_var = get_stats(Bi_ch)

    #-------------------------------------------------------------------------#
    # Process channel data using fit results
    #-------------------------------------------------------------------------#
    Bi_counts = fitter.get_peak_counts(Bi_ch, Bi_sig, Bi_A)
    Bi_counts = cut_outliers(Bi_counts)
    Bi_mean, Bi_var = get_stats(np.asarray(Bi_counts))

    print('K-40 <channel> = {} +/- {}'.format(K_ch_ave, K_ch_var))
    print('K-40 <N> = {} +/- {}'.format(K_mean, K_var))

    print('Bi-214 <channel> = {} +/- {}'.format(B_ch_ave, B_ch_var))
    print('Bi-214 <N> = {} +/- {}'.format(Bi_mean, Bi_var))

    #-------------------------------------------------------------------------#
    # Process weather data
    #-------------------------------------------------------------------------#
    # LBL weather station
    #location = 'KCABERKE89'
    #location = 'KCABERKE86'
    #wtimes,temps = get_weather_data(location,nhours,tstart,tstop)
    times_both, counts, temps = merge_data(bin_times, Bi_counts, wtimes, temps)
    #-------------------------------------------------------------------------#
    # Plots of everything we are interested in!
    #-------------------------------------------------------------------------#
    make_plot(bin_times,K_counts,np.sqrt(K_counts), \
              'Time','counts','K-40 counts vs Time','go','g')
    fig_name = '/Users/alihanks/Google Drive/NQUAKE_analysis/D3S/K_counts_{}_5-8.png'.format(
        stationID)
    plt.savefig(fig_name)

    make_plot(times,calibs,calib_err, \
              'Time','keV/channel','keV/channel vs Time','bo','b', \
              2.4,2.6)
    fig_name = '/Users/alihanks/Google Drive/NQUAKE_analysis/D3S/calibs_{}_5-8.png'.format(
        stationID)
    plt.savefig(fig_name)

    make_plot(bin_times,Bi_counts,np.sqrt(Bi_counts), \
             'Time','counts','Bi-214 counts vs Time','go','g')
    fig_name = '/Users/alihanks/Google Drive/NQUAKE_analysis/D3S/Bi_counts_{}_5-8.png'.format(
        stationID)
    plt.savefig(fig_name)

    #make_plot(Ktimes,K_ch,K_ch_errs, \
    #           'Time','1460 center channel','1460 channel vs Time','ro','r')

    #make_plot(times,Bi_ch,Bi_ch_errs, \
    #         'Time','609 center channel','609 channel vs Time','ro','r', \
    #         B_ch_ave-10*B_ch_var,B_ch_ave+10*B_ch_var)

    make_plot(temps,counts,np.sqrt(counts), \
              'Temp (F)','Bi-214 counts','Bi-214 counts vs Temp (F)','ro','r')
    fig_name = '/Users/alihanks/Google Drive/NQUAKE_analysis/D3S/Bi_counts_vs_T_{}_5-8.png'.format(
        stationID)
    plt.savefig(fig_name)

    plt.show()
def main(times,spectra,nhours,stationID=0,wtimes=[],temps=[]):
    #---------------------------------------------------------------------#
    # Get fit results for ndays integrating over nhours for Potassium
    #---------------------------------------------------------------------#
    # single_peak_fit args: channel lims, expo offset, plot flag
    #args = [210,310,100,False]
    #args = [180,280,100,True]
    args = [360,780,7.0,100,False,'K']
    calibs,calib_err = get_calibrations(spectra, fitter.single_peak_fit,args)
    E_spectra, bin_times = calibrate_spectra(spectra,calibs,times,nhours)

    args = [180,390,7.0,100,False,'K']
    K_peaks, K_sigmas, K_amps = get_peak_fits(E_spectra, \
                                              fitter.single_peak_fit,args)
    #-------------------------------------------------------------------------#
    # Varify and break apart mean,sigma,amp values and uncertainties
    #-------------------------------------------------------------------------#
    K_ch, K_ch_errs = get_arrays(K_peaks)
    K_sig = [i[0] for i in K_sigmas]
    K_A = [i[0] for i in K_amps]

    K_ch_ave, K_ch_var = get_stats(K_ch)
    K_counts = fitter.get_peak_counts(K_ch,K_sig,K_A)

    K_count = cut_outliers(K_counts)
    K_mean, K_var = get_stats(np.asarray(K_counts))

    for i in range(len(K_ch)):
        if abs(K_ch[i]-K_ch_ave) > 3*K_ch_var:
            print('Bad K-40 fit: peak channel = {}'.format(K_ch[i]))

    #---------------------------------------------------------------------#
    # Do the same for Bizmuth-214
    #---------------------------------------------------------------------#
    # double_peak_fit args: channel lims, gaus index, expo offset, plot flag
    #args = [50,130,1,1,True]

    if stationID==0:
        args = [50,130,1,1,False,'Bi']
        Bi_peaks,Bi_sigmas,Bi_amps = get_peak_fits(E_spectra, \
                                                   fitter.double_peak_fit,args)
    if stationID==1:
        args = [90,150,5.0,1,False,'Bi']
        Bi_peaks,Bi_sigmas,Bi_amps = get_peak_fits(E_spectra, \
                                                   fitter.single_peak_fit,args)
    Bi_ch, Bi_ch_errs = get_arrays(Bi_peaks)
    Bi_sig = [i[0] for i in Bi_sigmas]
    Bi_A = [i[0] for i in Bi_amps]
    B_ch_ave,B_ch_var = get_stats(Bi_ch)

    #-------------------------------------------------------------------------#
    # Process channel data using fit results
    #-------------------------------------------------------------------------#
    Bi_counts = fitter.get_peak_counts(Bi_ch,Bi_sig,Bi_A)
    Bi_counts = cut_outliers(Bi_counts)
    Bi_mean, Bi_var = get_stats(np.asarray(Bi_counts))

    print('K-40 <channel> = {} +/- {}'.format(K_ch_ave,K_ch_var))
    print('K-40 <N> = {} +/- {}'.format(K_mean,K_var))

    print('Bi-214 <channel> = {} +/- {}'.format(B_ch_ave,B_ch_var))
    print('Bi-214 <N> = {} +/- {}'.format(Bi_mean,Bi_var))

    #-------------------------------------------------------------------------#
    # Process weather data
    #-------------------------------------------------------------------------#
    # LBL weather station
    #location = 'KCABERKE89'
    #location = 'KCABERKE86'
    #wtimes,temps = get_weather_data(location,nhours,tstart,tstop)
    times_both,counts,temps = merge_data(bin_times,Bi_counts,wtimes,temps)
    #-------------------------------------------------------------------------#
    # Plots of everything we are interested in!
    #-------------------------------------------------------------------------#
    make_plot(bin_times,K_counts,np.sqrt(K_counts), \
              'Time','counts','K-40 counts vs Time','go','g')
    fig_name = '/Users/alihanks/Google Drive/NQUAKE_analysis/D3S/K_counts_{}_5-8.png'.format(stationID)
    plt.savefig(fig_name)

    make_plot(times,calibs,calib_err, \
              'Time','keV/channel','keV/channel vs Time','bo','b', \
              2.4,2.6)
    fig_name = '/Users/alihanks/Google Drive/NQUAKE_analysis/D3S/calibs_{}_5-8.png'.format(stationID)
    plt.savefig(fig_name)

    make_plot(bin_times,Bi_counts,np.sqrt(Bi_counts), \
             'Time','counts','Bi-214 counts vs Time','go','g')
    fig_name = '/Users/alihanks/Google Drive/NQUAKE_analysis/D3S/Bi_counts_{}_5-8.png'.format(stationID)
    plt.savefig(fig_name)

    #make_plot(Ktimes,K_ch,K_ch_errs, \
    #           'Time','1460 center channel','1460 channel vs Time','ro','r')

    #make_plot(times,Bi_ch,Bi_ch_errs, \
    #         'Time','609 center channel','609 channel vs Time','ro','r', \
    #         B_ch_ave-10*B_ch_var,B_ch_ave+10*B_ch_var)

    make_plot(temps,counts,np.sqrt(counts), \
              'Temp (F)','Bi-214 counts','Bi-214 counts vs Temp (F)','ro','r')
    fig_name = '/Users/alihanks/Google Drive/NQUAKE_analysis/D3S/Bi_counts_vs_T_{}_5-8.png'.format(stationID)
    plt.savefig(fig_name)

    plt.show()