Exemplo n.º 1
0
def main():
    """
    NAME 
        change_case_magic.py

    DESCRIPTION
        picks out key and converts to upper or lower case

    SYNTAX
        change_case_magic.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input magic format file 
        -F FILE: specify output magic format file , default is to overwrite input file
        -keys KEY1:KEY2 specify colon delimited list of keys to convert
        -[U,l] : specify [U]PPER or [l]ower case, default is lower 

    """
    dir_path = "./"
    change = 'l'
    if '-WD' in sys.argv:
        ind = sys.argv.index('-WD')
        dir_path = sys.argv[ind + 1]
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-f' in sys.argv:
        ind = sys.argv.index('-f')
        magic_file = dir_path + '/' + sys.argv[ind + 1]
    else:
        print(main.__doc__)
        sys.exit()
    if '-F' in sys.argv:
        ind = sys.argv.index('-F')
        out_file = dir_path + '/' + sys.argv[ind + 1]
    else:
        out_file = magic_file
    if '-keys' in sys.argv:
        ind = sys.argv.index('-keys')
        grab_keys = sys.argv[ind + 1].split(":")
    else:
        print(main.__doc__)
        sys.exit()
    if '-U' in sys.argv: change = 'U'
    #
    #
    # get data read in
    Data, file_type = pmag.magic_read(magic_file)
    if len(Data) > 0:
        for grab_key in grab_keys:
            for rec in Data:
                if change == 'l':
                    rec[grab_key] = rec[grab_key].lower()
                else:
                    rec[grab_key] = rec[grab_key].upper()
    else:
        print('bad file name')
    pmag.magic_write(out_file, Data, file_type)
Exemplo n.º 2
0
def main():
    """
    NAME
        measurements_normalize.py
   
    DESCRIPTION
        takes magic_measurements file and normalized moment by sample_weight and sample_volume in the er_specimens table
 
    SYNTAX
        measurements_normalize.py [command line options]

    OPTIONS
        -f FILE: specify input file, default is: magic_measurements.txt
        -fsp FILE: specify input specimen file, default is: er_specimens.txt 
        -F FILE: specify output measurements, default is to overwrite input file

    """
    #
    # initialize variables
    #
    #
    #
    dir_path='.'
    if "-WD" in sys.argv:
        ind=sys.argv.index("-WD")
        dir_path=sys.argv[ind+1]
    meas_file,spec_file= dir_path+"/magic_measurements.txt",dir_path+"/er_specimens.txt"
    out_file=meas_file
    MeasRecs,SpecRecs=[],[]
    OutRecs=[]
    if "-h" in sys.argv:
        print(main.__doc__)
        sys.exit()
    if "-f" in sys.argv:
        ind=sys.argv.index("-f")
        meas_file=dir_path+'/'+sys.argv[ind+1]
    if "-fsp" in sys.argv:
        ind=sys.argv.index("-fsp")
        spec_file=dir_path+'/'+sys.argv[ind+1]
    if "-F" in sys.argv:
        ind=sys.argv.index("-F")
        out_file=dir_path+'/'+sys.argv[ind+1]
    MeasRecs,file_type=pmag.magic_read(meas_file)
    Specs,file_type=pmag.magic_read(spec_file)
    for rec in MeasRecs:
        if 'measurement_magn_moment' in list(rec.keys()) and rec['measurement_magn_moment'] != "":
            for spec in Specs:
                if spec['er_specimen_name']==rec['er_specimen_name']:
                    if 'specimen_weight' in list(spec.keys()) and spec['specimen_weight']!="":
                        rec['measurement_magn_mass']='%e'%(old_div(float(rec['measurement_magn_moment']),float(spec['specimen_weight'])))
                    if 'specimen_volume' in list(spec.keys()) and spec['specimen_volume']!="":
                        rec['measurement_magn_volume']='%e'%(old_div(float(rec['measurement_magn_moment']),float(spec['specimen_volume'])))
                    break
        if 'measurement_magn_volume' not in list(rec.keys()): rec['measurement_magn_volume']=''
        if 'measurement_magn_mass' not in list(rec.keys()): rec['measurement_magn_mass']=''
        OutRecs.append(rec) 
    pmag.magic_write(out_file,OutRecs,"magic_measurements")
    print("Data saved in ", out_file)
Exemplo n.º 3
0
def main():
    """
    NAME 
        change_case_magic.py

    DESCRIPTION
        picks out key and converts to upper or lower case

    SYNTAX
        change_case_magic.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input magic format file 
        -F FILE: specify output magic format file , default is to overwrite input file
        -keys KEY1:KEY2 specify colon delimited list of keys to convert
        -[U,l] : specify [U]PPER or [l]ower case, default is lower 

    """
    dir_path="./"
    change='l'
    if '-WD' in sys.argv: 
        ind=sys.argv.index('-WD')
        dir_path=sys.argv[ind+1]
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        magic_file=dir_path+'/'+sys.argv[ind+1]
    else:
        print(main.__doc__)
        sys.exit()
    if '-F' in sys.argv:
        ind=sys.argv.index('-F')
        out_file=dir_path+'/'+sys.argv[ind+1]
    else: out_file=magic_file
    if '-keys' in sys.argv:
        ind=sys.argv.index('-keys')
        grab_keys=sys.argv[ind+1].split(":")
    else:
        print(main.__doc__)
        sys.exit()
    if '-U' in sys.argv: change='U'
    #
    #
    # get data read in
    Data,file_type=pmag.magic_read(magic_file) 
    if len(Data)>0:
      for grab_key in grab_keys:
        for rec in Data: 
            if change=='l':
                rec[grab_key]=rec[grab_key].lower()
            else:
                rec[grab_key]=rec[grab_key].upper()
    else:
        print('bad file name')
    pmag.magic_write(out_file,Data,file_type)
Exemplo n.º 4
0
def main():
    """
    NAME
        extract_methods.py
 
    DESCRIPTION
        reads in a magic table and creates a file with method codes

    SYNTAX
        extract_methods.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -f FILE: specify magic format input file, default is magic_measurements.txt
        -F FILE: specify method code output file, default is magic_methods.txt
    """
    citation='This study'
    args=sys.argv
    outfile='magic_methods.txt'
    infile='magic_measurements.txt'
#
# get command line arguments
#
    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")
        outfile=args[ind+1]
    if '-f' in args:
        ind=args.index("-f")
        infile=args[ind+1]
    infile=dir_path+'/'+infile
    outfile=dir_path+'/'+outfile
    data,file_type=pmag.magic_read(infile)
    MethRecs=[]
    methods=[]
    for rec in data:
        meths=rec['magic_method_codes'].split(":")
        for meth in meths:
            if meth not in methods:
                MethRec={}
                methods.append(meth)
                MethRec['magic_method_code']=meth
                MethRecs.append(MethRec)
    pmag.magic_write(outfile,MethRecs,'magic_methods')
Exemplo n.º 5
0
def main():
    """
    NAME
        extract_methods.py
 
    DESCRIPTION
        reads in a magic table and creates a file with method codes

    SYNTAX
        extract_methods.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -f FILE: specify magic format input file, default is magic_measurements.txt
        -F FILE: specify method code output file, default is magic_methods.txt
    """
    citation='This study'
    args=sys.argv
    outfile='magic_methods.txt'
    infile='magic_measurements.txt'
#
# get command line arguments
#
    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")
        outfile=args[ind+1]
    if '-f' in args:
        ind=args.index("-f")
        infile=args[ind+1]
    infile=dir_path+'/'+infile
    outfile=dir_path+'/'+outfile
    data,file_type=pmag.magic_read(infile)
    MethRecs=[]
    methods=[]
    for rec in data:
        meths=rec['magic_method_codes'].split(":")
        for meth in meths:
            if meth not in methods:
                MethRec={}
                methods.append(meth)
                MethRec['magic_method_code']=meth
                MethRecs.append(MethRec)
    pmag.magic_write(outfile,MethRecs,'magic_methods')
Exemplo n.º 6
0
def save_redo(SpecRecs, inspec):
    SpecRecs, keys = pmag.fillkeys(SpecRecs)
    pmag.magic_write(inspec, SpecRecs, 'pmag_specimens')
