Example #1
0
    def __init__(self, WD=None, name='Main Frame'):
        try:
            version= pmag.get_version()
        except:
            version = ""
        title = "MakeMagIC   version: %s"%version
        wx.Frame.__init__(self, None, wx.ID_ANY, title, name=name)
        #
        self.grid_frame = None
        self.panel = wx.Panel(self, size=wx.GetDisplaySize(), name='main panel')
        print '-I- Fetching working directory'
        self.WD = os.path.realpath(WD) or os.getcwd()

        print '-I- Initializing magic data object'
        self.data_model = validate_upload.get_data_model()
        self.er_magic = builder.ErMagicBuilder(self.WD, self.data_model)
        self.edited = False
        self.validation_mode = False

        # initialize magic data object
        # attempt to read magic_measurements.txt, and all er_* and pmag_* files
        print '-I- Read in any available data from working directory'
        self.er_magic.get_all_magic_info()
        
        # POSSIBLY RELOCATE THIS EVENTUALLY:
        print '-I- Initializing headers'
        self.er_magic.init_default_headers()
        self.er_magic.init_actual_headers()
        #
        print '-I- Initializing interface'
        self.InitUI()
Example #2
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 #3
0
def main():
    """
    NAME
        core_depthplot.py

    DESCRIPTION
        plots various measurements versus core_depth or age.  plots data flagged as 'FS-SS-C' as discrete samples.

    SYNTAX
        core_depthplot.py [command line optins]

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input magic_measurments format file from magi
        -fsum FILE: specify input LIMS database (IODP) core summary csv file
        -fwig FILE: specify input depth,wiggle to plot, in magic format with sample_core_depth key for depth
        -fsa FILE: specify input er_samples format file from magic for depth
        -fa FILE: specify input er_ages format file from magic for age
              NB: must have either -fsa OR -fa (not both)
        -fsp FILE sym size: specify input zeq_specimen format file from magic, sym and size
              NB: PCAs will have specified color, while fisher means will be white with specified color as the edgecolor
        -fres FILE specify input pmag_results file from magic, sym and size
        -LP [AF,T,ARM,IRM, X] step [in mT,C,mT,mT, mass/vol] to plot
        -S do not plot blanket treatment data (if this is set, you don't need the -LP)
        -sym SYM SIZE, symbol, size for continuous points (e.g., ro 5, bs 10, g^ 10 for red dot, blue square, green triangle), default is blue dot at 5 pt
        -D do not plot declination
        -M do not plot magnetization
        -log  plot magnetization  on a log scale
        -L do not connect dots with a line
        -I do not plot inclination
        -d min max [in m] depth range to plot
        -n normalize by weight in er_specimen table
        -Iex: plot the expected inc at lat - only available for results with lat info in file
        -ts TS amin amax: plot the GPTS for the time interval between amin and amax (numbers in Ma)
           TS: [ck95, gts04, gts12]
        -ds [mbsf,mcd] specify depth scale, mbsf default
        -fmt [svg, eps, pdf, png] specify output format for plot (default: svg)
        -sav save plot silently

     DEFAULTS:
         Measurements file: magic_measurements.txt
         Samples file: er_samples.txt
         NRM step
         Summary file: none
    """
    meas_file='magic_measurements.txt'
    intlist=['measurement_magnitude','measurement_magn_moment','measurement_magn_volume','measurement_magn_mass']
    samp_file='er_samples.txt'
    depth_scale='sample_core_depth'
    wt_file=''
    verbose=pmagplotlib.verbose
    width=10
    sym,size='bo',5
    Ssym,Ssize='cs',5
    method,fmt="LT-NO",'.svg'
    step=0
    pcol=3
    pel=3
    pltD,pltI,pltM,pltL,pltS=1,1,1,1,1
    logit=0
    maxInt=-1000
    minInt=1e10
    maxSuc=-1000
    minSuc=10000
    plotexp,pTS=0,0
    dir_path="."
    sum_file=""
    suc_file=""
    age_file=""
    spc_file=""
    res_file=""
    ngr_file=""
    wig_file=""
    title,location="",""
    if '-WD' in sys.argv:
        ind=sys.argv.index('-WD')
        dir_path=sys.argv[ind+1]
    norm=0
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-L' in sys.argv:
        pltL=0
    if '-S' in sys.argv: pltS=0 # don't plot the bulk measurements at all
    if '-D' in sys.argv:
        pltD=0
        pcol-=1
        pel-=1
        width-=2
    if '-I' in sys.argv:
        pltI=0
        pcol-=1
        pel-=1
        width-=2
    if '-M' in sys.argv:
        pltM=0
        pcol-=1
        pel-=1
        width-=2
    if '-log' in sys.argv:logit=1
    if '-ds' in sys.argv and 'mcd' in sys.argv:depth_scale='sample_composite_depth'
    if '-sym' in sys.argv:
        ind=sys.argv.index('-sym')
        sym=sys.argv[ind+1]
        size=float(sys.argv[ind+2])
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        meas_file=sys.argv[ind+1]
    if '-fsa' in sys.argv:
        ind=sys.argv.index('-fsa')
        samp_file=sys.argv[ind+1]
        if '-fa' in sys.argv:
            print(main.__doc__)
            print('only -fsa OR -fa - not both')
            sys.exit()
    elif '-fa' in sys.argv:
        ind=sys.argv.index('-fa')
        age_file=sys.argv[ind+1]
    if '-fsp' in sys.argv:
        ind=sys.argv.index('-fsp')
        spc_file=dir_path+'/'+sys.argv[ind+1]
        spc_sym=sys.argv[ind+2]
        spc_size=float(sys.argv[ind+3])
    if '-fres' in sys.argv:
        ind=sys.argv.index('-fres')
        res_file=dir_path+'/'+sys.argv[ind+1]
        res_sym=sys.argv[ind+2]
        res_size=float(sys.argv[ind+3])
    if '-fwig' in sys.argv:
        ind=sys.argv.index('-fwig')
        wig_file=dir_path+'/'+sys.argv[ind+1]
        pcol+=1
        width+=2
    if '-fsum' in sys.argv:
        ind=sys.argv.index('-fsum')
        sum_file=dir_path+'/'+sys.argv[ind+1]
    if '-fmt' in sys.argv:
        ind=sys.argv.index('-fmt')
        fmt='.'+sys.argv[ind+1]
    if '-sav' in sys.argv:
        plots=1
        verbose=0
    if '-LP' in sys.argv:
        ind=sys.argv.index('-LP')
        meth=sys.argv[ind+1]
        if meth=="AF":
            step=round(float(sys.argv[ind+2])*1e-3,6)
            method='LT-AF-Z'
        elif meth== 'T':
            step=round(float(sys.argv[ind+2])+273,6)
            method='LT-T-Z'
        elif meth== 'ARM':
            method='LT-AF-I'
            step=round(float(sys.argv[ind+2])*1e-3,6)
        elif meth== 'IRM':
            method='LT-IRM'
            step=round(float(sys.argv[ind+2])*1e-3,6)
        elif meth== 'X':
            method='LP-X'
            pcol+=1
            if sys.argv[ind+2]=='mass':
                suc_key='measurement_chi_mass'
            elif sys.argv[ind+2]=='vol':
                suc_key='measurement_chi_volume'
            else:
                print('error in susceptibility units')
                sys.exit()
        else:
           print('method not supported')
           sys.exit()
    if '-n' in sys.argv:
        ind=sys.argv.index('-n')
        wt_file=dir_path+'/'+sys.argv[ind+1]
        norm=1
    dmin,dmax=-1,-1
    if '-d' in sys.argv:
        ind=sys.argv.index('-d')
        dmin=float(sys.argv[ind+1])
        dmax=float(sys.argv[ind+2])
    if '-ts' in sys.argv:
        ind=sys.argv.index('-ts')
        ts=sys.argv[ind+1]
        amin=float(sys.argv[ind+2])
        amax=float(sys.argv[ind+3])
        pTS=1
        pcol+=1
        width+=2
    #
    #
    # get data read in
    meas_file=dir_path+'/'+meas_file
    if age_file=="":
        samp_file=dir_path+'/'+samp_file
        Samps,file_type=pmag.magic_read(samp_file)
    else:
        depth_scale='age'
        age_file=dir_path+'/'+age_file
        Samps,file_type=pmag.magic_read(age_file)
        age_unit=""
    if spc_file!="":Specs,file_type=pmag.magic_read(spc_file)
    if res_file!="":Results,file_type=pmag.magic_read(res_file)
    if norm==1:
        ErSpecs,file_type=pmag.magic_read(wt_file)
        print(len(ErSpecs), ' specimens read in from ',wt_file)
    Cores=[]
    core_depth_key="Top depth cored CSF (m)"
    if sum_file!="":
        input=open(sum_file,'r').readlines()
        if "Core Summary" in input[0]:
            headline=1
        else:
            headline=0
        keys=input[headline].replace('\n','').split(',')
        if "Core Top (m)" in keys:core_depth_key="Core Top (m)"
        if "Core Label" in keys:core_label_key="Core Label"
        if "Core label" in keys:core_label_key="Core label"
        for line in input[2:]:
            if 'TOTALS' not in line:
                CoreRec={}
                for k in range(len(keys)):CoreRec[keys[k]]=line.split(',')[k]
                Cores.append(CoreRec)
        if len(Cores)==0:
            print('no Core depth information available: import core summary file')
            sum_file=""
    Data=[]
    if depth_scale=='sample_core_depth':
        ylab="Depth (mbsf)"
    elif depth_scale=='age':
        ylab="Age"
    else:
        ylab="Depth (mcd)"
    # collect the data for plotting declination
    Depths,Decs,Incs,Ints=[],[],[],[]
    SDepths,SDecs,SIncs,SInts=[],[],[],[]
    SSucs=[]
    samples=[]
    methods,steps,m2=[],[],[]
    if pltS: # plot the bulk measurement data
        Meas,file_type=pmag.magic_read(meas_file)
        meas_key='measurement_magn_moment'
        print(len(Meas), ' measurements read in from ',meas_file)
        for m in intlist: # find the intensity key with data
            meas_data=pmag.get_dictitem(Meas,m,'','F') # get all non-blank data for this specimen
            if len(meas_data)>0:
                meas_key=m
                break
        m1=pmag.get_dictitem(Meas,'magic_method_codes',method,'has') # fish out the desired method code
        if method=='LT-T-Z':
            m2=pmag.get_dictitem(m1,'treatment_temp',str(step),'eval') # fish out the desired step
        elif 'LT-AF' in method:
            m2=pmag.get_dictitem(m1,'treatment_ac_field',str(step),'eval')
        elif 'LT-IRM' in method:
            m2=pmag.get_dictitem(m1,'treatment_dc_field',str(step),'eval')
        elif 'LT-X' in method:
            m2=pmag.get_dictitem(m1,suc_key,'','F')
        if len(m2)>0:
          for rec in m2: # fish out depths and weights
            D=pmag.get_dictitem(Samps,'er_sample_name',rec['er_sample_name'],'T')
            if not D:  # if using an age_file, you may need to sort by site
                D=pmag.get_dictitem(Samps,'er_site_name',rec['er_site_name'],'T')
            depth=pmag.get_dictitem(D,depth_scale,'','F')
            if len(depth)>0:
                if ylab=='Age': ylab=ylab+' ('+depth[0]['age_unit']+')' # get units of ages - assume they are all the same!

                rec['core_depth'] = float(depth[0][depth_scale])
                rec['magic_method_codes'] = rec['magic_method_codes']+':'+depth[0]['magic_method_codes']
                if norm==1:
                    specrecs=pmag.get_dictitem(ErSpecs,'er_specimen_name',rec['er_specimen_name'],'T')
                    specwts=pmag.get_dictitem(specrecs,'specimen_weight',"",'F')
                    if len(specwts)>0:
                        rec['specimen_weight'] = specwts[0]['specimen_weight']
                        Data.append(rec) # fish out data with core_depth and (if needed) weights
                else:
                    Data.append(rec) # fish out data with core_depth and (if needed) weights
                if title=="":
                   pieces=rec['er_sample_name'].split('-')
                   location=rec['er_location_name']
                   title=location

        SData=pmag.sort_diclist(Data,'core_depth')
        for rec in SData: # fish out bulk measurement data from desired depths
            if dmax==-1 or float(rec['core_depth'])<dmax and float(rec['core_depth'])>dmin:
                Depths.append((rec['core_depth']))
                if method=="LP-X":
                    SSucs.append(float(rec[suc_key]))
                else:
                   if pltD==1:Decs.append(float(rec['measurement_dec']))
                   if pltI==1:Incs.append(float(rec['measurement_inc']))
                   if norm==0 and pltM==1:Ints.append(float(rec[meas_key]))
                   if norm==1 and pltM==1:Ints.append(old_div(float(rec[meas_key]),float(rec['specimen_weight'])))
            if len(SSucs)>0:
                maxSuc=max(SSucs)
                minSuc=min(SSucs)
            if len(Ints)>1:
                maxInt=max(Ints)
                minInt=min(Ints)
        if len(Depths)==0:
            print('no bulk measurement data matched your request')
    SpecDepths,SpecDecs,SpecIncs=[],[],[]
    FDepths,FDecs,FIncs=[],[],[]
    if spc_file!="": # add depths to spec data
        print('spec file found')
        BFLs=pmag.get_dictitem(Specs,'magic_method_codes','DE-BFL','has')  # get all the discrete data with best fit lines
        for spec in BFLs:
            if location=="":
               location=spec['er_location_name']
            samp=pmag.get_dictitem(Samps,'er_sample_name',spec['er_sample_name'],'T')
            if len(samp)>0 and depth_scale in list(samp[0].keys()) and samp[0][depth_scale]!="":
              if ylab=='Age': ylab=ylab+' ('+samp[0]['age_unit']+')' # get units of ages - assume they are all the same!
              if dmax==-1 or float(samp[0][depth_scale])<dmax and float(samp[0][depth_scale])>dmin: # filter for depth
                SpecDepths.append(float(samp[0][depth_scale])) # fish out data with core_depth
                SpecDecs.append(float(spec['specimen_dec'])) # fish out data with core_depth
                SpecIncs.append(float(spec['specimen_inc'])) # fish out data with core_depth
            else:
                print('no core_depth found for: ',spec['er_specimen_name'])
        FMs=pmag.get_dictitem(Specs,'magic_method_codes','DE-FM','has')  # get all the discrete data with best fit lines
        for spec in FMs:
            if location=="":
               location=spec['er_location_name']
            samp=pmag.get_dictitem(Samps,'er_sample_name',spec['er_sample_name'],'T')
        if len(samp)>0 and depth_scale in list(samp[0].keys()) and samp[0][depth_scale]!="":
              if ylab=='Age': ylab=ylab+' ('+samp[0]['age_unit']+')' # get units of ages - assume they are all the same!
              if dmax==-1 or float(samp[0][depth_scale])<dmax and float(samp[0][depth_scale])>dmin: # filter for depth
                FDepths.append(float(samp[0][depth_scale]))# fish out data with core_depth
                FDecs.append(float(spec['specimen_dec'])) # fish out data with core_depth
                FIncs.append(float(spec['specimen_inc'])) # fish out data with core_depth
            else:
                print('no core_depth found for: ',spec['er_specimen_name'])
    ResDepths,ResDecs,ResIncs=[],[],[]
    if 'age' in depth_scale: # set y-key
        res_scale='average_age'
    else:
        res_scale='average_height'
    if res_file!="": #creates lists of Result Data
        for res in Results:
            meths=res['magic_method_codes'].split(":")
            if 'DE-FM' in meths:
              if dmax==-1 or float(res[res_scale])<dmax and float(res[res_scale])>dmin: # filter for depth
                ResDepths.append(float(res[res_scale])) # fish out data with core_depth
                ResDecs.append(float(res['average_dec'])) # fish out data with core_depth
                ResIncs.append(float(res['average_inc'])) # fish out data with core_depth
                Susc,Sus_depths=[],[]
    if dmin==-1:
        if len(Depths)>0: dmin,dmax=Depths[0],Depths[-1]
        if len(FDepths)>0: dmin,dmax=Depths[0],Depths[-1]
        if pltS==1 and len(SDepths)>0:
            if SDepths[0]<dmin:dmin=SDepths[0]
            if SDepths[-1]>dmax:dmax=SDepths[-1]
        if len(SpecDepths)>0:
            if min(SpecDepths)<dmin:dmin=min(SpecDepths)
            if max(SpecDepths)>dmax:dmax=max(SpecDepths)
        if len(ResDepths)>0:
            if min(ResDepths)<dmin:dmin=min(ResDepths)
            if max(ResDepths)>dmax:dmax=max(ResDepths)
    if suc_file!="":
        sucdat=open(suc_file,'r').readlines()
        keys=sucdat[0].replace('\n','').split(',') # splits on underscores
        for line in sucdat[1:]:
            SucRec={}
            for k in range(len(keys)):SucRec[keys[k]]=line.split(',')[k]
            if float(SucRec['Top Depth (m)'])<dmax and float(SucRec['Top Depth (m)'])>dmin and SucRec['Magnetic Susceptibility (80 mm)']!="":
                Susc.append(float(SucRec['Magnetic Susceptibility (80 mm)']))
                if Susc[-1]>maxSuc:maxSuc=Susc[-1]
                if Susc[-1]<minSuc:minSuc=Susc[-1]
                Sus_depths.append(float(SucRec['Top Depth (m)']))
    WIG,WIG_depths=[],[]
    if wig_file!="":
        wigdat,file_type=pmag.magic_read(wig_file)
        swigdat=pmag.sort_diclist(wigdat,depth_scale)
        keys=list(wigdat[0].keys())
        for key in keys:
            if key!=depth_scale:
                plt_key=key
                break
        for wig in swigdat:
            if float(wig[depth_scale])<dmax and float(wig[depth_scale])>dmin:
                WIG.append(float(wig[plt_key]))
                WIG_depths.append(float(wig[depth_scale]))
    tint=4.5
    plt=1
    if len(Decs)>0 and len(Depths)>0 or (len(SpecDecs)>0 and len(SpecDepths)>0) or (len(ResDecs)>0 and len(ResDepths)>0) or (len(SDecs)>0 and len(SDepths)>0) or (len(SInts)>0 and len(SDepths)>0) or (len(SIncs)>0 and len(SDepths)>0):
        pylab.figure(1,figsize=(width,8))
        version_num=pmag.get_version()
        pylab.figtext(.02,.01,version_num)
        if pltD==1:
            ax=pylab.subplot(1,pcol,plt)
            if pltL==1:
                pylab.plot(Decs,Depths,'k')
            if len(Decs)>0:
                pylab.plot(Decs,Depths,sym,markersize=size)
            if len(Decs)==0 and pltL==1 and len(SDecs)>0:
                pylab.plot(SDecs,SDepths,'k')
            if len(SDecs)>0:
                pylab.plot(SDecs,SDepths,Ssym,markersize=Ssize)
            if spc_file!="":
                pylab.plot(SpecDecs,SpecDepths,spc_sym,markersize=spc_size)
            if spc_file!="" and len(FDepths)>0:
                pylab.scatter(FDecs,FDepths,marker=spc_sym[-1],edgecolor=spc_sym[0],facecolor='white',s=spc_size**2)
            if res_file!="":
                pylab.plot(ResDecs,ResDepths,res_sym,markersize=res_size)
            if sum_file!="":
                for core in Cores:
                     depth=float(core[core_depth_key])
                     if depth>dmin and depth<dmax:
                        pylab.plot([0,360.],[depth,depth],'b--')
                        if pel==plt:pylab.text(360,depth+tint,core[core_label_key])
            if pel==plt:
                pylab.axis([0,400,dmax,dmin])
            else:
                pylab.axis([0,360.,dmax,dmin])
            pylab.xlabel('Declination')
            pylab.ylabel(ylab)
            plt+=1
            pmagplotlib.delticks(ax) # dec xticks are too crowded otherwise
    if pltI==1:
            pylab.subplot(1,pcol,plt)
            if pltL==1:pylab.plot(Incs,Depths,'k')
            if len(Incs)>0:pylab.plot(Incs,Depths,sym,markersize=size)
            if len(Incs)==0 and pltL==1 and len(SIncs)>0:pylab.plot(SIncs,SDepths,'k')
            if len(SIncs)>0:pylab.plot(SIncs,SDepths,Ssym,markersize=Ssize)
            if spc_file!="" and len(SpecDepths)>0:pylab.plot(SpecIncs,SpecDepths,spc_sym,markersize=spc_size)
            if spc_file!="" and len(FDepths)>0:
                pylab.scatter(FIncs,FDepths,marker=spc_sym[-1],edgecolor=spc_sym[0],facecolor='white',s=spc_size**2)
            if res_file!="":pylab.plot(ResIncs,ResDepths,res_sym,markersize=res_size)
            if sum_file!="":
                for core in Cores:
                     depth=float(core[core_depth_key])
                     if depth>dmin and depth<dmax:
                         if pel==plt:pylab.text(90,depth+tint,core[core_label_key])
                         pylab.plot([-90,90],[depth,depth],'b--')
            pylab.plot([0,0],[dmax,dmin],'k-')
            if pel==plt:
                pylab.axis([-90,110,dmax,dmin])
            else:
                pylab.axis([-90,90,dmax,dmin])
            pylab.xlabel('Inclination')
            pylab.ylabel('')
            plt+=1
    if pltM==1 and len(Ints)>0 or len(SInts)>0:
            pylab.subplot(1,pcol,plt)
            for pow in range(-10,10):
                if maxInt*10**pow>1:break
            if logit==0:
                for k in range(len(Ints)):
                    Ints[k]=Ints[k]*10**pow
                for k in range(len(SInts)):
                    SInts[k]=SInts[k]*10**pow
                if pltL==1 and len(Ints)>0: pylab.plot(Ints,Depths,'k')
                if len(Ints)>0:pylab.plot(Ints,Depths,sym,markersize=size)
                if len(Ints)==0 and pltL==1 and len(SInts)>0:pylab.plot(SInts,SDepths,'k-')
                if len(SInts)>0:pylab.plot(SInts,SDepths,Ssym,markersize=Ssize)
                if sum_file!="":
                    for core in Cores:
                         depth=float(core[core_depth_key])
                         pylab.plot([0,maxInt*10**pow+.1],[depth,depth],'b--')
                         if depth>dmin and depth<dmax:pylab.text(maxInt*10**pow-.2*maxInt*10**pow,depth+tint,core[core_label_key])
                pylab.axis([0,maxInt*10**pow+.1,dmax,dmin])
                if norm==0:
                    pylab.xlabel('%s %i %s'%('Intensity (10^-',pow,' Am^2)'))
                else:
                    pylab.xlabel('%s %i %s'%('Intensity (10^-',pow,' Am^2/kg)'))
            else:
                if pltL==1: pylab.semilogx(Ints,Depths,'k')
                if len(Ints)>0:pylab.semilogx(Ints,Depths,sym,markersize=size)
                if len(Ints)==0 and pltL==1 and len(SInts)>0:pylab.semilogx(SInts,SDepths,'k')
                if len(Ints)==0 and pltL==1 and len(SInts)>0:pylab.semilogx(SInts,SDepths,'k')
                if len(SInts)>0:pylab.semilogx(SInts,SDepths,Ssym,markersize=Ssize)
                if sum_file!="":
                    for core in Cores:
                         depth=float(core[core_depth_key])
                         pylab.semilogx([minInt,maxInt],[depth,depth],'b--')
                         if depth>dmin and depth<dmax:pylab.text(maxInt-.2*maxInt,depth+tint,core[core_label_key])
                pylab.axis([0,maxInt,dmax,dmin])
                if norm==0:
                    pylab.xlabel('Intensity (Am^2)')
                else:
                    pylab.xlabel('Intensity (Am^2/kg)')
            plt+=1
    if suc_file!="" or len(SSucs)>0:
            pylab.subplot(1,pcol,plt)
            if len(Susc)>0:
                if pltL==1:pylab.plot(Susc,Sus_depths,'k')
                if logit==0:pylab.plot(Susc,Sus_depths,sym,markersize=size)
                if logit==1:pylab.semilogx(Susc,Sus_depths,sym,markersize=size)
            if len(SSucs)>0:
                if logit==0:pylab.plot(SSucs,SDepths,sym,markersize=size)
                if logit==1:pylab.semilogx(SSucs,SDepths,sym,markersize=size)
            if sum_file!="":
                for core in Cores:
                     depth=float(core[core_depth_key])
                     if logit==0:pylab.plot([minSuc,maxSuc],[depth,depth],'b--')
                     if logit==1:pylab.semilogx([minSuc,maxSuc],[depth,depth],'b--')
            pylab.axis([minSuc,maxSuc,dmax,dmin])
            pylab.xlabel('Susceptibility')
            plt+=1
    if wig_file!="":
            pylab.subplot(1,pcol,plt)
            pylab.plot(WIG,WIG_depths,'k')
            if sum_file!="":
                for core in Cores:
                     depth=float(core[core_depth_key])
                     pylab.plot([WIG[0],WIG[-1]],[depth,depth],'b--')
            pylab.axis([min(WIG),max(WIG),dmax,dmin])
            pylab.xlabel(plt_key)
            plt+=1
    if pTS==1:
            ax1=pylab.subplot(1,pcol,plt)
            ax1.axis([-.25,1.5,amax,amin])
            plt+=1
            TS,Chrons=pmag.get_ts(ts)
            X,Y,Y2=[0,1],[],[]
            cnt=0
            if amin<TS[1]: # in the Brunhes
                Y=[amin,amin] # minimum age
                Y1=[TS[1],TS[1]] # age of the B/M boundary
                ax1.fill_between(X,Y,Y1,facecolor='black') # color in Brunhes, black
            for d in TS[1:]:
                pol=cnt%2
                cnt+=1
                if d<=amax and d>=amin:
                   ind=TS.index(d)
                   Y=[TS[ind],TS[ind]]
                   Y1=[TS[ind+1],TS[ind+1]]
                   if pol: ax1.fill_between(X,Y,Y1,facecolor='black') # fill in every other time
            ax1.plot([0,1,1,0,0],[amin,amin,amax,amax,amin],'k-')
            ax2=ax1.twinx()
            pylab.ylabel("Age (Ma): "+ts)
            for k in range(len(Chrons)-1):
                c=Chrons[k]
                cnext=Chrons[k+1]
                d=cnext[1]-old_div((cnext[1]-c[1]),3.)
                if d>=amin and d<amax:
                    ax2.plot([1,1.5],[c[1],c[1]],'k-') # make the Chron boundary tick
                    ax2.text(1.05,d,c[0]) #
            ax2.axis([-.25,1.5,amax,amin])
    figname=location+'_m:_'+method+'_core-depthplot'+fmt
    pylab.title(location)
    if verbose:
        pylab.draw()
        ans=input("S[a]ve plot? ")
        if ans=='a':
            pylab.savefig(figname)
            print('Plot saved as ',figname)
    elif plots:
        pylab.savefig(figname)
        print('Plot saved as ',figname)
    sys.exit()
Example #4
0
def main():
    """
    NAME
        thellier_magic_redo.py

    DESCRIPTION
        Calculates paleointensity parameters for thellier-thellier type data using bounds
        stored in the "redo" file

    SYNTAX
        thellier_magic_redo [command line options]

    OPTIONS
        -h prints help message
        -usr USER:   identify user, default is ""
        -fcr CRIT, set criteria for grading
        -f IN: specify input file, default is magic_measurements.txt
        -fre REDO: specify redo file, default is "thellier_redo"
        -F OUT: specify output file, default is thellier_specimens.txt
        -leg:  attaches "Recalculated from original measurements; supercedes published results. " to comment field
        -CR PERC TYPE: apply a cooling rate correction.  
            PERC should be a percentage of original (say reduce to 90%)
            TYPE should be one of the following:
               EG (for educated guess); PS (based on pilots); TRM (based on comparison of two TRMs) 
        -Fcr  CRout: specify pmag_specimen format file for cooling rate corrected data
        -ANI: there are anisotropy data to correct thellier results
        -fan ANIFILE: specify rmag_anisotropy format file, default is rmag_anisotropy.txt 
        -Fac  ACout: specify pmag_specimen format file for anisotropy corrected data
                 default is AC_specimens.txt
        -NLT: there are non-linear trm data in the measurements file to correct thellier results
        -fnl NLTFILE: specify magic_measurments format file, default is magic_measurements.txt
        -Fnl NLTout: specify pmag_specimen format file for non-linear trm corrected data
                 default is NLT_specimens.txt
        -z use z component differenences for pTRM calculation
    """
    dir_path='.'
    critout=""
    version_num=pmag.get_version()
    field,first_save=-1,1
    spec,recnum,start,end=0,0,0,0
    frac=0
    NltRecs,PmagSpecs,AniSpecRecs,NltSpecRecs,CRSpecs=[],[],[],[],[]
    meas_file,pmag_file,mk_file="magic_measurements.txt","thellier_specimens.txt","thellier_redo"
    anis_file="rmag_anisotropy.txt"
    anisout,nltout="AC_specimens.txt","NLT_specimens.txt"
    crout="CR_specimens.txt"
    nlt_file=""
    
    comment,user="","unknown"
    anis,nltrm=0,0
    jackknife=0 # maybe in future can do jackknife
    args=sys.argv
    Zdiff=0
    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 "-leg" in args: comment="Recalculated from original measurements; supercedes published results. "
    if "-CR" in args:
        ind=args.index("-CR")
        frac=.01*float(sys.argv[ind+1])
        crtype=sys.argv[ind+2]
    if "-Fcr" in args:
        ind=args.index("-Fcr")
        crout=sys.argv[ind+1]
    if "-f" in args:
        ind=args.index("-f")
        meas_file=sys.argv[ind+1]
    if "-F" in args:
        ind=args.index("-F")
        pmag_file=sys.argv[ind+1]
    if "-fre" in args:
        ind=args.index("-fre")
        mk_file=args[ind+1]
    #
    #
    if "-ANI" in args:
        anis=1
        ind=args.index("-ANI")
        if "-Fac" in args:
            ind=args.index("-Fac")
            anisout=args[ind+1]
        if "-fan" in args:
            ind=args.index("-fan")
            anis_file=args[ind+1]
    #
    if "-NLT" in args:
        nltrm=1
        if "-Fnl" in args:
            ind=args.index("-Fnl")
            nltout=args[ind+1]
        if "-fnl" in args:
            ind=args.index("-fnl")
            nlt_file=args[ind+1]
    if "-z" in args: Zdiff=1
    if '-fcr' in sys.argv: 
        ind=args.index("-fcr")
        critout=sys.argv[ind+1]
#
#  start reading in data:
#
    meas_file=dir_path+"/"+meas_file
    mk_file=dir_path+"/"+mk_file
    critout=dir_path+"/"+critout
    try:
        open(critout,'rU')
        accept_keys=['specimen_int_ptrm_n','specimen_md','specimen_fvds','specimen_b_beta','specimen_dang','specimen_drats','specimen_Z']
        crit_data,file_type=pmag.magic_read(critout)
        print "Acceptance criteria read in from ", critout
        accept={}
        accept['specimen_int_ptrm_n']=2.0
        for critrec in crit_data:
            if critrec["pmag_criteria_code"]=="IE-SPEC":
                for key in accept_keys:
                    if key not in critrec.keys():
                        accept[key]=-1
                    else:
                        accept[key]=float(critrec[key])
    except:
        critout="" # no acceptance criteria specified
    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()
    try:
        mk_f=open(mk_file,'rU')
    except:
        print "Bad redo file"
        sys.exit()
    mkspec=[]
    speclist=[]
    for line in mk_f.readlines():
        tmp=line.split()
        mkspec.append(tmp)
        speclist.append(tmp[0])
    if anis==1:
        anis_file=dir_path+"/"+anis_file 
        anis_data,file_type=pmag.magic_read(anis_file)
        if file_type != 'rmag_anisotropy':
            print file_type
            print file_type,"This is not a valid rmag_anisotropy file "
            sys.exit()
    if nlt_file=="":
        nlt_data=meas_data  # look for trm acquisition data in the meas_data file
    else:
        nlt_file=dir_path+"/"+nlt_file 
        nlt_data,file_type=pmag.magic_read(nlt_file)
#
# sort the specimen names and step through one by one
#
    sids=pmag.get_specs(meas_data)
# 
    print 'Processing ',len(speclist),' specimens - please wait '
    while spec < len(speclist):
        s=speclist[spec]
        recnum=0
        datablock=[]
        PmagSpecRec={}
        PmagSpecRec["er_analyst_mail_names"]=user
        PmagSpecRec["er_citation_names"]="This study"
        PmagSpecRec["magic_software_packages"]=version_num
        methcodes,inst_code=[],""
    #
    # find the data from the meas_data file for this specimen
    #
        for rec in meas_data:
            if rec["er_specimen_name"].lower()==s.lower(): 
                if "magic_instrument_codes" not in rec.keys():
                    rec["magic_instrument_codes"]="unknown"
                meths=rec["magic_method_codes"]
                for meth in meths:meth.strip()   # get rid of annoying spaces in method codes
                if "LP-PI-TRM" in meths: datablock.append(rec)
    #
    #  collect info for the PmagSpecRec dictionary
    #
        if len(datablock)>0:
            rec=datablock[0]
            PmagSpecRec["er_specimen_name"]=s
            PmagSpecRec["er_sample_name"]=rec["er_sample_name"]
            PmagSpecRec["er_site_name"]=rec["er_site_name"]
            PmagSpecRec["er_location_name"]=rec["er_location_name"]
            PmagSpecRec["measurement_step_unit"]="K"
            PmagSpecRec["specimen_correction"]='u'
            if "magic_instrument_codes" not in rec.keys():
                PmagSpecRec["magic_instrument_codes"]="unknown"
            else:
                PmagSpecRec["magic_instrument_codes"]=rec["magic_instrument_codes"]
            if "magic_experiment_name" not in rec.keys():
                rec["magic_experiment_name"]=""
            else:
                PmagSpecRec["magic_experiment_names"]=rec["magic_experiment_name"]
            meths=rec["magic_experiment_name"].split(":")
            for meth in meths:
                if meth.strip() not in methcodes and "LP-" in meth:methcodes.append(meth.strip())
    #
    # sort out the data into first_Z, first_I, ptrm_check, ptrm_tail
    #
            araiblock,field=pmag.sortarai(datablock,s,Zdiff)
            first_Z=araiblock[0]
            first_I=araiblock[1]
            ptrm_check=araiblock[2]
            ptrm_tail=araiblock[3]
            if len(first_I)<3 or len(first_Z)<4:
                spec+=1
                print 'skipping specimen ', s 
            else:
    #
    # get start, end
    #
                for redospec in mkspec:
                    if redospec[0]==s:
    	                b,e=float(redospec[1]),float(redospec[2])
                        break
                if e > float(first_Z[-1][0]):e=float(first_Z[-1][0])
                for recnum in range(len(first_Z)):
            	    if first_Z[recnum][0]==b:start=recnum
            	    if first_Z[recnum][0]==e:end=recnum
                nsteps=end-start
                if nsteps>2:
                    zijdblock,units=pmag.find_dmag_rec(s,meas_data)
                    pars,errcode=pmag.PintPars(araiblock,zijdblock,start,end)
                    pars['measurement_step_unit']=units
                    pars["specimen_lab_field_dc"]=field
                    pars["specimen_int"]=-1*field*pars["specimen_b"]
                    PmagSpecRec["measurement_step_min"]='%8.3e' % (pars["measurement_step_min"])
                    PmagSpecRec["measurement_step_max"]='%8.3e' % (pars["measurement_step_max"])
                    PmagSpecRec["specimen_int_n"]='%i'%(pars["specimen_int_n"])
                    PmagSpecRec["specimen_lab_field_dc"]='%8.3e'%(pars["specimen_lab_field_dc"])
                    PmagSpecRec["specimen_int"]='%9.4e '%(pars["specimen_int"])
                    PmagSpecRec["specimen_b"]='%5.3f '%(pars["specimen_b"])
                    PmagSpecRec["specimen_q"]='%5.1f '%(pars["specimen_q"])
                    PmagSpecRec["specimen_f"]='%5.3f '%(pars["specimen_f"])
                    PmagSpecRec["specimen_fvds"]='%5.3f'%(pars["specimen_fvds"])
                    PmagSpecRec["specimen_b_beta"]='%5.3f'%(pars["specimen_b_beta"])
                    PmagSpecRec["specimen_int_mad"]='%7.1f'%(pars["specimen_int_mad"])
                    PmagSpecRec["specimen_Z"]='%7.1f'%(pars["specimen_Z"])
                    PmagSpecRec["specimen_gamma"]='%7.1f'%(pars["specimen_gamma"])
                    if pars["method_codes"]!="" and pars["method_codes"] not in methcodes: methcodes.append(pars["method_codes"])
                    PmagSpecRec["specimen_dec"]='%7.1f'%(pars["specimen_dec"])
                    PmagSpecRec["specimen_inc"]='%7.1f'%(pars["specimen_inc"])
                    PmagSpecRec["specimen_tilt_correction"]='-1'
                    PmagSpecRec["specimen_direction_type"]='l'
                    PmagSpecRec["direction_type"]='l' # this is redudant, but helpful - won't be imported
                    PmagSpecRec["specimen_dang"]='%7.1f '%(pars["specimen_dang"])
                    PmagSpecRec["specimen_drats"]='%7.1f '%(pars["specimen_drats"])
                    PmagSpecRec["specimen_int_ptrm_n"]='%i '%(pars["specimen_int_ptrm_n"])
                    PmagSpecRec["specimen_rsc"]='%6.4f '%(pars["specimen_rsc"])
                    PmagSpecRec["specimen_md"]='%i '%(int(pars["specimen_md"]))
                    if PmagSpecRec["specimen_md"]=='-1':PmagSpecRec["specimen_md"]=""
                    PmagSpecRec["specimen_b_sigma"]='%5.3f '%(pars["specimen_b_sigma"])
                    if "IE-TT" not in  methcodes:methcodes.append("IE-TT")
                    methods=""
                    for meth in methcodes:
                        methods=methods+meth+":"
                    PmagSpecRec["magic_method_codes"]=methods[:-1]
                    PmagSpecRec["magic_software_packages"]=version_num
                    PmagSpecRec["specimen_description"]=comment
                    if critout!="":
                        score,kill=pmag.grade(PmagSpecRec,accept)
                        Grade=""
                        if score==len(accept.keys()):Grade='A'
                        if score==len(accept.keys())-1:Grade='B'
                        if score==len(accept.keys())-2:Grade='C'
                        if score==len(accept.keys())-3:Grade='D'
                        if score<=len(accept.keys())-4:Grade='F'
                        PmagSpecRec["specimen_grade"]=Grade
                    else:
                        PmagSpecRec["specimen_grade"]=""
                    if nltrm==0 and anis==0 and frac!=0: # apply cooling rate correction
                        CrSpecRec={}
                        for key in PmagSpecRec.keys():CrSpecRec[key]=PmagSpecRec[key]
                        inten=frac*float(CrSpecRec['specimen_int'])
                        CrSpecRec["specimen_int"]='%9.4e '%(inten) # adjust specimen intensity by cooling rate correction
                        CrSpecRec['magic_method_codes'] = CrSpecRec['magic_method_codes']+':DA-CR-'+crtype
                        CrSpecRec["specimen_correction"]='c'
                        CRSpecs.append(CrSpecRec)
                    PmagSpecs.append(PmagSpecRec)
                    NltSpecRec=""
    #
    # check on non-linear TRM correction
    #
                    if nltrm==1:
    #
    # find the data from the nlt_data list for this specimen
    #
                        TRMs,Bs=[],[]
                        NltSpecRec=""
                        NltRecs=[]
                        for NltRec in nlt_data:
                            if NltRec['er_specimen_name']==PmagSpecRec["er_specimen_name"]:
                                meths=NltRec["magic_method_codes"].split(":")
                                for meth in meths:meth.strip()
                                if "LP-TRM" in meths: NltRecs.append(NltRec)
                        if len(NltRecs) > 2:
                            for NltRec in NltRecs:
                                Bs.append(float(NltRec['treatment_dc_field']))
                                TRMs.append(float(NltRec['measurement_magn_moment']))
                            NLTpars=nlt.NLtrm(Bs,TRMs,float(PmagSpecRec['specimen_int']),float(PmagSpecRec['specimen_lab_field_dc']),0) 
                            if NLTpars['banc']>0:
                                NltSpecRec={}
                                for key in PmagSpecRec.keys():
                                    NltSpecRec[key]=PmagSpecRec[key]
                                NltSpecRec['specimen_int']='%9.4e'%(NLTpars['banc'])  
                                NltSpecRec['magic_method_codes']=PmagSpecRec["magic_method_codes"]+":DA-NL"
                                NltSpecRec["specimen_correction"]='c'
                                NltSpecRec['specimen_grade']=PmagSpecRec['specimen_grade']
                                NltSpecRec["magic_software_packages"]=version_num
                                print NltSpecRec['er_specimen_name'],  ' Banc= ',float(NLTpars['banc'])*1e6
                                if anis==0 and frac!=0:
                                    CrSpecRec={}
                                    for key in NltSpecRec.keys():CrSpecRec[key]=NltSpecRec[key]
                                    inten=frac*float(CrSpecRec['specimen_int'])
                                    CrSpecRec["specimen_int"]='%9.4e '%(inten) # adjust specimen intensity by cooling rate correction
                                    CrSpecRec['magic_method_codes'] = CrSpecRec['magic_method_codes']+':DA-CR-'+crtype
                                    CRSpecs.append(CrSpecRec)
                                NltSpecRecs.append(NltSpecRec)
    #
    # check on anisotropy correction
                        if anis==1:
                            if NltSpecRec!="":  
                                Spc=NltSpecRec
                            else: # find uncorrected data
                                Spc=PmagSpecRec
                            for AniSpec in anis_data:
                                if AniSpec["er_specimen_name"]==PmagSpecRec["er_specimen_name"]:
                                    AniSpecRec=pmag.thellier_anis_corr(Spc,AniSpec)
                                    AniSpecRec['specimen_grade']=PmagSpecRec['specimen_grade']
                                    inst_codes=Spc["magic_instrument_codes"]
                                    if "magic_instrument_codes" in AniSpec.keys():
                                        if inst_codes=="unknown":
                                            inst_codes=AniSpec["magic_instrument_codes"]
                                        else:
                                            inst_codes=inst_codes+":"+AniSpec["magic_instrument_codes"]
                                    AniSpecRec["magic_instrument_codes"]=inst_codes
                                    AniSpecRec["specimen_correction"]='c'
                                    AniSpecRec["magic_software_packages"]=version_num
                                    if frac!=0:
                                        CrSpecRec={}
                                        for key in AniSpecRec.keys():CrSpecRec[key]=AniSpecRec[key]
                                        inten=frac*float(CrSpecRec['specimen_int'])
                                        CrSpecRec["specimen_int"]='%9.4e '%(inten) # adjust specimen intensity by cooling rate correction
                                        CrSpecRec['magic_method_codes'] = CrSpecRec['magic_method_codes']+':DA-CR-'+crtype
                                        CRSpecs.append(CrSpecRec)
                                    AniSpecRecs.append(AniSpecRec) 
                                    break
                    elif anis==1:
                        for AniSpec in anis_data:
                            if AniSpec["er_specimen_name"]==PmagSpecRec["er_specimen_name"]:
                                AniSpecRec=pmag.thellier_anis_corr(PmagSpecRec,AniSpec)
                                AniSpecRec['specimen_grade']=PmagSpecRec['specimen_grade']
                                inst_codes=PmagSpecRec["magic_instrument_codes"]
                                if "magic_instrument_codes" in AniSpec.keys():
                                    if inst_codes=="unknown":
                                        inst_codes=AniSpec["magic_instrument_codes"]
                                    else:
                                        inst_codes=inst_codes+":"+AniSpec["magic_instrument_codes"]
                                AniSpecRec["magic_instrument_codes"]=inst_codes
                                AniSpecRec["specimen_correction"]='c'
                                AniSpecRec["magic_software_packages"]=version_num
                                if frac!=0:
                                    CrSpecRec={}
                                    for key in AniSpecRec.keys():CrSpecRec[key]=AniSpecRec[key]
                                    inten=frac*float(CrSpecRec['specimen_int'])
                                    CrSpecRec["specimen_int"]='%9.4e '%(inten) # adjust specimen intensity by cooling rate correction
                                    CrSpecRec['magic_method_codes'] = CrSpecRec['magic_method_codes']+':DA-CR-'+crtype
                                    CRSpecs.append(CrSpecRec)
                                AniSpecRecs.append(AniSpecRec) 
                                break
                spec +=1
        else:
            print "skipping ",s
            spec+=1
    pmag_file=dir_path+'/'+pmag_file
    pmag.magic_write(pmag_file,PmagSpecs,'pmag_specimens')
    if anis==1:
        anisout=dir_path+'/'+anisout
        pmag.magic_write(anisout,AniSpecRecs,'pmag_specimens')
    if nltrm==1:
        nltout=dir_path+'/'+nltout
        pmag.magic_write(nltout,NltSpecRecs,'pmag_specimens')
    if frac!=0:
        crout=dir_path+'/'+crout
        pmag.magic_write(crout,CRSpecs,'pmag_specimens')
Example #5
0
def main():
    """

    NAME
        HUJI_magic.py
 
    DESCRIPTION
        converts HUJI format files to magic_measurements format files

    SYNTAX
        HUJI_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify input file, required
        -F FILE: specify output file, default is magic_measurements.txt
        -LP [colon delimited list of protocols, include all that apply]
            AF:  af demag
            T: thermal including thellier but not trm acquisition
            N: NRM only
            TRM: trm acquisition
            ANI: anisotropy experiment
            CR: cooling rate experiment.
                The treatment coding of the measurement file should be: XXX.00,XXX.10, XXX.20 ...XX.70 etc. (XXX.00 is optional)
                where XXX in the temperature and .10,.20... are running numbers of the cooling rates steps.
                XXX.00 is optional zerofield baseline. XXX.70 is alteration check.
                syntax in sio_magic is: -LP CR xxx,yyy,zzz,.....xx
                where xx, yyy,zzz...xxx  are cooling time in [K/minutes], seperated by comma, ordered at the same order as XXX.10,XXX.20 ...XX.70
                if you use a zerofield step then no need to specify the cooling rate for the zerofield
            
        -spc NUM : specify number of characters to designate a  specimen, default = 0
        -loc LOCNAME : specify location/study name, must have either LOCNAME or SAMPFILE or be a synthetic
        -dc B PHI THETA: dc lab field (in micro tesla) and phi,theta, default is none
              NB: use PHI, THETA = -1 -1 to signal that it changes, i.e. in anisotropy experiment
        # to do! -ac B : peak AF field (in mT) for ARM acquisition, default is none
        -ncn NCON:  specify naming convention: default is #1 below
        
       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] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name same as sample
            [6] site is entered under a separate column
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.
 
            [8] synthetic - has no site name
            [9] ODP naming convention 
    INPUT
        separate experiments ( AF, thermal, thellier, trm aquisition) should be seperate  files
        (eg. af.txt, thermal.txt, etc.)

        HUJI masurement file format  (space delimited text):   
        Spec lab-running-numbe-code  Date Hour Treatment-type(T/N/A) Treatment(XXX.XX) dec(geo) inc(geo) dec(tilt) inc(tilt)

        ---------

        conventions:
        Spec: specimen name
        Treat:  treatment step
            XXX T in Centigrade
            XXX AF in mT
            for special experiments:
              Thellier:
                XXX.0  first zero field step
                XXX.1  first in field step [XXX.0 and XXX.1 can be done in any order]
                XXX.2  second in-field step at lower temperature (pTRM check)

              ATRM:
                X.00 optional baseline
                X.1 ATRM step (+X)
                X.2 ATRM step (+Y)
                X.3 ATRM step (+Z)
                X.4 ATRM step (-X)
                X.5 ATRM step (-Y)
                X.6 ATRM step (-Z)
                X.7 optional alteration check (+X)

              TRM:
                XXX.YYY  XXX is temperature step of total TRM
                         YYY is dc field in microtesla
         
         Intensity assumed to be total moment in 10^3 Am^2 (emu)
         Declination:  Declination in specimen coordinate system
         Inclination:  Inclination in specimen coordinate system

         Optional metatdata string:  mm/dd/yy;hh:mm;[dC,mT];xx.xx;UNITS;USER;INST;NMEAS
             hh in 24 hours.  
             dC or mT units of treatment XXX (see Treat above) for thermal or AF respectively
             xx.xxx   DC field
             UNITS of DC field (microT, mT)
             INST:  instrument code, number of axes, number of positions (e.g., G34 is 2G, three axes, 
                    measured in four positions)
             NMEAS: number of measurements in a single position (1,3,200...)
     
    """
    args=sys.argv
    meas_file="magic_measurements.txt"
    user=""
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-usr" in args:
        ind=args.index("-usr")
        user=args[ind+1]
    else:
        user=""
    if '-F' in args:
        ind=args.index("-F")
        meas_file=args[ind+1]
    if '-f' in args:
        ind=args.index("-f")
        magfile=args[ind+1]
        try:
            input=open(magfile,'rU')
        except:
            print "bad mag file name"
            sys.exit()
    else: 
        print "mag_file field is required option"
        print main.__doc__
        sys.exit()
    if "-dc" in args:
        ind=args.index("-dc")
        labfield=float(args[ind+1])*1e-6
        phi=float(args[ind+2])
        theta=float(args[ind+3])
    if "-ac" in args:
        ind=args.index("-ac")
        peakfield=float(args[ind+1])*1e-3
    if "-spc" in args:
        ind=args.index("-spc")
        specnum=int(args[ind+1])
        if specnum!=0:specnum=-specnum
    if "-loc" in args:
        ind=args.index("-loc")
        er_location_name=args[ind+1]
        
    if "-ncn" in args:
        ind=args.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=int(samp_con.split("-")[1])
                samp_con="4"
        if "7-" in samp_con:
            if "-" not in samp_con:
                print "option [7] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z=int(samp_con.split("-")[1])
                samp_con="7"
                
    # lab process:
    if '-LP' in args:
        ind=args.index("-LP")
        codelist=args[ind+1]
        codes=codelist.split(':')
        if "AF" in codes:
            demag='AF' 
            LPcode="LP-DIR-AF"
        if "T" in codes:
            demag="T"
            if '-dc' not in args: LPcode="LP-DIR-T"
            if '-dc' in args: LPcode="LP-PI-TRM"
            if "ANI" in codes:
                if '-dc' not in args:
                    print "missing option -dc exiting"
                    exit()

                LPcode="LP-AN-TRM"
                
        if "TRM" in codes: 
            demag="T"
            LPcode="LP-TRM"
            #trm=1
            
        if "CR" in codes:
            demag="T"
            # dc should be in the code
            if '-dc' not in args:
                print "missing option -dc exiting"
                exit()
            
            LPcode="LP-TRM-CR" # TRM in different cooling rates
            ind=args.index("-LP")
            CR_cooling_times=args[ind+2].split(",")
            
            #print CR_cooling_time ,"CR_cooling_time"

    version_num=pmag.get_version()

    MagRecs=[]
    
    #--------------------------------------
    # Read the file
    # Assumption:
    # 1. different lab protocolsa are in different files
    # 2. measurements are in the correct order
    #--------------------------------------

    Data={}

    line_no=0

    for line in input.readlines():
        line_no+=1
        this_line_data={}
        line_no+=1
        instcode=""
        if len(line)<2:
            continue
        if line[0]=="#": #HUJI way of marking bad data points
            continue
        
        rec=line.strip('\n').split()
        specimen=rec[0]
        date=rec[2].split("/")
        hour=rec[3].split(":")
        treatment_type=rec[4]
        treatment=rec[5].split(".")
        dec=rec[6]
        inc=rec[7]
        dec_tilted=rec[8]
        inc_tilted=rec[9]
        moment_emu=float(rec[10])

        if specimen not in Data.keys():
            Data[specimen]=[]
            
        # check duplicate treatments:
        # if yes, delete the first and use the second

        if len(Data[specimen])>0:
            if treatment==Data[specimen][-1]['treatment']:
                del(Data[specimen][-1])
                print "-W- Identical treatimens in file %s magfile line %i: specimen %s, treatment %s deleting the first. " %(magfile, line_no, specimen,".".join(treatment))

        this_line_data={}
        this_line_data['specimen']=specimen
        this_line_data['date']=date
        this_line_data['hour']=hour
        this_line_data['treatment_type']=treatment_type
        this_line_data['treatment']=treatment
        this_line_data['dec']=dec
        this_line_data['inc']=inc
        this_line_data['dec_tilted']=dec_tilted
        this_line_data['inc_tilted']=inc_tilted
        this_line_data['moment_emu']=moment_emu                                     
        Data[specimen].append(this_line_data)

        
    print "-I- done reading file %s"%magfile

    #--------------------------------------
    # Convert to MagIC
    #--------------------------------------
    
    specimens_list=Data.keys()
    specimens_list.sort()


    MagRecs=[]
    for specimen in  specimens_list:
        for i in range(len(Data[specimen])):
            #print LPcode
            this_line_data=Data[specimen][i]
            methcode=""
            MagRec={}
            MagRec["er_specimen_name"]=this_line_data['specimen']
            if specnum!=0:
                MagRec["er_sample_name"]=this_line_data['specimen'][:specnum]
            else:
                MagRec["er_sample_name"]=this_line_data['specimen']

            if samp_con=="1":
                MagRec["er_site_name"]=MagRec["er_sample_name"][:-1]
            elif samp_con=="2":
                parts=MagRec["er_sample_name"].split('-')
                MagRec["er_site_name"]= parts[0]
            elif samp_con=="3":
                parts=MagRec["er_sample_name"].split('.')
                MagRec["er_site_name"]= parts[0]
            elif samp_con=='4':
                MagRec["er_site_name"]=MagRec["er_sample_name"][0:-Z]
            elif samp_con=='5':
                MagRec["er_site_name"]=MagRec["er_sample_name"]
            elif samp_con=='7':
                MagRec["er_site_name"]=MagRec["er_sample_name"][0:Z]                
            else:
                MagRec["er_site_name"]=MagRec["er_sample_name"] # site=sample by default
            
            if "-loc" in args:
                MagRec['er_location_name']=er_location_name
            else:
                MagRec['er_location_name']=MagRec["er_site_name"]
                
            MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
            MagRec["measurement_magn_moment"]='%10.3e'% (float(this_line_data['moment_emu'])*1e-3) # moment in Am^2 (from emu)
            MagRec["measurement_dec"]=this_line_data['dec']
            MagRec["measurement_inc"]=this_line_data['inc']
            date=this_line_data['date']
            hour=this_line_data['hour']    

            if float(date[2])>80:
                yyyy="19"+date[2]
            else:
                yyyy="20"+date[2]
            if len (date[0])==1:
                date[0]="0"+date[0]
            if len (date[1])==1:
                date[1]="0"+date[1]
            MagRec["measurement_date"]=":".join([yyyy,date[0],date[1],hour[0],hour[1],"00.00"])
            #print MagRec["measurement_date"],"   Ron check please"
            MagRec["measurement_time_zone"]='JER'
            MagRec['er_analyst_mail_names'] =user         
            MagRec["er_citation_names"]="This study"
            MagRec["magic_instrument_codes"]="HUJI-2G"
            MagRec["measurement_flag"]="g"
            MagRec["measurement_positions"]="1"
            MagRec["measurement_positions"]="1"
            MagRec["measurement_standard"]="u"
            MagRec["measurement_description"]=""
            MagRec["treatment_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin
            #----------------------------------------            
            # AF demag
            # do not support AARM yet
            #----------------------------------------
            
            if demag=="AF":
                
                # demag in zero field
                if LPcode != "LP-AN-ARM":
                    MagRec["treatment_ac_field"]='%8.3e' %(float(this_line_data['treatment'][0])*1e-3) # peak field in tesla
                    MagRec["treatment_dc_field"]='0'
                    MagRec["treatment_dc_field_phi"]='0'
                    MagRec["treatment_dc_field_theta"]='0'
                    if treatment_type=="N":
                        methcode="LP-DIR-AF:LT-NO"
                    elif treatment_type=="A":
                        methcode="LP-DIR-AF:LT-AF-Z"
                    else:
                        print "ERROR in treatment field line %i... exiting until you fix the problem" %line_no
                        exit()
                                            
                # AARM experiment    
                else:
                    print "Dont supprot AARM in HUJI format yet. sorry... do be DONE"
                MagRec["magic_method_codes"]=methcode
                MagRec["magic_experiment_name"]=specimen+ ":" + LPcode
                MagRec["measurement_number"]="%i"%i
                MagRec["measurement_description"]=""

                MagRecs.append(MagRec)
                #continue
                                
            #----------------------------------------
            # Thermal:  
            # Thellier experiment: "IZ", "ZI", "IZZI", pTRM checks
            # Thermal demag
            # Thermal cooling rate experiment
            # Thermal NLT
            #----------------------------------------


            if demag=="T": 

                treatment=this_line_data['treatment']
                treatment_type=this_line_data['treatment_type']
                
                    
                #----------------------------------------
                # Thellier experimet
                #----------------------------------------

                if LPcode == "LP-PI-TRM"  : # Thelllier experiment

                    

                    MagRec["magic_experiment_name"]=specimen+ ":" + LPcode
                    methcode=LPcode        
                    
                    if treatment_type=="N" or ( (treatment[1]=='0' or  treatment[1]=='00') and float(treatment[0])==0):
                            LT_code="LT-NO"
                            MagRec["treatment_dc_field_phi"]='0' 
                            MagRec["treatment_dc_field_theta"]='0' 
                            MagRec["treatment_dc_field"]='0'
                            MagRec["treatment_temp"]='273.'
                                                                  
                    elif treatment[1]=='0' or  treatment[1]=='00':
                            LT_code="LT-T-Z"
                            MagRec["treatment_dc_field_phi"]='0' 
                            MagRec["treatment_dc_field_theta"]='0' 
                            MagRec["treatment_dc_field"]='%8.3e'%(0)
                            MagRec["treatment_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin

                            # check if this is ZI or IZ:
                            #  check if the same temperature already measured:
                            methcode="LP-PI-TRM:LP-PI-TRM-ZI"
                            for j in range (0,i):
                                if Data[specimen][j]['treatment'][0] == treatment[0]:
                                    if Data[specimen][j]['treatment'][1] == '1' or Data[specimen][j]['treatment'][1] == '10':
                                        methcode="LP-PI-TRM:LP-PI-TRM-IZ"
                                    else:
                                        methcode="LP-PI-TRM:LP-PI-TRM-ZI"
                                                                               
                                    
                    elif treatment[1]=='1' or  treatment[1]=='10':
                            LT_code="LT-T-I"
                            MagRec["treatment_dc_field"]='%8.3e' % (labfield) # labfield in tesla (convert from microT)
                            MagRec["treatment_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                            MagRec["treatment_dc_field_theta"]='%7.1f' % (theta) # labfield theta
                            MagRec["treatment_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin

                            # check if this is ZI or IZ:
                            #  check if the same temperature already measured:
                            methcode="LP-PI-TRM:LP-PI-TRM-IZ"
                            for j in range (0,i):
                                if Data[specimen][j]['treatment'][0] == treatment[0]:
                                    if Data[specimen][j]['treatment'][1] == '0' or Data[specimen][j]['treatment'][1] == '00':
                                        methcode="LP-PI-TRM:LP-PI-TRM-ZI"
                                    else:
                                        methcode="LP-PI-TRM:LP-PI-TRM-IZ"
                            
                    elif treatment[1]=='2' or  treatment[1]=='20':
                            LT_code="LT-PTRM-I"
                            MagRec["treatment_dc_field"]='%8.3e' % (labfield) # labfield in tesla (convert from microT)
                            MagRec["treatment_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                            MagRec["treatment_dc_field_theta"]='%7.1f' % (theta) # labfield theta
                            MagRec["treatment_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin
                            methcode="LP-PI-TRM:LP-PI-TRM-IZ"

                    else:
                            print "ERROR in treatment field line %i... exiting until you fix the problem" %line_no
                            exit()
                    
                    MagRec["magic_method_codes"]=LT_code+":"+methcode
                    MagRec["measurement_number"]="%i"%i            
                    MagRec["measurement_description"]=""
                    MagRecs.append(MagRec)
                    #continue
                    
                                            
                #----------------------------------------
                # demag experimet
                #----------------------------------------


                if LPcode == "LP-DIR-T"  :
                    MagRec["magic_experiment_name"]=specimen+ ":" + LPcode
                    methcode=LPcode        
                    
                    if treatment_type=="N":
                        LT_code="LT-NO"
                    else:
                        LT_code="LT-T-Z"
                                            
                        methcode=LPcode+":"+"LT-T-Z"
                    MagRec["treatment_dc_field_phi"]='0' 
                    MagRec["treatment_dc_field_theta"]='0' 
                    MagRec["treatment_dc_field"]='%8.3e'%(0)
                    MagRec["treatment_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin
                    MagRec["magic_method_codes"]=LT_code+":"+methcode
                    MagRec["measurement_number"]="%i"%i            
                    MagRec["measurement_description"]=""
                    MagRecs.append(MagRec)
                    #continue
                        

                #----------------------------------------
                # ATRM measurements
                # The direction of the magnetization is used to determine the
                # direction of the lab field.
                #----------------------------------------
                

                if LPcode =="LP-AN-TRM" :
                    
                    MagRec["magic_experiment_name"]=specimen+ ":" + LPcode
                    methcode=LPcode        

                    if float(treatment[1])==0:
                        MagRec["magic_method_codes"]="LP-AN-TRM:LT-T-Z"
                        MagRec["treatment_dc_field_phi"]='0'
                        MagRec["treatment_dc_field_theta"]='0'
                        MagRec["treatment_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin
                        MagRec["treatment_dc_field"]='0'
                    else:
                        if float(treatment[1])==7:
                            # alteration check
                            methcode="LP-AN-TRM:LT-PTRM-I"
                            MagRec["measurement_number"]='7'# -z
                        else:    
                            MagRec["magic_method_codes"]="LP-AN-TRM:LT-T-I"
                            inc=float(MagRec["measurement_inc"]);dec=float(MagRec["measurement_dec"])
                            if abs(inc)<45 and (dec<45 or dec>315): # +x
                                tdec,tinc=0,0
                                MagRec["measurement_number"]='1'
                            if abs(inc)<45 and (dec<135 and dec>45):
                                tdec,tinc=90,0
                                MagRec["measurement_number"]='2' # +y
                            if inc>45 :
                                tdec,tinc=0,90
                                MagRec["measurement_number"]='3' # +z
                            if abs(inc)<45 and (dec<225 and dec>135):
                                tdec,tinc=180,0
                                MagRec["measurement_number"]='4' # -x
                            if abs(inc)<45 and (dec<315 and dec>225):
                                tdec,tinc=270,0
                                MagRec["measurement_number"]='5'# -y
                            if inc<-45 :
                                tdec,tinc=0,-90
                                MagRec["measurement_number"]='6'# -z
                        
                        MagRec["treatment_dc_field_phi"]='%7.1f' %(tdec)
                        MagRec["treatment_dc_field_theta"]='%7.1f'% (tinc)
                        MagRec["treatment_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin
                        MagRec["treatment_dc_field"]='%8.3e'%(labfield)
                    MagRec["measurement_description"]=""
                    MagRecs.append(MagRec)
                    #continue

                #----------------------------------------
                # NLT measurements
                # or TRM acquisistion experiment
                #----------------------------------------

                
                if LPcode == "LP-TRM"  :
                    MagRec["magic_experiment_name"]=specimen+ ":" + LPcode
                    MagRec["magic_method_codes"]="LP-TRM:LT-T-I"
                    if float(treatment[1])==0:
                        labfield=0
                    else:
                        labfield=float(float(treatment[1]))*1e-6
                    MagRec["treatment_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin                
                    MagRec["treatment_dc_field"]='%8.3e' % (labfield) # labfield in tesla (convert from microT)
                    MagRec["treatment_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                    MagRec["treatment_dc_field_theta"]='%7.1f' % (theta) # labfield theta
                    MagRec["measurement_number"]="%i"%i            
                    MagRec["measurement_description"]=""
                    MagRecs.append(MagRec)
                    #continue
    

                #----------------------------------------
                # Cooling rate experiments
                #----------------------------------------
                
                if  LPcode =="LP-TRM-CR":
                    index=int(treatment[1][0])
                    #print index,"index"
                    #print CR_cooling_times,"CR_cooling_times"
                    #print CR_cooling_times[index-1]
                    #print CR_cooling_times[0:index-1]
                    CR_cooling_time=CR_cooling_times[index-1]
                    if CR_cooling_time in CR_cooling_times[0:index-1]:
                        MagRec["magic_method_codes"]="LP-TRM-CR"+":" +"LT-PTRM-I"
                    else:    
                        MagRec["magic_method_codes"]="LP-TRM-CR"
                    MagRec["magic_experiment_name"]=specimen+ ":" + LPcode
                    MagRec["treatment_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin                
                    MagRec["treatment_dc_field"]='%8.3e' % (labfield) # labfield in tesla (convert from microT)
                    MagRec["treatment_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                    MagRec["treatment_dc_field_theta"]='%7.1f' % (theta) # labfield theta
                    MagRec["measurement_number"]="%i"%index
                    MagRec["measurement_description"]="%i minutes cooling time"%int(CR_cooling_time)
                    MagRecs.append(MagRec)
                    #continue

    
    pmag.magic_write(meas_file,MagRecs,'magic_measurements')
    print "-I- results put in ",meas_file
Example #6
0
def main():
    """
    NAME
        thellier_magic.py
    
    DESCRIPTION
        plots Thellier-Thellier, allowing interactive setting of bounds
        and customizing of selection criteria.  Saves and reads interpretations
        from a pmag_specimen formatted table, default: thellier_specimens.txt

    SYNTAX 
        thellier_magic.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f MEAS, set magic_measurements input file
        -fsp PRIOR, set pmag_specimen prior interpretations file
        -fan ANIS, set rmag_anisotropy file for doing the anisotropy corrections
        -fcr CRIT, set criteria file for grading.  
        -fmt [svg,png,jpg], format for images - default is svg
        -sav,  saves plots with out review (default format)
        -spc SPEC, plots single specimen SPEC, saves plot with specified format
            with optional -b bounds adn quits
        -b BEG END: sets  bounds for calculation
           BEG: starting step for slope calculation
           END: ending step for slope calculation
        -z use only z component difference for pTRM calculation
        
    DEFAULTS
        MEAS: magic_measurements.txt
        REDO: thellier_redo
        CRIT: NONE
        PRIOR: NONE
  
    OUTPUT 
        figures:
            ALL:  numbers refer to temperature steps in command line window
            1) Arai plot:  closed circles are zero-field first/infield
                           open circles are infield first/zero-field
                           triangles are pTRM checks
                           squares are pTRM tail checks
                           VDS is vector difference sum
                           diamonds are bounds for interpretation
            2) Zijderveld plot:  closed (open) symbols are X-Y (X-Z) planes
                                 X rotated to NRM direction
            3) (De/Re)Magnetization diagram:
                           circles are NRM remaining
                           squares are pTRM gained
            4) equal area projections:
 			   green triangles are pTRM gained direction
                           red (purple) circles are lower(upper) hemisphere of ZI step directions 
                           blue (cyan) squares are lower(upper) hemisphere IZ step directions 
            5) Optional:  TRM acquisition
            6) Optional: TDS normalization
        command line window:
            list is: temperature step numbers, temperatures (C), Dec, Inc, Int (units of magic_measuements)
                     list of possible commands: type letter followed by return to select option
                     saving of plots creates .svg format files with specimen_name, plot type as name
    """ 
#
#   initializations
#
    meas_file,critout,inspec="magic_measurements.txt","","thellier_specimens.txt"
    first=1
    inlt=0
    version_num=pmag.get_version()
    TDinit,Tinit,field,first_save=0,0,-1,1
    user,comment,AniSpec,locname="",'',"",""
    ans,specimen,recnum,start,end=0,0,0,0,0
    plots,pmag_out,samp_file,style=0,"","","svg"
    verbose=pmagplotlib.verbose 
    fmt='.'+style
#
# default acceptance criteria
#
    accept=pmag.default_criteria(0)[0] # set the default criteria
#
# parse command line options
#
    Zdiff,anis=0,0
    spc,BEG,END="","",""
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        meas_file=sys.argv[ind+1]
    if '-fsp' in sys.argv:
        ind=sys.argv.index('-fsp')
        inspec=sys.argv[ind+1]
    if '-fan' in sys.argv:
        ind=sys.argv.index('-fan')
        anisfile=sys.argv[ind+1]
        anis=1
        anis_data,file_type=pmag.magic_read(anisfile)
        if verbose: print "Anisotropy data read in from ", anisfile
    if '-fmt' in sys.argv:
        ind=sys.argv.index('-fmt')
        fmt='.'+sys.argv[ind+1]
    if '-sav' in sys.argv: 
        plots=1
        verbose=0
    if '-z' in sys.argv: Zdiff=1
    if '-spc' in sys.argv:
        ind=sys.argv.index('-spc')
        spc=sys.argv[ind+1]
        if '-b' in sys.argv:
            ind=sys.argv.index('-b')
            BEG=int(sys.argv[ind+1])
            END=int(sys.argv[ind+2])
    if '-fcr' in sys.argv:
        ind=sys.argv.index('-fcr')
        critout=sys.argv[ind+1]
        crit_data,file_type=pmag.magic_read(critout)
        if file_type!='pmag_criteria':
            if verbose: print 'bad pmag_criteria file, using no acceptance criteria'
            accept=pmag.default_criteria(1)[0]
        else:
            if verbose: print "Acceptance criteria read in from ", critout
            accept={'pmag_criteria_code':'ACCEPTANCE','er_citation_names':'This study'}
            for critrec in crit_data:
                if 'sample_int_sigma_uT' in critrec.keys(): # accommodate Shaar's new criterion
                    critrec['sample_int_sigma']='%10.3e'%(eval(critrec['sample_int_sigma_uT'])*1e-6)
                for key in critrec.keys():
                    if key not in accept.keys() and critrec[key]!='':
                        accept[key]=critrec[key]
    try:
        open(inspec,'rU')
        PriorRecs,file_type=pmag.magic_read(inspec)
        if file_type != 'pmag_specimens':
            print file_type
            print file_type,inspec," is not a valid pmag_specimens file " 
            sys.exit()
        for rec in PriorRecs:
            if 'magic_software_packages' not in rec.keys():rec['magic_software_packages']=""
    except IOError:
        PriorRecs=[]
        if verbose:print "starting new specimen interpretation file: ",inspec
    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()
    backup=0
    # define figure numbers for arai, zijderveld and 
    #   de-,re-magization diagrams
    AZD={}
    AZD['deremag'], AZD['zijd'],AZD['arai'],AZD['eqarea']=1,2,3,4
    pmagplotlib.plot_init(AZD['arai'],5,5)
    pmagplotlib.plot_init(AZD['zijd'],5,5)
    pmagplotlib.plot_init(AZD['deremag'],5,5)
    pmagplotlib.plot_init(AZD['eqarea'],5,5)
    #
    #
    #
    # get list of unique specimen names
    #
    CurrRec=[]
    sids=pmag.get_specs(meas_data)
    # get plots for specimen s - default is just to step through arai diagrams
    #
    if spc!="": specimen =sids.index(spc)
    while specimen < len(sids):
        methcodes=[]
       
        if verbose:
            print sids[specimen],specimen+1, 'of ', len(sids)
        MeasRecs=[]
        s=sids[specimen]
        datablock,trmblock,tdsrecs=[],[],[]
        PmagSpecRec={}
        if first==0:
           for key in keys:PmagSpecRec[key]="" # make sure all new records have same set of keys
        PmagSpecRec["er_analyst_mail_names"]=user
        PmagSpecRec["specimen_correction"]='u'
    #
    # find the data from the meas_data file for this specimen
    #
        for rec in meas_data:
            if rec["er_specimen_name"]==s:
                MeasRecs.append(rec)
                if "magic_method_codes" not in rec.keys():
                    rec["magic_method_codes"]=""
                methods=rec["magic_method_codes"].split(":")
                meths=[]
                for meth in methods:
                    meths.append(meth.strip()) # take off annoying spaces
                methods=""
                for meth in meths:
                    if meth.strip() not in methcodes and "LP-" in meth:methcodes.append(meth.strip())
                    methods=methods+meth+":"
                methods=methods[:-1]
                rec["magic_method_codes"]=methods 
                if "LP-PI-TRM" in meths: datablock.append(rec)
                if "LP-TRM" in meths: trmblock.append(rec)
                if "LP-TRM-TD" in meths: tdsrecs.append(rec)
        if len(trmblock)>2 and inspec!="":
            if Tinit==0:
                Tinit=1
                AZD['TRM']=5
                pmagplotlib.plot_init(AZD['TRM'],5,5)
        elif Tinit==1: # clear the TRM figure if not needed
            pmagplotlib.clearFIG(AZD['TRM'])
        if len(tdsrecs)>2:
            if TDinit==0:
                TDinit=1
                AZD['TDS']=6
                pmagplotlib.plot_init(AZD['TDS'],5,5)
        elif TDinit==1: # clear the TDS figure if not needed
            pmagplotlib.clearFIG(AZD['TDS'])
        if len(datablock) <4:
           if backup==0:
               specimen+=1
               if verbose:
                   print 'skipping specimen - moving forward ', s
           else:
               specimen-=1
               if verbose:
                   print 'skipping specimen - moving backward ', s
    #
    #  collect info for the PmagSpecRec dictionary
    #
        else:
           rec=datablock[0]
           PmagSpecRec["er_citation_names"]="This study"
           PmagSpecRec["er_specimen_name"]=s
           PmagSpecRec["er_sample_name"]=rec["er_sample_name"]
           PmagSpecRec["er_site_name"]=rec["er_site_name"]
           PmagSpecRec["er_location_name"]=rec["er_location_name"]
           locname=rec['er_location_name'].replace('/','-')
           if "er_expedition_name" in rec.keys():PmagSpecRec["er_expedition_name"]=rec["er_expedition_name"]
           if "magic_instrument_codes" not in rec.keys():rec["magic_instrument_codes"]=""
           PmagSpecRec["magic_instrument_codes"]=rec["magic_instrument_codes"]
           PmagSpecRec["measurement_step_unit"]="K"
           if "magic_experiment_name" not in rec.keys():
               rec["magic_experiment_name"]=""
           else:
               PmagSpecRec["magic_experiment_names"]=rec["magic_experiment_name"]
    
           meths=rec["magic_method_codes"].split()
       # sort data into types
           araiblock,field=pmag.sortarai(datablock,s,Zdiff)
           first_Z=araiblock[0]
           GammaChecks=araiblock[5]
           if len(first_Z)<3:
               if backup==0:
                   specimen+=1
                   if verbose:
                       print 'skipping specimen - moving forward ', s
               else:
                   specimen-=1
                   if verbose:
                       print 'skipping specimen - moving backward ', s
           else:
               backup=0
               zijdblock,units=pmag.find_dmag_rec(s,meas_data)
               recnum=0
               if verbose:
                   print "index step Dec   Inc  Int       Gamma"
                   for plotrec in zijdblock:
                       if GammaChecks!="":
                           gamma=""
                           for g in GammaChecks:
                               if g[0]==plotrec[0]-273:
                                   gamma=g[1]
                                   break
                       if gamma!="":
                           print '%i     %i %7.1f %7.1f %8.3e %7.1f' % (recnum,plotrec[0]-273,plotrec[1],plotrec[2],plotrec[3],gamma)
                       else:
                           print '%i     %i %7.1f %7.1f %8.3e ' % (recnum,plotrec[0]-273,plotrec[1],plotrec[2],plotrec[3])
                       recnum += 1
               pmagplotlib.plotAZ(AZD,araiblock,zijdblock,s,units[0])
               if verbose:pmagplotlib.drawFIGS(AZD)
               if len(tdsrecs)>2: # a TDS experiment
                   tdsblock=[] # make a list for the TDS  data
                   Mkeys=['measurement_magnitude','measurement_magn_moment','measurement_magn_volume','measuruement_magn_mass']
                   mkey,k="",0
                   while mkey=="" and k<len(Mkeys)-1: # find which type of intensity
                       key= Mkeys[k]
                       if key in tdsrecs[0].keys() and tdsrecs[0][key]!="": mkey=key
                       k+=1
                   if mkey=="":break # get outta here
                   Tnorm=""
                   for tdrec in tdsrecs:
                       meths=tdrec['magic_method_codes'].split(":")
                       for meth in meths: meth.replace(" ","") # strip off potential nasty spaces
                       if  'LT-T-I' in meths and Tnorm=="": # found first total TRM 
                           Tnorm=float(tdrec[mkey]) # normalize by total TRM 
                           tdsblock.append([273,zijdblock[0][3]/Tnorm,1.]) # put in the zero step
                       if  'LT-T-Z' in meths and Tnorm!="": # found a LP-TRM-TD demag step, now need complementary LT-T-Z from zijdblock
                           step=float(tdrec['treatment_temp'])
                           Tint=""
                           if mkey!="":
                               Tint=float(tdrec[mkey])
                           if Tint!="":
                               for zrec in zijdblock:
                                   if zrec[0]==step:  # found matching
                                       tdsblock.append([step,zrec[3]/Tnorm,Tint/Tnorm])
                                       break
                   if len(tdsblock)>2: 
                       pmagplotlib.plotTDS(AZD['TDS'],tdsblock,s+':LP-PI-TDS:')
                       if verbose:pmagplotlib(drawFIGS(AZD)) 
                   else: 
                       print "Something wrong here"
               if anis==1:   # look up anisotropy data for this specimen
                   AniSpec=""
                   for aspec in anis_data:
                       if aspec["er_specimen_name"]==PmagSpecRec["er_specimen_name"]:
                           AniSpec=aspec
                           if verbose: print 'Found anisotropy record...'
                           break
               if inspec !="":
                   if verbose: print 'Looking up saved interpretation....'
                   found = 0
                   for k in range(len(PriorRecs)):
                       try:
                         if PriorRecs[k]["er_specimen_name"]==s:
                           found =1
                           CurrRec.append(PriorRecs[k])
                           for j in range(len(zijdblock)):
                               if float(zijdblock[j][0])==float(PriorRecs[k]["measurement_step_min"]):start=j
                               if float(zijdblock[j][0])==float(PriorRecs[k]["measurement_step_max"]):end=j
                           pars,errcode=pmag.PintPars(datablock,araiblock,zijdblock,start,end,accept)
                           pars['measurement_step_unit']="K"
                           pars['experiment_type']='LP-PI-TRM'
                           del PriorRecs[k]  # put in CurrRec, take out of PriorRecs
                           if errcode!=1:
                               pars["specimen_lab_field_dc"]=field
                               pars["specimen_int"]=-1*field*pars["specimen_b"]
                               pars["er_specimen_name"]=s
                               if verbose:
                                   print 'Saved interpretation: '
                               pars,kill=pmag.scoreit(pars,PmagSpecRec,accept,'',verbose)
                               pmagplotlib.plotB(AZD,araiblock,zijdblock,pars)
                               if verbose:pmagplotlib.drawFIGS(AZD)
                               if len(trmblock)>2:
                                   blab=field
                                   best=pars["specimen_int"]
                                   Bs,TRMs=[],[]
                                   for trec in trmblock:
                                       Bs.append(float(trec['treatment_dc_field']))
                                       TRMs.append(float(trec['measurement_magn_moment']))
                                   NLpars=nlt.NLtrm(Bs,TRMs,best,blab,0) # calculate best fit parameters through TRM acquisition data, and get new banc
                                   Mp,Bp=[],[]
                                   for k in  range(int(max(Bs)*1e6)):
                                       Bp.append(float(k)*1e-6)
                                       npred=nlt.TRM(Bp[-1],NLpars['xopt'][0],NLpars['xopt'][1]) # predicted NRM for this field
                                       Mp.append(npred)
                                   pmagplotlib.plotTRM(AZD['TRM'],Bs,TRMs,Bp,Mp,NLpars,trec['magic_experiment_name'])
                                   PmagSpecRec['specimen_int']=NLpars['banc'] 
                                   if verbose:
                                       print 'Banc= ',float(NLpars['banc'])*1e6
                                       pmagplotlib.drawFIGS(AZD)
                               mpars=pmag.domean(araiblock[1],start,end,'DE-BFL')
                               if verbose:
                                       print 'pTRM direction= ','%7.1f'%(mpars['specimen_dec']),' %7.1f'%(mpars['specimen_inc']),' MAD:','%7.1f'%(mpars['specimen_mad'])
                               if AniSpec!="":
                                   CpTRM=pmag.Dir_anis_corr([mpars['specimen_dec'],mpars['specimen_inc']],AniSpec)
                                   AniSpecRec=pmag.doaniscorr(PmagSpecRec,AniSpec)
                                   if verbose:
                                       print 'Anisotropy corrected TRM direction= ','%7.1f'%(CpTRM[0]),' %7.1f'%(CpTRM[1])
                                       print 'Anisotropy corrected intensity= ',float(AniSpecRec['specimen_int'])*1e6
                           else:
                               print 'error on specimen ',s
                       except:
                         pass
                   if verbose and found==0: print  '    None found :(  ' 
               if spc!="":
                   if BEG!="": 
                       pars,errcode=pmag.PintPars(datablock,araiblock,zijdblock,BEG,END,accept)
                       pars['measurement_step_unit']="K"
                       pars["specimen_lab_field_dc"]=field
                       pars["specimen_int"]=-1*field*pars["specimen_b"]
                       pars["er_specimen_name"]=s
                       pars['specimen_grade']='' # ungraded
                       pmagplotlib.plotB(AZD,araiblock,zijdblock,pars)
                       if verbose:pmagplotlib.drawFIGS(AZD)
                       if len(trmblock)>2:
                           if inlt==0:
                               inlt=1
                           blab=field
                           best=pars["specimen_int"]
                           Bs,TRMs=[],[]
                           for trec in trmblock:
                               Bs.append(float(trec['treatment_dc_field']))
                               TRMs.append(float(trec['measurement_magn_moment']))
                           NLpars=nlt.NLtrm(Bs,TRMs,best,blab,0) # calculate best fit parameters through TRM acquisition data, and get new banc
    #
                           Mp,Bp=[],[]
                           for k in  range(int(max(Bs)*1e6)):
                               Bp.append(float(k)*1e-6)
                               npred=nlt.TRM(Bp[-1],NLpars['xopt'][0],NLpars['xopt'][1]) # predicted NRM for this field
                   files={}
                   for key in AZD.keys():
                       files[key]=s+'_'+key+fmt 
                   pmagplotlib.saveP(AZD,files)
                   sys.exit()
               if verbose:
                   ans='b'
                   while ans != "":
                       print """
               s[a]ve plot, set [b]ounds for calculation, [d]elete current interpretation, [p]revious, [s]ample, [q]uit:
               """
                       ans=raw_input('Return for next specimen \n')
                       if ans=="": 
                           specimen +=1
                       if ans=="d": 
                           save_redo(PriorRecs,inspec)
                           CurrRec=[]
                           pmagplotlib.plotAZ(AZD,araiblock,zijdblock,s,units[0])
                           if verbose:pmagplotlib.drawFIGS(AZD)
                       if ans=='a':
                           files={}
                           for key in AZD.keys():
                               files[key]="LO:_"+locname+'_SI:_'+PmagSpecRec['er_site_name']+'_SA:_'+PmagSpecRec['er_sample_name']+'_SP:_'+s+'_CO:_s_TY:_'+key+fmt
                           pmagplotlib.saveP(AZD,files)
                           ans=""
                       if ans=='q':
                           print "Good bye"
                           sys.exit()
                       if ans=='p':
                           specimen =specimen -1
                           backup = 1
                           ans=""
                       if ans=='s':
                           keepon=1
                           spec=raw_input('Enter desired specimen name (or first part there of): ')
                           while keepon==1:
                               try:
                                   specimen =sids.index(spec)
                                   keepon=0
                               except:
                                   tmplist=[]
                                   for qq in range(len(sids)):
                                       if spec in sids[qq]:tmplist.append(sids[qq])
                                   print specimen," not found, but this was: "
                                   print tmplist
                                   spec=raw_input('Select one or try again\n ')
                           ans=""
                       if  ans=='b':
                           if end==0 or end >=len(zijdblock):end=len(zijdblock)-1
                           GoOn=0
                           while GoOn==0:
                               answer=raw_input('Enter index of first point for calculation: ['+str(start)+']  ')
                               try:
                                   start=int(answer)
                                   answer=raw_input('Enter index  of last point for calculation: ['+str(end)+']  ')
                                   end=int(answer)
                                   if start >=0 and start <len(zijdblock)-2 and end >0 and end <len(zijdblock) or start>=end:
                                       GoOn=1
                                   else:
                                       print "Bad endpoints - try again! "
                                       start,end=0,len(zijdblock)
                               except ValueError:
                                   print "Bad endpoints - try again! "
                                   start,end=0,len(zijdblock)
                           s=sids[specimen] 
                           pars,errcode=pmag.PintPars(datablock,araiblock,zijdblock,start,end,accept)
                           pars['measurement_step_unit']="K"
                           pars["specimen_lab_field_dc"]=field
                           pars["specimen_int"]=-1*field*pars["specimen_b"]
                           pars["er_specimen_name"]=s
                           pars,kill=pmag.scoreit(pars,PmagSpecRec,accept,'',0)
                           PmagSpecRec['specimen_scat']=pars['specimen_scat']
                           PmagSpecRec['specimen_frac']='%5.3f'%(pars['specimen_frac'])
                           PmagSpecRec['specimen_gmax']='%5.3f'%(pars['specimen_gmax'])
                           PmagSpecRec["measurement_step_min"]='%8.3e' % (pars["measurement_step_min"])
                           PmagSpecRec["measurement_step_max"]='%8.3e' % (pars["measurement_step_max"])
                           PmagSpecRec["measurement_step_unit"]="K"
                           PmagSpecRec["specimen_int_n"]='%i'%(pars["specimen_int_n"])
                           PmagSpecRec["specimen_lab_field_dc"]='%8.3e'%(pars["specimen_lab_field_dc"])
                           PmagSpecRec["specimen_int"]='%9.4e '%(pars["specimen_int"])
                           PmagSpecRec["specimen_b"]='%5.3f '%(pars["specimen_b"])
                           PmagSpecRec["specimen_q"]='%5.1f '%(pars["specimen_q"])
                           PmagSpecRec["specimen_f"]='%5.3f '%(pars["specimen_f"])
                           PmagSpecRec["specimen_fvds"]='%5.3f'%(pars["specimen_fvds"])
                           PmagSpecRec["specimen_b_beta"]='%5.3f'%(pars["specimen_b_beta"])
                           PmagSpecRec["specimen_int_mad"]='%7.1f'%(pars["specimen_int_mad"])
                           PmagSpecRec["specimen_Z"]='%7.1f'%(pars["specimen_Z"])
                           PmagSpecRec["specimen_gamma"]='%7.1f'%(pars["specimen_gamma"])
                           PmagSpecRec["specimen_grade"]=pars["specimen_grade"]
                           if pars["method_codes"]!="":
                               tmpcodes=pars["method_codes"].split(":")
                               for t in tmpcodes:
                                   if t.strip() not in methcodes:methcodes.append(t.strip())
                           PmagSpecRec["specimen_dec"]='%7.1f'%(pars["specimen_dec"])
                           PmagSpecRec["specimen_inc"]='%7.1f'%(pars["specimen_inc"])
                           PmagSpecRec["specimen_tilt_correction"]='-1'
                           PmagSpecRec["specimen_direction_type"]='l'
                           PmagSpecRec["direction_type"]='l' # this is redundant, but helpful - won't be imported
                           PmagSpecRec["specimen_int_dang"]='%7.1f '%(pars["specimen_int_dang"])
                           PmagSpecRec["specimen_drats"]='%7.1f '%(pars["specimen_drats"])
                           PmagSpecRec["specimen_drat"]='%7.1f '%(pars["specimen_drat"])
                           PmagSpecRec["specimen_int_ptrm_n"]='%i '%(pars["specimen_int_ptrm_n"])
                           PmagSpecRec["specimen_rsc"]='%6.4f '%(pars["specimen_rsc"])
                           PmagSpecRec["specimen_md"]='%i '%(int(pars["specimen_md"]))
                           if PmagSpecRec["specimen_md"]=='-1':PmagSpecRec["specimen_md"]=""
                           PmagSpecRec["specimen_b_sigma"]='%5.3f '%(pars["specimen_b_sigma"])
                           if "IE-TT" not in  methcodes:methcodes.append("IE-TT")
                           methods=""
                           for meth in methcodes:
                               methods=methods+meth+":"
                           PmagSpecRec["magic_method_codes"]=methods[:-1]
                           PmagSpecRec["specimen_description"]=comment
                           PmagSpecRec["magic_software_packages"]=version_num
                           pmagplotlib.plotAZ(AZD,araiblock,zijdblock,s,units[0])
                           pmagplotlib.plotB(AZD,araiblock,zijdblock,pars)
                           if verbose:pmagplotlib.drawFIGS(AZD)
                           if len(trmblock)>2:
                               blab=field
                               best=pars["specimen_int"]
                               Bs,TRMs=[],[]
                               for trec in trmblock:
                                   Bs.append(float(trec['treatment_dc_field']))
                                   TRMs.append(float(trec['measurement_magn_moment']))
                               NLpars=nlt.NLtrm(Bs,TRMs,best,blab,0) # calculate best fit parameters through TRM acquisition data, and get new banc
                               Mp,Bp=[],[]
                               for k in  range(int(max(Bs)*1e6)):
                                   Bp.append(float(k)*1e-6)
                                   npred=nlt.TRM(Bp[-1],NLpars['xopt'][0],NLpars['xopt'][1]) # predicted NRM for this field
                                   Mp.append(npred)
                               pmagplotlib.plotTRM(AZD['TRM'],Bs,TRMs,Bp,Mp,NLpars,trec['magic_experiment_name'])
                               if verbose:
                                   print 'Non-linear TRM corrected intensity= ',float(NLpars['banc'])*1e6
                           if verbose:pmagplotlib.drawFIGS(AZD)
                           pars["specimen_lab_field_dc"]=field
                           pars["specimen_int"]=-1*field*pars["specimen_b"]
                           pars,kill=pmag.scoreit(pars,PmagSpecRec,accept,'',verbose)
                           saveit=raw_input("Save this interpretation? [y]/n \n")
                           if saveit!='n':
                               PriorRecs.append(PmagSpecRec) # put back an interpretation
                               specimen+=1
                               save_redo(PriorRecs,inspec)
                           ans=""
               elif plots==1:
                   specimen+=1
                   if fmt != ".pmag":
                       files={}
                       for key in AZD.keys():
                           files[key]="LO:_"+locname+'_SI:_'+PmagSpecRec['er_site_name']+'_SA:_'+PmagSpecRec['er_sample_name']+'_SP:_'+s+'_CO:_s_TY:_'+key+'_'+fmt
                       if pmagplotlib.isServer:
                           black     = '#000000'
                           purple    = '#800080'
                           titles={}
                           titles['deremag']='DeReMag Plot'
                           titles['zijd']='Zijderveld Plot'
                           titles['arai']='Arai Plot'
                           AZD = pmagplotlib.addBorders(AZD,titles,black,purple)
                       pmagplotlib.saveP(AZD,files)
    #                   pmagplotlib.combineFigs(s,files,3)
                   else:  # save in pmag format 
                       script="grep "+s+" output.mag | thellier -mfsi"
                       script=script+' %8.4e'%(field)
                       min='%i'%((pars["measurement_step_min"]-273))
                       Max='%i'%((pars["measurement_step_max"]-273))
                       script=script+" "+min+" "+Max
                       script=script+" |plotxy;cat mypost >>thellier.ps\n"
                       pltf.write(script)
                       pmag.domagicmag(outf,MeasRecs)
        if len(CurrRec)>0:
            for rec in CurrRec:
                PriorRecs.append(rec)
        CurrRec=[]
    if plots!=1 and verbose:
        ans=raw_input(" Save last plot? 1/[0] ")
        if ans=="1":
            if fmt != ".pmag":
                files={}
                for key in AZD.keys():
                    files[key]=s+'_'+key+fmt
                pmagplotlib.saveP(AZD,files)
        else:
            print "\n Good bye\n"
            sys.exit()
        if len(CurrRec)>0:PriorRecs.append(CurrRec) # put back an interpretation
        if len(PriorRecs)>0:
            save_redo(PriorRecs,inspec)
            print 'Updated interpretations saved in ',inspec
    if verbose:
        print "Good bye"
Example #7
0
def main():
    """
    NAME
        LIVMW_magic.py
 
    DESCRIPTION
        converts Liverpool microwave format files to magic_measurements format files

    SYNTAX
        LIVMW_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -f FILE: specify liverpool format input file, required
        -usr USER:   identify user, default is ""
        -ins INST:   identify instrument, e.g., LIV-TRISTAN, LIV-OLD14GHZ, default is ""
        -loc LOCNAME : specify location/study name, required
        -F FILE: specify output file, default is magic_measurements.txt
        -Fsa FILE: specify er_samples formatted  file for appending, default is new er_samples.txt
        -spc NUM : specify number of characters to designate a  specimen, default = 1
        -sit Site_name : specify site name for this specimen
        -unc measurement units are uncalibrated (default is uAm^2)
        -B PHI THETA: dc lab field phi, theta, default is 0, 90
        -ncn NCON:  specify naming convention  - required if -sit not specified
       Sample naming convention:
            do not use if -sit option used!
            [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] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name same as sample
            [6] site is entered under a separate column
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.
 

    """
    # initialize some stuff
    version_num = pmag.get_version()
    noave = 0
    methcode, instcode = "", ""
    phi, theta, peakfield, labfield = 0, 0, 0, 0
    pMRM, MD, samp_con, Z, site = 0, 0, "6", "", ""
    er_location_name = ""
    citation = "This study"
    args = sys.argv
    methcode = "LP-NO"  # NRM
    specnum, measnum = 1, 1
    powt_max = 0
    ErSamps, Samps = [], []
    #
    # get command line arguments
    #
    dirpath = "."
    meas_file, samp_file = dirpath + "/magic_measurements.txt", dirpath + "/er_samples.txt"
    user = ""
    unc = 0
    if "-WD" in args:
        ind = args.index("-WD")
        dirpath = args[ind + 1]
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-usr" in args:
        ind = args.index("-usr")
        user = args[ind + 1]
    if "-ins" in args:
        ind = args.index("-ins")
        instcode = args[ind + 1]
    if "-F" in args:
        ind = args.index("-F")
        meas_file = dirpath + "/" + args[ind + 1]
    if "-Fsa" in args:
        ind = args.index("-Fsa")
        samp_file = args[ind + 1]
        samp_file = dirpath + "/" + samp_file
        try:
            open(samp_file, "rU")
            ErSamps, file_type = pmag.magic_read(samp_file)
            print "sample information will be appended to new er_samples.txt file"
        except:
            print "er_samples.txt file does not exist"
            print "sample information will be stored in new er_samples.txt file"
    if "-f" in args:
        ind = args.index("-f")
        magfile = args[ind + 1]
        magfile = dirpath + "/" + magfile
        try:
            input = open(magfile, "rU")
        except:
            print "bad mag file name"
            sys.exit()
    else:
        print "mag_file field is required option"
        print main.__doc__
        sys.exit()
    if "-B" in args:
        ind = args.index("-B")
        phi = args[ind + 1]
        theta = args[ind + 2]
    else:
        phi, theta = "0.", "90."
    if "-spc" in args:
        ind = args.index("-spc")
        specnum = int(args[ind + 1])
        if specnum != 0:
            specnum = -specnum
    if "-loc" in args:
        ind = args.index("-loc")
        er_location_name = args[ind + 1]
    if "-unc" in args:
        unc = 1
    if "-sit" in args:
        ind = args.index("-sit")
        site = args[ind + 1]
    if "-ncn" in args:
        ind = args.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 "7" in samp_con:
            if "-" not in samp_con:
                print "option [7] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z = samp_con.split("-")[1]
                samp_con = "7"
    if samp_con == "6" and site == "":
        print "you must either specify a naming convention, or a site name"
        print main.__doc__
        sys.exit()
    MagRecs = []
    if len(ErSamps) > 1:
        for samp in ErSamps:
            if samp["er_sample_name"] not in Samps:
                Samps.append(samp["er_sample_name"])
    Data = input.readlines()
    if 1:  # never mind
        for line in Data:
            if len(line) > 1:
                rec = line.split(",")
                MagRec = {}
                MagRec["er_citation_names"] = "This study"
                MagRec["er_location_name"] = er_location_name
                MagRec["magic_software_packages"] = version_num
                MagRec["treatment_temp"] = "%8.3e" % (273)  # room temp in kelvin
                MagRec["measurement_temp"] = "%8.3e" % (273)  # room temp in kelvin
                MagRec["treatment_ac_field"] = "0"
                MagRec["treatment_dc_field"] = "0"
                MagRec["treatment_dc_field_phi"] = ""
                MagRec["treatment_dc_field_theta"] = ""
                MagRec["treatment_mw_integral"] = ""
                meas_type = "LT-NO"
                MagRec["er_specimen_name"] = rec[0][1:-1]
                MagRec["er_location_name"] = er_location_name
                if specnum != 0:
                    MagRec["er_sample_name"] = rec[0][1:-1][:specnum]
                else:
                    MagRec["er_sample_name"] = rec[0][1:-1]
                if site == "":
                    site = pmag.parse_site(MagRec["er_sample_name"], samp_con, Z)
                MagRec["er_site_name"] = site
                MagRec["treatment_mw_power"] = rec[2]
                MagRec["treatment_mw_time"] = rec[3]
                powt = int(float(MagRec["treatment_mw_power"]) * (float(MagRec["treatment_mw_time"])))
                MagRec["treatment_mw_energy"] = "%7.1f" % (powt)
                if powt > powt_max:
                    powt_max = powt
                treat = rec[1].strip('"').upper()
                if treat == "Z":  #  in zero field
                    meas_type = "LT-M-Z"  #  as opposed to LT-MV-Z
                    if powt < powt_max:
                        meas_type = "LT-PMRM-Z"
                elif treat == "A":  #  in zero field
                    meas_type = "LT-M-I"  #  as opposed to LT-MV-I
                    labfield = float(rec[10]) * 1e-6  # assuming uT, convert to T
                    MagRec["treatment_dc_field"] = "%8.3e" % (labfield)  # labfield in tesla (convert from microT)
                    MagRec["treatment_dc_field_phi"] = phi  # labfield phi
                    MagRec["treatment_dc_field_theta"] = theta  # labfield theta
                    if powt < powt_max:
                        meas_type = "LT-PMRM-I"
                if len(rec) > 10:
                    MagRec["treatment_mw_integral"] = rec[10]
                if unc == 0:
                    MagRec["measurement_magn_moment"] = "%10.3e" % (float(rec[4]) * 1e-6)  # moment in Am^2 (from uAm^2)
                if unc == 1:
                    MagRec["measurement_magnitude"] = rec[4]  # uncalibrated moment
                cart = []
                cart.append(float(rec[7]))
                cart.append(float(rec[8]))
                cart.append(float(rec[9]))
                dir = pmag.cart2dir(cart)
                MagRec["measurement_dec"] = "%9.3f" % (dir[0])
                MagRec["measurement_inc"] = "%9.3f" % (dir[1])
                MagRec["magic_instrument_codes"] = instcode
                MagRec["magic_method_codes"] = meas_type
                MagRec["measurement_flag"] = "g"
                MagRec["measurement_standard"] = "u"
                MagRec["measurement_number"] = "%i" % (measnum)
                MagRec["magic_experiment_name"] = MagRec["er_specimen_name"] + ":" + methcode
                measnum += 1
                MagRecs.append(MagRec)
                if MagRec["er_sample_name"] not in Samps:  # add this puppy to the list in er_samples.txt
                    Samps.append(MagRec["er_sample_name"])
                    ErSamp = {}
                    ErSamp["er_sample_name"] = Samps[-1]
                    ErSamp["er_location_name"] = MagRec["er_location_name"]
                    ErSamp["er_site_name"] = site
                    ErSamp["er_citation_names"] = "This study"
                    gdec = float(rec[5])
                    ginc = float(rec[6])
                    az, pl = pmag.get_azpl(dir[0], dir[1], gdec, ginc)
                    ErSamp["sample_azimuth"] = "%7.1f" % az
                    ErSamp["sample_dip"] = "%7.1f" % pl
                    ErSamps.append(ErSamp)
    MagOuts = pmag.mw_measurements_methods(MagRecs)
    pmag.magic_write(meas_file, MagOuts, "magic_measurements")
    print "measurements put in ", meas_file
    pmag.magic_write(samp_file, ErSamps, "er_samples")
    print "sample names put in ", samp_file
Example #8
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 #9
0
def main():
    """
    NAME
        ODP_dcs_magic.py
 
    DESCRIPTION
        converts ODP discrete sample format files to magic_measurements format files

    SYNTAX
        ODP_dsc_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -F FILE: specify output  measurements file, default is magic_measurements.txt
        -Fsp FILE: specify output er_specimens.txt file, default is er_specimens.txt
        -Fsa FILE: specify output er_samples.txt file for appending, default is er_samples.txt
        -Fsi FILE: specify output er_sites.txt file, default is er_sites.txt
        -dc B PHI THETA: dc lab field (in micro tesla) and phi,theta, default is none
              NB: use PHI, THETA = -1 -1 to signal that it changes, i.e. in anisotropy experiment
        -ac B : peak AF field (in mT) for ARM acquisition, default is none
        -A : don't average replicate measurements
    INPUT
        Put data from separate experiments (all AF, thermal, thellier, trm aquisition, Shaw, etc.)  in separate directory

    """
#        
#
    version_num=pmag.get_version()
    meas_file='magic_measurements.txt'
    spec_file='er_specimens.txt'
    samp_file='er_samples.txt'
    site_file='er_sites.txt'
    ErSpecs,ErSamps,ErSites,ErLocs,ErCits=[],[],[],[],[]
    MagRecs=[]
    citation="This study"
    dir_path,demag='.','NRM'
    args=sys.argv
    noave=0
    if '-WD' in args:
        ind=args.index("-WD")
        dir_path=args[ind+1]
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-A" in args: noave=1
    if '-F' in args:
        ind=args.index("-F")
        meas_file=args[ind+1]
    if '-Fsp' in args:
        ind=args.index("-Fsp")
        spec_file=args[ind+1]
    if '-Fsa' in args:
        ind=args.index("-Fsa")
        samp_file=dir_path+'/'+args[ind+1]
        ErSamps,file_type=pmag.magic_read(samp_file)
    else:
        samp_file=dir_path+'/'+samp_file
    if '-LP' in args:
        ind=args.index("-LP")
        codelist=args[ind+1]
        codes=codelist.split(':')
        if "AF" in codes:
            demag='AF' 
            if'-dc' not in args: methcode="LT-AF-Z"
            if'-dc' in args: methcode="LT-AF-I"
        if "T" in codes:
            demag="T"
            if '-dc' not in args: methcode="LT-T-Z"
            if '-dc' in args: methcode="LT-T-I"
        if "I" in codes:
            methcode="LP-IRM"
        if "S" in codes: 
            demag="S"
            methcode="LP-PI-TRM:LP-PI-ALT-AFARM"
            trm_labfield=labfield
            ans=raw_input("DC lab field for ARM step: [50uT] ")
            if ans=="":
                arm_labfield=50e-6
            else: 
                arm_labfield=float(ans)*1e-6
            ans=raw_input("temperature for total trm step: [600 C] ")
            if ans=="":
                trm_peakT=600+273 # convert to kelvin
            else: 
                trm_peakT=float(ans)+273 # convert to kelvin
        if "G" in codes: methcode="LT-AF-G"
	if "D" in codes: methcode="LT-AF-D"
        if "TRM" in codes: 
            demag="T"
            trm=1
    if demag=="T" and "ANI" in codes:
        methcode="LP-AN-TRM"
    if demag=="AF" and "ANI" in codes:
        methcode="LP-AN-ARM"
        if labfield==0: labfield=50e-6
        if peakfield==0: peakfield=.180
    spec_file=dir_path+'/'+spec_file
    site_file=dir_path+'/'+site_file
    meas_file=dir_path+'/'+meas_file
    filelist=os.listdir(dir_path) # read in list of files to import
    specimens,samples,sites=[],[],[]
    MagRecs,SpecRecs,SampRecs=[],[],[]
    for samp in ErSamps:
        if samp['er_sample_name'] not in samples:
            samples.append(samp['er_sample_name'])
            SampRecs.append(samp)
    for file in filelist: # parse each file
        if file[-3:].lower()=='dsc':
            print 'processing: ',file
            MagRec,SpecRec,SampRec={},{},{}
            treatment_type,treatment_value,user="","",""
            inst="ODP-SRM"
            input=open(dir_path+'/'+file,'rU').readlines()
            IDs=file.split('_') # splits on underscores
            pieces=IDs[0].split('-')
            expedition=pieces[0]
            location=pieces[1]
            if file[0]!='_':
                while len(pieces[2])<4:pieces[2]='0'+pieces[2] # pad core to be 3 characters
                specimen=""
            else:
                specimen="test"
            for piece in pieces:
                specimen=specimen+piece+'-'
            specimen=specimen[:-1]
            alt_spec=IDs[1] # alternate specimen is second field in field name
# set up specimen record for Er_specimens table
            SpecRec['er_expedition_name']=expedition
            SpecRec['er_location_name']=location
            SpecRec['er_site_name']=specimen
            SpecRec['er_sample_name']=specimen
            SpecRec['er_citation_names']=citation
            for key in SpecRec.keys():SampRec[key]=SpecRec[key]
            SampRec['sample_azimuth']='0'
            SampRec['sample_dip']='0'
            SampRec['magic_method_codes']='FS-C-DRILL-IODP:FS-SS-C:SO-V'
            SpecRec['er_specimen_name']=specimen
            for key in SpecRec.keys():MagRec[key]=SpecRec[key]
# set up measurement record - default is NRM 
            MagRec['er_analyst_mail_names']=user
            MagRec['magic_method_codes']='LT-NO'
            MagRec['magic_software_packages']=version_num
            MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
            MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
            MagRec["treatment_ac_field"]=0.
            MagRec["treatment_dc_field"]='0'
            MagRec["treatment_dc_field_phi"]='0'
            MagRec["treatment_dc_field_theta"]='0'
            MagRec["measurement_flag"]='g' # assume all data are "good"
            MagRec["measurement_standard"]='u' # assume all data are "good"
            MagRec["measurement_csd"]='' # set csd to blank
            SpecRec['er_specimen_alternatives']=alt_spec
            vol=7e-6 # assume 7 cc samples
            datestamp=input[1].split() # date time is second line of file
            mmddyy=datestamp[0].split('/') # break into month day year
            date=mmddyy[2]+':'+mmddyy[0]+":"+mmddyy[1] +':' +datestamp[1]
            MagRec["measurement_date"]=date
            for k in range(len(input)):
                fields= input[k].split("=")
                if 'treatment_type' in fields[0]:
                    if "Alternating Frequency Demagnetization" in fields[1]:
                        MagRec['magic_method_codes'] = 'LT-AF-Z'
                        inst=inst+':ODP-DTECH' # measured on shipboard AF DTECH D2000
                        treatment_type="AF"
                    if "Anhysteretic Remanent Magnetization" in fields[1]:
                        MagRec['magic_method_codes'] = 'LT-AF-I'
                        inst=inst+':ODP-DTECH' # measured on shipboard AF DTECH D2000
                        treatment_type="ARM"
                    if "Isothermal Remanent Magnetization" in fields[1]:
                        MagRec['magic_method_codes'] = 'LT-IRM'
                        inst=inst+':ODP-IMP' # measured on shipboard ASC IMPULSE magnetizer
                        treatment_type="IRM"
                if "treatment_value" in fields[0]:
                    values=fields[1].split(',')
                    value=values[0]
                    if value!=" \n":
                        if treatment_type=="AF":
                            treatment_value=float(value)*1e-3
                            MagRec["treatment_ac_field"]=treatment_value # AF demag in treat mT => T
                        elif treatment_type=="IRM":
                            treatment_value=float(value)*1e-3
                            MagRec["treatment_dc_field"]='%8.3e'%(treatment_value) # IRM treat mT => T
                        if treatment_type=="ARM":
                            treatment_value=float(value)*1e-3
                            dc_value=float(values[1])*1e-3
                            MagRec["treatment_ac_field"]=treatment_value # AF demag in treat mT => T
                            MagRec["treatment_dc_field"]='%8.3e'%(dc_value) # DC mT => T
                if 'user' in fields[0]: 
                    user=fields[-1]
                    MagRec["er_analyst_mail_names"]=user
                if 'sample_orientation' in fields[0]: 
                    MagRec["measurement_description"]=fields[-1]
                MagRec["measurement_standard"]='u' # assume all data are "good"
                if 'sample_area' in fields[0]:  vol=float(fields[1])*1e-6 # takes volume (cc) and converts to m^3
                if 'run_number' in fields[0]:  
                    MagRec['external_database_ids']=fields[1] # run number is the LIMS measurement number
                    MagRec['external_database_names']='LIMS'
                if input[k][0:7]=='<MULTI>':
                    rec=input[k+1].split(',') # list of data
                    for item in rec:
                        items=item.split('=')
                        if items[0].strip()=='demag_level' and  treatment_value=="" :
                            treat= float(items[1])
                            if treat!=0:
                                MagRec['magic_method_codes']='LT-AF-Z'
                                inst=inst+':ODP-SRM-AF'
                                MagRec["treatment_ac_field"]=treat*1e-3 # AF demag in treat mT => T
                        if items[0].strip()=='inclination_w_tray_w_bkgrd': MagRec['measurement_inc']=items[1]
                        if items[0].strip()=='declination_w_tray_w_bkgrd': MagRec['measurement_dec']=items[1]
                        if items[0].strip()=='intensity_w_tray_w_bkgrd': MagRec['measurement_magn_moment']='%8.3e'%(float(items[1])*vol) # convert intensity from A/m to Am^2 using vol
                        if items[0].strip()=='x_stdev':MagRec['measurement_x_sd']=items[1]
                        if items[0].strip()=='y_stdev':MagRec['measurement_y_sd']=items[1]
                        if items[0].strip()=='z_stdev':MagRec['measurement_z_sd']=items[1]
                        MagRec['magic_instrument_codes']=inst
                        MagRec['measurement_number']='1'
            MagRecs.append(MagRec)
            if specimen not in specimens:
                specimens.append(specimen)
                SpecRecs.append(SpecRec)
            if MagRec['er_sample_name'] not in samples:
                samples.append(MagRec['er_sample_name'])
                SampRecs.append(SampRec)
    MagOuts=pmag.sort_diclist(MagRecs,'treatment_ac_field')
    for MagRec in MagOuts:
        MagRec["treatment_ac_field"]='%8.3e'%(MagRec["treatment_ac_field"]) # convert to string
    pmag.magic_write(spec_file,SpecRecs,'er_specimens')
    if len(SampRecs)>0:
        SampOut,keys=pmag.fillkeys(SampRecs)
        pmag.magic_write(samp_file,SampOut,'er_samples')
        print 'samples stored in ',samp_file
    pmag.magic_write(samp_file,SampRecs,'er_samples')
    print 'specimens stored in ',spec_file
    Fixed=pmag.measurements_methods(MagOuts,noave)
    pmag.magic_write(meas_file,Fixed,'magic_measurements')
    print 'data stored in ',meas_file
Example #10
0
def main():
    """
    NAME
        convert_samples.py
   
    DESCRIPTION
        takes an er_samples.txt file and creates an orient.txt file
 
    SYNTAX
        convert_samples.py [command line options]

    OPTIONS
        -f FILE: specify input file, default is er_samples.txt
        -F FILE: specify output file, default is: location_orient.txt 

    INPUT FORMAT
        er_samples.txt formatted file
    OUTPUT
        orient.txt formatted file
    """
    #
    # initialize variables
    #
    version_num=pmag.get_version()
    orient_file,samp_file = "orient","er_samples.txt"
    args=sys.argv
    dir_path='.'
    #
    #
    if '-WD' in args:
        ind=args.index('-WD')
        dir_path=args[ind+1]
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-F" in args:
        ind=args.index("-F")
        opath=sys.argv[ind+1]
        pathlist=opath.split('/')
        opath=""
        for  p in pathlist[:-1]:opath=opath+p+"/"
        orient_file=pathlist[-1]
    if "-f" in args:
        ind=args.index("-f")
        samp_file=dir_path+'/'+sys.argv[ind+1]
    #
    # read in file to convert
    #
    Samps,file_type=pmag.magic_read(samp_file)
    Locs=[]
    OrKeys=['sample_name','mag_azimuth','field_dip','sample_class','sample_type','sample_lithology','lat','long','stratigraphic_height','method_codes','site_name','site_description']
    SampKeys=['er_sample_name','sample_azimuth','sample_dip','sample_class','sample_type','sample_lithology','sample_lat','sample_lon','sample_height','magic_method_codes','er_site_name','er_sample_description']

    for samp in Samps:
        if samp['er_location_name'] not in Locs:Locs.append(samp['er_location_name']) # get all the location names
    for location_name in Locs:
        OrOut=[]
        for samp in Samps:
            if samp['er_location_name']==location_name:
                OrRec={}
                if 'sample_date' in samp.keys() and samp['sample_date'].strip()!="":
                    date=samp['sample_date'].split(':')
                    OrRec['date']=date[1]+'/'+date[2]+'/'+date[0][2:4]
                for i in range(len(SampKeys)): 
                   if SampKeys[i] in samp.keys():OrRec[OrKeys[i]]=samp[SampKeys[i]]
                OrOut.append(OrRec)
        loc=location_name.replace(" ","_") 
        outfile=opath+orient_file+'_'+loc+'.txt'
        pmag.magic_write(outfile,OrOut,location_name)
        print "Data saved in: ", outfile
Example #11
0
def main():
    """
    NAME
        LDGO_magic.py
 
    DESCRIPTION
        converts LDGO  format files to magic_measurements format files

    SYNTAX
        LDGO_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify .ldgo format input file, required
        -fsa SAMPFILE : specify er_samples.txt file relating samples, site and locations names,default is none
        -F FILE: specify output file, default is magic_measurements.txt
        -Fsy: specify er_synthetics file, default is er_sythetics.txt
        -Fsa: specify output er_samples file, default is NONE (only for LDGO formatted files)
        -LP [colon delimited list of protocols, include all that apply]
            AF:  af demag
            T: thermal including thellier but not trm acquisition
            S: Shaw method
            I: IRM (acquisition)
            N: NRM only
            TRM: trm acquisition
            ANI: anisotropy experiment
            D: double AF demag
            G: triple AF demag (GRM protocol)
        -V [1,2,3] units of IRM field in volts using ASC coil #1,2 or 3
        -spc NUM : specify number of characters to designate a  specimen, default = 0
        -loc LOCNAME : specify location/study name, must have either LOCNAME or SAMPFILE or be a synthetic
        -syn INST TYPE:  sets these specimens as synthetics created at institution INST and of type TYPE
        -ins INST : specify which demag instrument was used (e.g, SIO-Suzy or SIO-Odette),default is ""
        -dc B PHI THETA: dc lab field (in micro tesla) and phi,theta, default is none
              NB: use PHI, THETA = -1 -1 to signal that it changes, i.e. in anisotropy experiment
        -ac B : peak AF field (in mT) for ARM acquisition, default is none
        -ncn NCON:  specify naming convention: default is #1 below
        -A: don't average replicate measurements
       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] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name same as sample
            [6] site is entered under a separate column
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.
 
            [8] synthetic - has no site name
    INPUT
        Best to put separate experiments (all AF, thermal, thellier, trm aquisition, Shaw, etc.) in 
           seperate .mag files (eg. af.mag, thermal.mag, etc.)

        Format of LDEO files:   
isaf2.fix       
LAT:   .00  LON:    .00
    ID     TREAT  I  CD    J    CDECL CINCL  GDECL GINCL  BDECL BINCL  SUSC  M/V
________________________________________________________________________________
is031c2       .0  SD  0 461.600 163.9  17.5  337.1  74.5  319.1  74.4    .0   .0
        
        ID: specimen name
        TREAT:  treatment step
        I:  Instrument 
        CD:  Circular standard devation
        J: intensity.  assumed to be total moment in 10^-4 (emu)
        CDECL:  Declination in specimen coordinate system
        CINCL:  Declination in specimen coordinate system
        GDECL:  Declination in geographic coordinate system
        GINCL:  Declination in geographic coordinate system
        BDECL:  Declination in bedding adjusted coordinate system
        BINCL:  Declination in bedding adjusted coordinate system
        SUSC:  magnetic susceptibility (in micro SI)a
        M/V: mass or volume for nomalizing (0 won't normalize)
     
    """
    # initialize some stuff
    noave = 0
    methcode, inst = "LP-NO", ""
    phi, theta, peakfield, labfield = 0, 0, 0, 0
    pTRM, MD, samp_con, Z = 0, 0, '1', 1
    dec = [315, 225, 180, 135, 45, 90, 270, 270, 270, 90, 180, 180, 0, 0, 0]
    inc = [0, 0, 0, 0, 0, -45, -45, 0, 45, 45, 45, -45, -90, -45, 45]
    tdec = [0, 90, 0, 180, 270, 0, 0, 90, 0]
    tinc = [0, 0, 90, 0, 0, -90, 0, 0, 90]
    missing = 1
    demag = "N"
    er_location_name = ""
    citation = 'This study'
    args = sys.argv
    fmt = 'old'
    syn = 0
    synfile = 'er_synthetics.txt'
    samp_file, ErSamps = '', []
    trm = 0
    irm = 0
    specnum = 0
    coil = ""
    #
    # get command line arguments
    #
    meas_file = "magic_measurements.txt"
    user = ""
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-usr" in args:
        ind = args.index("-usr")
        user = args[ind + 1]
    if '-F' in args:
        ind = args.index("-F")
        meas_file = args[ind + 1]
    if '-Fsy' in args:
        ind = args.index("-Fsy")
        synfile = args[ind + 1]
    if '-Fsa' in args:
        ind = args.index("-Fsa")
        samp_file = args[ind + 1]
        try:
            open(samp_file, 'rU')
            ErSamps, file_type = pmag.magic_read(samp_file)
            print 'sample information will be appended to new er_samples.txt file'
        except:
            print 'sample information will be stored in new er_samples.txt file'
    if '-f' in args:
        ind = args.index("-f")
        magfile = args[ind + 1]
        try:
            input = open(magfile, 'rU')
        except:
            print "bad mag file name"
            sys.exit()
    else:
        print "mag_file field is required option"
        print main.__doc__
        sys.exit()
    if "-dc" in args:
        ind = args.index("-dc")
        labfield = float(args[ind + 1]) * 1e-6
        phi = float(args[ind + 2])
        theta = float(args[ind + 3])
    if "-ac" in args:
        ind = args.index("-ac")
        peakfield = float(args[ind + 1]) * 1e-3
    if "-spc" in args:
        ind = args.index("-spc")
        specnum = int(args[ind + 1])
        if specnum != 0: specnum = -specnum
    if "-loc" in args:
        ind = args.index("-loc")
        er_location_name = args[ind + 1]
    if "-fsa" in args:
        ind = args.index("-fsa")
        Samps, file_type = pmag.magic_read(args[ind + 1])
    if '-syn' in args:
        syn = 1
        ind = args.index("-syn")
        institution = args[ind + 1]
        syntype = args[ind + 2]
        if '-fsy' in args:
            ind = args.index("-fsy")
            synfile = args[ind + 1]
    if "-ins" in args:
        ind = args.index("-ins")
        inst = args[ind + 1]
    if "-A" in args: noave = 1
    if "-ncn" in args:
        ind = args.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 "7" in samp_con:
            if "-" not in samp_con:
                print "option [7] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z = samp_con.split("-")[1]
                samp_con = "4"
    if '-LP' in args:
        ind = args.index("-LP")
        codelist = args[ind + 1]
        codes = codelist.split(':')
        if "AF" in codes:
            demag = 'AF'
            if '-dc' not in args: methcode = "LT-AF-Z"
            if '-dc' in args: methcode = "LT-AF-I"
        if "T" in codes:
            demag = "T"
            if '-dc' not in args: methcode = "LT-T-Z"
            if '-dc' in args: methcode = "LT-T-I"
        if "I" in codes:
            methcode = "LP-IRM"
            irmunits = "mT"
        if "S" in codes:
            demag = "S"
            methcode = "LP-PI-TRM:LP-PI-ALT-AFARM"
            trm_labfield = labfield
            ans = raw_input("DC lab field for ARM step: [50uT] ")
            if ans == "":
                arm_labfield = 50e-6
            else:
                arm_labfield = float(ans) * 1e-6
            ans = raw_input("temperature for total trm step: [600 C] ")
            if ans == "":
                trm_peakT = 600 + 273  # convert to kelvin
            else:
                trm_peakT = float(ans) + 273  # convert to kelvin
        if "G" in codes: methcode = "LT-AF-G"
        if "D" in codes: methcode = "LT-AF-D"
        if "TRM" in codes:
            demag = "T"
            trm = 1
    if "-V" in args:
        methcode = "LP-IRM"
        ind = args.index("-V")
        irmunits = "V"
        coil = args[ind + 1]
        if coil not in ["1", "2", "3"]:
            print main.__doc__
            print 'not a valid coil specification'
            sys.exit()
    if demag == "T" and "ANI" in codes:
        methcode = "LP-AN-TRM"
    if demag == "AF" and "ANI" in codes:
        methcode = "LP-AN-ARM"
        if labfield == 0: labfield = 50e-6
        if peakfield == 0: peakfield = .180
    SynRecs, MagRecs = [], []
    version_num = pmag.get_version()
    if 1:  # ldgo file format
        #
        # find start of data:
        #
        Samps = []  # keeps track of sample orientations
        DIspec = []
        Data, k = input.readlines(), 0
        for k in range(len(Data)):
            rec = Data[k].split()
            if rec[0][0] == "_" or rec[0][0:2] == "!_":
                break
        start = k + 1
        for k in range(start, len(Data)):
            rec = Data[k].split()
            if len(rec) > 0:
                MagRec = {}
                MagRec["treatment_temp"] = '%8.3e' % (273
                                                      )  # room temp in kelvin
                MagRec["measurement_temp"] = '%8.3e' % (
                    273)  # room temp in kelvin
                MagRec["treatment_ac_field"] = '0'
                MagRec["treatment_dc_field"] = '0'
                MagRec["treatment_dc_field_phi"] = '0'
                MagRec["treatment_dc_field_theta"] = '0'
                meas_type = "LT-NO"
                MagRec["measurement_flag"] = 'g'
                MagRec["measurement_standard"] = 'u'
                MagRec["measurement_number"] = '1'
                MagRec["er_specimen_name"] = rec[0]
                if specnum != 0:
                    MagRec["er_sample_name"] = rec[0][:specnum]
                else:
                    MagRec["er_sample_name"] = rec[0]
                site = pmag.parse_site(MagRec['er_sample_name'], samp_con, Z)
                MagRec["er_site_name"] = site
                MagRec["er_location_name"] = er_location_name
                MagRec["measurement_csd"] = rec[3]
                MagRec["measurement_magn_moment"] = '%10.3e' % (
                    float(rec[4]) * 1e-7)  # moment in Am^2 (from 10^-4 emu)
                #
                #if samp_file!="" and MagRec["er_sample_name"] not in Samps:        # create er_samples.txt file with these data
                #    cdec,cinc=float(rec[5]),float(rec[6])
                #    gdec,ginc=float(rec[7]),float(rec[8])
                #    az,pl=pmag.get_azpl(cdec,cinc,gdec,ginc)
                #    bdec,binc=float(rec[9]),float(rec[10])
                #    if rec[7]!=rec[9] and rec[6]!=rec[8]:
                #        dipdir,dip=pmag.get_tilt(gdec,ginc,bdec,binc)
                #    else:
                #        dipdir,dip=0,0
                #    ErSampRec={}
                #    ErSampRec['er_location_name']=MagRec['er_location_name']
                #    ErSampRec['er_sample_name']=MagRec['er_sample_name']
                #    ErSampRec['er_site_name']=MagRec['er_site_name']
                #    ErSampRec['sample_azimuth']='%7.1f'%(az)
                #    ErSampRec['sample_dip']='%7.1f'%(pl)
                #    ErSampRec['sample_bed_dip_direction']='%7.1f'%(dipdir)
                #    ErSampRec['sample_bed_dip']='%7.1f'%(dip)
                #    ErSampRec['sample_description']='az,pl,dip_dir and dip recalculated from [c,g,b][dec,inc] in ldeo file'
                #    ErSampRec['magic_method_codes']='SO-REC'
                #    ErSamps.append(ErSampRec)
                #    Samps.append(ErSampRec['er_sample_name'])
                MagRec["measurement_dec"] = rec[5]
                MagRec["measurement_inc"] = rec[6]
                MagRec["measurement_chi"] = '%10.3e' % (
                    float(rec[11]) * 1e-5
                )  #convert to SI (assume Bartington, 10-5 SI)
                #MagRec["magic_instrument_codes"]=rec[2]
                #MagRec["er_analyst_mail_names"]=""
                MagRec["er_citation_names"] = "This study"
                MagRec["magic_method_codes"] = meas_type
                if demag == "AF":
                    if methcode != "LP-AN-ARM":
                        MagRec["treatment_ac_field"] = '%8.3e' % (
                            float(rec[1]) * 1e-3)  # peak field in tesla
                        meas_type = "LT-AF-Z"
                        MagRec["treatment_dc_field"] = '0'
                    else:  # AARM experiment
                        if treat[1][0] == '0':
                            meas_type = "LT-AF-Z"
                            MagRec["treatment_ac_field"] = '%8.3e' % (
                                peakfield)  # peak field in tesla
                        else:
                            meas_type = "LT-AF-I"
                            ipos = int(treat[0]) - 1
                            MagRec["treatment_dc_field_phi"] = '%7.1f' % (
                                dec[ipos])
                            MagRec["treatment_dc_field_theta"] = '%7.1f' % (
                                inc[ipos])
                            MagRec["treatment_dc_field"] = '%8.3e' % (labfield)
                            MagRec["treatment_ac_field"] = '%8.3e' % (
                                peakfield)  # peak field in tesla
                elif demag == "T":
                    if rec[1][0] == ".": rec[1] = "0" + rec[1]
                    treat = rec[1].split('.')
                    if len(treat) == 1: treat.append('0')
                    MagRec["treatment_temp"] = '%8.3e' % (float(rec[1]) + 273.
                                                          )  # temp in kelvin
                    meas_type = "LT-T-Z"
                    MagRec["treatment_temp"] = '%8.3e' % (
                        float(treat[0]) + 273.)  # temp in kelvin
                    if trm == 0:  # demag=T and not trmaq
                        if treat[1][0] == '0':
                            meas_type = "LT-T-Z"
                        else:
                            MagRec["treatment_dc_field"] = '%8.3e' % (
                                labfield
                            )  # labfield in tesla (convert from microT)
                            MagRec["treatment_dc_field_phi"] = '%7.1f' % (
                                phi)  # labfield phi
                            MagRec["treatment_dc_field_theta"] = '%7.1f' % (
                                theta)  # labfield theta
                            if treat[1][0] == '1':
                                meas_type = "LT-T-I"  # in-field thermal step
                            if treat[1][0] == '2':
                                meas_type = "LT-PTRM-I"  # pTRM check
                                pTRM = 1
                            if treat[1][0] == '3':
                                MagRec[
                                    "treatment_dc_field"] = '0'  # this is a zero field step
                                meas_type = "LT-PTRM-MD"  # pTRM tail check
                    else:
                        meas_type = "LT-T-I"  # trm acquisition experiment
                MagRec['magic_method_codes'] = meas_type
                MagRecs.append(MagRec)
    MagOuts = pmag.measurements_methods(MagRecs, noave)
    pmag.magic_write(meas_file, MagOuts, 'magic_measurements')
    print "results put in ", meas_file
    if samp_file != "":
        pmag.magic_write(samp_file, ErSamps, 'er_samples')
        print "sample orientations put in ", samp_file
    if len(SynRecs) > 0:
        pmag.magic_write(synfile, SynRecs, 'er_synthetics')
        print "synthetics put in ", synfile
Example #12
0
def main():
    """
    NAME
        IODP_csv_magic.py
 
    DESCRIPTION
        converts ODP LIMS sample format files to magic_measurements format files


    SYNTAX
        IODP_csv_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -f FILE: specify input .csv file, default is all in directory
        -F FILE: specify output  measurements file, default is magic_measurements.txt
        -Fsp FILE: specify output er_specimens.txt file, default is er_specimens.txt
        -Fsa FILE: specify output er_samples.txt file, default is er_samples.txt
        -Fsi FILE: specify output er_sites.txt file, default is er_sites.txt
        -A : don't average replicate measurements
    INPUTS
 	 IODP .csv file format exported from LIMS database
    """
#        
#
    version_num=pmag.get_version()
    meas_file='magic_measurements.txt'
    spec_file='er_specimens.txt'
    samp_file='er_samples.txt'
    site_file='er_sites.txt'
    csv_file=''
    ErSpecs,ErSamps,ErSites,ErLocs,ErCits=[],[],[],[],[]
    MagRecs=[]
    citation="This study"
    dir_path,demag='.','NRM'
    args=sys.argv
    noave=0
    depth_method='a'
    if '-WD' in args:
        ind=args.index("-WD")
        dir_path=args[ind+1]
        print "dir_path", dir_path
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-A" in args: noave=1
    if '-f' in args:
        ind=args.index("-f")
#        csv_file=args[ind+1] # original
        csv_file=dir_path + '/' + args[ind+1] # LJ
    if '-F' in args:
        ind=args.index("-F")
        meas_file=args[ind+1]
    if '-Fsp' in args:
        ind=args.index("-Fsp")
        spec_file=dir_path+'/'+args[ind+1]
        Specs,file_type=pmag.magic_read(spec_file)
    else:
        spec_file=dir_path+'/'+spec_file
    if '-Fsi' in args:
        ind=args.index("-Fsi")
        site_file=args[ind+1]
    if '-Fsa' in args:
        ind=args.index("-Fsa")
        samp_file=dir_path+'/'+args[ind+1]
        ErSamps,file_type=pmag.magic_read(samp_file)
    else:
        samp_file=dir_path+'/'+samp_file
    site_file=dir_path+'/'+site_file
    meas_file=dir_path+'/'+meas_file
    if csv_file=="":
        filelist=os.listdir(dir_path) # read in list of files to import
    else:
        filelist=[csv_file]
    specimens,samples,sites=[],[],[]
    MagRecs,SpecRecs,SampRecs,SiteRecs=[],[],[],[]
    for samp in ErSamps:
        if samp['er_sample_name'] not in samples:
            samples.append(samp['er_sample_name'])
            SampRecs.append(samp)
    print "filelist", filelist # LJ
    for file in filelist: # parse each file
        if file[-3:].lower()=='csv':
            print 'processing: ',file
            input=open(file,'rU').readlines()
            keys=input[0].replace('\n','').split(',') # splits on underscores
            if "Interval Top (cm) on SHLF" in keys:interval_key="Interval Top (cm) on SHLF"
            if " Interval Bot (cm) on SECT" in keys:interval_key=" Interval Bot (cm) on SECT"
            if "Top Depth (m)" in keys:depth_key="Top Depth (m)"
            if "CSF-A Top (m)" in keys:depth_key="CSF-A Top (m)" 
            if "CSF-B Top (m)" in keys: 
                comp_depth_key="CSF-B Top (m)" # use this model if available 
            else:
                comp_depth_key=""
            if "Demag level (mT)" in keys:demag_key="Demag level (mT)"
            if "Demag Level (mT)" in keys: demag_key="Demag Level (mT)"
            if "Inclination (Tray- and Bkgrd-Corrected) (deg)" in keys:inc_key="Inclination (Tray- and Bkgrd-Corrected) (deg)"
            if "Inclination background + tray corrected  (deg)" in keys:inc_key="Inclination background + tray corrected  (deg)"
            if "Inclination background + tray corrected  (\xc2\xb0)" in keys:inc_key="Inclination background + tray corrected  (\xc2\xb0)"
            if "Declination (Tray- and Bkgrd-Corrected) (deg)" in keys:dec_key="Declination (Tray- and Bkgrd-Corrected) (deg)"
            if "Declination background + tray corrected (deg)" in keys:dec_key="Declination background + tray corrected (deg)"
            if "Declination background + tray corrected (\xc2\xb0)" in keys:dec_key="Declination background + tray corrected (\xc2\xb0)"
            if "Intensity (Tray- and Bkgrd-Corrected) (A/m)" in keys:int_key="Intensity (Tray- and Bkgrd-Corrected) (A/m)"
            if "Intensity background + tray corrected  (A/m)" in keys:int_key="Intensity background + tray corrected  (A/m)"
            if "Core Type" in keys:
                type="Core Type"
            else: type="Type" 
            for line in input[1:]:
              InRec={}
              for k in range(len(keys)):InRec[keys[k]]=line.split(',')[k]
              try:
                run_number=""
                inst="ODP-SRM"
                volume='15.59' # set default volume to this
                MagRec,SpecRec,SampRec,SiteRec={},{},{},{}
                expedition=InRec['Exp']
                location=InRec['Site']+InRec['Hole']
# Maintain backward compatibility for the ever-changing LIMS format (Argh!)
                while len(InRec['Core'])<3:
                    InRec['Core']='0'+InRec['Core']
                if "Last Tray Measurment" in InRec.keys() and "Discrete" in InRec['Last Tray Measurement'] or 'dscr' in csv_file :  # assume discrete sample
                    specimen=expedition+'-'+location+'-'+InRec['Core']+InRec[type]+"-"+InRec['Section']+'-'+InRec['Section Half']+'-'+InRec[interval_key]
                else: # mark as continuous measurements
                    specimen=expedition+'-'+location+'-'+InRec['Core']+InRec[type]+"_"+InRec['Section']+InRec['Section Half']+'-'+InRec[interval_key]
                SpecRec['er_expedition_name']=expedition
                SpecRec['er_location_name']=location
                SpecRec['er_site_name']=specimen
                SpecRec['er_citation_names']=citation
                for key in SpecRec.keys():SampRec[key]=SpecRec[key]
                for key in SpecRec.keys():SiteRec[key]=SpecRec[key]
                SampRec['sample_azimuth']='0'
                SampRec['sample_dip']='0'
                SampRec['sample_core_depth']=InRec[depth_key]
                if comp_depth_key!='':
                    SampRec['sample_composite_depth']=InRec[comp_depth_key]
                if "Discrete" in InRec['Last Tray Measurement']: 
                    SampRec['magic_method_codes']='FS-C-DRILL-IODP:SP-SS-C:SO-V'
                else:
                    SampRec['magic_method_codes']='FS-C-DRILL-IODP:SO-V'
                SpecRec['er_specimen_name']=specimen
                SpecRec['er_sample_name']=specimen
                SampRec['er_sample_name']=specimen
                SampRec['er_specimen_names']=specimen
                SiteRec['er_specimen_names']=specimen

                for key in SpecRec.keys():MagRec[key]=SpecRec[key]
# set up measurement record - default is NRM 
                MagRec['er_analyst_mail_names']=InRec['Test Entered By']
                MagRec['magic_software_packages']=version_num
                MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
                MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
                MagRec["treatment_ac_field"]=0
                MagRec["treatment_dc_field"]='0'
                MagRec["treatment_dc_field_phi"]='0'
                MagRec["treatment_dc_field_theta"]='0'
                MagRec["measurement_flag"]='g' # assume all data are "good"
                MagRec["measurement_standard"]='u' # assume all data are "good"
                SpecRec['er_specimen_alternatives']=InRec['Text Id']
                if 'Sample Area (cm?)' in InRec.keys() and  InRec['Sample Area (cm?)']!= "": volume=InRec['Sample Area (cm?)']
                if InRec['Run Number']!= "": run_number=InRec['Run Number']
                datestamp=InRec['Test Changed On'].split() # date time is second line of file
                mmddyy=datestamp[0].split('/') # break into month day year
                if len(mmddyy[0])==1: mmddyy[0]='0'+mmddyy[0] # make 2 characters
                if len(mmddyy[1])==1: mmddyy[1]='0'+mmddyy[1] # make 2 characters
                if len(datestamp[1])==1: datestamp[1]='0'+datestamp[1] # make 2 characters
                date='20'+mmddyy[2]+':'+mmddyy[0]+":"+mmddyy[1] +':' +datestamp[1]+":00.00"
                MagRec["measurement_date"]=date
                MagRec["magic_method_codes"]='LT-NO'
                if InRec[demag_key]!="0":
                    MagRec['magic_method_codes'] = 'LT-AF-Z'
                    inst=inst+':ODP-SRM-AF' # measured on shipboard in-line 2G AF
                    treatment_value=float(InRec[demag_key].strip('"'))*1e-3 # convert mT => T
                    MagRec["treatment_ac_field"]=treatment_value # AF demag in treat mT => T
                if InRec['Treatment Type']!="":
                    if 'Alternating Frequency' in InRec['Treatment Type']:
                        MagRec['magic_method_codes'] = 'LT-AF-Z'
                        inst=inst+':ODP-DTECH' # measured on shipboard Dtech D2000
                        treatment_value=float(InRec['Treatment Value'])*1e-3 # convert mT => T
                        MagRec["treatment_ac_field"]=treatment_value # AF demag in treat mT => T
                    elif 'Thermal' in InRec['Treatment Type']:
                        MagRec['magic_method_codes'] = 'LT-T-Z'
                        inst=inst+':ODP-TDS' # measured on shipboard Schonstedt thermal demagnetizer
                        treatment_value=float(InRec['Treatment Value'])+273 # convert C => K
                        MagRec["treatment_temp"]='%8.3e'%(treatment_value) # 
                MagRec["measurement_standard"]='u' # assume all data are "good"
                vol=float(volume)*1e-6 # convert from cc to m^3
                if run_number!="":
                    MagRec['external_database_ids']=run_number
                    MagRec['external_database_names']='LIMS'
                else:
                    MagRec['external_database_ids']=""
                    MagRec['external_database_names']=''
                MagRec['measurement_inc']=InRec[inc_key].strip('"')
                MagRec['measurement_dec']=InRec[dec_key].strip('"')
                intens= InRec[int_key].strip('"')
                MagRec['measurement_magn_moment']='%8.3e'%(float(intens)*vol) # convert intensity from A/m to Am^2 using vol
                MagRec['magic_instrument_codes']=inst
                MagRec['measurement_number']='1'
                MagRec['measurement_csd']=''
                MagRec['measurement_positions']=''
                MagRecs.append(MagRec)
                if specimen not in specimens:
                    specimens.append(specimen)
                    SpecRecs.append(SpecRec)
                if MagRec['er_sample_name']  not in samples:
                    samples.append(MagRec['er_sample_name'])
                    SampRecs.append(SampRec)
                if MagRec['er_site_name']  not in sites:
                    sites.append(MagRec['er_site_name'])
                    SiteRecs.append(SiteRec)
              except:
                 pass
    if len(SpecRecs)>0:
        pmag.magic_write(spec_file,SpecRecs,'er_specimens')
        print 'specimens stored in ',spec_file
    if len(SampRecs)>0:
        SampOut,keys=pmag.fillkeys(SampRecs)
        pmag.magic_write(samp_file,SampOut,'er_samples')
        print 'samples stored in ',samp_file
    if len(SiteRecs)>0:
        pmag.magic_write(site_file,SiteRecs,'er_sites')
        print 'sites stored in ',site_file
    MagSort=pmag.sortbykeys(MagRecs,["er_specimen_name","treatment_ac_field"])
    MagOuts=[]
    for MagRec in MagSort:
       MagRec["treatment_ac_field"]='%8.3e'%(MagRec['treatment_ac_field']) # convert to string
       MagOuts.append(MagRec)
    Fixed=pmag.measurements_methods(MagOuts,noave)
    pmag.magic_write(meas_file,Fixed,'magic_measurements')
    print 'data stored in ',meas_file
def main():
    """
    NAME
	specimens_results_magic.py

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

    SYNTAX
	specimens_results_magic.py [command line options]

    OPTIONS
	-h prints help message and quits
	-usr USER:   identify user, default is ""
	-f: specimen input magic_measurements format file, default is "magic_measurements.txt"
	-fsp: specimen input pmag_specimens format file, default is "pmag_specimens.txt"
	-fsm: sample input er_samples format file, default is "er_samples.txt"
	-fsi: specimen input er_sites format file, default is "er_sites.txt"
	-fla: specify a file with paleolatitudes for calculating VADMs, default is not to calculate VADMS
               format is:  site_name paleolatitude (space delimited file)
	-fa AGES: specify er_ages format file with age information
	-crd [s,g,t,b]:   specify coordinate system
	    (s, specimen, g geographic, t, tilt corrected, b, geographic and tilt corrected)
	    Default is to assume geographic
	    NB: only the tilt corrected data will appear on the results table, if both g and t are selected.
        -cor [AC:CR:NL]: colon delimited list of required data adjustments for all specimens 
            included in intensity calculations (anisotropy, cooling rate, non-linear TRM)
            unless specified, corrections will not be applied
        -pri [TRM:ARM] colon delimited list of priorities for anisotropy correction (-cor must also be set to include AC). default is TRM, then ARM 
	-age MIN MAX UNITS:   specify age boundaries and units
	-exc:  use exiting selection criteria (in pmag_criteria.txt file), default is default criteria
	-C: no acceptance criteria
	-aD:  average directions per sample, default is NOT
	-aI:  average multiple specimen intensities per sample, default is by site 
	-aC:  average all components together, default is NOT
	-pol:  calculate polarity averages
	-sam:  save sample level vgps and v[a]dms, default is by site
	-xSi:  skip the site level intensity calculation
	-p: plot directions and look at intensities by site, default is NOT
	    -fmt: specify output for saved images, default is svg (only if -p set)
	-lat: use present latitude for calculating VADMs, default is not to calculate VADMs
	-xD: skip directions
	-xI: skip intensities
    OUPUT
	writes pmag_samples, pmag_sites, pmag_results tables
    """
# set defaults
    Comps=[] # list of components
    version_num=pmag.get_version()
    args=sys.argv
    DefaultAge=["none"]
    skipdirs,coord,excrit,custom,vgps,average,Iaverage,plotsites,opt=1,0,0,0,0,0,0,0,0
    get_model_lat=0 # this skips VADM calculation altogether, when get_model_lat=1, uses present day
    fmt='svg'
    dir_path="."
    model_lat_file=""
    Caverage=0
    infile='pmag_specimens.txt'
    measfile="magic_measurements.txt"
    sampfile="er_samples.txt"
    sitefile="er_sites.txt"
    agefile="er_ages.txt"
    specout="er_specimens.txt"
    sampout="pmag_samples.txt"
    siteout="pmag_sites.txt"
    resout="pmag_results.txt"
    critout="pmag_criteria.txt"
    instout="magic_instruments.txt"
    sigcutoff,OBJ="",""
    noDir,noInt=0,0
    polarity=0
    coords=['0']
    Dcrit,Icrit,nocrit=0,0,0
    corrections=[]
    nocorrection=['DA-NL','DA-AC','DA-CR']
    priorities=['DA-AC-ARM','DA-AC-TRM'] # priorities for anisotropy correction
# get command line stuff
    if "-h" in args:
	print main.__doc__
	sys.exit()
    if '-WD' in args:
	ind=args.index("-WD")
	dir_path=args[ind+1]
    if '-cor' in args:
        ind=args.index('-cor')
        cors=args[ind+1].split(':') # list of required data adjustments
        for cor in cors:
            nocorrection.remove('DA-'+cor)
            corrections.append('DA-'+cor)
    if '-pri' in args:
        ind=args.index('-pri')
        priorities=args[ind+1].split(':') # list of required data adjustments
        for p in priorities:
            p='DA-AC-'+p
    if '-f' in args:
	ind=args.index("-f")
	measfile=args[ind+1]
    if '-fsp' in args:
	ind=args.index("-fsp")
	infile=args[ind+1]
    if '-fsi' in args:
	ind=args.index("-fsi")
	sitefile=args[ind+1]
    if "-crd" in args:
	ind=args.index("-crd")
	coord=args[ind+1]
	if coord=='s':coords=['-1']
	if coord=='g':coords=['0']
	if coord=='t':coords=['100']
	if coord=='b':coords=['0','100']
    if "-usr" in args:
	ind=args.index("-usr")
	user=sys.argv[ind+1]
    else: user=""
    if "-C" in args: Dcrit,Icrit,nocrit=1,1,1 # no selection criteria
    if "-sam" in args: vgps=1 # save sample level VGPS/VADMs
    if "-xSi" in args: 
        nositeints=1 # skip site level intensity
    else:
        nositeints=0
    if "-age" in args:
	ind=args.index("-age")
	DefaultAge[0]=args[ind+1]
	DefaultAge.append(args[ind+2])
	DefaultAge.append(args[ind+3])
    Daverage,Iaverage,Caverage=0,0,0
    if "-aD" in args: Daverage=1 # average by sample directions
    if "-aI" in args: Iaverage=1 # average by sample intensities
    if "-aC" in args: Caverage=1 # average all components together ???  why???
    if "-pol" in args: polarity=1 # calculate averages by polarity
    if '-xD' in args:noDir=1
    if '-xI' in args:
	noInt=1
    elif "-fla" in args: 
	if '-lat' in args:
	    print "you should set a paleolatitude file OR use present day lat - not both"
	    sys.exit()
	ind=args.index("-fla")
	model_lat_file=dir_path+'/'+args[ind+1]
	get_model_lat=2
	mlat=open(model_lat_file,'rU')
	ModelLats=[]
	for line in mlat.readlines():
	    ModelLat={}
	    tmp=line.split()
	    ModelLat["er_site_name"]=tmp[0]
	    ModelLat["site_model_lat"]=tmp[1]
	    ModelLat["er_sample_name"]=tmp[0] 
	    ModelLat["sample_lat"]=tmp[1]
	    ModelLats.append(ModelLat)
	get_model_lat=2
    elif '-lat' in args:
	get_model_lat=1
    if "-p" in args: 
	plotsites=1
	if "-fmt" in args: 
	    ind=args.index("-fmt")
	    fmt=args[ind+1]
	if noDir==0: # plot by site - set up plot window
	    import pmagplotlib
	    EQ={}
	    EQ['eqarea']=1
	    pmagplotlib.plot_init(EQ['eqarea'],5,5) # define figure 1 as equal area projection
            pmagplotlib.plotNET(EQ['eqarea']) # I don't know why this has to be here, but otherwise the first plot never plots...
            pmagplotlib.drawFIGS(EQ)
    if '-WD' in args:
	infile=dir_path+'/'+infile
	measfile=dir_path+'/'+measfile
	instout=dir_path+'/'+instout
	sampfile=dir_path+'/'+sampfile
	sitefile=dir_path+'/'+sitefile
	agefile=dir_path+'/'+agefile
	specout=dir_path+'/'+specout
	sampout=dir_path+'/'+sampout
	siteout=dir_path+'/'+siteout
	resout=dir_path+'/'+resout
	critout=dir_path+'/'+critout
    if "-exc" in args: # use existing pmag_criteria file 
	if "-C" in args:
	    print 'you can not use both existing and no criteria - choose either -exc OR -C OR neither (for default)'
	    sys.exit()
	crit_data,file_type=pmag.magic_read(critout)
	print "Acceptance criteria read in from ", critout
    else  : # use default criteria (if nocrit set, then get really loose criteria as default)
	crit_data=pmag.default_criteria(nocrit)
	if nocrit==0:
	    print "Acceptance criteria are defaults"
	else:
	    print "No acceptance criteria used "
    accept={}
    for critrec in crit_data:
        for key in critrec.keys():
            if 'sample_int_sigma_uT' in critrec.keys():
                critrec['sample_int_sigma']='%10.3e'%(eval(critrec['sample_int_sigma_uT'])*1e-6)
            if key not in accept.keys() and critrec[key]!='':
                accept[key]=critrec[key]
    #
    #
    if "-exc" not in args and "-C" not in args:
        print "args",args
        pmag.magic_write(critout,[accept],'pmag_criteria')
        print "\n Pmag Criteria stored in ",critout,'\n'
#
# now we're done slow dancing
#
    SiteNFO,file_type=pmag.magic_read(sitefile) # read in site data - has the lats and lons
    SampNFO,file_type=pmag.magic_read(sampfile) # read in site data - has the lats and lons
    height_nfo=pmag.get_dictitem(SiteNFO,'site_height','','F') # find all the sites with height info.  
    if agefile !="":AgeNFO,file_type=pmag.magic_read(agefile) # read in the age information
    Data,file_type=pmag.magic_read(infile) # read in specimen interpretations
    IntData=pmag.get_dictitem(Data,'specimen_int','','F') # retrieve specimens with intensity data
    comment,orient="",[]
    samples,sites=[],[]
    for rec in Data: # run through the data filling in missing keys and finding all components, coordinates available
# fill in missing fields, collect unique sample and site names
	if 'er_sample_name' not in rec.keys():
	    rec['er_sample_name']=""
	elif rec['er_sample_name'] not in samples:
	    samples.append(rec['er_sample_name'])
	if 'er_site_name' not in rec.keys():
	    rec['er_site_name']=""
	elif rec['er_site_name'] not in sites:
	    sites.append(rec['er_site_name'])
	if 'specimen_int' not in rec.keys():rec['specimen_int']=''
	if 'specimen_comp_name' not in rec.keys() or rec['specimen_comp_name']=="":rec['specimen_comp_name']='A'
	if rec['specimen_comp_name'] not in Comps:Comps.append(rec['specimen_comp_name'])
        rec['specimen_tilt_correction']=rec['specimen_tilt_correction'].strip('\n')
	if "specimen_tilt_correction" not in rec.keys(): rec["specimen_tilt_correction"]="-1" # assume sample coordinates
	if rec["specimen_tilt_correction"] not in orient: orient.append(rec["specimen_tilt_correction"])  # collect available coordinate systems
	if "specimen_direction_type" not in rec.keys(): rec["specimen_direction_type"]='l'  # assume direction is line - not plane
	if "specimen_dec" not in rec.keys(): rec["specimen_direction_type"]=''  # if no declination, set direction type to blank
	if "specimen_n" not in rec.keys(): rec["specimen_n"]=''  # put in n
	if "specimen_alpha95" not in rec.keys(): rec["specimen_alpha95"]=''  # put in alpha95 
	if "magic_method_codes" not in rec.keys(): rec["magic_method_codes"]=''
     #
     # start parsing data into SpecDirs, SpecPlanes, SpecInts 
    SpecInts,SpecDirs,SpecPlanes=[],[],[]
    samples.sort() # get sorted list of samples and sites
    sites.sort()
    if noInt==0: # don't skip intensities
	IntData=pmag.get_dictitem(Data,'specimen_int','','F') # retrieve specimens with intensity data
	if nocrit==0: # use selection criteria
	    for rec in IntData: # do selection criteria
		kill=pmag.grade(rec,accept,'specimen_int')
		if len(kill)==0: SpecInts.append(rec) # intensity record to be included in sample, site calculations
	else:
	    SpecInts=IntData[:] # take everything - no selection criteria
# check for required data adjustments
        if len(corrections)>0 and len(SpecInts)>0:
            for cor in corrections:
                SpecInts=pmag.get_dictitem(SpecInts,'magic_method_codes',cor,'has') # only take specimens with the required corrections
        if len(nocorrection)>0 and len(SpecInts)>0:
            for cor in nocorrection:
                SpecInts=pmag.get_dictitem(SpecInts,'magic_method_codes',cor,'not') # exclude the corrections not specified for inclusion
# take top priority specimen of its name in remaining specimens (only one per customer)
        PrioritySpecInts=[]
        specimens=pmag.get_specs(SpecInts) # get list of uniq specimen names
        for spec in specimens:
            ThisSpecRecs=pmag.get_dictitem(SpecInts,'er_specimen_name',spec,'T') # all the records for this specimen
            if len(ThisSpecRecs)==1:
                PrioritySpecInts.append(ThisSpecRecs[0])
            elif len(ThisSpecRecs)>1: # more than one
                prec=[]
                for p in priorities:
                    ThisSpecRecs=pmag.get_dictitem(SpecInts,'magic_method_codes',p,'has') # all the records for this specimen
                    if len(ThisSpecRecs)>0:prec.append(ThisSpecRecs[0])
                PrioritySpecInts.append(prec[0]) # take the best one
        SpecInts=PrioritySpecInts # this has the first specimen record 
    if noDir==0: # don't skip directions
	AllDirs=pmag.get_dictitem(Data,'specimen_direction_type','','F') # retrieve specimens with directed lines and planes
	Ns=pmag.get_dictitem(AllDirs,'specimen_n','','F')  # get all specimens with specimen_n information 
	if nocrit!=1: # use selection criteria
	    for rec in Ns: # look through everything with specimen_n for "good" data
                kill=pmag.grade(rec,accept,'specimen_dir')
                if len(kill)==0: # nothing killed it
			SpecDirs.append(rec)
	else: # no criteria
	    SpecDirs=AllDirs[:] # take them all
# SpecDirs is now the list of all specimen directions (lines and planes) that pass muster
#
    PmagSamps,SampDirs=[],[] # list of all sample data and list of those that pass the DE-SAMP criteria
    PmagSites,PmagResults=[],[] # list of all site data and selected results
    SampInts=[]
    for samp in samples: # run through the sample names
	if Daverage==1: #  average by sample if desired
	   SampDir=pmag.get_dictitem(SpecDirs,'er_sample_name',samp,'T') # get all the directional data for this sample
	   if len(SampDir)>0: # there are some directions
	       for coord in coords: # step through desired coordinate systems
		   CoordDir=pmag.get_dictitem(SampDir,'specimen_tilt_correction',coord,'T') # get all the directions for this sample
		   if len(CoordDir)>0: # there are some with this coordinate system
		       if Caverage==0: # look component by component
			   for comp in Comps:
			       CompDir=pmag.get_dictitem(CoordDir,'specimen_comp_name',comp,'T') # get all directions from this component
			       if len(CompDir)>0: # there are some
				   PmagSampRec=pmag.lnpbykey(CompDir,'sample','specimen') # get a sample average from all specimens
				   PmagSampRec["er_location_name"]=CompDir[0]['er_location_name'] # decorate the sample record
				   PmagSampRec["er_site_name"]=CompDir[0]['er_site_name']
				   PmagSampRec["er_sample_name"]=samp
				   PmagSampRec["er_citation_names"]="This study"
				   PmagSampRec["er_analyst_mail_names"]=user
				   PmagSampRec['magic_software_packages']=version_num
				   if nocrit!=1:PmagSampRec['pmag_criteria_codes']="ACCEPT"
				   if agefile != "": PmagSampRec= pmag.get_age(PmagSampRec,"er_site_name","sample_inferred_",AgeNFO,DefaultAge)
				   site_height=pmag.get_dictitem(height_nfo,'er_site_name',PmagSampRec['er_site_name'],'T')
				   if len(site_height)>0:PmagSampRec["sample_height"]=site_height[0]['site_height'] # add in height if available
				   PmagSampRec['sample_comp_name']=comp
				   PmagSampRec['sample_tilt_correction']=coord
				   PmagSampRec['er_specimen_names']= pmag.get_list(CompDir,'er_specimen_name') # get a list of the specimen names used
				   PmagSampRec['magic_method_codes']= pmag.get_list(CompDir,'magic_method_codes') # get a list of the methods used
				   if nocrit!=1: # apply selection criteria
                                       kill=pmag.grade(PmagSampRec,accept,'sample_dir')
                                   else:
                                       kill=[]
				   if len(kill)==0:
					SampDirs.append(PmagSampRec)
					if vgps==1: # if sample level VGP info desired, do that now
					    PmagResRec=pmag.getsampVGP(PmagSampRec,SiteNFO)
					    if PmagResRec!="":PmagResults.append(PmagResRec)
                                        PmagSamps.append(PmagSampRec)
		       if Caverage==1: # average all components together  basically same as above
			   PmagSampRec=pmag.lnpbykey(CoordDir,'sample','specimen')
			   PmagSampRec["er_location_name"]=CoordDir[0]['er_location_name']
			   PmagSampRec["er_site_name"]=CoordDir[0]['er_site_name']
			   PmagSampRec["er_sample_name"]=samp
			   PmagSampRec["er_citation_names"]="This study"
			   PmagSampRec["er_analyst_mail_names"]=user
			   PmagSampRec['magic_software_packages']=version_num
			   if nocrit!=1:PmagSampRec['pmag_criteria_codes']=""
			   if agefile != "": PmagSampRec= pmag.get_age(PmagSampRec,"er_site_name","sample_inferred_",AgeNFO,DefaultAge)
			   site_height=pmag.get_dictitem(height_nfo,'er_site_name',site,'T')
			   if len(site_height)>0:PmagSampRec["sample_height"]=site_height[0]['site_height'] # add in height if available
			   PmagSampRec['sample_tilt_correction']=coord
			   PmagSampRec['sample_comp_name']= pmag.get_list(CoordDir,'specimen_comp_name') # get components used
			   PmagSampRec['er_specimen_names']= pmag.get_list(CoordDir,'er_specimen_name') # get specimne names averaged
			   PmagSampRec['magic_method_codes']= pmag.get_list(CoordDir,'magic_method_codes') # assemble method codes
			   if nocrit!=1: # apply selection criteria
                               kill=pmag.grade(PmagSampRec,accept,'sample_dir')
			       if len(kill)==0: # passes the mustard
				   SampDirs.append(PmagSampRec)
				   if vgps==1:
				       PmagResRec=pmag.getsampVGP(PmagSampRec,SiteNFO)
				       if PmagResRec!="":PmagResults.append(PmagResRec)
			   else: # take everything
			       SampDirs.append(PmagSampRec)
			       if vgps==1:
				   PmagResRec=pmag.getsampVGP(PmagSampRec,SiteNFO)
				   if PmagResRec!="":PmagResults.append(PmagResRec)
			   PmagSamps.append(PmagSampRec)
	if Iaverage==1: #  average by sample if desired
	   SampI=pmag.get_dictitem(SpecInts,'er_sample_name',samp,'T') # get all the intensity data for this sample
	   if len(SampI)>0: # there are some
	       PmagSampRec=pmag.average_int(SampI,'specimen','sample') # get average intensity stuff
	       PmagSampRec["sample_description"]="sample intensity" # decorate sample record
	       PmagSampRec["sample_direction_type"]=""
	       PmagSampRec['er_site_name']=SampI[0]["er_site_name"]
	       PmagSampRec['er_sample_name']=samp
	       PmagSampRec['er_location_name']=SampI[0]["er_location_name"]
	       PmagSampRec["er_citation_names"]="This study"
	       PmagSampRec["er_analyst_mail_names"]=user
	       if agefile != "":   PmagSampRec=pmag.get_age(PmagSampRec,"er_site_name","sample_inferred_", AgeNFO,DefaultAge)
	       site_height=pmag.get_dictitem(height_nfo,'er_site_name',PmagSampRec['er_site_name'],'T')
	       if len(site_height)>0:PmagSampRec["sample_height"]=site_height[0]['site_height'] # add in height if available
	       PmagSampRec['er_specimen_names']= pmag.get_list(SampI,'er_specimen_name')
	       PmagSampRec['magic_method_codes']= pmag.get_list(SampI,'magic_method_codes')
	       if nocrit!=1:  # apply criteria!
                   kill=pmag.grade(PmagSampRec,accept,'sample_int')
                   if len(kill)==0:
                       PmagSampRec['pmag_criteria_codes']="ACCEPT"
	               SampInts.append(PmagSampRec)
	               PmagSamps.append(PmagSampRec)
                   else:PmagSampRec={} # sample rejected
               else: # no criteria
	           SampInts.append(PmagSampRec)
	           PmagSamps.append(PmagSampRec)
                   PmagSampRec['pmag_criteria_codes']=""
	       if vgps==1 and get_model_lat!=0 and PmagSampRec!={}: #
		  if get_model_lat==1: # use sample latitude
		      PmagResRec=pmag.getsampVDM(PmagSampRec,SampNFO)
                      del(PmagResRec['model_lat']) # get rid of the model lat key
		  elif get_model_lat==2: # use model latitude
		      PmagResRec=pmag.getsampVDM(PmagSampRec,ModelLats)
		      if PmagResRec!={}:PmagResRec['magic_method_codes']=PmagResRec['magic_method_codes']+":IE-MLAT"
		  if PmagResRec!={}:
                      PmagResRec['er_specimen_names']=PmagSampRec['er_specimen_names']
                      PmagResRec['er_sample_names']=PmagSampRec['er_sample_name']
                      PmagResRec['pmag_criteria_codes']='ACCEPT'
                      PmagResRec['average_int_sigma_perc']=PmagSampRec['sample_int_sigma_perc']
                      PmagResRec['average_int_sigma']=PmagSampRec['sample_int_sigma']
                      PmagResRec['average_int_n']=PmagSampRec['sample_int_n']
                      PmagResRec['vadm_n']=PmagSampRec['sample_int_n']
                      PmagResRec['data_type']='i'
                      PmagResults.append(PmagResRec)
    if len(PmagSamps)>0:
	TmpSamps,keylist=pmag.fillkeys(PmagSamps) # fill in missing keys from different types of records       
	pmag.magic_write(sampout,TmpSamps,'pmag_samples') # save in sample output file
	print ' sample averages written to ',sampout
   
#
#create site averages from specimens or samples as specified
#
    for site in sites:
	if Daverage==0: key,dirlist='specimen',SpecDirs # if specimen averages at site level desired
	if Daverage==1: key,dirlist='sample',SampDirs # if sample averages at site level desired
	tmp=pmag.get_dictitem(dirlist,'er_site_name',site,'T') # get all the sites with  directions
	tmp1=pmag.get_dictitem(tmp,key+'_tilt_correction',coords[-1],'T') # use only the last coordinate if Caverage==0
	sd=pmag.get_dictitem(SiteNFO,'er_site_name',site,'T') # fish out site information (lat/lon, etc.)
	if len(sd)>0:
            sitedat=sd[0]
	    if Caverage==0: # do component wise averaging
		for comp in Comps:
		    siteD=pmag.get_dictitem(tmp1,key+'_comp_name',comp,'T') # get all components comp
		    if len(siteD)>0: # there are some for this site and component name
			PmagSiteRec=pmag.lnpbykey(siteD,'site',key) # get an average for this site
			PmagSiteRec['site_comp_name']=comp # decorate the site record
			PmagSiteRec["er_location_name"]=siteD[0]['er_location_name']
			PmagSiteRec["er_site_name"]=siteD[0]['er_site_name']
			PmagSiteRec['site_tilt_correction']=coords[-1]
			PmagSiteRec['site_comp_name']= pmag.get_list(siteD,key+'_comp_name')
                        if Daverage==1:
			    PmagSiteRec['er_sample_names']= pmag.get_list(siteD,'er_sample_name')
                        else:
			    PmagSiteRec['er_specimen_names']= pmag.get_list(siteD,'er_specimen_name')
# determine the demagnetization code (DC3,4 or 5) for this site
			AFnum=len(pmag.get_dictitem(siteD,'magic_method_codes','LP-DIR-AF','has'))
			Tnum=len(pmag.get_dictitem(siteD,'magic_method_codes','LP-DIR-T','has'))
			DC=3
			if AFnum>0:DC+=1
			if Tnum>0:DC+=1
			PmagSiteRec['magic_method_codes']= pmag.get_list(siteD,'magic_method_codes')+':'+ 'LP-DC'+str(DC)
			PmagSiteRec['magic_method_codes'].strip(":")
			if plotsites==1:
                            print PmagSiteRec['er_site_name']
                            pmagplotlib.plotSITE(EQ['eqarea'],PmagSiteRec,siteD,key) # plot and list the data
                            pmagplotlib.drawFIGS(EQ)
			PmagSites.append(PmagSiteRec) 
	    else: # last component only
	        siteD=tmp1[:] # get the last orientation system specified
	        if len(siteD)>0: # there are some
	            PmagSiteRec=pmag.lnpbykey(siteD,'site',key) # get the average for this site 
	            PmagSiteRec["er_location_name"]=siteD[0]['er_location_name'] # decorate the record
    		    PmagSiteRec["er_site_name"]=siteD[0]['er_site_name']
		    PmagSiteRec['site_comp_name']=comp
		    PmagSiteRec['site_tilt_correction']=coords[-1]
		    PmagSiteRec['site_comp_name']= pmag.get_list(siteD,key+'_comp_name')
		    PmagSiteRec['er_specimen_names']= pmag.get_list(siteD,'er_specimen_name')
		    PmagSiteRec['er_sample_names']= pmag.get_list(siteD,'er_sample_name')
    		    AFnum=len(pmag.get_dictitem(siteD,'magic_method_codes','LP-DIR-AF','has'))
    	    	    Tnum=len(pmag.get_dictitem(siteD,'magic_method_codes','LP-DIR-T','has'))
	    	    DC=3
		    if AFnum>0:DC+=1
		    if Tnum>0:DC+=1
		    PmagSiteRec['magic_method_codes']= pmag.get_list(siteD,'magic_method_codes')+':'+ 'LP-DC'+str(DC)
		    PmagSiteRec['magic_method_codes'].strip(":")
		    if Daverage==0:PmagSiteRec['site_comp_name']= pmag.get_list(siteD,key+'_comp_name')
	    	    if plotsites==1:
                        pmagplotlib.plotSITE(EQ['eqarea'],PmagSiteRec,siteD,key)
                        pmagplotlib.drawFIGS(EQ)
		    PmagSites.append(PmagSiteRec)
        else:
            print 'site information not found in er_sites for site, ',site,' site will be skipped'
    for PmagSiteRec in PmagSites: # now decorate each dictionary some more, and calculate VGPs etc. for results table
	PmagSiteRec["er_citation_names"]="This study"
	PmagSiteRec["er_analyst_mail_names"]=user
	PmagSiteRec['magic_software_packages']=version_num
	if agefile != "": PmagSiteRec= pmag.get_age(PmagSiteRec,"er_site_name","site_inferred_",AgeNFO,DefaultAge)
	PmagSiteRec['pmag_criteria_codes']='ACCEPT'
	if 'site_n_lines' in PmagSiteRec.keys() and 'site_n_planes' in PmagSiteRec.keys() and PmagSiteRec['site_n_lines']!="" and PmagSiteRec['site_n_planes']!="":
	    if int(PmagSiteRec["site_n_planes"])>0:
		PmagSiteRec["magic_method_codes"]=PmagSiteRec['magic_method_codes']+":DE-FM-LP"
	    elif int(PmagSiteRec["site_n_lines"])>2:
		PmagSiteRec["magic_method_codes"]=PmagSiteRec['magic_method_codes']+":DE-FM"
	    kill=pmag.grade(PmagSiteRec,accept,'site_dir')
            if len(kill)==0: 
		PmagResRec={} # set up dictionary for the pmag_results table entry
		PmagResRec['data_type']='i' # decorate it a bit
		PmagResRec['magic_software_packages']=version_num
		PmagSiteRec['site_description']='Site direction included in results table' 
		PmagResRec['pmag_criteria_codes']='ACCEPT'
		dec=float(PmagSiteRec["site_dec"])
		inc=float(PmagSiteRec["site_inc"])
                if 'site_alpha95' in PmagSiteRec.keys() and PmagSiteRec['site_alpha95']!="": 
		    a95=float(PmagSiteRec["site_alpha95"])
                else:a95=180.
	        sitedat=pmag.get_dictitem(SiteNFO,'er_site_name',PmagSiteRec['er_site_name'],'T')[0] # fish out site information (lat/lon, etc.)
		lat=float(sitedat['site_lat'])
		lon=float(sitedat['site_lon'])
		plong,plat,dp,dm=pmag.dia_vgp(dec,inc,a95,lat,lon) # get the VGP for this site
		if PmagSiteRec['site_tilt_correction']=='-1':C=' (spec coord) '
		if PmagSiteRec['site_tilt_correction']=='0':C=' (geog. coord) '
		if PmagSiteRec['site_tilt_correction']=='100':C=' (strat. coord) '
		PmagResRec["pmag_result_name"]="VGP Site: "+PmagSiteRec["er_site_name"] # decorate some more
		PmagResRec["result_description"]="Site VGP, coord system = "+str(coord)+' component: '+comp
		PmagResRec['er_site_names']=PmagSiteRec['er_site_name']
		PmagResRec['pmag_criteria_codes']='ACCEPT'
		PmagResRec['er_citation_names']='This study'
		PmagResRec['er_analyst_mail_names']=user
		PmagResRec["er_location_names"]=PmagSiteRec["er_location_name"]
                if Daverage==1:
		    PmagResRec["er_sample_names"]=PmagSiteRec["er_sample_names"]
                else:
		    PmagResRec["er_specimen_names"]=PmagSiteRec["er_specimen_names"]
		PmagResRec["tilt_correction"]=PmagSiteRec['site_tilt_correction']
		PmagResRec["pole_comp_name"]=PmagSiteRec['site_comp_name']
		PmagResRec["average_dec"]=PmagSiteRec["site_dec"]
		PmagResRec["average_inc"]=PmagSiteRec["site_inc"]
		PmagResRec["average_alpha95"]=PmagSiteRec["site_alpha95"]
		PmagResRec["average_n"]=PmagSiteRec["site_n"]
		PmagResRec["average_n_lines"]=PmagSiteRec["site_n_lines"]
		PmagResRec["average_n_planes"]=PmagSiteRec["site_n_planes"]            
		PmagResRec["vgp_n"]=PmagSiteRec["site_n"]
		PmagResRec["average_k"]=PmagSiteRec["site_k"]
		PmagResRec["average_r"]=PmagSiteRec["site_r"]
		PmagResRec["average_lat"]='%10.4f ' %(lat)
		PmagResRec["average_lon"]='%10.4f ' %(lon)
		if agefile != "": PmagResRec= pmag.get_age(PmagResRec,"er_site_names","average_",AgeNFO,DefaultAge)
		site_height=pmag.get_dictitem(height_nfo,'er_site_name',site,'T')
		if len(site_height)>0:PmagResRec["average_height"]=site_height[0]['site_height']
		PmagResRec["vgp_lat"]='%7.1f ' % (plat)
		PmagResRec["vgp_lon"]='%7.1f ' % (plong)
		PmagResRec["vgp_dp"]='%7.1f ' % (dp)
		PmagResRec["vgp_dm"]='%7.1f ' % (dm)
		PmagResRec["magic_method_codes"]= PmagSiteRec["magic_method_codes"]
		if PmagSiteRec['site_tilt_correction']=='0':PmagSiteRec['magic_method_codes']=PmagSiteRec['magic_method_codes']+":DA-DIR-GEO"
                if PmagSiteRec['site_tilt_correction']=='100':PmagSiteRec['magic_method_codes']=PmagSiteRec['magic_method_codes']+":DA-DIR-TILT"
                PmagSiteRec['site_polarity']=""
                if polarity==1: # assign polarity based on angle of pole lat to spin axis - may want to re-think this sometime
                      angle=pmag.angle([0,0],[0,(90-plat)])
                      if angle <= 55.: PmagSiteRec["site_polarity"]='n'
                      if angle > 55. and angle < 125.: PmagSiteRec["site_polarity"]='t'
                      if angle >= 125.: PmagSiteRec["site_polarity"]='r'
                PmagResults.append(PmagResRec)
    if noInt!=1 and nositeints!=1:
      for site in sites: # now do intensities for each site
        if plotsites==1:print site
        if Iaverage==0: key,intlist='specimen',SpecInts # if using specimen level data
        if Iaverage==1: key,intlist='sample',PmagSamps # if using sample level data
        Ints=pmag.get_dictitem(intlist,'er_site_name',site,'T') # get all the intensities  for this site
        if len(Ints)>0: # there are some
            PmagSiteRec=pmag.average_int(Ints,key,'site') # get average intensity stuff for site table
            PmagResRec=pmag.average_int(Ints,key,'average') # get average intensity stuff for results table
            if plotsites==1: # if site by site examination requested - print this site out to the screen
                for rec in Ints:print rec['er_'+key+'_name'],' %7.1f'%(1e6*float(rec[key+'_int']))
                if len(Ints)>1:
                    print 'Average: ','%7.1f'%(1e6*float(PmagResRec['average_int'])),'N: ',len(Ints)
                    print 'Sigma: ','%7.1f'%(1e6*float(PmagResRec['average_int_sigma'])),'Sigma %: ',PmagResRec['average_int_sigma_perc']
                raw_input('Press any key to continue\n')
            er_location_name=Ints[0]["er_location_name"] 
            PmagSiteRec["er_location_name"]=er_location_name # decorate the records
            PmagSiteRec["er_citation_names"]="This study"
            PmagResRec["er_location_names"]=er_location_name
            PmagResRec["er_citation_names"]="This study"
            PmagSiteRec["er_analyst_mail_names"]=user
            PmagResRec["er_analyst_mail_names"]=user
            PmagResRec["data_type"]='i'
            if Iaverage==0:
                PmagSiteRec['er_specimen_names']= pmag.get_list(Ints,'er_specimen_name') # list of all specimens used
                PmagResRec['er_specimen_names']= pmag.get_list(Ints,'er_specimen_name')
            PmagSiteRec['er_sample_names']= pmag.get_list(Ints,'er_sample_name') # list of all samples used
            PmagResRec['er_sample_names']= pmag.get_list(Ints,'er_sample_name')
            PmagSiteRec['er_site_name']= site
            PmagResRec['er_site_names']= site
            PmagSiteRec['magic_method_codes']= pmag.get_list(Ints,'magic_method_codes')
            PmagResRec['magic_method_codes']= pmag.get_list(Ints,'magic_method_codes')
            kill=pmag.grade(PmagSiteRec,accept,'site_int')
            if nocrit==1 or len(kill)==0:
                b,sig=float(PmagResRec['average_int']),""
                if(PmagResRec['average_int_sigma'])!="":sig=float(PmagResRec['average_int_sigma'])
                sdir=pmag.get_dictitem(PmagResults,'er_site_names',site,'T') # fish out site direction
                if len(sdir)>0 and  sdir[-1]['average_inc']!="": # get the VDM for this record using last average inclination (hope it is the right one!)
                        inc=float(sdir[0]['average_inc']) # 
                        mlat=pmag.magnetic_lat(inc) # get magnetic latitude using dipole formula
                        PmagResRec["vdm"]='%8.3e '% (pmag.b_vdm(b,mlat)) # get VDM with magnetic latitude
                        PmagResRec["vdm_n"]=PmagResRec['average_int_n']
                        if 'average_int_sigma' in PmagResRec.keys() and PmagResRec['average_int_sigma']!="":
                            vdm_sig=pmag.b_vdm(float(PmagResRec['average_int_sigma']),mlat)
                            PmagResRec["vdm_sigma"]='%8.3e '% (vdm_sig)
                        else:
                            PmagResRec["vdm_sigma"]=""
                mlat="" # define a model latitude
                if get_model_lat==1: # use present site latitude
                    mlats=pmag.get_dictitem(SiteNFO,'er_site_name',site,'T')
                    if len(mlats)>0: mlat=mlats[0]['site_lat']
                elif get_model_lat==2: # use a model latitude from some plate reconstruction model (or something)
                    mlats=pmag.get_dictitem(ModelLats,'er_site_name',site,'T')
                    if len(mlats)>0: PmagResRec['model_lat']=mlats[0]['site_model_lat']
                    mlat=PmagResRec['model_lat']
                if mlat!="":
                    PmagResRec["vadm"]='%8.3e '% (pmag.b_vdm(b,float(mlat))) # get the VADM using the desired latitude
                    if sig!="":
                        vdm_sig=pmag.b_vdm(float(PmagResRec['average_int_sigma']),float(mlat))
                        PmagResRec["vadm_sigma"]='%8.3e '% (vdm_sig)
                        PmagResRec["vadm_n"]=PmagResRec['average_int_n']
                    else:
                        PmagResRec["vadm_sigma"]=""
	        sitedat=pmag.get_dictitem(SiteNFO,'er_site_name',PmagSiteRec['er_site_name'],'T') # fish out site information (lat/lon, etc.)
                if len(sitedat)>0:
                    sitedat=sitedat[0]
                    PmagResRec['average_lat']=sitedat['site_lat']
                    PmagResRec['average_lon']=sitedat['site_lon']
                else:
                    PmagResRec['average_lon']='UNKNOWN'
                    PmagResRec['average_lon']='UNKNOWN'
                PmagResRec['magic_software_packages']=version_num
                PmagResRec["pmag_result_name"]="V[A]DM: Site "+site
                PmagResRec["result_description"]="V[A]DM of site"
                PmagResRec["pmag_criteria_codes"]="ACCEPT"
                if agefile != "": PmagResRec= pmag.get_age(PmagResRec,"er_site_names","average_",AgeNFO,DefaultAge)
                site_height=pmag.get_dictitem(height_nfo,'er_site_name',site,'T')
                if len(site_height)>0:PmagResRec["average_height"]=site_height[0]['site_height']
                PmagSites.append(PmagSiteRec)
                PmagResults.append(PmagResRec)
    if len(PmagSites)>0:
        Tmp,keylist=pmag.fillkeys(PmagSites)         
        pmag.magic_write(siteout,Tmp,'pmag_sites')
        print ' sites written to ',siteout
    else: print "No Site level table"
    if len(PmagResults)>0:
        TmpRes,keylist=pmag.fillkeys(PmagResults)         
        pmag.magic_write(resout,TmpRes,'pmag_results')
        print ' results written to ',resout
    else: print "No Results level table"
Example #14
0
def main(command_line=True, **kwargs):
    """
    NAME
        PMD_magic.py
 
    DESCRIPTION
        converts PMD (Enkin)  format files to magic_measurements format files

    SYNTAX
        PMD_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -f FILE: specify  input file, or
        -F FILE: specify output file, default is magic_measurements.txt
        -Fsa: specify er_samples format file for appending, default is new er_samples.txt
        -spc NUM : specify number of characters to designate a  specimen, default = 1
        -loc LOCNAME : specify location/study name
        -A: don't average replicate measurements
        -ncn NCON: specify naming convention
        -mcd [SO-MAG,SO-SUN,SO-SIGHT...] supply how these samples were oriented
       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] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name same as sample
            [6] site is entered under a separate column
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.
 
    INPUT
        PMD format files
    """
# initialize some stuff
    noave=0
    inst=""
    samp_con,Z='1',""
    missing=1
    demag="N"
    er_location_name="unknown"
    citation='This study'
    args=sys.argv
    meth_code="LP-NO"
    specnum=-1
    MagRecs=[]
    version_num=pmag.get_version()
    Samps=[] # keeps track of sample orientations
    DIspec=[]
    MagFiles=[]

    user=""
    mag_file=""
    dir_path='.'
    ErSamps=[]
    SampOuts=[]

    samp_file = 'er_samples.txt'
    meas_file = 'magic_measurements.txt'


    #
    # get command line arguments
    #
    
    if command_line:
        if '-WD' in sys.argv:
            ind = sys.argv.index('-WD')
            dir_path=sys.argv[ind+1]
        if '-ID' in sys.argv:
            ind = sys.argv.index('-ID')
            input_dir_path = sys.argv[ind+1]
        else:
            input_dir_path = dir_path
        output_dir_path = dir_path
        if "-h" in args:
            print main.__doc__
            return False
        if '-F' in args:
            ind=args.index("-F")
            meas_file = args[ind+1]
        if '-Fsa' in args:
            ind = args.index("-Fsa")
            samp_file = args[ind+1]
            #try:
            #    open(samp_file,'rU')
            #    ErSamps,file_type=pmag.magic_read(samp_file)
            #    print 'sample information will be appended to ', samp_file 
            #except:
            #    print samp_file,' not found: sample information will be stored in new er_samples.txt file'
            #    samp_file = output_dir_path+'/er_samples.txt'
        if '-f' in args:
            ind = args.index("-f")
            mag_file= args[ind+1]
        if "-spc" in args:
            ind = args.index("-spc")
            specnum = int(args[ind+1])
        if "-ncn" in args:
            ind=args.index("-ncn")
            samp_con=sys.argv[ind+1]
        if "-loc" in args:
            ind=args.index("-loc")
            er_location_name=args[ind+1]
        if "-A" in args: noave=1
        if "-mcd" in args: 
            ind=args.index("-mcd")
            meth_code=args[ind+1]

    if not command_line:
        dir_path = kwargs.get('dir_path', '.')
        input_dir_path = kwargs.get('input_dir_path', dir_path)
        output_dir_path = dir_path
        meas_file = kwargs.get('meas_file', 'magic_measurements.txt')
        mag_file = kwargs.get('mag_file')
        samp_file = kwargs.get('samp_file', 'er_samples.txt')
        specnum = kwargs.get('specnum', 0)
        samp_con = kwargs.get('samp_con', '1')
        er_location_name = kwargs.get('er_location_name', '')
        noave = kwargs.get('noave', 0) # default (0) means DO average
        meth_code = kwargs.get('meth_code', "LP-NO")

    print samp_con
    # format variables
    mag_file = input_dir_path+"/" + mag_file
    meas_file = output_dir_path+"/" + meas_file
    samp_file = output_dir_path+"/" + samp_file
    if specnum!=0:specnum=-specnum
    if "4" in samp_con:
        if "-" not in samp_con:
            print "naming convention option [4] must be in form 4-Z where Z is an integer"
            return False, "naming convention option [4] must be in form 4-Z where Z is an integer"
        else:
            Z=samp_con.split("-")[1]
            samp_con="4"
    if "7" in samp_con:
        if "-" not in samp_con:
            print "option [7] must be in form 7-Z where Z is an integer"
            return False, "naming convention option [7] must be in form 7-Z where Z is an integer"
        else:
            Z=samp_con.split("-")[1]
            samp_con="7"

    # parse data
    data=open(mag_file,'rU').readlines() # read in data from file
    comment=data[0]
    line=data[1].strip()
    line=line.replace("=","= ")  # make finding orientations easier
    rec=line.split() # read in sample orientation, etc.
    er_specimen_name=rec[0]
    ErSampRec,ErSiteRec={},{} # make a  sample record
    if specnum!=0:
        er_sample_name=rec[0][:specnum]
    else:
        er_sample_name=rec[0]
    if len(ErSamps)>0: # need to copy existing
       for samp in ErSamps:
           if samp['er_sample_name']==er_sample_name:
               ErSampRec=samp  # we'll ammend this one
           else:
               SampOuts.append(samp) # keep all the others
    if int(samp_con)<6:
        er_site_name=pmag.parse_site(er_sample_name,samp_con,Z)
    else:
        if 'er_site_name' in ErSampRec.keys():er_site_name=ErSampREc['er_site_name']
        if 'er_location_name' in ErSampRec.keys():er_location_name=ErSampREc['er_location_name']
    az_ind=rec.index('a=')+1
    ErSampRec['er_sample_name']=er_sample_name
    ErSampRec['er_sample_description']=comment
    ErSampRec['sample_azimuth']=rec[az_ind]
    dip_ind=rec.index('b=')+1
    dip=-float(rec[dip_ind])
    ErSampRec['sample_dip']='%7.1f'%(dip)
    strike_ind=rec.index('s=')+1
    ErSampRec['sample_bed_dip_direction']='%7.1f'%(float(rec[strike_ind])+90.)
    bd_ind=rec.index('d=')+1
    ErSampRec['sample_bed_dip']=rec[bd_ind]
    v_ind=rec.index('v=')+1
    vol=rec[v_ind][:-3]
    date=rec[-2]
    time=rec[-1]
    ErSampRec['magic_method_codes']=meth_code
    if 'er_location_name' not in ErSampRec.keys():ErSampRec['er_location_name']=er_location_name
    if 'er_site_name' not in ErSampRec.keys():ErSampRec['er_site_name']=er_site_name
    if 'er_citation_names' not in ErSampRec.keys():ErSampRec['er_citation_names']='This study'
    if 'magic_method_codes' not in ErSampRec.keys():ErSampRec['magic_method_codes']='SO-NO'
    SampOuts.append(ErSampRec)
    for k in range(3,len(data)): # read in data
      line=data[k]
      rec=line.split()
      if len(rec)>1: # skip blank lines at bottom  
        MagRec={}
        MagRec['measurement_description']='Date: '+date+' '+time
        MagRec["er_citation_names"]="This study"
        MagRec['er_location_name']=er_location_name
        MagRec['er_site_name']=er_site_name
        MagRec['er_sample_name']=er_sample_name
        MagRec['magic_software_packages']=version_num
        MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
        MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
        MagRec["measurement_flag"]='g'
        MagRec["measurement_standard"]='u'
        MagRec["measurement_number"]='1'
        MagRec["er_specimen_name"]=er_specimen_name
        if rec[0]=='NRM': 
            meas_type="LT-NO"
        elif rec[0][0]=='M' or rec[0][0]=='H': 
            meas_type="LT-AF-Z"
        elif rec[0][0]=='T': 
            meas_type="LT-T-Z"
        else:
            print "measurement type unknown"
            return False, "measurement type unknown"
        X=[float(rec[1]),float(rec[2]),float(rec[3])]
        Vec=pmag.cart2dir(X)
        MagRec["measurement_magn_moment"]='%10.3e'% (Vec[2]) # Am^2 
        MagRec["measurement_magn_volume"]=rec[4] # A/m 
        MagRec["measurement_dec"]='%7.1f'%(Vec[0])
        MagRec["measurement_inc"]='%7.1f'%(Vec[1])
        MagRec["treatment_ac_field"]='0'
        if meas_type!='LT-NO':
            treat=float(rec[0][1:])
        else:
            treat=0
        if meas_type=="LT-AF-Z":
            MagRec["treatment_ac_field"]='%8.3e' %(treat*1e-3) # convert from mT to tesla
        elif meas_type=="LT-T-Z":
            MagRec["treatment_temp"]='%8.3e' % (treat+273.) # temp in kelvin
        MagRec['magic_method_codes']=meas_type
        MagRecs.append(MagRec) 
    MagOuts=pmag.measurements_methods(MagRecs,noave)
    pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    print "results put in ",meas_file
    pmag.magic_write(samp_file,SampOuts,'er_samples')
    print "sample orientations put in ",samp_file
    return True, meas_file
Example #15
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 #16
0
def main():
    """
    NAME
        TDT_magic.py
 
    DESCRIPTION
        converts ThellierTool format files to magic_measurements format files

    SYNTAX
        TDT_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify .tdt format input file, required
        -F FILE: specify output file, default is magic_measurements.txt
        -spc NUM : specify number of characters to designate a  specimen, default = 0
        -loc LOCNAME : specify location/study name, must have either LOCNAME or SAMPFILE or be a synthetic
        -ncn NCON:  specify naming convention: default is #1 below
       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] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name same as sample
            [6] site is entered under a separate column
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.
 
 
    INPUT
        Format of ThellierTool  files:   
   2 line header:
   Thellier-tdt
   XX.0  (field in microtesla)
   Data:
   Spec Treat Intensity Declination Inclination
        
        Spec: specimen name
        Treat:  treatment step
                XXX.00  first zero field step
                XXX.11 (or .1)  first in field step [XXX.0 and XXX.1 can be done in any order]
                XXX.12 (or .2)second in-field step at lower temperature (pTRM check)
                XXX.13 (or .3) second zero-field step after infield (pTRM check step)
                       XXX.13 MUST be done in this order [XXX.00, XXX.11 [optional XXX.12] XXX.13]
         
         Intensity assumed to be total moment in 10^3 Am^2 (emu)
         Declination:  Declination in specimen coordinate system
         Inclination:  Declination in specimen coordinate system

    """
# initialize some stuff
    noave=0
    methcode,inst="",""
    phi,theta,labfield=0,90,0
    pTRM,MD,samp_con,Z=0,0,'1',1
    demag="N"
    er_location_name=""
    citation='This study'
    args=sys.argv
    methcode="LP-NO"
    specnum=0
#
# get command line arguments
#
    dir_path='.'
    meas_file,samp_file="magic_measurements.txt","er_samples.txt"
    user=""
    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=args[ind+1]
    if '-F' in args:
        ind=args.index("-F")
        meas_file=dir_path+'/'+args[ind+1]
    if '-f' in args:
        ind=args.index("-f")
        magfile=dir_path+'/'+args[ind+1]
        try:
            input=open(magfile,'rU')
        except:
            print "bad mag file name"
            sys.exit()
    else: 
        print "mag_file field is required option"
        print main.__doc__
        sys.exit()
    if "-spc" in args:
        ind=args.index("-spc")
        specnum=int(args[ind+1])
        if specnum!=0:specnum=-specnum
    if "-loc" in args:
        ind=args.index("-loc")
        er_location_name=args[ind+1]
    if "-ncn" in args:
        ind=args.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 "7" in samp_con:
            if "-" not in samp_con:
                print "option [4] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="4"
    MagRecs=[]
    demag="T"
    version_num=pmag.get_version()
    data=input.readlines()
    rec=data[1].split()
    labfield=float(rec[0])*1e-6
    for line in data[2:]:
        rec=line.split()
        if len(rec)>2:
            MagRec={}
            MagRec['er_location_name']=er_location_name
            MagRec['magic_software_packages']=version_num
            MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
            MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
            MagRec["treatment_ac_field"]='0'
            MagRec["treatment_dc_field"]='0'
            MagRec["treatment_dc_field_phi"]='0'
            MagRec["treatment_dc_field_theta"]='0'
            meas_type="LT-NO"
            MagRec["er_specimen_name"]=rec[0]
            MagRec["er_synthetic_name"]=""
            MagRec["er_site_name"]=""
            if specnum!=0:
                MagRec["er_sample_name"]=rec[0][:specnum]
            else:
                MagRec["er_sample_name"]=rec[0]
            if "-fsa" in args:
                for samp in Samps:
                    if samp["er_sample_name"] == MagRec["er_sample_name"]: 
                        MagRec["er_location_name"]=samp["er_location_name"]
                        MagRec["er_site_name"]=samp["er_site_name"]
                        break
            elif int(samp_con)!=6:
                site=pmag.parse_site(MagRec['er_sample_name'],samp_con,Z)
                MagRec["er_site_name"]=site
            if MagRec['er_site_name']=="":
                print 'No site name found for: ',MagRec['er_specimen_name'],MagRec['er_sample_name']
            if MagRec["er_location_name"]=="":
                print 'no location name for: ',MagRec["er_specimen_name"] 
            if rec[1]==".00":rec[1]="0.00"
            treat=rec[1].split('.')
            if float(rec[1])==0:
                pass 
            if len(treat)==1:treat.append('0')
            MagRec["treatment_temp"]='%8.3e' % (float(treat[0])+273.) # temp in kelvin
            if treat[1][0]=='0':
                meas_type="LT-T-Z"
            else: 
                MagRec["treatment_dc_field"]='%8.3e' % (labfield) # labfield in tesla (convert from microT)
                MagRec["treatment_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                MagRec["treatment_dc_field_theta"]='%7.1f' % (theta) # labfield theta
                if treat[1][-1]=='1':meas_type="LT-T-I" # in-field thermal step
                if treat[1][-1]=='2':
                    meas_type="LT-PTRM-I" # pTRM check
                    pTRM=1
                if treat[1][-1]=='3':
                    MagRec["treatment_dc_field"]='0'  # this is a zero field step
                    meas_type="LT-PTRM-MD" # pTRM tail check
            MagRec["measurement_magn_moment"]='%10.3e'% (float(rec[2])*1e-3) # moment in Am^2 (from emu)
            MagRec["measurement_dec"]=rec[3]
            MagRec["measurement_inc"]=rec[4]
            MagRec["er_analyst_mail_names"]=user
            MagRec["er_citation_names"]=citation
            MagRec["magic_method_codes"]=meas_type
            MagRec["measurement_flag"]='g'
            MagRec["er_specimen_name"]=rec[0]
            MagRec["measurement_standard"]='u'
            MagRec["measurement_number"]='1'
            MagRecs.append(MagRec) 
    MagOuts=pmag.measurements_methods(MagRecs,noave)
    pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    print "results put in ",meas_file
Example #17
0
def main(command_line=True, **kwargs):
    """
    NAME
        BGC_magic.py

    DESCRIPTION
        converts Berkeley Geochronology Center (BGC) format files to magic_measurements format files

    SYNTAX
        BGC_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -f FILE: specify  input file, or
        -F FILE: specify output file, default is magic_measurements.txt
        -Fsa: specify er_samples format file for appending, default is new er_samples.txt (Not working yet)
        -loc LOCNAME : specify location/study name
        -site SITENAME : specify site name
        -A: don't average replicate measurements
        -mcd [SO-MAG,SO-SUN,SO-SIGHT...] supply how these samples were oriented
        -v NUM : specify the volume in cc of the sample, default 2.5^3cc. Will use vol in data file if volume!=0 in file.

    INPUT
        BGC paleomag format file
    """
    # initialize some stuff
    noave = 0
    volume = 0.025 ** 3  # default volume is a 2.5cm cube
    # inst=""
    # samp_con,Z='1',""
    # missing=1
    # demag="N"
    er_location_name = "unknown"
    er_site_name = "unknown"
    # citation='This study'
    args = sys.argv
    meth_code = "LP-NO"
    # specnum=1
    version_num = pmag.get_version()

    mag_file = ""
    dir_path = "."
    MagRecs = []
    SampOuts = []

    samp_file = "er_samples.txt"
    meas_file = "magic_measurements.txt"
    meth_code = ""
    #
    # get command line arguments
    #

    if command_line:
        if "-WD" in sys.argv:
            ind = sys.argv.index("-WD")
            dir_path = sys.argv[ind + 1]
        if "-ID" in sys.argv:
            ind = sys.argv.index("-ID")
            input_dir_path = sys.argv[ind + 1]
        else:
            input_dir_path = dir_path
        output_dir_path = dir_path
        if "-h" in args:
            print main.__doc__
            return False
        if "-F" in args:
            ind = args.index("-F")
            meas_file = args[ind + 1]
        if "-Fsa" in args:
            ind = args.index("-Fsa")
            samp_file = args[ind + 1]
            # try:
            #    open(samp_file,'rU')
            #    ErSamps,file_type=pmag.magic_read(samp_file)
            #    print 'sample information will be appended to ', samp_file
            # except:
            #    print samp_file,' not found: sample information will be stored in new er_samples.txt file'
            #    samp_file = output_dir_path+'/er_samples.txt'
        if "-f" in args:
            ind = args.index("-f")
            mag_file = args[ind + 1]
        if "-loc" in args:
            ind = args.index("-loc")
            er_location_name = args[ind + 1]
        if "-site" in args:
            ind = args.index("-site")
            er_site_name = args[ind + 1]
        if "-A" in args:
            noave = 1
        if "-mcd" in args:
            ind = args.index("-mcd")
            meth_code = args[ind + 1]
            # samp_con='5'
        if "-v" in args:
            ind = args.index("-v")
            volume = float(args[ind + 1]) * 1e-6  # enter volume in cc, convert to m^3
    if not command_line:
        dir_path = kwargs.get("dir_path", ".")
        input_dir_path = kwargs.get("input_dir_path", dir_path)
        output_dir_path = dir_path
        meas_file = kwargs.get("meas_file", "magic_measurements.txt")
        mag_file = kwargs.get("mag_file")
        samp_file = kwargs.get("samp_file", "er_samples.txt")
        er_location_name = kwargs.get("er_location_name", "")
        er_site_name = kwargs.get("er_site_name", "")
        noave = kwargs.get("noave", 0)  # default (0) means DO average
        meth_code = kwargs.get("meth_code", "LP-NO")
        volume = float(kwargs.get("volume", 0))
        if not volume:
            volume = 0.025 ** 3  # default volume is a 2.5 cm cube, translated to meters cubed
        else:
            # convert cm^3 to m^3
            volume *= 1e-6

    # format variables
    if not mag_file:
        return False, "You must provide a BCG format file"
    mag_file = os.path.join(input_dir_path, mag_file)
    meas_file = os.path.join(output_dir_path, meas_file)
    samp_file = os.path.join(output_dir_path, samp_file)

    ErSampRec = {}

    # parse data

    # Open up the BGC file and read the header information
    print "mag_file in BGC_magic", mag_file
    pre_data = open(mag_file, "rU")
    line = pre_data.readline()
    line_items = line.split(" ")
    sample_name = line_items[2]
    sample_name = sample_name.replace("\n", "")
    line = pre_data.readline()
    line = pre_data.readline()
    line_items = line.split("\t")
    sample_azimuth = float(line_items[1])
    sample_dip = 90.0 - float(line_items[2])
    sample_bed_dip = line_items[3]
    sample_bed_azimuth = line_items[4]
    sample_lon = line_items[5]
    sample_lat = line_items[6]
    tmp_volume = line_items[7]
    if tmp_volume != 0.0:
        volume = float(tmp_volume) * 1e-6
    pre_data.close()

    data = pd.read_csv(mag_file, sep="\t", header=3, index_col=False)

    #    print "\ndata\n", data

    cart = np.array([data["X"], data["Y"], data["Z"]]).transpose()
    direction = pmag.cart2dir(cart).transpose()

    data["measurement_dec"] = direction[0]
    data["measurement_inc"] = direction[1]
    data["measurement_magn_moment"] = direction[2] * 1000 * volume  # the data are in EMU - this converts to Am^2
    data["measurement_magn_volume"] = direction[2] * 1000  # EMU  - data converted to A/m

    # DGEOs, IGEOs = [], []
    # print "len(data)=",len(data)
    # for ind in range(len(data)):
    #    dgeo,igeo=pmag.dogeo(data.ix[ind]['measurement_dec'],data.ix[ind]['measurement_inc'],90-sample_azimuth,sample_dip)
    #    DGEOs.append(dgeo)
    #    IGEOs.append(igeo)
    # data['specimen_dec']=DGEOs
    # data['specimen_inc']=IGEOs
    # data['specimen_tilt']='1'

    # print "data specimn_dec=",DGEOs
    # print "data specimn_inc=",IGEOs

    # Configure the er_sample table

    ErSampRec["er_sample_name"] = sample_name
    ErSampRec["sample_azimuth"] = sample_azimuth
    ErSampRec["sample_dip"] = sample_dip
    ErSampRec["sample_bed_dip_direction"] = sample_bed_azimuth
    ErSampRec["sample_bed_dip"] = sample_bed_dip
    ErSampRec["sample_lat"] = sample_lat
    ErSampRec["sample_lon"] = sample_lon
    ErSampRec["magic_method_codes"] = meth_code
    ErSampRec["er_location_name"] = er_location_name
    ErSampRec["er_site_name"] = er_site_name
    ErSampRec["er_citation_names"] = "This study"
    SampOuts.append(ErSampRec.copy())

    # Configure the magic_measurements table

    for rowNum, row in data.iterrows():
        MagRec = {}
        MagRec["measurement_description"] = "Date: " + str(row["Date"]) + " Time: " + str(row["Time"])
        MagRec["er_citation_names"] = "This study"
        MagRec["er_location_name"] = er_location_name
        MagRec["er_site_name"] = er_site_name
        MagRec["er_sample_name"] = sample_name
        MagRec["magic_software_packages"] = version_num
        MagRec["treatment_temp"] = "%8.3e" % (273)  # room temp in kelvin
        MagRec["measurement_temp"] = "%8.3e" % (273)  # room temp in kelvin
        MagRec["measurement_flag"] = "g"
        MagRec["measurement_standard"] = "u"
        MagRec["measurement_number"] = "1"
        MagRec["er_specimen_name"] = sample_name
        MagRec["treatment_ac_field"] = "0"
        if row["DM Val"] == "0":
            meas_type = "LT-NO"
        elif int(row["DM Type"]) > 0.0:
            meas_type = "LT-AF-Z"
            treat = float(row["DM Val"])
            MagRec["treatment_ac_field"] = "%8.3e" % (treat * 1e-3)  # convert from mT to tesla
        elif int(row["DM Type"]) == -1:
            meas_type = "LT-T-Z"
            treat = float(row["DM Val"])
            MagRec["treatment_temp"] = "%8.3e" % (treat + 273.0)  # temp in kelvin
        else:
            print "measurement type unknown:", row["DM Type"], " in row ", rowNum
        MagRec["measurement_magn_moment"] = str(row["measurement_magn_moment"])
        MagRec["measurement_magn_volume"] = str(row["measurement_magn_volume"])
        MagRec["measurement_dec"] = str(row["measurement_dec"])
        MagRec["measurement_inc"] = str(row["measurement_inc"])
        MagRec["magic_method_codes"] = meas_type
        MagRec["measurement_csd"] = "0.0"  # added due to magic.write error
        MagRec["measurement_positions"] = "1"  # added due to magic.write error
        MagRecs.append(MagRec.copy())
    pmag.magic_write(samp_file, SampOuts, "er_samples")
    print "sample orientations put in ", samp_file
    MagOuts = pmag.measurements_methods(MagRecs, noave)
    pmag.magic_write(meas_file, MagOuts, "magic_measurements")
    print "results put in ", meas_file
    print "exit!"
    return True, meas_file
Example #18
0
def main():
    """
    NAME
        IPG_magic.py
 
    DESCRIPTION
        converts PMD (IPG - PaleoMac)  format files to magic_measurements format files

    SYNTAX
        IPG_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify  input file, or
        -F FILE: specify output file, default is magic_measurements.txt
        -Fsa: specify er_samples format file for appending, default is new er_samples.txt
        -spc NUM : specify number of characters to designate a  specimen, default = 1
        -loc LOCNAME : specify location/study name
        -A: don't average replicate measurements
        -ncn NCON: specify naming convention
       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] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name same as sample
            [6] site is entered under a separate column
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.
 
 
    INPUT
        IPG-PMD format files
    """
    # initialize some stuff
    noave = 0
    methcode, inst = "", ""
    samp_con, Z = "1", ""
    missing = 1
    demag = "N"
    er_location_name = "unknown"
    citation = "This study"
    args = sys.argv
    methcode = "LP-NO"
    specnum = -1
    MagRecs = []
    version_num = pmag.get_version()
    Samps = []  # keeps track of sample orientations
    DIspec = []
    MagFiles = []
    #
    # get command line arguments
    #
    user = ""
    mag_file = ""
    dir_path = "."
    ErSamps = []
    SampOuts = []
    if "-WD" in sys.argv:
        ind = sys.argv.index("-WD")
        dir_path = sys.argv[ind + 1]
    samp_file = dir_path + "/er_samples.txt"
    meas_file = dir_path + "/magic_measurements.txt"
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-usr" in args:
        ind = args.index("-usr")
        user = args[ind + 1]
    if "-F" in args:
        ind = args.index("-F")
        meas_file = dir_path + "/" + args[ind + 1]
    if "-Fsa" in args:
        ind = args.index("-Fsa")
        samp_file = dir_path + "/" + args[ind + 1]
        try:
            open(samp_file, "rU")
            ErSamps, file_type = pmag.magic_read(samp_file)
            print "sample information will be appended to ", samp_file
        except:
            print samp_file, " not found: sample information will be stored in new er_samples.txt file"
            samp_file = dir_path + "/er_samples.txt"
    if "-f" in args:
        ind = args.index("-f")
        mag_file = dir_path + "/" + args[ind + 1]
    if "-spc" in args:
        ind = args.index("-spc")
        specnum = int(args[ind + 1])
        if specnum != 0:
            specnum = -specnum
    if "-ncn" in args:
        ind = args.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 "7" in samp_con:
            if "-" not in samp_con:
                print "option [7] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z = samp_con.split("-")[1]
                samp_con = "7"
    if "-loc" in args:
        ind = args.index("-loc")
        er_location_name = args[ind + 1]
    if "-A" in args:
        noave = 1
    data = open(mag_file, "rU").readlines()  # read in data from file
    for line in data:
        rec = line.split()
        if "E-" not in rec[1] and "E+" not in rec[1]:  # new specimen
            er_specimen_name = rec[0]
            ErSampRec, ErSiteRec = {}, {}  # make a  sample record
            if specnum != 0:
                er_sample_name = rec[0][:specnum]
            else:
                er_sample_name = rec[0]
            if len(ErSamps) > 0:  # need to copy existing
                for samp in ErSamps:
                    if samp["er_sample_name"] == er_sample_name:
                        ErSampRec = samp  # we'll ammend this one
                    else:
                        SampOuts.append(samp)  # keep all the others
            if int(samp_con) < 6:
                er_site_name = pmag.parse_site(er_sample_name, samp_con, Z)
            else:
                if "er_site_name" in ErSampRec.keys():
                    er_site_name = ErSampREc["er_site_name"]
                if "er_location_name" in ErSampRec.keys():
                    er_location_name = ErSampREc["er_location_name"]
            ErSampRec["er_sample_name"] = er_sample_name
            ErSampRec["sample_azimuth"] = rec[1]
            dip = -float(rec[2])
            ErSampRec["sample_dip"] = "%7.1f" % (dip)
            ErSampRec["sample_bed_dip_direction"] = "%7.1f" % (float(rec[3]) + 90.0)
            ErSampRec["sample_bed_dip"] = rec[4]
            if "er_location_name" not in ErSampRec.keys():
                ErSampRec["er_location_name"] = er_location_name
            if "er_site_name" not in ErSampRec.keys():
                ErSampRec["er_site_name"] = er_site_name
            if "er_citation_names" not in ErSampRec.keys():
                ErSampRec["er_citation_names"] = "This study"
            if "magic_method_codes" not in ErSampRec.keys():
                ErSampRec["magic_method_codes"] = "SO-NO"
            SampOuts.append(ErSampRec)
        elif rec[0][0] == "N" or rec[0][0] == "T" or rec[0][0] == "M":
            if len(rec) > 1:  # skip blank lines at bottom
                MagRec = {}
                #            MagRec['measurement_date']=measdate
                MagRec["er_citation_names"] = "This study"
                MagRec["er_location_name"] = er_location_name
                MagRec["er_site_name"] = er_site_name
                MagRec["er_sample_name"] = er_sample_name
                MagRec["magic_software_packages"] = version_num
                MagRec["treatment_temp"] = "%8.3e" % (273)  # room temp in kelvin
                MagRec["measurement_temp"] = "%8.3e" % (273)  # room temp in kelvin
                MagRec["measurement_flag"] = "g"
                MagRec["measurement_standard"] = "u"
                MagRec["measurement_number"] = "1"
                MagRec["er_specimen_name"] = er_specimen_name
                if rec[0] == "NRM":
                    meas_type = "LT-NO"
                elif rec[0][0] == "M":
                    meas_type = "LT-AF-Z"
                elif rec[0][0] == "T":
                    meas_type = "LT-T-Z"
                else:
                    print "measurement type unknown"
                    sys.exit()
                X = [float(rec[1]), float(rec[2]), float(rec[3])]
                Vec = pmag.cart2dir(X)
                MagRec["measurement_magn_moment"] = "%10.3e" % (Vec[2])  # Am^2
                #            MagRec["measurement_magn_volume"]=rec[4] # A/m
                MagRec["measurement_dec"] = "%7.1f" % (Vec[0])
                MagRec["measurement_inc"] = "%7.1f" % (Vec[1])
                MagRec["treatment_ac_field"] = "0"
                if meas_type != "LT-NO":
                    treat = float(rec[0][1:])
                else:
                    treat = 0
                if meas_type == "LT-AF-Z":
                    MagRec["treatment_ac_field"] = "%8.3e" % (treat * 1e-3)  # convert from mT to tesla
                elif meas_type == "LT-T-Z":
                    MagRec["treatment_temp"] = "%8.3e" % (treat + 273.0)  # temp in kelvin
                MagRec["magic_method_codes"] = meas_type
                MagRecs.append(MagRec)
    MagOuts = pmag.measurements_methods(MagRecs, noave)
    pmag.magic_write(meas_file, MagOuts, "magic_measurements")
    print "results put in ", meas_file
    pmag.magic_write(samp_file, SampOuts, "er_samples")
    print "sample orientations put in ", samp_file
Example #19
0
def main(command_line=True, **kwargs):
    """
    NAME
        IODP_jr6_magic.py
 
    DESCRIPTION
        converts shipboard .jr6 format files to magic_measurements format files

    SYNTAX
        IODP_jr6_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -f FILE: specify  input file, or
        -F FILE: specify output file, default is magic_measurements.txt
        -fsa FILE: specify  er_samples.txt file for sample name lookup ,
           default is 'er_samples.txt'
        -loc HOLE : specify hole name (U1456A)
        -A: don't average replicate measurements
 
    INPUT
        JR6 .jr6 format file
    """


    def fix_separation(filename, new_filename):
        old_file = open(filename, 'rU')
        data = old_file.readlines()
        new_data = []
        for line in data:
            new_line = line.replace('-', ' -')
            new_line = new_line.replace('  ', ' ')
            new_data.append(new_line)
        new_file = open(new_filename, 'w')
        for s in new_data:
            new_file.write(s)
        old_file.close()
        new_file.close()
        return new_filename
        

    def old_fix_separation(filename, new_filename):
        old_file = open(filename, 'rU')
        data = old_file.readlines()
        new_data = []
        for line in data:
            new_line = []
            for i in line.split():
                if '-' in i[1:]:
                    lead_char = '-' if i[0] == '-' else ''
                    if lead_char:
                        v = i[1:].split('-')
                    else:
                        v = i.split('-')
                    new_line.append(lead_char + v[0])
                    new_line.append('-' + v[1])
                else:
                    new_line.append(i)
            new_line = (' '.join(new_line)) + '\n'
            new_data.append(new_line)
        new_file = open(new_filename, 'w')
        for s in new_data:
            new_file.write(s)
        new_file.close()
        old_file.close()
        return new_filename


    
# initialize some stuff
    noave=0
    volume=2.5**3 #default volume is a 2.5cm cube
    inst=""
    samp_con,Z='5',""
    missing=1
    demag="N"
    er_location_name="unknown"
    citation='This study'
    args=sys.argv
    meth_code="LP-NO"
    version_num=pmag.get_version()
    dir_path='.'
    MagRecs=[]
    samp_file = 'er_samples.txt'
    meas_file = 'magic_measurements.txt'
    mag_file = ''
    #
    # get command line arguments
    #
    if command_line:
        if '-WD' in sys.argv:
            ind = sys.argv.index('-WD')
            dir_path=sys.argv[ind+1]
        if '-ID' in sys.argv:
            ind = sys.argv.index('-ID')
            input_dir_path = sys.argv[ind+1]
        else:
            input_dir_path = dir_path
        output_dir_path = dir_path
        if "-h" in args:
            print main.__doc__
            return False
        if '-F' in args:
            ind=args.index("-F")
            meas_file = args[ind+1]
        if '-fsa' in args:
            ind = args.index("-fsa")
            samp_file = args[ind+1]
            if samp_file[0]!='/':
                samp_file = os.path.join(input_dir_path, samp_file)
            try:
                open(samp_file,'rU')
                ErSamps,file_type=pmag.magic_read(samp_file)
            except:
                print samp_file,' not found: '
                print '   download csv file and import to MagIC with IODP_samples_magic.py'
        if '-f' in args:
            ind = args.index("-f")
            mag_file= args[ind+1]
        if "-loc" in args:
            ind=args.index("-loc")
            er_location_name=args[ind+1]
        if "-A" in args:
            noave=1
    if not command_line:
        dir_path = kwargs.get('dir_path', '.')
        input_dir_path = kwargs.get('input_dir_path', dir_path)
        output_dir_path = dir_path
        meas_file = kwargs.get('meas_file', 'magic_measurements.txt')
        mag_file = kwargs.get('mag_file', '')
        samp_file = kwargs.get('samp_file', 'er_samples.txt')
        specnum = kwargs.get('specnum', 1)
        samp_con = kwargs.get('samp_con', '1')
        if len(str(samp_con)) > 1:
            samp_con, Z = samp_con.split('-')
        else:
            Z = ''
        er_location_name = kwargs.get('er_location_name', '')
        noave = kwargs.get('noave', 0) # default (0) means DO average
        meth_code = kwargs.get('meth_code', "LP-NO")


    # format variables
    meth_code=meth_code+":FS-C-DRILL-IODP:SP-SS-C:SO-V"
    meth_code=meth_code.strip(":")
    if mag_file:
        mag_file = os.path.join(input_dir_path, mag_file)
    samp_file = os.path.join(input_dir_path, samp_file)
    meas_file = os.path.join(output_dir_path, meas_file)

    # validate variables
    if not mag_file:
        print "You must provide an IODP_jr6 format file"
        return False, "You must provide an IODP_jr6 format file"
    if not os.path.exists(mag_file):
        print 'The input file you provided: {} does not exist.\nMake sure you have specified the correct filename AND correct input directory name.'.format(os.path.join(input_dir_path, mag_file))
        return False, 'The input file you provided: {} does not exist.\nMake sure you have specified the correct filename AND correct input directory name.'.format(magfile)
    if not os.path.exists(samp_file):
        print 'samp_file', samp_file
        print "Your input directory:\n{}\nmust contain an er_samples.txt file, or you must explicitly provide one".format(input_dir_path)
        return False, "Your input directory:\n{}\nmust contain an er_samples.txt file, or you must explicitly provide one".format(input_dir_path)
    
    # parse data
    temp = os.path.join(output_dir_path, 'temp.txt')
    fix_separation(mag_file, temp)
    #os.rename('temp.txt', mag_file)
    #data = open(mag_file, 'rU').readlines()
    data=pd.read_csv(temp, delim_whitespace=True,header=None)
    os.remove(temp)
    samples,filetype = pmag.magic_read(samp_file)
    data.columns=['specname','step','negz','y','x','expon','sample_azimuth','sample_dip','sample_bed_dip_direction','sample_bed_dip','bed_dip_dir2','bed_dip2','param1','param2','param3','param4','measurement_csd']
    cart=np.array([data['x'],data['y'],-data['negz']]).transpose()
    dir= pmag.cart2dir(cart).transpose()
    data['measurement_dec']=dir[0]
    data['measurement_inc']=dir[1]
    data['measurement_magn_volume']=dir[2]*(10.0**data['expon']) # A/m  - data in A/m
    data['measurement_flag']='g'
    data['measurement_standard']='u'
    data['measurement_number']='1'
    data['measurement_temp']='273'
    data['er_location_name']=er_location_name
    for rowNum, row in data.iterrows():
        MagRec={}
        spec_text_id=row['specname'].split('_')[1]
        SampRecs=pmag.get_dictitem(samples,'er_sample_alternatives',spec_text_id,'has') # retrieve sample record for this specimen
        if len(SampRecs)>0: # found one
            MagRec['er_specimen_name']=SampRecs[0]['er_sample_name']
            MagRec['er_sample_name']=MagRec['er_specimen_name']
            MagRec['er_site_name']=MagRec['er_specimen_name']
            MagRec["er_citation_names"]="This study"
            MagRec['er_location_name']=er_location_name
            MagRec['magic_software_packages']=version_num
            MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
            MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
            MagRec["measurement_flag"]='g'
            MagRec["measurement_standard"]='u'
            MagRec["measurement_number"]='1'
            MagRec["treatment_ac_field"]='0'
            volume=float(SampRecs[0]['sample_volume'])
            moment=row['measurement_magn_volume'] * volume 
            MagRec["measurement_magn_moment"]=str(moment)
            MagRec["measurement_magn_volume"]=str(row['measurement_magn_volume'])
            MagRec["measurement_dec"]='%7.1f'%(row['measurement_dec'])
            MagRec["measurement_inc"]='%7.1f'%(row['measurement_inc'])
            if row['step'] == 'NRM':
                meas_type="LT-NO"
            elif row['step'][0:2] == 'AD':
                meas_type="LT-AF-Z"
                treat=float(row['step'][2:])
                MagRec["treatment_ac_field"]='%8.3e' %(treat*1e-3) # convert from mT to tesla
            elif row['step'][0] == 'TD':
                meas_type="LT-T-Z"
                treat=float(row['step'][2:])
                MagRec["treatment_temp"]='%8.3e' % (treat+273.) # temp in kelvin
            elif row['step'][0:3]=='ARM': # 
                meas_type="LT-AF-I"
                treat=float(row['step'][3:])
                MagRec["treatment_ac_field"]='%8.3e' %(treat*1e-3) # convert from mT to tesla
                MagRec["treatment_dc_field"]='%8.3e' %(50e-6) # assume 50uT DC field
                MagRec["measurement_description"]='Assumed DC field - actual unknown'
            elif row['step'][0:3]=='IRM': # 
                meas_type="LT-IRM"
                treat=float(row['step'][3:])
                MagRec["treatment_dc_field"]='%8.3e' %(treat*1e-3) # convert from mT to tesla
            else:
                print 'unknown treatment type for ',row
                return False, 'unknown treatment type for ',row
            MagRec['magic_method_codes']=meas_type
            MagRecs.append(MagRec.copy())
        else:
            print 'sample name not found: ',row['specname']
    MagOuts=pmag.measurements_methods(MagRecs,noave)
    file_created, error_message = pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    if file_created:
        return True, meas_file
    else:
        return False, 'Results not written to file'
Example #20
0
def main(command_line=True, **kwargs):
    """
    NAME
        IODP_dscr_magic.py
 
    DESCRIPTION
        converts ODP LIMS discrete sample format files to magic_measurements format files


    SYNTAX
        IODP_descr_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -f FILE: specify input .csv file, default is all in directory
        -F FILE: specify output  measurements file, default is magic_measurements.txt
        -A : don't average replicate measurements
    INPUTS
 	 IODP discrete sample .csv file format exported from LIMS database
    """
    #
    # initialize defaults
    version_num = pmag.get_version()
    meas_file = "magic_measurements.txt"
    csv_file = ""
    MagRecs, Specs = [], []
    citation = "This study"
    dir_path, demag = ".", "NRM"
    args = sys.argv
    noave = 0
    # get command line args
    if command_line:
        if "-WD" in args:
            ind = args.index("-WD")
            dir_path = args[ind + 1]
        if "-ID" in args:
            ind = args.index("-ID")
            input_dir_path = args[ind + 1]
        else:
            input_dir_path = dir_path
        output_dir_path = dir_path
        if "-h" in args:
            print main.__doc__
            return False
        if "-A" in args:
            noave = 1
        if "-f" in args:
            ind = args.index("-f")
            csv_file = args[ind + 1]
        if "-F" in args:
            ind = args.index("-F")
            meas_file = args[ind + 1]

    if not command_line:
        dir_path = kwargs.get("dir_path", ".")
        input_dir_path = kwargs.get("input_dir_path", dir_path)
        output_dir_path = dir_path  # rename dir_path after input_dir_path is set
        noave = kwargs.get("noave", 0)  # default (0) is DO average
        csv_file = kwargs.get("csv_file", "")
        meas_file = kwargs.get("meas_file", "magic_measurements.txt")

    # format variables

    meas_file = os.path.join(output_dir_path, meas_file)
    if csv_file == "":
        filelist = os.listdir(input_dir_path)  # read in list of files to import
    else:
        csv_file = os.path.join(input_dir_path, csv_file)
        filelist = [csv_file]
    # parsing the data
    file_found = False
    for file in filelist:  # parse each file
        if file[-3:].lower() == "csv":
            file_found = True
            print "processing: ", file
            input = open(file, "rU").readlines()
            keys = input[0].replace("\n", "").split(",")  # splits on underscores
            interval_key = "Offset (cm)"
            demag_key = "Demag level (mT)"
            offline_demag_key = "Treatment Value (mT or &deg;C)"
            offline_treatment_type = "Treatment type"
            run_key = "Test No."
            if "Inclination background + tray corrected  (deg)" in keys:
                inc_key = "Inclination background + tray corrected  (deg)"
            if "Inclination background &amp; tray corrected (deg)" in keys:
                inc_key = "Inclination background &amp; tray corrected (deg)"
            if "Declination background + tray corrected (deg)" in keys:
                dec_key = "Declination background + tray corrected (deg)"
            if "Declination background &amp; tray corrected (deg)" in keys:
                dec_key = "Declination background &amp; tray corrected (deg)"
            if "Intensity background + tray corrected  (A/m)" in keys:
                int_key = "Intensity background + tray corrected  (A/m)"
            if "Intensity background &amp; tray corrected (A/m)" in keys:
                int_key = "Intensity background &amp; tray corrected (A/m)"
            type = "Type"
            sect_key = "Sect"
            half_key = "A/W"
            # need to add volume_key to LORE format!
            if "Sample volume (cm^3)" in keys:
                volume_key = "Sample volume (cm^3)"
            if "Sample volume (cc)" in keys:
                volume_key = "Sample volume (cc)"
            if "Sample volume (cm&sup3;)" in keys:
                volume_key = "Sample volume (cm&sup3;)"
            for line in input[1:]:
                InRec = {}
                for k in range(len(keys)):
                    InRec[keys[k]] = line.split(",")[k]
                inst = "IODP-SRM"
                MagRec = {}
                expedition = InRec["Exp"]
                location = InRec["Site"] + InRec["Hole"]
                offsets = InRec[interval_key].split(
                    "."
                )  # maintain consistency with er_samples convention of using top interval
                if len(offsets) == 1:
                    offset = int(offsets[0])
                else:
                    offset = int(offsets[0]) - 1
                # interval=str(offset+1)# maintain consistency with er_samples convention of using top interval
                interval = str(offset)  # maintain consistency with er_samples convention of using top interval
                specimen = (
                    expedition
                    + "-"
                    + location
                    + "-"
                    + InRec["Core"]
                    + InRec[type]
                    + "-"
                    + InRec[sect_key]
                    + "_"
                    + InRec[half_key]
                    + "_"
                    + interval
                )
                if specimen not in Specs:
                    Specs.append(specimen)
                MagRec["er_expedition_name"] = expedition
                MagRec["er_location_name"] = location
                MagRec["er_site_name"] = specimen
                MagRec["er_citation_names"] = citation
                MagRec["er_specimen_name"] = specimen
                MagRec["er_sample_name"] = specimen
                MagRec["er_site_name"] = specimen
                # set up measurement record - default is NRM
                MagRec["magic_software_packages"] = version_num
                MagRec["treatment_temp"] = "%8.3e" % (273)  # room temp in kelvin
                MagRec["measurement_temp"] = "%8.3e" % (273)  # room temp in kelvin
                MagRec["treatment_ac_field"] = "0"
                MagRec["treatment_dc_field"] = "0"
                MagRec["treatment_dc_field_phi"] = "0"
                MagRec["treatment_dc_field_theta"] = "0"
                MagRec["measurement_flag"] = "g"  # assume all data are "good"
                MagRec["measurement_standard"] = "u"  # assume all data are "good"
                MagRec["measurement_csd"] = "0"  # assume all data are "good"
                volume = InRec[volume_key]
                MagRec["magic_method_codes"] = "LT-NO"
                sort_by = "treatment_ac_field"  # set default to AF demag
                if InRec[demag_key] != "0":
                    MagRec["magic_method_codes"] = "LT-AF-Z"
                    inst = inst + ":IODP-SRM-AF"  # measured on shipboard in-line 2G AF
                    treatment_value = float(InRec[demag_key].strip('"')) * 1e-3  # convert mT => T
                    if sort_by == "treatment_ac_field":
                        MagRec["treatment_ac_field"] = treatment_value  # AF demag in treat mT => T
                    else:
                        MagRec["treatment_ac_field"] = str(treatment_value)  # AF demag in treat mT => T
                elif offline_treatment_type in InRec.keys() and InRec[offline_treatment_type] != "":
                    if "Lowrie" in InRec["Comments"]:
                        MagRec["magic_method_codes"] = "LP-IRM-3D"
                        treatment_value = float(InRec[offline_demag_key].strip('"')) + 273.0  # convert C => K
                        MagRec["treatment_temp"] = treatment_value
                        MagRec["treatment_ac_field"] = "0"
                        sort_by = "treatment_temp"
                    elif "Isothermal" in InRec[offline_treatment_type]:
                        MagRec["magic_method_codes"] = "LT-IRM"
                        treatment_value = float(InRec[offline_demag_key].strip('"')) * 1e-3  # convert mT => T
                        MagRec["treatment_dc_field"] = treatment_value
                        MagRec["treatment_ac_field"] = "0"
                        sort_by = "treatment_dc_field"
                MagRec["measurement_standard"] = "u"  # assume all data are "good"
                vol = float(volume) * 1e-6  # convert from cc to m^3
                if run_key in InRec.keys():
                    run_number = InRec[run_key]
                    MagRec["external_database_ids"] = run_number
                    MagRec["external_database_names"] = "LIMS"
                else:
                    MagRec["external_database_ids"] = ""
                    MagRec["external_database_names"] = ""
                MagRec["measurement_description"] = "sample orientation: " + InRec["Sample orientation"]
                MagRec["measurement_inc"] = InRec[inc_key].strip('"')
                MagRec["measurement_dec"] = InRec[dec_key].strip('"')
                intens = InRec[int_key].strip('"')
                MagRec["measurement_magn_moment"] = "%8.3e" % (
                    float(intens) * vol
                )  # convert intensity from A/m to Am^2 using vol
                MagRec["magic_instrument_codes"] = inst
                MagRec["measurement_number"] = "1"
                MagRec["measurement_positions"] = ""
                MagRecs.append(MagRec)
    if not file_found:
        print "No .csv files were found"
        return False, "No .csv files were found"
    MagOuts = []
    for spec in Specs:
        Speclist = pmag.get_dictitem(MagRecs, "er_specimen_name", spec, "T")
        sorted = pmag.sort_diclist(Speclist, sort_by)
        for rec in sorted:
            for key in rec.keys():
                rec[key] = str(rec[key])
            MagOuts.append(rec)
    Fixed = pmag.measurements_methods(MagOuts, noave)
    Out, keys = pmag.fillkeys(Fixed)
    if pmag.magic_write(meas_file, Out, "magic_measurements"):
        print "data stored in ", meas_file
        return True, meas_file
    else:
        print "no data found.  bad magfile?"
        return False, "no data found.  bad magfile?"
Example #21
0
def main():
    """
    NAME
        MsT_magic.py
 
    DESCRIPTION
        converts MsT data (T,M) to magic_measurements format files

    SYNTAX
        MsT_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify T,M  format input file, required
        -fsa SFILE: name with sample, site, location information
        -F FILE: specify output file, default is MsT_measurements.txt
        -dc H: specify applied field during measurement, default is 0.5 T
        -syn  : This is a synthetic specimen and has no sample/site/location information
        -spn SPEC: specimen name 
        -spc NUM : specify number of characters to designate a  specimen, default = 0
        -loc LOCNAME : specify location/study name, must have either LOCNAME or SAMPFILE or be a synthetic
        -ncn NCON:  specify naming convention: default is #1 below
       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] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name same as sample
            [6] site is entered under a separate column
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.
        INPUT files: 
            T M:  T is in Centigrade and M is uncalibrated magnitude

    """
# initialize some stuff
    samp_con,Z="1","0"
    dir_path='.'
    citation='This study'
    args=sys.argv
    specnum,measnum=0,1
#
# get command line arguments
#
    user=""
    if '-WD' in args:
        ind=args.index("-WD")
        dir_path=args[ind+1]
    meas_file=dir_path+"/MsT_measurements.txt"
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-usr" in args:
        ind=args.index("-usr")
        user=args[ind+1]
    labfield='0.5'
    if "-dc" in args:
        ind=args.index("-dc")
        labfield=args[ind+1]
    if '-F' in args:
        ind=args.index("-F")
        meas_file=dir_path+'/'+args[ind+1]
    if "-fsa" in args:
        ind=args.index("-fsa")
        samp_file=dir_path+'/'+args[ind+1]
        Samps,file_type=pmag.magic_read(samp_file)
    if '-f' in args:
        ind=args.index("-f")
        infile=dir_path+'/'+args[ind+1]
        try:
            input=open(infile,'rU')
        except:
            print "bad mag file name"
            sys.exit()
    else: 
        print main.__doc__
        print "-f  is required option"
        sys.exit()
    if "-spc" in args:
        ind=args.index("-spc")
        specnum=int(args[ind+1])
        if specnum!=0:specnum=-specnum
    er_location_name,syn,specimen_name='unknown',0,'unknown'
    if "-loc" in args:
        ind=args.index("-loc")
        er_location_name=args[ind+1]
    if "-spn" in args:
        ind=args.index("-spn")
        specimen_name=args[ind+1]
    else: 
        print main.__doc__
        print "-spn  is required option"
        sys.exit()
    if "-syn" in args: syn=1
    if "-ncn" in args:
        ind=args.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"
            samp_con=sys.argv[ind+1]
        if "7" in samp_con:
            if "-" not in samp_con:
                print "option [7] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="7"
    MagRecs,specs=[],[]
    version_num=pmag.get_version()
    data=input.readlines()
    T0=float(data[0].split()[0])
    for line in data:
        instcode=""
        if len(line)>1:
            MagRec={}
            if syn==0: MagRec['er_location_name']=er_location_name
            MagRec['magic_software_packages']=version_num
            MagRec["treatment_dc_field"]=labfield
            rec=line.split()
            T=float(rec[0])
            MagRec["measurment_temp"]='%8.3e' % (float(rec[0])+273.) # temp in kelvin
            if T>T0:
                MagRec["magic_method_codes"]='LP-MW-I'
            elif T<T0:
                MagRec["magic_method_codes"]='LP-MC-I'
                T0=T 
            else:
                print 'skipping repeated temperature step'
                MagRec["magic_method_codes"]=''
            T0=T 
            MagRec["measurement_magnitude"]='%10.3e'% (float(rec[1])) # uncalibrated magnitude
            if syn==0:
                MagRec["er_specimen_name"]=specimen_name
                MagRec["er_site_name"]=""
                if specnum!=0:
                    MagRec["er_sample_name"]=specimen_name[:specnum]
                else:
                    MagRec["er_sample_name"]=specimen_name
                if "-fsa" in args:
                    for samp in Samps:
                        if samp["er_sample_name"] == MagRec["er_sample_name"]:
                            MagRec["er_location_name"]=samp["er_location_name"]
                            MagRec["er_site_name"]=samp["er_site_name"]
                            break
                elif int(samp_con)!=6:
                    site=pmag.parse_site(MagRec['er_sample_name'],samp_con,Z)
                    MagRec["er_site_name"]=site
                if MagRec['er_site_name']=="":
                    print 'No site name found for: ',MagRec['er_specimen_name'],MagRec['er_sample_name']
                if MagRec["er_location_name"]=="":
                    print 'no location name for: ',MagRec["er_specimen_name"] 
            else:
                MagRec["er_synthetic_name"]=specimen_name
                MagRec["er_location_name"]=""
                MagRec["er_sample_name"]=""
                MagRec["er_site_name"]=""
                MagRec["er_specimen_name"]=""
            MagRec["magic_instrument_codes"]=instcode
            MagRec["er_analyst_mail_names"]=user
            MagRec["er_citation_names"]=citation
            MagRec["measurement_flag"]='g'
            MagRec["measurement_number"]=str(measnum)
            measnum+=1
            MagRecs.append(MagRec) 
    for rec in MagRecs: # sort out the measurements by experiment type
        rec['magic_experiment_name']=specimen_name
        if rec['magic_method_codes']=='LP-MW-I':
            rec["magic_experiment_name"]=specimen_name+':LP-MW-I:Curie'
        elif rec['magic_method_codes']=='LP-MC-I':
            rec["magic_experiment_name"]=specimen_name+':LP-MC-I'
    pmag.magic_write(meas_file,MagRecs,'magic_measurements')
    print "results put in ",meas_file
Example #22
0
def main(command_line=True, **kwargs):
    """
    NAME
        IODP_jr6_magic.py
 
    DESCRIPTION
        converts shipboard .jr6 format files to magic_measurements format files

    SYNTAX
        IODP_jr6_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -f FILE: specify  input file, or
        -F FILE: specify output file, default is magic_measurements.txt
        -fsa FILE: specify  er_samples.txt file for sample name lookup ,
           default is 'er_samples.txt'
        -loc HOLE : specify hole name (U1456A)
        -A: don't average replicate measurements
 
    INPUT
        JR6 .jr6 format file
    """

    def fix_separation(filename, new_filename):
        old_file = open(filename, "rU")
        data = old_file.readlines()
        new_data = []
        for line in data:
            new_line = line.replace("-", " -")
            new_line = new_line.replace("  ", " ")
            new_data.append(new_line)
        new_file = open(new_filename, "w")
        for s in new_data:
            new_file.write(s)
        old_file.close()
        new_file.close()
        return new_filename

    def old_fix_separation(filename, new_filename):
        old_file = open(filename, "rU")
        data = old_file.readlines()
        new_data = []
        for line in data:
            new_line = []
            for i in line.split():
                if "-" in i[1:]:
                    lead_char = "-" if i[0] == "-" else ""
                    if lead_char:
                        v = i[1:].split("-")
                    else:
                        v = i.split("-")
                    new_line.append(lead_char + v[0])
                    new_line.append("-" + v[1])
                else:
                    new_line.append(i)
            new_line = (" ".join(new_line)) + "\n"
            new_data.append(new_line)
        new_file = open(new_filename, "w")
        for s in new_data:
            new_file.write(s)
        new_file.close()
        old_file.close()
        return new_filename

    # initialize some stuff
    noave = 0
    volume = 2.5 ** 3  # default volume is a 2.5cm cube
    inst = ""
    samp_con, Z = "5", ""
    missing = 1
    demag = "N"
    er_location_name = "unknown"
    citation = "This study"
    args = sys.argv
    meth_code = "LP-NO"
    version_num = pmag.get_version()
    dir_path = "."
    MagRecs = []
    samp_file = "er_samples.txt"
    meas_file = "magic_measurements.txt"
    mag_file = ""
    #
    # get command line arguments
    #
    if command_line:
        if "-WD" in sys.argv:
            ind = sys.argv.index("-WD")
            dir_path = sys.argv[ind + 1]
        if "-ID" in sys.argv:
            ind = sys.argv.index("-ID")
            input_dir_path = sys.argv[ind + 1]
        else:
            input_dir_path = dir_path
        output_dir_path = dir_path
        if "-h" in args:
            print main.__doc__
            return False
        if "-F" in args:
            ind = args.index("-F")
            meas_file = args[ind + 1]
        if "-fsa" in args:
            ind = args.index("-fsa")
            samp_file = args[ind + 1]
            if samp_file[0] != "/":
                samp_file = os.path.join(input_dir_path, samp_file)
            try:
                open(samp_file, "rU")
                ErSamps, file_type = pmag.magic_read(samp_file)
            except:
                print samp_file, " not found: "
                print "   download csv file and import to MagIC with IODP_samples_magic.py"
        if "-f" in args:
            ind = args.index("-f")
            mag_file = args[ind + 1]
        if "-loc" in args:
            ind = args.index("-loc")
            er_location_name = args[ind + 1]
        if "-A" in args:
            noave = 1
    if not command_line:
        dir_path = kwargs.get("dir_path", ".")
        input_dir_path = kwargs.get("input_dir_path", dir_path)
        output_dir_path = dir_path
        meas_file = kwargs.get("meas_file", "magic_measurements.txt")
        mag_file = kwargs.get("mag_file", "")
        samp_file = kwargs.get("samp_file", "er_samples.txt")
        specnum = kwargs.get("specnum", 1)
        samp_con = kwargs.get("samp_con", "1")
        if len(str(samp_con)) > 1:
            samp_con, Z = samp_con.split("-")
        else:
            Z = ""
        er_location_name = kwargs.get("er_location_name", "")
        noave = kwargs.get("noave", 0)  # default (0) means DO average
        meth_code = kwargs.get("meth_code", "LP-NO")

    # format variables
    meth_code = meth_code + ":FS-C-DRILL-IODP:SP-SS-C:SO-V"
    meth_code = meth_code.strip(":")
    if mag_file:
        mag_file = os.path.join(input_dir_path, mag_file)
    samp_file = os.path.join(input_dir_path, samp_file)
    meas_file = os.path.join(output_dir_path, meas_file)

    # validate variables
    if not mag_file:
        print "You must provide an IODP_jr6 format file"
        return False, "You must provide an IODP_jr6 format file"
    if not os.path.exists(mag_file):
        print "The input file you provided: {} does not exist.\nMake sure you have specified the correct filename AND correct input directory name.".format(
            mag_file
        )
        return (
            False,
            "The input file you provided: {} does not exist.\nMake sure you have specified the correct filename AND correct input directory name.".format(
                mag_file
            ),
        )
    if not os.path.exists(samp_file):
        print "Your input directory:\n{}\nmust contain an er_samples.txt file, or you must explicitly provide one".format(
            input_dir_path
        )
        return (
            False,
            "Your input directory:\n{}\nmust contain an er_samples.txt file, or you must explicitly provide one".format(
                input_dir_path
            ),
        )

    # parse data
    temp = os.path.join(output_dir_path, "temp.txt")
    fix_separation(mag_file, temp)
    samples, filetype = pmag.magic_read(samp_file)
    lines = open(temp, "rU").readlines()
    os.remove(temp)
    for line in lines:
        MagRec = {}
        line = line.split()
        spec_text_id = line[0].split("_")[1]
        SampRecs = pmag.get_dictitem(samples, "er_sample_alternatives", spec_text_id, "has")
        if len(SampRecs) > 0:  # found one
            MagRec["er_specimen_name"] = SampRecs[0]["er_sample_name"]
            MagRec["er_sample_name"] = MagRec["er_specimen_name"]
            MagRec["er_site_name"] = MagRec["er_specimen_name"]
            MagRec["er_citation_names"] = "This study"
            MagRec["er_location_name"] = er_location_name
            MagRec["magic_software_packages"] = version_num
            MagRec["treatment_temp"] = "%8.3e" % (273)  # room temp in kelvin
            MagRec["measurement_temp"] = "%8.3e" % (273)  # room temp in kelvin
            MagRec["measurement_flag"] = "g"
            MagRec["measurement_standard"] = "u"
            MagRec["measurement_number"] = "1"
            MagRec["treatment_ac_field"] = "0"

            volume = float(SampRecs[0]["sample_volume"])
            x = float(line[4])
            y = float(line[3])
            negz = float(line[2])
            cart = np.array([x, y, -negz]).transpose()
            direction = pmag.cart2dir(cart).transpose()
            expon = float(line[5])
            magn_volume = direction[2] * (10.0 ** expon)
            moment = magn_volume * volume

            MagRec["measurement_magn_moment"] = str(moment)
            MagRec["measurement_magn_volume"] = str(magn_volume)  # str(direction[2] * (10.0 ** expon))
            MagRec["measurement_dec"] = "%7.1f" % (direction[0])
            MagRec["measurement_inc"] = "%7.1f" % (direction[1])

            step = line[1]
            if step == "NRM":
                meas_type = "LT-NO"
            elif step[0:2] == "AD":
                meas_type = "LT-AF-Z"
                treat = float(step[2:])
                MagRec["treatment_ac_field"] = "%8.3e" % (treat * 1e-3)  # convert from mT to tesla
            elif step[0:2] == "TD":
                meas_type = "LT-T-Z"
                treat = float(step[2:])
                MagRec["treatment_temp"] = "%8.3e" % (treat + 273.0)  # temp in kelvin
            elif step[0:3] == "ARM":  #
                meas_type = "LT-AF-I"
                treat = float(row["step"][3:])
                MagRec["treatment_ac_field"] = "%8.3e" % (treat * 1e-3)  # convert from mT to tesla
                MagRec["treatment_dc_field"] = "%8.3e" % (50e-6)  # assume 50uT DC field
                MagRec["measurement_description"] = "Assumed DC field - actual unknown"
            elif step[0:3] == "IRM":  #
                meas_type = "LT-IRM"
                treat = float(step[3:])
                MagRec["treatment_dc_field"] = "%8.3e" % (treat * 1e-3)  # convert from mT to tesla
            else:
                print "unknown treatment type for ", row
                return False, "unknown treatment type for ", row

            MagRec["magic_method_codes"] = meas_type
            MagRecs.append(MagRec.copy())

        else:
            print "sample name not found: ", row["specname"]
    MagOuts = pmag.measurements_methods(MagRecs, noave)
    file_created, error_message = pmag.magic_write(meas_file, MagOuts, "magic_measurements")
    if file_created:
        return True, meas_file
    else:
        return False, "Results not written to file"
Example #23
0
def main():
    """
    NAME
        UCSC_leg_magic.py
 
    DESCRIPTION
        converts UCSC legacy format files to magic_measurements format files

    SYNTAX
        UCSC_leg_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify  input file, or
        -fin INDEX: specify  index file for reading whole directory: default is index.txt
        -F FILE: specify output file, default is magic_measurements.txt
        -Fsa: specify er_samples format file for appending, default is new er_samples.txt
        -spc NUM : specify number of characters to designate a  specimen, default = 1
        -loc LOCNAME : specify location/study name
        -A: don't average replicate measurements
       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] all others you will have to either customize your
                self or e-mail [email protected] for help.
 
    INPUT
        Format of UCSC legacy files:   
         
        Spec Treat CDec CInc GDec GInc SDec SInc Int [optional A95]
          Treat is HX where X is AF field in Oe, TX where X is T in C,  or NRM
        
         Intensity assumed to be total moment in (emu/cc) with a 10cc specimen volume
         CDec:  Declination in specimen coordinate system
         CInc:  Declination in specimen coordinate system
         GDec:  Declination in geographic coordinate system
         GInc:  Declination in geographic coordinate system
         SDec:  Declination in stratigraphic coordinate system
         SInc:  Declination in stratigraphic coordinate system
   index file:  must be formatted:
         FILENAME  SITE
    """
# initialize some stuff
    noave=0
    methcode,inst="",""
    samp_con,Z='4',3
    missing=1
    demag="N"
    er_location_name=""
    citation='This study'
    args=sys.argv
    methcode="LP-NO"
    specnum=-1
    MagRecs=[]
    version_num=pmag.get_version()
    Samps=[] # keeps track of sample orientations
    DIspec=[]
    MagFiles=[]
#
# get command line arguments
#
    user=""
    mag_file=""
    ind_file=""
    dir_path='.'
    ErSamps,ErSites=[],[]
    if '-WD' in sys.argv:
        ind = sys.argv.index('-WD')
        dir_path=sys.argv[ind+1]
    samp_file=dir_path+'/er_samples.txt'
    site_file=dir_path+'/er_sites.txt'
    meas_file=dir_path+"/magic_measurements.txt"
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-usr" in args:
        ind=args.index("-usr")
        user=args[ind+1]
    if '-F' in args:
        ind=args.index("-F")
        meas_file=dir_path+'/'+args[ind+1]
    if '-Fsi' in args:
        ind=args.index("-Fsi")
        site_file=dirpath+'.'+args[ind+1]
    try:
        open(site_file,'rU')
        ErSites,file_type=pmag.magic_read(site_file)
        print 'site information will be appended to ', site_file 
    except:
        print site_file,' not found: site information will be stored in new er_sites.txt file'
        site_file=dir_path+'/er_sites.txt'
    if '-Fsa' in args:
        ind=args.index("-Fsa")
        samp_file=dirpath+'/'+args[ind+1]
    try:
        open(samp_file,'rU')
        ErSamps,file_type=pmag.magic_read(samp_file)
        print 'sample information will be appended to ', samp_file 
    except:
        print samp_file,' not found: sample information will be stored in new er_samples.txt file'
        samp_file=dir_path+'/er_samples.txt'
    if '-f' in args:
        ind=args.index("-f")
        mag_file=args[ind+1]
        site=mag_file.split('.')[0]
        magfile=dir_path+'/'+mag_file
        try:
            input=open(magfile,'rU')
            MagFiles.append([magfile,site])
        except:
            print "bad input file name"
            sys.exit()
    elif '-fin' in args:
        ind=args.index("-fin")
        ind_file=args[ind+1]
        ind_file=dir_path+'/'+ind_file
        try:
            index_file=open(ind_file,'rU')
        except:
            print "bad index file name"
            sys.exit()
    elif '-fin' not in args:
        ind_file=dir_path+'/index.txt'
        try:
            index_file=open(ind_file,'rU')
        except:
            print "bad index file name"
            sys.exit()
    if ind_file!="": 
        Files=index_file.readlines()
        for file in Files:
            rec=file.split()
            MagFiles.append(rec)
    if "-spc" in args:
        ind=args.index("-spc")
        specnum=int(args[ind+1])
        if specnum!=0:specnum=-specnum
    if "-loc" in args:
        ind=args.index("-loc")
        er_location_name=args[ind+1]
    else:
        print "-loc  is required option"
        print main.__doc__
        sys.exit()
    if "-A" in args: noave=1
    Sites=[]
    for file in MagFiles:
        site=file[1] # attach site name either from file name or from index file
        if site not in Sites:
            Sites.append(site)
            ErSiteRec={'er_location_name': er_location_name,'er_site_name':site,'er_citation_names':citation,'site_definition':'s','site_lat':'','site_lon':"",'site_class':"",'site_lithology':"",'site_type':""}
            ErSites.append(ErSiteRec)
        print 'processing file: ',file[0],' for site: ',site
        data=open(dir_path+'/'+file[0],'rU').readlines() # read in data from file
        firstrec=data[0].split()
        if firstrec[0]=='FILE': # this file has a header, must look for start of data
            for k in range(len(data)):
                if data[k][0]=='-': break    
        else:
            k=-1
        while k<len(data)-1:
            k+=1
            line=data[k]
            if len(line)>2: # skip stupid terminal lines
                line=line.replace(' T  ',' T') # make columns consistent
                line=line.replace(' H  ',' H') # make columns consistent
                line=line.replace(' T ',' T') # make columns consistent
                line=line.replace(' H ',' H') # make columns consistent
                rec=line.split()
                if len(rec)<2: break # skip junk
                MagRec={}
                MagRec['er_location_name']=er_location_name
                MagRec['magic_software_packages']=version_num
                MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
                MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
                MagRec["treatment_ac_field"]='0'
                meas_type="LT-NO"
                MagRec["measurement_flag"]='g'
                MagRec["measurement_standard"]='u'
                MagRec["measurement_number"]='1'
                MagRec["er_specimen_name"]=rec[0]
                if specnum!=0:
                    MagRec["er_sample_name"]=rec[0][:specnum]
                else:
                    MagRec["er_sample_name"]=rec[0]
     #           site=pmag.parse_site(MagRec['er_sample_name'],samp_con,Z)
                MagRec["er_site_name"]=site
                MagRec["measurement_magn_moment"]='%10.3e'% (float(rec[8])*1e-4) # # int is in emu/cc; assuming 10cc, this converts to Am^2 
    #
                if samp_file!="" and MagRec["er_sample_name"] not in Samps:        # create er_samples.txt file with these data 
                    cdec,cinc=float(rec[2]),float(rec[3])
                    gdec,ginc=float(rec[4]),float(rec[5])
                    az,pl=pmag.get_azpl(cdec,cinc,gdec,ginc)
                    bdec,binc=float(rec[6]),float(rec[7])
                    if rec[4]!=rec[6] and rec[5]!=rec[7]:
                        dipdir,dip=pmag.get_tilt(gdec,ginc,bdec,binc)
                    else:
                        dipdir,dip=0,0
                    ErSampRec={}
                    ErSampRec['er_citation_names']='This study'
                    ErSampRec['er_location_name']=MagRec['er_location_name']
                    ErSampRec['er_site_name']=MagRec['er_site_name']
                    ErSampRec['er_sample_name']=MagRec['er_sample_name']
                    ErSampRec['sample_azimuth']='%7.1f'%(az)
                    ErSampRec['sample_dip']='%7.1f'%(pl)
                    ErSampRec['sample_bed_dip_direction']='%7.1f'%(dipdir)
                    ErSampRec['sample_bed_dip']='%7.1f'%(dip)
                    ErSampRec['sample_description']='az,pl,dip_dir and dip recalculated from [c,g,b][dec,inc] in UCSC legacy file'
                    ErSampRec['magic_method_codes']='SO-NO'
                    ErSamps.append(ErSampRec)
                    Samps.append(ErSampRec['er_sample_name'])
                MagRec["measurement_dec"]=rec[2]
                MagRec["measurement_inc"]=rec[3]
                MagRec["er_analyst_mail_names"]=""
                MagRec["er_citation_names"]="This study"
                demag=rec[1][0]
                if demag!='N':
                    treat=float(rec[1][1:])
                else:
                    treat=0
                if demag=="H":
                    MagRec["treatment_ac_field"]='%8.3e' %(treat*1e-4) # convert from oe to tesla
                    meas_type="LT-AF-Z"
                elif demag=="T":
                    MagRec["treatment_temp"]='%8.3e' % (treat+273.) # temp in kelvin
                    meas_type="LT-T-Z"
                MagRec['magic_method_codes']=meas_type
                MagRecs.append(MagRec) 
    MagOuts=pmag.measurements_methods(MagRecs,noave)
    pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    print "results put in ",meas_file
    pmag.magic_write(samp_file,ErSamps,'er_samples')
    print "sample orientations put in ",samp_file
    pmag.magic_write(site_file,ErSites,'er_sites')
    print "site names put in ",site_file
Example #24
0
def main():
    """
    NAME
        sio_magic.py
 
    DESCRIPTION
        converts SIO .mag format files to magic_measurements format files

    SYNTAX
        sio_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify .mag format input file, required
        -fsa SAMPFILE : specify er_samples.txt file relating samples, site and locations names,default is none
        -F FILE: specify output file, default is magic_measurements.txt
        -Fsy: specify er_synthetics file, default is er_sythetics.txt
        -Fsa: specify output er_samples file, default is NONE (only for LDGO formatted files)
        -LP [colon delimited list of protocols, include all that apply]
            AF:  af demag
            T: thermal including thellier but not trm acquisition
            S: Shaw method
            I: IRM (acquisition)
            I3d: 3D IRM experiment
            N: NRM only
            TRM: trm acquisition
            ANI: anisotropy experiment
            D: double AF demag
            G: triple AF demag (GRM protocol)
            CR: cooling rate experiment.
                The treatment coding of the measurement file should be: XXX.00,XXX.10, XXX.20 ...XX.70 etc. (XXX.00 is optional)
                where XXX in the temperature and .10,.20... are running numbers of the cooling rates steps.
                XXX.00 is optional zerofield baseline. XXX.70 is alteration check.
                syntax in sio_magic is: -LP CR xxx,yyy,zzz,.....xx -A
                where xx, yyy,zzz...xxx  are cooling time in [K/minutes], seperated by comma, ordered at the same order as XXX.10,XXX.20 ...XX.70
                if you use a zerofield step then no need to specify the cooling rate for the zerofield
                It is important to add to the command line the -A option so the measurements will not be evraged.
                But users need to make sure that there are no duplicate meaurements in the file
        -V [1,2,3] units of IRM field in volts using ASC coil #1,2 or 3
        -spc NUM : specify number of characters to designate a  specimen, default = 0
        -loc LOCNAME : specify location/study name, must have either LOCNAME or SAMPFILE or be a synthetic
        -syn INST TYPE:  sets these specimens as synthetics created at institution INST and of type TYPE
        -ins INST : specify which demag instrument was used (e.g, SIO-Suzy or SIO-Odette),default is ""
        -dc B PHI THETA: dc lab field (in micro tesla) and phi,theta, default is none
              NB: use PHI, THETA = -1 -1 to signal that it changes, i.e. in anisotropy experiment
        -ac B : peak AF field (in mT) for ARM acquisition, default is none
        -ncn NCON:  specify naming convention: default is #1 below
        -A: don't average replicate measurements
       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] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name same as sample
            [6] site is entered under a separate column
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.
 
            [8] synthetic - has no site name
            [9] ODP naming convention 
    INPUT
        Best to put separate experiments (all AF, thermal, thellier, trm aquisition, Shaw, etc.) in 
           seperate .mag files (eg. af.mag, thermal.mag, etc.)

        Format of SIO .mag files:   
        Spec Treat CSD Intensity Declination Inclination [optional metadata string]
        
        
        Spec: specimen name
        Treat:  treatment step
            XXX T in Centigrade
            XXX AF in mT
            for special experiments:
              Thellier:
                XXX.0  first zero field step
                XXX.1  first in field step [XXX.0 and XXX.1 can be done in any order]
                XXX.2  second in-field step at lower temperature (pTRM check)
                XXX.3  second zero-field step after infield (pTRM check step)
                       XXX.3 MUST be done in this order [XXX.0, XXX.1 [optional XXX.2] XXX.3]
              AARM:
                X.00  baseline step (AF in zero bias field - high peak field)
                X.1   ARM step (in field step)  where
                   X is the step number in the 15 position scheme 
                      (see Appendix to Lecture 13 - http://magician.ucsd.edu/Essentials_2)
              ATRM:
                X.00 optional baseline
                X.1 ATRM step (+X)
                X.2 ATRM step (+Y)
                X.3 ATRM step (+Z)
                X.4 ATRM step (-X)
                X.5 ATRM step (-Y)
                X.6 ATRM step (-Z)
                X.7 optional alteration check (+X)

              TRM:
                XXX.YYY  XXX is temperature step of total TRM
                         YYY is dc field in microtesla

         
         Intensity assumed to be total moment in 10^3 Am^2 (emu)
         Declination:  Declination in specimen coordinate system
         Inclination:  Declination in specimen coordinate system

         Optional metatdata string:  mm/dd/yy;hh:mm;[dC,mT];xx.xx;UNITS;USER;INST;NMEAS
             hh in 24 hours.  
             dC or mT units of treatment XXX (see Treat above) for thermal or AF respectively
             xx.xxx   DC field
             UNITS of DC field (microT, mT)
             INST:  instrument code, number of axes, number of positions (e.g., G34 is 2G, three axes, 
                    measured in four positions)
             NMEAS: number of measurements in a single position (1,3,200...)
       
     
    """
# initialize some stuff
    infile_type="mag"
    noave=0
    methcode,inst="LP-NO",""
    phi,theta,peakfield,labfield=0,0,0,0
    pTRM,MD,samp_con,Z=0,0,'1',1
    dec=[315,225,180,135,45,90,270,270,270,90,180,180,0,0,0]
    inc=[0,0,0,0,0,-45,-45,0,45,45,45,-45,-90,-45,45]
    tdec=[0,90,0,180,270,0,0,90,0]
    tinc=[0,0,90,0,0,-90,0,0,90]
    missing=1
    demag="N"
    er_location_name=""
    citation='This study'
    args=sys.argv
    fmt='old'
    syn=0
    synfile='er_synthetics.txt'
    samp_file,ErSamps='',[]
    trm=0
    irm=0
    specnum=0
    coil=""
#
# get command line arguments
#
    meas_file="magic_measurements.txt"
    user=""
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-usr" in args:
        ind=args.index("-usr")
        user=args[ind+1]
    if '-F' in args:
        ind=args.index("-F")
        meas_file=args[ind+1]
    if '-Fsy' in args:
        ind=args.index("-Fsy")
        synfile=args[ind+1]
    if '-Fsa' in args:
        ind=args.index("-Fsa")
        samp_file=args[ind+1]
        try:
            open(samp_file,'rU')
            ErSamps,file_type=pmag.magic_read(samp_file)
            print 'sample information will be appended to new er_samples.txt file'
        except:
            print 'sample information will be stored in new er_samples.txt file'
    if '-f' in args:
        ind=args.index("-f")
        magfile=args[ind+1]
        try:
            input=open(magfile,'rU')
        except:
            print "bad mag file name"
            sys.exit()
    else: 
        print "mag_file field is required option"
        print main.__doc__
        sys.exit()
    if "-dc" in args:
        ind=args.index("-dc")
        labfield=float(args[ind+1])*1e-6
        phi=float(args[ind+2])
        theta=float(args[ind+3])
    if "-ac" in args:
        ind=args.index("-ac")
        peakfield=float(args[ind+1])*1e-3
    if "-spc" in args:
        ind=args.index("-spc")
        specnum=int(args[ind+1])
        if specnum!=0:specnum=-specnum
    if "-loc" in args:
        ind=args.index("-loc")
        er_location_name=args[ind+1]
    if "-fsa" in args:
        ind=args.index("-fsa")
        Samps,file_type=pmag.magic_read(args[ind+1])
    if '-syn' in args:
        syn=1
        ind=args.index("-syn")
        institution=args[ind+1]
        syntype=args[ind+2]
        if '-fsy' in args:
            ind=args.index("-fsy")
            synfile=args[ind+1]
    if "-ins" in args:
        ind=args.index("-ins")
        inst=args[ind+1]
    if "-A" in args: noave=1
    if "-ncn" in args:
        ind=args.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 "7-" in samp_con:
            if "-" not in samp_con:
                print "option [7] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="7"
    if '-LP' in args:
        ind=args.index("-LP")
        codelist=args[ind+1]
        codes=codelist.split(':')
        if "AF" in codes:
            demag='AF' 
            if'-dc' not in args: methcode="LT-AF-Z"
            if'-dc' in args: methcode="LT-AF-I"
        if "T" in codes:
            demag="T"
            if '-dc' not in args: methcode="LT-T-Z"
            if '-dc' in args: methcode="LT-T-I"
        if "I" in codes:
            methcode="LP-IRM"
            irmunits="mT"
        if "I3d" in codes:
            methcode="LT-T-Z:LP-IRM-3D"
        if "S" in codes: 
            demag="S"
            methcode="LP-PI-TRM:LP-PI-ALT-AFARM"
            trm_labfield=labfield
            ans=raw_input("DC lab field for ARM step: [50uT] ")
            if ans=="":
                arm_labfield=50e-6
            else: 
                arm_labfield=float(ans)*1e-6
            ans=raw_input("temperature for total trm step: [600 C] ")
            if ans=="":
                trm_peakT=600+273 # convert to kelvin
            else: 
                trm_peakT=float(ans)+273 # convert to kelvin
        if "G" in codes: methcode="LT-AF-G"
	if "D" in codes: methcode="LT-AF-D"
        if "TRM" in codes: 
            demag="T"
            trm=1
        if "CR" in codes: 
            demag="T"
            cooling_rate_experiment=1
            ind=args.index("CR")
            coolling_times=args[ind+1]
            coolling_times_list=coolling_times.split(',')
            
    if "-V" in args:
        methcode="LP-IRM"
        ind=args.index("-V")
        irmunits="V"
        coil=args[ind+1]
        if coil not in ["1","2","3"]:
            print main.__doc__
            print 'not a valid coil specification'
            sys.exit()
    if demag=="T" and "ANI" in codes:
        methcode="LP-AN-TRM"
    if demag=="T" and "CR" in codes:
        methcode="LP-CR-TRM"
    if demag=="AF" and "ANI" in codes:
        methcode="LP-AN-ARM"
        if labfield==0: labfield=50e-6
        if peakfield==0: peakfield=.180
    SynRecs,MagRecs=[],[]
    version_num=pmag.get_version()
    if 1:
    #if infile_type=="SIO format":
        for line in input.readlines():
            instcode=""
            if len(line)>2:
                SynRec={}
                MagRec={}
                MagRec['er_location_name']=er_location_name
                MagRec['magic_software_packages']=version_num
                MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
                MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
                MagRec["treatment_ac_field"]='0'
                MagRec["treatment_dc_field"]='0'
                MagRec["treatment_dc_field_phi"]='0'
                MagRec["treatment_dc_field_theta"]='0'
                meas_type="LT-NO"
                rec=line.split()
                if rec[1]==".00":rec[1]="0.00"
                treat=rec[1].split('.')
                if methcode=="LP-IRM":
                    if irmunits=='mT':
                        labfield=float(treat[0])*1e-3
                    else:
                        labfield=pmag.getfield(irmunits,coil,treat[0])
                    if rec[1][0]!="-":
                        phi,theta=0.,90.
                    else:
                        phi,theta=0.,-90.
                    meas_type="LT-IRM"
                    MagRec["treatment_dc_field"]='%8.3e'%(labfield)
                    MagRec["treatment_dc_field_phi"]='%7.1f'%(phi)
                    MagRec["treatment_dc_field_theta"]='%7.1f'%(theta)
                if len(rec)>6:
                  code1=rec[6].split(';') # break e.g., 10/15/02;7:45 indo date and time
                  if len(code1)==2: # old format with AM/PM
                    missing=0
                    code2=code1[0].split('/') # break date into mon/day/year
                    code3=rec[7].split(';') # break e.g., AM;C34;200  into time;instr/axes/measuring pos;number of measurements
                    yy=int(code2[2])
                    if yy <90:
                        yyyy=str(2000+yy)
                    else: yyyy=str(1900+yy)
                    mm=int(code2[0])
                    if mm<10:
                        mm="0"+str(mm)
                    else: mm=str(mm)
                    dd=int(code2[1])
                    if dd<10:
                        dd="0"+str(dd)
                    else: dd=str(dd)
                    time=code1[1].split(':')
                    hh=int(time[0])
                    if code3[0]=="PM":hh=hh+12
                    if hh<10:
                        hh="0"+str(hh)
                    else: hh=str(hh)
                    min=int(time[1])
                    if min<10:
                       min= "0"+str(min)
                    else: min=str(min)
                    MagRec["measurement_date"]=yyyy+":"+mm+":"+dd+":"+hh+":"+min+":00.00"
                    MagRec["measurement_time_zone"]='SAN'
                    if inst=="":
                        if code3[1][0]=='C':instcode='SIO-bubba'
                        if code3[1][0]=='G':instcode='SIO-flo'
                    else:
                        instcode=''
                    MagRec["measurement_positions"]=code3[1][2]
                  elif len(code1)>2: # newest format (cryo7 or later)
                    if "LP-AN-ARM" not in methcode:labfield=0
                    fmt='new'
                    date=code1[0].split('/') # break date into mon/day/year
                    yy=int(date[2])
                    if yy <90:
                        yyyy=str(2000+yy)
                    else: yyyy=str(1900+yy)
                    mm=int(date[0])
                    if mm<10:
                        mm="0"+str(mm)
                    else: mm=str(mm)
                    dd=int(date[1])
                    if dd<10:
                        dd="0"+str(dd)
                    else: dd=str(dd)
                    time=code1[1].split(':')
                    hh=int(time[0])
                    if hh<10:
                        hh="0"+str(hh)
                    else: hh=str(hh)
                    min=int(time[1])
                    if min<10:
                       min= "0"+str(min)
                    else: min=str(min)
                    MagRec["measurement_date"]=yyyy+":"+mm+":"+dd+":"+hh+":"+min+":00.00"
                    MagRec["measurement_time_zone"]='SAN'
                    if inst=="":
                        if code1[6][0]=='C':instcode='SIO-bubba'
                        if code1[6][0]=='G':instcode='SIO-flo'
                    else:
                        instcode=''
                    if len(code1)>1:
                        MagRec["measurement_positions"]=code1[6][2]
                    else:
                        MagRec["measurement_positions"]=code1[7]   # takes care of awkward format with bubba and flo being different
                    if user=="":user=code1[5]
                    if code1[2][-1]=='C': 
                        demag="T"
                        if code1[4]=='microT' and float(code1[3])!=0. and "LP-AN-ARM" not in methcode: labfield=float(code1[3])*1e-6
                    if code1[2]=='mT' and methcode!="LP-IRM": 
                        demag="AF"
                        if code1[4]=='microT' and float(code1[3])!=0.: labfield=float(code1[3])*1e-6
                    if code1[4]=='microT' and labfield!=0. and meas_type!="LT-IRM":
                        phi,theta=0.,-90.
                        if demag=="T": meas_type="LT-T-I"
                        if demag=="AF": meas_type="LT-AF-I"
                        MagRec["treatment_dc_field"]='%8.3e'%(labfield)
                        MagRec["treatment_dc_field_phi"]='%7.1f'%(phi)
                        MagRec["treatment_dc_field_theta"]='%7.1f'%(theta)
                    if code1[4]=='' or labfield==0. and meas_type!="LT-IRM":
                        if demag=='T':meas_type="LT-T-Z"
                        if demag=="AF":meas_type="LT-AF-Z"
                        MagRec["treatment_dc_field"]='0'
                if syn==0:
                    MagRec["er_specimen_name"]=rec[0]
                    MagRec["er_synthetic_name"]=""
                    MagRec["er_site_name"]=""
                    if specnum!=0:
                        MagRec["er_sample_name"]=rec[0][:specnum]
                    else:
                        MagRec["er_sample_name"]=rec[0]
                    if "-fsa" in args:
                        samp=pmag.get_dictitem(Samps,'er_sample_name',MagRec['er_sample_name'],'T')
                        if len(samp)>0:
                            MagRec["er_location_name"]=samp[0]["er_location_name"]
                            MagRec["er_site_name"]=samp[0]["er_site_name"]
                        else: 
                            MagRec['er_location_name']=''
                            MagRec["er_site_name"]=''
                    elif int(samp_con)!=6:
                        site=pmag.parse_site(MagRec['er_sample_name'],samp_con,Z)
                        MagRec["er_site_name"]=site
                    if MagRec['er_site_name']=="":
                        print 'No site name found for: ',MagRec['er_specimen_name'],MagRec['er_sample_name']
                    if MagRec["er_location_name"]=="":
                        print 'no location name for: ',MagRec["er_specimen_name"] 
                else:
                    if specnum!=0:
                        MagRec["er_sample_name"]=rec[0][:specnum]
                    else:
                        MagRec["er_sample_name"]=rec[0]
                    MagRec["er_site_name"]=""
                    MagRec["er_synthetic_name"]=MagRec["er_specimen_name"]
                    SynRec["er_synthetic_name"]=MagRec["er_specimen_name"]
                    site=pmag.parse_site(MagRec['er_sample_name'],samp_con,Z)
                    SynRec["synthetic_parent_sample"]=site
                    SynRec["er_citation_names"]="This study"
                    SynRec["synthetic_institution"]=institution
                    SynRec["synthetic_type"]=syntype
                    SynRecs.append(SynRec)
                if float(rec[1])==0:
                    pass 
                elif demag=="AF":
                    if methcode != "LP-AN-ARM":
                        MagRec["treatment_ac_field"]='%8.3e' %(float(rec[1])*1e-3) # peak field in tesla
                        if meas_type=="LT-AF-Z": MagRec["treatment_dc_field"]='0'
                    else: # AARM experiment
                        if treat[1][0]=='0':
                            meas_type="LT-AF-Z:LP-AN-ARM:"
                            MagRec["treatment_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
                            MagRec["treatment_dc_field"]='%8.3e'%(0)
                            if labfield!=0 and methcode!="LP-AN-ARM": print "Warning - inconsistency in mag file with lab field - overriding file with 0"
                        else:
                            meas_type="LT-AF-I:LP-AN-ARM"
                            ipos=int(treat[0])-1
                            MagRec["treatment_dc_field_phi"]='%7.1f' %(dec[ipos])
                            MagRec["treatment_dc_field_theta"]='%7.1f'% (inc[ipos])
                            MagRec["treatment_dc_field"]='%8.3e'%(labfield)
                            MagRec["treatment_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
                elif demag=="T" and methcode == "LP-AN-TRM":
                    MagRec["treatment_temp"]='%8.3e' % (float(treat[0])+273.) # temp in kelvin
                    if treat[1][0]=='0':
                        meas_type="LT-T-Z:LP-AN-TRM"
                        MagRec["treatment_dc_field"]='%8.3e'%(0)
                        MagRec["treatment_dc_field_phi"]='0'
                        MagRec["treatment_dc_field_theta"]='0'
                    else:
                        MagRec["treatment_dc_field"]='%8.3e'%(labfield)
                        if treat[1][0]=='7': # alteration check as final measurement
                                meas_type="LT-PTRM-I:LP-AN-TRM"
                        else:
                                meas_type="LT-T-I:LP-AN-TRM"

                        # find the direction of the lab field in two ways:
                        # (1) using the treatment coding (XX.1=+x, XX.2=+y, XX.3=+z, XX.4=-x, XX.5=-y, XX.6=-z)
                        ipos_code=int(treat[1][0])-1
                        # (2) using the magnetization
                        DEC=float(rec[4])
                        INC=float(rec[5])
                        if INC < 45 and INC > -45:
                            if DEC>315  or DEC<45: ipos_guess=0
                            if DEC>45 and DEC<135: ipos_guess=1
                            if DEC>135 and DEC<225: ipos_guess=3
                            if DEC>225 and DEC<315: ipos_guess=4
                        else:
                            if INC >45: ipos_guess=2
                            if INC <-45: ipos_guess=5
                        # prefer the guess over the code
                        ipos=ipos_guess
                        MagRec["treatment_dc_field_phi"]='%7.1f' %(tdec[ipos])
                        MagRec["treatment_dc_field_theta"]='%7.1f'% (tinc[ipos])
                        # check it 
                        if ipos_guess!=ipos_code and treat[1][0]!='7':
                            print "-E- ERROR: check specimen %s step %s, ATRM measurements, coding does not match the direction of the lab field!"%(rec[0],".".join(list(treat)))
                        

                elif demag=="S": # Shaw experiment
                    if treat[1][1]=='0':
                        if  int(treat[0])!=0:
                            MagRec["treatment_ac_field"]='%8.3e' % (float(treat[0])*1e-3) # AF field in tesla
                            MagRec["treatment_dc_field"]='0'
                            meas_type="LT-AF-Z" # first AF
                        else:
                            meas_type="LT-NO"
                            MagRec["treatment_ac_field"]='0'
                            MagRec["treatment_dc_field"]='0'
                    elif treat[1][1]=='1':
                        if int(treat[0])==0:
                            MagRec["treatment_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
                            MagRec["treatment_dc_field"]='%8.3e'%(arm_labfield)
                            MagRec["treatment_dc_field_phi"]='%7.1f'%(phi)
                            MagRec["treatment_dc_field_theta"]='%7.1f'%(theta)
                            meas_type="LT-AF-I"
                        else:
                            MagRec["treatment_ac_field"]='%8.3e' % ( float(treat[0])*1e-3) # AF field in tesla
                            MagRec["treatment_dc_field"]='0'
                            meas_type="LT-AF-Z"
                    elif treat[1][1]=='2':
                        if int(treat[0])==0:
                            MagRec["treatment_ac_field"]='0'
                            MagRec["treatment_dc_field"]='%8.3e'%(trm_labfield)
                            MagRec["treatment_dc_field_phi"]='%7.1f'%(phi)
                            MagRec["treatment_dc_field_theta"]='%7.1f'%(theta)
                            MagRec["treatment_temp"]='%8.3e' % (trm_peakT)
                            meas_type="LT-T-I"
                        else:
                            MagRec["treatment_ac_field"]='%8.3e' % ( float(treat[0])*1e-3) # AF field in tesla
                            MagRec["treatment_dc_field"]='0'
                            meas_type="LT-AF-Z"
                    elif treat[1][1]=='3':
                        if int(treat[0])==0:
                            MagRec["treatment_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
                            MagRec["treatment_dc_field"]='%8.3e'%(arm_labfield)
                            MagRec["treatment_dc_field_phi"]='%7.1f'%(phi)
                            MagRec["treatment_dc_field_theta"]='%7.1f'%(theta)
                            meas_type="LT-AF-I"
                        else:
                            MagRec["treatment_ac_field"]='%8.3e' % ( float(treat[0])*1e-3) # AF field in tesla
                            MagRec["treatment_dc_field"]='0'
                            meas_type="LT-AF-Z"


                # Cooling rate experient # added by rshaar
                elif demag=="T" and methcode == "LP-CR-TRM":

                    MagRec["treatment_temp"]='%8.3e' % (float(treat[0])+273.) # temp in kelvin
                    if treat[1][0]=='0':
                        meas_type="LT-T-Z:LP-CR-TRM"
                        MagRec["treatment_dc_field"]='%8.3e'%(0)
                        MagRec["treatment_dc_field_phi"]='0'
                        MagRec["treatment_dc_field_theta"]='0'
                    else:
                        MagRec["treatment_dc_field"]='%8.3e'%(labfield)
                        if treat[1][0]=='7': # alteration check as final measurement
                                meas_type="LT-PTRM-I:LP-CR-TRM"
                        else:
                                meas_type="LT-T-I:LP-CR-TRM"
                        MagRec["treatment_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                        MagRec["treatment_dc_field_theta"]='%7.1f' % (theta) # labfield theta
                        
                        indx=int(treat[1][0])-1
                        # alteration check matjed as 0.7 in the measurement file
                        if indx==6:
                           cooling_time= coolling_times_list[-1]
                        else:
                            cooling_time=coolling_times_list[indx]
                        MagRec["measurement_description"]="cooling_rate"+":"+cooling_time+":"+"K/min"


                elif demag!='N':  
                  if len(treat)==1:treat.append('0')
                  MagRec["treatment_temp"]='%8.3e' % (float(treat[0])+273.) # temp in kelvin
                  if trm==0:  # demag=T and not trmaq
                    if treat[1][0]=='0':
                        meas_type="LT-T-Z"
                    else: 
                        MagRec["treatment_dc_field"]='%8.3e' % (labfield) # labfield in tesla (convert from microT)
                        MagRec["treatment_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                        MagRec["treatment_dc_field_theta"]='%7.1f' % (theta) # labfield theta
                        if treat[1][0]=='1':meas_type="LT-T-I" # in-field thermal step
                        if treat[1][0]=='2':
                            meas_type="LT-PTRM-I" # pTRM check
                            pTRM=1
                        if treat[1][0]=='3':
                            MagRec["treatment_dc_field"]='0'  # this is a zero field step
                            meas_type="LT-PTRM-MD" # pTRM tail check
                  else: 
                    labfield=float(treat[1])*1e-6
                    MagRec["treatment_dc_field"]='%8.3e' % (labfield) # labfield in tesla (convert from microT)
                    MagRec["treatment_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                    MagRec["treatment_dc_field_theta"]='%7.1f' % (theta) # labfield theta
                    meas_type="LT-T-I:LP-TRM" # trm acquisition experiment

                            

                MagRec["measurement_csd"]=rec[2]
                MagRec["measurement_magn_moment"]='%10.3e'% (float(rec[3])*1e-3) # moment in Am^2 (from emu)
                MagRec["measurement_dec"]=rec[4]
                MagRec["measurement_inc"]=rec[5]
                MagRec["magic_instrument_codes"]=instcode
                MagRec["er_analyst_mail_names"]=user
                MagRec["er_citation_names"]=citation
                if "LP-IRM-3D" in methcode : meas_type=methcode
                #MagRec["magic_method_codes"]=methcode.strip(':')
                MagRec["magic_method_codes"]=meas_type
                MagRec["measurement_flag"]='g'
                MagRec["er_specimen_name"]=rec[0]
                if 'std' in rec[0]:
                    MagRec["measurement_standard"]='s'
                else:
                    MagRec["measurement_standard"]='u'
                MagRec["measurement_number"]='1'
                #print MagRec['treatment_temp']
                MagRecs.append(MagRec) 
    MagOuts=pmag.measurements_methods(MagRecs,noave)
    pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    print "results put in ",meas_file
    if samp_file!="":
        pmag.magic_write(samp_file,ErSamps,'er_samples')
        print "sample orientations put in ",samp_file
    if len(SynRecs)>0:
        pmag.magic_write(synfile,SynRecs,'er_synthetics')
        print "synthetics put in ",synfile
Example #25
0
def main():
    """
    NAME
        thellier_magic_redo.py

    DESCRIPTION
        Calculates paleointensity parameters for thellier-thellier type data using bounds
        stored in the "redo" file

    SYNTAX
        thellier_magic_redo [command line options]

    OPTIONS
        -h prints help message
        -usr USER:   identify user, default is ""
        -fcr CRIT, set criteria for grading
        -f IN: specify input file, default is magic_measurements.txt
        -fre REDO: specify redo file, default is "thellier_redo"
        -F OUT: specify output file, default is thellier_specimens.txt
        -leg:  attaches "Recalculated from original measurements; supercedes published results. " to comment field
        -CR PERC TYPE: apply a blanket cooling rate correction if none supplied in the er_samples.txt file 
            PERC should be a percentage of original (say reduce to 90%)
            TYPE should be one of the following:
               EG (for educated guess); PS (based on pilots); TRM (based on comparison of two TRMs) 
        -ANI:  perform anisotropy correction
        -fsa SAMPFILE: er_samples.txt file with cooling rate correction information, default is NO CORRECTION
        -Fcr  CRout: specify pmag_specimen format file for cooling rate corrected data
        -fan ANIFILE: specify rmag_anisotropy format file, default is rmag_anisotropy.txt 
        -Fac  ACout: specify pmag_specimen format file for anisotropy corrected data
                 default is AC_specimens.txt
        -fnl NLTFILE: specify magic_measurments format file, default is magic_measurements.txt
        -Fnl NLTout: specify pmag_specimen format file for non-linear trm corrected data
                 default is NLT_specimens.txt
        -z use z component differenences for pTRM calculation

    INPUT
        a thellier_redo file is Specimen_name Tmin Tmax (where Tmin and Tmax are in Centigrade)
    """
    dir_path = "."
    critout = ""
    version_num = pmag.get_version()
    field, first_save = -1, 1
    spec, recnum, start, end = 0, 0, 0, 0
    crfrac = 0
    NltRecs, PmagSpecs, AniSpecRecs, NltSpecRecs, CRSpecs = [], [], [], [], []
    meas_file, pmag_file, mk_file = "magic_measurements.txt", "thellier_specimens.txt", "thellier_redo"
    anis_file = "rmag_anisotropy.txt"
    anisout, nltout = "AC_specimens.txt", "NLT_specimens.txt"
    crout = "CR_specimens.txt"
    nlt_file = ""
    samp_file = ""
    comment, user = "", "unknown"
    anis, nltrm = 0, 0
    jackknife = 0  # maybe in future can do jackknife
    args = sys.argv
    Zdiff = 0
    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 "-leg" in args:
        comment = "Recalculated from original measurements; supercedes published results. "
    cool = 0
    if "-CR" in args:
        cool = 1
        ind = args.index("-CR")
        crfrac = 0.01 * float(sys.argv[ind + 1])
        crtype = "DA-CR-" + sys.argv[ind + 2]
    if "-Fcr" in args:
        ind = args.index("-Fcr")
        crout = sys.argv[ind + 1]
    if "-f" in args:
        ind = args.index("-f")
        meas_file = sys.argv[ind + 1]
    if "-F" in args:
        ind = args.index("-F")
        pmag_file = sys.argv[ind + 1]
    if "-fre" in args:
        ind = args.index("-fre")
        mk_file = args[ind + 1]
    if "-fsa" in args:
        ind = args.index("-fsa")
        samp_file = dir_path + "/" + args[ind + 1]
        Samps, file_type = pmag.magic_read(samp_file)
        SampCRs = pmag.get_dictitem(Samps, "cooling_rate_corr", "", "F")  # get samples cooling rate corrections
        cool = 1
        if file_type != "er_samples":
            print "not a valid er_samples.txt file"
            sys.exit()
    #
    #
    if "-ANI" in args:
        anis = 1
        ind = args.index("-ANI")
        if "-Fac" in args:
            ind = args.index("-Fac")
            anisout = args[ind + 1]
        if "-fan" in args:
            ind = args.index("-fan")
            anis_file = args[ind + 1]
    #
    if "-NLT" in args:
        if "-Fnl" in args:
            ind = args.index("-Fnl")
            nltout = args[ind + 1]
        if "-fnl" in args:
            ind = args.index("-fnl")
            nlt_file = args[ind + 1]
    if "-z" in args:
        Zdiff = 1
    if "-fcr" in sys.argv:
        ind = args.index("-fcr")
        critout = sys.argv[ind + 1]
    #
    #  start reading in data:
    #
    meas_file = dir_path + "/" + meas_file
    mk_file = dir_path + "/" + mk_file
    accept = pmag.default_criteria(1)[0]  # set criteria to none
    if critout != "":
        critout = dir_path + "/" + critout
        crit_data, file_type = pmag.magic_read(critout)
        if file_type != "pmag_criteria":
            print "bad pmag_criteria file, using no acceptance criteria"
        print "Acceptance criteria read in from ", critout
        for critrec in crit_data:
            if "sample_int_sigma_uT" in critrec.keys():  # accommodate Shaar's new criterion
                critrec["sample_int_sigma"] = "%10.3e" % (eval(critrec["sample_int_sigma_uT"]) * 1e-6)
            for key in critrec.keys():
                if key not in accept.keys() and critrec[key] != "":
                    accept[key] = critrec[key]
    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()
    try:
        mk_f = open(mk_file, "rU")
    except:
        print "Bad redo file"
        sys.exit()
    mkspec = []
    speclist = []
    for line in mk_f.readlines():
        tmp = line.split()
        mkspec.append(tmp)
        speclist.append(tmp[0])
    if anis == 1:
        anis_file = dir_path + "/" + anis_file
        anis_data, file_type = pmag.magic_read(anis_file)
        if file_type != "rmag_anisotropy":
            print file_type
            print file_type, "This is not a valid rmag_anisotropy file "
            sys.exit()
    if nlt_file == "":
        nlt_data = pmag.get_dictitem(
            meas_data, "magic_method_codes", "LP-TRM", "has"
        )  # look for trm acquisition data in the meas_data file
    else:
        nlt_file = dir_path + "/" + nlt_file
        nlt_data, file_type = pmag.magic_read(nlt_file)
    if len(nlt_data) > 0:
        nltrm = 1
    #
    # sort the specimen names and step through one by one
    #
    sids = pmag.get_specs(meas_data)
    #
    print "Processing ", len(speclist), " specimens - please wait "
    while spec < len(speclist):
        s = speclist[spec]
        recnum = 0
        datablock = []
        PmagSpecRec = {}
        PmagSpecRec["er_analyst_mail_names"] = user
        PmagSpecRec["er_citation_names"] = "This study"
        PmagSpecRec["magic_software_packages"] = version_num
        methcodes, inst_code = [], ""
        #
        # find the data from the meas_data file for this specimen
        #
        datablock = pmag.get_dictitem(meas_data, "er_specimen_name", s, "T")
        datablock = pmag.get_dictitem(
            datablock, "magic_method_codes", "LP-PI-TRM", "has"
        )  # pick out the thellier experiment data
        if len(datablock) > 0:
            for rec in datablock:
                if "magic_instrument_codes" not in rec.keys():
                    rec["magic_instrument_codes"] = "unknown"
            #
            #  collect info for the PmagSpecRec dictionary
            #
            rec = datablock[0]
            PmagSpecRec["er_specimen_name"] = s
            PmagSpecRec["er_sample_name"] = rec["er_sample_name"]
            PmagSpecRec["er_site_name"] = rec["er_site_name"]
            PmagSpecRec["er_location_name"] = rec["er_location_name"]
            PmagSpecRec["measurement_step_unit"] = "K"
            PmagSpecRec["specimen_correction"] = "u"
            if "er_expedition_name" in rec.keys():
                PmagSpecRec["er_expedition_name"] = rec["er_expedition_name"]
            if "magic_instrument_codes" not in rec.keys():
                PmagSpecRec["magic_instrument_codes"] = "unknown"
            else:
                PmagSpecRec["magic_instrument_codes"] = rec["magic_instrument_codes"]
            if "magic_experiment_name" not in rec.keys():
                rec["magic_experiment_name"] = ""
            else:
                PmagSpecRec["magic_experiment_names"] = rec["magic_experiment_name"]
            meths = rec["magic_experiment_name"].split(":")
            for meth in meths:
                if meth.strip() not in methcodes and "LP-" in meth:
                    methcodes.append(meth.strip())
            #
            # sort out the data into first_Z, first_I, ptrm_check, ptrm_tail
            #
            araiblock, field = pmag.sortarai(datablock, s, Zdiff)
            first_Z = araiblock[0]
            first_I = araiblock[1]
            ptrm_check = araiblock[2]
            ptrm_tail = araiblock[3]
            if len(first_I) < 3 or len(first_Z) < 4:
                spec += 1
                print "skipping specimen ", s
            else:
                #
                # get start, end
                #
                for redospec in mkspec:
                    if redospec[0] == s:
                        b, e = float(redospec[1]), float(redospec[2])
                        break
                if e > float(first_Z[-1][0]):
                    e = float(first_Z[-1][0])
                for recnum in range(len(first_Z)):
                    if first_Z[recnum][0] == b:
                        start = recnum
                    if first_Z[recnum][0] == e:
                        end = recnum
                nsteps = end - start
                if nsteps > 2:
                    zijdblock, units = pmag.find_dmag_rec(s, meas_data)
                    pars, errcode = pmag.PintPars(datablock, araiblock, zijdblock, start, end, accept)
                    if "specimen_scat" in pars.keys():
                        PmagSpecRec["specimen_scat"] = pars["specimen_scat"]
                    if "specimen_frac" in pars.keys():
                        PmagSpecRec["specimen_frac"] = "%5.3f" % (pars["specimen_frac"])
                    if "specimen_gmax" in pars.keys():
                        PmagSpecRec["specimen_gmax"] = "%5.3f" % (pars["specimen_gmax"])
                    pars["measurement_step_unit"] = units
                    pars["specimen_lab_field_dc"] = field
                    pars["specimen_int"] = -1 * field * pars["specimen_b"]
                    PmagSpecRec["measurement_step_min"] = "%8.3e" % (pars["measurement_step_min"])
                    PmagSpecRec["measurement_step_max"] = "%8.3e" % (pars["measurement_step_max"])
                    PmagSpecRec["specimen_int_n"] = "%i" % (pars["specimen_int_n"])
                    PmagSpecRec["specimen_lab_field_dc"] = "%8.3e" % (pars["specimen_lab_field_dc"])
                    PmagSpecRec["specimen_int"] = "%9.4e " % (pars["specimen_int"])
                    PmagSpecRec["specimen_b"] = "%5.3f " % (pars["specimen_b"])
                    PmagSpecRec["specimen_q"] = "%5.1f " % (pars["specimen_q"])
                    PmagSpecRec["specimen_f"] = "%5.3f " % (pars["specimen_f"])
                    PmagSpecRec["specimen_fvds"] = "%5.3f" % (pars["specimen_fvds"])
                    PmagSpecRec["specimen_b_beta"] = "%5.3f" % (pars["specimen_b_beta"])
                    PmagSpecRec["specimen_int_mad"] = "%7.1f" % (pars["specimen_int_mad"])
                    PmagSpecRec["specimen_Z"] = "%7.1f" % (pars["specimen_Z"])
                    PmagSpecRec["specimen_gamma"] = "%7.1f" % (pars["specimen_gamma"])
                    if pars["method_codes"] != "" and pars["method_codes"] not in methcodes:
                        methcodes.append(pars["method_codes"])
                    PmagSpecRec["specimen_dec"] = "%7.1f" % (pars["specimen_dec"])
                    PmagSpecRec["specimen_inc"] = "%7.1f" % (pars["specimen_inc"])
                    PmagSpecRec["specimen_tilt_correction"] = "-1"
                    PmagSpecRec["specimen_direction_type"] = "l"
                    PmagSpecRec["direction_type"] = "l"  # this is redudant, but helpful - won't be imported
                    PmagSpecRec["specimen_dang"] = "%7.1f " % (pars["specimen_dang"])
                    PmagSpecRec["specimen_drats"] = "%7.1f " % (pars["specimen_drats"])
                    PmagSpecRec["specimen_drat"] = "%7.1f " % (pars["specimen_drat"])
                    PmagSpecRec["specimen_int_ptrm_n"] = "%i " % (pars["specimen_int_ptrm_n"])
                    PmagSpecRec["specimen_rsc"] = "%6.4f " % (pars["specimen_rsc"])
                    PmagSpecRec["specimen_md"] = "%i " % (int(pars["specimen_md"]))
                    if PmagSpecRec["specimen_md"] == "-1":
                        PmagSpecRec["specimen_md"] = ""
                    PmagSpecRec["specimen_b_sigma"] = "%5.3f " % (pars["specimen_b_sigma"])
                    if "IE-TT" not in methcodes:
                        methcodes.append("IE-TT")
                    methods = ""
                    for meth in methcodes:
                        methods = methods + meth + ":"
                    PmagSpecRec["magic_method_codes"] = methods.strip(":")
                    PmagSpecRec["magic_software_packages"] = version_num
                    PmagSpecRec["specimen_description"] = comment
                    if critout != "":
                        kill = pmag.grade(PmagSpecRec, accept, "specimen_int")
                        if len(kill) > 0:
                            Grade = "F"  # fails
                        else:
                            Grade = "A"  # passes
                        PmagSpecRec["specimen_grade"] = Grade
                    else:
                        PmagSpecRec["specimen_grade"] = ""  # not graded
                    if nltrm == 0 and anis == 0 and cool != 0:  # apply cooling rate correction
                        SCR = pmag.get_dictitem(
                            SampCRs, "er_sample_name", PmagSpecRec["er_sample_name"], "T"
                        )  # get this samples, cooling rate correction
                        CrSpecRec = pmag.cooling_rate(PmagSpecRec, SCR, crfrac, crtype)
                        if CrSpecRec["er_specimen_name"] != "none":
                            CrSpecs.append(CrSpecRec)
                    PmagSpecs.append(PmagSpecRec)
                    NltSpecRec = ""
                    #
                    # check on non-linear TRM correction
                    #
                    if nltrm == 1:
                        #
                        # find the data from the nlt_data list for this specimen
                        #
                        TRMs, Bs = [], []
                        NltSpecRec = ""
                        NltRecs = pmag.get_dictitem(
                            nlt_data, "er_specimen_name", PmagSpecRec["er_specimen_name"], "has"
                        )  # fish out all the NLT data for this specimen
                        if len(NltRecs) > 2:
                            for NltRec in NltRecs:
                                Bs.append(float(NltRec["treatment_dc_field"]))
                                TRMs.append(float(NltRec["measurement_magn_moment"]))
                            NLTpars = nlt.NLtrm(
                                Bs,
                                TRMs,
                                float(PmagSpecRec["specimen_int"]),
                                float(PmagSpecRec["specimen_lab_field_dc"]),
                                0,
                            )
                            if NLTpars["banc"] > 0:
                                NltSpecRec = {}
                                for key in PmagSpecRec.keys():
                                    NltSpecRec[key] = PmagSpecRec[key]
                                NltSpecRec["specimen_int"] = "%9.4e" % (NLTpars["banc"])
                                NltSpecRec["magic_method_codes"] = PmagSpecRec["magic_method_codes"] + ":DA-NL"
                                NltSpecRec["specimen_correction"] = "c"
                                NltSpecRec["specimen_grade"] = PmagSpecRec["specimen_grade"]
                                NltSpecRec["magic_software_packages"] = version_num
                                print NltSpecRec["er_specimen_name"], " Banc= ", float(NLTpars["banc"]) * 1e6
                                if anis == 0 and cool != 0:
                                    SCR = pmag.get_dictitem(
                                        SampCRs, "er_sample_name", NltSpecRec["er_sample_name"], "T"
                                    )  # get this samples, cooling rate correction
                                    CrSpecRec = pmag.cooling_rate(NltSpecRec, SCR, crfrac, crtype)
                                    if CrSpecRec["er_specimen_name"] != "none":
                                        CrSpecs.append(CrSpecRec)
                                NltSpecRecs.append(NltSpecRec)
                        #
                        # check on anisotropy correction
                        if anis == 1:
                            if NltSpecRec != "":
                                Spc = NltSpecRec
                            else:  # find uncorrected data
                                Spc = PmagSpecRec
                            AniSpecs = pmag.get_dictitem(
                                anis_data, "er_specimen_name", PmagSpecRec["er_specimen_name"], "T"
                            )
                            if len(AniSpecs) > 0:
                                AniSpec = AniSpecs[0]
                                AniSpecRec = pmag.doaniscorr(Spc, AniSpec)
                                AniSpecRec["specimen_grade"] = PmagSpecRec["specimen_grade"]
                                AniSpecRec["magic_instrument_codes"] = PmagSpecRec["magic_instrument_codes"]
                                AniSpecRec["specimen_correction"] = "c"
                                AniSpecRec["magic_software_packages"] = version_num
                                if cool != 0:
                                    SCR = pmag.get_dictitem(
                                        SampCRs, "er_sample_name", AniSpecRec["er_sample_name"], "T"
                                    )  # get this samples, cooling rate correction
                                    CrSpecRec = pmag.cooling_rate(AniSpecRec, SCR, crfrac, crtype)
                                    if CrSpecRec["er_specimen_name"] != "none":
                                        CrSpecs.append(CrSpecRec)
                                AniSpecRecs.append(AniSpecRec)
                    elif anis == 1:
                        AniSpecs = pmag.get_dictitem(
                            anis_data, "er_specimen_name", PmagSpecRec["er_specimen_name"], "T"
                        )
                        if len(AniSpecs) > 0:
                            AniSpec = AniSpecs[0]
                            AniSpecRec = pmag.doaniscorr(PmagSpecRec, AniSpec)
                            AniSpecRec["specimen_grade"] = PmagSpecRec["specimen_grade"]
                            AniSpecRec["magic_instrument_codes"] = PmagSpecRec["magic_instrument_codes"]
                            AniSpecRec["specimen_correction"] = "c"
                            AniSpecRec["magic_software_packages"] = version_num
                            if crfrac != 0:
                                CrSpecRec = {}
                                for key in AniSpecRec.keys():
                                    CrSpecRec[key] = AniSpecRec[key]
                                inten = frac * float(CrSpecRec["specimen_int"])
                                CrSpecRec["specimen_int"] = "%9.4e " % (
                                    inten
                                )  # adjust specimen intensity by cooling rate correction
                                CrSpecRec["magic_method_codes"] = CrSpecRec["magic_method_codes"] + ":DA-CR-" + crtype
                                CRSpecs.append(CrSpecRec)
                            AniSpecRecs.append(AniSpecRec)
                spec += 1
        else:
            print "skipping ", s
            spec += 1
    pmag_file = dir_path + "/" + pmag_file
    pmag.magic_write(pmag_file, PmagSpecs, "pmag_specimens")
    print "uncorrected thellier data saved in: ", pmag_file
    if anis == 1 and len(AniSpecRecs) > 0:
        anisout = dir_path + "/" + anisout
        pmag.magic_write(anisout, AniSpecRecs, "pmag_specimens")
        print "anisotropy corrected data saved in: ", anisout
    if nltrm == 1 and len(NltSpecRecs) > 0:
        nltout = dir_path + "/" + nltout
        pmag.magic_write(nltout, NltSpecRecs, "pmag_specimens")
        print "non-linear TRM corrected data saved in: ", nltout
    if crfrac != 0:
        crout = dir_path + "/" + crout
        pmag.magic_write(crout, CRSpecs, "pmag_specimens")
        print "cooling rate corrected data saved in: ", crout
Example #26
0
def main():
    """
    NAME
        HUJI_sample_magic.py
   
    DESCRIPTION
        takes tab delimited Hebrew University sample file and converts to MagIC formatted tables
 
    SYNTAX
        HUJI_sample_magic.py [command line options]

    OPTIONS
        -f FILE: specify input file
        -Fsa FILE: specify sample output file, default is: er_samples.txt 
        -Fsi FILE: specify site output file, default is: er_sites.txt 
        -Iso:  import sample orientation info - default is to set sample_az/dip to 0,0
        -ncn NCON:  specify naming convention: default is #1 below
        -mcd: specify sampling method codes as a colon delimited string:  [default is: FS-FD:SO-POM]
             FS-FD field sampling done with a drill
             FS-H field sampling done with hand samples
             FS-LOC-GPS  field location done with GPS
             FS-LOC-MAP  field location done with map
             SO-POM   a Pomeroy orientation device was used
             SO-ASC   an ASC orientation device was used
             SO-MAG   orientation with magnetic compass
        -loc: location name, default="unknown"

    INPUT FORMAT
        Input files must be tab delimited:
            Samp  Az Dip Dip_dir Dip 
        Orientation convention:
             Lab arrow azimuth = mag_azimuth; Lab arrow dip = 90-field_dip
                e.g. field_dip is degrees from horizontal of drill direction
       
         Magnetic declination convention:
             Az is already corrected in file 
    
         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
            [5] all others you will have to either customize your 
                self or e-mail [email protected] for help.  
    OUTPUT
            output saved in er_samples.txt  will overwrite any existing files 
    """
    #
    # initialize variables
    #
    version_num=pmag.get_version()
    samp_file,or_con,corr = "er_samples.txt","1","1"
    site_file='er_sites.txt'
    args=sys.argv
    date,lat,lon="","",""  # date of sampling, latitude (pos North), longitude (pos East)
    bed_dip,bed_dip_dir="",""
    participantlist=""
    sites=[]   # list of site names
    Lats,Lons=[],[] # list of latitudes and longitudes
    SampRecs,SiteRecs,ImageRecs,imagelist=[],[],[],[]  # lists of Sample records and Site records
    samp_con,Z,average_bedding="1",1,"0"
    newbaseline,newbeddir,newbeddip="","",""
    meths='FS-FD:SO-POM:SO-SUN'
    delta_u="0"
    sclass,lithology,type="","",""
    newclass,newlith,newtype='','',''
    user=""
    or_con='3'
    corr=="3"
    DecCorr=0.
    location_name="unknown"
    ignore=1
    #
    #
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-f" in args:
        ind=args.index("-f")
        orient_file=sys.argv[ind+1]
    else:
        "Must have orientation file name"
        sys.exit() 
    if "-Fsa" in args:
        ind=args.index("-Fsa")
        samp_file=sys.argv[ind+1]
    if "-ncn" in args:
        ind=args.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 3-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="4"
            print samp_con, Z
    if "-mcd" in args:
        ind=args.index("-mcd")
        meths=(sys.argv[ind+1])
    if "-loc" in args:
        ind=args.index("-loc")
        location_name=(sys.argv[ind+1])
    if "-Iso" in args: ignore=0
    #
    # read in file to convert
    #
    azfile=open(orient_file,'rU')
    AzDipDat=azfile.readlines()
    azfile.close()
    SampOut=[]
    SiteOut=[]
    for line in AzDipDat[1:]: 
      orec=line.split()
      if len(orec)>1:
        labaz,labdip=pmag.orient(float(orec[1]),float(orec[2]),or_con)
        bed_dip_dir=(orec[3])
        bed_dip=(orec[4])
        SampRec={}
        SiteRec={}
        SampRec["er_location_name"]=location_name
        SampRec["er_citation_names"]="This study"
        SiteRec["er_location_name"]=location_name
        SiteRec["er_citation_names"]="This study"
        SiteRec["site_class"]=""
        SiteRec["site_lithology"]=""
        SiteRec["site_type"]=""
        SiteRec["site_definition"]="s"
        SiteRec["er_citation_names"]="This study"
    #
    # parse information common to all orientation methods
    #
        SampRec["er_sample_name"]=orec[0]
        SampRec["sample_bed_dip_direction"]=orec[3]
        SampRec["sample_bed_dip"]=orec[4]
        SiteRec["site_bed_dip_direction"]=orec[3]
        SiteRec["site_bed_dip"]=orec[4]
        if ignore==0:
            SampRec["sample_dip"]='%7.1f'%(labdip)
            SampRec["sample_azimuth"]='%7.1f'%(labaz)
        else:
            SampRec["sample_dip"]='0'
            SampRec["sample_azimuth"]='0'
        SampRec["sample_lat"]=orec[5]
        SampRec["sample_lon"]=orec[6]
        SiteRec["site_lat"]=orec[5]
        SiteRec["site_lon"]=orec[6]
        methods=meths.split(":")
        SampRec["magic_method_codes"]=meths
        site=pmag.parse_site(orec[0],samp_con,Z) # parse out the site name
        SampRec["er_site_name"]=site
        SampRec['magic_software_packages']=version_num
        SiteRec["er_site_name"]=site
        SiteRec['magic_software_packages']=version_num
        SampOut.append(SampRec)
        SiteOut.append(SiteRec)
    pmag.magic_write(samp_file,SampOut,"er_samples")
    print "Sample info saved in ", samp_file
    pmag.magic_write(site_file,SiteOut,"er_sites")
    print "Site info saved in ", site_file
Example #27
0
def main():
    """
    NAME
        zeq_magic_redo.py
   
    DESCRIPTION
        Calculate principal components through demagnetization data using bounds and calculation type stored in "redo" file
  
    SYNTAX
        zeq_magic_redo.py [command line options]

    OPTIONS
        -h prints help message
        -usr USER:   identify user, default is ""
        -f: specify input file, default is magic_measurements.txt
        -F: specify output file, default is zeq_specimens.txt
        -fre  REDO: specify redo file, default is "zeq_redo"
        -fsa  SAMPFILE: specify er_samples format file, default is "er_samples.txt"
        -A : don't average replicate measurements, default is yes
        -crd [s,g,t] : 
             specify coordinate system [s,g,t]  [default is specimen coordinates]
                 are specimen, geographic, and tilt corrected respectively
             NB: you must have a SAMPFILE in this directory to rotate from specimen coordinates
        -leg:  attaches "Recalculated from original measurements; supercedes published results. " to comment field
    INPUTS
        zeq_redo format file is:
        specimen_name calculation_type[DE-BFL,DE-BFL-A,DE-BFL-O,DE-BFP,DE-FM]  step_min step_max component_name[A,B,C]
    """
    dir_path='.'
    INCL=["LT-NO","LT-AF-Z","LT-T-Z","LT-M-Z"] # looking for demag data
    beg,end,pole,geo,tilt,askave,save=0,0,[],0,0,0,0
    user,doave,comment= "",1,""
    geo,tilt=0,0
    version_num=pmag.get_version()
    args=sys.argv
    if '-WD' in args:
        ind=args.index('-WD')
        dir_path=args[ind+1]
    meas_file,pmag_file,mk_file= dir_path+"/"+"magic_measurements.txt",dir_path+"/"+"zeq_specimens.txt",dir_path+"/"+"zeq_redo"
    samp_file,coord=dir_path+"/"+"er_samples.txt",""
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-usr" in args:
        ind=args.index("-usr")
        user=sys.argv[ind+1]
    if "-A" in args:doave=0
    if "-leg" in args: comment="Recalculated from original measurements; supercedes published results. "
    if "-f" in args:
        ind=args.index("-f")
        meas_file=dir_path+'/'+sys.argv[ind+1]
    if "-F" in args:
        ind=args.index("-F")
        pmag_file=dir_path+'/'+sys.argv[ind+1]
    if "-fre" in args:
        ind=args.index("-fre")
        mk_file=dir_path+"/"+args[ind+1]
    try:
        mk_f=open(mk_file,'rU')
    except:
        print "Bad redo file"
        sys.exit()
    mkspec,skipped=[],[]
    speclist=[]
    for line in mk_f.readlines():
        tmp=line.split()
        mkspec.append(tmp)
        speclist.append(tmp[0])
    if "-fsa" in args:
        ind=args.index("-fsa")
        samp_file=dir_path+'/'+sys.argv[ind+1]
    if "-crd" in args:
        ind=args.index("-crd")
        coord=sys.argv[ind+1]
        if coord=="g":geo,tilt=1,0
        if coord=="t":geo,tilt=1,1
#
# now get down to bidness
    if geo==1:
        samp_data,file_type=pmag.magic_read(samp_file)
        if file_type != 'er_samples':
            print file_type
            print "This is not a valid er_samples file " 
            sys.exit()
    # set orientation priorities
        SO_methods=[]
        for rec in samp_data:
           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)
    #
    #
    #

    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
    #
    k = 0
    print 'Processing ',len(speclist), ' specimens - please wait'
    PmagSpecs=[]
    while k < len(speclist):
        s=speclist[k]
        recnum=0
        PmagSpecRec={}
        method_codes,inst_codes=[],[]
    # find the data from the meas_data file for this sample
    #
    #  collect info for the PmagSpecRec dictionary
    #
        meas_meth=[]
        for rec in  meas_data: # copy of vital stats to PmagSpecRec from first spec record in demag block
           skip=1
           if rec["er_specimen_name"]==s: 
               methods=rec["magic_method_codes"].split(":")
               if len(set(methods) & set(INCL))>0:
                   PmagSpecRec["er_analyst_mail_names"]=user
                   PmagSpecRec["magic_software_packages"]=version_num
                   PmagSpecRec["er_specimen_name"]=s
                   PmagSpecRec["er_sample_name"]=rec["er_sample_name"]
                   PmagSpecRec["er_site_name"]=rec["er_site_name"]
                   PmagSpecRec["er_location_name"]=rec["er_location_name"]
                   PmagSpecRec["er_citation_names"]="This study"
                   if "magic_experiment_name" not in rec.keys(): rec["magic_experiment_name"]=""
                   PmagSpecRec["magic_experiment_names"]=rec["magic_experiment_name"]
                   if "magic_instrument_codes" not in rec.keys(): rec["magic_instrument_codes"]=""
                   inst=rec['magic_instrument_codes'].split(":")
                   for I in inst:
                       if I not in inst_codes:  # copy over instruments
                           inst_codes.append(I)
                   meths=rec["magic_method_codes"].split(":")
                   for meth in meths:
                       if meth.strip() not in meas_meth:meas_meth.append(meth)
                   if "LP-DIR-AF" in meas_meth or "LT-AF-Z" in meas_meth: 
                       PmagSpecRec["measurement_step_unit"]="T"
                       if "LP-DIR-AF" not in method_codes:method_codes.append("LP-DIR-AF") 
                   if "LP-DIR-T" in meas_meth or "LT-T-Z" in meas_meth: 
                       PmagSpecRec["measurement_step_unit"]="K"
                       if "LP-DIR-T" not in method_codes:method_codes.append("LP-DIR-T") 
                   if "LP-DIR-M" in meas_meth or "LT-M-Z" in meas_meth: 
                       PmagSpecRec["measurement_step_unit"]="J"
                       if "LP-DIR-M" not in method_codes:method_codes.append("LP-DIR-M") 
        if PmagSpecRec=={}:
            print 'no data found for specimen:  ',s
            print 'delete from zeq_redo input file...., then try again'
            sys.exit()
    #
    #
        data,units=pmag.find_dmag_rec(s,meas_data)
    #
        datablock=data
        noskip=1
        if len(datablock) <2 or s not in speclist : 
            noskip=0
            k+=1
#            print 'skipping ', s,len(datablock)
        if noskip:
        #
        # find replicate measurements at given treatment step and average them
        #
#            step_meth,avedata=pmag.vspec(data)
#
#            if len(avedata) != len(datablock):
#                if doave==1: 
#                    method_codes.append("DE-VM")
#                    datablock=avedata
        #
        # do geo or stratigraphic correction now
        #
            if geo==1:
       # find top priority orientation method
                redo,p=1,0
                if len(SO_methods)<=1:
                    az_type=SO_methods[0]
                    orient=pmag.find_samp_rec(PmagSpecRec["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
        #
        #  if tilt selected,  get stratigraphic correction
        #
                tiltblock,geoblock=[],[]
                for rec in datablock:
                    if "sample_azimuth" in orient.keys() and orient["sample_azimuth"]!="":
                        d_geo,i_geo=pmag.dogeo(rec[1],rec[2],orient["sample_azimuth"],orient["sample_dip"])
                        geoblock.append([rec[0],d_geo,i_geo,rec[3],rec[4],rec[5]])
                        if tilt==1 and "sample_bed_dip_direction" in orient.keys(): 
                            d_tilt,i_tilt=pmag.dotilt(d_geo,i_geo,orient["sample_bed_dip_direction"],orient["sample_bed_dip"])
                            tiltblock.append([rec[0],d_tilt,i_tilt,rec[3],rec[4],rec[5]])
                        elif tilt==1:
                            if PmagSpecRec["er_sample_name"] not in skipped:
                                print 'no tilt correction for ', PmagSpecRec["er_sample_name"],' skipping....'
                                skipped.append(PmagSpecRec["er_sample_name"])
                    else:
                        if PmagSpecRec["er_sample_name"] not in skipped:
                            print 'no geographic correction for ', PmagSpecRec["er_sample_name"],' skipping....'
                            skipped.append(PmagSpecRec["er_sample_name"])
    #
    #	get beg_pca, end_pca, pca
            if PmagSpecRec['er_sample_name'] not in skipped:
                compnum=-1
                for spec in mkspec:
                    if spec[0]==s:
                        CompRec={}
                        for key in PmagSpecRec.keys():CompRec[key]=PmagSpecRec[key]
                        compnum+=1
                        calculation_type=spec[1]
                        beg=float(spec[2])
                        end=float(spec[3])
                        if len(spec)>4:
                            comp_name=spec[4]
                        else:
                            comp_name=string.uppercase[compnum]
                        CompRec['specimen_comp_name']=comp_name
                        if beg < float(datablock[0][0]):beg=float(datablock[0][0])
                        if end > float(datablock[-1][0]):end=float(datablock[-1][0])
                        for l  in range(len(datablock)):
                            if datablock[l][0]==beg:beg_pca=l
                            if datablock[l][0]==end:end_pca=l
                        if geo==1 and tilt==0:
                            mpars=pmag.domean(geoblock,beg_pca,end_pca,calculation_type)
                            if mpars["specimen_direction_type"]!="Error":
                                CompRec["specimen_dec"]='%7.1f ' %(mpars["specimen_dec"])
                                CompRec["specimen_inc"]='%7.1f ' %(mpars["specimen_inc"])
                                CompRec["specimen_tilt_correction"]='0'
                        if geo==1 and tilt==1:
                            mpars=pmag.domean(tiltblock,beg_pca,end_pca,calculation_type)
                            if mpars["specimen_direction_type"]!="Error":
                                CompRec["specimen_dec"]='%7.1f ' %(mpars["specimen_dec"])
                                CompRec["specimen_inc"]='%7.1f ' %(mpars["specimen_inc"])
                                CompRec["specimen_tilt_correction"]='100'
                        if geo==0 and tilt==0: 
                            mpars=pmag.domean(datablock,beg_pca,end_pca,calculation_type)
                            if mpars["specimen_direction_type"]!="Error":
                                CompRec["specimen_dec"]='%7.1f ' %(mpars["specimen_dec"])
                                CompRec["specimen_inc"]='%7.1f ' %(mpars["specimen_inc"])
                                CompRec["specimen_tilt_correction"]='-1'
                        if mpars["specimen_direction_type"]=="Error": 
                            pass
                        else: 
                            CompRec["measurement_step_min"]='%8.3e '%(datablock[beg_pca][0])
                            try:
                                CompRec["measurement_step_max"]='%8.3e '%(datablock[end_pca][0] )
                            except:
                                print 'error in end_pca ',PmagSpecRec['er_specimen_name']
                            CompRec["specimen_correction"]='u'
                            if calculation_type!='DE-FM':
                                CompRec["specimen_mad"]='%7.1f '%(mpars["specimen_mad"])
                                CompRec["specimen_alpha95"]=""
                            else:
                                CompRec["specimen_mad"]=""
                                CompRec["specimen_alpha95"]='%7.1f '%(mpars["specimen_alpha95"])
                            CompRec["specimen_n"]='%i '%(mpars["specimen_n"])
                            CompMeths=[]
                            for meth in method_codes:
                                if meth not in CompMeths:CompMeths.append(meth)
                            if calculation_type not in CompMeths:CompMeths.append(calculation_type)
                            if geo==1: CompMeths.append("DA-DIR-GEO")
                            if tilt==1: CompMeths.append("DA-DIR-TILT")
                            if "DE-BFP" not in calculation_type:
                                CompRec["specimen_direction_type"]='l'
                            else:
                                CompRec["specimen_direction_type"]='p'
                            CompRec["magic_method_codes"]=""
                            if len(CompMeths) != 0:
                                methstring=""
                                for meth in CompMeths:
                                    methstring=methstring+ ":" +meth
                                CompRec["magic_method_codes"]=methstring.strip(':')
                            CompRec["specimen_description"]=comment
                            if len(inst_codes) != 0:
                                inststring=""
                                for inst in inst_codes:
                                    inststring=inststring+ ":" +inst
                                CompRec["magic_instrument_codes"]=inststring.strip(':')
                            PmagSpecs.append(CompRec)
            k+=1
    pmag.magic_write(pmag_file,PmagSpecs,'pmag_specimens')
    print "Recalculated specimen data stored in ",pmag_file
Example #28
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 #29
0
def main():
    """
    NAME
        ODP_srm_magic.py
 
    DESCRIPTION
        converts ODP measurement format files to magic_measurements format files

    SYNTAX
        ODP_srm_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -F FILE: specify output  measurements file, default is magic_measurements.txt
        -Fsa FILE: specify output er_sample.txt file, default is er_sample.txt
        -A : don't average replicate measurements
    INPUT
        put data from a single core into a directory. depths will be below core top

    """
#        
#
    version_num=pmag.get_version()
    meas_file='magic_measurements.txt'
    samp_file='er_samples.txt'
    ErSpecs,ErSamps,ErSites,ErLocs,ErCits=[],[],[],[],[]
    MagRecs=[]
    citation="This study"
    dir_path,demag='.','NRM'
    args=sys.argv
    noave=0,
    if '-WD' in args:
        ind=args.index("-WD")
        dir_path=args[ind+1]
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-A" in args: noave=1
    if '-F' in args:
        ind=args.index("-F")
        meas_file=args[ind+1]
    if '-Fsa' in args:
        ind=args.index("-Fsa")
        samp_file=args[ind+1]
    if '-LP' in args:
        ind=args.index("-LP")
        codelist=args[ind+1]
        codes=codelist.split(':')
        if "AF" in codes:
            demag='AF' 
            if'-dc' not in args: methcode="LT-AF-Z"
            if'-dc' in args: methcode="LT-AF-I"
        if "T" in codes:
            demag="T"
            if '-dc' not in args: methcode="LT-T-Z"
            if '-dc' in args: methcode="LT-T-I"
        if "I" in codes:
            methcode="LP-IRM"
        if "S" in codes: 
            demag="S"
            methcode="LP-PI-TRM:LP-PI-ALT-AFARM"
            trm_labfield=labfield
            ans=raw_input("DC lab field for ARM step: [50uT] ")
            if ans=="":
                arm_labfield=50e-6
            else: 
                arm_labfield=float(ans)*1e-6
            ans=raw_input("temperature for total trm step: [600 C] ")
            if ans=="":
                trm_peakT=600+273 # convert to kelvin
            else: 
                trm_peakT=float(ans)+273 # convert to kelvin
        if "G" in codes: methcode="LT-AF-G"
	if "D" in codes: methcode="LT-AF-D"
        if "TRM" in codes: 
            demag="T"
            trm=1
    if demag=="T" and "ANI" in codes:
        methcode="LP-AN-TRM"
    if demag=="AF" and "ANI" in codes:
        methcode="LP-AN-ARM"
        if labfield==0: labfield=50e-6
        if peakfield==0: peakfield=.180
    samp_file=dir_path+'/'+samp_file
    meas_file=dir_path+'/'+meas_file
    filelist=os.listdir(dir_path) # read in list of files to import
    specimens,samples,sites=[],[],[]
    MagRecs,SpecRecs,SampRecs=[],[],[]
    for file in filelist: # parse each file
        if file[-3:].lower()=='srm':
            print 'processing: ',file
            Nfo=file.split('_')[0].split('-')
            try:
                sect=int(Nfo[3][:-1])
            except:
                sect=1
            input=open(file,'rU').readlines()
            MagRec,SpecRec,SampRec={},{},{}
            alt_spec,treatment_type,treatment_value,user="","","",""
            inst="ODP-SRM"
            SampRec['sample_azimuth']='0'
            SampRec['sample_dip']='0'
            SampRec['magic_method_code']='FS-C-DRILL-IODP:SP-SS-C'
            MagRec['er_analyst_mail_names']=user
            MagRec['magic_method_codes']='LT-NO'
            MagRec['magic_software_packages']=version_num
            MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
            MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
            MagRec["treatment_ac_field"]='0'
            MagRec["treatment_dc_field"]='0'
            MagRec["treatment_dc_field_phi"]='0'
            MagRec["treatment_dc_field_theta"]='0'
            MagRec["measurement_flag"]='g' # assume all data are "good"
            MagRec["measurement_standard"]='u' # assume all data are "good"
            MagRec["measurement_csd"]='' # set csd to blank
            SpecRec['er_specimen_alternatives']=alt_spec
            vol=7e-6 # assume 7 cc samples
            datestamp=input[1].split() # date time is second line of file
            mmddyy=datestamp[0].split('/') # break into month day year
            date=mmddyy[2]+':'+mmddyy[0]+":"+mmddyy[1] +':' +datestamp[1]
            MagRec["measurement_date"]=date
            treatment_value,inst="","ODP-SRM"
            k=0
            while 1:
                fields= input[k].replace('\n','').split("=")
                if 'treatment_type' in fields[0]:
                    if "Alternating Frequency Demagnetization" in fields[1]:
                        MagRec['magic_method_codes'] = 'LT-AF-Z'
                        inst=inst+':ODP-DTECH' # measured on shipboard AF DTECH D2000
                if "treatment_value" in fields[0]:
                    value=fields[1]
                    if value!=" ":
                        treatment_value=float(value)*1e-3
                        MagRec["treatment_ac_field"]='%8.3e'%(treatment_value) # AF demag in treat mT => T
                if 'user' in fields[0]: 
                    user=fields[-1]
                    MagRec["er_analyst_mail_names"]=user
                MagRec["measurement_standard"]='u' # assume all data are "good"
                if 'sample_area' in fields[0]:  vol=float(fields[1])*1e-6 # takes volume (cc) and converts to m^3
                if 'run_number' in fields[0]:  
                    MagRec['external_database_ids']=fields[1] # run number is the LIMS measurement number
                    MagRec['external_database_names']='LIMS'
                k+=1
                if input[k][0:7]=='<MULTI>': 
                    break 
            while 1:
                k+=1
                line = input[k]
                if line[0:5]=='<RAW>': 
                    break 
                treatment_value=""
                rec=line.replace('\n','').split(',') # list of data
                if len(rec)>2:
                    MeasRec,SampRec={},{'core_depth':'0','er_sample_name':'0','er_site_name':'0','er_location_name':'location'}
                    for key in MagRec.keys():MeasRec[key]=MagRec[key]
                    for item in rec:
                             items=item.split('=')
                             if 'demag_level' in items[0]:
                                treat= float(items[1])
                                if treat!=0:
                                    MeasRec['magic_method_codes']='LT-AF-Z'
                                    inst=inst+':ODP-SRM-AF'
                                    MeasRec["treatment_ac_field"]='%8.3e'%(treat*1e-3) # AF demag in treat mT => T
                             if 'inclination_w_tray_w_bkgrd' in items[0]: MeasRec['measurement_inc']=items[1]
                             if 'declination_w_tray_w_bkgrd' in items[0]: MeasRec['measurement_dec']=items[1]
                             if 'intensity_w_tray_w_bkgrd' in items[0]: MeasRec['measurement_magn_moment']='%8.3e'%(float(items[1])*vol) # convert intensity from A/m to Am^2 using vol
                             MeasRec['magic_instrument_codes']=inst
                             if 'offset' in items[0]:
                                 depth='%7.3f'%(float(sect-1)*1.5+float(items[1]))
                                 SampRec['core_depth']=depth
                                 MeasRec['er_specimen_name']=depth
                                 MeasRec['er_sample_name']=depth
                                 MeasRec['er_site_name']=depth
                                 MeasRec['er_location_name']='location'
                                 SampRec['er_sample_name']=depth
                                 SampRec['er_site_name']=depth
                                 SampRec['er_location_name']='location'
                                 MeasRec['measurement_number']='1'
                    SampRecs.append(SampRec)
                    MagRecs.append(MeasRec)
    pmag.magic_write(samp_file,SampRecs,'er_samples')
    print 'samples stored in ',samp_file
    Fixed=pmag.measurements_methods(MagRecs,noave)
    pmag.magic_write(meas_file,Fixed,'magic_measurements')
    print 'data stored in ',meas_file
Example #30
0
def main():
    """
    NAME
        UMICH_magic.py
 
    DESCRIPTION
        converts UMICH .mag format files to magic_measurements format files

    SYNTAX
        UMICH_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify .mag format input file, required
        -fsa SAMPFILE : specify er_samples.txt file relating samples, site and locations names,default is none
        -F FILE: specify output file, default is magic_measurements.txt
        -spc NUM : specify number of characters to designate a  specimen, default = 0
        -loc LOCNAME : specify location/study name, must have either LOCNAME or SAMPFILE or be a synthetic
        -ncn NCON:  specify naming convention: default is #1 below
        -A: don't average replicate measurements
       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] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name same as sample
            [6] site is entered under a separate column
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.
 
        Format of UMICH .mag files:   
        Spec Treat CSD Intensity Declination Inclination metadata string
        Spec: specimen name
        Treat:  treatment step
            XXX T in Centigrade
            XXX AF in mT
         Intensity assumed to be total moment in 10^3 Am^2 (emu)
         Declination:  Declination in specimen coordinate system
         Inclination:  Declination in specimen coordinate system

         metatdata string:  mm/dd/yy;hh:mm;[dC,mT];xx.xx;UNITS;USER;INST;NMEAS
             hh in 24 hours.  
             dC or mT units of treatment XXX (see Treat above) for thermal or AF respectively
             xx.xxx   DC field
             UNITS of DC field (microT, mT)
             INST:  instrument code, number of axes, number of positions (e.g., G34 is 2G, three axes, 
                    measured in four positions)
             NMEAS: number of measurements in a single position (1,3,200...)
    """
# initialize some stuff
    dir_path='.'
    infile_type="mag"
    noave=0
    methcode,inst="",""
    phi,theta,peakfield,labfield=0,0,0,0
    pTRM,MD,samp_con,Z=0,0,'1',1
    missing=1
    demag="N"
    er_location_name=""
    citation='This study'
    args=sys.argv
    methcode="LP-NO"
    samp_file,ErSamps='',[]
    specnum=0
#
# get command line arguments
#
    meas_file="magic_measurements.txt"
    user=""
    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=args[ind+1]
    if '-F' in args:
        ind=args.index("-F")
        meas_file=dir_path+'/'+args[ind+1]
    if '-f' in args:
        ind=args.index("-f")
        magfile=dir_path+'/'+args[ind+1]
        try:
            input=open(magfile,'rU')
        except:
            print "bad mag file name"
            sys.exit()
    else: 
        print "mag_file field is required option"
        print main.__doc__
        sys.exit()
    if "-spc" in args:
        ind=args.index("-spc")
        specnum=int(args[ind+1])
        if specnum!=0:specnum=-specnum
    if "-loc" in args:
        ind=args.index("-loc")
        er_location_name=args[ind+1]
    if "-fsa" in args:
        ind=args.index("-fsa")
        samp_file=dir_path+'/'+args[ind+1]
        Samps,file_type=pmag.magic_read(samp_file)
    if "-A" in args: noave=1
    if "-ncn" in args:
        ind=args.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"
            samp_con=sys.argv[ind+1]
        if "7" in samp_con:
            if "-" not in samp_con:
                print "option [7] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="7"
    MagRecs,specs=[],[]
    version_num=pmag.get_version()
    if infile_type=="mag":
        for line in input.readlines():
            instcode=""
            if len(line)>2:
                MagRec={}
                MagRec['er_location_name']=er_location_name
                MagRec['magic_software_packages']=version_num
                MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
                MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
                MagRec["treatment_ac_field"]='0'
                MagRec["treatment_dc_field"]='0'
                MagRec["treatment_dc_field_phi"]='0'
                MagRec["treatment_dc_field_theta"]='0'
                meas_type="LT-NO"
                rec=line.split()
                labfield=0
                code1=rec[6].split(';')
                date=code1[0].split('/') # break date into mon/day/year
                yy=int(date[2])
                if yy <90:
                    yyyy=str(2000+yy)
                else: yyyy=str(1900+yy)
                mm=int(date[0])
                if mm<10:
                    mm="0"+str(mm)
                else: mm=str(mm)
                dd=int(date[1])
                if dd<10:
                    dd="0"+str(dd)
                else: dd=str(dd)
                time=code1[1].split(':')
                hh=int(time[0])
                if hh<10:
                    hh="0"+str(hh)
                else: hh=str(hh)
                min=int(time[1])
                if min<10:
                   min= "0"+str(min)
                else: min=str(min)
                MagRec["measurement_date"]=yyyy+":"+mm+":"+dd+":"+hh+":"+min+":00.00"
                MagRec["measurement_time_zone"]=''
                instcode=''
                if len(code1)>1:
                    MagRec["measurement_positions"]=code1[6][2]
                else:
                    MagRec["measurement_positions"]=code1[7]   # takes care of awkward format with bubba and flo being different
                if user=="":user=code1[5]
                if code1[2][-1]=='C': demag="T"
                if code1[2]=='mT': demag="AF"
                treat=rec[1].split('.')
                if len(treat)==1:treat.append('0')
                if demag=='T' and treat!=0:
                    meas_type="LT-T-Z"
                    MagRec["treatment_temp"]='%8.3e' % (float(treat[0])+273.) # temp in kelvin
                if demag=="AF":
                    meas_type="LT-AF-Z"
                    MagRec["treatment_ac_field"]='%8.3e' % (float(treat[0])*1e-3) # Af field in T
                MagRec["treatment_dc_field"]='0'
                MagRec["er_specimen_name"]=rec[0]
                if rec[0] not in specs:specs.append(rec[0]) # get a list of specimen names
                experiment=rec[0]+":"
                MagRec["er_site_name"]=""
                if specnum!=0:
                    MagRec["er_sample_name"]=rec[0][:specnum]
                else:
                    MagRec["er_sample_name"]=rec[0]
                if "-fsa" in args:
                    for samp in Samps:
                        if samp["er_sample_name"] == MagRec["er_sample_name"]: 
                            MagRec["er_location_name"]=samp["er_location_name"]
                            MagRec["er_site_name"]=samp["er_site_name"]
                            break
                elif int(samp_con)!=6:
                    site=pmag.parse_site(MagRec['er_sample_name'],samp_con,Z)
                    MagRec["er_site_name"]=site
                if MagRec['er_site_name']=="":
                    print 'No site name found for: ',MagRec['er_specimen_name'],MagRec['er_sample_name']
                if MagRec["er_location_name"]=="":
                    print 'no location name for: ',MagRec["er_specimen_name"] 
                if rec[1]==".00":rec[1]="0.00"
                MagRec["measurement_csd"]=rec[2]
                MagRec["measurement_magn_moment"]='%10.3e'% (float(rec[3])*1e-3) # moment in Am^2 (from emu)
                MagRec["measurement_dec"]=rec[4]
                MagRec["measurement_inc"]=rec[5]
                MagRec["magic_instrument_codes"]=instcode
                MagRec["er_analyst_mail_names"]=user
                MagRec["er_citation_names"]=citation
                MagRec["magic_method_codes"]=meas_type
                MagRec["measurement_flag"]='g'
                MagRec["er_specimen_name"]=rec[0]
                MagRec["measurement_number"]='1'
                MagRecs.append(MagRec) 
    MagOuts=[]
    for spec in specs:  # gather all demag types for this specimen
        SpecRecs,meths,measnum=[],[],1
        for rec in MagRecs:
            if rec['er_specimen_name']==spec:
                rec['measurement_number']=str(measnum)
                measnum+=1
                if rec['magic_method_codes'] not in meths:meths.append(rec['magic_method_codes'])
                SpecRecs.append(rec)
        expname=spec
        if "LT-AF-Z" in meths:expname=expname+ ':LP-DIR-AF'
        if "LT-T-Z" in meths:expname=expname+ ':LP-DIR-T'
        for rec in SpecRecs:
            rec['magic_experiment_name']=expname
            MagOuts.append(rec)
    pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    print "results put in ",meas_file