예제 #1
0
def main(argv):
    #----------------------------------------------------------
    #INPUTS
    #----------------------------------------------------------

    loc = 'par'

    iyear = 2011
    fyear = 2016

    savePDF = False
    pltFile = '/data1/ancillary_data/NCEPdata/' + loc.upper(
    ) + '_TropHeight.pdf'

    if savePDF: pdfsav = PdfPages(pltFile)

    #----------------------------------------------------------
    #                           MAIN
    #----------------------------------------------------------

    TH_ncep = []
    TP_ncep = []
    dt_ncep = []
    doy_ncep = []

    nyears = int(fyear) - int(iyear) + 1

    if loc.lower() == 'ahs':
        NCEPhgtDir = '/data1/projects/ocs/ldr/'
    else:
        NCEPhgtDir = '/data1/projects/ocs/' + loc.lower() + '/'

    if loc.lower() == 'tab':
        loc2 = 'Thule'

    elif loc.lower() == 'mlo':
        loc2 = 'Mauna Loa'

    elif loc.lower() == 'bld':
        loc2 = 'Boulder'

    elif loc.lower() == 'eur':
        loc2 = 'Eureka'

    elif loc.lower() == 'stp':
        loc2 = 'St Petersburg'

    elif loc.lower() == 'jfj':
        loc2 = 'Jungfraujoch'

    elif loc.lower() == 'rkb':
        loc2 = 'Rikubetsu'

    elif loc.lower() == 'tor':
        loc2 = 'Toronto'

    elif loc.lower() == 'tsk':
        loc2 = 'Tsukuba'

    elif loc.lower() == 'wlo':
        loc2 = 'Wollongong'

    elif loc.lower() == 'ldr':
        loc2 = 'Lauder'

    elif loc.lower() == 'ahs':
        loc2 = 'AHTS'

    elif loc.lower() == 'mai':
        loc2 = 'Maido'

    elif loc.lower() == 'std':
        loc2 = 'Stdenis'

    elif loc.lower() == 'zgp':
        loc2 = 'Zugspitze'

    elif loc.lower() == 'kir':
        loc2 = 'Kiruna'

    elif loc.lower() == 'iza':
        loc2 = 'Izana'

    elif loc.lower() == 'par':
        loc2 = 'Paris'

    elif loc.lower() == 'nya':
        loc2 = 'Ny Alesund'

    elif loc.lower() == 'bre':
        loc2 = 'Bremen'

    elif loc.lower() == 'pmb':
        loc2 = 'Paramaribo'

    elif loc.lower() == 'alt':
        loc2 = 'Altzomoni'

    else:
        'Site does not exist!'
        exit()

    for i in range(nyears):

        f2 = open(
            NCEPhgtDir + 'NCEP_trpp/TropHght_' + loc.lower() + '_' +
            str(int(iyear) + i) + '.dat', 'r')

        lines = f2.readlines()
        f2.close()

        c1 = []
        c2 = []
        c3 = []
        dt_temp = []

        count = 0
        for line in lines:
            if count >= 1:
                p = line.split()
                yyyy = int(p[0][0:4])
                mm = int(p[0][4:6])
                dd = int(p[0][6:8])
                c1.append(str(p[0]))
                c2.append(float(p[1]))
                c3.append(float(p[1]))
                dt_temp.append(dt.date(yyyy, mm, dd))
            count += 1

        TH_ncep.extend(np.asarray(c2))
        TP_ncep.extend(np.asarray(c3))
        dt_ncep.extend(np.asarray(dt_temp))
        doy_ncep.extend(mf.toYearFraction(np.asarray(dt_temp)))

    TH_ncep = np.asarray(TH_ncep)
    TP_ncep = np.asarray(TP_ncep)
    dt_ncep = np.asarray(dt_ncep)
    doy_ncep = np.asarray(doy_ncep)

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

    MeasData = NCEPhgtDir + '/Dates_' + loc2 + '.ascii'

    f2 = open(MeasData, 'r')
    lines = f2.readlines()
    f2.close()

    dateMeas = []
    hh = []

    for i, line in enumerate(lines):
        if i >= 5:
            p = line.split()
            yyyy = int(p[0][0:4])
            mm = int(p[0][4:6])
            dd = int(p[0][6:8])

            hh.append(p[1])

            dateMeas.append(dt.date(yyyy, mm, dd))

    dateMeas = np.asarray(dateMeas)
    doyMeas = mf.toYearFraction(dateMeas)
    hh = np.asarray(hh)

    TP_ncep_interp = interp1d(doy_ncep, TP_ncep, kind='linear')(doyMeas)

    if loc.lower() == 'ahs':
        outData = '/data1/projects/ocs/ldr/Dates_' + loc2 + '_dNCEP.ascii'
    else:
        outData = '/data1/projects/ocs/' + loc.lower(
        ) + '/Dates_' + loc2 + '_dNCEP.ascii'

    #outData  = '/data1/projects/ocs/'+loc.lower()+'/Dates_Mauna Loa_dNCEP.ascii'

    #----------------------------------------
    #CREATE A FILE WITH DATE AND TIME (TO SEND AND GET BACK THE TROPOPAUSE HEIGHT)
    #----------------------------------------

    j = 0

    with open(outData, 'wb') as fopen:
        for i, lin in enumerate(lines[0:5]):
            fopen.write('{}'.format(lin))

        for i, dd in enumerate(dateMeas):
            YYYYMMDD = '{0:4d}{1:02d}{2:02d}'.format(dd.year, dd.month, dd.day)
            fopen.write('{0:13}{1:13}{2:.3f}\n'.format(YYYYMMDD, hh[i],
                                                       TP_ncep_interp[i]))
