Ejemplo n.º 1
0
def main():
    """
    NAME
       revtest_MM1990.py

    DESCRIPTION
       calculates Watson's V statistic from input files through Monte Carlo simulation in order to test whether normal and reversed populations could have been drawn from a common mean (equivalent to watsonV.py). Also provides the critical angle between the two sample mean directions and the corresponding McFadden and McElhinny (1990) classification.

    INPUT FORMAT
       takes dec/inc as first two columns in two space delimited files (one file for normal directions, one file for reversed directions).

    SYNTAX
       revtest_MM1990.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f FILE
        -f2 FILE
        -P  (don't plot the Watson V cdf)

    OUTPUT
        Watson's V between the two populations and the Monte Carlo Critical Value Vc.
        M&M1990 angle, critical angle and classification
        Plot of Watson's V CDF from Monte Carlo simulation (red line), V is solid and Vc is dashed.

    """
    D1,D2=[],[]
    plot=1
    Flip=1
    if '-h' in sys.argv: # check if help is needed
        print(main.__doc__)
        sys.exit() # graceful quit
    if '-P' in  sys.argv: plot=0
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        file1=sys.argv[ind+1]
    f1=open(file1,'r')
    for line in f1.readlines():
        rec=line.split()
        Dec,Inc=float(rec[0]),float(rec[1])
        D1.append([Dec,Inc,1.])
    f1.close()
    if '-f2' in sys.argv:
        ind=sys.argv.index('-f2')
        file2=sys.argv[ind+1]
        f2=open(file2,'r')
        print("be patient, your computer is doing 5000 simulations...")
        for line in f2.readlines():
            rec=line.split()
            Dec,Inc=float(rec[0]),float(rec[1])
            D2.append([Dec,Inc,1.])
        f2.close()
    #take the antipode for the directions in file 2
    D2_flip=[]
    for rec in D2:
        d,i=(rec[0]-180.)%360.,-rec[1]
        D2_flip.append([d,i,1.])

    pars_1=pmag.fisher_mean(D1)
    pars_2=pmag.fisher_mean(D2_flip)

    cart_1=pmag.dir2cart([pars_1["dec"],pars_1["inc"],pars_1["r"]])
    cart_2=pmag.dir2cart([pars_2['dec'],pars_2['inc'],pars_2["r"]])
    Sw=pars_1['k']*pars_1['r']+pars_2['k']*pars_2['r'] # k1*r1+k2*r2
    xhat_1=pars_1['k']*cart_1[0]+pars_2['k']*cart_2[0] # k1*x1+k2*x2
    xhat_2=pars_1['k']*cart_1[1]+pars_2['k']*cart_2[1] # k1*y1+k2*y2
    xhat_3=pars_1['k']*cart_1[2]+pars_2['k']*cart_2[2] # k1*z1+k2*z2
    Rw=numpy.sqrt(xhat_1**2+xhat_2**2+xhat_3**2)
    V=2*(Sw-Rw)
#
#keep weighted sum for later when determining the "critical angle" let's save it as Sr (notation of McFadden and McElhinny, 1990)
#
    Sr=Sw
#
# do monte carlo simulation of datasets with same kappas, but common mean
#
    counter,NumSims=0,5000
    Vp=[] # set of Vs from simulations
    for k in range(NumSims):
#
# get a set of N1 fisher distributed vectors with k1, calculate fisher stats
#
        Dirp=[]
        for i in range(pars_1["n"]):
            Dirp.append(pmag.fshdev(pars_1["k"]))
        pars_p1=pmag.fisher_mean(Dirp)
#
# get a set of N2 fisher distributed vectors with k2, calculate fisher stats
#
        Dirp=[]
        for i in range(pars_2["n"]):
            Dirp.append(pmag.fshdev(pars_2["k"]))
        pars_p2=pmag.fisher_mean(Dirp)
#
# get the V for these
#
        Vk=pmag.vfunc(pars_p1,pars_p2)
        Vp.append(Vk)
#
# sort the Vs, get Vcrit (95th percentile one)
#
    Vp.sort()
    k=int(.95*NumSims)
    Vcrit=Vp[k]
#
# equation 18 of McFadden and McElhinny, 1990 calculates the critical value of R (Rwc)
#
    Rwc=Sr-(old_div(Vcrit,2))
#
#following equation 19 of McFadden and McElhinny (1990) the critical angle is calculated.
#
    k1=pars_1['k']
    k2=pars_2['k']
    R1=pars_1['r']
    R2=pars_2['r']
    critical_angle=numpy.degrees(numpy.arccos(old_div(((Rwc**2)-((k1*R1)**2)-((k2*R2)**2)),(2*k1*R1*k2*R2))))
    D1_mean=(pars_1['dec'],pars_1['inc'])
    D2_mean=(pars_2['dec'],pars_2['inc'])
    angle=pmag.angle(D1_mean,D2_mean)
#
# print the results of the test
#
    print("")
    print("Results of Watson V test: ")
    print("")
    print("Watson's V:           " '%.1f' %(V))
    print("Critical value of V:  " '%.1f' %(Vcrit))

    if V<Vcrit:
        print('"Pass": Since V is less than Vcrit, the null hypothesis that the two populations are drawn from distributions that share a common mean direction (antipodal to one another) cannot be rejected.')
    elif V>Vcrit:
        print('"Fail": Since V is greater than Vcrit, the two means can be distinguished at the 95% confidence level.')
    print("")
    print("M&M1990 classification:")
    print("")
    print("Angle between data set means: " '%.1f'%(angle))
    print("Critical angle of M&M1990:   " '%.1f'%(critical_angle))

    if V>Vcrit:
        print("")
    elif V<Vcrit:
        if critical_angle<5:
            print("The McFadden and McElhinny (1990) classification for this test is: 'A'")
        elif critical_angle<10:
            print("The McFadden and McElhinny (1990) classification for this test is: 'B'")
        elif critical_angle<20:
            print("The McFadden and McElhinny (1990) classification for this test is: 'C'")
        else:
            print("The McFadden and McElhinny (1990) classification for this test is: 'INDETERMINATE;")
    if plot==1:
        CDF={'cdf':1}
        pmagplotlib.plot_init(CDF['cdf'],5,5)
        p1 = pmagplotlib.plotCDF(CDF['cdf'],Vp,"Watson's V",'r',"")
        p2 = pmagplotlib.plotVs(CDF['cdf'],[V],'g','-')
        p3 = pmagplotlib.plotVs(CDF['cdf'],[Vp[k]],'b','--')
        pmagplotlib.drawFIGS(CDF)
        files,fmt={},'svg'
        if file2!="":
            files['cdf']='WatsonsV_'+file1+'_'+file2+'.'+fmt
        else:
            files['cdf']='WatsonsV_'+file1+'.'+fmt
        if pmagplotlib.isServer:
            black     = '#000000'
            purple    = '#800080'
            titles={}
            titles['cdf']='Cumulative Distribution'
            CDF = pmagplotlib.addBorders(CDF,titles,black,purple)
            pmagplotlib.saveP(CDF,files)
        else:
            ans=input(" S[a]ve to save plot, [q]uit without saving:  ")
            if ans=="a": pmagplotlib.saveP(CDF,files)
Ejemplo n.º 2
0
def main():
    """
    NAME
        eqarea_magic.py

    DESCRIPTION
       makes equal area projections from declination/inclination data

    SYNTAX
        eqarea_magic.py [command line options]

    INPUT
       takes magic formatted sites, samples, specimens, or measurements

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input magic format file from magic, default='sites.txt'
         supported types=[measurements, specimens, samples, sites]
        -fsp FILE: specify specimen file name, (required if you want to plot measurements by sample)
                default='specimens.txt'
        -fsa FILE: specify sample file name, (required if you want to plot specimens by site)
                default='samples.txt'
        -fsi FILE: specify site file name, default='sites.txt'

        -obj OBJ: specify  level of plot  [all, sit, sam, spc], default is all
        -crd [s,g,t]: specify coordinate system, [s]pecimen, [g]eographic, [t]ilt adjusted
                default is geographic, unspecified assumed geographic
        -fmt [svg,png,jpg] format for output plots
        -ell [F,K,B,Be,Bv] plot Fisher, Kent, Bingham, Bootstrap ellipses or Boostrap eigenvectors
        -c plot as colour contour
        -sav save plot and quit quietly
    NOTE
        all: entire file; sit: site; sam: sample; spc: specimen
    """
    # initialize some default variables
    FIG = {} # plot dictionary
    FIG['eqarea'] = 1 # eqarea is figure 1
    plotE = 0
    plt = 0  # default to not plotting
    verbose = pmagplotlib.verbose
    # extract arguments from sys.argv
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    dir_path = pmag.get_named_arg_from_sys("-WD", default_val=".")
    pmagplotlib.plot_init(FIG['eqarea'],5,5)
    in_file = pmag.get_named_arg_from_sys("-f", default_val="sites.txt")
    in_file = pmag.resolve_file_name(in_file, dir_path)
    if "-WD" not in sys.argv:
        dir_path = os.path.split(in_file)[0]
    #full_in_file = os.path.join(dir_path, in_file)
    plot_by = pmag.get_named_arg_from_sys("-obj", default_val="all").lower()
    spec_file = pmag.get_named_arg_from_sys("-fsp", default_val="specimens.txt")
    samp_file = pmag.get_named_arg_from_sys("-fsa", default_val="samples.txt")
    site_file = pmag.get_named_arg_from_sys("-fsi", default_val="sites.txt")
    if plot_by == 'all':
        plot_key = 'all'
    elif plot_by == 'sit':
        plot_key = 'site'
    elif plot_by == 'sam':
        plot_key = 'sample'
    elif plot_by == 'spc':
        plot_key = 'specimen'
    else:
        plot_by = 'all'
        plot_key = 'all'
    if '-c' in sys.argv:
        contour = 1
    else:
        contour = 0
    if '-sav' in sys.argv:
        plt = 1
        verbose = 0
    if '-ell' in sys.argv:
        plotE = 1
        ind = sys.argv.index('-ell')
        ell_type = sys.argv[ind+1]
        ell_type = pmag.get_named_arg_from_sys("-ell", "F")
        dist = ell_type.upper()
        # if dist type is unrecognized, use Fisher
        if dist not in ['F', 'K', 'B', 'BE', 'BV']:
            dist = 'F'
        if dist == "BV":
            FIG['bdirs'] = 2
            pmagplotlib.plot_init(FIG['bdirs'],5,5)
    crd = pmag.get_named_arg_from_sys("-crd", default_val="g")
    if crd == "s":
        coord = "-1"
    elif crd == "t":
        coord = "100"
    else:
        coord = "0"

    fmt = pmag.get_named_arg_from_sys("-fmt", "svg")

    dec_key = 'dir_dec'
    inc_key = 'dir_inc'
    tilt_key = 'dir_tilt_correction'
    #Dir_type_keys=['','site_direction_type','sample_direction_type','specimen_direction_type']

    #
    fnames = {"specimens": spec_file, "samples": samp_file, 'sites': site_file}
    contribution = nb.Contribution(dir_path, custom_filenames=fnames,
                                   single_file=in_file)

    try:
        contribution.propagate_location_to_samples()
        contribution.propagate_location_to_specimens()
        contribution.propagate_location_to_measurements()
    except KeyError as ex:
        pass

    # the object that contains the DataFrame + useful helper methods:
    table_name = list(contribution.tables.keys())[0]
    data_container = contribution.tables[table_name]
    # the actual DataFrame:
    data = data_container.df

    if plot_key != "all" and plot_key not in data.columns:
        print("-E- You can't plot by {} with the data provided".format(plot_key))
        return

    # add tilt key into DataFrame columns if it isn't there already
    if tilt_key not in data.columns:
        data.loc[:, tilt_key] = None

    if verbose:
        print(len(data), ' records read from ', in_file)

    # find desired dec,inc data:
    dir_type_key = ''
    #
    # get plotlist if not plotting all records
    #
    plotlist=[]
    if plot_key != "all":
        # return all where plot_key is not blank
        if plot_key not in data.columns:
            print('Can\'t plot by "{}".  That header is not in infile: {}'.format(plot_key, in_file))
            return
        plots = data[data[plot_key].notnull()]
        plotlist = plots[plot_key].unique() # grab unique values
    else:
        plotlist.append('All')

    for plot in plotlist:
        if verbose:
            print(plot)
        if plot == 'All':
            # plot everything at once
            plot_data = data
        else:
            # pull out only partial data
            plot_data = data[data[plot_key] == plot]

        DIblock = []
        GCblock = []
        # SLblock, SPblock = [], []
        title = plot
        mode = 1
        k = 0


        if dec_key not in plot_data.columns:
            print("-W- No dec/inc data")
            continue
        # get all records where dec & inc values exist
        plot_data = plot_data[plot_data[dec_key].notnull() & plot_data[inc_key].notnull()]
        if plot_data.empty:
            continue
        # this sorting out is done in get_di_bock
        #if coord == '0':  # geographic, use records with no tilt key (or tilt_key 0)
        #    cond1 = plot_data[tilt_key].fillna('') == coord
        #    cond2 = plot_data[tilt_key].isnull()
        #    plot_data = plot_data[cond1 | cond2]
        #else:  # not geographic coordinates, use only records with correct tilt_key
        #    plot_data = plot_data[plot_data[tilt_key] == coord]

        # get metadata for naming the plot file
        locations = data_container.get_name('location', df_slice=plot_data)
        site = data_container.get_name('site', df_slice=plot_data)
        sample = data_container.get_name('sample', df_slice=plot_data)
        specimen = data_container.get_name('specimen', df_slice=plot_data)

        # make sure method_codes is in plot_data
        if 'method_codes' not in plot_data.columns:
            plot_data['method_codes'] = ''

        # get data blocks
        DIblock = data_container.get_di_block(df_slice=plot_data,
                                              tilt_corr=coord, excl=['DE-BFP'])
        #SLblock = [[ind, row['method_codes']] for ind, row in plot_data.iterrows()]
        # get great circles
        great_circle_data = data_container.get_records_for_code('DE-BFP', incl=True,
                                                                use_slice=True, sli=plot_data)

        if len(great_circle_data) > 0:
            gc_cond = great_circle_data[tilt_key] == coord
            GCblock = [[float(row[dec_key]), float(row[inc_key])] for ind, row in great_circle_data[gc_cond].iterrows()]
            #SPblock = [[ind, row['method_codes']] for ind, row in great_circle_data[gc_cond].iterrows()]

        if len(DIblock) > 0:
            if contour == 0:
                pmagplotlib.plotEQ(FIG['eqarea'], DIblock, title)
            else:
                pmagplotlib.plotEQcont(FIG['eqarea'], DIblock)
        else:
            pmagplotlib.plotNET(FIG['eqarea'])
        if len(GCblock)>0:
            for rec in GCblock:
                pmagplotlib.plotC(FIG['eqarea'], rec, 90., 'g')
        if len(DIblock) == 0 and len(GCblock) == 0:
            if verbose:
                print("no records for plotting")
            continue
            #sys.exit()
        if plotE == 1:
            ppars = pmag.doprinc(DIblock) # get principal directions
            nDIs, rDIs, npars, rpars = [], [], [], []
            for rec in DIblock:
                angle=pmag.angle([rec[0],rec[1]],[ppars['dec'],ppars['inc']])
                if angle>90.:
                    rDIs.append(rec)
                else:
                    nDIs.append(rec)
            if dist=='B': # do on whole dataset
                etitle="Bingham confidence ellipse"
                bpars=pmag.dobingham(DIblock)
                for key in list(bpars.keys()):
                    if key!='n' and verbose: print("    ",key, '%7.1f'%(bpars[key]))
                    if key=='n' and verbose: print("    ",key, '       %i'%(bpars[key]))
                npars.append(bpars['dec'])
                npars.append(bpars['inc'])
                npars.append(bpars['Zeta'])
                npars.append(bpars['Zdec'])
                npars.append(bpars['Zinc'])
                npars.append(bpars['Eta'])
                npars.append(bpars['Edec'])
                npars.append(bpars['Einc'])
            if dist=='F':
                etitle="Fisher confidence cone"
                if len(nDIs)>2:
                    fpars=pmag.fisher_mean(nDIs)
                    for key in list(fpars.keys()):
                        if key!='n' and verbose: print("    ",key, '%7.1f'%(fpars[key]))
                        if key=='n' and verbose: print("    ",key, '       %i'%(fpars[key]))
                    mode+=1
                    npars.append(fpars['dec'])
                    npars.append(fpars['inc'])
                    npars.append(fpars['alpha95']) # Beta
                    npars.append(fpars['dec'])
                    isign=old_div(abs(fpars['inc']),fpars['inc'])
                    npars.append(fpars['inc']-isign*90.) #Beta inc
                    npars.append(fpars['alpha95']) # gamma
                    npars.append(fpars['dec']+90.) # Beta dec
                    npars.append(0.) #Beta inc
                if len(rDIs)>2:
                    fpars=pmag.fisher_mean(rDIs)
                    if verbose: print("mode ",mode)
                    for key in list(fpars.keys()):
                        if key!='n' and verbose: print("    ",key, '%7.1f'%(fpars[key]))
                        if key=='n' and verbose: print("    ",key, '       %i'%(fpars[key]))
                    mode+=1
                    rpars.append(fpars['dec'])
                    rpars.append(fpars['inc'])
                    rpars.append(fpars['alpha95']) # Beta
                    rpars.append(fpars['dec'])
                    isign=old_div(abs(fpars['inc']),fpars['inc'])
                    rpars.append(fpars['inc']-isign*90.) #Beta inc
                    rpars.append(fpars['alpha95']) # gamma
                    rpars.append(fpars['dec']+90.) # Beta dec
                    rpars.append(0.) #Beta inc
            if dist=='K':
                etitle="Kent confidence ellipse"
                if len(nDIs)>3:
                    kpars=pmag.dokent(nDIs,len(nDIs))
                    if verbose: print("mode ",mode)
                    for key in list(kpars.keys()):
                        if key!='n' and verbose: print("    ",key, '%7.1f'%(kpars[key]))
                        if key=='n' and verbose: print("    ",key, '       %i'%(kpars[key]))
                    mode+=1
                    npars.append(kpars['dec'])
                    npars.append(kpars['inc'])
                    npars.append(kpars['Zeta'])
                    npars.append(kpars['Zdec'])
                    npars.append(kpars['Zinc'])
                    npars.append(kpars['Eta'])
                    npars.append(kpars['Edec'])
                    npars.append(kpars['Einc'])
                if len(rDIs)>3:
                    kpars=pmag.dokent(rDIs,len(rDIs))
                    if verbose: print("mode ",mode)
                    for key in list(kpars.keys()):
                        if key!='n' and verbose: print("    ",key, '%7.1f'%(kpars[key]))
                        if key=='n' and verbose: print("    ",key, '       %i'%(kpars[key]))
                    mode+=1
                    rpars.append(kpars['dec'])
                    rpars.append(kpars['inc'])
                    rpars.append(kpars['Zeta'])
                    rpars.append(kpars['Zdec'])
                    rpars.append(kpars['Zinc'])
                    rpars.append(kpars['Eta'])
                    rpars.append(kpars['Edec'])
                    rpars.append(kpars['Einc'])
            else: # assume bootstrap
                if dist=='BE':
                    if len(nDIs)>5:
                        BnDIs=pmag.di_boot(nDIs)
                        Bkpars=pmag.dokent(BnDIs,1.)
                        if verbose: print("mode ",mode)
                        for key in list(Bkpars.keys()):
                            if key!='n' and verbose: print("    ",key, '%7.1f'%(Bkpars[key]))
                            if key=='n' and verbose: print("    ",key, '       %i'%(Bkpars[key]))
                        mode+=1
                        npars.append(Bkpars['dec'])
                        npars.append(Bkpars['inc'])
                        npars.append(Bkpars['Zeta'])
                        npars.append(Bkpars['Zdec'])
                        npars.append(Bkpars['Zinc'])
                        npars.append(Bkpars['Eta'])
                        npars.append(Bkpars['Edec'])
                        npars.append(Bkpars['Einc'])
                    if len(rDIs)>5:
                        BrDIs=pmag.di_boot(rDIs)
                        Bkpars=pmag.dokent(BrDIs,1.)
                        if verbose: print("mode ",mode)
                        for key in list(Bkpars.keys()):
                            if key!='n' and verbose: print("    ",key, '%7.1f'%(Bkpars[key]))
                            if key=='n' and verbose: print("    ",key, '       %i'%(Bkpars[key]))
                        mode+=1
                        rpars.append(Bkpars['dec'])
                        rpars.append(Bkpars['inc'])
                        rpars.append(Bkpars['Zeta'])
                        rpars.append(Bkpars['Zdec'])
                        rpars.append(Bkpars['Zinc'])
                        rpars.append(Bkpars['Eta'])
                        rpars.append(Bkpars['Edec'])
                        rpars.append(Bkpars['Einc'])
                    etitle="Bootstrapped confidence ellipse"
                elif dist=='BV':
                    sym={'lower':['o','c'],'upper':['o','g'],'size':3,'edgecolor':'face'}
                    if len(nDIs)>5:
                        BnDIs=pmag.di_boot(nDIs)
                        pmagplotlib.plotEQsym(FIG['bdirs'],BnDIs,'Bootstrapped Eigenvectors', sym)
                    if len(rDIs)>5:
                        BrDIs=pmag.di_boot(rDIs)
                        if len(nDIs)>5:  # plot on existing plots
                            pmagplotlib.plotDIsym(FIG['bdirs'],BrDIs,sym)
                        else:
                            pmagplotlib.plotEQ(FIG['bdirs'],BrDIs,'Bootstrapped Eigenvectors')
            if dist=='B':
                if len(nDIs)> 3 or len(rDIs)>3: pmagplotlib.plotCONF(FIG['eqarea'],etitle,[],npars,0)
            elif len(nDIs)>3 and dist!='BV':
                pmagplotlib.plotCONF(FIG['eqarea'],etitle,[],npars,0)
                if len(rDIs)>3:
                    pmagplotlib.plotCONF(FIG['eqarea'],etitle,[],rpars,0)
            elif len(rDIs)>3 and dist!='BV':
                pmagplotlib.plotCONF(FIG['eqarea'],etitle,[],rpars,0)

        for key in list(FIG.keys()):
            files = {}
            filename = pmag.get_named_arg_from_sys('-fname')
            if filename: # use provided filename
                filename+= '.' + fmt
            elif pmagplotlib.isServer: # use server plot naming convention
                filename='LO:_'+locations+'_SI:_'+site+'_SA:_'+sample+'_SP:_'+specimen+'_CO:_'+crd+'_TY:_'+key+'_.'+fmt
            elif plot_key == 'all':
                filename = 'all'
                if 'location' in plot_data.columns:
                    locs = plot_data['location'].unique()
                    loc_string = "_".join([loc.replace(' ', '_') for loc in locs])
                    filename += "_" + loc_string
                filename += "_" + crd + "_" + key
                filename += ".{}".format(fmt)
            else: # use more readable naming convention
                filename = ''
                # fix this if plot_by is location , for example
                use_names = {'location': [locations], 'site': [locations, site],
                             'sample': [locations, site, sample],
                             'specimen': [locations, site, sample, specimen]}
                use = use_names[plot_key]
                use.extend([crd, key])
                for item in use: #[locations, site, sample, specimen, crd, key]:
                    if item:
                        item = item.replace(' ', '_')
                        filename += item + '_'
                if filename.endswith('_'):
                    filename = filename[:-1]
                filename += ".{}".format(fmt)

            files[key]=filename

        if pmagplotlib.isServer:
            black     = '#000000'
            purple    = '#800080'
            titles={}
            titles['eq']='Equal Area Plot'
            FIG = pmagplotlib.addBorders(FIG,titles,black,purple)
            pmagplotlib.saveP(FIG,files)

        if plt:
            pmagplotlib.saveP(FIG,files)
            continue
        if verbose:
            pmagplotlib.drawFIGS(FIG)
            ans=input(" S[a]ve to save plot, [q]uit, Return to continue:  ")
            if ans == "q":
                sys.exit()
            if ans == "a":
                pmagplotlib.saveP(FIG,files)
        continue
Ejemplo n.º 3
0
def main():
    """
    NAME
       plotdi_e.py

    DESCRIPTION
       plots equal area projection  from dec inc data and cones of confidence 
           (Fisher, kent or Bingham or bootstrap).

    INPUT FORMAT
       takes dec/inc as first two columns in space delimited file

    SYNTAX
       plotdi_e.py [command line options]

    OPTIONS
        -h prints help message and quits
        -i for interactive parameter entry
        -f FILE, sets input filename on command line 
        -Fish plots unit vector mean direction, alpha95
        -Bing plots Principal direction, Bingham confidence ellipse
        -Kent plots unit vector mean direction, confidence ellipse
        -Boot E plots unit vector mean direction, bootstrapped confidence ellipse
        -Boot V plots  unit vector mean direction, distribution of bootstrapped means

    """
    dist = 'F'  # default distribution is Fisherian
    mode = 1
    EQ = {'eq': 1}
    if len(sys.argv) > 0:
        if '-h' in sys.argv:  # check if help is needed
            print main.__doc__
            sys.exit()  # graceful quit
        if '-i' in sys.argv:  # ask for filename
            file = raw_input("Enter file name with dec, inc data: ")
            dist = raw_input(
                "Enter desired distrubution: [Fish]er, [Bing]ham, [Kent] [Boot] [default is Fisher]: "
            )
            if dist == "": dist = "F"
            if dist == "Boot":
                type = raw_input(
                    " Ellipses or distribution of vectors? [E]/V ")
                if type == "" or type == "E":
                    dist = "BE"
                else:
                    dist = "BE"
        else:
            #
            if '-f' in sys.argv:
                ind = sys.argv.index('-f')
                file = sys.argv[ind + 1]
            else:
                print 'you must specify a file name'
                print main.__doc__
                sys.exit()
            if '-Bing' in sys.argv: dist = 'B'
            if '-Kent' in sys.argv: dist = 'K'
            if '-Boot' in sys.argv:
                ind = sys.argv.index('-Boot')
                type = sys.argv[ind + 1]
                if type == 'E':
                    dist = 'BE'
                elif type == 'V':
                    dist = 'BV'
                    EQ['bdirs'] = 2
                    pmagplotlib.plot_init(EQ['bdirs'], 5, 5)
                else:
                    print main.__doc__
                    sys.exit()
    pmagplotlib.plot_init(EQ['eq'], 5, 5)
    #
    # get to work
    f = open(file, 'r')
    data = f.readlines()
    #
    DIs = []  # set up list for dec inc data
    DiRecs = []
    pars = []
    nDIs, rDIs, npars, rpars = [], [], [], []
    mode = 1
    for line in data:  # read in the data from standard input
        DiRec = {}
        rec = line.split()  # split each line on space to get records
        DIs.append((float(rec[0]), float(rec[1]), 1.))
        DiRec['dec'] = rec[0]
        DiRec['inc'] = rec[1]
        DiRec['direction_type'] = 'l'
        DiRecs.append(DiRec)
    # split into two modes
    ppars = pmag.doprinc(DIs)  # get principal directions
    for rec in DIs:
        angle = pmag.angle([rec[0], rec[1]], [ppars['dec'], ppars['inc']])
        if angle > 90.:
            rDIs.append(rec)
        else:
            nDIs.append(rec)
    if dist == 'B':  # do on whole dataset
        title = "Bingham confidence ellipse"
        bpars = pmag.dobingham(DIs)
        for key in bpars.keys():
            if key != 'n': print "    ", key, '%7.1f' % (bpars[key])
            if key == 'n': print "    ", key, '       %i' % (bpars[key])
        npars.append(bpars['dec'])
        npars.append(bpars['inc'])
        npars.append(bpars['Zeta'])
        npars.append(bpars['Zdec'])
        npars.append(bpars['Zinc'])
        npars.append(bpars['Eta'])
        npars.append(bpars['Edec'])
        npars.append(bpars['Einc'])
    if dist == 'F':
        title = "Fisher confidence cone"
        if len(nDIs) > 3:
            fpars = pmag.fisher_mean(nDIs)
            print "mode ", mode
            for key in fpars.keys():
                if key != 'n': print "    ", key, '%7.1f' % (fpars[key])
                if key == 'n': print "    ", key, '       %i' % (fpars[key])
            mode += 1
            npars.append(fpars['dec'])
            npars.append(fpars['inc'])
            npars.append(fpars['alpha95'])  # Beta
            npars.append(fpars['dec'])
            isign = abs(fpars['inc']) / fpars['inc']
            npars.append(fpars['inc'] - isign * 90.)  #Beta inc
            npars.append(fpars['alpha95'])  # gamma
            npars.append(fpars['dec'] + 90.)  # Beta dec
            npars.append(0.)  #Beta inc
        if len(rDIs) > 3:
            fpars = pmag.fisher_mean(rDIs)
            print "mode ", mode
            for key in fpars.keys():
                if key != 'n': print "    ", key, '%7.1f' % (fpars[key])
                if key == 'n': print "    ", key, '       %i' % (fpars[key])
            mode += 1
            rpars.append(fpars['dec'])
            rpars.append(fpars['inc'])
            rpars.append(fpars['alpha95'])  # Beta
            rpars.append(fpars['dec'])
            isign = abs(fpars['inc']) / fpars['inc']
            rpars.append(fpars['inc'] - isign * 90.)  #Beta inc
            rpars.append(fpars['alpha95'])  # gamma
            rpars.append(fpars['dec'] + 90.)  # Beta dec
            rpars.append(0.)  #Beta inc
    if dist == 'K':
        title = "Kent confidence ellipse"
        if len(nDIs) > 3:
            kpars = pmag.dokent(nDIs, len(nDIs))
            print "mode ", mode
            for key in kpars.keys():
                if key != 'n': print "    ", key, '%7.1f' % (kpars[key])
                if key == 'n': print "    ", key, '       %i' % (kpars[key])
            mode += 1
            npars.append(kpars['dec'])
            npars.append(kpars['inc'])
            npars.append(kpars['Zeta'])
            npars.append(kpars['Zdec'])
            npars.append(kpars['Zinc'])
            npars.append(kpars['Eta'])
            npars.append(kpars['Edec'])
            npars.append(kpars['Einc'])
        if len(rDIs) > 3:
            kpars = pmag.dokent(rDIs, len(rDIs))
            print "mode ", mode
            for key in kpars.keys():
                if key != 'n': print "    ", key, '%7.1f' % (kpars[key])
                if key == 'n': print "    ", key, '       %i' % (kpars[key])
            mode += 1
            rpars.append(kpars['dec'])
            rpars.append(kpars['inc'])
            rpars.append(kpars['Zeta'])
            rpars.append(kpars['Zdec'])
            rpars.append(kpars['Zinc'])
            rpars.append(kpars['Eta'])
            rpars.append(kpars['Edec'])
            rpars.append(kpars['Einc'])
    else:  # assume bootstrap
        if dist == 'BE':
            if len(nDIs) > 5:
                BnDIs = pmag.di_boot(nDIs)
                Bkpars = pmag.dokent(BnDIs, 1.)
                print "mode ", mode
                for key in Bkpars.keys():
                    if key != 'n': print "    ", key, '%7.1f' % (Bkpars[key])
                    if key == 'n':
                        print "    ", key, '       %i' % (Bkpars[key])
                mode += 1
                npars.append(Bkpars['dec'])
                npars.append(Bkpars['inc'])
                npars.append(Bkpars['Zeta'])
                npars.append(Bkpars['Zdec'])
                npars.append(Bkpars['Zinc'])
                npars.append(Bkpars['Eta'])
                npars.append(Bkpars['Edec'])
                npars.append(Bkpars['Einc'])
            if len(rDIs) > 5:
                BrDIs = pmag.di_boot(rDIs)
                Bkpars = pmag.dokent(BrDIs, 1.)
                print "mode ", mode
                for key in Bkpars.keys():
                    if key != 'n': print "    ", key, '%7.1f' % (Bkpars[key])
                    if key == 'n':
                        print "    ", key, '       %i' % (Bkpars[key])
                mode += 1
                rpars.append(Bkpars['dec'])
                rpars.append(Bkpars['inc'])
                rpars.append(Bkpars['Zeta'])
                rpars.append(Bkpars['Zdec'])
                rpars.append(Bkpars['Zinc'])
                rpars.append(Bkpars['Eta'])
                rpars.append(Bkpars['Edec'])
                rpars.append(Bkpars['Einc'])
            title = "Bootstrapped confidence ellipse"
        elif dist == 'BV':
            if len(nDIs) > 5:
                pmagplotlib.plotEQ(EQ['eq'], nDIs, 'Data')
                BnDIs = pmag.di_boot(nDIs)
                pmagplotlib.plotEQ(EQ['bdirs'], BnDIs,
                                   'Bootstrapped Eigenvectors')
            if len(rDIs) > 5:
                BrDIs = pmag.di_boot(rDIs)
                if len(nDIs) > 5:  # plot on existing plots
                    pmagplotlib.plotDI(EQ['eq'], rDIs)
                    pmagplotlib.plotDI(EQ['bdirs'], BrDIs)
                else:
                    pmagplotlib.plotEQ(EQ['eq'], rDIs, 'Data')
                    pmagplotlib.plotEQ(EQ['bdirs'], BrDIs,
                                       'Bootstrapped Eigenvectors')
            pmagplotlib.drawFIGS(EQ)
            ans = raw_input('s[a]ve, [q]uit ')
            if ans == 'q': sys.exit()
            if ans == 'a':
                files = {}
                for key in EQ.keys():
                    files[key] = 'BE_' + key + '.svg'
                pmagplotlib.saveP(EQ, files)
            sys.exit()
    if len(nDIs) > 5:
        pmagplotlib.plotCONF(EQ['eq'], title, DiRecs, npars, 1)
        if len(rDIs) > 5 and dist != 'B':
            pmagplotlib.plotCONF(EQ['eq'], title, [], rpars, 0)
    elif len(rDIs) > 5 and dist != 'B':
        pmagplotlib.plotCONF(EQ['eq'], title, DiRecs, rpars, 1)
    pmagplotlib.drawFIGS(EQ)
    ans = raw_input('s[a]ve, [q]uit ')
    if ans == 'q': sys.exit()
    if ans == 'a':
        files = {}
        for key in EQ.keys():
            files[key] = key + '.svg'
        pmagplotlib.saveP(EQ, files)