Exemplo n.º 7
0
def main():
    """
    NAME
        trmaq_magic.py

    DESCTIPTION
        does non-linear trm acquisisiton correction
  
    SYNTAX
        trmaq_magic.py [-h][-i][command line options]

    OPTIONS
        -h prints help message and quits
        -i allows interactive setting of file names
        -f MFILE, sets magic_measurements input file
        -ft TSPEC, sets thellier_specimens input file
        -F OUT, sets output for non-linear TRM acquisition corrected data


    DEFAULTS
        MFILE: trmaq_measurements.txt
        TSPEC: thellier_specimens.txt
        OUT: NLT_specimens.txt
    """
    meas_file = 'trmaq_measurements.txt'
    tspec = "thellier_specimens.txt"
    output = 'NLT_specimens.txt'
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-i' in sys.argv:
        meas_file = input(
            "Input magic_measurements file name? [trmaq_measurements.txt] ")
        if meas_file == "": meas_file = "trmaq_measurements.txt"
        tspec = input(
            " thellier_specimens file name? [thellier_specimens.txt] ")
        if tspec == "": tspec = "thellier_specimens.txt"
        output = input(
            "File for non-linear TRM adjusted specimen data: [NLTspecimens.txt] "
        )
        if output == "": output = "NLT_specimens.txt"
    if '-f' in sys.argv:
        ind = sys.argv.index('-f')
        meas_file = sys.argv[ind + 1]
    if '-ft' in sys.argv:
        ind = sys.argv.index('-ft')
        tspec = sys.argv[ind + 1]
    if '-F' in sys.argv:
        ind = sys.argv.index('-F')
        output = sys.argv[ind + 1]
    #
    PLT = {'aq': 1}
    pmagplotlib.plot_init(PLT['aq'], 5, 5)
    #
    # get name of file from command line
    #
    comment = ""
    #
    #
    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()
    sids = pmag.get_specs(meas_data)
    specimen = 0
    #
    # read in thellier_specimen data
    #
    nrm, file_type = pmag.magic_read(tspec)
    PmagSpecRecs = []
    while specimen < len(sids):
        #
        # find corresoponding paleointensity data for this specimen
        #
        s = sids[specimen]
        blab, best = "", ""
        for nrec in nrm:  # pick out the Banc data for this spec
            if nrec["er_specimen_name"] == s:
                blab = float(nrec["specimen_lab_field_dc"])
                best = float(nrec["specimen_int"])
                TrmRec = nrec
                break
        if blab == "":
            print("skipping ", s, " : no best ")
            specimen += 1
        else:
            print(sids[specimen], specimen + 1, 'of ', len(sids), 'Best = ',
                  best * 1e6)
            MeasRecs = []
            #
            # find the data from the meas_data file for this specimen
            #
            for rec in meas_data:
                if rec["er_specimen_name"] == s:
                    meths = rec["magic_method_codes"].split(":")
                    methcodes = []
                    for meth in meths:
                        methcodes.append(meth.strip())
                    if "LP-TRM" in methcodes: MeasRecs.append(rec)
            if len(MeasRecs) < 2:
                specimen += 1
                print('skipping specimen -  no trm acquisition data ', s)
    #
    #  collect info for the PmagSpecRec dictionary
    #
            else:
                TRMs, Bs = [], []
                for rec in MeasRecs:
                    Bs.append(float(rec['treatment_dc_field']))
                    TRMs.append(float(rec['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(PLT['aq'], Bs, TRMs, Bp, Mp, NLpars,
                                    rec['magic_experiment_name'])
                pmagplotlib.drawFIGS(PLT)
                print('Banc= ', float(NLpars['banc']) * 1e6)
                trmTC = {}
                for key in list(TrmRec.keys()):
                    trmTC[key] = TrmRec[
                        key]  # copy of info from thellier_specimens record
                trmTC['specimen_int'] = '%8.3e' % (NLpars['banc'])
                trmTC['magic_method_codes'] = TrmRec[
                    "magic_method_codes"] + ":DA-NL"
                PmagSpecRecs.append(trmTC)
                ans = input("Return for next specimen, s[a]ve plot  ")
                if ans == 'a':
                    Name = {'aq': rec['er_specimen_name'] + '_TRM.svg'}
                    pmagplotlib.saveP(PLT, Name)
                specimen += 1
    pmag.magic_write(output, PmagSpecRecs, 'pmag_specimens')
Exemplo n.º 8
0
def main():
    """
    NAME
        site_edit_magic.py

    DESCRIPTION
       makes equal area projections site by site
         from pmag_specimens.txt file with
         Fisher confidence ellipse using McFadden and McElhinny (1988)
         technique for combining lines and planes
         allows testing and reject specimens for bad orientations

    SYNTAX
        site_edit_magic.py [command line options]

    OPTIONS
       -h: prints help and quits
       -f: specify pmag_specimen format file, default is pmag_specimens.txt
       -fsa: specify er_samples.txt file
       -exc: use existing pmag_criteria.txt file
       -N: reset all sample flags to good
    
    OUPUT
       edited er_samples.txt file

    """
    dir_path='.'
    FIG={} # plot dictionary
    FIG['eqarea']=1 # eqarea is figure 1
    in_file='pmag_specimens.txt'
    sampfile='er_samples.txt'
    out_file=""
    fmt,plot='svg',1
    Crits=""
    M,N=180.,1
    repeat=''
    renew=0
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-WD' in sys.argv:
        ind=sys.argv.index('-WD')
        dir_path=sys.argv[ind+1]
    if '-f' in sys.argv:
        ind=sys.argv.index("-f")
        in_file=sys.argv[ind+1]
    if '-fsa' in sys.argv:
        ind=sys.argv.index("-fsa")
        sampfile=sys.argv[ind+1]
    if '-exc' in sys.argv:
        Crits,file_type=pmag.magic_read(dir_path+'/pmag_criteria.txt')
        for crit in Crits:
            if crit['pmag_criteria_code']=='DE-SPEC':
                M=float(crit['specimen_mad'])
                N=float(crit['specimen_n'])
    if '-fmt' in sys.argv:
        ind=sys.argv.index("-fmt")
        fmt=sys.argv[ind+1]
    if '-N' in sys.argv: renew=1
# 
    if in_file[0]!="/":in_file=dir_path+'/'+in_file
    if sampfile[0]!="/":sampfile=dir_path+'/'+sampfile
    crd='s'
    Specs,file_type=pmag.magic_read(in_file)
    if file_type!='pmag_specimens':
        print ' bad pmag_specimen input file'
        sys.exit()
    Samps,file_type=pmag.magic_read(sampfile)
    if file_type!='er_samples':
        print ' bad er_samples input file'
        sys.exit()
    SO_methods=[]
    for rec in Samps:
       if 'sample_orientation_flag' not in rec.keys(): rec['sample_orientation_flag']='g'
       if 'sample_description' not in rec.keys(): rec['sample_description']=''
       if renew==1:
          rec['sample_orientation_flag']='g'
          description=rec['sample_description']
          if '#' in description:
               newdesc=""
               c=0
               while description[c]!='#' and c<len(description)-1: # look for first pound sign
                   newdesc=newdesc+description[c]
                   c+=1
               while description[c]=='#': 
                   c+=1# skip first set of pound signs
               while description[c]!='#':c+=1 # find second set of pound signs
               while description[c]=='#' and c<len(description)-1:c+=1 # skip second set of pound signs
               while c<len(description)-1: # look for first pound sign
                   newdesc=newdesc+description[c]
                   c+=1
               rec['sample_description']=newdesc # edit out old comment about orientations
       if "magic_method_codes" in rec:
           methlist=rec["magic_method_codes"]
           for meth in methlist.split(":"):
               if "SO" in meth.strip() and "SO-POM" not in meth.strip():
                   if meth.strip() not in SO_methods: SO_methods.append(meth.strip())
    pmag.magic_write(sampfile,Samps,'er_samples')
    SO_priorities=pmag.set_priorities(SO_methods,0)
    sitelist=[]
    for rec in Specs:
        if rec['er_site_name'] not in sitelist: sitelist.append(rec['er_site_name'])
    sitelist.sort()
    EQ={} 
    EQ['eqarea']=1
    pmagplotlib.plot_init(EQ['eqarea'],5,5)
    k=0
    while k<len(sitelist):
        site=sitelist[k]
        print site
        data=[]
        ThisSiteSpecs=pmag.get_dictitem(Specs,'er_site_name',site,'T')
        ThisSiteSpecs=pmag.get_dictitem(ThisSiteSpecs,'specimen_tilt_correction','-1','T') # get all the unoriented data
        for spec in ThisSiteSpecs:
                if spec['specimen_mad']!="" and spec['specimen_n']!="" and float(spec['specimen_mad'])<=M and float(spec['specimen_n'])>=N: 
# good spec, now get orientation....
                    redo,p=1,0
                    if len(SO_methods)<=1:
                        az_type=SO_methods[0]
                        orient=pmag.find_samp_rec(spec["er_sample_name"],Samps,az_type)
                        redo=0
                    while redo==1:
                        if p>=len(SO_priorities):
                            print "no orientation data for ",spec['er_sample_name']
                            orient["sample_azimuth"]=""
                            orient["sample_dip"]=""
                            redo=0
                        else:
                            az_type=SO_methods[SO_methods.index(SO_priorities[p])]
                            orient=pmag.find_samp_rec(spec["er_sample_name"],Samps,az_type)
                            if orient["sample_azimuth"]  !="":
                                redo=0
                        p+=1
                    if orient['sample_azimuth']!="":
                        rec={}
                        for key in spec.keys():rec[key]=spec[key]
                        rec['dec'],rec['inc']=pmag.dogeo(float(spec['specimen_dec']),float(spec['specimen_inc']),float(orient['sample_azimuth']),float(orient['sample_dip']))
                        rec["tilt_correction"]='1'
                        crd='g'
                        rec['sample_azimuth']=orient['sample_azimuth']
                        rec['sample_dip']=orient['sample_dip']
                        data.append(rec)
        if len(data)>2:
            print 'specimen, dec, inc, n_meas/MAD,| method codes '
            for i  in range(len(data)):
                print '%s: %7.1f %7.1f %s / %s | %s' % (data[i]['er_specimen_name'], data[i]['dec'], data[i]['inc'], data[i]['specimen_n'], data[i]['specimen_mad'], data[i]['magic_method_codes'])

            fpars=pmag.dolnp(data,'specimen_direction_type')
            print "\n Site lines planes  kappa   a95   dec   inc"
            print site, fpars["n_lines"], fpars["n_planes"], fpars["K"], fpars["alpha95"], fpars["dec"], fpars["inc"], fpars["R"]
            if out_file!="":
                if float(fpars["alpha95"])<=acutoff and float(fpars["K"])>=kcutoff:
                    out.write('%s %s %s\n'%(fpars["dec"],fpars['inc'],fpars['alpha95']))
            pmagplotlib.plotLNP(EQ['eqarea'],site,data,fpars,'specimen_direction_type')
            pmagplotlib.drawFIGS(EQ)
            if k!=0 and repeat!='y':
                ans=raw_input("s[a]ve plot, [q]uit, [e]dit specimens, [p]revious site, <return> to continue:\n ")
            elif k==0 and repeat!='y':
                ans=raw_input("s[a]ve plot, [q]uit, [e]dit specimens, <return> to continue:\n ")
            if ans=="p": k-=2
            if ans=="a":
                files={}
                files['eqarea']=site+'_'+crd+'_eqarea'+'.'+fmt
                pmagplotlib.saveP(EQ,files)
            if ans=="q": sys.exit()
            if ans=="e" and Samps==[]:
                print "can't edit samples without orientation file, sorry"
            elif ans=="e": 
#                k-=1
                testspec=raw_input("Enter name of specimen to check: ")
                for spec in data:
                    if spec['er_specimen_name']==testspec:
# first test wrong direction of drill arrows (flip drill direction in opposite direction and re-calculate d,i
                        d,i=pmag.dogeo(float(spec['specimen_dec']),float(spec['specimen_inc']),float(spec['sample_azimuth'])-180.,-float(spec['sample_dip']))
                        XY=pmag.dimap(d,i)
                        pmagplotlib.plotXY(EQ['eqarea'],[XY[0]],[XY[1]],sym='g^')
# first test wrong end of compass (take az-180.)
                        d,i=pmag.dogeo(float(spec['specimen_dec']),float(spec['specimen_inc']),float(spec['sample_azimuth'])-180.,float(spec['sample_dip']))
                        XY=pmag.dimap(d,i)
                        pmagplotlib.plotXY(EQ['eqarea'],[XY[0]],[XY[1]],sym='kv')
# did the sample spin in the hole?  
# now spin around specimen's z
                        X_up,Y_up,X_d,Y_d=[],[],[],[]
                        for incr in range(0,360,5):
                            d,i=pmag.dogeo(float(spec['specimen_dec'])+incr,float(spec['specimen_inc']),float(spec['sample_azimuth']),float(spec['sample_dip']))
                            XY=pmag.dimap(d,i)
                            if i>=0:
                                X_d.append(XY[0])
                                Y_d.append(XY[1])
                            else:
                                X_up.append(XY[0])
                                Y_up.append(XY[1])
                        pmagplotlib.plotXY(EQ['eqarea'],X_d,Y_d,sym='b.')
                        pmagplotlib.plotXY(EQ['eqarea'],X_up,Y_up,sym='c.')
                        pmagplotlib.drawFIGS(EQ)
                        break
                print "Triangle: wrong arrow for drill direction."
                print "Delta: wrong end of compass."
                print "Small circle:  wrong mark on sample. [cyan upper hemisphere]"
                deleteme=raw_input("Mark this sample as bad? y/[n]  ")
                if deleteme=='y':
                    reason=raw_input("Reason: [1] broke, [2] wrong drill direction, [3] wrong compass direction, [4] bad mark, [5] displaced block [6] other ")
                    if reason=='1':
                       description=' sample broke while drilling'
                    if reason=='2':
                       description=' wrong drill direction '
                    if reason=='3':
                       description=' wrong compass direction '
                    if reason=='4':
                       description=' bad mark in field'
                    if reason=='5':
                       description=' displaced block'
                    if reason=='6':
                       description=raw_input('Enter brief reason for deletion:   ')
                    for samp in Samps:
                        if samp['er_sample_name']==spec['er_sample_name']:
                            samp['sample_orientation_flag']='b'
                            samp['sample_description']=samp['sample_description']+' ## direction deleted because: '+description+'##' # mark description
                    pmag.magic_write(sampfile,Samps,'er_samples')
                repeat=raw_input("Mark another sample, this site? y/[n]  ")
                if repeat=='y': k-=1
        else:
            print 'skipping site - not enough data with specified coordinate system'
        k+=1 
    print "sample flags stored in ",sampfile
Exemplo n.º 9
0
def main(command_line=True, **kwargs):
    """
    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 -- values in SAMPFILE will override selections for -loc (location), -spc (designate specimen), and -ncn (sample-site naming convention)
        -F FILE: specify output file, default is magic_measurements.txt
        -Fsy: specify er_synthetics file, default is er_sythetics.txt
        -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,..... xxx -A
                where xxx, 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 averaged.
                But users need to make sure that there are no duplicate measurements 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 NOT CURRENTLY SUPPORTED
            [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
    mag_file = None
    codelist = None
    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_infile,Samps='',[]
    trm=0
    irm=0
    specnum=0
    coil=""
    mag_file=""
#
# get command line arguments
#
    meas_file="magic_measurements.txt"
    user=""
    if not command_line:
        user = kwargs.get('user', '')
        meas_file = kwargs.get('meas_file', '')
        syn_file = kwargs.get('syn_file', '')
        mag_file = kwargs.get('mag_file', '')
        labfield = kwargs.get('labfield', '')
        if labfield:
            labfield = float(labfield) *1e-6
        else:
            labfield = 0
        phi = kwargs.get('phi', 0)
        if phi:
            phi = float(phi)
        else:
            phi = 0
        theta = kwargs.get('theta', 0)
        if theta:
            theta=float(theta)
        else:
            theta = 0
        peakfield = kwargs.get('peakfield', 0)
        if peakfield:
            peakfield=float(peakfield) *1e-3
        else:
            peakfield = 0
        specnum = kwargs.get('specnum', 0)
        samp_con = kwargs.get('samp_con', '1')
        er_location_name = kwargs.get('er_location_name', '')
        samp_infile = kwargs.get('samp_infile', '')
        syn = kwargs.get('syn', 0)
        institution = kwargs.get('institution', '')
        syntype = kwargs.get('syntype', '')
        inst = kwargs.get('inst', '')
        noave = kwargs.get('noave', 0)
        codelist = kwargs.get('codelist', '')
        coil = kwargs.get('coil', '')
        cooling_rates = kwargs.get('cooling_rates', '')
    if command_line:
        if "-h" in args:
            print(main.__doc__)
            return False
        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 '-f' in args:
            ind=args.index("-f")
            mag_file=args[ind+1]
        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 "-loc" in args:
            ind=args.index("-loc")
            er_location_name=args[ind+1]
        if "-fsa" in args:
            ind=args.index("-fsa")
            samp_infile = 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 '-LP' in args:
            ind=args.index("-LP")
            codelist=args[ind+1]

        if "-V" in args:
            ind=args.index("-V")
            coil=args[ind+1]


    # make sure all initial values are correctly set up (whether they come from the command line or a GUI)
    if samp_infile:
        Samps, file_type = pmag.magic_read(samp_infile)
    if coil:
        coil = str(coil)
        methcode="LP-IRM"
        irmunits = "V"
        if coil not in ["1","2","3"]:
            print(main.__doc__)
            print('not a valid coil specification')
            return False, '{} is not a valid coil specification'.format(coil)
    if mag_file:
        try:
            #with open(mag_file,'r') as finput:
            #    lines = finput.readlines()
            lines=pmag.open_file(mag_file)
        except:
            print("bad mag file name")
            return False, "bad mag file name"
    if not mag_file:
        print(main.__doc__)
        print("mag_file field is required option")
        return False, "mag_file field is required option"
    if specnum!=0:
        specnum=-specnum
    #print 'samp_con:', samp_con
    if samp_con:
        if "4" == samp_con[0]:
            if "-" not in samp_con:
                print("naming convention option [4] must be in form 4-Z where Z is an integer")
                print('---------------')
                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" == samp_con[0]:
            if "-" not in samp_con:
                print("option [7] must be in form 7-Z where Z is an integer")
                return False, "option [7] must be in form 7-Z where Z is an integer"
            else:
                Z=samp_con.split("-")[1]
                samp_con="7"

    if codelist:
        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=input("DC lab field for ARM step: [50uT] ")
            if ans=="":
                arm_labfield=50e-6
            else:
                arm_labfield=float(ans)*1e-6
            ans=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
            if command_line:
                ind=args.index("CR")
                cooling_rates=args[ind+1]
                cooling_rates_list=cooling_rates.split(',')
            else:
                cooling_rates_list=str(cooling_rates).split(',')
    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 lines:
            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 samp_infile and Samps: # if samp_infile was provided AND yielded sample data
                        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:
                    MagRec["er_specimen_name"]=rec[0]
                    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= cooling_rates_list[-1]
                        else:
                           cooling_time=cooling_rates_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 len(SynRecs)>0:
        pmag.magic_write(synfile,SynRecs,'er_synthetics')
        print("synthetics put in ",synfile)
    return True, meas_file
Exemplo n.º 10
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
Exemplo n.º 11
0
def save_redo(SpecRecs, inspec):
    print("Saving changes to specimen file")
    pmag.magic_write(inspec, SpecRecs, 'specimens')
Exemplo n.º 12
0
def main():
    """
    NAME
        lsq_redo.py

    DESCRIPTION
        converts a tab delimited LSQ format to PmagPy redo file and edits the magic_measurements table to mark "bad" measurements.

    SYNTAX
        lsq_redo.py [-h] [command line options]

    OPTIONS
        -h: prints help message and quits
        -f FILE: specify LSQ input file
        -fm MFILE: specify measurements file for editting, default is
            magic_measurements.txt
        -F FILE: specify output file, default is 'zeq_redo'
    """
    letters=string.ascii_uppercase
    for l in string.ascii_lowercase: letters=letters+l
    dir_path='.'
    if '-WD' in sys.argv:
        ind=sys.argv.index('-WD')
        dir_path=sys.argv[ind+1]
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        inspec=dir_path+'/'+sys.argv[ind+1]
    else:
        zfile=dir_path+'/zeq_redo'
    if '-fm' in sys.argv:
        ind=sys.argv.index('-f')
        meas_file=dir_path+'/'+sys.argv[ind+1]
    else:
        meas_file=dir_path+'/magic_measurements.txt'
    if '-F' in sys.argv:
        ind=sys.argv.index('-F')
        zfile=dir_path+'/'+sys.argv[ind+1]
    else:
        zfile=dir_path+'/zeq_redo'
    try:
        open(meas_file,"r")
        meas_data,file_type=pmag.magic_read(meas_file)
    except IOError:
        print(main.__doc__)
        print("""You must have a valid measurements file prior to converting
                this LSQ file""")
        sys.exit()
    zredo=open(zfile,"w")
    MeasRecs=[]
#
# read in LSQ file
#
    specs,MeasOuts=[],[]
    prior_spec_data=open(inspec,'r').readlines()
    for line in prior_spec_data:
        if len(line)<2:
            sys.exit()
#        spec=line[0:14].strip().replace(" ","") # get out the specimen name = collapsing spaces
#        rec=line[14:].split() # split up the rest of the line
        rec=line.split('\t')
        spec=rec[0].lower()
        specs.append(spec)
        comp_name=rec[2] # assign component name
        calculation_type="DE-FM"
        if rec[1][0]=="L":
            calculation_type="DE-BFL" # best-fit line
        else:
            calculation_type="DE-BFP" # best-fit line
        lists=rec[7].split('-') # get list of data used
        incl=[]
        for l in lists[0]:
            incl.append(letters.index(l))
        for l in letters[letters.index(lists[0][-1])+1:letters.index(lists[1][0])]:
            incl.append(letters.index(l)) # add in the  in between parts
        for l in lists[1]:
            incl.append(letters.index(l))
        if len(lists)>2:
            for l in letters[letters.index(lists[1][-1])+1:letters.index(lists[2][0])]:
               incl.append(letters.index(l)) # add in the  in between parts
            for l in lists[2]:
                incl.append(letters.index(l))
# now find all the data for this specimen in measurements
        datablock,min,max=[],"",""
        demag='N'
        for s in meas_data:
            if s['er_specimen_name'].lower()==spec.lower():
                meths=s['magic_method_codes'].replace(" ","").split(":")
                if 'LT-NO' in meths or 'LT-AF-Z' in meths or 'LT-T-Z' in meths:
                    datablock.append(s)
        if len(datablock)>0:
            for t in datablock:print(t['magic_method_codes'])
            incl_int=len(incl)
            while incl[-1]>len(datablock)-1:
                del incl[-1] # don't include measurements beyond what is in file
            if len(incl)!=incl_int:
                'converting calculation type to best-fit line'
            meths0= datablock[incl[0]]['magic_method_codes'].replace(" ","").split(':')
            meths1= datablock[incl[-1]]['magic_method_codes'].replace(" ","").split(':')
            H0=datablock[incl[0]]['treatment_ac_field']
            T0=datablock[incl[0]]['treatment_temp']
            H1=datablock[incl[-1]]['treatment_ac_field']
            T1=datablock[incl[-1]]['treatment_temp']
            if 'LT-T-Z' in meths1:
               max=T1
               demag="T"
            elif 'LT-AF-Z' in meths1:
               demag="AF"
               max=H1
            if 'LT-NO' in meths0:
                if demag=='T':
                   min=273
                else:
                   min=0
            elif 'LT-T-Z' in meths0:
                min=T0
            else:
                min=H0
            for ind in range(incl[0]):
                MeasRecs.append(datablock[ind])
            for ind in range(incl[0],incl[-1]):
                if ind not in incl: # datapoint not used in calculation
                    datablock[ind]['measurement_flag']='b'
                MeasRecs.append(datablock[ind])
            for ind in range(incl[-1],len(datablock)):
                MeasRecs.append(datablock[ind])
            outstring='%s %s %s %s %s \n'%(spec,calculation_type,min,max,comp_name)
            zredo.write(outstring)
    for s in meas_data: # collect the rest of the measurement data not already included
        if s['er_specimen_name'] not in specs:
            MeasRecs.append(s)
    pmag.magic_write(meas_file,MeasRecs,'magic_measurements')  # write out annotated measurements
Exemplo n.º 13
0
def main():
    """
    NAME
        agm_magic.py

    DESCRIPTION
        converts Micromag agm files to magic format

    SYNTAX
        agm_magic.py [-h] [command line options]

    OPTIONS
        -usr USER:   identify user, default is "" - put in quotation marks!
        -bak:  this is a IRM backfield curve
        -f FILE, specify input file, required
        -fsa SAMPFILE, specify er_samples.txt file relating samples, site and locations names,default is none
        -F MFILE, specify magic measurements formatted output file, default is agm_measurements.txt
        -spn SPEC, specimen name, default is base of input file name, e.g. SPECNAME.agm
        -spc NUM, specify number of characters to designate a  specimen, default = 0
        -Fsp SPECFILE : name of er_specimens.txt file for appending data to
             [default: er_specimens.txt]
        -ncn NCON,: specify naming convention: default is #1 below
        -syn SYN,  synthetic specimen name
        -loc LOCNAME : specify location/study name,
             should have either LOCNAME or SAMPFILE (unless synthetic)
        -ins INST : specify which instrument was used (e.g, SIO-Maud), default is ""
        -u units:  [cgs,SI], default is cgs
       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 -- NOT CURRENTLY SUPPORTED
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            [8] specimen is a synthetic - it has no sample, site, location information
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.

    OUTPUT
        MagIC format files: magic_measurements, er_specimens, er_sample, er_site
    """
    citation='This study'
    MeasRecs=[]
    units='cgs'
    meth="LP-HYS"
    version_num=pmag.get_version()
    args=sys.argv
    fmt='old'
    er_sample_name,er_site_name,er_location_name="","",""
    inst=""
    er_location_name="unknown"
    er_synthetic_name=""
    user=""
    er_site_name=""
    dir_path='.'
    dm=3
    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
    specfile = output_dir_path+'/er_specimens.txt'
    output = output_dir_path+"/agm_measurements.txt"
    if "-h" in args:
        print(main.__doc__)
        sys.exit()
    if "-bak" in args:
        meth="LP-IRM-DCD"
        output = output_dir_path+"/irm_measurements.txt"
    if "-new" in args: fmt='new'
    if "-usr" in args:
        ind=args.index("-usr")
        user=args[ind+1]
    if '-F' in args:
        ind=args.index("-F")
        output = output_dir_path+'/'+args[ind+1]
    if '-f' in args:
        ind=args.index("-f")
        agm_file= input_dir_path+'/'+args[ind+1]
        er_specimen_name=args[ind+1].split('.')[0]
    else:
        print("agm_file field is required option")
        print(main.__doc__)
        sys.exit()
    if '-Fsp' in args:
        ind=args.index("-Fsp")
        specfile= output_dir_path+'/'+args[ind+1]
    specnum,samp_con,Z=0,'1',1
    if "-spc" in args:
        ind=args.index("-spc")
        specnum=int(args[ind+1])
        if specnum!=0:specnum=-specnum
    if "-spn" in args:
        ind=args.index("-spn")
        er_specimen_name=args[ind+1]
    #elif "-syn" not in args:
    #    print "you must specify a specimen name"
    #    sys.exit()
    if "-syn" in args:
        ind=args.index("-syn")
        er_synthetic_name=args[ind+1]
        er_specimen_name=""
    if "-loc" in args:
        ind=args.index("-loc")
        er_location_name=args[ind+1]
    if "-fsa" in args:
        ind=args.index("-fsa")
        sampfile = input_dir_path+'/'+args[ind+1]
        Samps,file_type=pmag.magic_read(sampfile)
        print('sample_file successfully read in')
    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 "-ins" in args:
        ind=args.index("-ins")
        inst=args[ind+1]
    if "-u" in args:
        ind=args.index("-u")
        units=args[ind+1]
    dm = pmag.get_named_arg_from_sys("-DM", 2)
    ErSpecRecs,filetype=pmag.magic_read(specfile)
    ErSpecRec,MeasRec={},{}
    ErSpecRec['er_citation_names']="This study"
    ErSpecRec['er_specimen_name']=er_specimen_name
    ErSpecRec['er_synthetic_name']=er_synthetic_name
    if specnum!=0:
        ErSpecRec["er_sample_name"]=er_specimen_name[:specnum]
    else:
        ErSpecRec["er_sample_name"]=er_specimen_name
    if "-fsa" in args and er_synthetic_name=="":
        for samp in Samps:
            if samp["er_sample_name"] == ErSpecRec["er_sample_name"]:
                ErSpecRec["er_location_name"]=samp["er_location_name"]
                ErSpecRec["er_site_name"]=samp["er_site_name"]
                break
    elif int(samp_con)!=6 and int(samp_con)!=8:
        site=pmag.parse_site(ErSpecRec['er_sample_name'],samp_con,Z)
        ErSpecRec["er_site_name"]=site
        ErSpecRec["er_location_name"]=er_location_name
    ErSpecRec['er_scientist_mail_names']=user.strip()
    insert=1
    for rec in ErSpecRecs:
        if rec['er_specimen_name']==er_specimen_name:
            insert=0
            break
    if insert==1:
        ErSpecRecs.append(ErSpecRec)
        ErSpecRecs,keylist=pmag.fillkeys(ErSpecRecs)
        pmag.magic_write(specfile,ErSpecRecs,'er_specimens')
        print("specimen name put in ",specfile)
    f=open(agm_file,'r')
    Data=f.readlines()
    if "ASCII" not in Data[0]:fmt='new'
    measnum,start=1,""
    if fmt=='new': # new Micromag formatted file
        end=2
        for skip in range(len(Data)):
            line=Data[skip]
            rec=line.split()
            if 'Units' in line:units=rec[-1]
            if "Raw" in rec:
                start=skip+2
            if "Field" in rec and "Moment" in rec and start=="":
                start=skip+2
                break
    else:
        start = 2
        end=1
    for i in range(start,len(Data)-end): # skip header stuff

        MeasRec={}
        for key in list(ErSpecRec.keys()):
            MeasRec[key]=ErSpecRec[key]
        MeasRec['magic_instrument_codes']=inst
        MeasRec['magic_method_codes']=meth
        if 'er_synthetic_name' in list(MeasRec.keys()) and MeasRec['er_synthetic_name']!="":
            MeasRec['magic_experiment_name']=er_synthetic_name+':'+meth
        else:
            MeasRec['magic_experiment_name']=er_specimen_name+':'+meth
        line=Data[i]
        rec=line.split(',') # data comma delimited
        if rec[0]!='\n':
            if units=='cgs':
                field =float(rec[0])*1e-4 # convert from oe to tesla
            else:
                field =float(rec[0]) # field in tesla
            if meth=="LP-HYS":
                MeasRec['measurement_lab_field_dc']='%10.3e'%(field)
                MeasRec['treatment_dc_field']=''
            else:
                MeasRec['measurement_lab_field_dc']=''
                MeasRec['treatment_dc_field']='%10.3e'%(field)
            if units=='cgs':
                MeasRec['measurement_magn_moment']='%10.3e'%(float(rec[1])*1e-3) # convert from emu to Am^2
            else:
                MeasRec['measurement_magn_moment']='%10.3e'%(float(rec[1])) # Am^2
            MeasRec['treatment_temp']='273' # temp in kelvin
            MeasRec['measurement_temp']='273' # temp in kelvin
            MeasRec['measurement_flag']='g'
            MeasRec['measurement_standard']='u'
            MeasRec['measurement_number']='%i'%(measnum)
            measnum+=1
            MeasRec['magic_software_packages']=version_num
            MeasRecs.append(MeasRec)
# now we have to relabel LP-HYS method codes.  initial loop is LP-IMT, minor loops are LP-M  - do this in measurements_methods function
    if meth=='LP-HYS':
        recnum=0
        while float(MeasRecs[recnum]['measurement_lab_field_dc'])<float(MeasRecs[recnum+1]['measurement_lab_field_dc']) and recnum+1<len(MeasRecs): # this is LP-IMAG
            MeasRecs[recnum]['magic_method_codes']='LP-IMAG'
            MeasRecs[recnum]['magic_experiment_name']=MeasRecs[recnum]['er_specimen_name']+":"+'LP-IMAG'
            recnum+=1
#
    if int(dm)==2:
        pmag.magic_write(output,MeasRecs,'magic_measurements')
    else:
        print ('MagIC 3 is not supported yet')
        sys.exit()
        pmag.magic_write(output,MeasRecs,'measurements')

    print("results put in ", output)
Exemplo n.º 14
0
def main(command_line=True, **kwargs):
    """
    NAME
        _2g_bin_magic.py
   
    DESCRIPTION
        takes the binary 2g format magnetometer files and converts them to magic_measurements, er_samples.txt and er_sites.txt file
 
    SYNTAX
        2g_bin_magic.py [command line options]

    OPTIONS
        -f FILE: specify input 2g (binary) file
        -F FILE: specify magic_measurements output file, default is: magic_measurements.txt
        -Fsa FILE: specify output file, default is: er_samples.txt 
        -Fsi FILE: specify output file, default is: er_sites.txt 
        -ncn NCON:  specify naming convention: default is #2 below
        -ocn OCON:  specify orientation convention, default is #5 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
             SO-SUN   orientation with sun compass
        -loc: location name, default="unknown"
        -spc NUM : specify number of characters to designate a  specimen, default = 0     
        -ins INST : specify instsrument name
        -a: average replicate measurements

    INPUT FORMAT
        Input files are horrible mag binary format (who knows why?)
        Orientation convention:
            [1] Lab arrow azimuth= mag_azimuth; Lab arrow dip=-field_dip
                i.e., field_dip is degrees from vertical down - the hade [default]
            [2] Lab arrow azimuth = mag_azimuth-90; Lab arrow dip = -field_dip
                i.e., mag_azimuth is strike and field_dip is hade
            [3] Lab arrow azimuth = mag_azimuth; Lab arrow dip = 90-field_dip
                i.e.,  lab arrow same as field arrow, but field_dip was a hade.
            [4] lab azimuth and dip are same as mag_azimuth, field_dip
            [5] lab azimuth is same as mag_azimuth,lab arrow dip=field_dip-90
            [6] Lab arrow azimuth = mag_azimuth-90; Lab arrow dip = 90-field_dip
            [7] all others you will have to either customize your 
                self or e-mail [email protected] for help.  
 
         Magnetic declination convention:
             Az will use supplied declination to correct azimuth 
    
       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 = sample name
        [6] site name entered in site_name column in the orient.txt format input file  -- NOT CURRENTLY SUPPORTED
        [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
        NB: all others you will have to either customize your
            self or e-mail [email protected] for help.

    OUTPUT
            output saved in magic_measurements.txt & er_samples.txt formatted files
              will overwrite any existing files 
    """
    #
    # initialize variables
    #
    mag_file = ''
    specnum = 0
    ub_file, samp_file, or_con, corr, meas_file = "", "er_samples.txt", "3", "1", "magic_measurements.txt"
    pos_file, site_file = "", "er_sites.txt"
    noave = 1
    args = sys.argv
    bed_dip, bed_dip_dir = "", ""
    samp_con, Z, average_bedding = "2", 1, "0"
    meths = 'FS-FD'
    sclass, lithology, _type = "", "", ""
    user, inst = "", ""
    DecCorr = 0.
    location_name = "unknown"
    months = [
        'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct',
        'Nov', 'Dec'
    ]
    gmeths = ""
    #
    #
    dir_path = '.'
    if command_line:
        if '-WD' in args:
            ind = args.index("-WD")
            dir_path = sys.argv[ind + 1]
        if "-h" in args:
            print(main.__doc__)
            return False
        if "-f" in args:
            ind = args.index("-f")
            mag_file = sys.argv[ind + 1]
        if "-fpos" in args:
            ind = args.index("-fpos")
            pos_file = sys.argv[ind + 1]
        if "-F" in args:
            ind = args.index("-F")
            meas_file = sys.argv[ind + 1]
        if "-Fsa" in args:
            ind = args.index("-Fsa")
            samp_file = sys.argv[ind + 1]
        if "-Fsi" in args:
            ind = args.index("-Fsi")
            site_file = sys.argv[ind + 1]
        if "-ocn" in args:
            ind = args.index("-ocn")
            or_con = sys.argv[ind + 1]
        if "-ncn" in args:
            ind = args.index("-ncn")
            samp_con = sys.argv[ind + 1]
        if "-mcd" in args:
            ind = args.index("-mcd")
            gmeths = (sys.argv[ind + 1])
        if "-loc" in args:
            ind = args.index("-loc")
            location_name = (sys.argv[ind + 1])
        if "-spc" in args:
            ind = args.index("-spc")
            specnum = int(args[ind + 1])

        if "-ins" in args:
            ind = args.index("-ins")
            inst = args[ind + 1]
        if "-a" in args:
            noave = 0
        #
        ID = False
        if '-ID' in args:
            ind = args.index('-ID')
            ID = args[ind + 1]
        #

    if not command_line:
        dir_path = kwargs.get('dir_path', '.')
        mag_file = kwargs.get('mag_file', '')
        pos_file = kwargs.get('pos_file', '')
        meas_file = kwargs.get('meas_file', 'magic_measurements.txt')
        samp_file = kwargs.get('samp_file', 'er_samples.txt')
        site_file = kwargs.get('site_file', 'er_sites.txt')
        or_con = kwargs.get('or_con', '3')
        samp_con = kwargs.get('samp_con', '2')
        corr = kwargs.get('corr', '1')
        gmeths = kwargs.get('gmeths', '')
        location_name = kwargs.get('location_name', '')
        specnum = int(kwargs.get('specnum', 0))
        inst = kwargs.get('inst', '')
        noave = kwargs.get('noave', 1)  # default is DO average
        ID = kwargs.get('ID', '')

    # format and fix variables acquired from command line args or input with **kwargs
    if specnum != 0: specnum = -specnum

    if ID:
        input_dir_path = ID
    else:
        input_dir_path = dir_path

    if samp_con:
        if "4" in samp_con:
            if "-" not in samp_con:
                print("option [4] must be in form 4-Z where Z is an integer")
                return False, "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, "option [7] must be in form 7-Z where Z is an integer"
            else:
                Z = samp_con.split("-")[1]
                samp_con = "7"
        if "6" in samp_con:
            try:
                Samps, file_type = pmag.magic_read(
                    os.path.join(input_dir_path, 'er_samples.txt'))
            except:
                print(
                    "there is no er_samples.txt file in your input directory - you can't use naming convention #6"
                )
                return False, "there is no er_samples.txt file in your input directory - you can't use naming convention #6"
            if file_type == 'bad_file':
                print(
                    "there is no er_samples.txt file in your input directory - you can't use naming convention #6"
                )
                return False, "there is no er_samples.txt file in your input directory - you can't use naming convention #6"

    if not mag_file:
        print("mag file is required input")
        return False, "mag file is required input"
    output_dir_path = dir_path
    mag_file = os.path.join(input_dir_path, mag_file)
    samp_file = output_dir_path + '/' + samp_file
    site_file = output_dir_path + '/' + site_file
    meas_file = output_dir_path + '/' + meas_file
    samplist = []
    try:
        Samps, file_type = pmag.magic_read(samp_file)
        for samp in Samps:
            if samp['er_sample_name'] not in samplist:
                samplist.append(samp['er_sample_name'])
    except:
        Samps = []
    MagRecs = []
    try:
        f = open(mag_file, 'brU')
        input = str(f.read()).strip("b '")
        f.close()
    except Exception as ex:
        print('ex', ex)
        print("bad mag file")
        return False, "bad mag file"
    firstline, date = 1, ""
    d = input.split('\\xcd')
    for line in d:
        rec = line.split('\\x00')
        if firstline == 1:
            firstline = 0
            spec, vol = "", 1
            el = 51
            while line[el:el + 1] != "\\":
                spec = spec + line[el]
                el += 1
            # check for bad sample name
            test = spec.split('.')
            date = ""
            if len(test) > 1:
                spec = test[0]
                kk = 24
                while line[kk] != '\\x01' and line[kk] != '\\x00':
                    kk += 1
                vcc = line[24:kk]
                el = 10
                while rec[el].strip() != '':
                    el += 1
                date, comments = rec[el + 7], []
            else:
                el = 9
                while rec[el] != '\\x01':
                    el += 1
                vcc, date, comments = rec[el - 3], rec[el + 7], []
            specname = spec.lower()
            print('importing ', specname)
            el += 8
            while rec[el].isdigit() == False:
                comments.append(rec[el])
                el += 1
            while rec[el] == "":
                el += 1
            az = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            pl = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            bed_dip_dir = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            bed_dip = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            if rec[el] == '\\x01':
                bed_dip = 180. - bed_dip
                el += 1
                while rec[el] == "":
                    el += 1
            fold_az = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            fold_pl = rec[el]
            el += 1
            while rec[el] == "":
                el += 1
            if rec[el] != "" and rec[el] != '\\x02' and rec[el] != '\\x01':
                deccorr = float(rec[el])
                az += deccorr
                bed_dip_dir += deccorr
                fold_az += deccorr
                if bed_dip_dir >= 360: bed_dip_dir = bed_dip_dir - 360.
                if az >= 360.: az = az - 360.
                if fold_az >= 360.: fold_az = fold_az - 360.
            else:
                deccorr = 0
            if specnum != 0:
                sample = specname[:specnum]
            else:
                sample = specname
            SampRec = {}
            SampRec["er_sample_name"] = sample
            SampRec["er_location_name"] = location_name
            SampRec["er_citation_names"] = "This study"
            labaz, labdip = pmag.orient(az, pl,
                                        or_con)  # convert to labaz, labpl
            #
            # parse information common to all orientation methods
            #
            SampRec["sample_bed_dip"] = '%7.1f' % (bed_dip)
            SampRec["sample_bed_dip_direction"] = '%7.1f' % (bed_dip_dir)
            SampRec["sample_dip"] = '%7.1f' % (labdip)
            SampRec["sample_azimuth"] = '%7.1f' % (labaz)
            if vcc.strip() != "":
                vol = float(vcc) * 1e-6  # convert to m^3 from cc
            SampRec["sample_volume"] = '%10.3e' % (vol)  #
            SampRec["sample_class"] = sclass
            SampRec["sample_lithology"] = lithology
            SampRec["sample_type"] = _type
            SampRec["sample_declination_correction"] = '%7.1f' % (deccorr)
            methods = gmeths.split(':')
            if deccorr != "0":
                if 'SO-MAG' in methods: del methods[methods.index('SO-MAG')]
                methods.append('SO-CMD-NORTH')
            meths = ""
            for meth in methods:
                meths = meths + meth + ":"
            meths = meths[:-1]
            SampRec["magic_method_codes"] = meths
            if int(samp_con) < 6 or int(samp_con) == 7:
                site = pmag.parse_site(SampRec["er_sample_name"], samp_con,
                                       Z)  # parse out the site name
                SampRec["er_site_name"] = site
            elif len(Samps) > 1:
                site, location = "", ""
                for samp in Samps:
                    if samp["er_sample_name"] == SampRec["er_sample_name"]:
                        site = samp["er_site_name"]
                        location = samp["er_location_name"]
                        break
                SampRec["er_location_name"] = samp["er_location_name"]
                SampRec["er_site_name"] = samp["er_site_name"]
            if sample not in samplist:
                samplist.append(sample)
                Samps.append(SampRec)
        else:
            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"] = specname
            MagRec["er_sample_name"] = SampRec['er_sample_name']
            MagRec["er_site_name"] = SampRec['er_site_name']
            MagRec["er_location_name"] = location_name
            el, demag = 1, ''
            treat = rec[el]
            if treat[-1] == 'C':
                demag = 'T'
            elif treat != 'NRM':
                demag = 'AF'
            el += 1
            while rec[el] == "":
                el += 1
            MagRec["measurement_dec"] = rec[el]
            cdec = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            MagRec["measurement_inc"] = rec[el]
            cinc = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            gdec = rec[el]
            el += 1
            while rec[el] == "":
                el += 1
            ginc = rec[el]
            el = skip(2, el, rec)  # skip bdec,binc
            #                el=skip(4,el,rec) # skip gdec,ginc,bdec,binc
            #                print 'moment emu: ',rec[el]
            MagRec["measurement_magn_moment"] = '%10.3e' % (
                float(rec[el]) * 1e-3)  # moment in Am^2 (from emu)
            MagRec["measurement_magn_volume"] = '%10.3e' % (
                float(rec[el]) * 1e-3 / vol)  # magnetization in A/m
            el = skip(2, el, rec)  # skip to xsig
            MagRec["measurement_sd_x"] = '%10.3e' % (float(rec[el]) * 1e-3
                                                     )  # convert from emu
            el = skip(3, el, rec)  # skip to ysig
            MagRec["measurement_sd_y"] = '%10.3e' % (float(rec[el]) * 1e-3
                                                     )  # convert from emu
            el = skip(3, el, rec)  # skip to zsig
            MagRec["measurement_sd_z"] = '%10.3e' % (float(rec[el]) * 1e-3
                                                     )  # convert from emu
            el += 1  # skip to positions
            MagRec["measurement_positions"] = rec[el]
            #                    el=skip(5,el,rec) # skip to date
            #                    mm=str(months.index(date[0]))
            #                    if len(mm)==1:
            #                        mm='0'+str(mm)
            #                    else:
            #                        mm=str(mm)
            #                    dstring=date[2]+':'+mm+':'+date[1]+":"+date[3]
            #                    MagRec['measurement_date']=dstring
            MagRec["magic_instrument_codes"] = inst
            MagRec["er_analyst_mail_names"] = ""
            MagRec["er_citation_names"] = "This study"
            MagRec["magic_method_codes"] = meas_type
            if demag == "AF":
                MagRec["treatment_ac_field"] = '%8.3e' % (
                    float(treat[:-2]) * 1e-3)  # peak field in tesla
                meas_type = "LT-AF-Z"
                MagRec["treatment_dc_field"] = '0'
            elif demag == "T":
                MagRec["treatment_temp"] = '%8.3e' % (float(treat[:-1]) + 273.
                                                      )  # temp in kelvin
                meas_type = "LT-T-Z"
            MagRec['magic_method_codes'] = meas_type
            MagRecs.append(MagRec)
    MagOuts = pmag.measurements_methods(MagRecs, noave)
    MagOuts, keylist = pmag.fillkeys(MagOuts)
    pmag.magic_write(meas_file, MagOuts, 'magic_measurements')
    print("Measurements put in ", meas_file)
    SampsOut, sampkeys = pmag.fillkeys(Samps)
    pmag.magic_write(samp_file, SampsOut, "er_samples")
    Sites = []
    for samp in Samps:
        SiteRec = {}
        SiteRec['er_site_name'] = samp['er_site_name']
        SiteRec['er_location_name'] = samp['er_location_name']
        SiteRec['site_definition'] = 's'
        SiteRec['er_citation_names'] = 'This study'
        if 'sample_class' in list(samp.keys()):
            SiteRec['site_class'] = samp['sample_class']
        if 'sample_lithology' in list(samp.keys()):
            SiteRec['site_lithology'] = samp['sample_lithology']
        if 'sample_type' in list(samp.keys()):
            SiteRec['site_lithology'] = samp['sample_lithology']
        if 'sample_lat' in list(samp.keys()):
            SiteRec['site_lat'] = samp['sample_lat']
        else:
            SiteRec['site_lat'] = "-999"
        if 'sample_lon' in list(samp.keys()):
            SiteRec['site_lon'] = samp['sample_lon']
        else:
            SiteRec['site_lon'] = "-999"
        if 'sample_height' in list(samp.keys()):
            SiteRec['site_height'] = samp['sample_height']
        Sites.append(SiteRec)
    pmag.magic_write(site_file, Sites, 'er_sites')
    return True, meas_file
Exemplo n.º 15
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=.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 list(critrec.keys()): # accommodate Shaar's new criterion
                    critrec['sample_int_sigma']='%10.3e'%(eval(critrec['sample_int_sigma_uT'])*1e-6)
            for key in list(critrec.keys()):
                if key not in list(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,'r')
    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 list(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 list(rec.keys()):PmagSpecRec["er_expedition_name"]=rec["er_expedition_name"]
            if "magic_instrument_codes" not in list(rec.keys()):
                PmagSpecRec["magic_instrument_codes"]="unknown"
            else:
                PmagSpecRec["magic_instrument_codes"]=rec["magic_instrument_codes"]
            if "magic_experiment_name" not in list(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 list(pars.keys()): PmagSpecRec['specimen_scat']=pars['specimen_scat']
                    if 'specimen_frac' in list(pars.keys()): PmagSpecRec['specimen_frac']='%5.3f'%(pars['specimen_frac'])
                    if 'specimen_gmax' in list(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_gamma"]='%7.1f'%(pars["specimen_gamma"])
                    if pars["magic_method_codes"]!="" and pars["magic_method_codes"] not in methcodes: methcodes.append(pars["magic_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 list(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 list(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)
Exemplo n.º 16
0
def main():
    """
    NAME
        trmaq_magic.py

    DESCTIPTION
        does non-linear trm acquisisiton correction

    SYNTAX
        trmaq_magic.py [-h][-i][command line options]

    OPTIONS
        -h prints help message and quits
        -i allows interactive setting of file names
        -f MFILE, sets magic_measurements input file
        -ft TSPEC, sets thellier_specimens input file
        -F OUT, sets output for non-linear TRM acquisition corrected data
        -sav save figures and quit
        -fmt [png, svg, pdf]
        -DM [2, 3] MagIC data model, default 3


    DEFAULTS
        MFILE: trmaq_measurements.txt
        TSPEC: thellier_specimens.txt
        OUT: NLT_specimens.txt
    """
    meas_file = 'trmaq_measurements.txt'
    tspec = "thellier_specimens.txt"
    output = 'NLT_specimens.txt'
    data_model_num = int(float(pmag.get_named_arg("-DM", 3)))
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-i' in sys.argv:
        meas_file = input(
            "Input magic_measurements file name? [trmaq_measurements.txt] ")
        if meas_file == "":
            meas_file = "trmaq_measurements.txt"
        tspec = input(
            " thellier_specimens file name? [thellier_specimens.txt] ")
        if tspec == "":
            tspec = "thellier_specimens.txt"
        output = input(
            "File for non-linear TRM adjusted specimen data: [NLTspecimens.txt] ")
        if output == "":
            output = "NLT_specimens.txt"
    if '-f' in sys.argv:
        ind = sys.argv.index('-f')
        meas_file = sys.argv[ind+1]
    if '-ft' in sys.argv:
        ind = sys.argv.index('-ft')
        tspec = sys.argv[ind+1]
    if '-F' in sys.argv:
        ind = sys.argv.index('-F')
        output = sys.argv[ind+1]
    if '-sav' in sys.argv:
        save_plots = True
    else:
        save_plots = False
    fmt = pmag.get_named_arg("-fmt", "svg")
    #
    PLT = {'aq': 1}
    if not save_plots:
        pmagplotlib.plot_init(PLT['aq'], 5, 5)
    #
    # get name of file from command line
    #
    comment = ""
    #
    #
    meas_data, file_type = pmag.magic_read(meas_file)
    if 'measurements' not in file_type:
        print(file_type, "This is not a valid measurements file ")
        sys.exit()

    if data_model_num == 2:
        spec_col = "er_specimen_name"
        lab_field_dc_col = "specimen_lab_field_dc"
        int_col = "specimen_int"
        meth_col = "magic_method_codes"
        treat_dc_col = "treatment_dc_field"
        magn_moment_col = "measurement_magn_moment"
        experiment_col = "magic_experiment_name"
        outfile_type = "pmag_specimens"
    else:
        spec_col = "specimen"
        lab_field_dc_col = "int_treat_dc_field"
        int_col = "int_abs"
        meth_col = "method_codes"
        treat_dc_col = "treat_dc_field"
        magn_moment_col = "magn_moment"
        experiment_col = "experiment"
        outfile_type = "specimens"

    sids = pmag.get_specs(meas_data)
    specimen = 0
    #
    # read in thellier_specimen data
    #
    nrm, file_type = pmag.magic_read(tspec)
    PmagSpecRecs= []
    while specimen < len(sids):
        #
        # find corresoponding paleointensity data for this specimen
        #
        s = sids[specimen]
        blab, best = "", ""
        for nrec in nrm:   # pick out the Banc data for this spec
            if nrec[spec_col] == s:
                try:
                    blab = float(nrec[lab_field_dc_col])
                except ValueError:
                    continue
                best = float(nrec[int_col])
                TrmRec = nrec
                break
        if blab == "":
            print("skipping ", s, " : no best ")
            specimen += 1
        else:
            print(sids[specimen], specimen+1, 'of ',
                  len(sids), 'Best = ', best*1e6)
            MeasRecs = []
    #
    # find the data from the meas_data file for this specimen
    #
            for rec in meas_data:
                if rec[spec_col] == s:
                    meths = rec[meth_col].split(":")
                    methcodes = []
                    for meth in meths:
                        methcodes.append(meth.strip())
                    if "LP-TRM" in methcodes:
                        MeasRecs.append(rec)
            if len(MeasRecs) < 2:
                specimen += 1
                print('skipping specimen -  no trm acquisition data ', s)
    #
    #  collect info for the PmagSpecRec dictionary
    #
            else:
                TRMs, Bs = [], []
                for rec in MeasRecs:
                    Bs.append(float(rec[treat_dc_col]))
                    TRMs.append(float(rec[magn_moment_col]))
                # calculate best fit parameters through TRM acquisition data, and get new banc
                NLpars = nlt.NLtrm(Bs, TRMs, best, blab, 0)
    #
                Mp, Bp = [], []
                for k in range(int(max(Bs)*1e6)):
                    Bp.append(float(k)*1e-6)
                    # predicted NRM for this field
                    npred = nlt.TRM(Bp[-1], NLpars['xopt']
                                    [0], NLpars['xopt'][1])
                    Mp.append(npred)
                pmagplotlib.plot_trm(
                    PLT['aq'], Bs, TRMs, Bp, Mp, NLpars, rec[experiment_col])
                if not save_plots:
                    pmagplotlib.draw_figs(PLT)
                print('Banc= ', float(NLpars['banc'])*1e6)
                trmTC = {}
                for key in list(TrmRec.keys()):
                    # copy of info from thellier_specimens record
                    trmTC[key] = TrmRec[key]
                trmTC[int_col] = '%8.3e' % (NLpars['banc'])
                trmTC[meth_col] = TrmRec[meth_col]+":DA-NL"
                PmagSpecRecs.append(trmTC)
                if not save_plots:
                    ans = input("Return for next specimen, s[a]ve plot  ")
                    if ans == 'a':
                        Name = {'aq': rec[spec_col]+'_TRM.{}'.format(fmt)}
                        pmagplotlib.save_plots(PLT, Name)
                else:
                    Name = {'aq': rec[spec_col]+'_TRM.{}'.format(fmt)}
                    pmagplotlib.save_plots(PLT, Name)

                specimen += 1

    pmag.magic_write(output, PmagSpecRecs, outfile_type)
Exemplo n.º 17
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] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name = sample name
            [6] site name entered in site_name column in the orient.txt format input file  -- NOT CURRENTLY SUPPORTED
            [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
            NB: 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,'r')
    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)
Exemplo n.º 18
0
def main(command_line=True, **kwargs):
    """
    NAME
        cit_magic.py

    DESCRIPTION
        converts CIT and .sam  format files to magic_measurements format files

    SYNTAX
        cit_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify .sam format input file, required
        -WD Working Directory: output directory where files are going to go
        -fsi SITEFILE : specify file with site names and locations [tab delimited magic file]
        -F FILE: specify output  measurements file, default is magic_measurements.txt
        -Fsp FILE: specify output er_specimens.txt file, default is er_specimens.txt
        -Fsi FILE: specify output er_sites.txt file, default is er_sites.txt
        -Fsa FILE: specify output er_samples.txt file, default is er_samples.txt  # LORI
        -n [gm,kg,cc,m3]: specify normalization
        -A: don't average replicate measurements
        -spc NUM : specify number of characters to designate a  specimen, default = 0
        -ncn NCON: specify naming convention
        -loc LOCNAME : specify location/study name, must have either LOCNAME or SITEFILE or be a synthetic
        -mcd [FS-FD:SO-MAG,.....] colon delimited list for method codes applied to all specimens in .sam file
        -dc B PHI THETA: dc lab field (in microTesla), phi,and theta must be input as a tuple "(DC,PHI,THETA)". If not input user will be asked for values, this is advantagious if there are differing dc fields between steps or specimens. Note: this currently only works with the decimal IZZI naming convetion (XXX.0,1,2 where XXX is the treatment temperature and 0 is a zero field step, 1 is in field, and 2 is a pTRM check). All other steps are hardcoded dc_field = 0.

    INPUT
        Best to put separate experiments in separate files (all AF, thermal, thellier, trm aquisition, Shaw, etc.)

    NOTES:
         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: default] 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 = sample name
        [6] site name entered in site_name column in the orient.txt format input file  -- NOT CURRENTLY SUPPORTED
        [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
        NB: all others you will have to either customize your
            self or e-mail [email protected] or [email protected] for help.
    """

#
#              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
    #
    #initialize variables
    norm='cc'
    samp_con,Z='3',1
    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=[],[],[],[],[]
    MeasRecs=[]
    specnum,units,locname=0,"1","unknown"
    citation="This study"
    dir_path='.'
    args=sys.argv
    if command_line:
        if '-WD' in args:
            ind=args.index("-WD")
            dir_path=args[ind+1]
        if "-h" in args:
            print(main.__doc__)
            return False
        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 '-Fsp' in args:
            ind=args.index("-Fsp")
            spec_file=args[ind+1]
        if '-Fsa' in args:
            ind=args.index("-Fsa")
            samp_file=args[ind+1]
        if '-Fsi' in args:   # LORI addition
            ind=args.index("-Fsi")
            site_file=args[ind+1]
        if '-loc' in args:
            ind=args.index("-loc")
            locname=args[ind+1]
        if '-mcd' in args:
            ind=args.index("-mcd")
            methods=args[ind+1]
        else:
            methods='SO-MAG'
        if '-spc' in args:
            ind=args.index("-spc")
            specnum=-int(args[ind+1])
        if '-n' in args:
            ind=args.index("-n")
            norm=args[ind+1]
        if "-A" in args:
            avg=1
        else:
            avg=0
        if '-dc' in args:
            ind=args.index('-dc')
            DC_FIELD,DC_PHI,DC_THETA=list(map(float,args[ind+1].strip('( ) [ ]').split(',')))
            DC_FIELD *= 1e-6
            yn=''
            GET_DC_PARAMS=False
        else: GET_DC_PARAMS,DC_FIELD,DC_PHI,DC_THETA,yn=True,0,0,-90,''
        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")
                    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"
            elif "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"
        if '-f' in args:
            ind=args.index("-f")
            magfile=args[ind+1]
        if '-ID' in args:
            ind = args.index('-ID')
            input_dir_path = args[ind+1]
        else:
            input_dir_path = os.path.split(magfile)[0]
        output_dir_path = dir_path
        # LJ

    # if you are running as a module:
    elif not command_line:
        dir_path = kwargs.get('dir_path', '.')
        user = kwargs.get('user', '')
        meas_file = kwargs.get('meas_file', 'magic_measurements.txt') # outfile
        spec_file = kwargs.get('spec_file', 'er_specimens.txt') # specimen outfile
        samp_file = kwargs.get('samp_file', 'er_samples.txt') # sample outfile
        site_file = kwargs.get('site_file', 'er_sites.txt') # site outfile
        locname = kwargs.get('locname', '')
        methods = kwargs.get('methods', ['SO-MAG'])
        specnum = -int(kwargs.get('specnum', 0))
        norm = kwargs.get('norm', 'cc')
        avg = kwargs.get('avg', 0)  # 0 means do average, 1 means don't
        samp_con = kwargs.get('samp_con', '3')
        magfile = kwargs.get('magfile', '')
        input_dir_path = kwargs.get('input_dir_path', os.path.split(magfile)[0])
        output_dir_path = dir_path
        DC_FIELD,DC_PHI,DC_THETA = list(map(float, kwargs.get('dc_params', (0,0,-90))))
        DC_FIELD *= 1e-6
        yn = ''
        if DC_FIELD==0 and DC_PHI==0 and DC_THETA==-90: GET_DC_PARAMS=True
        else: GET_DC_PARAMS=False
        # done with module-specific stuff

        # formatting and checking variables
        if "4" in samp_con:
            if "-" not in samp_con:
                print("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"
        elif "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"

    magfile = os.path.join(input_dir_path, magfile)
    spec_file = os.path.join(output_dir_path, spec_file)
    samp_file = os.path.join(output_dir_path, samp_file)
    site_file = os.path.join(output_dir_path, site_file)
    meas_file= os.path.join(output_dir_path, meas_file)
    FIRST_GET_DC=True
    try:
        with open(magfile,'r') as file_input:
            File = file_input.readlines()
    except Exception as ex:
        print("bad sam file name: ", magfile)
        return False, "bad sam file name"
    if len(File) == 1: File = File[0].split('\r'); File = [x+"\r\n" for x in File]
    sids,ln,format=[],0,'CIT'
    formats=['CIT','2G','APP','JRA']
    if File[ln].strip()=='CIT': ln+=1
    ErLocRec={}
    ErLocRec["er_location_name"]=locname
    ErLocRec["er_citation_names"]=citation
    comment=File[ln]
    if comment=='CIT':
       format=comment
       ln+=1
    comment=File[ln]
    print(comment)
    ln+=1
    specimens,samples,sites=[],[],[]
    if format=='CIT':
        line=File[ln].split()
        site_lat=line[0]
        site_lon=line[1]
        ErLocRec["location_begin_lat"]=site_lat
        ErLocRec["location_begin_lon"]=site_lon
        ErLocRec["location_end_lat"]=site_lat
        ErLocRec["location_end_lon"]=site_lon
        ErLocs.append(ErLocRec)
        try: Cdec=float(line[2])
        except ValueError: pdb.set_trace()
        for k in range(ln+1,len(File)):
            line=File[k]
            rec=line.split()
            if rec == []: continue
            specimen=rec[0]
            specimens.append(specimen)
    for specimen in specimens:
        ErSpecRec,ErSampRec,ErSiteRec={},{},{}
        if specnum!=0:
            sample=specimen[:specnum]
        else: sample=specimen
        site=pmag.parse_site(sample,samp_con,Z)
        ErSpecRec['er_specimen_name']=specimen
        ErSpecRec['er_sample_name']=sample
        ErSpecRec['er_site_name']=site
        ErSpecRec['er_location_name']=locname
        ErSpecRec['er_citation_names']=citation
        ErSampRec['er_sample_name']=sample
        ErSampRec['er_site_name']=site
        ErSampRec['er_location_name']=locname
        ErSampRec['er_citation_names']=citation
        ErSampRec['magic_method_codes']=methods
        ErSampRec['sample_declination_correction']='%7.1f'%(Cdec)
        ErSiteRec['er_site_name']=site
        ErSiteRec['er_location_name']=locname
        ErSiteRec['er_citation_names']=citation
        ErSiteRec['site_lat']=site_lat
        ErSiteRec['site_lon']=site_lon
        with open(os.path.join(input_dir_path,specimen),'r') as finput:
            Lines = list(finput.readlines())
        comment = ""
        line=Lines[0].split()
        if len(line)>2:
            comment=line[2]
        info=Lines[1].split()
        vol=float(info[-1])
        if vol!=1.0:
            if norm=='cc':units="1"
            if norm=='m3':units="2"
            ErSpecRec['specimen_weight']=""
            if units=="1" or "":
                ErSpecRec['specimen_volume']='%10.3e'%(vol*1e-6)
            else:
                ErSpecRec['specimen_volume']='%10.3e'%(vol)
        else:
            if norm=='cc':units="1"
            if norm=='m3':units="2"
            ErSpecRec['specimen_volume']=""
            if units=="1" or "":
                ErSpecRec['specimen_weight']='%10.3e'%(vol*1e-3)
            else:
                ErSpecRec['specimen_weight']='%10.3e'%(vol)
        dip=float(info[-2])
        dip_direction=float(info[-3])+Cdec+90.
        sample_dip=-float(info[-4])
        sample_azimuth=float(info[-5])+Cdec-90.
        if len(info)>5:
            ErSampRec['sample_height']=info[-6]
        else:
            ErSampRec['sample_height']='0'
        ErSampRec['sample_azimuth']='%7.1f'%(sample_azimuth)
        ErSampRec['sample_dip']='%7.1f'%(sample_dip)
        ErSampRec['sample_bed_dip']='%7.1f'%(dip)
        ErSampRec['sample_bed_dip_direction']='%7.1f'%(dip_direction)
        ErSampRec['sample_class']=''
        ErSampRec['sample_type']=''
        ErSampRec['sample_lithology']=''
        if Cdec!=0 or Cdec!="":
            ErSampRec['magic_method_codes']='SO-CMD-NORTH'
        else:
            ErSampRec['magic_method_codes']='SO-MAG'
        for line in Lines[2:len(Lines)]:
            if line == '\n': continue
            MeasRec=ErSpecRec.copy()
#           Remove specimen_volume and specimen_weight as they do not exits in the magic_measurement table
            del MeasRec["specimen_volume"]
            del MeasRec["specimen_weight"]
            treat_type=line[0:3]
            if treat_type[1] == '.':
                treat_type = 'NRM'
            treat=line[2:6]
            try: float(treat)
            except ValueError: treat = line[3:6]
            if treat_type.startswith('NRM'):
                MeasRec['magic_method_codes']='LT-NO'
                MeasRec['measurement_temp']='273'
                MeasRec['treatment_temp']='273'
                MeasRec['treatment_dc_field']='0'
                MeasRec['treatment_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treatment_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treatment_ac_field']='0'
            elif treat_type.startswith('AF'):
                MeasRec['magic_method_codes']='LT-AF-Z'
                MeasRec['measurement_temp']='273'
                MeasRec['treatment_temp']='273'
                MeasRec['treatment_dc_field']='0'
                MeasRec['treatment_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treatment_dc_field_theta'] = '%1.2f'%DC_THETA
                if treat.strip() == '':
                    MeasRec['treatment_ac_field']='0'
                else:
                    MeasRec['treatment_ac_field']='%10.3e'%(float(treat)*1e-3)
            elif treat_type.startswith('ARM'):
                MeasRec['magic_method_codes']="LP-ARM"
                MeasRec['measurement_temp']='273'
                MeasRec['treatment_temp']='273'
                MeasRec['treatment_dc_field']='0'
                MeasRec['treatment_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treatment_dc_field_theta'] = '%1.2f'%DC_THETA
                if treat.strip() == '':
                    MeasRec['treatment_ac_field']='0'
                else:
                    MeasRec['magic_method_codes']="LP-ARM-AFD"
                    MeasRec['treatment_ac_field']='%10.3e'%(float(treat)*1e-3)
            elif treat_type.startswith('TT'):
                MeasRec['magic_method_codes']='LT-T-Z'
                MeasRec['measurement_temp']='273'
                if treat.strip() == '':
                    MeasRec['treatment_temp']='273'
                else:
                    MeasRec['treatment_temp']='%7.1f'%(float(treat)+273)
                MeasRec['treatment_dc_field']='0'
                MeasRec['treatment_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treatment_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treatment_ac_field']='0'
            elif treat_type.startswith('LT') or treat_type.startswith('LN2'):
                MeasRec['magic_method_codes']='LT-LT-Z'
                MeasRec['measurement_temp']='273'
                MeasRec['treatment_temp']='77'
                MeasRec['treatment_dc_field']='0'
                MeasRec['treatment_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treatment_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treatment_ac_field']='0'
            elif line[4] == '0': #assume decimal IZZI format 0 field thus can hardcode the dc fields
                MeasRec['magic_method_codes']='LT-T-Z'
                MeasRec['measurement_temp']='273'
                MeasRec['treatment_temp']=str(int(treat_type) + 273)
                MeasRec['treatment_dc_field']='0'
                MeasRec['treatment_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treatment_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treatment_ac_field']='0'
            elif line[4] == '1': #assume decimal IZZI format in constant field
                if GET_DC_PARAMS: GET_DC_PARAMS, FIRST_GET_DC, yn, DC_FIELD, DC_PHI, DC_THETA = get_dc_params(FIRST_GET_DC,specimen,treat_type,yn)
                MeasRec['magic_method_codes']='LT-T-I'
                MeasRec['measurement_temp']='273'
                MeasRec['treatment_temp']=str(int(treat_type) + 273)
                MeasRec['treatment_dc_field']='%1.2e'%DC_FIELD
                MeasRec['treatment_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treatment_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treatment_ac_field']='0'
            elif line[4] == '2': #assume decimal IZZI format PTRM step
                if GET_DC_PARAMS: GET_DC_PARAMS, FIRST_GET_DC, yn, DC_FIELD, DC_PHI, DC_THETA = get_dc_params(FIRST_GET_DC,specimen,treat_type,yn)
                MeasRec['magic_method_codes']='LT-PTRM-I'
                MeasRec['measurement_temp']='273'
                MeasRec['treatment_temp']=str(int(treat_type) + 273)
                MeasRec['treatment_dc_field']='%1.2e'%DC_FIELD
                MeasRec['treatment_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treatment_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treatment_ac_field']='0'
            else:
                print("trouble with your treatment steps")
            MeasRec['measurement_dec']=line[46:51]
            MeasRec['measurement_inc']=line[52:58]
            M='%8.2e'%(float(line[31:39])*vol*1e-3) # convert to Am2
            MeasRec['measurement_magn_moment']=M
            MeasRec['measurement_csd']='%7.1f'%(eval(line[41:46]))
            MeasRec["measurement_positions"]='1'
            MeasRec['measurement_standard']='u'
            if len(line)>60:
                MeasRec['magic_instrument_codes']=line[85:]
                MeasRec['measurement_sd_x']='%8.2e'%(float(line[58:67])*1e-8) #(convert e-5emu to Am2)
                MeasRec['measurement_sd_y']='%8.2e'%(float(line[67:76])*1e-8)
                MeasRec['measurement_sd_z']='%8.2e'%(float(line[76:85])*1e-8)
            MeasRecs.append(MeasRec)
        ErSpecs.append(ErSpecRec)
        if sample not in samples:
            samples.append(sample)
            ErSamps.append(ErSampRec)
        site=pmag.parse_site(sample,samp_con,Z)
        if site not in sites:
            sites.append(site)
            ErSites.append(ErSiteRec)
    pmag.magic_write(spec_file,ErSpecs,'er_specimens')
    print('specimens stored in ',spec_file)
    pmag.magic_write(samp_file,ErSamps,'er_samples')
    print('samples stored in ',samp_file)
    pmag.magic_write(site_file,ErSites,'er_sites')
    print('sites stored in ', site_file)
    Fixed=pmag.measurements_methods(MeasRecs,avg)
    pmag.magic_write(meas_file,Fixed,'magic_measurements')
    print('data stored in ',meas_file)
    return True, meas_file
Exemplo n.º 19
0
def main(command_line=True, **kwargs):
    """

    NAME
        huji_magic_new.py
 
    DESCRIPTION
        converts HUJI new 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 -- NOT CURRENTLY SUPPORTED
            [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...)
     
    """
    # initialize some variables
    mag_file = ''
    meas_file = "magic_measurements.txt"
    user = ""
    specnum = 0
    samp_con = '1'
    labfield = 0
    er_location_name = ''
    codelist = None

    # get command line args
    if command_line:
        args = sys.argv
        if "-h" in args:
            print(main.__doc__)
            return False
        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]
            print("got magfile:", magfile)
        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 "-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 '-LP' in args:
            ind = args.index("-LP")
            codelist = args[ind + 1]

        # lab process:

    # unpack key-word args if used as module
    if not command_line:
        user = kwargs.get('user', '')
        meas_file = kwargs.get('meas_file', 'magic_measurements.txt')
        magfile = kwargs.get('magfile', '')
        specnum = int(kwargs.get('specnum', 0))
        labfield = int(kwargs.get('labfield', 0)) * 1e-6
        phi = int(kwargs.get('phi', 0))
        theta = int(kwargs.get('theta', 0))
        peakfield = kwargs.get('peakfield', 0)
        if peakfield:
            peakfield = float(peakfield) * 1e-3
        er_location_name = kwargs.get('er_location_name', '')
        samp_con = kwargs.get('samp_con', '1')
        codelist = kwargs.get('codelist', '')
        CR_cooling_times = kwargs.get('CR_cooling_times', None)

    # format and validate variables
    if magfile:
        try:
            input = open(magfile, 'r')
        except:
            print("bad mag file name")
            return False, "bad mag file name"
    else:
        print("mag_file field is required option")
        print(main.__doc__)
        return False, "mag_file field is required option"

    if specnum != 0:
        specnum = -specnum
    if "4" in samp_con:
        if "-" not in samp_con:
            print("option [4] must be in form 4-Z where Z is an integer")
            return False, "option [4] must be in form 4-Z where Z is an integer"
        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")
            return False, "option [7] must be in form 7-Z where Z is an integer"
        else:
            Z = int(samp_con.split("-")[1])
            samp_con = "7"

    if codelist:
        codes = codelist.split(':')
    else:
        print("Must select experiment type (-LP option)")
        return False, "Must select experiment type (-LP option)"
    if "AF" in codes:
        demag = 'AF'
        LPcode = "LP-DIR-AF"
    if "T" in codes:
        demag = "T"
        if not labfield: LPcode = "LP-DIR-T"
        if labfield: LPcode = "LP-PI-TRM"
        if "ANI" in codes:
            if not labfield:
                print("missing lab field option")
                return False, "missing lab field option"
            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 not labfield:
            print("missing lab field option")
            return False, "missing lab field option"

        LPcode = "LP-CR-TRM"  # TRM in different cooling rates
        if command_line:
            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_core = rec[6]
        inc_core = rec[7]
        dec_geo = rec[8]
        inc_geo = rec[9]
        dec_tilted = rec[10]
        inc_tilted = rec[11]
        moment_emu = float(rec[12])

        if specimen not in list(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 treatments in file %s magfile line %i: specimen %s, treatment %s ignoring 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_core'] = dec_core
        this_line_data['inc_core'] = inc_core
        this_line_data['dec_geo'] = dec_geo
        this_line_data['inc_geo'] = inc_geo
        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 = list(Data.keys())
    specimens_list.sort()

    MagRecs = []
    for specimen in specimens_list:
        for i in range(len(Data[specimen])):
            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 er_location_name:
                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_core']
            MagRec["measurement_inc"] = this_line_data['inc_core']
            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"])
            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":
                treatment_type = this_line_data['treatment_type']
                # 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)
                        print(this_line_data)
                        return False, "ERROR in treatment field line %i... exiting until you fix the problem" % line_no

                # 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)

            #----------------------------------------
            # 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)
                        return False, "ERROR in treatment field line %i... exiting until you fix the problem" % line_no

                    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"] = '%i' % (
                        int(phi))  # labfield phi
                    MagRec["treatment_dc_field_theta"] = '%i' % (
                        int(theta))  # labfield theta
                    MagRec["measurement_number"] = "%i" % i
                    MagRec["measurement_description"] = ""
                    MagRecs.append(MagRec)
                    #continue

                #----------------------------------------
                # Cooling rate experiments
                #----------------------------------------

                if LPcode == "LP-CR-TRM":
                    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]
                    if index == 7 or index == 70:  # alteration check as final measurement
                        meas_type = "LT-PTRM-I:LP-CR-TRM"
                        CR_cooling_time = CR_cooling_times[-1]
                    else:
                        meas_type = "LT-T-I:LP-CR-TRM"
                        CR_cooling_time = CR_cooling_times[index - 1]
                    MagRec["magic_method_codes"] = meas_type
                    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"] = "cooling_rate" + ":" + CR_cooling_time + ":" + "K/min"
                    #MagRec["measurement_description"]="%.1f minutes per 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)
    return True, meas_file
Exemplo n.º 20
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 -- NOT CURRENTLY SUPPORTED
            [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
Exemplo n.º 21
0
def main(command_line=True, **kwargs):
    """
    NAME
        jr6_jr6_magic.py
 
    DESCRIPTION
        converts JR6 .jr6 format files to magic_measurements format files

    SYNTAX
        jr6_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: specify er_samples format file for appending, default is new er_samples.txt (Not working yet)
        -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 sample naming convention (6 and 7 not yet implemented)
        -mcd [SO-MAG,SO-SUN,SO-SIGHT...] supply how these samples were oriented
        -JR  IODP samples measured on the JOIDES RESOLUTION
        -v NUM : specify the volume in cc of the sample, default 2.5^3cc
       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 -- NOT CURRENTLY SUPPORTED
            [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
        JR6 .jr6 format file
    """
# initialize some stuff
    noave=0
    #volume=2.5**3 #default volume is a 2.5cm cube
    volume = 2.5 * 1e-6 #default volume is a 2.5 cm cube, translated to meters cubed
    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
    version_num=pmag.get_version()
    Samps=[] # keeps track of sample orientations

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

    samp_file = 'er_samples.txt'
    meas_file = 'magic_measurements.txt'
    tmp_file= "fixed.jr6"
    meth_code,JR="",0
    #
    # 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 "-JR" in args: 
            meth_code=meth_code+":FS-C-DRILL-IODP:SP-SS-C:SO-V"
            meth_code=meth_code.strip(":")
            JR=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')
        specnum = kwargs.get('specnum', 1)
        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")
        volume = float(kwargs.get('volume', 0))
        if not volume:
            volume = 2.5 * 1e-6 #default volume is a 2.5 cm cube, translated to meters cubed
        else:
            #convert cm^3 to m^3
            volume *= 1e-6
        JR = kwargs.get('JR', 0)
        if JR:
            if meth_code == "LP-NO":
                meth_code = ""
            meth_code=meth_code+":FS-C-DRILL-IODP:SP-SS-C:SO-V"
            meth_code=meth_code.strip(":")
            samp_con='5'

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

    ErSampRec,ErSiteRec={},{}

    # parse data

    # fix .jr6 file so that there are spaces between all the columns.
    pre_data=open(mag_file, 'rU')
    tmp_data=open(tmp_file, 'w')
    line=pre_data.readline()
    while line !='':
        line=line.replace('-',' -')
        #print "line=", line
        tmp_data.write(line)
        line=pre_data.readline()
    tmp_data.close()
    pre_data.close()

    data=pd.read_csv(tmp_file, delim_whitespace=True,header=None)

    if JR==0: #
        data.columns=['er_specimen_name','step','x','y','z','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['z']]).transpose()
    else: # measured on the Joides Resolution JR6
        data.columns=['er_specimen_name','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_moment']=dir[2]*(10.0**data['expon'])*volume # the data are in A/m - this converts to Am^2 
    data['measurement_magn_volume']=dir[2]*(10.0**data['expon']) # A/m  - data in A/m
    data['sample_dip']=-data['sample_dip']
    DGEOs,IGEOs=[],[]
    for ind in range(len(data)):
        dgeo,igeo=pmag.dogeo(data.ix[ind]['measurement_dec'],data.ix[ind]['measurement_inc'],data.ix[ind]['sample_azimuth'],data.ix[ind]['sample_dip'])
        DGEOs.append(dgeo)
        IGEOs.append(igeo)
    data['specimen_dec']=DGEOs
    data['specimen_inc']=IGEOs
    data['specimen_tilt']='1'
    if specnum!=0: 
        data['er_sample_name']=data['er_specimen_name'][:specnum]
    else:
        data['er_sample_name']=data['er_specimen_name']

    if int(samp_con) in [1, 2, 3, 4, 5, 7]:
        data['er_site_name']=pmag.parse_site(data['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']

    # Configure the er_sample table        

    for rowNum, row in data.iterrows():
        sampleFlag=0
        for sampRec in SampOuts:
            if sampRec['er_sample_name'] == row['er_sample_name']:
                sampleFlag=1
                break
        if sampleFlag == 0:
            ErSampRec['er_sample_name']=row['er_sample_name']
            ErSampRec['sample_azimuth']=str(row['sample_azimuth'])
            ErSampRec['sample_dip']=str(row['sample_dip'])
            ErSampRec['magic_method_codes']=meth_code 
            ErSampRec['er_location_name']=er_location_name
            ErSampRec['er_site_name']=row['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: '+date
        MagRec["er_citation_names"]="This study"
        MagRec['er_location_name']=er_location_name
        MagRec['er_site_name']=row['er_site_name']
        MagRec['er_sample_name']=row['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"]=row['er_specimen_name']
        MagRec["treatment_ac_field"]='0'
        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
        else: # need to add IRM, and ARM options
            print "measurement type unknown", row['step']
            return False, "measurement type unknown"
        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
        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
Exemplo n.º 22
0
def main():
    """
    NAME
        make_magic_plots.py

    DESCRIPTION
        inspects magic directory for available data and makes plots

    SYNTAX
        make_magic_plots.py [command line options]

    INPUT
        magic files

    OPTIONS
        -h prints help message and quits
        -f FILE specifies input file name
        -fmt [png,eps,svg,jpg,pdf] specify format, default is png
    """
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    # reset log files
    for fname in ['log.txt', 'errors.txt']:
        f = os.path.join(os.getcwd(), fname)
        if os.path.exists(f):
            os.remove(f)
    image_recs = []
    dirlist = ['./']
    dir_path = os.getcwd()
    #
    if '-fmt' in sys.argv:
        ind = sys.argv.index("-fmt")
        fmt = sys.argv[ind + 1]
    else:
        fmt = 'png'
    if '-f' in sys.argv:
        ind = sys.argv.index("-f")
        filelist = [sys.argv[ind + 1]]
    else:
        filelist = os.listdir(dir_path)
    ## initialize some variables
    samp_file = 'samples.txt'
    meas_file = 'measurements.txt'
    #loc_key = 'location'
    loc_file = 'locations.txt'
    method_key = 'method_codes'
    dec_key = 'dir_dec'
    inc_key = 'dir_inc'
    tilt_corr_key = "dir_tilt_correction"
    aniso_tilt_corr_key = "aniso_tilt_correction"
    hyst_bcr_key = "hyst_bcr"
    hyst_mr_key = "hyst_mr_moment"
    hyst_ms_key = "hyst_ms_moment"
    hyst_bc_key = "hyst_bc"
    Mkeys = ['magnitude', 'magn_moment', 'magn_volume', 'magn_mass']
    results_file = 'sites.txt'
    hyst_file = 'specimens.txt'
    aniso_file = 'specimens.txt'
    # create contribution and propagate data throughout
    full_con = cb.Contribution()
    full_con.propagate_location_to_measurements()
    full_con.propagate_location_to_specimens()
    full_con.propagate_location_to_samples()
    if not full_con.tables:
        print('-E- No MagIC tables could be found in this directory')
        error_log("No MagIC tables found")
        return
    # try to get the contribution id for error logging
    con_id = ""
    if 'contribution' in full_con.tables:
        if 'id' in full_con.tables['contribution'].df.columns:
            con_id = full_con.tables['contribution'].df.iloc[0]['id']
    # check to see if propagation worked, otherwise you can't plot by location
    lowest_table = None
    for table in full_con.ancestry:
        if table in full_con.tables:
            lowest_table = table
            break

    do_full_directory = False
    # check that locations propagated down to the lowest table in the contribution
    if 'location' in full_con.tables[lowest_table].df.columns:
        if 'locations' not in full_con.tables:
            info_log(
                'location names propagated to {}, but could not be validated'.
                format(lowest_table))
        # are there any locations in the lowest table?
        elif not all(full_con.tables[lowest_table].df['location'].isnull()):
            locs = full_con.tables['locations'].df.index.unique()
            lowest_locs = full_con.tables[lowest_table].df['location'].unique()
            incorrect_locs = set(lowest_locs).difference(set(locs))
            # are they actual locations?
            if not incorrect_locs:
                info_log(
                    'location names propagated to {}'.format(lowest_table))
            else:
                do_full_directory = True
                error_log(
                    'location names did not propagate fully to {} table (looks like there are some naming inconsistencies between tables)'
                    .format(lowest_table),
                    con_id=con_id)
        else:
            do_full_directory = True
            error_log(
                'could not propagate location names down to {} table'.format(
                    lowest_table),
                con_id=con_id)
    else:
        do_full_directory = True
        error_log('could not propagate location names down to {} table'.format(
            lowest_table),
                  con_id=con_id)

    all_data = {}
    all_data['measurements'] = full_con.tables.get('measurements', None)
    all_data['specimens'] = full_con.tables.get('specimens', None)
    all_data['samples'] = full_con.tables.get('samples', None)
    all_data['sites'] = full_con.tables.get('sites', None)
    all_data['locations'] = full_con.tables.get('locations', None)
    if 'locations' in full_con.tables:
        locations = full_con.tables['locations'].df.index.unique()
    else:
        locations = ['']
    dirlist = [
        loc for loc in locations if cb.not_null(loc, False) and loc != 'nan'
    ]
    if not dirlist:
        dirlist = ["./"]
    if do_full_directory:
        dirlist = ["./"]

    # plot the whole contribution as one location
    if dirlist == ["./"]:
        error_log('plotting the entire contribution as one location',
                  con_id=con_id)
        for fname in os.listdir("."):
            if fname.endswith(".txt"):
                shutil.copy(fname, "tmp_" + fname)

    # if possible, go through all data by location
    # use tmp_*.txt files to separate out by location

    for loc in dirlist:
        print('\nworking on: ', loc)

        def get_data(dtype, loc_name):
            """
            Extract data of type dtype for location loc_name.
            Write tmp_dtype.txt files if possible.
            """
            if cb.not_null(all_data[dtype], False):
                data_container = all_data[dtype]
                if loc_name == "./":
                    data_df = data_container.df
                else:
                    # awkward workaround for chars like "(" and "?" that break in regex
                    try:
                        data_df = data_container.df[data_container.df[
                            'location'].astype(str).str.contains(loc_name,
                                                                 na=False)]
                    except:  #sre_constants.error:
                        data_df = data_container.df[
                            data_container.df['location'] == loc_name]

                data = data_container.convert_to_pmag_data_list(df=data_df)
                res = data_container.write_magic_file(
                    'tmp_{}.txt'.format(dtype), df=data_df)
                if not res:
                    return [], []
                return data, data_df
            return [], []

        meas_data, meas_df = get_data('measurements', loc)
        spec_data, spec_df = get_data('specimens', loc)
        samp_data, samp_df = get_data('samples', loc)
        site_data, site_df = get_data('sites', loc)
        loc_data, loc_df = get_data('locations', loc)

        con = cb.Contribution(read_tables=[])
        con.tables['measurements'] = cb.MagicDataFrame(df=meas_df,
                                                       dtype="measurements")
        con.tables['specimens'] = cb.MagicDataFrame(df=spec_df,
                                                    dtype="specimens")
        con.tables['samples'] = cb.MagicDataFrame(df=samp_df, dtype="samples")
        con.tables['sites'] = cb.MagicDataFrame(df=site_df, dtype="sites")
        con.tables['locations'] = cb.MagicDataFrame(df=loc_df,
                                                    dtype="locations")

        if loc == "./":  # if you can't sort by location, do everything together
            con = full_con
            try:
                meas_data = con.tables[
                    'measurements'].convert_to_pmag_data_list()
            except KeyError:
                meas_data = None
            try:
                spec_data = con.tables['specimens'].convert_to_pmag_data_list()
            except KeyError:
                spec_data = None
            try:
                samp_data = con.tables['samples'].convert_to_pmag_data_list()
            except KeyError:
                samp_data = None
            try:
                site_data = con.tables['sites'].convert_to_pmag_data_list()
            except KeyError:
                site_data = None

        crd = 's'
        if 'samples' in con.tables:
            if 'azimuth' in con.tables['samples'].df.columns:
                if any(con.tables['samples'].df['azimuth'].dropna()):
                    crd = 'g'
        if crd == 's':
            print('using specimen coordinates')
        else:
            print('using geographic coordinates')
        if meas_file in filelist and meas_data:  # start with measurement data
            print('working on plotting measurements data')
            data = meas_data
            file_type = 'measurements'
            # looking for  zeq_magic possibilities
            # get all non blank method codes
            AFZrecs = pmag.get_dictitem(data, method_key, 'LT-AF-Z', 'has')
            # get all non blank method codes
            TZrecs = pmag.get_dictitem(data, method_key, 'LT-T-Z', 'has')
            # get all non blank method codes
            MZrecs = pmag.get_dictitem(data, method_key, 'LT-M-Z', 'has')
            # get all dec measurements
            Drecs = pmag.get_dictitem(data, dec_key, '', 'F')
            # get all inc measurements
            Irecs = pmag.get_dictitem(data, inc_key, '', 'F')
            for key in Mkeys:
                Mrecs = pmag.get_dictitem(data, key, '',
                                          'F')  # get intensity data
                if len(Mrecs) > 0:
                    break
            # potential for stepwise demag curves
            if len(AFZrecs) > 0 or len(TZrecs) > 0 or len(MZrecs) > 0 and len(
                    Drecs) > 0 and len(Irecs) > 0 and len(Mrecs) > 0:
                #CMD = 'zeq_magic.py -f tmp_measurements.txt -fsp tmp_specimens.txt -fsa tmp_samples.txt -fsi tmp_sites.txt -sav -fmt ' + fmt + ' -crd ' + crd + " -new"
                CMD = "ipmag.zeq_magic(crd={}, n_plots='all', contribution={}, image_records=True)".format(
                    crd, con)
                print(CMD)
                info_log(CMD, loc)
                res, outfiles, zeq_images = ipmag.zeq_magic(crd=crd,
                                                            n_plots='all',
                                                            contribution=con,
                                                            image_records=True)
                image_recs.extend(zeq_images)
            # looking for  thellier_magic possibilities
            if len(pmag.get_dictitem(data, method_key, 'LP-PI-TRM',
                                     'has')) > 0:
                #CMD = 'thellier_magic.py -f tmp_measurements.txt -fsp tmp_specimens.txt -sav -fmt ' + fmt
                CMD = "ipmag.thellier_magic(n_specs='all', fmt='png', contribution={}, image_records=True)".format(
                    con)
                print(CMD)
                info_log(CMD, loc)
                res, outfiles, thellier_images = ipmag.thellier_magic(
                    n_specs='all',
                    fmt="png",
                    contribution=con,
                    image_records=True)
                image_recs.extend(thellier_images)
            # looking for hysteresis possibilities
            if len(pmag.get_dictitem(data, method_key, 'LP-HYS',
                                     'has')) > 0:  # find hyst experiments
                # check for reqd columns
                missing = check_for_reqd_cols(data, ['treat_temp'])
                if missing:
                    error_log(
                        'LP-HYS method code present, but required column(s) [{}] missing'
                        .format(", ".join(missing)),
                        loc,
                        "quick_hyst.py",
                        con_id=con_id)
                else:
                    #CMD = 'quick_hyst.py -f tmp_measurements.txt -sav -fmt ' + fmt
                    CMD = "ipmag.quick_hyst(fmt='png', n_plots='all', contribution={}, image_records=True)".format(
                        con)
                    print(CMD)
                    info_log(CMD, loc)
                    res, outfiles, quick_hyst_recs = ipmag.quick_hyst(
                        fmt="png",
                        n_plots='all',
                        contribution=con,
                        image_records=True)
                    image_recs.extend(quick_hyst_recs)
            # equal area plots of directional data
            # at measurement level (by specimen)
            if data:
                missing = check_for_reqd_cols(data, ['dir_dec', 'dir_inc'])
                if not missing:
                    #CMD = "eqarea_magic.py -f tmp_measurements.txt -obj spc -sav -no-tilt -fmt " + fmt
                    CMD = "ipmag.eqarea_magic(fmt='png', n_plots='all', ignore_tilt=True, plot_by='spc', contribution={}, source_table='measurements', image_records=True)".format(
                        con)
                    print(CMD)
                    info_log(CMD, loc, "eqarea_magic.py")
                    res, outfiles, eqarea_spc_images = ipmag.eqarea_magic(
                        fmt="png",
                        n_plots='all',
                        ignore_tilt=True,
                        plot_by="spc",
                        contribution=con,
                        source_table="measurements",
                        image_records=True)
                    image_recs.extend(eqarea_spc_images)

        else:
            if VERBOSE:
                print('-I- No measurement data found')

        # site data
        if results_file in filelist and site_data:
            print('-I- result file found', results_file)
            data = site_data
            file_type = 'sites'
            print('-I- working on site directions')
            print('number of datapoints: ', len(data), loc)
            dec_key = 'dir_dec'
            inc_key = 'dir_inc'
            int_key = 'int_abs'
            SiteDIs = pmag.get_dictitem(data, dec_key, "", 'F')  # find decs
            SiteDIs = pmag.get_dictitem(SiteDIs, inc_key, "",
                                        'F')  # find decs and incs
            dir_data_found = len(SiteDIs)
            print('{} Dec/inc pairs found'.format(dir_data_found))
            if SiteDIs:
                # then convert tilt_corr_key to correct format
                old_SiteDIs = SiteDIs
                SiteDIs = []
                for rec in old_SiteDIs:
                    if tilt_corr_key not in rec:
                        rec[tilt_corr_key] = "0"
                    # make sure tilt_corr_key is a correct format
                    try:
                        rec[tilt_corr_key] = str(int(float(
                            rec[tilt_corr_key])))
                    except ValueError:
                        rec[tilt_corr_key] = "0"
                    SiteDIs.append(rec)

                print('number of individual directions: ', len(SiteDIs))
                # tilt corrected coordinates
                SiteDIs_t = pmag.get_dictitem(SiteDIs,
                                              tilt_corr_key,
                                              '100',
                                              'T',
                                              float_to_int=True)
                print('number of tilt corrected directions: ', len(SiteDIs_t))
                SiteDIs_g = pmag.get_dictitem(
                    SiteDIs, tilt_corr_key, '0', 'T',
                    float_to_int=True)  # geographic coordinates
                print('number of geographic  directions: ', len(SiteDIs_g))
                SiteDIs_s = pmag.get_dictitem(
                    SiteDIs, tilt_corr_key, '-1', 'T',
                    float_to_int=True)  # sample coordinates
                print('number of sample  directions: ', len(SiteDIs_s))
                SiteDIs_x = pmag.get_dictitem(SiteDIs, tilt_corr_key, '',
                                              'T')  # no coordinates
                print('number of no coordinates  directions: ', len(SiteDIs_x))
                if len(SiteDIs_t) > 0 or len(SiteDIs_g) > 0 or len(
                        SiteDIs_s) > 0 or len(SiteDIs_x) > 0:
                    CRD = ""
                    if len(SiteDIs_t) > 0:
                        CRD = ' -crd t'
                        crd = "t"
                    elif len(SiteDIs_g) > 0:
                        CRD = ' -crd g'
                        crd = "g"
                    elif len(SiteDIs_s) > 0:
                        CRD = ' -crd s'
                        crd = "s"
                    #CMD = 'eqarea_magic.py -f tmp_sites.txt -fsp tmp_specimens.txt -fsa tmp_samples.txt -flo tmp_locations.txt -sav -fmt ' + fmt + CRD
                    CMD = "ipmag.eqarea_magic(crd={}, fmt='png', n_plots='all', contribution={}, source_table='sites')".format(
                        crd, con)
                    print(CMD)
                    info_log(CMD, loc)
                    res, outfiles, eqarea_site_recs = ipmag.eqarea_magic(
                        crd=crd,
                        fmt="png",
                        n_plots='all',
                        contribution=con,
                        source_table="sites",
                        image_records=True)
                    image_recs.extend(eqarea_site_recs)
                else:
                    if dir_data_found:
                        error_log(
                            '{} dec/inc pairs found, but no equal area plots were made'
                            .format(dir_data_found),
                            loc,
                            "equarea_magic.py",
                            con_id=con_id)
            #
            print('-I- working on VGP map')
            VGPs = pmag.get_dictitem(SiteDIs, 'vgp_lat', "",
                                     'F')  # are there any VGPs?
            if len(VGPs) > 0:  # YES!
                #CMD = 'vgpmap_magic.py -f tmp_sites.txt -prj moll -res c -sym ro 5 -sav -fmt png'
                CMD = "ipmag.vgpmap_magic(proj='moll', sym='ro', size=5, fmt='png', contribution={})".format(
                    con)
                print(CMD)
                info_log(CMD, loc, 'vgpmap_magic.py')
                res, outfiles, vgpmap_recs = ipmag.vgpmap_magic(
                    proj='moll',
                    sym='ro',
                    size=5,
                    fmt="png",
                    contribution=con,
                    image_records=True)
                image_recs.extend(vgpmap_recs)
            else:
                print('-I- No vgps found')

            print('-I- Look for intensities')
            # is there any intensity data?
            if site_data:
                if int_key in site_data[0].keys():
                    # old way, wasn't working right:
                    #CMD = 'magic_select.py  -key ' + int_key + ' 0. has -F tmp1.txt -f tmp_sites.txt'
                    Selection = pmag.get_dictkey(site_data, int_key, dtype="f")
                    selection = [i * 1e6 for i in Selection if i != 0]
                    loc = loc.replace(" ", "_")
                    if loc == "./":
                        loc_name = ""
                    else:
                        loc_name = loc
                    histfile = 'LO:_' + loc_name + \
                        '_TY:_intensities_histogram:_.' + fmt
                    CMD = "histplot.py -twin -b 1 -xlab 'Intensity (uT)' -sav -f intensities.txt -F " + histfile
                    CMD = "ipmag.histplot(data=selection, outfile=histfile, xlab='Intensity (uT)', binsize=1, norm=-1, save_plots=True)".format(
                        histfile)
                    info_log(CMD, loc)
                    print(CMD)
                    ipmag.histplot(data=selection,
                                   outfile=histfile,
                                   xlab="Intensity (uT)",
                                   binsize=1,
                                   norm=-1,
                                   save_plots=True)
                    histplot_rec = {
                        'file': histfile,
                        'type': 'Other',
                        'title': 'Intensity histogram',
                        'software_packages': version.version,
                        'keywords': "",
                        'timestamp': datetime.date.today().isoformat()
                    }
                    image_recs.append(histplot_rec)
                else:
                    print('-I- No intensities found')
            else:
                print('-I- No intensities found')

        ##
        if hyst_file in filelist and spec_data:
            print('working on hysteresis', hyst_file)
            data = spec_data
            file_type = 'specimens'
            hdata = pmag.get_dictitem(data, hyst_bcr_key, '', 'F')
            hdata = pmag.get_dictitem(hdata, hyst_mr_key, '', 'F')
            hdata = pmag.get_dictitem(hdata, hyst_ms_key, '', 'F')
            # there are data for a dayplot
            hdata = pmag.get_dictitem(hdata, hyst_bc_key, '', 'F')
            if len(hdata) > 0:
                CMD = "ipmag.dayplot_magic(save=True, fmt='png', contribution={}, image_records=True)".format(
                    con)
                info_log(CMD, loc)
                print(CMD)
                res, outfiles, dayplot_recs = ipmag.dayplot_magic(
                    save=True, fmt='png', contribution=con, image_records=True)
                image_recs.extend(dayplot_recs)
            else:
                print('no hysteresis data found')
        if aniso_file in filelist and spec_data:  # do anisotropy plots if possible
            print('working on anisotropy', aniso_file)
            data = spec_data
            file_type = 'specimens'

            # make sure there is some anisotropy data
            if not data:
                print('No anisotropy data found')
            elif 'aniso_s' not in data[0]:
                print('No anisotropy data found')
            else:
                # get specimen coordinates
                if aniso_tilt_corr_key not in data[0]:
                    sdata = data
                else:
                    sdata = pmag.get_dictitem(data,
                                              aniso_tilt_corr_key,
                                              '-1',
                                              'T',
                                              float_to_int=True)
                # get specimen coordinates
                gdata = pmag.get_dictitem(data,
                                          aniso_tilt_corr_key,
                                          '0',
                                          'T',
                                          float_to_int=True)
                # get specimen coordinates
                tdata = pmag.get_dictitem(data,
                                          aniso_tilt_corr_key,
                                          '100',
                                          'T',
                                          float_to_int=True)
                if len(sdata) > 3:
                    CMD = "ipmag.aniso_magic(iboot=0, ihext=1, crd='s', fmt='png', contribution={})".format(
                        con)
                    print(CMD)
                    info_log(CMD, loc)
                    res, files, aniso_recs = ipmag.aniso_magic(
                        iboot=0,
                        ihext=1,
                        crd="s",
                        fmt="png",
                        contribution=con,
                        image_records=True)
                    image_recs.extend(aniso_recs)
                if len(gdata) > 3:
                    CMD = "ipmag.aniso_magic(iboot=0, ihext=1, crd='g', fmt='png', contribution={})".format(
                        con)
                    print(CMD)
                    info_log(CMD, loc)
                    res, files, aniso_recs = ipmag.aniso_magic(
                        iboot=0,
                        ihext=1,
                        crd="g",
                        fmt="png",
                        contribution=con,
                        image_records=True)
                    image_recs.extend(aniso_recs)
                if len(tdata) > 3:
                    CMD = "ipmag.aniso_magic(iboot=0, ihext=1, crd='g', fmt='png', contribution={})".format(
                        con)
                    print(CMD)
                    info_log(CMD, loc)
                    res, files, aniso_recs = ipmag.aniso_magic(
                        iboot=0,
                        ihext=1,
                        crd="t",
                        fmt="png",
                        contribution=con,
                        image_records=True)
                    image_recs.extend(aniso_recs)

        # remove temporary files
        for fname in glob.glob('tmp*.txt'):
            os.remove(fname)

    # now we need full contribution data
    if loc_file in filelist and loc_data:
        #data, file_type = pmag.magic_read(loc_file)  # read in location data
        data = loc_data
        print('-I- working on pole map')
        poles = pmag.get_dictitem(data, 'pole_lat', "",
                                  'F')  # are there any poles?
        poles = pmag.get_dictitem(poles, 'pole_lon', "",
                                  'F')  # are there any poles?
        if len(poles) > 0:  # YES!
            CMD = 'polemap_magic.py -sav -fmt png -rev gv 40'
            CMD = 'ipmag.polemap_magic(flip=True, rsym="gv", rsymsize=40, fmt="png", contribution={})'.format(
                full_con)
            print(CMD)
            info_log(CMD, "all locations", "polemap_magic.py")
            res, outfiles, polemap_recs = ipmag.polemap_magic(
                flip=True,
                rsym="gv",
                rsymsize=40,
                fmt="png",
                contribution=full_con,
                image_records=True)
            image_recs.extend(polemap_recs)
        else:
            print('-I- No poles found')

    if image_recs:
        new_image_file = os.path.join(dir_path, 'new_images.txt')
        old_image_file = os.path.join(dir_path, 'images.txt')
        pmag.magic_write(new_image_file, image_recs, 'images')
        if os.path.exists(old_image_file):
            ipmag.combine_magic([old_image_file, new_image_file],
                                outfile=old_image_file,
                                magic_table="images",
                                dir_path=dir_path)
        else:
            os.rename(new_image_file, old_image_file)
    if set_env.isServer:
        thumbnails.make_thumbnails(dir_path)
Exemplo n.º 23
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, 'r')
        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, 'r')
        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, 'r')
                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)
    with open(temp, 'r') as finput:
        lines = finput.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.
                                                      )  # 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'
Exemplo n.º 24
0
def save_redo(SpecRecs, inspec):
    pmag.magic_write(inspec, SpecRecs, 'pmag_specimens')
Exemplo n.º 25
0
def main():
    """
    NAME
        hysteresis_magic.py

    DESCRIPTION
        calculates hystereis parameters and saves them in rmag_hystereis format file
        makes plots if option selected

    SYNTAX
        hysteresis_magic.py [command line options]

    OPTIONS
        -h prints help message and quits
        -usr USER:   identify user, default is ""
        -f: specify input file, default is agm_measurements.txt
        -fh: specify rmag_hysteresis.txt input file
        -F: specify output file, default is rmag_hysteresis.txt
        -P: do not make the plots
        -spc SPEC: specify specimen name to plot and quit
        -sav save all plots and quit
        -fmt [png,svg,eps,jpg]
    """
    args = sys.argv
    PLT = 1
    plots = 0
    user, meas_file, rmag_out, rmag_file = "", "agm_measurements.txt", "rmag_hysteresis.txt", ""
    pltspec = ""
    dir_path = '.'
    fmt = 'svg'
    verbose = pmagplotlib.verbose
    version_num = pmag.get_version()
    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 = args[ind + 1]
    if '-F' in args:
        ind = args.index("-F")
        rmag_out = args[ind + 1]
    if '-fh' in args:
        ind = args.index("-fh")
        rmag_file = args[ind + 1]
        rmag_file = dir_path + '/' + rmag_file
    if '-P' in args:
        PLT = 0
        irm_init, imag_init = -1, -1
    if '-sav' in args:
        verbose = 0
        plots = 1
    if '-spc' in args:
        ind = args.index("-spc")
        pltspec = args[ind + 1]
        verbose = 0
        plots = 1
    if '-fmt' in args:
        ind = args.index("-fmt")
        fmt = args[ind + 1]
    rmag_out = dir_path + '/' + rmag_out
    meas_file = dir_path + '/' + meas_file
    rmag_rem = dir_path + "/rmag_remanence.txt"
    #
    #
    meas_data, file_type = pmag.magic_read(meas_file)
    if file_type != 'magic_measurements':
        print main.__doc__
        print 'bad file'
        sys.exit()
    #
    # initialize some variables
    # define figure numbers for hyst,deltaM,DdeltaM curves
    HystRecs, RemRecs = [], []
    HDD = {}
    if verbose:
        if verbose and PLT:
            print "Plots may be on top of each other - use mouse to place "
    if PLT:
        HDD['hyst'], HDD['deltaM'], HDD['DdeltaM'] = 1, 2, 3
        pmagplotlib.plot_init(HDD['DdeltaM'], 5, 5)
        pmagplotlib.plot_init(HDD['deltaM'], 5, 5)
        pmagplotlib.plot_init(HDD['hyst'], 5, 5)
        imag_init = 0
        irm_init = 0
    else:
        HDD['hyst'], HDD['deltaM'], HDD['DdeltaM'], HDD['irm'], HDD[
            'imag'] = 0, 0, 0, 0, 0
    #
    if rmag_file != "": hyst_data, file_type = pmag.magic_read(rmag_file)
    #
    # get list of unique experiment names and specimen names
    #
    experiment_names, sids = [], []
    for rec in meas_data:
        meths = rec['magic_method_codes'].split(':')
        methods = []
        for meth in meths:
            methods.append(meth.strip())
        if 'LP-HYS' in methods:
            if 'er_synthetic_name' in rec.keys(
            ) and rec['er_synthetic_name'] != "":
                rec['er_specimen_name'] = rec['er_synthetic_name']
            if rec['magic_experiment_name'] not in experiment_names:
                experiment_names.append(rec['magic_experiment_name'])
            if rec['er_specimen_name'] not in sids:
                sids.append(rec['er_specimen_name'])
    #
    k = 0
    locname = ''
    if pltspec != "":
        k = sids.index(pltspec)
        print sids[k]
    while k < len(sids):
        s = sids[k]
        if verbose and PLT: print s, k + 1, 'out of ', len(sids)
        #
        #
        B, M, Bdcd, Mdcd = [], [], [], [
        ]  #B,M for hysteresis, Bdcd,Mdcd for irm-dcd data
        Bimag, Mimag = [], []  #Bimag,Mimag for initial magnetization curves
        first_dcd_rec, first_rec, first_imag_rec = 1, 1, 1
        for rec in meas_data:
            methcodes = rec['magic_method_codes'].split(':')
            meths = []
            for meth in methcodes:
                meths.append(meth.strip())
            if rec['er_specimen_name'] == s and "LP-HYS" in meths:
                B.append(float(rec['measurement_lab_field_dc']))
                M.append(float(rec['measurement_magn_moment']))
                if first_rec == 1:
                    e = rec['magic_experiment_name']
                    HystRec = {}
                    first_rec = 0
                    if "er_location_name" in rec.keys():
                        HystRec["er_location_name"] = rec["er_location_name"]
                        locname = rec['er_location_name'].replace('/', '-')
                    if "er_sample_name" in rec.keys():
                        HystRec["er_sample_name"] = rec["er_sample_name"]
                    if "er_site_name" in rec.keys():
                        HystRec["er_site_name"] = rec["er_site_name"]
                    if "er_synthetic_name" in rec.keys(
                    ) and rec['er_synthetic_name'] != "":
                        HystRec["er_synthetic_name"] = rec["er_synthetic_name"]
                    else:
                        HystRec["er_specimen_name"] = rec["er_specimen_name"]
            if rec['er_specimen_name'] == s and "LP-IRM-DCD" in meths:
                Bdcd.append(float(rec['treatment_dc_field']))
                Mdcd.append(float(rec['measurement_magn_moment']))
                if first_dcd_rec == 1:
                    RemRec = {}
                    irm_exp = rec['magic_experiment_name']
                    first_dcd_rec = 0
                    if "er_location_name" in rec.keys():
                        RemRec["er_location_name"] = rec["er_location_name"]
                    if "er_sample_name" in rec.keys():
                        RemRec["er_sample_name"] = rec["er_sample_name"]
                    if "er_site_name" in rec.keys():
                        RemRec["er_site_name"] = rec["er_site_name"]
                    if "er_synthetic_name" in rec.keys(
                    ) and rec['er_synthetic_name'] != "":
                        RemRec["er_synthetic_name"] = rec["er_synthetic_name"]
                    else:
                        RemRec["er_specimen_name"] = rec["er_specimen_name"]
            if rec['er_specimen_name'] == s and "LP-IMAG" in meths:
                if first_imag_rec == 1:
                    imag_exp = rec['magic_experiment_name']
                    first_imag_rec = 0
                Bimag.append(float(rec['measurement_lab_field_dc']))
                Mimag.append(float(rec['measurement_magn_moment']))
    #
    # now plot the hysteresis curve
    #
        if len(B) > 0:
            hmeths = []
            for meth in meths:
                hmeths.append(meth)
            hpars = pmagplotlib.plotHDD(HDD, B, M, e)
            if verbose and PLT: pmagplotlib.drawFIGS(HDD)
            #
            # get prior interpretations from hyst_data
            if rmag_file != "":
                hpars_prior = {}
                for rec in hyst_data:
                    if rec['magic_experiment_names'] == e:
                        if rec['hysteresis_bcr'] != "" and rec[
                                'hysteresis_mr_moment'] != "":
                            hpars_prior['hysteresis_mr_moment'] = rec[
                                'hysteresis_mr_moment']
                            hpars_prior['hysteresis_ms_moment'] = rec[
                                'hysteresis_ms_moment']
                            hpars_prior['hysteresis_bc'] = rec['hysteresis_bc']
                            hpars_prior['hysteresis_bcr'] = rec[
                                'hysteresis_bcr']
                            break
                if verbose: pmagplotlib.plotHPARS(HDD, hpars_prior, 'ro')
            else:
                if verbose: pmagplotlib.plotHPARS(HDD, hpars, 'bs')
                HystRec['hysteresis_mr_moment'] = hpars['hysteresis_mr_moment']
                HystRec['hysteresis_ms_moment'] = hpars['hysteresis_ms_moment']
                HystRec['hysteresis_bc'] = hpars['hysteresis_bc']
                HystRec['hysteresis_bcr'] = hpars['hysteresis_bcr']
                HystRec['hysteresis_xhf'] = hpars['hysteresis_xhf']
                HystRec['magic_experiment_names'] = e
                HystRec['magic_software_packages'] = version_num
                if hpars["magic_method_codes"] not in hmeths:
                    hmeths.append(hpars["magic_method_codes"])
                methods = ""
                for meth in hmeths:
                    methods = methods + meth.strip() + ":"
                HystRec["magic_method_codes"] = methods[:-1]
                HystRec["er_citation_names"] = "This study"
                HystRecs.append(HystRec)
    #
        if len(Bdcd) > 0:
            rmeths = []
            for meth in meths:
                rmeths.append(meth)
            if verbose and PLT: print 'plotting IRM'
            if irm_init == 0:
                HDD['irm'] = 5
                pmagplotlib.plot_init(HDD['irm'], 5, 5)
                irm_init = 1
            rpars = pmagplotlib.plotIRM(HDD['irm'], Bdcd, Mdcd, irm_exp)
            RemRec['remanence_mr_moment'] = rpars['remanence_mr_moment']
            RemRec['remanence_bcr'] = rpars['remanence_bcr']
            RemRec['magic_experiment_names'] = irm_exp
            if rpars["magic_method_codes"] not in meths:
                meths.append(rpars["magic_method_codes"])
            methods = ""
            for meth in rmeths:
                methods = methods + meth.strip() + ":"
            RemRec["magic_method_codes"] = methods[:-1]
            RemRec["er_citation_names"] = "This study"
            RemRecs.append(RemRec)
        else:
            if irm_init: pmagplotlib.clearFIG(HDD['irm'])
        if len(Bimag) > 0:
            if verbose: print 'plotting initial magnetization curve'
            # first normalize by Ms
            Mnorm = []
            for m in Mimag:
                Mnorm.append(m / float(hpars['hysteresis_ms_moment']))
            if imag_init == 0:
                HDD['imag'] = 4
                pmagplotlib.plot_init(HDD['imag'], 5, 5)
                imag_init = 1
            pmagplotlib.plotIMAG(HDD['imag'], Bimag, Mnorm, imag_exp)
        else:
            if imag_init: pmagplotlib.clearFIG(HDD['imag'])
    #
        files = {}
        if plots:
            if pltspec != "": s = pltspec
            files = {}
            for key in HDD.keys():
                files[key] = locname + '_' + s + '_' + key + '.' + fmt
            pmagplotlib.saveP(HDD, files)
            if pltspec != "": sys.exit()
        if verbose and PLT:
            pmagplotlib.drawFIGS(HDD)
            ans = raw_input(
                "S[a]ve plots, [s]pecimen name, [q]uit, <return> to continue\n "
            )
            if ans == "a":
                files = {}
                for key in HDD.keys():
                    files[key] = locname + '_' + s + '_' + key + '.' + fmt
                pmagplotlib.saveP(HDD, files)
            if ans == '': k += 1
            if ans == "p":
                del HystRecs[-1]
                k -= 1
            if ans == 'q':
                print "Good bye"
                sys.exit()
            if ans == 's':
                keepon = 1
                specimen = raw_input(
                    'Enter desired specimen name (or first part there of): ')
                while keepon == 1:
                    try:
                        k = sids.index(specimen)
                        keepon = 0
                    except:
                        tmplist = []
                        for qq in range(len(sids)):
                            if specimen in sids[qq]: tmplist.append(sids[qq])
                        print specimen, " not found, but this was: "
                        print tmplist
                        specimen = raw_input('Select one or try again\n ')
                        k = sids.index(specimen)
        else:
            k += 1
        if len(B) == 0 and len(Bdcd) == 0:
            if verbose: print 'skipping this one - no hysteresis data'
            k += 1
    if rmag_out == "" and ans == 's' and verbose:
        really = raw_input(
            " Do you want to overwrite the existing rmag_hystersis.txt file? 1/[0] "
        )
        if really == "":
            print 'i thought not - goodbye'
            sys.exit()
        rmag_out = "rmag_hysteresis.txt"
    if len(HystRecs) > 0:
        pmag.magic_write(rmag_out, HystRecs, "rmag_hysteresis")
        if verbose: print "hysteresis parameters saved in ", rmag_out
    if len(RemRecs) > 0:
        pmag.magic_write(rmag_rem, RemRecs, "rmag_remanence")
        if verbose: print "remanence parameters saved in ", rmag_rem
Exemplo n.º 26
0
def main():
    """
    NAME
        s_magic.py
  
    DESCRIPTION
        converts .s format data to magic_measurements  format.

    SYNTAX
        s_magic.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f SFILE specifies the .s file name
        -sig last column has sigma
        -typ Anisotropy type:  AMS,AARM,ATRM (default is AMS)
        -F RFILE specifies the rmag_anisotropy file name
        -usr USER specify username
        -loc location specify location/study name
        -spc NUM : specify number of characters to 
              designate a  specimen, default = 0
        -spn SPECNAME, this specimen has the name SPECNAME
        -n first column has specimen name 
        -crd [s,g,t], specify coordinate system of data
           s=specimen,g=geographic,t=tilt adjusted, default is 's'
        -ncn NCON: naming conventionconvention NCON
       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 -- NOT CURRENTLY SUPPORTED
            [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
            NB: all others you will have to either customize your
                self or e-mail [email protected] for help.

    
    DEFAULT
        RFILE:  rmag_anisotropy.txt

    INPUT
        X11,X22,X33,X12,X23,X13  (.s format file)
        X11,X22,X33,X12,X23,X13,sigma (.s format file with -sig option)
        SID, X11,X22,X33,X12,X23,X13  (.s format file with -n option)

    OUTPUT 
        rmag_anisotropy.txt format file

    NOTE
        because .s files do not have specimen names or location information, the output MagIC files
        will have to be changed prior to importing to data base.   
    """
    sfile, anisfile = "", "rmag_anisotropy.txt"
    location = 'unknown'
    user = ""
    sitename, specnum = 'unknown', 0
    samp_con, Z = "", 1
    user = ""
    dir_path = '.'
    name, sigma, spec = 0, 0, 'unknown'
    type = 'AMS'
    if '-WD' in sys.argv:
        ind = sys.argv.index('-WD')
        dir_path = sys.argv[ind + 1]
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if "-spc" in sys.argv:
        ind = sys.argv.index("-spc")
        specnum = int(sys.argv[ind + 1])
        if specnum != 0: specnum = -specnum
    if "-spn" in sys.argv:
        ind = sys.argv.index("-spn")
        spec = sys.argv[ind + 1]
    if '-f' in sys.argv:
        ind = sys.argv.index('-f')
        sfile = sys.argv[ind + 1]
    if '-sig' in sys.argv: sigma = 1
    if '-typ' in sys.argv:
        ind = sys.argv.index('-typ')
        type = sys.argv[ind + 1]
    if '-F' in sys.argv:
        ind = sys.argv.index('-F')
        anisfile = sys.argv[ind + 1]
    if '-usr' in sys.argv:
        ind = sys.argv.index('-usr')
        user = sys.argv[ind + 1]
    if '-loc' in sys.argv:
        ind = sys.argv.index('-loc')
        location = sys.argv[ind + 1]
    if "-n" in sys.argv:
        name = 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 "-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')
    #
    # get down to bidness
    sfile = dir_path + '/' + sfile
    anisfile = dir_path + '/' + anisfile
    input = open(sfile, 'r')
    AnisRecs = []
    linecnt = 0
    citation = "This study"
    # read in data
    for line in input.readlines():
        AnisRec = {}
        rec = line.split()
        if name == 1:
            k = 1
            spec = rec[0]
        else:
            k = 0
        trace = float(rec[k]) + float(rec[k + 1]) + float(rec[k + 2])
        s1 = '%10.9e' % (old_div(float(rec[k]), trace))
        s2 = '%10.9e' % (old_div(float(rec[k + 1]), trace))
        s3 = '%10.9e' % (old_div(float(rec[k + 2]), trace))
        s4 = '%10.9e' % (old_div(float(rec[k + 3]), trace))
        s5 = '%10.9e' % (old_div(float(rec[k + 4]), trace))
        s6 = '%10.9e' % (old_div(float(rec[k + 5]), trace))
        AnisRec["er_citation_names"] = citation
        AnisRec["er_specimen_name"] = spec
        if specnum != 0:
            AnisRec["er_sample_name"] = spec[:specnum]
        else:
            AnisRec["er_sample_name"] = spec
        if samp_con == "6":
            for samp in Samps:
                if samp['er_sample_name'] == AnisRec["er_sample_name"]:
                    sitename = samp['er_site_name']
                    location = samp['er_location_name']
        elif samp_con != "":
            sitename = pmag.parse_site(AnisRec['er_sample_name'], samp_con, Z)
        AnisRec["er_location_name"] = location
        AnisRec["er_site_name"] = sitename
        AnisRec["er_anylist_mail_names"] = user
        if type == 'AMS':
            AnisRec["anisotropy_type"] = "AMS"
            AnisRec["magic_experiment_names"] = spec + ":LP-X"
        else:
            AnisRec["anisotropy_type"] = type
            AnisRec["magic_experiment_names"] = spec + ":LP-" + type
        AnisRec["anisotropy_s1"] = s1
        AnisRec["anisotropy_s2"] = s2
        AnisRec["anisotropy_s3"] = s3
        AnisRec["anisotropy_s4"] = s4
        AnisRec["anisotropy_s5"] = s5
        AnisRec["anisotropy_s6"] = s6
        if sigma == 1:
            AnisRec["anisotropy_sigma"] = '%10.8e' % (old_div(
                float(rec[k + 6]), trace))
        AnisRec["anisotropy_unit"] = 'SI'
        AnisRec["anisotropy_tilt_correction"] = coord
        AnisRec["magic_method_codes"] = 'LP-' + type
        AnisRecs.append(AnisRec)
    pmag.magic_write(anisfile, AnisRecs, 'rmag_anisotropy')
    print('data saved in ', anisfile)
Exemplo n.º 27
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] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name = sample name
            [6] site name entered in site_name column in the orient.txt format input file  -- NOT CURRENTLY SUPPORTED
            [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
            NB: 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, 'r')
    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)
Exemplo n.º 28
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 = .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
Exemplo n.º 29
0
def main():
    """
    NAME
        atrm_magic.py

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

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

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

    DESCRIPTION
        Reads in a pmag_specimen formatted file and separates it into different components (A,B...etc.)

    SYNTAX 
        sort_specimens.py [-h] [command line options]

    INPUT
        takes pmag_specimens.txt formatted input file

    OPTIONS
        -h: prints help message and quits
        -f FILE: specify input file, default is 'pmag_specimens.txt'

    OUTPUT
        makes pmag_specimen formatted files with input filename plus _X_Y 
        where X is the component name and Y is s,g,t for coordinate system
    """
    dir_path='.'
    inspec="pmag_specimens.txt"
    if '-WD' in sys.argv:
        ind=sys.argv.index('-WD')
        dir_path=sys.argv[ind+1]
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        inspec=sys.argv[ind+1]
    basename=inspec.split('.')[:-1]
    inspec=dir_path+"/"+inspec
    ofile_base=dir_path+"/"+basename[0]
#
# read in data
#
    prior_spec_data,file_type=pmag.magic_read(inspec)
    if file_type != 'pmag_specimens':
        print(file_type, " this is not a valid pmag_specimens file")
        sys.exit()
# get list of specimens in file, components, coordinate systems available
    specs,comps,coords=[],[],[]
    for spec in prior_spec_data:
        if spec['er_specimen_name'] not in specs:specs.append(spec['er_specimen_name'])
        if 'specimen_comp_name' not in list(spec.keys()):spec['specimen_comp_name']='A'
        if 'specimen_tilt_correction'  not in list(spec.keys()):spec['tilt_correction']='-1' # assume specimen coordinates
        if spec['specimen_comp_name'] not in comps:comps.append(spec['specimen_comp_name'])
        if spec['specimen_tilt_correction'] not in coords:coords.append(spec['specimen_tilt_correction'])
# work on separating out components, coordinate systems by specimen
    for coord in coords:
        print(coord)
        for comp in comps:
            print(comp)
            speclist=[]
            for spec in prior_spec_data:
                if spec['specimen_tilt_correction']==coord and spec['specimen_comp_name']==comp:speclist.append(spec)
            ofile=ofile_base+'_'+coord+'_'+comp+'.txt'
            pmag.magic_write(ofile,speclist,'pmag_specimens')
            print('coordinate system: ',coord,' component name: ',comp,' saved in ',ofile)
Exemplo n.º 31
0
def main():
    """
    NAME
        customize_criteria.py

    DESCRIPTION
        Allows user to specify acceptance criteria, saves them in pmag_criteria.txt

    SYNTAX
        customize_criteria.py [-h][command line options]

    OPTIONS
        -h prints help message and quits
        -f IFILE, reads in existing criteria
        -F OFILE, writes to pmag_criteria format file

    DEFAULTS
         IFILE: pmag_criteria.txt
         OFILE: pmag_criteria.txt
  
    OUTPUT
        creates a pmag_criteria.txt formatted output file
    """
    infile,critout="","pmag_criteria.txt"
# parse command line options
    if  '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        infile=sys.argv[ind+1]
        crit_data,file_type=pmag.magic_read(infile)
        if file_type!='pmag_criteria':
            print 'bad input file'
            print main.__doc__
            sys.exit()
        print "Acceptance criteria read in from ", infile
    if '-F' in sys.argv:
        ind=sys.argv.index('-F')
        critout=sys.argv[ind+1]
    Dcrit,Icrit,nocrit=0,0,0
    custom='1'
    crit=raw_input(" [0] Use no acceptance criteria?\n [1] Use default criteria\n [2] customize criteria \n ")
    if crit=='0':
        print 'Very very loose criteria saved in ',critout
        crit_data=pmag.default_criteria(1)
        pmag.magic_write(critout,crit_data,'pmag_criteria')
        sys.exit()
    crit_data=pmag.default_criteria(0)
    if crit=='1':
        print 'Default criteria saved in ',critout
        pmag.magic_write(critout,crit_data,'pmag_criteria')
        sys.exit()
    CritRec=crit_data[0]
    crit_keys=CritRec.keys()
    crit_keys.sort()
    print "Enter new threshold value.\n Return to keep default.\n Leave blank to not use as a criterion\n "
    for key in crit_keys:
        if key!='pmag_criteria_code' and key!='er_citation_names' and key!='criteria_definition' and CritRec[key]!="":
            print key, CritRec[key]
            new=raw_input('new value: ')
            if new != "": CritRec[key]=(new)
    pmag.magic_write(critout,[CritRec],'pmag_criteria')
    print "Criteria saved in pmag_criteria.txt"
Exemplo n.º 32
0
def main():
    """
    NAME
        hysteresis_magic.py

    DESCRIPTION
        calculates hystereis parameters and saves them in rmag_hystereis format file
        makes plots if option selected

    SYNTAX
        hysteresis_magic.py [command line options]

    OPTIONS
        -h prints help message and quits
        -usr USER:   identify user, default is ""
        -f: specify input file, default is agm_measurements.txt
        -fh: specify rmag_hysteresis.txt input file
        -F: specify output file, default is rmag_hysteresis.txt
        -P: do not make the plots
        -spc SPEC: specify specimen name to plot and quit
        -sav save all plots and quit
        -fmt [png,svg,eps,jpg]
    """
    args=sys.argv
    PLT=1
    plots=0
    user,meas_file,rmag_out,rmag_file="","agm_measurements.txt","rmag_hysteresis.txt",""
    pltspec=""
    dir_path='.'
    fmt='svg'
    verbose=pmagplotlib.verbose
    version_num=pmag.get_version()
    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=args[ind+1]
    if '-F' in args:
        ind=args.index("-F")
        rmag_out=args[ind+1]
    if '-fh' in args:
        ind=args.index("-fh")
        rmag_file=args[ind+1]
        rmag_file=dir_path+'/'+rmag_file
    if '-P' in args:
        PLT=0
        irm_init,imag_init=-1,-1
    if '-sav' in args:
        verbose=0
        plots=1
    if '-spc' in args:
        ind=args.index("-spc")
        pltspec= args[ind+1]
        verbose=0
        plots=1
    if '-fmt' in args:
        ind=args.index("-fmt")
        fmt=args[ind+1]
    rmag_out=dir_path+'/'+rmag_out
    meas_file=dir_path+'/'+meas_file
    rmag_rem=dir_path+"/rmag_remanence.txt"
    #
    #
    meas_data,file_type=pmag.magic_read(meas_file)
    if file_type!='magic_measurements':
        print(main.__doc__)
        print('bad file')
        sys.exit()
    #
    # initialize some variables
    # define figure numbers for hyst,deltaM,DdeltaM curves
    HystRecs,RemRecs=[],[]
    HDD={}
    if verbose:
        if verbose and PLT:print("Plots may be on top of each other - use mouse to place ")
    if PLT:
        HDD['hyst'],HDD['deltaM'],HDD['DdeltaM']=1,2,3
        pmagplotlib.plot_init(HDD['DdeltaM'],5,5)
        pmagplotlib.plot_init(HDD['deltaM'],5,5)
        pmagplotlib.plot_init(HDD['hyst'],5,5)
        imag_init=0
        irm_init=0
    else:
        HDD['hyst'],HDD['deltaM'],HDD['DdeltaM'],HDD['irm'],HDD['imag']=0,0,0,0,0
    #
    if rmag_file!="":hyst_data,file_type=pmag.magic_read(rmag_file)
    #
    # get list of unique experiment names and specimen names
    #
    experiment_names,sids=[],[]
    for rec in meas_data:
      meths=rec['magic_method_codes'].split(':')
      methods=[]
      for meth in meths:
        methods.append(meth.strip())
      if 'LP-HYS' in methods:
        if 'er_synthetic_name' in list(rec.keys()) and rec['er_synthetic_name']!="":
            rec['er_specimen_name']=rec['er_synthetic_name']
        if rec['magic_experiment_name'] not in experiment_names:experiment_names.append(rec['magic_experiment_name'])
        if rec['er_specimen_name'] not in sids:sids.append(rec['er_specimen_name'])
    #
    k=0
    locname=''
    if pltspec!="":
        k=sids.index(pltspec)
        print(sids[k])
    while k < len(sids):
        s=sids[k]
        if verbose and PLT:print(s, k+1 , 'out of ',len(sids))
    #
    #
        B,M,Bdcd,Mdcd=[],[],[],[] #B,M for hysteresis, Bdcd,Mdcd for irm-dcd data
        Bimag,Mimag=[],[] #Bimag,Mimag for initial magnetization curves
        first_dcd_rec,first_rec,first_imag_rec=1,1,1
        for rec in  meas_data:
            methcodes=rec['magic_method_codes'].split(':')
            meths=[]
            for meth in methcodes:
                meths.append(meth.strip())
            if rec['er_specimen_name']==s and "LP-HYS" in meths:
                B.append(float(rec['measurement_lab_field_dc']))
                M.append(float(rec['measurement_magn_moment']))
                if first_rec==1:
                    e=rec['magic_experiment_name']
                    HystRec={}
                    first_rec=0
                    if "er_location_name" in list(rec.keys()):
                        HystRec["er_location_name"]=rec["er_location_name"]
                        locname=rec['er_location_name'].replace('/','-')
                    if "er_sample_name" in list(rec.keys()):HystRec["er_sample_name"]=rec["er_sample_name"]
                    if "er_site_name" in list(rec.keys()):HystRec["er_site_name"]=rec["er_site_name"]
                    if "er_synthetic_name" in list(rec.keys()) and rec['er_synthetic_name']!="":
                        HystRec["er_synthetic_name"]=rec["er_synthetic_name"]
                    else:
                        HystRec["er_specimen_name"]=rec["er_specimen_name"]
            if rec['er_specimen_name']==s and "LP-IRM-DCD" in meths:
                Bdcd.append(float(rec['treatment_dc_field']))
                Mdcd.append(float(rec['measurement_magn_moment']))
                if first_dcd_rec==1:
                    RemRec={}
                    irm_exp=rec['magic_experiment_name']
                    first_dcd_rec=0
                    if "er_location_name" in list(rec.keys()):RemRec["er_location_name"]=rec["er_location_name"]
                    if "er_sample_name" in list(rec.keys()):RemRec["er_sample_name"]=rec["er_sample_name"]
                    if "er_site_name" in list(rec.keys()):RemRec["er_site_name"]=rec["er_site_name"]
                    if "er_synthetic_name" in list(rec.keys()) and rec['er_synthetic_name']!="":
                        RemRec["er_synthetic_name"]=rec["er_synthetic_name"]
                    else:
                        RemRec["er_specimen_name"]=rec["er_specimen_name"]
            if rec['er_specimen_name']==s and "LP-IMAG" in meths:
                if first_imag_rec==1:
                    imag_exp=rec['magic_experiment_name']
                    first_imag_rec=0
                Bimag.append(float(rec['measurement_lab_field_dc']))
                Mimag.append(float(rec['measurement_magn_moment']))
    #
    # now plot the hysteresis curve
    #
        if len(B)>0:
            hmeths=[]
            for meth in meths: hmeths.append(meth)
            hpars=pmagplotlib.plotHDD(HDD,B,M,e)
            if verbose and PLT:pmagplotlib.drawFIGS(HDD)
    #
    # get prior interpretations from hyst_data
            if rmag_file!="":
                hpars_prior={}
                for rec in hyst_data:
                    if rec['magic_experiment_names']==e:
                        if rec['hysteresis_bcr'] !="" and rec['hysteresis_mr_moment']!="":
                            hpars_prior['hysteresis_mr_moment']=rec['hysteresis_mr_moment']
                            hpars_prior['hysteresis_ms_moment']=rec['hysteresis_ms_moment']
                            hpars_prior['hysteresis_bc']=rec['hysteresis_bc']
                            hpars_prior['hysteresis_bcr']=rec['hysteresis_bcr']
                            break
                if verbose:pmagplotlib.plotHPARS(HDD,hpars_prior,'ro')
            else:
                if verbose:pmagplotlib.plotHPARS(HDD,hpars,'bs')
                HystRec['hysteresis_mr_moment']=hpars['hysteresis_mr_moment']
                HystRec['hysteresis_ms_moment']=hpars['hysteresis_ms_moment']
                HystRec['hysteresis_bc']=hpars['hysteresis_bc']
                HystRec['hysteresis_bcr']=hpars['hysteresis_bcr']
                HystRec['hysteresis_xhf']=hpars['hysteresis_xhf']
                HystRec['magic_experiment_names']=e
                HystRec['magic_software_packages']=version_num
                if hpars["magic_method_codes"] not in hmeths:hmeths.append(hpars["magic_method_codes"])
                methods=""
                for meth in hmeths:
                    methods=methods+meth.strip()+":"
                HystRec["magic_method_codes"]=methods[:-1]
                HystRec["er_citation_names"]="This study"
                HystRecs.append(HystRec)
    #
        if len(Bdcd)>0:
            rmeths=[]
            for meth in meths: rmeths.append(meth)
            if verbose and PLT:print('plotting IRM')
            if irm_init==0:
                HDD['irm']=5
                pmagplotlib.plot_init(HDD['irm'],5,5)
                irm_init=1
            rpars=pmagplotlib.plotIRM(HDD['irm'],Bdcd,Mdcd,irm_exp)
            RemRec['remanence_mr_moment']=rpars['remanence_mr_moment']
            RemRec['remanence_bcr']=rpars['remanence_bcr']
            RemRec['magic_experiment_names']=irm_exp
            if rpars["magic_method_codes"] not in meths:meths.append(rpars["magic_method_codes"])
            methods=""
            for meth in rmeths:
                methods=methods+meth.strip()+":"
            RemRec["magic_method_codes"]=methods[:-1]
            RemRec["er_citation_names"]="This study"
            RemRecs.append(RemRec)
        else:
            if irm_init:pmagplotlib.clearFIG(HDD['irm'])
        if len(Bimag)>0:
            if verbose:print('plotting initial magnetization curve')
# first normalize by Ms
            Mnorm=[]
            for m in Mimag: Mnorm.append(old_div(m,float(hpars['hysteresis_ms_moment'])))
            if imag_init==0:
                HDD['imag']=4
                pmagplotlib.plot_init(HDD['imag'],5,5)
                imag_init=1
            pmagplotlib.plotIMAG(HDD['imag'],Bimag,Mnorm,imag_exp)
        else:
            if imag_init:pmagplotlib.clearFIG(HDD['imag'])
    #
        files={}
        if plots:
            if pltspec!="":s=pltspec
            files={}
            for key in list(HDD.keys()):
                files[key]=locname+'_'+s+'_'+key+'.'+fmt
            pmagplotlib.saveP(HDD,files)
            if pltspec!="":sys.exit()
        if verbose and PLT:
            pmagplotlib.drawFIGS(HDD)
            ans=input("S[a]ve plots, [s]pecimen name, [q]uit, <return> to continue\n ")
            if ans=="a":
                files={}
                for key in list(HDD.keys()):
                    files[key]=locname+'_'+s+'_'+key+'.'+fmt
                pmagplotlib.saveP(HDD,files)
            if ans=='':k+=1
            if ans=="p":
                del HystRecs[-1]
                k-=1
            if  ans=='q':
                print("Good bye")
                sys.exit()
            if ans=='s':
                keepon=1
                specimen=input('Enter desired specimen name (or first part there of): ')
                while keepon==1:
                    try:
                        k =sids.index(specimen)
                        keepon=0
                    except:
                        tmplist=[]
                        for qq in range(len(sids)):
                            if specimen in sids[qq]:tmplist.append(sids[qq])
                        print(specimen," not found, but this was: ")
                        print(tmplist)
                        specimen=input('Select one or try again\n ')
                        k =sids.index(specimen)
        else:
            k+=1
        if len(B)==0 and len(Bdcd)==0:
            if verbose:print('skipping this one - no hysteresis data')
            k+=1
    if rmag_out=="" and ans=='s' and verbose:
        really=input(" Do you want to overwrite the existing rmag_hystersis.txt file? 1/[0] ")
        if really=="":
            print('i thought not - goodbye')
            sys.exit()
        rmag_out="rmag_hysteresis.txt"
    if len(HystRecs)>0:
        pmag.magic_write(rmag_out,HystRecs,"rmag_hysteresis")
        if verbose:print("hysteresis parameters saved in ",rmag_out)
    if len(RemRecs)>0:
        pmag.magic_write(rmag_rem,RemRecs,"rmag_remanence")
        if verbose:print("remanence parameters saved in ",rmag_rem)
Exemplo n.º 33
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)
            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']
            if "anisotropy_n" not in rec.keys(): rec["anisotropy_n"] = "6"
            if "anisotropy_sigma" not in rec.keys():
                rec["anisotropy_sigma"] = "0"
            fpars = pmag.dohext(
                int(rec["anisotropy_n"]) - 6, float(rec["anisotropy_sigma"]),
                s)
            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:
            if pmagplotlib.isServer:
                title = "LO:_" + ResRec[
                    'er_location_names'] + '_SI:_' + site + '_SA:__SP:__CO:_' + crd
            else:
                title = ResRec['er_location_names']
                if site:
                    title += "_{}".format(site)
                title += '_{}'.format(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., 'g')
                pmagplotlib.plotC(ANIS['conf'], PDir, 90., '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 = input("")
                if ans == "q":
                    sys.exit()
                if ans == "e":
                    iboot, ipar, ihext, ivec = 1, 0, 0, 0
                    e = input("Do Hext Statistics  1/[0]: ")
                    if e == "1": ihext = 1
                    e = input("Suppress bootstrap 1/[0]: ")
                    if e == "1": iboot = 0
                    if iboot == 1:
                        e = input("Parametric bootstrap 1/[0]: ")
                        if e == "1": ipar = 1
                        e = 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 = 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 = 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 = 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., 'g')
                    pmagplotlib.plotC(ANIS['conf'], PDir, 90., '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 = 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 = input('Select one or try again\n ')
                            k = sitelist.index(site)
                    goon, ans = 0, ""
                if ans == "a":
                    locs = pmag.makelist(Locs)
                    if pmagplotlib.isServer:  # use server plot naming convention
                        title = "LO:_" + locs + '_SI:__' + '_SA:__SP:__CO:_' + crd
                    else:  # use more readable plot naming convention
                        title = "{}_{}".format(locs, 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 ")
Exemplo n.º 34
0
def main():
    """
    NAME 
        update_measurements.py

    DESCRIPTION
        update the magic_measurements table with new orientation info
 
    SYNTAX
        update_measurements.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f MFILE, specify magic_measurements file; default is magic_measurements.txt
        -fsa SFILE, specify er_samples table; default is er_samples.txt
        -F OFILE, specify output file, default is same as MFILE
    """
    dir_path = '.'
    meas_file = 'magic_measurements.txt'
    samp_file = "er_samples.txt"
    out_file = 'magic_measurements.txt'
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-WD' in sys.argv:
        ind = sys.argv.index('-WD')
        dir_path = sys.argv[ind + 1]
    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 '-F' in sys.argv:
        ind = sys.argv.index('-F')
        out_file = sys.argv[ind + 1]
    # read in measurements file
    meas_file = dir_path + '/' + meas_file
    out_file = dir_path + '/' + out_file
    samp_file = dir_path + '/' + samp_file
    data, file_type = pmag.magic_read(meas_file)
    samps, file_type = pmag.magic_read(samp_file)
    MeasRecs = []
    sampnames, sflag = [], 0
    for rec in data:
        for samp in samps:
            if samp['er_sample_name'].lower() == rec['er_sample_name'].lower():
                if samp['er_sample_name'] not in sampnames:
                    sampnames.append(samp['er_sample_name'].lower())
                rec['er_site_name'] = samp['er_site_name']
                rec['er_location_name'] = samp['er_location_name']
                MeasRecs.append(rec)
                break
        if rec['er_sample_name'].lower() not in sampnames:
            sampnames.append(rec['er_sample_name'].lower())
            sflag = 1
            SampRec = {}
            for key in samps[0].keys():
                SampRec[key] = ""
            SampRec['er_sample_name'] = rec['er_sample_name']
            SampRec['er_citation_names'] = "This study"
            SampRec['er_site_name'] = 'MISSING'
            SampRec['er_location_name'] = 'MISSING'
            SampRec[
                'sample_desription'] = 'recorded added by update_measurements - edit as needed'
            samps.append(SampRec)
            print rec[
                'er_sample_name'], ' missing from er_samples.txt file - edit orient.txt file and re-import'
            rec['er_site_name'] = 'MISSING'
            rec['er_location_name'] = 'MISSING'
            MeasRecs.append(rec)
    pmag.magic_write(out_file, MeasRecs, 'magic_measurements')
    print "updated measurements file stored in ", out_file
    if sflag == 1:
        pmag.magic_write(samp_file, samps, 'er_samples')
        print "updated sample file stored in ", samp_file
Exemplo n.º 35
0
def main():
    """
    NAME
        trmaq_magic.py

    DESCTIPTION
        does non-linear trm acquisisiton correction
  
    SYNTAX
        trmaq_magic.py [-h][-i][command line options]

    OPTIONS
        -h prints help message and quits
        -i allows interactive setting of file names
        -f MFILE, sets magic_measurements input file
        -ft TSPEC, sets thellier_specimens input file
        -F OUT, sets output for non-linear TRM acquisition corrected data


    DEFAULTS
        MFILE: trmaq_measurements.txt
        TSPEC: thellier_specimens.txt
        OUT: NLT_specimens.txt
    """
    meas_file='trmaq_measurements.txt'
    tspec="thellier_specimens.txt"
    output='NLT_specimens.txt'
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-i' in sys.argv:
        meas_file=raw_input("Input magic_measurements file name? [trmaq_measurements.txt] ")
        if meas_file=="":meas_file="trmaq_measurements.txt" 
        tspec=raw_input(" thellier_specimens file name? [thellier_specimens.txt] ")
        if tspec=="":tspec="thellier_specimens.txt" 
        output=raw_input("File for non-linear TRM adjusted specimen data: [NLTspecimens.txt] ")
        if output=="":output="NLT_specimens.txt"
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        meas_file=sys.argv[ind+1]
    if '-ft' in sys.argv:
        ind=sys.argv.index('-ft')
        tspec=sys.argv[ind+1]
    if '-F' in sys.argv:
        ind=sys.argv.index('-F')
        output=sys.argv[ind+1]
    # 
    PLT={'aq':1}
    pmagplotlib.plot_init(PLT['aq'],5,5)
    #
    # get name of file from command line
    #
    comment=""
    #
    #
    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()
    sids=pmag.get_specs(meas_data)
    specimen=0
    #
    # read in thellier_specimen data
    #
    nrm,file_type=pmag.magic_read(tspec)
    PmagSpecRecs=[]
    while specimen < len(sids):
    #
    # find corresoponding paleointensity data for this specimen
    #
        s=sids[specimen]
        blab,best="",""
        for nrec in nrm:   # pick out the Banc data for this spec
            if nrec["er_specimen_name"]==s:
                blab=float(nrec["specimen_lab_field_dc"])
                best=float(nrec["specimen_int"])
                TrmRec=nrec
                break
        if blab=="":
            print "skipping ",s," : no best "
            specimen+=1
        else:
            print sids[specimen],specimen+1, 'of ', len(sids),'Best = ',best*1e6
            MeasRecs=[]
    #
    # find the data from the meas_data file for this specimen
    #
            for rec in meas_data:
                if rec["er_specimen_name"]==s:
                    meths=rec["magic_method_codes"].split(":")
                    methcodes=[]
                    for meth in meths:
                        methcodes.append(meth.strip()) 
                    if "LP-TRM" in methcodes: MeasRecs.append(rec)
            if len(MeasRecs) <2:
               specimen+=1
               print 'skipping specimen -  no trm acquisition data ', s
    #
    #  collect info for the PmagSpecRec dictionary
    #
            else:
                TRMs,Bs=[],[]
                for rec in MeasRecs:
                    Bs.append(float(rec['treatment_dc_field']))
                    TRMs.append(float(rec['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(PLT['aq'],Bs,TRMs,Bp,Mp,NLpars,rec['magic_experiment_name'])
                pmagplotlib.drawFIGS(PLT)
                print 'Banc= ',float(NLpars['banc'])*1e6
                trmTC={}
                for key in TrmRec.keys():
                    trmTC[key]=TrmRec[key] # copy of info from thellier_specimens record
                trmTC['specimen_int']='%8.3e'%(NLpars['banc'])
                trmTC['magic_method_codes']=TrmRec["magic_method_codes"]+":DA-NL"
                PmagSpecRecs.append(trmTC)
                ans=raw_input("Return for next specimen, s[a]ve plot  ")
                if ans=='a':
                    Name={'aq':rec['er_specimen_name']+'_TRM.svg'}
                    pmagplotlib.saveP(PLT,Name)
                specimen+=1
    pmag.magic_write(output,PmagSpecRecs,'pmag_specimens')
Exemplo n.º 36
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
        -n 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 '-n' in args:
        ind = args.index('-n')
        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)
            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']
            if "anisotropy_n" not in rec.keys():
                rec["anisotropy_n"] = "6"
            if "anisotropy_sigma" not in rec.keys():
                rec["anisotropy_sigma"] = "0"
            fpars = pmag.dohext(
                int(rec["anisotropy_n"])-6, float(rec["anisotropy_sigma"]), s)
            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:
            if pmagplotlib.isServer:
                title = "LO:_"+ResRec['er_location_names'] + \
                    '_SI:_'+site+'_SA:__SP:__CO:_'+crd
            else:
                title = ResRec['er_location_names']
                if site:
                    title += "_{}".format(site)
                title += '_{}'.format(crd)
            ResRec['er_location_names'] = pmag.makelist(Locs)
            bpars, hpars = pmagplotlib.plot_anis(
                ANIS, Ss, iboot, ihext, ivec, ipar, title, iplot, comp, vec, Dir, nb)
            if len(PDir) > 0:
                pmagplotlib.plot_circ(ANIS['data'], PDir, 90., 'g')
                pmagplotlib.plot_circ(ANIS['conf'], PDir, 90., 'g')
            if verbose and plots == 0:
                pmagplotlib.draw_figs(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"] = ''
                # regular bootstrap
                BootRec["magic_method_codes"] = 'LP-AN:AE-H:AE-BS'
                if ipar == 1:
                    # parametric bootstrap
                    BootRec["magic_method_codes"] = 'LP-AN:AE-H:AE-BS-P'
                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 = input("")
                if ans == "q":
                    sys.exit()
                if ans == "e":
                    iboot, ipar, ihext, ivec = 1, 0, 0, 0
                    e = input("Do Hext Statistics  1/[0]: ")
                    if e == "1":
                        ihext = 1
                    e = input("Suppress bootstrap 1/[0]: ")
                    if e == "1":
                        iboot = 0
                    if iboot == 1:
                        e = input("Parametric bootstrap 1/[0]: ")
                        if e == "1":
                            ipar = 1
                        e = 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.plot_anis(
                        ANIS, Ss, iboot, ihext, ivec, ipar, title, iplot, comp, vec, Dir, nb)
                    if verbose and plots == 0:
                        pmagplotlib.draw_figs(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 = 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 = 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.plot_anis(
                        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 = 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.plot_circ(ANIS['data'], PDir, 90., 'g')
                    pmagplotlib.plot_circ(ANIS['conf'], PDir, 90., 'g')
                    if verbose and plots == 0:
                        pmagplotlib.draw_figs(ANIS)
                if ans == "p":
                    k -= 2
                    goon = 0
                if ans == "q":
                    k = plt
                    goon = 0
                if ans == "s":
                    keepon = 1
                    site = 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 = input('Select one or try again\n ')
                            k = sitelist.index(site)
                    goon, ans = 0, ""
                if ans == "a":
                    locs = pmag.makelist(Locs)
                    if pmagplotlib.isServer:  # use server plot naming convention
                        title = "LO:_"+locs+'_SI:__'+'_SA:__SP:__CO:_'+crd
                    else:  # use more readable plot naming convention
                        title = "{}_{}".format(locs, 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 ")
Exemplo n.º 37
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 -- NOT CURRENTLY SUPPORTED
            [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
Exemplo n.º 38
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
Exemplo n.º 39
0
def main(command_line=True, **kwargs):
    """
    NAME
        generic_magic.py

    DESCRIPTION
        converts magnetometer files in generic format to MagIC measurements format

    SYNTAX
        generic_magic.py [command line options]

    OPTIONS
        -h
            prints the help message and quits.
        -usr USER
            identify user, default is ""
        -f FILE:
            specify path to input file, required
        -fsa SAMPFILE:
            specify the samples file for sample orientation data. default is er_samples.txt
        -F FILE
            specify output file, default is magic_measurements.txt

        -Fsa FILE
            specify output file, default is er_samples.txt

        -exp EXPERIMENT-TYPE
            Demag:
                AF and/or Thermal
            PI:
                paleointenisty thermal experiment (ZI/IZ/IZZI)
            ATRM n:

                ATRM in n positions (n=6)

            AARM n:
                AARM in n positions
            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 rates in [K/minutes], seperated by comma, ordered at the same order as XXX.10,XXX.20 ...XX.70

                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 averaged.
                But users need to make sure that there are no duplicate meaurements in the file

            NLT:
                non-linear-TRM experiment

        -samp X Y
            specimen-sample naming convention.
            X determines which kind of convention (initial characters, terminal characters, or delimiter
            Y determines how many characters to remove to go from specimen --> sample OR which delimiter to use
            X=0 Y=n: specimen is distinguished from sample by n initial characters.
                     (example: "generic_magic.py -samp 0 4"
                      if n=4 then and specimen = mgf13a then sample = mgf13)
            X=1 Y=n: specimen is distiguished from sample by n terminate characters.
                     (example: "generic_magic.py -samp 1 1)
                      if n=1 then and specimen = mgf13a then sample = mgf13)
            X=2 Y=c: specimen is distinguishing from sample by a delimiter.
                     (example: "generic_magic.py -samp 2 -"
                      if c=- then and specimen = mgf13-a then sample = mgf13)
            default: sample is the same as specimen name

        -site X Y
            sample-site naming convention.
            X determines which kind of convention (initial characters, terminal characters, or delimiter
            Y determines how many characters to remove to go from sample --> site OR which delimiter to use
            X=0 Y=n: sample is distiguished from site by n initial characters.
                     (example: "generic_magic.py --site 0 3"
                      if n=3 then and sample = mgf13 then sample = mgf)
            X=1 Y=n: sample is distiguished from site by n terminate characters.
                     (example: "generic_magic.py --site 1 2"
                      if n=2 and sample = mgf13 then site = mgf)
            X=2 Y=c: specimen is distiguishing from sample by a delimiter.
                     (example: "generic_magic.py -site 2 -"
                      if c='-' and sample = 'mgf-13' then site = mgf)
            default: site name is the same as sample name

        -loc LOCNAM
            specify location/study name.

        -dc B PHI THETA:
            B: dc lab field (in micro tesla)
            PHI (declination). takes numbers from 0 to 360
            THETA (inclination). takes numbers from -90 to 90

            NB: use PHI, THETA = -1 -1 to signal that it changes, i.e. in anisotropy experiment.

        -A: don't average replicate measurements. Take the last measurement from replicate measurements.

        -WD working directory

    INPUT

        A generic file is a tab-delimited file. Each column should have a header.
        The file must include the following headers (the order of the columns is not important):

            specimen
                string specifying specimen name

            treatment:
                a number with one or two decimal point (X.Y)
                coding for thermal demagnetization:
                    0.0 or 0 is NRM.
                    X is temperature in celsius
                    Y is always 0
                coding for AF demagnetization:
                    0.0 or 0 is NRM.
                    X is AF peak field in mT
                    Y is always 0
                coding for Thellier-type experiment:
                    0.0 or 0 is NRM
                    X is temperature in celsius
                    Y=0: zerofield
                    Y=1: infield
                    Y=2: pTRM check
                    Y=3: pTRM tail check
                    Y=4: Additivity check
                    # Ron, Add also 5 for Thellier protocol
                coding for ATRM experiment (6 poitions):
                    X is temperature in celsius
                    Y=0: zerofield baseline to be subtracted
                    Y=1: +x
                    Y=2: -x
                    Y=3: +y
                    Y=4: -y
                    Y=5: +z
                    Y=6: -z
                    Y=7: alteration check
                coding for NLT experiment:
                    X is temperature in celsius
                    Y=0: zerofield baseline to be subtracted
                    Y!=0: oven field  in microT
                coding for CR experiment:
                    see "OPTIONS" list above

            treatment_type:
                N: NRM
                A: AF
                T: Thermal

            moment:
                magnetic moment in emu !!

        In addition, at least one of the following headers are required:

            dec_s:
                declination in specimen coordinate system (0 to 360)
            inc_s:
                inclination in specimen coordinate system (-90 to 90)

            dec_g:
                declination in geographic coordinate system (0 to 360)
            inc_g:
                inclination in geographic coordinate system (-90 to 90)

            dec_t:
                declination in tilt-corrected coordinate system (0 to 360)
            inc_t:
                inclination in tilt-corrected coordinate system (-90 to 90)

    """

    #--------------------------------------
    # functions
    #--------------------------------------


    def sort_magic_file(path,ignore_lines_n,sort_by_this_name):
        '''
        reads a file with headers. Each line is stored as a dictionary following the headers.
        Lines are sorted in DATA by the sort_by_this_name header
        DATA[sort_by_this_name]=[dictionary1,dictionary2,...]
        '''
        DATA={}
        fin=open(path,'r')
        #ignore first lines
        for i in range(ignore_lines_n):
            fin.readline()
        #header
        line=fin.readline()
        header=line.strip('\n').split('\t')
        #print header
        for line in fin.readlines():
            if line[0]=="#":
                continue
            tmp_data={}
            tmp_line=line.strip('\n').split('\t')
            #print tmp_line
            for i in range(len(tmp_line)):
                if i>= len(header):
                    continue
                tmp_data[header[i]]=tmp_line[i]
            DATA[tmp_data[sort_by_this_name]]=tmp_data
        fin.close()
        return(DATA)

    def read_generic_file(path,average_replicates):
        '''
        reads a generic file format. If average_replicates==True average replicate measurements.
        Rrturns a Data dictionary with measurements line sorted by specimen
        Data[specimen_name][dict1,dict2,...]
        '''
        Data={}
        Fin=open(path,'r')
        header=Fin.readline().strip('\n').split('\t')
        duplicates=[]
        for line in Fin.readlines():
            tmp_data={}
            #found_duplicate=False
            l=line.strip('\n').split('\t')
            for i in range(min(len(header),len(l))):
                tmp_data[header[i]]=l[i]
            specimen=tmp_data['specimen']
            if specimen not in list(Data.keys()):
                Data[specimen]=[]
            Data[specimen].append(tmp_data)
        # search fro duplicates
        for specimen in list(Data.keys()):
            x=len(Data[specimen])-1
            new_data=[]
            duplicates=[]
            for i in range(1,x):
                while i< len(Data[specimen]) and Data[specimen][i]['treatment']==Data[specimen][i-1]['treatment'] and Data[specimen][i]['treatment_type']==Data[specimen][i-1]['treatment_type']:
                   duplicates.append(Data[specimen][i])
                   del(Data[specimen][i])
                if len(duplicates)>0:
                   if average_replicates:
                       duplicates.append(Data[specimen][i-1])
                       Data[specimen][i-1]=average_duplicates(duplicates)
                       print("-W- WARNING: averaging %i duplicates for specimen %s treatmant %s"%(len(duplicates),specimen,duplicates[-1]['treatment']))
                       duplicates=[]
                   else:
                       Data[specimen][i-1]=duplicates[-1]
                       print("-W- WARNING: found %i duplicates for specimen %s treatmant %s. Taking the last measurement only"%(len(duplicates),specimen,duplicates[-1]['treatment']))
                       duplicates=[]

                if i==len(Data[specimen])-1:
                    break



            #    if tmp_data['treatment']==Data[specimen][-1]['treatment'] and tmp_data['treatment_type']==Data[specimen][-1]['treatment_type']:
            #
            ## check replicates
            #if tmp_data['treatment']==Data[specimen][-1]['treatment'] and tmp_data['treatment_type']==Data[specimen][-1]['treatment_type']:
            #    #found_duplicate=True
            #    duplicates.append(Data[specimen][-1])
            #    duplicates.append(tmp_data)
            #    del(Data[specimen][-1])
            #    continue
            #else:
            #    if len(duplicates)>0:
            #        if average_replicates:
            #            Data[specimen].append(average_duplicates(duplicates))
            #            print "-W- WARNING: averaging %i duplicates for specimen %s treatmant %s"%(len(duplicates),specimen,duplicates[-1]['treatment'])
            #        else:
            #            Data[specimen].append(duplicates[-1])
            #            print "-W- WARNING: found %i duplicates for specimen %s treatmant %s. Taking the last measurement only"%(len(duplicates),specimen,duplicates[-1]['treatment'])
            #    duplicates=[]
            #    Data[specimen].append(tmp_data)


        return(Data)

    def average_duplicates(duplicates):
        '''
        avarage replicate measurements.
        '''
        carts_s,carts_g,carts_t=[],[],[]
        for rec in duplicates:
            moment=float(rec['moment'])
            if 'dec_s' in list(rec.keys()) and 'inc_s' in list(rec.keys()):
                if rec['dec_s']!="" and rec['inc_s']!="":
                    dec_s=float(rec['dec_s'])
                    inc_s=float(rec['inc_s'])
                    cart_s=pmag.dir2cart([dec_s,inc_s,moment])
                    carts_s.append(cart_s)
            if 'dec_g' in list(rec.keys()) and 'inc_g' in list(rec.keys()):
                if rec['dec_g']!="" and rec['inc_g']!="":
                    dec_g=float(rec['dec_g'])
                    inc_g=float(rec['inc_g'])
                    cart_g=pmag.dir2cart([dec_g,inc_g,moment])
                    carts_g.append(cart_g)
            if 'dec_t' in list(rec.keys()) and 'inc_t' in list(rec.keys()):
                if rec['dec_t']!="" and rec['inc_t']!="":
                    dec_t=float(rec['dec_t'])
                    inc_t=float(rec['inc_t'])
                    cart_t=pmag.dir2cart([dec_t,inc_t,moment])
                    carts_t.append(cart_t)
        if len(carts_s)>0:
            carts=scipy.array(carts_s)
            x_mean=scipy.mean(carts[:,0])
            y_mean=scipy.mean(carts[:,1])
            z_mean=scipy.mean(carts[:,2])
            mean_dir=pmag.cart2dir([x_mean,y_mean,z_mean])
            mean_dec_s="%.2f"%mean_dir[0]
            mean_inc_s="%.2f"%mean_dir[1]
            mean_moment="%10.3e"%mean_dir[2]
        else:
            mean_dec_s,mean_inc_s="",""
        if len(carts_g)>0:
            carts=scipy.array(carts_g)
            x_mean=scipy.mean(carts[:,0])
            y_mean=scipy.mean(carts[:,1])
            z_mean=scipy.mean(carts[:,2])
            mean_dir=pmag.cart2dir([x_mean,y_mean,z_mean])
            mean_dec_g="%.2f"%mean_dir[0]
            mean_inc_g="%.2f"%mean_dir[1]
            mean_moment="%10.3e"%mean_dir[2]
        else:
            mean_dec_g,mean_inc_g="",""

        if len(carts_t)>0:
            carts=scipy.array(carts_t)
            x_mean=scipy.mean(carts[:,0])
            y_mean=scipy.mean(carts[:,1])
            z_mean=scipy.mean(carts[:,2])
            mean_dir=pmag.cart2dir([x_mean,y_mean,z_mean])
            mean_dec_t="%.2f"%mean_dir[0]
            mean_inc_t="%.2f"%mean_dir[1]
            mean_moment="%10.3e"%mean_dir[2]
        else:
            mean_dec_t,mean_inc_t="",""

        meanrec={}
        for key in list(duplicates[0].keys()):
            if key in ['dec_s','inc_s','dec_g','inc_g','dec_t','inc_t','moment']:
                continue
            else:
                meanrec[key]=duplicates[0][key]
        meanrec['dec_s']=mean_dec_s
        meanrec['dec_g']=mean_dec_g
        meanrec['dec_t']=mean_dec_t
        meanrec['inc_s']=mean_inc_s
        meanrec['inc_g']=mean_inc_g
        meanrec['inc_t']=mean_inc_t
        meanrec['moment']=mean_moment
        return meanrec

    def get_upper_level_name(name,nc):
        '''
        get sample/site name from specimen/sample using naming convention
        '''
        if float(nc[0])==0:
            if float(nc[1])!=0:
                number_of_char=int(nc[1])
                high_name=name[:number_of_char]
            else:
                high_name=name
        elif float(nc[0])==1:
            if float(nc[1])!=0:
                number_of_char=int(nc[1])*-1
                high_name=name[:number_of_char]
            else:
                high_name=name
        elif float(nc[0])==2:
            d=str(nc[1])
            name_splitted=name.split(d)
            if len(name_splitted)==1:
                high_name=name_splitted[0]
            else:
                high_name=d.join(name_splitted[:-1])
        else:
            high_name=name
        return high_name

    def merge_pmag_recs(old_recs):
        recs={}
        recs=copy.deepcopy(old_recs)
        headers=[]
        for rec in recs:
            for key in list(rec.keys()):
                if key not in headers:
                    headers.append(key)
        for rec in recs:
            for header in headers:
                if header not in list(rec.keys()):
                    rec[header]=""
        return recs


    # initialize some variables
    experiment = ''
    sample_nc = [1, 0]
    site_nc = [1, 0]
    meas_file = "magic_measurements.txt"
    labfield = 0

    #--------------------------------------
    # get command line arguments
    #--------------------------------------

    if command_line:
        args=sys.argv
        user=""
        if "-h" in args:
            print(main.__doc__)
            return False
        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 '-Fsa' in args:
            ind=args.index("-Fsa")
            samp_file=args[ind+1]
        else:
            samp_file="er_samples.txt"

        if '-f' in args:
            ind=args.index("-f")
            magfile=args[ind+1]

        if "-dc" in args:
            ind=args.index("-dc")
            labfield=float(args[ind+1])*1e-6
            labfield_phi=float(args[ind+2])
            labfield_theta=float(args[ind+3])
        if '-exp' in args:
            ind=args.index("-exp")
            experiment=args[ind+1]
        if "-samp" in args:
            ind=args.index("-samp")
            sample_nc=[]
            sample_nc.append(args[ind+1])
            sample_nc.append(args[ind+2])
        if "-site" in args:
            ind=args.index("-site")
            site_nc=[]
            site_nc.append(args[ind+1])
            site_nc.append(args[ind+2])
        if "-loc" in args:
            ind=args.index("-loc")
            er_location_name=args[ind+1]
        else:
            er_location_name=""
        if "-A" in args:
            noave=1
        else:
            noave=0

        if "-WD" in args:
            ind=args.index("-WD")
            WD=args[ind+1]
            os.chdir(WD)

    # unpack keyword args if using as module
    if not command_line:
        user = kwargs.get('user', '')
        meas_file = kwargs.get('meas_file', 'magic_measurements.txt')
        samp_file = kwargs.get('samp_file', 'er_samples.txt')
        magfile = kwargs.get('magfile', '')
        labfield = int(kwargs.get('labfield', 0))
        if labfield:
            labfield *= 1e-6
        labfield_phi = int(kwargs.get('labfield_phi', 0))
        labfield_theta = int(kwargs.get('labfield_theta', 0))
        experiment = kwargs.get('experiment', '')
        cooling_times_list = kwargs.get('cooling_times_list', [])
        sample_nc = kwargs.get('sample_nc', [1, 0])
        site_nc = kwargs.get('site_nc', [1, 0])
        er_location_name = kwargs.get('er_location_name', '')
        noave = kwargs.get('noave', 0) # 0 is default, means do average
        WD = kwargs.get('WD', '.')
        #os.chdir(WD)

    # format and validate variables
    if magfile:
        try:
            input=open(magfile,'r')
        except:
            print("bad mag file:",magfile)
            return False, "bad mag file"
    else:
        print("mag_file field is required option")
        print(main.__doc__)
        return False, "mag_file field is required option"

    if not experiment:
        print("-exp is required option. Please provide experiment type of: Demag, PI, ATRM n (n of positions), CR (see below for format), NLT")
        print(main.__doc__)
        return False, "-exp is required option"

    if experiment=='ATRM':
        if command_line:
            ind=args.index("ATRM")
            atrm_n_pos=int(args[ind+1])
        else:
            atrm_n_pos = 6

    if experiment=='AARM':
        if command_line:
            ind=args.index("AARM")
            aarm_n_pos=int(args[ind+1])
        else:
            aarm_n_pos = 6

    if  experiment=='CR':
        if command_line:
            ind=args.index("CR")
            cooling_times=args[ind+1]
            cooling_times_list=cooling_times.split(',')
        # if not command line, cooling_times_list is already set




    #--------------------------------------
    # read data from er_samples.txt
    #--------------------------------------

    #if "-fsa" in args:
    #   ind=args.index("-fsa")
    #    er_sample_file=args[ind+1]
    #else:
    #    er_sample_file="er_samples.txt"

    er_sample_data={}
    #er_sample_data=sort_magic_file(samp_file,1,'er_sample_name')
    try:
        er_sample_data=sort_magic_file(samp_file,1,'er_sample_name')
        print("-I- Found er_samples.txt")
        print('-I- sample information will be appended to existing er_samples.txt file')
    except:
        print("-I- Cant find file er_samples.txt")
        print('-I- sample information will be stored in new er_samples.txt file')

    #--------------------------------------
    # read data from generic file
    #--------------------------------------

    if  noave:
        mag_data=read_generic_file(magfile,False)
    else:
        mag_data=read_generic_file(magfile,True)

    #--------------------------------------
    # for each specimen get the data, and translate it to MagIC format
    #--------------------------------------

    ErSamplesRecs=[]
    MagRecs=[]
    specimens_list=list(mag_data.keys())
    specimens_list.sort()
    for specimen in specimens_list:
        measurement_running_number=0
        this_specimen_treatments=[] # a list of all treatments
        MagRecs_this_specimen=[]
        LP_this_specimen=[] # a list of all lab protocols
        IZ,ZI=0,0 # counter for IZ and ZI steps

        for meas_line in mag_data[specimen]:

            #------------------
            # trivial MagRec data
            #------------------

            MagRec={}
            MagRec['er_citation_names']="This study"
            MagRec["er_specimen_name"]=meas_line['specimen']
            MagRec["er_sample_name"]=get_upper_level_name(MagRec["er_specimen_name"],sample_nc)
            MagRec["er_site_name"]=get_upper_level_name(MagRec["er_sample_name"],site_nc)
            MagRec['er_location_name']=er_location_name
            MagRec['er_analyst_mail_names']=user
            MagRec["magic_instrument_codes"]=""
            MagRec["measurement_flag"]='g'
            MagRec["measurement_number"]="%i"%measurement_running_number

            MagRec["measurement_magn_moment"]='%10.3e'%(float(meas_line["moment"])*1e-3) # in Am^2
            MagRec["measurement_temp"]='273.' # room temp in kelvin

            #------------------
            #  decode treatments from treatment column in the generic file
            #------------------

            treatment=[]
            treatment_code=str(meas_line['treatment']).split(".")
            treatment.append(float(treatment_code[0]))
            if len(treatment_code)==1:
                treatment.append(0)
            else:
                treatment.append(float(treatment_code[1]))

            #------------------
            #  lab field direction
            #------------------

            if experiment in ['PI','NLT','CR']:

                if float(treatment[1])==0:
                    MagRec["treatment_dc_field"]="0"
                    MagRec["treatment_dc_field_phi"]="0"
                    MagRec["treatment_dc_field_theta"]="0"
                elif not labfield:
                    print("-W- WARNING: labfield (-dc) is a required argument for this experiment type")
                    return False, "labfield (-dc) is a required argument for this experiment type"

                else:
                    MagRec["treatment_dc_field"]='%8.3e'%(float(labfield))
                    MagRec["treatment_dc_field_phi"]="%.2f"%(float(labfield_phi))
                    MagRec["treatment_dc_field_theta"]="%.2f"%(float(labfield_theta))
            else:
                MagRec["treatment_dc_field"]=""
                MagRec["treatment_dc_field_phi"]=""
                MagRec["treatment_dc_field_theta"]=""

            #------------------
            # treatment temperature/peak field
            #------------------

            if experiment == 'Demag':
                if meas_line['treatment_type']=='A':
                    MagRec['treatment_temp']="273."
                    MagRec["treatment_ac_field"]="%.3e"%(treatment[0]*1e-3)
                elif meas_line['treatment_type']=='N':
                    MagRec['treatment_temp']="273."
                    MagRec["treatment_ac_field"]=""
                else:
                    MagRec['treatment_temp']="%.2f"%(treatment[0]+273.)
                    MagRec["treatment_ac_field"]=""
            else:
                    MagRec['treatment_temp']="%.2f"%(treatment[0]+273.)
                    MagRec["treatment_ac_field"]=""


            #---------------------
            # Lab treatment
            # Lab protocol
            #---------------------

            #---------------------
            # Lab treatment and lab protocoal for NRM:
            #---------------------

            if float(meas_line['treatment'])==0:
                LT="LT-NO"
                LP="" # will be filled later after finishing reading all measurements line

            #---------------------
            # Lab treatment and lab protocoal for paleointensity experiment
            #---------------------

            elif experiment =='PI':
                LP="LP-PI-TRM"
                if treatment[1]==0:
                    LT="LT-T-Z"
                elif  treatment[1]==1 or treatment[1]==10: # infield
                    LT="LT-T-I"
                elif treatment[1]==2 or treatment[1]==20:  # pTRM check
                    LT="LT-PTRM-I"
                    LP=LP+":"+"LP-PI-ALT-PTRM"
                elif treatment[1]==3 or treatment[1]==30: # Tail check
                    LT="LT-PTRM-MD"
                    LP=LP+":"+"LP-PI-BT-MD"
                elif treatment[1]==4 or treatment[1]==40: # Additivity check
                    LT="LT-PTRM-AC"
                    LP=LP+":"+"LP-PI-BT-MD"
                else:
                    print("-E- unknown measurement code specimen %s treatmemt %s"%(meas_line['specimen'],meas_line['treatment']))
                    MagRec={}
                    continue
                # save all treatment in a list
                # we will use this later to distinguidh between ZI / IZ / and IZZI

                this_specimen_treatments.append(float(meas_line['treatment']))
                if LT=="LT-T-Z":
                    if float(treatment[0]+0.1) in this_specimen_treatments:
                        LP=LP+":"+"LP-PI-IZ"
                if LT=="LT-T-I":
                    if float(treatment[0]+0.0) in this_specimen_treatments:
                        LP=LP+":"+"LP-PI-ZI"
            #---------------------
            # Lab treatment and lab protocoal for demag experiment
            #---------------------

            elif "Demag" in experiment:
                if meas_line['treatment_type']=='A':
                    LT="LT-AF-Z"
                    LP="LP-DIR-AF"
                else:
                    LT="LT-T-Z"
                    LP="LP-DIR-T"

            #---------------------
            # Lab treatment and lab protocoal for ATRM experiment
            #---------------------

            elif experiment in ['ATRM','AARM']:

                if experiment=='ATRM':
                    LP="LP-AN-TRM"
                    n_pos=atrm_n_pos
                    if n_pos!=6:
                        print("the program does not support ATRM in %i position."%n_pos)
                        continue

                if experiment=='AARM':
                    #MagRec['treatment_temp']="273."
                    #MagRec["treatment_ac_field"]=""
                    LP="LP-AN-ARM"
                    n_pos=aarm_n_pos
                    if n_pos!=6:
                        print("the program does not support AARM in %i position."%n_pos)
                        continue

                if treatment[1]==0:
                    if experiment=='ATRM':
                        LT="LT-T-Z"
                        MagRec['treatment_temp']="%.2f"%(treatment[0]+273.)
                        MagRec["treatment_ac_field"]=""

                    else:
                        LT="LT-AF-Z"
                        MagRec['treatment_temp']="273."
                        MagRec["treatment_ac_field"]="%.3e"%(treatment[0]*1e-3)
                    MagRec["treatment_dc_field"]='0'
                    MagRec["treatment_dc_field_phi"]='0'
                    MagRec["treatment_dc_field_theta"]='0'
                else:
                    if experiment=='ATRM':
                        if float(treatment[1])==70 or float(treatment[1])==7: # alteration check as final measurement
                            LT="LT-PTRM-I"
                        else:
                            LT="LT-T-I"
                    else:
                        LT="LT-AF-I"
                    MagRec["treatment_dc_field"]='%8.3e'%(float(labfield))

                    # 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)
                    tdec=[0,90,0,180,270,0,0,90,0]
                    tinc=[0,0,90,0,0,-90,0,0,90]
                    if treatment[1] < 10:
                        ipos_code=int(treatment[1])-1
                    else:
                        ipos_code=int(old_div(treatment[1],10))-1

                    # (2) using the magnetization
                    if meas_line["dec_s"]!="":
                        DEC=float(meas_line["dec_s"])
                        INC=float(meas_line["inc_s"])
                    elif meas_line["dec_g"]!="":
                        DEC=float(meas_line["dec_g"])
                        INC=float(meas_line["inc_g"])
                    elif meas_line["dec_t"]!="":
                        DEC=float(meas_line["dec_t"])
                        INC=float(meas_line["inc_t"])
                    if DEC<0 and DEC>-359:
                        DEC=360.+DEC

                    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
                    # check it
                    if treatment[1]!= 7 and treatment[1]!= 70:
                        if ipos_guess!=ipos_code:
                            print("-W- WARNING: check specimen %s step %s, anistropy measurements, coding does not match the direction of the lab field"%(specimen,meas_line['treatment']))
                    MagRec["treatment_dc_field_phi"]='%7.1f' %(tdec[ipos])
                    MagRec["treatment_dc_field_theta"]='%7.1f'% (tinc[ipos])


            #---------------------
            # Lab treatment and lab protocoal for cooling rate experiment
            #---------------------

            elif experiment == "CR":

                cooling_times_list
                LP="LP-CR-TRM"
                MagRec["treatment_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin

                if treatment[1]==0:
                    LT="LT-T-Z"
                    MagRec["treatment_dc_field"]="0"
                    MagRec["treatment_dc_field_phi"]='0'
                    MagRec["treatment_dc_field_theta"]='0'
                else:
                    if treatment[1]==7: # alteration check as final measurement
                            LT="LT-PTRM-I"
                    else:
                            LT="LT-T-I"
                    MagRec["treatment_dc_field"]='%8.3e'%(labfield)
                    MagRec["treatment_dc_field_phi"]='%7.1f' % (labfield_phi) # labfield phi
                    MagRec["treatment_dc_field_theta"]='%7.1f' % (labfield_theta) # labfield theta

                    indx=int(treatment[1])-1
                    # alteration check matjed as 0.7 in the measurement file
                    if indx==6:
                        cooling_time= cooling_times_list[-1]
                    else:
                        cooling_time=cooling_times_list[indx]
                    MagRec["measurement_description"]="cooling_rate"+":"+cooling_time+":"+"K/min"


            #---------------------
            # Lab treatment and lab protocoal for NLT experiment
            #---------------------

            elif 'NLT' in experiment :
                print("Dont support yet NLT rate experiment file. Contact [email protected]")

            #---------------------
            # magic_method_codes for this measurement only
            # LP will be fixed after all measurement lines are read
            #---------------------

            MagRec["magic_method_codes"]=LT+":"+LP

            #---------------------
            # Demag experiments only:
            # search if orientation data exists in er_samples.txt
            # if not: create one and save
            #---------------------

            # see if core azimuth and tilt-corrected data are in er_samples.txt
            sample=MagRec["er_sample_name"]
            found_sample_azimuth,found_sample_dip,found_sample_bed_dip_direction,found_sample_bed_dip=False,False,False,False
            if sample in list(er_sample_data.keys()):
                if "sample_azimuth" in list(er_sample_data[sample].keys()) and er_sample_data[sample]['sample_azimuth'] !="":
                    sample_azimuth=float(er_sample_data[sample]['sample_azimuth'])
                    found_sample_azimuth=True
                if "sample_dip" in list(er_sample_data[sample].keys()) and er_sample_data[sample]['sample_dip']!="":
                    sample_dip=float(er_sample_data[sample]['sample_dip'])
                    found_sample_dip=True
                if "sample_bed_dip_direction" in list(er_sample_data[sample].keys()) and er_sample_data[sample]['sample_bed_dip_direction']!="":
                    sample_bed_dip_direction=float(er_sample_data[sample]['sample_bed_dip_direction'])
                    found_sample_bed_dip_direction=True
                if "sample_bed_dip" in list(er_sample_data[sample].keys()) and er_sample_data[sample]['sample_bed_dip']!="":
                    sample_bed_dip=float(er_sample_data[sample]['sample_bed_dip'])
                    found_sample_bed_dip=True
            else:
                er_sample_data[sample]={}

            #--------------------
            # deal with specimen orientation and different coordinate system
            #--------------------

            found_s,found_geo,found_tilt=False,False,False
            if "dec_s" in list(meas_line.keys()) and "inc_s" in list(meas_line.keys()):
                if meas_line["dec_s"]!="" and meas_line["inc_s"]!="":
                    found_s=True
                MagRec["measurement_dec"]=meas_line["dec_s"]
                MagRec["measurement_inc"]=meas_line["inc_s"]
            if "dec_g" in list(meas_line.keys()) and "inc_g" in list(meas_line.keys()):
                if meas_line["dec_g"]!="" and meas_line["inc_g"]!="":
                    found_geo=True
            if "dec_t" in list(meas_line.keys()) and "inc_t" in list(meas_line.keys()):
                if meas_line["dec_t"]!="" and meas_line["inc_t"]!="":
                    found_tilt=True

            #-----------------------------
            # specimen coordinates: no
            # geographic coordinates: yes
            #-----------------------------

            if found_geo and not found_s:
                MagRec["measurement_dec"]=meas_line["dec_g"]
                MagRec["measurement_inc"]=meas_line["inc_g"]

                # core azimuth/plunge is not in er_samples.txt
                if not found_sample_dip or not found_sample_azimuth:
                    er_sample_data[sample]['sample_azimuth']="0"
                    er_sample_data[sample]['sample_dip']="0"

                # core azimuth/plunge is in er_samples.txt
                else:
                    sample_azimuth=float(er_sample_data[sample]['sample_azimuth'])
                    sample_dip=float(er_sample_data[sample]['sample_dip'])
                    if sample_azimuth!=0 and sample_dip!=0:
                        print("-W- WARNING: delete core azimuth/plunge in er_samples.txt\n\
                        becasue dec_s and inc_s are unavaialable")

            #-----------------------------
            # specimen coordinates: no
            # geographic coordinates: no
            #-----------------------------
            if not found_geo and not found_s:
                print("-E- ERROR: sample %s does not have dec_s/inc_s or dec_g/inc_g. Ignore specimen %s "%(sample,specimen))
                break

            #-----------------------------
            # specimen coordinates: yes
            # geographic coordinates: yes
            #
            # commant: Ron, this need to be tested !!
            #-----------------------------
            if found_geo and found_s:

                cdec,cinc=float(meas_line["dec_s"]),float(meas_line["inc_s"])
                gdec,ginc=float(meas_line["dec_g"]),float(meas_line["inc_g"])
                az,pl=pmag.get_azpl(cdec,cinc,gdec,ginc)

                # core azimuth/plunge is not in er_samples.txt:
                # calculate core az/pl and add it to er_samples.txt
                if not found_sample_dip or not found_sample_azimuth:
                    er_sample_data[sample]['sample_azimuth']="%.1f"%az
                    er_sample_data[sample]['sample_dip']="%.1f"%pl

                # core azimuth/plunge is in er_samples.txt
                else:
                    if float(er_sample_data[sample]['sample_azimuth'])!= az:
                        print("-E- ERROR in sample_azimuth sample %s. Check it! using the value in er_samples.txt"%sample)

                    if float(er_sample_data[sample]['sample_dip'])!= pl:
                        print("-E- ERROR in sample_dip sample %s. Check it! using the value in er_samples.txt"%sample)

            #-----------------------------
            # specimen coordinates: yes
            # geographic coordinates: no
            #-----------------------------
            if not found_geo and found_s:
                if found_sample_dip and found_sample_azimuth:
                    pass
                    # (nothing to do)
                else:
                    if "Demag" in experiment:
                        print("-W- WARNING: missing sample_dip or sample_azimuth for sample %s"%sample)

            #-----------------------------
            # tilt-corrected coordinates: yes
            # geographic coordinates: no
            #-----------------------------
            if found_tilt and not found_geo:
                    print("-E- ERROR: missing geographic data for sample %s. Ignoring tilt-corrected data "%sample)
            if found_tilt and found_geo:
                dec_geo,inc_geo=float(meas_line["dec_g"]),float(meas_line["inc_g"])
                dec_tilt,inc_tilt=float(meas_line["dec_t"]),float(meas_line["inc_t"])
                if dec_geo==dec_tilt and inc_geo==inc_tilt:
                    DipDir,Dip=0.,0.
                else:
                    DipDir,Dip=pmag.get_tilt(dec_geo,inc_geo,dec_tilt,inc_tilt)

                if not found_sample_bed_dip_direction or not found_sample_bed_dip:
                    print("-I- calculating dip and dip direction used for tilt correction sample %s. results are put in er_samples.txt"%sample)
                    er_sample_data[sample]['sample_bed_dip_direction']="%.1f"%DipDir
                    er_sample_data[sample]['sample_bed_dip']="%.1f"%Dip

            #-----------------------------
            # er_samples method codes
            # geographic coordinates: no
            #-----------------------------
            if found_tilt or found_geo:
                er_sample_data[sample]['magic_method_codes']="SO-NO"


            #-----------------
            # er_samples_data
            #-----------------
            if sample in list(er_sample_data.keys()):
                er_sample_data[sample]['er_sample_name']=sample
                er_sample_data[sample]['er_site_name']=MagRec["er_site_name"]
                er_sample_data[sample]['er_location_name']=MagRec["er_location_name"]

            #MagRec["magic_method_codes"]=LT
            MagRecs_this_specimen.append(MagRec)

            #if LP!="" and LP not in LP_this_specimen:
            #    LP_this_specimen.append(LP)

            measurement_running_number+=1
            #-------

        #-------
        # after reading all the measurements lines for this specimen
        # 1) add magic_experiment_name
        # 2) fix magic_method_codes with the correct lab protocol
        #-------
        LP_this_specimen=[]
        for MagRec in MagRecs_this_specimen:
            magic_method_codes=MagRec["magic_method_codes"].split(":")
            for code in magic_method_codes:
                if "LP" in code and code not in LP_this_specimen:
                    LP_this_specimen.append(code)
        # check IZ/ZI/IZZI
        if "LP-PI-ZI" in   LP_this_specimen and "LP-PI-IZ" in   LP_this_specimen:
            LP_this_specimen.remove("LP-PI-ZI")
            LP_this_specimen.remove("LP-PI-IZ")
            LP_this_specimen.append("LP-PI-BT-IZZI")

        # add the right LP codes and fix experiment name
        for MagRec in MagRecs_this_specimen:
            MagRec["magic_experiment_name"]=MagRec["er_specimen_name"]+":"+":".join(LP_this_specimen)
            magic_method_codes=MagRec["magic_method_codes"].split(":")
            LT=""
            for code in magic_method_codes:
                if code[:3]=="LT-":
                    LT=code;
                    break
            MagRec["magic_method_codes"]=LT+":"+":".join(LP_this_specimen)

            MagRecs.append(MagRec)

    #--
    # write magic_measurements.txt
    #--
    MagRecs_fixed=merge_pmag_recs(MagRecs)
    pmag.magic_write(meas_file,MagRecs_fixed,'magic_measurements')
    print("-I- MagIC file is saved in  %s"%meas_file)

    #--
    # write er_samples.txt
    #--
    ErSamplesRecs=[]
    samples=list(er_sample_data.keys())
    samples.sort()
    for sample in samples:
        ErSamplesRecs.append(er_sample_data[sample])
    ErSamplesRecs_fixed=merge_pmag_recs(ErSamplesRecs)
    pmag.magic_write(samp_file,ErSamplesRecs_fixed,'er_samples')
    return True, meas_file
Exemplo n.º 40
0
def main():
    """
        NAME
            nrm_specimens_magic.py
    
        DESCRIPTION
            converts NRM data in a magic_measurements type file to 
            geographic and tilt corrected data in a pmag_specimens type file
    
        SYNTAX
           nrm_specimens_magic.py [-h][command line options]
        
        OPTIONS:
            -h prints the help message and quits
            -f MFILE: specify input file
            -fsa SFILE: specify er_samples format file [with orientations]
            -F PFILE: specify output file
            -A  do not average replicate measurements
            -crd [g, t]: specify coordinate system ([g]eographic or [t]ilt adjusted)
                 NB: you must have the  SFILE in this directory

        DEFAULTS
            MFILE: magic_measurements.txt
            PFILE: nrm_specimens.txt
            SFILE: er_samples.txt
            coord: specimen
            average replicate measurements?: YES

        
    """
#
#   define some variables
#
    beg,end,pole,geo,tilt,askave,save=0,0,[],0,0,0,0
    samp_file=1
    args=sys.argv
    geo,tilt,orient=0,0,0
    doave=1
    user,comment,doave,coord="","",1,""
    dir_path='.'
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if '-WD' in sys.argv:
        ind=sys.argv.index('-WD')
        dir_path=sys.argv[ind+1]
    meas_file=dir_path+"/magic_measurements.txt"
    pmag_file=dir_path+"/nrm_specimens.txt"
    samp_file=dir_path+"/er_samples.txt"
    if "-A" in args: doave=0
    if "-f" in args:
        ind=args.index("-f")
        meas_file=sys.argv[ind+1]
    if "-F" in args:
        ind=args.index("-F")
        pmag_file=dir_path+'/'+sys.argv[ind+1]
    speclist=[]
    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,orient=1,1
        if coord=="t":
            tilt,orient,geo=1,1,1
#
# read in data
    if samp_file!="":
        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()
        else: print samp_file,' read in with ',len(samp_data),' records'
    else:
        print 'no orientations - will create file in specimen coordinates'
        geo,tilt,orient=0,0,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()
    #
    if orient==1:
    # set orientation priorities
        SO_methods=[]
        orientation_priorities={'0':'SO-SUN','1':'SO-GPS-DIFF','2':'SO-SIGHT-BACK','3':'SO-CMD-NORTH','4':'SO-MAG'}
        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())
    #
    # sort the sample names
    #
    sids=pmag.get_specs(meas_data)
    #
    #
    PmagSpecRecs=[]
    for s in sids:
        skip=0
        recnum=0
        PmagSpecRec={}
        PmagSpecRec["er_analyst_mail_names"]=user
        method_codes,inst_code=[],""
    # 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
           if rec["er_specimen_name"]==s: 
               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"
               PmagSpecRec["magic_instrument_codes"]=""
               if "magic_experiment_name" not in rec.keys():
                   rec["magic_experiment_name"]=""
               if "magic_instrument_codes" not in rec.keys():
                   rec["magic_instrument_codes"]=""
               else:
                   PmagSpecRec["magic_experiment_names"]=rec["magic_experiment_name"]
               if len(rec["magic_instrument_codes"]) > len(inst_code):
                   inst_code=rec["magic_instrument_codes"]
                   PmagSpecRec["magic_instrument_codes"]=inst_code  # copy over instruments
               break
    #
    # now check for correct method labels for all measurements
    #
        nrm_data=[]
        for meas_rec in meas_data:
            if meas_rec['er_specimen_name']==PmagSpecRec['er_specimen_name']:
                meths=meas_rec["magic_method_codes"].split(":")
                for meth in meths:
                    if meth.strip() not in meas_meth:meas_meth.append(meth)
                if "LT-NO" in meas_meth:nrm_data.append(meas_rec)
    #
        data,units=pmag.find_dmag_rec(s,nrm_data)
    #
        datablock=data
        #
        # find replicate measurements at NRM step and average them
        #
        Specs=[]
        if doave==1:
            step_meth,avedata=pmag.vspec(data)
            if len(avedata) != len(datablock):
                method_codes.append("DE-VM")
                SpecRec=avedata[0]
                print 'averaging data '
            else: SpecRec=data[0]
            Specs.append(SpecRec)
        else:
            for spec in data:Specs.append(spec)
        for SpecRec in Specs:
        #
        # 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(orientation_priorities):
                        print "no orientation data for ",s 
                        skip,redo=1,0
                        break
                    az_type=orientation_priorities[str(p)]
                    orient=pmag.find_samp_rec(PmagSpecRec["er_sample_name"],samp_data,az_type)
                    if orient["sample_azimuth"]  !="":
                        method_codes.append(az_type.strip())
                        redo=0
                    elif orient["sample_azimuth"]  =="":
                        p+=1
            #
            #  if stratigraphic selected,  get stratigraphic correction
            #
                if skip==0 and orient["sample_azimuth"]!="" and orient["sample_dip"]!="":
                    d_geo,i_geo=pmag.dogeo(SpecRec[1],SpecRec[2],orient["sample_azimuth"],orient["sample_dip"])
                    SpecRec[1]=d_geo
                    SpecRec[2]=i_geo
                    if tilt==1 and "sample_bed_dip" in orient.keys() and orient['sample_bed_dip']!="": 
                        d_tilt,i_tilt=pmag.dotilt(d_geo,i_geo,orient["sample_bed_dip_direction"],orient["sample_bed_dip"])
                        SpecRec[1]=d_tilt
                        SpecRec[2]=i_tilt
            if skip==0:
                PmagSpecRec["specimen_dec"]='%7.1f ' %(SpecRec[1])
                PmagSpecRec["specimen_inc"]='%7.1f ' %(SpecRec[2])
                if geo==1 and tilt==0:PmagSpecRec["specimen_tilt_correction"]='0'
                if geo==1 and tilt==1: PmagSpecRec["specimen_tilt_correction"]='100'
                if geo==0 and tilt==0: PmagSpecRec["specimen_tilt_correction"]='-1'
                PmagSpecRec["specimen_direction_type"]='l'
                PmagSpecRec["magic_method_codes"]="LT-NO"
                if len(method_codes) != 0:
                    methstring=""
                    for meth in method_codes:
                        methstring=methstring+ ":" +meth
                    PmagSpecRec["magic_method_codes"]=methstring[1:]
                PmagSpecRec["specimen_description"]="NRM data"
                PmagSpecRecs.append(PmagSpecRec)
    pmag.magic_write(pmag_file,PmagSpecRecs,'pmag_specimens')
    print "Data saved in ",pmag_file
Exemplo n.º 41
0
def main():
    """
    NAME
        customize_criteria.py
        NB:  This program has been deprecated - use demag_gui or thellier_gui
           to customize acceptance criteria - OR pandas from within a jupyter notebook

    DESCRIPTION
        Allows user to specify acceptance criteria, saves them in pmag_criteria.txt

    SYNTAX
        customize_criteria.py [-h][command line options]

    OPTIONS
        -h prints help message and quits
        -f IFILE, reads in existing criteria
        -F OFILE, writes to pmag_criteria format file

    DEFAULTS
         IFILE: pmag_criteria.txt
         OFILE: pmag_criteria.txt
  
    OUTPUT
        creates a pmag_criteria.txt formatted output file
    """
    infile, critout = "", "pmag_criteria.txt"
    # parse command line options
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-f' in sys.argv:
        ind = sys.argv.index('-f')
        infile = sys.argv[ind + 1]
        crit_data, file_type = pmag.magic_read(infile)
        if file_type != 'pmag_criteria':
            print('bad input file')
            print(main.__doc__)
            sys.exit()
        print("Acceptance criteria read in from ", infile)
    if '-F' in sys.argv:
        ind = sys.argv.index('-F')
        critout = sys.argv[ind + 1]
    Dcrit, Icrit, nocrit = 0, 0, 0
    custom = '1'
    crit = input(
        " [0] Use no acceptance criteria?\n [1] Use default criteria\n [2] customize criteria \n "
    )
    if crit == '0':
        print('Very very loose criteria saved in ', critout)
        crit_data = pmag.default_criteria(1)
        pmag.magic_write(critout, crit_data, 'pmag_criteria')
        sys.exit()
    crit_data = pmag.default_criteria(0)
    if crit == '1':
        print('Default criteria saved in ', critout)
        pmag.magic_write(critout, crit_data, 'pmag_criteria')
        sys.exit()
    CritRec = crit_data[0]
    crit_keys = list(CritRec.keys())
    crit_keys.sort()
    print(
        "Enter new threshold value.\n Return to keep default.\n Leave blank to not use as a criterion\n "
    )
    for key in crit_keys:
        if key != 'pmag_criteria_code' and key != 'er_citation_names' and key != 'criteria_definition' and CritRec[
                key] != "":
            print(key, CritRec[key])
            new = input('new value: ')
            if new != "": CritRec[key] = (new)
    pmag.magic_write(critout, [CritRec], 'pmag_criteria')
    print("Criteria saved in pmag_criteria.txt")
Exemplo n.º 42
0
def save_redo(SpecRecs,inspec):
    SpecRecs,keys=pmag.fillkeys(SpecRecs)
    pmag.magic_write(inspec,SpecRecs,'pmag_specimens')
Exemplo n.º 43
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,'r')
            #    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, 'r')
    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 = 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)

    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'] = old_div(direction[2], 1000)  # the data are in EMU - this converts to Am^2 
    data['measurement_magn_volume'] = old_div((old_div(direction[2], 1000)), volume) # EMU  - data converted to A/m
    
    # 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"] = rowNum
        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.) # 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)
    return True, meas_file
Exemplo n.º 44
0
def main(command_line=True, **kwargs):
    """
    NAME
        old_iodp_jr6_magic.py
 
    DESCRIPTION
        converts shipboard .jr6 format files to magic_measurements format files

    SYNTAX
        old_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'
Exemplo n.º 45
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 -- NOT CURRENTLY SUPPORTED
            [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
Exemplo n.º 46
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 fname in filelist:  # parse each file
        if fname[-3:].lower() == 'csv':
            file_found = True
            print('processing: ', fname)
            with open(fname, 'r') as finput:
                data = list(finput.readlines())
            keys = data[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 data[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 list(
                        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.  # 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 list(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')
        Meassorted = sorted(Speclist,
                            key=lambda x, y=None: int(
                                round(float(x[sort_by]) - float(y[sort_by])))
                            if y != None else 0)
        for rec in Meassorted:
            for key in list(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?'
Exemplo n.º 47
0
def main(command_line=True, **kwargs):
    """
    NAME
        jr6_magic.py
 
    DESCRIPTION
        converts JR6 format files to magic_measurements format files

    SYNTAX
        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: 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 sample naming convention (6 and 7 not yet implemented)
        -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 NOT CURRENTLY SUPPORTED
            [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
        JR6 .txt format file
    """
# initialize some stuff
    noave=0
    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

    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,'r')
            #    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', 1)
        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")


    # 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("option [4] must be in form 4-Z where Z is an integer")
            return False
        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
        else:
            Z=samp_con.split("-")[1]
            samp_con="7"

    ErSampRec,ErSiteRec={},{}

    # parse data
    data=open(mag_file,'r')
    line=data.readline()
    line=data.readline()
    line=data.readline()
    while line !='':
        parsedLine=line.split()
        sampleName=parsedLine[0]
        demagLevel=parsedLine[2]
        date=parsedLine[3]
        line=data.readline()
        line=data.readline()
        line=data.readline()
        line=data.readline()
        parsedLine=line.split()
        specimenAngleDec=parsedLine[1]
        specimenAngleInc=parsedLine[2]
        while parsedLine[0] != 'MEAN' :
            line=data.readline() 
            parsedLine=line.split()
            if len(parsedLine) == 0:
                parsedLine=["Hello"]
        Mx=parsedLine[1]
        My=parsedLine[2]
        Mz=parsedLine[3]
        line=data.readline() 
        line=data.readline() 
        parsedLine=line.split()
        splitExp = parsedLine[2].split('A')
        intensityStr=parsedLine[1] + splitExp[0]
        intensity = float(intensityStr)

        # check and see if Prec is too big and messes with the parcing.
        precisionStr=''
        if len(parsedLine) == 6:  #normal line
            precisionStr=parsedLine[5][0:-1]
        else:
            precisionStr=parsedLine[4][0:-1]
            
        precisionPer = float(precisionStr)
        precision=intensity*precisionPer/100

        while parsedLine[0] != 'SPEC.' :
            line=data.readline() 
            parsedLine=line.split()
            if len(parsedLine) == 0:
                parsedLine=["Hello"]

        specimenDec=parsedLine[2]    
        specimenInc=parsedLine[3]    
        line=data.readline()
        line=data.readline()
        parsedLine=line.split()
        geographicDec=parsedLine[1]
        geographicInc=parsedLine[2]
    
        # Add data to various MagIC data tables.

        er_specimen_name = sampleName

        if specnum!=0:
            er_sample_name=er_specimen_name[:specnum]
        else:
            er_sample_name=er_specimen_name

        if int(samp_con) in [1, 2, 3, 4, 5, 7]:
            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']

        # check sample list(SampOuts) to see if sample already exists in list before adding new sample info
        sampleFlag=0
        for sampRec in SampOuts:
            if sampRec['er_sample_name'] == er_sample_name:
                sampleFlag=1
                break
        if sampleFlag == 0:
            ErSampRec['er_sample_name']=er_sample_name
            ErSampRec['sample_azimuth']=specimenAngleDec
            ErSampRec['sample_dip']=specimenAngleInc
            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())

        MagRec={}
        MagRec['measurement_description']='Date: '+date
        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
        MagRec["treatment_ac_field"]='0'
        if demagLevel == 'NRM':
            meas_type="LT-NO"
        elif demagLevel[0] == 'A':
            meas_type="LT-AF-Z"
            treat=float(demagLevel[1:])
            MagRec["treatment_ac_field"]='%8.3e' %(treat*1e-3) # convert from mT to tesla
        elif demagLevel[0] == 'T':
            meas_type="LT-T-Z"
            treat=float(demagLevel[1:])
            MagRec["treatment_temp"]='%8.3e' % (treat+273.) # temp in kelvin
        else:
            print("measurement type unknown")
            return False
#        X=[float(Mx),float(My),float(Mz)]
#        Vec=pmag.cart2dir(X)
#        MagRec["measurement_magn_moment"]='%10.3e'% (Vec[2]) # Am^2
        MagRec["measurement_magn_moment"]=str(intensity*0.025*0.025*0.025) # Am^2 assume 2.5cm cube sample
        MagRec["measurement_magn_volume"]=intensityStr
        MagRec["measurement_dec"]=specimenDec
        MagRec["measurement_inc"]=specimenInc
        MagRec['magic_method_codes']=meas_type
        MagRecs.append(MagRec.copy())

        #read lines till end of record
        line=data.readline()
        line=data.readline()
        line=data.readline()
        line=data.readline()
        line=data.readline()

        # read all the rest of the special characters. Some data files not consistantly formatted.
        while (len(line) <=3 and line!=''):
            line=data.readline()
            
        #end of data while loop

    MagOuts=pmag.measurements_methods(MagRecs,noave)
    pmag.magic_write(samp_file,SampOuts,'er_samples') 
    print("sample orientations put in ",samp_file)
    pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    print("results put in ",meas_file)
    return True
Exemplo n.º 48
0
def main():
    """
    NAME 
        pt_rot.py 

    DESCRIPTION
        rotates pt according to specified age and plate
 
    SYNTAX
        pt_rot.py [command line options]

    OPTIONS
        -h prints help and quits
        -f file with lon lat plate age Dplate as space delimited input
           Dplate is the destination plate coordinates desires 
           - default is "fixed south africa"
           Dplate should be one of: [nwaf, neaf,saf,aus, eur, ind, sam, ant, grn, nam]
        -ff file Efile,   file  has lat lon data file and Efile has sequential rotation poles: Elat Elon Omega 
        -F OFILE, output pmag_results formatted file with rotated points stored in vgp_lon, vgp_lat
           default is to print out rotated lon, lat to standard output
    
    """
    dir_path='.'
    PTS=[]
    ResRecs=[]
    ofile=""
    Dplates=['nwaf', 'neaf','saf','aus', 'eur', 'ind', 'sam', 'ant', 'grn', 'nam']
    if '-WD' in sys.argv:
        ind = sys.argv.index('-WD')
        dir_path=sys.argv[ind+1]
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-F' in sys.argv:
        ind = sys.argv.index('-F')
        ofile=dir_path+'/'+sys.argv[ind+1]
    if '-f' in sys.argv:
        ind = sys.argv.index('-f')
        file=dir_path+'/'+sys.argv[ind+1]
        f=open(file,'r')
        data=f.readlines()
    elif '-ff' in sys.argv:
        ind = sys.argv.index('-ff')
        file=dir_path+'/'+sys.argv[ind+1]
        f=open(file,'r')
        data=f.readlines()
        Efile=dir_path+'/'+sys.argv[ind+2]
        f=open(Efile,'r')
        edata=f.readlines()
        Poles=[]
        for p in edata:
             rec=p.split()
             pole=[float(rec[0]),float(rec[1]),float(rec[2])] # pole is lat/lon/omega
             Poles.append(pole)
    else:
        data=sys.stdin.readlines()
    for line in data:
        PtRec={}
        rec=line.split()
        PtRec['site_lon']=rec[0]
        PtRec['site_lat']=rec[1]
        if '-ff' in sys.argv:
            pt_lat,pt_lon=float(rec[1]),float(rec[0])
            for pole in Poles:
                ptrot= pmag.PTrot(pole,[pt_lat],[pt_lon])
                pt_lat=ptrot[0][0]
                pt_lon=ptrot[1][0]
            if ofile=="":
                print(ptrot[1][0], ptrot[0][0])
            else:
                ResRec={'vgp_lat': '%7.1f'%(ptrot[0][0]),'vgp_lon':'%7.1f'%( ptrot[1][0])}
                ResRecs.append(ResRec)
        else:
            PtRec['cont']=rec[2]
            if PtRec['cont']=='af':PtRec['cont']='saf' # use fixed south africa
            PtRec['age']=rec[3]
            if len(rec)>4:
               PtRec['dcont']=rec[4]
            PTS.append(PtRec)
    if '-ff' not in sys.argv:
        for pt in PTS:
            pole='not specified'
            pt_lat=float(pt['site_lat'])
            pt_lon=float(pt['site_lon'])
            age=float(pt['age'])
            ptrot=[[pt_lat],[pt_lon]]
            if pt['cont']=='ib':
                pole=frp.get_pole(pt['cont'],age)
                ptrot= pmag.PTrot(pole,[pt_lat],[pt_lon])
                pt_lat=ptrot[0][0]
                pt_lon=ptrot[1][0]
                pt['cont']='eur'
            if pt['cont']!='saf':
                pole1=frp.get_pole(pt['cont'],age)
                ptrot= pmag.PTrot(pole1,[pt_lat],[pt_lon])
                if 'dcont' in list(pt.keys()):
                    pt_lat=ptrot[0][0]
                    pt_lon=ptrot[1][0]
                    pole=frp.get_pole(pt['dcont'],age)
                    pole[2]=-pole[2] 
                    ptrot= pmag.PTrot(pole,[pt_lat],[pt_lon])
                if ofile=="":
                    print(ptrot[1][0], ptrot[0][0])
                else:
                    ResRec={'vgp_lat': '%7.1f'%(ptrot[0][0]),'vgp_lon':'%7.1f'%( ptrot[1][0])}
                    ResRecs.append(ResRec)
            else:
                if 'dcont' in list(pt.keys()):
                    pole=frp.get_pole(pt['dcont'],age)
                    pole[2]=-pole[2] 
                    ptrot= pmag.PTrot(pole,[pt_lat],[pt_lon])
                    print(ptrot)
                    if ofile=="":
                        print(ptrot[1][0], ptrot[0][0]) 
                    else:
                        ResRec={'vgp_lat': '%7.1f'%(ptrot[0][0]),'vgp_lon':'%7.1f'%( ptrot[1][0])}
                        ResRecs.append(ResRec)
                else:
                    if ofile=="":
                        print(ptrot[1][0], ptrot[0][0])
                    else:
                        ResRec={'vgp_lat': '%7.1f'%(ptrot[0][0]),'vgp_lon':'%7.1f'%( ptrot[1][0])}
                        ResRecs.append(ResRec)
    if len(ResRecs)>0:
        pmag.magic_write(ofile,ResRecs,'pmag_results')
Exemplo n.º 49
0
def main(command_line=True, **kwargs):
    """
    NAME
        utrecht_magic.py

    DESCRIPTION
        converts Utrecht magnetometer data files to magic_measurements files

    SYNTAX
        utrecht_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
        -lat latitude of samples
        -lon longitude of samples
        -A: don't average replicate measurements
        -mcd [SO-MAG,SO-SUN,SO-SIGHT...] supply how these samples were oriented

    INPUT
        Utrecht magnetometer data file
    """
    # initialize some stuff
    sample_lat = 0.0
    sample_lon = 0.0
    noave = 0
    er_location_name = "unknown"
    er_site_name = "unknown"
    args = sys.argv
    meth_code = "LP-NO"
    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 "-lat" in args:
            ind = args.index("-lat")
            sample_lat = args[ind + 1]
        if "-lon" in args:
            ind = args.index("-lon")
            sample_lon = 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 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', '')
        sample_lat = kwargs.get('sample_lat', '')
        sample_lon = kwargs.get('sample_lon', '')
        #oave = kwargs.get('noave', 0) # default (0) means DO average
        meth_code = kwargs.get('meth_code', "LP-NO")

    # format variables
    if not mag_file:
        return False, 'You must provide a Utrecht formated 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 Utrecht file and read the header information
    print 'mag_file in utrecht_file', mag_file
    data = open(mag_file, 'rU')
    line = data.readline()
    line_items = line.split(',')
    operator = line_items[0]
    operator = operator.replace("\"", "")
    machine = line_items[1]
    machine = machine.replace("\"", "")
    machine = machine.rstrip('\n')
    print "operator=", operator
    print "machine=", machine
    line = data.readline()
    while line != '"END"':
        line_items = line.split(',')
        sample_name = line_items[0]
        sample_name = sample_name.replace("\"", "")
        print "sample_name=", sample_name
        free_string = line_items[1]
        free_string = free_string.replace("\"", "")
        print "free_string=", free_string
        dec = line_items[2]
        print "dec=", dec
        inc = line_items[3]
        print "inc=", inc
        volume = float(line_items[4])
        volume = volume * 1e-6  # enter volume in cm^3, convert to m^3
        print "volume=", volume
        bed_plane = line_items[5]
        print "bed_plane=", bed_plane
        bed_tilt = line_items[6]
        print "bed_tilt=", bed_tilt

        # Configure et er_samples table
        ErSampRec['er_sample_name'] = sample_name
        ErSampRec['sample_azimuth'] = dec
        ErSampRec['sample_dip'] = inc
        ErSampRec['sample_bed_dip_direction'] = bed_plane
        ErSampRec['sample_bed_tilt'] = bed_tilt
        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())

        line = data.readline()
        line = line.rstrip("\n")
        items = line.split(",")
        #    exit()
        while line != '9999':
            print line
            step = items[0]
            step = step.split('.')
            step_value = step[0]
            step_type = ""
            if len(step) == 2:
                step_type = step[1]
            A = float(items[1])
            B = float(items[2])
            C = float(items[3])
            #  convert to MagIC coordinates
            Z = -A
            X = -B
            Y = C
            cart = np.array([X, Y, Z]).transpose()
            direction = pmag.cart2dir(cart).transpose()
            measurement_dec = direction[0]
            measurement_inc = direction[1]
            measurement_magn_moment = direction[
                2] * 1.0e-12  # the data are in pico-Am^2 - this converts to Am^2
            measurement_magn_volume = direction[
                2] * 1.0e-12 / volume  # data volume normalized - converted to A/m
            print "measurement_magn_moment=", measurement_magn_moment
            print "measurement_magn_volume=", measurement_magn_volume
            error = items[4]
            date = items[5]
            date = date.strip('"')
            date = date.split("-")
            print date
            time = items[6]
            time = time.strip('"')
            time = time.split(":")
            print time
            date_time = date[0] + ":" + date[1] + ":" + date[2] + ":" + time[
                0] + ":" + time[1] + ":" + "0.0"
            print date_time

            MagRec = {}

            MagRec["er_analyst_mail_names"] = operator
            MagRec["magic_instrument_codes"] = "Utrecht_" + machine
            MagRec["measurement_description"] = "free string = " + free_string
            MagRec["measurement_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'] = sample_name
            MagRec['magic_software_packages'] = version_num
            MagRec["measurement_temp"] = '%8.3e' % (273)  # room temp in kelvin
            MagRec["measurement_flag"] = 'g'
            MagRec["measurement_standard"] = 'u'
            MagRec[
                "magic_experiment_name"] = er_location_name + er_site_name + sample_name
            MagRec[
                "measurement_number"] = er_location_name + er_site_name + sample_name + items[
                    0]
            MagRec["er_specimen_name"] = sample_name
            # MagRec["treatment_ac_field"] = '0'
            MagRec["treatment_temp"] = '%8.3e' % (float(step_value) + 273.
                                                  )  # temp in kelvin
            meas_type = "LP-DIR-T"
            if step_value == '0':
                meas_type = "LT-NO"
            print "step_type=", step_type
            if step_type == '0':
                if meas_type == "":
                    meas_type = "LT-T-Z"
                else:
                    meas_type = meas_type + ":" + "LT-T-Z"
            elif step_type == '1':
                if meas_type == "":
                    meas_type = "LT-T-I"
                else:
                    meas_type = meas_type + ":" + "LT-T-I"
            elif step_type == '2':
                if meas_type == "":
                    meas_type = "LT-PTRM-I"
                else:
                    meas_type = meas_type + ":" + "LT-PTRM-I"
            elif step_type == '3':
                if meas_type == "":
                    meas_type = "LT-PTRM-Z"
                else:
                    meas_type = meas_type + ":" + "LT-PTRM-Z"
            print "meas_type=", meas_type
            MagRec['magic_method_codes'] = meas_type
            MagRec["measurement_magn_moment"] = measurement_magn_moment
            MagRec["measurement_magn_volume"] = measurement_magn_volume
            MagRec["measurement_dec"] = measurement_dec
            MagRec["measurement_inc"] = measurement_inc
            MagRec['measurement_csd'] = error
            #           MagRec['measurement_positions'] = '1'
            MagRecs.append(MagRec.copy())

            line = data.readline()
            line = line.rstrip("\n")
            items = line.split(",")
        line = data.readline()
        line = line.rstrip("\n")
        items = line.split(",")


# write out the data to MagIC data files
    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')
    pmag.magic_write(meas_file, MagRecs, 'magic_measurements')
    print "results put in ", meas_file
    print "exit!"
    return True, meas_file
Exemplo n.º 50
0
def main():
    """
    NAME
        sites_locations.py

    DESCRIPTION
        reads in er_sites.txt file and finds all locations and bounds of locations
        outputs er_locations.txt file

    SYNTAX
        sites_locations.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f: specimen input er_sites format file, default is "er_sites.txt"
        -F: locations table: default is "er_locations.txt"
    """
    # set defaults
    site_file = "er_sites.txt"
    loc_file = "er_locations.txt"
    Names, user = [], "unknown"
    Done = []
    version_num = pmag.get_version()
    args = sys.argv
    dir_path = '.'
    # get command line stuff
    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")
        site_file = args[ind + 1]
    if '-F' in args:
        ind = args.index("-F")
        loc_file = args[ind + 1]
    #
    site_file = dir_path + '/' + site_file
    loc_file = dir_path + '/' + loc_file
    Sites, file_type = pmag.magic_read(site_file)
    if file_type != 'er_sites':
        print(file_type)
        print(file_type, "This is not a valid er_sites file ")
        sys.exit()
    # read in site data
    #
    LocNames, Locations = [], []
    for site in Sites:
        if site['er_location_name'] not in LocNames:  # new location name
            LocNames.append(site['er_location_name'])
            sites_locs = pmag.get_dictitem(Sites, 'er_location_name',
                                           site['er_location_name'],
                                           'T')  # get all sites for this loc
            lats = pmag.get_dictkey(sites_locs, 'site_lat',
                                    'f')  # get all the latitudes as floats
            lons = pmag.get_dictkey(sites_locs, 'site_lon',
                                    'f')  # get all the longitudes as floats
            LocRec = {
                'er_citation_names': 'This study',
                'er_location_name': site['er_location_name'],
                'location_type': ''
            }
            LocRec['location_begin_lat'] = str(min(lats))
            LocRec['location_end_lat'] = str(max(lats))
            LocRec['location_begin_lon'] = str(min(lons))
            LocRec['location_end_lon'] = str(max(lons))
            Locations.append(LocRec)
    if len(Locations) > 0:
        pmag.magic_write(loc_file, Locations, "er_locations")
        print("Locations written to: ", loc_file)
Exemplo n.º 51
0
def main():
    """
    NAME
        lsq_redo.py

    DESCRIPTION
        converts a tab delimited LSQ format to PmagPy redo file and edits the magic_measurements table to mark "bad" measurements.

    SYNTAX
        lsq_redo.py [-h] [command line options]

    OPTIONS
        -h: prints help message and quits
        -f FILE: specify LSQ input file
        -fm MFILE: specify measurements file for editting, default is
            magic_measurements.txt
        -F FILE: specify output file, default is 'zeq_redo'
    """
    letters = string.ascii_uppercase
    for l in string.ascii_lowercase:
        letters = letters + l
    dir_path = '.'
    if '-WD' in sys.argv:
        ind = sys.argv.index('-WD')
        dir_path = sys.argv[ind + 1]
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-f' in sys.argv:
        ind = sys.argv.index('-f')
        inspec = dir_path + '/' + sys.argv[ind + 1]
    else:
        zfile = dir_path + '/zeq_redo'
    if '-fm' in sys.argv:
        ind = sys.argv.index('-f')
        meas_file = dir_path + '/' + sys.argv[ind + 1]
    else:
        meas_file = dir_path + '/magic_measurements.txt'
    if '-F' in sys.argv:
        ind = sys.argv.index('-F')
        zfile = dir_path + '/' + sys.argv[ind + 1]
    else:
        zfile = dir_path + '/zeq_redo'
    try:
        open(meas_file, "r")
        meas_data, file_type = pmag.magic_read(meas_file)
    except IOError:
        print(main.__doc__)
        print("""You must have a valid measurements file prior to converting
                this LSQ file""")
        sys.exit()
    zredo = open(zfile, "w")
    MeasRecs = []
    #
    # read in LSQ file
    #
    specs, MeasOuts = [], []
    prior_spec_data = open(inspec, 'r').readlines()
    for line in prior_spec_data:
        if len(line) < 2:
            sys.exit()
#        spec=line[0:14].strip().replace(" ","") # get out the specimen name = collapsing spaces
#        rec=line[14:].split() # split up the rest of the line
        rec = line.split('\t')
        spec = rec[0].lower()
        specs.append(spec)
        comp_name = rec[2]  # assign component name
        calculation_type = "DE-FM"
        if rec[1][0] == "L":
            calculation_type = "DE-BFL"  # best-fit line
        else:
            calculation_type = "DE-BFP"  # best-fit line
        lists = rec[7].split('-')  # get list of data used
        incl = []
        for l in lists[0]:
            incl.append(letters.index(l))
        for l in letters[letters.index(lists[0][-1]) +
                         1:letters.index(lists[1][0])]:
            incl.append(letters.index(l))  # add in the  in between parts
        for l in lists[1]:
            incl.append(letters.index(l))
        if len(lists) > 2:
            for l in letters[letters.index(lists[1][-1]) +
                             1:letters.index(lists[2][0])]:
                incl.append(letters.index(l))  # add in the  in between parts
            for l in lists[2]:
                incl.append(letters.index(l))


# now find all the data for this specimen in measurements
        datablock, min, max = [], "", ""
        demag = 'N'
        for s in meas_data:
            if s['er_specimen_name'].lower() == spec.lower():
                meths = s['magic_method_codes'].replace(" ", "").split(":")
                if 'LT-NO' in meths or 'LT-AF-Z' in meths or 'LT-T-Z' in meths:
                    datablock.append(s)
        if len(datablock) > 0:
            for t in datablock:
                print(t['magic_method_codes'])
            incl_int = len(incl)
            while incl[-1] > len(datablock) - 1:
                del incl[
                    -1]  # don't include measurements beyond what is in file
            if len(incl) != incl_int:
                'converting calculation type to best-fit line'
            meths0 = datablock[incl[0]]['magic_method_codes'].replace(
                " ", "").split(':')
            meths1 = datablock[incl[-1]]['magic_method_codes'].replace(
                " ", "").split(':')
            H0 = datablock[incl[0]]['treatment_ac_field']
            T0 = datablock[incl[0]]['treatment_temp']
            H1 = datablock[incl[-1]]['treatment_ac_field']
            T1 = datablock[incl[-1]]['treatment_temp']
            if 'LT-T-Z' in meths1:
                max = T1
                demag = "T"
            elif 'LT-AF-Z' in meths1:
                demag = "AF"
                max = H1
            if 'LT-NO' in meths0:
                if demag == 'T':
                    min = 273
                else:
                    min = 0
            elif 'LT-T-Z' in meths0:
                min = T0
            else:
                min = H0
            for ind in range(incl[0]):
                MeasRecs.append(datablock[ind])
            for ind in range(incl[0], incl[-1]):
                if ind not in incl:  # datapoint not used in calculation
                    datablock[ind]['measurement_flag'] = 'b'
                MeasRecs.append(datablock[ind])
            for ind in range(incl[-1], len(datablock)):
                MeasRecs.append(datablock[ind])
            outstring = '%s %s %s %s %s \n' % (spec, calculation_type, min,
                                               max, comp_name)
            zredo.write(outstring)
    for s in meas_data:  # collect the rest of the measurement data not already included
        if s['er_specimen_name'] not in specs:
            MeasRecs.append(s)
    pmag.magic_write(meas_file, MeasRecs,
                     'magic_measurements')  # write out annotated measurements
Exemplo n.º 52
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 = input("DC lab field for ARM step: [50uT] ")
            if ans == "":
                arm_labfield = 50e-6
            else:
                arm_labfield = float(ans) * 1e-6
            ans = 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, 'r').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 list(SpecRec.keys()):
                SampRec[key] = SpecRec[key]
            SampRec['sample_azimuth'] = '0'
            SampRec['sample_dip'] = '0'
            SampRec['magic_method_codes'] = 'FS-C-DRILL-IODP:SP-SS-C:SO-V'
            SpecRec['er_specimen_name'] = specimen
            SampRec['er_specimen_names'] = specimen
            for key in list(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_sd_z'] = items[1]
                        MagRec['magic_instrument_codes'] = inst
                        MagRec['measurement_number'] = '1'
                        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)
    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)
Exemplo n.º 53
0
def main(command_line=True, **kwargs):
    """
    NAME
        jr6_txt_magic.py
 
    DESCRIPTION
        converts JR6 .txt format files to magic_measurements format files

    SYNTAX
        jr6_txt_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)
        -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 sample naming convention (6 and 7 not yet implemented)
        -mcd [SO-MAG,SO-SUN,SO-SIGHT...] supply how these samples were oriented
        -v NUM : specify the volume of the sample, default 2.5cm^3.
       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 NOT CURRENTLY SUPPORTED
            [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
        JR6 .txt format file
    """
    # initialize some stuff
    noave=0
    volume = 2.5 * 1e-6 # default volume is 2.5 cm^3 (2.5 * 1e-6 meters^3)
    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

    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,'r')
            #    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 "-v" in args: 
            ind=args.index("-v")
            volume=float(args[ind+1]) * 1e-6

    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')
        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")
        volume = float(kwargs.get('volume', 0))
        if not volume:
            volume = 2.5 * 1e-6 #default volume is a 2.5 cm cube, translated to meters cubed
        else:
            #convert cm^3 to m^3
            volume *= 1e-6


    # 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("option [4] must be in form 4-Z where Z is an integer")
            return False, "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, "option [7] must be in form 7-Z where Z is an integer"
        else:
            Z=samp_con.split("-")[1]
            samp_con="7"

    ErSampRec,ErSiteRec={},{}

    # parse data
    data=open(mag_file,'r')
    line=data.readline()
    line=data.readline()
    line=data.readline()
    while line !='':
        parsedLine=line.split()
        sampleName=parsedLine[0]
        demagLevel=parsedLine[2]
        date=parsedLine[3]
        line=data.readline()
        line=data.readline()
        line=data.readline()
        line=data.readline()
        parsedLine=line.split()
        specimenAngleDec=parsedLine[1]
        specimenAngleInc=parsedLine[2]
        while parsedLine[0] != 'MEAN' :
            line=data.readline() 
            parsedLine=line.split()
            if len(parsedLine) == 0:
                parsedLine=["Hello"]
        Mx=parsedLine[1]
        My=parsedLine[2]
        Mz=parsedLine[3]
        line=data.readline() 
        line=data.readline() 
        parsedLine=line.split()
        splitExp = parsedLine[2].split('A')
        intensityVolStr=parsedLine[1] + splitExp[0]
        intensityVol = float(intensityVolStr)

        # check and see if Prec is too big and messes with the parcing.
        precisionStr=''
        if len(parsedLine) == 6:  #normal line
            precisionStr=parsedLine[5][0:-1]
        else:
            precisionStr=parsedLine[4][0:-1]
            
        precisionPer = float(precisionStr)
        precision=intensityVol*precisionPer/100

        while parsedLine[0] != 'SPEC.' :
            line=data.readline() 
            parsedLine=line.split()
            if len(parsedLine) == 0:
                parsedLine=["Hello"]

        specimenDec=parsedLine[2]    
        specimenInc=parsedLine[3]    
        line=data.readline()
        line=data.readline()
        parsedLine=line.split()
        geographicDec=parsedLine[1]
        geographicInc=parsedLine[2]
    
        # Add data to various MagIC data tables.

        er_specimen_name = sampleName

        if specnum!=0: 
            er_sample_name=er_specimen_name[:specnum]
        else:
            er_sample_name=er_specimen_name

        if int(samp_con) in [1, 2, 3, 4, 5, 7]:
            er_site_name=pmag.parse_site(er_sample_name,samp_con,Z)
        else:
            print("-W- Using unreognized sample convention option: ", samp_con)
        # 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']

        # check sample list(SampOuts) to see if sample already exists in list before adding new sample info
        sampleFlag=0
        for sampRec in SampOuts:
            if sampRec['er_sample_name'] == er_sample_name:
                sampleFlag=1
                break
        if sampleFlag == 0:
            ErSampRec['er_sample_name']=er_sample_name
            ErSampRec['sample_azimuth']=specimenAngleDec
            sample_dip=str(float(specimenAngleInc)-90.0) #convert to magic orientation
            ErSampRec['sample_dip']=sample_dip
            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())

        MagRec={}
        MagRec['measurement_description']='Date: '+date
        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
        MagRec["treatment_ac_field"]='0'
        if demagLevel == 'NRM':
            meas_type="LT-NO"
        elif demagLevel[0] == 'A':
            meas_type="LT-AF-Z"
            treat=float(demagLevel[1:])
            MagRec["treatment_ac_field"]='%8.3e' %(treat*1e-3) # convert from mT to tesla
        elif demagLevel[0] == 'T':
            meas_type="LT-T-Z"
            treat=float(demagLevel[1:])
            MagRec["treatment_temp"]='%8.3e' % (treat+273.) # temp in kelvin
        else:
            print("measurement type unknown", demag_level)
            return False, "measurement type unknown"

        MagRec["measurement_magn_moment"]=str(intensityVol*volume) # Am^2
        MagRec["measurement_magn_volume"]=intensityVolStr # A/m
        MagRec["measurement_dec"]=specimenDec
        MagRec["measurement_inc"]=specimenInc
        MagRec['magic_method_codes']=meas_type
        MagRecs.append(MagRec.copy())

        #read lines till end of record
        line=data.readline()
        line=data.readline()
        line=data.readline()
        line=data.readline()
        line=data.readline()

        # read all the rest of the special characters. Some data files not consistantly formatted.
        while (len(line) <=3 and line!=''):
            line=data.readline()
            
        #end of data while loop

    MagOuts=pmag.measurements_methods(MagRecs,noave)
    pmag.magic_write(samp_file,SampOuts,'er_samples') 
    print("sample orientations put in ",samp_file)
    pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    print("results put in ",meas_file)
    return True, meas_file
Exemplo n.º 54
0
def main():
    """
    NAME
        hysteresis_magic.py

    DESCRIPTION
        calculates hystereis parameters and saves them in 3.0 specimen format file
        makes plots if option selected

    SYNTAX
        hysteresis_magic.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f: specify input file, default is agm_measurements.txt
        -F: specify specimens.txt output file
        -P: do not make the plots
        -spc SPEC: specify specimen name to plot and quit
        -sav save all plots and quit
        -fmt [png,svg,eps,jpg]
    """
    args=sys.argv
    PLT=1
    plots=0
    fmt=pmag.get_named_arg_from_sys('-fmt','svg')
    dir_path=pmag.get_named_arg_from_sys('-WD','.')
    dir_path=os.path.realpath(dir_path)
    verbose=pmagplotlib.verbose
    version_num=pmag.get_version()
    user=pmag.get_named_arg_from_sys('-usr','')
    if "-h" in args:
        print(main.__doc__)
        sys.exit()
    meas_file=pmag.get_named_arg_from_sys('-f','agm_measurements.txt')
    spec_file=pmag.get_named_arg_from_sys('-F','specimens.txt')
    if '-P' in args:
        PLT=0
        irm_init,imag_init=-1,-1
    if '-sav' in args:
        verbose=0
        plots=1
    pltspec=pmag.get_named_arg_from_sys('-spc',0)
    if pltspec:
        #pltspec= args[ind+1]
        verbose=0
        plots=1
    spec_file=dir_path+'/'+spec_file
    meas_file=dir_path+'/'+meas_file
    SpecRecs=[]
    #
    #
    meas_data,file_type=pmag.magic_read(meas_file)
    if file_type!='measurements':
        print(main.__doc__)
        print('bad file')
        sys.exit()
    #
    # initialize some variables
    # define figure numbers for hyst,deltaM,DdeltaM curves
    HystRecs,RemRecs=[],[]
    HDD={}
    if verbose:
        if verbose and PLT:print("Plots may be on top of each other - use mouse to place ")
    if PLT:
        HDD['hyst'],HDD['deltaM'],HDD['DdeltaM']=1,2,3
        pmagplotlib.plot_init(HDD['DdeltaM'],5,5)
        pmagplotlib.plot_init(HDD['deltaM'],5,5)
        pmagplotlib.plot_init(HDD['hyst'],5,5)
        imag_init=0
        irm_init=0
    else:
        HDD['hyst'],HDD['deltaM'],HDD['DdeltaM'],HDD['irm'],HDD['imag']=0,0,0,0,0
    #
    if spec_file: prior_data,file_type=pmag.magic_read(spec_file)
    #
    # get list of unique experiment names and specimen names
    #
    experiment_names,sids=[],[]
    hys_data=pmag.get_dictitem(meas_data,'method_codes','LP-HYS','has')
    dcd_data=pmag.get_dictitem(meas_data,'method_codes','LP-IRM-DCD','has')
    imag_data=pmag.get_dictitem(meas_data,'method_codes','LP-IMAG','has')
    for rec in hys_data:
        if rec['experiment'] not in experiment_names:experiment_names.append(rec['experiment'])
        if rec['specimen'] not in sids:sids.append(rec['specimen'])
    #
    k=0
    if pltspec:
        k=sids.index(pltspec)
        print(sids[k])
    while k < len(sids):
        specimen=sids[k]
        HystRec={'specimen':specimen,'experiment':""} # initialize a new specimen hysteresis record
        if verbose and PLT:print(specimen, k+1 , 'out of ',len(sids))
    #
    #
        B,M,Bdcd,Mdcd=[],[],[],[] #B,M for hysteresis, Bdcd,Mdcd for irm-dcd data
        Bimag,Mimag=[],[] #Bimag,Mimag for initial magnetization curves
        spec_data=pmag.get_dictitem(hys_data,'specimen',specimen,'T') # fish out all the LP-HYS data for this specimen
        if len(spec_data)>0:
            meths=spec_data[0]['method_codes'].split(':')
            e=spec_data[0]['experiment']
            HystRec['experiment']=spec_data[0]['experiment']
            for rec in  spec_data:
                B.append(float(rec['meas_field_dc']))
                M.append(float(rec['magn_moment']))
        spec_data=pmag.get_dictitem(dcd_data,'specimen',specimen,'T') # fish out all the data for this specimen
        if len(spec_data)>0:
            HystRec['experiment']=HystRec['experiment']+':'+spec_data[0]['experiment']
            irm_exp=spec_data[0]['experiment']
            for rec in  spec_data:
                Bdcd.append(float(rec['treat_dc_field']))
                Mdcd.append(float(rec['magn_moment']))
        spec_data=pmag.get_dictitem(imag_data,'specimen',specimen,'T') # fish out all the data for this specimen
        if len(spec_data)>0:
            imag_exp=spec_data[0]['experiment']
            for rec in  spec_data:
                Bimag.append(float(rec['meas_field_dc']))
                Mimag.append(float(rec['magn_moment']))
    #
    # now plot the hysteresis curve
    #
        if len(B)>0:
            hmeths=[]
            for meth in meths: hmeths.append(meth)

            hpars=pmagplotlib.plotHDD(HDD,B,M,e)
            if verbose and PLT:pmagplotlib.drawFIGS(HDD)
    #
            if verbose:pmagplotlib.plotHPARS(HDD,hpars,'bs')
            HystRec['hyst_mr_moment']=hpars['hysteresis_mr_moment']
            HystRec['hyst_ms_moment']=hpars['hysteresis_ms_moment']
            HystRec['hyst_bc']=hpars['hysteresis_bc']
            HystRec['hyst_bcr']=hpars['hysteresis_bcr']
            HystRec['susc_h']=hpars['hysteresis_xhf']
            HystRec['experiments']=e
            HystRec['software_packages']=version_num
            if hpars["magic_method_codes"] not in hmeths:hmeths.append(hpars["magic_method_codes"])
            methods=""
            for meth in hmeths:
                methods=methods+meth.strip()+":"
            HystRec["method_codes"]=methods[:-1]
            HystRec["citations"]="This study"
    #
        if len(Bdcd)>0:
            rmeths=[]
            for meth in meths: rmeths.append(meth)
            if verbose and PLT:print('plotting IRM')
            if irm_init==0:
                HDD['irm']=5
                pmagplotlib.plot_init(HDD['irm'],5,5)
                irm_init=1
            rpars=pmagplotlib.plotIRM(HDD['irm'],Bdcd,Mdcd,irm_exp)
            HystRec['rem_mr_moment']=rpars['remanence_mr_moment']
            HystRec['rem_bcr']=rpars['remanence_bcr']
            HystRec['experiments']=specimen+':'+irm_exp
            if rpars["magic_method_codes"] not in meths:meths.append(rpars["magic_method_codes"])
            methods=""
            for meth in rmeths:
                methods=methods+meth.strip()+":"
            HystRec["method_codes"]=HystRec['method_codes']+':'+methods[:-1]
            HystRec["citations"]="This study"
        else:
            if irm_init:pmagplotlib.clearFIG(HDD['irm'])
        if len(Bimag)>0:
            if verbose and PLT:print('plotting initial magnetization curve')
# first normalize by Ms
            Mnorm=[]
            for m in Mimag: Mnorm.append(old_div(m,float(hpars['hysteresis_ms_moment'])))
            if imag_init==0:
                HDD['imag']=4
                pmagplotlib.plot_init(HDD['imag'],5,5)
                imag_init=1
            pmagplotlib.plotIMAG(HDD['imag'],Bimag,Mnorm,imag_exp)
        else:
            if imag_init:pmagplotlib.clearFIG(HDD['imag'])
        if len(list(HystRec.keys()))>0:HystRecs.append(HystRec)
    #
        files={}
        if plots:
            if pltspec:s=pltspec
            files={}
            for key in list(HDD.keys()):
                files[key]=s+'_'+key+'.'+fmt
            pmagplotlib.saveP(HDD,files)
            if pltspec:sys.exit()
        if verbose and PLT:
            pmagplotlib.drawFIGS(HDD)
            ans=input("S[a]ve plots, [s]pecimen name, [q]uit, <return> to continue\n ")
            if ans=="a":
                files={}
                for key in list(HDD.keys()):
                    files[key]=specimen+'_'+key+'.'+fmt
                pmagplotlib.saveP(HDD,files)
            if ans=='':k+=1
            if ans=="p":
                del HystRecs[-1]
                k-=1
            if  ans=='q':
                print("Good bye")
                sys.exit()
            if ans=='s':
                keepon=1
                specimen=input('Enter desired specimen name (or first part there of): ')
                while keepon==1:
                    try:
                        k =sids.index(specimen)
                        keepon=0
                    except:
                        tmplist=[]
                        for qq in range(len(sids)):
                            if specimen in sids[qq]:tmplist.append(sids[qq])
                        print(specimen," not found, but this was: ")
                        print(tmplist)
                        specimen=input('Select one or try again\n ')
                        k =sids.index(specimen)
        else:
            k+=1
        if len(B)==0 and len(Bdcd)==0:
            if verbose:print('skipping this one - no hysteresis data')
            k+=1
    if len(HystRecs)>0:
    #  go through prior_data, clean out prior results and save combined file as spec_file
        SpecRecs,keys=[],list(HystRecs[0].keys())
        if len(prior_data)>0:
            prior_keys=list(prior_data[0].keys())
        else: prior_keys=[]
        for rec in prior_data:
            for key in keys:
                if key not in list(rec.keys()):rec[key]=""
            if  'LP-HYS' not in rec['method_codes']:
                SpecRecs.append(rec)
        for rec in HystRecs:
            for key in prior_keys:
                if key not in list(rec.keys()):rec[key]=""
            prior=pmag.get_dictitem(prior_data,'specimen',rec['specimen'],'T')
            if len(prior)>0 and 'sample' in list(prior[0].keys()):
                rec['sample']=prior[0]['sample'] # pull sample name from prior specimens table
            SpecRecs.append(rec)
        pmag.magic_write(spec_file,SpecRecs,"specimens")
        if verbose:print("hysteresis parameters saved in ",spec_file)
Exemplo n.º 55
0
def main(command_line=True, **kwargs):
    """
    NAME
        _2g_bin_magic.py
   
    DESCRIPTION
        takes the binary 2g format magnetometer files and converts them to magic_measurements, er_samples.txt and er_sites.txt file
 
    SYNTAX
        2g_bin_magic.py [command line options]

    OPTIONS
        -f FILE: specify input 2g (binary) file
        -F FILE: specify magic_measurements output file, default is: magic_measurements.txt
        -Fsa FILE: specify output file, default is: er_samples.txt 
        -Fsi FILE: specify output file, default is: er_sites.txt 
        -ncn NCON:  specify naming convention: default is #2 below
        -ocn OCON:  specify orientation convention, default is #5 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
             SO-SUN   orientation with sun compass
        -loc: location name, default="unknown"
        -spc NUM : specify number of characters to designate a  specimen, default = 0     
        -ins INST : specify instsrument name
        -a: average replicate measurements

    INPUT FORMAT
        Input files are horrible mag binary format (who knows why?)
        Orientation convention:
            [1] Lab arrow azimuth= mag_azimuth; Lab arrow dip=-field_dip
                i.e., field_dip is degrees from vertical down - the hade [default]
            [2] Lab arrow azimuth = mag_azimuth-90; Lab arrow dip = -field_dip
                i.e., mag_azimuth is strike and field_dip is hade
            [3] Lab arrow azimuth = mag_azimuth; Lab arrow dip = 90-field_dip
                i.e.,  lab arrow same as field arrow, but field_dip was a hade.
            [4] lab azimuth and dip are same as mag_azimuth, field_dip
            [5] lab azimuth is same as mag_azimuth,lab arrow dip=field_dip-90
            [6] Lab arrow azimuth = mag_azimuth-90; Lab arrow dip = 90-field_dip
            [7] all others you will have to either customize your 
                self or e-mail [email protected] for help.  
 
         Magnetic declination convention:
             Az will use supplied declination to correct azimuth 
    
       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 = sample name
        [6] site name entered in site_name column in the orient.txt format input file  -- NOT CURRENTLY SUPPORTED
        [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
        NB: all others you will have to either customize your
            self or e-mail [email protected] for help.

    OUTPUT
            output saved in magic_measurements.txt & er_samples.txt formatted files
              will overwrite any existing files 
    """
    #
    # initialize variables
    #
    mag_file = ''
    specnum=0
    ub_file,samp_file,or_con,corr,meas_file = "","er_samples.txt","3","1","magic_measurements.txt"
    pos_file,site_file="","er_sites.txt"
    noave=1
    args=sys.argv
    bed_dip,bed_dip_dir="",""
    samp_con,Z,average_bedding="2",1,"0"
    meths='FS-FD'
    sclass,lithology,_type="","",""
    user,inst="",""
    DecCorr=0.
    location_name="unknown"
    months=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    gmeths=""
    #
    #
    dir_path='.'
    if command_line:
        if '-WD' in args:
            ind=args.index("-WD")
            dir_path=sys.argv[ind+1]
        if "-h" in args:
            print main.__doc__
            return False
        if "-f" in args:
            ind=args.index("-f")
            mag_file=sys.argv[ind+1]
        if "-fpos" in args:
            ind=args.index("-fpos")
            pos_file=sys.argv[ind+1]
        if "-F" in args:
            ind=args.index("-F")
            meas_file=sys.argv[ind+1]
        if "-Fsa" in args:
            ind=args.index("-Fsa")
            samp_file=sys.argv[ind+1]
        if "-Fsi" in args:
            ind=args.index("-Fsi")
            site_file=sys.argv[ind+1]
        if "-ocn" in args:
            ind=args.index("-ocn")
            or_con=sys.argv[ind+1]
        if "-ncn" in args:
            ind=args.index("-ncn")
            samp_con=sys.argv[ind+1]
        if "-mcd" in args:
            ind=args.index("-mcd")
            gmeths=(sys.argv[ind+1])
        if "-loc" in args:
            ind=args.index("-loc")
            location_name=(sys.argv[ind+1])
        if "-spc" in args:
            ind=args.index("-spc")
            specnum=int(args[ind+1])

        if "-ins" in args:
            ind=args.index("-ins")
            inst=args[ind+1]
        if "-a" in args:
            noave=0
        #
        ID = False
        if '-ID' in args:
            ind = args.index('-ID')
            ID = args[ind+1]
        #

    if not command_line:
        dir_path = kwargs.get('dir_path', '.')
        mag_file = kwargs.get('mag_file', '')
        pos_file = kwargs.get('pos_file', '')
        meas_file = kwargs.get('meas_file', 'magic_measurements.txt')
        samp_file = kwargs.get('samp_file', 'er_samples.txt')
        site_file = kwargs.get('site_file', 'er_sites.txt')
        or_con = kwargs.get('or_con', '3')
        samp_con = kwargs.get('samp_con', '2')
        corr = kwargs.get('corr', '1')
        gmeths = kwargs.get('gmeths', '')
        location_name = kwargs.get('location_name', '')
        specnum = int(kwargs.get('specnum', 0))
        inst = kwargs.get('inst', '')
        noave = kwargs.get('noave', 1) # default is DO average
        ID = kwargs.get('ID', '')

    # format and fix variables acquired from command line args or input with **kwargs
    if specnum!=0:specnum=-specnum

    if ID:
        input_dir_path = ID
    else:
        input_dir_path = dir_path

    if samp_con:
        if "4" in samp_con:
            if "-" not in samp_con:
                print "option [4] must be in form 4-Z where Z is an integer"
                return False, "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, "option [7] must be in form 7-Z where Z is an integer"
            else:
                Z=samp_con.split("-")[1]
                samp_con="7"
        if "6" in samp_con:
            try:
                Samps,file_type=pmag.magic_read(os.path.join(input_dir_path, 'er_samples.txt'))
            except:
                print "there is no er_samples.txt file in your input directory - you can't use naming convention #6"
                return False, "there is no er_samples.txt file in your input directory - you can't use naming convention #6"
            if file_type == 'bad_file':
                print "there is no er_samples.txt file in your input directory - you can't use naming convention #6"
                return False, "there is no er_samples.txt file in your input directory - you can't use naming convention #6"
                

    if not mag_file:
        print "mag file is required input"
        return False, "mag file is required input"
    output_dir_path = dir_path
    mag_file = os.path.join(input_dir_path, mag_file)
    samp_file = output_dir_path+'/'+samp_file
    site_file = output_dir_path+'/'+site_file
    meas_file= output_dir_path+'/'+meas_file
    samplist=[]
    try:
        Samps,file_type=pmag.magic_read(samp_file)
        for samp in Samps:
            if samp['er_sample_name'] not in samplist: samplist.append(samp['er_sample_name'])
    except:
        Samps=[]
    MagRecs=[]
    try:
        f=open(mag_file,'rU')
        input=f.read()
        f.close()
    except Exception as ex:
        print 'ex', ex
        print "bad mag file"
        return False, "bad mag file"
    firstline,date=1,""
    d=input.split('\xcd')
    for line in d:
                rec=line.split('\x00')
                if firstline==1:
                    firstline=0
                    spec,vol="",1
                    for c in line[15:23]:
                        if c!='\x00':spec=spec+c 
# check for bad sample name
                    test=spec.split('.')
                    date=""
                    if len(test)>1:
                            spec=test[0]
                            kk=24
                            while line[kk]!='\x01' and line[kk]!='\x00':
                                kk+=1
                            vcc=line[24:kk]
                            el=10
                            while rec[el].strip()!='':el+=1
                            date,comments=rec[el+7],[]
                    else:
                        el=9
                        while rec[el]!='\x01':el+=1
                        vcc,date,comments=rec[el-3],rec[el+7],[]
                    specname=spec.lower()
                    print 'importing ',specname
                    el+=8
                    while rec[el].isdigit()==False:
                        comments.append(rec[el])
                        el+=1
                    while rec[el]=="":el+=1
                    az=float(rec[el])
                    el+=1
                    while rec[el]=="":el+=1
                    pl=float(rec[el])
                    el+=1
                    while rec[el]=="":el+=1
                    bed_dip_dir=float(rec[el])
                    el+=1
                    while rec[el]=="":el+=1
                    bed_dip=float(rec[el])
                    el+=1
                    while rec[el]=="":el+=1
                    if rec[el]=='\x01': 
                        bed_dip=180.-bed_dip
                        el+=1
                        while rec[el]=="":el+=1
                    fold_az=float(rec[el])
                    el+=1
                    while rec[el]=="":el+=1
                    fold_pl=rec[el]
                    el+=1
                    while rec[el]=="":el+=1
                    if rec[el]!="" and rec[el]!='\x02' and rec[el]!='\x01':
                        deccorr=float(rec[el])
                        az+=deccorr
                        bed_dip_dir+=deccorr
                        fold_az+=deccorr
                        if bed_dip_dir>=360:bed_dip_dir=bed_dip_dir-360.
                        if az>=360.:az=az-360.
                        if fold_az>=360.:fold_az=fold_az-360.
                    else:
                        deccorr=0
                    if specnum!=0:
                        sample=specname[:specnum]
                    else:
                        sample=specname
                    SampRec={}
                    SampRec["er_sample_name"]=sample
                    SampRec["er_location_name"]=location_name
                    SampRec["er_citation_names"]="This study"
                    labaz,labdip=pmag.orient(az,pl,or_con) # convert to labaz, labpl
        #
        # parse information common to all orientation methods
        #
                    SampRec["sample_bed_dip"]='%7.1f'%(bed_dip)
                    SampRec["sample_bed_dip_direction"]='%7.1f'%(bed_dip_dir)
                    SampRec["sample_dip"]='%7.1f'%(labdip)
                    SampRec["sample_azimuth"]='%7.1f'%(labaz)
                    if vcc.strip()!="":vol=float(vcc)*1e-6 # convert to m^3 from cc
                    SampRec["sample_volume"]='%10.3e'%(vol) # 
                    SampRec["sample_class"]=sclass
                    SampRec["sample_lithology"]=lithology
                    SampRec["sample_type"]=_type
                    SampRec["sample_declination_correction"]='%7.1f'%(deccorr)
                    methods=gmeths.split(':')
                    if deccorr!="0":
                        if 'SO-MAG' in methods:del methods[methods.index('SO-MAG')]
                        methods.append('SO-CMD-NORTH')
                    meths=""
                    for meth in methods:meths=meths+meth+":"
                    meths=meths[:-1]
                    SampRec["magic_method_codes"]=meths
                    if int(samp_con)<6 or int(samp_con) == 7: 
                        site=pmag.parse_site(SampRec["er_sample_name"],samp_con,Z) # parse out the site name
                        SampRec["er_site_name"]=site
                    elif len(Samps)>1:
                        site,location="",""
                        for samp in Samps: 
                            if samp["er_sample_name"] == SampRec["er_sample_name"]:
                                site=samp["er_site_name"]
                                location=samp["er_location_name"]
                                break
                        SampRec["er_location_name"]=samp["er_location_name"]
                        SampRec["er_site_name"]=samp["er_site_name"]
                    if sample not in samplist:
                        samplist.append(sample)
                        Samps.append(SampRec)
                else:
                    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"]=specname
                    MagRec["er_sample_name"]=SampRec['er_sample_name']
                    MagRec["er_site_name"]=SampRec['er_site_name']
                    MagRec["er_location_name"]=location_name
                    el,demag=1,''
                    treat=rec[el]
                    if treat[-1]=='C':
                        demag='T'
                    elif treat!='NRM':
                        demag='AF'  
                    el+=1
                    while rec[el]=="":el+=1
                    MagRec["measurement_dec"]=rec[el]
                    cdec=float(rec[el])
                    el+=1
                    while rec[el]=="":el+=1
                    MagRec["measurement_inc"]=rec[el]
                    cinc=float(rec[el])
                    el+=1
                    while rec[el]=="":el+=1
                    gdec=rec[el]
                    el+=1
                    while rec[el]=="":el+=1
                    ginc=rec[el]
                    el=skip(2,el,rec) # skip bdec,binc
#                el=skip(4,el,rec) # skip gdec,ginc,bdec,binc
#                print 'moment emu: ',rec[el]
                    MagRec["measurement_magn_moment"]='%10.3e'% (float(rec[el])*1e-3) # moment in Am^2 (from emu)
                    MagRec["measurement_magn_volume"]='%10.3e'% (float(rec[el])*1e-3/vol) # magnetization in A/m
                    el=skip(2,el,rec) # skip to xsig
                    MagRec["measurement_sd_x"]='%10.3e'% (float(rec[el])*1e-3) # convert from emu
                    el=skip(3,el,rec) # skip to ysig
                    MagRec["measurement_sd_y"]='%10.3e'% (float(rec[el])*1e-3) # convert from emu
                    el=skip(3,el,rec) # skip to zsig
                    MagRec["measurement_sd_z"]='%10.3e'% (float(rec[el])*1e-3) # convert from emu
                    el+=1 # skip to positions
                    MagRec["measurement_positions"]=rec[el]
#                    el=skip(5,el,rec) # skip to date
#                    mm=str(months.index(date[0]))
#                    if len(mm)==1:
#                        mm='0'+str(mm)
#                    else:
#                        mm=str(mm)
#                    dstring=date[2]+':'+mm+':'+date[1]+":"+date[3]
#                    MagRec['measurement_date']=dstring
                    MagRec["magic_instrument_codes"]=inst
                    MagRec["er_analyst_mail_names"]=""
                    MagRec["er_citation_names"]="This study"
                    MagRec["magic_method_codes"]=meas_type
                    if demag=="AF":
                        MagRec["treatment_ac_field"]='%8.3e' %(float(treat[:-2])*1e-3) # peak field in tesla
                        meas_type="LT-AF-Z"
                        MagRec["treatment_dc_field"]='0'
                    elif demag=="T":
                        MagRec["treatment_temp"]='%8.3e' % (float(treat[:-1])+273.) # temp in kelvin
                        meas_type="LT-T-Z"
                    MagRec['magic_method_codes']=meas_type
                    MagRecs.append(MagRec) 
    MagOuts=pmag.measurements_methods(MagRecs,noave)
    MagOuts, keylist = pmag.fillkeys(MagOuts) 
    pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    print "Measurements put in ",meas_file
    SampsOut,sampkeys=pmag.fillkeys(Samps)
    pmag.magic_write(samp_file,SampsOut,"er_samples")
    Sites=[]
    for samp in Samps:
        SiteRec={}
        SiteRec['er_site_name']=samp['er_site_name']
        SiteRec['er_location_name']=samp['er_location_name']
        SiteRec['site_definition']='s'
        SiteRec['er_citation_names']='This study'
        if 'sample_class' in samp.keys():SiteRec['site_class']=samp['sample_class']
        if 'sample_lithology' in samp.keys():SiteRec['site_lithology']=samp['sample_lithology']
        if 'sample_type' in samp.keys():SiteRec['site_lithology']=samp['sample_lithology']
        if 'sample_lat' in samp.keys():
            SiteRec['site_lat']=samp['sample_lat']
        else:
            SiteRec['site_lat']="-999"
        if 'sample_lon' in samp.keys():
            SiteRec['site_lon']=samp['sample_lon']
        else:
            SiteRec['site_lon']="-999"
        if 'sample_height' in samp.keys():SiteRec['site_height']=samp['sample_height']
        Sites.append(SiteRec)
    pmag.magic_write(site_file,Sites,'er_sites')
    return True, meas_file
Exemplo n.º 56
0
def main():
    """
    NAME 
        pt_rot.py 

    DESCRIPTION
        rotates pt according to specified age and plate
 
    SYNTAX
        pt_rot.py [command line options]

    OPTIONS
        -h prints help and quits
        -f file with lon lat plate age Dplate as space delimited input
           Dplate is the destination plate coordinates desires 
           - default is "fixed south africa"
           Dplate should be one of: [nwaf, neaf,saf,aus, eur, ind, sam, ant, grn, nam]
        -ff file Efile,   file  has lat lon data file and Efile has sequential rotation poles: Elat Elon Omega 
        -F OFILE, output pmag_results formatted file with rotated points stored in vgp_lon, vgp_lat
           default is to print out rotated lon, lat to standard output
    
    """
    dir_path='.'
    PTS=[]
    ResRecs=[]
    ofile=""
    Dplates=['nwaf', 'neaf','saf','aus', 'eur', 'ind', 'sam', 'ant', 'grn', 'nam']
    if '-WD' in sys.argv:
        ind = sys.argv.index('-WD')
        dir_path=sys.argv[ind+1]
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-F' in sys.argv:
        ind = sys.argv.index('-F')
        ofile=dir_path+'/'+sys.argv[ind+1]
    if '-f' in sys.argv:
        ind = sys.argv.index('-f')
        file=dir_path+'/'+sys.argv[ind+1]
        f=open(file,'rU')
        data=f.readlines()
    elif '-ff' in sys.argv:
        ind = sys.argv.index('-ff')
        file=dir_path+'/'+sys.argv[ind+1]
        f=open(file,'rU')
        data=f.readlines()
        Efile=dir_path+'/'+sys.argv[ind+2]
        f=open(Efile,'rU')
        edata=f.readlines()
        Poles=[]
        for p in edata:
             rec=p.split()
             pole=[float(rec[0]),float(rec[1]),float(rec[2])] # pole is lat/lon/omega
             Poles.append(pole)
    else:
        data=sys.stdin.readlines()
    for line in data:
        PtRec={}
        rec=line.split()
        PtRec['site_lon']=rec[0]
        PtRec['site_lat']=rec[1]
        if '-ff' in sys.argv:
            pt_lat,pt_lon=float(rec[1]),float(rec[0])
            for pole in Poles:
                ptrot= pmag.PTrot(pole,[pt_lat],[pt_lon])
                pt_lat=ptrot[0][0]
                pt_lon=ptrot[1][0]
            if ofile=="":
                print ptrot[1][0], ptrot[0][0]
            else:
                ResRec={'vgp_lat': '%7.1f'%(ptrot[0][0]),'vgp_lon':'%7.1f'%( ptrot[1][0])}
                ResRecs.append(ResRec)
        else:
            PtRec['cont']=rec[2]
            if PtRec['cont']=='af':PtRec['cont']='saf' # use fixed south africa
            PtRec['age']=rec[3]
            if len(rec)>4:
               PtRec['dcont']=rec[4]
            PTS.append(PtRec)
    if '-ff' not in sys.argv:
        for pt in PTS:
            pole='not specified'
            pt_lat=float(pt['site_lat'])
            pt_lon=float(pt['site_lon'])
            age=float(pt['age'])
            ptrot=[[pt_lat],[pt_lon]]
            if pt['cont']=='ib':
                pole=frp.get_pole(pt['cont'],age)
                ptrot= pmag.PTrot(pole,[pt_lat],[pt_lon])
                pt_lat=ptrot[0][0]
                pt_lon=ptrot[1][0]
                pt['cont']='eur'
            if pt['cont']!='saf':
                pole1=frp.get_pole(pt['cont'],age)
                ptrot= pmag.PTrot(pole1,[pt_lat],[pt_lon])
                if 'dcont' in pt.keys():
                    pt_lat=ptrot[0][0]
                    pt_lon=ptrot[1][0]
                    pole=frp.get_pole(pt['dcont'],age)
                    pole[2]=-pole[2] 
                    ptrot= pmag.PTrot(pole,[pt_lat],[pt_lon])
                if ofile=="":
                    print ptrot[1][0], ptrot[0][0]
                else:
                    ResRec={'vgp_lat': '%7.1f'%(ptrot[0][0]),'vgp_lon':'%7.1f'%( ptrot[1][0])}
                    ResRecs.append(ResRec)
            else:
                if 'dcont' in pt.keys():
                    pole=frp.get_pole(pt['dcont'],age)
                    pole[2]=-pole[2] 
                    ptrot= pmag.PTrot(pole,[pt_lat],[pt_lon])
                    print ptrot
                    if ofile=="":
                        print ptrot[1][0], ptrot[0][0] 
                    else:
                        ResRec={'vgp_lat': '%7.1f'%(ptrot[0][0]),'vgp_lon':'%7.1f'%( ptrot[1][0])}
                        ResRecs.append(ResRec)
                else:
                    if ofile=="":
                        print ptrot[1][0], ptrot[0][0]
                    else:
                        ResRec={'vgp_lat': '%7.1f'%(ptrot[0][0]),'vgp_lon':'%7.1f'%( ptrot[1][0])}
                        ResRecs.append(ResRec)
    if len(ResRecs)>0:
        pmag.magic_write(ofile,ResRecs,'pmag_results')
Exemplo n.º 57
0
def main(command_line=True, **kwargs):
    """
    NAME
        cit_magic.py

    DESCRIPTION
        converts CIT and .sam  format files to magic_measurements format files

    SYNTAX
        cit_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify .sam format input file, required
        -fsi SITEFILE : specify file with site names and locations [tab delimited magic file]
        -F FILE: specify output  measurements file, default is magic_measurements.txt
        -Fsp FILE: specify output er_specimens.txt file, default is er_specimens.txt
        -Fsi FILE: specify output er_sites.txt file, default is er_sites.txt
        -Fsa FILE: specify output er_samples.txt file, default is er_samples.txt  # LORI
        -n [gm,kg,cc,m3]: specify normalization
        -A: don't average replicate measurements
        -spc NUM : specify number of characters to designate a  specimen, default = 0
        -ncn NCON: specify naming convention
        -loc LOCNAME : specify location/study name, must have either LOCNAME or SITEFILE or be a synthetic
        -mcd [FS-FD:SO-MAG,.....] colon delimited list for method codes applied to all specimens in .sam file
        -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
        -dm: specify which magic data model you want your output in 3.0 or 2.5 default is 2.5

    INPUT
        Best to put separate experiments (all AF, thermal, thellier, trm aquisition, Shaw, etc.)

    NOTES:
         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 = sample name
        [6] site name entered in site_name column in the orient.txt format input file  -- NOT CURRENTLY SUPPORTED
        [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
        NB: all others you will have to either customize your
            self or e-mail [email protected] for help.
    """
    #
    #initialize variables
    norm='cc'
    samp_con,Z='3',1
    meas_file='measurements.txt'
    spec_file='specimens.txt'
    samp_file='samples.txt'
    site_file='sites.txt'
    Specs,Samps,Sites,Locs=[],[],[],[]
    MeasRecs=[]
    specnum,units,locname=0,"1","unknown"
    citation="This study"
    dir_path='.'
    args=sys.argv
    if command_line:
        if '-WD' in args:
            ind=args.index("-WD")
            dir_path=args[ind+1]
        if "-h" in args:
            print(main.__doc__)
            return False
        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 '-Fsp' in args:
            ind=args.index("-Fsp")
            spec_file=args[ind+1]
        if '-Fsa' in args:
            ind=args.index("-Fsa")
            samp_file=args[ind+1]
        if '-Fsi' in args:   # LORI addition
            ind=args.index("-Fsi")
            site_file=args[ind+1]
        if '-loc' in args:
            ind=args.index("-loc")
            locname=args[ind+1]
        if '-mcd' in args:
            ind=args.index("-mcd")
            methods=args[ind+1]
        else:
            methods='SO-MAG'
        if '-spc' in args:
            ind=args.index("-spc")
            specnum=-int(args[ind+1])
        if '-n' in args:
            ind=args.index("-n")
            norm=args[ind+1]
        if "-A" in args:
            avg=1
        else:
            avg=0
        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")
                    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 '-f' in args:
            ind=args.index("-f")
            magfile=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 '-dm' in args:
            dmi = args.index('-dm')
            data_model = args[dmi+1]

    # if you are running as a module:
    elif not command_line:
        dir_path = kwargs.get('dir_path', '.')
        user = kwargs.get('user', '')
        meas_file = kwargs.get('meas_file', 'measurements.txt') # outfile
        spec_file = kwargs.get('spec_file', 'specimens.txt') # specimen outfile
        samp_file = kwargs.get('samp_file', 'samples.txt') # sample outfile
        site_file = kwargs.get('site_file', 'sites.txt') # site outfile
        loc_file = kwargs.get('loc_file', 'locations.txt') # location outfile
        data_model = kwargs.get('data_model', 2.5) # data_model to output as
        locname = kwargs.get('locname', '')
        methods = kwargs.get('methods', ['SO-MAG'])
        specnum = -int(kwargs.get('specnum', 0))
        norm = kwargs.get('norm', 'cc')
        avg = kwargs.get('avg', 0)  # 0 means do average, 1 means don't
        samp_con = kwargs.get('samp_con', '3')
        magfile = kwargs.get('magfile', '')
        input_dir_path = kwargs.get('input_dir_path', dir_path)
        output_dir_path = dir_path
    # done with module-specific stuff

    # formatting and checking variables
    if "4" in samp_con:
        if "-" not in samp_con:
            print("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"

    magfile = os.path.join(input_dir_path, magfile)
    spec_file = os.path.join(output_dir_path, spec_file)
    samp_file = os.path.join(output_dir_path, samp_file)
    site_file = os.path.join(output_dir_path, site_file)
    meas_file= os.path.join(output_dir_path, meas_file)
    try:
        file_input=open(magfile,'r')
    except Exception as ex:
        print("bad sam file name: ", magfile)
        return False, "bad sam file name"
    File = file_input.readlines()
    if len(File) == 1: File = File[0].split('\r'); File = [x+"\r\n" for x in File]
    sids,ln,format=[],0,'CIT'
    formats=['CIT','2G','APP','JRA']
    if File[ln].strip()=='CIT': ln+=1
    LocRec={}
    LocRec["er_location_name"]=locname
    LocRec["er_citation_names"]=citation
    comment=File[ln]
    if comment=='CIT':
       format=comment
       ln+=1
    comment=File[ln]
    print(comment)
    ln+=1
    specimens,samples,sites=[],[],[]
    if format=='CIT':
        line=File[ln].split()
        site_lat=line[0]
        site_lon=line[1]
        LocRec["location_begin_lat"]=site_lat
        LocRec["location_begin_lon"]=site_lon
        LocRec["location_end_lat"]=site_lat
        LocRec["location_end_lon"]=site_lon
        Locs.append(LocRec)
        try: Cdec=float(line[2])
        except ValueError: pdb.set_trace()
        for k in range(ln+1,len(File)):
            line=File[k]
            rec=line.split()
            if rec == []: continue
            specimen=rec[0]
            specimens.append(specimen)
    for specimen in specimens:
        SpecRec,SampRec,SiteRec={},{},{}
        if specnum!=0:
            sample=specimen[:specnum]
        else: sample=specimen
        site=pmag.parse_site(sample,samp_con,Z)
        SpecRec['er_specimen_name']=specimen
        SpecRec['er_sample_name']=sample
        SpecRec['er_site_name']=site
        SpecRec['er_location_name']=locname
        SpecRec['er_citation_names']=citation
        SampRec['er_sample_name']=sample
        SampRec['er_site_name']=site
        SampRec['er_location_name']=locname
        SampRec['er_citation_names']=citation
        SampRec['magic_method_codes']=methods
        SampRec['sample_declination_correction']='%7.1f'%(Cdec)
        SiteRec['er_site_name']=site
        SiteRec['er_location_name']=locname
        SiteRec['er_citation_names']=citation
        SiteRec['site_lat']=site_lat
        SiteRec['site_lon']=site_lon
        f=open(input_dir_path+'/'+specimen,'rU')
        Lines=f.readlines()
        comment=""
        line=Lines[0].split()
        if len(line)>2:
            comment=line[2]
        info=Lines[1].split()
        vol=float(info[-1])
        if vol!=1.0:
            if norm=='cc':units="1"
            if norm=='m3':units="2"
            SpecRec['specimen_weight']=""
            if units=="1" or "":
                SpecRec['specimen_volume']='%10.3e'%(vol*1e-6)
            else:
                SpecRec['specimen_volume']='%10.3e'%(vol)
        else:
            if norm=='cc':units="1"
            if norm=='m3':units="2"
            SpecRec['specimen_volume']=""
            if units=="1" or "":
                SpecRec['specimen_weight']='%10.3e'%(vol*1e-3)
            else:
                SpecRec['specimen_weight']='%10.3e'%(vol)
        dip=float(info[-2])
        dip_direction=float(info[-3])+Cdec+90.
        sample_dip=-float(info[-4])
        sample_azimuth=float(info[-5])+Cdec-90.
        if len(info)>5:
            SampRec['sample_height']=info[-6]
        else:
            SampRec['sample_height']='0'
        SampRec['sample_azimuth']='%7.1f'%(sample_azimuth)
        SampRec['sample_dip']='%7.1f'%(sample_dip)
        SampRec['sample_bed_dip']='%7.1f'%(dip)
        SampRec['sample_bed_dip_direction']='%7.1f'%(dip_direction)
        SampRec['sample_class']=''
        SampRec['sample_type']=''
        SampRec['sample_lithology']=''
        if Cdec!=0 or Cdec!="":
            SampRec['magic_method_codes']='SO-CMD-NORTH'
        else:
            SampRec['magic_method_codes']='SO-MAG'
        for line in Lines[2:len(Lines)]:
            #print 'line:', line
            MeasRec=SpecRec.copy()

#           Remove specimen_volume and specimen_weight as they do not exits in the magic_measurement table
            del MeasRec["specimen_volume"]
            del MeasRec["specimen_weight"]

            treat_type=line[0:3]
            treat=line[2:6]
            try: float(treat)
            except ValueError: treat = line[3:6]
            if treat_type.startswith('NRM'):
                MeasRec['magic_method_codes']='LT-NO'
                MeasRec['measurement_temp']='273'
                MeasRec['treatment_temp']='273'
                MeasRec['treatment_dc_field']='0'
                MeasRec['treatment_ac_field']='0'
            elif treat_type.startswith('AF'):
                MeasRec['magic_method_codes']='LT-AF-Z'
                MeasRec['measurement_temp']='273'
                MeasRec['treatment_temp']='273'
                MeasRec['treatment_dc_field']='0'
                if treat.strip() == '':
                    MeasRec['treatment_ac_field']='0'
                else:
                    MeasRec['treatment_ac_field']='%10.3e'%(float(treat)*1e-3)
            elif treat_type.startswith('ARM'):
                MeasRec['magic_method_codes']="LP-ARM"
                MeasRec['measurement_temp']='273'
                MeasRec['treatment_temp']='273'
                MeasRec['treatment_dc_field']='0'
                if treat.strip() == '':
                    MeasRec['treatment_ac_field']='0'
                else:
                    MeasRec['magic_method_codes']="LP-ARM-AFD"
                    MeasRec['treatment_ac_field']='%10.3e'%(float(treat)*1e-3)
            elif treat_type.startswith('TT'):
                MeasRec['magic_method_codes']='LT-T-Z'
                MeasRec['measurement_temp']='273'
                if treat.strip() == '':
                    MeasRec['treatment_temp']='273'
                else:
                    MeasRec['treatment_temp']='%7.1f'%(float(treat)+273)
                MeasRec['treatment_dc_field']='0'
                MeasRec['treatment_ac_field']='0'
            elif treat_type.startswith('LT') or treat_type.startswith('LN2'):
                MeasRec['magic_method_codes']='LT-LT-Z'
                MeasRec['measurement_temp']='273'
                MeasRec['treatment_temp']='77'
                MeasRec['treatment_dc_field']='0'
                MeasRec['treatment_ac_field']='0'
            else:
                print("trouble with your treatment steps")
            MeasRec['measurement_dec']=line[46:51]
            MeasRec['measurement_inc']=line[52:58]
            M='%8.2e'%(float(line[31:39])*vol*1e-3) # convert to Am2
            MeasRec['measurement_magn_moment']=M
            MeasRec['measurement_csd']='%7.1f'%(eval(line[41:46]))
            MeasRec["measurement_positions"]='1'
            MeasRec['measurement_standard']='u'
            if len(line)>60:
                MeasRec['magic_instrument_codes']=line[85:].strip('\n \r \t "')
                MeasRec['measurement_sd_x']='%8.2e'%(float(line[58:67])*1e-8) #(convert e-5emu to Am2)
                MeasRec['measurement_sd_y']='%8.2e'%(float(line[67:76])*1e-8)
                MeasRec['measurement_sd_z']='%8.2e'%(float(line[76:85])*1e-8)
            MeasRecs.append(MeasRec)
        Specs.append(SpecRec)
        if sample not in samples:
            samples.append(sample)
            Samps.append(SampRec)
        site=pmag.parse_site(sample,samp_con,Z)
        if site not in sites:
            sites.append(site)
            Sites.append(SiteRec)

    if float(data_model)==3.0:
        con = nb.Contribution('.',read_tables=[])

        con.add_empty_magic_table('specimens')
        con.add_empty_magic_table('samples')
        con.add_empty_magic_table('sites')
        con.add_empty_magic_table('locations')
        con.add_empty_magic_table('measurements')

        con.tables['specimens'].df = DataFrame(Specs).rename(columns=mapm.spec_magic2_2_magic3_map)
        con.tables['samples'].df = DataFrame(Samps).rename(columns=mapm.samp_magic2_2_magic3_map)
        con.tables['sites'].df = DataFrame(Sites).rename(columns=mapm.site_magic2_2_magic3_map)
        con.tables['locations'].df = DataFrame(Locs).rename(columns=mapm.loc_magic2_2_magic3_map)
        Fixed=pmag.measurements_methods(MeasRecs,avg)
        con.tables['measurements'].df = DataFrame(Fixed).rename(columns=mapm.meas_magic2_2_magic3_map)

        con.tables['specimens'].write_magic_file(custom_name=spec_file)
        print('specimens stored in ',spec_file)
        con.tables['samples'].write_magic_file(custom_name=samp_file)
        print('samples stored in ',samp_file)
        con.tables['sites'].write_magic_file(custom_name=site_file)
        print('sites stored in ', site_file)
        con.tables['locations'].write_magic_file(custom_name=loc_file)
        print('locations stored in ', loc_file)
        con.tables['measurements'].write_magic_file(custom_name=meas_file)
        print('data stored in ',meas_file)
    else:
        pmag.magic_write(spec_file,Specs,'er_specimens')
        print('specimens stored in ',spec_file)
        pmag.magic_write(samp_file,Samps,'er_samples')
        print('samples stored in ',samp_file)
        pmag.magic_write(site_file,Sites,'er_sites')
        print('sites stored in ', site_file)
        Fixed=pmag.measurements_methods(MeasRecs,avg)
        pmag.magic_write(meas_file,Fixed,'magic_measurements')
        print('data stored in ',meas_file)
        return True, meas_file

    return True, meas_file
Exemplo n.º 58
0
def main(command_line=True, **kwargs):
    """
    NAME
        utrecht_magic.py

    DESCRIPTION
        converts Utrecht magnetometer data files to magic_measurements files

    SYNTAX
        utrecht_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)
        -WD: output directory for MagIC files
        -ncn: Site Naming Convention
         Site to 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: default] 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 = sample name
            [6] site name entered in site_name column in the orient.txt format input file  -- NOT CURRENTLY SUPPORTED
            [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
        -spc: number of characters to remove to generate sample names from specimen names
        -dmy: European date format
        -loc LOCNAME : specify location/study name
        -lat latitude of samples
        -lon longitude of samples
        -A: don't average replicate measurements
        -mcd: [SO-MAG,SO-SUN,SO-SIGHT...] supply how these samples were oriented
        -dc: B PHI THETA: dc lab field (in microTesla), phi,and theta must be input as a tuple "(DC,PHI,THETA)". If not input user will be asked for values, this is advantagious if there are differing dc fields between steps or specimens. Note: this currently only works with the decimal IZZI naming convetion (XXX.0,1,2,3 where XXX is the treatment temperature and 0 is a zero field step, 1 is in field, and 2 is a pTRM check, 3 is a tail check). All other steps are hardcoded dc_field = 0.

    INPUT
        Utrecht magnetometer data file
    """
# initialize some stuff
    sample_lat = 0.0
    sample_lon = 0.0
    noave = 0
    er_location_name = "unknown"
    args = sys.argv
    meth_code = "LP-NO"
    version_num = pmag.get_version()
    site_num = 1

    mag_file = ""
    dir_path = '.'
    MagRecs = []
    SpecOuts = []
    SampOuts = []
    SiteOuts = []

    meas_file='magic_measurements.txt'
    spec_file='er_specimens.txt'
    samp_file='er_samples.txt'
    site_file='er_sites.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 '-Fsp' in args:
            ind=args.index("-Fsp")
            spec_file=args[ind+1]
        if '-Fsa' in args:
            ind = args.index("-Fsa")
            samp_file = args[ind+1]
        if '-Fsi' in args:   # LORI addition
            ind=args.index("-Fsi")
            site_file=args[ind+1]
            #try:
            #    open(samp_file,'r')
            #    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 "-lat" in args:
            ind = args.index("-lat")
            site_lat = args[ind+1]
        if "-lon" in args:
            ind = args.index("-lon")
            site_lon = 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 "-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")
                    return False, "naming convention option [4] must be in form 4-Z where Z is an integer"
                else:
                    site_num=samp_con.split("-")[1]
                    samp_con="4"
            elif "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:
                    site_num=samp_con.split("-")[1]
                    samp_con="7"
        else: samp_con="1"
        if '-dc' in args:
            ind=args.index('-dc')
            DC_FIELD,DC_PHI,DC_THETA=list(map(float,args[ind+1].strip('( ) [ ]').split(',')))
            DC_FIELD *= 1e-6
            yn=''
            GET_DC_PARAMS=False
        else: DC_FIELD,DC_PHI,DC_THETA=0,0,-90
        if '-spc' in args:
            ind=args.index("-spc")
            specnum=-int(args[ind+1])
        else: specnum = 0
        if '-dmy' in args:
            ind=args.index("-dmy")
            dmy_flag=True
        else: dmy_flag=False


    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')
        spec_file = kwargs.get('spec_file', 'er_specimens.txt') # specimen outfile
        samp_file = kwargs.get('samp_file', 'er_samples.txt')
        site_file = kwargs.get('site_file', 'er_sites.txt') # site outfile
        er_location_name = kwargs.get('location_name', '')
        site_lat = kwargs.get('site_lat', '')
        site_lon = kwargs.get('site_lon', '')
        #oave = kwargs.get('noave', 0) # default (0) means DO average
        meth_code = kwargs.get('meth_code', "LP-NO")
        specnum = -int(kwargs.get('specnum', 0))
        samp_con = kwargs.get('samp_con', '2')
        if "4" in samp_con:
            if "-" not in samp_con:
                print("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:
                site_num=samp_con.split("-")[1]
                samp_con="4"
        elif "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:
                site_num=samp_con.split("-")[1]
                samp_con="7"
        DC_FIELD,DC_PHI,DC_THETA = list(map(float, kwargs.get('dc_params', (0,0,-90))))
        DC_FIELD *= 1e-6
        noave = kwargs.get('avg', True)
        dmy_flag = kwargs.get('dmy_flag', False)

    # format variables
    if not mag_file:
        return False, 'You must provide a Utrecht formated file'
    mag_file = os.path.join(input_dir_path, mag_file)
    meas_file = os.path.join(output_dir_path, meas_file)
    spec_file = os.path.join(output_dir_path, spec_file)
    samp_file = os.path.join(output_dir_path, samp_file)
    site_file = os.path.join(output_dir_path, site_file)

    # parse data

    # Open up the Utrecht file and read the header information
    print('mag_file in utrecht_file', mag_file)
    AF_or_T = mag_file.split('.')[-1]
    data = open(mag_file, 'r')
    line = data.readline()
    line_items = line.split(',')
    operator=line_items[0]
    operator=operator.replace("\"","")
    machine=line_items[1]
    machine=machine.replace("\"","")
    machine=machine.rstrip('\n')
    print("operator=", operator)
    print("machine=", machine)

    #read in measurement data
    line = data.readline()
    while line != "END" and line != '"END"':
        ErSpecRec,ErSampRec,ErSiteRec = {},{},{}
        line_items = line.split(',')
        spec_name=line_items[0]
        spec_name=spec_name.replace("\"","")
        print("spec_name=", spec_name)
        free_string=line_items[1]
        free_string=free_string.replace("\"","")
        print("free_string=", free_string)
        dec=line_items[2]
        print("dec=", dec)
        inc=line_items[3]
        print("inc=", inc)
        volume=float(line_items[4])
        volume=volume * 1e-6 # enter volume in cm^3, convert to m^3
        print("volume=", volume)
        bed_plane=line_items[5]
        print("bed_plane=", bed_plane)
        bed_tilt=line_items[6]
        print("bed_tilt=", bed_tilt)

        # Configure et er_ tables
        ErSpecRec['er_specimen_name'] = spec_name
        if specnum==0: sample_name = spec_name
        else: sample_name = spec_name[:specnum]
        ErSampRec['er_sample_name'] = sample_name
        ErSpecRec['er_sample_name'] = sample_name
        er_site_name = pmag.parse_site(sample_name,samp_con,site_num)
        ErSpecRec['er_site_name']=er_site_name
        ErSpecRec['er_location_name']=er_location_name
        ErSampRec['sample_azimuth'] = dec
        ErSampRec['sample_dip'] = str(float(inc)-90)
        ErSampRec['sample_bed_dip_direction'] = bed_plane
        ErSampRec['sample_bed_tilt'] = bed_tilt
        ErSiteRec['site_lat'] = site_lat
        ErSiteRec['site_lon'] = site_lon
        ErSpecRec['magic_method_codes'] = meth_code
        ErSampRec['er_location_name'] = er_location_name
        ErSiteRec['er_location_name'] = er_location_name
        ErSiteRec['er_site_name'] = er_site_name
        ErSampRec['er_site_name'] = er_site_name
        ErSampRec['er_citation_names'] = 'This study'
        SpecOuts.append(ErSpecRec)
        SampOuts.append(ErSampRec)
        SiteOuts.append(ErSiteRec)

        #measurement data
        line = data.readline()
        line = line.rstrip("\n")
        items = line.split(",")
        while line != '9999':
            print(line)
            step=items[0]
            step=step.split('.')
            step_value=step[0]
            step_type = ""
            if len(step) == 2:
                step_type=step[1]
            if step_type=='5':
                step_value = items[0]
            A=float(items[1])
            B=float(items[2])
            C=float(items[3])
#  convert to MagIC coordinates
            Z=-A
            X=-B
            Y=C
            cart = np.array([X, Y, Z]).transpose()
            direction = pmag.cart2dir(cart).transpose()
            measurement_dec = direction[0]
            measurement_inc = direction[1]
            measurement_magn_moment = direction[2] * 1.0e-12 # the data are in pico-Am^2 - this converts to Am^2
            measurement_magn_volume = direction[2] * 1.0e-12 / volume # data volume normalized - converted to A/m
            print("measurement_magn_moment=", measurement_magn_moment)
            print("measurement_magn_volume=", measurement_magn_volume)
            error = items[4]
            date=items[5]
            date=date.strip('"')
            if date.count("-") > 0:
                date=date.split("-")
            elif date.count("/") > 0:
                date=date.split("/")
            else: print("date format seperator cannot be identified")
            print(date)
            time=items[6]
            time=time.strip('"')
            time=time.split(":")
            print(time)
            if dmy_flag:
                date_time = date[1] + ":" + date[0] + ":" + date[2] + ":" + time[0] + ":" + time[1] + ":" + "0.0"
            else:
                date_time = date[0] + ":" + date[1] + ":" + date[2] + ":" + time[0] + ":" + time[1] + ":" + "0.0"
            print(date_time)

            MagRec = {}
            MagRec["er_analyst_mail_names"] = operator
            MagRec["magic_instrument_codes"] = "Utrecht_" + machine
            MagRec["measurement_description"] = "free string = " + free_string
            MagRec["measurement_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'] = sample_name
            MagRec['magic_software_packages'] = version_num
            MagRec["measurement_temp"] = '%8.3e' % (273) # room temp in kelvin
            MagRec["measurement_flag"] = 'g'
            MagRec["measurement_standard"] = 'u'
            MagRec["magic_experiment_name"] = er_location_name + er_site_name + spec_name
            MagRec["measurement_number"] = er_location_name + er_site_name + spec_name + items[0]
            MagRec["er_specimen_name"] = spec_name
            # MagRec["treatment_ac_field"] = '0'
            if AF_or_T.lower() == "th":
                MagRec["treatment_temp"] = '%8.3e' % (float(step_value)+273.) # temp in kelvin
                MagRec['treatment_ac_field']='0'
                meas_type = "LP-DIR-T:LT-T-Z"
            else:
                MagRec['treatment_temp']='273'
                MagRec['treatment_ac_field']='%10.3e'%(float(step_value)*1e-3)
                meas_type = "LP-DIR-AF:LT-AF-Z"
            MagRec['treatment_dc_field']='0'
            if step_value == '0':
                meas_type = "LT-NO"
            print("step_type=", step_type)
            if step_type == '0' and AF_or_T.lower() == 'th':
                if meas_type == "":
                    meas_type = "LT-T-Z"
                else:
                    meas_type = meas_type + ":" + "LT-T-Z"
            elif step_type == '1':
                if meas_type == "":
                    meas_type = "LT-T-I"
                else:
                    meas_type = meas_type + ":" + "LT-T-I"
                MagRec['treatment_dc_field']='%1.2e'%DC_FIELD
            elif step_type == '2':
                if meas_type == "":
                    meas_type = "LT-PTRM-I"
                else:
                    meas_type = meas_type + ":" + "LT-PTRM-I"
                MagRec['treatment_dc_field']='%1.2e'%DC_FIELD
            elif step_type == '3':
                if meas_type == "" :
                    meas_type = "LT-PTRM-Z"
                else:
                    meas_type = meas_type + ":" + "LT-PTRM-Z"
            print("meas_type=", meas_type)
            MagRec['treatment_dc_field_phi'] = '%1.2f'%DC_PHI
            MagRec['treatment_dc_field_theta'] = '%1.2f'%DC_THETA
            MagRec['magic_method_codes'] = meas_type
            MagRec["measurement_magn_moment"] = measurement_magn_moment
            MagRec["measurement_magn_volume"] = measurement_magn_volume
            MagRec["measurement_dec"] = measurement_dec
            MagRec["measurement_inc"] = measurement_inc
            MagRec['measurement_csd'] = error 
#           MagRec['measurement_positions'] = '1'
            MagRecs.append(MagRec)

            line = data.readline()
            line = line.rstrip("\n")
            items = line.split(",")
        line = data.readline()
        line = line.rstrip("\n")
        items = line.split(",")

# write out the data to MagIC data files
    pmag.magic_write(spec_file, SpecOuts, 'er_specimens')
    pmag.magic_write(samp_file, SampOuts, 'er_samples')
    pmag.magic_write(site_file, SiteOuts, 'er_sites')
#    MagOuts = pmag.measurements_methods(MagRecs, noave)
#    pmag.magic_write(meas_file, MagOuts, 'magic_measurements')
    pmag.magic_write(meas_file, MagRecs, 'magic_measurements')
    print("results put in ", meas_file)
    print("exit!")
    return True, meas_file