コード例 #1
0
ファイル: generic_magic.py プロジェクト: setiabudidaya/PmagPy
def main():
    """
    NAME
        generic_magic.py
 
    DESCRIPTION
        converts magnetometer files in generic format to magic_measurements format

    SYNTAX
        generic_magic.py [command line options]
                      
    OPTIONS
        -h 
            prints the help message and quits.
        -usr USER
            identify user, default is ""
        -f FILE:
            specify path to input file, required
        -fsa SAMPFILE:
            specify er_samples.txt file for sample orientation data. default is er_samples.txt
        -F FILE
            specify output file, default is magic_measurements.txt
        
        ???-Fsy: specify er_synthetics file, default is er_sythetics.txt
        
        #-Fsa:
        #    specify output er_samples file, default is NONE 
       
        -exp EXPERIMENT-TYPE 
            Demag:
                AF and/or Thermal
            PI:
                paleointenisty thermal experiment (ZI/IZ/IZZI)
            ATRM n:
                ATRM in n positions
            AARM n:
                AARM in n positions
            CR:
                cooling rate experiment
                The treatment coding of the measurement file should be: XXX.00,XXX.10, XXX.20 ...XX.70 etc. (XXX.00 is optional)
                where XXX in the temperature and .10,.20... are running numbers of the cooling rates steps.
                XXX.00 is optional zerofield baseline. XXX.70 is alteration check.
                syntax in sio_magic is: -LP CR xxx,yyy,zzz,.....xx -A
                where xx, yyy,zzz...xxx  are cooling rates in [K/minutes], seperated by comma, ordered at the same order as XXX.10,XXX.20 ...XX.70
                
                No need to specify the cooling rate for the zerofield
                It is important to add to the command line the -A option so the measurements will not be evraged.
                But users need to make sure that there are no duplicate meaurements in the file
            
            NLT:
                non-linear-TRM experiment
                
        -samp X Y
            specimen-sample naming convention.
            X=0 Y=n: specimen is distiguished from sample by n initial characters.
                     (example: if n=4 then and specimen = mgf13a then sample = mgf13)
            X=1 Y=n: specimen is distiguished from sample by n terminate characters.
                     (example: if n=1 then and specimen = mgf13a then sample = mgf13)
            X=2 Y=c: specimen is distiguishing from sample by a delimiter.
                     (example: if c=- then annd specimen = mgf13-a then sample = mgf13)

        -site X Y
            sample-site naming convention.
            X=0 Y=n: sample is distiguished from site by n initial characters.
                     (example: if n=3 then and sample = mgf13 then sample = mgf)
            X=1 Y=n: sample is distiguished from site by n terminate characters.
                     (example: if n=2 and sample = mgf13 then site = mgf)
            X=2 Y=c: specimen is distiguishing from sample by a delimiter.
                     (example: if c='-' and sample = 'mgf-13' then site = mgf)
        
        -loc LOCNAM 
            specify location/study name.
        
        -dc B PHI THETA:
            B: dc lab field (in micro tesla)
            PHI (declination). takes numbers from 0 to 360
            THETA (inclination). takes numbers from -90 to 90
              
            NB: use PHI, THETA = -1 -1 to signal that it changes, i.e. in anisotropy experiment.
              
        -A: don't average replicate measurements. Take the last measurement from replicate measurements.
        
        -WD working directory

    INPUT
    
            A generic file is a tab-delimited file. Each columns should have a header.
            The file must include the follwing headers. The order of the columns is not important.
            specimen:
                string specifying specimen name
            treatment:
                a number with one or two decimal point (X.Y)
                coding for thermal demagnetization: 
                    0.0 or 0 is NRM.
                    X is temperature in celcsius
                    Y is always 0
                coding for AF demagnetization:
                    0.0 or 0 is NRM.
                    X is AF peak field in mT
                    Y is always 0
                coding for Thellier-type experiment:
                    0.0 or 0 is NRM
                    X is temperature in celcsius
                    Y=0: zerofield              
                    Y=1: infield              
                    Y=2: pTRM check              
                    Y=3: pTRM tail check              
                    Y=4: Additivity check 
                    # Ron, Add also 5 for Thellier protocol            
        treatment_type:
            N: NRM
            A: AF
            T: Thermal
        moment:
            magnetic moment in emu !!           
        
        In addition. at least one of the following headers are requiered:   
        dec_s:
            declination in specimen coordinate system (0 to 360)  
        inc_s:
            inclination in specimen coordinate system (-90 to 90)                
        
        Testing:
            1) make a genetric file with AF
            2) make a genetric file with Thermal
            3) make a genetric file with Thermal + AF 
            4) make a genetric file with IZZI 
            5) check duplicates option
                                        
    """

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


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

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

    def average_duplicates(duplicates):
        '''
        avarage replicate measurements.
        '''
        carts_s,carts_g,carts_t=[],[],[]   
        for rec in duplicates:
            moment=float(rec['moment'])
            if 'dec_s' in rec.keys() and 'inc_s' in rec.keys():
                if rec['dec_s']!="" and rec['inc_s']!="":
                    dec_s=float(rec['dec_s'])
                    inc_s=float(rec['inc_s'])
                    cart_s=pmag.dir2cart([dec_s,inc_s,moment])
                    carts_s.append(cart_s)
            if 'dec_g' in rec.keys() and 'inc_g' in rec.keys():
                if rec['dec_g']!="" and rec['inc_g']!="":
                    dec_g=float(rec['dec_g'])
                    inc_g=float(rec['inc_g'])
                    cart_g=pmag.dir2cart([dec_g,inc_g,moment])
                    carts_g.append(cart_g)
            if 'dec_t' in rec.keys() and 'inc_t' in rec.keys():
                if rec['dec_t']!="" and rec['inc_t']!="":
                    dec_t=float(rec['dec_t'])
                    inc_t=float(rec['inc_t'])
                    cart_t=pmag.dir2cart([dec_t,inc_t,moment])
                    carts_t.append(cart_t)
        if len(carts_s)>0:                               
            carts=scipy.array(carts_s)
            x_mean=scipy.mean(carts[:,0])
            y_mean=scipy.mean(carts[:,1])
            z_mean=scipy.mean(carts[:,2])
            mean_dir=pmag.cart2dir([x_mean,y_mean,z_mean])
            mean_dec_s="%.2f"%mean_dir[0]
            mean_inc_s="%.2f"%mean_dir[1]
            mean_moment="%10.3e"%mean_dir[2]
        else:
            mean_dec_s,mean_inc_s="",""
        if len(carts_g)>0:                               
            carts=scipy.array(carts_g)
            x_mean=scipy.mean(carts[:,0])
            y_mean=scipy.mean(carts[:,1])
            z_mean=scipy.mean(carts[:,2])
            mean_dir=pmag.cart2dir([x_mean,y_mean,z_mean])
            mean_dec_g="%.2f"%mean_dir[0]
            mean_inc_g="%.2f"%mean_dir[1]
            mean_moment="%10.3e"%mean_dir[2]
        else:
            mean_dec_g,mean_inc_g="",""
            
        if len(carts_t)>0:                               
            carts=scipy.array(carts_t)
            x_mean=scipy.mean(carts[:,0])
            y_mean=scipy.mean(carts[:,1])
            z_mean=scipy.mean(carts[:,2])
            mean_dir=pmag.cart2dir([x_mean,y_mean,z_mean])
            mean_dec_t="%.2f"%mean_dir[0]
            mean_inc_t="%.2f"%mean_dir[1]
            mean_moment="%10.3e"%mean_dir[2]
        else:
            mean_dec_t,mean_inc_t="",""
                                                                        
        meanrec={}
        for key in duplicates[0].keys():
            if key in ['dec_s','inc_s','dec_g','inc_g','dec_t','inc_t','moment']:
                continue
            else:
                meanrec[key]=duplicates[0][key]
        meanrec['dec_s']=mean_dec_s
        meanrec['dec_g']=mean_dec_g
        meanrec['dec_t']=mean_dec_t
        meanrec['inc_s']=mean_inc_s
        meanrec['inc_g']=mean_inc_g
        meanrec['inc_t']=mean_inc_t
        meanrec['moment']=mean_moment
        return meanrec
                    
    def get_upper_level_name(name,nc):
        '''
        get sample/site name from specimen/sample using naming convention
        '''
        if float(nc[0])==0:
            if float(nc[1])!=0:
                number_of_char=int(nc[1])
                high_name=name[:number_of_char]
            else:
                high_name=name
        elif float(nc[0])==1:
            if float(nc[1])!=0:
                number_of_char=int(nc[1])*-1
                high_name=name[:number_of_char]
            else:
                high_name=name
        elif float(nc[0])==2:
            d=str(nc[1])
            name_splitted=name.split(d)
            if len(name_splitted)==1:
                high_name=name_splitted[0]
            else:
                high_name=d.join(name_splitted[:-1])
        else:
            high_name=name
        return high_name
                            
    def merge_pmag_recs(old_recs):
        recs={}
        recs=copy.deepcopy(old_recs)
        headers=[]
        for rec in recs:
            for key in rec.keys():
                if key not in headers:
                    headers.append(key)
        for rec in recs:
            for header in headers:
                if header not in rec.keys():
                    rec[header]=""
        return recs
    
    #--------------------------------------
    # get command line arguments
    #--------------------------------------

    args=sys.argv
    user=""
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-usr" in args:
        ind=args.index("-usr")
        user=args[ind+1]
    else:
        user=""
    if '-F' in args:
        ind=args.index("-F")
        meas_file=args[ind+1]
    if '-Fsa' in args:
        ind=args.index("-Fsa")
        samp_file=args[ind+1]
    else:
        samp_file="er_samples.txt"

    if '-f' in args:
        ind=args.index("-f")
        magfile=args[ind+1]
        try:
            input=open(magfile,'rU')
        except:
            print "bad mag file:",magfile
            sys.exit()
    else: 
        print "mag_file field is required option"
        print main.__doc__
        sys.exit()
    
    if "-dc" in args:
        ind=args.index("-dc")
        labfield=float(args[ind+1])*1e-6
        labfield_phi=float(args[ind+2])
        labfield_theta=float(args[ind+3])
    if '-exp' in args:
        ind=args.index("-exp")
        experiment=args[ind+1]        
    else: 
        print "-LP is required option"
        print main.__doc__
        sys.exit()

    if experiment=='ATRM':
        ind=args.index("ATRM")
        atrm_n_pos=int(args[ind+1])    
            
    if experiment=='AARM':
        ind=args.index("AARM")
        aarm_n_pos=int(args[ind+1])        

    if  experiment=='CR':
        ind=args.index("CR")
        coolling_times=args[ind+1]
        coolling_times_list=coolling_times.split(',')
              
    if "-samp" in args:
        ind=args.index("-samp")
        sample_nc=[]
        sample_nc.append(args[ind+1])
        sample_nc.append(args[ind+2])

    if "-site" in args:
        ind=args.index("-site")
        site_nc=[]
        site_nc.append(args[ind+1])
        site_nc.append(args[ind+2])
    if "-loc" in args:
        ind=args.index("-loc")
        er_location_name=args[ind+1]
    else:
        er_location_name=""
    if "-A" in args:
        noave=1 
    else:
        noave=0             

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

        


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

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

    er_sample_data={}
    #er_sample_data=sort_magic_file(samp_file,1,'er_sample_name')
    try:
        er_sample_data=sort_magic_file(samp_file,1,'er_sample_name')
        print "-I- Found er_samples.txt"
        print '-I- sample information will be appended to existing er_samples.txt file'
    except:
        print "-I- Cant find file er_samples.txt"
        print '-I- sample information will be stored in new er_samples.txt file'
            
    #--------------------------------------
    # read data from generic file
    #--------------------------------------
    
    if  noave:   
        mag_data=read_generic_file(magfile,False)
    else:
        mag_data=read_generic_file(magfile,True)
        
    #--------------------------------------
    # for each specimen get the data, and translate it to MagIC format
    #--------------------------------------

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

            MagRec={}
            MagRec['er_citation_names']="This study"
            MagRec["er_specimen_name"]=meas_line['specimen']
            MagRec["er_sample_name"]=get_upper_level_name(MagRec["er_specimen_name"],sample_nc)
            MagRec["er_site_name"]=get_upper_level_name(MagRec["er_sample_name"],site_nc)
            MagRec['er_location_name']=er_location_name
            MagRec['er_analyst_mail_names']=user 
            MagRec["magic_instrument_codes"]="" 
            MagRec["measurement_flag"]='g'
            MagRec["measurement_number"]="%i"%measurement_running_number
            
            MagRec["measurement_magn_moment"]='%10.3e'%(float(meas_line["moment"])*1e-3) # in Am^2
            MagRec["measurement_temp"]='273.' # room temp in kelvin

            #------------------
            #  decode treatments from treatment column in the generic file 
            #------------------
            
            treatment=[]
            treatment_code=str(meas_line['treatment']).split(".")
            treatment.append(float(treatment_code[0]))
            if len(treatment_code)==1:
                treatment.append(0)
            else:
                treatment.append(float(treatment_code[1]))

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


            if experiment in ['PI','NLT','CR']:
                
                if float(treatment[1])==0:
                    MagRec["treatment_dc_field"]="0"
                    MagRec["treatment_dc_field_phi"]="0"
                    MagRec["treatment_dc_field_theta"]="0"
                else:                        
                    MagRec["treatment_dc_field"]='%8.3e'%(float(labfield))
                    MagRec["treatment_dc_field_phi"]="%.2f"%(float(labfield_phi))
                    MagRec["treatment_dc_field_theta"]="%.2f"%(float(labfield_theta))
            else:
                MagRec["treatment_dc_field"]=""
                MagRec["treatment_dc_field_phi"]=""
                MagRec["treatment_dc_field_theta"]=""
            
            #------------------
            # treatment temperature/peak field 
            #------------------
            
            if experiment == 'Demag':
                if meas_line['treatment_type']=='A':
                    MagRec['treatment_temp']="273."
                    MagRec["treatment_ac_field"]="%.3e"%(treatment[0]*1e-3)                                                        
                elif meas_line['treatment_type']=='N':
                    MagRec['treatment_temp']="273."
                    MagRec["treatment_ac_field"]=""                                                        
                else:
                    MagRec['treatment_temp']="%.2f"%(treatment[0]+273.)
                    MagRec["treatment_ac_field"]=""                                                        
            else: 
                    MagRec['treatment_temp']="%.2f"%(treatment[0]+273.)
                    MagRec["treatment_ac_field"]=""                                                        


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

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

            #---------------------                    
            # Lab treatment and lab protocoal for ATRM experiment
            #---------------------
                                
            elif experiment in ['ATRM','AARM']:
                
                if experiment=='ATRM':
                    LP="LP-AN-TRM"
                    n_pos=atrm_n_pos                    
                    if n_pos!=6:
                        print "the program does not support ATRM in %i position."%n_pos
                        continue
                        
                if experiment=='AARM':
                    #MagRec['treatment_temp']="273."
                    #MagRec["treatment_ac_field"]=""                                                        
                    LP="LP-AN-ARM"
                    n_pos=aarm_n_pos
                    if n_pos!=6:
                        print "the program does not support AARM in %i position."%n_pos
                        continue
                                
                if treatment[1]==0:
                    if experiment=='ATRM':
                        LT="LT-T-Z"
                        MagRec['treatment_temp']="%.2f"%(treatment[0]+273.)
                        MagRec["treatment_ac_field"]="" 
                                                                               
                    else:
                        LT="LT-AF-Z"                        
                        MagRec['treatment_temp']="273."
                        MagRec["treatment_ac_field"]="%.3e"%(treatment[0]*1e-3)  
                    MagRec["treatment_dc_field"]='0'                                               
                    MagRec["treatment_dc_field_phi"]='0'
                    MagRec["treatment_dc_field_theta"]='0'
                else:
                    if experiment=='ATRM':
                        if float(treatment[1])==70 or float(treatment[1])==7: # alteration check as final measurement
                            LT="LT-PTRM-I"
                        else:
                            LT="LT-T-I"
                    else:
                        LT="LT-AF-I"
                    MagRec["treatment_dc_field"]='%8.3e'%(float(labfield))                                               
                            
                    # find the direction of the lab field in two ways:
                    
                    # (1) using the treatment coding (XX.1=+x, XX.2=+y, XX.3=+z, XX.4=-x, XX.5=-y, XX.6=-z)
                    tdec=[0,90,0,180,270,0,0,90,0]
                    tinc=[0,0,90,0,0,-90,0,0,90]
                    if treatment[1] < 10:
                        ipos_code=int(treatment[1])-1
                    else:
                        ipos_code=int(treatment[1]/10)-1
                    
                    # (2) using the magnetization
                    if meas_line["dec_s"]!="":
                        DEC=float(meas_line["dec_s"])
                        INC=float(meas_line["inc_s"])
                    elif meas_line["dec_g"]!="":
                        DEC=float(meas_line["dec_g"])
                        INC=float(meas_line["inc_g"])
                    elif meas_line["dec_t"]!="":
                        DEC=float(meas_line["dec_t"])
                        INC=float(meas_line["inc_t"])
                    if DEC<0 and DEC>-359:
                        DEC=360.+DEC
                        
                    if INC < 45 and INC > -45:
                        if DEC>315  or DEC<45: ipos_guess=0
                        if DEC>45 and DEC<135: ipos_guess=1
                        if DEC>135 and DEC<225: ipos_guess=3
                        if DEC>225 and DEC<315: ipos_guess=4
                    else:
                        if INC >45: ipos_guess=2
                        if INC <-45: ipos_guess=5
                    # prefer the guess over the code
                    ipos=ipos_guess
                    # check it 
                    if treatment[1]!= 7 and treatment[1]!= 70:
                        if ipos_guess!=ipos_code:
                            print "-W- WARNING: check specimen %s step %s, anistropy measurements, coding does not match the direction of the lab field"%(specimen,meas_line['treatment'])
                    MagRec["treatment_dc_field_phi"]='%7.1f' %(tdec[ipos])
                    MagRec["treatment_dc_field_theta"]='%7.1f'% (tinc[ipos])
                        
        
            #---------------------                    
            # Lab treatment and lab protocoal for cooling rate experiment
            #---------------------
                                                        
            elif experiment == "CR":
                
                coolling_times_list
                LP="LP-CR-TRM"                
                MagRec["treatment_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin
                
                if treatment[1]==0:
                    LT="LT-T-Z"                    
                    MagRec["treatment_dc_field"]="0"
                    MagRec["treatment_dc_field_phi"]='0'
                    MagRec["treatment_dc_field_theta"]='0'
                else:                     
                    if treatment[1]==7: # alteration check as final measurement
                            LT="LT-PTRM-I"
                    else:
                            LT="LT-T-I" 
                    MagRec["treatment_dc_field"]='%8.3e'%(labfield)
                    MagRec["treatment_dc_field_phi"]='%7.1f' % (labfield_phi) # labfield phi
                    MagRec["treatment_dc_field_theta"]='%7.1f' % (labfield_theta) # labfield theta                    

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

            #---------------------                    
            # Lab treatment and lab protocoal for NLT experiment
            #---------------------
            
            elif 'NLT' in experiment :
                print "Dont support yet NLT rate experiment file. Contact [email protected]"

            #---------------------                                        
            # magic_method_codes for this measurement only
            # LP will be fixed after all measurement lines are read
            #---------------------
            
            MagRec["magic_method_codes"]=LT+":"+LP

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

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

            found_s,found_geo,found_tilt=False,False,False
            if "dec_s" in meas_line.keys() and "inc_s" in meas_line.keys():
                if meas_line["dec_s"]!="" and meas_line["inc_s"]!="":
                    found_s=True
                MagRec["measurement_dec"]=meas_line["dec_s"]
                MagRec["measurement_inc"]=meas_line["inc_s"]
            if "dec_g" in meas_line.keys() and "inc_g" in meas_line.keys():
                if meas_line["dec_g"]!="" and meas_line["inc_g"]!="":
                    found_geo=True
            if "dec_t" in meas_line.keys() and "inc_t" in meas_line.keys():
                if meas_line["dec_t"]!="" and meas_line["inc_t"]!="":
                    found_tilt=True
                
            #-----------------------------                    
            # specimen coordinates: no
            # geographic coordinates: yes
            #-----------------------------                    
            
            if found_geo and not found_s:
                MagRec["measurement_dec"]=meas_line["dec_g"]
                MagRec["measurement_inc"]=meas_line["inc_g"]
                
                # core azimuth/plunge is not in er_samples.txt
                if not found_sample_dip or not found_sample_azimuth:
                    er_sample_data[sample]['sample_azimuth']="0"
                    er_sample_data[sample]['sample_dip']="0"

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

            #-----------------------------                                                
            # specimen coordinates: no
            # geographic coordinates: no
            #-----------------------------                    
            if not found_geo and not found_s:
                print "-E- ERROR: sample %s does not have dec_s/inc_s or dec_g/inc_g. Ignore specimen %s "%(sample,specimen)
                break
                    
            #-----------------------------                                                
            # specimen coordinates: yes
            # geographic coordinates: yes
            #
            # commant: Ron, this need to be tested !!
            #-----------------------------                    
            if found_geo and found_s:
                
                cdec,cinc=float(meas_line["dec_s"]),float(meas_line["inc_s"])
                gdec,ginc=float(meas_line["dec_g"]),float(meas_line["inc_g"])
                az,pl=pmag.get_azpl(cdec,cinc,gdec,ginc)

                # core azimuth/plunge is not in er_samples.txt:
                # calculate core az/pl and add it to er_samples.txt
                if not found_sample_dip or not found_sample_azimuth:
                    er_sample_data[sample]['sample_azimuth']="%.1f"%az
                    er_sample_data[sample]['sample_dip']="%.1f"%pl
                
                # core azimuth/plunge is in er_samples.txt
                else:
                    if float(er_sample_data[sample]['sample_azimuth'])!= az:
                        print "-E- ERROR in sample_azimuth sample %s. Check it! using the value in er_samples.txt"%sample
                        
                    if float(er_sample_data[sample]['sample_dip'])!= pl:
                        print "-E- ERROR in sample_dip sample %s. Check it! using the value in er_samples.txt"%sample
                    
            #-----------------------------                                                
            # specimen coordinates: yes
            # geographic coordinates: no
            #-----------------------------                    
            if not found_geo and found_s:
                if found_sample_dip and found_sample_azimuth:
                    pass
                    # (nothing to do)
                else:
                    if "Demag" in experiment:
                        print "-W- WARNING: missing sample_dip or sample_azimuth for sample %s"%sample

            #-----------------------------                                                
            # tilt-corrected coordinates: yes
            # geographic coordinates: no
            #-----------------------------                    
            if found_tilt and not found_geo:
                    print "-E- ERROR: missing geographic data for sample %s. Ignoring tilt-corrected data "%sample
            if found_tilt and found_geo:
                dec_geo,inc_geo=float(meas_line["dec_g"]),float(meas_line["inc_g"])
                dec_tilt,inc_tilt=float(meas_line["dec_t"]),float(meas_line["inc_t"])
                if dec_geo==dec_tilt and inc_geo==inc_tilt:
                    DipDir,Dip=0.,0. 
                else:
                    DipDir,Dip=pmag.get_tilt(dec_geo,inc_geo,dec_tilt,inc_tilt)
                    
                if not found_sample_bed_dip_direction or not found_sample_bed_dip:
                    print "-I- calculating dip and dip direction used for tilt correction sample %s. results are put in er_samples.txt"%sample
                    er_sample_data[sample]['sample_bed_dip_direction']="%.1f"%DipDir
                    er_sample_data[sample]['sample_bed_dip']="%.1f"%Dip

            #-----------------------------                                                
            # er_samples method codes
            # geographic coordinates: no
            #-----------------------------                    
            if found_tilt or found_geo:
                er_sample_data[sample]['magic_method_codes']="SO-NO"               
            
            
            #-----------------
            # er_samples_data
            #-----------------
            if sample in er_sample_data.keys():
                er_sample_data[sample]['er_sample_name']=sample
                er_sample_data[sample]['er_site_name']=MagRec["er_site_name"]
                er_sample_data[sample]['er_location_name']=MagRec["er_location_name"]

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

            #if LP!="" and LP not in LP_this_specimen:
            #    LP_this_specimen.append(LP)
            
            measurement_running_number+=1
            #-------                    

        #-------  
        # after reading all the measurements lines for this specimen                  
        # 1) add magic_experiment_name
        # 2) fix magic_method_codes with the correct lab protocol
        #------- 
        LP_this_specimen=[]                   
        for MagRec in MagRecs_this_specimen:
            magic_method_codes=MagRec["magic_method_codes"].split(":")
            for code in magic_method_codes:
                if "LP" in code and code not in LP_this_specimen:
                    LP_this_specimen.append(code)
        # check IZ/ZI/IZZI
        if "LP-PI-ZI" in   LP_this_specimen and "LP-PI-IZ" in   LP_this_specimen:
            LP_this_specimen.remove("LP-PI-ZI")
            LP_this_specimen.remove("LP-PI-IZ")
            LP_this_specimen.append("LP-PI-BT-IZZI")
        
        # add the right LP codes and fix experiment name    
        for MagRec in MagRecs_this_specimen:                                                          
            MagRec["magic_experiment_name"]=MagRec["er_specimen_name"]+":"+":".join(LP_this_specimen)
            magic_method_codes=MagRec["magic_method_codes"].split(":")
            LT=""
            for code in magic_method_codes:
                if code[:3]=="LT-":
                    LT=code;
                    break            
            MagRec["magic_method_codes"]=LT+":"+":".join(LP_this_specimen)
            
            MagRecs.append(MagRec)   
                                
    #--
    # write magic_measurements.txt
    #--
    MagRecs_fixed=merge_pmag_recs(MagRecs)
    pmag.magic_write(meas_file,MagRecs_fixed,'magic_measurements')
    print "-I- MagIC file is saved in  %s"%meas_file

    #--
    # write er_samples.txt
    #--
    ErSamplesRecs=[]
    samples=er_sample_data.keys()
    samples.sort()
    for sample in samples:
        ErSamplesRecs.append(er_sample_data[sample])
    ErSamplesRecs_fixed=merge_pmag_recs(ErSamplesRecs)
    pmag.magic_write("er_samples.txt",ErSamplesRecs_fixed,'er_samples')
コード例 #2
0
ファイル: kly-asc_magic.py プロジェクト: jholmes/PmagPy
def main():
    """
    NAME
        kly-asc_magic.py

    DESCRIPTION
        converts ascii files generated by SUFAR ver.1.2 to MagIC formated
        files for use with PmagPy plotting software

    SYNTAX
        kly-asc_magic.py -h [command line options]

    OPTIONS
        -h: prints the help message and quits
        -f FILE: specify .asc input file name
        -Fa AFILE: specify rmag_anisotropy output file
        -Fr RFILE: specify rmag_results output file
        -Fs SFILE: specify er_specimens output file with location, sample, site, etc. information
        -usr USER: specify who made the measurements
        -loc LOC: specify location name for study 
        -ins INST: specify instrument used
        -spc SPEC: specify number of characters to specify specimen from sample
        -ncn NCON:  specify naming convention: default is #2 below

    DEFAULTS
        AFILE: rmag_anisotropy.txt
        RFILE: rmag_results.txt
        SFILE: default is to create new er_specimen.txt file
        USER: ""
        LOC: "unknown"
        INST: "PGL-KLY3"
        SPEC: 0  sample name is same as site (if SPEC is 1, sample is all but last character)
        creates new 'er_specimens.txt' file
        Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length) 
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXXYYY:  YYY is sample designation with Z characters from site XXX
            [5] all others you will have to either customize your 
                self or e-mail [email protected] for help.  

    """
    citation = "This study"
    cont = 0
    samp_con, Z = "1", 1
    ResRecs, SucRecs, SpecRecs, SampRecs, SiteRecs = [], [], [], [], []
    user, locname, specfile = "", "locname", "er_specimens.txt"
    isspec, inst, specnum = "0", "PGL-KLY3", 0
    aoutput, routput = "rmag_anisotropy.txt", "rmag_results.txt"
    dir_path = "."
    if "-h" in sys.argv:
        print main.__doc__
        sys.exit()
    if "-usr" in sys.argv:
        ind = sys.argv.index("-usr")
        user = sys.argv[ind + 1]
    if "-ncn" in sys.argv:
        ind = sys.argv.index("-ncn")
        samp_con = sys.argv[ind + 1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print "option [4] must be in form 3-Z where Z is an integer"
                sys.exit()
            else:
                Z = samp_con.split("-")[1]
                samp_con = "4"
    if "-f" in sys.argv:
        ind = sys.argv.index("-f")
        ascfile = sys.argv[ind + 1]
    elif "-i" not in sys.argv:
        print "must specify ascii input file or use interactive option [-i]\n\n "
        print main.__doc__
        sys.exit()
    if "-Fa" in sys.argv:
        ind = sys.argv.index("-Fa")
        aoutput = sys.argv[ind + 1]
    if "-Fr" in sys.argv:
        ind = sys.argv.index("-Fr")
        routput = sys.argv[ind + 1]
    if "-Fs" in sys.argv:
        ind = sys.argv.index("-Fs")
        specfile = sys.argv[ind + 1]
        isspec = "1"
    elif "-loc" in sys.argv:
        ind = sys.argv.index("-loc")
        locname = sys.argv[ind + 1]
        samp_con, Z = pmag.get_samp_con()
        if "-spc" in sys.argv:
            ind = sys.argv.index("-spc")
            specnum = -(int(sys.argv[ind + 1]))
    if "-i" in sys.argv:
        user = raw_input("Who made these measurements? \n Use the earthref mailname please, [] \n")
        if user == "":
            user = ""
        isspec = raw_input(
            " Is there an  existing er_specimen file [1] or do you wish to create one? [0] <return> to skip "
        )
        if isspec == "1":
            specfile = raw_input("Enter er_specimen file name: [er_specimens.txt] ")
            if specfile == "":
                specfile = "er_specimens.txt"
        elif isspec == "0":
            locname = raw_input("Enter location name for this study ")
            samp_con, Z = pmag.get_samp_con()
            ans = raw_input("How many characters to specify specimen [1] ")
            if ans == "":
                specnum = -1
            else:
                specnum = -int(ans)
        aoutput = raw_input("Filename for output [rmag_anisotropy.txt] ")
        if aoutput == "":
            aoutput = "rmag_anisotropy.txt"
        routput = raw_input("Filename for results output [rmag_results.txt] ")
        if routput == "":
            routput = "rmag_results.txt"
        ascfile = raw_input("Enter asc file for processing ")
    if isspec == "1":
        specs, file_type = pmag.magic_read(specfile)
    try:
        input = open(ascfile, "rU")
    except:
        print "Error opening file: ", ascfile
        sys.exit()
    Data = input.readlines()
    k = 0
    while k < len(Data):
        line = Data[k]
        words = line.split()
        if "ANISOTROPY" in words:  # first line of data for the spec
            SucRec, SpecRec, SampRec, SiteRec, ResRec = {}, {}, {}, {}, {}
            specname = words[0]
            SucRec["er_specimen_name"] = specname
            if isspec == "1":
                for spec in specs:
                    if spec["er_specimen_name"] == specname:
                        SucRec["er_sample_name"] = spec["er_sample_name"]
                        SucRec["er_site_name"] = spec["er_site_name"]
                        SucRec["er_location_name"] = spec["er_location_name"]
                        break
            elif isspec == "0":
                SucRec["er_sample_name"] = specname[:specnum]
                SpecRec["er_sample_name"] = specname[:specnum]
                SiteRec["er_sample_name"] = specname[:specnum]
                SucRec["er_site_name"] = pmag.parse_site(SucRec["er_sample_name"], samp_con, Z)
                SpecRec["er_site_name"] = pmag.parse_site(SucRec["er_sample_name"], samp_con, Z)
                SampRec["er_site_name"] = pmag.parse_site(SucRec["er_sample_name"], samp_con, Z)
                SiteRec["er_site_name"] = pmag.parse_site(SucRec["er_sample_name"], samp_con, Z)
                SucRec["er_location_name"] = locname
                SpecRec["er_location_name"] = locname
                SampRec["er_location_name"] = locname
                SiteRec["er_location_name"] = locname
                SpecRec["er_citation_names"] = "This study"
                SampRec["er_citation_names"] = "This study"
                SiteRec["er_citation_names"] = "This study"
                ResRec["er_sample_names"] = SucRec["er_sample_name"]
                ResRec["er_site_names"] = SucRec["er_site_name"]
            SucRec["er_citation_names"] = "This study"
            SucRec["magic_instrument_codes"] = inst
            SucRec["magic_method_codes"] = "LP-X:AE-H:LP-AN-MS"
            SucRec["magic_experiment_names"] = specname + ":" + "LP-AN-MS"
            SucRec["er_analyst_mail_names"] = user
            SucRec["anisotropy_type"] = "AMS"
            SucRec["anisotropy_n"] = "15"
            ResRec["rmag_result_name"] = SucRec["er_specimen_name"] + "Specimen Coordinates"
            ResRec["er_specimen_names"] = SucRec["er_specimen_name"]
            ResRec["magic_experiment_names"] = SucRec["magic_experiment_names"]
            ResRec["anisotropy_type"] = SucRec["anisotropy_type"]
        if "Azi" in words and isspec == "0":
            SampRec["sample_azimuth"] = words[1]
            SampRec["sample_dip"] = "%7.1f" % (-float(words[1]))
            SpecRec["specimen_vol"] = "%8.3e" % (float(words[10]) * 1e-6)  # convert actual volume to m^3 from cm^3
        if "[A/m]" in words:
            k += 2  # read in second line down
            line = Data[k]
            words = line.split()
            SucRec["anisotropy_mean"] = words[1]
            SucRec["anisotropy_sigma"] = words[2]
            SucRec["anisotropy_unit"] = "SI"
            ResRec["anisotropy_ftest"] = words[3]
            ResRec["anisotropy_ftest12"] = words[4]
            ResRec["anisotropy_ftest23"] = words[5]
        if "susceptibilities" in words:
            k += 2  # read in second line down
            line = Data[k]
            words = line.split()
            ResRec["anisotropy_t1"] = "%6.4f" % (float(words[0]) / 3.0)
            ResRec["anisotropy_t2"] = "%6.4f" % (float(words[1]) / 3.0)
            ResRec["anisotropy_t3"] = "%6.4f" % (float(words[2]) / 3.0)
            ResRec["anisotropy_v1_eta_semi_angle"] = words[3]
            ResRec["anisotropy_v2_eta_semi_angle"] = words[4]
            ResRec["anisotropy_v3_eta_semi_angle"] = words[5]
            k += 2  # read in second line down
            line = Data[k]
            words = line.split()
            ResRec["anisotropy_v1_zeta_semi_angle"] = words[4]
            ResRec["anisotropy_v2_zeta_semi_angle"] = words[5]
            ResRec["anisotropy_v3_zeta_semi_angle"] = words[6]
        if "factors" in words:
            k += 4  # read in second line down
            line = Data[k]
            words = line.split()
            ResRec["anisotropy_l"] = words[0]
            ResRec["anisotropy_f"] = words[1]
            ResRec["anisotropy_p"] = words[2]
            ResRec["anisotropy_pp"] = words[3]
            ResRec["anisotropy_t"] = words[4]
        if "Specimen" in words:  # first part of specimen data
            ResRec["anisotropy_v1_dec"] = words[2]
            ResRec["anisotropy_v2_dec"] = words[3]
            ResRec["anisotropy_v3_dec"] = words[4]
            SucRec["anisotropy_s1"] = "%6.4f" % (float(words[5]) / 3.0)  # eigenvalues sum to unity - not 3
            SucRec["anisotropy_s2"] = "%6.4f" % (float(words[6]) / 3.0)
            SucRec["anisotropy_s3"] = "%6.4f" % (float(words[7]) / 3.0)
            k += 1
            line = Data[k]
            words = line.split()
            ResRec["anisotropy_v1_inc"] = words[2]
            ResRec["anisotropy_v2_inc"] = words[3]
            ResRec["anisotropy_v3_inc"] = words[4]
            SucRec["anisotropy_s4"] = "%6.4f" % (float(words[5]) / 3.0)  # eigenvalues sum to unity - not 3
            SucRec["anisotropy_s5"] = "%6.4f" % (float(words[6]) / 3.0)
            SucRec["anisotropy_s6"] = "%6.4f" % (float(words[7]) / 3.0)
            SucRec["anisotropy_tilt_correction"] = "-1"
            ResRec["anisotropy_v1_eta_dec"] = ResRec["anisotropy_v2_dec"]
            ResRec["anisotropy_v1_eta_inc"] = ResRec["anisotropy_v2_inc"]
            ResRec["anisotropy_v1_zeta_dec"] = ResRec["anisotropy_v3_dec"]
            ResRec["anisotropy_v1_zeta_inc"] = ResRec["anisotropy_v3_inc"]
            ResRec["anisotropy_v2_eta_dec"] = ResRec["anisotropy_v1_dec"]
            ResRec["anisotropy_v2_eta_inc"] = ResRec["anisotropy_v1_inc"]
            ResRec["anisotropy_v2_zeta_dec"] = ResRec["anisotropy_v3_dec"]
            ResRec["anisotropy_v2_zeta_inc"] = ResRec["anisotropy_v3_inc"]
            ResRec["anisotropy_v3_eta_dec"] = ResRec["anisotropy_v1_dec"]
            ResRec["anisotropy_v3_eta_inc"] = ResRec["anisotropy_v1_inc"]
            ResRec["anisotropy_v3_zeta_dec"] = ResRec["anisotropy_v2_dec"]
            ResRec["anisotropy_v3_zeta_inc"] = ResRec["anisotropy_v2_inc"]
            ResRec["anisotropy_tilt_correction"] = "-1"
            k += 2
            line = Data[k]
            words = line.split()
            SucRecs.append(SucRec)
            ResRecs.append(ResRec)
            ResRecG = copy.copy(ResRec)
            SucRecG = copy.copy(SucRec)
            ResRecG["rmag_result_name"] = SucRec["er_specimen_name"] + "Geographic Coordinates"
            ResRecG["anisotropy_v1_dec"] = words[2]
            ResRecG["anisotropy_v2_dec"] = words[3]
            ResRecG["anisotropy_v3_dec"] = words[4]
            SucRecG["anisotropy_s1"] = "%6.4f" % (float(words[5]) / 3.0)  # eigenvalues sum to unity - not 3
            SucRecG["anisotropy_s2"] = "%6.4f" % (float(words[6]) / 3.0)
            SucRecG["anisotropy_s3"] = "%6.4f" % (float(words[7]) / 3.0)
            k += 1
            line = Data[k]
            words = line.split()
            ResRecG["anisotropy_v1_inc"] = words[2]
            ResRecG["anisotropy_v2_inc"] = words[3]
            ResRecG["anisotropy_v3_inc"] = words[4]
            SucRecG["anisotropy_s4"] = "%6.4f" % (float(words[5]) / 3.0)  # eigenvalues sum to unity - not 3
            SucRecG["anisotropy_s5"] = "%6.4f" % (float(words[6]) / 3.0)
            SucRecG["anisotropy_s6"] = "%6.4f" % (float(words[7]) / 3.0)
            SucRecG["anisotropy_tilt_correction"] = "0"
            ResRecG["anisotropy_tilt_correction"] = "0"
            Dgeo, Igeo = float(ResRecG["anisotropy_v1_dec"]), float(ResRecG["anisotropy_v1_inc"])
            ResRecG["anisotropy_v1_eta_dec"] = ResRecG["anisotropy_v2_dec"]
            ResRecG["anisotropy_v1_eta_inc"] = ResRecG["anisotropy_v2_inc"]
            ResRecG["anisotropy_v1_zeta_dec"] = ResRecG["anisotropy_v3_dec"]
            ResRecG["anisotropy_v1_zeta_inc"] = ResRecG["anisotropy_v3_inc"]
            ResRecG["anisotropy_v2_eta_dec"] = ResRecG["anisotropy_v1_dec"]
            ResRecG["anisotropy_v2_eta_inc"] = ResRecG["anisotropy_v1_inc"]
            ResRecG["anisotropy_v2_zeta_dec"] = ResRecG["anisotropy_v3_dec"]
            ResRecG["anisotropy_v2_zeta_inc"] = ResRecG["anisotropy_v3_inc"]
            ResRecG["anisotropy_v3_eta_dec"] = ResRecG["anisotropy_v1_dec"]
            ResRecG["anisotropy_v3_eta_inc"] = ResRecG["anisotropy_v1_inc"]
            ResRecG["anisotropy_v3_zeta_dec"] = ResRecG["anisotropy_v2_dec"]
            ResRecG["anisotropy_v3_zeta_inc"] = ResRecG["anisotropy_v2_inc"]
            k += 2
            line = Data[k]
            words = line.split()
            ResRecs.append(ResRecG)
            SucRecs.append(SucRecG)
            SucRecT = copy.copy(SucRecG)
            ResRecT = copy.copy(ResRecG)
            ResRecT["anisotropy_v1_dec"] = words[3]
            ResRecT["anisotropy_v2_dec"] = words[4]
            ResRecT["anisotropy_v3_dec"] = words[5]
            SucRecT["anisotropy_s1"] = "%6.4f" % (float(words[6]) / 3.0)  # eigenvalues sum to unity - not 3
            SucRecT["anisotropy_s2"] = "%6.4f" % (float(words[7]) / 3.0)
            SucRecT["anisotropy_s3"] = "%6.4f" % (float(words[8]) / 3.0)
            k += 1  # skip down 1
            line = Data[k]
            words = line.split()
            ResRecT["anisotropy_v1_inc"] = words[2]
            ResRecT["anisotropy_v2_inc"] = words[3]
            ResRecT["anisotropy_v3_inc"] = words[4]
            Dtilt, Itilt = float(ResRecT["anisotropy_v1_dec"]), float(ResRecT["anisotropy_v1_inc"])
            DipDir, Dip = pmag.get_tilt(Dgeo, Igeo, Dtilt, Itilt)
            SampRec["sample_bed_dip_direction"] = "%7.1f" % (DipDir)
            SampRec["sample_bed_dip"] = "%7.1f" % (Dip)
            SucRecT["anisotropy_s4"] = "%6.4f" % (float(words[5]) / 3.0)  # eigenvalues sum to unity - not 3
            SucRecT["anisotropy_s5"] = "%6.4f" % (float(words[6]) / 3.0)
            SucRecT["anisotropy_s6"] = "%6.4f" % (float(words[7]) / 3.0)
            SucRecT["anisotropy_tilt_correction"] = "100"
            ResRecT["anisotropy_tilt_correction"] = "100"
            ResRecT["anisotropy_v1_eta_dec"] = ResRecT["anisotropy_v2_dec"]
            ResRecT["anisotropy_v1_eta_inc"] = ResRecT["anisotropy_v2_inc"]
            ResRecT["anisotropy_v1_zeta_dec"] = ResRecT["anisotropy_v3_dec"]
            ResRecT["anisotropy_v1_zeta_inc"] = ResRecT["anisotropy_v3_inc"]
            ResRecT["anisotropy_v2_eta_dec"] = ResRecT["anisotropy_v1_dec"]
            ResRecT["anisotropy_v2_eta_inc"] = ResRecT["anisotropy_v1_inc"]
            ResRecT["anisotropy_v2_zeta_dec"] = ResRecT["anisotropy_v3_dec"]
            ResRecT["anisotropy_v2_zeta_inc"] = ResRecT["anisotropy_v3_inc"]
            ResRecT["anisotropy_v3_eta_dec"] = ResRecT["anisotropy_v1_dec"]
            ResRecT["anisotropy_v3_eta_inc"] = ResRecT["anisotropy_v1_inc"]
            ResRecT["anisotropy_v3_zeta_dec"] = ResRecT["anisotropy_v2_dec"]
            ResRecT["anisotropy_v3_zeta_inc"] = ResRecT["anisotropy_v2_inc"]
            SucRecs.append(SucRecT)
            ResRecs.append(ResRecT)
            SpecRecs.append(SpecRec)
            SampRecs.append(SampRec)
            SiteRecs.append(SiteRec)
        k += 1
    pmag.magic_write(aoutput, SucRecs, "rmag_anisotropy")
    print "anisotropy tensors put in ", aoutput
    pmag.magic_write(routput, ResRecs, "rmag_results")
    print "anisotropy results put in ", routput
    if isspec == "0":
        output = "er_specimens.txt"
        pmag.magic_write(output, SpecRecs, "er_specimens")
        print "specimen info put in ", output
        output = "er_samples.txt"
        pmag.magic_write(output, SampRecs, "er_samples")
        print "sample info put in ", output
        output = "er_sites.txt"
        pmag.magic_write(output, SiteRecs, "er_sites")
        print "site info put in ", output
    print """"
コード例 #3
0
ファイル: UCSC_leg_magic.py プロジェクト: jholmes/PmagPy
def main():
    """
    NAME
        UCSC_leg_magic.py
 
    DESCRIPTION
        converts UCSC legacy format files to magic_measurements format files

    SYNTAX
        UCSC_leg_magic.py [command line options]

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

    SYNTAX
        mag_magic.py [command line options]

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

        Format of SIO .mag files:   
        Spec Treat CSD Intensity Declination Inclination [optional metadata string]
        
        
        Spec: specimen name
        Treat:  treatment step
            XXX T in Centigrade
            XXX AF in mT
            for special experiments:
              Thellier:
                XXX.0  first zero field step
                XXX.1  first in field step [XXX.0 and XXX.1 can be done in any order]
                XXX.2  second in-field step at lower temperature (pTRM check)
                XXX.3  second zero-field step after infield (pTRM check step)
                       XXX.3 MUST be done in this order [XXX.0, XXX.1 [optional XXX.2] XXX.3]
              AARM:
                X.00  baseline step (AF in zero bias field - high peak field)
                X.1   ARM step (in field step)  where
                   X is the step number in the 15 position scheme 
                      (see Appendix to Lecture 13 - Lectures in Paleomagnetism, 2007)
              TRM:
                XXX.YYY  XXX is temperature step of total TRM
                         YYY is dc field in microtesla

              Shaw:
                XXX.YY   XXX is AF field
                    YY=00 is AF of NRM
                    YY=01 is AF of ARM1
                    YY=02 is AF of TRM
                    YY=03 is AF of ARM2
                specify temperature and field of Total TRM step on command line
         
         Intensity assumed to be total moment in 10^3 Am^2 (emu)
         Declination:  Declination in specimen coordinate system
         Inclination:  Declination in specimen coordinate system

         Optional metatdata string:  mm/dd/yy;hh:mm;[dC,mT];xx.xx;UNITS;USER;INST;NMEAS
             hh in 24 hours.  
             dC or mT units of treatment XXX (see Treat above) for thermal or AF respectively
             xx.xxx   DC field
             UNITS of DC field (microT, mT)
             INST:  instrument code, number of axes, number of positions (e.g., G34 is 2G, three axes, 
                    measured in four positions)
             NMEAS: number of measurements in a single position (1,3,200...)
       
      Format of LDGO files:   
        SPEC TREAT INST CSD Intensity CDECL CINCL [GDECL GINCL BDECL BINCL SUSC ]
     
    """
# initialize some stuff
    infile_type="mag"
    noave=0
    methcode,inst="",""
    phi,theta,peakfield,labfield=0,0,0,0
    pTRM,MD,samp_con,Z=0,0,'1',1
    dec=[315,225,180,135,45,90,270,270,270,90,180,180,0,0,0]
    inc=[0,0,0,0,0,-45,-45,0,45,45,45,-45,-90,-45,45]
    missing=1
    demag="N"
    er_location_name=""
    citation='This study'
    args=sys.argv
    methcode="LP-NO"
    fmt='old'
    syn=0
    synfile='er_synthetics.txt'
    samp_file,ErSamps='',[]
    trm=0
    irm=0
    specnum=0
#
# get command line arguments
#
    meas_file="magic_measurements.txt"
    user=""
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-usr" in args:
        ind=args.index("-usr")
        user=args[ind+1]
    if '-F' in args:
        ind=args.index("-F")
        meas_file=args[ind+1]
    if '-Fsy' in args:
        ind=args.index("-Fsy")
        synfile=args[ind+1]
    if '-Fsa' in args:
        ind=args.index("-Fsa")
        samp_file=args[ind+1]
        try:
            open(samp_file,'rU')
            ErSamps,file_type=pmag.magic_read(samp_file)
            print 'sample information will be appended to new er_samples.txt file'
        except:
            print 'sample information will be stored in new er_samples.txt file'
    if '-f' in args:
        ind=args.index("-f")
        magfile=args[ind+1]
        try:
            input=open(magfile,'rU')
        except:
            print "bad mag file name"
            sys.exit()
    else: 
        print "mag_file field is required option"
        print main.__doc__
        sys.exit()
    if "-dc" in args:
        ind=args.index("-dc")
        labfield=float(args[ind+1])*1e-6
        phi=float(args[ind+2])
        theta=float(args[ind+3])
    if "-ac" in args:
        ind=args.index("-ac")
        peakfield=float(args[ind+1])*1e-3
    if "-spc" in args:
        ind=args.index("-spc")
        specnum=int(args[ind+1])
        if specnum!=0:specnum=-specnum
    if "-loc" in args:
        ind=args.index("-loc")
        er_location_name=args[ind+1]
    if "-fsa" in args:
        ind=args.index("-fsa")
        Samps,file_type=pmag.magic_read(args[ind+1])
    if '-syn' in args:
        syn=1
        ind=args.index("-syn")
        institution=args[ind+1]
        syntype=args[ind+2]
        if '-fsy' in args:
            ind=args.index("-fsy")
            synfile=args[ind+1]
    if "-ins" in args:
        ind=args.index("-ins")
        inst=args[ind+1]
    if '-FT' in args:
        ind=args.index("-FT")
        FT=args[ind+1]
        if FT=="LDGO":infile_type="ldgo" # default is "mag"
    if "-A" in args: noave=1
    if "-ncn" in args:
        ind=args.index("-ncn")
        samp_con=sys.argv[ind+1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print "option [4] must be in form 4-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="4"
        if "7" in samp_con:
            if "-" not in samp_con:
                print "option [7] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="4"
    if '-LP' in args:
        ind=args.index("-LP")
        codelist=args[ind+1]
        codes=codelist.split(':')
        if "AF" in codes:
            demag='AF' 
            if'-dc' not in args: methcode="LT-AF-Z"
            if'-dc' in args: methcode="LT-AF-I"
        if "T" in codes:
            demag="T"
            if '-dc' not in args: methcode="LT-T-Z"
            if '-dc' in args: methcode="LT-T-I"
        if "I" in codes:
            methcode="LP-IRM"
        if "S" in codes: 
            demag="S"
            methcode="LP-PI-TRM:LP-PI-ALT-AFARM"
            trm_labfield=labfield
            ans=raw_input("DC lab field for ARM step: [50uT] ")
            if ans=="":
                arm_labfield=50e-6
            else: 
                arm_labfield=float(ans)*1e-6
            ans=raw_input("temperature for total trm step: [600 C] ")
            if ans=="":
                trm_peakT=600+273 # convert to kelvin
            else: 
                trm_peakT=float(ans)+273 # convert to kelvin
        if "G" in codes: methcode="LT-AF-G"
	if "D" in codes: methcode="LT-AF-D"
        if "TRM" in codes: 
            demag="T"
            trm=1
    if demag=="T" and "ANI" in codes:
        methcode="LP-AN-TRM"
    if demag=="AF" and "ANI" in codes:
        methcode="LP-AN-ARM"
        if labfield==0: labfield=50e-6
        if peakfield==0: peakfield=.180
    SynRecs,MagRecs=[],[]
    version_num=pmag.get_version()
    if infile_type=="mag":
        for line in input.readlines():
            instcode=""
            if len(line)>2:
                SynRec={}
                MagRec={}
                MagRec['er_location_name']=er_location_name
                MagRec['magic_software_packages']=version_num
                MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
                MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
                MagRec["treatment_ac_field"]='0'
                MagRec["treatment_dc_field"]='0'
                MagRec["treatment_dc_field_phi"]='0'
                MagRec["treatment_dc_field_theta"]='0'
                meas_type="LT-NO"
                rec=line.split()
                if len(rec)>6:
                  code1=rec[6].split(';') # break e.g., 10/15/02;7:45 indo date and time
                  if len(code1)==2: # old format with AM/PM
                    missing=0
                    code2=code1[0].split('/') # break date into mon/day/year
                    code3=rec[7].split(';') # break e.g., AM;C34;200  into time;instr/axes/measuring pos;number of measurements
                    yy=int(code2[2])
                    if yy <90:
                        yyyy=str(2000+yy)
                    else: yyyy=str(1900+yy)
                    mm=int(code2[0])
                    if mm<10:
                        mm="0"+str(mm)
                    else: mm=str(mm)
                    dd=int(code2[1])
                    if dd<10:
                        dd="0"+str(dd)
                    else: dd=str(dd)
                    time=code1[1].split(':')
                    hh=int(time[0])
                    if code3[0]=="PM":hh=hh+12
                    if hh<10:
                        hh="0"+str(hh)
                    else: hh=str(hh)
                    min=int(time[1])
                    if min<10:
                       min= "0"+str(min)
                    else: min=str(min)
                    MagRec["measurement_date"]=yyyy+":"+mm+":"+dd+":"+hh+":"+min+":00.00"
                    MagRec["measurement_time_zone"]='SAN'
                    if inst=="":
                        if code3[1][0]=='C':instcode='SIO-bubba'
                        if code3[1][0]=='G':instcode='SIO-flo'
                    else:
                        instcode=''
                    MagRec["measurement_positions"]=code3[1][2]
                  elif len(code1)>2: # newest format (cryo7 or later)
                    labfield=0
                    fmt='new'
                    date=code1[0].split('/') # break date into mon/day/year
                    yy=int(date[2])
                    if yy <90:
                        yyyy=str(2000+yy)
                    else: yyyy=str(1900+yy)
                    mm=int(date[0])
                    if mm<10:
                        mm="0"+str(mm)
                    else: mm=str(mm)
                    dd=int(date[1])
                    if dd<10:
                        dd="0"+str(dd)
                    else: dd=str(dd)
                    time=code1[1].split(':')
                    hh=int(time[0])
                    if hh<10:
                        hh="0"+str(hh)
                    else: hh=str(hh)
                    min=int(time[1])
                    if min<10:
                       min= "0"+str(min)
                    else: min=str(min)
                    MagRec["measurement_date"]=yyyy+":"+mm+":"+dd+":"+hh+":"+min+":00.00"
                    MagRec["measurement_time_zone"]='SAN'
                    if inst=="":
                        if code1[6][0]=='C':instcode='SIO-bubba'
                        if code1[6][0]=='G':instcode='SIO-flo'
                    else:
                        instcode=''
                    if len(code1)>1:
                        MagRec["measurement_positions"]=code1[6][2]
                    else:
                        MagRec["measurement_positions"]=code1[7]   # takes care of awkward format with bubba and flo being different
                    if user=="":user=code1[5]
                    if code1[2][-1]=='C': demag="T"
                    if code1[2]=='mT':
                        if methcode=="LP-IRM":
                            labfield=float(code1[3])*1e-3
                            if rec[1][0]!="-":
                                phi,theta=0.,90.
                            else:
                                phi,theta=0.,-90.
                            meas_type="LT-IRM"
                            MagRec["treatment_dc_field"]='%8.3e'%(labfield)
                            MagRec["treatment_dc_field_phi"]='%7.1f'%(phi)
                            MagRec["treatment_dc_field_theta"]='%7.1f'%(theta)
                        else:  
                            demag="AF"
                    labfield=float(code1[3])*1e-6
                    if code1[4]=='microT' and labfield!=0.:
                        phi,theta=0.,90.
                        if demag=="T": meas_type="LT-T-I"
                        if demag=="AF": meas_type="LT-AF-I"
                        MagRec["treatment_dc_field"]='%8.3e'%(labfield)
                        MagRec["treatment_dc_field_phi"]='%7.1f'%(phi)
                        MagRec["treatment_dc_field_theta"]='%7.1f'%(theta)
                    if code1[4]=='' or labfield==0.:
                        if demag=='T':meas_type="LT-T-Z"
                        if demag=="AF":meas_type="LT-AF-Z"
                        MagRec["treatment_dc_field"]='0'
                if syn==0:
                    MagRec["er_specimen_name"]=rec[0]
                    MagRec["er_synthetic_name"]=""
                    MagRec["er_site_name"]=""
                    if specnum!=0:
                        MagRec["er_sample_name"]=rec[0][:specnum]
                    else:
                        MagRec["er_sample_name"]=rec[0]
                    if "-fsa" in args:
                        for samp in Samps:
                            if samp["er_sample_name"] == MagRec["er_sample_name"]: 
                                MagRec["er_location_name"]=samp["er_location_name"]
                                MagRec["er_site_name"]=samp["er_site_name"]
                                break
                    elif int(samp_con)!=6:
                        site=pmag.parse_site(MagRec['er_sample_name'],samp_con,Z)
                        MagRec["er_site_name"]=site
                    if MagRec['er_site_name']=="":
                        print 'No site name found for: ',MagRec['er_specimen_name'],MagRec['er_sample_name']
                    if MagRec["er_location_name"]=="":
                        print 'no location name for: ',MagRec["er_specimen_name"] 
                else:
                    if specnum!=0:
                        MagRec["er_sample_name"]=rec[0][:specnum]
                    else:
                        MagRec["er_sample_name"]=rec[0]
                    MagRec["er_site_name"]=""
                    MagRec["er_synthetic_name"]=MagRec["er_specimen_name"]
                    SynRec["er_synthetic_name"]=MagRec["er_specimen_name"]
                    site=pmag.parse_site(MagRec['er_sample_name'],samp_con,Z)
                    SynRec["synthetic_parent_sample"]=site
                    SynRec["er_citation_names"]="This study"
                    SynRec["synthetic_institution"]=institution
                    SynRec["synthetic_type"]=syntype
                    SynRecs.append(SynRec)
                if rec[1]==".00":rec[1]="0.00"
                treat=rec[1].split('.')
                if float(rec[1])==0:
                    pass 
                elif demag=="AF":
                    if methcode != "LP-AN-ARM":
                        MagRec["treatment_ac_field"]='%8.3e' %(float(rec[1])*1e-3) # peak field in tesla
                        meas_type="LT-AF-Z"
                        MagRec["treatment_dc_field"]='0'
                    else: # AARM experiment
                        if treat[1][0]=='0':
                            meas_type="LT-AF-Z"
                            MagRec["treatment_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
                            MagRec["treatment_dc_field"]='%8.3e'%(0)
                            if labfield!=0: print "Warning - inconsistency in mag file with lab field - overriding file with 0"
                        else:
                            meas_type="LT-AF-I"
                            ipos=int(treat[0])-1
                            MagRec["treatment_dc_field_phi"]='%7.1f' %(dec[ipos])
                            MagRec["treatment_dc_field_theta"]='%7.1f'% (inc[ipos])
                            MagRec["treatment_dc_field"]='%8.3e'%(labfield)
                            MagRec["treatment_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
                elif demag=="S": # Shaw experiment
                    if treat[1][1]=='0':
                        if  int(treat[0])!=0:
                            MagRec["treatment_ac_field"]='%8.3e' % (float(treat[0])*1e-3) # AF field in tesla
                            MagRec["treatment_dc_field"]='0'
                            meas_type="LT-AF-Z" # first AF
                        else:
                            meas_type="LT-NO"
                            MagRec["treatment_ac_field"]='0'
                            MagRec["treatment_dc_field"]='0'
                    elif treat[1][1]=='1':
                        if int(treat[0])==0:
                            MagRec["treatment_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
                            MagRec["treatment_dc_field"]='%8.3e'%(arm_labfield)
                            MagRec["treatment_dc_field_phi"]='%7.1f'%(phi)
                            MagRec["treatment_dc_field_theta"]='%7.1f'%(theta)
                            meas_type="LT-AF-I"
                        else:
                            MagRec["treatment_ac_field"]='%8.3e' % ( float(treat[0])*1e-3) # AF field in tesla
                            MagRec["treatment_dc_field"]='0'
                            meas_type="LT-AF-Z"
                    elif treat[1][1]=='2':
                        if int(treat[0])==0:
                            MagRec["treatment_ac_field"]='0'
                            MagRec["treatment_dc_field"]='%8.3e'%(trm_labfield)
                            MagRec["treatment_dc_field_phi"]='%7.1f'%(phi)
                            MagRec["treatment_dc_field_theta"]='%7.1f'%(theta)
                            MagRec["treatment_temp"]='%8.3e' % (trm_peakT)
                            meas_type="LT-T-I"
                        else:
                            MagRec["treatment_ac_field"]='%8.3e' % ( float(treat[0])*1e-3) # AF field in tesla
                            MagRec["treatment_dc_field"]='0'
                            meas_type="LT-AF-Z"
                    elif treat[1][1]=='3':
                        if int(treat[0])==0:
                            MagRec["treatment_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
                            MagRec["treatment_dc_field"]='%8.3e'%(arm_labfield)
                            MagRec["treatment_dc_field_phi"]='%7.1f'%(phi)
                            MagRec["treatment_dc_field_theta"]='%7.1f'%(theta)
                            meas_type="LT-AF-I"
                        else:
                            MagRec["treatment_ac_field"]='%8.3e' % ( float(treat[0])*1e-3) # AF field in tesla
                            MagRec["treatment_dc_field"]='0'
                            meas_type="LT-AF-Z"
                else: 
                  if len(treat)==1:treat.append('0')
                  MagRec["treatment_temp"]='%8.3e' % (float(treat[0])+273.) # temp in kelvin
                  if trm==0:  # demag=T and not trmaq
                    if treat[1][0]=='0':
                        meas_type="LT-T-Z"
                    else: 
                        MagRec["treatment_dc_field"]='%8.3e' % (labfield) # labfield in tesla (convert from microT)
                        MagRec["treatment_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                        MagRec["treatment_dc_field_theta"]='%7.1f' % (theta) # labfield theta
                        if treat[1][0]=='1':meas_type="LT-T-I" # in-field thermal step
                        if treat[1][0]=='2':
                            meas_type="LT-PTRM-I" # pTRM check
                            pTRM=1
                        if treat[1][0]=='3':
                            MagRec["treatment_dc_field"]='0'  # this is a zero field step
                            meas_type="LT-PTRM-MD" # pTRM tail check
                  else: 
                    labfield=float(treat[1])*1e-6
                    MagRec["treatment_dc_field"]='%8.3e' % (labfield) # labfield in tesla (convert from microT)
                    MagRec["treatment_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                    MagRec["treatment_dc_field_theta"]='%7.1f' % (theta) # labfield theta
                    meas_type="LT-T-I:LP-TRM" # trm acquisition experiment
                MagRec["measurement_csd"]=rec[2]
                MagRec["measurement_magn_moment"]='%10.3e'% (float(rec[3])*1e-3) # moment in Am^2 (from emu)
                MagRec["measurement_dec"]=rec[4]
                MagRec["measurement_inc"]=rec[5]
                MagRec["magic_instrument_codes"]=instcode
                MagRec["er_analyst_mail_names"]=user
                MagRec["er_citation_names"]=citation
                MagRec["magic_method_codes"]=meas_type
                MagRec["measurement_flag"]='g'
                MagRec["er_specimen_name"]=rec[0]
                if 'std' in rec[0]:
                    MagRec["measurement_standard"]='s'
                else:
                    MagRec["measurement_standard"]='u'
                MagRec["measurement_number"]='1'
                MagRec["er_specimen_name"],meas_type
                MagRecs.append(MagRec) 
    elif infile_type=="ldgo":
#
# find start of data:
#
        Samps=[] # keeps track of sample orientations
        DIspec=[]
        Data,k=input.readlines(),0
        for k in range(len(Data)):
            rec=Data[k].split()
            if rec[0][0]=="_" or rec[0][0:2]=="!_":
                break
        start=k+1
        for k in range(start,len(Data)):
          rec=Data[k].split()
          if len(rec)>0:
            MagRec={}
            MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
            MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
            MagRec["treatment_ac_field"]='0'
            MagRec["treatment_dc_field"]='0'
            MagRec["treatment_dc_field_phi"]='0'
            MagRec["treatment_dc_field_theta"]='0'
            meas_type="LT-NO"
            MagRec["measurement_flag"]='g'
            MagRec["measurement_standard"]='u'
            MagRec["measurement_number"]='1'
            MagRec["er_specimen_name"]=rec[0]
            if specnum!=0:
                MagRec["er_sample_name"]=rec[0][:specnum]
            else:
                MagRec["er_sample_name"]=rec[0]
            site=pmag.parse_site(MagRec['er_sample_name'],samp_con,Z)
            MagRec["er_site_name"]=site
            MagRec["er_site_name"]=MagRec['er_sample_name'][0:-2]
            MagRec["er_location_name"]=er_location_name
            MagRec["measurement_csd"]=rec[3]
            MagRec["measurement_magn_moment"]='%10.3e'% (float(rec[4])*1e-7) # moment in Am^2 (from 10^-4 emu)
#
            if samp_file!="" and MagRec["er_sample_name"] not in Samps:        # create er_samples.txt file with these data 
                cdec,cinc=float(rec[5]),float(rec[6])
                gdec,ginc=float(rec[7]),float(rec[8])
                az,pl=pmag.get_azpl(cdec,cinc,gdec,ginc)
                bdec,binc=float(rec[9]),float(rec[10])
                if rec[7]!=rec[9] and rec[6]!=rec[8]:
                    dipdir,dip=pmag.get_tilt(gdec,ginc,bdec,binc)
                else:
                    dipdir,dip=0,0
                ErSampRec={}
                ErSampRec['er_location_name']=MagRec['er_location_name']
                ErSampRec['er_sample_name']=MagRec['er_sample_name']
                ErSampRec['er_site_name']=MagRec['er_site_name']
                ErSampRec['sample_azimuth']='%7.1f'%(az)
                ErSampRec['sample_dip']='%7.1f'%(pl)
                ErSampRec['sample_bed_dip_direction']='%7.1f'%(dipdir)
                ErSampRec['sample_bed_dip']='%7.1f'%(dip)
                ErSampRec['sample_description']='az,pl,dip_dir and dip recalculated from [c,g,b][dec,inc] in ldeo file'
                ErSampRec['magic_method_codes']='SO-REC'
                ErSamps.append(ErSampRec)
                Samps.append(ErSampRec['er_sample_name'])
            MagRec["measurement_dec"]=rec[5]
            MagRec["measurement_inc"]=rec[6]
            MagRec["magic_instrument_codes"]=rec[2]
            MagRec["er_analyst_mail_names"]=""
            MagRec["er_citation_names"]="This study"
            MagRec["magic_method_codes"]=meas_type
            if demag=="AF":
                if methcode != "LP-AN-ARM":
                    MagRec["treatment_ac_field"]='%8.3e' %(float(rec[1])*1e-3) # peak field in tesla
                    meas_type="LT-AF-Z"
                    MagRec["treatment_dc_field"]='0'
                else: # AARM experiment
                    if treat[1][0]=='0':
                        meas_type="LT-AF-Z"
                        MagRec["treatment_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
                    else:
                        meas_type="LT-AF-I"
                        ipos=int(treat[0])-1
                        MagRec["treatment_dc_field_phi"]='%7.1f' %(dec[ipos])
                        MagRec["treatment_dc_field_theta"]='%7.1f'% (inc[ipos])
                        MagRec["treatment_dc_field"]='%8.3e'%(labfield)
                        MagRec["treatment_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
            elif demag=="T":
                if rec[1][0]==".":rec[1]="0"+rec[1]
                treat=rec[1].split('.')
                if len(treat)==1:treat.append('0')
                MagRec["treatment_temp"]='%8.3e' % (float(rec[1])+273.) # temp in kelvin
                meas_type="LT-T-Z"
                MagRec["treatment_temp"]='%8.3e' % (float(treat[0])+273.) # temp in kelvin
                if trm==0:  # demag=T and not trmaq
                    if treat[1][0]=='0':
                        meas_type="LT-T-Z"
                    else: 
                        MagRec["treatment_dc_field"]='%8.3e' % (labfield) # labfield in tesla (convert from microT)
                        MagRec["treatment_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                        MagRec["treatment_dc_field_theta"]='%7.1f' % (theta) # labfield theta
                        if treat[1][0]=='1':meas_type="LT-T-I" # in-field thermal step
                        if treat[1][0]=='2':
                            meas_type="LT-PTRM-I" # pTRM check
                            pTRM=1
                        if treat[1][0]=='3':
                            MagRec["treatment_dc_field"]='0'  # this is a zero field step
                            meas_type="LT-PTRM-MD" # pTRM tail check
                else: 
                    meas_type="LT-T-I" # trm acquisition experiment
            MagRec['magic_method_codes']=meas_type
            MagRecs.append(MagRec) 
    MagOuts=pmag.measurements_methods(MagRecs,noave)
    pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    print "results put in ",meas_file
    if samp_file!="":
        pmag.magic_write(samp_file,ErSamps,'er_samples')
        print "sample orientations put in ",samp_file
    if len(SynRecs)>0:
        pmag.magic_write(synfile,SynRecs,'er_synthetics')
        print "synthetics put in ",synfile