Ejemplo n.º 4
0
def main():
    """
    NAME
        eqarea_ell.py

    DESCRIPTION
       makes equal area projections from declination/inclination data
       and plot ellipses

    SYNTAX
        eqarea_ell.py -h [command line options]

    INPUT
       takes space delimited Dec/Inc data

    OPTIONS
        -h prints help message and quits
        -f FILE
        -fmt [svg,png,jpg] format for output plots
        -sav  saves figures and quits
        -ell [F,K,B,Be,Bv] plot Fisher, Kent, Bingham, Bootstrap ellipses or Boostrap eigenvectors
    """
    FIG={} # plot dictionary
    FIG['eq']=1 # eqarea is figure 1
    fmt,dist,mode,plot='svg','F',1,0
    sym={'lower':['o','r'],'upper':['o','w'],'size':10}
    plotE=0
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if not set_env.IS_WIN:
        pmagplotlib.plot_init(FIG['eq'],5,5)
    if '-sav' in sys.argv:plot=1
    if '-f' in sys.argv:
        ind=sys.argv.index("-f")
        title=sys.argv[ind+1]
        data=numpy.loadtxt(title).transpose()
    if '-ell' in sys.argv:
        plotE=1
        ind=sys.argv.index('-ell')
        ell_type=sys.argv[ind+1]
        if ell_type=='F':dist='F'
        if ell_type=='K':dist='K'
        if ell_type=='B':dist='B'
        if ell_type=='Be':dist='BE'
        if ell_type=='Bv':
            dist='BV'
            FIG['bdirs']=2
            pmagplotlib.plot_init(FIG['bdirs'],5,5)
    if '-fmt' in sys.argv:
        ind=sys.argv.index("-fmt")
        fmt=sys.argv[ind+1]
    DIblock=numpy.array([data[0],data[1]]).transpose()
    if len(DIblock)>0:
        pmagplotlib.plot_eq_sym(FIG['eq'],DIblock,title,sym)
        #if plot==0:pmagplotlib.draw_figs(FIG)
    else:
        print("no data to plot")
        sys.exit()
    if plotE==1:
        ppars=pmag.doprinc(DIblock) # get principal directions
        nDIs,rDIs,npars,rpars=[],[],[],[]
        for rec in DIblock:
            angle=pmag.angle([rec[0],rec[1]],[ppars['dec'],ppars['inc']])
            if angle>90.:
                rDIs.append(rec)
            else:
                nDIs.append(rec)
        if dist=='B': # do on whole dataset
            etitle="Bingham confidence ellipse"
            bpars=pmag.dobingham(DIblock)
            for key in list(bpars.keys()):
                if key!='n' and pmagplotlib.verbose:print("    ",key, '%7.1f'%(bpars[key]))
                if key=='n' and pmagplotlib.verbose:print("    ",key, '       %i'%(bpars[key]))
            npars.append(bpars['dec'])
            npars.append(bpars['inc'])
            npars.append(bpars['Zeta'])
            npars.append(bpars['Zdec'])
            npars.append(bpars['Zinc'])
            npars.append(bpars['Eta'])
            npars.append(bpars['Edec'])
            npars.append(bpars['Einc'])
        if dist=='F':
            etitle="Fisher confidence cone"
            if len(nDIs)>3:
                fpars=pmag.fisher_mean(nDIs)
                for key in list(fpars.keys()):
                    if key!='n' and pmagplotlib.verbose:print("    ",key, '%7.1f'%(fpars[key]))
                    if key=='n' and pmagplotlib.verbose:print("    ",key, '       %i'%(fpars[key]))
                mode+=1
                npars.append(fpars['dec'])
                npars.append(fpars['inc'])
                npars.append(fpars['alpha95']) # Beta
                npars.append(fpars['dec'])
                isign=abs(fpars['inc']) / fpars['inc']
                npars.append(fpars['inc']-isign*90.) #Beta inc
                npars.append(fpars['alpha95']) # gamma
                npars.append(fpars['dec']+90.) # Beta dec
                npars.append(0.) #Beta inc
            if len(rDIs)>3:
                fpars=pmag.fisher_mean(rDIs)
                if pmagplotlib.verbose:print("mode ",mode)
                for key in list(fpars.keys()):
                    if key!='n' and pmagplotlib.verbose:print("    ",key, '%7.1f'%(fpars[key]))
                    if key=='n' and pmagplotlib.verbose:print("    ",key, '       %i'%(fpars[key]))
                mode+=1
                rpars.append(fpars['dec'])
                rpars.append(fpars['inc'])
                rpars.append(fpars['alpha95']) # Beta
                rpars.append(fpars['dec'])
                isign=abs(fpars['inc']) / fpars['inc']
                rpars.append(fpars['inc']-isign*90.) #Beta inc
                rpars.append(fpars['alpha95']) # gamma
                rpars.append(fpars['dec']+90.) # Beta dec
                rpars.append(0.) #Beta inc
        if dist=='K':
            etitle="Kent confidence ellipse"
            if len(nDIs)>3:
                kpars=pmag.dokent(nDIs,len(nDIs))
                if pmagplotlib.verbose:print("mode ",mode)
                for key in list(kpars.keys()):
                    if key!='n' and pmagplotlib.verbose:print("    ",key, '%7.1f'%(kpars[key]))
                    if key=='n' and pmagplotlib.verbose:print("    ",key, '       %i'%(kpars[key]))
                mode+=1
                npars.append(kpars['dec'])
                npars.append(kpars['inc'])
                npars.append(kpars['Zeta'])
                npars.append(kpars['Zdec'])
                npars.append(kpars['Zinc'])
                npars.append(kpars['Eta'])
                npars.append(kpars['Edec'])
                npars.append(kpars['Einc'])
            if len(rDIs)>3:
                kpars=pmag.dokent(rDIs,len(rDIs))
                if pmagplotlib.verbose:print("mode ",mode)
                for key in list(kpars.keys()):
                    if key!='n' and pmagplotlib.verbose:print("    ",key, '%7.1f'%(kpars[key]))
                    if key=='n' and pmagplotlib.verbose:print("    ",key, '       %i'%(kpars[key]))
                mode+=1
                rpars.append(kpars['dec'])
                rpars.append(kpars['inc'])
                rpars.append(kpars['Zeta'])
                rpars.append(kpars['Zdec'])
                rpars.append(kpars['Zinc'])
                rpars.append(kpars['Eta'])
                rpars.append(kpars['Edec'])
                rpars.append(kpars['Einc'])
        else: # assume bootstrap
            if len(nDIs)<10 and len(rDIs)<10:
                print('too few data points for bootstrap')
                sys.exit()
            if dist=='BE':
                print('Be patient for bootstrap...')
                if len(nDIs)>=10:
                    BnDIs=pmag.di_boot(nDIs)
                    Bkpars=pmag.dokent(BnDIs,1.)
                    if pmagplotlib.verbose:print("mode ",mode)
                    for key in list(Bkpars.keys()):
                        if key!='n' and pmagplotlib.verbose:print("    ",key, '%7.1f'%(Bkpars[key]))
                        if key=='n' and pmagplotlib.verbose:print("    ",key, '       %i'%(Bkpars[key]))
                    mode+=1
                    npars.append(Bkpars['dec'])
                    npars.append(Bkpars['inc'])
                    npars.append(Bkpars['Zeta'])
                    npars.append(Bkpars['Zdec'])
                    npars.append(Bkpars['Zinc'])
                    npars.append(Bkpars['Eta'])
                    npars.append(Bkpars['Edec'])
                    npars.append(Bkpars['Einc'])
                if len(rDIs)>=10:
                    BrDIs=pmag.di_boot(rDIs)
                    Bkpars=pmag.dokent(BrDIs,1.)
                    if pmagplotlib.verbose:print("mode ",mode)
                    for key in list(Bkpars.keys()):
                        if key!='n' and pmagplotlib.verbose:print("    ",key, '%7.1f'%(Bkpars[key]))
                        if key=='n' and pmagplotlib.verbose:print("    ",key, '       %i'%(Bkpars[key]))
                    mode+=1
                    rpars.append(Bkpars['dec'])
                    rpars.append(Bkpars['inc'])
                    rpars.append(Bkpars['Zeta'])
                    rpars.append(Bkpars['Zdec'])
                    rpars.append(Bkpars['Zinc'])
                    rpars.append(Bkpars['Eta'])
                    rpars.append(Bkpars['Edec'])
                    rpars.append(Bkpars['Einc'])
                etitle="Bootstrapped confidence ellipse"
            elif dist=='BV':
                print('Be patient for bootstrap...')
                vsym={'lower':['+','k'],'upper':['x','k'],'size':5}
                if len(nDIs)>5:
                    BnDIs=pmag.di_boot(nDIs)
                    pmagplotlib.plot_eq_sym(FIG['bdirs'],BnDIs,'Bootstrapped Eigenvectors',vsym)
                if len(rDIs)>5:
                    BrDIs=pmag.di_boot(rDIs)
                    if len(nDIs)>5:  # plot on existing plots
                        pmagplotlib.plot_di_sym(FIG['bdirs'],BrDIs,vsym)
                    else:
                        pmagplotlib.plot_eq(FIG['bdirs'],BrDIs,'Bootstrapped Eigenvectors',vsym)
        if dist=='B':
            if len(nDIs)> 3 or len(rDIs)>3: pmagplotlib.plot_conf(FIG['eq'],etitle,[],npars,0)
        elif len(nDIs)>3 and dist!='BV':
            pmagplotlib.plot_conf(FIG['eq'],etitle,[],npars,0)
            if len(rDIs)>3:
                pmagplotlib.plot_conf(FIG['eq'],etitle,[],rpars,0)
        elif len(rDIs)>3 and dist!='BV':
            pmagplotlib.plot_conf(FIG['eq'],etitle,[],rpars,0)
        #if plot==0:pmagplotlib.draw_figs(FIG)
    if plot==0:pmagplotlib.draw_figs(FIG)
        #
    files={}
    for key in list(FIG.keys()):
        files[key]=title+'_'+key+'.'+fmt
    if pmagplotlib.isServer:
        black     = '#000000'
        purple    = '#800080'
        titles={}
        titles['eq']='Equal Area Plot'
        FIG = pmagplotlib.add_borders(FIG,titles,black,purple)
        pmagplotlib.save_plots(FIG,files)
    elif plot==0:
        ans=input(" S[a]ve to save plot, [q]uit, Return to continue:  ")
        if ans=="q": sys.exit()
        if ans=="a":
            pmagplotlib.save_plots(FIG,files)
    else:
        pmagplotlib.save_plots(FIG,files)
Ejemplo n.º 5
0
def main():
    """
    NAME
       plotdi_e.py

    DESCRIPTION
       plots equal area projection  from dec inc data and cones of confidence 
           (Fisher, kent or Bingham or bootstrap).

    INPUT FORMAT
       takes dec/inc as first two columns in space delimited file

    SYNTAX
       plotdi_e.py [command line options]

    OPTIONS
        -h prints help message and quits
        -i for interactive parameter entry
        -f FILE, sets input filename on command line 
        -Fish plots unit vector mean direction, alpha95
        -Bing plots Principal direction, Bingham confidence ellipse
        -Kent plots unit vector mean direction, confidence ellipse
        -Boot E plots unit vector mean direction, bootstrapped confidence ellipse
        -Boot V plots  unit vector mean direction, distribution of bootstrapped means

    """
    dist='F' # default distribution is Fisherian
    mode=1
    EQ={'eq':1}
    if len(sys.argv) > 0:
        if '-h' in sys.argv: # check if help is needed
            print main.__doc__
            sys.exit() # graceful quit
        if '-i' in sys.argv: # ask for filename
            file=raw_input("Enter file name with dec, inc data: ")
            dist=raw_input("Enter desired distrubution: [Fish]er, [Bing]ham, [Kent] [Boot] [default is Fisher]: ")
            if dist=="":dist="F"
            if dist=="Boot":
                 type=raw_input(" Ellipses or distribution of vectors? [E]/V ")
                 if type=="" or type=="E":
                     dist="BE"
                 else:
                     dist="BE"
        else:
#
            if '-f' in sys.argv:
                ind=sys.argv.index('-f')
                file=sys.argv[ind+1]
            else:
                print 'you must specify a file name'
                print main.__doc__
                sys.exit()
            if '-Bing' in sys.argv:dist='B'
            if '-Kent' in sys.argv:dist='K'
            if '-Boot' in sys.argv:
                ind=sys.argv.index('-Boot')
                type=sys.argv[ind+1]
                if type=='E': 
                    dist='BE'
                elif type=='V': 
                    dist='BV'
                    EQ['bdirs']=2
                    pmagplotlib.plot_init(EQ['bdirs'],5,5)
                else:
                    print main.__doc__
                    sys.exit()
    pmagplotlib.plot_init(EQ['eq'],5,5)
#
# get to work
    f=open(file,'r')
    data=f.readlines()
#
    DIs= [] # set up list for dec inc data
    DiRecs=[]
    pars=[]
    nDIs,rDIs,npars,rpars=[],[],[],[]
    mode =1
    for line in data:   # read in the data from standard input
        DiRec={}
        rec=line.split() # split each line on space to get records
        DIs.append((float(rec[0]),float(rec[1]),1.))
        DiRec['dec']=rec[0]
        DiRec['inc']=rec[1]
        DiRec['direction_type']='l'
        DiRecs.append(DiRec)
    # split into two modes
    ppars=pmag.doprinc(DIs) # get principal directions
    for rec in DIs:
        angle=pmag.angle([rec[0],rec[1]],[ppars['dec'],ppars['inc']])
        if angle>90.:
            rDIs.append(rec)
        else:
            nDIs.append(rec)
    if dist=='B': # do on whole dataset
        title="Bingham confidence ellipse"
        bpars=pmag.dobingham(DIs)
        for key in bpars.keys():
            if key!='n':print "    ",key, '%7.1f'%(bpars[key])
            if key=='n':print "    ",key, '       %i'%(bpars[key])
        npars.append(bpars['dec']) 
        npars.append(bpars['inc'])
        npars.append(bpars['Zeta']) 
        npars.append(bpars['Zdec']) 
        npars.append(bpars['Zinc'])
        npars.append(bpars['Eta']) 
        npars.append(bpars['Edec']) 
        npars.append(bpars['Einc'])
    if dist=='F':
        title="Fisher confidence cone"
        if len(nDIs)>3:
            fpars=pmag.fisher_mean(nDIs)
            print "mode ",mode
            for key in fpars.keys():
                if key!='n':print "    ",key, '%7.1f'%(fpars[key])
                if key=='n':print "    ",key, '       %i'%(fpars[key])
            mode+=1
            npars.append(fpars['dec']) 
            npars.append(fpars['inc'])
            npars.append(fpars['alpha95']) # Beta
            npars.append(fpars['dec']) 
            isign=abs(fpars['inc'])/fpars['inc'] 
            npars.append(fpars['inc']-isign*90.) #Beta inc
            npars.append(fpars['alpha95']) # gamma 
            npars.append(fpars['dec']+90.) # Beta dec
            npars.append(0.) #Beta inc
        if len(rDIs)>3:
            fpars=pmag.fisher_mean(rDIs)
            print "mode ",mode
            for key in fpars.keys():
                if key!='n':print "    ",key, '%7.1f'%(fpars[key])
                if key=='n':print "    ",key, '       %i'%(fpars[key])
            mode+=1
            rpars.append(fpars['dec']) 
            rpars.append(fpars['inc'])
            rpars.append(fpars['alpha95']) # Beta
            rpars.append(fpars['dec']) 
            isign=abs(fpars['inc'])/fpars['inc'] 
            rpars.append(fpars['inc']-isign*90.) #Beta inc
            rpars.append(fpars['alpha95']) # gamma 
            rpars.append(fpars['dec']+90.) # Beta dec
            rpars.append(0.) #Beta inc
    if dist=='K':
        title="Kent confidence ellipse"
        if len(nDIs)>3:
            kpars=pmag.dokent(nDIs,len(nDIs))
            print "mode ",mode
            for key in kpars.keys():
                if key!='n':print "    ",key, '%7.1f'%(kpars[key])
                if key=='n':print "    ",key, '       %i'%(kpars[key])
            mode+=1
            npars.append(kpars['dec']) 
            npars.append(kpars['inc'])
            npars.append(kpars['Zeta']) 
            npars.append(kpars['Zdec']) 
            npars.append(kpars['Zinc'])
            npars.append(kpars['Eta']) 
            npars.append(kpars['Edec']) 
            npars.append(kpars['Einc'])
        if len(rDIs)>3:
            kpars=pmag.dokent(rDIs,len(rDIs))
            print "mode ",mode
            for key in kpars.keys():
                if key!='n':print "    ",key, '%7.1f'%(kpars[key])
                if key=='n':print "    ",key, '       %i'%(kpars[key])
            mode+=1
            rpars.append(kpars['dec']) 
            rpars.append(kpars['inc'])
            rpars.append(kpars['Zeta']) 
            rpars.append(kpars['Zdec']) 
            rpars.append(kpars['Zinc'])
            rpars.append(kpars['Eta']) 
            rpars.append(kpars['Edec']) 
            rpars.append(kpars['Einc'])
    else: # assume bootstrap
        if dist=='BE':
            if len(nDIs)>5:
                BnDIs=pmag.di_boot(nDIs)
                Bkpars=pmag.dokent(BnDIs,1.)
                print "mode ",mode
                for key in Bkpars.keys():
                    if key!='n':print "    ",key, '%7.1f'%(Bkpars[key])
                    if key=='n':print "    ",key, '       %i'%(Bkpars[key])
                mode+=1
                npars.append(Bkpars['dec']) 
                npars.append(Bkpars['inc'])
                npars.append(Bkpars['Zeta']) 
                npars.append(Bkpars['Zdec']) 
                npars.append(Bkpars['Zinc'])
                npars.append(Bkpars['Eta']) 
                npars.append(Bkpars['Edec']) 
                npars.append(Bkpars['Einc'])
            if len(rDIs)>5:
                BrDIs=pmag.di_boot(rDIs)
                Bkpars=pmag.dokent(BrDIs,1.)
                print "mode ",mode
                for key in Bkpars.keys():
                    if key!='n':print "    ",key, '%7.1f'%(Bkpars[key])
                    if key=='n':print "    ",key, '       %i'%(Bkpars[key])
                mode+=1
                rpars.append(Bkpars['dec']) 
                rpars.append(Bkpars['inc'])
                rpars.append(Bkpars['Zeta']) 
                rpars.append(Bkpars['Zdec']) 
                rpars.append(Bkpars['Zinc'])
                rpars.append(Bkpars['Eta']) 
                rpars.append(Bkpars['Edec']) 
                rpars.append(Bkpars['Einc'])
            title="Bootstrapped confidence ellipse"
        elif dist=='BV':
            if len(nDIs)>5:
                pmagplotlib.plotEQ(EQ['eq'],nDIs,'Data')
                BnDIs=pmag.di_boot(nDIs)
                pmagplotlib.plotEQ(EQ['bdirs'],BnDIs,'Bootstrapped Eigenvectors')
            if len(rDIs)>5:
                BrDIs=pmag.di_boot(rDIs)
                if len(nDIs)>5:  # plot on existing plots
                    pmagplotlib.plotDI(EQ['eq'],rDIs)
                    pmagplotlib.plotDI(EQ['bdirs'],BrDIs)
                else:
                    pmagplotlib.plotEQ(EQ['eq'],rDIs,'Data')
                    pmagplotlib.plotEQ(EQ['bdirs'],BrDIs,'Bootstrapped Eigenvectors')
            pmagplotlib.drawFIGS(EQ)
            ans=raw_input('s[a]ve, [q]uit ')
            if ans=='q':sys.exit()
            if ans=='a':
                files={}
                for key in EQ.keys():
                    files[key]='BE_'+key+'.svg'
                pmagplotlib.saveP(EQ,files)
            sys.exit() 
    if len(nDIs)>5:
        pmagplotlib.plotCONF(EQ['eq'],title,DiRecs,npars,1)
        if len(rDIs)>5 and dist!='B': 
            pmagplotlib.plotCONF(EQ['eq'],title,[],rpars,0)
    elif len(rDIs)>5 and dist!='B': 
        pmagplotlib.plotCONF(EQ['eq'],title,DiRecs,rpars,1)
    pmagplotlib.drawFIGS(EQ)
    ans=raw_input('s[a]ve, [q]uit ')
    if ans=='q':sys.exit()
    if ans=='a':
        files={}
        for key in EQ.keys():
            files[key]=key+'.svg'
        pmagplotlib.saveP(EQ,files)
