Пример #1
0
def main():
    """
    NAME
        FLA_magic.py

    DESCRIPTION
        import data files from the Univ. Florida, Gainesvile format to magic

    SYNTAX
        FLA_magic.py 

    """
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    meas_type,methcode,instcode,experiment_name,izzi="LT-NO","","","",0
    phi,theta,peakfield,labfield=0,0,0,0
    pTRM,MD,ispec=0,0,0
    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]
    er_location_name=raw_input("Enter the location name for this study, or <return> for none ")
    if er_location_name=="":
        er_sites_file=raw_input("Enter site location file name, or <return> for none ")
        if er_sites_file=="": 
            er_location_name="none"
        else:
            site_locations,file_type=pmag.magic_read(er_sites_file)
    magfile=raw_input("Enter florida measurement filename for processing  ")
    print "Enter whether [A]F or [T]hermal de-(re)magnetization  "
    ans=raw_input(" <return> for NRMs only  ")
    inst=""
    methcode="LT-NO"
    if ans=="A":
        inst="UFG-AF"
        methcode="LT-AF-Z"
    if ans=="T":
        inst="UFG-thermal"
        methcode="LT-T-Z"
    if ans=="":methcode="LT-NO"  
    input=open(magfile,'r')
    MagRecs=[]
    for line in input.readlines():
        print line
        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"]='%8.3e' %(peakfield) # peak field in tesla
        MagRec["treatment_dc_field"]='%8.3e' %(labfield) # lab field in tesla
        MagRec["treatment_dc_field_phi"]='%7.1f' %(phi)
        MagRec["treatment_dc_field_theta"]='%7.1f'% (theta)
        rec=line.split()
        MagRec["er_specimen_name"]=rec[0]
        MagRec["er_sample_name"]=rec[0]
        sitetmp=rec[0].split('.') # Florida naming convention splits on '.' between site and sample
        MagRec["er_site_name"]=sitetmp[0]
        MagRec["er_location_name"]="none"
        if er_location_name!="":
            MagRec["er_location_name"]=er_location_name
        else:
            for site in site_locations:
                if site["er_site_name"] == MagRec["er_site_name"]: 
                    MagRec["er_location_name"]=site["er_location_name"]
        if inst=="UFG-AF":
            MagRec["treatment_ac_field"]='%8.3e' % ( float(rec[1])*1e-4) # AF field in tesla from Oersted in Florida convention
            meas_type="LT-AF-Z"
            MagRec["treatment_dc_field"]='0'
        else: 
            MagRec["treatment_temp"]='%8.3e' % (float(rec[1])+273.) # temp in kelvin
            meas_type="LT-T-Z"
        MagRec["measurement_magn_moment"]='%10.3e'% ((float(rec[4])/100)*1e-5) # moment in Am^2 (from 50xA/m and assuming 5cc sample in Florida convention)
        MagRec["measurement_dec"]=rec[7]
        MagRec["measurement_inc"]=rec[8]
        MagRec["magic_method_codes"]=meas_type
        MagRecs.append(MagRec) 
    output=raw_input("Filename for output [magic_measurements.txt] ")
    if output=="":output="magic_measurements.txt"
    MagOuts=pmag.measurements_methods(MagRecs,0)
    pmag.magic_write(output,MagOuts,'magic_measurements')
    print "results put in ",output
Пример #2
0
def main():
    """
    NAME
        HUJI_magic.py
 
    DESCRIPTION
        converts Hebrew University, Jerusalem format files to magic_measurements format files

    SYNTAX
        HUJI_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify HUJI format input file, required
        -F FILE: specify output file, default is magic_measurements.txt
        -LP [colon delimited list of protocols, include all that apply]
            AF:  af demag
            T: thermal including thellier but not trm acquisition
            N: NRM only
            TRM: trm acquisition
            ANI: anisotropy experiment
            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 
        -dc B PHI THETA: dc lab field (in micro tesla) and phi,theta, default is none
              NB: use PHI, THETA = -1 -1 to signal that it changes, i.e. in anisotropy experiment
        -ac B : peak AF field (in mT) for ARM acquisition, default is none
        -ncn NCON:  specify naming convention: default is #1 below
        -A: don't average replicate measurements
       Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name same as sample
            [6] site is entered under a separate column
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            [8] this is a synthetic sample with no site name
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.
 
    INPUT
        Best to put separate experiments (all AF, thermal, thellier, trm aquisition, Shaw, etc.) in 
           seperate files (eg. af.txt, thermal.txt, etc.)
        
        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
         
         Intensity assumed to be total moment in 10^3 Am^2 (emu)
         Declination:  Declination in geographic coordinate system
         Inclination:  Declination in geographic coordinate system

    """
# initialize some stuff
    WD="."
    version_num=pmag.get_version()
    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] # applied field for remanence anisotropy directions
    inc=[0,0,0,0,0,-45,-45,0,45,45,45,-45,-90,-45,45]
    demag="N"
    er_location_name=""
    citation='This study'
    args=sys.argv
    methcode="LP-NO" # NRM
    syn=0
    synfile='er_synthetics.txt'
    samp_file,site_file,ErSamps='er_samples.txt','er_sites.txt',[]
    samp_file_in=""
    meas_file="magic_measurements.txt"
    trm=0
    irm=0
    specnum=-1