예제 #2
0
def main():
    #-----------------------------------------------------------------------------------------
    #                             Initializations
    #-----------------------------------------------------------------------------------------

    region       = 'poleS' 

    #-------------------------------------
    #Global Data Directory
    #-------------------------------------
    GDataDir     = '/data1/projects/'
    #-------------------------------------
    #three letter ID ; ID in the HDF Files
    #-------------------------------------
        #-------------------------------------
    #
    #-------------------------------------
    if region    == 'poleN':
        locs         = ['kir', 'tab', 'spu', 'bre', 'eur', 'nya']
        locID        = ['kiruna', 'thule', 'st.petersburg', 'bremen', '_eureka_', 'ny.alesund'] 
        pltID        = ['Kiruna', 'Thule', 'St Petersburg', 'Bremen', 'Eureka', 'Ny Alesund'] 
    
    elif region == 'middleN':
        locs         = ['zgp', 'rkb',  'iza']  #'tor',  'jfj'
        locID        = ['zugspitze',  'rikubetsu', 'izana']  #'_toronto_', 'jungfraujoch', 
        pltID        = ['Zugspitze',  'Rikubetsu', 'Izana'] #'Toronto',  'Jungfraujoch',
    
    elif region == 'tropics':
        locs         = ['mlo', 'alz']#, 'pmb']
        locID        = ['mauna.loa.h', 'altzomoni']#, 'paramaribo'] 
        pltID        = ['Mauna Loa', 'Altzomoni']#, 'Paramaribo']
    
    elif region == 'middleS':
        locs         = ['std', 'mai', 'wlg', 'ldr']
        locID        = ['stdenis', 'maido', 'wollongong', 'niwa001' ] 
        pltID        = ['St Denis', 'Maido', 'Wollongong', 'Lauder']

    elif region == 'poleS':
        locs         = ['ahs']
        locID        = ['arrival.heights'] 
        pltID        = ['AHTS']
    else:
        print 'An error ocurred: region is not defined'
        exit()
    #-------------------------------------
    #Inputs
    #-------------------------------------
    gasName1      = 'ocs'
    gasName2      = 'n2o'

    AvgType        = 'Monthly'   #'Monthly'  'Daily'
    smthFlg        = False
    period         = 1.0
    fitFlg         = False

    ColFlg         = False


    #------
    # Flags
    #------
    saveFlg       = True                  # Flag to either save data to pdf file (saveFlg=True) or plot to screen (saveFlg=False)
    errorFlg      = False                  # Flag to process error data
    fltrFlg       = True                   # Flag to filter the data

    dateFlg       = True                  # Flag to filter based on min and max dates
    tcFlg         = True                   # Flag to filter total column amount < 0
    tcMMFlg       = True                   # Flag to filter based on min and max total column amount
    pcFlg         = True                     # Flag to filter profiles with negative partial columns
    szaFlg        = True                   # Flag to filter based on min and max SZA    

    minSZA        = 0.0                    # Min SZA for filtering
    maxSZA        = 90.0                   # Max SZA for filtering
    maxTC         = 1.0e25                 # Max Total column amount for filtering
    minTC         = 0.0                    # Min Total column amount for filtering

    iyear         = 2009   
    imonth        = 1
    iday          = 1
    fyear         = 2016
    fmonth        = 12
    fday          = 31
    
    sclfct        = 1.0E9                  # Scale factor to apply to vmr plots (ppmv=1.0E6, ppbv=1.0E9, etc)
    sclfctName    = 'ppb'                 # Name of scale factor for labeling plots
    
    TCsclfct      = 1.0e16
    TCsclfctName  = 'x10$^{16}$'

    pColsFlg      = True                   #Calculate tropospheric and stratospheric columns?

    pltPcol       = False                  #plot the time series in partial columns
    pltWvmr       = True                   #plot the time series in weighted VMR
    
    Adth          = 16.0                   #Altitude in km of tropopause in case NCEP or DTH is not available
    offH          = 5.0                    #Additional altitude above the tropopause height



    #-------------------------------------
    # Flag for Plots
    #-------------------------------------


                                    #----------------------------#
                                    #        --- START ---       #
                                    #----------------------------#

    #-------------------------------------
    #Name of PDF with Figures
    #-------------------------------------
    if ColFlg: pltFile = GDataDir+'/ocs/figures/LifeTime_OCS_Column_'+region+'.pdf'
    else: pltFile = GDataDir+'/ocs/figures/LifeTime_OCS_pCol_'+region+'.pdf'

    if saveFlg: pdfsav = PdfPages(pltFile)
    else: pdfsav = ''

    #-------------------------------------
    # Check file and directories
    #-------------------------------------
    dataDir1    = [GDataDir+gasName1+'/'+l+'/'  for l in locs]
    dataDir2    = [GDataDir+gasName2+'/'+l+'/'  for l in locs]

    for d in dataDir1:  ckDir(d,exit=True)
    for d in dataDir2:  ckDir(d,exit=True)
    ckDir(os.path.dirname(os.path.realpath(pltFile)),exit=True)

    #-------------------------------------
    # Create instance of output data class   
    #-------------------------------------
    statDataCl  = OrderedDict()
    statDataCl2 = OrderedDict()

    Group1 = zip(dataDir1,locID, pltID, locs)
    Group1.sort(key=lambda Group1: Group1[2])

    Group2 = zip(dataDir2,locID, pltID, locs)
    Group2.sort(key=lambda Group2: Group2[2])

    locs = [l for dd, id, pl, l in Group1]
    pltID.sort()

    
    for dd, id, pl, l in Group1:

        #-------------------------------------
        # Some HDF files are in specific folder: change here accordingly
        #-------------------------------------
        if pl == 'Wollongong':      dd = dd + 'ocs_hippov2/'
        elif pl == 'Jungfraujoch' : dd = dd + 'OCS.39_1b3144b4fe4a58f29f1f_/'
        elif pl == 'Toronto' :      dd = dd + 'OCS/'
        elif pl == 'Eureka' :       dd = dd + 'OCS/'
        elif pl == 'Rikubetsu':     dd = dd + 'HDF_Fil4/'
        elif pl == 'Tsukuba' :      dd = dd + 'HDFfiles/'
        elif pl == 'Zugspitze':     dd = dd + 'OCS_Zugspitze/'
        elif pl == 'Kiruna':        dd = dd + 'OCS_Kiruna/'
        elif pl == 'Izana':         dd = dd + 'OCS_Izana/'
        elif pl == 'St Petersburg': dd = dd + 'HDF_OCS_SPb_O3_atm16/'
        elif pl == 'Paris':         dd = dd + '2019_Paris/'
        else: dd = dd

        statDataCl[pl]  = dc.ReadHDFData(dd, id, gasName1)
    
    for dd, id, pl, l in Group2:

        statDataCl2[pl] = dc.ReadHDFData(dd, id, gasName2)


    #-------------------------------------
    # Variables from HDF files 
    #-------------------------------------
    datesJD2K    = OrderedDict()
    rPrf         = OrderedDict();  rPrf_2         = OrderedDict() #retrieved Prf in mixing ratio
    aPrf         = OrderedDict()   #apriori Prf in mixing ratio
    rPrfMol      = OrderedDict();  rPrfMol_2      = OrderedDict()   #retrieved Prf partial Column (molec/cm2)
    aPrfMol      = OrderedDict()   #apriori Prf partial Column (molec/cm2)
    totClmn      = OrderedDict();  totClmn_2      = OrderedDict() #retrieved total column (molec/cm2)
    atotClmn     = OrderedDict()   #apriori total column (molec/cm2)
    avkVMR       = OrderedDict()   #Averaging kernel (VMR)
    avkTC        = OrderedDict()   #Averaging kernel total column
    alt          = OrderedDict()   #Altitude 
    sza          = OrderedDict()   #Solar Zenith Angle
    TempPrf      = OrderedDict()   #Temperature Profile
    PresPrf      = OrderedDict()   #Pressure Profile

    #-------------------------------------
    # Variables calculated 
    #-------------------------------------
    #alt_orig     = OrderedDict()
    dates        = OrderedDict()
    dates_2       = OrderedDict()
    
    avkSCF       = OrderedDict()   #Averaging kernel (scale factor)
    dofs         = OrderedDict()   #degrees of freedom
    AirMPrf      = OrderedDict(); AirMPrf_2    = OrderedDict()   #Airmass
    rPrfMol      = OrderedDict()   #retrieved Prf in molec/cm2
    aPrfMol      = OrderedDict()   #apriori Prf in molec/cm2

    totWvmr      = OrderedDict()    #Weightet VMR A priori
    atotWvmr     = OrderedDict()

    alttpp       = OrderedDict()
    alttpp2      = OrderedDict()

    altbl1       = OrderedDict()
    altbl2       = OrderedDict()

    altft1       = OrderedDict()
    altft2       = OrderedDict()

    altst1       = OrderedDict()
    altst2       = OrderedDict()

    Lat          = []
    Lon          = []

    if errorFlg:
        tot_rnd       = OrderedDict()
        tot_sys       = OrderedDict()
        tot_std       = OrderedDict()
        vmr_rnd_err   = OrderedDict()
        vmr_sys_err   = OrderedDict()
        vmr_tot_err   = OrderedDict()

    if pColsFlg:
        dtp           = OrderedDict()
        datesdtp      = OrderedDict()
        
        PcolStrat     = OrderedDict()   #partial columns
        PcolTrop1     = OrderedDict()
        PcolTrop2     = OrderedDict()

        PcolStratapr  = OrderedDict()   #partial columns A priori
        PcolTropapr1  = OrderedDict()
        PcolTropapr2  = OrderedDict()

        WvmrStrat     = OrderedDict(); WvmrStrat_2     = OrderedDict()   #Weighted VMR
        WvmrTrop1     = OrderedDict()
        WvmrTrop1_2     = OrderedDict()
        WvmrTrop2     = OrderedDict(); WvmrTrop2_2     = OrderedDict()

        WvmrStratapr  = OrderedDict()    #Weighted VMR A priori
        WvmrTropapr1  = OrderedDict()
        WvmrTropapr2  = OrderedDict()

        rPcol         = OrderedDict(); rPcol_2         = OrderedDict() 
        aPcol         = OrderedDict()

        rPvmr         = OrderedDict(); rPvmr_2         = OrderedDict()
        aPvmr         = OrderedDict()


    for ii, idhdf in enumerate(pltID):

        print idhdf

        datesJD2K[idhdf]    = statDataCl[idhdf].HDF[statDataCl[idhdf].getDatetimeName()]
        dates[idhdf]        = dc.jdf_2_datetime(datesJD2K[idhdf])

        datesJD2K_2         = statDataCl2[idhdf].HDF[statDataCl2[idhdf].getDatetimeName()]
        dates_2[idhdf]      = dc.jdf_2_datetime(datesJD2K_2)
        

        alt[idhdf]          = statDataCl[idhdf].HDF[statDataCl[idhdf].getAltitudeName()]
        sza[idhdf]          = statDataCl[idhdf].HDF[statDataCl[idhdf].getAngleSolarZenithAstronomicalName()]
        
        conv                = statDataCl[idhdf].HDF[statDataCl[idhdf].PrimaryGas.upper()+'.'+statDataCl[idhdf].getMixingRatioAbsorptionSolarName()+'VAR_SI_CONVERSION']            
        rPrf[idhdf]         = statDataCl[idhdf].HDF[statDataCl[idhdf].PrimaryGas.upper()+'.'+statDataCl[idhdf].getMixingRatioAbsorptionSolarName()]*float(conv[0][1])*sclfct

        conv_2              = statDataCl2[idhdf].HDF[statDataCl2[idhdf].PrimaryGas.upper()+'.'+statDataCl2[idhdf].getMixingRatioAbsorptionSolarName()+'VAR_SI_CONVERSION']    
        rPrf_2[idhdf]       = statDataCl2[idhdf].HDF[statDataCl2[idhdf].PrimaryGas.upper()+'.'+statDataCl[idhdf].getMixingRatioAbsorptionSolarName()]*float(conv_2[0][1])*sclfct

        aPrf[idhdf]         = statDataCl[idhdf].HDF[statDataCl[idhdf].PrimaryGas.upper()+'.'+statDataCl[idhdf].getMixingRatioAbsorptionSolarAprioriName()]*float(conv[0][1])*sclfct

        conv                = statDataCl[idhdf].HDF[statDataCl[idhdf].PrimaryGas.upper()+'.'+statDataCl[idhdf].getColumnPartialAbsorptionSolarName()+'VAR_SI_CONVERSION']
        rPrfMol[idhdf]      = statDataCl[idhdf].HDF[statDataCl[idhdf].PrimaryGas.upper()+'.'+statDataCl[idhdf].getColumnPartialAbsorptionSolarName()]*float(conv[0][1])*(6.02e23/100./100.)
        
        conv_2                = statDataCl2[idhdf].HDF[statDataCl2[idhdf].PrimaryGas.upper()+'.'+statDataCl2[idhdf].getColumnPartialAbsorptionSolarName()+'VAR_SI_CONVERSION']
        rPrfMol_2[idhdf]    = statDataCl2[idhdf].HDF[statDataCl2[idhdf].PrimaryGas.upper()+'.'+statDataCl2[idhdf].getColumnPartialAbsorptionSolarName()]*float(conv_2[0][1])*(6.02e23/100./100.)

        aPrfMol[idhdf]       = statDataCl[idhdf].HDF[statDataCl[idhdf].PrimaryGas.upper()+'.'+statDataCl[idhdf].getColumnPartialAbsorptionSolarAprioriName()]*float(conv[0][1])*(6.02e23/100./100.)

        conv                = statDataCl[idhdf].HDF[statDataCl[idhdf].PrimaryGas.upper()+'.'+statDataCl[idhdf].getColumnAbsorptionSolarName()+'VAR_SI_CONVERSION']
        totClmn[idhdf]      = statDataCl[idhdf].HDF[statDataCl[idhdf].PrimaryGas.upper()+'.'+statDataCl[idhdf].getColumnAbsorptionSolarName()]*float(conv[0][1]) * (6.02e23) /100./100. / TCsclfct

        conv_2                = statDataCl2[idhdf].HDF[statDataCl2[idhdf].PrimaryGas.upper()+'.'+statDataCl2[idhdf].getColumnAbsorptionSolarName()+'VAR_SI_CONVERSION']
        totClmn_2[idhdf]     = statDataCl2[idhdf].HDF[statDataCl2[idhdf].PrimaryGas.upper()+'.'+statDataCl2[idhdf].getColumnAbsorptionSolarName()]*float(conv_2[0][1]) * (6.02e23) /100./100. / TCsclfct

        atotClmn[idhdf]     = statDataCl[idhdf].HDF[statDataCl[idhdf].PrimaryGas.upper()+'.'+statDataCl[idhdf].getColumnAbsorptionSolarAprioriName()]*float(conv[0][1]) * (6.02e23) /100./100. / TCsclfct
        
        PresPrf[idhdf]      = statDataCl[idhdf].HDF[statDataCl[idhdf].getPressureIndependentName()]
        TempPrf[idhdf]      = statDataCl[idhdf].HDF[statDataCl[idhdf].getTemperatureIndependentName()]

        AltBo               = statDataCl[idhdf].HDF[statDataCl[idhdf].getAltitudeBoundariesName()]
             
        nobs                = rPrf[idhdf].shape[0]
        n_layer             = rPrf[idhdf].shape[1]

        if statDataCl[idhdf].PrimaryGas.upper()+'.'+statDataCl[idhdf].getMixingRatioAbsorptionSolarAvkName() in statDataCl[idhdf].HDF.keys():
            avkVMR[idhdf]       = statDataCl[idhdf].HDF[statDataCl[idhdf].PrimaryGas.upper()+'.'+statDataCl[idhdf].getMixingRatioAbsorptionSolarAvkName()]
            avkTC[idhdf]        = statDataCl[idhdf].HDF[statDataCl[idhdf].PrimaryGas.upper()+'.'+statDataCl[idhdf].getColumnAbsorptionSolarAvkName()]
        else:
            avkVMR[idhdf]  = np.empty([nobs,n_layer,n_layer])
            avkTC[idhdf]   = np.empty([nobs,n_layer,n_layer])
            avkVMR[idhdf].fill('nan')
            avkTC[idhdf].fill('nan')

        #----------------------------------------
        #CALCULATED AIR MASS
        #----------------------------------------
        AirMPrf[idhdf]     =  np.divide(rPrfMol[idhdf], rPrf[idhdf])*sclfct

        AirMPrf_2[idhdf]   =  np.divide(rPrfMol_2[idhdf], rPrf_2[idhdf])*sclfct

        #----------------------------------------
        #EXTRACT SINGLE ALTITUDE VECTOR
        #----------------------------------------
        if (idhdf == 'Kiruna') or (idhdf == 'Zugspitze') or (idhdf == 'Izana') or (idhdf == 'Paris'):
            alt[idhdf]          = alt[idhdf][0, :]
        else:
            alt[idhdf]          = alt[idhdf][0:n_layer]

        #----------------------------------------
        #READ LAT/LON/HEIGHT OF INSTRUMENT
        #----------------------------------------
        Lat_i           = statDataCl[idhdf].HDF[statDataCl[idhdf].getLatitudeInstrumentName()]
        Lon_i           = statDataCl[idhdf].HDF[statDataCl[idhdf].getLongitudeInstrumentName()]
        alt_instru      = statDataCl[idhdf].HDF[statDataCl[idhdf].getAltitudeInstrumentName()]

        Lat.append(float(Lat_i[0]))
        Lon.append(float(Lon_i[0]))

        print '\n'
        print idhdf
        print 'Latitude          = {0:.2f}'.format(Lat_i[0])
        print 'Longitude         = {0:.2f}'.format(Lon_i[0])
        print 'Altitude of Instr = {0:.2f}'.format(alt_instru[0])

        #----------------------------------------
        #CALCULATE SCALING FACTOR AK
        #----------------------------------------
        if statDataCl[idhdf].PrimaryGas.upper()+'.'+statDataCl[idhdf].getMixingRatioAbsorptionSolarAvkName() in statDataCl[idhdf].HDF.keys():
            avkSCF[idhdf]  = np.zeros((nobs,n_layer,n_layer))

            for obs in range(0,nobs):
                Iapriori        = np.zeros((n_layer,n_layer))
                IaprioriInv     = np.zeros((n_layer,n_layer))
                np.fill_diagonal(Iapriori, aPrf[idhdf][obs])
                np.fill_diagonal(IaprioriInv, 1.0 / (aPrf[idhdf][obs]))
                avkSCF[idhdf][obs,:,:] = np.dot(np.dot(IaprioriInv,np.squeeze(avkVMR[idhdf][obs,:,:])),Iapriori)

            dofs[idhdf]         = np.asarray([np.trace(aki) for aki in avkSCF[idhdf]])
        else:
            avkSCF[idhdf]  = np.zeros((nobs,n_layer,n_layer))
            avkSCF[idhdf].fill('nan')


        #----------------------------------------
        # FILTER DATA
        #----------------------------------------
        if fltrFlg: statDataCl[idhdf].fltrData(statDataCl[idhdf].PrimaryGas,iyear=iyear, imonth=imonth, iday=iday, fyear=fyear, fmonth=fmonth, fday=fday, minsza=minSZA,
                                               mxsza=maxSZA,minTC=minTC,maxTC=maxTC, tcFlg=tcFlg,pcFlg=pcFlg,szaFlg=szaFlg,tcMMFlg=tcMMFlg, dateFlg=dateFlg)
        else:    statDataCl[idhdf].inds = np.array([]) 
        
        try:
            dates[idhdf]    = np.delete(dates[idhdf], statDataCl[idhdf].inds)
            sza[idhdf]      = np.delete(sza[idhdf], statDataCl[idhdf].inds)
            totClmn[idhdf]  = np.delete(totClmn[idhdf], statDataCl[idhdf].inds)
            atotClmn[idhdf] = np.delete(atotClmn[idhdf], statDataCl[idhdf].inds)
            rPrf[idhdf]     = np.delete(rPrf[idhdf], statDataCl[idhdf].inds, axis=0)
            rPrfMol[idhdf]  = np.delete(rPrfMol[idhdf], statDataCl[idhdf].inds, axis=0)
            aPrf[idhdf]     = np.delete(aPrf[idhdf], statDataCl[idhdf].inds, axis=0)
            aPrfMol[idhdf]  = np.delete(aPrfMol[idhdf], statDataCl[idhdf].inds, axis=0)
            avkVMR[idhdf]   = np.delete(avkVMR[idhdf], statDataCl[idhdf].inds, axis=0)
            avkSCF[idhdf]   = np.delete(avkSCF[idhdf], statDataCl[idhdf].inds, axis=0)
            avkTC[idhdf]    = np.delete(avkTC[idhdf], statDataCl[idhdf].inds, axis=0)
            AirMPrf[idhdf]  = np.delete(AirMPrf[idhdf], statDataCl[idhdf].inds, axis=0)

        except Exception as errmsg:
            print '\nError: ', errmsg


        if pColsFlg:

            #---------------------------------------------------
            #STATISTICS OF TROPOPAUSE HEIGHT BASED ON DAILY AVERAGES
            #---------------------------------------------------
            # AvgTpp       = mf.dailyAvg(dtp[idhdf], dates[idhdf], dateAxis=1, meanAxis=0)
            # AvgTpp       = AvgTpp['dailyAvg']

            # maxTpp       = np.max(AvgTpp)
            # minTpp       = np.min(AvgTpp)
            # meanTpp      = np.mean(AvgTpp)
            # stdTpp       = np.std(AvgTpp)

            #print '\nMean TPH: {0:.2f} +/- {1:.2f}'.format(meanTpp, stdTpp)

            #----------------------------------------------------
            #
            #----------------------------------------------------
            if float(Lat_i[0]) >=70.: 
                meanTpp = 8.8
                stdTpp  = 1.2
            
            elif (float(Lat_i[0]) >= 60.0) & (float(Lat_i[0]) < 70.0):
                meanTpp = 9.8
                stdTpp  = 1.3
            
            elif (float(Lat_i[0]) >= 50.0) & (float(Lat_i[0]) < 60.0):
                meanTpp = 10.9
                stdTpp  = 1.2

            elif (float(Lat_i[0]) >= 40.0) & (float(Lat_i[0]) < 50.0):
                meanTpp = 11.6
                stdTpp  = 1.6

            elif (float(Lat_i[0]) >= 30.0) & (float(Lat_i[0]) < 40.0):
                meanTpp = 12.9 #12.58
                stdTpp  = 2.4  #2.72

            elif (float(Lat_i[0]) >= 20.0) & (float(Lat_i[0]) < 30.0):
                meanTpp = 15.0
                stdTpp  = 1.3

            elif (float(Lat_i[0]) >= -25.0) & (float(Lat_i[0]) < 20.0):
                meanTpp = 16.5
                stdTpp  = 0.4

            elif (float(Lat_i[0]) >= -40.0) & (float(Lat_i[0]) < -25.0):
                meanTpp = 12.3
                stdTpp  = 2.2

            elif (float(Lat_i[0]) >= -50.0) & (float(Lat_i[0]) < -40.0):
                meanTpp = 11.1
                stdTpp  = 1.3

            elif float(Lat_i[0]) < -50:
                meanTpp = 8.8
                stdTpp  = 1.7


            partialCols  = [ [0.0, 4.0], [4.0, (meanTpp - stdTpp*2.)], [(meanTpp+stdTpp*2.), 40.] ]


            for ii, pc in enumerate(partialCols):

                inds = np.where( (alt[idhdf] >= pc[0]) & (alt[idhdf] <= pc[1])  )[0]

                #---------------------------------------------------
                #THESE SITES REPORT INCREASING ALTITUDE
                #---------------------------------------------------
                if (idhdf == 'Kiruna') or (idhdf == 'Izana') or (idhdf == 'Paris') or (idhdf == 'Altzomoni'):       
                

                    rPcol[idhdf+str(pc)]  = np.sum(rPrfMol[idhdf][:,inds], axis=1)
                    aPcol[idhdf+str(pc)]  = np.sum(aPrfMol[idhdf][:,inds], axis=1)

                    rPcol_2[idhdf+str(pc)]  = np.sum(rPrfMol_2[idhdf][:,inds], axis=1)

                    try:

                        rPvmr[idhdf+str(pc)]  = np.average(rPrf[idhdf][:,inds], weights=AirMPrf[idhdf][:,inds],axis=1)
                        aPvmr[idhdf+str(pc)]  = np.average(aPrf[idhdf][:,inds], weights=AirMPrf[idhdf][:,inds],axis=1)

                        rPvmr_2[idhdf+str(pc)]  = np.average(rPrf_2[idhdf][:,inds], weights=AirMPrf_2[idhdf][:,inds],axis=1)
                    
                    except Exception as errmsg:
                        rPvmr[idhdf+str(pc)]    = np.zeros(len(rPrfMol[idhdf][:,0]))
                        rPvmr[idhdf+str(pc)][:] = float('nan')

                        rPvmr_2[idhdf+str(pc)]    = np.zeros(len(rPrfMol_2[idhdf][:,0]))
                        rPvmr_2[idhdf+str(pc)][:] = float('nan')

                        aPvmr[idhdf+str(pc)]    = np.zeros(len(rPrfMol[idhdf][:,0]))
                        aPvmr[idhdf+str(pc)][:] = float('nan')

                else: 


                    rPcol[idhdf+str(pc)]  = np.sum(rPrfMol[idhdf][:,inds], axis=1)
                    aPcol[idhdf+str(pc)]  = np.sum(aPrfMol[idhdf][:,inds], axis=1)

                    rPcol_2[idhdf+str(pc)]  = np.sum(rPrfMol_2[idhdf][:,inds], axis=1)

                    try:

                        rPvmr[idhdf+str(pc)]  = np.average(rPrf[idhdf][:,inds], weights=AirMPrf[idhdf][:,inds],axis=1)
                        aPvmr[idhdf+str(pc)]  = np.average(aPrf[idhdf][:,inds], weights=AirMPrf[idhdf][:,inds],axis=1)

                        rPvmr_2[idhdf+str(pc)]  = np.average(rPrf_2[idhdf][:,inds], weights=AirMPrf_2[idhdf][:,inds],axis=1)

                    except Exception as errmsg:
                        rPvmr[idhdf+str(pc)]    = np.zeros(len(rPrfMol[idhdf][:,0]))
                        rPvmr[idhdf+str(pc)][:] = float('nan')

                        aPvmr[idhdf+str(pc)]    = np.zeros(len(rPrfMol[idhdf][:,0]))
                        aPvmr[idhdf+str(pc)][:] = float('nan')

                        rPvmr_2[idhdf+str(pc)]    = np.zeros(len(rPrfMol_2[idhdf][:,0]))
                        rPvmr_2[idhdf+str(pc)][:] = float('nan')

                if ii == 0:
                    PcolTrop1[idhdf]     = np.asarray(rPcol[idhdf+str(pc)])/TCsclfct
                    PcolTropapr1[idhdf]  = np.asarray(aPcol[idhdf+str(pc)])/TCsclfct

                    WvmrTrop1[idhdf]     = np.asarray(rPvmr[idhdf+str(pc)])
                    WvmrTropapr1[idhdf]  = np.asarray(aPvmr[idhdf+str(pc)])

                    altbl1[idhdf]       = np.zeros(len(rPrfMol[idhdf][:,0]))
                    altbl1[idhdf][:]    = np.asarray(alt[idhdf][inds[-1]])

                    altbl2[idhdf]       = np.zeros(len(rPrfMol[idhdf][:,0]))
                    altbl2[idhdf][:]    = np.asarray(alt[idhdf][inds[0]])

                    WvmrTrop1_2[idhdf]     = np.asarray(rPvmr_2[idhdf+str(pc)])

                elif ii == 1:
                    PcolTrop2[idhdf]     = np.asarray(rPcol[idhdf+str(pc)])/TCsclfct
                    PcolTropapr2[idhdf]  = np.asarray(aPcol[idhdf+str(pc)])/TCsclfct

                    WvmrTrop2[idhdf]     = np.asarray(rPvmr[idhdf+str(pc)])
                    WvmrTropapr2[idhdf]  = np.asarray(aPvmr[idhdf+str(pc)])

                    altft1[idhdf]       = np.zeros(len(rPrfMol[idhdf][:,0]))
                    altft1[idhdf][:]    = np.asarray(alt[idhdf][inds[-1]])

                    altft2[idhdf]       = np.zeros(len(rPrfMol[idhdf][:,0]))
                    altft2[idhdf][:]    = np.asarray(alt[idhdf][inds[0]])


                    WvmrTrop2_2[idhdf]     = np.asarray(rPvmr_2[idhdf+str(pc)])

                elif ii == 2:
                    PcolStrat[idhdf]    = np.asarray(rPcol[idhdf+str(pc)])/TCsclfct
                    PcolStratapr[idhdf] = np.asarray(aPcol[idhdf+str(pc)])/TCsclfct

                    WvmrStrat[idhdf]    = np.asarray(rPvmr[idhdf+str(pc)])
                    WvmrStratapr[idhdf] = np.asarray(aPvmr[idhdf+str(pc)])

                    altst1[idhdf]       = np.zeros(len(rPrfMol[idhdf][:,0]))
                    altst1[idhdf][:]    = np.asarray(alt[idhdf][inds[-1]])

                    altst2[idhdf]       = np.zeros(len(rPrfMol[idhdf][:,0]))
                    altst2[idhdf][:]    = np.asarray(alt[idhdf][inds[0]])

                    WvmrStrat_2[idhdf]   = np.asarray(rPvmr_2[idhdf+str(pc)])


        totWvmr[idhdf]  = np.average(rPrf[idhdf], axis=1, weights=AirMPrf[idhdf])
        atotWvmr[idhdf] = np.average(aPrf[idhdf], axis=1, weights=AirMPrf[idhdf])
    
    
    clmap = 'jet'
    cm           = plt.get_cmap(clmap)
    yearsLc      = YearLocator()
    daysLc       = DayLocator()
    months       = MonthLocator()
    DateFmt      = DateFormatter('%m')
    
    
    fig, ax   = plt.subplots(2, figsize=(8, 9), sharex=True)
    fig2, ax2 = plt.subplots(figsize=(7, 6))

    OCS_all   = []
    OCS_e_all = []
    
    N2O_all   = []
    N2O_e_all = []

    OCS_trop_all = []
    OCS_trop_e_all = []

    N2O_trop_all = []
    N2O_trop_e_all = []

    lifetime_all = []

    for i, idhdf in enumerate(pltID):

        if ColFlg:

            Avg            = mf.mnthlyAvg(totClmn[idhdf], dates[idhdf], dateAxis=1, meanAxis=0)
            Dates          = Avg['dates']
            dateYearFrac   = mf.toYearFraction(Avg['dates'])
            AvgData        =  Avg['mnthlyAvg']
            std            =  Avg['std']

            Avg_2          = mf.mnthlyAvg(totClmn_2[idhdf], dates_2[idhdf], dateAxis=1, meanAxis=0)
            Dates_2        = Avg_2['dates']
            dateYearFrac_2 = mf.toYearFraction(Avg_2['dates'])
            AvgData_2      =  Avg_2['mnthlyAvg']
            std_2          =  Avg_2['std']

        else:

            Avg            = mf.mnthlyAvg(WvmrStrat[idhdf], dates[idhdf], dateAxis=1, meanAxis=0)
            Dates          = Avg['dates']
            dateYearFrac   = mf.toYearFraction(Avg['dates'])
            AvgData        =  Avg['mnthlyAvg']
            std            =  Avg['std']

            Avg_2          = mf.mnthlyAvg(WvmrStrat_2[idhdf], dates_2[idhdf], dateAxis=1, meanAxis=0)
            Dates_2        = Avg_2['dates']
            dateYearFrac_2 = mf.toYearFraction(Avg_2['dates'])
            AvgData_2      =  Avg_2['mnthlyAvg']
            std_2          =  Avg_2['std']


        AvgTrop        = mf.mnthlyAvg(WvmrTrop2[idhdf], dates[idhdf], dateAxis=1, meanAxis=0)
        OCStrop        =  AvgTrop['mnthlyAvg']
        OCStrop_e      = AvgTrop['std']
        
        AvgTrop2       = mf.mnthlyAvg(WvmrTrop2_2[idhdf], dates_2[idhdf], dateAxis=1, meanAxis=0)
        N2Otrop        =  AvgTrop2['mnthlyAvg']
        N2Otrop_e      = AvgTrop2['std']


        intrsctVals = np.intersect1d(dateYearFrac, dateYearFrac_2, assume_unique=False)
        
        inds1       = np.nonzero( np.in1d( dateYearFrac, intrsctVals, assume_unique=False ) )[0]
        inds2       = np.nonzero( np.in1d( dateYearFrac_2, intrsctVals, assume_unique=False ) )[0]

        print '\n'
        print idhdf
        #print 'Total Number of Monthly OCS = ' +str(len(dateYearFrac))
        #print 'Total Number of Monthly N2O = ' +str(len(dateYearFrac_2))
        #print 'Total Number of coincident dates between OCS and N2O = ' +str(len(intrsctVals))


        AvgData   = AvgData[inds1]
        AvgData_2 = AvgData_2[inds2]

        std       = std[inds1]
        std_2     = std_2[inds2]

        indsZero   = np.where(std <= 0.)[0]
        indsZero_2 = np.where(std_2 <= 0.)[0]

        std[indsZero] =  AvgData[indsZero]*0.05 
        std_2[indsZero_2] =  AvgData_2[indsZero_2]*0.05    
        
        Dates     = Dates[inds1]
        Dates_2   = Dates_2[inds2]

        OCS_all.extend(AvgData)
        N2O_all.extend(AvgData_2)

        OCS_e_all.extend(std)
        N2O_e_all.extend(std_2)

        OCS_trop_all.extend(OCStrop[inds1])

        N2O_trop_all.extend(N2Otrop[inds2])


        meanTropOCS   = np.nanmean(OCStrop[inds1])
        meanTropN2O   = np.nanmean(N2Otrop[inds2])


        ax[i].plot(Dates, AvgData,   linestyle='-', marker ='', color='b', label='OCS')
        ax[i].scatter(Dates, AvgData, s=35, edgecolor='k', color='b')
        ax[i].set_title(idhdf)

        axr = ax[i].twinx()

        axr.plot(Dates_2, AvgData_2,   linestyle='-', marker ='', color='r', label='N2O')
        axr.scatter(Dates_2, AvgData_2, s=35, edgecolor='k', color='r')

        if i == 0:  
            ax[i].legend(prop={'size':12}, loc=2)
            axr.legend(prop={'size':12}, loc=3)

        
        ax2.plot(AvgData, AvgData_2, linestyle='none', marker ='')
        ax2.scatter(AvgData, AvgData_2, s=35, edgecolor='k', label=idhdf)


        odr, odrErr  = mf.orthoregress(AvgData, AvgData_2, xerr= std, yerr=std_2,  InError=True)
        slopelr, interceptlr, r_valueln, p_valuelr, std_errlr = stats.linregress(AvgData, AvgData_2)

        slope      = float(odr[0])
        slope_e    = float(odrErr[0])
        
        intercept  = float(odr[1])
        intercept_e  = float(odrErr[1])

        ax[i].xaxis.set_tick_params(which='major',labelsize=12)
        ax[i].xaxis.set_tick_params(which='minor',labelbottom='off')
        if ColFlg: ax[i].set_ylabel('OCS [{}]'.format(TCsclfctName), fontsize=16)
        else: ax[i].set_ylabel('OCS [ppb]', fontsize=16)
        #ax[i].set_xlabel('OCS [ppt]', fontsize=16)
        ax[i].tick_params(axis='both', which='major', labelsize=14)
        ax[i].grid(True)

        if ColFlg: axr.set_ylabel('N$_2$O  [{}]'.format(TCsclfctName), fontsize=16)
        else: axr.set_ylabel('N$_2$O [ppb]', fontsize=16)

        #ax[i].set_xlabel('OCS [ppt]', fontsize=16)
        axr.tick_params(axis='both', which='major', labelsize=14)
        axr.grid(True)

        if i == 0:  
            ax[i].legend(prop={'size':12}, loc=2)
            axr.legend(prop={'size':12}, loc=3)

        lifetime = slope * (meanTropOCS / meanTropN2O) * 117.

        lifetime_e  = np.sqrt( (slope_e/slope)**2 + (20./117.)**2  +  (np.std(OCStrop[inds1])/np.mean(OCStrop[inds1]))**2 +  (np.std(N2Otrop[inds2])/np.mean(N2Otrop[inds2]))**2   ) * lifetime

        print '\nSlope: {0:.2f} +/- {1:.2f}'.format(slope, slope_e)
        print 'Intercept = {0:.3f} +/- {1:.3f}'.format(intercept, intercept_e)
        print 'R value = {0:.2f}'.format(float(r_valueln))
        print 'Trop OCS [ppb] = {0:.3f} +/- {1:.3f}'.format(np.mean(OCStrop[inds1]), np.std(OCStrop[inds1]))
        print 'Trop N2O [ppb] = {0:.3f} +/- {1:.3f}'.format(np.mean(N2Otrop[inds2]), np.std(N2Otrop[inds2]))

        print 'Lifetime = {0:.2f} +/- {1:.2f}'.format(float(lifetime), float(lifetime_e))

        lifetime_all.append(lifetime)


    
    OCS_all        = np.asarray(OCS_all)
    OCS_e_all      = np.asarray(OCS_e_all)
    N2O_e_all      = np.asarray(N2O_e_all)
    N2O_all        = np.asarray(N2O_all)

    OCS_trop_all   = np.asarray(OCS_trop_all)
    N2O_trop_all   = np.asarray(N2O_trop_all)

    odr, odrErr  = mf.orthoregress(OCS_all, N2O_all, xerr=OCS_e_all, yerr=N2O_e_all, InError=True)
    slopelr, interceptlr, r_valueln, p_valuelr, std_errlr = stats.linregress(OCS_all, N2O_all)

    slope      = float(odr[0])
    slope_e    = float(odrErr[0])
    
    intercept  = float(odr[1])
    intercept_e  = float(odrErr[1])

    lifetime    = slope * (np.mean(OCS_trop_all) / np.mean(N2O_trop_all)) * 117.
    lifetime_e  = np.sqrt( (slope_e/slope)**2 + (20./117.)**2  +  (np.std(OCS_trop_all)/np.mean(OCS_trop_all))**2 +  (np.std(N2O_trop_all)/np.mean(N2O_trop_all))**2   ) * lifetime
   
    
    print '\nAll'
    print '\nSlope        = {0:.2f} +/- {1:.2f}'.format(slope, slope_e)
    print 'Intercept      = {0:.3f} +/- {1:.3f}'.format(intercept, intercept_e)
    print 'R value        = {0:.2f}'.format(float(r_valueln))
    print 'Trop OCS [ppb] = {0:.3f} +/- {1:.3f}'.format(np.mean(OCS_trop_all), np.std(OCS_trop_all))
    print 'Trop N2O [ppb] = {0:.3f} +/- {1:.3f}'.format(np.mean(N2O_trop_all), np.std(N2O_trop_all))
    print 'Lifetime       = {0:.2f} +/- {1:.2f}'.format(float(lifetime), float(lifetime_e))

    lifetime_all = np.asarray(lifetime_all)

    print 'Lifetime all (Mean)   = {0:.2f} +/- {1:.2f}'.format(np.mean(lifetime_all), np.std(lifetime_all))
    print 'Lifetime all (Median) = {0:.2f} +/- {1:.2f}'.format(np.median(lifetime_all), np.std(lifetime_all))




    ax2.legend(prop={'size':10})


    ax2.xaxis.set_tick_params(which='major',labelsize=12)
    ax2.xaxis.set_tick_params(which='minor',labelbottom='off')

    if ColFlg:
        ax2.set_xlabel('OCS [{} mole/cm$^2$]'.format(TCsclfctName), fontsize=16)
        ax2.set_ylabel('N$_2$O [{} mole/cm$^2$]'.format(TCsclfctName), fontsize=16)

    else:
        ax2.set_ylabel('N$_2$O [ppb]', fontsize=16)
        ax2.set_xlabel('OCS [ppb]', fontsize=16)
    ax2.tick_params(axis='both', which='major', labelsize=14)
    #ax2.set_ylim(300, 350)
    #ax2.set_xlim(0.3, 0.5)
    ax2.grid(True)

    fig.subplots_adjust(left = 0.12, bottom=0.075, top=0.95, right = 0.9)
    fig2.subplots_adjust(left = 0.12, bottom=0.12, top=0.95, right = 0.95)


    if saveFlg: 
        pdfsav.savefig(fig,dpi=200)
        pdfsav.savefig(fig2,dpi=200)
        pdfsav.close() 
    else:           
        plt.show(block= False)
        user_input = raw_input('Press any key to exit >>> ')
        sys.exit()   
