Example #1
0
def plot_AMS(sdata,pointsize=50,errors='None',bedding=[],incolor='N',):
    """
    adapted from pmagplotlib.plotANIS
    set errors to 'h' for hext ellipses, 'b' for bootstrap, 'p' for parametric bootstrap
    bootstrap trials currently hard-coded as 1000
    for bedding, give as [strike,dip] ;
    """
    subplot_net() #set up stereonet
    if incolor=='N': colours=['0.4','0.6','0.5'] #specify greyscale colours
    else: colours=['lightcoral','lightskyblue','lightgreen']
    #if bedding plot bedding plane
    if bedding !=[]:
        # hard-coded version of pmag.plotC
        D_c,I_c=pmag.circ(bedding[0]-90.,90.-bedding[1],90.)
        X_c_d,Y_c_d=[],[]
        for k in range(len(D_c)):
            XY=pmag.dimap(D_c[k],I_c[k])
            if I_c[k]>0:
                X_c_d.append(XY[0])
                Y_c_d.append(XY[1])
        plt.plot(X_c_d,Y_c_d,color='grey',dashes=[10,2],linewidth=3,zorder=1)
    Vs=[]
    #plot individual sample data
    for s in sdata:
        tau,V=pmag.doseigs(s)
        Vs.append(V)
    plotEVEC(Vs,pointsize,colours)
    #plot mean eigenvectors
    nf,sigma,avs=pmag.sbar(sdata)
    Vs=[]
    mtau,mV=pmag.doseigs(avs)
    Vs.append(mV)
    plotEVEC(Vs,pointsize*4,['w','w','w'], 'black',7)
    #plot confidence limits
    hpars=pmag.dohext(nf,sigma,avs)
    if errors=='h':
        ellpars=[hpars["v1_dec"],hpars["v1_inc"],hpars["e12"],hpars["v2_dec"],hpars["v2_inc"],hpars["e13"],hpars["v3_dec"],hpars["v3_inc"]]
        plotELL(ellpars,'black',1,1)
        ellpars=[hpars["v2_dec"],hpars["v2_inc"],hpars["e23"],hpars["v3_dec"],hpars["v3_inc"],hpars["e12"],hpars["v1_dec"],hpars["v1_inc"]]
        plotELL(ellpars,'black',1,1)
        ellpars=[hpars["v3_dec"],hpars["v3_inc"],hpars["e13"],hpars["v1_dec"],hpars["v1_inc"],hpars["e23"],hpars["v2_dec"],hpars["v2_inc"]]
        plotELL(ellpars,'black',1,1)
    elif errors=='b' or errors=='p':
        if errors=='p': ipar=1
        else: ipar=0
        Tmean,Vmean,Taus,BVs=pmag.s_boot(sdata,ipar,1000) # get eigenvectors of mean tensor
        # the problem with the bootstrap is that for fabrics where one or more eigenvectors is close to the horizontal,
        # you end up with dipolar data        
                        
        bpars=pmag.sbootpars(Taus,BVs,Vmean)
        bpars['t1']=hpars['t1']
        bpars['t2']=hpars['t2']
        bpars['t3']=hpars['t3']
        #have to pair zeta value with eta dec/inc, and vice-versa, to align error ellipses correctly.
        ellpars=[bpars["v1_dec"],bpars["v1_inc"],bpars["v1_eta"],bpars["v1_eta_dec"],bpars["v1_eta_inc"],bpars["v1_zeta"],bpars["v1_zeta_dec"],bpars["v1_zeta_inc"]]
        plotELL(ellpars,'black',1,1)
        ellpars=[bpars["v2_dec"],bpars["v2_inc"],bpars["v2_eta"],bpars["v2_eta_dec"],bpars["v2_eta_inc"],bpars["v2_zeta"],bpars["v2_zeta_dec"],bpars["v2_zeta_inc"]]
        plotELL(ellpars,'black',1,1)
        ellpars=[bpars["v3_dec"],bpars["v3_inc"],bpars["v3_eta"],bpars["v3_eta_dec"],bpars["v3_eta_inc"],bpars["v3_zeta"],bpars["v3_zeta_dec"],bpars["v3_zeta_inc"]]
        plotELL(ellpars,'black',1,1) 
Example #2
0
def plot_AMS(
    sdata,
    pointsize=50,
    errors='None',
    bedding=[],
    incolor='N',
):
    """
    adapted from pmagplotlib.plotANIS
    set errors to 'h' for hext ellipses, 'b' for bootstrap, 'p' for parametric bootstrap
    bootstrap trials currently hard-coded as 1000
    for bedding, give as [strike,dip] ;
    """
    subplot_net()  #set up stereonet
    if incolor == 'N':
        colours = ['0.4', '0.6', '0.5']  #specify greyscale colours
    else:
        colours = ['lightcoral', 'lightskyblue', 'lightgreen']
    #if bedding plot bedding plane
    if bedding != []:
        # hard-coded version of pmag.plotC
        D_c, I_c = pmag.circ(bedding[0] - 90., 90. - bedding[1], 90.)
        X_c_d, Y_c_d = [], []
        for k in range(len(D_c)):
            XY = pmag.dimap(D_c[k], I_c[k])
            if I_c[k] > 0:
                X_c_d.append(XY[0])
                Y_c_d.append(XY[1])
        plt.plot(X_c_d,
                 Y_c_d,
                 color='grey',
                 dashes=[10, 2],
                 linewidth=3,
                 zorder=1)
    Vs = []
    #plot individual sample data
    for s in sdata:
        tau, V = pmag.doseigs(s)
        Vs.append(V)
    plotEVEC(Vs, pointsize, colours)
    #plot mean eigenvectors
    nf, sigma, avs = pmag.sbar(sdata)
    Vs = []
    mtau, mV = pmag.doseigs(avs)
    Vs.append(mV)
    plotEVEC(Vs, pointsize * 4, ['w', 'w', 'w'], 'black', 7)
    #plot confidence limits
    hpars = pmag.dohext(nf, sigma, avs)
    if errors == 'h':
        ellpars = [
            hpars["v1_dec"], hpars["v1_inc"], hpars["e12"], hpars["v2_dec"],
            hpars["v2_inc"], hpars["e13"], hpars["v3_dec"], hpars["v3_inc"]
        ]
        plotELL(ellpars, 'black', 1, 1)
        ellpars = [
            hpars["v2_dec"], hpars["v2_inc"], hpars["e23"], hpars["v3_dec"],
            hpars["v3_inc"], hpars["e12"], hpars["v1_dec"], hpars["v1_inc"]
        ]
        plotELL(ellpars, 'black', 1, 1)
        ellpars = [
            hpars["v3_dec"], hpars["v3_inc"], hpars["e13"], hpars["v1_dec"],
            hpars["v1_inc"], hpars["e23"], hpars["v2_dec"], hpars["v2_inc"]
        ]
        plotELL(ellpars, 'black', 1, 1)
    elif errors == 'b' or errors == 'p':
        if errors == 'p': ipar = 1
        else: ipar = 0
        Tmean, Vmean, Taus, BVs = pmag.s_boot(
            sdata, ipar, 1000)  # get eigenvectors of mean tensor
        # the problem with the bootstrap is that for fabrics where one or more eigenvectors is close to the horizontal,
        # you end up with dipolar data

        bpars = pmag.sbootpars(Taus, BVs, Vmean)
        bpars['t1'] = hpars['t1']
        bpars['t2'] = hpars['t2']
        bpars['t3'] = hpars['t3']
        #have to pair zeta value with eta dec/inc, and vice-versa, to align error ellipses correctly.
        ellpars = [
            bpars["v1_dec"], bpars["v1_inc"], bpars["v1_eta"],
            bpars["v1_eta_dec"], bpars["v1_eta_inc"], bpars["v1_zeta"],
            bpars["v1_zeta_dec"], bpars["v1_zeta_inc"]
        ]
        plotELL(ellpars, 'black', 1, 1)
        ellpars = [
            bpars["v2_dec"], bpars["v2_inc"], bpars["v2_eta"],
            bpars["v2_eta_dec"], bpars["v2_eta_inc"], bpars["v2_zeta"],
            bpars["v2_zeta_dec"], bpars["v2_zeta_inc"]
        ]
        plotELL(ellpars, 'black', 1, 1)
        ellpars = [
            bpars["v3_dec"], bpars["v3_inc"], bpars["v3_eta"],
            bpars["v3_eta_dec"], bpars["v3_eta_inc"], bpars["v3_zeta"],
            bpars["v3_zeta_dec"], bpars["v3_zeta_inc"]
        ]
        plotELL(ellpars, 'black', 1, 1)
Example #3
0
def main():
    """
    NAME
        k15_magic.py

    DESCRIPTION
        converts .k15 format data to magic_measurements  format.
        assums Jelinek Kappabridge measurement scheme
   
    SYNTAX 
        k15_magic.py [-h] [command line options]
    
    OPTIONS
        -h prints help message and quits
        -f KFILE: specify .k15 format input file
        -F MFILE: specify magic_measurements format output file
        -Fsa SFILE, specify er_samples format file for output 
        -Fa AFILE, specify rmag_anisotropy format file for output
        -loc LOC: specify location name for study
        -ins INST: specify instrument that measurements were made on
        -spc NUM: specify number of digets for specimen ID, default is 0
        -ncn NCOM: specify naming convention (default is #1)
       Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXXYYY:  YYY is sample designation with Z characters from site XXX
            [5] sample = site
            [6] sample, site, location info in er_samples.txt
            [7] all others you will have to either customize your

    DEFAULTS
        MFILE: k15_measurements.txt
        SFILE: er_samples.txt
        AFILE: rmag_anisotropy.txt
        LOC: unknown
        INST: unknown
        
    INPUT
      name [az,pl,strike,dip], followed by
      3 rows of 5 measurements for each specimen

    """
#
# initialize some variables
#
    version_num=pmag.get_version()
    specnum=0
    sampfile, measfile="er_samples.txt","k15_measurements.txt"
    anisfile='rmag_anisotropy.txt'
    resfile='rmag_results.txt'
    syn=0
    er_location_name="unknown"
    inst="unknown"
    itilt,igeo,linecnt,key=0,0,0,"" 
    first_save=1
    k15,specnum=[],0 
    citation='This study'
    dir_path='.'
    if '-WD' in sys.argv:
        ind=sys.argv.index('-WD')
        dir_path=sys.argv[ind+1]