Ejemplo n.º 6
0
def main():
    """
    NAME
        eqarea_magic.py

    DESCRIPTION
       makes equal area projections from declination/inclination data

    SYNTAX
        eqarea_magic.py [command line options]

    INPUT
       takes magic formatted sites, samples, specimens, or measurements

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input magic format file from magic, default='sites.txt'
         supported types=[measurements, specimens, samples, sites]
        -fsp FILE: specify specimen file name, (required if you want to plot measurements by sample)
                default='specimens.txt'
        -fsa FILE: specify sample file name, (required if you want to plot specimens by site)
                default='samples.txt'
        -fsi FILE: specify site file name, default='sites.txt'

        -obj OBJ: specify  level of plot  [all, sit, sam, spc], default is all
        -crd [s,g,t]: specify coordinate system, [s]pecimen, [g]eographic, [t]ilt adjusted
                default is geographic, unspecified assumed geographic
        -fmt [svg,png,jpg] format for output plots
        -ell [F,K,B,Be,Bv] plot Fisher, Kent, Bingham, Bootstrap ellipses or Boostrap eigenvectors
        -c plot as colour contour
        -sav save plot and quit quietly
    NOTE
        all: entire file; sit: site; sam: sample; spc: specimen
    """
    # initialize some default variables
    FIG = {} # plot dictionary
    FIG['eqarea'] = 1 # eqarea is figure 1
    plotE = 0
    plt = 0  # default to not plotting
    verbose = pmagplotlib.verbose
    # extract arguments from sys.argv
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    dir_path = pmag.get_named_arg_from_sys("-WD", default_val=os.getcwd())
    pmagplotlib.plot_init(FIG['eqarea'],5,5)
    in_file = pmag.get_named_arg_from_sys("-f", default_val="sites.txt")
    full_in_file = os.path.join(dir_path, in_file)
    plot_by = pmag.get_named_arg_from_sys("-obj", default_val="all").lower()
    spec_file = pmag.get_named_arg_from_sys("-fsp", default_val="specimens.txt")
    samp_file = pmag.get_named_arg_from_sys("-fsa", default_val="samples.txt")
    site_file = pmag.get_named_arg_from_sys("-fsi", default_val="sites.txt")
    if plot_by == 'all':
        plot_key = 'all'
    elif plot_by == 'sit':
        plot_key = 'site'
    elif plot_by == 'sam':
        plot_key = 'sample'
    elif plot_by == 'spc':
        plot_key = 'specimen'
    else:
        plot_key = 'all'
    if '-c' in sys.argv:
        contour = 1
    else:
        contour = 0
    if '-sav' in sys.argv:
        plt = 1
        verbose = 0
    if '-ell' in sys.argv:
        plotE = 1
        ind = sys.argv.index('-ell')
        ell_type = sys.argv[ind+1]
        ell_type = pmag.get_named_arg_from_sys("-ell", "F")
        dist = ell_type.upper()
        # if dist type is unrecognized, use Fisher
        if dist not in ['F', 'K', 'B', 'BE', 'BV']:
            dist = 'F'
        if dist == "BV":
            FIG['bdirs'] = 2
            pmagplotlib.plot_init(FIG['bdirs'],5,5)
    crd = pmag.get_named_arg_from_sys("-crd", default_val="g")
    if crd == "s":
        coord = "-1"
    elif crd == "t":
        coord = "100"
    else:
        coord = "0"

    fmt = pmag.get_named_arg_from_sys("-fmt", "svg")

    dec_key = 'dir_dec'
    inc_key = 'dir_inc'
    tilt_key = 'dir_tilt_correction'
    #Dir_type_keys=['','site_direction_type','sample_direction_type','specimen_direction_type']

    #
    fnames = {"specimens": spec_file, "samples": samp_file, 'sites': site_file}
    contribution = nb.Contribution(dir_path, custom_filenames=fnames,
                                   single_file=in_file)
    # the object that contains the DataFrame + useful helper methods:
    table_name = list(contribution.tables.keys())[0]
    data_container = contribution.tables[table_name]
    # the actual DataFrame:
    data = data_container.df

    # uses sample infile to add temporary site_name
    # column to the specimen table



    data_container = contribution.tables[table_name]
    data = data_container.df

    if (plot_key != "all") and (plot_key not in data.columns):
        contribution.propagate_location_to_measurements()
        contribution.propagate_location_to_specimens()

    # add tilt key into DataFrame columns if it isn't there already
    if tilt_key not in data.columns:
        data.loc[:, tilt_key] = None

    if verbose:
        print(len(data), ' records read from ', in_file)

    # find desired dec,inc data:
    dir_type_key = ''
    #
    # get plotlist if not plotting all records
    #
    plotlist=[]
    if plot_key != "all":
        # return all where plot_key is not blank
        if plot_key not in data.columns:
            print('Can\'t plot by "{}".  That header is not in infile: {}'.format(plot_key, in_file))
            return
        plots = data[data[plot_key].notnull()]
        plotlist = plots[plot_key].unique() # grab unique values
    else:
        plotlist.append('All')

    for plot in plotlist:
        if verbose:
            print(plot)
        if plot == 'All':
            # plot everything at once
            plot_data = data
        else:
            # pull out only partial data
            plot_data = data[data[plot_key] == plot]

        DIblock = []
        GCblock = []
        # SLblock, SPblock = [], []
        title = plot
        mode = 1
        k = 0


        if dec_key not in plot_data.columns:
            print("-W- No dec/inc data")
            continue
        # get all records where dec & inc values exist
        plot_data = plot_data[plot_data[dec_key].notnull() & plot_data[inc_key].notnull()]
        if plot_data.empty:
            continue
        # this sorting out is done in get_di_bock
        #if coord == '0':  # geographic, use records with no tilt key (or tilt_key 0)
        #    cond1 = plot_data[tilt_key].fillna('') == coord
        #    cond2 = plot_data[tilt_key].isnull()
        #    plot_data = plot_data[cond1 | cond2]
        #else:  # not geographic coordinates, use only records with correct tilt_key
        #    plot_data = plot_data[plot_data[tilt_key] == coord]

        # get metadata for naming the plot file
        locations = data_container.get_name('location', df_slice=plot_data)
        site = data_container.get_name('site', df_slice=plot_data)
        sample = data_container.get_name('sample', df_slice=plot_data)
        specimen = data_container.get_name('specimen', df_slice=plot_data)

        # make sure method_codes is in plot_data
        if 'method_codes' not in plot_data.columns:
            plot_data['method_codes'] = ''

        # get data blocks
        DIblock = data_container.get_di_block(df_slice=plot_data,
                                              tilt_corr=coord, excl=['DE-BFP'])
        #SLblock = [[ind, row['method_codes']] for ind, row in plot_data.iterrows()]
        # get great circles
        great_circle_data = data_container.get_records_for_code('DE-BFP', incl=True,
                                                                use_slice=True, sli=plot_data)

        if len(great_circle_data) > 0:
            gc_cond = great_circle_data[tilt_key] == coord
            GCblock = [[float(row[dec_key]), float(row[inc_key])] for ind, row in great_circle_data[gc_cond].iterrows()]
            #SPblock = [[ind, row['method_codes']] for ind, row in great_circle_data[gc_cond].iterrows()]

        if len(DIblock) > 0:
            if contour == 0:
                pmagplotlib.plotEQ(FIG['eqarea'], DIblock, title)
            else:
                pmagplotlib.plotEQcont(FIG['eqarea'], DIblock)
        else:
            pmagplotlib.plotNET(FIG['eqarea'])
        if len(GCblock)>0:
            for rec in GCblock:
                pmagplotlib.plotC(FIG['eqarea'], rec, 90., 'g')
        if len(DIblock) == 0 and len(GCblock) == 0:
            if verbose:
                print("no records for plotting")
            continue
            #sys.exit()
        if plotE == 1:
            ppars = pmag.doprinc(DIblock) # get principal directions
            nDIs, rDIs, npars, rpars = [], [], [], []
            for rec in DIblock:
                angle=pmag.angle([rec[0],rec[1]],[ppars['dec'],ppars['inc']])
                if angle>90.:
                    rDIs.append(rec)
                else:
                    nDIs.append(rec)
            if dist=='B': # do on whole dataset
                etitle="Bingham confidence ellipse"
                bpars=pmag.dobingham(DIblock)
                for key in list(bpars.keys()):
                    if key!='n' and verbose: print("    ",key, '%7.1f'%(bpars[key]))
                    if key=='n' and verbose: print("    ",key, '       %i'%(bpars[key]))
                npars.append(bpars['dec'])
                npars.append(bpars['inc'])
                npars.append(bpars['Zeta'])
                npars.append(bpars['Zdec'])
                npars.append(bpars['Zinc'])
                npars.append(bpars['Eta'])
                npars.append(bpars['Edec'])
                npars.append(bpars['Einc'])
            if dist=='F':
                etitle="Fisher confidence cone"
                if len(nDIs)>2:
                    fpars=pmag.fisher_mean(nDIs)
                    for key in list(fpars.keys()):
                        if key!='n' and verbose: print("    ",key, '%7.1f'%(fpars[key]))
                        if key=='n' and verbose: print("    ",key, '       %i'%(fpars[key]))
                    mode+=1
                    npars.append(fpars['dec'])
                    npars.append(fpars['inc'])
                    npars.append(fpars['alpha95']) # Beta
                    npars.append(fpars['dec'])
                    isign=old_div(abs(fpars['inc']),fpars['inc'])
                    npars.append(fpars['inc']-isign*90.) #Beta inc
                    npars.append(fpars['alpha95']) # gamma
                    npars.append(fpars['dec']+90.) # Beta dec
                    npars.append(0.) #Beta inc
                if len(rDIs)>2:
                    fpars=pmag.fisher_mean(rDIs)
                    if verbose: print("mode ",mode)
                    for key in list(fpars.keys()):
                        if key!='n' and verbose: print("    ",key, '%7.1f'%(fpars[key]))
                        if key=='n' and verbose: print("    ",key, '       %i'%(fpars[key]))
                    mode+=1
                    rpars.append(fpars['dec'])
                    rpars.append(fpars['inc'])
                    rpars.append(fpars['alpha95']) # Beta
                    rpars.append(fpars['dec'])
                    isign=old_div(abs(fpars['inc']),fpars['inc'])
                    rpars.append(fpars['inc']-isign*90.) #Beta inc
                    rpars.append(fpars['alpha95']) # gamma
                    rpars.append(fpars['dec']+90.) # Beta dec
                    rpars.append(0.) #Beta inc
            if dist=='K':
                etitle="Kent confidence ellipse"
                if len(nDIs)>3:
                    kpars=pmag.dokent(nDIs,len(nDIs))
                    if verbose: print("mode ",mode)
                    for key in list(kpars.keys()):
                        if key!='n' and verbose: print("    ",key, '%7.1f'%(kpars[key]))
                        if key=='n' and verbose: print("    ",key, '       %i'%(kpars[key]))
                    mode+=1
                    npars.append(kpars['dec'])
                    npars.append(kpars['inc'])
                    npars.append(kpars['Zeta'])
                    npars.append(kpars['Zdec'])
                    npars.append(kpars['Zinc'])
                    npars.append(kpars['Eta'])
                    npars.append(kpars['Edec'])
                    npars.append(kpars['Einc'])
                if len(rDIs)>3:
                    kpars=pmag.dokent(rDIs,len(rDIs))
                    if verbose: print("mode ",mode)
                    for key in list(kpars.keys()):
                        if key!='n' and verbose: print("    ",key, '%7.1f'%(kpars[key]))
                        if key=='n' and verbose: print("    ",key, '       %i'%(kpars[key]))
                    mode+=1
                    rpars.append(kpars['dec'])
                    rpars.append(kpars['inc'])
                    rpars.append(kpars['Zeta'])
                    rpars.append(kpars['Zdec'])
                    rpars.append(kpars['Zinc'])
                    rpars.append(kpars['Eta'])
                    rpars.append(kpars['Edec'])
                    rpars.append(kpars['Einc'])
            else: # assume bootstrap
                if dist=='BE':
                    if len(nDIs)>5:
                        BnDIs=pmag.di_boot(nDIs)
                        Bkpars=pmag.dokent(BnDIs,1.)
                        if verbose: print("mode ",mode)
                        for key in list(Bkpars.keys()):
                            if key!='n' and verbose: print("    ",key, '%7.1f'%(Bkpars[key]))
                            if key=='n' and verbose: print("    ",key, '       %i'%(Bkpars[key]))
                        mode+=1
                        npars.append(Bkpars['dec'])
                        npars.append(Bkpars['inc'])
                        npars.append(Bkpars['Zeta'])
                        npars.append(Bkpars['Zdec'])
                        npars.append(Bkpars['Zinc'])
                        npars.append(Bkpars['Eta'])
                        npars.append(Bkpars['Edec'])
                        npars.append(Bkpars['Einc'])
                    if len(rDIs)>5:
                        BrDIs=pmag.di_boot(rDIs)
                        Bkpars=pmag.dokent(BrDIs,1.)
                        if verbose: print("mode ",mode)
                        for key in list(Bkpars.keys()):
                            if key!='n' and verbose: print("    ",key, '%7.1f'%(Bkpars[key]))
                            if key=='n' and verbose: print("    ",key, '       %i'%(Bkpars[key]))
                        mode+=1
                        rpars.append(Bkpars['dec'])
                        rpars.append(Bkpars['inc'])
                        rpars.append(Bkpars['Zeta'])
                        rpars.append(Bkpars['Zdec'])
                        rpars.append(Bkpars['Zinc'])
                        rpars.append(Bkpars['Eta'])
                        rpars.append(Bkpars['Edec'])
                        rpars.append(Bkpars['Einc'])
                    etitle="Bootstrapped confidence ellipse"
                elif dist=='BV':
                    sym={'lower':['o','c'],'upper':['o','g'],'size':3,'edgecolor':'face'}
                    if len(nDIs)>5:
                        BnDIs=pmag.di_boot(nDIs)
                        pmagplotlib.plotEQsym(FIG['bdirs'],BnDIs,'Bootstrapped Eigenvectors', sym)
                    if len(rDIs)>5:
                        BrDIs=pmag.di_boot(rDIs)
                        if len(nDIs)>5:  # plot on existing plots
                            pmagplotlib.plotDIsym(FIG['bdirs'],BrDIs,sym)
                        else:
                            pmagplotlib.plotEQ(FIG['bdirs'],BrDIs,'Bootstrapped Eigenvectors')
            if dist=='B':
                if len(nDIs)> 3 or len(rDIs)>3: pmagplotlib.plotCONF(FIG['eqarea'],etitle,[],npars,0)
            elif len(nDIs)>3 and dist!='BV':
                pmagplotlib.plotCONF(FIG['eqarea'],etitle,[],npars,0)
                if len(rDIs)>3:
                    pmagplotlib.plotCONF(FIG['eqarea'],etitle,[],rpars,0)
            elif len(rDIs)>3 and dist!='BV':
                pmagplotlib.plotCONF(FIG['eqarea'],etitle,[],rpars,0)

        for key in list(FIG.keys()):
            files = {}
            filename = pmag.get_named_arg_from_sys('-fname')
            if filename: # use provided filename
                filename+= '.' + fmt
            elif pmagplotlib.isServer: # use server plot naming convention
                filename='LO:_'+locations+'_SI:_'+site+'_SA:_'+sample+'_SP:_'+specimen+'_CO:_'+crd+'_TY:_'+key+'_.'+fmt
            else: # use more readable naming convention
                filename = ''
                for item in [locations, site, sample, specimen, crd, key]:
                    if item:
                        item = item.replace(' ', '_')
                        filename += item + '_'
                if filename.endswith('_'):
                    filename = filename[:-1]
                filename += ".{}".format(fmt)

            files[key]=filename

        if pmagplotlib.isServer:
            black     = '#000000'
            purple    = '#800080'
            titles={}
            titles['eq']='Equal Area Plot'
            FIG = pmagplotlib.addBorders(FIG,titles,black,purple)
            pmagplotlib.saveP(FIG,files)

        if plt:
            pmagplotlib.saveP(FIG,files)
            continue
        if verbose:
            pmagplotlib.drawFIGS(FIG)
            ans=input(" S[a]ve to save plot, [q]uit, Return to continue:  ")
            if ans == "q":
                sys.exit()
            if ans == "a":
                pmagplotlib.saveP(FIG,files)
        continue
Ejemplo n.º 7
0
def main():
    """
    NAME
	specimens_results_magic.py

    DESCRIPTION
	combines pmag_specimens.txt file with age, location, acceptance criteria and
	outputs pmag_results table along with other MagIC tables necessary for uploading to the database

    SYNTAX
	specimens_results_magic.py [command line options]

    OPTIONS
	-h prints help message and quits
	-usr USER:   identify user, default is ""
	-f: specimen input magic_measurements format file, default is "magic_measurements.txt"
	-fsp: specimen input pmag_specimens format file, default is "pmag_specimens.txt"
	-fsm: sample input er_samples format file, default is "er_samples.txt"
	-fsi: specimen input er_sites format file, default is "er_sites.txt"
	-fla: specify a file with paleolatitudes for calculating VADMs, default is not to calculate VADMS
               format is:  site_name paleolatitude (space delimited file)
	-fa AGES: specify er_ages format file with age information
	-crd [s,g,t,b]:   specify coordinate system
	    (s, specimen, g geographic, t, tilt corrected, b, geographic and tilt corrected)
	    Default is to assume geographic
	    NB: only the tilt corrected data will appear on the results table, if both g and t are selected.
        -cor [AC:CR:NL]: colon delimited list of required data adjustments for all specimens 
            included in intensity calculations (anisotropy, cooling rate, non-linear TRM)
            unless specified, corrections will not be applied
        -pri [TRM:ARM] colon delimited list of priorities for anisotropy correction (-cor must also be set to include AC). default is TRM, then ARM 
	-age MIN MAX UNITS:   specify age boundaries and units
	-exc:  use exiting selection criteria (in pmag_criteria.txt file), default is default criteria
	-C: no acceptance criteria
	-aD:  average directions per sample, default is NOT
	-aI:  average multiple specimen intensities per sample, default is by site 
	-aC:  average all components together, default is NOT
	-pol:  calculate polarity averages
	-sam:  save sample level vgps and v[a]dms, default is by site
	-xSi:  skip the site level intensity calculation
	-p: plot directions and look at intensities by site, default is NOT
	    -fmt: specify output for saved images, default is svg (only if -p set)
	-lat: use present latitude for calculating VADMs, default is not to calculate VADMs
	-xD: skip directions
	-xI: skip intensities
    OUPUT
	writes pmag_samples, pmag_sites, pmag_results tables
    """
    # set defaults
    Comps = []  # list of components
    version_num = pmag.get_version()
    args = sys.argv
    DefaultAge = ["none"]
    skipdirs, coord, excrit, custom, vgps, average, Iaverage, plotsites, opt = 1, 0, 0, 0, 0, 0, 0, 0, 0
    get_model_lat = 0  # this skips VADM calculation altogether, when get_model_lat=1, uses present day
    fmt = 'svg'
    dir_path = "."
    model_lat_file = ""
    Caverage = 0
    infile = 'pmag_specimens.txt'
    measfile = "magic_measurements.txt"
    sampfile = "er_samples.txt"
    sitefile = "er_sites.txt"
    agefile = "er_ages.txt"
    specout = "er_specimens.txt"
    sampout = "pmag_samples.txt"
    siteout = "pmag_sites.txt"
    resout = "pmag_results.txt"
    critout = "pmag_criteria.txt"
    instout = "magic_instruments.txt"
    sigcutoff, OBJ = "", ""
    noDir, noInt = 0, 0
    polarity = 0
    coords = ['0']
    Dcrit, Icrit, nocrit = 0, 0, 0
    corrections = []
    nocorrection = ['DA-NL', 'DA-AC', 'DA-CR']
    priorities = ['DA-AC-ARM',
                  'DA-AC-TRM']  # priorities for anisotropy correction
    # get command line stuff
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if '-WD' in args:
        ind = args.index("-WD")
        dir_path = args[ind + 1]
    if '-cor' in args:
        ind = args.index('-cor')
        cors = args[ind + 1].split(':')  # list of required data adjustments
        for cor in cors:
            nocorrection.remove('DA-' + cor)
            corrections.append('DA-' + cor)
    if '-pri' in args:
        ind = args.index('-pri')
        priorities = args[ind + 1].split(
            ':')  # list of required data adjustments
        for p in priorities:
            p = 'DA-AC-' + p
    if '-f' in args:
        ind = args.index("-f")
        measfile = args[ind + 1]
    if '-fsp' in args:
        ind = args.index("-fsp")
        infile = args[ind + 1]
    if '-fsi' in args:
        ind = args.index("-fsi")
        sitefile = args[ind + 1]
    if "-crd" in args:
        ind = args.index("-crd")
        coord = args[ind + 1]
        if coord == 's': coords = ['-1']
        if coord == 'g': coords = ['0']
        if coord == 't': coords = ['100']
        if coord == 'b': coords = ['0', '100']
    if "-usr" in args:
        ind = args.index("-usr")
        user = sys.argv[ind + 1]
    else:
        user = ""
    if "-C" in args: Dcrit, Icrit, nocrit = 1, 1, 1  # no selection criteria
    if "-sam" in args: vgps = 1  # save sample level VGPS/VADMs
    if "-xSi" in args:
        nositeints = 1  # skip site level intensity
    else:
        nositeints = 0
    if "-age" in args:
        ind = args.index("-age")
        DefaultAge[0] = args[ind + 1]
        DefaultAge.append(args[ind + 2])
        DefaultAge.append(args[ind + 3])
    Daverage, Iaverage, Caverage = 0, 0, 0
    if "-aD" in args: Daverage = 1  # average by sample directions
    if "-aI" in args: Iaverage = 1  # average by sample intensities
    if "-aC" in args:
        Caverage = 1  # average all components together ???  why???
    if "-pol" in args: polarity = 1  # calculate averages by polarity
    if '-xD' in args: noDir = 1
    if '-xI' in args:
        noInt = 1
    elif "-fla" in args:
        if '-lat' in args:
            print "you should set a paleolatitude file OR use present day lat - not both"
            sys.exit()
        ind = args.index("-fla")
        model_lat_file = dir_path + '/' + args[ind + 1]
        get_model_lat = 2
        mlat = open(model_lat_file, 'rU')
        ModelLats = []
        for line in mlat.readlines():
            ModelLat = {}
            tmp = line.split()
            ModelLat["er_site_name"] = tmp[0]
            ModelLat["site_model_lat"] = tmp[1]
            ModelLat["er_sample_name"] = tmp[0]
            ModelLat["sample_lat"] = tmp[1]
            ModelLats.append(ModelLat)
        get_model_lat = 2
    elif '-lat' in args:
        get_model_lat = 1
    if "-p" in args:
        plotsites = 1
        if "-fmt" in args:
            ind = args.index("-fmt")
            fmt = args[ind + 1]
        if noDir == 0:  # plot by site - set up plot window
            import pmagplotlib
            EQ = {}
            EQ['eqarea'] = 1
            pmagplotlib.plot_init(
                EQ['eqarea'], 5, 5)  # define figure 1 as equal area projection
            pmagplotlib.plotNET(
                EQ['eqarea']
            )  # I don't know why this has to be here, but otherwise the first plot never plots...
            pmagplotlib.drawFIGS(EQ)
    if '-WD' in args:
        infile = dir_path + '/' + infile
        measfile = dir_path + '/' + measfile
        instout = dir_path + '/' + instout
        sampfile = dir_path + '/' + sampfile
        sitefile = dir_path + '/' + sitefile
        agefile = dir_path + '/' + agefile
        specout = dir_path + '/' + specout
        sampout = dir_path + '/' + sampout
        siteout = dir_path + '/' + siteout
        resout = dir_path + '/' + resout
        critout = dir_path + '/' + critout
    if "-exc" in args:  # use existing pmag_criteria file
        if "-C" in args:
            print 'you can not use both existing and no criteria - choose either -exc OR -C OR neither (for default)'
            sys.exit()
        crit_data, file_type = pmag.magic_read(critout)
        print "Acceptance criteria read in from ", critout
    else:  # use default criteria (if nocrit set, then get really loose criteria as default)
        crit_data = pmag.default_criteria(nocrit)
        if nocrit == 0:
            print "Acceptance criteria are defaults"
        else:
            print "No acceptance criteria used "
    accept = {}
    for critrec in crit_data:
        for key in critrec.keys():
            # need to migrate specimen_dang to specimen_int_dang for intensity data using old format
            if 'IE-SPEC' in critrec.keys() and 'specimen_dang' in critrec.keys(
            ) and 'specimen_int_dang' not in critrec.keys():
                critrec['specimen_int_dang'] = critrec['specimen_dang']
                del critrec['specimen_dang']
# need to get rid of ron shaars sample_int_sigma_uT
            if 'sample_int_sigma_uT' in critrec.keys():
                critrec['sample_int_sigma'] = '%10.3e' % (
                    eval(critrec['sample_int_sigma_uT']) * 1e-6)
            if key not in accept.keys() and critrec[key] != '':
                accept[key] = critrec[key]
    #
    #
    if "-exc" not in args and "-C" not in args:
        print "args", args
        pmag.magic_write(critout, [accept], 'pmag_criteria')
        print "\n Pmag Criteria stored in ", critout, '\n'
#
# now we're done slow dancing
#
    SiteNFO, file_type = pmag.magic_read(
        sitefile)  # read in site data - has the lats and lons
    SampNFO, file_type = pmag.magic_read(
        sampfile)  # read in site data - has the lats and lons
    height_nfo = pmag.get_dictitem(SiteNFO, 'site_height', '',
                                   'F')  # find all the sites with height info.
    if agefile != "":
        AgeNFO, file_type = pmag.magic_read(
            agefile)  # read in the age information
    Data, file_type = pmag.magic_read(
        infile)  # read in specimen interpretations
    IntData = pmag.get_dictitem(Data, 'specimen_int', '',
                                'F')  # retrieve specimens with intensity data
    comment, orient = "", []
    samples, sites = [], []
    for rec in Data:  # run through the data filling in missing keys and finding all components, coordinates available
        # fill in missing fields, collect unique sample and site names
        if 'er_sample_name' not in rec.keys():
            rec['er_sample_name'] = ""
        elif rec['er_sample_name'] not in samples:
            samples.append(rec['er_sample_name'])
        if 'er_site_name' not in rec.keys():
            rec['er_site_name'] = ""
        elif rec['er_site_name'] not in sites:
            sites.append(rec['er_site_name'])
        if 'specimen_int' not in rec.keys(): rec['specimen_int'] = ''
        if 'specimen_comp_name' not in rec.keys(
        ) or rec['specimen_comp_name'] == "":
            rec['specimen_comp_name'] = 'A'
        if rec['specimen_comp_name'] not in Comps:
            Comps.append(rec['specimen_comp_name'])
        rec['specimen_tilt_correction'] = rec[
            'specimen_tilt_correction'].strip('\n')
        if "specimen_tilt_correction" not in rec.keys():
            rec["specimen_tilt_correction"] = "-1"  # assume sample coordinates
        if rec["specimen_tilt_correction"] not in orient:
            orient.append(rec["specimen_tilt_correction"]
                          )  # collect available coordinate systems
        if "specimen_direction_type" not in rec.keys():
            rec["specimen_direction_type"] = 'l'  # assume direction is line - not plane
        if "specimen_dec" not in rec.keys():
            rec["specimen_direction_type"] = ''  # if no declination, set direction type to blank
        if "specimen_n" not in rec.keys(): rec["specimen_n"] = ''  # put in n
        if "specimen_alpha95" not in rec.keys():
            rec["specimen_alpha95"] = ''  # put in alpha95
        if "magic_method_codes" not in rec.keys():
            rec["magic_method_codes"] = ''
    #
    # start parsing data into SpecDirs, SpecPlanes, SpecInts
    SpecInts, SpecDirs, SpecPlanes = [], [], []
    samples.sort()  # get sorted list of samples and sites
    sites.sort()
    if noInt == 0:  # don't skip intensities
        IntData = pmag.get_dictitem(
            Data, 'specimen_int', '',
            'F')  # retrieve specimens with intensity data
        if nocrit == 0:  # use selection criteria
            for rec in IntData:  # do selection criteria
                kill = pmag.grade(rec, accept, 'specimen_int')
                if len(kill) == 0:
                    SpecInts.append(
                        rec
                    )  # intensity record to be included in sample, site calculations
        else:
            SpecInts = IntData[:]  # take everything - no selection criteria
# check for required data adjustments
        if len(corrections) > 0 and len(SpecInts) > 0:
            for cor in corrections:
                SpecInts = pmag.get_dictitem(
                    SpecInts, 'magic_method_codes', cor,
                    'has')  # only take specimens with the required corrections
        if len(nocorrection) > 0 and len(SpecInts) > 0:
            for cor in nocorrection:
                SpecInts = pmag.get_dictitem(
                    SpecInts, 'magic_method_codes', cor, 'not'
                )  # exclude the corrections not specified for inclusion
# take top priority specimen of its name in remaining specimens (only one per customer)
        PrioritySpecInts = []
        specimens = pmag.get_specs(SpecInts)  # get list of uniq specimen names
        for spec in specimens:
            ThisSpecRecs = pmag.get_dictitem(
                SpecInts, 'er_specimen_name', spec,
                'T')  # all the records for this specimen
            if len(ThisSpecRecs) == 1:
                PrioritySpecInts.append(ThisSpecRecs[0])
            elif len(ThisSpecRecs) > 1:  # more than one
                prec = []
                for p in priorities:
                    ThisSpecRecs = pmag.get_dictitem(
                        SpecInts, 'magic_method_codes', p,
                        'has')  # all the records for this specimen
                    if len(ThisSpecRecs) > 0: prec.append(ThisSpecRecs[0])
                PrioritySpecInts.append(prec[0])  # take the best one
        SpecInts = PrioritySpecInts  # this has the first specimen record
    if noDir == 0:  # don't skip directions
        AllDirs = pmag.get_dictitem(
            Data, 'specimen_direction_type', '',
            'F')  # retrieve specimens with directed lines and planes
        Ns = pmag.get_dictitem(
            AllDirs, 'specimen_n', '',
            'F')  # get all specimens with specimen_n information
        if nocrit != 1:  # use selection criteria
            for rec in Ns:  # look through everything with specimen_n for "good" data
                kill = pmag.grade(rec, accept, 'specimen_dir')
                if len(kill) == 0:  # nothing killed it
                    SpecDirs.append(rec)
        else:  # no criteria
            SpecDirs = AllDirs[:]  # take them all
# SpecDirs is now the list of all specimen directions (lines and planes) that pass muster
#
    PmagSamps, SampDirs = [], [
    ]  # list of all sample data and list of those that pass the DE-SAMP criteria
    PmagSites, PmagResults = [], [
    ]  # list of all site data and selected results
    SampInts = []
    for samp in samples:  # run through the sample names
        if Daverage == 1:  #  average by sample if desired
            SampDir = pmag.get_dictitem(
                SpecDirs, 'er_sample_name', samp,
                'T')  # get all the directional data for this sample
            if len(SampDir) > 0:  # there are some directions
                for coord in coords:  # step through desired coordinate systems
                    CoordDir = pmag.get_dictitem(
                        SampDir, 'specimen_tilt_correction', coord,
                        'T')  # get all the directions for this sample
                    if len(CoordDir
                           ) > 0:  # there are some with this coordinate system
                        if Caverage == 0:  # look component by component
                            for comp in Comps:
                                CompDir = pmag.get_dictitem(
                                    CoordDir, 'specimen_comp_name', comp, 'T'
                                )  # get all directions from this component
                                if len(CompDir) > 0:  # there are some
                                    PmagSampRec = pmag.lnpbykey(
                                        CompDir, 'sample', 'specimen'
                                    )  # get a sample average from all specimens
                                    PmagSampRec["er_location_name"] = CompDir[0][
                                        'er_location_name']  # decorate the sample record
                                    PmagSampRec["er_site_name"] = CompDir[0][
                                        'er_site_name']
                                    PmagSampRec["er_sample_name"] = samp
                                    PmagSampRec[
                                        "er_citation_names"] = "This study"
                                    PmagSampRec["er_analyst_mail_names"] = user
                                    PmagSampRec[
                                        'magic_software_packages'] = version_num
                                    if nocrit != 1:
                                        PmagSampRec[
                                            'pmag_criteria_codes'] = "ACCEPT"
                                    if agefile != "":
                                        PmagSampRec = pmag.get_age(
                                            PmagSampRec, "er_site_name",
                                            "sample_inferred_", AgeNFO,
                                            DefaultAge)
                                    site_height = pmag.get_dictitem(
                                        height_nfo, 'er_site_name',
                                        PmagSampRec['er_site_name'], 'T')
                                    if len(site_height) > 0:
                                        PmagSampRec[
                                            "sample_height"] = site_height[0][
                                                'site_height']  # add in height if available
                                    PmagSampRec['sample_comp_name'] = comp
                                    PmagSampRec[
                                        'sample_tilt_correction'] = coord
                                    PmagSampRec[
                                        'er_specimen_names'] = pmag.get_list(
                                            CompDir, 'er_specimen_name'
                                        )  # get a list of the specimen names used
                                    PmagSampRec[
                                        'magic_method_codes'] = pmag.get_list(
                                            CompDir, 'magic_method_codes'
                                        )  # get a list of the methods used
                                    if nocrit != 1:  # apply selection criteria
                                        kill = pmag.grade(
                                            PmagSampRec, accept, 'sample_dir')
                                    else:
                                        kill = []
                                    if len(kill) == 0:
                                        SampDirs.append(PmagSampRec)
                                        if vgps == 1:  # if sample level VGP info desired, do that now
                                            PmagResRec = pmag.getsampVGP(
                                                PmagSampRec, SiteNFO)
                                            if PmagResRec != "":
                                                PmagResults.append(PmagResRec)
                                        PmagSamps.append(PmagSampRec)
                        if Caverage == 1:  # average all components together  basically same as above
                            PmagSampRec = pmag.lnpbykey(
                                CoordDir, 'sample', 'specimen')
                            PmagSampRec["er_location_name"] = CoordDir[0][
                                'er_location_name']
                            PmagSampRec["er_site_name"] = CoordDir[0][
                                'er_site_name']
                            PmagSampRec["er_sample_name"] = samp
                            PmagSampRec["er_citation_names"] = "This study"
                            PmagSampRec["er_analyst_mail_names"] = user
                            PmagSampRec[
                                'magic_software_packages'] = version_num
                            if nocrit != 1:
                                PmagSampRec['pmag_criteria_codes'] = ""
                            if agefile != "":
                                PmagSampRec = pmag.get_age(
                                    PmagSampRec, "er_site_name",
                                    "sample_inferred_", AgeNFO, DefaultAge)
                            site_height = pmag.get_dictitem(
                                height_nfo, 'er_site_name', site, 'T')
                            if len(site_height) > 0:
                                PmagSampRec["sample_height"] = site_height[0][
                                    'site_height']  # add in height if available
                            PmagSampRec['sample_tilt_correction'] = coord
                            PmagSampRec['sample_comp_name'] = pmag.get_list(
                                CoordDir,
                                'specimen_comp_name')  # get components used
                            PmagSampRec['er_specimen_names'] = pmag.get_list(
                                CoordDir, 'er_specimen_name'
                            )  # get specimne names averaged
                            PmagSampRec['magic_method_codes'] = pmag.get_list(
                                CoordDir,
                                'magic_method_codes')  # assemble method codes
                            if nocrit != 1:  # apply selection criteria
                                kill = pmag.grade(PmagSampRec, accept,
                                                  'sample_dir')
                                if len(kill) == 0:  # passes the mustard
                                    SampDirs.append(PmagSampRec)
                                    if vgps == 1:
                                        PmagResRec = pmag.getsampVGP(
                                            PmagSampRec, SiteNFO)
                                        if PmagResRec != "":
                                            PmagResults.append(PmagResRec)
                            else:  # take everything
                                SampDirs.append(PmagSampRec)
                                if vgps == 1:
                                    PmagResRec = pmag.getsampVGP(
                                        PmagSampRec, SiteNFO)
                                    if PmagResRec != "":
                                        PmagResults.append(PmagResRec)
                            PmagSamps.append(PmagSampRec)
        if Iaverage == 1:  #  average by sample if desired
            SampI = pmag.get_dictitem(
                SpecInts, 'er_sample_name', samp,
                'T')  # get all the intensity data for this sample
            if len(SampI) > 0:  # there are some
                PmagSampRec = pmag.average_int(
                    SampI, 'specimen', 'sample')  # get average intensity stuff
                PmagSampRec[
                    "sample_description"] = "sample intensity"  # decorate sample record
                PmagSampRec["sample_direction_type"] = ""
                PmagSampRec['er_site_name'] = SampI[0]["er_site_name"]
                PmagSampRec['er_sample_name'] = samp
                PmagSampRec['er_location_name'] = SampI[0]["er_location_name"]
                PmagSampRec["er_citation_names"] = "This study"
                PmagSampRec["er_analyst_mail_names"] = user
                if agefile != "":
                    PmagSampRec = pmag.get_age(PmagSampRec, "er_site_name",
                                               "sample_inferred_", AgeNFO,
                                               DefaultAge)
                site_height = pmag.get_dictitem(height_nfo, 'er_site_name',
                                                PmagSampRec['er_site_name'],
                                                'T')
                if len(site_height) > 0:
                    PmagSampRec["sample_height"] = site_height[0][
                        'site_height']  # add in height if available
                PmagSampRec['er_specimen_names'] = pmag.get_list(
                    SampI, 'er_specimen_name')
                PmagSampRec['magic_method_codes'] = pmag.get_list(
                    SampI, 'magic_method_codes')
                if nocrit != 1:  # apply criteria!
                    kill = pmag.grade(PmagSampRec, accept, 'sample_int')
                    if len(kill) == 0:
                        PmagSampRec['pmag_criteria_codes'] = "ACCEPT"
                        SampInts.append(PmagSampRec)
                        PmagSamps.append(PmagSampRec)
                    else:
                        PmagSampRec = {}  # sample rejected
                else:  # no criteria
                    SampInts.append(PmagSampRec)
                    PmagSamps.append(PmagSampRec)
                    PmagSampRec['pmag_criteria_codes'] = ""
                if vgps == 1 and get_model_lat != 0 and PmagSampRec != {}:  #
                    if get_model_lat == 1:  # use sample latitude
                        PmagResRec = pmag.getsampVDM(PmagSampRec, SampNFO)
                        del (PmagResRec['model_lat']
                             )  # get rid of the model lat key
                    elif get_model_lat == 2:  # use model latitude
                        PmagResRec = pmag.getsampVDM(PmagSampRec, ModelLats)
                        if PmagResRec != {}:
                            PmagResRec['magic_method_codes'] = PmagResRec[
                                'magic_method_codes'] + ":IE-MLAT"
                    if PmagResRec != {}:
                        PmagResRec['er_specimen_names'] = PmagSampRec[
                            'er_specimen_names']
                        PmagResRec['er_sample_names'] = PmagSampRec[
                            'er_sample_name']
                        PmagResRec['pmag_criteria_codes'] = 'ACCEPT'
                        PmagResRec['average_int_sigma_perc'] = PmagSampRec[
                            'sample_int_sigma_perc']
                        PmagResRec['average_int_sigma'] = PmagSampRec[
                            'sample_int_sigma']
                        PmagResRec['average_int_n'] = PmagSampRec[
                            'sample_int_n']
                        PmagResRec['vadm_n'] = PmagSampRec['sample_int_n']
                        PmagResRec['data_type'] = 'i'
                        PmagResults.append(PmagResRec)
    if len(PmagSamps) > 0:
        TmpSamps, keylist = pmag.fillkeys(
            PmagSamps)  # fill in missing keys from different types of records
        pmag.magic_write(sampout, TmpSamps,
                         'pmag_samples')  # save in sample output file
        print ' sample averages written to ', sampout