예제 #3
0
파일: pltSAGE.py 프로젝트: iortegam/pbin
def main():

    #-----------------------------------------------------------------------------------------
    #                             Initializations for FTIR
    #-----------------------------------------------------------------------------------------
    readftsFlg = True

    saveFlg = True

    loc = 'fl0'
    gasName = ['o3']

    if loc.lower() == 'mlo':
        ver = ['Current']  # Name of retrieval version to process
        ctlF = ['sfit4.ctl']
    elif loc.lower() == 'fl0':
        ver = ['Current_WP']  # Name of retrieval version to process
        ctlF = ['sfit4.ctl']
    else:
        print 'Error!!'
        exit()
    #----------------------
    # First Level Retrieval Directory
    #----------------------
    retDir = '/data1/ebaumer/' + loc.lower()

    pltFile = '/data/iortega/pbin/SAGE/SAGEIII-FTS-' + loc.upper() + '.pdf'

    #------
    # Flags
    #------
    errorFlg = True  # Flag to process error data
    fltrFlg = True  # Flag to filter the data
    byYrFlg = False  # Flag to create plots for each individual year in date range
    szaFlg = True  # Flag to filter based on min and max SZA
    dofFlg = True  # Flag to filter based on min DOFs
    pcNegFlg = True  # Flag to filter profiles with negative partial columns
    tcNegFlg = True  # Flagsag to filter profiles with negative total columns
    tcMMFlg = False  # Flag to filter based on min and max total column amount
    cnvrgFlg = True  # Flag to filter profiles that did not converge
    rmsFlg = True  # Flag to filter based on max RMS
    chiFlg = False  # Flag to filter based on max CHI_2_Y
    mnthFlg = False  # Flag to filter based on

    mnths = [6, 7,
             8]  # Months to filter on (these are the months to include data)
    maxRMS = [
        1.5
    ]  # Max Fit RMS to filter data. Data is filtered according to <= maxrms
    minDOF = [1.0]  # Min DOFs for filtering

    minSZA = 0.0  # Min SZA for filtering
    maxSZA = 90.0  # Max SZA for filtering
    maxCHI = 2.0  # Max CHI_y_2 value
    maxTC = 5.0E24  # Max Total column amount for filtering
    minTC = 0.0  # Min Total column amount for filtering
    sclfct = 1.0E9  # Scale factor to apply to vmr plots (ppmv=1.0E6, ppbv=1.0E9, etc)
    sclfctName = 'ppbv'  # Name of scale factor for labeling plots

    pColsFlg = True
    pCols = [[16., 24.], [24., 34.],
             [16.,
              34.]]  #--ALTITUDE TO CALCULATE PARTIAL COLUMNS AND WEIGHTED VMR

    #----------------------
    # Date range to process
    #----------------------
    iyear = 2017
    imnth = 6
    iday = 1
    fyear = 2018
    fmnth = 12
    fday = 30

    if loc.lower() == 'mlo':
        latfts = 19.4
        lonfts = -155.57  # 204.43

        thick = [
            14., 11.5, 9.3, 8.2, 7.23, 6.37, 5.62, 4.93, 4.3, 3.74, 3.24, 2.8,
            2.43, 2.11, 1.86, 1.68, 1.55, 1.48, 1.44, 1.41, 1.37, 1.34, 1.3,
            1.26, 1.23, 1.19, 1.16, 1.12, 1.08, 1.05, 1.01, 0.98, 0.942,
            0.9061, 0.8801, 0.8442, 0.8182, 0.7823, 0.7463, 0.7204, 0.6844
        ]

        mid = [
            113., 100.25, 89.85, 81.1, 73.385, 66.585, 60.59, 55.315, 50.7,
            46.68, 43.19, 40.17, 37.555, 35.285, 33.3, 31.53, 29.915, 28.4,
            26.94, 25.515, 24.125, 22.77, 21.45, 20.17, 18.925, 17.715, 16.54,
            15.4, 14.3, 13.235, 12.205, 11.21, 10.249, 9.3249, 8.4318, 7.5697,
            6.7385, 5.9382, 5.174, 4.4406, 3.7382
        ]

    elif loc.lower() == 'fl0':
        latfts = 40.4
        lonfts = -105.24  # 204.43

        thick = [
            14., 11.5, 9.3, 8.2, 7.23, 6.37, 5.62, 4.93, 4.3, 3.74, 3.24, 2.8,
            2.43, 2.11, 1.86, 1.68, 1.55, 1.48, 1.44, 1.41, 1.37, 1.34, 1.3,
            1.26, 1.23, 1.19, 1.16, 1.12, 1.08, 1.05, 1.01, 0.98, 0.9414,
            0.9043, 0.8772, 0.8401, 0.813, 0.7759, 0.7388, 0.7117, 0.6746,
            0.6474, 0.6103, 0.5732
        ]

        mid = [
            113., 100.25, 89.85, 81.1, 73.385, 66.585, 60.59, 55.315, 50.7,
            46.68, 43.19, 40.17, 37.555, 35.285, 33.3, 31.53, 29.915, 28.4,
            26.94, 25.515, 24.125, 22.77, 21.45, 20.17, 18.925, 17.715, 16.54,
            15.4, 14.3, 13.235, 12.205, 11.21, 10.2493, 9.3264, 8.4356, 7.5769,
            6.7504, 5.9559, 5.1986, 4.4734, 3.7803, 3.1193, 2.4904, 1.8986
        ]

    else:
        print 'Error:'
        exit()

    thick = [float(t) * 1000. * 100. for t in thick]
    thick = np.asarray(thick)

    mid = [float(t) for t in mid]
    mid = np.asarray(mid)

    #-----------------------------------------------------------------------------------------
    #                             Initializations for SAGE III/ISS
    #-----------------------------------------------------------------------------------------

    readSAGEFlg = True

    DirSAGE3_ISS = '/data1/Campaign/Satellite/SAGEIII-ISS'

    saveSAGEFlg = False
    pltFilESAGE3 = '/data/iortega/pbin/SAGE/SAGE3_ISSS.pdf'

    pltsage = False

    #----------------------------#
    #                            #
    #        --- START ---       #
    #                            #
    #----------------------------#

    if saveFlg: pdfsav = PdfPages(pltFile)

    #---------------------------------
    #     -- Read FTS --
    #---------------------------------
    if readftsFlg:

        fts = cfts.FTSClass(gasName, retDir, ctlF, ver, iyear, imnth, iday,
                            fyear, fmnth, fday)

        fts.ReadFTS(fltrFlg=fltrFlg,
                    sclfct=sclfct,
                    sclname=sclfctName,
                    mnthFltr=mnths,
                    mnthFltFlg=mnthFlg,
                    errFlg=errorFlg,
                    minSZA=minSZA,
                    maxSZA=maxSZA,
                    maxRMS=maxRMS,
                    minTC=minTC,
                    maxTC=maxTC,
                    minDOF=minDOF,
                    maxCHI=maxCHI,
                    dofFlg=dofFlg,
                    rmsFlg=rmsFlg,
                    tcFlg=tcNegFlg,
                    pcFlg=pcNegFlg,
                    szaFlg=szaFlg,
                    cnvrgFlg=cnvrgFlg,
                    chiFlg=chiFlg,
                    tcMMflg=tcMMFlg,
                    pColsFlg=False,
                    pCols=pCols)

    #-------------------------
    #    -- SAGE III --
    #-------------------------
    if pltsage:
        ckDir(os.path.dirname(os.path.realpath(pltFilESAGE3)), exit=True)

    if readSAGEFlg:
        ckDir(DirSAGE3_ISS, exit=True)

        sage = cs.SAGE3Cls(DirSAGE3_ISS,
                           saveFlg=saveSAGEFlg,
                           outFname=pltFilESAGE3)
        sage.ReadVariables()

        if pltsage: sage.pltSAGE3()
        if saveSAGEFlg: sage.closeFig()

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

    #-------------------------
    GasVer = []

    for (g, v) in izip(gasName, ver):
        GasVer.append(g + '_' + v)

    for gv in GasVer:

        #-------------------------
        # Find Distances close to FTS
        #-------------------------
        latsage = sage.Lat
        lonsage = sage.Lon

        #distance      = np.asarray([mf.haversine(lonfts, latfts, lonsage[i],  latsage[i]) for i in range(len(lonsage))])
        #indsDist      = np.where(distance < 1000.)[0]

        if loc.lower() == 'mlo':
            indsDist = np.where((latsage >= 14.) & (latsage <= 26.)
                                & (lonsage <= -140.) & (lonsage >= -166.))[0]
            #elif loc.lower() == 'fl0' : indsDist        = np.where((latsage >= 35.) & (latsage <= 45.) & (lonsage <= -85.) & (lonsage >= -125.) )[0]
        elif loc.lower() == 'fl0':
            indsDist = np.where((latsage >= 35.) & (latsage <= 45.)
                                & (lonsage <= -95.) & (lonsage >= -115.))[0]

        else:
            print 'Error!!'
            exit()

        indsDist = np.asarray(indsDist)

        prfsage = sage.O3_prf_conc[indsDist]
        dtsage = sage.dateTime[indsDist]
        altsage = sage.altitude[indsDist]
        latsage2 = sage.Lat[indsDist]
        lonsage2 = sage.Lon[indsDist]
        datesage = [
            dt.date(singDate.year, singDate.month, singDate.day)
            for singDate in dtsage
        ]
        datesage = np.asarray(datesage)

        print 'Total Number of SAGE profiles < max distance = ' + str(
            len(dtsage)) + '\n'

        #-------------------------
        # FIND Same days
        #-------------------------
        listdsage = [
            dt.date(singDate.year, singDate.month, singDate.day)
            for singDate in dtsage
        ]
        doysage = mf.toYearFraction(listdsage)

        listdfts = [
            dt.date(singDate.year, singDate.month, singDate.day)
            for singDate in fts.dates[gv]
        ]
        doyfts = mf.toYearFraction(listdfts)

        intrsctVals = np.intersect1d(doysage, doyfts, assume_unique=False)

        indssage = np.nonzero(
            np.in1d(doysage, intrsctVals, assume_unique=False))[0]
        indsfts = np.nonzero(np.in1d(doyfts, intrsctVals,
                                     assume_unique=False))[0]

        indssage = np.asarray(indssage)
        indsfts = np.asarray(indsfts)

        #-------------------------
        #
        #-------------------------
        prfsage = prfsage[indssage]
        dtsage = dtsage[indssage]
        altsage = altsage[indssage]
        latsage3 = latsage2[indssage]
        lonsage3 = lonsage2[indssage]

        prftfts = fts.rPrfMol[gv][
            indsfts, :] / thick  #; prftfts  = prftfts[:,indsalt ] /thick[indsalt]
        aprftfts = fts.aPrfMol[gv][
            indsfts, :] / thick  #; aprftfts = aprftfts[:,indsalt ] /thick[indsalt]
        datesfts = fts.dates[gv][indsfts]
        altfts = mid  #fts.alt[gv]
        avkSCFfts = fts.avkSCF[gv][
            indsfts, :, :]  #; avkSCFfts = avkSCFfts[:, indsalt, :]; avkSCFfts = avkSCFfts[:, :, indsalt]

        Airmassfts = fts.Airmass[gv][indsfts, :]

        avkSCFftsMean = np.mean(avkSCFfts, axis=0)

        indsalt = np.where((fts.alt[gv] > 10.) & (fts.alt[gv] < 50.))[0]

        prftfts2 = prftfts[:, indsalt]
        aprftfts2 = aprftfts[:, indsalt]
        avkSCFfts2 = avkSCFfts[:, indsalt, :]
        avkSCFfts2 = avkSCFfts2[:, :, indsalt]
        altfts2 = altfts[indsalt]

        avkSCFfts2 = np.mean(avkSCFfts2, axis=0)

        datesftslist = np.asarray(
            [dt.date(d.year, d.month, d.day) for d in datesfts])

        #------
        # Daily
        #------
        dailyVals = mf.dailyAvg(prftfts, datesfts, dateAxis=0, meanAxis=0)
        prftfts_daily = dailyVals['dailyAvg']
        dates_daily = dailyVals['dates']
        prftfts_std_daily = dailyVals['std']

        prftfts_daily3 = prftfts_daily

        dailyVals = mf.dailyAvg(aprftfts, datesfts, dateAxis=0, meanAxis=0)
        aprftfts_daily = dailyVals['dailyAvg']

        dailyVals = mf.dailyAvg(prftfts2, datesfts, dateAxis=0, meanAxis=0)
        prftfts_daily2 = dailyVals['dailyAvg']
        dates_daily2 = dailyVals['dates']
        prftfts_std_daily2 = dailyVals['std']

        dailyVals = mf.dailyAvg(aprftfts2, datesfts, dateAxis=0, meanAxis=0)
        aprftfts_daily2 = dailyVals['dailyAvg']

        print 'Total Number of coincident dates between FTS and SAGE = ' + str(
            len(dates_daily)) + '\n'

        prfsage_inte = np.zeros((prftfts_daily.shape))
        prfsage_res = np.zeros((prftfts_daily.shape))
        prfsage_resApr = np.zeros((prftfts_daily.shape))
        prfsage_res_rel = np.zeros((prftfts_daily.shape))
        prfsage_resApr_rel = np.zeros((prftfts_daily.shape))

        #-------------------------
        #
        #-------------------------
        prfsage_inte2 = np.zeros((prftfts_daily.shape))
        prfsage_smth = np.zeros((prftfts_daily.shape))

        prfsage_res2 = np.zeros((prftfts_daily.shape))
        prfsage_res_rel2 = np.zeros((prftfts_daily.shape))

        for i in range(len(dates_daily)):

            indsday = np.where(datesftslist == dates_daily[i])[0]
            AKdaily = np.mean(avkSCFfts[indsday, :, :], axis=0)

            #print 'shape', AKdaily.shape

            prfsage_i = prfsage[i, :]
            altsage_i = altsage[i, :]

            indsnan = np.argwhere(np.isnan(prfsage_i))
            if len(indsnan) >= 1:
                prfsage_i = np.delete(prfsage_i, indsnan)
                altsage_i = np.delete(altsage_i, indsnan)

            #prfsage_interp     = interpolate.interp1d(altsage_i, prfsage_i,fill_value='extrapolate', bounds_error=False, kind='cubic')(altfts2 )
            prfsage_interp = interpolate.interp1d(altsage_i,
                                                  prfsage_i,
                                                  bounds_error=False,
                                                  kind='linear')(altfts)
            prfsage_inte[i, :] = prfsage_interp

            prfsage_res[i, :] = (prftfts_daily[i, :] - prfsage_interp)
            prfsage_resApr[i, :] = (aprftfts_daily[i, :] - prfsage_interp)

            prfsage_res_rel[i, :] = (prftfts_daily[i, :] - prfsage_interp
                                     ) / prftfts_daily[i, :] * 100.
            prfsage_resApr_rel[i, :] = (aprftfts_daily[i, :] - prfsage_interp
                                        ) / prftfts_daily[i, :] * 100.

            #-------------------------
            # Partial Columns
            #-------------------------
            prfsage_interp2 = interpolate.interp1d(
                altsage_i,
                prfsage_i,
                fill_value=(prftfts_daily2[i, -1], prftfts_daily2[i, 0]),
                bounds_error=False,
                kind='cubic')(altfts)
            prfsage_inte2[i, :] = prfsage_interp2

            indsnan = np.argwhere(np.isnan(prfsage_interp))

            prfsage_smth[i, :] = aprftfts_daily[i, :] + np.dot(
                AKdaily, (prfsage_interp2 - aprftfts_daily[i, :]))

            prfsage_res2[i, :] = (prftfts_daily[i, :] - prfsage_smth[i, :])
            prfsage_res_rel2[i, :] = (prftfts_daily[i, :] - prfsage_smth[i, :]
                                      ) / prftfts_daily[i, :] * 100.

            prfsage_smth[i, indsnan] = np.nan
            prfsage_res2[i, indsnan] = np.nan
            prfsage_res_rel2[i, indsnan] = np.nan
            prftfts_daily3[i, indsnan] = np.nan

        #-------------------------
        # Partial Columns
        #-------------------------

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

        # fig, ax  = plt.subplots(figsize=(6,6))

        # ax.plot(lonsage, latsage, 'k.' , markersize=1)
        # ax.plot(lonsage2, latsage2, 'k.', color='blue', markersize=5)
        # ax.plot(lonfts, latfts, 'r^', markersize=5)
        #     #ax.plot(O3_prf_conc[i,:]/1e13,altitude[i,:], 'k.')

        # #ax.fill_betweenx(alt,Prfmean-prfSTD,Prfmean+prfSTD,alpha=0.5,color='0.75')
        # ax.set_title('Track - SAGE III/ISS - {} - {}'.format(dtsage[0], dtsage[-1]), fontsize=14)
        # ax.set_ylabel('Latitude', fontsize=14)
        # ax.set_xlabel('Longitude', fontsize=14)
        # ax.set_xlim(xmin=-200, xmax=200)
        # ax.set_ylim(ymin=-80, ymax=80)
        # ax.grid(True,which='both')
        # ax.tick_params(labelsize=14)

        # fig.subplots_adjust(left=0.12, bottom=0.1,top=0.94, right=0.95)

        # plt.show(block=False)

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

        fig, ax = plt.subplots(figsize=(10, 6))

        m = Basemap(projection='cyl',
                    resolution='c',
                    llcrnrlat=-90,
                    urcrnrlat=90,
                    llcrnrlon=-180,
                    urcrnrlon=180,
                    lat_0=45,
                    lon_0=-20)

        xfts, yfts = m(lonfts, latfts)
        ax.plot(xfts, yfts, 'r^', markersize=10)

        m.drawcoastlines(color='black')
        m.drawcountries(color='lightgrey')

        m.drawmeridians(np.arange(0, 360, 60),
                        linewidth=.2,
                        labels=[1, 0, 0, 1],
                        labelstyle='+/-',
                        color='grey')
        m.drawparallels(np.arange(-90, 90, 30),
                        linewidth=.2,
                        labels=[1, 0, 0, 1],
                        labelstyle='+/-',
                        color='grey')

        x, y = m(lonsage, latsage)
        ax.plot(x, y, '.k', markersize=5)

        ax.set_title('Track - SAGE III/ISS - {} - {}'.format(
            datesage[0], datesage[-1]),
                     fontsize=14)

        if saveFlg:
            pdfsav.savefig(fig, dpi=200)
            #plt.savefig(pltDir+'Time_Series_mnth.pdf', bbox_inches='tight')
        else:
            plt.show(block=False)

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

        fig, ax = plt.subplots(figsize=(10, 6))

        if loc.lower() == 'mlo':

            m = Basemap(llcrnrlat=14,
                        urcrnrlat=26,
                        llcrnrlon=-166,
                        urcrnrlon=-147,
                        rsphere=(6378137.00, 6356752.3142),
                        resolution='l',
                        area_thresh=1000.,
                        projection='lcc',
                        lat_1=20,
                        lon_0=-156)

        elif loc.lower() == 'fl0':

            m = Basemap(llcrnrlat=35,
                        urcrnrlat=45,
                        llcrnrlon=-115,
                        urcrnrlon=-95,
                        rsphere=(6378137.00, 6356752.3142),
                        resolution='l',
                        area_thresh=1000.,
                        projection='lcc',
                        lat_1=40,
                        lon_0=-105)

        xfts, yfts = m(lonfts, latfts)

        m.drawcoastlines(color='black')
        m.drawcountries(color='lightgrey')

        m.drawstates(color='gray')

        m.drawparallels(np.arange(10., 40., 2),
                        labels=[1, 0, 0, 0],
                        alpha=0.25)
        m.drawmeridians(np.arange(-180., 181., 2),
                        labels=[0, 0, 0, 1],
                        alpha=0.25)

        ax.plot(xfts, yfts, 'r^', markersize=10, label='MLO')

        x, y = m(lonsage, latsage)
        ax.plot(x, y, '.k', markersize=10)

        #x, y = m(lonsage2, latsage2)
        #ax.plot(x,y,'.b', markersize=10, label='< Distance')

        x, y = m(lonsage3, latsage3)
        ax.plot(x, y, '.r', markersize=10., label='Coincident Dates')

        ax.set_title('Track - Zoom In - SAGE III/ISS - {} - {}'.format(
            datesage[0], datesage[-1]),
                     fontsize=14)

        if saveFlg:
            pdfsav.savefig(fig, dpi=200)
            #plt.savefig(pltDir+'Time_Series_mnth.pdf', bbox_inches='tight')
        else:
            plt.show(block=False)

        #-------------------------
        #
        #-------------------------
        clmap = 'jet'
        npanels = int(math.ceil(len(dates_daily) / 3.0))

        #-------------------------
        fig = plt.figure(figsize=(14, 14))
        outer_grid = gridspec.GridSpec(npanels, 3, wspace=0.15, hspace=0.125)

        for i in range(len(dates_daily)):
            #----------------------------
            #
            #----------------------------
            ax = plt.Subplot(fig, outer_grid[i])

            p1, = ax.plot(prftfts_daily[i, :],
                          altfts,
                          linewidth=2.5,
                          label='ret-FTIR',
                          color='b',
                          zorder=1)
            ax.scatter(prftfts_daily[i, :],
                       altfts,
                       facecolors='white',
                       s=10,
                       color='b',
                       zorder=2)
            ax.text(0.8,
                    0.92,
                    dates_daily[i],
                    va='center',
                    transform=ax.transAxes,
                    fontsize=9)
            #ax.errorbar(dates_mnth[g], vmr_mnth[g], fmt='o', yerr=vmr_std_mnth[g] ,markersize=6, color='red', ecolor='red', elinewidth=1.5, zorder=1)

            p2, = ax.plot(aprftfts_daily[i, :],
                          altfts,
                          linewidth=2.5,
                          label='apr-FTIR',
                          color='gray',
                          zorder=1)

            p3, = ax.plot(prfsage[i, :],
                          altsage[i, :],
                          color='green',
                          linewidth=2.5,
                          zorder=3,
                          label='SAGEIII-ISS')
            ax.scatter(prfsage[i, :],
                       altsage[i, :],
                       color='green',
                       s=10,
                       facecolors='white',
                       zorder=4)

            p4, = ax.plot(prfsage_inte[i, :],
                          altfts,
                          color='red',
                          linewidth=2.5,
                          zorder=3,
                          label='SAGEIII-ISS - interpolated')
            ax.scatter(prfsage_inte[i, :],
                       altfts,
                       color='red',
                       s=10,
                       facecolors='white',
                       zorder=4)

            p5, = ax.plot(prfsage_smth[i, :],
                          altfts,
                          color='orange',
                          linewidth=2.5,
                          zorder=3,
                          label='SAGEIII-ISS - smoothed')
            ax.scatter(prfsage_smth[i, :],
                       altfts,
                       color='orange',
                       s=10,
                       facecolors='white',
                       zorder=4)

            # Create a legend for the first line.
            if i == 0:
                first_legend = fig.legend(handles=[p1, p2, p3, p4, p5],
                                          prop={'size': 11},
                                          loc='center left',
                                          bbox_to_anchor=(0.7, 0.15))

            ax.grid(True, alpha=0.35)
            ax.tick_params(labelsize=14)
            ax.grid(True, which='both', alpha=0.35)
            #ax.text(0.03, 0.9, gasStr, va='center',transform=ax.transAxes,fontsize=24)
            ax.set_xlim(xmin=0)
            ax.set_ylim(ymin=10, ymax=50)

            fig.add_subplot(ax)

        all_axes = fig.get_axes()

        #show only the outside spines
        for ax in all_axes:
            for sp in ax.spines.values():
                sp.set_visible(False)
                plt.setp(ax.get_xticklabels(), visible=False)
                ax.spines['top'].set_visible(True)
                ax.spines['left'].set_visible(True)
                ax.spines['right'].set_visible(True)
                ax.spines['bottom'].set_visible(True)
                if ax.is_last_row():
                    ax.spines['bottom'].set_visible(True)
                    plt.setp(ax.get_xticklabels(), visible=True)

        fig.text(0.02,
                 0.5,
                 'Altitude [km]',
                 fontsize=18,
                 va='center',
                 rotation='vertical')
        fig.text(0.5,
                 0.02,
                 'Concentration [molec/cm$^{3}$]',
                 fontsize=18,
                 ha='center',
                 rotation='horizontal')

        fig.subplots_adjust(bottom=0.075, top=0.95, left=0.095, right=0.95)

        if saveFlg:
            pdfsav.savefig(fig, dpi=200)
            #plt.savefig(pltDir+'Time_Series_mnth.pdf', bbox_inches='tight')
        else:
            plt.show(block=False)

        #-------------------------
        fig = plt.figure(figsize=(14, 14))
        outer_grid = gridspec.GridSpec(npanels, 3, wspace=0.15, hspace=0.125)

        for i in range(len(dates_daily)):
            #----------------------------
            #
            #----------------------------
            ax = plt.Subplot(fig, outer_grid[i])

            p1, = ax.plot(prfsage_res[i, :],
                          altfts,
                          linewidth=2.5,
                          label='FTIR - SAGEIII interpolated',
                          color='red',
                          zorder=1)
            ax.scatter(prfsage_res[i, :],
                       altfts,
                       facecolors='white',
                       s=10,
                       color='red',
                       zorder=2)

            ax.text(0.8,
                    0.92,
                    dates_daily[i],
                    va='center',
                    transform=ax.transAxes,
                    fontsize=9)
            #ax.errorbar(dates_mnth[g], vmr_mnth[g], fmt='o', yerr=vmr_std_mnth[g] ,markersize=6, color='red', ecolor='red', elinewidth=1.5, zorder=1)

            p2, = ax.plot(prfsage_res2[i, :],
                          altfts,
                          linewidth=2.5,
                          label='FTIR - SAGEIII smoothed',
                          color='orange',
                          zorder=1)
            ax.scatter(prfsage_res2[i, :],
                       altfts,
                       facecolors='white',
                       s=10,
                       color='orange',
                       zorder=2)

            #ax.axvline(x = 0., color='k', linestyle='--')

            # Create a legend for the first line.
            if i == 0:
                first_legend = fig.legend(handles=[p1, p2],
                                          prop={'size': 11},
                                          loc='center left',
                                          bbox_to_anchor=(0.7, 0.2))

            ax.grid(True, alpha=0.35)
            ax.tick_params(labelsize=14)
            ax.grid(True, which='both', alpha=0.35)
            #ax.text(0.03, 0.9, gasStr, va='center',transform=ax.transAxes,fontsize=24)
            ax.set_xlim(-1.5e12, 1.5e12)
            ax.set_ylim(ymin=10, ymax=50)

            fig.add_subplot(ax)

        all_axes = fig.get_axes()

        #show only the outside spines
        for ax in all_axes:
            for sp in ax.spines.values():
                sp.set_visible(False)
                plt.setp(ax.get_xticklabels(), visible=False)
                ax.spines['top'].set_visible(True)
                ax.spines['left'].set_visible(True)
                ax.spines['right'].set_visible(True)
                ax.spines['bottom'].set_visible(True)
                if ax.is_last_row():
                    ax.spines['bottom'].set_visible(True)
                    plt.setp(ax.get_xticklabels(), visible=True)

        fig.text(0.02,
                 0.5,
                 'Altitude [km]',
                 fontsize=18,
                 va='center',
                 rotation='vertical')
        fig.text(0.5,
                 0.02,
                 'Difference (FTIR - SAGE III) [molec/cm$^{3}$]',
                 fontsize=18,
                 ha='center',
                 rotation='horizontal')

        fig.subplots_adjust(bottom=0.075, top=0.95, left=0.095, right=0.95)

        if saveFlg:
            pdfsav.savefig(fig, dpi=200)
            #plt.savefig(pltDir+'Time_Series_mnth.pdf', bbox_inches='tight')
        else:
            plt.show(block=False)
        #-------------------------
        #-------------------------

        fig = plt.figure(figsize=(14, 14))
        outer_grid = gridspec.GridSpec(npanels, 3, wspace=0.15, hspace=0.125)

        for i in range(len(dates_daily)):
            #----------------------------
            #
            #----------------------------
            ax = plt.Subplot(fig, outer_grid[i])

            p1, = ax.plot(prfsage_res_rel[i, :],
                          altfts,
                          linewidth=2.5,
                          label='FTIR - SAGEIII interpolated',
                          color='red',
                          zorder=1)
            ax.scatter(prfsage_res_rel[i, :],
                       altfts,
                       facecolors='white',
                       s=10,
                       color='red',
                       zorder=2)
            ax.text(0.8,
                    0.92,
                    dates_daily[i],
                    va='center',
                    transform=ax.transAxes,
                    fontsize=9)
            #ax.errorbar(dates_mnth[g], vmr_mnth[g], fmt='o', yerr=vmr_std_mnth[g] ,markersize=6, color='red', ecolor='red', elinewidth=1.5, zorder=1)

            p2, = ax.plot(prfsage_res_rel2[i, :],
                          altfts,
                          linewidth=2.5,
                          label='FTIR - SAGEIII smoothed',
                          color='orange',
                          zorder=1)
            ax.scatter(prfsage_res_rel2[i, :],
                       altfts,
                       facecolors='white',
                       s=10,
                       color='orange',
                       zorder=2)

            #p2, = ax.plot(prfsage_resApr_rel[i, :], altfts, linewidth=2.5, label='aFTIR', color='gray', zorder=1)

            ax.axvline(x=0., color='k', linestyle='--')

            # Create a legend for the first line.
            if i == 0:
                first_legend = fig.legend(handles=[p1, p2],
                                          prop={'size': 12},
                                          loc='center left',
                                          bbox_to_anchor=(0.7, 0.2))

            ax.grid(True, alpha=0.35)
            ax.tick_params(labelsize=14)
            ax.grid(True, which='both', alpha=0.35)
            #ax.text(0.03, 0.9, gasStr, va='center',transform=ax.transAxes,fontsize=24)
            ax.set_xlim(-150, 150)
            ax.set_ylim(ymin=10, ymax=50)

            fig.add_subplot(ax)

        all_axes = fig.get_axes()

        #show only the outside spines
        for ax in all_axes:
            for sp in ax.spines.values():
                sp.set_visible(False)
                plt.setp(ax.get_xticklabels(), visible=False)
                ax.spines['top'].set_visible(True)
                ax.spines['left'].set_visible(True)
                ax.spines['right'].set_visible(True)
                ax.spines['bottom'].set_visible(True)
                if ax.is_last_row():
                    ax.spines['bottom'].set_visible(True)
                    plt.setp(ax.get_xticklabels(), visible=True)

        fig.text(0.02,
                 0.5,
                 'Altitude [km]',
                 fontsize=18,
                 va='center',
                 rotation='vertical')
        fig.text(0.5,
                 0.02,
                 'Relative Difference (FTIR - SAGEIII / FTIR) [%]',
                 fontsize=18,
                 ha='center',
                 rotation='horizontal')

        fig.subplots_adjust(bottom=0.075, top=0.95, left=0.095, right=0.95)

        if saveFlg:
            pdfsav.savefig(fig, dpi=200)
            #plt.savefig(pltDir+'Time_Series_mnth.pdf', bbox_inches='tight')
        else:
            plt.show(block=False)
        #-------------------------

        if len(datesfts) > 1:

            fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(10, 7))

            prf_relMean = np.nanmean(prfsage_res_rel, axis=0)
            prf_relStd = np.nanmean(prfsage_res_rel, axis=0)

            prf_relMean2 = np.nanmean(prfsage_res_rel2, axis=0)
            prf_relStd2 = np.nanmean(prfsage_res_rel2, axis=0)

            #ax1.plot(prf_relMean,altfts,color='k')
            #ax1.fill_betweenx(altfts,prf_relMean-prf_relStd, prf_relMean+prf_relStd,alpha=0.5,color='0.75')

            ax1.plot(prf_relMean, altfts, color='k')
            ax1.errorbar(prf_relMean,
                         altfts,
                         yerr=altfts * 0,
                         xerr=prf_relStd,
                         fmt='o',
                         markersize=6,
                         ecolor='grey',
                         color='k',
                         elinewidth=1.5,
                         zorder=1)
            ax1.scatter(prf_relMean,
                        altfts,
                        color='k',
                        s=45,
                        facecolors='white',
                        linewidths=2,
                        zorder=2)

            #ax2.plot(prf_relMean2,altfts,color='k')
            #ax2.fill_betweenx(altfts,prf_relMean2-prf_relStd2, prf_relMean2+prf_relStd2,alpha=0.5,color='0.75')

            ax2.plot(prf_relMean2, altfts, color='k')
            ax2.errorbar(prf_relMean2,
                         altfts,
                         yerr=altfts * 0,
                         xerr=prf_relStd2,
                         fmt='o',
                         markersize=6,
                         ecolor='grey',
                         color='k',
                         elinewidth=1.5,
                         zorder=1)
            ax2.scatter(prf_relMean2,
                        altfts,
                        color='k',
                        s=45,
                        facecolors='white',
                        linewidths=2,
                        zorder=2)

            ax1.grid(True, which='both')
            ax2.grid(True, which='both')

            ax1.set_title(
                'Relative difference (Mean +/- std)\nSAGE III interpolated')
            ax2.set_title(
                'Relative difference (Mean +/- std)\nSAGE III smoothed')

            ax1.set_ylabel('Altitude [km]', fontsize=14)
            ax1.set_xlabel('Rel Difference (FTIR - SAGEIII / FTIR) [%]',
                           fontsize=13)
            ax2.set_xlabel('Rel Difference (FTIR - SAGEIII / FTIR) [%]',
                           fontsize=13)

            ax1.tick_params(labelsize=14)
            ax2.tick_params(labelsize=14)

            ax1.set_xlim(-150, 150)
            ax2.set_xlim(-150, 150)

            ax1.set_ylim(ymin=10, ymax=50)
            ax2.set_ylim(ymin=10, ymax=50)

            if saveFlg:
                pdfsav.savefig(fig, dpi=200)
                #plt.savefig(pltDir+'Time_Series_mnth.pdf', bbox_inches='tight')
            else:
                plt.show(block=False)

        #---------------------------------
        # Plot : Averaging Kernel Smoothing Function (row of avk)
        #---------------------------------

        avkSCFftsMean

        clmap = 'jet'
        cm = plt.get_cmap(clmap)
        fig = plt.figure(figsize=(10, 7))
        gs = gridspec.GridSpec(1, 3, width_ratios=[3, 1, 1])
        ax = plt.subplot(gs[0])
        axb = plt.subplot(gs[1])
        axc = plt.subplot(gs[2])
        cm = plt.get_cmap(clmap)
        cNorm = colors.Normalize(vmin=np.min(altfts), vmax=np.max(altfts))
        scalarMap = mplcm.ScalarMappable(norm=cNorm, cmap=clmap)
        scalarMap.set_array(altfts)

        #---------------------------------
        ax.set_color_cycle([scalarMap.to_rgba(x) for x in altfts])

        for i in range(len(altfts)):
            ax.plot(avkSCFftsMean[i, :], altfts)

        ax.set_ylabel('Altitude [km]', fontsize=14)
        ax.set_xlabel('AK', fontsize=14)
        #ax.grid(True, alpha=0.5)
        #ax.set_title('(a)', loc='left', fontsize=14)
        #ax.text(0.025, 0.95,'(a)', fontsize=16,transform=ax.transAxes)

        cbaxes = fig.add_axes([0.4, 0.55, 0.02, 0.4])
        cbar = fig.colorbar(scalarMap, orientation='vertical', cax=cbaxes)
        cbar.set_label('Altitude [km]', fontsize=14)
        #ax.set_title('H2O Averaging Kernels Scale Factor', fontsize=14)
        ax.tick_params(labelsize=14)
        ax.axvline(x=0, color='k', linestyle='--')
        ax.set_ylim((0, 60))

        #----------------------------------------
        # Calculate total column averaging kernel
        #----------------------------------------
        nobs = len(datesfts)
        nlyrs = len(altfts)
        avkTC = np.zeros((nobs, nlyrs))

        for i in range(0, nobs):
            AirMtemp = np.squeeze(Airmassfts[i, :])
            akTemp = np.squeeze(avkSCFfts[i, :, :])
            AirMinv = np.diag(1.0 / AirMtemp)
            avkTC[i, :] = np.dot(np.dot(AirMtemp, akTemp), AirMinv)

        avkTCAv = np.mean(avkTC, axis=0)

        #---------------------------------
        #axb.plot(np.sum(avkSCFav[gasVer],axis=0), alt[gasVer],color='k')
        axb.plot(avkTCAv, altfts, color='k')

        #axb.grid(True,  alpha=0.5)
        axb.set_xlabel('Total Column AK', fontsize=14)
        axb.tick_params(labelsize=14)
        #axb.set_title('(b)', loc='left', fontsize=14)
        #axb.text(0.725, 0.95,'(b)', fontsize=16,transform=axb.transAxes)

        major_ticks = np.arange(0, 3, 1)
        axb.set_xticks(major_ticks)
        axb.set_ylim((0, 60))

        #---------------------------------
        dofs_cs = np.cumsum(np.diag(avkSCFftsMean)[::-1])[::-1]
        axc.plot(dofs_cs,
                 altfts,
                 color='k',
                 label='Cumulative Sum of DOFS (starting at surface)')
        #axc.plot(np.sum(avkSCFav[gasVer],axis=0), alt[gasVer],color='k')
        xval = range(0, int(np.ceil(max(dofs_cs))) + 2)

        if pColsFlg:

            for pcol in pCols:
                ind1 = mf.nearestind(pcol[0], altfts)
                ind2 = mf.nearestind(pcol[1], altfts)
                axc.fill_between(xval,
                                 altfts[ind1],
                                 altfts[ind2],
                                 alpha=0.5,
                                 color='0.75')
                axc.axhline(altfts[ind2], color='k', linestyle='--')
                dofsPcol = dofs_cs[ind2] - dofs_cs[ind1]
                axc.text(0.15, (altfts[ind1] + altfts[ind2]) / 2.0,
                         'DOFs = {2:.3f}'.format(altfts[ind1], altfts[ind2],
                                                 dofsPcol),
                         fontsize=9)

        #ind1         = mf.nearestind(Pcol[0], alt[gasVer])
        #ind2         = mf.nearestind(Pcol[1], alt[gasVer])

        #axc.fill_between(xval,alt[gasVer][ind1],alt[gasVer][ind2],alpha=0.5,color='0.75')
        #axc.axhline(alt[gasVer][ind2],color='k',linestyle='--')
        #dofsPcol = dofs_cs[ind2] - dofs_cs[ind1]
        #axc.text(0.15,(alt[idhdf][ind1]+alt[idhdf][ind2])/2.0,
        #         'DOFs for layer {0:.2f}-{1:.2f}[km] = {2:.3f}'.format(alt[idhdf][ind1],alt[idhdf][ind2],dofsPcol),
        #         fontsize=9)
        #axc.set_title('DOFs Profile - ' +str(pltID[i]))
        #axc.set_ylabel('Altitude [km]')

        axc.set_xlabel('Cumulative\nSum of DOFS', fontsize=14)
        axc.tick_params(labelsize=14)
        #axc.set_title('(c)', loc='left', fontsize=14)
        #axc.text(0.025, 0.95,'(c)', fontsize=16,transform=axc.transAxes)
        #axc.set_title('DOFs for layer {0:.1f}-{1:.1f}[km] = {2:.2f}'.format(alt[idhdf][ind1],alt[idhdf][ind2],dofsPcol), fontsize=9)

        major_ticks = np.arange(0, 6, 1)
        axc.set_xticks(major_ticks)

        axc.set_ylim((0, 60))
        #axc.grid(True,which='both',  alpha=0.5)

        plt.tight_layout()

        if saveFlg:
            pdfsav.savefig(fig, dpi=200)
            #plt.savefig(pltDir+'Time_Series_mnth.pdf', bbox_inches='tight')
        else:
            plt.show(block=False)

        #---------------------------------
        #
        #---------------------------------
        if pColsFlg:

            one2one = np.arange(0, 1e14, 1e12)

            fig, ax = plt.subplots(len(pCols), figsize=(7, 12))

            for i, pcol in enumerate(pCols):

                inds = np.where((altfts >= pcol[0]) & (altfts <= pcol[1]))[0]

                TCpfts = np.nansum(prftfts_daily3[:, inds],
                                   axis=1)  #*thick[indsalt][inds]
                TCpsage_smth = np.nansum(prfsage_smth[:, inds],
                                         axis=1)  #*thick[indsalt][inds]
                TCpsage_inte = np.nansum(prfsage_inte2[:, inds],
                                         axis=1)  #*thick[indsalt][inds]

                #ax[0].errorbar(mnthVals['dates'],mnthVals['mnthlyAvg'], linestyle='None', yerr=mnthVals['std'],ecolor=clr[i-1])
                ax[i].scatter(TCpfts,
                              TCpsage_inte,
                              color='red',
                              s=35,
                              label='SAGEIII-ISS')
                ax[i].scatter(TCpfts,
                              TCpsage_smth,
                              color='green',
                              s=35,
                              label='SAGEIII-ISS - smoothed')

                if i == 0: ax[i].legend(prop={'size': 11})
                ax[i].grid(True)
                ax[i].set_ylabel('Partial Column - SAGE III \n[molec/cm$^2$]',
                                 fontsize=14)
                if i == len(pCols) - 1:
                    ax[i].set_xlabel('Partial Column - FTIR [molec/cm$^2$]',
                                     fontsize=14)
                ax[i].set_xlim(
                    np.min(TCpsage_smth) - np.min(TCpsage_smth) * 0.2,
                    np.max(TCpsage_smth) + np.max(TCpsage_smth) * 0.2)
                ax[i].set_ylim(
                    np.min(TCpsage_smth) - np.min(TCpsage_smth) * 0.2,
                    np.max(TCpsage_smth) + np.max(TCpsage_smth) * 0.2)

                ax[i].set_title('Altitude Layer ' + str(altfts[inds][-1]) +
                                ' - ' + str(altfts[inds][0]) + ' km',
                                multialignment='center',
                                fontsize=14)

                ax[i].plot(one2one,
                           one2one,
                           ls='--',
                           color='gray',
                           linewidth=2)
                ax[i].plot(one2one,
                           one2one * 0.8,
                           ls='--',
                           color='gray',
                           linewidth=2)
                ax[i].plot(one2one * 0.8,
                           one2one,
                           ls='--',
                           color='gray',
                           linewidth=2)

                ax[i].tick_params(labelsize=14)

                print 'Altitude Layer ' + str(altfts[inds][-1]) + ' - ' + str(
                    altfts[inds][0]) + ' km'
                print 'Mean +/- std - SAGEIII: {} +/- {}'.format(
                    np.nanmean(TCpsage_smth), np.nanstd(TCpsage_smth))
                print 'Mean +/- std - FTS:     {} +/- {}'.format(
                    np.nanmean(TCpfts), np.nanstd(TCpfts))

                slopelr, interceptlr, r_valueln, p_valuelr, std_errlr = stats.linregress(
                    TCpfts, TCpsage_inte)
                print slopelr, interceptlr, r_valueln, p_valuelr

                ax[i].text(0.06,
                           0.9,
                           'Coeff Correlation r = {:.3f}'.format(
                               float(r_valueln)),
                           color='red',
                           va='center',
                           transform=ax[i].transAxes,
                           fontsize=16)

                slopelr, interceptlr, r_valueln, p_valuelr, std_errlr = stats.linregress(
                    TCpfts, TCpsage_smth)
                print slopelr, interceptlr, r_valueln, p_valuelr

                ax[i].text(0.06,
                           0.8,
                           'Coeff Correlation r = {:.3f}'.format(
                               float(r_valueln)),
                           color='green',
                           va='center',
                           transform=ax[i].transAxes,
                           fontsize=16)

        fig.subplots_adjust(bottom=0.08, top=0.95, left=0.15, right=0.95)

        if saveFlg:
            pdfsav.savefig(fig, dpi=200)
            #plt.savefig(pltDir+'Time_Series_mnth.pdf', bbox_inches='tight')
        else:
            plt.show(block=False)

        if saveFlg: pdfsav.close()
        else:
            user_input = raw_input('Press any key to exit >>> ')
            sys.exit()

    #print('\nFinished Plots.......\n')

    #--------------------------------
    # Pause so user can look at plots
    #--------------------------------
    if pltsage:
        user_input = raw_input('Press any key to exit >>> ')
        sys.exit()  # Exit program
