예제 #1
0
파일: di_geo.py 프로젝트: jbowles100/PmagPy
def main():
    """
    NAME
       di_geo.py

    DESCRIPTION
       rotates specimen coordinate dec, inc data to geographic
       coordinates using the azimuth and plunge of the X direction

    INPUT FORMAT
        declination inclination azimuth plunge

    SYNTAX
       di_geo.py [-h][-i][-f FILE] [< filename ]

    OPTIONS
        -h prints help message and quits
        -i for interactive data entry
        -f FILE command line entry of file name
        -F OFILE, specify output file, default is standard output
    OUTPUT:
        declination inclination
 """
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-F' in sys.argv:
        ind = sys.argv.index('-F')
        ofile = sys.argv[ind + 1]
        out = open(ofile, 'w')
        print ofile, ' opened for output'
    else:
        ofile = ""

    if '-i' in sys.argv:  # interactive flag
        while 1:
            try:
                Dec = float(raw_input("Declination: <cntrl-D> to quit  "))
            except EOFError:
                print "\n Good-bye\n"
                sys.exit()
            Inc = float(raw_input("Inclination: "))
            Az = float(raw_input("Azimuth: "))
            Pl = float(raw_input("Plunge: "))
            print '%7.1f %7.1f' % (pmag.dogeo(Dec, Inc, Az, Pl))
    elif '-f' in sys.argv:
        ind = sys.argv.index('-f')
        file = sys.argv[ind + 1]
        data = numpy.loadtxt(file)
    else:
        data = numpy.loadtxt(
            sys.stdin, dtype=numpy.float)  # read in the data from the datafile
    D, I = pmag.dogeo_V(data)
    for k in range(len(D)):
        if ofile == "":
            print '%7.1f %7.1f' % (D[k], I[k])
        else:
            out.write('%7.1f %7.1f\n' % (D[k], I[k]))
예제 #2
0
def main():
    """
    NAME
       di_geo.py

    DESCRIPTION
       rotates specimen coordinate dec, inc data to geographic
       coordinates using the azimuth and plunge of the X direction

    INPUT FORMAT
        declination inclination azimuth plunge

    SYNTAX
       di_geo.py [-h][-i][-f FILE] [< filename ]

    OPTIONS
        -h prints help message and quits
        -i for interactive data entry
        -f FILE command line entry of file name
        -F OFILE, specify output file, default is standard output
    OUTPUT:
        declination inclination
 """
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-F' in sys.argv:
        ind=sys.argv.index('-F')
        ofile=sys.argv[ind+1]
        out=open(ofile,'w')
        print ofile, ' opened for output'
    else: ofile=""

    if '-i' in sys.argv: # interactive flag
        while 1:
            try:
                Dec=float(raw_input("Declination: <cntrl-D> to quit  "))
            except EOFError:
                print "\n Good-bye\n"
                sys.exit()
            Inc=float(raw_input("Inclination: "))
            Az=float(raw_input("Azimuth: "))
            Pl=float(raw_input("Plunge: "))
            print '%7.1f %7.1f'%(pmag.dogeo(Dec,Inc,Az,Pl))
    elif '-f' in sys.argv:
        ind=sys.argv.index('-f')
        file=sys.argv[ind+1]
        data=numpy.loadtxt(file)
    else:
        data=numpy.loadtxt(sys.stdin,dtype=numpy.float) # read in the data from the datafile
    D,I=pmag.dogeo_V(data)
    for k in range(len(D)):
        if ofile=="":
            print '%7.1f %7.1f'%(D[k],I[k])
        else:
            out.write('%7.1f %7.1f\n'%(D[k],I[k]))