# pick off stuff from command line
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        k15file=sys.argv[ind+1] 
    if '-F' in sys.argv:
        ind=sys.argv.index('-F')
        measfile=sys.argv[ind+1] 
    if '-Fsa' in sys.argv:
        ind=sys.argv.index('-Fsa')
        sampfile=sys.argv[ind+1] 
    if '-Fa' in sys.argv:
        ind=sys.argv.index('-Fa')
        anisfile=sys.argv[ind+1] 
    if '-loc' in sys.argv:
        ind=sys.argv.index('-loc')
        er_location_name=sys.argv[ind+1] 
    if '-spc' in sys.argv:
        ind=sys.argv.index('-spc')
        specnum=-int(sys.argv[ind+1])
    samp_con,Z="1",""
    if "-ncn" in sys.argv:
        ind=sys.argv.index("-ncn")
        samp_con=sys.argv[ind+1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print "option [4] must be in form 4-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="4"
        if samp_con=='6':
            Samps,filetype=pmag.magic_read(dirpath+'/er_samples.txt')
    sampfile, measfile=dir_path+'/'+sampfile,dir_path+'/'+measfile
    anisfile=dir_path+'/'+anisfile
    resfile=dir_path+'/'+resfile
    k15file=dir_path+'/'+k15file
    try:
        SampRecs,filetype=pmag.magic_read(sampfile) # append new records to existing
        samplist=[]
        for samp in SampRecs:
            if samp['er_sample_name'] not in samplist:samplist.append(samp['er_sample_name'])
    except IOError:
        SampRecs=[]
    # measurement directions for Jelinek 1977 protocol:
    Decs=[315,225,180,135,45,90,270,270,270,90,180,180,0,0,0]
    Incs=[0,0,0,0,0,-45,-45,0,45,45,45,-45,-90,-45,45]
    # some defaults to read in  .k15 file format
    # list of measurements and default number of characters for specimen ID
# some magic default definitions
    #
    # read in data
    input=open(k15file,'rU')
    MeasRecs,SpecRecs,AnisRecs,ResRecs=[],[],[],[]
    # read in data
    MeasRec,SpecRec,SampRec,SiteRec,AnisRec,ResRec={},{},{},{},{},{}
    for line in input.readlines():
            linecnt+=1
            rec=line.split()
            if linecnt==1:
                MeasRec["magic_method_codes"]=""
                SpecRec["magic_method_codes"]=""
                SampRec["magic_method_codes"]=""
                AnisRec["magic_method_codes"]=""
                SiteRec["magic_method_codes"]=""
                ResRec["magic_smethod_codes"]=""
                MeasRec["magic_software_packages"]=version_num
                SpecRec["magic_software_packages"]=version_num
                SampRec["magic_software_packages"]=version_num
                AnisRec["magic_software_packages"]=version_num
                SiteRec["magic_software_packages"]=version_num
                ResRec["magic_software_packages"]=version_num
                MeasRec["magic_method_codes"]="LP-X"
                MeasRec["measurement_flag"]="g"
                MeasRec["measurement_standard"]="u"
                MeasRec["er_citation_names"]="This study"
                SpecRec["er_citation_names"]="This study"
                SampRec["er_citation_names"]="This study"
                AnisRec["er_citation_names"]="This study"
                ResRec["er_citation_names"]="This study"
                MeasRec["er_specimen_name"]=rec[0]
                MeasRec["magic_experiment_name"]=rec[0]+":LP-AN-MS"
                AnisRec["magic_experiment_names"]=rec[0]+":AMS"
                ResRec["magic_experiment_names"]=rec[0]+":AMS"
                SpecRec["er_specimen_name"]=rec[0]
                AnisRec["er_specimen_name"]=rec[0]
                SampRec["er_specimen_name"]=rec[0]
                ResRec["rmag_result_name"]=rec[0]
                if specnum!=0: MeasRec["er_sample_name"]=rec[0][:specnum]
                if specnum==0: MeasRec["er_sample_name"]=rec[0]
                SampRec["er_sample_name"]=MeasRec["er_sample_name"]
                SpecRec["er_sample_name"]=MeasRec["er_sample_name"]
                AnisRec["er_sample_name"]=MeasRec["er_sample_name"]
                ResRec["er_sample_names"]=MeasRec["er_sample_name"]
                if samp_con=="6":
                    for samp in Samps:
                        if samp['er_sample_name']==AnisRec["er_sample_name"]:
                            sitename=samp['er_site_name']
                            er_location_name=samp['er_location_name']
                elif samp_con!="":
                    sitename=pmag.parse_site(AnisRec['er_sample_name'],samp_con,Z)
                MeasRec["er_site_name"]=sitename
                MeasRec["er_location_name"]=er_location_name
                SampRec["er_site_name"]=MeasRec["er_site_name"]
                SpecRec["er_site_name"]=MeasRec["er_site_name"]
                AnisRec["er_site_name"]=MeasRec["er_site_name"]
                ResRec["er_site_names"]=MeasRec["er_site_name"]
                SampRec["er_location_name"]=MeasRec["er_location_name"]
                SpecRec["er_location_name"]=MeasRec["er_location_name"]
                AnisRec["er_location_name"]=MeasRec["er_location_name"]
                ResRec["er_location_names"]=MeasRec["er_location_name"]
                if len(rec)>=3: 
                    SampRec["sample_azimuth"],SampRec["sample_dip"]=rec[1],rec[2]
                    az,pl,igeo=float(rec[1]),float(rec[2]),1
                if len(rec)==5: 
                    SampRec["sample_bed_dip_direction"],SampRec["sample_bed_dip"]= '(%7.1f)'%(90.+float(rec[3])),(rec[4])
                    bed_az,bed_dip,itilt,igeo=90.+float(rec[3]),float(rec[4]),1,1
            else: 
                for i in range(5):
                    k15.append(1e-6*float(rec[i])) # assume measurements in micro SI
                if linecnt==4:
                    sbar,sigma,bulk=pmag.dok15_s(k15) 
                    hpars=pmag.dohext(9,sigma,sbar) 
                    MeasRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
                    MeasRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
                    for i in range(15):
                        NewMeas=copy.deepcopy(MeasRec)
                        NewMeas["measurement_orient_phi"]='%7.1f' %(Decs[i])
                        NewMeas["measurement_orient_theta"]='%7.1f'% (Incs[i])
                        NewMeas["measurement_chi_volume"]='%12.10f'% (k15[i])
                        NewMeas["measurement_number"]='%i'% (i+1)
                        NewMeas["magic_experiment_name"]=rec[0]+":LP-AN-MS"
                        MeasRecs.append(NewMeas)
                    if SampRec['er_sample_name'] not in samplist:
                        SampRecs.append(SampRec)
                        samplist.append(SampRec['er_sample_name'])
                    SpecRecs.append(SpecRec)
                    AnisRec["anisotropy_type"]="AMS"
                    ResRec["anisotropy_type"]="AMS"
                    AnisRec["anisotropy_s1"]='%12.10f'%(sbar[0])
                    AnisRec["anisotropy_s2"]='%12.10f'%(sbar[1])
                    AnisRec["anisotropy_s3"]='%12.10f'%(sbar[2])
                    AnisRec["anisotropy_s4"]='%12.10f'%(sbar[3])
                    AnisRec["anisotropy_s5"]='%12.10f'%(sbar[4])
                    AnisRec["anisotropy_s6"]='%12.10f'%(sbar[5])
                    AnisRec["anisotropy_mean"]='%12.10f'%(bulk)
                    AnisRec["anisotropy_sigma"]='%12.10f'%(sigma)
                    AnisRec["anisotropy_unit"]='SI'
                    AnisRec["anisotropy_n"]='15'
                    AnisRec["anisotropy_tilt_correction"]='-1'
                    AnisRec["magic_method_codes"]='LP-X:AE-H:LP-AN-MS'
                    AnisRecs.append(AnisRec)
                    ResRec["magic_method_codes"]='LP-X:AE-H:LP-AN-MS'
                    ResRec["anisotropy_tilt_correction"]='-1'
                    ResRec["anisotropy_t1"]='%12.10f'%(hpars['t1'])
                    ResRec["anisotropy_t2"]='%12.10f'%(hpars['t2'])
                    ResRec["anisotropy_t3"]='%12.10f'%(hpars['t3'])
                    ResRec["anisotropy_fest"]='%12.10f'%(hpars['F'])
                    ResRec["anisotropy_ftest12"]='%12.10f'%(hpars['F12'])
                    ResRec["anisotropy_ftest23"]='%12.10f'%(hpars['F23'])
                    ResRec["anisotropy_v1_dec"]='%7.1f'%(hpars['v1_dec'])
                    ResRec["anisotropy_v2_dec"]='%7.1f'%(hpars['v2_dec'])
                    ResRec["anisotropy_v3_dec"]='%7.1f'%(hpars['v3_dec'])
                    ResRec["anisotropy_v1_inc"]='%7.1f'%(hpars['v1_inc'])
                    ResRec["anisotropy_v2_inc"]='%7.1f'%(hpars['v2_inc'])
                    ResRec["anisotropy_v3_inc"]='%7.1f'%(hpars['v3_inc'])
                    ResRec['anisotropy_v1_eta_dec']=ResRec['anisotropy_v2_dec']
                    ResRec['anisotropy_v1_eta_inc']=ResRec['anisotropy_v2_inc']
                    ResRec['anisotropy_v1_zeta_dec']=ResRec['anisotropy_v3_dec']
                    ResRec['anisotropy_v1_zeta_inc']=ResRec['anisotropy_v3_inc']
                    ResRec['anisotropy_v2_eta_dec']=ResRec['anisotropy_v1_dec']
                    ResRec['anisotropy_v2_eta_inc']=ResRec['anisotropy_v1_inc']
                    ResRec['anisotropy_v2_zeta_dec']=ResRec['anisotropy_v3_dec']
                    ResRec['anisotropy_v2_zeta_inc']=ResRec['anisotropy_v3_inc']
                    ResRec['anisotropy_v3_eta_dec']=ResRec['anisotropy_v1_dec']
                    ResRec['anisotropy_v3_eta_inc']=ResRec['anisotropy_v1_inc']
                    ResRec['anisotropy_v3_zeta_dec']=ResRec['anisotropy_v2_dec']
                    ResRec['anisotropy_v3_zeta_inc']=ResRec['anisotropy_v2_inc']
                    ResRec["anisotropy_v1_eta_semi_angle"]='%7.1f'%(hpars['e12'])
                    ResRec["anisotropy_v1_zeta_semi_angle"]='%7.1f'%(hpars['e13'])
                    ResRec["anisotropy_v2_eta_semi_angle"]='%7.1f'%(hpars['e12'])
                    ResRec["anisotropy_v2_zeta_semi_angle"]='%7.1f'%(hpars['e23'])
                    ResRec["anisotropy_v3_eta_semi_angle"]='%7.1f'%(hpars['e13'])
                    ResRec["anisotropy_v3_zeta_semi_angle"]='%7.1f'%(hpars['e23'])
                    ResRec["result_description"]='Critical F: '+hpars["F_crit"]+';Critical F12/F13: '+hpars["F12_crit"]
                    ResRecs.append(ResRec)
                    if igeo==1: 
                        sbarg=pmag.dosgeo(sbar,az,pl) 
                        hparsg=pmag.dohext(9,sigma,sbarg) 
                        AnisRecG=copy.copy(AnisRec)
                        ResRecG=copy.copy(ResRec)
                        AnisRecG["anisotropy_s1"]='%12.10f'%(sbarg[0])
                        AnisRecG["anisotropy_s2"]='%12.10f'%(sbarg[1])
                        AnisRecG["anisotropy_s3"]='%12.10f'%(sbarg[2])
                        AnisRecG["anisotropy_s4"]='%12.10f'%(sbarg[3])
                        AnisRecG["anisotropy_s5"]='%12.10f'%(sbarg[4])
                        AnisRecG["anisotropy_s6"]='%12.10f'%(sbarg[5])
                        AnisRecG["anisotropy_tilt_correction"]='0'
                        ResRecG["anisotropy_tilt_correction"]='0'
                        ResRecG["anisotropy_v1_dec"]='%7.1f'%(hparsg['v1_dec'])
                        ResRecG["anisotropy_v2_dec"]='%7.1f'%(hparsg['v2_dec'])
                        ResRecG["anisotropy_v3_dec"]='%7.1f'%(hparsg['v3_dec'])
                        ResRecG["anisotropy_v1_inc"]='%7.1f'%(hparsg['v1_inc'])
                        ResRecG["anisotropy_v2_inc"]='%7.1f'%(hparsg['v2_inc'])
                        ResRecG["anisotropy_v3_inc"]='%7.1f'%(hparsg['v3_inc'])
                        ResRecG['anisotropy_v1_eta_dec']=ResRecG['anisotropy_v2_dec']
                        ResRecG['anisotropy_v1_eta_inc']=ResRecG['anisotropy_v2_inc']
                        ResRecG['anisotropy_v1_zeta_dec']=ResRecG['anisotropy_v3_dec']
                        ResRecG['anisotropy_v1_zeta_inc']=ResRecG['anisotropy_v3_inc']
                        ResRecG['anisotropy_v2_eta_dec']=ResRecG['anisotropy_v1_dec']
                        ResRecG['anisotropy_v2_eta_inc']=ResRecG['anisotropy_v1_inc']
                        ResRecG['anisotropy_v2_zeta_dec']=ResRecG['anisotropy_v3_dec']
                        ResRecG['anisotropy_v2_zeta_inc']=ResRecG['anisotropy_v3_inc']
                        ResRecG['anisotropy_v3_eta_dec']=ResRecG['anisotropy_v1_dec']
                        ResRecG['anisotropy_v3_eta_inc']=ResRecG['anisotropy_v1_inc']
                        ResRecG['anisotropy_v3_zeta_dec']=ResRecG['anisotropy_v2_dec']
                        ResRecG['anisotropy_v3_zeta_inc']=ResRecG['anisotropy_v2_inc']
                        ResRecG["result_description"]='Critical F: '+hpars["F_crit"]+';Critical F12/F13: '+hpars["F12_crit"]
                        ResRecs.append(ResRecG)
                        AnisRecs.append(AnisRecG)
                    if itilt==1: 
                        sbart=pmag.dostilt(sbarg,bed_az,bed_dip) 
                        hparst=pmag.dohext(9,sigma,sbart)
                        AnisRecT=copy.copy(AnisRec)
                        ResRecT=copy.copy(ResRec)
                        AnisRecT["anisotropy_s1"]='%12.10f'%(sbart[0])
                        AnisRecT["anisotropy_s2"]='%12.10f'%(sbart[1])
                        AnisRecT["anisotropy_s3"]='%12.10f'%(sbart[2])
                        AnisRecT["anisotropy_s4"]='%12.10f'%(sbart[3])
                        AnisRecT["anisotropy_s5"]='%12.10f'%(sbart[4])
                        AnisRecT["anisotropy_s6"]='%12.10f'%(sbart[5])
                        AnisRecT["anisotropy_tilt_correction"]='100'
                        ResRecT["anisotropy_v1_dec"]='%7.1f'%(hparst['v1_dec'])
                        ResRecT["anisotropy_v2_dec"]='%7.1f'%(hparst['v2_dec'])
                        ResRecT["anisotropy_v3_dec"]='%7.1f'%(hparst['v3_dec'])
                        ResRecT["anisotropy_v1_inc"]='%7.1f'%(hparst['v1_inc'])
                        ResRecT["anisotropy_v2_inc"]='%7.1f'%(hparst['v2_inc'])
                        ResRecT["anisotropy_v3_inc"]='%7.1f'%(hparst['v3_inc'])
                        ResRecT['anisotropy_v1_eta_dec']=ResRecT['anisotropy_v2_dec']
                        ResRecT['anisotropy_v1_eta_inc']=ResRecT['anisotropy_v2_inc']
                        ResRecT['anisotropy_v1_zeta_dec']=ResRecT['anisotropy_v3_dec']
                        ResRecT['anisotropy_v1_zeta_inc']=ResRecT['anisotropy_v3_inc']
                        ResRecT['anisotropy_v2_eta_dec']=ResRecT['anisotropy_v1_dec']
                        ResRecT['anisotropy_v2_eta_inc']=ResRecT['anisotropy_v1_inc']
                        ResRecT['anisotropy_v2_zeta_dec']=ResRecT['anisotropy_v3_dec']
                        ResRecT['anisotropy_v2_zeta_inc']=ResRecT['anisotropy_v3_inc']
                        ResRecT['anisotropy_v3_eta_dec']=ResRecT['anisotropy_v1_dec']
                        ResRecT['anisotropy_v3_eta_inc']=ResRecT['anisotropy_v1_inc']
                        ResRecT['anisotropy_v3_zeta_dec']=ResRecT['anisotropy_v2_dec']
                        ResRecT['anisotropy_v3_zeta_inc']=ResRecT['anisotropy_v2_inc']
                        ResRecT["anisotropy_tilt_correction"]='100'
                        ResRecT["result_description"]='Critical F: '+hpars["F_crit"]+';Critical F12/F13: '+hpars["F12_crit"]
                        ResRecs.append(ResRecT)
                        AnisRecs.append(AnisRecT)
                    k15,linecnt=[],0
                    MeasRec,SpecRec,SampRec,SiteRec,AnisRec={},{},{},{},{}
    pmag.magic_write(sampfile,SampRecs,'er_samples')
    pmag.magic_write(anisfile,AnisRecs,'rmag_anisotropy')
    pmag.magic_write(resfile,ResRecs,'rmag_results')
    pmag.magic_write(measfile,MeasRecs,'magic_measurements')
    print "Data saved to: ",sampfile,anisfile,resfile,measfile
Example #4
0
def main():
    """
    NAME
        aniso_magic.py

    DESCRIPTION
        plots anisotropy data with either bootstrap or hext ellipses
    
    SYNTAX
        aniso_magic.py [-h] [command line options]
    OPTIONS 
        -h plots help message and quits
        -usr USER: set the user name
        -f AFILE, specify rmag_anisotropy formatted file for input
        -F RFILE, specify rmag_results formatted file for output
        -x Hext [1963] and bootstrap
        -B DON'T do bootstrap, do Hext
        -par Tauxe [1998] parametric bootstrap
        -v plot bootstrap eigenvectors instead of ellipses
        -sit plot by site instead of entire file
        -crd [s,g,t] coordinate system, default is specimen (g=geographic, t=tilt corrected)
        -P don't make any plots - just make rmag_results table
        -sav don't make the rmag_results table - just save all the plots
        -fmt [svg, jpg, eps] format for output images, pdf default
        -gtc DEC INC  dec,inc of pole to great circle [down(up) in green (cyan)
        -d Vi DEC INC; Vi (1,2,3) to compare to direction DEC INC
        -nb N; specifies the number of bootstraps - default is 1000
    DEFAULTS  
       AFILE:  rmag_anisotropy.txt
       RFILE:  rmag_results.txt
       plot bootstrap ellipses of Constable & Tauxe [1987]
    NOTES
       minor axis: circles
       major axis: triangles
       principal axis: squares
       directions are plotted on the lower hemisphere
       for bootstrapped eigenvector components: Xs: blue, Ys: red, Zs: black
"""
    #
    dir_path = "."
    version_num = pmag.get_version()
    verbose = pmagplotlib.verbose
    args = sys.argv
    ipar, ihext, ivec, iboot, imeas, isite, iplot, vec = 0, 0, 0, 1, 1, 0, 1, 0
    hpars, bpars, PDir = [], [], []
    CS, crd = "-1", "s"
    nb = 1000
    fmt = "pdf"
    ResRecs = []
    orlist = []
    outfile, comp, Dir, gtcirc, PDir = "rmag_results.txt", 0, [], 0, []
    infile = "rmag_anisotropy.txt"
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-WD" in args:
        ind = args.index("-WD")
        dir_path = args[ind + 1]
    if "-nb" in args:
        ind = args.index("-nb")
        nb = int(args[ind + 1])
    if "-usr" in args:
        ind = args.index("-usr")
        user = args[ind + 1]
    else:
        user = ""
    if "-B" in args:
        iboot, ihext = 0, 1
    if "-par" in args:
        ipar = 1
    if "-x" in args:
        ihext = 1
    if "-v" in args:
        ivec = 1
    if "-sit" in args:
        isite = 1
    if "-P" in args:
        iplot = 0
    if "-f" in args:
        ind = args.index("-f")
        infile = args[ind + 1]
    if "-F" in args:
        ind = args.index("-F")
        outfile = args[ind + 1]
    if "-crd" in sys.argv:
        ind = sys.argv.index("-crd")
        crd = sys.argv[ind + 1]
        if crd == "g":
            CS = "0"
        if crd == "t":
            CS = "100"
    if "-fmt" in args:
        ind = args.index("-fmt")
        fmt = args[ind + 1]
    if "-sav" in args:
        plots = 1
        verbose = 0
    else:
        plots = 0
    if "-gtc" in args:
        ind = args.index("-gtc")
        d, i = float(args[ind + 1]), float(args[ind + 2])
        PDir.append(d)
        PDir.append(i)
    if "-d" in args:
        comp = 1
        ind = args.index("-d")
        vec = int(args[ind + 1]) - 1
        Dir = [float(args[ind + 2]), float(args[ind + 3])]
    #
    # set up plots
    #
    if infile[0] != "/":
        infile = dir_path + "/" + infile
    if outfile[0] != "/":
        outfile = dir_path + "/" + outfile
    ANIS = {}
    initcdf, inittcdf = 0, 0
    ANIS["data"], ANIS["conf"] = 1, 2
    if iboot == 1:
        ANIS["tcdf"] = 3
        if iplot == 1:
            inittcdf = 1
            pmagplotlib.plot_init(ANIS["tcdf"], 5, 5)
        if comp == 1 and iplot == 1:
            initcdf = 1
            ANIS["vxcdf"], ANIS["vycdf"], ANIS["vzcdf"] = 4, 5, 6
            pmagplotlib.plot_init(ANIS["vxcdf"], 5, 5)
            pmagplotlib.plot_init(ANIS["vycdf"], 5, 5)
            pmagplotlib.plot_init(ANIS["vzcdf"], 5, 5)
    if iplot == 1:
        pmagplotlib.plot_init(ANIS["conf"], 5, 5)
        pmagplotlib.plot_init(ANIS["data"], 5, 5)
    # read in the data
    data, ifiletype = pmag.magic_read(infile)
    for rec in data:  # find all the orientation systems
        if "anisotropy_tilt_correction" not in rec.keys():
            rec["anisotropy_tilt_correction"] = "-1"
        if rec["anisotropy_tilt_correction"] not in orlist:
            orlist.append(rec["anisotropy_tilt_correction"])
    if CS not in orlist:
        if len(orlist) > 0:
            CS = orlist[0]
        else:
            CS = "-1"
        if CS == "-1":
            crd = "s"
        if CS == "0":
            crd = "g"
        if CS == "100":
            crd = "t"
        if verbose:
            print "desired coordinate system not available, using available: ", crd
    if isite == 1:
        sitelist = []
        for rec in data:
            if rec["er_site_name"] not in sitelist:
                sitelist.append(rec["er_site_name"])
        sitelist.sort()
        plt = len(sitelist)
    else:
        plt = 1
    k = 0
    while k < plt:
        site = ""
        sdata, Ss = [], []  # list of S format data
        Locs, Sites, Samples, Specimens, Cits = [], [], [], [], []
        if isite == 0:
            sdata = data
        else:
            site = sitelist[k]
            for rec in data:
                if rec["er_site_name"] == site:
                    sdata.append(rec)
        anitypes = []
        csrecs = pmag.get_dictitem(sdata, "anisotropy_tilt_correction", CS, "T")
        for rec in csrecs:
            if rec["anisotropy_type"] not in anitypes:
                anitypes.append(rec["anisotropy_type"])
            if rec["er_location_name"] not in Locs:
                Locs.append(rec["er_location_name"])
            if rec["er_site_name"] not in Sites:
                Sites.append(rec["er_site_name"])
            if rec["er_sample_name"] not in Samples:
                Samples.append(rec["er_sample_name"])
            if rec["er_specimen_name"] not in Specimens:
                Specimens.append(rec["er_specimen_name"])
            if rec["er_citation_names"] not in Cits:
                Cits.append(rec["er_citation_names"])
            s = []
            s.append(float(rec["anisotropy_s1"]))
            s.append(float(rec["anisotropy_s2"]))
            s.append(float(rec["anisotropy_s3"]))
            s.append(float(rec["anisotropy_s4"]))
            s.append(float(rec["anisotropy_s5"]))
            s.append(float(rec["anisotropy_s6"]))
            if s[0] <= 1.0:
                Ss.append(s)  # protect against crap
            # tau,Vdirs=pmag.doseigs(s)
            fpars = pmag.dohext(int(rec["anisotropy_n"]) - 6, float(rec["anisotropy_sigma"]), s)
            ResRec = {}
            ResRec["er_location_names"] = rec["er_location_name"]
            ResRec["er_citation_names"] = rec["er_citation_names"]
            ResRec["er_site_names"] = rec["er_site_name"]
            ResRec["er_sample_names"] = rec["er_sample_name"]
            ResRec["er_specimen_names"] = rec["er_specimen_name"]
            ResRec["rmag_result_name"] = rec["er_specimen_name"] + ":" + rec["anisotropy_type"]
            ResRec["er_analyst_mail_names"] = user
            ResRec["tilt_correction"] = CS
            ResRec["anisotropy_type"] = rec["anisotropy_type"]
            ResRec["anisotropy_v1_dec"] = "%7.1f" % (fpars["v1_dec"])
            ResRec["anisotropy_v2_dec"] = "%7.1f" % (fpars["v2_dec"])
            ResRec["anisotropy_v3_dec"] = "%7.1f" % (fpars["v3_dec"])
            ResRec["anisotropy_v1_inc"] = "%7.1f" % (fpars["v1_inc"])
            ResRec["anisotropy_v2_inc"] = "%7.1f" % (fpars["v2_inc"])
            ResRec["anisotropy_v3_inc"] = "%7.1f" % (fpars["v3_inc"])
            ResRec["anisotropy_t1"] = "%10.8f" % (fpars["t1"])
            ResRec["anisotropy_t2"] = "%10.8f" % (fpars["t2"])
            ResRec["anisotropy_t3"] = "%10.8f" % (fpars["t3"])
            ResRec["anisotropy_ftest"] = "%10.3f" % (fpars["F"])
            ResRec["anisotropy_ftest12"] = "%10.3f" % (fpars["F12"])
            ResRec["anisotropy_ftest23"] = "%10.3f" % (fpars["F23"])
            ResRec["result_description"] = "F_crit: " + fpars["F_crit"] + "; F12,F23_crit: " + fpars["F12_crit"]
            ResRec["anisotropy_type"] = pmag.makelist(anitypes)
            ResRecs.append(ResRec)
        if len(Ss) > 1:
            title = "LO:_" + ResRec["er_location_names"] + "_SI:_" + site + "_SA:__SP:__CO:_" + crd
            ResRec["er_location_names"] = pmag.makelist(Locs)
            bpars, hpars = pmagplotlib.plotANIS(ANIS, Ss, iboot, ihext, ivec, ipar, title, iplot, comp, vec, Dir, nb)
            if len(PDir) > 0:
                pmagplotlib.plotC(ANIS["data"], PDir, 90.0, "g")
                pmagplotlib.plotC(ANIS["conf"], PDir, 90.0, "g")
            if verbose and plots == 0:
                pmagplotlib.drawFIGS(ANIS)
            ResRec["er_location_names"] = pmag.makelist(Locs)
            if plots == 1:
                save(ANIS, fmt, title)
            ResRec = {}
            ResRec["er_citation_names"] = pmag.makelist(Cits)
            ResRec["er_location_names"] = pmag.makelist(Locs)
            ResRec["er_site_names"] = pmag.makelist(Sites)
            ResRec["er_sample_names"] = pmag.makelist(Samples)
            ResRec["er_specimen_names"] = pmag.makelist(Specimens)
            ResRec["rmag_result_name"] = pmag.makelist(Sites) + ":" + pmag.makelist(anitypes)
            ResRec["anisotropy_type"] = pmag.makelist(anitypes)
            ResRec["er_analyst_mail_names"] = user
            ResRec["tilt_correction"] = CS
            if isite == "0":
                ResRec["result_description"] = "Study average using coordinate system: " + CS
            if isite == "1":
                ResRec["result_description"] = "Site average using coordinate system: " + CS
            if hpars != [] and ihext == 1:
                HextRec = {}
                for key in ResRec.keys():
                    HextRec[key] = ResRec[key]  # copy over stuff
                HextRec["anisotropy_v1_dec"] = "%7.1f" % (hpars["v1_dec"])
                HextRec["anisotropy_v2_dec"] = "%7.1f" % (hpars["v2_dec"])
                HextRec["anisotropy_v3_dec"] = "%7.1f" % (hpars["v3_dec"])
                HextRec["anisotropy_v1_inc"] = "%7.1f" % (hpars["v1_inc"])
                HextRec["anisotropy_v2_inc"] = "%7.1f" % (hpars["v2_inc"])
                HextRec["anisotropy_v3_inc"] = "%7.1f" % (hpars["v3_inc"])
                HextRec["anisotropy_t1"] = "%10.8f" % (hpars["t1"])
                HextRec["anisotropy_t2"] = "%10.8f" % (hpars["t2"])
                HextRec["anisotropy_t3"] = "%10.8f" % (hpars["t3"])
                HextRec["anisotropy_hext_F"] = "%7.1f " % (hpars["F"])
                HextRec["anisotropy_hext_F12"] = "%7.1f " % (hpars["F12"])
                HextRec["anisotropy_hext_F23"] = "%7.1f " % (hpars["F23"])
                HextRec["anisotropy_v1_eta_semi_angle"] = "%7.1f " % (hpars["e12"])
                HextRec["anisotropy_v1_eta_dec"] = "%7.1f " % (hpars["v2_dec"])
                HextRec["anisotropy_v1_eta_inc"] = "%7.1f " % (hpars["v2_inc"])
                HextRec["anisotropy_v1_zeta_semi_angle"] = "%7.1f " % (hpars["e13"])
                HextRec["anisotropy_v1_zeta_dec"] = "%7.1f " % (hpars["v3_dec"])
                HextRec["anisotropy_v1_zeta_inc"] = "%7.1f " % (hpars["v3_inc"])
                HextRec["anisotropy_v2_eta_semi_angle"] = "%7.1f " % (hpars["e12"])
                HextRec["anisotropy_v2_eta_dec"] = "%7.1f " % (hpars["v1_dec"])
                HextRec["anisotropy_v2_eta_inc"] = "%7.1f " % (hpars["v1_inc"])
                HextRec["anisotropy_v2_zeta_semi_angle"] = "%7.1f " % (hpars["e23"])
                HextRec["anisotropy_v2_zeta_dec"] = "%7.1f " % (hpars["v3_dec"])
                HextRec["anisotropy_v2_zeta_inc"] = "%7.1f " % (hpars["v3_inc"])
                HextRec["anisotropy_v3_eta_semi_angle"] = "%7.1f " % (hpars["e12"])
                HextRec["anisotropy_v3_eta_dec"] = "%7.1f " % (hpars["v1_dec"])
                HextRec["anisotropy_v3_eta_inc"] = "%7.1f " % (hpars["v1_inc"])
                HextRec["anisotropy_v3_zeta_semi_angle"] = "%7.1f " % (hpars["e23"])
                HextRec["anisotropy_v3_zeta_dec"] = "%7.1f " % (hpars["v2_dec"])
                HextRec["anisotropy_v3_zeta_inc"] = "%7.1f " % (hpars["v2_inc"])
                HextRec["magic_method_codes"] = "LP-AN:AE-H"
                if verbose:
                    print "Hext Statistics: "
                    print " tau_i, V_i_D, V_i_I, V_i_zeta, V_i_zeta_D, V_i_zeta_I, V_i_eta, V_i_eta_D, V_i_eta_I"
                    print HextRec["anisotropy_t1"], HextRec["anisotropy_v1_dec"], HextRec["anisotropy_v1_inc"], HextRec[
                        "anisotropy_v1_eta_semi_angle"
                    ], HextRec["anisotropy_v1_eta_dec"], HextRec["anisotropy_v1_eta_inc"], HextRec[
                        "anisotropy_v1_zeta_semi_angle"
                    ], HextRec[
                        "anisotropy_v1_zeta_dec"
                    ], HextRec[
                        "anisotropy_v1_zeta_inc"
                    ]
                    print HextRec["anisotropy_t2"], HextRec["anisotropy_v2_dec"], HextRec["anisotropy_v2_inc"], HextRec[
                        "anisotropy_v2_eta_semi_angle"
                    ], HextRec["anisotropy_v2_eta_dec"], HextRec["anisotropy_v2_eta_inc"], HextRec[
                        "anisotropy_v2_zeta_semi_angle"
                    ], HextRec[
                        "anisotropy_v2_zeta_dec"
                    ], HextRec[
                        "anisotropy_v2_zeta_inc"
                    ]
                    print HextRec["anisotropy_t3"], HextRec["anisotropy_v3_dec"], HextRec["anisotropy_v3_inc"], HextRec[
                        "anisotropy_v3_eta_semi_angle"
                    ], HextRec["anisotropy_v3_eta_dec"], HextRec["anisotropy_v3_eta_inc"], HextRec[
                        "anisotropy_v3_zeta_semi_angle"
                    ], HextRec[
                        "anisotropy_v3_zeta_dec"
                    ], HextRec[
                        "anisotropy_v3_zeta_inc"
                    ]
                HextRec["magic_software_packages"] = version_num
                ResRecs.append(HextRec)
            if bpars != []:
                BootRec = {}
                for key in ResRec.keys():
                    BootRec[key] = ResRec[key]  # copy over stuff
                BootRec["anisotropy_v1_dec"] = "%7.1f" % (bpars["v1_dec"])
                BootRec["anisotropy_v2_dec"] = "%7.1f" % (bpars["v2_dec"])
                BootRec["anisotropy_v3_dec"] = "%7.1f" % (bpars["v3_dec"])
                BootRec["anisotropy_v1_inc"] = "%7.1f" % (bpars["v1_inc"])
                BootRec["anisotropy_v2_inc"] = "%7.1f" % (bpars["v2_inc"])
                BootRec["anisotropy_v3_inc"] = "%7.1f" % (bpars["v3_inc"])
                BootRec["anisotropy_t1"] = "%10.8f" % (bpars["t1"])
                BootRec["anisotropy_t2"] = "%10.8f" % (bpars["t2"])
                BootRec["anisotropy_t3"] = "%10.8f" % (bpars["t3"])
                BootRec["anisotropy_v1_eta_inc"] = "%7.1f " % (bpars["v1_eta_inc"])
                BootRec["anisotropy_v1_eta_dec"] = "%7.1f " % (bpars["v1_eta_dec"])
                BootRec["anisotropy_v1_eta_semi_angle"] = "%7.1f " % (bpars["v1_eta"])
                BootRec["anisotropy_v1_zeta_inc"] = "%7.1f " % (bpars["v1_zeta_inc"])
                BootRec["anisotropy_v1_zeta_dec"] = "%7.1f " % (bpars["v1_zeta_dec"])
                BootRec["anisotropy_v1_zeta_semi_angle"] = "%7.1f " % (bpars["v1_zeta"])
                BootRec["anisotropy_v2_eta_inc"] = "%7.1f " % (bpars["v2_eta_inc"])
                BootRec["anisotropy_v2_eta_dec"] = "%7.1f " % (bpars["v2_eta_dec"])
                BootRec["anisotropy_v2_eta_semi_angle"] = "%7.1f " % (bpars["v2_eta"])
                BootRec["anisotropy_v2_zeta_inc"] = "%7.1f " % (bpars["v2_zeta_inc"])
                BootRec["anisotropy_v2_zeta_dec"] = "%7.1f " % (bpars["v2_zeta_dec"])
                BootRec["anisotropy_v2_zeta_semi_angle"] = "%7.1f " % (bpars["v2_zeta"])
                BootRec["anisotropy_v3_eta_inc"] = "%7.1f " % (bpars["v3_eta_inc"])
                BootRec["anisotropy_v3_eta_dec"] = "%7.1f " % (bpars["v3_eta_dec"])
                BootRec["anisotropy_v3_eta_semi_angle"] = "%7.1f " % (bpars["v3_eta"])
                BootRec["anisotropy_v3_zeta_inc"] = "%7.1f " % (bpars["v3_zeta_inc"])
                BootRec["anisotropy_v3_zeta_dec"] = "%7.1f " % (bpars["v3_zeta_dec"])
                BootRec["anisotropy_v3_zeta_semi_angle"] = "%7.1f " % (bpars["v3_zeta"])
                BootRec["anisotropy_hext_F"] = ""
                BootRec["anisotropy_hext_F12"] = ""
                BootRec["anisotropy_hext_F23"] = ""
                BootRec["magic_method_codes"] = "LP-AN:AE-H:AE-BS"  # regular bootstrap
                if ipar == 1:
                    BootRec["magic_method_codes"] = "LP-AN:AE-H:AE-BS-P"  # parametric bootstrap
                if verbose:
                    print "Boostrap Statistics: "
                    print " tau_i, V_i_D, V_i_I, V_i_zeta, V_i_zeta_D, V_i_zeta_I, V_i_eta, V_i_eta_D, V_i_eta_I"
                    print BootRec["anisotropy_t1"], BootRec["anisotropy_v1_dec"], BootRec["anisotropy_v1_inc"], BootRec[
                        "anisotropy_v1_eta_semi_angle"
                    ], BootRec["anisotropy_v1_eta_dec"], BootRec["anisotropy_v1_eta_inc"], BootRec[
                        "anisotropy_v1_zeta_semi_angle"
                    ], BootRec[
                        "anisotropy_v1_zeta_dec"
                    ], BootRec[
                        "anisotropy_v1_zeta_inc"
                    ]
                    print BootRec["anisotropy_t2"], BootRec["anisotropy_v2_dec"], BootRec["anisotropy_v2_inc"], BootRec[
                        "anisotropy_v2_eta_semi_angle"
                    ], BootRec["anisotropy_v2_eta_dec"], BootRec["anisotropy_v2_eta_inc"], BootRec[
                        "anisotropy_v2_zeta_semi_angle"
                    ], BootRec[
                        "anisotropy_v2_zeta_dec"
                    ], BootRec[
                        "anisotropy_v2_zeta_inc"
                    ]
                    print BootRec["anisotropy_t3"], BootRec["anisotropy_v3_dec"], BootRec["anisotropy_v3_inc"], BootRec[
                        "anisotropy_v3_eta_semi_angle"
                    ], BootRec["anisotropy_v3_eta_dec"], BootRec["anisotropy_v3_eta_inc"], BootRec[
                        "anisotropy_v3_zeta_semi_angle"
                    ], BootRec[
                        "anisotropy_v3_zeta_dec"
                    ], BootRec[
                        "anisotropy_v3_zeta_inc"
                    ]
                BootRec["magic_software_packages"] = version_num
                ResRecs.append(BootRec)
            k += 1
            goon = 1
            while goon == 1 and iplot == 1 and verbose:
                if iboot == 1:
                    print "compare with [d]irection "
                print " plot [g]reat circle,  change [c]oord. system, change [e]llipse calculation,  s[a]ve plots, [q]uit "
                if isite == 1:
                    print "  [p]revious, [s]ite, [q]uit, <return> for next "
                ans = raw_input("")
                if ans == "q":
                    sys.exit()
                if ans == "e":
                    iboot, ipar, ihext, ivec = 1, 0, 0, 0
                    e = raw_input("Do Hext Statistics  1/[0]: ")
                    if e == "1":
                        ihext = 1
                    e = raw_input("Suppress bootstrap 1/[0]: ")
                    if e == "1":
                        iboot = 0
                    if iboot == 1:
                        e = raw_input("Parametric bootstrap 1/[0]: ")
                        if e == "1":
                            ipar = 1
                        e = raw_input("Plot bootstrap eigenvectors:  1/[0]: ")
                        if e == "1":
                            ivec = 1
                        if iplot == 1:
                            if inittcdf == 0:
                                ANIS["tcdf"] = 3
                                pmagplotlib.plot_init(ANIS["tcdf"], 5, 5)
                                inittcdf = 1
                    bpars, hpars = pmagplotlib.plotANIS(
                        ANIS, Ss, iboot, ihext, ivec, ipar, title, iplot, comp, vec, Dir, nb
                    )
                    if verbose and plots == 0:
                        pmagplotlib.drawFIGS(ANIS)
                if ans == "c":
                    print "Current Coordinate system is: "
                    if CS == "-1":
                        print " Specimen"
                    if CS == "0":
                        print " Geographic"
                    if CS == "100":
                        print " Tilt corrected"
                    key = raw_input(" Enter desired coordinate system: [s]pecimen, [g]eographic, [t]ilt corrected ")
                    if key == "s":
                        CS = "-1"
                    if key == "g":
                        CS = "0"
                    if key == "t":
                        CS = "100"
                    if CS not in orlist:
                        if len(orlist) > 0:
                            CS = orlist[0]
                        else:
                            CS = "-1"
                        if CS == "-1":
                            crd = "s"
                        if CS == "0":
                            crd = "g"
                        if CS == "100":
                            crd = "t"
                        print "desired coordinate system not available, using available: ", crd
                    k -= 1
                    goon = 0
                if ans == "":
                    if isite == 1:
                        goon = 0
                    else:
                        print "Good bye "
                        sys.exit()
                if ans == "d":
                    if initcdf == 0:
                        initcdf = 1
                        ANIS["vxcdf"], ANIS["vycdf"], ANIS["vzcdf"] = 4, 5, 6
                        pmagplotlib.plot_init(ANIS["vxcdf"], 5, 5)
                        pmagplotlib.plot_init(ANIS["vycdf"], 5, 5)
                        pmagplotlib.plot_init(ANIS["vzcdf"], 5, 5)
                    Dir, comp = [], 1
                    print """ 
                      Input: Vi D I to  compare  eigenvector Vi with direction D/I
                             where Vi=1: principal
                                   Vi=2: major
                                   Vi=3: minor
                                   D= declination of comparison direction
                                   I= inclination of comparison direction"""
                    con = 1
                    while con == 1:
                        try:
                            vdi = raw_input("Vi D I: ").split()
                            vec = int(vdi[0]) - 1
                            Dir = [float(vdi[1]), float(vdi[2])]
                            con = 0
                        except IndexError:
                            print " Incorrect entry, try again "
                    bpars, hpars = pmagplotlib.plotANIS(
                        ANIS, Ss, iboot, ihext, ivec, ipar, title, iplot, comp, vec, Dir, nb
                    )
                    Dir, comp = [], 0
                if ans == "g":
                    con, cnt = 1, 0
                    while con == 1:
                        try:
                            print " Input:  input pole to great circle ( D I) to  plot a great circle:   "
                            di = raw_input(" D I: ").split()
                            PDir.append(float(di[0]))
                            PDir.append(float(di[1]))
                            con = 0
                        except:
                            cnt += 1
                            if cnt < 10:
                                print " enter the dec and inc of the pole on one line "
                            else:
                                print "ummm - you are doing something wrong - i give up"
                                sys.exit()
                    pmagplotlib.plotC(ANIS["data"], PDir, 90.0, "g")
                    pmagplotlib.plotC(ANIS["conf"], PDir, 90.0, "g")
                    if verbose and plots == 0:
                        pmagplotlib.drawFIGS(ANIS)
                if ans == "p":
                    k -= 2
                    goon = 0
                if ans == "q":
                    k = plt
                    goon = 0
                if ans == "s":
                    keepon = 1
                    site = raw_input(" print site or part of site desired: ")
                    while keepon == 1:
                        try:
                            k = sitelist.index(site)
                            keepon = 0
                        except:
                            tmplist = []
                            for qq in range(len(sitelist)):
                                if site in sitelist[qq]:
                                    tmplist.append(sitelist[qq])
                            print site, " not found, but this was: "
                            print tmplist
                            site = raw_input("Select one or try again\n ")
                            k = sitelist.index(site)
                    goon, ans = 0, ""
                if ans == "a":
                    locs = pmag.makelist(Locs)
                    title = "LO:_" + locs + "_SI:__" + "_SA:__SP:__CO:_" + crd
                    save(ANIS, fmt, title)
                    goon = 0
        else:
            if verbose:
                print "skipping plot - not enough data points"
            k += 1
    #   put rmag_results stuff here
    if len(ResRecs) > 0:
        ResOut, keylist = pmag.fillkeys(ResRecs)
        pmag.magic_write(outfile, ResOut, "rmag_results")
    if verbose:
        print " Good bye "
Example #5
0
def main():
    """
    NAME
        aarm_magic.py

    DESCRIPTION
        Converts AARM  data to best-fit tensor (6 elements plus sigma)
         Original program ARMcrunch written to accomodate ARM anisotropy data
          collected from 6 axial directions (+X,+Y,+Z,-X,-Y,-Z) using the
          off-axis remanence terms to construct the tensor. A better way to
          do the anisotropy of ARMs is to use 9,12 or 15 measurements in
          the Hext rotational scheme.
    
    SYNTAX 
        aarm_magic.py [-h][command line options]

    OPTIONS
        -h prints help message and quits
        -usr USER:   identify user, default is ""
        -f FILE: specify input file, default is aarm_measurements.txt
        -Fa FILE: specify anisotropy output file, default is rmag_anisotropy.txt
        -Fr FILE: specify results output file, default is rmag_results.txt

    INPUT  
        Input for the present program is a series of baseline, ARM pairs.
      The baseline should be the AF demagnetized state (3 axis demag is
      preferable) for the following ARM acquisition. The order of the
      measurements is:
    
           positions 1,2,3, 6,7,8, 11,12,13 (for 9 positions)
           positions 1,2,3,4, 6,7,8,9, 11,12,13,14 (for 12 positions)
           positions 1-15 (for 15 positions)
    """
    # initialize some parameters
    args=sys.argv
    user=""
    meas_file="aarm_measurements.txt"
    rmag_anis="rmag_anisotropy.txt"
    rmag_res="rmag_results.txt"
    dir_path='.'
    #
    # get name of file from command line
    #
    if '-WD' in args:
        ind=args.index('-WD')
        dir_path=args[ind+1]
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-usr" in args:
        ind=args.index("-usr")
        user=sys.argv[ind+1]
    if "-f" in args:
        ind=args.index("-f")
        meas_file=sys.argv[ind+1]
    if "-Fa" in args:
        ind=args.index("-Fa")
        rmag_anis=args[ind+1]
    if "-Fr" in args:
        ind=args.index("-Fr")
        rmag_res=args[ind+1]
    meas_file=dir_path+'/'+meas_file
    rmag_anis=dir_path+'/'+rmag_anis
    rmag_res=dir_path+'/'+rmag_res
    # read in data
    meas_data,file_type=pmag.magic_read(meas_file)
    if file_type != 'magic_measurements':
        print file_type
        print file_type,"This is not a valid magic_measurements file " 
        sys.exit()
    #
    # sort the specimen names
    #
    ssort=[]
    for rec in meas_data:
      spec=rec["er_specimen_name"]
      ssort.append(spec)
    ssort.sort()
    bak=ssort[0]
    #
    # get list of unique specimen names
    #
    sids=[bak]
    for s in ssort:
       if s !=  bak: 
          sids.append(s)
          bak=s
    #
    # work on each specimen
    #
    specimen=0
    RmagSpecRecs,RmagResRecs=[],[]
    while specimen < len(sids):
        s=sids[specimen]
        data=[]
        RmagSpecRec={}
        RmagResRec={}
        method_codes=[]
    #
    # find the data from the meas_data file for this sample
    #
        for rec in meas_data:
            if rec["er_specimen_name"]==s: 
                data.append(rec)
    #
    # find out the number of measurements (9, 12 or 15)
    #
        npos=len(data)/2
        if npos==9:
            print 'Processing: ',s, ' Number of positions: ',npos
        #
        # get dec, inc, int and convert to x,y,z
        #
            B,H,tmpH=pmag.designAARM(npos)  # B matrix made from design matrix for positions
            X=[]
            for rec in data:
                Dir=[]
                Dir.append(float(rec["measurement_dec"]))
                Dir.append(float(rec["measurement_inc"]))
                Dir.append(float(rec["measurement_magn_moment"]))
                X.append(pmag.dir2cart(Dir))
        #
        # subtract baseline and put in a work array
        #
            work=numpy.zeros((npos,3),'f')
            for i in range(npos):
                for j in range(3):
                    work[i][j]=X[2*i+1][j]-X[2*i][j]
        #
        # calculate tensor elements
        # first put ARM components in w vector
        #
            w=numpy.zeros((npos*3),'f')
            index=0
            for i in range(npos):
                for j in range(3):
                    w[index]=work[i][j] 
                    index+=1
            s=numpy.zeros((6),'f') # initialize the s matrix
            for i in range(6):
                for j in range(len(w)):
                    s[i]+=B[i][j]*w[j] 
            trace=s[0]+s[1]+s[2]   # normalize by the trace
            for i in range(6):
                s[i]=s[i]/trace
            a=pmag.s2a(s)
        #------------------------------------------------------------
        #  Calculating dels is different than in the Kappabridge
        #  routine. Use trace normalized tensor (a) and the applied
        #  unit field directions (tmpH) to generate model X,Y,Z
        #  components. Then compare these with the measured values.
        #------------------------------------------------------------
            S=0.
            comp=numpy.zeros((npos*3),'f')
            for i in range(npos):
                for j in range(3):
                    index=i*3+j
                    compare=a[j][0]*tmpH[i][0]+a[j][1]*tmpH[i][1]+a[j][2]*tmpH[i][2]
                    comp[index]=compare
            for i in range(npos*3):
                d=w[i]/trace - comp[i] # del values
                S+=d*d
            nf=float(npos*3-6) # number of degrees of freedom
            if S >0: 
                sigma=math.sqrt(S/nf)
            else: sigma=0
            hpars=pmag.dohext(nf,sigma,s)
        #
        # prepare for output
        #
            RmagSpecRec["rmag_anisotropy_name"]=data[0]["er_specimen_name"]
            RmagSpecRec["er_location_name"]=data[0]["er_location_name"]
            RmagSpecRec["er_specimen_name"]=data[0]["er_specimen_name"]
            RmagSpecRec["er_sample_name"]=data[0]["er_sample_name"]
            RmagSpecRec["er_site_name"]=data[0]["er_site_name"]
            RmagSpecRec["magic_experiment_names"]=RmagSpecRec["rmag_anisotropy_name"]+":AARM"
            RmagSpecRec["er_citation_names"]="This study"
            RmagResRec["rmag_result_name"]=data[0]["er_specimen_name"]
            RmagResRec["er_location_names"]=data[0]["er_location_name"]
            RmagResRec["er_specimen_names"]=data[0]["er_specimen_name"]
            RmagResRec["er_sample_names"]=data[0]["er_sample_name"]
            RmagResRec["er_site_names"]=data[0]["er_site_name"]
            RmagResRec["magic_experiment_names"]=RmagSpecRec["rmag_anisotropy_name"]+":AARM"
            RmagResRec["er_citation_names"]="This study"
            if "magic_instrument_codes" in data[0].keys():
                RmagSpecRec["magic_instrument_codes"]=data[0]["magic_instrument_codes"]
            else:  
                RmagSpecRec["magic_instrument_codes"]=""
            RmagSpecRec["anisotropy_type"]="AARM"
            RmagSpecRec["anisotropy_description"]="Hext statistics adapted to AARM"
            RmagSpecRec["anisotropy_s1"]='%8.6f'%(s[0])
            RmagSpecRec["anisotropy_s2"]='%8.6f'%(s[1])
            RmagSpecRec["anisotropy_s3"]='%8.6f'%(s[2])
            RmagSpecRec["anisotropy_s4"]='%8.6f'%(s[3])
            RmagSpecRec["anisotropy_s5"]='%8.6f'%(s[4])
            RmagSpecRec["anisotropy_s6"]='%8.6f'%(s[5])
            RmagSpecRec["anisotropy_mean"]='%8.3e'%(trace/3)
            RmagSpecRec["anisotropy_sigma"]='%8.6f'%(sigma)
            RmagSpecRec["anisotropy_unit"]="Am^2"
            RmagSpecRec["anisotropy_n"]='%i'%(npos)
            RmagSpecRec["anisotropy_tilt_correction"]='-1'
            RmagResRec["anisotropy_t1"]='%8.6f '%(hpars["t1"])
            RmagResRec["anisotropy_t2"]='%8.6f '%(hpars["t2"])
            RmagResRec["anisotropy_t3"]='%8.6f '%(hpars["t3"])
            RmagResRec["anisotropy_v1_dec"]='%7.1f '%(hpars["v1_dec"])
            RmagResRec["anisotropy_v2_dec"]='%7.1f '%(hpars["v2_dec"])
            RmagResRec["anisotropy_v3_dec"]='%7.1f '%(hpars["v3_dec"])
            RmagResRec["anisotropy_v1_inc"]='%7.1f '%(hpars["v1_inc"])
            RmagResRec["anisotropy_v2_inc"]='%7.1f '%(hpars["v2_inc"])
            RmagResRec["anisotropy_v3_inc"]='%7.1f '%(hpars["v3_inc"])
            RmagResRec["anisotropy_ftest"]='%7.1f '%(hpars["F"])
            RmagResRec["anisotropy_ftest12"]='%7.1f '%(hpars["F12"])
            RmagResRec["anisotropy_ftest23"]='%7.1f '%(hpars["F23"])
            if hpars["e12"]>hpars["e13"]:
                RmagResRec["anisotropy_v1_zeta_semi_angle"]='%7.1f '%(hpars['e12'])
                RmagResRec["anisotropy_v1_zeta_dec"]='%7.1f '%(hpars['v2_dec'])
                RmagResRec["anisotropy_v1_zeta_inc"]='%7.1f '%(hpars['v2_inc'])
                RmagResRec["anisotropy_v2_zeta_semi_angle"]='%7.1f '%(hpars['e12'])
                RmagResRec["anisotropy_v2_zeta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v2_zeta_inc"]='%7.1f '%(hpars['v1_inc'])
                RmagResRec["anisotropy_v1_eta_semi_angle"]='%7.1f '%(hpars['e13'])
                RmagResRec["anisotropy_v1_eta_dec"]='%7.1f '%(hpars['v3_dec'])
                RmagResRec["anisotropy_v1_eta_inc"]='%7.1f '%(hpars['v3_inc'])
                RmagResRec["anisotropy_v3_eta_semi_angle"]='%7.1f '%(hpars['e13'])
                RmagResRec["anisotropy_v3_eta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v3_eta_inc"]='%7.1f '%(hpars['v1_inc'])
            else:
                RmagResRec["anisotropy_v1_zeta_semi_angle"]='%7.1f '%(hpars['e13'])
                RmagResRec["anisotropy_v1_zeta_dec"]='%7.1f '%(hpars['v3_dec'])
                RmagResRec["anisotropy_v1_zeta_inc"]='%7.1f '%(hpars['v3_inc'])
                RmagResRec["anisotropy_v3_zeta_semi_angle"]='%7.1f '%(hpars['e13'])
                RmagResRec["anisotropy_v3_zeta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v3_zeta_inc"]='%7.1f '%(hpars['v1_inc'])
                RmagResRec["anisotropy_v1_eta_semi_angle"]='%7.1f '%(hpars['e12'])
                RmagResRec["anisotropy_v1_eta_dec"]='%7.1f '%(hpars['v2_dec'])
                RmagResRec["anisotropy_v1_eta_inc"]='%7.1f '%(hpars['v2_inc'])
                RmagResRec["anisotropy_v2_eta_semi_angle"]='%7.1f '%(hpars['e12'])
                RmagResRec["anisotropy_v2_eta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v2_eta_inc"]='%7.1f '%(hpars['v1_inc'])
            if hpars["e23"]>hpars['e12']:
                RmagResRec["anisotropy_v2_zeta_semi_angle"]='%7.1f '%(hpars['e23'])
                RmagResRec["anisotropy_v2_zeta_dec"]='%7.1f '%(hpars['v3_dec'])
                RmagResRec["anisotropy_v2_zeta_inc"]='%7.1f '%(hpars['v3_inc'])
                RmagResRec["anisotropy_v3_zeta_semi_angle"]='%7.1f '%(hpars['e23'])
                RmagResRec["anisotropy_v3_zeta_dec"]='%7.1f '%(hpars['v2_dec'])
                RmagResRec["anisotropy_v3_zeta_inc"]='%7.1f '%(hpars['v2_inc'])
                RmagResRec["anisotropy_v3_eta_semi_angle"]='%7.1f '%(hpars['e13'])
                RmagResRec["anisotropy_v3_eta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v3_eta_inc"]='%7.1f '%(hpars['v1_inc'])
                RmagResRec["anisotropy_v2_eta_semi_angle"]='%7.1f '%(hpars['e12'])
                RmagResRec["anisotropy_v2_eta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v2_eta_inc"]='%7.1f '%(hpars['v1_inc'])
            else:
                RmagResRec["anisotropy_v2_zeta_semi_angle"]='%7.1f '%(hpars['e12'])
                RmagResRec["anisotropy_v2_zeta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v2_zeta_inc"]='%7.1f '%(hpars['v1_inc'])
                RmagResRec["anisotropy_v3_eta_semi_angle"]='%7.1f '%(hpars['e23'])
                RmagResRec["anisotropy_v3_eta_dec"]='%7.1f '%(hpars['v2_dec'])
                RmagResRec["anisotropy_v3_eta_inc"]='%7.1f '%(hpars['v2_inc'])
                RmagResRec["anisotropy_v3_zeta_semi_angle"]='%7.1f '%(hpars['e13'])
                RmagResRec["anisotropy_v3_zeta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v3_zeta_inc"]='%7.1f '%(hpars['v1_inc'])
                RmagResRec["anisotropy_v2_eta_semi_angle"]='%7.1f '%(hpars['e23'])
                RmagResRec["anisotropy_v2_eta_dec"]='%7.1f '%(hpars['v3_dec'])
                RmagResRec["anisotropy_v2_eta_inc"]='%7.1f '%(hpars['v3_inc'])
            RmagResRec["tilt_correction"]='-1'
            RmagResRec["anisotropy_type"]='AARM'
            RmagResRec["magic_method_codes"]='LP-AN-ARM:AE-H'
            RmagSpecRec["magic_method_codes"]='LP-AN-ARM:AE-H'
            RmagResRec["magic_software_packages"]=pmag.get_version()
            RmagSpecRec["magic_software_packages"]=pmag.get_version()
            specimen+=1
            RmagSpecRecs.append(RmagSpecRec)
            RmagResRecs.append(RmagResRec)
        else:
            print npos
            print 'skipping specimen ',s,' only 9 positions supported'
            specimen+=1
    if rmag_anis=="":rmag_anis="rmag_anisotropy.txt"
    pmag.magic_write(rmag_anis,RmagSpecRecs,'rmag_anisotropy')
    print "specimen tensor elements stored in ",rmag_anis
    if rmag_res=="":rmag_res="rmag_results.txt"
    pmag.magic_write(rmag_res,RmagResRecs,'rmag_results')
    print "specimen statistics and eigenparameters stored in ",rmag_res
Example #6
0
def main():
    """
    NAME
        aarm_magic.py

    DESCRIPTION
        Converts AARM  data to best-fit tensor (6 elements plus sigma)
         Original program ARMcrunch written to accomodate ARM anisotropy data
          collected from 6 axial directions (+X,+Y,+Z,-X,-Y,-Z) using the
          off-axis remanence terms to construct the tensor. A better way to
          do the anisotropy of ARMs is to use 9,12 or 15 measurements in
          the Hext rotational scheme.
    
    SYNTAX 
        aarm_magic.py [-h][command line options]

    OPTIONS
        -h prints help message and quits
        -usr USER:   identify user, default is ""
        -f FILE: specify input file, default is aarm_measurements.txt
        -crd [s,g,t] specify coordinate system, requires er_samples.txt file
        -fsa  FILE: specify er_samples.txt file, default is er_samples.txt
        -Fa FILE: specify anisotropy output file, default is arm_anisotropy.txt
        -Fr FILE: specify results output file, default is aarm_results.txt

    INPUT  
        Input for the present program is a series of baseline, ARM pairs.
      The baseline should be the AF demagnetized state (3 axis demag is
      preferable) for the following ARM acquisition. The order of the
      measurements is:
    
           positions 1,2,3, 6,7,8, 11,12,13 (for 9 positions)
           positions 1,2,3,4, 6,7,8,9, 11,12,13,14 (for 12 positions)
           positions 1-15 (for 15 positions)
    """
    # initialize some parameters
    args=sys.argv
    user=""
    meas_file="aarm_measurements.txt"
    samp_file="er_samples.txt"
    rmag_anis="arm_anisotropy.txt"
    rmag_res="aarm_results.txt"
    dir_path='.'
    #
    # get name of file from command line
    #
    if '-WD' in args:
        ind=args.index('-WD')
        dir_path=args[ind+1]
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-usr" in args:
        ind=args.index("-usr")
        user=sys.argv[ind+1]
    if "-f" in args:
        ind=args.index("-f")
        meas_file=sys.argv[ind+1]
    coord='-1'
    if "-crd" in sys.argv:
        ind=sys.argv.index("-crd")
        coord=sys.argv[ind+1]
        if coord=='s':coord='-1'
        if coord=='g':coord='0'
        if coord=='t':coord='100'
        if "-fsa" in args:
            ind=args.index("-fsa")
            samp_file=sys.argv[ind+1]
    if "-Fa" in args:
        ind=args.index("-Fa")
        rmag_anis=args[ind+1]
    if "-Fr" in args:
        ind=args.index("-Fr")
        rmag_res=args[ind+1]
    meas_file=dir_path+'/'+meas_file
    samp_file=dir_path+'/'+samp_file
    rmag_anis=dir_path+'/'+rmag_anis
    rmag_res=dir_path+'/'+rmag_res
    # read in data
    meas_data,file_type=pmag.magic_read(meas_file)
    meas_data=pmag.get_dictitem(meas_data,'magic_method_codes','LP-AN-ARM','has')
    if file_type != 'magic_measurements':
        print file_type
        print file_type,"This is not a valid magic_measurements file " 
        sys.exit()
    if coord!='-1': # need to read in sample data
        samp_data,file_type=pmag.magic_read(samp_file)
        if file_type != 'er_samples':
            print file_type
            print file_type,"This is not a valid er_samples file " 
            print "Only specimen coordinates will be calculated"
            coord='-1'
    #
    # sort the specimen names
    #
    ssort=[]
    for rec in meas_data:
      spec=rec["er_specimen_name"]
      if spec not in ssort: ssort.append(spec)
    if len(ssort)>1:
        sids=sorted(ssort)
    else:
        sids=ssort
    #
    # work on each specimen
    #
    specimen=0
    RmagSpecRecs,RmagResRecs=[],[]
    while specimen < len(sids):
        s=sids[specimen]
        data=[]
        RmagSpecRec={}
        RmagResRec={}
        method_codes=[]
    #
    # find the data from the meas_data file for this sample
    #
        data=pmag.get_dictitem(meas_data,'er_specimen_name',s,'T')
    #
    # find out the number of measurements (9, 12 or 15)
    #
        npos=len(data)/2
        if npos==9:
        #
        # get dec, inc, int and convert to x,y,z
        #
            B,H,tmpH=pmag.designAARM(npos)  # B matrix made from design matrix for positions
            X=[]
            for rec in data:
                Dir=[]
                Dir.append(float(rec["measurement_dec"]))
                Dir.append(float(rec["measurement_inc"]))
                Dir.append(float(rec["measurement_magn_moment"]))
                X.append(pmag.dir2cart(Dir))
        #
        # subtract baseline and put in a work array
        #
            work=numpy.zeros((npos,3),'f')
            for i in range(npos):
                for j in range(3):
                    work[i][j]=X[2*i+1][j]-X[2*i][j]
        #
        # calculate tensor elements
        # first put ARM components in w vector
        #
            w=numpy.zeros((npos*3),'f')
            index=0
            for i in range(npos):
                for j in range(3):
                    w[index]=work[i][j] 
                    index+=1
            s=numpy.zeros((6),'f') # initialize the s matrix
            for i in range(6):
                for j in range(len(w)):
                    s[i]+=B[i][j]*w[j] 
            trace=s[0]+s[1]+s[2]   # normalize by the trace
            for i in range(6):
                s[i]=s[i]/trace
            a=pmag.s2a(s)
        #------------------------------------------------------------
        #  Calculating dels is different than in the Kappabridge
        #  routine. Use trace normalized tensor (a) and the applied
        #  unit field directions (tmpH) to generate model X,Y,Z
        #  components. Then compare these with the measured values.
        #------------------------------------------------------------
            S=0.
            comp=numpy.zeros((npos*3),'f')
            for i in range(npos):
                for j in range(3):
                    index=i*3+j
                    compare=a[j][0]*tmpH[i][0]+a[j][1]*tmpH[i][1]+a[j][2]*tmpH[i][2]
                    comp[index]=compare
            for i in range(npos*3):
                d=w[i]/trace - comp[i] # del values
                S+=d*d
            nf=float(npos*3-6) # number of degrees of freedom
            if S >0: 
                sigma=numpy.sqrt(S/nf)
            else: sigma=0
            RmagSpecRec["rmag_anisotropy_name"]=data[0]["er_specimen_name"]
            RmagSpecRec["er_location_name"]=data[0]["er_location_name"]
            RmagSpecRec["er_specimen_name"]=data[0]["er_specimen_name"]
            RmagSpecRec["er_sample_name"]=data[0]["er_sample_name"]
            RmagSpecRec["er_site_name"]=data[0]["er_site_name"]
            RmagSpecRec["magic_experiment_names"]=RmagSpecRec["rmag_anisotropy_name"]+":AARM"
            RmagSpecRec["er_citation_names"]="This study"
            RmagResRec["rmag_result_name"]=data[0]["er_specimen_name"]+":AARM"
            RmagResRec["er_location_names"]=data[0]["er_location_name"]
            RmagResRec["er_specimen_names"]=data[0]["er_specimen_name"]
            RmagResRec["er_sample_names"]=data[0]["er_sample_name"]
            RmagResRec["er_site_names"]=data[0]["er_site_name"]
            RmagResRec["magic_experiment_names"]=RmagSpecRec["rmag_anisotropy_name"]+":AARM"
            RmagResRec["er_citation_names"]="This study"
            if "magic_instrument_codes" in data[0].keys():
                RmagSpecRec["magic_instrument_codes"]=data[0]["magic_instrument_codes"]
            else:  
                RmagSpecRec["magic_instrument_codes"]=""
            RmagSpecRec["anisotropy_type"]="AARM"
            RmagSpecRec["anisotropy_description"]="Hext statistics adapted to AARM"
            if coord!='-1': # need to rotate s
    # set orientation priorities
                SO_methods=[]
                for rec in samp_data:
                   if "magic_method_codes" not in rec:
                       rec['magic_method_codes']='SO-NO'
                   if "magic_method_codes" in rec:
                       methlist=rec["magic_method_codes"]
                       for meth in methlist.split(":"):
                           if "SO" in meth and "SO-POM" not in meth.strip():
                               if meth.strip() not in SO_methods: SO_methods.append(meth.strip())
                SO_priorities=pmag.set_priorities(SO_methods,0)
# continue here
                redo,p=1,0
                if len(SO_methods)<=1:
                    az_type=SO_methods[0]
                    orient=pmag.find_samp_rec(RmagSpecRec["er_sample_name"],samp_data,az_type)
                    if orient["sample_azimuth"]  !="": method_codes.append(az_type)
                    redo=0
                while redo==1:
                    if p>=len(SO_priorities):
                        print "no orientation data for ",s
                        orient["sample_azimuth"]=""
                        orient["sample_dip"]=""
                        method_codes.append("SO-NO")
                        redo=0
                    else:
                        az_type=SO_methods[SO_methods.index(SO_priorities[p])]
                        orient=pmag.find_samp_rec(PmagSpecRec["er_sample_name"],samp_data,az_type)
                        if orient["sample_azimuth"]  !="":
                            method_codes.append(az_type)
                            redo=0
                    p+=1
                az,pl=orient['sample_azimuth'],orient['sample_dip']
                s=pmag.dosgeo(s,az,pl) # rotate to geographic coordinates
                if coord=='100': 
                    sampe_bed_dir,sample_bed_dip=orient['sample_bed_dip_direction'],orient['sample_bed_dip']
                    s=pmag.dostilt(s,bed_dir,bed_dip) # rotate to geographic coordinates
            hpars=pmag.dohext(nf,sigma,s)
        #
        # prepare for output
        #
            RmagSpecRec["anisotropy_s1"]='%8.6f'%(s[0])
            RmagSpecRec["anisotropy_s2"]='%8.6f'%(s[1])
            RmagSpecRec["anisotropy_s3"]='%8.6f'%(s[2])
            RmagSpecRec["anisotropy_s4"]='%8.6f'%(s[3])
            RmagSpecRec["anisotropy_s5"]='%8.6f'%(s[4])
            RmagSpecRec["anisotropy_s6"]='%8.6f'%(s[5])
            RmagSpecRec["anisotropy_mean"]='%8.3e'%(trace/3)
            RmagSpecRec["anisotropy_sigma"]='%8.6f'%(sigma)
            RmagSpecRec["anisotropy_unit"]="Am^2"
            RmagSpecRec["anisotropy_n"]='%i'%(npos)
            RmagSpecRec["anisotropy_tilt_correction"]=coord
            RmagSpecRec["anisotropy_F"]='%7.1f '%(hpars["F"]) # used by thellier_gui - must be taken out for uploading
            RmagSpecRec["anisotropy_F_crit"]=hpars["F_crit"] # used by thellier_gui - must be taken out for uploading
            RmagResRec["anisotropy_t1"]='%8.6f '%(hpars["t1"])
            RmagResRec["anisotropy_t2"]='%8.6f '%(hpars["t2"])
            RmagResRec["anisotropy_t3"]='%8.6f '%(hpars["t3"])
            RmagResRec["anisotropy_v1_dec"]='%7.1f '%(hpars["v1_dec"])
            RmagResRec["anisotropy_v2_dec"]='%7.1f '%(hpars["v2_dec"])
            RmagResRec["anisotropy_v3_dec"]='%7.1f '%(hpars["v3_dec"])
            RmagResRec["anisotropy_v1_inc"]='%7.1f '%(hpars["v1_inc"])
            RmagResRec["anisotropy_v2_inc"]='%7.1f '%(hpars["v2_inc"])
            RmagResRec["anisotropy_v3_inc"]='%7.1f '%(hpars["v3_inc"])
            RmagResRec["anisotropy_ftest"]='%7.1f '%(hpars["F"])
            RmagResRec["anisotropy_ftest12"]='%7.1f '%(hpars["F12"])
            RmagResRec["anisotropy_ftest23"]='%7.1f '%(hpars["F23"])
            RmagResRec["result_description"]='Critical F: '+hpars["F_crit"]+';Critical F12/F13: '+hpars["F12_crit"]
            if hpars["e12"]>hpars["e13"]:
                RmagResRec["anisotropy_v1_zeta_semi_angle"]='%7.1f '%(hpars['e12'])
                RmagResRec["anisotropy_v1_zeta_dec"]='%7.1f '%(hpars['v2_dec'])
                RmagResRec["anisotropy_v1_zeta_inc"]='%7.1f '%(hpars['v2_inc'])
                RmagResRec["anisotropy_v2_zeta_semi_angle"]='%7.1f '%(hpars['e12'])
                RmagResRec["anisotropy_v2_zeta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v2_zeta_inc"]='%7.1f '%(hpars['v1_inc'])
                RmagResRec["anisotropy_v1_eta_semi_angle"]='%7.1f '%(hpars['e13'])
                RmagResRec["anisotropy_v1_eta_dec"]='%7.1f '%(hpars['v3_dec'])
                RmagResRec["anisotropy_v1_eta_inc"]='%7.1f '%(hpars['v3_inc'])
                RmagResRec["anisotropy_v3_eta_semi_angle"]='%7.1f '%(hpars['e13'])
                RmagResRec["anisotropy_v3_eta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v3_eta_inc"]='%7.1f '%(hpars['v1_inc'])
            else:
                RmagResRec["anisotropy_v1_zeta_semi_angle"]='%7.1f '%(hpars['e13'])
                RmagResRec["anisotropy_v1_zeta_dec"]='%7.1f '%(hpars['v3_dec'])
                RmagResRec["anisotropy_v1_zeta_inc"]='%7.1f '%(hpars['v3_inc'])
                RmagResRec["anisotropy_v3_zeta_semi_angle"]='%7.1f '%(hpars['e13'])
                RmagResRec["anisotropy_v3_zeta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v3_zeta_inc"]='%7.1f '%(hpars['v1_inc'])
                RmagResRec["anisotropy_v1_eta_semi_angle"]='%7.1f '%(hpars['e12'])
                RmagResRec["anisotropy_v1_eta_dec"]='%7.1f '%(hpars['v2_dec'])
                RmagResRec["anisotropy_v1_eta_inc"]='%7.1f '%(hpars['v2_inc'])
                RmagResRec["anisotropy_v2_eta_semi_angle"]='%7.1f '%(hpars['e12'])
                RmagResRec["anisotropy_v2_eta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v2_eta_inc"]='%7.1f '%(hpars['v1_inc'])
            if hpars["e23"]>hpars['e12']:
                RmagResRec["anisotropy_v2_zeta_semi_angle"]='%7.1f '%(hpars['e23'])
                RmagResRec["anisotropy_v2_zeta_dec"]='%7.1f '%(hpars['v3_dec'])
                RmagResRec["anisotropy_v2_zeta_inc"]='%7.1f '%(hpars['v3_inc'])
                RmagResRec["anisotropy_v3_zeta_semi_angle"]='%7.1f '%(hpars['e23'])
                RmagResRec["anisotropy_v3_zeta_dec"]='%7.1f '%(hpars['v2_dec'])
                RmagResRec["anisotropy_v3_zeta_inc"]='%7.1f '%(hpars['v2_inc'])
                RmagResRec["anisotropy_v3_eta_semi_angle"]='%7.1f '%(hpars['e13'])
                RmagResRec["anisotropy_v3_eta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v3_eta_inc"]='%7.1f '%(hpars['v1_inc'])
                RmagResRec["anisotropy_v2_eta_semi_angle"]='%7.1f '%(hpars['e12'])
                RmagResRec["anisotropy_v2_eta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v2_eta_inc"]='%7.1f '%(hpars['v1_inc'])
            else:
                RmagResRec["anisotropy_v2_zeta_semi_angle"]='%7.1f '%(hpars['e12'])
                RmagResRec["anisotropy_v2_zeta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v2_zeta_inc"]='%7.1f '%(hpars['v1_inc'])
                RmagResRec["anisotropy_v3_eta_semi_angle"]='%7.1f '%(hpars['e23'])
                RmagResRec["anisotropy_v3_eta_dec"]='%7.1f '%(hpars['v2_dec'])
                RmagResRec["anisotropy_v3_eta_inc"]='%7.1f '%(hpars['v2_inc'])
                RmagResRec["anisotropy_v3_zeta_semi_angle"]='%7.1f '%(hpars['e13'])
                RmagResRec["anisotropy_v3_zeta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v3_zeta_inc"]='%7.1f '%(hpars['v1_inc'])
                RmagResRec["anisotropy_v2_eta_semi_angle"]='%7.1f '%(hpars['e23'])
                RmagResRec["anisotropy_v2_eta_dec"]='%7.1f '%(hpars['v3_dec'])
                RmagResRec["anisotropy_v2_eta_inc"]='%7.1f '%(hpars['v3_inc'])
            RmagResRec["tilt_correction"]='-1'
            RmagResRec["anisotropy_type"]='AARM'
            RmagResRec["magic_method_codes"]='LP-AN-ARM:AE-H'
            RmagSpecRec["magic_method_codes"]='LP-AN-ARM:AE-H'
            RmagResRec["magic_software_packages"]=pmag.get_version()
            RmagSpecRec["magic_software_packages"]=pmag.get_version()
            specimen+=1
            RmagSpecRecs.append(RmagSpecRec)
            RmagResRecs.append(RmagResRec)
        else:
            print 'skipping specimen ',s,' only 9 positions supported','; this has ',npos
            specimen+=1
    if rmag_anis=="":rmag_anis="rmag_anisotropy.txt"
    pmag.magic_write(rmag_anis,RmagSpecRecs,'rmag_anisotropy')
    print "specimen tensor elements stored in ",rmag_anis
    if rmag_res=="":rmag_res="rmag_results.txt"
    pmag.magic_write(rmag_res,RmagResRecs,'rmag_results')
    print "specimen statistics and eigenparameters stored in ",rmag_res
Example #7
0
def main():
    """
    NAME
        s_hext.py

    DESCRIPTION
     calculates Hext statistics for tensor data

    SYNTAX
        s_hext.py [-h][-i][-f file] [<filename]

    OPTIONS
        -h prints help message and quits
        -f file specifies filename on command line
        -l NMEAS do line by line instead of whole file, use number of measurements NMEAS for degrees of freedom
        < filename, reads from standard input (Unix like operating systems only)

    INPUT
        x11,x22,x33,x12,x23,x13,sigma [sigma only if line by line]

    OUTPUT
       F  F12  F23  sigma
       and three sets of:
        tau dec inc Eij dec inc Eik dec inc
    
    DEFAULT
       average whole file
    """
    ave=1
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-l' in sys.argv:
        ind=sys.argv.index('-l')
        npts=int(sys.argv[ind+1])
        ave=0
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        file=sys.argv[ind+1]
        f=open(file,'rU')
        data=f.readlines()
        f.close()
    else:
        data=sys.stdin.readlines()
    Ss=[]
    for line in data:
        s=[]
        rec=line.split()
        for i in range(6):
            s.append(float(rec[i]))
        if ave==0:
            sig=float(rec[6])
            hpars=pmag.dohext(npts-6,sig,s)
            print '%s %4.2f %s %4.2f %s %4.2f'%('F = ',hpars['F'],'F12 = ',hpars['F12'],'F23 = ',hpars['F23'])
            print '%s %i %s %14.12f'%('Nmeas = ',npts,' sigma = ',sig)
            print '%7.5f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f'%(hpars["t1"],hpars["v1_dec"],hpars["v1_inc"],hpars["e12"],hpars["v2_dec"],hpars["v2_inc"],hpars["e13"],hpars["v3_dec"],hpars["v3_inc"] )
            print '%7.5f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f'%(hpars["t2"],hpars["v2_dec"],hpars["v2_inc"],hpars["e23"],hpars["v3_dec"],hpars["v3_inc"],hpars["e12"],hpars["v1_dec"],hpars["v1_inc"] )
            print '%7.5f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f'%(hpars["t3"],hpars["v3_dec"],hpars["v3_inc"],hpars["e13"],hpars["v1_dec"],hpars["v1_inc"],hpars["e23"],hpars["v2_dec"],hpars["v2_inc"] )
        else:
            Ss.append(s)
    if ave==1:
        npts=len(Ss)
        nf,sigma,avs=pmag.sbar(Ss)
        hpars=pmag.dohext(nf,sigma,avs)
        print '%s %4.2f %s %4.2f %s %4.2f'%('F = ',hpars['F'],'F12 = ',hpars['F12'],'F23 = ',hpars['F23'])
        print '%s %i %s %14.12f'%('N = ',npts,' sigma = ',sigma)
        print '%7.5f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f'%(hpars["t1"],hpars["v1_dec"],hpars["v1_inc"],hpars["e12"],hpars["v2_dec"],hpars["v2_inc"],hpars["e13"],hpars["v3_dec"],hpars["v3_inc"] )
        print '%7.5f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f'%(hpars["t2"],hpars["v2_dec"],hpars["v2_inc"],hpars["e23"],hpars["v3_dec"],hpars["v3_inc"],hpars["e12"],hpars["v1_dec"],hpars["v1_inc"] )
        print '%7.5f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f'%(hpars["t3"],hpars["v3_dec"],hpars["v3_inc"],hpars["e13"],hpars["v1_dec"],hpars["v1_inc"],hpars["e23"],hpars["v2_dec"],hpars["v2_inc"] )
 def calculate_aniso_parameters(B,K):
 
     aniso_parameters={}
     S_bs=dot(B,K)
     
     # normalize by trace
     trace=S_bs[0]+S_bs[1]+S_bs[2]
     S_bs=S_bs/trace
     s1,s2,s3,s4,s5,s6=S_bs[0],S_bs[1],S_bs[2],S_bs[3],S_bs[4],S_bs[5]
     s_matrix=[[s1,s4,s6],[s4,s2,s5],[s6,s5,s3]]
     
     # calculate eigen vector,
     t,evectors=eig(s_matrix)
     # sort vectors
     t=list(t)
     t1=max(t)
     ix_1=t.index(t1)
     t3=min(t)
     ix_3=t.index(t3)
     for tt in range(3):
         if t[tt]!=t1 and t[tt]!=t3:
             t2=t[tt]
             ix_2=t.index(t2)
             
     v1=[evectors[0][ix_1],evectors[1][ix_1],evectors[2][ix_1]]
     v2=[evectors[0][ix_2],evectors[1][ix_2],evectors[2][ix_2]]
     v3=[evectors[0][ix_3],evectors[1][ix_3],evectors[2][ix_3]]
 
 
     DIR_v1=pmag.cart2dir(v1)
     DIR_v2=pmag.cart2dir(v2)
     DIR_v3=pmag.cart2dir(v3)
 
                         
     aniso_parameters['anisotropy_s1']="%f"%s1
     aniso_parameters['anisotropy_s2']="%f"%s2
     aniso_parameters['anisotropy_s3']="%f"%s3
     aniso_parameters['anisotropy_s4']="%f"%s4
     aniso_parameters['anisotropy_s5']="%f"%s5
     aniso_parameters['anisotropy_s6']="%f"%s6
     aniso_parameters['anisotropy_degree']="%f"%(t1/t3)
     aniso_parameters['anisotropy_t1']="%f"%t1
     aniso_parameters['anisotropy_t2']="%f"%t2
     aniso_parameters['anisotropy_t3']="%f"%t3
     aniso_parameters['anisotropy_v1_dec']="%.1f"%DIR_v1[0]
     aniso_parameters['anisotropy_v1_inc']="%.1f"%DIR_v1[1]
     aniso_parameters['anisotropy_v2_dec']="%.1f"%DIR_v2[0]
     aniso_parameters['anisotropy_v2_inc']="%.1f"%DIR_v2[1]
     aniso_parameters['anisotropy_v3_dec']="%.1f"%DIR_v3[0]
     aniso_parameters['anisotropy_v3_inc']="%.1f"%DIR_v3[1]
 
     # modified from pmagpy:
     if len(K)/3==9 or len(K)/3==6 or len(K)/3==15:
         n_pos=len(K)/3
         tmpH = Matrices[n_pos]['tmpH']
         a=s_matrix
         S=0.
         comp=zeros((n_pos*3),'f')
         for i in range(n_pos):
             for j in range(3):
                 index=i*3+j
                 compare=a[j][0]*tmpH[i][0]+a[j][1]*tmpH[i][1]+a[j][2]*tmpH[i][2]
                 comp[index]=compare
         for i in range(n_pos*3):
             d=K[i]/trace - comp[i] # del values
             S+=d*d
         nf=float(n_pos*3-6) # number of degrees of freedom
         if S >0: 
             sigma=math.sqrt(S/nf)
         hpars=pmag.dohext(nf,sigma,[s1,s2,s3,s4,s5,s6])
         
         aniso_parameters['anisotropy_sigma']="%f"%sigma
         aniso_parameters['anisotropy_ftest']="%f"%hpars["F"]
         aniso_parameters['anisotropy_ftest12']="%f"%hpars["F12"]
         aniso_parameters['anisotropy_ftest23']="%f"%hpars["F23"]
         aniso_parameters['result_description']="Critical F: %s"%(hpars['F_crit'])
         aniso_parameters['anisotropy_F_crit']="%f"%float(hpars['F_crit'])
         aniso_parameters['anisotropy_n']=n_pos
         
     return(aniso_parameters)
Example #9
0
def main():
    """
    NAME
        atrm_magic.py

    DESCRIPTION
        Converts ATRM  data to best-fit tensor (6 elements plus sigma)
         Original program ARMcrunch written to accomodate ARM anisotropy data
          collected from 6 axial directions (+X,+Y,+Z,-X,-Y,-Z) using the
          off-axis remanence terms to construct the tensor. A better way to
          do the anisotropy of ARMs is to use 9,12 or 15 measurements in
          the Hext rotational scheme.
    
    SYNTAX 
        atrm_magic.py [-h][command line options]

    OPTIONS
        -h prints help message and quits
        -usr USER:   identify user, default is ""
        -f FILE: specify input file, default is atrm_measurements.txt
        -Fa FILE: specify anisotropy output file, default is trm_anisotropy.txt
        -Fr FILE: specify results output file, default is atrm_results.txt

    INPUT  
        Input for the present program is a TRM acquisition data with an optional baseline.
      The order of the measurements is:
    Decs=[0,90,0,180,270,0,0,90,0]
    Incs=[0,0,90,0,0,-90,0,0,90]
     The last two measurements are optional
    
    """
    # initialize some parameters
    args=sys.argv
    user=""
    meas_file="atrm_measurements.txt"
    rmag_anis="trm_anisotropy.txt"
    rmag_res="atrm_results.txt"
    dir_path='.'
    #
    # get name of file from command line
    #
    if '-WD' in args:
        ind=args.index('-WD')
        dir_path=args[ind+1]
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-usr" in args:
        ind=args.index("-usr")
        user=sys.argv[ind+1]
    if "-f" in args:
        ind=args.index("-f")
        meas_file=sys.argv[ind+1]
    if "-Fa" in args:
        ind=args.index("-Fa")
        rmag_anis=args[ind+1]
    if "-Fr" in args:
        ind=args.index("-Fr")
        rmag_res=args[ind+1]
    meas_file=dir_path+'/'+meas_file
    rmag_anis=dir_path+'/'+rmag_anis
    rmag_res=dir_path+'/'+rmag_res
    # read in data
    meas_data,file_type=pmag.magic_read(meas_file)
    meas_data=pmag.get_dictitem(meas_data,'magic_method_codes','LP-AN-TRM','has')
    if file_type != 'magic_measurements':
        print file_type
        print file_type,"This is not a valid magic_measurements file " 
        sys.exit()
    #
    #
    # get sorted list of unique specimen names
    ssort=[]
    for rec in meas_data:
      spec=rec["er_specimen_name"]
      if spec not in ssort:ssort.append(spec)
    sids=sorted(ssort)
    #
    #
    # work on each specimen
    #
    specimen,npos=0,6
    RmagSpecRecs,RmagResRecs=[],[]
    while specimen < len(sids):
        nmeas=0 
        s=sids[specimen]
        RmagSpecRec={}
        RmagResRec={}
        BX,X=[],[]
        method_codes=[]
        Spec0=""
    #
    # find the data from the meas_data file for this sample
        # and get dec, inc, int and convert to x,y,z
        #
        data=pmag.get_dictitem(meas_data,'er_specimen_name',s,'T') # fish out data for this specimen name
        if len(data)>5:
            RmagSpecRec["rmag_anisotropy_name"]=data[0]["er_specimen_name"]
            RmagSpecRec["er_location_name"]=data[0]["er_location_name"]
            RmagSpecRec["er_specimen_name"]=data[0]["er_specimen_name"]
            RmagSpecRec["er_sample_name"]=data[0]["er_sample_name"]
            RmagSpecRec["er_site_name"]=data[0]["er_site_name"]
            RmagSpecRec["magic_experiment_names"]=RmagSpecRec["rmag_anisotropy_name"]+":ATRM"
            RmagSpecRec["er_citation_names"]="This study"
            RmagResRec["rmag_result_name"]=data[0]["er_specimen_name"]+":ATRM"
            RmagResRec["er_location_names"]=data[0]["er_location_name"]
            RmagResRec["er_specimen_names"]=data[0]["er_specimen_name"]
            RmagResRec["er_sample_names"]=data[0]["er_sample_name"]
            RmagResRec["er_site_names"]=data[0]["er_site_name"]
            RmagResRec["magic_experiment_names"]=RmagSpecRec["rmag_anisotropy_name"]+":ATRM"
            RmagResRec["er_citation_names"]="This study"
            RmagSpecRec["anisotropy_type"]="ATRM"
            if "magic_instrument_codes" in data[0].keys():
                RmagSpecRec["magic_instrument_codes"]=data[0]["magic_instrument_codes"]
            else:  
                RmagSpecRec["magic_instrument_codes"]=""
                RmagSpecRec["anisotropy_description"]="Hext statistics adapted to ATRM"
            for rec in data:
                meths=rec['magic_method_codes'].strip().split(':')
                Dir=[]
                Dir.append(float(rec["measurement_dec"]))
                Dir.append(float(rec["measurement_inc"]))
                Dir.append(float(rec["measurement_magn_moment"]))
                if "LT-T-Z" in meths:
                    BX.append(pmag.dir2cart(Dir)) # append baseline steps
                elif "LT-T-I" in meths: 
                    X.append(pmag.dir2cart(Dir))
                    nmeas+=1
    #
        if len(BX)==1:
            for i in range(len(X)-1):BX.append(BX[0]) # assume first 0 field step as baseline
        elif len(BX)== 0: # assume baseline is zero
            for i in range(len(X)):BX.append([0.,0.,0.]) # assume baseline of 0
        elif len(BX)!= len(X): # if BX isn't just one measurement or one in between every infield step, just assume it is zero
            print 'something odd about the baselines - just assuming zero'
            for i in range(len(X)):BX.append([0.,0.,0.]) # assume baseline of 0
        if nmeas<6: # must have at least 6 measurements right now - 
            print 'skipping specimen ',s,' too few measurements'
            specimen+=1
        else:
            B,H,tmpH=pmag.designATRM(npos)  # B matrix made from design matrix for positions
        #
        # subtract optional baseline and put in a work array
        #
            work=numpy.zeros((nmeas,3),'f')
            for i in range(nmeas):
                for j in range(3):
                    work[i][j]=X[i][j]-BX[i][j] # subtract baseline, if available
        #
        # calculate tensor elements
        # first put ARM components in w vector
        #
            w=numpy.zeros((npos*3),'f')
            index=0
            for i in range(npos):
                for j in range(3):
                    w[index]=work[i][j] 
                    index+=1
            s=numpy.zeros((6),'f') # initialize the s matrix
            for i in range(6):
                for j in range(len(w)):
                    s[i]+=B[i][j]*w[j] 
            trace=s[0]+s[1]+s[2]   # normalize by the trace
            for i in range(6):
                s[i]=s[i]/trace
            a=pmag.s2a(s)
            
        #------------------------------------------------------------
        #  Calculating dels is different than in the Kappabridge
        #  routine. Use trace normalized tensor (a) and the applied
        #  unit field directions (tmpH) to generate model X,Y,Z
        #  components. Then compare these with the measured values.
        #------------------------------------------------------------
            S=0.
            comp=numpy.zeros((npos*3),'f')
            for i in range(npos):
                for j in range(3):
                    index=i*3+j
                    compare=a[j][0]*tmpH[i][0]+a[j][1]*tmpH[i][1]+a[j][2]*tmpH[i][2]
                    comp[index]=compare
            for i in range(npos*3):
                d=w[i]/trace - comp[i] # del values
                S+=d*d
            nf=float(npos*3.-6.) # number of degrees of freedom
            if S >0: 
                sigma=numpy.sqrt(S/nf)
            else: sigma=0
            hpars=pmag.dohext(nf,sigma,s)
        #
        # prepare for output
        #
            RmagSpecRec["anisotropy_s1"]='%8.6f'%(s[0])
            RmagSpecRec["anisotropy_s2"]='%8.6f'%(s[1])
            RmagSpecRec["anisotropy_s3"]='%8.6f'%(s[2])
            RmagSpecRec["anisotropy_s4"]='%8.6f'%(s[3])
            RmagSpecRec["anisotropy_s5"]='%8.6f'%(s[4])
            RmagSpecRec["anisotropy_s6"]='%8.6f'%(s[5])
            RmagSpecRec["anisotropy_mean"]='%8.3e'%(trace/3)
            RmagSpecRec["anisotropy_sigma"]='%8.6f'%(sigma)
            RmagSpecRec["anisotropy_unit"]="Am^2"
            RmagSpecRec["anisotropy_n"]='%i'%(npos)
            RmagSpecRec["anisotropy_tilt_correction"]='-1'
            RmagSpecRec["anisotropy_F"]='%7.1f '%(hpars["F"]) # used by thellier_gui - must be taken out for uploading
            RmagSpecRec["anisotropy_F_crit"]=hpars["F_crit"] # used by thellier_gui - must be taken out for uploading
            RmagResRec["anisotropy_t1"]='%8.6f '%(hpars["t1"])
            RmagResRec["anisotropy_t2"]='%8.6f '%(hpars["t2"])
            RmagResRec["anisotropy_t3"]='%8.6f '%(hpars["t3"])
            RmagResRec["anisotropy_v1_dec"]='%7.1f '%(hpars["v1_dec"])
            RmagResRec["anisotropy_v2_dec"]='%7.1f '%(hpars["v2_dec"])
            RmagResRec["anisotropy_v3_dec"]='%7.1f '%(hpars["v3_dec"])
            RmagResRec["anisotropy_v1_inc"]='%7.1f '%(hpars["v1_inc"])
            RmagResRec["anisotropy_v2_inc"]='%7.1f '%(hpars["v2_inc"])
            RmagResRec["anisotropy_v3_inc"]='%7.1f '%(hpars["v3_inc"])
            RmagResRec["anisotropy_ftest"]='%7.1f '%(hpars["F"])
            RmagResRec["anisotropy_ftest12"]='%7.1f '%(hpars["F12"])
            RmagResRec["anisotropy_ftest23"]='%7.1f '%(hpars["F23"])
            RmagResRec["result_description"]='Critical F: '+hpars["F_crit"]+';Critical F12/F13: '+hpars["F12_crit"]
            if hpars["e12"]>hpars["e13"]:
                RmagResRec["anisotropy_v1_zeta_semi_angle"]='%7.1f '%(hpars['e12'])
                RmagResRec["anisotropy_v1_zeta_dec"]='%7.1f '%(hpars['v2_dec'])
                RmagResRec["anisotropy_v1_zeta_inc"]='%7.1f '%(hpars['v2_inc'])
                RmagResRec["anisotropy_v2_zeta_semi_angle"]='%7.1f '%(hpars['e12'])
                RmagResRec["anisotropy_v2_zeta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v2_zeta_inc"]='%7.1f '%(hpars['v1_inc'])
                RmagResRec["anisotropy_v1_eta_semi_angle"]='%7.1f '%(hpars['e13'])
                RmagResRec["anisotropy_v1_eta_dec"]='%7.1f '%(hpars['v3_dec'])
                RmagResRec["anisotropy_v1_eta_inc"]='%7.1f '%(hpars['v3_inc'])
                RmagResRec["anisotropy_v3_eta_semi_angle"]='%7.1f '%(hpars['e13'])
                RmagResRec["anisotropy_v3_eta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v3_eta_inc"]='%7.1f '%(hpars['v1_inc'])
            else:
                RmagResRec["anisotropy_v1_zeta_semi_angle"]='%7.1f '%(hpars['e13'])
                RmagResRec["anisotropy_v1_zeta_dec"]='%7.1f '%(hpars['v3_dec'])
                RmagResRec["anisotropy_v1_zeta_inc"]='%7.1f '%(hpars['v3_inc'])
                RmagResRec["anisotropy_v3_zeta_semi_angle"]='%7.1f '%(hpars['e13'])
                RmagResRec["anisotropy_v3_zeta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v3_zeta_inc"]='%7.1f '%(hpars['v1_inc'])
                RmagResRec["anisotropy_v1_eta_semi_angle"]='%7.1f '%(hpars['e12'])
                RmagResRec["anisotropy_v1_eta_dec"]='%7.1f '%(hpars['v2_dec'])
                RmagResRec["anisotropy_v1_eta_inc"]='%7.1f '%(hpars['v2_inc'])
                RmagResRec["anisotropy_v2_eta_semi_angle"]='%7.1f '%(hpars['e12'])
                RmagResRec["anisotropy_v2_eta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v2_eta_inc"]='%7.1f '%(hpars['v1_inc'])
            if hpars["e23"]>hpars['e12']:
                RmagResRec["anisotropy_v2_zeta_semi_angle"]='%7.1f '%(hpars['e23'])
                RmagResRec["anisotropy_v2_zeta_dec"]='%7.1f '%(hpars['v3_dec'])
                RmagResRec["anisotropy_v2_zeta_inc"]='%7.1f '%(hpars['v3_inc'])
                RmagResRec["anisotropy_v3_zeta_semi_angle"]='%7.1f '%(hpars['e23'])
                RmagResRec["anisotropy_v3_zeta_dec"]='%7.1f '%(hpars['v2_dec'])
                RmagResRec["anisotropy_v3_zeta_inc"]='%7.1f '%(hpars['v2_inc'])
                RmagResRec["anisotropy_v3_eta_semi_angle"]='%7.1f '%(hpars['e13'])
                RmagResRec["anisotropy_v3_eta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v3_eta_inc"]='%7.1f '%(hpars['v1_inc'])
                RmagResRec["anisotropy_v2_eta_semi_angle"]='%7.1f '%(hpars['e12'])
                RmagResRec["anisotropy_v2_eta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v2_eta_inc"]='%7.1f '%(hpars['v1_inc'])
            else:
                RmagResRec["anisotropy_v2_zeta_semi_angle"]='%7.1f '%(hpars['e12'])
                RmagResRec["anisotropy_v2_zeta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v2_zeta_inc"]='%7.1f '%(hpars['v1_inc'])
                RmagResRec["anisotropy_v3_eta_semi_angle"]='%7.1f '%(hpars['e23'])
                RmagResRec["anisotropy_v3_eta_dec"]='%7.1f '%(hpars['v2_dec'])
                RmagResRec["anisotropy_v3_eta_inc"]='%7.1f '%(hpars['v2_inc'])
                RmagResRec["anisotropy_v3_zeta_semi_angle"]='%7.1f '%(hpars['e13'])
                RmagResRec["anisotropy_v3_zeta_dec"]='%7.1f '%(hpars['v1_dec'])
                RmagResRec["anisotropy_v3_zeta_inc"]='%7.1f '%(hpars['v1_inc'])
                RmagResRec["anisotropy_v2_eta_semi_angle"]='%7.1f '%(hpars['e23'])
                RmagResRec["anisotropy_v2_eta_dec"]='%7.1f '%(hpars['v3_dec'])
                RmagResRec["anisotropy_v2_eta_inc"]='%7.1f '%(hpars['v3_inc'])
            RmagResRec["tilt_correction"]='-1'
            RmagResRec["anisotropy_type"]='ATRM'
            RmagResRec["magic_method_codes"]='LP-AN-TRM:AE-H'
            RmagSpecRec["magic_method_codes"]='LP-AN-TRM:AE-H'
            RmagResRec["magic_software_packages"]=pmag.get_version()
            RmagSpecRec["magic_software_packages"]=pmag.get_version()
            RmagSpecRecs.append(RmagSpecRec)
            RmagResRecs.append(RmagResRec)
            specimen+=1
    pmag.magic_write(rmag_anis,RmagSpecRecs,'rmag_anisotropy')
    print "specimen tensor elements stored in ",rmag_anis
    pmag.magic_write(rmag_res,RmagResRecs,'rmag_results')
    print "specimen statistics and eigenparameters stored in ",rmag_res