#
# get command line arguments
#
    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 '-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 input 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 '-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 "-A" in args: noave=1
    if "-ncn" in args:
        ind=args.index("-ncn")
        samp_con=sys.argv[ind+1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print "option [4] must be in form 4-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="4"
        if "7" in samp_con:
            if "-" not in samp_con:
                print "option [7] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="7"
    if '-LP' in args:
        ind=args.index("-LP")
        codelist=args[ind+1]
        codes=codelist.split(':')
        if "AF" in codes:
            demag='AF' 
            if'-dc' not in args: methcode="LT-AF-Z"
            if'-dc' in args: methcode="LT-AF-I"
        if "T" in codes:
            demag="T"
            if '-dc' not in args: methcode="LT-T-Z"
            if '-dc' in args: methcode="LT-T-I"
        if "I" in codes:
            methcode="LP-IRM"
        if "G" in codes: methcode="LT-AF-G"
        if "TRM" in codes: 
            trm=1
            noave=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=[],[]
    Data=input.readlines()
    for line in Data:
        instcode="HUJI-2G"
        if len(line)>1:
            rec=line.split()
            if rec[0][0]!='#': # HUJI way of marking bad data points
                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"
                mmddyy=rec[2].split('/') # break date into mon/day/year
                hhmm=rec[3]
                yyyy=mmddyy[2]
                mm=int(mmddyy[0])
                if mm<10:
                    mm="0"+str(mm)
                else: 
                    mm=str(mm)
                dd=int(mmddyy[1])
                if dd<10:
                    dd="0"+str(dd)
                else: 
                    dd=str(dd)
                    time=mmddyy[1].split(':')
                MagRec["measurement_date"]=yyyy+":"+mm+":"+dd+":"+hhmm+":00.00"
                MagRec["measurement_time_zone"]='30'
                MagRec["measurement_positions"]='1'
                if rec[4]=='T': demag="T"
                if rec[4]=='A': demag="AF"
                MagRec["er_specimen_name"]=rec[0]
                if specnum!=0:
                    MagRec["er_sample_name"]=rec[0][:specnum]
                else:
                    MagRec["er_sample_name"]=rec[0]
                if samp_con!='8':
                    site=pmag.parse_site(MagRec['er_sample_name'],samp_con,Z)
                    MagRec["er_site_name"]=site
                    MagRec["er_location_name"]=er_location_name
                else: 
                    MagRec["er_site_name"]=""
                    MagRec["er_location_name"]=""
                    MagRec["er_synthetic_name"]=rec[0]
                treat=rec[5].split('.')
                if len(treat)==1:treat.append('.0')
                if demag=="AF":
                    if methcode != "LP-AN-ARM":
                        MagRec["treatment_ac_field"]='%8.3e' %(float(treat[0])*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 "inconsistency in mag file with lab field"
                        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
                else:  # thermal treatment
                  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["treatment_dc_field"]='%8.3e' % (float(treat[1])*1e-6) # labfield in tesla (convert from microT)
                MagRec["measurement_magn_moment"]='%10.3e'% (float(rec[10])*1e-3) # moment in Am^2 (from emu)
                MagRec["measurement_dec"]=rec[6]
                MagRec["measurement_inc"]=rec[7]
                MagRec["magic_instrument_codes"]=instcode
                MagRec["er_citation_names"]=citation
                MagRec["magic_method_codes"]=meas_type
                MagRec["measurement_flag"]='g'
                MagRec["measurement_standard"]='u'
                MagRec["measurement_number"]='1'
                MagRecs.append(MagRec) 
            else:
                print 'skipping: ',rec[0]
    MagOuts=pmag.measurements_methods(MagRecs,noave)
    pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    print "results put in ",meas_file
Пример #3
0
def main(command_line=True, **kwargs):
    """
    NAME
        CIT_magic.py

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

    SYNTAX
        CIT_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify .sam format input file, required
        -fsi SITEFILE : specify file with site names and locations [tab delimited magic file]
        -F FILE: specify output  measurements file, default is magic_measurements.txt
        -Fsp FILE: specify output er_specimens.txt file, default is er_specimens.txt
        -Fsi FILE: specify output er_sites.txt file, default is er_sites.txt
        -Fsa FILE: specify output er_samples.txt file, default is er_samples.txt  # LORI
        -n [gm,kg,cc,m3]: specify normalization
        -A: don't average replicate measurements
        -spc NUM : specify number of characters to designate a  specimen, default = 0
        -ncn NCON: specify naming convention
        -loc LOCNAME : specify location/study name, must have either LOCNAME or SITEFILE or be a synthetic
        -mcd [FS-FD:SO-MAG,.....] colon delimited list for method codes applied to all specimens in .sam file
        -dc B PHI THETA: dc lab field (in micro tesla) and phi,theta, default is none
              NB: use PHI, THETA = -1 -1 to signal that it changes, i.e. in anisotropy experiment
        -ac B : peak AF field (in mT) for ARM acquisition, default is none

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

    NOTES:
         Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length) [default]
            [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.

    """
    #
    #initialize variables
    norm='cc'
    samp_con,Z='3',1
    meas_file='magic_measurements.txt'
    spec_file='er_specimens.txt'
    samp_file='er_samples.txt'
    site_file='er_sites.txt'
    ErSpecs,ErSamps,ErSites,ErLocs,ErCits=[],[],[],[],[]
    MeasRecs=[]
    specnum,units,locname=0,"1","unknown"
    citation="This study"
    dir_path='.'
    args=sys.argv
    if command_line:
        if '-WD' in args:
            ind=args.index("-WD")
            dir_path=args[ind+1]
        if "-h" in args:
            print main.__doc__
            return False
        if "-usr" in args:
            ind=args.index("-usr")
            user=args[ind+1]
        if '-F' in args:
            ind=args.index("-F")
            meas_file=args[ind+1]
        if '-Fsp' in args:
            ind=args.index("-Fsp")
            spec_file=args[ind+1]
        if '-Fsa' in args:
            ind=args.index("-Fsa")
            samp_file=args[ind+1]
        if '-Fsi' in args:   # LORI addition
            ind=args.index("-Fsi")
            site_file=args[ind+1]
        if '-loc' in args:
            ind=args.index("-loc")
            locname=args[ind+1]
        if '-mcd' in args:
            ind=args.index("-mcd")
            methods=args[ind+1]
        else:
            methods='SO-MAG'
        if '-spc' in args:
            ind=args.index("-spc")
            specnum=-int(args[ind+1])
        if '-n' in args:
            ind=args.index("-n")
            norm=args[ind+1]
        if "-A" in args:
            avg=1
        else:
            avg=0
        if "-ncn" in args:
            ind=args.index("-ncn")
            samp_con=sys.argv[ind+1]
            if "4" in samp_con:
                if "-" not in samp_con:
                    print "option [4] must be in form 4-Z where Z is an integer"
                    return False, "naming convention option [4] must be in form 4-Z where Z is an integer"
                else:
                    Z=samp_con.split("-")[1]
                    samp_con="4"
        if '-f' in args:
            ind=args.index("-f")
            magfile=args[ind+1]
        if '-ID' in args:
            ind = args.index('-ID')
            input_dir_path = args[ind+1]
        else:
            input_dir_path = dir_path
        output_dir_path = dir_path
        # LJ

    # if you are running as a module:
    elif not command_line:
        dir_path = kwargs.get('dir_path', '.')
        user = kwargs.get('user', '')
        meas_file = kwargs.get('meas_file', 'magic_measurements.txt') # outfile
        spec_file = kwargs.get('spec_file', 'er_specimens.txt') # specimen outfile
        samp_file = kwargs.get('samp_file', 'er_samples.txt') # sample outfile
        site_file = kwargs.get('site_file', 'er_sites.txt') # site outfile
        locname = kwargs.get('locname', '')
        methods = kwargs.get('methods', ['SO-MAG'])
        specnum = -int(kwargs.get('specnum', 0))
        norm = kwargs.get('norm', 'cc')
        avg = kwargs.get('avg', 0)  # 0 means do average, 1 means don't
        samp_con = kwargs.get('samp_con', '3')
        magfile = kwargs.get('magfile', '')
        input_dir_path = kwargs.get('input_dir_path', dir_path)
        output_dir_path = dir_path
    # done with module-specific stuff

    # formatting and checking variables
    if "4" in samp_con:
        if "-" not in samp_con:
            print "option [4] must be in form 4-Z where Z is an integer"
            return False, "naming convention option [4] must be in form 4-Z where Z is an integer"
        else:
            Z=samp_con.split("-")[1]
            samp_con="4"

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

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

            treat_type=line[0:3]
            treat=line[3:6]
            #print 'treat:', treat
            if treat_type.strip()=='NRM':
                MeasRec['magic_method_codes']='LT-NO'
                MeasRec['measurement_temp']='273'
                MeasRec['treatment_temp']='273'
                MeasRec['treatment_dc_field']='0'
                MeasRec['treatment_ac_field']='0'
            elif treat_type.strip()=='AF':
                MeasRec['magic_method_codes']='LT-AF-Z'
                MeasRec['measurement_temp']='273'
                MeasRec['treatment_temp']='273'
                MeasRec['treatment_dc_field']='0'
                if treat == '   ':
                    MeasRec['treatment_ac_field']='0'
                else:
                    MeasRec['treatment_ac_field']='%10.3e'%(float(treat)*1e-3)
            elif treat_type.strip()=='TT':
                MeasRec['magic_method_codes']='LT-T-Z'
                MeasRec['measurement_temp']='273'
                if treat == '   ':
                    MeasRec['treatment_temp']='273'
                else:
                    MeasRec['treatment_temp']='%7.1f'%(float(treat)+273)
                MeasRec['treatment_dc_field']='0'
                MeasRec['treatment_ac_field']='0'
            elif treat_type.strip()=='LT' or treat_type.strip()=='LN2':
                MeasRec['magic_method_codes']='LT-LT-Z'
                MeasRec['measurement_temp']='273'
                MeasRec['treatment_temp']='77'
                MeasRec['treatment_dc_field']='0'
                MeasRec['treatment_ac_field']='0'
            else:
                print "trouble with your treatment steps"
            MeasRec['measurement_dec']=line[46:51]
            MeasRec['measurement_inc']=line[52:58]
            M='%8.2e'%(float(line[31:39])*vol*1e-3) # convert to Am2
            MeasRec['measurement_magn_moment']=M
            MeasRec['measurement_sd_x']='%8.2e'%(float(line[58:67])*1e-8) #(convert e-5emu to Am2)
            MeasRec['measurement_sd_y']='%8.2e'%(float(line[67:76])*1e-8)
            MeasRec['measurement_sd_z']='%8.2e'%(float(line[76:85])*1e-8)
            MeasRec['measurement_csd']='%7.1f'%(eval(line[41:46]))
            MeasRec['magic_instrument_codes']=line[85:]
            MeasRec["measurement_positions"]='1'
            MeasRec['measurement_standard']='u'

            MeasRecs.append(MeasRec)
        ErSpecs.append(ErSpecRec)
        if sample not in samples:
            samples.append(sample)
            ErSamps.append(ErSampRec)
        site=pmag.parse_site(sample,samp_con,Z)
        if site not in sites:
            sites.append(site)
            ErSites.append(ErSiteRec)
    pmag.magic_write(spec_file,ErSpecs,'er_specimens')
    print 'specimens stored in ',spec_file
    pmag.magic_write(samp_file,ErSamps,'er_samples')
    print 'samples stored in ',samp_file
    pmag.magic_write(site_file,ErSites,'er_sites')
    print 'sites stored in ', site_file
    Fixed=pmag.measurements_methods(MeasRecs,avg)
    pmag.magic_write(meas_file,Fixed,'magic_measurements')
    print 'data stored in ',meas_file
    return True, meas_file
Пример #4
0
def main():
    """
    NAME
        UCSC_leg_magic.py
 
    DESCRIPTION
        converts UCSC legacy format files to magic_measurements format files

    SYNTAX
        UCSC_leg_magic.py [command line options]

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

    OPTIONS
        -f FILE: specify master txt file
        -fpos FILE: specify stratigraphic position  file (.saf format)
        -F FILE: specify magic_measurements output file, default is: magic_measurements.txt
        -Fsa FILE: specify output file, default is: er_samples.txt 
        -ncn NCON:  specify naming convention: default is #2 below
        -ocn OCON:  specify orientation convention, default is #5 below
        -mcd: specify sampling method codes as a colon delimited string:  [default is: FS-FD:SO-POM]
             FS-FD field sampling done with a drill
             FS-H field sampling done with hand samples
             FS-LOC-GPS  field location done with GPS
             FS-LOC-MAP  field location done with map
             SO-POM   a Pomeroy orientation device was used
             SO-ASC   an ASC orientation device was used
             SO-MAG   orientation with magnetic compass
             SO-SUN   orientation with sun compass
        -loc: location name, default="unknown"
        -spc NUM : specify number of characters to designate a  specimen, default = 0     
        -ins INST : specify instsrument name
        -a: average replicate measurements

    INPUT FORMAT
        Input files are horrible 2G binary format (who knows why?)
        Orientation convention:
            [1] Lab arrow azimuth= mag_azimuth; Lab arrow dip=-field_dip
                i.e., field_dip is degrees from vertical down - the hade [default]
            [2] Lab arrow azimuth = mag_azimuth-90; Lab arrow dip = -field_dip
                i.e., mag_azimuth is strike and field_dip is hade
            [3] Lab arrow azimuth = mag_azimuth; Lab arrow dip = 90-field_dip
                e.g. field_dip is degrees from horizontal of drill direction
            [4] lab azimuth and dip are same as mag_azimuth, field_dip
            [5] lab azimuth and dip are same as mag_azimuth, field_dip-90
            [6] all others you will have to either customize your 
                self or e-mail [email protected] for help.  
       
         Magnetic declination convention:
             Az will use supplied declination to correct azimuth 
    
       Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name 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.
 
    OUTPUT
            output saved in magic_measurements.txt & er_samples.txt formatted files
              will overwrite any existing files 
    """
    #
    # initialize variables
    #
    specnum=0
    ub_file,samp_file,or_con,corr,meas_file = "","er_samples.txt","3","1","magic_measurements.txt"
    pos_file=""
    noave=1
    args=sys.argv
    bed_dip,bed_dip_dir="",""
    samp_con,Z,average_bedding="2",1,"0"
    meths='FS-FD'
    sclass,lithology,type="","",""
    user,inst="",""
    DecCorr=0.
    location_name="unknown"
    months=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    gmeths=""
    #
    #
    dir_path='.'
    if '-WD' in args:
        ind=args.index("-WD")
        dir_path=sys.argv[ind+1]
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-f" in args:
        ind=args.index("-f")
        ub_file=sys.argv[ind+1]
    if "-fpos" in args:
        ind=args.index("-fpos")
        pos_file=sys.argv[ind+1]
    if "-F" in args:
        ind=args.index("-F")
        meas_file=sys.argv[ind+1]
    if "-Fsa" in args:
        ind=args.index("-Fsa")
        samp_file=sys.argv[ind+1]
    if "-ocn" in args:
        ind=args.index("-ocn")
        or_con=sys.argv[ind+1]
    if "-ncn" in args:
        ind=args.index("-ncn")
        samp_con=sys.argv[ind+1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print "option [4] must be in form 4-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="4"
        if "7" in samp_con:
            if "-" not in samp_con:
                print "option [7] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="7"
    if "-mcd" in args:
        ind=args.index("-mcd")
        gmeths=(sys.argv[ind+1])
    if "-loc" in args:
        ind=args.index("-loc")
        location_name=(sys.argv[ind+1])
    if "-spc" in args:
        ind=args.index("-spc")
        specnum=int(args[ind+1])
        if specnum!=0:specnum=-specnum
    if "-ins" in args:
        ind=args.index("-ins")
        inst=args[ind+1]
    if "-a" in args:noave=0
    #
    #
    ub_file=dir_path+'/'+ub_file
    if pos_file!="":pos_file=dir_path+'/'+pos_file
    samp_file=dir_path+'/'+samp_file
    meas_file=dir_path+'/'+meas_file
    samplist=[]
    try:
        SampOut,file_type=pmag.magic_read(samp_file)
        for rec in SampOut:
            if samp['er_sample_name'] not in samplist: samplist.append(samp['er_sample_name'])
    except:
        SampOut=[]
    PosData=[]
    if pos_file != "":
        p=open(pos_file,'rU')
        PosIn=p.readlines()
        p.close()
        for line in PosIn:
            srec=line.split()[0].split(',')
            Prec={'er_site_name':srec[0],'sample_height':srec[1]}
            PosData.append(Prec)
    mastfile=open(ub_file,'rU')
    Files=mastfile.readlines()
    mastfile.close()
    MagRecs,SampRecs=[],[]
    for file in Files:
      cfile=file.split()[0]
      if cfile!='end':
        cfile=dir_path+'/'+file.split()[0]+'.dat'
        f=open(cfile,'rU')
        input=f.read()
        f.close()
        firstline=1
        d=input.split('\xcd')
        for line in d:
                rec=line.split('\x00')
                if firstline==1:
                    firstline=0
                    specname,vol="",1
                    for c in line[15:23]:
                        if c!='\x00':specname=specname+c 
                    print 'importing ',specname
                    el=9
                    while rec[el]!='\x01':el+=1
                    vcc,date,comment=rec[el-3],rec[el+7],rec[el+8]
                    el+=10
                    while rec[el]=="":el+=1
                    az=float(rec[el])
                    el+=1
                    while rec[el]=="":el+=1
                    pl=float(rec[el])
                    el+=1
                    while rec[el]=="":el+=1
                    bed_dip_dir=float(rec[el])
                    el+=1
                    while rec[el]=="":el+=1
                    bed_dip=float(rec[el])
                    el+=1
                    while rec[el]=="":el+=1
                    if rec[el]=='\x01': 
                        bed_dip=180.-bed_dip
                        el+=1
                        while rec[el]=="":el+=1
                    fold_az=float(rec[el])
                    el+=1
                    while rec[el]=="":el+=1
                    fold_pl=rec[el]
                    el+=1
                    while rec[el]=="":el+=1
                    if rec[el]!="":
                        deccorr=float(rec[el])
                        az+=deccorr
                        bed_dip_dir+=deccorr
                        fold_az+=deccorr
                        if bed_dip_dir>=360:bed_dip_dir=bed_dip_dir-360.
                        if az>=360.:az=az-360.
                        if fold_az>=360.:fold_az=fold_az-360.
                    else:
                        deccorr=0
                    if specnum!=0:
                        sample=specname[:specnum]
                    else:
                        sample=specname
                    if sample not in samplist:
                        samplist.append(sample)
                        SampRec={}
                        SampRec["er_sample_name"]=sample
                        SampRec["er_location_name"]=location_name
                        SampRec["er_citation_names"]="This study"
                        labaz,labdip=pmag.orient(az,pl,or_con) # convert to labaz, labpl
        #
        # parse information common to all orientation methods
        #
                        SampRec["sample_bed_dip"]='%7.1f'%(bed_dip)
                        SampRec["sample_bed_dip_direction"]='%7.1f'%(bed_dip_dir)
                        SampRec["sample_dip"]='%7.1f'%(labdip)
                        SampRec["sample_azimuth"]='%7.1f'%(labaz)
                        if vcc!="":vol=float(vcc)*1e-6 # convert to m^3 from cc
                        SampRec["sample_volume"]='%10.3e'%(vol) # 
                        SampRec["sample_class"]=sclass
                        SampRec["sample_lithology"]=lithology
                        SampRec["sample_type"]=type
                        SampRec["sample_declination_correction"]='%7.1f'%(deccorr)
                        methods=gmeths.split(':')
                        if deccorr!="0":
                            if 'SO-MAG' in methods:del methods[methods.index('SO-MAG')]
                            methods.append('SO-CMD-NORTH')
                        meths=""
                        for meth in methods:meths=meths+meth+":"
                        meths=meths[:-1]
                        SampRec["magic_method_codes"]=meths
                        site=pmag.parse_site(SampRec["er_sample_name"],samp_con,Z) # parse out the site name
                        SampRec["er_site_name"]=site
    #
    # find position data
    #
                    if PosData!=[]:
                        SampRec['sample_height']=""
                        for srec in PosData:
                            if srec['er_site_name']==site:
                                SampRec['sample_height']=srec['sample_height']
                                break
                    SampOut.append(SampRec)
                else:
                    MagRec={}
                    MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
                    MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
                    MagRec["treatment_ac_field"]='0'
                    MagRec["treatment_dc_field"]='0'
                    MagRec["treatment_dc_field_phi"]='0'
                    MagRec["treatment_dc_field_theta"]='0'
                    meas_type="LT-NO"
                    MagRec["measurement_flag"]='g'
                    MagRec["measurement_standard"]='u'
                    MagRec["measurement_number"]='1'
                    MagRec["er_specimen_name"]=specname
                    MagRec["er_sample_name"]=SampRec['er_sample_name']
                    MagRec["er_site_name"]=SampRec['er_site_name']
                    MagRec["er_location_name"]=location_name
                    el,demag=1,''
                    treat=rec[el]
                    if treat[-1]=='C':
                        demag='T'
                    elif treat!='NRM':
                        demag='AF'  
                    el+=1
                    while rec[el]=="":el+=1
                    MagRec["measurement_dec"]=rec[el]
                    cdec=float(rec[el])
                    el+=1
                    while rec[el]=="":el+=1
                    MagRec["measurement_inc"]=rec[el]
                    cinc=float(rec[el])
                    el+=1
                    while rec[el]=="":el+=1
                    gdec=rec[el]
                    el+=1
                    while rec[el]=="":el+=1
                    ginc=rec[el]
                    el=skip(2,el,rec) # skip bdec,binc
#                    el=skip(4,el,rec) # skip gdec,ginc,bdec,binc
#                    print 'moment emu: ',rec[el]
                    MagRec["measurement_magn_moment"]='%10.3e'% (float(rec[el])*1e-3) # moment in Am^2 (from emu)
                    MagRec["measurement_magn_volume"]='%10.3e'% (float(rec[el])*1e-3/vol) # magnetization in A/m
                    el=skip(2,el,rec) # skip to xsig
                    MagRec["measurement_sd_x"]='%10.3e'% (float(rec[el])*1e-3) # convert from emu
                    el=skip(3,el,rec) # skip to ysig
                    MagRec["measurement_sd_y"]='%10.3e'% (float(rec[el])*1e-3) # convert from emu
                    el=skip(3,el,rec) # skip to zsig
                    MagRec["measurement_sd_z"]='%10.3e'% (float(rec[el])*1e-3) # convert from emu
                    el+=1 # skip to positions
                    MagRec["measurement_positions"]=rec[el]
                    el=skip(5,el,rec) # skip to date
                    date=rec[el].split()
                    mm=str(months.index(date[0]))
                    if len(mm)==1:
                        mm='0'+str(mm)
                    else:
                        mm=str(mm)
                    dstring=date[2]+':'+mm+':'+date[1]+":"+date[3]
                    MagRec['measurement_date']=dstring
                    MagRec["magic_instrument_codes"]=inst
                    MagRec["er_analyst_mail_names"]="unknown"
                    MagRec["er_citation_names"]="This study"
                    MagRec["magic_method_codes"]=meas_type
                    if demag=="AF":
                        MagRec["treatment_ac_field"]='%8.3e' %(float(treat[:-2])*1e-3) # peak field in tesla
                        meas_type="LT-AF-Z"
                        MagRec["treatment_dc_field"]='0'
                    elif demag=="T":
                        MagRec["treatment_temp"]='%8.3e' % (float(treat[:-1])+273.) # temp in kelvin
                        meas_type="LT-T-Z"
                    MagRec['magic_method_codes']=meas_type
                    MagRecs.append(MagRec) 
    MagOuts=pmag.measurements_methods(MagRecs,noave)
    pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    print "Measurements put in ",meas_file
    pmag.magic_write(samp_file,SampOut,"er_samples")
    print "Sample orientation info  saved in ", samp_file
    print "Good bye"
Пример #6
0
def main():
    """
    NAME
        TDT_magic.py
 
    DESCRIPTION
        converts ThellierTool format files to magic_measurements format files

    SYNTAX
        TDT_magic.py [command line options]

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

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

    OPTIONS
        -f FILE: specify input file
        -fpos FILE: specify stratigraphic position  file (.saf format)
        -F FILE: specify magic_measurements output file, default is: magic_measurements.txt
        -Fsa FILE: specify output file, default is: er_samples.txt 
        -ncn NCON:  specify naming convention: default is #1 below
        -ocn OCON:  specify orientation convention, default is #1 below
        -mcd: specify sampling method codes as a colon delimited string:  [default is: FS-FD:SO-POM]
             FS-FD field sampling done with a drill
             FS-H field sampling done with hand samples
             FS-LOC-GPS  field location done with GPS
             FS-LOC-MAP  field location done with map
             SO-POM   a Pomeroy orientation device was used
             SO-ASC   an ASC orientation device was used
             SO-MAG   orientation with magnetic compass
             SO-SUN   orientation with sun compass
        -loc: location name, default="unknown"
        -spc NUM : specify number of characters to designate a  specimen, default = 0     
        -ins INST : specify instsrument name

    INPUT FORMAT
        Input files must be colon delimited:
            "file_name", "instrument"
            "specimen name","",az,pl,vol(cc),strike,dip
            treatment,X,Y,Z,CSD,"yy-mm-dd","hh:mm"
        Orientation convention:
            [1] Lab arrow azimuth= mag_azimuth; Lab arrow dip=-dip
                i.e., dip is degrees from vertical down - the hade [default]
            [2] Lab arrow azimuth = mag_azimuth-90; Lab arrow dip = -dip                
                i.e., mag_azimuth is strike and dip is hade
            [3] Lab arrow azimuth = mag_azimuth; Lab arrow dip = dip-90
                e.g. dip is degrees from horizontal of drill direction            
            [4] Lab arrow azimuth = mag_azimuth; Lab arrow dip = dip
            [5] Lab arrow azimuth = mag_azimuth; Lab arrow dip = 90-dip
            [6] all others you will have to either customize your
                self or e-mail [email protected] for help. 
       
         Magnetic declination convention:
             Az is already corrected in file 
    
       Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name 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.
 
    OUTPUT
            output saved in magic_measurements.txt & er_samples.txt formatted files
              will overwrite any existing files 
    """
    #
    # initialize variables
    #
    uu_file,samp_file,or_con,corr,meas_file = "","er_samples.txt","1","1","magic_measurements.txt"
    pos_file=""
    specnum=-1
    args=sys.argv
    bed_dip,bed_dip_dir="",""
    samp_con,Z,average_bedding="1",1,"0"
    meths='FS-FD:SO-POM'
    sclass,lithology,type="","",""
    user,inst="",""
    or_con='1'
    corr=="3"
    DecCorr=0.
    location_name="unknown"
    #
    #
    dir_path='.'
    if '-WD' in args:
        ind=args.index("-WD")
        dir_path=sys.argv[ind+1]
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-f" in args:
        ind=args.index("-f")
        uu_file=sys.argv[ind+1]
        d=uu_file.split('.')
        if d[1].upper()=="AF":demag="AF"
        if d[1].upper()=="TH":demag="T"
    if "-fpos" in args:
        ind=args.index("-fpos")
        pos_file=sys.argv[ind+1]
    if "-F" in args:
        ind=args.index("-F")
        meas_file=sys.argv[ind+1]
    if "-Fsa" in args:
        ind=args.index("-Fsa")
        samp_file=sys.argv[ind+1]
    if "-ocn" in args:
        ind=args.index("-ocn")
        or_con=sys.argv[ind+1]
    if "-ncn" in args:
        ind=args.index("-ncn")
        samp_con=sys.argv[ind+1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print "option [4] must be in form 4-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="4"
        if "7" in samp_con:
            if "-" not in samp_con:
                print "option [7] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="7"
    if "-mcd" in args:
        ind=args.index("-mcd")
        meths=(sys.argv[ind+1])
    if "-loc" in args:
        ind=args.index("-loc")
        location_name=(sys.argv[ind+1])
    if "-spc" in args:
        ind=args.index("-spc")
        specnum=-int(args[ind+1])
        if specnum!=0:specnum=-specnum
    if "-ins" in args:
        ind=args.index("-ins")
        inst=args[ind+1]
    #
    #
    uu_file=dir_path+'/'+uu_file
    samp_file=dir_path+'/'+samp_file
    meas_file=dir_path+'/'+meas_file
    if pos_file!="":pos_file=dir_path+'/'+pos_file
    samplist=[]
    try:
        SampOut,file_type=pmag.magic_read(samp_file)
        for rec in SampOut:
            if rec['er_sample_name'] not in samplist: samplist.append(rec['er_sample_name'])
    except:
        SampOut=[]
    PosData=[]
    if pos_file != "":
        p=open(pos_file,'rU')
        PosIn=p.readlines()
        p.close()
        for line in PosIn:
            srec=line.split()[0].split(',')
            Prec={'er_site_name':srec[0],'sample_height':srec[1]}
            PosData.append(Prec)
    infile=open(uu_file,'rU')
    Data=infile.readlines()
    infile.close()
    MagRecs=[]
    header=Data[0].split(',')
    if inst=="":inst=header[1].strip('"')
    if inst=='2G DC':
        inst=="FH-2GDC" # Dc Squid machine at Fort Hoofddijk
    else: 
        inst=""
    newsamp=1
    for k in range(1,len(Data)-1):  # step through file, skipping header  and "END" statement
        line=Data[k].split('\n')[0]
        rec=line.split(',')
        if newsamp==1 and rec[0].lower()!='end':
            newsamp=0
            specname=rec[0].strip('"') # take off quotation marks 
            if specnum!=0:
                sample=specname[:specnum]
            else:
                sample=specname
            site=pmag.parse_site(sample,samp_con,Z) # parse out the site name
            SampRec={}
            SampRec["er_sample_name"]=sample
            SampRec["er_location_name"]=location_name
            SampRec["er_citation_names"]="This study"
            labaz,labdip=pmag.orient(float(rec[2]),float(rec[3]),or_con)
            bed_dip=float(rec[6])
            if bed_dip!=0:
                bed_dip_dir=float(rec[5])+90. # assume dip to right of strike
                if bed_dip_dir>=360:bed_dip_dir=bed_dip_dir-360.
            else: 
                bed_dip_dir=float(rec[5]) 
    
    # parse information common to all orientation methods
    #
            SampRec["sample_bed_dip"]='%7.1f'%(bed_dip)
            SampRec["sample_bed_dip_direction"]='%7.1f'%(bed_dip_dir)
            SampRec["sample_dip"]='%7.1f'%(labdip)
            SampRec["sample_azimuth"]='%7.1f'%(labaz)
            vol=float(rec[4])*1e-6
            SampRec["sample_volume"]='%10.3e'%(vol) # convert cc into m^3
            SampRec["sample_class"]=sclass
            SampRec["sample_lithology"]=lithology
            SampRec["sample_type"]=type
            SampRec["magic_method_codes"]=meths
            SampRec["er_site_name"]=site
#
# find position data
#
            if PosData!=[]:
                    for srec in PosData:
                        if srec['er_site_name']==site:
                            SampRec['sample_height']=srec['sample_height']
                            break
            if sample not in samplist:
                samplist.append(sample)
                SampOut.append(SampRec)
        elif rec[0]=='9999': # end of this specimen
            k=k+1 # move on
            newsamp=1
        elif rec[0].lower()!='end' and rec[0]!="":  # got some data
            line=Data[k].split('\n')[0]
            rec=line.split(',')
            MagRec={}
            MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
            MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
            MagRec["treatment_ac_field"]='0'
            MagRec["treatment_dc_field"]='0'
            MagRec["treatment_dc_field_phi"]='0'
            MagRec["treatment_dc_field_theta"]='0'
            meas_type="LT-NO"
            MagRec["measurement_flag"]='g'
            MagRec["measurement_standard"]='u'
            MagRec["measurement_number"]='1'
            MagRec["er_specimen_name"]=specname
            MagRec["er_sample_name"]=sample
            MagRec["er_site_name"]=site
            MagRec["er_location_name"]=location_name
            MagRec["measurement_csd"]=rec[4]
            cart=[]
            cart.append(-float(rec[2])) # appending x,y,z from data record
            cart.append(float(rec[3]))
            cart.append(-float(rec[1]))
            Dir=pmag.cart2dir(cart)
            MagRec["measurement_magn_volume"]='%10.3e'% (float(Dir[2])*1e-6) # moment in A/m (from 10^-6 A/m)
            MagRec["measurement_magn_moment"]='%10.3e'% (float(Dir[2])*vol*1e-6) # moment in Am^2  
            MagRec["measurement_dec"]='%7.1f'%(Dir[0])
            MagRec["measurement_inc"]='%7.1f'%(Dir[1])
            MagRec["magic_instrument_codes"]=inst
            MagRec["er_analyst_mail_names"]=""
            MagRec["er_citation_names"]="This study"
            MagRec["magic_method_codes"]=meas_type
            if demag=="AF":
                MagRec["treatment_ac_field"]='%8.3e' %(float(rec[0])*1e-3) # peak field in tesla
                meas_type="LT-AF-Z"
                MagRec["treatment_dc_field"]='0'
            elif demag=="T":
                MagRec["treatment_temp"]='%8.3e' % (float(rec[0])+273.) # temp in kelvin
                meas_type="LT-T-Z"
            MagRec['magic_method_codes']=meas_type
#            date=rec[5].strip('"').split('-')
#            time=rec[6].strip('"')
#            if int(date[0])<50:  # assume this century 
#                yyyy='20'+date[0]
#            else:
#                yyyy='19'+date[0] # assume last century
#            dstring=yyyy+':'+date[1]+':'+date[2]+":"+time
#            MagRec['measurement_date']=dstring
            MagRecs.append(MagRec) 
    MagOuts=pmag.measurements_methods(MagRecs,0)
    pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    print "Measurements put in ",meas_file
    pmag.magic_write(samp_file,SampOut,"er_samples")
    print "Sample orientation info  saved in ", samp_file
    print "Good bye"
Пример #8
0
def main(command_line=True, **kwargs):
    """
    NAME
        IODP_jr6_magic.py
 
    DESCRIPTION
        converts shipboard .jr6 format files to magic_measurements format files

    SYNTAX
        IODP_jr6_magic.py [command line options]

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

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

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

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

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

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

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

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

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

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

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

        else:
            print "sample name not found: ", row["specname"]
    MagOuts = pmag.measurements_methods(MagRecs, noave)
    file_created, error_message = pmag.magic_write(meas_file, MagOuts, "magic_measurements")
    if file_created:
        return True, meas_file
    else:
        return False, "Results not written to file"
Пример #9
0
def main():
    """
    NAME
        CIT_magic.py
 
    DESCRIPTION
        converts CIT and .sam  format files to magic_measurements format files

    SYNTAX
        CIT_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify .sam format input file, required
        -fsi SITEFILE : specify file with site names and locations [tab delimited magic file]
        -F FILE: specify output  measurements file, default is magic_measurements.txt
        -Fsp FILE: specify output er_specimens.txt file, default is er_specimens.txt
        -Fsi FILE: specify output er_sites.txt file, default is er_sites.txt
        -Fsa FILE: specify output er_samples.txt file, default is er_samples.txt  # LORI
        -n [gm,kg,cc,m3]: specify normalization
        -A: don't average replicate measurements
        -spc NUM : specify number of characters to designate a  specimen, default = 0
        -ncn NCON: specify naming convention
        -loc LOCNAME : specify location/study name, must have either LOCNAME or SITEFILE or be a synthetic
        -mcd [FS-FD:SO-MAG,.....] colon delimited list for method codes applied to all specimens in .sam file
        -dc B PHI THETA: dc lab field (in micro tesla) and phi,theta, default is none
              NB: use PHI, THETA = -1 -1 to signal that it changes, i.e. in anisotropy experiment
        -ac B : peak AF field (in mT) for ARM acquisition, default is none

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

    NOTES:
         Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length) [default]
            [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.
 
    """
    #
    #
    norm = 'cc'
    samp_con, Z = '3', 1
    meas_file = 'magic_measurements.txt'
    spec_file = 'er_specimens.txt'
    samp_file = 'er_samples.txt'
    site_file = 'er_sites.txt'
    ErSpecs, ErSamps, ErSites, ErLocs, ErCits = [], [], [], [], []
    MeasRecs = []
    specnum, units, locname = 0, "1", "unknown"
    citation = "This study"
    dir_path = '.'
    args = sys.argv
    if '-WD' in args:
        ind = args.index("-WD")
        dir_path = args[ind + 1]
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-usr" in args:
        ind = args.index("-usr")
        user = args[ind + 1]
    if '-F' in args:
        ind = args.index("-F")
        meas_file = args[ind + 1]
    if '-Fsp' in args:
        ind = args.index("-Fsp")
        spec_file = args[ind + 1]
    if '-Fsa' in args:
        ind = args.index("-Fsa")
        samp_file = args[ind + 1]
    if '-Fsi' in args:  # LORI addition
        ind = args.index("-Fsi")
        site_file = args[ind + 1]
    if '-loc' in args:
        ind = args.index("-loc")
        locname = args[ind + 1]
    if '-mcd' in args:
        ind = args.index("-mcd")
        methods = args[ind + 1]
    else:
        methods = 'SO-MAG'
    if '-spc' in args:
        ind = args.index("-spc")
        specnum = -int(args[ind + 1])
    if '-n' in args:
        ind = args.index("-n")
        norm = args[ind + 1]
    if "-A" in args:
        avg = 1
    else:
        avg = 0
    if "-ncn" in args:
        ind = args.index("-ncn")
        samp_con = sys.argv[ind + 1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print "option [4] must be in form 3-Z where Z is an integer"
                sys.exit()
            else:
                Z = samp_con.split("-")[1]
                samp_con = "4"
    if '-f' in args:
        ind = args.index("-f")
        magfile = args[ind + 1]
    # LJ
    if '-ID' in args:
        ind = args.index('-ID')
        input_dir_path = args[ind + 1]
    else:
        input_dir_path = dir_path
    output_dir_path = dir_path
    # LJ
    magfile = input_dir_path + '/' + magfile
    spec_file = output_dir_path + '/' + spec_file
    samp_file = output_dir_path + '/' + samp_file
    site_file = output_dir_path + '/' + site_file
    meas_file = output_dir_path + '/' + meas_file
    try:
        input = open(magfile, 'r')
    except Exception as ex:
        print "bad sam file name"
        sys.exit()
    File = input.readlines()
    sids, ln, format = [], 0, 'CIT'
    formats = ['CIT', '2G', 'APP', 'JRA']
    ErLocRec = {}
    ErLocRec["er_location_name"] = locname
    ErLocRec["er_citation_name"] = citation
    comment = File[ln]
    if comment == 'CIT':
        format = comment
        ln += 1
    comment = File[ln]
    print comment
    ln += 1
    specimens, samples, sites = [], [], []
    if format == 'CIT':
        line = File[ln].split()
        site_lat = line[0]
        site_lon = line[1]
        ErLocRec["location_begin_lat"] = site_lat
        ErLocRec["location_begin_lon"] = site_lon
        ErLocRec["location_end_lat"] = site_lat
        ErLocRec["location_end_lon"] = site_lon
        ErLocs.append(ErLocRec)
        Cdec = float(line[2])
        for k in range(ln + 1, len(File)):
            line = File[k]
            rec = line.split()
            specimen = rec[0]
            specimens.append(specimen)
    for specimen in specimens:
        ErSpecRec, ErSampRec, ErSiteRec = {}, {}, {}
        if specnum != 0:
            sample = specimen[:specnum]
        else:
            sample = specimen
        site = pmag.parse_site(sample, samp_con, Z)
        ErSpecRec['er_specimen_name'] = specimen
        ErSpecRec['er_sample_name'] = sample
        ErSpecRec['er_site_name'] = site
        ErSpecRec['er_location_name'] = locname
        ErSpecRec['er_citation_name'] = citation
        ErSampRec['er_sample_name'] = sample
        ErSampRec['er_site_name'] = site
        ErSampRec['er_location_name'] = locname
        ErSampRec['er_citation_name'] = citation
        ErSampRec['magic_method_codes'] = methods
        ErSampRec['sample_declination_correction'] = '%7.1f' % (Cdec)
        ErSiteRec['er_site_name'] = site
        ErSiteRec['er_location_name'] = locname
        ErSiteRec['er_citation_name'] = citation
        ErSiteRec['site_lat'] = site_lat
        ErSiteRec['site_lon'] = site_lon
        f = open(input_dir_path + '/' + specimen, 'rU')
        Lines = f.readlines()
        comment = ""
        line = Lines[0].split()
        if len(line) > 2: comment = line[2]
        info = Lines[1].split()
        vol = float(info[-1])
        if vol != 1.0:
            if norm == 'cc': units = "1"
            if norm == 'm3': units = "2"
            ErSampRec['specimen_weight'] = ""
            if units == "1" or "":
                ErSampRec['specimen_volume'] = '%10.3e' % (vol * 1e-6)
            else:
                ErSampRec['specimen_volume'] = '%10.3e' % (vol)
        else:
            if norm == 'cc': units = "1"
            if norm == 'm3': units = "2"
            ErSampRec['specimen_volume'] = ""
            if units == "1" or "":
                ErSampRec['specimen_weight'] = '%10.3e' % (vol * 1e-3)
            else:
                ErSampRec['specimen_weight'] = '%10.3e' % (vol)
        dip = float(info[-2])
        dip_direction = float(info[-3]) + Cdec + 90.
        sample_dip = -float(info[-4])
        sample_azimuth = float(info[-5]) + Cdec - 90.
        if len(info) > 5:
            ErSampRec['sample_height'] = info[-6]
        else:
            ErSampRec['sample_height'] = '0'
        ErSampRec['sample_azimuth'] = '%7.1f' % (sample_azimuth)
        ErSampRec['sample_dip'] = '%7.1f' % (sample_dip)
        ErSampRec['sample_bed_dip'] = '%7.1f' % (dip)
        ErSampRec['sample_bed_dip_direction'] = '%7.1f' % (dip_direction)
        ErSampRec['sample_class'] = ''
        ErSampRec['sample_type'] = ''
        ErSampRec['sample_lithology'] = ''
        if Cdec != 0 or Cdec != "":
            ErSampRec['magic_method_codes'] = 'SO-CMD-NORTH'
        else:
            ErSampRec['magic_method_codes'] = 'SO-MAG'
        for line in Lines[2:len(Lines) - 1]:
            MeasRec = ErSpecRec.copy()
            treat_type = line[0:3]
            treat = line[3:6]
            if treat_type.strip(
            ) == 'NRM' or treat == '   ':  # reverting back.  otherwise breaks examples.
                MeasRec['magic_method_codes'] = 'LT-NO'
                MeasRec['measurement_temp'] = '273'
                MeasRec['treatment_temp'] = '273'
                MeasRec['treatment_dc_field'] = '0'
                MeasRec['treatment_ac_field'] = '0'
            elif treat_type.strip() == 'AF':
                MeasRec['magic_method_codes'] = 'LT-AF-Z'
                MeasRec['measurement_temp'] = '273'
                MeasRec['treatment_temp'] = '273'
                MeasRec['treatment_dc_field'] = '0'
                MeasRec['treatment_ac_field'] = '%10.3e' % (float(treat) *
                                                            1e-3)
            elif treat_type.strip() == 'TT':
                MeasRec['magic_method_codes'] = 'LT-T-Z'
                MeasRec['measurement_temp'] = '273'
                MeasRec['treatment_temp'] = '%7.1f' % (float(treat) + 273)
                MeasRec['treatment_dc_field'] = '0'
                MeasRec['treatment_ac_field'] = '0'
            elif treat_type.strip() == 'LT' or treat_type.strip() == 'LN2':
                MeasRec['magic_method_codes'] = 'LT-LT-Z'
                MeasRec['measurement_temp'] = '273'
                MeasRec['treatment_temp'] = '77'
                MeasRec['treatment_dc_field'] = '0'
                MeasRec['treatment_ac_field'] = '0'
            else:
                print "trouble with your treatment steps"
            MeasRec['measurement_dec'] = line[46:51]
            MeasRec['measurement_inc'] = line[52:58]
            M = '%8.2e' % (float(line[31:39]) * vol * 1e-3)  # convert to Am2
            MeasRec['measurement_magn_moment'] = M
            MeasRec['measurement_x_sd'] = '%8.2e' % (
                float(line[58:67]) * 1e-8)  #(convert e-5emu to Am2)
            MeasRec['measurement_y_sd'] = '%8.2e' % (float(line[67:76]) * 1e-8)
            MeasRec['measurement_z_sd'] = '%8.2e' % (float(line[76:85]) * 1e-8)
            MeasRec['measurement_csd'] = '%7.1f' % (eval(line[41:46]))
            MeasRec['magic_instrument_codes'] = line[85:]
            MeasRec["measurement_positions"] = '1'

            MeasRecs.append(MeasRec)
        ErSpecs.append(ErSpecRec)
        if sample not in samples:
            samples.append(sample)
            ErSamps.append(ErSampRec)
        site = pmag.parse_site(sample, samp_con, Z)
        if site not in sites:
            sites.append(site)
            ErSites.append(ErSiteRec)
    pmag.magic_write(spec_file, ErSpecs, 'er_specimens')
    print 'specimens stored in ', spec_file
    pmag.magic_write(samp_file, ErSamps, 'er_samples')
    print 'samples stored in ', samp_file
    pmag.magic_write(site_file, ErSites, 'er_sites')
    print 'sites stored in ', site_file
    Fixed = pmag.measurements_methods(MeasRecs, avg)
    pmag.magic_write(meas_file, Fixed, 'magic_measurements')
    print 'data stored in ', meas_file
Пример #10
0
def main():
    """
    NAME
        ODP_spn_magic.py
 
    DESCRIPTION
        converts ODP's Molspin's .spn format files to magic_measurements format files

    SYNTAX
        ODP_spn_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -f FILE: specify .spn format input file, required
        -F FILE: specify output file, default is magic_measurements.txt
        -LP [AF, T, A FIELD, I N] specify one (FIELD is DC field in uT) 
            AF:  af demag
            T: thermal 
            A: anhysteretic remanence 
            I: isothermal remanence 
            N: NRM only
        -v  vol , specify volume used in MolSpin program in cm^3
        -A: don't average replicate measurements
 
    INPUT
        Best to put separate experiments (all AF, thermal, ARM, etc. files in
           seperate .spn files 

        Format of  .spn files:   
        header with: 
       Leg Sit H Cor T Sec Top Bot      Dec     Inc     Intens   Demag. Stage
followed by data
        Leg: Expedition number 
        Sit: is ODP Site
        H: Hole letter 
        Cor: Core number
        T:  Core type (R,H,X,etc.)
        Sec: section number
        top:  top of sample interval
        bot: bottom of sample interval
        Intens in mA/m
        Demag Stage:
            XXX T in Centigrade
            XXX AF in mT
    """
    # initialize some stuff
    noave = 0
    methcode, inst = "", ""
    phi, theta, peakfield, labfield = 0, 0, 0, 0
    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"
    trm = 0
    irm = 0
    dc = "0"
    dir_path = "."
    #
    # get command line arguments
    #
    meas_file = "magic_measurements.txt"
    user = ""
    if "-WD" in args:
        ind = args.index("-WD")
        dir_path = args[ind + 1]
    samp_file = dir_path + "/" + "er_samples.txt"
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-F" in args:
        ind = args.index("-F")
        meas_file = args[ind + 1]
    if "-f" in args:
        ind = args.index("-f")
        mag_file = dir_path + "/" + args[ind + 1]
        try:
            input = open(mag_file, "rU")
        except:
            print "bad mag file name"
            sys.exit()
    else:
        print "spn_file field is required option"
        print main.__doc__
        sys.exit()
    vol = 10.5e-6  # default for spinner program
    if "-V" in args:
        ind = args.index("-V")
        vol = float(args[ind + 1]) * 1e-6  # convert volume to m^3
    if "-A" in args:
        noave = 1
    if "-LP" in args:
        ind = args.index("-LP")
        codelist = args[ind + 1]
        codes = codelist.split(":")
        if "AF" in codes:
            demag = "AF"
            methcode = "LT-AF-Z"
        if "T" in codes:
            demag = "T"
            methcode = "LT-T-Z"
        if "I" in codes:
            methcode = "LP-IRM"
        if "A" in codes:
            methcode = "LT-AF-I"
            dc = "%10.3e" % (1e-3 * float(args[ind + 1]))
    MagRecs = []
    version_num = pmag.get_version()
    meas_file = dir_path + "/" + meas_file
    for line in input.readlines():
        instcode = "ODP-MSPN"
        rec = line.split()
        if len(rec) > 2 and "Leg" not in line:
            MagRec = {}
            MagRec["er_expedition_name"] = rec[0]
            MagRec["er_location_name"] = rec[1] + rec[2]
            MagRec["er_specimen_name"] = (
                rec[0]
                + "-"
                + "U"
                + rec[1]
                + rec[2].upper()
                + "-"
                + rec[3]
                + rec[4].upper()
                + "-"
                + rec[5]
                + "-"
                + "W"
                + "-"
                + rec[6]
            )
            MagRec["er_site_name"] = MagRec["er_specimen_name"]
            MagRec["er_sample_name"] = MagRec["er_specimen_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"] = dc
            MagRec["treatment_dc_field_phi"] = "0"
            MagRec["treatment_dc_field_theta"] = "0"
            meas_type = "LT-NO"
            if float(rec[11]) == 0:
                pass
            elif demag == "AF":
                MagRec["treatment_ac_field"] = "%8.3e" % (float(rec[11]) * 1e-3)  # peak field in tesla
                meas_type = "LT-AF-Z"
                MagRec["treatment_dc_field"] = "0"
            else:
                MagRec["treatment_temp"] = "%8.3e" % (float(rec[11]) + 273.0)  # temp in kelvin
                meas_type = "LT-T-Z"
            intens = 1e-3 * float(rec[10]) * vol  # convert mA/m to Am^2
            MagRec["measurement_magn_moment"] = "%10.3e" % (intens)
            MagRec["measurement_dec"] = rec[8]
            MagRec["measurement_inc"] = rec[9]
            MagRec["magic_instrument_codes"] = "ODP-MSPN"
            MagRec["er_analyst_mail_names"] = user
            MagRec["er_citation_names"] = citation
            MagRec["magic_method_codes"] = meas_type
            MagRec["measurement_flag"] = "g"
            MagRec["measurement_csd"] = ""
            MagRec["measurement_number"] = "1"
            MagRecs.append(MagRec)
    MagOuts = pmag.measurements_methods(MagRecs, noave)
    pmag.magic_write(meas_file, MagOuts, "magic_measurements")
    print "results put in ", meas_file
Пример #11
0
def main():
    """
    NAME
        UR_jr6_magic.py
 
    DESCRIPTION
        converts University of Rome JR6 format files to magic_measurements format files

    SYNTAX
        UR_jr6_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -F FILE: specify output  measurements file, default is magic_measurements.txt
        -Fsa FILE: specify output er_samples.txt file for appending, default is er_samples.txt
        -dc B PHI THETA: dc lab field (in micro tesla) and phi,theta, default is none
              NB: use PHI, THETA = -1 -1 to signal that it changes, i.e. in anisotropy experiment
        -ac B : peak AF field (in mT) for ARM acquisition, default is none
        -A : don't average replicate measurements
        -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
        -mcd: specify sampling method codes as a colon delimited string:  [default is: FS-FD:SO-POM:SO-SUN]
             FS-FD field sampling done with a drill
             FS-H field sampling done with hand samples
             FS-LOC-GPS  field location done with GPS
             FS-LOC-MAP  field location done with map
             SO-POM   a Pomeroy orientation device was used 
             SO-SUN   orientations are from a sun compass
             SO-MAG   orientations are from a magnetic compass
             SO-MAG-CMD   orientations declination corrected magnetic compass
        -ncn NCON:  specify naming convention: default is #1 below
       Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name same as sample
            [6] site is entered under a separate column
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.

            [8] synthetic - has no site name
            [9] ODP naming convention


    INPUT
        Put data from separate experiments (all AF, thermal, thellier, trm aquisition, Shaw, etc.)  in separate directory

    """
#        
#
    version_num=pmag.get_version()
    er_location_name=""
    ErSpecs,ErSamps,ErSites,ErLocs,ErCits=[],[],[],[],[]
    MagRecs=[]
    citation="This study"
    dir_path,demag='.','NRM'
    args=sys.argv
    noave=0
    specnum=0
    sampmeths='FS-FD:SO-POM:SO-SUN'
    samp_con,Z="4","2"
    if '-WD' in args:
        ind=args.index("-WD")
        dir_path=args[ind+1]
    meas_file=dir_path+'/'+'magic_measurements.txt'
    samp_file=dir_path+'/'+'er_samples.txt'
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-A" in args: noave=1
    if '-F' in args:
        ind=args.index("-F")
        meas_file=dir_path+'/'+args[ind+1]
    if '-Fsa' in args:
        ind=args.index("-Fsa")
        samp_file=dir_path+'/'+args[ind+1]
        ErSamps,file_type=pmag.magic_read(samp_file)
    if "-ncn" in args:
        ind=args.index("-ncn")
        samp_con=sys.argv[ind+1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print "option [4] must be in form 4-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="4"
        if "7" in samp_con:
            if "-" not in samp_con:
                print "option [4] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="4"
    if "-mcd" in args:
        ind=args.index("-mcd")
        sampmeths=(sys.argv[ind+1])
    if "-loc" in args:
        ind=args.index("-loc")
        er_location_name=args[ind+1]
    if "-spc" in args:
        ind=args.index("-spc")
        specnum=int(args[ind+1])
        if specnum!=0:specnum=-specnum
    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
    filelist=os.listdir(dir_path) # read in list of files to import
    samples=[]
    MagRecs,SampRecs=[],[]
    for samp in ErSamps:
        if samp['er_sample_name'] not in samples:
            samples.append(samp['er_sample_name'])
            SampRecs.append(samp)
    for file in filelist: # parse each file
        parts=file.split('.')
        if parts[1].upper()=='JR6':
            print 'processing: ',file
            treatment_type,treatment_value,user="","",""
            inst="UR-JR6"
            input=open(dir_path+'/'+file,'rU').readlines()
            for line in input:
                newline=line.replace('-',' -')
                rec=newline.split()
                MagRec,SampRec={},{}
                specimen=rec[0]
                if specnum!=0:
                    SampRec['er_sample_name']=specimen[:specnum]
                else:
                    SampRec['er_sample_name']=specimen
                er_site_name=pmag.parse_site(SampRec['er_sample_name'],samp_con,Z)
                SampRec['er_site_name']=er_site_name
                SampRec['er_location_name']=er_location_name
                for key in SampRec.keys():MagRec[key]=SampRec[key]
                SampRec['sample_azimuth']=rec[7]
                SampRec['sample_dip']='-'+rec[8]
                SampRec['sample_bed_dip_direction']=rec[9]
                SampRec['sample_bed_dip']=rec[10]
                SampRec['magic_method_codes']=sampmeths
                MagRec['er_specimen_name']=specimen
                MagRec['er_analyst_mail_names']=user
                MagRec['magic_software_packages']=version_num
                MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
                MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
                MagRec["treatment_ac_field"]='0'
                MagRec["treatment_dc_field"]='0'
                MagRec["treatment_dc_field_phi"]='0'
                MagRec["treatment_dc_field_theta"]='0'
                MagRec["measurement_flag"]='g' # assume all data are "good"
                MagRec["measurement_standard"]='u' # assume all data are "good"
                MagRec["measurement_csd"]='' # set csd to blank
                MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
                MagRec['magic_method_codes']='LT-NO'
                if rec[2]=='C':
                    temp=float(rec[1])+273.     
                    if temp>298: 
                        MagRec["treatment_temp"]='%8.3e' % (temp) # room temp in kelvin
                        MagRec['magic_method_codes']='LT-T-Z'
                else: # measurement is in oe
                    AC=float(rec[1])*1e-4 # convert to tesla
                    if AC!=0.:
                        MagRec["treatment_ac_field"]='%8.3e' %(AC)
                        MagRec['magic_method_codes']='LT-AF-Z'
                vol=10.8*1e-6 # standard Roma lab volume
                MagRec['magic_instrument_codes']=inst
                MagRec['measurement_number']='1'
                mexp=10**(float(rec[6]))
                x,y,z=mexp*float(rec[3]),mexp*float(rec[4]),mexp*float(rec[5])
                Cart=[x,y,z]
                Dir=pmag.cart2dir(Cart)
                MagRec['measurement_dec']='%7.1f'%(Dir[0])
                MagRec['measurement_inc']='%7.1f'%(Dir[1])
                MagRec['measurement_magn_volume']='%8.3e'%(Dir[2])
                MagRec['measurement_magn_moment']='%8.3e'%(Dir[2]*vol)
                MagRec['measurement_description']='converted A/m to Am^2 using volume of '+str(vol)+' m^3'
                MagRecs.append(MagRec)
                if MagRec['er_sample_name'] not in samples:
                    samples.append(MagRec['er_sample_name'])
                    SampRecs.append(SampRec)
    if len(SampRecs)>0:
        SampOut,keys=pmag.fillkeys(SampRecs)
        pmag.magic_write(samp_file,SampOut,'er_samples')
        print 'samples stored in ',samp_file
    Fixed=pmag.measurements_methods(MagRecs,noave)
    pmag.magic_write(meas_file,Fixed,'magic_measurements')
    print 'data stored in ',meas_file
Пример #12
0
def main(command_line=True, **kwargs):
    """
    NAME
        2G_bin_magic.py
   
    DESCRIPTION
        takes the binary 2G format magnetometer files and converts them to magic_measurements, er_samples.txt and er_sites.txt file
 
    SYNTAX
        2G_bin_magic.py [command line options]

    OPTIONS
        -f FILE: specify input 2G (binary) file
        -F FILE: specify magic_measurements output file, default is: magic_measurements.txt
        -Fsa FILE: specify output file, default is: er_samples.txt 
        -Fsi FILE: specify output file, default is: er_sites.txt 
        -ncn NCON:  specify naming convention: default is #2 below
        -ocn OCON:  specify orientation convention, default is #5 below
        -mcd: specify sampling method codes as a colon delimited string:  [default is: FS-FD:SO-POM]
             FS-FD field sampling done with a drill
             FS-H field sampling done with hand samples
             FS-LOC-GPS  field location done with GPS
             FS-LOC-MAP  field location done with map
             SO-POM   a Pomeroy orientation device was used
             SO-ASC   an ASC orientation device was used
             SO-MAG   orientation with magnetic compass
             SO-SUN   orientation with sun compass
        -loc: location name, default="unknown"
        -spc NUM : specify number of characters to designate a  specimen, default = 0     
        -ins INST : specify instsrument name
        -a: average replicate measurements

    INPUT FORMAT
        Input files are horrible mag binary format (who knows why?)
        Orientation convention:
            [1] Lab arrow azimuth= mag_azimuth; Lab arrow dip=-field_dip
                i.e., field_dip is degrees from vertical down - the hade [default]
            [2] Lab arrow azimuth = mag_azimuth-90; Lab arrow dip = -field_dip
                i.e., mag_azimuth is strike and field_dip is hade
            [3] Lab arrow azimuth = mag_azimuth; Lab arrow dip = 90-field_dip
                i.e.,  lab arrow same as field arrow, but field_dip was a hade.
            [4] lab azimuth and dip are same as mag_azimuth, field_dip
            [5] lab azimuth is same as mag_azimuth,lab arrow dip=field_dip-90
            [6] Lab arrow azimuth = mag_azimuth-90; Lab arrow dip = 90-field_dip
            [7] all others you will have to either customize your 
                self or e-mail [email protected] for help.  
 
         Magnetic declination convention:
             Az will use supplied declination to correct azimuth 
    
       Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name 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.
 

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

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

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

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

    if ID:
        input_dir_path = ID
    else:
        input_dir_path = dir_path

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

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

    SYNTAX
        FIN_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify index file with list of files for importing [default is index.txt]
        -F FILE: specify output  measurements file, default is magic_measurements.txt
        -Fsp FILE: specify output er_specimens.txt file, default is er_specimens.txt
        -Fsi FILE: specify output er_sites.txt file, default is er_sites.txt
        -n [gm,kg,cc,m3]: specify normalization
        -spc NUM : specify number of characters to designate a  specimen, default = 1
        -ncn NCON: specify naming convention [default is #2 below]
        -loc LOCNAME : specify location/study name, must have either LOCNAME or SITEFILE or be a synthetic
        -dc B PHI THETA: dc lab field (in micro tesla) and phi,theta, default is none
              NB: use PHI, THETA = -1 -1 to signal that it changes, i.e. in anisotropy experiment
        -ac B : peak AF field (in mT) for ARM acquisition, default is none

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

    NOTES:
       Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name 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.
 
 
    """
#        
#
    norm='cc'
    samp_con,Z='2',1        
    meas_file='magic_measurements.txt'
    spec_file='er_specimens.txt'
    samp_file='er_samples.txt'
    site_file='er_sites.txt'
    magfile='index.txt'
    ErSpecs,ErSamps,ErSites,ErLocs,ErCits=[],[],[],[],[]
    MeasRecs=[]
    specnum,unis,locname=-1,"1","unknown"
    citation="This study"
    dir_path='.'
    args=sys.argv
    labfield,phi,theta="","",""
    if '-WD' in args:
        ind=args.index("-WD")
        dir_path=args[ind+1]
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-usr" in args:
        ind=args.index("-usr")
        user=args[ind+1]
    if '-F' in args:
        ind=args.index("-F")
        meas_file=args[ind+1]
    if '-Fsp' in args:
        ind=args.index("-Fsp")
        spec_file=args[ind+1]
    if '-Fsa' in args:
        ind=args.index("-Fsa")
        samp_file=args[ind+1]
    if '-loc' in args:
        ind=args.index("-loc")
        locname=args[ind+1]
    if '-spc' in args:
        ind=args.index("-spc")
        specnum=-int(args[ind+1])
    if '-dc' in args:
        ind=args.index("-dc")
        labfield=float(args[ind+1])*1e-6
        phi=args[ind+2]
        theta=args[ind+3]
    if '-n' in args:
        ind=args.index("-n")
        norm=args[ind+1]
    if "-ncn" in args:
        ind=args.index("-ncn")
        samp_con=sys.argv[ind+1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print "option [4] must be in form 4-Z where Z is an integer"
                sys.exit()
            else: 
                Z=samp_con.split("-")[1]
                samp_con="4"
        if "7" in samp_con:
            if "-" not in samp_con:
                print "option [7] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="7"
    if '-f' in args:
        ind=args.index("-f")
        magfile=args[ind+1]
    magfile=dir_path+'/'+magfile
    spec_file=dir_path+'/'+spec_file
    samp_file=dir_path+'/'+samp_file
    site_file=dir_path+'/'+site_file
    meas_file=dir_path+'/'+meas_file
    try:
        input=open(magfile,'rU')
    except:
        print "bad sam file name"
        sys.exit()
    Files=input.readlines()
    sids,ln=[],0
    specimens,samples,sites=[],[],[]
    for file in Files:
        Zsteps,Isteps=[],[]
        print 'processing ',file.split()[0]
        data=open(file.split()[0],'rU').readlines()
# parse out header info
        line= data[0]
        k=6
        while k<len(line) and line[k]==' ':k+=1
        inst=line[k:]
        line= data[1]
        rec=line.split(':')
        specimen=rec[1].split()[0].lower()
        if specnum!=0:
            sample=specimen[:specnum]
        else: sample=specimen
        site=pmag.parse_site(sample,samp_con,Z)
        line= data[2]
        rec=line.split(':')
        lithology=rec[1]
        line=data[7]
        rec=line.split() 
        site_lat=rec[0]
        site_lon=rec[1]
#        sample_azimuth=(float(rec[2])-90.)
#        if sample_azimuth<0:sample_azimuth+=360.
##        sample_dip=(90.-float(rec[3]))
        dip=float(rec[5])
        if dip!=0:
            dip_direction=(float(rec[4])-90.)
        else:
            dip_direction=0
        if dip_direction<0:dip_direction+=360.
        sample_azimuth=0
        sample_dip=0 
## the data are in geographic coordinates already
        vol=float(rec[6])
        weight=float(rec[7])
        line= data[8]
        treat_type=line[:2]
        ErSpecRec,ErSampRec,ErSiteRec={},{},{}
        ErSpecRec['er_specimen_name']=specimen
        ErSpecRec['er_sample_name']=sample
        ErSpecRec['er_site_name']=site
        ErSpecRec['er_location_name']=locname
        ErSpecRec['er_citation_name']=citation
        ErSampRec['er_sample_name']=sample
        ErSampRec['er_site_name']=site
        ErSampRec['er_location_name']=locname
        ErSampRec['er_citation_name']=citation
        ErSiteRec['sample_lat']=site_lat
        ErSiteRec['sample_lon']=site_lon
        ErSiteRec['er_site_name']=site
        ErSiteRec['er_location_name']=locname
        ErSiteRec['er_citation_name']=citation
        ErSiteRec['site_lat']=site_lat
        ErSiteRec['site_lon']=site_lon
        if weight!=0:
            ErSpecRec['specimen_weight']='%10.3e'%(weight*1e-3) # convert to kg
        else: 
            ErSpecRec['specimen_weight']=""
        if vol!=0:
            if norm=='cc':units="1"
            if norm=='m3':units="2"
            if units=="1" or "":
                ErSpecRec['specimen_volume']='%10.3e'%(vol*1e-6)
            else: 
                ErSpecRec['specimen_volume']='%10.3e'%(vol)
        else:
            ErSpecRec['specimen_volume']=""
        ErSampRec['sample_azimuth']='%7.1f'%(sample_azimuth)
        ErSampRec['sample_dip']='%7.1f'%(sample_dip)
        ErSampRec['sample_bed_dip']='%7.1f'%(dip)
        ErSampRec['sample_bed_dip_direction']='%7.1f'%(dip_direction)
        ErSampRec['sample_class']=''
        ErSampRec['sample_type']=''
        ErSampRec['sample_lithology']=lithology
        ErSampRec['magic_method_codes']="SO-CMD-NORTH"
        lastrec=data[-1]
        while len(lastrec.split())<2: # stripping off blanks at end
            del data[-1]
            lastrec=data[-1]
        for line in data[9:]:
            MeasRec=ErSpecRec.copy()
            MeasRec['measurement_standard']='u'
            MeasRec['measurement_temp']='273'
            rec=line.split()
            if len(rec)==1:
                rec=line.split('\t')
            treat=rec[0].strip()
            if treat=="0":
                MeasRec['magic_method_codes']='LT-NO'
                MeasRec['treatment_temp']='273'
                MeasRec['treatment_dc_field']='0'
                MeasRec['treatment_dc_field_phi']=''
                MeasRec['treatment_dc_field_theta']=''
                MeasRec['treatment_ac_field']='0'
            else:
                if treat_type.strip()=='AF':
                    MeasRec['magic_method_codes']='LT-AF-Z'
                    MeasRec['treatment_temp']='273'
                    MeasRec['treatment_dc_field']='0'
                    MeasRec['treatment_dc_field_phi']=''
                    MeasRec['treatment_dc_field_theta']=''
                    MeasRec['treatment_ac_field']='%10.3e'%(float(treat)*1e-4) # convert from Oe
                elif treat_type.strip()=='TH':
                    if labfield=="":
                        MeasRec['magic_method_codes']='LT-T-Z'
                        MeasRec['treatment_temp']='%7.1f'%(float(treat)+273)
                        MeasRec['treatment_dc_field']='0'
                        MeasRec['treatment_dc_field_phi']=''
                        MeasRec['treatment_dc_field_theta']=''
                        MeasRec['treatment_ac_field']='0'
                    else:
                        if treat not in Zsteps:
                            print 'first Z: ',treat
                            Zsteps.append(treat)
                            MeasRec['magic_method_codes']='LT-T-Z'
                            MeasRec['treatment_temp']='%7.1f'%(float(treat)+273)
                            MeasRec['treatment_dc_field']='0'
                            MeasRec['treatment_dc_field_phi']=''
                            MeasRec['treatment_dc_field_theta']=''
                            MeasRec['treatment_ac_field']='0'
                        elif treat not in Isteps:
                            print 'first I: ',treat
                            Isteps.append(treat)
                            MeasRec['magic_method_codes']='LT-T-I'
                            MeasRec['treatment_temp']='%7.1f'%(float(treat)+273)
                            MeasRec['treatment_dc_field']='%10.3e'%(labfield)
                            MeasRec['treatment_dc_field_phi']=phi
                            MeasRec['treatment_dc_field_theta']=theta
                            MeasRec['treatment_ac_field']='0'
                        elif treat in Isteps:
                            if float(treat)<float(Zsteps[-1]):
                                print 'pTRM ',treat
                                MeasRec['magic_method_codes']='LT-PTRM-Z'
                                MeasRec['treatment_temp']='%7.1f'%(float(treat)+273)
                                MeasRec['treatment_dc_field']=''
                                MeasRec['treatment_dc_field_phi']=''
                                MeasRec['treatment_dc_field_theta']=''
                                MeasRec['treatment_ac_field']='0'
                            else:
                                print 'pTRM tail ',treat
                                MeasRec['magic_method_codes']='LT-PTRM-MD'
                                MeasRec['treatment_temp']='%7.1f'%(float(treat)+273)
                                MeasRec['treatment_dc_field']=''
                                MeasRec['treatment_dc_field_phi']=''
                                MeasRec['treatment_dc_field_theta']=''
                                MeasRec['treatment_ac_field']='0'
            MeasRec['measurement_dec']=rec[1]
            MeasRec['measurement_inc']=rec[2]
            intens='%8.2e'%(float(rec[3])*1e-3) # convert to A/m 
            MeasRec['measurement_magn_volume']=intens
            MeasRec['measurement_csd']='%7.1f'%(eval(rec[5]))
            MeasRec['magic_instrument_codes']=inst
            MeasRecs.append(MeasRec)
        ErSpecs.append(ErSpecRec)
        if sample not in samples:
            samples.append(sample)
            ErSamps.append(ErSampRec)
        if site not in sites:
            sites.append(site)
            ErSites.append(ErSiteRec)
    pmag.magic_write(spec_file,ErSpecs,'er_specimens')
    print 'specimens stored in ',spec_file
    pmag.magic_write(samp_file,ErSamps,'er_samples')
    print 'samples stored in ',samp_file
    pmag.magic_write(site_file,ErSites,'er_sites')
    Fixed=pmag.measurements_methods(MeasRecs,1)
    pmag.magic_write(meas_file,Fixed,'magic_measurements')
    print 'data stored in ',meas_file
Пример #14
0
def main():
    """
    NAME
        IPG_magic.py
 
    DESCRIPTION
        converts PMD (IPG - PaleoMac)  format files to magic_measurements format files

    SYNTAX
        IPG_magic.py [command line options]

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

    SYNTAX
        MINI_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify input file, required
        -F FILE: specify output file, default is magic_measurements.txt
        -LP [colon delimited list of protocols, include all that apply]
            AF:  af demag
            T: thermal including thellier but not trm acquisition
        -spc NUM : specify number of characters to designate a  specimen, default = 0
        -ncn NCON:  specify naming convention: default is #1 below
        -A: don't average replicate measurements
        -vol: volume assumed for measurement in cm^3
       Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name same as sample
            [6] site is entered under a separate column
            [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 
            [10] LL-SI-SA-SP_STEP where LL is location, SI is site, SA is sample and SP is specimen and STEP is demagnetization step

    INPUT
        Must put separate experiments (all AF, thermal,  etc.) in 
           seperate files 

        Format of Yale MINI files:   
        LL-SI-SP_STEP, Declination, Inclination, Intensity (mA/m), X,Y,Z
        
     
    """
# initialize some stuff
    noave=0
    methcode,inst="LP-NO",""
    samp_con,Z='10',1
    demag="N"
    er_location_name=""
    citation='This study'
    args=sys.argv
    coil=""
    volume=10e-6 # assume a volume of 10cc
#
# get command line arguments
#
    user=""
    dir_path='./'
    if '-WD' in args:
        ind=args.index('-WD')
        dir_path=args[ind+1]
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-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]
    else:
        meas_file=dir_path+'magic_measurements.txt'
    if '-f' in args:
        ind=args.index("-f")
        magfile=dir_path+args[ind+1]
        try:
            input=open(magfile,'rU')
        except:
            print "bad mag file name"
            sys.exit()
    else: 
        print "mag_file field is required option"
        print main.__doc__
        sys.exit()
    specnum=0
    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 "-ins" in args:
        ind=args.index("-ins")
        inst=args[ind+1]
    if "-vol" in args:
        ind=args.index("-vol")
        volume=1e-6*float(args[ind+1])
    if "-A" in args: noave=1
    if "-ncn" in args:
        ind=args.index("-ncn")
        samp_con=sys.argv[ind+1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print "option [4] must be in form 4-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="4"
        if "7" in samp_con:
            if "-" not in samp_con:
                print "option [7] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="4"
    if '-LP' in args:
        ind=args.index("-LP")
        codelist=args[ind+1]
        codes=codelist.split(':')
        if "AF" in codes:
            demag='AF' 
            methcode="LT-AF-Z"
        if "T" in codes:
            demag="T"
    MagRecs=[]
    version_num=pmag.get_version()
    for line in input.readlines():
        rec=line.split(',')
        if len(rec)>1:
            MagRec={}
            IDs=rec[0].split('_')
            treat=IDs[1]
            MagRec["er_specimen_name"]=IDs[0]
            print MagRec['er_specimen_name']
            sids=IDs[0].split('-')
            MagRec['er_location_name']=sids[0]
            MagRec['er_site_name']=sids[0]+'-'+sids[1]
            if len(sids)==2:
                MagRec["er_sample_name"]=IDs[0]
            else:
                MagRec["er_sample_name"]=sids[0]+'-'+sids[1]+'-'+sids[2]
            print MagRec
            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"
            if demag=="AF":
                MagRec["treatment_ac_field"]='%8.3e' %(float(treat)*1e-3) # peak field in tesla
            if demag=="T":
                meas_type="LT-T-Z"
                MagRec["treatment_dc_field"]='%8.3e'%(0)
                MagRec["treatment_temp"]='%8.3e' % (float(treat)+273.) # temp in kelvin
            if demag=="N":
                meas_type="LT-NO"
                MagRec["treatment_ac_field"]='0'
                MagRec["treatment_dc_field"]='0'
            MagRec["measurement_magn_moment"]='%10.3e'% (volume*float(rec[3])*1e-3) # moment in Am2 (from mA/m)
            MagRec["measurement_dec"]=rec[1]
            MagRec["measurement_inc"]=rec[2]
            MagRec["magic_instrument_codes"]=inst
            MagRec["er_analyst_mail_names"]=user
            MagRec["er_citation_names"]=citation
            MagRec["magic_method_codes"]=methcode.strip(':')
            MagRec["measurement_flag"]='g'
            MagRec["measurement_standard"]='u'
            MagRec["measurement_number"]='1'
            MagRecs.append(MagRec) 
    MagOuts=pmag.measurements_methods(MagRecs,noave)
    pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    print "results put in ",meas_file
Пример #16
0
def main(command_line=True, **kwargs):
    """
    NAME
        IODP_dscr_magic.py
 
    DESCRIPTION
        converts ODP LIMS discrete sample format files to magic_measurements format files


    SYNTAX
        IODP_descr_magic.py [command line options]

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

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

    # format variables

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

    SYNTAX
        JR6_txt_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -f FILE: specify  input file, or
        -F FILE: specify output file, default is magic_measurements.txt
        -Fsa: specify er_samples format file for appending, default is new er_samples.txt (Not working yet)
        -spc NUM : specify number of characters to designate a  specimen, default = 1
        -loc LOCNAME : specify location/study name
        -A: don't average replicate measurements
        -ncn NCON: specify sample naming convention (6 and 7 not yet implemented)
        -mcd [SO-MAG,SO-SUN,SO-SIGHT...] supply how these samples were oriented
        -v NUM : specify the volume of the sample, default 2.5cm^3.
       Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name same as sample
            [6] site is entered under a separate column
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.
 
    INPUT
        JR6 .txt format file
    """
# initialize some stuff
    noave=0
    volume = 2.5 * 1e-6 # default volume is 2.5 cm^3 (2.5 * 1e-6 meters^3)
    inst=""
    samp_con,Z='1',""
    missing=1
    demag="N"
    er_location_name="unknown"
    citation='This study'
    args=sys.argv
    meth_code="LP-NO"
    specnum=-1
    MagRecs=[]
    version_num=pmag.get_version()
    Samps=[] # keeps track of sample orientations

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

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


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

    if not command_line:
        dir_path = kwargs.get('dir_path', '.')
        input_dir_path = kwargs.get('input_dir_path', dir_path)
        output_dir_path = dir_path
        meas_file = kwargs.get('meas_file', 'magic_measurements.txt')
        mag_file = kwargs.get('mag_file')
        samp_file = kwargs.get('samp_file', 'er_samples.txt')
        specnum = kwargs.get('specnum', 1)
        samp_con = kwargs.get('samp_con', '1')
        er_location_name = kwargs.get('er_location_name', '')
        noave = kwargs.get('noave', 0) # default (0) means DO average
        meth_code = kwargs.get('meth_code', "LP-NO")
        volume = float(kwargs.get('volume', 0))
        if not volume:
            volume = 2.5 * 1e-6 #default volume is a 2.5 cm cube, translated to meters cubed
        else:
            #convert cm^3 to m^3
            volume *= 1e-6


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

    ErSampRec,ErSiteRec={},{}

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

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

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

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

        er_specimen_name = sampleName

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

        if int(samp_con)<6:
            er_site_name=pmag.parse_site(er_sample_name,samp_con,Z)
        # else:
        #     if 'er_site_name' in ErSampRec.keys():er_site_name=ErSampRec['er_site_name']
        #     if 'er_location_name' in ErSampRec.keys():er_location_name=ErSampRec['er_location_name']

        # check sample list(SampOuts) to see if sample already exists in list before adding new sample info
        sampleFlag=0
        for sampRec in SampOuts:
            if sampRec['er_sample_name'] == er_sample_name:
                sampleFlag=1
                break
        if sampleFlag == 0:
            ErSampRec['er_sample_name']=er_sample_name
            ErSampRec['sample_azimuth']=specimenAngleDec
            sample_dip=str(float(specimenAngleInc)-90.0) #convert to magic orientation
            ErSampRec['sample_dip']=sample_dip
            ErSampRec['magic_method_codes']=meth_code 
            ErSampRec['er_location_name']=er_location_name
            ErSampRec['er_site_name']=er_site_name
            ErSampRec['er_citation_names']='This study'
            SampOuts.append(ErSampRec.copy())

        MagRec={}
        MagRec['measurement_description']='Date: '+date
        MagRec["er_citation_names"]="This study"
        MagRec['er_location_name']=er_location_name
        MagRec['er_site_name']=er_site_name
        MagRec['er_sample_name']=er_sample_name
        MagRec['magic_software_packages']=version_num
        MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
        MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
        MagRec["measurement_flag"]='g'
        MagRec["measurement_standard"]='u'
        MagRec["measurement_number"]='1'
        MagRec["er_specimen_name"]=er_specimen_name
        MagRec["treatment_ac_field"]='0'
        if demagLevel == 'NRM':
            meas_type="LT-NO"
        elif demagLevel[0] == 'A':
            meas_type="LT-AF-Z"
            treat=float(demagLevel[1:])
            MagRec["treatment_ac_field"]='%8.3e' %(treat*1e-3) # convert from mT to tesla
        elif demagLevel[0] == 'T':
            meas_type="LT-T-Z"
            treat=float(demagLevel[1:])
            MagRec["treatment_temp"]='%8.3e' % (treat+273.) # temp in kelvin
        else:
            print "measurement type unknown", demag_level
            return False, "measurement type unknown"

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

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

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

    MagOuts=pmag.measurements_methods(MagRecs,noave)
    pmag.magic_write(samp_file,SampOuts,'er_samples') 
    print "sample orientations put in ",samp_file
    pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    print "results put in ",meas_file
    return True, meas_file
Пример #18
0
def main():
    """
    NAME
        TAN_magic.py
    
    DESCRIPTION
        import data files from the Tanaka format to magic

    SYNTAX
        TAN_magic.py -h [command line options]
        -h prints help message and quits
        -f FILE input pi or dmg file name
        -F FILE output magic_measuremetns name (default is magic_measurements.txt)
        -lab LABFIELD PHI THETA lab field in microT oriented phi,theta with respect to X
        -loc LOC location name
        -sam SAMP number of characters used to define specimen from sample
        -sit SITE number of characters used to define sample from site
        -cls [i,s,m], igneous, sedimentary or metamorphic [default is igneous]
        -lth [lithology], default is andesite
        -typ [lava flow, sedimentary layer, etc.], default is lava flow
    """
    specnum,sampnum=1,4 # setting defaults for parsing specimen names to sample and site
    labfield,phi,theta='0','0','90'
    meas_file="magic_measurements.txt"
    cl,litho,tp='igneous','andesite','lava flow'
# setting some variables from command line (sys.argv)
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-cls' in sys.argv:
        ind=sys.argv.index('-cls')
        cl=sys.argv[ind+1]
    if '-lth' in sys.argv:
        ind=sys.argv.index('-lth')
        litho=sys.argv[ind+1]
    if '-typ' in sys.argv:
        ind=sys.argv.index('-typ')
        tp=sys.argv[ind+1]
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        file=sys.argv[ind+1]
    if '-F' in sys.argv:
        ind=sys.argv.index('-F')
        meas_file=sys.argv[ind+1]
    if '-loc' in sys.argv:
        ind=sys.argv.index('-loc')
        locname=sys.argv[ind+1]
    if '-lab' in sys.argv:
        ind=sys.argv.index('-lab')
        labfield='%10.3e'%(1e-6*float(sys.argv[ind+1])) # assume labfield in microT
        phi=sys.argv[ind+2]
        theta=sys.argv[ind+3]
    if '-sam' in sys.argv:
        ind=sys.argv.index('-sam')
        specnum=int(sys.argv[ind+1])
    if '-sit' in sys.argv:
        ind=sys.argv.index('-sit')
        sampnum=int(sys.argv[ind+1])
    ErSpecs,ErSamps=[],[] # setup lists for dictionaries of specimens and samples
    MeasRecs=[]
    samples=[] # keep a list of unique sample names
    citation="This study" # default citation name
    f=open(file,'rU')
    data=f.readlines() # read in data
    k=0 # line number in datafile (-1)
    while k<len(data):
            line=data[k] # read in next record
            rec=line.split() # split on spaces into list called rec
            if rec[0]=='$':  # new specimen 
                ErSpecRec={} # set up dictionary for this specimen
                name=rec[1][:-1].upper()
                ErSpecRec['er_specimen_alternatives']=name
                treat_type=rec[1][-1].upper()
                sample=name[sampnum:-specnum]
                if len(sample)==1:sample='0'+sample
                ErSpecRec['er_specimen_name']=name[:sampnum]+'-'+sample+'-'+name[-specnum:]
                ErSpecRec['er_sample_name']=name[:sampnum]+'-'+sample
                ErSpecRec['er_site_name']=name[:sampnum]
                print 'processing ',ErSpecRec['er_specimen_name']
                ErSpecRec['er_location_name']=locname
                ErSpecRec['er_citation_names']=citation
                ErSpecRec['specimen_class']=cl
                ErSpecRec['specimen_lithology']=litho
                ErSpecRec['specimen_type']=tp
                if ErSpecRec['er_sample_name'] not in samples: # new sample
                    samples.append(ErSpecRec['er_sample_name']) # append to list
                    azimuth=float(rec[2])+90
                    dip=float(rec[3])
                    ErSampRec={} # set up dictionary for this sample
                    ErSampRec['er_sample_name']=ErSpecRec['er_sample_name']
                    ErSampRec['er_site_name']=ErSpecRec['er_site_name']
                    ErSampRec['er_citation_names']=citation
                    ErSampRec['sample_azimuth']='%7.1f'%(azimuth)
                    ErSampRec['sample_dip']='%7.1f'%(dip)
                    ErSampRec['magic_method_codes']='FS-FD:SO-SM'
                    ErSampRec['er_location_name']=locname
                    ErSampRec['er_citation_names']=citation
                    ErSampRec['sample_class']=cl
                    ErSampRec['sample_lithology']=litho
                    ErSampRec['sample_type']=tp
                    ErSamps.append(ErSampRec) # append to the sample list
                Ts=[] # keep a list of treatment temperatures for paleointensity experiments
                while 1:  # continue until next specimen
                    k+=1
                    if k==len(data):break
                    line=data[k] # read in next record
                    rec=line.split() # split on spaces into list called rec
                    if rec[0]=='$':  break
                    if rec[0][0]!='#': # skip commented out specimens
                        MeasRec={}
                        MeasRec['er_location_name']=locname
                        MeasRec['er_citation_names']=citation
                        MeasRec['er_specimen_name']=ErSpecRec['er_specimen_name']
                        MeasRec['er_sample_name']=ErSpecRec['er_sample_name']
                        MeasRec['er_site_name']=ErSpecRec['er_site_name']
                        MeasRec['measurement_temp']='273'  # assume room T measurements
                        MeasRec['measurement_flag']='g'  # good measurement
                        MeasRec['measurement_standard']='u'  # unknown (not a standard)
                        MeasRec['measurement_number']='1'  # measurement number
                        MeasRec['measurement_magn_moment']='%10.3e'%(1e-3*float(rec[1])) # convert to Am^2
                        MeasRec['measurement_inc']=rec[2]
                        MeasRec['measurement_dec']='%7.1f'%(float(rec[3])-90.)
                        MeasRec['magnetization_chi_volume']=''
                        treat=rec[0]
                        if treat_type=='A':
                            MeasRec['treatment_dc_field']='0'  # zero lab field
                            MeasRec['treatment_dc_field_phi']='0'  # zero lab field
                            MeasRec['treatment_dc_field_theta']='0'  # zero lab field
                            MeasRec['treatment_ac_field']='%8.3e'%(float(treat)*1e-3)
                            MeasRec['treatment_temp']='273' # room temperature treatment
                            if treat!="00":
                                MeasRec['magic_method_codes']='LT-AF-Z' # Af demag step
                            else:
                                MeasRec['magic_method_codes']='LT-NO' # Af demag step
                        elif treat_type=='T':
                            MeasRec['treatment_dc_field']='0'  # zero lab field
                            MeasRec['treatment_dc_field_phi']='0'  # zero lab field
                            MeasRec['treatment_dc_field_theta']='0'  # zero lab field
                            MeasRec['treatment_ac_field']='0'
                            MeasRec['treatment_temp']='%i'%(int(treat)+273) # treatment in kelvin
                            if treat!="00" and treat!="20":
                                MeasRec['magic_method_codes']='LT-T-Z' # Af demag step
                            else:
                                MeasRec['magic_method_codes']='LT-NO' # Af demag step
                        elif treat_type=='P':
                            T=int(treat)
                            MeasRec['treatment_temp']='%i'%(int(treat)+273) # treatment in kelvin
                            if treat[0]=='+': # infield step
                                if T not in Ts: # first infield step
                                    Ts.append(T) # put it in there
                                    MeasRec['magic_method_codes']='LT-T-I' # first infield step
                                    MeasRec['treatment_dc_field']=labfield
                                    MeasRec['treatment_dc_field_phi']=phi  # zero lab field
                                    MeasRec['treatment_dc_field_theta']=theta  # zero lab field
                                    MeasRec['treatment_ac_field']='0'
                                else:
                                    MeasRec['magic_method_codes']='LT-PTRM-I' # ptrm check
                                    MeasRec['treatment_dc_field']=labfield
                                    MeasRec['treatment_dc_field_phi']=phi
                                    MeasRec['treatment_dc_field_theta']=theta
                                    MeasRec['treatment_ac_field']='0'
                            else: # zero field step
                                if treat!="20":
                                    MeasRec['magic_method_codes']='LT-T-Z' # Af demag step
                                else:
                                    MeasRec['magic_method_codes']='LT-NO' # Af demag step
                                MeasRec['treatment_dc_field']='0'
                                MeasRec['treatment_dc_field_phi']=phi
                                MeasRec['treatment_dc_field_theta']=theta
                                MeasRec['treatment_ac_field']='0'
                        if len(rec)>4: MeasRec['magnetization_chi_volume']='%10.3e'%(1e-5*float(rec[4]))
                        MeasRecs.append(MeasRec) # save measurement record
                        ErSpecs.append(ErSpecRec) # save measurement record
    MagOuts=pmag.measurements_methods(MeasRecs,0)  # fix the method codes
    pmag.magic_write(meas_file,MagOuts,'magic_measurements') # set up measurement output file
    print "results put in ",meas_file
    stem=file.split('.')[0]+'_'
    pmag.magic_write(stem+'er_specimens.txt',ErSpecs,'er_specimens') # set up specimens output file
    print "specimens put in ",stem+"er_specimens.txt"
    pmag.magic_write(stem+'er_samples.txt',ErSamps,'er_samples') # set up samples output file
    print "specimens put in" ,stem+"er_specimens.txt"
Пример #19
0
def main(command_line=True, **kwargs):
    """
    NAME
        PMD_magic.py
 
    DESCRIPTION
        converts PMD (Enkin)  format files to magic_measurements format files

    SYNTAX
        PMD_magic.py [command line options]

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

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

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


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

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

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

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

    SYNTAX
        sio_magic.py [command line options]

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

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

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

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

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

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

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


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

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


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

                            

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


    SYNTAX
        IODP_csv_magic.py [command line options]

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

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

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

    SYNTAX
        BGC_magic.py [command line options]

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

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

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

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

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

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

    ErSampRec = {}

    # parse data

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

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

    #    print "\ndata\n", data

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

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

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

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

    # Configure the er_sample table

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

    # Configure the magic_measurements table

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

    SYNTAX
        LDGO_magic.py [command line options]

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

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

    SYNTAX
        IODP_jr6_magic.py [command line options]

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


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

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


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


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

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

    SYNTAX
        ODP_dsc_magic.py [command line options]

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

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

    SYNTAX
        CIT_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify .sam format input file, required
        -fsi SITEFILE : specify file with site names and locations [tab delimited magic file]
        -F FILE: specify output  measurements file, default is magic_measurements.txt
        -Fsp FILE: specify output er_specimens.txt file, default is er_specimens.txt
        -Fsi FILE: specify output er_sites.txt file, default is er_sites.txt
        -n [gm,kg,cc,m3]: specify normalization
        -spc NUM : specify number of characters to designate a  specimen, default = 0
        -ncn NCON: specify naming convention
        -loc LOCNAME : specify location/study name, must have either LOCNAME or SITEFILE or be a synthetic
        -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

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

    NOTES:
 
       Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name 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.
 
    """
#        
#
    norm='cc'
    samp_con,Z='3',1        
    meas_file='magic_measurements.txt'
    spec_file='er_specimens.txt'
    samp_file='er_samples.txt'
    site_file='er_sites.txt'
    ErSpecs,ErSamps,ErSites,ErLocs,ErCits=[],[],[],[],[]
    MeasRecs=[]
    specnum,units,locname=0,"1","unknown"
    citation="This study"
    dir_path='.'
    args=sys.argv
    if '-WD' in args:
        ind=args.index("-WD")
        dir_path=args[ind+1]
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-usr" in args:
        ind=args.index("-usr")
        user=args[ind+1]
    if '-F' in args:
        ind=args.index("-F")
        meas_file=args[ind+1]
    if '-Fsp' in args:
        ind=args.index("-Fsp")
        spec_file=args[ind+1]
    if '-Fsa' in args:
        ind=args.index("-Fsa")
        samp_file=args[ind+1]
    if '-loc' in args:
        ind=args.index("-loc")
        locname=args[ind+1]
    if '-spc' in args:
        ind=args.index("-spc")
        specnum=-int(args[ind+1])
    if '-n' in args:
        ind=args.index("-n")
        norm=args[ind+1]
    if "-ncn" in args:
        ind=args.index("-ncn")
        samp_con=sys.argv[ind+1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print "option [4] must be in form 4-Z where Z is an integer"
                sys.exit()
            else: 
                Z=samp_con.split("-")[1]
                samp_con="4"
        if "7" in samp_con:
            if "-" not in samp_con:
                print "option [7] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="7"
    if '-f' in args:
        ind=args.index("-f")
        magfile=args[ind+1]
    magfile=dir_path+'/'+magfile
    spec_file=dir_path+'/'+spec_file
    samp_file=dir_path+'/'+samp_file
    site_file=dir_path+'/'+site_file
    meas_file=dir_path+'/'+meas_file
    try:
        input=open(magfile,'r')
    except:
        print "bad sam file name"
        sys.exit()
    File=input.readlines()
    sids,ln,format=[],0,'CIT'
    formats=['CIT','2G','APP','JRA']
    ErLocRec={}
    ErLocRec["er_location_name"]=locname
    ErLocRec["er_citation_name"]=citation
    comment=File[ln]
    if comment=='CIT':
       format=comment
       ln+=1
    comment=File[ln]
    print comment
    ln+=1
    specimens,samples,sites=[],[],[]
    if format=='CIT':
        line=File[ln].split()
        site_lat=line[0]
        site_lon=line[1]
        ErLocRec["location_begin_lat"]=site_lat
        ErLocRec["location_begin_lon"]=site_lon
        ErLocRec["location_end_lat"]=site_lat
        ErLocRec["location_end_lon"]=site_lon
        ErLocs.append(ErLocRec)
        Cdec=float(line[2])
        if len(line)>4:
            fa_az=line[3]
            fa_pl=line[4]
        if len(line)>6:
            bed_dip_dir='%7.1f'%(float(line[5]+90.))
            bed_dip=line[6]
        for k in range(ln+1,len(File)):
            line=File[k]
            rec=line.split()
            specimen=rec[0]
            specimens.append(specimen)
    else:
        print 'file type not yet supported'
        sys.exit()
    for specimen in specimens:
        ErSpecRec,ErSampRec,ErSiteRec={},{},{}
        if specnum!=0:
            sample=specimen[:specnum]
        else: sample=specimen
        site=pmag.parse_site(sample,samp_con,Z)
        ErSpecRec['er_specimen_name']=specimen
        ErSpecRec['er_sample_name']=sample
        ErSpecRec['er_site_name']=site
        ErSpecRec['er_location_name']=locname
        ErSpecRec['er_citation_name']=citation
        ErSampRec['er_sample_name']=sample
        ErSampRec['er_site_name']=site
        ErSampRec['er_location_name']=locname
        ErSampRec['er_citation_name']=citation
        for k in range(ln+1,len(File)):
            line=File[k]
            rec=line.split()
            specimen=rec[0]
            specimens.append(specimen)
    for specimen in specimens:
        ErSpecRec,ErSampRec,ErSiteRec={},{},{}
        if specnum!=0:
            sample=specimen[:specnum]
        else: sample=specimen
        site=pmag.parse_site(sample,samp_con,Z)
        ErSpecRec['er_specimen_name']=specimen
        ErSpecRec['er_sample_name']=sample
        ErSpecRec['er_site_name']=site
        ErSpecRec['er_location_name']=locname
        ErSpecRec['er_citation_name']=citation
        ErSampRec['er_sample_name']=sample
        ErSampRec['er_site_name']=site
        ErSampRec['er_location_name']=locname
        ErSampRec['er_citation_name']=citation
        ErSiteRec['er_site_name']=site
        ErSiteRec['er_location_name']=locname
        ErSiteRec['er_citation_name']=citation
        ErSiteRec['site_lat']=site_lat
        ErSiteRec['site_lon']=site_lon
        f=open(dir_path+'/'+specimen,'rU')
        Lines=f.readlines()
        comment=Lines[0][10:]
        if len(line)>2:comment=line[2]
        info=Lines[1].split()
        vol=float(info[-1])
        if vol!=1.0:
            if norm=='cc':units="1"
            if norm=='m3':units="2"
            ErSampRec['specimen_weight']=""
            if units=="1" or "":
                ErSampRec['specimen_volume']='%10.3e'%(vol*1e-6)
            else: 
                ErSampRec['specimen_volume']='%10.3e'%(vol)
        else:
            if norm=='cc':units="1"
            if norm=='m3':units="2"
            ErSampRec['specimen_volume']=""
            if units=="1" or "":
                ErSampRec['specimen_weight']='%10.3e'%(vol*1e-3)
            else: 
                ErSampRec['specimen_weight']='%10.3e'%(vol)
        dip=float(info[-2])
        dip_direction=float(info[-3])+Cdec+90.
        sample_dip=-float(info[-4])
        sample_azimuth=float(info[-5])+Cdec-90.
        if len(info)>5:
            ErSampRec['sample_height']=info[-6]
        else:
            ErSampRec['sample_height']='0'
        ErSampRec['sample_azimuth']='%7.1f'%(sample_azimuth)
        ErSampRec['sample_dip']='%7.1f'%(sample_dip)
        ErSampRec['sample_bed_dip']='%7.1f'%(dip)
        ErSampRec['sample_bed_dip_direction']='%7.1f'%(dip_direction)
        ErSampRec['sample_class']=''
        ErSampRec['sample_type']=''
        ErSampRec['sample_lithology']=''
        if Cdec!=0 or Cdec!="":
            ErSampRec['magic_method_codes']='SO-CMD-NORTH'
        else:
            ErSampRec['magic_method_codes']='SO-MAG'
        for line in Lines[2:len(Lines)-1]:
            MeasRec=ErSpecRec.copy()
            treat_type=line[0:3]
            treat=line[3:6]
            if treat_type.strip()=='NRM' or treat=="   ":
                MeasRec['magic_method_codes']='LT-NO'
                MeasRec['measurement_temp']='273'
                MeasRec['treatment_temp']='273'
                MeasRec['treatment_dc_field']='0'
                MeasRec['treatment_ac_field']='0'
            elif treat_type.strip()=='AF':
                MeasRec['magic_method_codes']='LT-AF-Z'
                MeasRec['measurement_temp']='273'
                MeasRec['treatment_temp']='273'
                MeasRec['treatment_dc_field']='0'
                MeasRec['treatment_ac_field']='%10.3e'%(float(treat)*1e-3)
            elif treat_type.strip()=='TT':
                MeasRec['magic_method_codes']='LT-T-Z'
                MeasRec['measurement_temp']='273'
                MeasRec['treatment_temp']='%7.1f'%(float(treat)+273)
                MeasRec['treatment_dc_field']='0'
                MeasRec['treatment_ac_field']='0'
            else:
                print "trouble with your treatment steps"
            MeasRec['measurement_dec']=line[46:51]
            MeasRec['measurement_inc']=line[52:58]
            int='%8.2e'%(float(line[31:39])*vol*1e-3) # convert to Am2
            MeasRec['measurement_magn_moment']=int
            MeasRec['measurement_x_sd']='%8.2e'%(float(line[58:67])*1e-8) #(convert e-5emu to Am2)
            MeasRec['measurement_y_sd']='%8.2e'%(float(line[67:76])*1e-8)
            MeasRec['measurement_z_sd']='%8.2e'%(float(line[76:85])*1e-8)
            MeasRec['measurement_csd']='%7.1f'%(eval(line[41:46]))
            MeasRec['magic_instrument_codes']=line[85:]
            MeasRecs.append(MeasRec)
        ErSpecs.append(ErSpecRec)
        if sample not in samples:
            samples.append(sample)
            ErSamps.append(ErSampRec)
        site=pmag.parse_site(sample,samp_con,Z)
        if site not in sites:
            sites.append(site)
            ErSites.append(ErSiteRec)
    pmag.magic_write(spec_file,ErSpecs,'er_specimens')
    print 'specimens stored in ',spec_file
    pmag.magic_write(samp_file,ErSamps,'er_samples')
    print 'samples stored in ',samp_file
    pmag.magic_write(site_file,ErSites,'er_sites')
    Fixed=pmag.measurements_methods(MeasRecs,0)
    pmag.magic_write(meas_file,Fixed,'magic_measurements')
    print 'data stored in ',meas_file
Пример #27
0
def main():
    """
    NAME
        2G_bin_magic.py
   
    DESCRIPTION
        takes the binary 2G format magnetometer files and converts them to magic_measurements, er_samples.txt and er_sites.txt file
 
    SYNTAX
        2G_magic.py [command line options]

    OPTIONS
        -f FILE: specify input 2G (binary) file
        -F FILE: specify magic_measurements output file, default is: magic_measurements.txt
        -Fsa FILE: specify output file, default is: er_samples.txt 
        -Fsi FILE: specify output file, default is: er_sies.txt 
        -ncn NCON:  specify naming convention: default is #2 below
        -ocn OCON:  specify orientation convention, default is #5 below
        -mcd: specify sampling method codes as a colon delimited string:  [default is: FS-FD:SO-POM]
             FS-FD field sampling done with a drill
             FS-H field sampling done with hand samples
             FS-LOC-GPS  field location done with GPS
             FS-LOC-MAP  field location done with map
             SO-POM   a Pomeroy orientation device was used
             SO-ASC   an ASC orientation device was used
             SO-MAG   orientation with magnetic compass
             SO-SUN   orientation with sun compass
        -loc: location name, default="unknown"
        -spc NUM : specify number of characters to designate a  specimen, default = 0     
        -ins INST : specify instsrument name
        -a: average replicate measurements

    INPUT FORMAT
        Input files are horrible mag binary format (who knows why?)
        Orientation convention:
            [1] Lab arrow azimuth= mag_azimuth; Lab arrow dip=-field_dip
                i.e., field_dip is degrees from vertical down - the hade [default]
            [2] Lab arrow azimuth = mag_azimuth-90; Lab arrow dip = -field_dip
                i.e., mag_azimuth is strike and field_dip is hade
            [3] Lab arrow azimuth = mag_azimuth; Lab arrow dip = 90-field_dip
                i.e.,  lab arrow same as field arrow, but field_dip was a hade.
            [4] lab azimuth and dip are same as mag_azimuth, field_dip
            [5] lab azimuth is same as mag_azimuth,lab arrow dip=field_dip-90
            [6] Lab arrow azimuth = mag_azimuth-90; Lab arrow dip = 90-field_dip
            [7] all others you will have to either customize your 
                self or e-mail [email protected] for help.  
 
         Magnetic declination convention:
             Az will use supplied declination to correct azimuth 
    
       Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name 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.
 

    OUTPUT
            output saved in magic_measurements.txt & er_samples.txt formatted files
              will overwrite any existing files 
    """
    #
    # initialize variables
    #
    specnum = 0
    ub_file, samp_file, or_con, corr, meas_file = "", "er_samples.txt", "3", "1", "magic_measurements.txt"
    pos_file, site_file = "", "er_sites.txt"
    noave = 1
    args = sys.argv
    bed_dip, bed_dip_dir = "", ""
    samp_con, Z, average_bedding = "2", 1, "0"
    meths = "FS-FD"
    sclass, lithology, type = "", "", ""
    user, inst = "", ""
    DecCorr = 0.0
    location_name = "unknown"
    months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    gmeths = ""
    #
    #
    dir_path = "."
    if "-WD" in args:
        ind = args.index("-WD")
        dir_path = sys.argv[ind + 1]
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-f" in args:
        ind = args.index("-f")
        mag_file = sys.argv[ind + 1]
    if "-fpos" in args:
        ind = args.index("-fpos")
        pos_file = sys.argv[ind + 1]
    if "-F" in args:
        ind = args.index("-F")
        meas_file = sys.argv[ind + 1]
    if "-Fsa" in args:
        ind = args.index("-Fsa")
        samp_file = sys.argv[ind + 1]
    if "-Fsi" in args:
        ind = args.index("-Fsi")
        site_file = sys.argv[ind + 1]
    if "-ocn" in args:
        ind = args.index("-ocn")
        or_con = sys.argv[ind + 1]
    if "-ncn" in args:
        ind = args.index("-ncn")
        samp_con = sys.argv[ind + 1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print "option [4] must be in form 4-Z where Z is an integer"
                sys.exit()
            else:
                Z = samp_con.split("-")[1]
                samp_con = "4"
        if "7" in samp_con:
            if "-" not in samp_con:
                print "option [7] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z = samp_con.split("-")[1]
                samp_con = "7"
        if "6" in samp_con:
            try:
                Samps, file_type = pmag.magic_read(dir_path + "/er_samples.txt")
            except:
                print "there is no er_samples.txt file available - you can't use naming convention #6"
                sys.exit()
    if "-mcd" in args:
        ind = args.index("-mcd")
        gmeths = sys.argv[ind + 1]
    if "-loc" in args:
        ind = args.index("-loc")
        location_name = sys.argv[ind + 1]
    if "-spc" in args:
        ind = args.index("-spc")
        specnum = int(args[ind + 1])
        if specnum != 0:
            specnum = -specnum
    if "-ins" in args:
        ind = args.index("-ins")
        inst = args[ind + 1]
    if "-a" in args:
        noave = 0
    #
    #
    mag_file = dir_path + "/" + mag_file
    samp_file = dir_path + "/" + samp_file
    site_file = dir_path + "/" + site_file
    meas_file = dir_path + "/" + meas_file
    samplist = []
    try:
        Samps, file_type = pmag.magic_read(samp_file)
        for samp in Samps:
            if samp["er_sample_name"] not in samplist:
                samplist.append(samp["er_sample_name"])
    except:
        Samps = []
    MagRecs = []
    f = open(mag_file, "rU")
    input = f.read()
    f.close()
    firstline, date = 1, ""
    d = input.split("\xcd")
    for line in d:
        rec = line.split("\x00")
        if firstline == 1:
            firstline = 0
            spec, vol = "", 1
            for c in line[15:23]:
                if c != "\x00":
                    spec = spec + c
            # check for bad sample name
            test = spec.split(".")
            date = ""
            if len(test) > 1:
                spec = test[0]
                kk = 24
                while line[kk] != "\x01" and line[kk] != "\x00":
                    kk += 1
                vcc = line[24:kk]
                el = 10
                while rec[el].strip() != "":
                    el += 1
                date, comments = rec[el + 7], []
            else:
                el = 9
                while rec[el] != "\x01":
                    el += 1
                vcc, date, comments = rec[el - 3], rec[el + 7], []
            specname = spec.lower()
            print "importing ", specname
            el += 8
            while rec[el].isdigit() == False:
                comments.append(rec[el])
                el += 1
            while rec[el] == "":
                el += 1
            az = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            pl = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            bed_dip_dir = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            bed_dip = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            if rec[el] == "\x01":
                bed_dip = 180.0 - bed_dip
                el += 1
                while rec[el] == "":
                    el += 1
            fold_az = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            fold_pl = rec[el]
            el += 1
            while rec[el] == "":
                el += 1
            if rec[el] != "" and rec[el] != "\x02" and rec[el] != "\x01":
                deccorr = float(rec[el])
                az += deccorr
                bed_dip_dir += deccorr
                fold_az += deccorr
                if bed_dip_dir >= 360:
                    bed_dip_dir = bed_dip_dir - 360.0
                if az >= 360.0:
                    az = az - 360.0
                if fold_az >= 360.0:
                    fold_az = fold_az - 360.0
            else:
                deccorr = 0
            if specnum != 0:
                sample = specname[:specnum]
            else:
                sample = specname
            SampRec = {}
            SampRec["er_sample_name"] = sample
            SampRec["er_location_name"] = location_name
            SampRec["er_citation_names"] = "This study"
            labaz, labdip = pmag.orient(az, pl, or_con)  # convert to labaz, labpl
            #
            # parse information common to all orientation methods
            #
            SampRec["sample_bed_dip"] = "%7.1f" % (bed_dip)
            SampRec["sample_bed_dip_direction"] = "%7.1f" % (bed_dip_dir)
            SampRec["sample_dip"] = "%7.1f" % (labdip)
            SampRec["sample_azimuth"] = "%7.1f" % (labaz)
            if vcc.strip() != "":
                vol = float(vcc) * 1e-6  # convert to m^3 from cc
            SampRec["sample_volume"] = "%10.3e" % (vol)  #
            SampRec["sample_class"] = sclass
            SampRec["sample_lithology"] = lithology
            SampRec["sample_type"] = type
            SampRec["sample_declination_correction"] = "%7.1f" % (deccorr)
            methods = gmeths.split(":")
            if deccorr != "0":
                if "SO-MAG" in methods:
                    del methods[methods.index("SO-MAG")]
                methods.append("SO-CMD-NORTH")
            meths = ""
            for meth in methods:
                meths = meths + meth + ":"
            meths = meths[:-1]
            SampRec["magic_method_codes"] = meths
            if int(samp_con) < 6:
                site = pmag.parse_site(SampRec["er_sample_name"], samp_con, Z)  # parse out the site name
                SampRec["er_site_name"] = site
            elif len(Samps) > 1:
                site, location = "", ""
                for samp in Samps:
                    if samp["er_sample_name"] == SampRec["er_sample_name"]:
                        site = samp["er_site_name"]
                        location = samp["er_location_name"]
                        break
                SampRec["er_location_name"] = samp["er_location_name"]
                SampRec["er_site_name"] = samp["er_site_name"]
            if sample not in samplist:
                samplist.append(sample)
                Samps.append(SampRec)
        else:
            MagRec = {}
            MagRec["treatment_temp"] = "%8.3e" % (273)  # room temp in kelvin
            MagRec["measurement_temp"] = "%8.3e" % (273)  # room temp in kelvin
            MagRec["treatment_ac_field"] = "0"
            MagRec["treatment_dc_field"] = "0"
            MagRec["treatment_dc_field_phi"] = "0"
            MagRec["treatment_dc_field_theta"] = "0"
            meas_type = "LT-NO"
            MagRec["measurement_flag"] = "g"
            MagRec["measurement_standard"] = "u"
            MagRec["measurement_number"] = "1"
            MagRec["er_specimen_name"] = specname
            MagRec["er_sample_name"] = SampRec["er_sample_name"]
            MagRec["er_site_name"] = SampRec["er_site_name"]
            MagRec["er_location_name"] = location_name
            el, demag = 1, ""
            treat = rec[el]
            if treat[-1] == "C":
                demag = "T"
            elif treat != "NRM":
                demag = "AF"
            el += 1
            while rec[el] == "":
                el += 1
            MagRec["measurement_dec"] = rec[el]
            cdec = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            MagRec["measurement_inc"] = rec[el]
            cinc = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            gdec = rec[el]
            el += 1
            while rec[el] == "":
                el += 1
            ginc = rec[el]
            el = skip(2, el, rec)  # skip bdec,binc
            #                el=skip(4,el,rec) # skip gdec,ginc,bdec,binc
            #                print 'moment emu: ',rec[el]
            MagRec["measurement_magn_moment"] = "%10.3e" % (float(rec[el]) * 1e-3)  # moment in Am^2 (from emu)
            MagRec["measurement_magn_volume"] = "%10.3e" % (float(rec[el]) * 1e-3 / vol)  # magnetization in A/m
            el = skip(2, el, rec)  # skip to xsig
            MagRec["measurement_sd_x"] = "%10.3e" % (float(rec[el]) * 1e-3)  # convert from emu
            el = skip(3, el, rec)  # skip to ysig
            MagRec["measurement_sd_y"] = "%10.3e" % (float(rec[el]) * 1e-3)  # convert from emu
            el = skip(3, el, rec)  # skip to zsig
            MagRec["measurement_sd_z"] = "%10.3e" % (float(rec[el]) * 1e-3)  # convert from emu
            el += 1  # skip to positions
            MagRec["measurement_positions"] = rec[el]
            #                    el=skip(5,el,rec) # skip to date
            #                    mm=str(months.index(date[0]))
            #                    if len(mm)==1:
            #                        mm='0'+str(mm)
            #                    else:
            #                        mm=str(mm)
            #                    dstring=date[2]+':'+mm+':'+date[1]+":"+date[3]
            #                    MagRec['measurement_date']=dstring
            MagRec["magic_instrument_codes"] = inst
            MagRec["er_analyst_mail_names"] = ""
            MagRec["er_citation_names"] = "This study"
            MagRec["magic_method_codes"] = meas_type
            if demag == "AF":
                MagRec["treatment_ac_field"] = "%8.3e" % (float(treat[:-2]) * 1e-3)  # peak field in tesla
                meas_type = "LT-AF-Z"
                MagRec["treatment_dc_field"] = "0"
            elif demag == "T":
                MagRec["treatment_temp"] = "%8.3e" % (float(treat[:-1]) + 273.0)  # 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 "Measurements put in ", meas_file
    SampsOut, sampkeys = pmag.fillkeys(Samps)
    pmag.magic_write(samp_file, SampsOut, "er_samples")
    Sites = []
    for samp in Samps:
        SiteRec = {}
        SiteRec["er_site_name"] = samp["er_site_name"]
        SiteRec["er_location_name"] = samp["er_location_name"]
        SiteRec["site_definition"] = "s"
        SiteRec["er_citation_names"] = "This study"
        if "sample_class" in samp.keys():
            SiteRec["site_class"] = samp["sample_class"]
        if "sample_lithology" in samp.keys():
            SiteRec["site_lithology"] = samp["sample_lithology"]
        if "sample_type" in samp.keys():
            SiteRec["site_lithology"] = samp["sample_lithology"]
        if "sample_lat" in samp.keys():
            SiteRec["site_lat"] = samp["sample_lat"]
        else:
            SiteRec["site_lat"] = "-999"
        if "sample_lon" in samp.keys():
            SiteRec["site_lon"] = samp["sample_lon"]
        else:
            SiteRec["site_lon"] = "-999"
        if "sample_height" in samp.keys():
            SiteRec["site_height"] = samp["sample_height"]
        Sites.append(SiteRec)
    pmag.magic_write(site_file, Sites, "er_sites")
Пример #28
0
def main():
    """
    NAME
        ODP_srm_magic.py
 
    DESCRIPTION
        converts ODP measurement format files to magic_measurements format files

    SYNTAX
        ODP_srm_magic.py [command line options]

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

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