#
#create site averages from specimens or samples as specified
#
    for site in sites:
        if Daverage == 0:
            key, dirlist = 'specimen', SpecDirs  # if specimen averages at site level desired
        if Daverage == 1:
            key, dirlist = 'sample', SampDirs  # if sample averages at site level desired
        tmp = pmag.get_dictitem(dirlist, 'er_site_name', site,
                                'T')  # get all the sites with  directions
        tmp1 = pmag.get_dictitem(
            tmp, key + '_tilt_correction', coords[-1],
            'T')  # use only the last coordinate if Caverage==0
        sd = pmag.get_dictitem(
            SiteNFO, 'er_site_name', site,
            'T')  # fish out site information (lat/lon, etc.)
        if len(sd) > 0:
            sitedat = sd[0]
            if Caverage == 0:  # do component wise averaging
                for comp in Comps:
                    siteD = pmag.get_dictitem(tmp1, key + '_comp_name', comp,
                                              'T')  # get all components comp
                    if len(
                            siteD
                    ) > 0:  # there are some for this site and component name
                        PmagSiteRec = pmag.lnpbykey(
                            siteD, 'site', key)  # get an average for this site
                        PmagSiteRec[
                            'site_comp_name'] = comp  # decorate the site record
                        PmagSiteRec["er_location_name"] = siteD[0][
                            'er_location_name']
                        PmagSiteRec["er_site_name"] = siteD[0]['er_site_name']
                        PmagSiteRec['site_tilt_correction'] = coords[-1]
                        PmagSiteRec['site_comp_name'] = pmag.get_list(
                            siteD, key + '_comp_name')
                        if Daverage == 1:
                            PmagSiteRec['er_sample_names'] = pmag.get_list(
                                siteD, 'er_sample_name')
                        else:
                            PmagSiteRec['er_specimen_names'] = pmag.get_list(
                                siteD, 'er_specimen_name')


# determine the demagnetization code (DC3,4 or 5) for this site
                        AFnum = len(
                            pmag.get_dictitem(siteD, 'magic_method_codes',
                                              'LP-DIR-AF', 'has'))
                        Tnum = len(
                            pmag.get_dictitem(siteD, 'magic_method_codes',
                                              'LP-DIR-T', 'has'))
                        DC = 3
                        if AFnum > 0: DC += 1
                        if Tnum > 0: DC += 1
                        PmagSiteRec['magic_method_codes'] = pmag.get_list(
                            siteD,
                            'magic_method_codes') + ':' + 'LP-DC' + str(DC)
                        PmagSiteRec['magic_method_codes'].strip(":")
                        if plotsites == 1:
                            print PmagSiteRec['er_site_name']
                            pmagplotlib.plotSITE(EQ['eqarea'], PmagSiteRec,
                                                 siteD,
                                                 key)  # plot and list the data
                            pmagplotlib.drawFIGS(EQ)
                        PmagSites.append(PmagSiteRec)
            else:  # last component only
                siteD = tmp1[:]  # get the last orientation system specified
                if len(siteD) > 0:  # there are some
                    PmagSiteRec = pmag.lnpbykey(
                        siteD, 'site', key)  # get the average for this site
                    PmagSiteRec["er_location_name"] = siteD[0][
                        'er_location_name']  # decorate the record
                    PmagSiteRec["er_site_name"] = siteD[0]['er_site_name']
                    PmagSiteRec['site_comp_name'] = comp
                    PmagSiteRec['site_tilt_correction'] = coords[-1]
                    PmagSiteRec['site_comp_name'] = pmag.get_list(
                        siteD, key + '_comp_name')
                    PmagSiteRec['er_specimen_names'] = pmag.get_list(
                        siteD, 'er_specimen_name')
                    PmagSiteRec['er_sample_names'] = pmag.get_list(
                        siteD, 'er_sample_name')
                    AFnum = len(
                        pmag.get_dictitem(siteD, 'magic_method_codes',
                                          'LP-DIR-AF', 'has'))
                    Tnum = len(
                        pmag.get_dictitem(siteD, 'magic_method_codes',
                                          'LP-DIR-T', 'has'))
                    DC = 3
                    if AFnum > 0: DC += 1
                    if Tnum > 0: DC += 1
                    PmagSiteRec['magic_method_codes'] = pmag.get_list(
                        siteD, 'magic_method_codes') + ':' + 'LP-DC' + str(DC)
                    PmagSiteRec['magic_method_codes'].strip(":")
                    if Daverage == 0:
                        PmagSiteRec['site_comp_name'] = pmag.get_list(
                            siteD, key + '_comp_name')
                    if plotsites == 1:
                        pmagplotlib.plotSITE(EQ['eqarea'], PmagSiteRec, siteD,
                                             key)
                        pmagplotlib.drawFIGS(EQ)
                    PmagSites.append(PmagSiteRec)
        else:
            print 'site information not found in er_sites for site, ', site, ' site will be skipped'
    for PmagSiteRec in PmagSites:  # now decorate each dictionary some more, and calculate VGPs etc. for results table
        PmagSiteRec["er_citation_names"] = "This study"
        PmagSiteRec["er_analyst_mail_names"] = user
        PmagSiteRec['magic_software_packages'] = version_num
        if agefile != "":
            PmagSiteRec = pmag.get_age(PmagSiteRec, "er_site_name",
                                       "site_inferred_", AgeNFO, DefaultAge)
        PmagSiteRec['pmag_criteria_codes'] = 'ACCEPT'
        if 'site_n_lines' in PmagSiteRec.keys(
        ) and 'site_n_planes' in PmagSiteRec.keys() and PmagSiteRec[
                'site_n_lines'] != "" and PmagSiteRec['site_n_planes'] != "":
            if int(PmagSiteRec["site_n_planes"]) > 0:
                PmagSiteRec["magic_method_codes"] = PmagSiteRec[
                    'magic_method_codes'] + ":DE-FM-LP"
            elif int(PmagSiteRec["site_n_lines"]) > 2:
                PmagSiteRec["magic_method_codes"] = PmagSiteRec[
                    'magic_method_codes'] + ":DE-FM"
            kill = pmag.grade(PmagSiteRec, accept, 'site_dir')
            if len(kill) == 0:
                PmagResRec = {
                }  # set up dictionary for the pmag_results table entry
                PmagResRec['data_type'] = 'i'  # decorate it a bit
                PmagResRec['magic_software_packages'] = version_num
                PmagSiteRec[
                    'site_description'] = 'Site direction included in results table'
                PmagResRec['pmag_criteria_codes'] = 'ACCEPT'
                dec = float(PmagSiteRec["site_dec"])
                inc = float(PmagSiteRec["site_inc"])
                if 'site_alpha95' in PmagSiteRec.keys(
                ) and PmagSiteRec['site_alpha95'] != "":
                    a95 = float(PmagSiteRec["site_alpha95"])
                else:
                    a95 = 180.
                sitedat = pmag.get_dictitem(
                    SiteNFO, 'er_site_name', PmagSiteRec['er_site_name'],
                    'T')[0]  # fish out site information (lat/lon, etc.)
                lat = float(sitedat['site_lat'])
                lon = float(sitedat['site_lon'])
                plong, plat, dp, dm = pmag.dia_vgp(
                    dec, inc, a95, lat, lon)  # get the VGP for this site
                if PmagSiteRec['site_tilt_correction'] == '-1':
                    C = ' (spec coord) '
                if PmagSiteRec['site_tilt_correction'] == '0':
                    C = ' (geog. coord) '
                if PmagSiteRec['site_tilt_correction'] == '100':
                    C = ' (strat. coord) '
                PmagResRec["pmag_result_name"] = "VGP Site: " + PmagSiteRec[
                    "er_site_name"]  # decorate some more
                PmagResRec[
                    "result_description"] = "Site VGP, coord system = " + str(
                        coord) + ' component: ' + comp
                PmagResRec['er_site_names'] = PmagSiteRec['er_site_name']
                PmagResRec['pmag_criteria_codes'] = 'ACCEPT'
                PmagResRec['er_citation_names'] = 'This study'
                PmagResRec['er_analyst_mail_names'] = user
                PmagResRec["er_location_names"] = PmagSiteRec[
                    "er_location_name"]
                if Daverage == 1:
                    PmagResRec["er_sample_names"] = PmagSiteRec[
                        "er_sample_names"]
                else:
                    PmagResRec["er_specimen_names"] = PmagSiteRec[
                        "er_specimen_names"]
                PmagResRec["tilt_correction"] = PmagSiteRec[
                    'site_tilt_correction']
                PmagResRec["pole_comp_name"] = PmagSiteRec['site_comp_name']
                PmagResRec["average_dec"] = PmagSiteRec["site_dec"]
                PmagResRec["average_inc"] = PmagSiteRec["site_inc"]
                PmagResRec["average_alpha95"] = PmagSiteRec["site_alpha95"]
                PmagResRec["average_n"] = PmagSiteRec["site_n"]
                PmagResRec["average_n_lines"] = PmagSiteRec["site_n_lines"]
                PmagResRec["average_n_planes"] = PmagSiteRec["site_n_planes"]
                PmagResRec["vgp_n"] = PmagSiteRec["site_n"]
                PmagResRec["average_k"] = PmagSiteRec["site_k"]
                PmagResRec["average_r"] = PmagSiteRec["site_r"]
                PmagResRec["average_lat"] = '%10.4f ' % (lat)
                PmagResRec["average_lon"] = '%10.4f ' % (lon)
                if agefile != "":
                    PmagResRec = pmag.get_age(PmagResRec, "er_site_names",
                                              "average_", AgeNFO, DefaultAge)
                site_height = pmag.get_dictitem(height_nfo, 'er_site_name',
                                                site, 'T')
                if len(site_height) > 0:
                    PmagResRec["average_height"] = site_height[0][
                        'site_height']
                PmagResRec["vgp_lat"] = '%7.1f ' % (plat)
                PmagResRec["vgp_lon"] = '%7.1f ' % (plong)
                PmagResRec["vgp_dp"] = '%7.1f ' % (dp)
                PmagResRec["vgp_dm"] = '%7.1f ' % (dm)
                PmagResRec["magic_method_codes"] = PmagSiteRec[
                    "magic_method_codes"]
                if PmagSiteRec['site_tilt_correction'] == '0':
                    PmagSiteRec['magic_method_codes'] = PmagSiteRec[
                        'magic_method_codes'] + ":DA-DIR-GEO"
                if PmagSiteRec['site_tilt_correction'] == '100':
                    PmagSiteRec['magic_method_codes'] = PmagSiteRec[
                        'magic_method_codes'] + ":DA-DIR-TILT"
                PmagSiteRec['site_polarity'] = ""
                if polarity == 1:  # assign polarity based on angle of pole lat to spin axis - may want to re-think this sometime
                    angle = pmag.angle([0, 0], [0, (90 - plat)])
                    if angle <= 55.: PmagSiteRec["site_polarity"] = 'n'
                    if angle > 55. and angle < 125.:
                        PmagSiteRec["site_polarity"] = 't'
                    if angle >= 125.: PmagSiteRec["site_polarity"] = 'r'
                PmagResults.append(PmagResRec)
    if polarity == 1:
        crecs = pmag.get_dictitem(PmagSites, 'site_tilt_correction', '100',
                                  'T')  # find the tilt corrected data
        if len(crecs) < 2:
            crecs = pmag.get_dictitem(
                PmagSites, 'site_tilt_correction', '0',
                'T')  # if there aren't any, find the geographic corrected data
        if len(crecs) > 2:  # if there are some,
            comp = pmag.get_list(
                crecs,
                'site_comp_name').split(':')[0]  # find the first component
            crecs = pmag.get_dictitem(
                crecs, 'site_comp_name', comp,
                'T')  # fish out all of the first component
            precs = []
            for rec in crecs:
                precs.append({
                    'dec': rec['site_dec'],
                    'inc': rec['site_inc'],
                    'name': rec['er_site_name'],
                    'loc': rec['er_location_name']
                })
            polpars = pmag.fisher_by_pol(
                precs)  # calculate average by polarity
            for mode in polpars.keys(
            ):  # hunt through all the modes (normal=A, reverse=B, all=ALL)
                PolRes = {}
                PolRes['er_citation_names'] = 'This study'
                PolRes[
                    "pmag_result_name"] = "Polarity Average: Polarity " + mode  #
                PolRes["data_type"] = "a"
                PolRes["average_dec"] = '%7.1f' % (polpars[mode]['dec'])
                PolRes["average_inc"] = '%7.1f' % (polpars[mode]['inc'])
                PolRes["average_n"] = '%i' % (polpars[mode]['n'])
                PolRes["average_r"] = '%5.4f' % (polpars[mode]['r'])
                PolRes["average_k"] = '%6.0f' % (polpars[mode]['k'])
                PolRes["average_alpha95"] = '%7.1f' % (
                    polpars[mode]['alpha95'])
                PolRes['er_site_names'] = polpars[mode]['sites']
                PolRes['er_location_names'] = polpars[mode]['locs']
                PolRes['magic_software_packages'] = version_num
                PmagResults.append(PolRes)

    if noInt != 1 and nositeints != 1:
        for site in sites:  # now do intensities for each site
            if plotsites == 1: print site
            if Iaverage == 0:
                key, intlist = 'specimen', SpecInts  # if using specimen level data
            if Iaverage == 1:
                key, intlist = 'sample', PmagSamps  # if using sample level data
            Ints = pmag.get_dictitem(
                intlist, 'er_site_name', site,
                'T')  # get all the intensities  for this site
            if len(Ints) > 0:  # there are some
                PmagSiteRec = pmag.average_int(
                    Ints, key,
                    'site')  # get average intensity stuff for site table
                PmagResRec = pmag.average_int(
                    Ints, key,
                    'average')  # get average intensity stuff for results table
                if plotsites == 1:  # if site by site examination requested - print this site out to the screen
                    for rec in Ints:
                        print rec['er_' + key + '_name'], ' %7.1f' % (
                            1e6 * float(rec[key + '_int']))
                    if len(Ints) > 1:
                        print 'Average: ', '%7.1f' % (1e6 * float(
                            PmagResRec['average_int'])), 'N: ', len(Ints)
                        print 'Sigma: ', '%7.1f' % (
                            1e6 * float(PmagResRec['average_int_sigma'])
                        ), 'Sigma %: ', PmagResRec['average_int_sigma_perc']
                    raw_input('Press any key to continue\n')
                er_location_name = Ints[0]["er_location_name"]
                PmagSiteRec[
                    "er_location_name"] = er_location_name  # decorate the records
                PmagSiteRec["er_citation_names"] = "This study"
                PmagResRec["er_location_names"] = er_location_name
                PmagResRec["er_citation_names"] = "This study"
                PmagSiteRec["er_analyst_mail_names"] = user
                PmagResRec["er_analyst_mail_names"] = user
                PmagResRec["data_type"] = 'i'
                if Iaverage == 0:
                    PmagSiteRec['er_specimen_names'] = pmag.get_list(
                        Ints, 'er_specimen_name')  # list of all specimens used
                    PmagResRec['er_specimen_names'] = pmag.get_list(
                        Ints, 'er_specimen_name')
                PmagSiteRec['er_sample_names'] = pmag.get_list(
                    Ints, 'er_sample_name')  # list of all samples used
                PmagResRec['er_sample_names'] = pmag.get_list(
                    Ints, 'er_sample_name')
                PmagSiteRec['er_site_name'] = site
                PmagResRec['er_site_names'] = site
                PmagSiteRec['magic_method_codes'] = pmag.get_list(
                    Ints, 'magic_method_codes')
                PmagResRec['magic_method_codes'] = pmag.get_list(
                    Ints, 'magic_method_codes')
                kill = pmag.grade(PmagSiteRec, accept, 'site_int')
                if nocrit == 1 or len(kill) == 0:
                    b, sig = float(PmagResRec['average_int']), ""
                    if (PmagResRec['average_int_sigma']) != "":
                        sig = float(PmagResRec['average_int_sigma'])
                    sdir = pmag.get_dictitem(PmagResults, 'er_site_names',
                                             site,
                                             'T')  # fish out site direction
                    if len(sdir) > 0 and sdir[-1][
                            'average_inc'] != "":  # get the VDM for this record using last average inclination (hope it is the right one!)
                        inc = float(sdir[0]['average_inc'])  #
                        mlat = pmag.magnetic_lat(
                            inc)  # get magnetic latitude using dipole formula
                        PmagResRec["vdm"] = '%8.3e ' % (pmag.b_vdm(
                            b, mlat))  # get VDM with magnetic latitude
                        PmagResRec["vdm_n"] = PmagResRec['average_int_n']
                        if 'average_int_sigma' in PmagResRec.keys(
                        ) and PmagResRec['average_int_sigma'] != "":
                            vdm_sig = pmag.b_vdm(
                                float(PmagResRec['average_int_sigma']), mlat)
                            PmagResRec["vdm_sigma"] = '%8.3e ' % (vdm_sig)
                        else:
                            PmagResRec["vdm_sigma"] = ""
                    mlat = ""  # define a model latitude
                    if get_model_lat == 1:  # use present site latitude
                        mlats = pmag.get_dictitem(SiteNFO, 'er_site_name',
                                                  site, 'T')
                        if len(mlats) > 0: mlat = mlats[0]['site_lat']
                    elif get_model_lat == 2:  # use a model latitude from some plate reconstruction model (or something)
                        mlats = pmag.get_dictitem(ModelLats, 'er_site_name',
                                                  site, 'T')
                        if len(mlats) > 0:
                            PmagResRec['model_lat'] = mlats[0][
                                'site_model_lat']
                        mlat = PmagResRec['model_lat']
                    if mlat != "":
                        PmagResRec["vadm"] = '%8.3e ' % (
                            pmag.b_vdm(b, float(mlat))
                        )  # get the VADM using the desired latitude
                        if sig != "":
                            vdm_sig = pmag.b_vdm(
                                float(PmagResRec['average_int_sigma']),
                                float(mlat))
                            PmagResRec["vadm_sigma"] = '%8.3e ' % (vdm_sig)
                            PmagResRec["vadm_n"] = PmagResRec['average_int_n']
                        else:
                            PmagResRec["vadm_sigma"] = ""
                    sitedat = pmag.get_dictitem(
                        SiteNFO, 'er_site_name', PmagSiteRec['er_site_name'],
                        'T')  # fish out site information (lat/lon, etc.)
                    if len(sitedat) > 0:
                        sitedat = sitedat[0]
                        PmagResRec['average_lat'] = sitedat['site_lat']
                        PmagResRec['average_lon'] = sitedat['site_lon']
                    else:
                        PmagResRec['average_lon'] = 'UNKNOWN'
                        PmagResRec['average_lon'] = 'UNKNOWN'
                    PmagResRec['magic_software_packages'] = version_num
                    PmagResRec["pmag_result_name"] = "V[A]DM: Site " + site
                    PmagResRec["result_description"] = "V[A]DM of site"
                    PmagResRec["pmag_criteria_codes"] = "ACCEPT"
                    if agefile != "":
                        PmagResRec = pmag.get_age(PmagResRec, "er_site_names",
                                                  "average_", AgeNFO,
                                                  DefaultAge)
                    site_height = pmag.get_dictitem(height_nfo, 'er_site_name',
                                                    site, 'T')
                    if len(site_height) > 0:
                        PmagResRec["average_height"] = site_height[0][
                            'site_height']
                    PmagSites.append(PmagSiteRec)
                    PmagResults.append(PmagResRec)
    if len(PmagSites) > 0:
        Tmp, keylist = pmag.fillkeys(PmagSites)
        pmag.magic_write(siteout, Tmp, 'pmag_sites')
        print ' sites written to ', siteout
    else:
        print "No Site level table"
    if len(PmagResults) > 0:
        TmpRes, keylist = pmag.fillkeys(PmagResults)
        pmag.magic_write(resout, TmpRes, 'pmag_results')
        print ' results written to ', resout
    else:
        print "No Results level table"