예제 #4
0
    def PlotClySet(self):
        #----------------------------------------
        #Define global interes
        #----------------------------------------
        loi = 45  #Ltitude of interest
        poi = 10.  #Ppressure of interest
        y4trend = 1997  #initial year if trends

        #----------------------------------------
        #           GOZCARDS (MERGE OF HALOE, ACE, MLS)
        #----------------------------------------

        if self.ReadNCMrgFlg:

            time = np.asarray(self.Mrg['time'])
            Prfs = np.asarray(self.Mrg['average']) * 1e9
            lev = np.asarray(self.Mrg['lev'])
            Latitude = np.asarray(self.Mrg['lat'])
            std_dev = np.asarray(self.Mrg['std_dev']) * 1e9

            Nfiles = time.shape[0]
            NMonths = time.shape[1]
            Nlat = Latitude.shape[1]
            Nlev = lev.shape[1]

            print '\nN GOZCARDS files = ', Nfiles
            print 'N GOZCARDS Months = ', NMonths
            print 'N GOZCARDS Latitudes = ', Nlat
            print 'N GOZCARDS Levels = ', Nlev

            Dates = np.asarray(
                [[dt.date(1950, 1, 1) + dt.timedelta(days=int(t)) for t in tt]
                 for tt in time])

            DatesMrg = np.reshape(Dates, (Nfiles * NMonths))
            PrfsMrg = np.reshape(Prfs, (Nfiles * NMonths, Nlev, Nlat))
            PresMrg = lev[0, :]
            LatMrg = Latitude[0, :]
            StDMrg = np.reshape(std_dev, (Nfiles * NMonths, Nlev, Nlat))

            #----------------------------------------
            #DELETE POINTS IN THE pressure and latitude of interest (Important because some values are negatives)
            #----------------------------------------
            indMrg1 = mf.nearestind(poi, PresMrg)
            indLatMrg = mf.nearestind(loi, LatMrg)
            indsBad = np.where(PrfsMrg[:, indMrg1, indLatMrg] < 0.0)[0]

            print 'N points to remove in GOZCARDS = {}'.format(len(indsBad))
            PrfsMrg = np.delete(PrfsMrg, indsBad, axis=0)
            StDMrg = np.delete(StDMrg, indsBad, axis=0)
            DatesMrg = np.delete(DatesMrg, indsBad, axis=0)

            #----------------------------------------
            #           Read AGAGE
            #----------------------------------------

        if self.ReadAgaFlg:
            #----------------------------------------
            #DEFINE VARIABLES
            #----------------------------------------
            Nsites = len(self.AGA['site'])
            s = self.AGA['site']
            Lat = np.asarray(self.AGA['lat'])

            indLatAGA = mf.nearestind(53., Lat)  ## This is to read MACE, HEAD
            ##indLatAGA = mf.nearestind(loi, Lat)  ## This is to read Closest to loi

            print '\nAGAGE site for Plot: {}, --> Latitude: {}'.format(
                self.AGA['site'][indLatAGA], Lat[indLatAGA])

            DatesAGA = np.asarray(self.AGA[s[indLatAGA] + 'Dates'][0])

            if self.AgagefileID == 0:

                CFC11 = np.asarray(self.AGA[s[indLatAGA] + 'CFC11'][0])
                CFC11_e = np.asarray(self.AGA[s[indLatAGA] + 'CFC11_e'][0])

                CFC12 = np.asarray(self.AGA[s[indLatAGA] + 'CFC12'][0])
                CFC12_e = np.asarray(self.AGA[s[indLatAGA] + 'CFC12_e'][0])

                CCl4 = np.asarray(self.AGA[s[indLatAGA] + 'CCl4'][0])
                CCl4_e = np.asarray(self.AGA[s[indLatAGA] + 'CCl4_e'][0])

                CFC113 = np.asarray(self.AGA[s[indLatAGA] + 'CFC113'][0])
                CFC113_e = np.asarray(self.AGA[s[indLatAGA] + 'CFC113_e'][0])

                ind = np.where((CFC11 > 0.) & (CFC12 > 0.) & (CCl4 > 0.))[0]

                Cl = CFC11[ind] + CFC12[ind] + CCl4[ind]
                Cl_e = np.sqrt(CFC11_e[ind]**2 + CFC12_e[ind]**2 +
                               CCl4_e[ind]**2)
                DatesAGA = DatesAGA[ind]

            elif self.AgagefileID == 1:

                Cl = np.asarray(self.AGA[s[indLatAGA] + 'Cly'][0])

                #----------------------------------------
                #           Read JFJ
                #----------------------------------------

        f = open(self.jfjFile, 'r')

        HCl_Date_JFJ = []
        HCl_JFJ = []
        CLONO2_Date_JFJ = []
        CLONO2_JFJ = []

        Cl_Date_JFJ = []
        Cl_JFJ = []

        header1 = f.readline()

        for line in f:
            columns = line.split()
            atime = float(columns[0])

            da_i = mf.t2dt(atime)
            if da_i.year >= 1983:
                HCl_Date_JFJ.append(da_i)
                HCl_JFJ.append(float(columns[1]) / 1e15)

            if len(columns) >= 3:

                atime = float(columns[2])
                da_i = mf.t2dt(atime)
                if da_i.year >= 1983:
                    CLONO2_Date_JFJ.append(da_i)
                    CLONO2_JFJ.append(float(columns[3]) / 1e15)

            if len(columns) >= 5:
                atime = float(columns[4])
                da_i = mf.t2dt(atime)
                if da_i.year >= 1983:
                    Cl_Date_JFJ.append(da_i)
                    Cl_JFJ.append(float(columns[5]) / 1e15)

        HCl_Date_JFJ = np.asarray(HCl_Date_JFJ)
        HCl_JFJ = np.asarray(HCl_JFJ)

        Cl_JFJ = np.asarray(Cl_JFJ)
        Cl_Date_JFJ = np.asarray(Cl_Date_JFJ)

        #----------------------------------------
        #           Read MLO
        #----------------------------------------

        f = open(self.mloFile, 'r')

        HCl_Date_MLO = []
        HCl_MLO = []

        headers = [f.readline() for i in range(7)]

        for line in f:

            columns = line.split(',')

            yyyy = int(columns[1].strip()[0:4])
            mm = int(columns[1].strip()[5:7])
            dd = int(columns[1].strip()[8:10])

            hh = int(columns[2].strip()[0:2])
            mi = int(columns[2].strip()[3:5])
            se = int(columns[2].strip()[6:8])

            da = dt.datetime(yyyy, mm, dd, hh, mi, se)

            HCl_Date_MLO.append(da)
            HCl_MLO.append(float(columns[3].strip()))

        HCl_MLO = np.asarray(HCl_MLO) / 1e15
        HCl_Date_MLO = np.asarray(HCl_Date_MLO)

        Avg = mf.mnthlyAvg(HCl_MLO, HCl_Date_MLO, dateAxis=1)
        HCl_Date_MLO = Avg['dates']
        HCl_MLO = Avg['mnthlyAvg']
        HCl_std_MLO = Avg['std']

        #----------------------------------------------------------------------------
        #
        #----------------------------------------------------------------------------
        years = [singDate.year
                 for singDate in DatesMrg]  # Find years for all date entries
        if len(list(set(years))) > 1:
            yrsFlg = True  # Determine all unique years
        else:
            yrsFlg = False

        yearsLc = YearLocator(2)
        monthsAll = MonthLocator()
        #months       = MonthLocator()
        months = MonthLocator(bymonth=1, bymonthday=1)
        if yrsFlg: DateFmt = DateFormatter('%Y')
        else: DateFmt = DateFormatter('%m\n%Y')

        #----------------------------------------------------------------------------
        #
        #----------------------------------------------------------------------------
        indLatMrg = mf.nearestind(loi, LatMrg)

        #-----------------------------------------
        #             PLOT (ONE PANEL, THREE Y-AXIS)
        #-----------------------------------------
        fig, ax = plt.subplots(figsize=(12, 7), sharex=True)

        axes = [ax, ax.twinx(), ax.twinx()]

        axes[-1].spines['right'].set_position(('axes', 1.125))
        axes[-1].set_frame_on(True)
        axes[-1].patch.set_visible(False)

        #-----------------------------------------
        #PLOT GOZCARDS
        #-----------------------------------------
        if self.ReadHDFMLSFlg: ind1MLS = mf.nearestind(poi, PresMLS)
        if self.ReadNCMrgFlg: indMrg1 = mf.nearestind(poi, PresMrg)

        indsGood = np.where(PrfsMrg[:, indMrg1, indLatMrg] > 0.0)[0]

        PrfsMrg2 = PrfsMrg[indsGood, indMrg1, indLatMrg]
        StDMrg2 = StDMrg[indsGood, indMrg1, indLatMrg]
        DatesMrg2 = DatesMrg[indsGood]

        PrfsMrg2_smth = mf.smooth(PrfsMrg2, window_len=24, window='flat')
        axes[0].plot(DatesMrg2, PrfsMrg2_smth, linewidth=5, color='gray')

        #-----------------------------------------
        #GOZCARDS TRENDS ANALYSIS
        #-----------------------------------------
        yearGO = [single.year for single in DatesMrg2]
        yearGO = np.asarray(yearGO)

        inds = np.where(yearGO >= y4trend)[0]

        dateYearFrac = mf.toYearFraction(DatesMrg2[inds])
        weights = np.ones_like(dateYearFrac)
        res = mf.fit_driftfourier(dateYearFrac, PrfsMrg2[inds], weights, 2)
        f_drift, f_fourier, f_driftfourier = res[3:6]

        res_b = mf.cf_driftfourier(dateYearFrac, PrfsMrg2[inds], weights, 2)
        perc, intercept_b, slope_b, pfourier_b = res_b

        print "\nFitted trend GOZCARDS -- slope: {0:.3E} ({1:.3f}%)".format(
            res[1], res[1] / np.nanmean(PrfsMrg2[inds]) * 100.0)
        axes[0].scatter(
            DatesMrg2,
            PrfsMrg2,
            s=20,
            color='gray',
            edgecolors='gray',
            alpha=0.75,
            label=
            'GOZCARDS (10hPa, 40-50$^\circ$ N)\n {0:.2f} $\pm$ {1:.2f} %/yr'.
            format(res[1] / np.nanmean(PrfsMrg2[inds]) * 100.0,
                   np.std(slope_b) / np.mean(PrfsMrg2[inds]) *
                   100.0))  #facecolors='none'
        axes[0].tick_params(which='both', labelsize=14)
        axes[0].set_ylim(1.7, 3.4)
        axes[0].set_ylabel('VMR [ppb], GOZCARDS (HALOE, ACE & MLS) (HCl)',
                           fontsize=14)

        if yrsFlg:
            axes[0].xaxis.set_major_locator(yearsLc)
            axes[0].xaxis.set_minor_locator(months)
            axes[0].xaxis.set_major_formatter(DateFmt)

        else:
            axes[0].xaxis.set_major_locator(monthsAll)
            axes[0].xaxis.set_major_formatter(DateFmt)

        # #-----------------------------------------
        # #SAVE GOZCARDS DATA IN ASCII
        # #-----------------------------------------
        # YYYYMMDD = np.asarray(['{0:4d}-{1:02d}-{2:02d}'.format(DatesMrg2[i].year, DatesMrg2[i].month, DatesMrg2[i].day)    for i,dum in enumerate(DatesMrg2)])

        # with open('/data1/Campaign/Satellite/MLS/GOZCARD_pltCly.ascii','w') as fopen:
        #     fopen.write('#Hannigan, J.W., Ortega, I\n')
        #     fopen.write('#National Center for Atmospheric Research\n')
        #     fopen.write('#GOZCARDS Monthly data (GOZCARDS (10hPa, 40-50deg N)\n')
        #     fopen.write('#GOZCARDS smoothed data is included using a window of 24 (2 years)\n')
        #     fopen.write('#CONTACT_INFO: Hannigan, Jim, [email protected], 303-497-1853, NCAR, 3090 Center Green Drive, Boulder, CO 80301\n')
        #     fopen.write('#CONTACT_INFO: Ortega, Ivan, [email protected], 303-497-1861, NCAR, 3090 Center Green Drive, Boulder, CO 80301\n')

        #     fopen.write('Index, YYYY-MM-DD, HCl [ppb], HCl_smooth [ppb]\n')
        #     strFormat = '{0:d}, {1:>10s},  {2:.3f}, {3:.3f}\n'

        #     for i,sngTime in enumerate(YYYYMMDD):
        #         fopen.write(strFormat.format((i+1),YYYYMMDD[i], PrfsMrg2[i], PrfsMrg2_smth[i]))

        #-----------------------------------------
        #PLOT AGAGE
        #-----------------------------------------
        if self.AgagefileID == 0:
            axes[1].set_ylabel('VMR [ppt], AGAGE (CFC11 + CFC12 + CCl$_4$)',
                               color='green',
                               fontsize=14)
        if self.AgagefileID == 1:
            axes[1].set_ylabel('Mole Fraction [ppb], AGAGE (Total Cl)',
                               color='green',
                               fontsize=14)
        axes[1].tick_params(axis='y', colors='green', labelsize=14)
        if self.AgagefileID == 0: axes[1].set_ylim(380, 1100)
        if self.AgagefileID == 1: axes[1].set_ylim(1, 4.5)

        yearAGA = [single.year for single in DatesAGA]
        yearAGA = np.asarray(yearAGA)

        #-----------------------------------------
        #AGAGE TRENDS ANALYSIS
        #-----------------------------------------
        inds = np.where(yearAGA >= y4trend)[0]

        dateYearFrac = mf.toYearFraction(DatesAGA[inds])
        weights = np.ones_like(dateYearFrac)
        res = mf.fit_driftfourier(dateYearFrac, Cl[inds], weights, 2)
        f_drift, f_fourier, f_driftfourier = res[3:6]

        res_b = mf.cf_driftfourier(dateYearFrac, Cl[inds], weights, 2)
        perc, intercept_b, slope_b, pfourier_b = res_b

        print "Fitted trend AGAGE -- slope: {0:.3E} ({1:.3f}%)".format(
            res[1], res[1] / np.mean(Cl[inds]) * 100.0)
        if self.AgagefileID == 0:
            axes[1].scatter(
                DatesAGA,
                Cl,
                s=20,
                color='green',
                edgecolors='green',
                alpha=0.75,
                label=
                'AGAGE Mace Head, Ireland (53.33$^\circ$ N)\n {0:.2f} $\pm$ {1:.2f} %/yr'
                .format(res[1] / np.nanmean(Cl[inds]) * 100.0,
                        np.std(slope_b) / np.mean(Cl[inds]) *
                        100.0))  #facecolors='none'
        if self.AgagefileID == 1:
            axes[1].scatter(
                DatesAGA,
                Cl,
                s=20,
                color='green',
                edgecolors='green',
                alpha=0.75,
                label=
                'AGAGE Global Mean Tropospheric Total Cl\n {0:.2f} $\pm$ {1:.2f} %/yr'
                .format(res[1] / np.nanmean(Cl[inds]) * 100.0,
                        np.std(slope_b) / np.mean(Cl[inds]) *
                        100.0))  #facecolors='none'

        # #-----------------------------------------
        # #SAVE AGAGE DATA IN ASCII
        # #-----------------------------------------
        #  YYYYMMDD = np.asarray(['{0:4d}-{1:02d}-{2:02d}'.format(DatesAGA[i].year, DatesAGA[i].month, DatesAGA[i].day)    for i,dum in enumerate(DatesAGA)])

        #  with open('/data1/Campaign/Satellite/MLS/AGAGE_pltCly.ascii','w') as fopen:
        #      fopen.write('#Hannigan, J.W., Ortega, I\n')
        #      fopen.write('#National Center for Atmospheric Research\n')
        #      fopen.write('#AGAGE  Monthly data from Mace Head, Ireland (53.33deg N)\n')
        #      fopen.write('#AGAGE  Cly =  CFC11 + CFC12 + CCl4\n')
        #      fopen.write('#CONTACT_INFO: Hannigan, Jim, [email protected], 303-497-1853, NCAR, 3090 Center Green Drive, Boulder, CO 80301\n')
        #      fopen.write('#CONTACT_INFO: Ortega, Ivan, [email protected], 303-497-1861, NCAR, 3090 Center Green Drive, Boulder, CO 80301\n')

        #      fopen.write('Index, YYYY-MM-DD, Cly [ppm]\n')
        #      strFormat = '{0:d}, {1:>10s},  {2:.3f}\n'

        #      for i,sngTime in enumerate(YYYYMMDD):
        #          fopen.write(strFormat.format((i+1),YYYYMMDD[i], Cl[i]))

        #-----------------------------------------
        #PLT JFJ HRFTIR
        #-----------------------------------------

        HCl_JFJ_smth = mf.smooth(HCl_JFJ, window_len=24, window='flat')
        axes[2].plot(HCl_Date_JFJ, HCl_JFJ_smth, linewidth=5, color='blue')

        yearJFJ = [single.year for single in HCl_Date_JFJ]
        yearJFJ = np.asarray(yearJFJ)

        inds = np.where(yearJFJ >= y4trend)[0]

        dateYearFrac = mf.toYearFraction(HCl_Date_JFJ[inds])
        weights = np.ones_like(dateYearFrac)
        res = mf.fit_driftfourier(dateYearFrac, HCl_JFJ[inds], weights, 2)
        f_drift, f_fourier, f_driftfourier = res[3:6]

        res_b = mf.cf_driftfourier(dateYearFrac, HCl_JFJ[inds], weights, 2)
        perc, intercept_b, slope_b, pfourier_b = res_b

        print "Fitted trend JFJ -- slope: {0:.3E} ({1:.3f}%)".format(
            res[1], res[1] / np.mean(HCl_JFJ[inds]) * 100.0)
        axes[2].scatter(
            HCl_Date_JFJ,
            HCl_JFJ,
            s=20,
            color='blue',
            edgecolors='blue',
            alpha=0.75,
            label=
            'Jungfraujoch NDACC FTIR (46.55$^\circ$ N)\n {0:.2f} $\pm$ {1:.2f} %/yr'
            .format(res[1] / np.nanmean(HCl_JFJ[inds]) * 100.0,
                    np.std(slope_b) / np.mean(HCl_JFJ[inds]) * 100.0))

        # #-----------------------------------------
        # #SAVE JFJ DATA IN ASCII
        # #-----------------------------------------
        # YYYYMMDD = np.asarray(['{0:4d}-{1:02d}-{2:02d}'.format(HCl_Date_JFJ[i].year, HCl_Date_JFJ[i].month, HCl_Date_JFJ[i].day)    for i,dum in enumerate(HCl_Date_JFJ)])

        # with open('/data1/Campaign/Satellite/MLS/JFJ_pltCly.ascii','w') as fopen:
        #     fopen.write('#Hannigan, J.W., Ortega, I\n')
        #     fopen.write('#National Center for Atmospheric Research\n')
        #     fopen.write('#Jungfraujoch NDACC FTIR Monthly Data (46.55deg N)\n')
        #     fopen.write('#Jungfraujoch smoothed data is included using a window of 24 (2 years)\n')
        #     fopen.write('#CONTACT_INFO: Hannigan, Jim, [email protected], 303-497-1853, NCAR, 3090 Center Green Drive, Boulder, CO 80301\n')
        #     fopen.write('#CONTACT_INFO: Ortega, Ivan, [email protected], 303-497-1861, NCAR, 3090 Center Green Drive, Boulder, CO 80301\n')

        #     fopen.write('Index, YYYY-MM-DD, HCl [[x10^15 molec/cm^2]], HCl_smooth [[x10$^15 molec/cm^2]]\n')
        #     strFormat = '{0:d}, {1:>10s},  {2:.3f}, {3:.3f}\n'

        #     for i,sngTime in enumerate(YYYYMMDD):
        #         fopen.write(strFormat.format((i+1),YYYYMMDD[i], HCl_JFJ[i], HCl_JFJ_smth[i]))

        #-----------------------------------------
        #PLT MLO HRFTIR
        #-----------------------------------------

        HCl_MLO_smth = mf.smooth(HCl_MLO, window_len=24, window='flat')
        axes[2].plot(HCl_Date_MLO, HCl_MLO_smth, linewidth=5, color='navy')

        yearMLO = [single.year for single in HCl_Date_MLO]
        yearMLO = np.asarray(yearMLO)

        inds = np.where(yearMLO >= y4trend)[0]

        dateYearFrac = mf.toYearFraction(HCl_Date_MLO[inds])
        weights = np.ones_like(dateYearFrac)
        res = mf.fit_driftfourier(dateYearFrac, HCl_MLO[inds], weights, 2)
        f_drift, f_fourier, f_driftfourier = res[3:6]

        res_b = mf.cf_driftfourier(dateYearFrac, HCl_JFJ[inds], weights, 2)
        perc, intercept_b, slope_b, pfourier_b = res_b

        print "Fitted trend MLO -- slope: {0:.3E} ({1:.3f}%)".format(
            res[1], res[1] / np.mean(HCl_MLO[inds]) * 100.0)

        axes[2].scatter(
            HCl_Date_MLO,
            HCl_MLO,
            s=20,
            facecolors='none',
            edgecolors='navy',
            alpha=0.75,
            label=
            'Mauna Loa Observatory NDAC FTIR (19.54$^\circ$ N)\n {0:.2f} $\pm$ {1:.2f} %/yr'
            .format(res[1] / np.nanmean(HCl_MLO[inds]) * 100.0,
                    np.std(slope_b) / np.mean(HCl_MLO[inds]) * 100.0))

        axes[2].set_ylim(1.2, 5.3)
        axes[2].set_xlim(dt.date(1979, 1, 1), dt.date(2017, 12, 31))

        axes[2].set_ylabel(
            'Total Column [x10$^{15}$ molec/cm$^2$], FTIR (HCl)',
            color='blue',
            fontsize=14)
        axes[2].tick_params(axis='y', colors='blue', labelsize=14)

        axes[0].set_xlabel('Year', fontsize=14)

        fig.autofmt_xdate()
        fig.subplots_adjust(left=0.1, bottom=0.125, right=0.8, top=0.94)

        lines, labels = axes[0].get_legend_handles_labels()
        lines2, labels2 = axes[1].get_legend_handles_labels()
        lines3, labels3 = axes[2].get_legend_handles_labels()

        axes[0].legend(lines + lines2 + lines3,
                       labels + labels2 + labels3,
                       prop={'size': 10.5},
                       loc=2,
                       frameon=False,
                       ncol=2)  #'weight':'bold'

        plt.suptitle(
            'NH Inorganic Chlorine 1983 to 2016 (Trends 1997 to 2016)',
            fontsize=16)

        # #-----------------------------------------
        # #SAVE MLO DATA IN ASCII
        # #-----------------------------------------
        # YYYYMMDD = np.asarray(['{0:4d}-{1:02d}-{2:02d}'.format(HCl_Date_MLO[i].year, HCl_Date_MLO[i].month, HCl_Date_MLO[i].day)    for i,dum in enumerate(HCl_Date_MLO)])

        # with open('/data1/Campaign/Satellite/MLS/MLO_pltCly.ascii','w') as fopen:
        #     fopen.write('#Hannigan, J.W., Ortega, I\n')
        #     fopen.write('#National Center for Atmospheric Research\n')
        #     fopen.write('#Mauna Loa Observatory NDAC FTIR Monthly Data (19.54deg N)\n')
        #     fopen.write('#Mauna Loa smoothed data is included using a window of 24 (2 years)\n')
        #     fopen.write('#CONTACT_INFO: Hannigan, Jim, [email protected], 303-497-1853, NCAR, 3090 Center Green Drive, Boulder, CO 80301\n')
        #     fopen.write('#CONTACT_INFO: Ortega, Ivan, [email protected], 303-497-1861, NCAR, 3090 Center Green Drive, Boulder, CO 80301\n')

        #     fopen.write('Index, YYYY-MM-DD, HCl [[x10^15 molec/cm^2]], HCl_smooth [[x10$^15 molec/cm^2]]\n')
        #     strFormat = '{0:d}, {1:>10s},  {2:.3f}, {3:.3f}\n'

        #     for i,sngTime in enumerate(YYYYMMDD):
        #         fopen.write(strFormat.format((i+1),YYYYMMDD[i], HCl_MLO[i], HCl_MLO_smth[i]))

        if self.pdfsav:
            self.pdfsav.savefig(fig, dpi=200)
            if self.AgagefileID == 0:
                plt.savefig('/data1/Campaign/Satellite/MLS/Cly_all_Mace.pdf'
                            )  # bbox_inches='tight'
            if self.AgagefileID == 1:
                plt.savefig('/data1/Campaign/Satellite/MLS/Cly_all_Global.pdf'
                            )  # bbox_inches='tight'
        else:
            plt.draw()
            plt.show(block=False)
            user_input = raw_input('Press any key to exit >>> ')
            sys.exit()