예제 #3
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
예제 #4
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
예제 #5
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
예제 #6
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,'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 "-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, 'r')
    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.iloc[ind]['measurement_dec'],
                                data.iloc[ind]['measurement_inc'],
                                data.iloc[ind]['sample_azimuth'],
                                data.iloc[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
예제 #7
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 list(rec.keys()):
            rec['sample_orientation_flag'] = 'g'
        if 'sample_description' not in list(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 list(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.plot_lnp(EQ['eqarea'], site, data, fpars,
                                 'specimen_direction_type')
            pmagplotlib.draw_figs(EQ)
            if k != 0 and repeat != 'y':
                ans = input(
                    "s[a]ve plot, [q]uit, [e]dit specimens, [p]revious site, <return> to continue:\n "
                )
            elif k == 0 and repeat != 'y':
                ans = 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.save_plots(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 = 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.plot_xy(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.plot_xy(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.plot_xy(EQ['eqarea'], X_d, Y_d, sym='b.')
                        pmagplotlib.plot_xy(EQ['eqarea'], X_up, Y_up, sym='c.')
                        pmagplotlib.draw_figs(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 = input("Mark this sample as bad? y/[n]  ")
                if deleteme == 'y':
                    reason = 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 = 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 = 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)
예제 #8
0
def main():
    """
    NAME
        zeq_magic_redo.py
   
    DESCRIPTION
        Calculate principal components through demagnetization data using bounds and calculation type stored in "redo" file
  
    SYNTAX
        zeq_magic_redo.py [command line options]

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

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

    DESCRIPTION
        reads in magic_measurements formatted file, makes plots of remanence decay
        during demagnetization experiments.  Reads in prior interpretations saved in 
        a pmag_specimens formatted file and  allows re-interpretations of best-fit lines
        and planes and saves (revised or new) interpretations in a pmag_specimens file.  
        interpretations are saved in the coordinate system used. Also allows judicious editting of
        measurements to eliminate "bad" measurements.  These are marked as such in the magic_measurements
        input file.  they are NOT deleted, just ignored. 

    SYNTAX
        zeq_magic.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f  MEASFILE: sets magic_measurements format input file, default: magic_measurements.txt
        -fsp SPECFILE: sets pmag_specimens format file with prior interpreations, default: zeq_specimens.txt
        -Fp PLTFILE: sets filename for saved plot, default is name_type.fmt (where type is zijd, eqarea or decay curve)
        -crd [s,g,t]: sets coordinate system,  g=geographic, t=tilt adjusted, default: specimen coordinate system
        -fsa SAMPFILE: sets er_samples format file with orientation information, default: er_samples.txt
        -spc SPEC  plots single specimen SPEC, saves plot with specified format 
              with optional -dir settings and quits
        -dir [L,P,F][beg][end]: sets calculation type for principal component analysis, default is none
             beg: starting step for PCA calculation
             end: ending step for PCA calculation
             [L,P,F]: calculation type for line, plane or fisher mean
             must be used with -spc option
        -fmt FMT: set format of saved plot [png,svg,jpg]
        -A:  suppresses averaging of  replicate measurements, default is to average
        -sav: saves all plots without review
    SCREEN OUTPUT:
        Specimen, N, a95, StepMin, StepMax, Dec, Inc, calculation type

    """
    # initialize some variables
    doave,e,b=1,0,0 # average replicates, initial end and beginning step
    plots,coord=0,'s'
    noorient=0
    version_num=pmag.get_version()
    verbose=pmagplotlib.verbose
    beg_pca,end_pca,direction_type="","",'l'
    calculation_type,fmt="","svg"
    user,spec_keys,locname="",[],''
    plot_file=""
    sfile=""
    plot_file=""
    PriorRecs=[] # empty list for prior interpretations
    backup=0
    specimen="" # can skip everything and just plot one specimen with bounds e,b
    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]
    else:
        dir_path='.'
    inspec=dir_path+'/'+'zeq_specimens.txt'
    meas_file,geo,tilt,ask,samp_file=dir_path+'/magic_measurements.txt',0,0,0,dir_path+'/er_samples.txt'
    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')
        inspec=dir_path+'/'+sys.argv[ind+1]
    if '-fsa' in sys.argv:
        ind=sys.argv.index('-fsa')
        samp_file=dir_path+'/'+sys.argv[ind+1]
        sfile='ok'
    if '-crd' in sys.argv:
        ind=sys.argv.index('-crd')
        coord=sys.argv[ind+1]
        if coord=='g' or coord=='t':
            samp_data,file_type=pmag.magic_read(samp_file)
            if file_type=='er_samples':sfile='ok'
            geo=1
            if coord=='t':tilt=1
    if '-spc' in sys.argv:
        ind=sys.argv.index('-spc')
        specimen=sys.argv[ind+1]
        if '-dir' in sys.argv:
            ind=sys.argv.index('-dir')
            direction_type=sys.argv[ind+1]
            beg_pca=int(sys.argv[ind+2])
            end_pca=int(sys.argv[ind+3])
            if direction_type=='L':calculation_type='DE-BFL'
            if direction_type=='P':calculation_type='DE-BFP'
            if direction_type=='F':calculation_type='DE-FM'
        if '-Fp' in sys.argv: 
            ind=sys.argv.index('-Fp')
            plot_file=dir_path+'/'+sys.argv[ind+1]
    if '-A' in sys.argv: doave=0
    if '-sav' in sys.argv: 
        plots=1
        verbose=0
    if '-fmt' in sys.argv:
        ind=sys.argv.index('-fmt')
        fmt=sys.argv[ind+1]
    #
    first_save=1
    meas_data,file_type=pmag.magic_read(meas_file)
    changeM,changeS=0,0 # check if data or interpretations have changed
    if file_type != 'magic_measurements':
        print file_type
        print file_type,"This is not a valid magic_measurements file " 
        sys.exit()
    for rec in  meas_data:
        if  "magic_method_codes" not in rec.keys(): rec["magic_method_codes"]=""
        methods=""
        tmp=rec["magic_method_codes"].replace(" ","").split(":")
        for meth in tmp:
            methods=methods+meth+":"
        rec["magic_method_codes"]=methods[:-1]  # get rid of annoying spaces in Anthony's export files 
        if "magic_instrument_codes" not in rec.keys() :rec["magic_instrument_codes"]=""
    PriorSpecs=[]
    PriorRecs,file_type=pmag.magic_read(inspec)
    if len(PriorRecs)==0: 
        if verbose:print "starting new file ",inspec
    for Rec in PriorRecs:
        if 'magic_software_packages' not in Rec.keys():Rec['magic_software_packages']=""
        if Rec['er_specimen_name'] not in PriorSpecs:
            if 'specimen_comp_name' not in Rec.keys():Rec['specimen_comp_name']="A"
            PriorSpecs.append(Rec['er_specimen_name'])
        else:
            if 'specimen_comp_name' not in Rec.keys():Rec['specimen_comp_name']="A"
        if "magic_method_codes" in Rec.keys():
            methods=[]
            tmp=Rec["magic_method_codes"].replace(" ","").split(":")
            for meth in tmp:
                methods.append(meth)
            if 'DE-FM' in methods:
                Rec['calculation_type']='DE-FM' # this won't be imported but helps
            if 'DE-BFL' in methods:
                Rec['calculation_type']='DE-BFL'
            if 'DE-BFL-A' in methods:
                Rec['calculation_type']='DE-BFL-A'
            if 'DE-BFL-O' in methods:
                Rec['calculation_type']='DE-BFL-O'
            if 'DE-BFP' in methods:
                Rec['calculation_type']='DE-BFP'
        else:
            Rec['calculation_type']='DE-BFL' # default is to assume a best-fit line
    #
    # get list of unique specimen names
    #
    sids=pmag.get_specs(meas_data)
    #
    #  set up plots, angle sets X axis to horizontal,  direction_type 'l' is best-fit line
    # direction_type='p' is great circle
    #     
    #
    # draw plots for sample s - default is just to step through zijderveld diagrams
    #
    #
    # define figure numbers for equal area, zijderveld,  
    #  and intensity vs. demagnetiztion step respectively
    ZED={}
    ZED['eqarea'],ZED['zijd'],  ZED['demag']=1,2,3 
    pmagplotlib.plot_init(ZED['eqarea'],5,5)
    pmagplotlib.plot_init(ZED['zijd'],6,5)
    pmagplotlib.plot_init(ZED['demag'],5,5)
    save_pca=0
    if specimen=="":
        k = 0
    else:
        k=sids.index(specimen)
    angle,direction_type="",""
    setangle=0
    CurrRecs=[]
    while k < len(sids):
        CurrRecs=[]
        if setangle==0:angle=""
        method_codes,inst_code=[],""
        s=sids[k]
        PmagSpecRec={}
        PmagSpecRec["er_analyst_mail_names"]=user
        PmagSpecRec['magic_software_packages']=version_num
        PmagSpecRec['specimen_description']=""
        PmagSpecRec['magic_method_codes']=""
        if verbose and  s!="":print s, k , 'out of ',len(sids)
    #
    #  collect info for the PmagSpecRec dictionary
    #
        s_meas=pmag.get_dictitem(meas_data,'er_specimen_name',s,'T') # fish out this specimen
        s_meas=pmag.get_dictitem(s_meas,'magic_method_codes','Z','has') # fish out zero field steps
        if len(s_meas)>0:
          for rec in  s_meas: # fix up a few things for the output record
               PmagSpecRec["magic_instrument_codes"]=rec["magic_instrument_codes"]  # copy over instruments
               PmagSpecRec["er_citation_names"]="This study"
               PmagSpecRec["er_specimen_name"]=s
               PmagSpecRec["er_sample_name"]=rec["er_sample_name"]
               PmagSpecRec["er_site_name"]=rec["er_site_name"]
               PmagSpecRec["er_location_name"]=rec["er_location_name"]
               locname=rec['er_location_name']
               if 'er_expedition_name' in rec.keys(): PmagSpecRec["er_expedition_name"]=rec["er_expedition_name"]
               PmagSpecRec["magic_method_codes"]=rec["magic_method_codes"]
               if "magic_experiment_name" not in rec.keys():
                   PmagSpecRec["magic_experiment_names"]=""
               else:    
                   PmagSpecRec["magic_experiment_names"]=rec["magic_experiment_name"]
               break
    #
    # find the data from the meas_data file for this specimen
    #
          data,units=pmag.find_dmag_rec(s,meas_data)
          PmagSpecRec["measurement_step_unit"]= units
          u=units.split(":")
          if "T" in units:PmagSpecRec["magic_method_codes"]=PmagSpecRec["magic_method_codes"]+":LP-DIR-AF"
          if "K" in units:PmagSpecRec["magic_method_codes"]=PmagSpecRec["magic_method_codes"]+":LP-DIR-T"
          if "J" in units:PmagSpecRec["magic_method_codes"]=PmagSpecRec["magic_method_codes"]+":LP-DIR-M"
    #
    # find prior interpretation
    #
          if len(CurrRecs)==0: # check if already in
            beg_pca,end_pca="",""
            calculation_type=""
            if inspec !="":
              if verbose: print "    looking up previous interpretations..."
              precs=pmag.get_dictitem(PriorRecs,'er_specimen_name',s,'T') # get all the prior recs with this specimen name
              precs=pmag.get_dictitem(precs,'magic_method_codes','LP-DIR','has') # get the directional data
              PriorRecs=pmag.get_dictitem(PriorRecs,'er_specimen_name',s,'F') # take them all out of prior recs
         # get the ones that meet the current coordinate system
              for prec in precs:
                if 'specimen_tilt_correction' not in prec.keys() or prec['specimen_tilt_correction']=='-1':
                    crd='s'
                elif prec['specimen_tilt_correction']=='0':
                    crd='g'
                elif prec['specimen_tilt_correction']=='100':
                    crd='t'
                else:
                    crd='?'
                CurrRec={}
                for key in prec.keys():CurrRec[key]=prec[key]
                CurrRecs.append(CurrRec) # put in CurrRecs
                method_codes= CurrRec["magic_method_codes"].replace(" ","").split(':')
                calculation_type='DE-BFL'
                if 'DE-FM' in method_codes: calculation_type='DE-FM'
                if 'DE-BFP' in method_codes: calculation_type='DE-BFP'
                if 'DE-BFL-A' in method_codes: calculation_type='DE-BFL-A'
                if 'specimen_dang' not in CurrRec.keys():
                    if verbose:print 'Run mk_redo.py and zeq_magic_redo.py to get the specimen_dang values'
                    CurrRec['specimen_dang']=-1
                if calculation_type!='DE-FM' and crd==coord: # not a fisher mean
                    if verbose:print "Specimen  N    MAD    DANG  start     end      dec     inc  type  component coordinates"
                    if units=='K':
                            if verbose:print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f  %s  %s       %s \n' % (CurrRec["er_specimen_name"],int(CurrRec["specimen_n"]),float(CurrRec["specimen_mad"]),float(CurrRec["specimen_dang"]),float(CurrRec["measurement_step_min"])-273,float(CurrRec["measurement_step_max"])-273,float(CurrRec["specimen_dec"]),float(CurrRec["specimen_inc"]),calculation_type,CurrRec['specimen_comp_name'],crd)
                    elif units=='T':
                       if verbose:print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f  %s  %s %s \n' % (CurrRec["er_specimen_name"],int(CurrRec["specimen_n"]),float(CurrRec["specimen_mad"]),float(CurrRec["specimen_dang"]),float(CurrRec["measurement_step_min"])*1e3,float(CurrRec["measurement_step_max"])*1e3,float(CurrRec["specimen_dec"]),float(CurrRec["specimen_inc"]),calculation_type,CurrRec['specimen_comp_name'],crd)
                    elif 'T' in units and 'K' in units:
                            if float(CurrRec['measurement_step_min'])<1.0 :
                                min=float(CurrRec['measurement_step_min'])*1e3
                            else:
                                min=float(CurrRec['measurement_step_min'])-273
                            if float(CurrRec['measurement_step_max'])<1.0 :
                                max=float(CurrRec['measurement_step_max'])*1e3
                            else:
                                max=float(CurrRec['measurement_step_max'])-273
                            if verbose:print '%s %i %7.1f %i %i %7.1f %7.1f %7.1f, %s        %s\n' % (CurrRec["er_specimen_name"],int(CurrRec["specimen_n"]),float(CurrRec["specimen_mad"]),float(CurrRec['specimen_dang']),min,max,float(CurrRec["specimen_dec"]),float(CurrRec["specimen_inc"]),calculation_type,crd)
                    elif 'J' in units:
                       if verbose:print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f  %s  %s       %s \n' % (CurrRec["er_specimen_name"],int(CurrRec["specimen_n"]),float(CurrRec["specimen_mad"]),float(CurrRec['specimen_dang']),float(CurrRec["measurement_step_min"]),float(CurrRec["measurement_step_max"]),float(CurrRec["specimen_dec"]),float(CurrRec["specimen_inc"]),calculation_type,CurrRec['specimen_comp_name'],crd)
                elif calculation_type=='DE-FM' and crd==coord: # fisher mean
                    if verbose:print "Specimen  a95 DANG   start     end      dec     inc  type  component coordinates"
                    if units=='K':
                         if verbose:print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f  %s  %s       %s \n' % (CurrRec["er_specimen_name"],int(CurrRec["specimen_n"]),float(CurrRec["specimen_alpha95"]),float(CurrRec["measurement_step_min"])-273,float(CurrRec["measurement_step_max"])-273,float(CurrRec["specimen_dec"]),float(CurrRec["specimen_inc"]),calculation_type,CurrRec['specimen_comp_name'],crd)
                    elif units=='T':
                          if verbose:print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f  %s  %s       %s \n' % (CurrRec["er_specimen_name"],int(CurrRec["specimen_n"]),float(CurrRec["specimen_alpha95"]),float(CurrRec["measurement_step_min"])*1e3,float(CurrRec["measurement_step_max"])*1e3,float(CurrRec["specimen_dec"]),float(CurrRec["specimen_inc"]),calculation_type,CurrRec['specimen_comp_name'],crd)
                    elif 'T' in units and 'K' in units:
                            if float(CurrRec['measurement_step_min'])<1.0 :
                                min=float(CurrRec['measurement_step_min'])*1e3
                            else:
                                min=float(CurrRec['measurement_step_min'])-273
                            if float(CurrRec['measurement_step_max'])<1.0 :
                                max=float(CurrRec['measurement_step_max'])*1e3
                            else:
                                max=float(CurrRec['measurement_step_max'])-273
                            if verbose:print '%s %i %7.1f %i %i %7.1f %7.1f %s       %s \n' % (CurrRec["er_specimen_name"],int(CurrRec["specimen_n"]),float(CurrRec["specimen_alpha95"]),min,max,float(CurrRec["specimen_dec"]),float(CurrRec["specimen_inc"]),calculation_type,crd)
                    elif 'J' in units:
                       if verbose:print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %s %s       %s \n' % (CurrRec["er_specimen_name"],int(CurrRec["specimen_n"]),float(CurrRec["specimen_mad"]),float(CurrRec["measurement_step_min"]),float(CurrRec["measurement_step_max"]),float(CurrRec["specimen_dec"]),float(CurrRec["specimen_inc"]),calculation_type,CurrRec['specimen_comp_name'],crd)
              if len(CurrRecs)==0:beg_pca,end_pca="",""
          datablock=data
          noskip=1
          if len(datablock) <3: 
            noskip=0
            if backup==0:
                k+=1
            else:
                k-=1
            if len(CurrRecs)>0:
                for rec in CurrRecs:
                    PriorRecs.append(rec)
            CurrRecs=[]
          else:
            backup=0 
          if noskip:
        #
        # find replicate measurements at given treatment step and average them
        #
#            step_meth,avedata=pmag.vspec(data)
#            if len(avedata) != len(datablock):
#                if doave==1: 
#                    method_codes.append("DE-VM")
#                    datablock=avedata
#        #
        # do geo or stratigraphic correction now
        #
            if geo==1:
        #
        # find top priority orientation method
                orient,az_type=pmag.get_orient(samp_data,PmagSpecRec["er_sample_name"])
                if az_type=='SO-NO':
                    if verbose: print "no orientation data for ",s 
                    orient["sample_azimuth"]=0
                    orient["sample_dip"]=0
                    noorient=1
                    method_codes.append("SO-NO")
                    orient["sample_azimuth"]=0
                    orient["sample_dip"]=0
                    orient["sample_bed_dip_azimuth"]=0
                    orient["sample_bed_dip"]=0
                    noorient=1
                    method_codes.append("SO-NO")
                else: 
                    noorient=0
        #
        #  if stratigraphic selected,  get stratigraphic correction
        #
                tiltblock,geoblock=[],[]
                for rec in datablock:
                    d_geo,i_geo=pmag.dogeo(rec[1],rec[2],float(orient["sample_azimuth"]),float(orient["sample_dip"]))
                    geoblock.append([rec[0],d_geo,i_geo,rec[3],rec[4],rec[5],rec[6]])
                    if tilt==1 and "sample_bed_dip" in orient.keys() and float(orient['sample_bed_dip'])!=0: 
                        d_tilt,i_tilt=pmag.dotilt(d_geo,i_geo,float(orient["sample_bed_dip_direction"]),float(orient["sample_bed_dip"]))
                        tiltblock.append([rec[0],d_tilt,i_tilt,rec[3],rec[4],rec[5],rec[6]])
                    if tilt==1: plotblock=tiltblock
                    if geo==1 and tilt==0:plotblock=geoblock
            if geo==0 and tilt==0: plotblock=datablock
    #
    # set the end pca point to last point  if not set
            if e==0 or e>len(plotblock)-1: e=len(plotblock)-1
            if angle=="": angle=plotblock[0][1] # rotate to NRM declination
            title=s+'_s'
            if geo==1 and tilt==0 and noorient!=1:title=s+'_g'
            if tilt==1 and noorient!=1:title=s+'_t'
            pmagplotlib.plotZED(ZED,plotblock,angle,title,units)
            if verbose:pmagplotlib.drawFIGS(ZED)
            if len(CurrRecs)!=0:
                for prec in CurrRecs:
                    if 'calculation_type' not in prec.keys():
                        calculation_type=''
                    else:
                        calculation_type=prec["calculation_type"]
                    direction_type=prec["specimen_direction_type"]
                    if calculation_type !="":
                        beg_pca,end_pca="",""
                        for j in range(len(datablock)):
                            if data[j][0]==float(prec["measurement_step_min"]):beg_pca=j
                            if data[j][0]==float(prec["measurement_step_max"]):end_pca=j
                        if beg_pca=="" or end_pca=="":  
                            if verbose:
                                print "something wrong with prior interpretation "
                            break
                    if calculation_type!="":
                        if beg_pca=="":beg_pca=0
                        if end_pca=="":end_pca=len(plotblock)-1
                        if geo==1 and tilt==0:
                            mpars=pmag.domean(geoblock,beg_pca,end_pca,calculation_type)
                            if mpars["specimen_direction_type"]!="Error":
                                pmagplotlib.plotDir(ZED,mpars,geoblock,angle)
                                if verbose:pmagplotlib.drawFIGS(ZED)
                        if geo==1 and tilt==1:
                            mpars=pmag.domean(tiltblock,beg_pca,end_pca,calculation_type)
                            if mpars["specimen_direction_type"]!="Error":
                                pmagplotlib.plotDir(ZED,mpars,tiltblock,angle)
                                if verbose:pmagplotlib.drawFIGS(ZED)
                        if geo==0 and tilt==0: 
                            mpars=pmag.domean(datablock,beg_pca,end_pca,calculation_type)
                        if mpars["specimen_direction_type"]!="Error":
                                pmagplotlib.plotDir(ZED,mpars,plotblock,angle)
                                if verbose:pmagplotlib.drawFIGS(ZED)
    #
    # print out data for this sample to screen
    #
            recnum=0
            for plotrec in plotblock:
                if units=='T' and verbose: print '%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' % (plotrec[5], recnum,plotrec[0]*1e3," mT",plotrec[3],plotrec[1],plotrec[2],plotrec[6])
                if units=="K" and verbose: print '%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' % (plotrec[5], recnum,plotrec[0]-273,' C',plotrec[3],plotrec[1],plotrec[2],plotrec[6])
                if units=="J" and verbose: print '%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' % (plotrec[5], recnum,plotrec[0],' J',plotrec[3],plotrec[1],plotrec[2],plotrec[6])
                if 'K' in units and 'T' in units:
                    if plotrec[0]>=1. and verbose: print '%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' % (plotrec[5], recnum,plotrec[0]-273,' C',plotrec[3],plotrec[1],plotrec[2],plotrec[6])
                    if plotrec[0]<1. and  verbose: print '%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' % (plotrec[5], recnum,plotrec[0]*1e3," mT",plotrec[3],plotrec[1],plotrec[2],plotrec[6])
                recnum += 1
            if specimen!="":
                if plot_file=="":
                    basename=locname+'_'+s
                else:
                    basename=plot_file
                files={}
                for key in ZED.keys():
                    files[key]=basename+'_'+key+'.'+fmt 
                pmagplotlib.saveP(ZED,files)
                sys.exit()
            else:  # interactive
              if plots==0:
                ans='b'
                k+=1
                changeS=0
                while ans != "":
                    if len(CurrRecs)==0:
                        print """
                g/b: indicates  good/bad measurement.  "bad" measurements excluded from calculation

                set s[a]ve plot, [b]ounds for pca and calculate, [p]revious, [s]pecimen, 
                 change [h]orizontal projection angle,   change [c]oordinate systems, 
                 [e]dit data,  [q]uit: 
                """
                    else:
                        print """
                g/b: indicates  good/bad measurement.  "bad" measurements excluded from calculation

                 set s[a]ve plot, [b]ounds for pca and calculate, [p]revious, [s]pecimen, 
                 change [h]orizontal projection angle,   change [c]oordinate systems, 
                 [d]elete current interpretation(s), [e]dit data,   [q]uit: 
                """
                    ans=raw_input('<Return>  for  next specimen \n')
                    setangle=0
                    if ans=='d': # delete this interpretation
                        CurrRecs=[]
                        k-=1 # replot same specimen
                        ans=""
                        changeS=1
                    if  ans=='q': 
                        if changeM==1:
                            ans=raw_input('Save changes to magic_measurements.txt? y/[n] ')
                            if ans=='y':
                                pmag.magic_write(meas_file,meas_data,'magic_measurements')
                        print "Good bye"
                        sys.exit()
                    if  ans=='a':
                        if plot_file=="":
                            basename=locname+'_'+s+'_'
                        else:
                            basename=plot_file
                        files={}
                        for key in ZED.keys():
                            files[key]=basename+'_'+coord+'_'+key+'.'+fmt 
                        pmagplotlib.saveP(ZED,files)
                        ans=""
                    if  ans=='p':
                        k-=2
                        ans=""
                        backup=1
                    if ans=='c':
                        k-=1 # replot same block
                        if tilt==0 and geo ==1:print "You  are currently viewing geographic  coordinates "
                        if tilt==1 and geo ==1:print "You  are currently viewing stratigraphic  coordinates "
                        if tilt==0 and geo ==0: print "You  are currently viewing sample coordinates "
                        print "\n Which coordinate system do you wish to view? "
                        coord=raw_input(" <Return>  specimen, [g] geographic, [t] tilt corrected ")
                        if coord=="g":geo,tilt=1,0
                        if coord=="t":
                            geo=1
                            tilt=1
                        if coord=="":
                            coord='s'
                            geo=0
                            tilt=0
                        if geo==1 and sfile=="":
                            samp_file=raw_input(" Input er_samples file for sample orientations [er_samples.txt] " )
                            if samp_file=="":samp_file="er_samples.txt"
                            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 - coordinate system not changed" 
                            else:
                               sfile="ok"
                        ans=""
                    if ans=='s':
                        keepon=1
                        sample=raw_input('Enter desired specimen name (or first part there of): ')
                        while keepon==1:
                            try:
                                k =sids.index(sample)
                                keepon=0
                            except:
                                tmplist=[]
                                for qq in range(len(sids)):
                                    if sample in sids[qq]:tmplist.append(sids[qq])
                                print sample," not found, but this was: "
                                print tmplist
                                sample=raw_input('Select one or try again\n ')
                        angle,direction_type="",""
                        setangle=0
                        ans=""
                    if ans=='h':
                        k-=1
                        angle=raw_input("Enter desired  declination for X axis 0-360 ")
                        angle=float(angle)
                        if angle==0:angle=0.001
                        s=sids[k]
                        setangle=1
                        ans=""
                    if  ans=='e':
                        k-=1
                        ans=""
                        recnum=0
                        for plotrec in plotblock:
                            if plotrec[0]<=200 and verbose: print '%s: %i  %7.1f %s  %8.3e %7.1f %7.1f ' % (plotrec[5], recnum,plotrec[0]*1e3," mT",plotrec[3],plotrec[1],plotrec[2])
                            if plotrec[0]>200 and verbose: print '%s: %i  %7.1f %s  %8.3e %7.1f %7.1f ' % (plotrec[5], recnum,plotrec[0]-273,' C',plotrec[3],plotrec[1],plotrec[2])
                            recnum += 1
                        answer=raw_input('Enter index of point to change from bad to good or vice versa:  ')
                        try: 
                                ind=int(answer)
                                meas_data=pmag.mark_dmag_rec(s,ind,meas_data)
                                changeM=1
                        except:
                                'bad entry, try again'
                    if  ans=='b':
                        if end_pca=="":end_pca=len(plotblock)-1
                        if beg_pca=="":beg_pca=0
                        k-=1   # stay on same sample until through
                        GoOn=0
                        while GoOn==0:
                            print 'Enter index of first point for pca: ','[',beg_pca,']'
                            answer=raw_input('return to keep default  ')
                            if answer != "":
                                beg_pca=int(answer)
                            print 'Enter index  of last point for pca: ','[',end_pca,']'
                            answer=raw_input('return to keep default  ')
                            try:
                                end_pca=int(answer) 
                                if plotblock[beg_pca][5]=='b' or plotblock[end_pca][5]=='b': 
                                    print "Can't select 'bad' measurement for PCA bounds -try again"
                                    end_pca=len(plotblock)-1
                                    beg_pca=0
                                elif beg_pca >=0 and beg_pca<=len(plotblock)-2 and end_pca>0 and end_pca<len(plotblock): 
                                    GoOn=1
                                else:
                                    print beg_pca,end_pca, " are bad entry of indices - try again"
                                    end_pca=len(plotblock)-1
                                    beg_pca=0
                            except:
                                print beg_pca,end_pca, " are bad entry of indices - try again"
                                end_pca=len(plotblock)-1
                                beg_pca=0
                        GoOn=0
                        while GoOn==0:
                            if calculation_type!="":
                                print "Prior calculation type = ",calculation_type
                            ct=raw_input('Enter new Calculation Type: best-fit line,  plane or fisher mean [l]/p/f :  ' )
                            if ct=="" or ct=="l": 
                                direction_type="l"
                                calculation_type="DE-BFL"
                                GoOn=1
                            elif ct=='p':
                                direction_type="p"
                                calculation_type="DE-BFP"
                                GoOn=1
                            elif ct=='f':
                                direction_type="l"
                                calculation_type="DE-FM"
                                GoOn=1
                            else: 
                                print "bad entry of calculation type: try again. "
                        pmagplotlib.plotZED(ZED,plotblock,angle,s,units)
                        if verbose:pmagplotlib.drawFIGS(ZED)
                        if geo==1 and tilt==0:
                            mpars=pmag.domean(geoblock,beg_pca,end_pca,calculation_type)
                            if mpars['specimen_direction_type']=='Error':break
                            PmagSpecRec["specimen_dec"]='%7.1f ' %(mpars["specimen_dec"])
                            PmagSpecRec["specimen_inc"]='%7.1f ' %(mpars["specimen_inc"])
                            if "SO-NO" not in method_codes:
                                PmagSpecRec["specimen_tilt_correction"]='0'
                                method_codes.append("DA-DIR-GEO")
                            else:
                                PmagSpecRec["specimen_tilt_correction"]='-1'
                            pmagplotlib.plotDir(ZED,mpars,geoblock,angle)
                            if verbose:pmagplotlib.drawFIGS(ZED)
                        if geo==1 and  tilt==1:
                            mpars=pmag.domean(tiltblock,beg_pca,end_pca,calculation_type)
                            if mpars['specimen_direction_type']=='Error':break
                            PmagSpecRec["specimen_dec"]='%7.1f ' %(mpars["specimen_dec"])
                            PmagSpecRec["specimen_inc"]='%7.1f ' %(mpars["specimen_inc"])
                            if "SO-NO" not in method_codes:
                                PmagSpecRec["specimen_tilt_correction"]='100'
                                method_codes.append("DA-DIR-TILT")
                            else:
                                PmagSpecRec["specimen_tilt_correction"]='-1'
                            pmagplotlib.plotDir(ZED,mpars,tiltblock,angle)
                            if verbose:pmagplotlib.drawFIGS(ZED)
                        if geo==0 and tilt==0: 
                            mpars=pmag.domean(datablock,beg_pca,end_pca,calculation_type)
                            if mpars['specimen_direction_type']=='Error':break
                            PmagSpecRec["specimen_dec"]='%7.1f ' %(mpars["specimen_dec"])
                            PmagSpecRec["specimen_inc"]='%7.1f ' %(mpars["specimen_inc"])
                            PmagSpecRec["specimen_tilt_correction"]='-1'
                            pmagplotlib.plotDir(ZED,mpars,plotblock,angle)
                            if verbose:pmagplotlib.drawFIGS(ZED)
                        PmagSpecRec["measurement_step_min"]='%8.3e ' %(mpars["measurement_step_min"])
                        PmagSpecRec["measurement_step_max"]='%8.3e ' %(mpars["measurement_step_max"])
                        PmagSpecRec["specimen_correction"]='u'
                        PmagSpecRec["specimen_dang"]='%7.1f ' %(mpars['specimen_dang'])
                        print 'DANG: ',PmagSpecRec["specimen_dang"]
                        if calculation_type!='DE-FM':
                            PmagSpecRec["specimen_mad"]='%7.1f ' %(mpars["specimen_mad"])
                            PmagSpecRec["specimen_alpha95"]=""
                        else:
                            PmagSpecRec["specimen_alpha95"]='%7.1f ' %(mpars["specimen_alpha95"])
                            PmagSpecRec["specimen_mad"]=""
                        PmagSpecRec["specimen_n"]='%i ' %(mpars["specimen_n"])
                        PmagSpecRec["specimen_direction_type"]=direction_type
                        PmagSpecRec["calculation_type"]=calculation_type # redundant and won't be imported - just for convenience
                        method_codes=PmagSpecRec["magic_method_codes"].split(':')
                        if len(method_codes) != 0:
                            methstring=""
                            for meth in method_codes:
                                ctype=meth.split('-')
                                if 'DE' not in ctype:methstring=methstring+ ":" +meth # don't include old direction estimation methods
                        methstring=methstring+':'+calculation_type
                        PmagSpecRec["magic_method_codes"]= methstring.strip(':')
                        print 'Method codes: ',PmagSpecRec['magic_method_codes']
                        if calculation_type!='DE-FM':
                            if units=='K': 
                                print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f, %s \n' % (PmagSpecRec["er_specimen_name"],int(PmagSpecRec["specimen_n"]),float(PmagSpecRec["specimen_mad"]),float(PmagSpecRec["specimen_dang"]),float(PmagSpecRec["measurement_step_min"])-273,float(PmagSpecRec["measurement_step_max"])-273,float(PmagSpecRec["specimen_dec"]),float(PmagSpecRec["specimen_inc"]),calculation_type)
                            elif units== 'T':
                                print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f, %s \n' % (PmagSpecRec["er_specimen_name"],int(PmagSpecRec["specimen_n"]),float(PmagSpecRec["specimen_mad"]),float(PmagSpecRec["specimen_dang"]),float(PmagSpecRec["measurement_step_min"])*1e3,float(PmagSpecRec["measurement_step_max"])*1e3,float(PmagSpecRec["specimen_dec"]),float(PmagSpecRec["specimen_inc"]),calculation_type)
                            elif 'T' in units and 'K' in units:
                                if float(PmagSpecRec['measurement_step_min'])<1.0 :
                                    min=float(PmagSpecRec['measurement_step_min'])*1e3
                                else:
                                    min=float(PmagSpecRec['measurement_step_min'])-273
                                if float(PmagSpecRec['measurement_step_max'])<1.0 :
                                    max=float(PmagSpecRec['measurement_step_max'])*1e3
                                else:
                                    max=float(PmagSpecRec['measurement_step_max'])-273
                                print '%s %i %7.1f %i %i %7.1f %7.1f %7.1f, %s \n' % (PmagSpecRec["er_specimen_name"],int(PmagSpecRec["specimen_n"]),float(PmagSpecRec["specimen_mad"]),float(PmagSpecRec["specimen_dang"]),min,max,float(PmagSpecRec["specimen_dec"]),float(PmagSpecRec["specimen_inc"]),calculation_type)
                            else:
                                print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f, %s \n' % (PmagSpecRec["er_specimen_name"],int(PmagSpecRec["specimen_n"]),float(PmagSpecRec["specimen_mad"]),float(PmagSpecRec["specimen_dang"]),float(PmagSpecRec["measurement_step_min"]),float(PmagSpecRec["measurement_step_max"]),float(PmagSpecRec["specimen_dec"]),float(PmagSpecRec["specimen_inc"]),calculation_type)
                        else:
                            if 'K' in units:
                                print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f, %s \n' % (PmagSpecRec["er_specimen_name"],int(PmagSpecRec["specimen_n"]),float(PmagSpecRec["specimen_alpha95"]),float(PmagSpecRec["specimen_dang"]),float(PmagSpecRec["measurement_step_min"])-273,float(PmagSpecRec["measurement_step_max"])-273,float(PmagSpecRec["specimen_dec"]),float(PmagSpecRec["specimen_inc"]),calculation_type)
                            elif 'T' in units:
                                print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f, %s \n' % (PmagSpecRec["er_specimen_name"],int(PmagSpecRec["specimen_n"]),float(PmagSpecRec["specimen_alpha95"]),float(PmagSpecRec["specimen_dang"]),float(PmagSpecRec["measurement_step_min"])*1e3,float(PmagSpecRec["measurement_step_max"])*1e3,float(PmagSpecRec["specimen_dec"]),float(PmagSpecRec["specimen_inc"]),calculation_type)
                            elif 'T' in units and 'K' in units:
                                if float(PmagSpecRec['measurement_step_min'])<1.0 :
                                    min=float(PmagSpecRec['measurement_step_min'])*1e3
                                else:
                                    min=float(PmagSpecRec['measurement_step_min'])-273
                                if float(PmagSpecRec['measurement_step_max'])<1.0 :
                                    max=float(PmagSpecRec['measurement_step_max'])*1e3
                                else:
                                    max=float(PmagSpecRec['measurement_step_max'])-273
                                print '%s %i %7.1f %i %i %7.1f %7.1f, %s \n' % (PmagSpecRec["er_specimen_name"],int(PmagSpecRec["specimen_n"]),float(PmagSpecRec["specimen_alpha95"]),min,max,float(PmagSpecRec["specimen_dec"]),float(PmagSpecRec["specimen_inc"]),calculation_type)
                            else:
                                print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f, %s \n' % (PmagSpecRec["er_specimen_name"],int(PmagSpecRec["specimen_n"]),float(PmagSpecRec["specimen_alpha95"]),float(PmagSpecRec["measurement_step_min"]),float(PmagSpecRec["measurement_step_max"]),float(PmagSpecRec["specimen_dec"]),float(PmagSpecRec["specimen_inc"]),calculation_type)
                        saveit=raw_input("Save this interpretation? [y]/n \n")
                        if saveit!="n":
                            changeS=1
#
# put in details
#
                            angle,direction_type,setangle="","",0
                            if len(CurrRecs)>0:
                                replace=raw_input(" [0] add new component, or [1] replace existing interpretation(s) [default is replace] ")
                                if replace=="1" or replace=="":
                                    CurrRecs=[]
                                    PmagSpecRec['specimen_comp_name']='A'
                                    CurrRecs.append(PmagSpecRec)
                                else:
                                    print 'These are the current component names for this specimen: '
                                    for trec in CurrRecs:print trec['specimen_comp_name']
                                    compnum=raw_input("Enter new component name: ")
                                    PmagSpecRec['specimen_comp_name']=compnum
                                    print "Adding new component: ",PmagSpecRec['specimen_comp_name']
                                    CurrRecs.append(PmagSpecRec)
                            else:
                                PmagSpecRec['specimen_comp_name']='A'
                                CurrRecs.append(PmagSpecRec)
                            k+=1 
                            ans=""
                        else:
                            ans=""
              else:  # plots=1
                  k+=1
                  files={}
                  locname.replace('/','-')
                  print PmagSpecRec
                  for key in ZED.keys():
                      files[key]="LO:_"+locname+'_SI:_'+PmagSpecRec['er_site_name']+'_SA:_'+PmagSpecRec['er_sample_name']+'_SP:_'+s+'_CO:_'+coord+'_TY:_'+key+'_.'+fmt
                  if pmagplotlib.isServer:
                      black     = '#000000'
                      purple    = '#800080'
                      titles={}
                      titles['demag']='DeMag Plot'
                      titles['zijd']='Zijderveld Plot'
                      titles['eqarea']='Equal Area Plot'
                      ZED = pmagplotlib.addBorders(ZED,titles,black,purple)
                  pmagplotlib.saveP(ZED,files)
            if len(CurrRecs)>0:
                for rec in CurrRecs: PriorRecs.append(rec)
            if changeS==1:
                if len(PriorRecs)>0:
                    save_redo(PriorRecs,inspec)
                else:
                    os.system('rm '+inspec)
            CurrRecs,beg_pca,end_pca=[],"","" # next up
            changeS=0
        else: k+=1 # skip record - not enough data
    if changeM==1:
        pmag.magic_write(meas_file,meas_data,'magic_measurements')
예제 #10
0
def main():
    """
    NAME
        zeq_magic_redo.py
   
    DESCRIPTION
        Calculate principal components through demagnetization data using bounds and calculation type stored in "redo" file
  
    SYNTAX
        zeq_magic_redo.py [command line options]

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

    meas_data, file_type = pmag.magic_read(meas_file)
    if file_type != 'magic_measurements':
        print(file_type)
        print(file_type, "This is not a valid magic_measurements file ")
        sys.exit()
    #
    # sort the specimen names
    #
    k = 0
    print('Processing ', len(speclist), ' specimens - please wait')
    PmagSpecs = []
    while k < len(speclist):
        s = speclist[k]
        recnum = 0
        PmagSpecRec = {}
        method_codes, inst_codes = [], []
        # find the data from the meas_data file for this sample
        #
        #  collect info for the PmagSpecRec dictionary
        #
        meas_meth = []
        spec = pmag.get_dictitem(meas_data, 'er_specimen_name', s, 'T')
        if len(spec) == 0:
            print('no data found for specimen:  ', s)
            print('delete from zeq_redo input file...., then try again')
        else:
            for rec in spec:  # copy of vital stats to PmagSpecRec from first spec record in demag block
                skip = 1
                methods = rec["magic_method_codes"].split(":")
                if len(set(methods) & set(INCL)) > 0:
                    PmagSpecRec["er_analyst_mail_names"] = user
                    PmagSpecRec["magic_software_packages"] = version_num
                    PmagSpecRec["er_specimen_name"] = s
                    PmagSpecRec["er_sample_name"] = rec["er_sample_name"]
                    PmagSpecRec["er_site_name"] = rec["er_site_name"]
                    PmagSpecRec["er_location_name"] = rec["er_location_name"]
                    if "er_expedition_name" in list(rec.keys()):
                        PmagSpecRec["er_expedition_name"] = rec[
                            "er_expedition_name"]
                    PmagSpecRec["er_citation_names"] = "This study"
                    if "magic_experiment_name" not in list(rec.keys()):
                        rec["magic_experiment_name"] = ""
                    PmagSpecRec["magic_experiment_names"] = rec[
                        "magic_experiment_name"]
                    if "magic_instrument_codes" not in list(rec.keys()):
                        rec["magic_instrument_codes"] = ""
                    inst = rec['magic_instrument_codes'].split(":")
                    for I in inst:
                        if I not in inst_codes:  # copy over instruments
                            inst_codes.append(I)
                    meths = rec["magic_method_codes"].split(":")
                    for meth in meths:
                        if meth.strip() not in meas_meth:
                            meas_meth.append(meth)
                    if "LP-DIR-AF" in meas_meth or "LT-AF-Z" in meas_meth:
                        PmagSpecRec["measurement_step_unit"] = "T"
                        if "LP-DIR-AF" not in method_codes:
                            method_codes.append("LP-DIR-AF")
                    if "LP-DIR-T" in meas_meth or "LT-T-Z" in meas_meth:
                        PmagSpecRec["measurement_step_unit"] = "K"
                        if "LP-DIR-T" not in method_codes:
                            method_codes.append("LP-DIR-T")
                    if "LP-DIR-M" in meas_meth or "LT-M-Z" in meas_meth:
                        PmagSpecRec["measurement_step_unit"] = "J"
                        if "LP-DIR-M" not in method_codes:
                            method_codes.append("LP-DIR-M")
    #
    #
        datablock, units = pmag.find_dmag_rec(
            s, spec)  # fish out the demag data for this specimen
        #
        if len(datablock) < 2 or s not in speclist:
            k += 1


#            print 'skipping ', s,len(datablock)
        else:
            #
            # find replicate measurements at given treatment step and average them
            #
            #            step_meth,avedata=pmag.vspec(data)
            #
            #            if len(avedata) != len(datablock):
            #                if doave==1:
            #                    method_codes.append("DE-VM")
            #                    datablock=avedata
            #
            # do geo or stratigraphic correction now
            #
            if geo == 1 or tilt == 1:
                # find top priority orientation method
                orient, az_type = pmag.get_orient(
                    samp_data, PmagSpecRec["er_sample_name"])
                if az_type not in method_codes: method_codes.append(az_type)
                #
                #  if tilt selected,  get stratigraphic correction
                #
                tiltblock, geoblock = [], []
                for rec in datablock:
                    if "sample_azimuth" in list(
                            orient.keys()) and orient["sample_azimuth"] != "":
                        d_geo, i_geo = pmag.dogeo(
                            rec[1], rec[2], float(orient["sample_azimuth"]),
                            float(orient["sample_dip"]))
                        geoblock.append(
                            [rec[0], d_geo, i_geo, rec[3], rec[4], rec[5]])
                        if tilt == 1 and "sample_bed_dip_direction" in list(
                                orient.keys()):
                            d_tilt, i_tilt = pmag.dotilt(
                                d_geo, i_geo,
                                float(orient["sample_bed_dip_direction"]),
                                float(orient["sample_bed_dip"]))
                            tiltblock.append([
                                rec[0], d_tilt, i_tilt, rec[3], rec[4], rec[5]
                            ])
                        elif tilt == 1:
                            if PmagSpecRec["er_sample_name"] not in skipped:
                                print('no tilt correction for ',
                                      PmagSpecRec["er_sample_name"],
                                      ' skipping....')
                                skipped.append(PmagSpecRec["er_sample_name"])
                    else:
                        if PmagSpecRec["er_sample_name"] not in skipped:
                            print('no geographic correction for ',
                                  PmagSpecRec["er_sample_name"],
                                  ' skipping....')
                            skipped.append(PmagSpecRec["er_sample_name"])
    #
    #	get beg_pca, end_pca, pca
            if PmagSpecRec['er_sample_name'] not in skipped:
                compnum = -1
                for spec in mkspec:
                    if spec[0] == s:
                        CompRec = {}
                        for key in list(PmagSpecRec.keys()):
                            CompRec[key] = PmagSpecRec[key]
                        compnum += 1
                        calculation_type = spec[1]
                        beg = float(spec[2])
                        end = float(spec[3])
                        if len(spec) > 4:
                            comp_name = spec[4]
                        else:
                            comp_name = string.uppercase[compnum]
                        CompRec['specimen_comp_name'] = comp_name
                        if beg < float(datablock[0][0]):
                            beg = float(datablock[0][0])
                        if end > float(datablock[-1][0]):
                            end = float(datablock[-1][0])
                        for l in range(len(datablock)):
                            if datablock[l][0] == beg: beg_pca = l
                            if datablock[l][0] == end: end_pca = l
                        if geo == 1 and tilt == 0:
                            mpars = pmag.domean(geoblock, beg_pca, end_pca,
                                                calculation_type)
                            if mpars["specimen_direction_type"] != "Error":
                                CompRec["specimen_dec"] = '%7.1f ' % (
                                    mpars["specimen_dec"])
                                CompRec["specimen_inc"] = '%7.1f ' % (
                                    mpars["specimen_inc"])
                                CompRec["specimen_tilt_correction"] = '0'
                        if geo == 1 and tilt == 1:
                            mpars = pmag.domean(tiltblock, beg_pca, end_pca,
                                                calculation_type)
                            if mpars["specimen_direction_type"] != "Error":
                                CompRec["specimen_dec"] = '%7.1f ' % (
                                    mpars["specimen_dec"])
                                CompRec["specimen_inc"] = '%7.1f ' % (
                                    mpars["specimen_inc"])
                                CompRec["specimen_tilt_correction"] = '100'
                        if geo == 0 and tilt == 0:
                            mpars = pmag.domean(datablock, beg_pca, end_pca,
                                                calculation_type)
                            if mpars["specimen_direction_type"] != "Error":
                                CompRec["specimen_dec"] = '%7.1f ' % (
                                    mpars["specimen_dec"])
                                CompRec["specimen_inc"] = '%7.1f ' % (
                                    mpars["specimen_inc"])
                                CompRec["specimen_tilt_correction"] = '-1'
                        if mpars["specimen_direction_type"] == "Error":
                            pass
                        else:
                            CompRec["measurement_step_min"] = '%8.3e ' % (
                                datablock[beg_pca][0])
                            try:
                                CompRec["measurement_step_max"] = '%8.3e ' % (
                                    datablock[end_pca][0])
                            except:
                                print('error in end_pca ',
                                      PmagSpecRec['er_specimen_name'])
                            CompRec["specimen_correction"] = 'u'
                            if calculation_type != 'DE-FM':
                                CompRec["specimen_mad"] = '%7.1f ' % (
                                    mpars["specimen_mad"])
                                CompRec["specimen_alpha95"] = ""
                            else:
                                CompRec["specimen_mad"] = ""
                                CompRec["specimen_alpha95"] = '%7.1f ' % (
                                    mpars["specimen_alpha95"])
                            CompRec["specimen_n"] = '%i ' % (
                                mpars["specimen_n"])
                            CompRec["specimen_dang"] = '%7.1f ' % (
                                mpars["specimen_dang"])
                            CompMeths = []
                            for meth in method_codes:
                                if meth not in CompMeths:
                                    CompMeths.append(meth)
                            if calculation_type not in CompMeths:
                                CompMeths.append(calculation_type)
                            if geo == 1: CompMeths.append("DA-DIR-GEO")
                            if tilt == 1: CompMeths.append("DA-DIR-TILT")
                            if "DE-BFP" not in calculation_type:
                                CompRec["specimen_direction_type"] = 'l'
                            else:
                                CompRec["specimen_direction_type"] = 'p'
                            CompRec["magic_method_codes"] = ""
                            if len(CompMeths) != 0:
                                methstring = ""
                                for meth in CompMeths:
                                    methstring = methstring + ":" + meth
                                CompRec[
                                    "magic_method_codes"] = methstring.strip(
                                        ':')
                            CompRec["specimen_description"] = comment
                            if len(inst_codes) != 0:
                                inststring = ""
                                for inst in inst_codes:
                                    inststring = inststring + ":" + inst
                                CompRec[
                                    "magic_instrument_codes"] = inststring.strip(
                                        ':')
                            PmagSpecs.append(CompRec)
            k += 1
    pmag.magic_write(pmag_file, PmagSpecs, 'pmag_specimens')
    print("Recalculated specimen data stored in ", pmag_file)
예제 #11
0
def main():
    """
        NAME
            nrm_specimens_magic.py

        DESCRIPTION
            converts NRM data in a measurements type file to
            geographic and tilt corrected data in a 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 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: measurements.txt
            PFILE: nrm_specimens.txt
            SFILE: samples.txt
            coord: g
            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, "g"
    geo, orient = 1, 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 + "/measurements.txt"
    spec_file = dir_path + "/specimens.txt"
    samp_file = dir_path + "/samples.txt"
    out_file = dir_path + "/nrm_specimens.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")
        out_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 == "t":
            tilt, orient, geo = 1, 1, 1


#
# read in data
#
    meas_data = pd.read_csv(meas_file, header=1, sep='\t')
    meas_data = meas_data[meas_data['method_codes'].str.contains('LT-NO') ==
                          True]  # fish out NRM data
    meas_data = meas_data[['specimen', 'dir_dec', 'dir_inc']]
    meas_data = meas_data.dropna(subset=['dir_dec', 'dir_inc'])
    meas_data = pd.DataFrame(meas_data)
    #   import samples  for orientation info
    #
    ##
    if orient == 1:
        spec_data = pd.read_csv(spec_file, header=1, sep='\t')
        spec_data = spec_data[['specimen', 'sample']]
        meas_data = pd.merge(meas_data,
                             spec_data,
                             how='inner',
                             on=['specimen'])
        samp_data = pd.read_csv(samp_file, header=1, sep='\t')
    #
    sids = meas_data.specimen.unique()  # list of specimen names
    #
    #
    NrmSpecRecs = []
    for spec in sids:
        gdec, ginc, bdec, binc = "", "", "", ""
        this_spec_data = meas_data[meas_data.specimen.str.contains(spec)]
        this_sample = this_spec_data['sample'].iloc[-1]
        this_sample_data = samp_data[samp_data['sample'].str.contains(
            this_sample)]
        this_sample_data = this_sample_data.to_dict('records')
        for m in this_spec_data.to_dict('records'):
            NrmSpecRec = {'specimen': spec, 'sample': this_sample}
            if coord == 'g':
                NrmSpecRec['dir_tilt_correction'] = '0'
            elif coord == 't':
                NrmSpecRec['dir_tilt_correction'] = '100'
            else:
                NrmSpecRec['dir_tilt_correction'] = '-1'
            if not orient:
                NrmSpecRec['dir_dec'] = m['dir_dec']
                NrmSpecRec['dir_inc'] = m['dir_inc']
                NrmSpecRec['method_codes'] = 'SO-NO'
                NrmSpecRecs.append(NrmSpecRec)
            else:  # do geographic correction
                # get the azimuth
                or_info, az_type = pmag.get_orient(this_sample_data,
                                                   this_sample,
                                                   data_model=3)
                if 'azimuth' in or_info.keys() and or_info['azimuth'] != "":
                    azimuth = or_info['azimuth']
                    dip = or_info['dip']
                    gdec, ginc = pmag.dogeo(m['dir_dec'], m['dir_inc'],
                                            azimuth, dip)
                    if tilt:
                        # try tilt correction
                        if 'bed_dip' in or_info.keys(
                        ) and or_info['bed_dip'] != "":
                            bed_dip = or_info['bed_dip']
                            bed_dip_dir = or_info['bed_dip_direction']
                            bdec, binc = pmag.dogeo(gdec, ginc, bed_dip_dir,
                                                    bed_dip)
                            NrmSpecRec['dir_dec'] = bdec
                            NrmSpecRec['dir_inc'] = binc
                            NrmSpecRec['method_codes'] = az_type
                            NrmSpecRecs.append(NrmSpecRec)
                        else:
                            print('no bedding orientation data for ', spec)

                    else:
                        NrmSpecRec['dir_dec'] = gdec
                        NrmSpecRec['dir_inc'] = ginc
                        NrmSpecRec['method_codes'] = az_type
                        NrmSpecRecs.append(NrmSpecRec)
                else:
                    print('no geo orientation data for ', spec)

    pmag.magic_write(out_file, NrmSpecRecs, 'specimens')
예제 #12
0
def main():
    """
        NAME
            nrm_specimens_magic.py
            Note: this program has been deprecated and is not maintained

        DESCRIPTION
            converts NRM data in a measurements type file to
            geographic and tilt corrected data in a 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 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: measurements.txt
            PFILE: nrm_specimens.txt
            SFILE: samples.txt
            coord: g
            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, "g"
    geo, orient = 1, 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 + "/measurements.txt"
    spec_file = dir_path + "/specimens.txt"
    samp_file = dir_path + "/samples.txt"
    out_file = dir_path + "/nrm_specimens.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")
        out_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 == "t":
            tilt, orient, geo = 1, 1, 1
#
# read in data
    #
    meas_data = pd.read_csv(meas_file, header=1, sep='\t')
    meas_data = meas_data[meas_data['method_codes'].str.contains(
        'LT-NO') == True]  # fish out NRM data
    meas_data = meas_data[['specimen', 'dir_dec', 'dir_inc']]
    meas_data = meas_data.dropna(subset=['dir_dec', 'dir_inc'])
    meas_data = pd.DataFrame(meas_data)
#   import samples  for orientation info
#
##
    if orient == 1:
        spec_data = pd.read_csv(spec_file, header=1, sep='\t')
        spec_data = spec_data[['specimen', 'sample']]
        meas_data = pd.merge(meas_data, spec_data,
                             how='inner', on=['specimen'])
        samp_data = pd.read_csv(samp_file, header=1, sep='\t')
    #
    sids = meas_data.specimen.unique()  # list of specimen names
    #
    #
    NrmSpecRecs = []
    for spec in sids:
        gdec, ginc, bdec, binc = "", "", "", ""
        this_spec_data = meas_data[meas_data.specimen.str.contains(spec)]
        this_sample = this_spec_data['sample'].iloc[-1]
        this_sample_data = samp_data[samp_data['sample'].str.contains(
            this_sample)]
        this_sample_data = this_sample_data.to_dict('records')
        for m in this_spec_data.to_dict('records'):
            NrmSpecRec = {'specimen': spec, 'sample': this_sample}
            if coord == 'g':
                NrmSpecRec['dir_tilt_correction'] = '0'
            elif coord == 't':
                NrmSpecRec['dir_tilt_correction'] = '100'
            else:
                NrmSpecRec['dir_tilt_correction'] = '-1'
            if not orient:
                NrmSpecRec['dir_dec'] = m['dir_dec']
                NrmSpecRec['dir_inc'] = m['dir_inc']
                NrmSpecRec['method_codes'] = 'SO-NO'
                NrmSpecRecs.append(NrmSpecRec)
            else:  # do geographic correction
                # get the azimuth
                or_info, az_type = pmag.get_orient(
                    this_sample_data, this_sample, data_model=3)
                if 'azimuth' in or_info.keys() and or_info['azimuth'] != "":
                    azimuth = or_info['azimuth']
                    dip = or_info['dip']
                    gdec, ginc = pmag.dogeo(
                        m['dir_dec'], m['dir_inc'], azimuth, dip)
                    if tilt:
                        # try tilt correction
                        if 'bed_dip' in or_info.keys() and or_info['bed_dip'] != "":
                            bed_dip = or_info['bed_dip']
                            bed_dip_dir = or_info['bed_dip_direction']
                            bdec, binc = pmag.dogeo(
                                gdec, ginc, bed_dip_dir, bed_dip)
                            NrmSpecRec['dir_dec'] = bdec
                            NrmSpecRec['dir_inc'] = binc
                            NrmSpecRec['method_codes'] = az_type
                            NrmSpecRecs.append(NrmSpecRec)
                        else:
                            print('no bedding orientation data for ', spec)

                    else:
                        NrmSpecRec['dir_dec'] = gdec
                        NrmSpecRec['dir_inc'] = ginc
                        NrmSpecRec['method_codes'] = az_type
                        NrmSpecRecs.append(NrmSpecRec)
                else:
                    print('no geo orientation data for ', spec)

    pmag.magic_write(out_file, NrmSpecRecs, 'specimens')
예제 #13
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