Ejemplo n.º 8
0
def main():
    """
    NAME
        eqarea_magic.py

    DESCRIPTION
       makes equal area projections from declination/inclination data

    SYNTAX
        eqarea_magic.py [command line options]

    INPUT
       takes magic formatted sites, samples, specimens, or measurements

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input magic format file from magic, default='sites.txt'
         supported types=[measurements, specimens, samples, sites]
        -fsp FILE: specify specimen file name, (required if you want to plot measurements by sample)
                default='specimens.txt'
        -fsa FILE: specify sample file name, (required if you want to plot specimens by site)
                default='samples.txt'
        -fsi FILE: specify site file name, default='sites.txt'

        -obj OBJ: specify  level of plot  [all, sit, sam, spc], default is all
        -crd [s,g,t]: specify coordinate system, [s]pecimen, [g]eographic, [t]ilt adjusted
                default is geographic, unspecified assumed geographic
        -fmt [svg,png,jpg] format for output plots
        -ell [F,K,B,Be,Bv] plot Fisher, Kent, Bingham, Bootstrap ellipses or Boostrap eigenvectors
        -c plot as colour contour
        -sav save plot and quit quietly
    NOTE
        all: entire file; sit: site; sam: sample; spc: specimen
    """
    # initialize some default variables
    FIG = {}  # plot dictionary
    FIG["eqarea"] = 1  # eqarea is figure 1
    plotE = 0
    plt = 0  # default to not plotting
    verbose = pmagplotlib.verbose
    # extract arguments from sys.argv
    if "-h" in sys.argv:
        print main.__doc__
        sys.exit()
    dir_path = pmag.get_named_arg_from_sys("-WD", default_val=os.getcwd())
    pmagplotlib.plot_init(FIG["eqarea"], 5, 5)
    in_file = pmag.get_named_arg_from_sys("-f", default_val="sites.txt")
    full_in_file = os.path.join(dir_path, in_file)
    plot_by = pmag.get_named_arg_from_sys("-obj", default_val="all").lower()
    spec_file = pmag.get_named_arg_from_sys("-fsp", default_val="specimens.txt")
    samp_file = pmag.get_named_arg_from_sys("-fsa", default_val="samples.txt")
    site_file = pmag.get_named_arg_from_sys("-fsi", default_val="sites.txt")
    if plot_by == "all":
        plot_key = "all"
    elif plot_by == "sit":
        plot_key = "site"
    elif plot_by == "sam":
        plot_key = "sample"
    elif plot_by == "spc":
        plot_key = "specimen"
    else:
        plot_key = "all"
    if "-c" in sys.argv:
        contour = 1
    else:
        contour = 0
    if "-sav" in sys.argv:
        plt = 1
        verbose = 0
    if "-ell" in sys.argv:
        plotE = 1
        ind = sys.argv.index("-ell")
        ell_type = sys.argv[ind + 1]
        ell_type = pmag.get_named_arg_from_sys("-ell", "F")
        dist = ell_type.upper()
        # if dist type is unrecognized, use Fisher
        if dist not in ["F", "K", "B", "BE", "BV"]:
            dist = "F"
        if dist == "BV":
            FIG["bdirs"] = 2
            pmagplotlib.plot_init(FIG["bdirs"], 5, 5)
    crd = pmag.get_named_arg_from_sys("-crd", default_val="g")
    if crd == "s":
        coord = "-1"
    elif crd == "t":
        coord = "100"
    else:
        coord = "0"

    fmt = pmag.get_named_arg_from_sys("-fmt", "svg")

    dec_key = "dir_dec"
    inc_key = "dir_inc"
    tilt_key = "dir_tilt_correction"
    # Dir_type_keys=['','site_direction_type','sample_direction_type','specimen_direction_type']

    #
    fnames = {"specimens": spec_file, "samples": samp_file, "sites": site_file}
    contribution = nb.Contribution(dir_path, custom_filenames=fnames, single_file=in_file)
    # the object that contains the DataFrame + useful helper methods:
    table_name = contribution.tables.keys()[0]
    data_container = contribution.tables[table_name]
    # the actual DataFrame:
    data = data_container.df

    # uses sample infile to add temporary site_name
    # column to the specimen table

    data_container = contribution.tables[table_name]
    data = data_container.df

    if (plot_key != "all") and (plot_key not in data.columns):
        data = contribution.propagate_name_down(plot_key, table_name)

    # add tilt key into DataFrame columns if it isn't there already
    if tilt_key not in data.columns:
        data.loc[:, tilt_key] = None

    if verbose:
        print len(data), " records read from ", in_file

    # find desired dec,inc data:
    dir_type_key = ""
    #
    # get plotlist if not plotting all records
    #
    plotlist = []
    if plot_key != "all":
        # return all where plot_key is not blank
        if plot_key not in data.columns:
            print 'Can\'t plot by "{}".  That header is not in infile: {}'.format(plot_key, in_file)
            return
        plots = data[data[plot_key].notnull()]
        plotlist = plots[plot_key].unique()  # grab unique values
    else:
        plotlist.append("All")

    for plot in plotlist:
        if verbose:
            print plot
        if plot == "All":
            # plot everything at once
            plot_data = data
        else:
            # pull out only partial data
            plot_data = data[data[plot_key] == plot]

        DIblock = []
        GCblock = []
        # SLblock, SPblock = [], []
        title = plot
        mode = 1
        k = 0

        if dec_key not in plot_data.columns:
            print "-W- No dec/inc data"
            continue
        # get all records where dec & inc values exist
        plot_data = plot_data[plot_data[dec_key].notnull() & plot_data[inc_key].notnull()]
        if plot_data.empty:
            continue
        # this sorting out is done in get_di_bock
        # if coord == '0':  # geographic, use records with no tilt key (or tilt_key 0)
        #    cond1 = plot_data[tilt_key].fillna('') == coord
        #    cond2 = plot_data[tilt_key].isnull()
        #    plot_data = plot_data[cond1 | cond2]
        # else:  # not geographic coordinates, use only records with correct tilt_key
        #    plot_data = plot_data[plot_data[tilt_key] == coord]

        # get metadata for naming the plot file
        locations = data_container.get_name("location", df_slice=plot_data)
        site = data_container.get_name("site", df_slice=plot_data)
        sample = data_container.get_name("sample", df_slice=plot_data)
        specimen = data_container.get_name("specimen", df_slice=plot_data)

        # make sure method_codes is in plot_data
        if "method_codes" not in plot_data.columns:
            plot_data["method_codes"] = ""

        # get data blocks
        DIblock = data_container.get_di_block(df_slice=plot_data, tilt_corr=coord, excl=["DE-BFP"])
        # SLblock = [[ind, row['method_codes']] for ind, row in plot_data.iterrows()]
        # get great circles
        great_circle_data = data_container.get_records_for_code("DE-BFP", incl=True, use_slice=True, sli=plot_data)

        if len(great_circle_data) > 0:
            gc_cond = great_circle_data[tilt_key] == coord
            GCblock = [[float(row[dec_key]), float(row[inc_key])] for ind, row in great_circle_data[gc_cond].iterrows()]
            # SPblock = [[ind, row['method_codes']] for ind, row in great_circle_data[gc_cond].iterrows()]

        if len(DIblock) > 0:
            if contour == 0:
                pmagplotlib.plotEQ(FIG["eqarea"], DIblock, title)
            else:
                pmagplotlib.plotEQcont(FIG["eqarea"], DIblock)
        else:
            pmagplotlib.plotNET(FIG["eqarea"])
        if len(GCblock) > 0:
            for rec in GCblock:
                pmagplotlib.plotC(FIG["eqarea"], rec, 90.0, "g")
        if len(DIblock) == 0 and len(GCblock) == 0:
            if verbose:
                print "no records for plotting"
            continue
            # sys.exit()
        if plotE == 1:
            ppars = pmag.doprinc(DIblock)  # get principal directions
            nDIs, rDIs, npars, rpars = [], [], [], []
            for rec in DIblock:
                angle = pmag.angle([rec[0], rec[1]], [ppars["dec"], ppars["inc"]])
                if angle > 90.0:
                    rDIs.append(rec)
                else:
                    nDIs.append(rec)
            if dist == "B":  # do on whole dataset
                etitle = "Bingham confidence ellipse"
                bpars = pmag.dobingham(DIblock)
                for key in bpars.keys():
                    if key != "n" and verbose:
                        print "    ", key, "%7.1f" % (bpars[key])
                    if key == "n" and verbose:
                        print "    ", key, "       %i" % (bpars[key])
                npars.append(bpars["dec"])
                npars.append(bpars["inc"])
                npars.append(bpars["Zeta"])
                npars.append(bpars["Zdec"])
                npars.append(bpars["Zinc"])
                npars.append(bpars["Eta"])
                npars.append(bpars["Edec"])
                npars.append(bpars["Einc"])
            if dist == "F":
                etitle = "Fisher confidence cone"
                if len(nDIs) > 2:
                    fpars = pmag.fisher_mean(nDIs)
                    for key in fpars.keys():
                        if key != "n" and verbose:
                            print "    ", key, "%7.1f" % (fpars[key])
                        if key == "n" and verbose:
                            print "    ", key, "       %i" % (fpars[key])
                    mode += 1
                    npars.append(fpars["dec"])
                    npars.append(fpars["inc"])
                    npars.append(fpars["alpha95"])  # Beta
                    npars.append(fpars["dec"])
                    isign = abs(fpars["inc"]) / fpars["inc"]
                    npars.append(fpars["inc"] - isign * 90.0)  # Beta inc
                    npars.append(fpars["alpha95"])  # gamma
                    npars.append(fpars["dec"] + 90.0)  # Beta dec
                    npars.append(0.0)  # Beta inc
                if len(rDIs) > 2:
                    fpars = pmag.fisher_mean(rDIs)
                    if verbose:
                        print "mode ", mode
                    for key in fpars.keys():
                        if key != "n" and verbose:
                            print "    ", key, "%7.1f" % (fpars[key])
                        if key == "n" and verbose:
                            print "    ", key, "       %i" % (fpars[key])
                    mode += 1
                    rpars.append(fpars["dec"])
                    rpars.append(fpars["inc"])
                    rpars.append(fpars["alpha95"])  # Beta
                    rpars.append(fpars["dec"])
                    isign = abs(fpars["inc"]) / fpars["inc"]
                    rpars.append(fpars["inc"] - isign * 90.0)  # Beta inc
                    rpars.append(fpars["alpha95"])  # gamma
                    rpars.append(fpars["dec"] + 90.0)  # Beta dec
                    rpars.append(0.0)  # Beta inc
            if dist == "K":
                etitle = "Kent confidence ellipse"
                if len(nDIs) > 3:
                    kpars = pmag.dokent(nDIs, len(nDIs))
                    if verbose:
                        print "mode ", mode
                    for key in kpars.keys():
                        if key != "n" and verbose:
                            print "    ", key, "%7.1f" % (kpars[key])
                        if key == "n" and verbose:
                            print "    ", key, "       %i" % (kpars[key])
                    mode += 1
                    npars.append(kpars["dec"])
                    npars.append(kpars["inc"])
                    npars.append(kpars["Zeta"])
                    npars.append(kpars["Zdec"])
                    npars.append(kpars["Zinc"])
                    npars.append(kpars["Eta"])
                    npars.append(kpars["Edec"])
                    npars.append(kpars["Einc"])
                if len(rDIs) > 3:
                    kpars = pmag.dokent(rDIs, len(rDIs))
                    if verbose:
                        print "mode ", mode
                    for key in kpars.keys():
                        if key != "n" and verbose:
                            print "    ", key, "%7.1f" % (kpars[key])
                        if key == "n" and verbose:
                            print "    ", key, "       %i" % (kpars[key])
                    mode += 1
                    rpars.append(kpars["dec"])
                    rpars.append(kpars["inc"])
                    rpars.append(kpars["Zeta"])
                    rpars.append(kpars["Zdec"])
                    rpars.append(kpars["Zinc"])
                    rpars.append(kpars["Eta"])
                    rpars.append(kpars["Edec"])
                    rpars.append(kpars["Einc"])
            else:  # assume bootstrap
                if dist == "BE":
                    if len(nDIs) > 5:
                        BnDIs = pmag.di_boot(nDIs)
                        Bkpars = pmag.dokent(BnDIs, 1.0)
                        if verbose:
                            print "mode ", mode
                        for key in Bkpars.keys():
                            if key != "n" and verbose:
                                print "    ", key, "%7.1f" % (Bkpars[key])
                            if key == "n" and verbose:
                                print "    ", key, "       %i" % (Bkpars[key])
                        mode += 1
                        npars.append(Bkpars["dec"])
                        npars.append(Bkpars["inc"])
                        npars.append(Bkpars["Zeta"])
                        npars.append(Bkpars["Zdec"])
                        npars.append(Bkpars["Zinc"])
                        npars.append(Bkpars["Eta"])
                        npars.append(Bkpars["Edec"])
                        npars.append(Bkpars["Einc"])
                    if len(rDIs) > 5:
                        BrDIs = pmag.di_boot(rDIs)
                        Bkpars = pmag.dokent(BrDIs, 1.0)
                        if verbose:
                            print "mode ", mode
                        for key in Bkpars.keys():
                            if key != "n" and verbose:
                                print "    ", key, "%7.1f" % (Bkpars[key])
                            if key == "n" and verbose:
                                print "    ", key, "       %i" % (Bkpars[key])
                        mode += 1
                        rpars.append(Bkpars["dec"])
                        rpars.append(Bkpars["inc"])
                        rpars.append(Bkpars["Zeta"])
                        rpars.append(Bkpars["Zdec"])
                        rpars.append(Bkpars["Zinc"])
                        rpars.append(Bkpars["Eta"])
                        rpars.append(Bkpars["Edec"])
                        rpars.append(Bkpars["Einc"])
                    etitle = "Bootstrapped confidence ellipse"
                elif dist == "BV":
                    sym = {"lower": ["o", "c"], "upper": ["o", "g"], "size": 3, "edgecolor": "face"}
                    if len(nDIs) > 5:
                        BnDIs = pmag.di_boot(nDIs)
                        pmagplotlib.plotEQsym(FIG["bdirs"], BnDIs, "Bootstrapped Eigenvectors", sym)
                    if len(rDIs) > 5:
                        BrDIs = pmag.di_boot(rDIs)
                        if len(nDIs) > 5:  # plot on existing plots
                            pmagplotlib.plotDIsym(FIG["bdirs"], BrDIs, sym)
                        else:
                            pmagplotlib.plotEQ(FIG["bdirs"], BrDIs, "Bootstrapped Eigenvectors")
            if dist == "B":
                if len(nDIs) > 3 or len(rDIs) > 3:
                    pmagplotlib.plotCONF(FIG["eqarea"], etitle, [], npars, 0)
            elif len(nDIs) > 3 and dist != "BV":
                pmagplotlib.plotCONF(FIG["eqarea"], etitle, [], npars, 0)
                if len(rDIs) > 3:
                    pmagplotlib.plotCONF(FIG["eqarea"], etitle, [], rpars, 0)
            elif len(rDIs) > 3 and dist != "BV":
                pmagplotlib.plotCONF(FIG["eqarea"], etitle, [], rpars, 0)

        for key in FIG.keys():
            files = {}
            filename = pmag.get_named_arg_from_sys("-fname")
            if filename:
                filename += "." + fmt
            else:
                filename = (
                    "LO:_"
                    + locations
                    + "_SI:_"
                    + site
                    + "_SA:_"
                    + sample
                    + "_SP:_"
                    + specimen
                    + "_CO:_"
                    + crd
                    + "_TY:_"
                    + key
                    + "_."
                    + fmt
                )
            files[key] = filename

        if pmagplotlib.isServer:
            black = "#000000"
            purple = "#800080"
            titles = {}
            titles["eq"] = "Equal Area Plot"
            FIG = pmagplotlib.addBorders(FIG, titles, black, purple)
            pmagplotlib.saveP(FIG, files)

        if plt:
            pmagplotlib.saveP(FIG, files)
            continue
        if verbose:
            pmagplotlib.drawFIGS(FIG)
            ans = raw_input(" S[a]ve to save plot, [q]uit, Return to continue:  ")
            if ans == "q":
                sys.exit()
            if ans == "a":
                pmagplotlib.saveP(FIG, files)
        continue
Ejemplo n.º 9
0
def main():
    """
    NAME
       fishqq.py

    DESCRIPTION
       makes qq plot from dec,inc input data

    INPUT FORMAT
       takes dec/inc pairs in space delimited file

    SYNTAX
       fishqq.py [command line options]

    OPTIONS
        -h help message
        -f FILE, specify file on command line
        -F FILE, specify output file for statistics
        -sav save and quit [saves as input file name plus fmt extension]
        -fmt specify format for output [png, eps, svg, pdf] 

    OUTPUT:
        Dec Inc N Mu Mu_crit Me Me_crit Y/N
     where direction is the principal component and Y/N is Fisherian or not
     separate lines for each mode with N >=10 (N and R)
    """
    fmt,plot='svg',0
    outfile=""
    if '-h' in sys.argv: # check if help is needed
        print main.__doc__
        sys.exit() # graceful quit
    elif '-f' in sys.argv: # ask for filename
        ind=sys.argv.index('-f')
        file=sys.argv[ind+1]
        f=open(file,'rU')
        data=f.readlines()
    if '-F' in sys.argv:
        ind=sys.argv.index('-F')
        outfile=open(sys.argv[ind+1],'w') # open output file
    if '-sav' in sys.argv: plot=1
    if '-fmt' in sys.argv:
        ind=sys.argv.index('-fmt')
        fmt=sys.argv[ind+1]
    DIs,nDIs,rDIs= [],[],[] # set up list for data
    for line in data:   # read in the data from standard input
        if '\t' in line:
            rec=line.split('\t') # split each line on space to get records
        else:
            rec=line.split() # split each line on space to get records
        DIs.append([float(rec[0]),float(rec[1])]) # append data to Inc
# split into two modes
    ppars=pmag.doprinc(DIs) # get principal directions
    for rec in DIs:
        angle=pmag.angle([rec[0],rec[1]],[ppars['dec'],ppars['inc']])
        if angle>90.:
            rDIs.append(rec)
        else:
            nDIs.append(rec)
    
#
    if len(rDIs) >=10 or len(nDIs) >=10:
        D1,I1=[],[]
        QQ={'unf1':1,'exp1':2}
        pmagplotlib.plot_init(QQ['unf1'],5,5)
        pmagplotlib.plot_init(QQ['exp1'],5,5)
        if len(nDIs) < 10: 
            ppars=pmag.doprinc(rDIs) # get principal directions
            Drbar,Irbar=ppars['dec']-180.,-ppars['inc']
            Nr=len(rDIs)
            for di in rDIs:
                d,irot=pmag.dotilt(di[0],di[1],Drbar-180.,90.-Irbar) # rotate to mean
                drot=d-180.
                if drot<0:drot=drot+360.
                D1.append(drot)           
                I1.append(irot) 
                Dtit='Mode 2 Declinations'
                Itit='Mode 2 Inclinations'
        else:          
            ppars=pmag.doprinc(nDIs) # get principal directions
            Dnbar,Inbar=ppars['dec'],ppars['inc']
            Nn=len(nDIs)
            for di in nDIs:
                d,irot=pmag.dotilt(di[0],di[1],Dnbar-180.,90.-Inbar) # rotate to mean
                drot=d-180.
                if drot<0:drot=drot+360.
                D1.append(drot)
                I1.append(irot)
                Dtit='Mode 1 Declinations'
                Itit='Mode 1 Inclinations'
        Mu_n,Mu_ncr=pmagplotlib.plotQQunf(QQ['unf1'],D1,Dtit) # make plot
        Me_n,Me_ncr=pmagplotlib.plotQQexp(QQ['exp1'],I1,Itit) # make plot
        #print Mu_n,Mu_ncr,Me_n, Me_ncr
        if outfile!="":
#        Dec Inc N Mu Mu_crit Me Me_crit Y/N
            if Mu_n<=Mu_ncr and Me_n<=Me_ncr:
               F='Y'
            else:
               F='N'
            outstring='%7.1f %7.1f %i %5.3f %5.3f %5.3f %5.3f %s \n'%(Dnbar,Inbar,Nn,Mu_n,Mu_ncr,Me_n,Me_ncr,F)
            outfile.write(outstring)
    else:
        print 'you need N> 10 for at least one mode'
        sys.exit()
    if len(rDIs)>10 and len(nDIs)>10:
        D2,I2=[],[]
        QQ['unf2']=3
        QQ['exp2']=4
        pmagplotlib.plot_init(QQ['unf2'],5,5)
        pmagplotlib.plot_init(QQ['exp2'],5,5)
        ppars=pmag.doprinc(rDIs) # get principal directions
        Drbar,Irbar=ppars['dec']-180.,-ppars['inc']
        Nr=len(rDIs)
        for di in rDIs:
            d,irot=pmag.dotilt(di[0],di[1],Drbar-180.,90.-Irbar) # rotate to mean
            drot=d-180.
            if drot<0:drot=drot+360.
            D2.append(drot)           
            I2.append(irot) 
            Dtit='Mode 2 Declinations'
            Itit='Mode 2 Inclinations'
        Mu_r,Mu_rcr=pmagplotlib.plotQQunf(QQ['unf2'],D2,Dtit) # make plot
        Me_r,Me_rcr=pmagplotlib.plotQQexp(QQ['exp2'],I2,Itit) # make plot
        if outfile!="":
#        Dec Inc N Mu Mu_crit Me Me_crit Y/N
            if Mu_r<=Mu_rcr and Me_r<=Me_rcr:
               F='Y'
            else:
               F='N'
            outstring='%7.1f %7.1f %i %5.3f %5.3f %5.3f %5.3f %s \n'%(Drbar,Irbar,Nr,Mu_r,Mu_rcr,Me_r,Me_rcr,F)
            outfile.write(outstring)
    files={}
    for key in QQ.keys():
        files[key]=file+'_'+key+'.'+fmt 
    if pmagplotlib.isServer:
        black     = '#000000'
        purple    = '#800080'
        titles={}
        titles['eq']='Equal Area Plot'
        EQ = pmagplotlib.addBorders(EQ,titles,black,purple)
        pmagplotlib.saveP(QQ,files)
    elif plot==1:
        pmagplotlib.saveP(QQ,files)
    else:
        pmagplotlib.drawFIGS(QQ) 
        ans=raw_input(" S[a]ve to save plot, [q]uit without saving:  ")
        if ans=="a": pmagplotlib.saveP(QQ,files)
Ejemplo n.º 10
0
def main():
    """
    NAME
        eqarea_magic.py

    DESCRIPTION
       makes equal area projections from declination/inclination data

    SYNTAX 
        eqarea_magic.py [command line options]
    
    INPUT 
       takes magic formatted pmag_results, pmag_sites, pmag_samples or pmag_specimens
    
    OPTIONS
        -h prints help message and quits
        -f FILE: specify input magic format file from magic,default='pmag_results.txt'
         supported types=[magic_measurements,pmag_specimens, pmag_samples, pmag_sites, pmag_results, magic_web]
        -obj OBJ: specify  level of plot  [all, sit, sam, spc], default is all
        -crd [s,g,t]: specify coordinate system, [s]pecimen, [g]eographic, [t]ilt adjusted
                default is geographic, unspecified assumed geographic
        -fmt [svg,png,jpg] format for output plots
        -ell [F,K,B,Be,Bv] plot Fisher, Kent, Bingham, Bootstrap ellipses or Boostrap eigenvectors
        -c plot as colour contour 
        -sav save plot and quit quietly
    NOTE
        all: entire file; sit: site; sam: sample; spc: specimen
    """
    FIG={} # plot dictionary
    FIG['eqarea']=1 # eqarea is figure 1
    in_file,plot_key,coord,crd='pmag_results.txt','all',"0",'g'
    plotE,contour=0,0
    dir_path='.'
    fmt='svg'
    verbose=pmagplotlib.verbose
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-WD' in sys.argv:
        ind=sys.argv.index('-WD')
        dir_path=sys.argv[ind+1]
    pmagplotlib.plot_init(FIG['eqarea'],5,5)
    if '-f' in sys.argv:
        ind=sys.argv.index("-f")
        in_file=dir_path+"/"+sys.argv[ind+1]
    if '-obj' in sys.argv:
        ind=sys.argv.index('-obj')
        plot_by=sys.argv[ind+1]
        if plot_by=='all':plot_key='all'
        if plot_by=='sit':plot_key='er_site_name'
        if plot_by=='sam':plot_key='er_sample_name'
        if plot_by=='spc':plot_key='er_specimen_name'
    if '-c' in sys.argv: contour=1
    plt=0
    if '-sav' in sys.argv: 
        plt=1
        verbose=0
    if '-ell' in sys.argv:
        plotE=1
        ind=sys.argv.index('-ell')
        ell_type=sys.argv[ind+1]
        if ell_type=='F':dist='F' 
        if ell_type=='K':dist='K' 
        if ell_type=='B':dist='B' 
        if ell_type=='Be':dist='BE' 
        if ell_type=='Bv':
            dist='BV' 
            FIG['bdirs']=2
            pmagplotlib.plot_init(FIG['bdirs'],5,5)
    if '-crd' in sys.argv:
        ind=sys.argv.index("-crd")
        crd=sys.argv[ind+1]
        if crd=='s':coord="-1"
        if crd=='g':coord="0"
        if crd=='t':coord="100"
    if '-fmt' in sys.argv:
        ind=sys.argv.index("-fmt")
        fmt=sys.argv[ind+1]
    Dec_keys=['site_dec','sample_dec','specimen_dec','measurement_dec','average_dec','none']
    Inc_keys=['site_inc','sample_inc','specimen_inc','measurement_inc','average_inc','none']
    Tilt_keys=['tilt_correction','site_tilt_correction','sample_tilt_correction','specimen_tilt_correction','none']
    Dir_type_keys=['','site_direction_type','sample_direction_type','specimen_direction_type']
    Name_keys=['er_specimen_name','er_sample_name','er_site_name','pmag_result_name']
    data,file_type=pmag.magic_read(in_file)
    if file_type=='pmag_results' and plot_key!="all":plot_key=plot_key+'s' # need plural for results table
    if verbose:    
        print len(data),' records read from ',in_file
    #
    #
    # find desired dec,inc data:
    #
    dir_type_key=''
    #
    # get plotlist if not plotting all records
    #
    plotlist=[]
    if plot_key!="all":
        plots=pmag.get_dictitem(data,plot_key,'','F')
        for  rec in plots:
            if rec[plot_key] not in plotlist:
                plotlist.append(rec[plot_key])
        plotlist.sort()
    else:
        plotlist.append('All')
    for plot in plotlist:
        #if verbose: print plot
        DIblock=[]
        GCblock=[]
        SLblock,SPblock=[],[]
        title=plot
        mode=1
        dec_key,inc_key,tilt_key,name_key,k="","","","",0
        if plot!="All": 
            odata=pmag.get_dictitem(data,plot_key,plot,'T')
        else: odata=data # data for this obj
        for dec_key in Dec_keys:
            Decs=pmag.get_dictitem(odata,dec_key,'','F') # get all records with this dec_key not blank 
            if len(Decs)>0: break
        for inc_key in Inc_keys:
            Incs=pmag.get_dictitem(Decs,inc_key,'','F') # get all records with this inc_key not blank 
            if len(Incs)>0: break
        for tilt_key in Tilt_keys:
            if tilt_key in Incs[0].keys(): break # find the tilt_key for these records
        if tilt_key=='none': # no tilt key in data, need to fix this with fake data which will be unknown tilt
            tilt_key='tilt_correction'
            for rec in Incs:rec[tilt_key]=''
        cdata=pmag.get_dictitem(Incs,tilt_key,coord,'T') # get all records matching specified coordinate system
        if coord=='0': # geographic
            udata=pmag.get_dictitem(Incs,tilt_key,'','T') # get all the blank records - assume geographic
            if len(cdata)==0: crd='' 
            if len(udata)>0:
                for d in udata:cdata.append(d)  
                crd=crd+'u'
        for name_key in Name_keys:
            Names=pmag.get_dictitem(cdata,name_key,'','F') # get all records with this name_key not blank 
            if len(Names)>0: break
        for dir_type_key in Dir_type_keys:
            Dirs=pmag.get_dictitem(cdata,dir_type_key,'','F') # get all records with this direction type
            if len(Dirs)>0: break
        if dir_type_key=="":dir_type_key='direction_type'
        locations,site,sample,specimen="","","",""
        for rec in cdata: # pick out the data
            if 'er_location_name' in rec.keys() and rec['er_location_name']!="" and rec['er_location_name'] not in locations:locations=locations+rec['er_location_name'].replace("/","")+"_"
            if 'er_location_names' in rec.keys() and rec['er_location_names']!="":
               locs=rec['er_location_names'].split(':')
               for loc in locs:
                   if loc not in locations:locations=locations+loc.replace("/","")+'_'
            if plot_key=='er_site_name' or plot_key=='er_sample_name' or plot_key=='er_specimen_name':
                site=rec['er_site_name']
            if plot_key=='er_sample_name' or plot_key=='er_specimen_name':
                sample=rec['er_sample_name']
            if plot_key=='er_specimen_name':
                specimen=rec['er_specimen_name']
            if plot_key=='er_site_names' or plot_key=='er_sample_names' or plot_key=='er_specimen_names':
                site=rec['er_site_names']
            if plot_key=='er_sample_names' or plot_key=='er_specimen_names':
                sample=rec['er_sample_names']
            if plot_key=='er_specimen_names':
                specimen=rec['er_specimen_names']
            if dir_type_key not in rec.keys() or rec[dir_type_key]=="":rec[dir_type_key]='l'
            if 'magic_method_codes' not in rec.keys():rec['magic_method_codes']=""
            DIblock.append([float(rec[dec_key]),float(rec[inc_key])])
            SLblock.append([rec[name_key],rec['magic_method_codes']])
            if rec[tilt_key]==coord and rec[dir_type_key]!='l' and rec[dec_key]!="" and rec[inc_key]!="":
                GCblock.append([float(rec[dec_key]),float(rec[inc_key])])
                SPblock.append([rec[name_key],rec['magic_method_codes']])
        if len(DIblock)==0 and len(GCblock)==0:
            if verbose: print "no records for plotting"
            sys.exit()
        if verbose:
          for k in range(len(SLblock)):
            print '%s %s %7.1f %7.1f'%(SLblock[k][0],SLblock[k][1],DIblock[k][0],DIblock[k][1])
          for k in range(len(SPblock)):
            print '%s %s %7.1f %7.1f'%(SPblock[k][0],SPblock[k][1],GCblock[k][0],GCblock[k][1])
        if len(DIblock)>0: 
            if contour==0:
                pmagplotlib.plotEQ(FIG['eqarea'],DIblock,title)
            else:
                pmagplotlib.plotEQcont(FIG['eqarea'],DIblock)
        else:   pmagplotlib.plotNET(FIG['eqarea'])
        if len(GCblock)>0:
            for rec in GCblock: pmagplotlib.plotC(FIG['eqarea'],rec,90.,'g')
        if plotE==1:
            ppars=pmag.doprinc(DIblock) # get principal directions
            nDIs,rDIs,npars,rpars=[],[],[],[]
            for rec in DIblock:
                angle=pmag.angle([rec[0],rec[1]],[ppars['dec'],ppars['inc']])
                if angle>90.:
                    rDIs.append(rec)
                else:
                    nDIs.append(rec)
            if dist=='B': # do on whole dataset
                etitle="Bingham confidence ellipse"
                bpars=pmag.dobingham(DIblock)
                for key in bpars.keys():
                    if key!='n' and verbose:print "    ",key, '%7.1f'%(bpars[key])
                    if key=='n' and verbose:print "    ",key, '       %i'%(bpars[key])
                npars.append(bpars['dec']) 
                npars.append(bpars['inc'])
                npars.append(bpars['Zeta']) 
                npars.append(bpars['Zdec']) 
                npars.append(bpars['Zinc'])
                npars.append(bpars['Eta']) 
                npars.append(bpars['Edec']) 
                npars.append(bpars['Einc'])
            if dist=='F':
                etitle="Fisher confidence cone"
                if len(nDIs)>2:
                    fpars=pmag.fisher_mean(nDIs)
                    for key in fpars.keys():
                        if key!='n' and verbose:print "    ",key, '%7.1f'%(fpars[key])
                        if key=='n' and verbose:print "    ",key, '       %i'%(fpars[key])
                    mode+=1
                    npars.append(fpars['dec']) 
                    npars.append(fpars['inc'])
                    npars.append(fpars['alpha95']) # Beta
                    npars.append(fpars['dec']) 
                    isign=abs(fpars['inc'])/fpars['inc'] 
                    npars.append(fpars['inc']-isign*90.) #Beta inc
                    npars.append(fpars['alpha95']) # gamma 
                    npars.append(fpars['dec']+90.) # Beta dec
                    npars.append(0.) #Beta inc
                if len(rDIs)>2:
                    fpars=pmag.fisher_mean(rDIs)
                    if verbose:print "mode ",mode
                    for key in fpars.keys():
                        if key!='n' and verbose:print "    ",key, '%7.1f'%(fpars[key])
                        if key=='n' and verbose:print "    ",key, '       %i'%(fpars[key])
                    mode+=1
                    rpars.append(fpars['dec']) 
                    rpars.append(fpars['inc'])
                    rpars.append(fpars['alpha95']) # Beta
                    rpars.append(fpars['dec']) 
                    isign=abs(fpars['inc'])/fpars['inc'] 
                    rpars.append(fpars['inc']-isign*90.) #Beta inc
                    rpars.append(fpars['alpha95']) # gamma 
                    rpars.append(fpars['dec']+90.) # Beta dec
                    rpars.append(0.) #Beta inc
            if dist=='K':
                etitle="Kent confidence ellipse"
                if len(nDIs)>3:
                    kpars=pmag.dokent(nDIs,len(nDIs))
                    if verbose:print "mode ",mode
                    for key in kpars.keys():
                        if key!='n' and verbose:print "    ",key, '%7.1f'%(kpars[key])
                        if key=='n' and verbose:print "    ",key, '       %i'%(kpars[key])
                    mode+=1
                    npars.append(kpars['dec']) 
                    npars.append(kpars['inc'])
                    npars.append(kpars['Zeta']) 
                    npars.append(kpars['Zdec']) 
                    npars.append(kpars['Zinc'])
                    npars.append(kpars['Eta']) 
                    npars.append(kpars['Edec']) 
                    npars.append(kpars['Einc'])
                if len(rDIs)>3:
                    kpars=pmag.dokent(rDIs,len(rDIs))
                    if verbose:print "mode ",mode
                    for key in kpars.keys():
                        if key!='n' and verbose:print "    ",key, '%7.1f'%(kpars[key])
                        if key=='n' and verbose:print "    ",key, '       %i'%(kpars[key])
                    mode+=1
                    rpars.append(kpars['dec']) 
                    rpars.append(kpars['inc'])
                    rpars.append(kpars['Zeta']) 
                    rpars.append(kpars['Zdec']) 
                    rpars.append(kpars['Zinc'])
                    rpars.append(kpars['Eta']) 
                    rpars.append(kpars['Edec']) 
                    rpars.append(kpars['Einc'])
            else: # assume bootstrap
                if dist=='BE':
                    if len(nDIs)>5:
                        BnDIs=pmag.di_boot(nDIs)
                        Bkpars=pmag.dokent(BnDIs,1.)
                        if verbose:print "mode ",mode
                        for key in Bkpars.keys():
                            if key!='n' and verbose:print "    ",key, '%7.1f'%(Bkpars[key])
                            if key=='n' and verbose:print "    ",key, '       %i'%(Bkpars[key])
                        mode+=1
                        npars.append(Bkpars['dec']) 
                        npars.append(Bkpars['inc'])
                        npars.append(Bkpars['Zeta']) 
                        npars.append(Bkpars['Zdec']) 
                        npars.append(Bkpars['Zinc'])
                        npars.append(Bkpars['Eta']) 
                        npars.append(Bkpars['Edec']) 
                        npars.append(Bkpars['Einc'])
                    if len(rDIs)>5:
                        BrDIs=pmag.di_boot(rDIs)
                        Bkpars=pmag.dokent(BrDIs,1.)
                        if verbose:print "mode ",mode
                        for key in Bkpars.keys():
                            if key!='n' and verbose:print "    ",key, '%7.1f'%(Bkpars[key])
                            if key=='n' and verbose:print "    ",key, '       %i'%(Bkpars[key])
                        mode+=1
                        rpars.append(Bkpars['dec']) 
                        rpars.append(Bkpars['inc'])
                        rpars.append(Bkpars['Zeta']) 
                        rpars.append(Bkpars['Zdec']) 
                        rpars.append(Bkpars['Zinc'])
                        rpars.append(Bkpars['Eta']) 
                        rpars.append(Bkpars['Edec']) 
                        rpars.append(Bkpars['Einc'])
                    etitle="Bootstrapped confidence ellipse"
                elif dist=='BV':
                    sym={'lower':['o','c'],'upper':['o','g'],'size':3,'edgecolor':'face'}
                    if len(nDIs)>5:
                        BnDIs=pmag.di_boot(nDIs)
                        pmagplotlib.plotEQsym(FIG['bdirs'],BnDIs,'Bootstrapped Eigenvectors', sym)
                    if len(rDIs)>5:
                        BrDIs=pmag.di_boot(rDIs)
                        if len(nDIs)>5:  # plot on existing plots
                            pmagplotlib.plotDIsym(FIG['bdirs'],BrDIs,sym)
                        else:
                            pmagplotlib.plotEQ(FIG['bdirs'],BrDIs,'Bootstrapped Eigenvectors')
            if dist=='B':
                if len(nDIs)> 3 or len(rDIs)>3: pmagplotlib.plotCONF(FIG['eqarea'],etitle,[],npars,0)
            elif len(nDIs)>3 and dist!='BV':
                pmagplotlib.plotCONF(FIG['eqarea'],etitle,[],npars,0)
                if len(rDIs)>3:
                    pmagplotlib.plotCONF(FIG['eqarea'],etitle,[],rpars,0)
            elif len(rDIs)>3 and dist!='BV':
                pmagplotlib.plotCONF(FIG['eqarea'],etitle,[],rpars,0)
        if verbose:pmagplotlib.drawFIGS(FIG)
            #
        files={}
        locations=locations[:-1]
        for key in FIG.keys():
            filename='LO:_'+locations+'_SI:_'+site+'_SA:_'+sample+'_SP:_'+specimen+'_CO:_'+crd+'_TY:_'+key+'_.'+fmt
            files[key]=filename 
        if pmagplotlib.isServer:
            black     = '#000000'
            purple    = '#800080'
            titles={}
            titles['eq']='Equal Area Plot'
            FIG = pmagplotlib.addBorders(FIG,titles,black,purple)
            pmagplotlib.saveP(FIG,files)
        elif verbose:
            ans=raw_input(" S[a]ve to save plot, [q]uit, Return to continue:  ")
            if ans=="q": sys.exit()
            if ans=="a": pmagplotlib.saveP(FIG,files) 
        if plt:
           pmagplotlib.saveP(FIG,files) 