예제 #5
0
def main(argv):

    #----------------
    # Initializations for insitu
    #----------------
    dataDir = '/data1/ancillary_data/fl0/FRAPPE/BAO/'

    #----------------
    # Initializations for FTIR
    #----------------

    loc = 'fl0'  # Name of station location
    gasName = 'ccl4'  # Name of gas
    ver = 'Current_v5'  # Name of retrieval version to process
    ctlF = 'sfit4_v5.ctl'  # Name of ctl file

    #gasName    = 'nh3'                   # Name of gas
    #ver        = 'Current_v2'           # Name of retrieval version to process
    #ctlF       = 'sfit4_v2.ctl'            # Name of ctl file

    errorFlg = False  # Flag to process error data
    fltrFlg = True  # Flag to filter the data
    rmsFlg = True  # Flag to filter based on max RMS
    dofFlg = True  # Flag to filter based on min DOFs

    maxRMS = 1.5  # Max Fit RMS to filter data. Data is filtered according to <= maxrms
    minDOF = 0.5  # Min DOFs for filtering

    sclfct = 1.0E9  # Scale factor to apply to vmr plots (ppmv=1.0E6, ppbv=1.0E9, etc)
    sclfctName = 'ppbv'  # Name of scale factor for labeling plots

    #----------------------------
    # Partial Columns Bounds [km]
    #----------------------------
    pCols = [[0.0, 8.0]]

    #----------------------
    # Date range to process
    #----------------------
    iyear = 2014
    imnth = 7
    iday = 8
    fyear = 2014
    fmnth = 8
    fday = 23

    saveFlg = False
    pltFile = '/data/iortega/results/fl0/NH3_FRAPPE.pdf'

    #-------------------------------------------------
    #Reading both standard files
    #-------------------------------------------------

    if saveFlg: pdfsav = PdfPages(pltFile)
    else: pdfsav = False

    i_date = dt.date(iyear, imnth, iday)  # Initial Day
    f_date = dt.date(fyear, fmnth, fday)  # Final Day
    ndays = (f_date + dt.timedelta(days=1) - i_date).days
    dateList = [i_date + dt.timedelta(days=i) for i in range(0, ndays, 1)]

    #--------------------------------------------
    # Walk through first level of directories and
    # collect directory names for processing
    #--------------------------------------------

    dirlist = []

    for dd in dateList:
        # Find year month and day strings
        yrstr = "{0:02d}".format(dd.year)
        mnthstr = "{0:02d}".format(dd.month)
        daystr = "{0:02d}".format(dd.day)

        filename = 'frappe-PANGC_GROUND-BAO-TOWER_' + yrstr + mnthstr + daystr + '*.ict'
        #filename = 'FRAPPE-QCTILDAS-NH3_GROUND-BAO-TOWER_'+yrstr+mnthstr+daystr+'*.ict'

        icarttfile = glob.glob(dataDir + filename)

        if not icarttfile: continue

        for k in icarttfile:
            dirlist.append(k)

    dirlist.sort()

    insituvmr = []
    insitudate = []

    print 'Reading in-situ Files:'

    for indMain, sngDir in enumerate(dirlist):
        ckDir(sngDir)
        keys, data, dateutc, vnames = mf.read_ICARTT(sngDir)
        insituvmr.append(data[vnames[1]])
        insitudate.append(dateutc)

    insituvmr = np.asarray(insituvmr, dtype=np.float32)
    insitudate = np.asarray(insitudate)
    insituvmr = np.array(insituvmr).flatten()
    insitudate = np.array(insitudate).flatten()

    index = mf.dbFilterUL(insituvmr, lwrVal=-0.1, uprVal=10000.)

    insituvmr = insituvmr[index]
    insitudate = insitudate[index]

    #---------------------------------------------------------------------------------------
    #
    #                                    READING FTS data
    #
    #---------------------------------------------------------------------------------------

    print '\nReading FTS:'

    retDir = '/data1/ebaumer/' + loc.lower() + '/' + gasName.lower(
    ) + '/' + ver + '/'
    ctlFile = '/data1/ebaumer/' + loc.lower() + '/' + gasName.lower(
    ) + '/' + 'x.' + gasName.lower() + '/' + ctlF

    #---------------------------------
    # Check for the existance of files
    # directories from input file
    #---------------------------------
    ckDir(retDir, exit=True)
    ckFile(ctlFile, exit=True)

    if saveFlg: ckDir(os.path.dirname(os.path.realpath(pltFile)), exit=True)

    #-------------------------
    # Create Instance of Class
    #-------------------------
    gas = dc.PlotData(retDir,
                      ctlFile,
                      iyear=iyear,
                      imnth=imnth,
                      iday=iday,
                      fyear=fyear,
                      fmnth=fmnth,
                      fday=fday,
                      outFname=pltFile)

    #----------------------
    # Call to plot profiles
    #----------------------
    ds = gas.pltPrf(
        fltr=fltrFlg,
        allGas=True,
        sclfct=sclfct,
        sclname=sclfctName,
        errFlg=errorFlg,
        maxRMS=maxRMS,
        minTC=0.0,
        maxTC=1e24,
        minDOF=minDOF,
        dofFlg=dofFlg,
        rmsFlg=rmsFlg,
    )

    #----------------------
    # Call to plot profiles
    #----------------------
    for gas in ds['localGasList']:
        print 'Gas included in the Fit: ' + str(gas)
    prf = ds['rPrf']['PAN']
    alt = ds['alt']
    airmass = ds['airmass']
    vmr = prf[:, -1]
    vmr2 = prf[:, -1:]
    dates = ds['dates']

    #---------------------------------------------------------------------------------------
    #
    #                                    PLOTS
    #
    #---------------------------------------------------------------------------------------

    fig, ax = plt.subplots(figsize=(10, 6), sharex=True)
    ax.plot(insitudate,
            insituvmr / 1000.,
            'k',
            linewidth=2,
            label='insitu',
            alpha=0.5)
    ax.scatter(dates, vmr, facecolors='white', s=70, color='r', label='FTS')
    ax.set_ylabel('VMR [ppbv]', fontsize=16)
    ax.set_xlabel('Time [UT]', fontsize=16)
    ax.set_xlim(i_date, f_date)
    ax.tick_params(axis='both', which='major', labelsize=14)
    ax.grid(True)
    ax.legend(prop={'size': 12})
    fig.subplots_adjust(left=0.12, bottom=0.12, top=0.96, right=0.95)
    fig.autofmt_xdate()

    doy_fts = mf.toYearFraction(dates)
    doy_insitu = mf.toYearFraction(insitudate)

    insituvmr_interp = np.interp(doy_fts, doy_insitu, insituvmr)

    fig2, ax = plt.subplots(figsize=(10, 6), sharex=True)
    ax.scatter(dates,
               insituvmr_interp / 1000.,
               facecolors='blue',
               color='blue',
               s=35,
               label='insitu')
    ax.scatter(dates, vmr, facecolors='white', s=35, color='r', label='FTS')
    ax.set_ylabel('VMR [ppbv]', fontsize=16)
    ax.set_xlabel('Time [UT]', fontsize=16)
    ax.set_xlim(i_date, f_date)
    ax.tick_params(axis='both', which='major', labelsize=14)
    ax.grid(True)
    ax.legend(prop={'size': 12})
    fig2.subplots_adjust(left=0.12, bottom=0.12, top=0.96, right=0.95)
    fig2.autofmt_xdate()

    if saveFlg:
        pdfsav.savefig(fig, dpi=200)
        #pdfsav.savefig(fig2,dpi=200)

    else:
        plt.show(block=False)

    if saveFlg: pdfsav.close()

    print('\nFinished Plots.......\n')

    #--------------------------------
    # Pause so user can look at plots
    #--------------------------------
    if not saveFlg:
        user_input = raw_input('Press any key to exit >>> ')
        sys.exit()  # Exit program