Ejemplo n.º 11
0
def main():
    """
    NAME
        eqarea_ell.py

    DESCRIPTION
       makes equal area projections from declination/inclination data
       and plot ellipses

    SYNTAX 
        eqarea_ell.py -h [command line options]
    
    INPUT 
       takes space delimited Dec/Inc data
    
    OPTIONS
        -h prints help message and quits
        -f FILE
        -fmt [svg,png,jpg] format for output plots
        -sav  saves figures and quits
        -ell [F,K,B,Be,Bv] plot Fisher, Kent, Bingham, Bootstrap ellipses or Boostrap eigenvectors
    """
    FIG = {}  # plot dictionary
    FIG['eq'] = 1  # eqarea is figure 1
    fmt, dist, mode, plot = 'svg', 'F', 1, 0
    sym = {'lower': ['o', 'r'], 'upper': ['o', 'w'], 'size': 10}
    plotE = 0
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    pmagplotlib.plot_init(FIG['eq'], 5, 5)
    if '-sav' in sys.argv: plot = 1
    if '-f' in sys.argv:
        ind = sys.argv.index("-f")
        title = sys.argv[ind + 1]
        data = numpy.loadtxt(title).transpose()
    if '-ell' in sys.argv:
        plotE = 1
        ind = sys.argv.index('-ell')
        ell_type = sys.argv[ind + 1]
        if ell_type == 'F': dist = 'F'
        if ell_type == 'K': dist = 'K'
        if ell_type == 'B': dist = 'B'
        if ell_type == 'Be': dist = 'BE'
        if ell_type == 'Bv':
            dist = 'BV'
            FIG['bdirs'] = 2
            pmagplotlib.plot_init(FIG['bdirs'], 5, 5)
    if '-fmt' in sys.argv:
        ind = sys.argv.index("-fmt")
        fmt = sys.argv[ind + 1]
    DIblock = numpy.array([data[0], data[1]]).transpose()
    if len(DIblock) > 0:
        pmagplotlib.plotEQsym(FIG['eq'], DIblock, title, sym)
        if plot == 0: pmagplotlib.drawFIGS(FIG)
    else:
        print("no data to plot")
        sys.exit()
    if plotE == 1:
        ppars = pmag.doprinc(DIblock)  # get principal directions
        nDIs, rDIs, npars, rpars = [], [], [], []
        for rec in DIblock:
            angle = pmag.angle([rec[0], rec[1]], [ppars['dec'], ppars['inc']])
            if angle > 90.:
                rDIs.append(rec)
            else:
                nDIs.append(rec)
        if dist == 'B':  # do on whole dataset
            etitle = "Bingham confidence ellipse"
            bpars = pmag.dobingham(DIblock)
            for key in list(bpars.keys()):
                if key != 'n' and pmagplotlib.verbose:
                    print("    ", key, '%7.1f' % (bpars[key]))
                if key == 'n' and pmagplotlib.verbose:
                    print("    ", key, '       %i' % (bpars[key]))
            npars.append(bpars['dec'])
            npars.append(bpars['inc'])
            npars.append(bpars['Zeta'])
            npars.append(bpars['Zdec'])
            npars.append(bpars['Zinc'])
            npars.append(bpars['Eta'])
            npars.append(bpars['Edec'])
            npars.append(bpars['Einc'])
        if dist == 'F':
            etitle = "Fisher confidence cone"
            if len(nDIs) > 3:
                fpars = pmag.fisher_mean(nDIs)
                for key in list(fpars.keys()):
                    if key != 'n' and pmagplotlib.verbose:
                        print("    ", key, '%7.1f' % (fpars[key]))
                    if key == 'n' and pmagplotlib.verbose:
                        print("    ", key, '       %i' % (fpars[key]))
                mode += 1
                npars.append(fpars['dec'])
                npars.append(fpars['inc'])
                npars.append(fpars['alpha95'])  # Beta
                npars.append(fpars['dec'])
                isign = old_div(abs(fpars['inc']), fpars['inc'])
                npars.append(fpars['inc'] - isign * 90.)  #Beta inc
                npars.append(fpars['alpha95'])  # gamma
                npars.append(fpars['dec'] + 90.)  # Beta dec
                npars.append(0.)  #Beta inc
            if len(rDIs) > 3:
                fpars = pmag.fisher_mean(rDIs)
                if pmagplotlib.verbose: print("mode ", mode)
                for key in list(fpars.keys()):
                    if key != 'n' and pmagplotlib.verbose:
                        print("    ", key, '%7.1f' % (fpars[key]))
                    if key == 'n' and pmagplotlib.verbose:
                        print("    ", key, '       %i' % (fpars[key]))
                mode += 1
                rpars.append(fpars['dec'])
                rpars.append(fpars['inc'])
                rpars.append(fpars['alpha95'])  # Beta
                rpars.append(fpars['dec'])
                isign = old_div(abs(fpars['inc']), fpars['inc'])
                rpars.append(fpars['inc'] - isign * 90.)  #Beta inc
                rpars.append(fpars['alpha95'])  # gamma
                rpars.append(fpars['dec'] + 90.)  # Beta dec
                rpars.append(0.)  #Beta inc
        if dist == 'K':
            etitle = "Kent confidence ellipse"
            if len(nDIs) > 3:
                kpars = pmag.dokent(nDIs, len(nDIs))
                if pmagplotlib.verbose: print("mode ", mode)
                for key in list(kpars.keys()):
                    if key != 'n' and pmagplotlib.verbose:
                        print("    ", key, '%7.1f' % (kpars[key]))
                    if key == 'n' and pmagplotlib.verbose:
                        print("    ", key, '       %i' % (kpars[key]))
                mode += 1
                npars.append(kpars['dec'])
                npars.append(kpars['inc'])
                npars.append(kpars['Zeta'])
                npars.append(kpars['Zdec'])
                npars.append(kpars['Zinc'])
                npars.append(kpars['Eta'])
                npars.append(kpars['Edec'])
                npars.append(kpars['Einc'])
            if len(rDIs) > 3:
                kpars = pmag.dokent(rDIs, len(rDIs))
                if pmagplotlib.verbose: print("mode ", mode)
                for key in list(kpars.keys()):
                    if key != 'n' and pmagplotlib.verbose:
                        print("    ", key, '%7.1f' % (kpars[key]))
                    if key == 'n' and pmagplotlib.verbose:
                        print("    ", key, '       %i' % (kpars[key]))
                mode += 1
                rpars.append(kpars['dec'])
                rpars.append(kpars['inc'])
                rpars.append(kpars['Zeta'])
                rpars.append(kpars['Zdec'])
                rpars.append(kpars['Zinc'])
                rpars.append(kpars['Eta'])
                rpars.append(kpars['Edec'])
                rpars.append(kpars['Einc'])
        else:  # assume bootstrap
            if len(nDIs) < 10 and len(rDIs) < 10:
                print('too few data points for bootstrap')
                sys.exit()
            if dist == 'BE':
                print('Be patient for bootstrap...')
                if len(nDIs) >= 10:
                    BnDIs = pmag.di_boot(nDIs)
                    Bkpars = pmag.dokent(BnDIs, 1.)
                    if pmagplotlib.verbose: print("mode ", mode)
                    for key in list(Bkpars.keys()):
                        if key != 'n' and pmagplotlib.verbose:
                            print("    ", key, '%7.1f' % (Bkpars[key]))
                        if key == 'n' and pmagplotlib.verbose:
                            print("    ", key, '       %i' % (Bkpars[key]))
                    mode += 1
                    npars.append(Bkpars['dec'])
                    npars.append(Bkpars['inc'])
                    npars.append(Bkpars['Zeta'])
                    npars.append(Bkpars['Zdec'])
                    npars.append(Bkpars['Zinc'])
                    npars.append(Bkpars['Eta'])
                    npars.append(Bkpars['Edec'])
                    npars.append(Bkpars['Einc'])
                if len(rDIs) >= 10:
                    BrDIs = pmag.di_boot(rDIs)
                    Bkpars = pmag.dokent(BrDIs, 1.)
                    if pmagplotlib.verbose: print("mode ", mode)
                    for key in list(Bkpars.keys()):
                        if key != 'n' and pmagplotlib.verbose:
                            print("    ", key, '%7.1f' % (Bkpars[key]))
                        if key == 'n' and pmagplotlib.verbose:
                            print("    ", key, '       %i' % (Bkpars[key]))
                    mode += 1
                    rpars.append(Bkpars['dec'])
                    rpars.append(Bkpars['inc'])
                    rpars.append(Bkpars['Zeta'])
                    rpars.append(Bkpars['Zdec'])
                    rpars.append(Bkpars['Zinc'])
                    rpars.append(Bkpars['Eta'])
                    rpars.append(Bkpars['Edec'])
                    rpars.append(Bkpars['Einc'])
                etitle = "Bootstrapped confidence ellipse"
            elif dist == 'BV':
                print('Be patient for bootstrap...')
                vsym = {'lower': ['+', 'k'], 'upper': ['x', 'k'], 'size': 5}
                if len(nDIs) > 5:
                    BnDIs = pmag.di_boot(nDIs)
                    pmagplotlib.plotEQsym(FIG['bdirs'], BnDIs,
                                          'Bootstrapped Eigenvectors', vsym)
                if len(rDIs) > 5:
                    BrDIs = pmag.di_boot(rDIs)
                    if len(nDIs) > 5:  # plot on existing plots
                        pmagplotlib.plotDIsym(FIG['bdirs'], BrDIs, vsym)
                    else:
                        pmagplotlib.plotEQ(FIG['bdirs'], BrDIs,
                                           'Bootstrapped Eigenvectors', vsym)
        if dist == 'B':
            if len(nDIs) > 3 or len(rDIs) > 3:
                pmagplotlib.plotCONF(FIG['eq'], etitle, [], npars, 0)
        elif len(nDIs) > 3 and dist != 'BV':
            pmagplotlib.plotCONF(FIG['eq'], etitle, [], npars, 0)
            if len(rDIs) > 3:
                pmagplotlib.plotCONF(FIG['eq'], etitle, [], rpars, 0)
        elif len(rDIs) > 3 and dist != 'BV':
            pmagplotlib.plotCONF(FIG['eq'], etitle, [], rpars, 0)
        if plot == 0: pmagplotlib.drawFIGS(FIG)
    if plot == 0: pmagplotlib.drawFIGS(FIG)
    #
    files = {}
    for key in list(FIG.keys()):
        files[key] = title + '_' + key + '.' + fmt
    if pmagplotlib.isServer:
        black = '#000000'
        purple = '#800080'
        titles = {}
        titles['eq'] = 'Equal Area Plot'
        FIG = pmagplotlib.addBorders(FIG, titles, black, purple)
        pmagplotlib.saveP(FIG, files)
    elif plot == 0:
        ans = input(" S[a]ve to save plot, [q]uit, Return to continue:  ")
        if ans == "q": sys.exit()
        if ans == "a":
            pmagplotlib.saveP(FIG, files)
    else:
        pmagplotlib.saveP(FIG, files)
Ejemplo n.º 12
0
def main():
    """
    NAME
        angle.py
    
    DESCRIPTION
      calculates angle between two input directions D1,D2
    
    INPUT (COMMAND LINE ENTRY) 
           D1_dec D1_inc D1_dec D2_inc
    OUTPUT
           angle
    
    SYNTAX
        angle.py [-h][-i] [command line options] [< filename]
    
    OPTIONS
        -h prints help and quits 
        -i for interactive data entry
        -f FILE input filename
        -F FILE output filename (required if -F set)
        Standard I/O 
    """
    out=""
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-F' in sys.argv:
        ind=sys.argv.index('-F')
        o=sys.argv[ind+1]
        out=open(o,'w')
    if '-i' in sys.argv:
        cont=1
        while cont==1:
            dir1,dir2=[],[]
            try:
                ans=raw_input('Declination 1: [ctrl-D  to quit] ')
                dir1.append(float(ans))
                ans=raw_input('Inclination 1: ')
                dir1.append(float(ans))
                ans=raw_input('Declination 2: ')
                dir2.append(float(ans))
                ans=raw_input('Inclination 2: ')
                dir2.append(float(ans))
            except:
                print "\nGood bye\n"
                sys.exit()
                 
            ang= pmag.angle(dir1,dir2)  # send dirs  to angle and spit out result
            print '%7.1f '%(ang)
    elif '-f' in sys.argv:
        ind=sys.argv.index('-f')
        file=sys.argv[ind+1]
        input=numpy.loadtxt(file)
    else:
        input = numpy.loadtxt(sys.stdin.readlines(),dtype=numpy.float)  # read from standard input
    if len(input.shape)>1: # list of directions
        dir1,dir2=input[:,0:2],input[:,2:]
    else: dir1,dir2=input[0:2],input[2:]
    angs=pmag.angle(dir1,dir2)
    for ang in angs:   # read in the data (as string variable), line by line
        print '%7.1f'%(ang)
        if out!="":out.write('%7.1f \n'%(ang))
Ejemplo n.º 13
0
def main():
    """
    NAME
        angle.py
    
    DESCRIPTION
      calculates angle between two input directions D1,D2
    
    INPUT (COMMAND LINE ENTRY) 
           D1_dec D1_inc D1_dec D2_inc
    OUTPUT
           angle
    
    SYNTAX
        angle.py [-h][-i] [command line options] [< filename]
    
    OPTIONS
        -h prints help and quits 
        -i for interactive data entry
        -f FILE input filename
        -F FILE output filename (required if -F set)
        Standard I/O 
    """
    out = ""
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-F' in sys.argv:
        ind = sys.argv.index('-F')
        o = sys.argv[ind + 1]
        out = open(o, 'w')
    if '-i' in sys.argv:
        cont = 1
        while cont == 1:
            dir1, dir2 = [], []
            try:
                ans = raw_input('Declination 1: [ctrl-D  to quit] ')
                dir1.append(float(ans))
                ans = raw_input('Inclination 1: ')
                dir1.append(float(ans))
                ans = raw_input('Declination 2: ')
                dir2.append(float(ans))
                ans = raw_input('Inclination 2: ')
                dir2.append(float(ans))
            except:
                print "\nGood bye\n"
                sys.exit()

            ang = pmag.angle(dir1,
                             dir2)  # send dirs  to angle and spit out result
            print '%7.1f ' % (ang)
    elif '-f' in sys.argv:
        ind = sys.argv.index('-f')
        file = sys.argv[ind + 1]
        input = numpy.loadtxt(file)
    else:
        input = numpy.loadtxt(sys.stdin.readlines(),
                              dtype=numpy.float)  # read from standard input
    if len(input.shape) > 1:  # list of directions
        dir1, dir2 = input[:, 0:2], input[:, 2:]
    else:
        dir1, dir2 = input[0:2], input[2:]
    angs = pmag.angle(dir1, dir2)
    for ang in angs:  # read in the data (as string variable), line by line
        print '%7.1f' % (ang)
        if out != "": out.write('%7.1f \n' % (ang))
Ejemplo n.º 14
0
def main():
    """
    NAME
       revtest_MM1990.py

    DESCRIPTION
       calculates Watson's V statistic from input files through Monte Carlo simulation in order to test whether normal and reversed populations could have been drawn from a common mean (equivalent to watsonV.py). Also provides the critical angle between the two sample mean directions and the corresponding McFadden and McElhinny (1990) classification.

    INPUT FORMAT
       takes dec/inc as first two columns in two space delimited files (one file for normal directions, one file for reversed directions).

    SYNTAX
       revtest_MM1990.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f FILE
        -f2 FILE
        -P  (don't plot the Watson V cdf)

    OUTPUT
        Watson's V between the two populations and the Monte Carlo Critical Value Vc.
        M&M1990 angle, critical angle and classification
        Plot of Watson's V CDF from Monte Carlo simulation (red line), V is solid and Vc is dashed.

    """
    D1, D2 = [], []
    plot = 1
    Flip = 1
    if '-h' in sys.argv:  # check if help is needed
        print(main.__doc__)
        sys.exit()  # graceful quit
    if '-P' in sys.argv: plot = 0
    if '-f' in sys.argv:
        ind = sys.argv.index('-f')
        file1 = sys.argv[ind + 1]
    f1 = open(file1, 'r')
    for line in f1.readlines():
        rec = line.split()
        Dec, Inc = float(rec[0]), float(rec[1])
        D1.append([Dec, Inc, 1.])
    f1.close()
    if '-f2' in sys.argv:
        ind = sys.argv.index('-f2')
        file2 = sys.argv[ind + 1]
        f2 = open(file2, 'r')
        print("be patient, your computer is doing 5000 simulations...")
        for line in f2.readlines():
            rec = line.split()
            Dec, Inc = float(rec[0]), float(rec[1])
            D2.append([Dec, Inc, 1.])
        f2.close()
    #take the antipode for the directions in file 2
    D2_flip = []
    for rec in D2:
        d, i = (rec[0] - 180.) % 360., -rec[1]
        D2_flip.append([d, i, 1.])

    pars_1 = pmag.fisher_mean(D1)
    pars_2 = pmag.fisher_mean(D2_flip)

    cart_1 = pmag.dir2cart([pars_1["dec"], pars_1["inc"], pars_1["r"]])
    cart_2 = pmag.dir2cart([pars_2['dec'], pars_2['inc'], pars_2["r"]])
    Sw = pars_1['k'] * pars_1['r'] + pars_2['k'] * pars_2['r']  # k1*r1+k2*r2
    xhat_1 = pars_1['k'] * cart_1[0] + pars_2['k'] * cart_2[0]  # k1*x1+k2*x2
    xhat_2 = pars_1['k'] * cart_1[1] + pars_2['k'] * cart_2[1]  # k1*y1+k2*y2
    xhat_3 = pars_1['k'] * cart_1[2] + pars_2['k'] * cart_2[2]  # k1*z1+k2*z2
    Rw = numpy.sqrt(xhat_1**2 + xhat_2**2 + xhat_3**2)
    V = 2 * (Sw - Rw)
    #
    #keep weighted sum for later when determining the "critical angle" let's save it as Sr (notation of McFadden and McElhinny, 1990)
    #
    Sr = Sw
    #
    # do monte carlo simulation of datasets with same kappas, but common mean
    #
    counter, NumSims = 0, 5000
    Vp = []  # set of Vs from simulations
    for k in range(NumSims):
        #
        # get a set of N1 fisher distributed vectors with k1, calculate fisher stats
        #
        Dirp = []
        for i in range(pars_1["n"]):
            Dirp.append(pmag.fshdev(pars_1["k"]))
        pars_p1 = pmag.fisher_mean(Dirp)
        #
        # get a set of N2 fisher distributed vectors with k2, calculate fisher stats
        #
        Dirp = []
        for i in range(pars_2["n"]):
            Dirp.append(pmag.fshdev(pars_2["k"]))
        pars_p2 = pmag.fisher_mean(Dirp)
        #
        # get the V for these
        #
        Vk = pmag.vfunc(pars_p1, pars_p2)
        Vp.append(Vk)


#
# sort the Vs, get Vcrit (95th percentile one)
#
    Vp.sort()
    k = int(.95 * NumSims)
    Vcrit = Vp[k]
    #
    # equation 18 of McFadden and McElhinny, 1990 calculates the critical value of R (Rwc)
    #
    Rwc = Sr - (old_div(Vcrit, 2))
    #
    #following equation 19 of McFadden and McElhinny (1990) the critical angle is calculated.
    #
    k1 = pars_1['k']
    k2 = pars_2['k']
    R1 = pars_1['r']
    R2 = pars_2['r']
    critical_angle = numpy.degrees(
        numpy.arccos(
            old_div(((Rwc**2) - ((k1 * R1)**2) - ((k2 * R2)**2)),
                    (2 * k1 * R1 * k2 * R2))))
    D1_mean = (pars_1['dec'], pars_1['inc'])
    D2_mean = (pars_2['dec'], pars_2['inc'])
    angle = pmag.angle(D1_mean, D2_mean)
    #
    # print the results of the test
    #
    print("")
    print("Results of Watson V test: ")
    print("")
    print("Watson's V:           " '%.1f' % (V))
    print("Critical value of V:  " '%.1f' % (Vcrit))

    if V < Vcrit:
        print(
            '"Pass": Since V is less than Vcrit, the null hypothesis that the two populations are drawn from distributions that share a common mean direction (antipodal to one another) cannot be rejected.'
        )
    elif V > Vcrit:
        print(
            '"Fail": Since V is greater than Vcrit, the two means can be distinguished at the 95% confidence level.'
        )
    print("")
    print("M&M1990 classification:")
    print("")
    print("Angle between data set means: " '%.1f' % (angle))
    print("Critical angle of M&M1990:   " '%.1f' % (critical_angle))

    if V > Vcrit:
        print("")
    elif V < Vcrit:
        if critical_angle < 5:
            print(
                "The McFadden and McElhinny (1990) classification for this test is: 'A'"
            )
        elif critical_angle < 10:
            print(
                "The McFadden and McElhinny (1990) classification for this test is: 'B'"
            )
        elif critical_angle < 20:
            print(
                "The McFadden and McElhinny (1990) classification for this test is: 'C'"
            )
        else:
            print(
                "The McFadden and McElhinny (1990) classification for this test is: 'INDETERMINATE;"
            )
    if plot == 1:
        CDF = {'cdf': 1}
        pmagplotlib.plot_init(CDF['cdf'], 5, 5)
        p1 = pmagplotlib.plot_cdf(CDF['cdf'], Vp, "Watson's V", 'r', "")
        p2 = pmagplotlib.plot_vs(CDF['cdf'], [V], 'g', '-')
        p3 = pmagplotlib.plot_vs(CDF['cdf'], [Vp[k]], 'b', '--')
        pmagplotlib.draw_figs(CDF)
        files, fmt = {}, 'svg'
        if file2 != "":
            files['cdf'] = 'WatsonsV_' + file1 + '_' + file2 + '.' + fmt
        else:
            files['cdf'] = 'WatsonsV_' + file1 + '.' + fmt
        if pmagplotlib.isServer:
            black = '#000000'
            purple = '#800080'
            titles = {}
            titles['cdf'] = 'Cumulative Distribution'
            CDF = pmagplotlib.add_borders(CDF, titles, black, purple)
            pmagplotlib.save_plots(CDF, files)
        else:
            ans = input(" S[a]ve to save plot, [q]uit without saving:  ")
            if ans == "a": pmagplotlib.save_plots(CDF, files)
Ejemplo n.º 15
0
def main():
    """
    NAME
        eqarea_magic.py

    DESCRIPTION
       makes equal area projections from declination/inclination data

    SYNTAX
        eqarea_magic.py [command line options]

    INPUT
       takes magic formatted pmag_results, pmag_sites, pmag_samples or pmag_specimens

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input magic format file from magic,default='pmag_results.txt'
         supported types=[magic_measurements,pmag_specimens, pmag_samples, pmag_sites, pmag_results, magic_web]
        -obj OBJ: specify  level of plot  [all, sit, sam, spc], default is all
        -crd [s,g,t]: specify coordinate system, [s]pecimen, [g]eographic, [t]ilt adjusted
                default is geographic, unspecified assumed geographic
        -fmt [svg,png,jpg] format for output plots
        -ell [F,K,B,Be,Bv] plot Fisher, Kent, Bingham, Bootstrap ellipses or Boostrap eigenvectors
        -c plot as colour contour
        -sav save plot and quit quietly
    NOTE
        all: entire file; sit: site; sam: sample; spc: specimen
    """
    FIG = {}  # plot dictionary
    FIG['eqarea'] = 1  # eqarea is figure 1
    in_file, plot_key, coord, crd = 'pmag_results.txt', 'all', "0", 'g'
    plotE, contour = 0, 0
    dir_path = '.'
    fmt = 'svg'
    verbose = pmagplotlib.verbose
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-WD' in sys.argv:
        ind = sys.argv.index('-WD')
        dir_path = sys.argv[ind+1]
    pmagplotlib.plot_init(FIG['eqarea'], 5, 5)
    if '-f' in sys.argv:
        ind = sys.argv.index("-f")
        in_file = dir_path+"/"+sys.argv[ind+1]
    if '-obj' in sys.argv:
        ind = sys.argv.index('-obj')
        plot_by = sys.argv[ind+1]
        if plot_by == 'all':
            plot_key = 'all'
        if plot_by == 'sit':
            plot_key = 'er_site_name'
        if plot_by == 'sam':
            plot_key = 'er_sample_name'
        if plot_by == 'spc':
            plot_key = 'er_specimen_name'
    if '-c' in sys.argv:
        contour = 1
    plt = 0
    if '-sav' in sys.argv:
        plt = 1
        verbose = 0
    if '-ell' in sys.argv:
        plotE = 1
        ind = sys.argv.index('-ell')
        ell_type = sys.argv[ind+1]
        if ell_type == 'F':
            dist = 'F'
        if ell_type == 'K':
            dist = 'K'
        if ell_type == 'B':
            dist = 'B'
        if ell_type == 'Be':
            dist = 'BE'
        if ell_type == 'Bv':
            dist = 'BV'
            FIG['bdirs'] = 2
            pmagplotlib.plot_init(FIG['bdirs'], 5, 5)
    if '-crd' in sys.argv:
        ind = sys.argv.index("-crd")
        crd = sys.argv[ind+1]
        if crd == 's':
            coord = "-1"
        if crd == 'g':
            coord = "0"
        if crd == 't':
            coord = "100"
    if '-fmt' in sys.argv:
        ind = sys.argv.index("-fmt")
        fmt = sys.argv[ind+1]
    Dec_keys = ['site_dec', 'sample_dec', 'specimen_dec',
                'measurement_dec', 'average_dec', 'none']
    Inc_keys = ['site_inc', 'sample_inc', 'specimen_inc',
                'measurement_inc', 'average_inc', 'none']
    Tilt_keys = ['tilt_correction', 'site_tilt_correction',
                 'sample_tilt_correction', 'specimen_tilt_correction', 'none']
    Dir_type_keys = ['', 'site_direction_type',
                     'sample_direction_type', 'specimen_direction_type']
    Name_keys = ['er_specimen_name', 'er_sample_name',
                 'er_site_name', 'pmag_result_name']
    data, file_type = pmag.magic_read(in_file)
    if file_type == 'pmag_results' and plot_key != "all":
        plot_key = plot_key+'s'  # need plural for results table
    if verbose:
        print(len(data), ' records read from ', in_file)
    #
    #
    # find desired dec,inc data:
    #
    dir_type_key = ''
    #
    # get plotlist if not plotting all records
    #
    plotlist = []
    if plot_key != "all":
        plots = pmag.get_dictitem(data, plot_key, '', 'F')
        for rec in plots:
            if rec[plot_key] not in plotlist:
                plotlist.append(rec[plot_key])
        plotlist.sort()
    else:
        plotlist.append('All')
    for plot in plotlist:
        # if verbose: print plot
        DIblock = []
        GCblock = []
        SLblock, SPblock = [], []
        title = plot
        mode = 1
        dec_key, inc_key, tilt_key, name_key, k = "", "", "", "", 0
        if plot != "All":
            odata = pmag.get_dictitem(data, plot_key, plot, 'T')
        else:
            odata = data  # data for this obj
        for dec_key in Dec_keys:
            # get all records with this dec_key not blank
            Decs = pmag.get_dictitem(odata, dec_key, '', 'F')
            if len(Decs) > 0:
                break
        for inc_key in Inc_keys:
            # get all records with this inc_key not blank
            Incs = pmag.get_dictitem(Decs, inc_key, '', 'F')
            if len(Incs) > 0:
                break
        for tilt_key in Tilt_keys:
            if tilt_key in Incs[0].keys():
                break  # find the tilt_key for these records
        if tilt_key == 'none':  # no tilt key in data, need to fix this with fake data which will be unknown tilt
            tilt_key = 'tilt_correction'
            for rec in Incs:
                rec[tilt_key] = ''
        # get all records matching specified coordinate system
        cdata = pmag.get_dictitem(Incs, tilt_key, coord, 'T')
        if coord == '0':  # geographic
            # get all the blank records - assume geographic
            udata = pmag.get_dictitem(Incs, tilt_key, '', 'T')
            if len(cdata) == 0:
                crd = ''
            if len(udata) > 0:
                for d in udata:
                    cdata.append(d)
                crd = crd+'u'
        for name_key in Name_keys:
            # get all records with this name_key not blank
            Names = pmag.get_dictitem(cdata, name_key, '', 'F')
            if len(Names) > 0:
                break
        for dir_type_key in Dir_type_keys:
            # get all records with this direction type
            Dirs = pmag.get_dictitem(cdata, dir_type_key, '', 'F')
            if len(Dirs) > 0:
                break
        if dir_type_key == "":
            dir_type_key = 'direction_type'
        locations, site, sample, specimen = "", "", "", ""
        for rec in cdata:  # pick out the data
            if 'er_location_name' in rec.keys() and rec['er_location_name'] != "" and rec['er_location_name'] not in locations:
                locations = locations + \
                    rec['er_location_name'].replace("/", "")+"_"
            if 'er_location_names' in rec.keys() and rec['er_location_names'] != "":
                locs = rec['er_location_names'].split(':')
                for loc in locs:
                    if loc not in locations:
                        locations = locations+loc.replace("/", "")+'_'
            if plot_key == 'er_site_name' or plot_key == 'er_sample_name' or plot_key == 'er_specimen_name':
                site = rec['er_site_name']
            if plot_key == 'er_sample_name' or plot_key == 'er_specimen_name':
                sample = rec['er_sample_name']
            if plot_key == 'er_specimen_name':
                specimen = rec['er_specimen_name']
            if plot_key == 'er_site_names' or plot_key == 'er_sample_names' or plot_key == 'er_specimen_names':
                site = rec['er_site_names']
            if plot_key == 'er_sample_names' or plot_key == 'er_specimen_names':
                sample = rec['er_sample_names']
            if plot_key == 'er_specimen_names':
                specimen = rec['er_specimen_names']
            if dir_type_key not in rec.keys() or rec[dir_type_key] == "":
                rec[dir_type_key] = 'l'
            if 'magic_method_codes' not in rec.keys():
                rec['magic_method_codes'] = ""
            DIblock.append([float(rec[dec_key]), float(rec[inc_key])])
            SLblock.append([rec[name_key], rec['magic_method_codes']])
            if rec[tilt_key] == coord and rec[dir_type_key] != 'l' and rec[dec_key] != "" and rec[inc_key] != "":
                GCblock.append([float(rec[dec_key]), float(rec[inc_key])])
                SPblock.append([rec[name_key], rec['magic_method_codes']])
        if len(DIblock) == 0 and len(GCblock) == 0:
            if verbose:
                print("no records for plotting")
            sys.exit()
        if verbose:
            for k in range(len(SLblock)):
                print('%s %s %7.1f %7.1f' % (
                    SLblock[k][0], SLblock[k][1], DIblock[k][0], DIblock[k][1]))
            for k in range(len(SPblock)):
                print('%s %s %7.1f %7.1f' % (
                    SPblock[k][0], SPblock[k][1], GCblock[k][0], GCblock[k][1]))
        if len(DIblock) > 0:
            if contour == 0:
                pmagplotlib.plot_eq(FIG['eqarea'], DIblock, title)
            else:
                pmagplotlib.plot_eq_cont(FIG['eqarea'], DIblock)
        else:
            pmagplotlib.plot_net(FIG['eqarea'])
        if len(GCblock) > 0:
            for rec in GCblock:
                pmagplotlib.plot_circ(FIG['eqarea'], rec, 90., 'g')
        if plotE == 1:
            ppars = pmag.doprinc(DIblock)  # get principal directions
            nDIs, rDIs, npars, rpars = [], [], [], []
            for rec in DIblock:
                angle = pmag.angle([rec[0], rec[1]], [
                                   ppars['dec'], ppars['inc']])
                if angle > 90.:
                    rDIs.append(rec)
                else:
                    nDIs.append(rec)
            if dist == 'B':  # do on whole dataset
                etitle = "Bingham confidence ellipse"
                bpars = pmag.dobingham(DIblock)
                for key in bpars.keys():
                    if key != 'n' and verbose:
                        print("    ", key, '%7.1f' % (bpars[key]))
                    if key == 'n' and verbose:
                        print("    ", key, '       %i' % (bpars[key]))
                npars.append(bpars['dec'])
                npars.append(bpars['inc'])
                npars.append(bpars['Zeta'])
                npars.append(bpars['Zdec'])
                npars.append(bpars['Zinc'])
                npars.append(bpars['Eta'])
                npars.append(bpars['Edec'])
                npars.append(bpars['Einc'])
            if dist == 'F':
                etitle = "Fisher confidence cone"
                if len(nDIs) > 2:
                    fpars = pmag.fisher_mean(nDIs)
                    for key in fpars.keys():
                        if key != 'n' and verbose:
                            print("    ", key, '%7.1f' % (fpars[key]))
                        if key == 'n' and verbose:
                            print("    ", key, '       %i' % (fpars[key]))
                    mode += 1
                    npars.append(fpars['dec'])
                    npars.append(fpars['inc'])
                    npars.append(fpars['alpha95'])  # Beta
                    npars.append(fpars['dec'])
                    isign = abs(fpars['inc'])/fpars['inc']
                    npars.append(fpars['inc']-isign*90.)  # Beta inc
                    npars.append(fpars['alpha95'])  # gamma
                    npars.append(fpars['dec']+90.)  # Beta dec
                    npars.append(0.)  # Beta inc
                if len(rDIs) > 2:
                    fpars = pmag.fisher_mean(rDIs)
                    if verbose:
                        print("mode ", mode)
                    for key in fpars.keys():
                        if key != 'n' and verbose:
                            print("    ", key, '%7.1f' % (fpars[key]))
                        if key == 'n' and verbose:
                            print("    ", key, '       %i' % (fpars[key]))
                    mode += 1
                    rpars.append(fpars['dec'])
                    rpars.append(fpars['inc'])
                    rpars.append(fpars['alpha95'])  # Beta
                    rpars.append(fpars['dec'])
                    isign = abs(fpars['inc'])/fpars['inc']
                    rpars.append(fpars['inc']-isign*90.)  # Beta inc
                    rpars.append(fpars['alpha95'])  # gamma
                    rpars.append(fpars['dec']+90.)  # Beta dec
                    rpars.append(0.)  # Beta inc
            if dist == 'K':
                etitle = "Kent confidence ellipse"
                if len(nDIs) > 3:
                    kpars = pmag.dokent(nDIs, len(nDIs))
                    if verbose:
                        print("mode ", mode)
                    for key in kpars.keys():
                        if key != 'n' and verbose:
                            print("    ", key, '%7.1f' % (kpars[key]))
                        if key == 'n' and verbose:
                            print("    ", key, '       %i' % (kpars[key]))
                    mode += 1
                    npars.append(kpars['dec'])
                    npars.append(kpars['inc'])
                    npars.append(kpars['Zeta'])
                    npars.append(kpars['Zdec'])
                    npars.append(kpars['Zinc'])
                    npars.append(kpars['Eta'])
                    npars.append(kpars['Edec'])
                    npars.append(kpars['Einc'])
                if len(rDIs) > 3:
                    kpars = pmag.dokent(rDIs, len(rDIs))
                    if verbose:
                        print("mode ", mode)
                    for key in kpars.keys():
                        if key != 'n' and verbose:
                            print("    ", key, '%7.1f' % (kpars[key]))
                        if key == 'n' and verbose:
                            print("    ", key, '       %i' % (kpars[key]))
                    mode += 1
                    rpars.append(kpars['dec'])
                    rpars.append(kpars['inc'])
                    rpars.append(kpars['Zeta'])
                    rpars.append(kpars['Zdec'])
                    rpars.append(kpars['Zinc'])
                    rpars.append(kpars['Eta'])
                    rpars.append(kpars['Edec'])
                    rpars.append(kpars['Einc'])
            else:  # assume bootstrap
                if dist == 'BE':
                    if len(nDIs) > 5:
                        BnDIs = pmag.di_boot(nDIs)
                        Bkpars = pmag.dokent(BnDIs, 1.)
                        if verbose:
                            print("mode ", mode)
                        for key in Bkpars.keys():
                            if key != 'n' and verbose:
                                print("    ", key, '%7.1f' % (Bkpars[key]))
                            if key == 'n' and verbose:
                                print("    ", key, '       %i' % (Bkpars[key]))
                        mode += 1
                        npars.append(Bkpars['dec'])
                        npars.append(Bkpars['inc'])
                        npars.append(Bkpars['Zeta'])
                        npars.append(Bkpars['Zdec'])
                        npars.append(Bkpars['Zinc'])
                        npars.append(Bkpars['Eta'])
                        npars.append(Bkpars['Edec'])
                        npars.append(Bkpars['Einc'])
                    if len(rDIs) > 5:
                        BrDIs = pmag.di_boot(rDIs)
                        Bkpars = pmag.dokent(BrDIs, 1.)
                        if verbose:
                            print("mode ", mode)
                        for key in Bkpars.keys():
                            if key != 'n' and verbose:
                                print("    ", key, '%7.1f' % (Bkpars[key]))
                            if key == 'n' and verbose:
                                print("    ", key, '       %i' % (Bkpars[key]))
                        mode += 1
                        rpars.append(Bkpars['dec'])
                        rpars.append(Bkpars['inc'])
                        rpars.append(Bkpars['Zeta'])
                        rpars.append(Bkpars['Zdec'])
                        rpars.append(Bkpars['Zinc'])
                        rpars.append(Bkpars['Eta'])
                        rpars.append(Bkpars['Edec'])
                        rpars.append(Bkpars['Einc'])
                    etitle = "Bootstrapped confidence ellipse"
                elif dist == 'BV':
                    sym = {'lower': ['o', 'c'], 'upper': [
                        'o', 'g'], 'size': 3, 'edgecolor': 'face'}
                    if len(nDIs) > 5:
                        BnDIs = pmag.di_boot(nDIs)
                        pmagplotlib.plot_eq_sym(
                            FIG['bdirs'], BnDIs, 'Bootstrapped Eigenvectors', sym)
                    if len(rDIs) > 5:
                        BrDIs = pmag.di_boot(rDIs)
                        if len(nDIs) > 5:  # plot on existing plots
                            pmagplotlib.plot_di_sym(FIG['bdirs'], BrDIs, sym)
                        else:
                            pmagplotlib.plot_eq(
                                FIG['bdirs'], BrDIs, 'Bootstrapped Eigenvectors')
            if dist == 'B':
                if len(nDIs) > 3 or len(rDIs) > 3:
                    pmagplotlib.plot_conf(FIG['eqarea'], etitle, [], npars, 0)
            elif len(nDIs) > 3 and dist != 'BV':
                pmagplotlib.plot_conf(FIG['eqarea'], etitle, [], npars, 0)
                if len(rDIs) > 3:
                    pmagplotlib.plot_conf(FIG['eqarea'], etitle, [], rpars, 0)
            elif len(rDIs) > 3 and dist != 'BV':
                pmagplotlib.plot_conf(FIG['eqarea'], etitle, [], rpars, 0)
        if verbose:
            pmagplotlib.draw_figs(FIG)
            #
        files = {}
        locations = locations[:-1]
        for key in FIG.keys():
            if pmagplotlib.isServer:  # use server plot naming convention
                filename = 'LO:_'+locations+'_SI:_'+site+'_SA:_'+sample + \
                    '_SP:_'+specimen+'_CO:_'+crd+'_TY:_'+key+'_.'+fmt
            else:  # use more readable plot naming convention
                filename = ''
                for item in [locations, site, sample, specimen, crd, key]:
                    if item:
                        item = item.replace(' ', '_')
                        filename += item + '_'
                if filename.endswith('_'):
                    filename = filename[:-1]
                filename += ".{}".format(fmt)
            files[key] = filename
        if pmagplotlib.isServer:
            black = '#000000'
            purple = '#800080'
            titles = {}
            titles['eq'] = 'Equal Area Plot'
            FIG = pmagplotlib.add_borders(FIG, titles, black, purple)
            pmagplotlib.save_plots(FIG, files)
        elif verbose:
            ans = raw_input(
                " S[a]ve to save plot, [q]uit, Return to continue:  ")
            if ans == "q":
                sys.exit()
            if ans == "a":
                pmagplotlib.save_plots(FIG, files)
        if plt:
            pmagplotlib.save_plots(FIG, files)
Ejemplo n.º 16
0
def main():
    """
    NAME
       fishqq.py

    DESCRIPTION
       makes qq plot from dec,inc input data

    INPUT FORMAT
       takes dec/inc pairs in space delimited file

    SYNTAX
       fishqq.py [command line options]

    OPTIONS
        -h help message
        -f FILE, specify file on command line
        -F FILE, specify output file for statistics
        -sav save and quit [saves as input file name plus fmt extension]
        -fmt specify format for output [png, eps, svg, pdf] 

    OUTPUT:
        Dec Inc N Mu Mu_crit Me Me_crit Y/N
     where direction is the principal component and Y/N is Fisherian or not
     separate lines for each mode with N >=10 (N and R)
    """
    fmt,plot='svg',0
    outfile=""
    if '-h' in sys.argv: # check if help is needed
        print(main.__doc__)
        sys.exit() # graceful quit
    elif '-f' in sys.argv: # ask for filename
        ind=sys.argv.index('-f')
        file=sys.argv[ind+1]
        f=open(file,'r')
        data=f.readlines()
    if '-F' in sys.argv:
        ind=sys.argv.index('-F')
        outfile=open(sys.argv[ind+1],'w') # open output file
    if '-sav' in sys.argv: plot=1
    if '-fmt' in sys.argv:
        ind=sys.argv.index('-fmt')
        fmt=sys.argv[ind+1]
    DIs,nDIs,rDIs= [],[],[] # set up list for data
    for line in data:   # read in the data from standard input
        if '\t' in line:
            rec=line.split('\t') # split each line on space to get records
        else:
            rec=line.split() # split each line on space to get records
        DIs.append([float(rec[0]),float(rec[1])]) # append data to Inc
# split into two modes
    ppars=pmag.doprinc(DIs) # get principal directions
    for rec in DIs:
        angle=pmag.angle([rec[0],rec[1]],[ppars['dec'],ppars['inc']])
        if angle>90.:
            rDIs.append(rec)
        else:
            nDIs.append(rec)
    
#
    if len(rDIs) >=10 or len(nDIs) >=10:
        D1,I1=[],[]
        QQ={'unf1':1,'exp1':2}
        pmagplotlib.plot_init(QQ['unf1'],5,5)
        pmagplotlib.plot_init(QQ['exp1'],5,5)
        if len(nDIs) < 10: 
            ppars=pmag.doprinc(rDIs) # get principal directions
            Drbar,Irbar=ppars['dec']-180.,-ppars['inc']
            Nr=len(rDIs)
            for di in rDIs:
                d,irot=pmag.dotilt(di[0],di[1],Drbar-180.,90.-Irbar) # rotate to mean
                drot=d-180.
                if drot<0:drot=drot+360.
                D1.append(drot)           
                I1.append(irot) 
                Dtit='Mode 2 Declinations'
                Itit='Mode 2 Inclinations'
        else:          
            ppars=pmag.doprinc(nDIs) # get principal directions
            Dnbar,Inbar=ppars['dec'],ppars['inc']
            Nn=len(nDIs)
            for di in nDIs:
                d,irot=pmag.dotilt(di[0],di[1],Dnbar-180.,90.-Inbar) # rotate to mean
                drot=d-180.
                if drot<0:drot=drot+360.
                D1.append(drot)
                I1.append(irot)
                Dtit='Mode 1 Declinations'
                Itit='Mode 1 Inclinations'
        Mu_n,Mu_ncr=pmagplotlib.plot_qq_unf(QQ['unf1'],D1,Dtit) # make plot
        Me_n,Me_ncr=pmagplotlib.plot_qq_exp(QQ['exp1'],I1,Itit) # make plot
        #print Mu_n,Mu_ncr,Me_n, Me_ncr
        if outfile!="":
#        Dec Inc N Mu Mu_crit Me Me_crit Y/N
            if Mu_n<=Mu_ncr and Me_n<=Me_ncr:
               F='Y'
            else:
               F='N'
            outstring='%7.1f %7.1f %i %5.3f %5.3f %5.3f %5.3f %s \n'%(Dnbar,Inbar,Nn,Mu_n,Mu_ncr,Me_n,Me_ncr,F)
            outfile.write(outstring)
    else:
        print('you need N> 10 for at least one mode')
        sys.exit()
    if len(rDIs)>10 and len(nDIs)>10:
        D2,I2=[],[]
        QQ['unf2']=3
        QQ['exp2']=4
        pmagplotlib.plot_init(QQ['unf2'],5,5)
        pmagplotlib.plot_init(QQ['exp2'],5,5)
        ppars=pmag.doprinc(rDIs) # get principal directions
        Drbar,Irbar=ppars['dec']-180.,-ppars['inc']
        Nr=len(rDIs)
        for di in rDIs:
            d,irot=pmag.dotilt(di[0],di[1],Drbar-180.,90.-Irbar) # rotate to mean
            drot=d-180.
            if drot<0:drot=drot+360.
            D2.append(drot)           
            I2.append(irot) 
            Dtit='Mode 2 Declinations'
            Itit='Mode 2 Inclinations'
        Mu_r,Mu_rcr=pmagplotlib.plot_qq_unf(QQ['unf2'],D2,Dtit) # make plot
        Me_r,Me_rcr=pmagplotlib.plot_qq_exp(QQ['exp2'],I2,Itit) # make plot
        if outfile!="":
#        Dec Inc N Mu Mu_crit Me Me_crit Y/N
            if Mu_r<=Mu_rcr and Me_r<=Me_rcr:
               F='Y'
            else:
               F='N'
            outstring='%7.1f %7.1f %i %5.3f %5.3f %5.3f %5.3f %s \n'%(Drbar,Irbar,Nr,Mu_r,Mu_rcr,Me_r,Me_rcr,F)
            outfile.write(outstring)
    files={}
    for key in list(QQ.keys()):
        files[key]=file+'_'+key+'.'+fmt 
    if pmagplotlib.isServer:
        black     = '#000000'
        purple    = '#800080'
        titles={}
        titles['eq']='Equal Area Plot'
        EQ = pmagplotlib.add_borders(EQ,titles,black,purple)
        pmagplotlib.save_plots(QQ,files)
    elif plot==1:
        pmagplotlib.save_plots(QQ,files)
    else:
        pmagplotlib.draw_figs(QQ) 
        ans=input(" S[a]ve to save plot, [q]uit without saving:  ")
        if ans=="a": pmagplotlib.save_plots(QQ,files)
Ejemplo n.º 17
0
def main():
    """
    NAME
    specimens_results_magic.py

    DESCRIPTION
    combines pmag_specimens.txt file with age, location, acceptance criteria and
    outputs pmag_results table along with other MagIC tables necessary for uploading to the database

    SYNTAX
    specimens_results_magic.py [command line options]

    OPTIONS
    -h prints help message and quits
    -usr USER:   identify user, default is ""
    -f: specimen input magic_measurements format file, default is "magic_measurements.txt"
    -fsp: specimen input pmag_specimens format file, default is "pmag_specimens.txt"
    -fsm: sample input er_samples format file, default is "er_samples.txt"
    -fsi: specimen input er_sites format file, default is "er_sites.txt"
    -fla: specify a file with paleolatitudes for calculating VADMs, default is not to calculate VADMS
               format is:  site_name paleolatitude (space delimited file)
    -fa AGES: specify er_ages format file with age information
    -crd [s,g,t,b]:   specify coordinate system
        (s, specimen, g geographic, t, tilt corrected, b, geographic and tilt corrected)
        Default is to assume geographic
        NB: only the tilt corrected data will appear on the results table, if both g and t are selected.
        -cor [AC:CR:NL]: colon delimited list of required data adjustments for all specimens
            included in intensity calculations (anisotropy, cooling rate, non-linear TRM)
            unless specified, corrections will not be applied
        -pri [TRM:ARM] colon delimited list of priorities for anisotropy correction (-cor must also be set to include AC). default is TRM, then ARM
    -age MIN MAX UNITS:   specify age boundaries and units
    -exc:  use exiting selection criteria (in pmag_criteria.txt file), default is default criteria
    -C: no acceptance criteria
    -aD:  average directions per sample, default is NOT
    -aI:  average multiple specimen intensities per sample, default is by site
    -aC:  average all components together, default is NOT
    -pol:  calculate polarity averages
    -sam:  save sample level vgps and v[a]dms, default is by site
    -xSi:  skip the site level intensity calculation
    -p: plot directions and look at intensities by site, default is NOT
        -fmt: specify output for saved images, default is svg (only if -p set)
    -lat: use present latitude for calculating VADMs, default is not to calculate VADMs
    -xD: skip directions
    -xI: skip intensities
    OUPUT
    writes pmag_samples, pmag_sites, pmag_results tables
    """
# set defaults
    Comps=[] # list of components
    version_num=pmag.get_version()
    args=sys.argv
    DefaultAge=["none"]
    skipdirs,coord,excrit,custom,vgps,average,Iaverage,plotsites,opt=1,0,0,0,0,0,0,0,0
    get_model_lat=0 # this skips VADM calculation altogether, when get_model_lat=1, uses present day
    fmt='svg'
    dir_path="."
    model_lat_file=""
    Caverage=0
    infile='pmag_specimens.txt'
    measfile="magic_measurements.txt"
    sampfile="er_samples.txt"
    sitefile="er_sites.txt"
    agefile="er_ages.txt"
    specout="er_specimens.txt"
    sampout="pmag_samples.txt"
    siteout="pmag_sites.txt"
    resout="pmag_results.txt"
    critout="pmag_criteria.txt"
    instout="magic_instruments.txt"
    sigcutoff,OBJ="",""
    noDir,noInt=0,0
    polarity=0
    coords=['0']
    Dcrit,Icrit,nocrit=0,0,0
    corrections=[]
    nocorrection=['DA-NL','DA-AC','DA-CR']
    priorities=['DA-AC-ARM','DA-AC-TRM'] # priorities for anisotropy correction
# get command line stuff
    if "-h" in args:
        print(main.__doc__)
        sys.exit()
    if '-WD' in args:
        ind=args.index("-WD")
        dir_path=args[ind+1]
    if '-cor' in args:
        ind=args.index('-cor')
        cors=args[ind+1].split(':') # list of required data adjustments
        for cor in cors:
            nocorrection.remove('DA-'+cor)
            corrections.append('DA-'+cor)
    if '-pri' in args:
        ind=args.index('-pri')
        priorities=args[ind+1].split(':') # list of required data adjustments
        for p in priorities:
            p='DA-AC-'+p
    if '-f' in args:
        ind=args.index("-f")
        measfile=args[ind+1]
    if '-fsp' in args:
        ind=args.index("-fsp")
        infile=args[ind+1]
    if '-fsi' in args:
        ind=args.index("-fsi")
        sitefile=args[ind+1]
    if "-crd" in args:
        ind=args.index("-crd")
        coord=args[ind+1]
        if coord=='s':coords=['-1']
        if coord=='g':coords=['0']
        if coord=='t':coords=['100']
        if coord=='b':coords=['0','100']
    if "-usr" in args:
        ind=args.index("-usr")
        user=sys.argv[ind+1]
    else: user=""
    if "-C" in args: Dcrit,Icrit,nocrit=1,1,1 # no selection criteria
    if "-sam" in args: vgps=1 # save sample level VGPS/VADMs
    if "-xSi" in args:
        nositeints=1 # skip site level intensity
    else:
        nositeints=0
    if "-age" in args:
        ind=args.index("-age")
        DefaultAge[0]=args[ind+1]
        DefaultAge.append(args[ind+2])
        DefaultAge.append(args[ind+3])
    Daverage,Iaverage,Caverage=0,0,0
    if "-aD" in args: Daverage=1 # average by sample directions
    if "-aI" in args: Iaverage=1 # average by sample intensities
    if "-aC" in args: Caverage=1 # average all components together ???  why???
    if "-pol" in args: polarity=1 # calculate averages by polarity
    if '-xD' in args:noDir=1
    if '-xI' in args:
        noInt=1
    elif "-fla" in args:
        if '-lat' in args:
            print("you should set a paleolatitude file OR use present day lat - not both")
            sys.exit()
        ind=args.index("-fla")
        model_lat_file=dir_path+'/'+args[ind+1]
        get_model_lat=2
        mlat=open(model_lat_file,'r')
        ModelLats=[]
        for line in mlat.readlines():
            ModelLat={}
            tmp=line.split()
            ModelLat["er_site_name"]=tmp[0]
            ModelLat["site_model_lat"]=tmp[1]
            ModelLat["er_sample_name"]=tmp[0]
            ModelLat["sample_lat"]=tmp[1]
            ModelLats.append(ModelLat)
        get_model_lat=2
    elif '-lat' in args:
        get_model_lat=1
    if "-p" in args:
        plotsites=1
        if "-fmt" in args:
            ind=args.index("-fmt")
            fmt=args[ind+1]
        if noDir==0: # plot by site - set up plot window
            import pmagplotlib
            EQ={}
            EQ['eqarea']=1
            pmagplotlib.plot_init(EQ['eqarea'],5,5) # define figure 1 as equal area projection
            pmagplotlib.plotNET(EQ['eqarea']) # I don't know why this has to be here, but otherwise the first plot never plots...
            pmagplotlib.drawFIGS(EQ)
    if '-WD' in args:
        infile=dir_path+'/'+infile
        measfile=dir_path+'/'+measfile
        instout=dir_path+'/'+instout
        sampfile=dir_path+'/'+sampfile
        sitefile=dir_path+'/'+sitefile
        agefile=dir_path+'/'+agefile
        specout=dir_path+'/'+specout
        sampout=dir_path+'/'+sampout
        siteout=dir_path+'/'+siteout
        resout=dir_path+'/'+resout
        critout=dir_path+'/'+critout
    if "-exc" in args: # use existing pmag_criteria file
        if "-C" in args:
            print('you can not use both existing and no criteria - choose either -exc OR -C OR neither (for default)')
            sys.exit()
        crit_data,file_type=pmag.magic_read(critout)
        print("Acceptance criteria read in from ", critout)
    else  : # use default criteria (if nocrit set, then get really loose criteria as default)
        crit_data=pmag.default_criteria(nocrit)
        if nocrit==0:
            print("Acceptance criteria are defaults")
        else:
            print("No acceptance criteria used ")
    accept={}
    for critrec in crit_data:
        for key in list(critrec.keys()):
# need to migrate specimen_dang to specimen_int_dang for intensity data using old format
            if 'IE-SPEC' in list(critrec.keys()) and 'specimen_dang' in list(critrec.keys()) and 'specimen_int_dang' not in list(critrec.keys()):
                critrec['specimen_int_dang']=critrec['specimen_dang']
                del critrec['specimen_dang']
# need to get rid of ron shaars sample_int_sigma_uT
            if 'sample_int_sigma_uT' in list(critrec.keys()):
                critrec['sample_int_sigma']='%10.3e'%(eval(critrec['sample_int_sigma_uT'])*1e-6)
            if key not in list(accept.keys()) and critrec[key]!='':
                accept[key]=critrec[key]
    #
    #
    if "-exc" not in args and "-C" not in args:
        print("args",args)
        pmag.magic_write(critout,[accept],'pmag_criteria')
        print("\n Pmag Criteria stored in ",critout,'\n')
#
# now we're done slow dancing
#
    SiteNFO,file_type=pmag.magic_read(sitefile) # read in site data - has the lats and lons
    SampNFO,file_type=pmag.magic_read(sampfile) # read in site data - has the lats and lons
    height_nfo=pmag.get_dictitem(SiteNFO,'site_height','','F') # find all the sites with height info.
    if agefile !="":AgeNFO,file_type=pmag.magic_read(agefile) # read in the age information
    Data,file_type=pmag.magic_read(infile) # read in specimen interpretations
    IntData=pmag.get_dictitem(Data,'specimen_int','','F') # retrieve specimens with intensity data
    comment,orient="",[]
    samples,sites=[],[]
    for rec in Data: # run through the data filling in missing keys and finding all components, coordinates available