예제 #6
0
def main(argv):

    #-------------------------------------------------
    # Initializations for C-130 insitu data
    #-------------------------------------------------
    dataDir = '/ya4/Campaign/FL0/FRAPPE/C130/'

    #-------------------------------------------------
    # Date range to Read C-130 insitu data (ICART FILES)
    #-------------------------------------------------
    iyear = 2014
    imnth = 7
    iday = 25
    fyear = 2014
    fmnth = 8
    fday = 20

    pltFile = 'figFRAPPE/C130_FRAPPE.pdf'
    #-------------------------------------------------
    #                 FLAGS
    #-------------------------------------------------
    saveFlg = True

    #--------------FLAGS TO READ DATA-----------------
    RFFlag = True  #FLIGHT INFORMATION (ALWAYS TRUE TO INTERPOLATE LAT AND LON)

    C2H6Flag = True  #C2H6
    COFlag = True  #CO
    H2COFlag = True  #H2CO
    NH3Flag = True  #H2CO
    HCOOHFlag = True  #H2CO

    #--------------FLAGS TO PLOT DATA-----------------
    pltC2H6toCO = True  #Plt Ratio
    pltNH3toCO = False  #Plt Ratio
    pltH2COtoCO = False  #Plt Ratio

    pltMap = True  #ALWAYS TRUE (IF AT LEAST ONE MAP IS CREATED)

    pltMapRF = False  #Map of all RF
    pltMapC2H6 = True  #Map of all C2H6
    pltMapCO = True  #Map of all CO
    pltMapH2CO = True  #Map of all h2co
    pltMapNH3 = True  #Map of all NH3
    pltMapHCOOH = True  #Map of all NH3

    pltMapC2H6toCO = True  #Map of the Ratio
    pltMapNH3toCO = False  #Map of the Ratio
    pltMapH2COtoCO = False  #Map of the Ratio

    #---------------------------------------------------
    # LOCATIONS for AIR MASSES
    #---------------------------------------------------
    #locP = [ [-104.0, 40.5] , [-104.6, 39.2], [-106.75, 40.0]]
    #locP = [ [-104.6, 40.42] , [-104.6, 39.4], [-106.4, 40.0]]
    locP = [[-104.6, 40.42], [-105.245, 39.4], [-106.6, 39.5]]
    #maxD = 75.0          #Maximum Distance in Km
    maxD = 50.0
    clr = ['red', 'blue', 'green']
    IDP = ['O&NG/Feedlot', 'Urban', 'Background']

    lonB, latB = -105.245, 40.035  #LOCATION OF NCAR FOOTHILLS LAB in Boulder

    #------------------------------------------------------------------------------------------------------
    #
    #                                               START
    #
    #------------------------------------------------------------------------------------------------------

    #---------------------------------------------------
    # INITIALIZE A CLASS FOR MAPS
    #---------------------------------------------------
    if pltMap:
        mp = pc.MapClass(origin=(40.0, -105.0),
                         maxAlt=4600,
                         minAlt=500.,
                         DistLeft=250000,
                         DistRight=350000,
                         DistTop=250000,
                         DistBottom=200000,
                         saveFlg=saveFlg)

    #-------------------------------------------------
    #                 START
    #-------------------------------------------------
    if saveFlg: pdfsav = PdfPages(pltFile)
    else: pdfsav = False

    i_date = dt.date(iyear, imnth, iday)  # Initial Day
    f_date = dt.date(fyear, fmnth, fday)  # Final Day
    ndays = (f_date + dt.timedelta(days=1) - i_date).days
    dateList = [i_date + dt.timedelta(days=i) for i in range(0, ndays, 1)]

    #--------------------------------------------
    # Walk through first level of directories and
    # collect directory names for processing
    #--------------------------------------------
    dirlist = {}
    Data = {}
    gases = []

    for dd in dateList:
        # Find year month and day strings
        yrstr = "{0:02d}".format(dd.year)
        mnthstr = "{0:02d}".format(dd.month)
        daystr = "{0:02d}".format(dd.day)

        filename = 'FRAPPE-NCAR-LRT-NAV_C130_' + yrstr + mnthstr + daystr + '*.ict'
        icarttfile = glob.glob(dataDir + filename)
        #if not icarttfile: continue
        for k in icarttfile:
            #dirlist.append(k)
            dirlist.setdefault('RF', []).append(k)

        if COFlag:

            filename = 'frappe-CO_C130_' + yrstr + mnthstr + daystr + '*.ict'
            icarttfile = glob.glob(dataDir + filename)
            #if not icarttfile: continue
            for k in icarttfile:
                dirlist.setdefault('CO', []).append(k)

            gases.append('CO')

        if C2H6Flag:

            filename = 'frappe-C2H6_C130_' + yrstr + mnthstr + daystr + '*.ict'
            icarttfile = glob.glob(dataDir + filename)
            #if not icarttfile: continue
            for k in icarttfile:
                dirlist.setdefault('C2H6', []).append(k)

            gases.append('C2H6')

        if H2COFlag:

            filename = 'frappe-CH2O_C130_' + yrstr + mnthstr + daystr + '*.ict'
            icarttfile = glob.glob(dataDir + filename)
            #if not icarttfile: continue
            for k in icarttfile:
                dirlist.setdefault('H2CO', []).append(k)

            gases.append('H2CO')

        if NH3Flag:

            filename = 'FRAPPE-NH3_C130_' + yrstr + mnthstr + daystr + '*.ict'
            icarttfile = glob.glob(dataDir + filename)
            #if not icarttfile: continue
            for k in icarttfile:
                dirlist.setdefault('NH3', []).append(k)

            gases.append('NH3')

        if HCOOHFlag:

            filename = 'FRAPPE-PCIMS_C130_' + yrstr + mnthstr + daystr + '*3sec.ict'
            icarttfile = glob.glob(dataDir + filename)
            #if not icarttfile: continue
            for k in icarttfile:
                dirlist.setdefault('HCOOH', []).append(k)

            gases.append('HCOOH')

    for i in dirlist:
        dirlist[i].sort()

    gases = list(set(gases))

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

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

    print 'Reading C130:'
    altC130 = []
    LatC130 = []
    LonC130 = []
    DateC130 = []
    RF = []

    for indMain, sngDir in enumerate(dirlist['RF']):
        ckDir(sngDir)
        keys, data, dateutc, vnames = mf.read_ICARTT(sngDir)
        DateC = dateutc
        altC = data[vnames[9]]
        LatC = data[vnames[11]]
        LonC = data[vnames[14]]

        altC = np.array(altC, dtype=np.float)
        LatC = np.array(LatC, dtype=np.float)
        LonC = np.array(LonC, dtype=np.float)
        DateC = np.array(DateC)

        #index    = np.where( (altC >= 0.0) & (altC <= 3000.0) & (LatC >= -100.0)  & (LonC >= -3000.0) )[0]
        index = np.where((altC >= 0.0) & (LatC >= -100.0)
                         & (LonC >= -3000.0))[0]

        altC = altC[index]
        LatC = LatC[index]
        LonC = LonC[index]
        DateC = DateC[index]

        DateC130.append(DateC)
        altC130.append(altC)
        LatC130.append(LatC)
        LonC130.append(LonC)
        RF.append(indMain + 1)

    altC130 = np.array(altC130).flatten()
    LatC130 = np.array(LatC130).flatten()
    LonC130 = np.array(LonC130).flatten()
    RF = np.array(RF).flatten()
    DateC130 = np.array(DateC130).flatten()
    doyC130 = [mf.toYearFraction(d) for d in DateC130]

    if 'CO' in gases:

        print '\nReading CO from C130 in-situ Files:'
        COC130 = []
        DateC130_CO = []

        for indMain, sngDir in enumerate(dirlist['CO']):
            ckDir(sngDir)
            keys, data, dateutc, vnames = mf.read_ICARTT(sngDir)

            DateC = dateutc
            CO = data[vnames[1]]

            CO = np.array(CO, dtype=np.float)
            DateC = np.array(DateC)

            index = np.where((CO >= 0.0))[0]

            CO = CO[index]
            DateC = DateC[index]

            DateC130_CO.append(DateC)
            COC130.append(CO)

        COC130 = np.array(COC130).flatten()
        DateC130_CO = np.array(DateC130_CO).flatten()
        doyC130_CO = [mf.toYearFraction(d) for d in DateC130_CO]

        LatC130_CO_int = [
            np.interp(d, doyC130[i], LatC130[i])
            for i, d in enumerate(doyC130_CO)
        ]
        LonC130_CO_int = [
            np.interp(d, doyC130[i], LonC130[i])
            for i, d in enumerate(doyC130_CO)
        ]

        Data['CO'] = {
            'conc': COC130,
            'DT': DateC130_CO,
            'doy': doyC130_CO,
            'lat': LatC130_CO_int,
            'lon': LonC130_CO_int
        }

    if 'C2H6' in gases:
        print '\nReading C2H6 from C130 in-situ Files:'
        C2H6C130 = []
        C2H6C130_e = []
        DateC130_C2H6 = []

        for indMain, sngDir in enumerate(dirlist['C2H6']):
            ckDir(sngDir)
            keys, data, dateutc, vnames = mf.read_ICARTT(sngDir)

            DateC = dateutc
            C2H6 = data[vnames[3]]
            C2H6_e = data[vnames[8]]

            C2H6 = np.array(C2H6, dtype=np.float) / 1000.0  #ppt to ppb
            C2H6_e = np.array(C2H6_e, dtype=np.float) / 1000.0  #ppt to ppb
            DateC = np.array(DateC)

            index = np.where((C2H6 >= 0.0))[0]

            C2H6 = C2H6[index]
            C2H6_e = C2H6_e[index]
            DateC = DateC[index]

            DateC130_C2H6.append(DateC)
            C2H6C130.append(C2H6)
            C2H6C130_e.append(C2H6_e)

        C2H6C130 = np.array(C2H6C130).flatten()
        C2H6C130_e = np.array(C2H6C130_e).flatten()
        DateC130_C2H6 = np.array(DateC130_C2H6).flatten()
        doyC130_C2H6 = [mf.toYearFraction(d) for d in DateC130_C2H6]

        LatC130_C2H6_int = [
            np.interp(d, doyC130[i], LatC130[i])
            for i, d in enumerate(doyC130_C2H6)
        ]
        LonC130_C2H6_int = [
            np.interp(d, doyC130[i], LonC130[i])
            for i, d in enumerate(doyC130_C2H6)
        ]

        Data['C2H6'] = {
            'conc': C2H6C130,
            'DT': DateC130_C2H6,
            'doy': doyC130_C2H6,
            'lat': LatC130_C2H6_int,
            'lon': LonC130_C2H6_int
        }

    if 'H2CO' in gases:
        print '\nReading H2CO from C130 in-situ Files:'
        H2COC130 = []
        DateC130_H2CO = []

        for indMain, sngDir in enumerate(dirlist['H2CO']):
            ckDir(sngDir)
            keys, data, dateutc, vnames = mf.read_ICARTT(sngDir)

            DateC = dateutc
            H2CO = data[vnames[3]]

            H2CO = np.array(H2CO, dtype=np.float) / 1000.0  #ppt to ppb
            DateC = np.array(DateC)

            index = np.where((H2CO >= 0.0))[0]

            H2CO = H2CO[index]
            DateC = DateC[index]

            DateC130_H2CO.append(DateC)
            H2COC130.append(H2CO)

        H2COC130 = np.array(H2COC130).flatten()
        DateC130_H2CO = np.array(DateC130_H2CO).flatten()
        doyC130_H2CO = [mf.toYearFraction(d) for d in DateC130_H2CO]

        LatC130_H2CO_int = [
            np.interp(d, doyC130[i], LatC130[i])
            for i, d in enumerate(doyC130_H2CO)
        ]
        LonC130_H2CO_int = [
            np.interp(d, doyC130[i], LonC130[i])
            for i, d in enumerate(doyC130_H2CO)
        ]

        Data['H2CO'] = {
            'conc': H2COC130,
            'DT': DateC130_H2CO,
            'doy': doyC130_H2CO,
            'lat': LatC130_H2CO_int,
            'lon': LonC130_H2CO_int
        }

    if 'NH3' in gases:
        print '\nReading NH3 from C130 in-situ Files:'
        NH3C130 = []
        DateC130_NH3 = []

        for indMain, sngDir in enumerate(dirlist['NH3']):
            ckDir(sngDir)
            keys, data, dateutc, vnames = mf.read_ICARTT(sngDir)

            DateC = dateutc
            NH3 = data[vnames[3]]

            NH3 = np.array(NH3, dtype=np.float)
            DateC = np.array(DateC)

            index = np.where((NH3 >= 0.0))[0]

            NH3 = NH3[index]
            DateC = DateC[index]

            DateC130_NH3.append(DateC)
            NH3C130.append(NH3)

        NH3C130 = np.array(NH3C130).flatten()
        DateC130_NH3 = np.array(DateC130_NH3).flatten()
        doyC130_NH3 = [mf.toYearFraction(d) for d in DateC130_NH3]

        LatC130_NH3_int = [
            np.interp(d, doyC130[i], LatC130[i])
            for i, d in enumerate(doyC130_NH3)
        ]
        LonC130_NH3_int = [
            np.interp(d, doyC130[i], LonC130[i])
            for i, d in enumerate(doyC130_NH3)
        ]

        Data['NH3'] = {
            'conc': NH3C130,
            'DT': DateC130_NH3,
            'doy': doyC130_NH3,
            'lat': LatC130_NH3_int,
            'lon': LonC130_NH3_int
        }

    if 'HCOOH' in gases:
        print '\nReading HCOOH from C130 in-situ Files:'
        HCOOHC130 = []
        DateC130_HCOOH = []

        for indMain, sngDir in enumerate(dirlist['HCOOH']):
            ckDir(sngDir)
            keys, data, dateutc, vnames = mf.read_ICARTT(sngDir)

            DateC = dateutc
            HCOOH = data[vnames[5]]

            HCOOH = np.array(HCOOH, dtype=np.float) / 1000.0  #ppt to ppb
            DateC = np.array(DateC)

            index = np.where((HCOOH >= 0.0))[0]

            HCOOH = HCOOH[index]
            DateC = DateC[index]

            DateC130_HCOOH.append(DateC)
            HCOOHC130.append(HCOOH)

        HCOOHC130 = np.array(HCOOHC130).flatten()
        DateC130_HCOOH = np.array(DateC130_HCOOH).flatten()
        doyC130_HCOOH = [mf.toYearFraction(d) for d in DateC130_HCOOH]

        LatC130_HCOOH_int = [
            np.interp(d, doyC130[i], LatC130[i])
            for i, d in enumerate(doyC130_HCOOH)
        ]
        LonC130_HCOOH_int = [
            np.interp(d, doyC130[i], LonC130[i])
            for i, d in enumerate(doyC130_HCOOH)
        ]

        Data['HCOOH'] = {
            'conc': HCOOHC130,
            'DT': DateC130_HCOOH,
            'doy': doyC130_HCOOH,
            'lat': LatC130_HCOOH_int,
            'lon': LonC130_HCOOH_int
        }

    #---------------------------------------------------------------------------------------
    #
    #                                    PLOTS
    #
    #---------------------------------------------------------------------------------------

    ngases = len(gases)

    #-------------------------------------------------------
    # Box plot of Concentrations
    #-------------------------------------------------------

    locations = [[1, 2, 3], [5, 6, 7], [9, 10, 11], [13, 14, 15], [17, 18, 19]]
    lticks = [2, 6, 10, 14, 18]

    gases = ['C2H6', 'NH3', 'CO', 'HCOOH', 'H2CO']

    fig, ax = plt.subplots(figsize=(10, 6))
    gasname_w = []

    for k, g in enumerate(gases):

        y = []

        lat = np.asarray(Data[g]['lat'])
        lon = np.asarray(Data[g]['lon'])
        conc = np.asarray(Data[g]['conc'])

        lat = np.concatenate(lat)
        lon = np.concatenate(lon)
        conc = np.concatenate(conc)

        for p, loc in enumerate(locP):

            print IDP[p]

            Dist = []

            for i, la in enumerate(lat):
                c = mf.haversine(abs(loc[0]), abs(loc[1]), abs(lon[i]),
                                 abs(lat[i]))
                Dist.append(float(c))

            Dist = np.asarray(Dist)
            inds = np.where(Dist <= maxD)[0]
            conc_i = np.asarray(conc[inds])

            y.append(conc_i)

            IQR = stats.iqr(conc_i, rng=(25, 75))
            print 'IQR = ', str(IQR)

            PCy = np.percentile(conc_i, [25, 50, 75])
            print 'PCy = ', str(PCy)

            Qy = stats.mstats.hdquantiles(conc_i, prob=(0.75, 0.9))
            print 'Qy = ', str(Qy)

            me = np.median(conc_i)

            print np.median(conc_i) + PCy[2] + (IQR)
            print np.std(conc_i) * 2.698
            print np.median(conc_i)

        y = np.asarray(y)

        if g.upper() == 'CO':
            y = y / 5.

            maxyi = [np.amax(yi) for yi in y]
            ax.text(lticks[k],
                    np.amax(maxyi) + (np.amax(maxyi) * 0.05),
                    r'x5',
                    fontsize=18)

        if ((g.upper() == 'H2CO') or (g.upper() == 'HCOOH')):

            y = y * 10.

            maxyi = [np.amax(yi) for yi in y]
            ax.text(lticks[k],
                    np.amax(maxyi) + (np.amax(maxyi) * 0.05),
                    r'/10',
                    fontsize=18)

        meanpointprops = dict(marker='o',
                              markeredgecolor='black',
                              markerfacecolor='white',
                              markersize=6)

        bp = ax.boxplot(
            y,
            whis=[5, 95],
            positions=[locations[k][0], locations[k][1], locations[k][2]],
            widths=0.85,
            showfliers=True,
            showmeans=True,
            meanprops=meanpointprops,
            patch_artist=True)

        maxloc = locations[k][2] + 1.0

        gasname = mf.getgasname(g)
        gasname_w.append(gasname)

        for pi, patch in enumerate(bp['boxes']):
            patch.set_facecolor(clr[pi])
            patch.set(linewidth=2)

        for whisker in bp['whiskers']:
            whisker.set(color='k', linewidth=2)

        ## change color and linewidth of the caps
        for cap in bp['caps']:
            cap.set(color='k', linewidth=2)

        ## change color and linewidth of the medians
        for median in bp['medians']:
            median.set(color='k', linewidth=2)

        for fly in bp['fliers']:
            fly.set(markersize=2)

    ax.set_xlim((0, maxloc))
    ax.set_ylim((-1, 100))
    ax.grid(True, which='y', alpha=0.5)
    #ax.set_yscale("log", nonposy='clip')
    ax.set_xticklabels(gasname_w)
    ax.set_xticks(lticks[0:ngases])
    ax.xaxis.set_tick_params(which='major', labelsize=18)
    ax.yaxis.set_tick_params(which='major', labelsize=18)
    ax.set_ylabel('VMR [ppb]', fontsize=18)
    #ax.set_ylabel(idtitle, fontsize = 18)

    # draw temporary red and blue lines and use them to create a legend
    hB, = ax.plot([1, 1], linewidth=2.0, linestyle='-', color=clr[0])
    hR, = ax.plot([1, 1], linewidth=2.0, linestyle='-', color=clr[1])
    hC, = ax.plot([1, 1], linewidth=2.0, linestyle='-', color=clr[2])
    #ax.legend((hB, hR, hC),('O&NG', 'Urban', 'Background'), prop={'size':16})
    ax.legend((hB, hR, hC), ('O&NG/Feedlot', 'Urban', 'Background'),
              prop={'size': 16},
              loc='upper center',
              bbox_to_anchor=(0.5, 1.12),
              fancybox=True,
              ncol=3)
    #legend((hB, hR),(seasons))
    hB.set_visible(False)
    hR.set_visible(False)
    hC.set_visible(False)

    if saveFlg:
        pdfsav.savefig(fig, dpi=200)
    else:
        plt.show(block=False)

    fig.savefig('figFRAPPE/box_conc.pdf', bbox_inches='tight')

    #-------------------------------------------------------
    # Box plot of Ratios
    #-------------------------------------------------------

    locations = [[1, 2, 3], [5, 6, 7], [9, 10, 11], [13, 14, 15]]
    lticks = [2, 6, 10, 14]

    fig, ax = plt.subplots(figsize=(10, 6))
    gasname_w = []

    #-------------------------------------------------------
    #
    #-------------------------------------------------------
    lat_x = np.asarray(Data['CO']['lat'])
    lon_x = np.asarray(Data['CO']['lon'])
    dt_x = np.asarray(Data['CO']['DT'])
    doy_x = np.asarray(Data['CO']['doy'])
    conc_x = np.asarray(Data['CO']['conc'])

    lat_x = np.concatenate(lat_x)
    lon_x = np.concatenate(lon_x)
    conc_x = np.concatenate(conc_x)
    dt_x = np.concatenate(dt_x)
    doy_x = np.concatenate(doy_x)

    #-------------------------------------------------------
    #
    #-------------------------------------------------------
    lat_y = np.asarray(Data['C2H6']['lat'])
    lon_y = np.asarray(Data['C2H6']['lon'])
    dt_y = np.asarray(Data['C2H6']['DT'])
    doy_y = np.asarray(Data['C2H6']['doy'])
    conc_y = np.asarray(Data['C2H6']['conc'])

    lat_y = np.concatenate(lat_y)
    lon_y = np.concatenate(lon_y)
    conc_y = np.concatenate(conc_y)
    dt_y = np.concatenate(dt_y)
    doy_y = np.concatenate(doy_y)

    gases2 = [g for g in gases if g != 'CO']

    for k, g in enumerate(gases2):

        Ra = []

        #-------------------------------------------------------
        #
        #-------------------------------------------------------
        lat = np.asarray(Data[g]['lat'])
        lon = np.asarray(Data[g]['lon'])
        conc = np.asarray(Data[g]['conc'])
        doy = np.asarray(Data[g]['doy'])

        lat = np.concatenate(lat)
        lon = np.concatenate(lon)
        conc = np.concatenate(conc)
        doy = np.concatenate(doy)

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

        for p, loc in enumerate(locP):

            Dist = []

            # if g.upper() != 'NH3':

            #     print 'HERE - {}'.format(g)

            #     for i, la in enumerate(lat_y):
            #         c = mf.haversine(abs(loc[0]), abs(loc[1]), abs(lon_y[i]), abs(lat_y[i]) )
            #         Dist.append(float(c))

            #     Dist      = np.asarray(Dist)
            #     inds      = np.where(Dist <= maxD)[0]

            #     doy_y2    = np.asarray(doy_y[inds])
            #     conc_y2   = np.asarray(conc_y[inds])

            #     #conc_i    = np.asarray(conc[inds])
            #     #doy_i     = np.asarray(doy[inds])

            #     conc_x_i = interpolate.interp1d(doy_x, conc_x, kind='nearest',   fill_value=-999, bounds_error=False)(doy_y2)
            #     conc_i = interpolate.interp1d(doy, conc, kind='nearest',   fill_value=-999, bounds_error=False)(doy_y2)

            #     #IQR = stats.iqr(conc_i, rng=(25,75))
            #     #print 'IQR = ', str(IQR)

            #     #PCy = np.percentile(conc_i, [25, 50, 75])
            #     #print 'PCy = ', str(PCy)

            #     #Qy = stats.mstats.hdquantiles(conc_i, prob=(0.75,0.9))
            #     #print 'Qy = ', str(Qy)

            #     if p == 0:
            #         max_i  = 0. #np.median(conc_y2) + PCy[2]# + (IQR)

            #     else:
            #         max_i = 0.

            #     inds   = np.where( (conc_y2 >= max_i) & (conc_x_i > 0.) & (conc_i > 0.) )[0]
            #     Ra.append(conc_i[inds]/conc_x_i[inds])

            # else:

            print 'HERE 2 - {}'.format(g)

            for i, la in enumerate(lat):
                c = mf.haversine(abs(loc[0]), abs(loc[1]), abs(lon[i]),
                                 abs(lat[i]))
                Dist.append(float(c))

            Dist = np.asarray(Dist)
            inds = np.where(Dist <= maxD)[0]

            conc_i = np.asarray(conc[inds])
            doy_i = np.asarray(doy[inds])

            conc_x_i = interpolate.interp1d(doy_x,
                                            conc_x,
                                            kind='nearest',
                                            fill_value=-999,
                                            bounds_error=False)(doy_i)

            #IQR = stats.iqr(conc_i, rng=(25,75))
            #print 'IQR = ', str(IQR)

            #PCy = np.percentile(conc_i, [25, 50, 75])
            #print 'PCy = ', str(PCy)

            #Qy = stats.mstats.hdquantiles(conc_i, prob=(0.75,0.9))
            #print 'Qy = ', str(Qy)

            if p == 0:
                max_i = 0.  #np.median(conc_i)# + PCy[2]# + (IQR)

            else:
                max_i = 0.

            inds = np.where((conc_i >= max_i) & (conc_x_i > 0.))[0]
            Ra.append(conc_i[inds] / conc_x_i[inds])

            odr = mf.orthoregress(conc_x_i[inds],
                                  conc_i[inds],
                                  xerr=conc_x_i[inds] * 0.05,
                                  yerr=conc_i[inds] * 0.1,
                                  InError=False)
            slope = float(odr[0])
            intercept = float(odr[1])

            print 'gas = ', str(g)
            print 'loc = ', IDP[p]
            print 'Slope = ', str(slope)
            print 'intercept = ', str(intercept)
            print 'Median Ra = ', str(np.median(conc_i[inds] / conc_x_i[inds]))
            print 'Mean Ra = ', str(np.mean(conc_i[inds] / conc_x_i[inds]))
            print 'std Ra = ', str(np.std(conc_i[inds] / conc_x_i[inds]))

        Ra = np.asarray(Ra)

        if ((g.upper() == 'H2CO') or (g.upper() == 'HCOOH')):
            Ra = Ra * 10.
            maxRa = [np.amax(r) for r in Ra]
            ax.text(lticks[k],
                    np.amax(maxRa) + (np.amax(maxRa) * 0.05),
                    r'x10',
                    fontsize=18)

        meanpointprops = dict(marker='o',
                              markeredgecolor='black',
                              markerfacecolor='white',
                              markersize=6)

        bp = ax.boxplot(
            Ra,
            whis=1.5,
            positions=[locations[k][0], locations[k][1], locations[k][2]],
            widths=0.85,
            showfliers=True,
            showmeans=True,
            meanprops=meanpointprops,
            patch_artist=True)

        maxloc = locations[k][2] + 1.0

        gasname = mf.getgasname(g)
        gasname_w.append(gasname)

        for pi, patch in enumerate(bp['boxes']):
            patch.set_facecolor(clr[pi])
            patch.set(linewidth=2)

        for whisker in bp['whiskers']:
            whisker.set(color='k', linewidth=2)

        ## change color and linewidth of the caps
        for cap in bp['caps']:
            cap.set(color='k', linewidth=2)

        ## change color and linewidth of the medians
        for median in bp['medians']:
            median.set(color='k', linewidth=2)

        for fly in bp['fliers']:
            fly.set(markersize=2)

    ax.set_xlim((0, maxloc))
    ax.set_ylim((-0.05, 1.0))
    #ax.set_yscale("log", nonposy='clip')
    ax.grid(True, which='y', alpha=0.35)
    ax.set_xticklabels(gasname_w)
    ax.set_xticks(lticks[0:ngases])
    ax.xaxis.set_tick_params(which='major', labelsize=18)
    ax.yaxis.set_tick_params(which='major', labelsize=18)
    ax.set_ylabel('ER [ppb/ppb]', fontsize=18)
    #ax.set_ylabel(idtitle, fontsize = 18)

    # draw temporary red and blue lines and use them to create a legend
    hB, = ax.plot([1, 1], linewidth=2.0, linestyle='--', color=clr[0])
    hR, = ax.plot([1, 1], linewidth=2.0, linestyle='--', color=clr[1])
    hC, = ax.plot([1, 1], linewidth=2.0, linestyle='--', color=clr[2])
    ax.legend((hB, hR, hC), ('O&NG/Feedlot', 'Urban', 'Background'),
              prop={'size': 16},
              loc='upper center',
              bbox_to_anchor=(0.5, 1.12),
              fancybox=True,
              ncol=3)
    #legend((hB, hR),(seasons))
    hB.set_visible(False)
    hR.set_visible(False)
    hC.set_visible(False)

    if saveFlg:
        pdfsav.savefig(fig, dpi=200)
    else:
        plt.show(block=False)

    fig.savefig('figFRAPPE/box_Ra.pdf', bbox_inches='tight')

    #user_input = raw_input('Press any key to exit >>> ')
    #sys.exit()

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

    if pltC2H6toCO:

        x = np.concatenate(COC130)
        xDate = np.concatenate(DateC130_CO)
        xdoy = mf.toYearFraction(xDate)

        y = np.concatenate(C2H6C130)
        y_e = np.concatenate(C2H6C130_e)
        yDate = np.concatenate(DateC130_C2H6)
        ydoy = mf.toYearFraction(yDate)

        la = np.concatenate(LatC130)
        lo = np.concatenate(LonC130)
        d = np.concatenate(DateC130)
        ddoy = mf.toYearFraction(d)

        y_int = interpolate.interp1d(ydoy,
                                     y,
                                     kind='nearest',
                                     fill_value=-999,
                                     bounds_error=False)(xdoy)
        y_e_int = interpolate.interp1d(ydoy,
                                       y_e,
                                       kind='nearest',
                                       fill_value=-999,
                                       bounds_error=False)(xdoy)

        x_int = interpolate.interp1d(xdoy,
                                     x,
                                     kind='nearest',
                                     fill_value=-999,
                                     bounds_error=False)(ydoy)

        # inds = []

        # for xd in xdoy:

        #     ind_i = mf.nearestind(xd, ydoy)
        #     inds.append(ind_i)

        # x_int   = x[inds]

        la_int = interpolate.interp1d(ddoy,
                                      la,
                                      fill_value=-999,
                                      bounds_error=False)(ydoy)
        lo_int = interpolate.interp1d(ddoy,
                                      lo,
                                      fill_value=-999,
                                      bounds_error=False)(ydoy)

        #inds       = np.where(y_int > 0. )[0]
        inds = np.where((y > 0.) & (x_int > 0.))[0]

        x_int = x_int[inds]
        y = y[inds]
        y_e = y_e[inds]
        lo_int = lo_int[inds]
        la_int = la_int[inds]

        # Dist     = []

        # for i, k in enumerate(x):
        #     c = mf.haversine(abs(locP[-1][0]), abs(locP[-1][1]), abs(lo_int[i]), abs(la_int[i]) )
        #     Dist.append(float(c))

        # Dist = np.asarray(Dist)
        # inds = np.where(Dist <= maxD)[0]

        # xbkg = np.nanmean(np.asarray(x[inds]))
        # ybkg = np.nanmean(np.asarray(y_int[inds]))

        # print ybkg
        # print xbkg

        Ra = np.true_divide(y, x_int)

        fig, ax = plt.subplots(figsize=(10, 6), sharex=True)

        for p, loc in enumerate(locP):

            Dist = []

            for i, k in enumerate(y):
                c = mf.haversine(abs(loc[0]), abs(loc[1]), abs(lo_int[i]),
                                 abs(la_int[i]))
                Dist.append(float(c))

            Dist = np.asarray(Dist)
            inds = np.where(Dist <= maxD)[0]

            x1 = np.asarray(x_int[inds])
            y1 = np.asarray(y[inds])
            y1_e = np.asarray(y_e[inds])

            # x1bkg =  np.nanmin(x1)
            # y1bkg =  np.nanmin(y1)

            # x1 = x1 - x1bkg
            # y1 = y1 - y1bkg

            ax.scatter(x1,
                       y1,
                       facecolors='white',
                       s=70,
                       color=clr[p],
                       label=IDP[p])

            slope, intercept, r_value, p_value, std_err = stats.linregress(
                x1, y1)
            print '\nCorrelation of ' + IDP[p] + ' C2H6 to CO'
            print 'Slope = ', str(slope)
            print 'intercept = ', str(intercept)
            print 'r_value = ', str(r_value)
            print 'std_err =', str(std_err)
            print 'std_err =', str(std_err * np.sqrt(len(x1)))

            print 'mean C2H6 = ', str(np.mean(y1))
            print 'median C2H6 = ', str(np.median(y1))

            print 'mean CO = ', str(np.mean(x1))
            print 'median CO = ', str(np.median(x1))

            print 'Ratio = {} +/- {}'.format(np.mean(Ra[inds]),
                                             np.std(Ra[inds]))
            print 'Ratio = {} +/- {}'.format(np.median(Ra[inds]),
                                             np.std(Ra[inds]))

            odr = mf.orthoregress(x1,
                                  y1,
                                  xerr=x1 * 0.05,
                                  yerr=y1_e,
                                  InError=False)
            slope = float(odr[0])
            intercept = float(odr[1])

            print 'Slope = ', str(slope)
            print 'intercept = ', str(intercept)

            #slope_e     = float(odrErr[0])
            #intercept_e = float(odrErr[1])

            #print '\nSlope: {0:.2f} +/- {1:.2f}'.format(float(odr[0]), float(odrErr[0]))
            #print 'Intercept = {0:.3f} +/- {1:.3f}'.format(float(odr[1]), float(odrErr[1]))

            xx = range(0, 500, 2)
            xx = np.asarray(xx)
            Fit = slope * np.asarray(xx) + (intercept)  #*math.exp(-1.6/7.4)
            ax.plot(xx, Fit, color=clr[p], linewidth=2.0, linestyle='--')
            ax.fill_between(xx,
                            Fit - Fit * 0.2,
                            Fit + Fit * 0.2,
                            alpha=0.25,
                            color=clr[p])

        ax.set_ylabel('C$_2$H$_6$ VMR [ppbv]', fontsize=18)
        ax.set_xlabel('CO VMR [ppbv]', fontsize=18)
        ax.set_ylim(0, 62.0)
        ax.set_xlim(50, 400.0)
        ax.grid(True)
        ax.tick_params(axis='both', which='major', labelsize=18)
        ax.legend(prop={'size': 18})
        fig.subplots_adjust(left=0.12, bottom=0.12, top=0.96, right=0.95)

        if saveFlg:
            pdfsav.savefig(fig, dpi=200)

        else:
            plt.show(block=False)

        if pltMapC2H6toCO:
            mp.pltMapZ(LonData=lo_int,
                       LatData=la_int,
                       zData=Ra,
                       zmin=0.01,
                       zmax=0.15,
                       ztitle='Ratio C$_2$H$_6$ to CO [ppb]',
                       LatID=latB,
                       LonID=lonB,
                       SaveFile='figFRAPPE/C130_FRAPPE_MAP_C2H6toCO.pdf')

        #user_input = raw_input('Press any key to exit >>> ')
        #sys.exit()

    if pltNH3toCO:
        x = np.concatenate(COC130)
        xDate = np.concatenate(DateC130_CO)
        xdoy = mf.toYearFraction(xDate)

        y = np.concatenate(NH3C130)
        yDate = np.concatenate(DateC130_NH3)
        ydoy = mf.toYearFraction(yDate)

        la = np.concatenate(LatC130)
        lo = np.concatenate(LonC130)
        d = np.concatenate(DateC130)
        ddoy = mf.toYearFraction(d)

        y_int = interpolate.interp1d(ydoy, y, bounds_error=False)(xdoy)
        la_int = interpolate.interp1d(ddoy, la, bounds_error=False)(xdoy)
        lo_int = interpolate.interp1d(ddoy, lo, bounds_error=False)(xdoy)

        Ra = np.true_divide(y_int, x)

        fig, ax = plt.subplots(figsize=(10, 6), sharex=True)

        #ax.plot(x, y_int, '.k', markersize=1, linewidth=0, alpha=0.15)
        #ax.scatter(x, y_int, facecolors='white', s=50, color='gray', alpha=0.15)
        slope, intercept, r_value, p_value, std_err = stats.linregress(
            x, y_int)
        print '\nCorrelation of all data' + ' NH3 to CO'
        print 'Slope = ', str(slope)
        print 'intercept = ', str(intercept)
        print 'r_value = ', str(r_value)
        print 'std_err =', str(std_err)

        for p, loc in enumerate(locP):
            Dist = []

            for i, k in enumerate(x):
                c = mf.haversine(abs(loc[0]), abs(loc[1]), abs(lo_int[i]),
                                 abs(la_int[i]))
                Dist.append(float(c))

            Dist = np.asarray(Dist)
            inds = np.where(Dist <= maxD)[0]

            x1 = np.asarray(x[inds])
            y1 = np.asarray(y_int[inds])

            ax.scatter(x1,
                       y1,
                       facecolors='white',
                       s=70,
                       color=clr[p],
                       label=IDP[p])

            slope, intercept, r_value, p_value, std_err = stats.linregress(
                x1, y1)
            print '\nCorrelation of ' + IDP[p] + ' NH3 to CO'
            print 'Slope = ', str(slope)
            print 'intercept = ', str(intercept)
            print 'r_value = ', str(r_value)

            xx = range(0, 500, 2)
            xx = np.asarray(xx)
            Fit = slope * np.asarray(xx) + (intercept)  #*math.exp(-1.6/7.4)
            ax.plot(xx, Fit, color=clr[p], linewidth=2.0, linestyle='--')
            ax.fill_between(xx,
                            Fit - Fit * 0.2,
                            Fit + Fit * 0.2,
                            alpha=0.25,
                            color=clr[p])

        ax.set_ylabel('NH$_3$ VMR [ppbv]', fontsize=18)
        ax.set_xlabel('CO VMR [ppbv]', fontsize=18)
        ax.set_ylim(0, 40.0)
        ax.set_xlim(50, 400.0)
        ax.grid(True)
        ax.tick_params(axis='both', which='major', labelsize=18)
        ax.legend(prop={'size': 18})
        fig.subplots_adjust(left=0.12, bottom=0.12, top=0.96, right=0.95)

        if saveFlg:
            pdfsav.savefig(fig, dpi=200)

        else:
            plt.show(block=False)

        if pltMapNH3toCO:
            mp.pltMapZ(LonData=lo_int,
                       LatData=la_int,
                       zData=Ra,
                       zmin=0.01,
                       zmax=0.075,
                       ztitle='Ratio NH$_3$ to CO [ppb]',
                       LatID=latB,
                       LonID=lonB,
                       SaveFile='figFRAPPE/C130_FRAPPE_MAP_NH3toCO.pdf')

    if pltH2COtoCO:
        x = np.concatenate(COC130)
        xDate = np.concatenate(DateC130_CO)
        xdoy = mf.toYearFraction(xDate)

        y = np.concatenate(H2COC130)
        yDate = np.concatenate(DateC130_H2CO)
        ydoy = mf.toYearFraction(yDate)

        la = np.concatenate(LatC130)
        lo = np.concatenate(LonC130)
        d = np.concatenate(DateC130)
        ddoy = mf.toYearFraction(d)

        y_int = interpolate.interp1d(ydoy, y, bounds_error=False)(xdoy)
        la_int = interpolate.interp1d(ddoy, la, bounds_error=False)(xdoy)
        lo_int = interpolate.interp1d(ddoy, lo, bounds_error=False)(xdoy)

        Ra = np.true_divide(y_int, x)

        fig, ax = plt.subplots(figsize=(10, 6), sharex=True)

        #ax.plot(x, y_int, '.k', markersize=1, linewidth=0, alpha=0.15)
        #ax.scatter(x, y_int, facecolors='white', s=50, color='gray', alpha=0.15)
        slope, intercept, r_value, p_value, std_err = stats.linregress(
            x, y_int)
        print '\nCorrelation of all data' + ' H2CO to CO'
        print 'Slope = ', str(slope)
        print 'intercept = ', str(intercept)
        print 'r_value = ', str(r_value)
        print 'std_err =', str(std_err)

        for p, loc in enumerate(locP):
            Dist = []

            for i, k in enumerate(x):
                c = mf.haversine(abs(loc[0]), abs(loc[1]), abs(lo_int[i]),
                                 abs(la_int[i]))
                Dist.append(float(c))

            Dist = np.asarray(Dist)
            inds = np.where(Dist <= maxD)[0]

            x1 = np.asarray(x[inds])
            y1 = np.asarray(y_int[inds])

            ax.scatter(x1,
                       y1,
                       facecolors='white',
                       s=70,
                       color=clr[p],
                       label=IDP[p])

            slope, intercept, r_value, p_value, std_err = stats.linregress(
                x1, y1)
            print '\nCorrelation of ' + IDP[p] + ' H2CO to CO'
            print 'Slope = ', str(slope)
            print 'intercept = ', str(intercept)
            print 'r_value = ', str(r_value)
            print 'std_err =', str(std_err)

            xx = range(0, 500, 2)
            xx = np.asarray(xx)
            Fit = slope * np.asarray(xx) + (intercept)  #*math.exp(-1.6/7.4)
            ax.plot(xx, Fit, color=clr[p], linewidth=2.0, linestyle='--')
            ax.fill_between(xx,
                            Fit - Fit * 0.2,
                            Fit + Fit * 0.2,
                            alpha=0.25,
                            color=clr[p])

        ax.set_ylabel('H$_2$CO VMR [ppbv]', fontsize=16)
        ax.set_xlabel('CO VMR [ppbv]', fontsize=16)
        ax.set_ylim(0, 7.0)
        ax.set_xlim(50, 400.0)
        ax.grid(True)
        ax.tick_params(axis='both', which='major', labelsize=14)
        ax.legend(prop={'size': 12})
        fig.subplots_adjust(left=0.12, bottom=0.12, top=0.96, right=0.95)

        if saveFlg:
            pdfsav.savefig(fig, dpi=200)

        else:
            plt.show(block=False)

        if pltMapH2COtoCO:
            mp.pltMapZ(LonData=lo_int,
                       LatData=la_int,
                       zData=Ra,
                       zmin=0.005,
                       zmax=0.04,
                       ztitle='Ratio H$_2$CO to CO [ppb]',
                       LatID=latB,
                       LonID=lonB,
                       SaveFile='figFRAPPE/C130_FRAPPE_MAP_H2COtoCO.pdf')

    #------------------------------------------------------------------
    #                     PLOT MAP OF C2H6
    #------------------------------------------------------------------
    if pltMapC2H6:

        C2H6C130 = np.concatenate(C2H6C130)
        LonC130_int = np.concatenate(LonC130_C2H6_int)
        LatC130_int = np.concatenate(LatC130_C2H6_int)

        mp.pltMapZ(LonData=LonC130_int,
                   LatData=LatC130_int,
                   zData=C2H6C130,
                   zmin=0.5,
                   zmax=10,
                   ztitle='C$_2$H$_6$ [ppb]',
                   LatID=latB,
                   LonID=lonB,
                   SaveFile='figFRAPPE/C130_FRAPPE_MAP_C2H6.pdf')

    #------------------------------------------------------------------
    #                     PLOT MAP OF CO
    #------------------------------------------------------------------
    if pltMapCO:

        COC130 = np.concatenate(COC130)
        LonC130_int = np.concatenate(LonC130_CO_int)
        LatC130_int = np.concatenate(LatC130_CO_int)

        mp.pltMapZ(LonData=LonC130_int,
                   LatData=LatC130_int,
                   zData=COC130,
                   zmin=50,
                   zmax=150,
                   ztitle='CO [ppb]',
                   LatID=latB,
                   LonID=lonB,
                   SaveFile='figFRAPPE/C130_FRAPPE_MAP_CO.pdf')

    #------------------------------------------------------------------
    #                     PLOT MAP OF H2CO
    #------------------------------------------------------------------
    if pltMapH2CO:

        H2COC130 = np.concatenate(H2COC130)
        LonC130_int = np.concatenate(LonC130_H2CO_int)
        LatC130_int = np.concatenate(LatC130_H2CO_int)

        mp.pltMapZ(LonData=LonC130_int,
                   LatData=LatC130_int,
                   zData=H2COC130,
                   zmin=0.1,
                   zmax=4.0,
                   ztitle='H$_2$CO [ppb]',
                   LatID=latB,
                   LonID=lonB,
                   SaveFile='figFRAPPE/C130_FRAPPE_MAP_H2CO.pdf')

    #------------------------------------------------------------------
    #                     PLOT MAP OF NH3
    #------------------------------------------------------------------
    if pltMapNH3:

        NH3C130 = np.concatenate(NH3C130)
        LonC130_int = np.concatenate(LonC130_NH3_int)
        LatC130_int = np.concatenate(LatC130_NH3_int)

        mp.pltMapZ(LonData=LonC130_int,
                   LatData=LatC130_int,
                   zData=NH3C130,
                   zmin=0.05,
                   zmax=15.0,
                   ztitle='NH$_3$ [ppb]',
                   LatID=latB,
                   LonID=lonB,
                   SaveFile='figFRAPPE/C130_FRAPPE_MAP_NH3.pdf')

    #------------------------------------------------------------------
    #                     PLOT MAP OF NH3
    #------------------------------------------------------------------
    if pltMapHCOOH:

        HCOOHC130 = np.concatenate(HCOOHC130)
        LonC130_int = np.concatenate(LonC130_HCOOH_int)
        LatC130_int = np.concatenate(LatC130_HCOOH_int)

        mp.pltMapZ(LonData=LonC130_int,
                   LatData=LatC130_int,
                   zData=HCOOHC130,
                   zmin=0.05,
                   zmax=3.0,
                   ztitle='HCOOH [ppb]',
                   LatID=latB,
                   LonID=lonB,
                   SaveFile='figFRAPPE/C130_FRAPPE_MAP_HCOOH.pdf')

    #------------------------------------------------------------------
    #                     PLOT MAP WITH RF TRACKS
    #------------------------------------------------------------------
    if pltMapRF:
        mp.pltMapRF(LonData=LonC130,
                    LatData=LatC130,
                    zData=RF,
                    ztitle='Research Flight',
                    LatID=latB,
                    LonID=lonB,
                    locP=locP,
                    SaveFile='figFRAPPE/C130_FRAPPE_MAP_RF.pdf')

    #--------------------------------
    # Pause so user can look at plots
    #--------------------------------
    if saveFlg:
        pdfsav.close()
    else:
        user_input = raw_input('Press any key to exit >>> ')
        sys.exit()  # Exit program