# fill in missing fields, collect unique sample and site names
        if 'er_sample_name' not in list(rec.keys()):
            rec['er_sample_name']=""
        elif rec['er_sample_name'] not in samples:
            samples.append(rec['er_sample_name'])
        if 'er_site_name' not in list(rec.keys()):
            rec['er_site_name']=""
        elif rec['er_site_name'] not in sites:
            sites.append(rec['er_site_name'])
        if 'specimen_int' not in list(rec.keys()):rec['specimen_int']=''
        if 'specimen_comp_name' not in list(rec.keys()) or rec['specimen_comp_name']=="":rec['specimen_comp_name']='A'
        if rec['specimen_comp_name'] not in Comps:Comps.append(rec['specimen_comp_name'])
        rec['specimen_tilt_correction']=rec['specimen_tilt_correction'].strip('\n')
        if "specimen_tilt_correction" not in list(rec.keys()): rec["specimen_tilt_correction"]="-1" # assume sample coordinates
        if rec["specimen_tilt_correction"] not in orient: orient.append(rec["specimen_tilt_correction"])  # collect available coordinate systems
        if "specimen_direction_type" not in list(rec.keys()): rec["specimen_direction_type"]='l'  # assume direction is line - not plane
        if "specimen_dec" not in list(rec.keys()): rec["specimen_direction_type"]=''  # if no declination, set direction type to blank
        if "specimen_n" not in list(rec.keys()): rec["specimen_n"]=''  # put in n
        if "specimen_alpha95" not in list(rec.keys()): rec["specimen_alpha95"]=''  # put in alpha95
        if "magic_method_codes" not in list(rec.keys()): rec["magic_method_codes"]=''
     #
     # start parsing data into SpecDirs, SpecPlanes, SpecInts
    SpecInts,SpecDirs,SpecPlanes=[],[],[]
    samples.sort() # get sorted list of samples and sites
    sites.sort()
    if noInt==0: # don't skip intensities
        IntData=pmag.get_dictitem(Data,'specimen_int','','F') # retrieve specimens with intensity data
        if nocrit==0: # use selection criteria
            for rec in IntData: # do selection criteria
                kill=pmag.grade(rec,accept,'specimen_int')
                if len(kill)==0: SpecInts.append(rec) # intensity record to be included in sample, site calculations
        else:
            SpecInts=IntData[:] # take everything - no selection criteria
    # check for required data adjustments
        if len(corrections)>0 and len(SpecInts)>0:
            for cor in corrections:
                SpecInts=pmag.get_dictitem(SpecInts,'magic_method_codes',cor,'has') # only take specimens with the required corrections
        if len(nocorrection)>0 and len(SpecInts)>0:
            for cor in nocorrection:
                SpecInts=pmag.get_dictitem(SpecInts,'magic_method_codes',cor,'not') # exclude the corrections not specified for inclusion
# take top priority specimen of its name in remaining specimens (only one per customer)
        PrioritySpecInts=[]
        specimens=pmag.get_specs(SpecInts) # get list of uniq specimen names
        for spec in specimens:
            ThisSpecRecs=pmag.get_dictitem(SpecInts,'er_specimen_name',spec,'T') # all the records for this specimen
            if len(ThisSpecRecs)==1:
                PrioritySpecInts.append(ThisSpecRecs[0])
            elif len(ThisSpecRecs)>1: # more than one
                prec=[]
                for p in priorities:
                    ThisSpecRecs=pmag.get_dictitem(SpecInts,'magic_method_codes',p,'has') # all the records for this specimen
                    if len(ThisSpecRecs)>0:prec.append(ThisSpecRecs[0])
                PrioritySpecInts.append(prec[0]) # take the best one
        SpecInts=PrioritySpecInts # this has the first specimen record
    if noDir==0: # don't skip directions
        AllDirs=pmag.get_dictitem(Data,'specimen_direction_type','','F') # retrieve specimens with directed lines and planes
        Ns=pmag.get_dictitem(AllDirs,'specimen_n','','F')  # get all specimens with specimen_n information
        if nocrit!=1: # use selection criteria
            for rec in Ns: # look through everything with specimen_n for "good" data
                kill=pmag.grade(rec,accept,'specimen_dir')
                if len(kill)==0: # nothing killed it
                    SpecDirs.append(rec)
        else: # no criteria
            SpecDirs=AllDirs[:] # take them all
# SpecDirs is now the list of all specimen directions (lines and planes) that pass muster
#
    PmagSamps,SampDirs=[],[] # list of all sample data and list of those that pass the DE-SAMP criteria
    PmagSites,PmagResults=[],[] # list of all site data and selected results
    SampInts=[]
    for samp in samples: # run through the sample names
        if Daverage==1: #  average by sample if desired
           SampDir=pmag.get_dictitem(SpecDirs,'er_sample_name',samp,'T') # get all the directional data for this sample
           if len(SampDir)>0: # there are some directions
               for coord in coords: # step through desired coordinate systems
                   CoordDir=pmag.get_dictitem(SampDir,'specimen_tilt_correction',coord,'T') # get all the directions for this sample
                   if len(CoordDir)>0: # there are some with this coordinate system
                       if Caverage==0: # look component by component
                           for comp in Comps:
                               CompDir=pmag.get_dictitem(CoordDir,'specimen_comp_name',comp,'T') # get all directions from this component
                               if len(CompDir)>0: # there are some
                                   PmagSampRec=pmag.lnpbykey(CompDir,'sample','specimen') # get a sample average from all specimens
                                   PmagSampRec["er_location_name"]=CompDir[0]['er_location_name'] # decorate the sample record
                                   PmagSampRec["er_site_name"]=CompDir[0]['er_site_name']
                                   PmagSampRec["er_sample_name"]=samp
                                   PmagSampRec["er_citation_names"]="This study"
                                   PmagSampRec["er_analyst_mail_names"]=user
                                   PmagSampRec['magic_software_packages']=version_num
                                   if nocrit!=1:PmagSampRec['pmag_criteria_codes']="ACCEPT"
                                   if agefile != "": PmagSampRec= pmag.get_age(PmagSampRec,"er_site_name","sample_inferred_",AgeNFO,DefaultAge)
                                   site_height=pmag.get_dictitem(height_nfo,'er_site_name',PmagSampRec['er_site_name'],'T')
                                   if len(site_height)>0:PmagSampRec["sample_height"]=site_height[0]['site_height'] # add in height if available
                                   PmagSampRec['sample_comp_name']=comp
                                   PmagSampRec['sample_tilt_correction']=coord
                                   PmagSampRec['er_specimen_names']= pmag.get_list(CompDir,'er_specimen_name') # get a list of the specimen names used
                                   PmagSampRec['magic_method_codes']= pmag.get_list(CompDir,'magic_method_codes') # get a list of the methods used
                                   if nocrit!=1: # apply selection criteria
                                       kill=pmag.grade(PmagSampRec,accept,'sample_dir')
                                   else:
                                       kill=[]
                                   if len(kill)==0:
                                       SampDirs.append(PmagSampRec)
                                       if vgps==1: # if sample level VGP info desired, do that now
                                           PmagResRec=pmag.getsampVGP(PmagSampRec,SiteNFO)
                                           if PmagResRec!="":PmagResults.append(PmagResRec)
                                       PmagSamps.append(PmagSampRec)
                       if Caverage==1: # average all components together  basically same as above
                           PmagSampRec=pmag.lnpbykey(CoordDir,'sample','specimen')
                           PmagSampRec["er_location_name"]=CoordDir[0]['er_location_name']
                           PmagSampRec["er_site_name"]=CoordDir[0]['er_site_name']
                           PmagSampRec["er_sample_name"]=samp
                           PmagSampRec["er_citation_names"]="This study"
                           PmagSampRec["er_analyst_mail_names"]=user
                           PmagSampRec['magic_software_packages']=version_num
                           if nocrit!=1:PmagSampRec['pmag_criteria_codes']=""
                           if agefile != "": PmagSampRec= pmag.get_age(PmagSampRec,"er_site_name","sample_inferred_",AgeNFO,DefaultAge)
                           site_height=pmag.get_dictitem(height_nfo,'er_site_name',site,'T')
                           if len(site_height)>0:PmagSampRec["sample_height"]=site_height[0]['site_height'] # add in height if available
                           PmagSampRec['sample_tilt_correction']=coord
                           PmagSampRec['sample_comp_name']= pmag.get_list(CoordDir,'specimen_comp_name') # get components used
                           PmagSampRec['er_specimen_names']= pmag.get_list(CoordDir,'er_specimen_name') # get specimne names averaged
                           PmagSampRec['magic_method_codes']= pmag.get_list(CoordDir,'magic_method_codes') # assemble method codes
                           if nocrit!=1: # apply selection criteria
                               kill=pmag.grade(PmagSampRec,accept,'sample_dir')
                               if len(kill)==0: # passes the mustard
                                   SampDirs.append(PmagSampRec)
                                   if vgps==1:
                                       PmagResRec=pmag.getsampVGP(PmagSampRec,SiteNFO)
                                       if PmagResRec!="":PmagResults.append(PmagResRec)
                           else: # take everything
                               SampDirs.append(PmagSampRec)
                               if vgps==1:
                                   PmagResRec=pmag.getsampVGP(PmagSampRec,SiteNFO)
                                   if PmagResRec!="":PmagResults.append(PmagResRec)
                           PmagSamps.append(PmagSampRec)
        if Iaverage==1: #  average by sample if desired
           SampI=pmag.get_dictitem(SpecInts,'er_sample_name',samp,'T') # get all the intensity data for this sample
           if len(SampI)>0: # there are some
               PmagSampRec=pmag.average_int(SampI,'specimen','sample') # get average intensity stuff
               PmagSampRec["sample_description"]="sample intensity" # decorate sample record
               PmagSampRec["sample_direction_type"]=""
               PmagSampRec['er_site_name']=SampI[0]["er_site_name"]
               PmagSampRec['er_sample_name']=samp
               PmagSampRec['er_location_name']=SampI[0]["er_location_name"]
               PmagSampRec["er_citation_names"]="This study"
               PmagSampRec["er_analyst_mail_names"]=user
               if agefile != "":   PmagSampRec=pmag.get_age(PmagSampRec,"er_site_name","sample_inferred_", AgeNFO,DefaultAge)
               site_height=pmag.get_dictitem(height_nfo,'er_site_name',PmagSampRec['er_site_name'],'T')
               if len(site_height)>0:PmagSampRec["sample_height"]=site_height[0]['site_height'] # add in height if available
               PmagSampRec['er_specimen_names']= pmag.get_list(SampI,'er_specimen_name')
               PmagSampRec['magic_method_codes']= pmag.get_list(SampI,'magic_method_codes')
               if nocrit!=1:  # apply criteria!
                   kill=pmag.grade(PmagSampRec,accept,'sample_int')
                   if len(kill)==0:
                       PmagSampRec['pmag_criteria_codes']="ACCEPT"
                       SampInts.append(PmagSampRec)
                       PmagSamps.append(PmagSampRec)
                   else:PmagSampRec={} # sample rejected
               else: # no criteria
                   SampInts.append(PmagSampRec)
                   PmagSamps.append(PmagSampRec)
                   PmagSampRec['pmag_criteria_codes']=""
               if vgps==1 and get_model_lat!=0 and PmagSampRec!={}: #
                   if get_model_lat==1: # use sample latitude
                       PmagResRec=pmag.getsampVDM(PmagSampRec,SampNFO)
                       del(PmagResRec['model_lat']) # get rid of the model lat key
                   elif get_model_lat==2: # use model latitude
                       PmagResRec=pmag.getsampVDM(PmagSampRec,ModelLats)
                       if PmagResRec!={}:PmagResRec['magic_method_codes']=PmagResRec['magic_method_codes']+":IE-MLAT"
                   if PmagResRec!={}:
                          PmagResRec['er_specimen_names']=PmagSampRec['er_specimen_names']
                          PmagResRec['er_sample_names']=PmagSampRec['er_sample_name']
                          PmagResRec['pmag_criteria_codes']='ACCEPT'
                          PmagResRec['average_int_sigma_perc']=PmagSampRec['sample_int_sigma_perc']
                          PmagResRec['average_int_sigma']=PmagSampRec['sample_int_sigma']
                          PmagResRec['average_int_n']=PmagSampRec['sample_int_n']
                          PmagResRec['vadm_n']=PmagSampRec['sample_int_n']
                          PmagResRec['data_type']='i'
                          PmagResults.append(PmagResRec)
    if len(PmagSamps)>0:
        TmpSamps,keylist=pmag.fillkeys(PmagSamps) # fill in missing keys from different types of records
        pmag.magic_write(sampout,TmpSamps,'pmag_samples') # save in sample output file
        print(' sample averages written to ',sampout)

#
#create site averages from specimens or samples as specified
#
    for site in sites:
        if Daverage==0: key,dirlist='specimen',SpecDirs # if specimen averages at site level desired
        if Daverage==1: key,dirlist='sample',SampDirs # if sample averages at site level desired
        tmp=pmag.get_dictitem(dirlist,'er_site_name',site,'T') # get all the sites with  directions
        tmp1=pmag.get_dictitem(tmp,key+'_tilt_correction',coords[-1],'T') # use only the last coordinate if Caverage==0
        sd=pmag.get_dictitem(SiteNFO,'er_site_name',site,'T') # fish out site information (lat/lon, etc.)
        if len(sd)>0:
            sitedat=sd[0]
            if Caverage==0: # do component wise averaging
                for comp in Comps:
                    siteD=pmag.get_dictitem(tmp1,key+'_comp_name',comp,'T') # get all components comp
                    if len(siteD)>0: # there are some for this site and component name
                        PmagSiteRec=pmag.lnpbykey(siteD,'site',key) # get an average for this site
                        PmagSiteRec['site_comp_name']=comp # decorate the site record
                        PmagSiteRec["er_location_name"]=siteD[0]['er_location_name']
                        PmagSiteRec["er_site_name"]=siteD[0]['er_site_name']
                        PmagSiteRec['site_tilt_correction']=coords[-1]
                        PmagSiteRec['site_comp_name']= pmag.get_list(siteD,key+'_comp_name')
                        if Daverage==1:
                            PmagSiteRec['er_sample_names']= pmag.get_list(siteD,'er_sample_name')
                        else:
                            PmagSiteRec['er_specimen_names']= pmag.get_list(siteD,'er_specimen_name')
        # determine the demagnetization code (DC3,4 or 5) for this site
                        AFnum=len(pmag.get_dictitem(siteD,'magic_method_codes','LP-DIR-AF','has'))
                        Tnum=len(pmag.get_dictitem(siteD,'magic_method_codes','LP-DIR-T','has'))
                        DC=3
                        if AFnum>0:DC+=1
                        if Tnum>0:DC+=1
                        PmagSiteRec['magic_method_codes']= pmag.get_list(siteD,'magic_method_codes')+':'+ 'LP-DC'+str(DC)
                        PmagSiteRec['magic_method_codes'].strip(":")
                        if plotsites==1:
                            print(PmagSiteRec['er_site_name'])
                            pmagplotlib.plotSITE(EQ['eqarea'],PmagSiteRec,siteD,key) # plot and list the data
                            pmagplotlib.drawFIGS(EQ)
                        PmagSites.append(PmagSiteRec)
            else: # last component only
                siteD=tmp1[:] # get the last orientation system specified
                if len(siteD)>0: # there are some
                    PmagSiteRec=pmag.lnpbykey(siteD,'site',key) # get the average for this site
                    PmagSiteRec["er_location_name"]=siteD[0]['er_location_name'] # decorate the record
                    PmagSiteRec["er_site_name"]=siteD[0]['er_site_name']
                    PmagSiteRec['site_comp_name']=comp
                    PmagSiteRec['site_tilt_correction']=coords[-1]
                    PmagSiteRec['site_comp_name']= pmag.get_list(siteD,key+'_comp_name')
                    PmagSiteRec['er_specimen_names']= pmag.get_list(siteD,'er_specimen_name')
                    PmagSiteRec['er_sample_names']= pmag.get_list(siteD,'er_sample_name')
                    AFnum=len(pmag.get_dictitem(siteD,'magic_method_codes','LP-DIR-AF','has'))
                    Tnum=len(pmag.get_dictitem(siteD,'magic_method_codes','LP-DIR-T','has'))
                    DC=3
                    if AFnum>0:DC+=1
                    if Tnum>0:DC+=1
                    PmagSiteRec['magic_method_codes']= pmag.get_list(siteD,'magic_method_codes')+':'+ 'LP-DC'+str(DC)
                    PmagSiteRec['magic_method_codes'].strip(":")
                    if Daverage==0:PmagSiteRec['site_comp_name']= pmag.get_list(siteD,key+'_comp_name')
                    if plotsites==1:
                        pmagplotlib.plotSITE(EQ['eqarea'],PmagSiteRec,siteD,key)
                        pmagplotlib.drawFIGS(EQ)
                    PmagSites.append(PmagSiteRec)
        else:
            print('site information not found in er_sites for site, ',site,' site will be skipped')
    for PmagSiteRec in PmagSites: # now decorate each dictionary some more, and calculate VGPs etc. for results table
        PmagSiteRec["er_citation_names"]="This study"
        PmagSiteRec["er_analyst_mail_names"]=user
        PmagSiteRec['magic_software_packages']=version_num
        if agefile != "": PmagSiteRec= pmag.get_age(PmagSiteRec,"er_site_name","site_inferred_",AgeNFO,DefaultAge)
        PmagSiteRec['pmag_criteria_codes']='ACCEPT'
        if 'site_n_lines' in list(PmagSiteRec.keys()) and 'site_n_planes' in list(PmagSiteRec.keys()) and PmagSiteRec['site_n_lines']!="" and PmagSiteRec['site_n_planes']!="":
            if int(PmagSiteRec["site_n_planes"])>0:
                PmagSiteRec["magic_method_codes"]=PmagSiteRec['magic_method_codes']+":DE-FM-LP"
            elif int(PmagSiteRec["site_n_lines"])>2:
                PmagSiteRec["magic_method_codes"]=PmagSiteRec['magic_method_codes']+":DE-FM"
            kill=pmag.grade(PmagSiteRec,accept,'site_dir')
            if len(kill)==0:
                PmagResRec={} # set up dictionary for the pmag_results table entry
                PmagResRec['data_type']='i' # decorate it a bit
                PmagResRec['magic_software_packages']=version_num
                PmagSiteRec['site_description']='Site direction included in results table'
                PmagResRec['pmag_criteria_codes']='ACCEPT'
                dec=float(PmagSiteRec["site_dec"])
                inc=float(PmagSiteRec["site_inc"])
                if 'site_alpha95' in list(PmagSiteRec.keys()) and PmagSiteRec['site_alpha95']!="":
                    a95=float(PmagSiteRec["site_alpha95"])
                else:a95=180.
                sitedat=pmag.get_dictitem(SiteNFO,'er_site_name',PmagSiteRec['er_site_name'],'T')[0] # fish out site information (lat/lon, etc.)
                lat=float(sitedat['site_lat'])
                lon=float(sitedat['site_lon'])
                plong,plat,dp,dm=pmag.dia_vgp(dec,inc,a95,lat,lon) # get the VGP for this site
                if PmagSiteRec['site_tilt_correction']=='-1':C=' (spec coord) '
                if PmagSiteRec['site_tilt_correction']=='0':C=' (geog. coord) '
                if PmagSiteRec['site_tilt_correction']=='100':C=' (strat. coord) '
                PmagResRec["pmag_result_name"]="VGP Site: "+PmagSiteRec["er_site_name"] # decorate some more
                PmagResRec["result_description"]="Site VGP, coord system = "+str(coord)+' component: '+comp
                PmagResRec['er_site_names']=PmagSiteRec['er_site_name']
                PmagResRec['pmag_criteria_codes']='ACCEPT'
                PmagResRec['er_citation_names']='This study'
                PmagResRec['er_analyst_mail_names']=user
                PmagResRec["er_location_names"]=PmagSiteRec["er_location_name"]
                if Daverage==1:
                    PmagResRec["er_sample_names"]=PmagSiteRec["er_sample_names"]
                else:
                    PmagResRec["er_specimen_names"]=PmagSiteRec["er_specimen_names"]
                PmagResRec["tilt_correction"]=PmagSiteRec['site_tilt_correction']
                PmagResRec["pole_comp_name"]=PmagSiteRec['site_comp_name']
                PmagResRec["average_dec"]=PmagSiteRec["site_dec"]
                PmagResRec["average_inc"]=PmagSiteRec["site_inc"]
                PmagResRec["average_alpha95"]=PmagSiteRec["site_alpha95"]
                PmagResRec["average_n"]=PmagSiteRec["site_n"]
                PmagResRec["average_n_lines"]=PmagSiteRec["site_n_lines"]
                PmagResRec["average_n_planes"]=PmagSiteRec["site_n_planes"]
                PmagResRec["vgp_n"]=PmagSiteRec["site_n"]
                PmagResRec["average_k"]=PmagSiteRec["site_k"]
                PmagResRec["average_r"]=PmagSiteRec["site_r"]
                PmagResRec["average_lat"]='%10.4f ' %(lat)
                PmagResRec["average_lon"]='%10.4f ' %(lon)
                if agefile != "": PmagResRec= pmag.get_age(PmagResRec,"er_site_names","average_",AgeNFO,DefaultAge)
                site_height=pmag.get_dictitem(height_nfo,'er_site_name',site,'T')
                if len(site_height)>0:PmagResRec["average_height"]=site_height[0]['site_height']
                PmagResRec["vgp_lat"]='%7.1f ' % (plat)
                PmagResRec["vgp_lon"]='%7.1f ' % (plong)
                PmagResRec["vgp_dp"]='%7.1f ' % (dp)
                PmagResRec["vgp_dm"]='%7.1f ' % (dm)
                PmagResRec["magic_method_codes"]= PmagSiteRec["magic_method_codes"]
                if PmagSiteRec['site_tilt_correction']=='0':PmagSiteRec['magic_method_codes']=PmagSiteRec['magic_method_codes']+":DA-DIR-GEO"
                if PmagSiteRec['site_tilt_correction']=='100':PmagSiteRec['magic_method_codes']=PmagSiteRec['magic_method_codes']+":DA-DIR-TILT"
                PmagSiteRec['site_polarity']=""
                if polarity==1: # assign polarity based on angle of pole lat to spin axis - may want to re-think this sometime
                    angle=pmag.angle([0,0],[0,(90-plat)])
                    if angle <= 55.: PmagSiteRec["site_polarity"]='n'
                    if angle > 55. and angle < 125.: PmagSiteRec["site_polarity"]='t'
                    if angle >= 125.: PmagSiteRec["site_polarity"]='r'
                PmagResults.append(PmagResRec)
    if polarity==1:
        crecs=pmag.get_dictitem(PmagSites,'site_tilt_correction','100','T') # find the tilt corrected data
        if len(crecs)<2:crecs=pmag.get_dictitem(PmagSites,'site_tilt_correction','0','T') # if there aren't any, find the geographic corrected data
        if len(crecs)>2: # if there are some,
            comp=pmag.get_list(crecs,'site_comp_name').split(':')[0] # find the first component
            crecs=pmag.get_dictitem(crecs,'site_comp_name',comp,'T') # fish out all of the first component
            precs=[]
            for rec in crecs:
                precs.append({'dec':rec['site_dec'],'inc':rec['site_inc'],'name':rec['er_site_name'],'loc':rec['er_location_name']})
            polpars=pmag.fisher_by_pol(precs) # calculate average by polarity
            for mode in list(polpars.keys()): # hunt through all the modes (normal=A, reverse=B, all=ALL)
                PolRes={}
                PolRes['er_citation_names']='This study'
                PolRes["pmag_result_name"]="Polarity Average: Polarity "+mode #
                PolRes["data_type"]="a"
                PolRes["average_dec"]='%7.1f'%(polpars[mode]['dec'])
                PolRes["average_inc"]='%7.1f'%(polpars[mode]['inc'])
                PolRes["average_n"]='%i'%(polpars[mode]['n'])
                PolRes["average_r"]='%5.4f'%(polpars[mode]['r'])
                PolRes["average_k"]='%6.0f'%(polpars[mode]['k'])
                PolRes["average_alpha95"]='%7.1f'%(polpars[mode]['alpha95'])
                PolRes['er_site_names']= polpars[mode]['sites']
                PolRes['er_location_names']= polpars[mode]['locs']
                PolRes['magic_software_packages']=version_num
                PmagResults.append(PolRes)

    if noInt!=1 and nositeints!=1:
      for site in sites: # now do intensities for each site
        if plotsites==1:print(site)
        if Iaverage==0: key,intlist='specimen',SpecInts # if using specimen level data
        if Iaverage==1: key,intlist='sample',PmagSamps # if using sample level data
        Ints=pmag.get_dictitem(intlist,'er_site_name',site,'T') # get all the intensities  for this site
        if len(Ints)>0: # there are some
            PmagSiteRec=pmag.average_int(Ints,key,'site') # get average intensity stuff for site table
            PmagResRec=pmag.average_int(Ints,key,'average') # get average intensity stuff for results table
            if plotsites==1: # if site by site examination requested - print this site out to the screen
                for rec in Ints:print(rec['er_'+key+'_name'],' %7.1f'%(1e6*float(rec[key+'_int'])))
                if len(Ints)>1:
                    print('Average: ','%7.1f'%(1e6*float(PmagResRec['average_int'])),'N: ',len(Ints))
                    print('Sigma: ','%7.1f'%(1e6*float(PmagResRec['average_int_sigma'])),'Sigma %: ',PmagResRec['average_int_sigma_perc'])
                input('Press any key to continue\n')
            er_location_name=Ints[0]["er_location_name"]
            PmagSiteRec["er_location_name"]=er_location_name # decorate the records
            PmagSiteRec["er_citation_names"]="This study"
            PmagResRec["er_location_names"]=er_location_name
            PmagResRec["er_citation_names"]="This study"
            PmagSiteRec["er_analyst_mail_names"]=user
            PmagResRec["er_analyst_mail_names"]=user
            PmagResRec["data_type"]='i'
            if Iaverage==0:
                PmagSiteRec['er_specimen_names']= pmag.get_list(Ints,'er_specimen_name') # list of all specimens used
                PmagResRec['er_specimen_names']= pmag.get_list(Ints,'er_specimen_name')
            PmagSiteRec['er_sample_names']= pmag.get_list(Ints,'er_sample_name') # list of all samples used
            PmagResRec['er_sample_names']= pmag.get_list(Ints,'er_sample_name')
            PmagSiteRec['er_site_name']= site
            PmagResRec['er_site_names']= site
            PmagSiteRec['magic_method_codes']= pmag.get_list(Ints,'magic_method_codes')
            PmagResRec['magic_method_codes']= pmag.get_list(Ints,'magic_method_codes')
            kill=pmag.grade(PmagSiteRec,accept,'site_int')
            if nocrit==1 or len(kill)==0:
                b,sig=float(PmagResRec['average_int']),""
                if(PmagResRec['average_int_sigma'])!="":sig=float(PmagResRec['average_int_sigma'])
                sdir=pmag.get_dictitem(PmagResults,'er_site_names',site,'T') # fish out site direction
                if len(sdir)>0 and  sdir[-1]['average_inc']!="": # get the VDM for this record using last average inclination (hope it is the right one!)
                        inc=float(sdir[0]['average_inc']) #
                        mlat=pmag.magnetic_lat(inc) # get magnetic latitude using dipole formula
                        PmagResRec["vdm"]='%8.3e '% (pmag.b_vdm(b,mlat)) # get VDM with magnetic latitude
                        PmagResRec["vdm_n"]=PmagResRec['average_int_n']
                        if 'average_int_sigma' in list(PmagResRec.keys()) and PmagResRec['average_int_sigma']!="":
                            vdm_sig=pmag.b_vdm(float(PmagResRec['average_int_sigma']),mlat)
                            PmagResRec["vdm_sigma"]='%8.3e '% (vdm_sig)
                        else:
                            PmagResRec["vdm_sigma"]=""
                mlat="" # define a model latitude
                if get_model_lat==1: # use present site latitude
                    mlats=pmag.get_dictitem(SiteNFO,'er_site_name',site,'T')
                    if len(mlats)>0: mlat=mlats[0]['site_lat']
                elif get_model_lat==2: # use a model latitude from some plate reconstruction model (or something)
                    mlats=pmag.get_dictitem(ModelLats,'er_site_name',site,'T')
                    if len(mlats)>0: PmagResRec['model_lat']=mlats[0]['site_model_lat']
                    mlat=PmagResRec['model_lat']
                if mlat!="":
                    PmagResRec["vadm"]='%8.3e '% (pmag.b_vdm(b,float(mlat))) # get the VADM using the desired latitude
                    if sig!="":
                        vdm_sig=pmag.b_vdm(float(PmagResRec['average_int_sigma']),float(mlat))
                        PmagResRec["vadm_sigma"]='%8.3e '% (vdm_sig)
                        PmagResRec["vadm_n"]=PmagResRec['average_int_n']
                    else:
                        PmagResRec["vadm_sigma"]=""
                sitedat=pmag.get_dictitem(SiteNFO,'er_site_name',PmagSiteRec['er_site_name'],'T') # fish out site information (lat/lon, etc.)
                if len(sitedat)>0:
                    sitedat=sitedat[0]
                    PmagResRec['average_lat']=sitedat['site_lat']
                    PmagResRec['average_lon']=sitedat['site_lon']
                else:
                    PmagResRec['average_lon']='UNKNOWN'
                    PmagResRec['average_lon']='UNKNOWN'
                PmagResRec['magic_software_packages']=version_num
                PmagResRec["pmag_result_name"]="V[A]DM: Site "+site
                PmagResRec["result_description"]="V[A]DM of site"
                PmagResRec["pmag_criteria_codes"]="ACCEPT"
                if agefile != "": PmagResRec= pmag.get_age(PmagResRec,"er_site_names","average_",AgeNFO,DefaultAge)
                site_height=pmag.get_dictitem(height_nfo,'er_site_name',site,'T')
                if len(site_height)>0:PmagResRec["average_height"]=site_height[0]['site_height']
                PmagSites.append(PmagSiteRec)
                PmagResults.append(PmagResRec)
    if len(PmagSites)>0:
        Tmp,keylist=pmag.fillkeys(PmagSites)
        pmag.magic_write(siteout,Tmp,'pmag_sites')
        print(' sites written to ',siteout)
    else: print("No Site level table")
    if len(PmagResults)>0:
        TmpRes,keylist=pmag.fillkeys(PmagResults)
        pmag.magic_write(resout,TmpRes,'pmag_results')
        print(' results written to ',resout)
    else: print("No Results level table")