Example #1
0
def main(command_line=True, **kwargs):
    """
    NAME
        jr6_magic.py
 
    DESCRIPTION
        converts JR6 format files to magic_measurements format files

    SYNTAX
        jr6_magic.py [command line options]

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

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

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


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

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


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

    ErSampRec,ErSiteRec={},{}

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

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

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

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

        er_specimen_name = sampleName

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

        if int(samp_con) in [1, 2, 3, 4, 5, 7]:
            er_site_name=pmag.parse_site(er_sample_name,samp_con,Z)

        # else:
        #     if 'er_site_name' in ErSampRec.keys():er_site_name=ErSampRec['er_site_name']
        #     if 'er_location_name' in ErSampRec.keys():er_location_name=ErSampRec['er_location_name']

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

        MagRec={}
        MagRec['measurement_description']='Date: '+date
        MagRec["er_citation_names"]="This study"
        MagRec['er_location_name']=er_location_name
        MagRec['er_site_name']=er_site_name
        MagRec['er_sample_name']=er_sample_name
        MagRec['magic_software_packages']=version_num
        MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
        MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
        MagRec["measurement_flag"]='g'
        MagRec["measurement_standard"]='u'
        MagRec["measurement_number"]='1'
        MagRec["er_specimen_name"]=er_specimen_name
        MagRec["treatment_ac_field"]='0'
        if demagLevel == 'NRM':
            meas_type="LT-NO"
        elif demagLevel[0] == 'A':
            meas_type="LT-AF-Z"
            treat=float(demagLevel[1:])
            MagRec["treatment_ac_field"]='%8.3e' %(treat*1e-3) # convert from mT to tesla
        elif demagLevel[0] == 'T':
            meas_type="LT-T-Z"
            treat=float(demagLevel[1:])
            MagRec["treatment_temp"]='%8.3e' % (treat+273.) # temp in kelvin
        else:
            print("measurement type unknown")
            return False
#        X=[float(Mx),float(My),float(Mz)]
#        Vec=pmag.cart2dir(X)
#        MagRec["measurement_magn_moment"]='%10.3e'% (Vec[2]) # Am^2
        MagRec["measurement_magn_moment"]=str(intensity*0.025*0.025*0.025) # Am^2 assume 2.5cm cube sample
        MagRec["measurement_magn_volume"]=intensityStr
        MagRec["measurement_dec"]=specimenDec
        MagRec["measurement_inc"]=specimenInc
        MagRec['magic_method_codes']=meas_type
        MagRecs.append(MagRec.copy())

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

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

    MagOuts=pmag.measurements_methods(MagRecs,noave)
    pmag.magic_write(samp_file,SampOuts,'er_samples') 
    print("sample orientations put in ",samp_file)
    pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    print("results put in ",meas_file)
    return True
Example #2
0
def main(command_line=True, **kwargs):
    """
    NAME
        _2g_bin_magic.py
   
    DESCRIPTION
        takes the binary 2g format magnetometer files and converts them to magic_measurements, er_samples.txt and er_sites.txt file
 
    SYNTAX
        2g_bin_magic.py [command line options]

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

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

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

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

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

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

    if ID:
        input_dir_path = ID
    else:
        input_dir_path = dir_path

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

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

    SYNTAX
        jr6_jr6_magic.py [command line options]

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

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

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

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

    ErSampRec,ErSiteRec={},{}

    # parse data

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

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

    if JR==0: #
        data.columns=['er_specimen_name','step','x','y','z','expon','sample_azimuth','sample_dip',              'sample_bed_dip_direction','sample_bed_dip','bed_dip_dir2','bed_dip2','param1','param2','param3','param4','measurement_csd']
        cart=np.array([data['x'],data['y'],data['z']]).transpose()
    else: # measured on the Joides Resolution JR6
        data.columns=['er_specimen_name','step','negz','y','x','expon','sample_azimuth','sample_dip',              'sample_bed_dip_direction','sample_bed_dip','bed_dip_dir2','bed_dip2','param1','param2','param3','param4','measurement_csd']
        cart=np.array([data['x'],data['y'],-data['negz']]).transpose()
    dir= pmag.cart2dir(cart).transpose()
    data['measurement_dec']=dir[0]
    data['measurement_inc']=dir[1]
    data['measurement_magn_moment']=dir[2]*(10.0**data['expon'])*volume # the data are in A/m - this converts to Am^2 
    data['measurement_magn_volume']=dir[2]*(10.0**data['expon']) # A/m  - data in A/m
    data['sample_dip']=-data['sample_dip']
    DGEOs,IGEOs=[],[]
    for ind in range(len(data)):
        dgeo,igeo=pmag.dogeo(data.ix[ind]['measurement_dec'],data.ix[ind]['measurement_inc'],data.ix[ind]['sample_azimuth'],data.ix[ind]['sample_dip'])
        DGEOs.append(dgeo)
        IGEOs.append(igeo)
    data['specimen_dec']=DGEOs
    data['specimen_inc']=IGEOs
    data['specimen_tilt']='1'
    if specnum!=0: 
        data['er_sample_name']=data['er_specimen_name'][:specnum]
    else:
        data['er_sample_name']=data['er_specimen_name']

    if int(samp_con) in [1, 2, 3, 4, 5, 7]:
        data['er_site_name']=pmag.parse_site(data['er_sample_name'],samp_con,Z)
    # else:
    #     if 'er_site_name' in ErSampRec.keys():er_site_name=ErSampRec['er_site_name']
    #     if 'er_location_name' in ErSampRec.keys():er_location_name=ErSampRec['er_location_name']

    # Configure the er_sample table        

    for rowNum, row in data.iterrows():
        sampleFlag=0
        for sampRec in SampOuts:
            if sampRec['er_sample_name'] == row['er_sample_name']:
                sampleFlag=1
                break
        if sampleFlag == 0:
            ErSampRec['er_sample_name']=row['er_sample_name']
            ErSampRec['sample_azimuth']=str(row['sample_azimuth'])
            ErSampRec['sample_dip']=str(row['sample_dip'])
            ErSampRec['magic_method_codes']=meth_code 
            ErSampRec['er_location_name']=er_location_name
            ErSampRec['er_site_name']=row['er_site_name']
            ErSampRec['er_citation_names']='This study'
            SampOuts.append(ErSampRec.copy())

    # Configure the magic_measurements table

    for rowNum, row in data.iterrows():
        MagRec={}
#        MagRec['measurement_description']='Date: '+date
        MagRec["er_citation_names"]="This study"
        MagRec['er_location_name']=er_location_name
        MagRec['er_site_name']=row['er_site_name']
        MagRec['er_sample_name']=row['er_sample_name']
        MagRec['magic_software_packages']=version_num
        MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
        MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
        MagRec["measurement_flag"]='g'
        MagRec["measurement_standard"]='u'
        MagRec["measurement_number"]='1'
        MagRec["er_specimen_name"]=row['er_specimen_name']
        MagRec["treatment_ac_field"]='0'
        if row['step'] == 'NRM':
            meas_type="LT-NO"
        elif row['step'][0:2] == 'AD':
            meas_type="LT-AF-Z"
            treat=float(row['step'][2:])
            MagRec["treatment_ac_field"]='%8.3e' %(treat*1e-3) # convert from mT to tesla
        elif row['step'][0] == 'TD':
            meas_type="LT-T-Z"
            treat=float(row['step'][2:])
            MagRec["treatment_temp"]='%8.3e' % (treat+273.) # temp in kelvin
        else: # need to add IRM, and ARM options
            print "measurement type unknown", row['step']
            return False, "measurement type unknown"
        MagRec["measurement_magn_moment"]=str(row['measurement_magn_moment'])
        MagRec["measurement_magn_volume"]=str(row['measurement_magn_volume'])
        MagRec["measurement_dec"]=str(row['measurement_dec'])
        MagRec["measurement_inc"]=str(row['measurement_inc'])
        MagRec['magic_method_codes']=meas_type
        MagRecs.append(MagRec.copy())
    pmag.magic_write(samp_file,SampOuts,'er_samples')
    print "sample orientations put in ",samp_file
    MagOuts=pmag.measurements_methods(MagRecs,noave)
    pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    print "results put in ",meas_file
    print "exit!"
    return True, meas_file
Example #4
0
def main(command_line=True, **kwargs):
    """
    NAME
        pmd_magic.py
 
    DESCRIPTION
        converts PMD (Enkin)  format files to magic_measurements format files

    SYNTAX
        pmd_magic.py [command line options]

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

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

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


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

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

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

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

    SYNTAX
        old_iodp_jr6_magic.py [command line options]

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


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

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


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


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

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

    DESCRIPTION
        converts ODP discrete sample format files to magic_measurements format files

    SYNTAX
        odp_dsc_magic.py [command line options]

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

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

    Fixed = pmag.measurements_methods(MagOuts, noave)
    pmag.magic_write(meas_file, Fixed, 'magic_measurements')
    print('data stored in ', meas_file)
Example #7
0
def main(command_line=True, **kwargs):
    """
    NAME
        bgc_magic.py

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

    SYNTAX
        bgc_magic.py [command line options]

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

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

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

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

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

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

    ErSampRec = {}

    # parse data

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

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

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

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

    # Configure the er_sample table

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

    # Configure the magic_measurements table

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

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

    SYNTAX
        cit_magic.py [command line options]

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

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

    NOTES:
         Sample naming convention:
        [1] XXXXY: where XXXX is an arbitrary length site designation and Y
            is the single character sample designation.  e.g., TG001a is the
            first sample from site TG001.    [default]
        [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
        [3: default] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
        [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
        [5] site name = sample name
        [6] site name entered in site_name column in the orient.txt format input file  -- NOT CURRENTLY SUPPORTED
        [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
        NB: all others you will have to either customize your
            self or e-mail [email protected] or [email protected] for help.
    """

#
#              NB: use PHI, THETA = -1 -1 to signal that it changes, i.e. in anisotropy experiment
#        -ac B : peak AF field (in mT) for ARM acquisition, default is none
    #
    #initialize variables
    norm='cc'
    samp_con,Z='3',1
    meas_file='magic_measurements.txt'
    spec_file='er_specimens.txt'
    samp_file='er_samples.txt'
    site_file='er_sites.txt'
    ErSpecs,ErSamps,ErSites,ErLocs,ErCits=[],[],[],[],[]
    MeasRecs=[]
    specnum,units,locname=0,"1","unknown"
    citation="This study"
    dir_path='.'
    args=sys.argv
    if command_line:
        if '-WD' in args:
            ind=args.index("-WD")
            dir_path=args[ind+1]
        if "-h" in args:
            print(main.__doc__)
            return False
        if "-usr" in args:
            ind=args.index("-usr")
            user=args[ind+1]
        if '-F' in args:
            ind=args.index("-F")
            meas_file=args[ind+1]
        if '-Fsp' in args:
            ind=args.index("-Fsp")
            spec_file=args[ind+1]
        if '-Fsa' in args:
            ind=args.index("-Fsa")
            samp_file=args[ind+1]
        if '-Fsi' in args:   # LORI addition
            ind=args.index("-Fsi")
            site_file=args[ind+1]
        if '-loc' in args:
            ind=args.index("-loc")
            locname=args[ind+1]
        if '-mcd' in args:
            ind=args.index("-mcd")
            methods=args[ind+1]
        else:
            methods='SO-MAG'
        if '-spc' in args:
            ind=args.index("-spc")
            specnum=-int(args[ind+1])
        if '-n' in args:
            ind=args.index("-n")
            norm=args[ind+1]
        if "-A" in args:
            avg=1
        else:
            avg=0
        if '-dc' in args:
            ind=args.index('-dc')
            DC_FIELD,DC_PHI,DC_THETA=list(map(float,args[ind+1].strip('( ) [ ]').split(',')))
            DC_FIELD *= 1e-6
            yn=''
            GET_DC_PARAMS=False
        else: GET_DC_PARAMS,DC_FIELD,DC_PHI,DC_THETA,yn=True,0,0,-90,''
        if "-ncn" in args:
            ind=args.index("-ncn")
            samp_con=sys.argv[ind+1]
            if "4" in samp_con:
                if "-" not in samp_con:
                    print("option [4] must be in form 4-Z where Z is an integer")
                    return False, "naming convention option [4] must be in form 4-Z where Z is an integer"
                else:
                    Z=samp_con.split("-")[1]
                    samp_con="4"
            elif "7" in samp_con:
                if "-" not in samp_con:
                    print("option [7] must be in form 7-Z where Z is an integer")
                    return False, "naming convention option [7] must be in form 7-Z where Z is an integer"
                else:
                    Z=samp_con.split("-")[1]
                    samp_con="7"
        if '-f' in args:
            ind=args.index("-f")
            magfile=args[ind+1]
        if '-ID' in args:
            ind = args.index('-ID')
            input_dir_path = args[ind+1]
        else:
            input_dir_path = os.path.split(magfile)[0]
        output_dir_path = dir_path
        # LJ

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

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

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

    DESCRIPTION
        converts IODP LIMS and LORE SRM archive half sample format files to magic_measurements format files


    SYNTAX
        iodp_srm_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
    """
    #
    # initialize defaults
    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'
    # 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 '-Fsp' in args:
            ind=args.index("-Fsp")
            spec_file = args[ind+1]
        if '-Fsi' in args:
            ind=args.index("-Fsi")
            site_file=args[ind+1]
        if '-Fsa' in args:
            ind=args.index("-Fsa")
            samp_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')
        spec_file = kwargs.get('spec_file', 'er_specimens.txt')
        samp_file = kwargs.get('samp_file', 'er_samples.txt')
        site_file = kwargs.get('site_file', 'er_sites.txt')

    # format variables

    meas_file = os.path.join(output_dir_path, meas_file)
    spec_file = os.path.join(output_dir_path, spec_file)
    Specs,file_type = pmag.magic_read(spec_file)
    samp_file = os.path.join(output_dir_path, samp_file)
    ErSamps,file_type = pmag.magic_read(samp_file)
    site_file = os.path.join(output_dir_path, site_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
    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)
    file_found = False
    for f in filelist: # parse each file
        if f[-3:].lower()=='csv':
            file_found = True
            print('processing: ',f)
            full_file = os.path.join(input_dir_path, f)
            with open(full_file, 'r') as fin:
                file_input = fin.readlines()
            keys=file_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 "Offset (cm)" in keys: interval_key="Offset (cm)"
            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 "Depth CSF-A (m)" in keys:depth_key="Depth CSF-A (m)"
            if "CSF-B Top (m)" in keys:
                comp_depth_key="CSF-B Top (m)" # use this model if available
            elif "Depth CSF-B (m)" in keys:
                comp_depth_key="Depth CSF-B (m)"
            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 "Inclination background &amp; tray corrected (deg)" in keys:inc_key="Inclination background &amp; tray corrected (deg)"
            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 "Declination background &amp; tray corrected (deg)" in keys:dec_key="Declination background &amp; tray corrected (deg)"
            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 "Intensity background &amp; tray corrected (A/m)" in keys:int_key="Intensity background &amp; tray corrected (A/m)"
            if "Core Type" in keys:
                core_type="Core Type"
            else: core_type="Type"
            if 'Run Number' in keys: run_number_key='Run Number'
            if 'Test No.' in keys: run_number_key='Test No.'
            if 'Test Changed On' in keys: date_key='Test Changed On'
            if "Timestamp (UTC)" in keys: date_key="Timestamp (UTC)"
            if "Section" in keys: sect_key="Section"
            if "Sect" in keys: sect_key="Sect"
            if 'Section Half' in keys: half_key='Section Half'
            if "A/W" in keys: half_key="A/W"
            if "Text ID" in keys: text_id="Text ID"
            if "Text Id" in keys: text_id="Text Id"
            for line in file_input[1:]:
              InRec={}
              test=0
              recs=line.split(',')
              for k in range(len(keys)):
                  if len(recs)==len(keys):
                      InRec[keys[k]]=line.split(',')[k]
              if InRec['Exp']!="": test=1 # get rid of pesky blank lines
              if test==1:
                run_number=""
                inst="IODP-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 list(InRec.keys()) and "SHLF" not in InRec[text_id] or 'dscr' in csv_file :  # assume discrete sample
                    specimen=expedition+'-'+location+'-'+InRec['Core']+InRec[core_type]+"-"+InRec[sect_key]+'-'+InRec[half_key]+'-'+str(InRec[interval_key])
                else: # mark as continuous measurements
                    specimen=expedition+'-'+location+'-'+InRec['Core']+InRec[core_type]+"_"+InRec[sect_key]+InRec[half_key]+'-'+str(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 list(SpecRec.keys()):SampRec[key]=SpecRec[key]
                for key in list(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 "SHLF" not in InRec[text_id]:
                    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 list(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 list(InRec.keys()) and  InRec['Sample Area (cm?)']!= "": volume=InRec['Sample Area (cm?)']
                if InRec[run_number_key]!= "": run_number=InRec[run_number_key]
                datestamp=InRec[date_key].split() # date time is second line of file
                if '/' in datestamp[0]:
                    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"
                if '-' in datestamp[0]:
                    mmddyy=datestamp[0].split('-') # break into month day year
                    date=mmddyy[0]+':'+mmddyy[1]+":"+mmddyy[2] +':' +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+':IODP-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 'Treatment Type' in list(InRec.keys()) and InRec['Treatment Type']!="":
                    if 'Alternating Frequency' in InRec['Treatment Type']:
                        MagRec['magic_method_codes'] = 'LT-AF-Z'
                        inst=inst+':I`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+':IODP-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:
              #   print 'Boo-boo somewhere - no idea where'
    if not file_found:
        print("No .csv files were found")
        return False, "No .csv files were found"
    if len(SpecRecs)>0:
        print('spec_file', spec_file)
        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)
    if pmag.magic_write(meas_file,Fixed,'magic_measurements'):
        print('data stored in ',meas_file)
        return True, meas_file
    else:
        print('no data found.  bad magfile?')
        return False, 'no data found.  bad magfile?'
Example #10
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 NOT CURRENTLY SUPPORTED
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.
 
            [8] synthetic - has no site name
            [9] ODP naming convention 
            [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, 'r')
        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)
Example #11
0
def main(command_line=True, **kwargs):
    """
    NAME
        iodp_srm_magic.py
 
    DESCRIPTION
        converts IODP LIMS and LORE SRM archive half sample format files to magic_measurements format files


    SYNTAX
        iodp_srm_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
    """
    #        
    # initialize defaults
    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'
    # 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 '-Fsp' in args:
            ind=args.index("-Fsp")
            spec_file = args[ind+1]
        if '-Fsi' in args:
            ind=args.index("-Fsi")
            site_file=args[ind+1]
        if '-Fsa' in args:
            ind=args.index("-Fsa")
            samp_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')
        spec_file = kwargs.get('spec_file', 'er_specimens.txt')
        samp_file = kwargs.get('samp_file', 'er_samples.txt')
        site_file = kwargs.get('site_file', 'er_sites.txt')

    # format variables

    meas_file = os.path.join(output_dir_path, meas_file)
    spec_file = os.path.join(output_dir_path, spec_file)
    Specs,file_type = pmag.magic_read(spec_file)
    samp_file = os.path.join(output_dir_path, samp_file)
    ErSamps,file_type = pmag.magic_read(samp_file)
    site_file = os.path.join(output_dir_path, site_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
    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)
    file_found = False
    for f in filelist: # parse each file
        if f[-3:].lower()=='csv':
            file_found = True
            print 'processing: ',f
            full_file = os.path.join(input_dir_path, f)
            file_input=open(full_file,'rU').readlines()
            keys=file_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 "Offset (cm)" in keys: interval_key="Offset (cm)"
            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 "Depth CSF-A (m)" in keys:depth_key="Depth CSF-A (m)"
            if "CSF-B Top (m)" in keys: 
                comp_depth_key="CSF-B Top (m)" # use this model if available 
            elif "Depth CSF-B (m)" in keys:
                comp_depth_key="Depth CSF-B (m)"
            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 "Inclination background &amp; tray corrected (deg)" in keys:inc_key="Inclination background &amp; tray corrected (deg)"
            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 "Declination background &amp; tray corrected (deg)" in keys:dec_key="Declination background &amp; tray corrected (deg)"
            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 "Intensity background &amp; tray corrected (A/m)" in keys:int_key="Intensity background &amp; tray corrected (A/m)"
            if "Core Type" in keys:
                core_type="Core Type"
            else: core_type="Type" 
            if 'Run Number' in keys: run_number_key='Run Number'
            if 'Test No.' in keys: run_number_key='Test No.'
            if 'Test Changed On' in keys: date_key='Test Changed On'
            if "Timestamp (UTC)" in keys: date_key="Timestamp (UTC)"
            if "Section" in keys: sect_key="Section"
            if "Sect" in keys: sect_key="Sect"
            if 'Section Half' in keys: half_key='Section Half'
            if "A/W" in keys: half_key="A/W"
            if "Text ID" in keys: text_id="Text ID"
            if "Text Id" in keys: text_id="Text Id"
            for line in file_input[1:]:
              InRec={}
              test=0
              recs=line.split(',')
              for k in range(len(keys)):
                  if len(recs)==len(keys): 
                      InRec[keys[k]]=line.split(',')[k]
              if InRec['Exp']!="": test=1 # get rid of pesky blank lines
              if test==1:
                run_number=""
                inst="IODP-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 "SHLF" not in InRec[text_id] or 'dscr' in csv_file :  # assume discrete sample
                    specimen=expedition+'-'+location+'-'+InRec['Core']+InRec[core_type]+"-"+InRec[sect_key]+'-'+InRec[half_key]+'-'+str(InRec[interval_key])
                else: # mark as continuous measurements
                    specimen=expedition+'-'+location+'-'+InRec['Core']+InRec[core_type]+"_"+InRec[sect_key]+InRec[half_key]+'-'+str(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 "SHLF" not in InRec[text_id]: 
                    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_key]!= "": run_number=InRec[run_number_key]
                datestamp=InRec[date_key].split() # date time is second line of file
                if '/' in datestamp[0]:
                    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"
                if '-' in datestamp[0]:
                    mmddyy=datestamp[0].split('-') # break into month day year
                    date=mmddyy[0]+':'+mmddyy[1]+":"+mmddyy[2] +':' +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+':IODP-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 'Treatment Type' in InRec.keys() and InRec['Treatment Type']!="":
                    if 'Alternating Frequency' in InRec['Treatment Type']:
                        MagRec['magic_method_codes'] = 'LT-AF-Z'
                        inst=inst+':I`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+':IODP-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:
              #   print 'Boo-boo somewhere - no idea where'
    if not file_found:
        print "No .csv files were found"
        return False, "No .csv files were found"
    if len(SpecRecs)>0:
        print 'spec_file', spec_file
        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)
    if pmag.magic_write(meas_file,Fixed,'magic_measurements'):
        print 'data stored in ',meas_file
        return True, meas_file
    else:
        print 'no data found.  bad magfile?'
        return False, 'no data found.  bad magfile?'
Example #12
0
def main(command_line=True, **kwargs):
    """
    NAME
        cit_magic.py

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

    SYNTAX
        cit_magic.py [command line options]

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

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

    NOTES:
         Sample naming convention:
        [1] XXXXY: where XXXX is an arbitrary length site designation and Y
            is the single character sample designation.  e.g., TG001a is the
            first sample from site TG001.    [default]
        [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
        [3: default] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
        [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
        [5] site name = sample name
        [6] site name entered in site_name column in the orient.txt format input file  -- NOT CURRENTLY SUPPORTED
        [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
        NB: all others you will have to either customize your
            self or e-mail [email protected] or [email protected] for help.
    """

    #
    #              NB: use PHI, THETA = -1 -1 to signal that it changes, i.e. in anisotropy experiment
    #        -ac B : peak AF field (in mT) for ARM acquisition, default is none
    #
    #initialize variables
    norm = 'cc'
    samp_con, Z = '3', 1
    meas_file = 'magic_measurements.txt'
    spec_file = 'er_specimens.txt'
    samp_file = 'er_samples.txt'
    site_file = 'er_sites.txt'
    ErSpecs, ErSamps, ErSites, ErLocs, ErCits = [], [], [], [], []
    MeasRecs = []
    specnum, units, locname = 0, "1", "unknown"
    citation = "This study"
    dir_path = '.'
    args = sys.argv
    if command_line:
        if '-WD' in args:
            ind = args.index("-WD")
            dir_path = args[ind + 1]
        if "-h" in args:
            print(main.__doc__)
            return False
        if "-usr" in args:
            ind = args.index("-usr")
            user = args[ind + 1]
        if '-F' in args:
            ind = args.index("-F")
            meas_file = args[ind + 1]
        if '-Fsp' in args:
            ind = args.index("-Fsp")
            spec_file = args[ind + 1]
        if '-Fsa' in args:
            ind = args.index("-Fsa")
            samp_file = args[ind + 1]
        if '-Fsi' in args:  # LORI addition
            ind = args.index("-Fsi")
            site_file = args[ind + 1]
        if '-loc' in args:
            ind = args.index("-loc")
            locname = args[ind + 1]
        if '-mcd' in args:
            ind = args.index("-mcd")
            methods = args[ind + 1]
        else:
            methods = 'SO-MAG'
        if '-spc' in args:
            ind = args.index("-spc")
            specnum = -int(args[ind + 1])
        if '-n' in args:
            ind = args.index("-n")
            norm = args[ind + 1]
        if "-A" in args:
            avg = 1
        else:
            avg = 0
        if '-dc' in args:
            ind = args.index('-dc')
            DC_FIELD, DC_PHI, DC_THETA = list(
                map(float, args[ind + 1].strip('( ) [ ]').split(',')))
            DC_FIELD *= 1e-6
            yn = ''
            GET_DC_PARAMS = False
        else:
            GET_DC_PARAMS, DC_FIELD, DC_PHI, DC_THETA, yn = True, 0, 0, -90, ''
        if "-ncn" in args:
            ind = args.index("-ncn")
            samp_con = sys.argv[ind + 1]
            if "4" in samp_con:
                if "-" not in samp_con:
                    print(
                        "option [4] must be in form 4-Z where Z is an integer")
                    return False, "naming convention option [4] must be in form 4-Z where Z is an integer"
                else:
                    Z = samp_con.split("-")[1]
                    samp_con = "4"
            elif "7" in samp_con:
                if "-" not in samp_con:
                    print(
                        "option [7] must be in form 7-Z where Z is an integer")
                    return False, "naming convention option [7] must be in form 7-Z where Z is an integer"
                else:
                    Z = samp_con.split("-")[1]
                    samp_con = "7"
        if '-f' in args:
            ind = args.index("-f")
            magfile = args[ind + 1]
        if '-ID' in args:
            ind = args.index('-ID')
            input_dir_path = args[ind + 1]
        else:
            input_dir_path = os.path.split(magfile)[0]
        output_dir_path = dir_path
        # LJ

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

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

    magfile = os.path.join(input_dir_path, magfile)
    spec_file = os.path.join(output_dir_path, spec_file)
    samp_file = os.path.join(output_dir_path, samp_file)
    site_file = os.path.join(output_dir_path, site_file)
    meas_file = os.path.join(output_dir_path, meas_file)
    FIRST_GET_DC = True
    try:
        with open(magfile, 'r') as file_input:
            File = file_input.readlines()
    except Exception as ex:
        print("bad sam file name: ", magfile)
        return False, "bad sam file name"
    if len(File) == 1:
        File = File[0].split('\r')
        File = [x + "\r\n" for x in File]
    sids, ln, format = [], 0, 'CIT'
    formats = ['CIT', '2G', 'APP', 'JRA']
    if File[ln].strip() == 'CIT': ln += 1
    ErLocRec = {}
    ErLocRec["er_location_name"] = locname
    ErLocRec["er_citation_names"] = citation
    comment = File[ln]
    if comment == 'CIT':
        format = comment
        ln += 1
    comment = File[ln]
    print(comment)
    ln += 1
    specimens, samples, sites = [], [], []
    if format == 'CIT':
        line = File[ln].split()
        site_lat = line[0]
        site_lon = line[1]
        ErLocRec["location_begin_lat"] = site_lat
        ErLocRec["location_begin_lon"] = site_lon
        ErLocRec["location_end_lat"] = site_lat
        ErLocRec["location_end_lon"] = site_lon
        ErLocs.append(ErLocRec)
        try:
            Cdec = float(line[2])
        except ValueError:
            pdb.set_trace()
        for k in range(ln + 1, len(File)):
            line = File[k]
            rec = line.split()
            if rec == []: continue
            specimen = rec[0]
            specimens.append(specimen)
    for specimen in specimens:
        ErSpecRec, ErSampRec, ErSiteRec = {}, {}, {}
        if specnum != 0:
            sample = specimen[:specnum]
        else:
            sample = specimen
        site = pmag.parse_site(sample, samp_con, Z)
        ErSpecRec['er_specimen_name'] = specimen
        ErSpecRec['er_sample_name'] = sample
        ErSpecRec['er_site_name'] = site
        ErSpecRec['er_location_name'] = locname
        ErSpecRec['er_citation_names'] = citation
        ErSampRec['er_sample_name'] = sample
        ErSampRec['er_site_name'] = site
        ErSampRec['er_location_name'] = locname
        ErSampRec['er_citation_names'] = citation
        ErSampRec['magic_method_codes'] = methods
        ErSampRec['sample_declination_correction'] = '%7.1f' % (Cdec)
        ErSiteRec['er_site_name'] = site
        ErSiteRec['er_location_name'] = locname
        ErSiteRec['er_citation_names'] = citation
        ErSiteRec['site_lat'] = site_lat
        ErSiteRec['site_lon'] = site_lon
        with open(os.path.join(input_dir_path, specimen), 'r') as finput:
            Lines = list(finput.readlines())
        comment = ""
        line = Lines[0].split()
        if len(line) > 2:
            comment = line[2]
        info = Lines[1].split()
        vol = float(info[-1])
        if vol != 1.0:
            if norm == 'cc': units = "1"
            if norm == 'm3': units = "2"
            ErSpecRec['specimen_weight'] = ""
            if units == "1" or "":
                ErSpecRec['specimen_volume'] = '%10.3e' % (vol * 1e-6)
            else:
                ErSpecRec['specimen_volume'] = '%10.3e' % (vol)
        else:
            if norm == 'cc': units = "1"
            if norm == 'm3': units = "2"
            ErSpecRec['specimen_volume'] = ""
            if units == "1" or "":
                ErSpecRec['specimen_weight'] = '%10.3e' % (vol * 1e-3)
            else:
                ErSpecRec['specimen_weight'] = '%10.3e' % (vol)
        dip = float(info[-2])
        dip_direction = float(info[-3]) + Cdec + 90.
        sample_dip = -float(info[-4])
        sample_azimuth = float(info[-5]) + Cdec - 90.
        if len(info) > 5:
            ErSampRec['sample_height'] = info[-6]
        else:
            ErSampRec['sample_height'] = '0'
        ErSampRec['sample_azimuth'] = '%7.1f' % (sample_azimuth)
        ErSampRec['sample_dip'] = '%7.1f' % (sample_dip)
        ErSampRec['sample_bed_dip'] = '%7.1f' % (dip)
        ErSampRec['sample_bed_dip_direction'] = '%7.1f' % (dip_direction)
        ErSampRec['sample_class'] = ''
        ErSampRec['sample_type'] = ''
        ErSampRec['sample_lithology'] = ''
        if Cdec != 0 or Cdec != "":
            ErSampRec['magic_method_codes'] = 'SO-CMD-NORTH'
        else:
            ErSampRec['magic_method_codes'] = 'SO-MAG'
        for line in Lines[2:len(Lines)]:
            if line == '\n': continue
            MeasRec = ErSpecRec.copy()
            #           Remove specimen_volume and specimen_weight as they do not exits in the magic_measurement table
            del MeasRec["specimen_volume"]
            del MeasRec["specimen_weight"]
            treat_type = line[0:3]
            if treat_type[1] == '.':
                treat_type = 'NRM'
            treat = line[2:6]
            try:
                float(treat)
            except ValueError:
                treat = line[3:6]
            if treat_type.startswith('NRM'):
                MeasRec['magic_method_codes'] = 'LT-NO'
                MeasRec['measurement_temp'] = '273'
                MeasRec['treatment_temp'] = '273'
                MeasRec['treatment_dc_field'] = '0'
                MeasRec['treatment_dc_field_phi'] = '%1.2f' % DC_PHI
                MeasRec['treatment_dc_field_theta'] = '%1.2f' % DC_THETA
                MeasRec['treatment_ac_field'] = '0'
            elif treat_type.startswith('AF'):
                MeasRec['magic_method_codes'] = 'LT-AF-Z'
                MeasRec['measurement_temp'] = '273'
                MeasRec['treatment_temp'] = '273'
                MeasRec['treatment_dc_field'] = '0'
                MeasRec['treatment_dc_field_phi'] = '%1.2f' % DC_PHI
                MeasRec['treatment_dc_field_theta'] = '%1.2f' % DC_THETA
                if treat.strip() == '':
                    MeasRec['treatment_ac_field'] = '0'
                else:
                    MeasRec['treatment_ac_field'] = '%10.3e' % (float(treat) *
                                                                1e-3)
            elif treat_type.startswith('ARM'):
                MeasRec['magic_method_codes'] = "LP-ARM"
                MeasRec['measurement_temp'] = '273'
                MeasRec['treatment_temp'] = '273'
                MeasRec['treatment_dc_field'] = '0'
                MeasRec['treatment_dc_field_phi'] = '%1.2f' % DC_PHI
                MeasRec['treatment_dc_field_theta'] = '%1.2f' % DC_THETA
                if treat.strip() == '':
                    MeasRec['treatment_ac_field'] = '0'
                else:
                    MeasRec['magic_method_codes'] = "LP-ARM-AFD"
                    MeasRec['treatment_ac_field'] = '%10.3e' % (float(treat) *
                                                                1e-3)
            elif treat_type.startswith('TT'):
                MeasRec['magic_method_codes'] = 'LT-T-Z'
                MeasRec['measurement_temp'] = '273'
                if treat.strip() == '':
                    MeasRec['treatment_temp'] = '273'
                else:
                    MeasRec['treatment_temp'] = '%7.1f' % (float(treat) + 273)
                MeasRec['treatment_dc_field'] = '0'
                MeasRec['treatment_dc_field_phi'] = '%1.2f' % DC_PHI
                MeasRec['treatment_dc_field_theta'] = '%1.2f' % DC_THETA
                MeasRec['treatment_ac_field'] = '0'
            elif treat_type.startswith('LT') or treat_type.startswith('LN2'):
                MeasRec['magic_method_codes'] = 'LT-LT-Z'
                MeasRec['measurement_temp'] = '273'
                MeasRec['treatment_temp'] = '77'
                MeasRec['treatment_dc_field'] = '0'
                MeasRec['treatment_dc_field_phi'] = '%1.2f' % DC_PHI
                MeasRec['treatment_dc_field_theta'] = '%1.2f' % DC_THETA
                MeasRec['treatment_ac_field'] = '0'
            elif line[
                    4] == '0':  #assume decimal IZZI format 0 field thus can hardcode the dc fields
                MeasRec['magic_method_codes'] = 'LT-T-Z'
                MeasRec['measurement_temp'] = '273'
                MeasRec['treatment_temp'] = str(int(treat_type) + 273)
                MeasRec['treatment_dc_field'] = '0'
                MeasRec['treatment_dc_field_phi'] = '%1.2f' % DC_PHI
                MeasRec['treatment_dc_field_theta'] = '%1.2f' % DC_THETA
                MeasRec['treatment_ac_field'] = '0'
            elif line[4] == '1':  #assume decimal IZZI format in constant field
                if GET_DC_PARAMS:
                    GET_DC_PARAMS, FIRST_GET_DC, yn, DC_FIELD, DC_PHI, DC_THETA = get_dc_params(
                        FIRST_GET_DC, specimen, treat_type, yn)
                MeasRec['magic_method_codes'] = 'LT-T-I'
                MeasRec['measurement_temp'] = '273'
                MeasRec['treatment_temp'] = str(int(treat_type) + 273)
                MeasRec['treatment_dc_field'] = '%1.2e' % DC_FIELD
                MeasRec['treatment_dc_field_phi'] = '%1.2f' % DC_PHI
                MeasRec['treatment_dc_field_theta'] = '%1.2f' % DC_THETA
                MeasRec['treatment_ac_field'] = '0'
            elif line[4] == '2':  #assume decimal IZZI format PTRM step
                if GET_DC_PARAMS:
                    GET_DC_PARAMS, FIRST_GET_DC, yn, DC_FIELD, DC_PHI, DC_THETA = get_dc_params(
                        FIRST_GET_DC, specimen, treat_type, yn)
                MeasRec['magic_method_codes'] = 'LT-PTRM-I'
                MeasRec['measurement_temp'] = '273'
                MeasRec['treatment_temp'] = str(int(treat_type) + 273)
                MeasRec['treatment_dc_field'] = '%1.2e' % DC_FIELD
                MeasRec['treatment_dc_field_phi'] = '%1.2f' % DC_PHI
                MeasRec['treatment_dc_field_theta'] = '%1.2f' % DC_THETA
                MeasRec['treatment_ac_field'] = '0'
            else:
                print("trouble with your treatment steps")
            MeasRec['measurement_dec'] = line[46:51]
            MeasRec['measurement_inc'] = line[52:58]
            M = '%8.2e' % (float(line[31:39]) * vol * 1e-3)  # convert to Am2
            MeasRec['measurement_magn_moment'] = M
            MeasRec['measurement_csd'] = '%7.1f' % (eval(line[41:46]))
            MeasRec["measurement_positions"] = '1'
            MeasRec['measurement_standard'] = 'u'
            if len(line) > 60:
                MeasRec['magic_instrument_codes'] = line[85:]
                MeasRec['measurement_sd_x'] = '%8.2e' % (
                    float(line[58:67]) * 1e-8)  #(convert e-5emu to Am2)
                MeasRec['measurement_sd_y'] = '%8.2e' % (float(line[67:76]) *
                                                         1e-8)
                MeasRec['measurement_sd_z'] = '%8.2e' % (float(line[76:85]) *
                                                         1e-8)
            MeasRecs.append(MeasRec)
        ErSpecs.append(ErSpecRec)
        if sample not in samples:
            samples.append(sample)
            ErSamps.append(ErSampRec)
        site = pmag.parse_site(sample, samp_con, Z)
        if site not in sites:
            sites.append(site)
            ErSites.append(ErSiteRec)
    pmag.magic_write(spec_file, ErSpecs, 'er_specimens')
    print('specimens stored in ', spec_file)
    pmag.magic_write(samp_file, ErSamps, 'er_samples')
    print('samples stored in ', samp_file)
    pmag.magic_write(site_file, ErSites, 'er_sites')
    print('sites stored in ', site_file)
    Fixed = pmag.measurements_methods(MeasRecs, avg)
    pmag.magic_write(meas_file, Fixed, 'magic_measurements')
    print('data stored in ', meas_file)
    return True, meas_file
Example #13
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=input("DC lab field for ARM step: [50uT] ")
            if ans=="":
                arm_labfield=50e-6
            else:
                arm_labfield=float(ans)*1e-6
            ans=input("temperature for total trm step: [600 C] ")
            if ans=="":
                trm_peakT=600+273 # convert to kelvin
            else:
                trm_peakT=float(ans)+273 # convert to kelvin
        if "G" in codes: methcode="LT-AF-G"
    if "D" in codes: methcode="LT-AF-D"
    if "TRM" in codes:
        demag="T"
        trm=1
    if demag=="T" and "ANI" in codes:
        methcode="LP-AN-TRM"
    if demag=="AF" and "ANI" in codes:
        methcode="LP-AN-ARM"
        if labfield==0: labfield=50e-6
        if peakfield==0: peakfield=.180
    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,'r').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 list(MagRec.keys()):MeasRec[key]=MagRec[key]
                    for item in rec:
                             items=item.split('=')
                             if 'demag_level' in items[0]:
                                treat= float(items[1])
                                if treat!=0:
                                    MeasRec['magic_method_codes']='LT-AF-Z'
                                    inst=inst+':ODP-SRM-AF'
                                    MeasRec["treatment_ac_field"]='%8.3e'%(treat*1e-3) # AF demag in treat mT => T
                             if 'inclination_w_tray_w_bkgrd' in items[0]: MeasRec['measurement_inc']=items[1]
                             if 'declination_w_tray_w_bkgrd' in items[0]: MeasRec['measurement_dec']=items[1]
                             if 'intensity_w_tray_w_bkgrd' in items[0]: MeasRec['measurement_magn_moment']='%8.3e'%(float(items[1])*vol) # convert intensity from A/m to Am^2 using vol
                             MeasRec['magic_instrument_codes']=inst
                             if 'offset' in items[0]:
                                 depth='%7.3f'%(float(sect-1)*1.5+float(items[1]))
                                 SampRec['core_depth']=depth
                                 MeasRec['er_specimen_name']=depth
                                 MeasRec['er_sample_name']=depth
                                 MeasRec['er_site_name']=depth
                                 MeasRec['er_location_name']='location'
                                 SampRec['er_sample_name']=depth
                                 SampRec['er_site_name']=depth
                                 SampRec['er_location_name']='location'
                                 MeasRec['measurement_number']='1'
                    SampRecs.append(SampRec)
                    MagRecs.append(MeasRec)
    pmag.magic_write(samp_file,SampRecs,'er_samples')
    print('samples stored in ',samp_file)
    Fixed=pmag.measurements_methods(MagRecs,noave)
    pmag.magic_write(meas_file,Fixed,'magic_measurements')
    print('data stored in ',meas_file)
Example #14
0
def cit_magic(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)
        [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
        [5] site name = sample name
        [6] site name entered in site_name column in the orient.txt format input file  -- NOT CURRENTLY SUPPORTED
        [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
        NB: all others you will have to either customize your
            self or e-mail [email protected] for help.
    """
    #
    #initialize variables
    norm = 'cc'
    samp_con, Z = '3', 1
    meas_file = '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()
    if len(File) == 1:
        File = File[0].split('\r')
        File = map(lambda x: x + "\r\n", File)
    sids, ln, format = [], 0, 'CIT'
    formats = ['CIT', '2G', 'APP', 'JRA']
    if File[ln].strip() == 'CIT': ln += 1
    ErLocRec = {}
    ErLocRec["er_location_name"] = locname
    ErLocRec["er_citation_names"] = citation
    comment = File[ln]
    if comment == 'CIT':
        format = comment
        ln += 1
    comment = File[ln]
    #print comment
    ln += 1
    specimens, samples, sites = [], [], []
    if format == 'CIT':
        line = File[ln].split()
        site_lat = line[0]
        site_lon = line[1]
        ErLocRec["location_begin_lat"] = site_lat
        ErLocRec["location_begin_lon"] = site_lon
        ErLocRec["location_end_lat"] = site_lat
        ErLocRec["location_end_lon"] = site_lon
        ErLocs.append(ErLocRec)
        try:
            Cdec = float(line[2])
        except ValueError:
            pdb.set_trace()
        for k in range(ln + 1, len(File)):
            line = File[k]
            rec = line.split()
            if rec == []: continue
            specimen = rec[0]
            specimens.append(specimen)
    for specimen in specimens:
        ErSpecRec, ErSampRec, ErSiteRec = {}, {}, {}
        if specnum != 0:
            sample = specimen[:specnum]
        else:
            sample = specimen
        site = pmag.parse_site(sample, samp_con, Z)
        ErSpecRec['er_specimen_name'] = specimen
        ErSpecRec['er_sample_name'] = sample
        ErSpecRec['er_site_name'] = site
        ErSpecRec['er_location_name'] = locname
        ErSpecRec['er_citation_names'] = citation
        ErSampRec['er_sample_name'] = sample
        ErSampRec['er_site_name'] = site
        ErSampRec['er_location_name'] = locname
        ErSampRec['er_citation_names'] = citation
        ErSampRec['magic_method_codes'] = methods
        ErSampRec['sample_declination_correction'] = '%7.1f' % (Cdec)
        ErSiteRec['er_site_name'] = site
        ErSiteRec['er_location_name'] = locname
        ErSiteRec['er_citation_names'] = citation
        ErSiteRec['site_lat'] = site_lat
        ErSiteRec['site_lon'] = site_lon
        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[2:6]
            try:
                float(treat)
            except ValueError:
                treat = line[3:6]
            if treat_type.startswith('NRM'):
                MeasRec['magic_method_codes'] = 'LT-NO'
                MeasRec['measurement_temp'] = '273'
                MeasRec['treatment_temp'] = '273'
                MeasRec['treatment_dc_field'] = '0'
                MeasRec['treatment_ac_field'] = '0'
            elif treat_type.startswith('AF'):
                MeasRec['magic_method_codes'] = 'LT-AF-Z'
                MeasRec['measurement_temp'] = '273'
                MeasRec['treatment_temp'] = '273'
                MeasRec['treatment_dc_field'] = '0'
                if treat.strip() == '':
                    MeasRec['treatment_ac_field'] = '0'
                else:
                    MeasRec['treatment_ac_field'] = '%10.3e' % (float(treat) *
                                                                1e-3)
            elif treat_type.startswith('ARM'):
                MeasRec['magic_method_codes'] = "LP-ARM"
                MeasRec['measurement_temp'] = '273'
                MeasRec['treatment_temp'] = '273'
                MeasRec['treatment_dc_field'] = '0'
                if treat.strip() == '':
                    MeasRec['treatment_ac_field'] = '0'
                else:
                    MeasRec['magic_method_codes'] = "LP-ARM-AFD"
                    MeasRec['treatment_ac_field'] = '%10.3e' % (float(treat) *
                                                                1e-3)
            elif treat_type.startswith('TT'):
                MeasRec['magic_method_codes'] = 'LT-T-Z'
                MeasRec['measurement_temp'] = '273'
                if treat.strip() == '':
                    MeasRec['treatment_temp'] = '273'
                else:
                    MeasRec['treatment_temp'] = '%7.1f' % (float(treat) + 273)
                MeasRec['treatment_dc_field'] = '0'
                MeasRec['treatment_ac_field'] = '0'
            elif treat_type.startswith('LT') or treat_type.startswith('LN2'):
                MeasRec['magic_method_codes'] = 'LT-LT-Z'
                MeasRec['measurement_temp'] = '273'
                MeasRec['treatment_temp'] = '77'
                MeasRec['treatment_dc_field'] = '0'
                MeasRec['treatment_ac_field'] = '0'
            else:
                print "trouble with your treatment steps"
            MeasRec['measurement_dec'] = line[46:51]
            MeasRec['measurement_inc'] = line[52:58]
            M = '%8.2e' % (float(line[31:39]) * vol * 1e-3)  # convert to Am2
            MeasRec['measurement_magn_moment'] = M
            MeasRec['measurement_csd'] = '%7.1f' % (eval(line[41:46]))
            MeasRec["measurement_positions"] = '1'
            MeasRec['measurement_standard'] = 'u'
            if len(line) > 60:
                MeasRec['magic_instrument_codes'] = line[85:]
                MeasRec['measurement_sd_x'] = '%8.2e' % (
                    float(line[58:67]) * 1e-8)  #(convert e-5emu to Am2)
                MeasRec['measurement_sd_y'] = '%8.2e' % (float(line[67:76]) *
                                                         1e-8)
                MeasRec['measurement_sd_z'] = '%8.2e' % (float(line[76:85]) *
                                                         1e-8)
            MeasRecs.append(MeasRec)
        ErSpecs.append(ErSpecRec)
        if sample not in samples:
            samples.append(sample)
            ErSamps.append(ErSampRec)
        site = pmag.parse_site(sample, samp_con, Z)
        if site not in sites:
            sites.append(site)
            ErSites.append(ErSiteRec)
    er_specs = magic_write(spec_file, ErSpecs, 'er_specimens')
    #print 'specimens stored in "er_specs"'
    er_samps = magic_write(samp_file, ErSamps, 'er_samples')
    #print 'samples stored in "er_samps"'
    er_sites = magic_write(site_file, ErSites, 'er_sites')
    #print 'sites stored in "er_sites"'
    Fixed = pmag.measurements_methods(MeasRecs, avg)
    magic_meas = magic_write(meas_file, Fixed, 'magic_measurements')
    #print 'data stored in "magic_meas"'
    return True, meas_file, str(er_specs), str(er_samps), str(er_sites), str(
        magic_meas)
Example #15
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,'r')
        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.) # 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)
Example #16
0
def main(command_line=True, **kwargs):
    """
    NAME
        jr6_jr6_magic.py
 
    DESCRIPTION
        converts JR6 .jr6 format files to magic_measurements format files

    SYNTAX
        jr6_jr6_magic.py [command line options]

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

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

    samp_file = 'er_samples.txt'
    meas_file = 'magic_measurements.txt'
    tmp_file = "fixed.jr6"
    meth_code, JR = "", 0
    #
    # get command line arguments
    #

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

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

    ErSampRec, ErSiteRec = {}, {}

    # parse data

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

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

    if JR == 0:  #
        data.columns = [
            'er_specimen_name', 'step', 'x', 'y', 'z', 'expon',
            'sample_azimuth', 'sample_dip', 'sample_bed_dip_direction',
            'sample_bed_dip', 'bed_dip_dir2', 'bed_dip2', 'param1', 'param2',
            'param3', 'param4', 'measurement_csd'
        ]
        cart = np.array([data['x'], data['y'], data['z']]).transpose()
    else:  # measured on the Joides Resolution JR6
        data.columns = [
            'er_specimen_name', 'step', 'negz', 'y', 'x', 'expon',
            'sample_azimuth', 'sample_dip', 'sample_bed_dip_direction',
            'sample_bed_dip', 'bed_dip_dir2', 'bed_dip2', 'param1', 'param2',
            'param3', 'param4', 'measurement_csd'
        ]
        cart = np.array([data['x'], data['y'], -data['negz']]).transpose()
    dir = pmag.cart2dir(cart).transpose()
    data['measurement_dec'] = dir[0]
    data['measurement_inc'] = dir[1]
    data['measurement_magn_moment'] = dir[2] * (
        10.0**
        data['expon']) * volume  # the data are in A/m - this converts to Am^2
    data['measurement_magn_volume'] = dir[2] * (10.0**data['expon']
                                                )  # A/m  - data in A/m
    data['sample_dip'] = -data['sample_dip']
    DGEOs, IGEOs = [], []
    for ind in range(len(data)):
        dgeo, igeo = pmag.dogeo(data.iloc[ind]['measurement_dec'],
                                data.iloc[ind]['measurement_inc'],
                                data.iloc[ind]['sample_azimuth'],
                                data.iloc[ind]['sample_dip'])
        DGEOs.append(dgeo)
        IGEOs.append(igeo)
    data['specimen_dec'] = DGEOs
    data['specimen_inc'] = IGEOs
    data['specimen_tilt'] = '1'
    if specnum != 0:
        data['er_sample_name'] = data['er_specimen_name'][:specnum]
    else:
        data['er_sample_name'] = data['er_specimen_name']

    if int(samp_con) in [1, 2, 3, 4, 5, 7]:
        data['er_site_name'] = pmag.parse_site(data['er_sample_name'],
                                               samp_con, Z)
    # else:
    #     if 'er_site_name' in ErSampRec.keys():er_site_name=ErSampRec['er_site_name']
    #     if 'er_location_name' in ErSampRec.keys():er_location_name=ErSampRec['er_location_name']

    # Configure the er_sample table

    for rowNum, row in data.iterrows():
        sampleFlag = 0
        for sampRec in SampOuts:
            if sampRec['er_sample_name'] == row['er_sample_name']:
                sampleFlag = 1
                break
        if sampleFlag == 0:
            ErSampRec['er_sample_name'] = row['er_sample_name']
            ErSampRec['sample_azimuth'] = str(row['sample_azimuth'])
            ErSampRec['sample_dip'] = str(row['sample_dip'])
            ErSampRec['magic_method_codes'] = meth_code
            ErSampRec['er_location_name'] = er_location_name
            ErSampRec['er_site_name'] = row['er_site_name']
            ErSampRec['er_citation_names'] = 'This study'
            SampOuts.append(ErSampRec.copy())

    # Configure the magic_measurements table

    for rowNum, row in data.iterrows():
        MagRec = {}
        #        MagRec['measurement_description']='Date: '+date
        MagRec["er_citation_names"] = "This study"
        MagRec['er_location_name'] = er_location_name
        MagRec['er_site_name'] = row['er_site_name']
        MagRec['er_sample_name'] = row['er_sample_name']
        MagRec['magic_software_packages'] = version_num
        MagRec["treatment_temp"] = '%8.3e' % (273)  # room temp in kelvin
        MagRec["measurement_temp"] = '%8.3e' % (273)  # room temp in kelvin
        MagRec["measurement_flag"] = 'g'
        MagRec["measurement_standard"] = 'u'
        MagRec["measurement_number"] = '1'
        MagRec["er_specimen_name"] = row['er_specimen_name']
        MagRec["treatment_ac_field"] = '0'
        if row['step'] == 'NRM':
            meas_type = "LT-NO"
        elif row['step'][0:2] == 'AD':
            meas_type = "LT-AF-Z"
            treat = float(row['step'][2:])
            MagRec["treatment_ac_field"] = '%8.3e' % (
                treat * 1e-3)  # convert from mT to tesla
        elif row['step'][0] == 'TD':
            meas_type = "LT-T-Z"
            treat = float(row['step'][2:])
            MagRec["treatment_temp"] = '%8.3e' % (treat + 273.
                                                  )  # temp in kelvin
        else:  # need to add IRM, and ARM options
            print("measurement type unknown", row['step'])
            return False, "measurement type unknown"
        MagRec["measurement_magn_moment"] = str(row['measurement_magn_moment'])
        MagRec["measurement_magn_volume"] = str(row['measurement_magn_volume'])
        MagRec["measurement_dec"] = str(row['measurement_dec'])
        MagRec["measurement_inc"] = str(row['measurement_inc'])
        MagRec['magic_method_codes'] = meas_type
        MagRecs.append(MagRec.copy())
    pmag.magic_write(samp_file, SampOuts, 'er_samples')
    print("sample orientations put in ", samp_file)
    MagOuts = pmag.measurements_methods(MagRecs, noave)
    pmag.magic_write(meas_file, MagOuts, 'magic_measurements')
    print("results put in ", meas_file)
    print("exit!")
    return True, meas_file
Example #17
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 NOT CURRENTLY SUPPORTED
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.
 
            [8] synthetic - has no site name
            [9] ODP naming convention 
            [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,'r')
        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)
Example #18
0
def main(command_line=True, **kwargs):
    """
    NAME
        _2g_bin_magic.py
   
    DESCRIPTION
        takes the binary 2g format magnetometer files and converts them to magic_measurements, er_samples.txt and er_sites.txt file
 
    SYNTAX
        2g_bin_magic.py [command line options]

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

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

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

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

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

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

    if ID:
        input_dir_path = ID
    else:
        input_dir_path = dir_path

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

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

    DESCRIPTION
        converts ODP LIMS discrete sample format files to magic_measurements format files


    SYNTAX
        iodp_descr_magic.py [command line options]

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

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

    # format variables

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

    DESCRIPTION
        converts ODP LIMS discrete sample format files to magic_measurements format files


    SYNTAX
        iodp_descr_magic.py [command line options]

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

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

    # format variables

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

    SYNTAX
        ldeo_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify .ldeo format input file, required
        -F FILE: specify output file, default is magic_measurements.txt
        -Fsy: specify er_synthetics file, default is er_sythetics.txt
        -LP [colon delimited list of protocols, include all that apply]
            AF:  af demag
            T: thermal including thellier but not trm acquisition
            S: Shaw method
            I: IRM (acquisition)
            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

        -ARM_dc # default value is 50e-6
        -ARM_temp # default is 600c

        -ncn NCON:  specify naming convention: default is #1 below
        -A: don't average replicate measurements
       Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name same as sample
            [6] site is entered under a separate column -- NOT CURRENTLY SUPPORTED
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.
 
            [8] synthetic - has no site name
    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
    codelist = ''
    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'
    magfile = ''
    trm = 0
    irm = 0
    specnum = 0
    coil = ""
    arm_labfield = 50e-6
    trm_peakT = 600 + 273
    #
    # get command line arguments
    #

    meas_file = "magic_measurements.txt"
    user = ""
    if command_line:
        if "-h" in args:
            print(main.__doc__)
            return False
        if "-usr" in args:
            ind = args.index("-usr")
            user = args[ind + 1]
        if '-F' in args:
            ind = args.index("-F")
            meas_file = args[ind + 1]
        if '-Fsy' in args:
            ind = args.index("-Fsy")
            synfile = args[ind + 1]
        if '-f' in args:
            ind = args.index("-f")
            magfile = args[ind + 1]
        if "-dc" in args:
            ind = args.index("-dc")
            labfield = float(args[ind + 1]) * 1e-6
            phi = float(args[ind + 2])
            theta = float(args[ind + 3])
        if "-ac" in args:
            ind = args.index("-ac")
            peakfield = float(args[ind + 1]) * 1e-3
        if "-spc" in args:
            ind = args.index("-spc")
            specnum = int(args[ind + 1])
        if "-loc" in args:
            ind = args.index("-loc")
            er_location_name = args[ind + 1]
        if '-syn' in args:
            syn = 1
            ind = args.index("-syn")
            institution = args[ind + 1]
            syntype = args[ind + 2]
            if '-fsy' in args:
                ind = args.index("-fsy")
                synfile = args[ind + 1]
        if "-ins" in args:
            ind = args.index("-ins")
            inst = args[ind + 1]
        if "-A" in args: noave = 1
        if "-ncn" in args:
            ind = args.index("-ncn")
            samp_con = sys.argv[ind + 1]
        if '-LP' in args:
            ind = args.index("-LP")
            codelist = args[ind + 1]
        if "-V" in args:
            ind = args.index("-V")
            coil = args[ind + 1]
        if '-ARM_dc' in args:
            ind = args.index("-ARM_dc")
            arm_labfield = args[ind + 1]
        if '-ARM_temp' in args:
            ind = args.index('-ARM_temp')
            trm_peakT = args[ind + 1]

    if not command_line:
        user = kwargs.get('user', '')
        meas_file = kwargs.get('meas_file', 'magic_measurements.txt')
        synfile = kwargs.get('synfile', 'er_synthetics.txt')
        # rm samp_file = kwargs.get('samp_file', '')
        magfile = kwargs.get('magfile', '')
        labfield = int(kwargs.get('labfield', 0)) * 1e-6
        phi = int(kwargs.get('phi', 0))
        theta = int(kwargs.get('theta', 0))
        peakfield = int(kwargs.get('peakfield', 0)) * 1e-3
        specnum = int(kwargs.get('specnum', 0))
        er_location_name = kwargs.get('er_location_name', '')
        # rm samp_infile = kwargs.get('samp_infile', '')
        syn = kwargs.get('syn', 0)
        institution = kwargs.get('institution', '')
        syntype = kwargs.get('syntype', '')
        inst = kwargs.get('inst', '')
        noave = kwargs.get('noave', 0)  # 0 means "do average", is default
        samp_con = kwargs.get('samp_con', '1')
        codelist = kwargs.get('codelist', '')
        coil = kwargs.get('coil', '')
        arm_labfield = kwargs.get('arm_labfield', 50e-6)
        trm_peakT = kwargs.get('trm_peakT', 600 + 273)

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

    if specnum != 0: specnum = -specnum

    if "4" in samp_con:
        if "-" not in samp_con:
            print(
                "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(
                "naming convention 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 = "4"

    codes = codelist.split(':')
    if "AF" in codes:
        demag = 'AF'
        if not labfield: methcode = "LT-AF-Z"
        if labfield: methcode = "LT-AF-I"
    if "T" in codes:
        demag = "T"
        if not labfield: methcode = "LT-T-Z"
        if labfield: 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
        # should use arm_labfield and trm_peakT as well, but these values are currently never asked for
    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 coil:
        methcode = "LP-IRM"
        irmunits = "V"
        if coil not in ["1", "2", "3"]:
            print(main.__doc__)
            print('not a valid coil specification')
            return False, 'not a valid coil specification'

    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:  # ldeo file format
        #
        # find start of data:
        #
        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 len(SynRecs) > 0:
        pmag.magic_write(synfile, SynRecs, 'er_synthetics')
        print("synthetics put in ", synfile)
    return True, meas_file
Example #22
0
def main(command_line=True, **kwargs):
    """
    NAME
        pmd_magic.py
 
    DESCRIPTION
        converts PMD (Enkin)  format files to magic_measurements format files

    SYNTAX
        pmd_magic.py [command line options]

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

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

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

    #
    # get command line arguments
    #

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

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

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

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

    SYNTAX
        ldeo_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify .ldeo format input file, required
        -F FILE: specify output file, default is magic_measurements.txt
        -Fsy: specify er_synthetics file, default is er_sythetics.txt
        -LP [colon delimited list of protocols, include all that apply]
            AF:  af demag
            T: thermal including thellier but not trm acquisition
            S: Shaw method
            I: IRM (acquisition)
            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

        -ARM_dc # default value is 50e-6
        -ARM_temp # default is 600c

        -ncn NCON:  specify naming convention: default is #1 below
        -A: don't average replicate measurements
       Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name same as sample
            [6] site is entered under a separate column -- NOT CURRENTLY SUPPORTED
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.
 
            [8] synthetic - has no site name
    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
    codelist = ''
    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'
    magfile = ''
    trm=0
    irm=0
    specnum=0
    coil=""
    arm_labfield = 50e-6
    trm_peakT = 600+273
    #
    # get command line arguments
    #

    meas_file="magic_measurements.txt"
    user=""
    if command_line:
        if "-h" in args:
            print main.__doc__
            return False
        if "-usr" in args:
            ind=args.index("-usr")
            user=args[ind+1]
        if '-F' in args:
            ind=args.index("-F")
            meas_file=args[ind+1]
        if '-Fsy' in args:
            ind=args.index("-Fsy")
            synfile=args[ind+1]
        if '-f' in args:
            ind=args.index("-f")
            magfile=args[ind+1]
        if "-dc" in args:
            ind=args.index("-dc")
            labfield=float(args[ind+1])*1e-6
            phi=float(args[ind+2])
            theta=float(args[ind+3])
        if "-ac" in args:
            ind=args.index("-ac")
            peakfield=float(args[ind+1])*1e-3
        if "-spc" in args:
            ind=args.index("-spc")
            specnum=int(args[ind+1])
        if "-loc" in args:
            ind=args.index("-loc")
            er_location_name=args[ind+1]
        if '-syn' in args:
            syn=1
            ind=args.index("-syn")
            institution=args[ind+1]
            syntype=args[ind+2]
            if '-fsy' in args:
                ind=args.index("-fsy")
                synfile=args[ind+1]
        if "-ins" in args:
            ind=args.index("-ins")
            inst=args[ind+1]
        if "-A" in args: noave=1
        if "-ncn" in args:
            ind=args.index("-ncn")
            samp_con=sys.argv[ind+1]
        if '-LP' in args:
            ind=args.index("-LP")
            codelist=args[ind+1]
        if "-V" in args:
            ind=args.index("-V")
            coil=args[ind+1]
        if '-ARM_dc' in args:
            ind = args.index("-ARM_dc")
            arm_labfield = args[ind+1]
        if '-ARM_temp' in args:
            ind = args.index('-ARM_temp')
            trm_peakT = args[ind+1]


    if not command_line:
        user = kwargs.get('user', '')
        meas_file = kwargs.get('meas_file', 'magic_measurements.txt')
        synfile = kwargs.get('synfile', 'er_synthetics.txt')
        # rm samp_file = kwargs.get('samp_file', '')
        magfile = kwargs.get('magfile', '')
        labfield = int(kwargs.get('labfield', 0)) *1e-6
        phi = int(kwargs.get('phi', 0))
        theta = int(kwargs.get('theta', 0))
        peakfield = int(kwargs.get('peakfield', 0))*1e-3
        specnum = int(kwargs.get('specnum', 0))
        er_location_name = kwargs.get('er_location_name', '')
        # rm samp_infile = kwargs.get('samp_infile', '')
        syn = kwargs.get('syn', 0)        
        institution = kwargs.get('institution', '')
        syntype = kwargs.get('syntype', '')
        inst = kwargs.get('inst', '')
        noave = kwargs.get('noave', 0) # 0 means "do average", is default
        samp_con = kwargs.get('samp_con', '1')
        codelist = kwargs.get('codelist', '')
        coil = kwargs.get('coil', '')
        arm_labfield = kwargs.get('arm_labfield', 50e-6)
        trm_peakT = kwargs.get('trm_peakT', 600+273)


    # format/organize variables
    if magfile:
        try:
            input=open(magfile,'rU')
        except:
            print "bad mag file name"
            return False, "bad mag file name"
    else: 
        print "mag_file field is required option"
        print main.__doc__
        return False, "mag_file field is required option"
        
    if specnum!=0:specnum=-specnum

    if "4" in samp_con:
        if "-" not in samp_con:
            print "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 "naming convention 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="4"

    codes=codelist.split(':')
    if "AF" in codes:
        demag='AF' 
        if not labfield: methcode="LT-AF-Z"
        if labfield: methcode="LT-AF-I"
    if "T" in codes:
        demag="T"
        if not labfield: methcode="LT-T-Z"
        if labfield: 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
        # should use arm_labfield and trm_peakT as well, but these values are currently never asked for
    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 coil:
        methcode="LP-IRM"
        irmunits="V"
        if coil not in ["1","2","3"]:
            print main.__doc__
            print 'not a valid coil specification'
            return False, 'not a valid coil specification'

    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: # ldeo file format
#
# find start of data:
#
        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 len(SynRecs)>0:
        pmag.magic_write(synfile,SynRecs,'er_synthetics')
        print "synthetics put in ",synfile
    return True, meas_file
Example #24
0
def main(command_line=True, **kwargs):
    """
    NAME
        bgc_magic.py

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

    SYNTAX
        bgc_magic.py [command line options]

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

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

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

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

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

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

    ErSampRec = {}

    # parse data

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

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

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

    data['measurement_dec'] = direction[0]
    data['measurement_inc'] = direction[1]
    data['measurement_magn_moment'] = old_div(direction[2], 1000)  # the data are in EMU - this converts to Am^2 
    data['measurement_magn_volume'] = old_div((old_div(direction[2], 1000)), volume) # EMU  - data converted to A/m
    
    # Configure the er_sample table

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

    # Configure the magic_measurements table

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

    DESCRIPTION
        converts shipboard .jr6 format files to magic_measurements format files

    SYNTAX
        iodp_jr6_magic.py [command line options]

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

    INPUT
        JR6 .jr6 format file
    """
    def fix_separation(filename, new_filename):
        old_file = open(filename, 'r')
        data = old_file.readlines()
        new_data = []
        for line in data:
            new_line = line.replace('-', ' -')
            new_line = new_line.replace('  ', ' ')
            new_data.append(new_line)
        new_file = open(new_filename, 'w')
        for s in new_data:
            new_file.write(s)
        old_file.close()
        new_file.close()
        return new_filename

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


# initialize some stuff

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

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

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

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

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

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

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

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

        else:
            print('sample name not found: ', row['specname'])
    MagOuts = pmag.measurements_methods(MagRecs, noave)
    file_created, error_message = pmag.magic_write(meas_file, MagOuts,
                                                   'magic_measurements')
    if file_created:
        return True, meas_file
    else:
        return False, 'Results not written to file'
Example #27
0
def main():
    """
    NAME
        odp_dcs_magic.py

    DESCRIPTION
        converts ODP discrete sample format files to magic_measurements format files

    SYNTAX
        odp_dsc_magic.py [command line options]

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

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

    Fixed=pmag.measurements_methods(MagOuts,noave)
    pmag.magic_write(meas_file,Fixed,'magic_measurements')
    print('data stored in ',meas_file)
Example #28
0
def main(command_line=True, **kwargs):
    """
    NAME
        jr6_txt_magic.py
 
    DESCRIPTION
        converts JR6 .txt format files to magic_measurements format files

    SYNTAX
        jr6_txt_magic.py [command line options]

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

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

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


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

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


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

    ErSampRec,ErSiteRec={},{}

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

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

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

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

        er_specimen_name = sampleName

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

        if int(samp_con) in [1, 2, 3, 4, 5, 7]:
            er_site_name=pmag.parse_site(er_sample_name,samp_con,Z)
        else:
            print("-W- Using unreognized sample convention option: ", samp_con)
        # else:
        #     if 'er_site_name' in ErSampRec.keys():er_site_name=ErSampRec['er_site_name']
        #     if 'er_location_name' in ErSampRec.keys():er_location_name=ErSampRec['er_location_name']

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

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

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

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

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

    MagOuts=pmag.measurements_methods(MagRecs,noave)
    pmag.magic_write(samp_file,SampOuts,'er_samples') 
    print("sample orientations put in ",samp_file)
    pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    print("results put in ",meas_file)
    return True, meas_file
Example #29
0
def main(command_line=True, **kwargs):
    """
    NAME
        sio_magic.py

    DESCRIPTION
        converts SIO .mag format files to magic_measurements format files

    SYNTAX
        sio_magic.py [command line options]

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

            [8] synthetic - has no site name
            [9] ODP naming convention
    INPUT
        Best to put separate experiments (all AF, thermal, thellier, trm aquisition, Shaw, etc.) in
           seperate .mag files (eg. af.mag, thermal.mag, etc.)

        Format of SIO .mag files:
        Spec Treat CSD Intensity Declination Inclination [optional metadata string]


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

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


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

         Optional metatdata string:  mm/dd/yy;hh:mm;[dC,mT];xx.xx;UNITS;USER;INST;NMEAS
             hh in 24 hours.
             dC or mT units of treatment XXX (see Treat above) for thermal or AF respectively
             xx.xxx   DC field
             UNITS of DC field (microT, mT)
             INST:  instrument code, number of axes, number of positions (e.g., G34 is 2G, three axes,
                    measured in four positions)
             NMEAS: number of measurements in a single position (1,3,200...)


    """
    # initialize some stuff
    mag_file = None
    codelist = None
    infile_type="mag"
    noave=0
    methcode,inst="LP-NO",""
    phi,theta,peakfield,labfield=0,0,0,0
    pTRM,MD,samp_con,Z=0,0,'1',1
    dec=[315,225,180,135,45,90,270,270,270,90,180,180,0,0,0]
    inc=[0,0,0,0,0,-45,-45,0,45,45,45,-45,-90,-45,45]
    tdec=[0,90,0,180,270,0,0,90,0]
    tinc=[0,0,90,0,0,-90,0,0,90]
    missing=1
    demag="N"
    er_location_name=""
    citation='This study'
    args=sys.argv
    fmt='old'
    syn=0
    synfile='er_synthetics.txt'
    samp_infile,Samps='',[]
    trm=0
    irm=0
    specnum=0
    coil=""
    mag_file=""
#
# get command line arguments
#
    meas_file="magic_measurements.txt"
    user=""
    if not command_line:
        user = kwargs.get('user', '')
        meas_file = kwargs.get('meas_file', '')
        syn_file = kwargs.get('syn_file', '')
        mag_file = kwargs.get('mag_file', '')
        labfield = kwargs.get('labfield', '')
        if labfield:
            labfield = float(labfield) *1e-6
        else:
            labfield = 0
        phi = kwargs.get('phi', 0)
        if phi:
            phi = float(phi)
        else:
            phi = 0
        theta = kwargs.get('theta', 0)
        if theta:
            theta=float(theta)
        else:
            theta = 0
        peakfield = kwargs.get('peakfield', 0)
        if peakfield:
            peakfield=float(peakfield) *1e-3
        else:
            peakfield = 0
        specnum = kwargs.get('specnum', 0)
        samp_con = kwargs.get('samp_con', '1')
        er_location_name = kwargs.get('er_location_name', '')
        samp_infile = kwargs.get('samp_infile', '')
        syn = kwargs.get('syn', 0)
        institution = kwargs.get('institution', '')
        syntype = kwargs.get('syntype', '')
        inst = kwargs.get('inst', '')
        noave = kwargs.get('noave', 0)
        codelist = kwargs.get('codelist', '')
        coil = kwargs.get('coil', '')
        cooling_rates = kwargs.get('cooling_rates', '')
    if command_line:
        if "-h" in args:
            print(main.__doc__)
            return False
        if "-usr" in args:
            ind=args.index("-usr")
            user=args[ind+1]
        if '-F' in args:
            ind=args.index("-F")
            meas_file=args[ind+1]
        if '-Fsy' in args:
            ind=args.index("-Fsy")
            synfile=args[ind+1]
        if '-f' in args:
            ind=args.index("-f")
            mag_file=args[ind+1]
        if "-dc" in args:
            ind=args.index("-dc")
            labfield=float(args[ind+1])*1e-6
            phi=float(args[ind+2])
            theta=float(args[ind+3])
        if "-ac" in args:
            ind=args.index("-ac")
            peakfield=float(args[ind+1])*1e-3
        if "-spc" in args:
            ind=args.index("-spc")
            specnum=int(args[ind+1])
        if "-loc" in args:
            ind=args.index("-loc")
            er_location_name=args[ind+1]
        if "-fsa" in args:
            ind=args.index("-fsa")
            samp_infile = args[ind+1]
        if '-syn' in args:
            syn=1
            ind=args.index("-syn")
            institution=args[ind+1]
            syntype=args[ind+2]
            if '-fsy' in args:
                ind=args.index("-fsy")
                synfile=args[ind+1]
        if "-ins" in args:
            ind=args.index("-ins")
            inst=args[ind+1]
        if "-A" in args: noave=1
        if "-ncn" in args:
            ind=args.index("-ncn")
            samp_con=sys.argv[ind+1]
        if '-LP' in args:
            ind=args.index("-LP")
            codelist=args[ind+1]

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


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

    if codelist:
        codes=codelist.split(':')
        if "AF" in codes:
            demag='AF'
            if'-dc' not in args: methcode="LT-AF-Z"
            if'-dc' in args: methcode="LT-AF-I"
        if "T" in codes:
            demag="T"
            if '-dc' not in args: methcode="LT-T-Z"
            if '-dc' in args: methcode="LT-T-I"
        if "I" in codes:
            methcode="LP-IRM"
            irmunits="mT"
        if "I3d" in codes:
            methcode="LT-T-Z:LP-IRM-3D"
        if "S" in codes:
            demag="S"
            methcode="LP-PI-TRM:LP-PI-ALT-AFARM"
            trm_labfield=labfield
            ans=input("DC lab field for ARM step: [50uT] ")
            if ans=="":
                arm_labfield=50e-6
            else:
                arm_labfield=float(ans)*1e-6
            ans=input("temperature for total trm step: [600 C] ")
            if ans=="":
                trm_peakT=600+273 # convert to kelvin
            else:
                trm_peakT=float(ans)+273 # convert to kelvin
        if "G" in codes: methcode="LT-AF-G"
        if "D" in codes: methcode="LT-AF-D"
        if "TRM" in codes:
            demag="T"
            trm=1
        if "CR" in     codes:
            demag="T"
            cooling_rate_experiment=1
            if command_line:
                ind=args.index("CR")
                cooling_rates=args[ind+1]
                cooling_rates_list=cooling_rates.split(',')
            else:
                cooling_rates_list=str(cooling_rates).split(',')
    if demag=="T" and "ANI" in codes:
        methcode="LP-AN-TRM"
    if demag=="T" and "CR" in codes:
        methcode="LP-CR-TRM"
    if demag=="AF" and "ANI" in codes:
        methcode="LP-AN-ARM"
        if labfield==0: labfield=50e-6
        if peakfield==0: peakfield=.180
    SynRecs,MagRecs=[],[]
    version_num=pmag.get_version()


    ##################################

    if 1:
    #if infile_type=="SIO format":
        for line in lines:
            instcode=""
            if len(line)>2:
                SynRec={}
                MagRec={}
                MagRec['er_location_name']=er_location_name
                MagRec['magic_software_packages']=version_num
                MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
                MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
                MagRec["treatment_ac_field"]='0'
                MagRec["treatment_dc_field"]='0'
                MagRec["treatment_dc_field_phi"]='0'
                MagRec["treatment_dc_field_theta"]='0'
                meas_type="LT-NO"
                rec=line.split()
                if rec[1]==".00":rec[1]="0.00"
                treat=rec[1].split('.')
                if methcode=="LP-IRM":
                    if irmunits=='mT':
                        labfield=float(treat[0])*1e-3
                    else:
                        labfield=pmag.getfield(irmunits,coil,treat[0])
                    if rec[1][0]!="-":
                        phi,theta=0.,90.
                    else:
                        phi,theta=0.,-90.
                    meas_type="LT-IRM"
                    MagRec["treatment_dc_field"]='%8.3e'%(labfield)
                    MagRec["treatment_dc_field_phi"]='%7.1f'%(phi)
                    MagRec["treatment_dc_field_theta"]='%7.1f'%(theta)
                if len(rec)>6:
                  code1=rec[6].split(';') # break e.g., 10/15/02;7:45 indo date and time
                  if len(code1)==2: # old format with AM/PM
                    missing=0
                    code2=code1[0].split('/') # break date into mon/day/year
                    code3=rec[7].split(';') # break e.g., AM;C34;200  into time;instr/axes/measuring pos;number of measurements
                    yy=int(code2[2])
                    if yy <90:
                        yyyy=str(2000+yy)
                    else: yyyy=str(1900+yy)
                    mm=int(code2[0])
                    if mm<10:
                        mm="0"+str(mm)
                    else: mm=str(mm)
                    dd=int(code2[1])
                    if dd<10:
                        dd="0"+str(dd)
                    else: dd=str(dd)
                    time=code1[1].split(':')
                    hh=int(time[0])
                    if code3[0]=="PM":hh=hh+12
                    if hh<10:
                        hh="0"+str(hh)
                    else: hh=str(hh)
                    min=int(time[1])
                    if min<10:
                       min= "0"+str(min)
                    else: min=str(min)
                    MagRec["measurement_date"]=yyyy+":"+mm+":"+dd+":"+hh+":"+min+":00.00"
                    MagRec["measurement_time_zone"]='SAN'
                    if inst=="":
                        if code3[1][0]=='C':instcode='SIO-bubba'
                        if code3[1][0]=='G':instcode='SIO-flo'
                    else:
                        instcode=''
                    MagRec["measurement_positions"]=code3[1][2]
                  elif len(code1)>2: # newest format (cryo7 or later)
                    if "LP-AN-ARM" not in methcode:labfield=0
                    fmt='new'
                    date=code1[0].split('/') # break date into mon/day/year
                    yy=int(date[2])
                    if yy <90:
                        yyyy=str(2000+yy)
                    else: yyyy=str(1900+yy)
                    mm=int(date[0])
                    if mm<10:
                        mm="0"+str(mm)
                    else: mm=str(mm)
                    dd=int(date[1])
                    if dd<10:
                        dd="0"+str(dd)
                    else: dd=str(dd)
                    time=code1[1].split(':')
                    hh=int(time[0])
                    if hh<10:
                        hh="0"+str(hh)
                    else: hh=str(hh)
                    min=int(time[1])
                    if min<10:
                       min= "0"+str(min)
                    else:
                        min=str(min)
                    MagRec["measurement_date"]=yyyy+":"+mm+":"+dd+":"+hh+":"+min+":00.00"
                    MagRec["measurement_time_zone"]='SAN'
                    if inst=="":
                        if code1[6][0]=='C':
                            instcode='SIO-bubba'
                        if code1[6][0]=='G':
                            instcode='SIO-flo'
                    else:
                        instcode=''
                    if len(code1)>1:
                        MagRec["measurement_positions"]=code1[6][2]
                    else:
                        MagRec["measurement_positions"]=code1[7]   # takes care of awkward format with bubba and flo being different
                    if user=="":user=code1[5]
                    if code1[2][-1]=='C':
                        demag="T"
                        if code1[4]=='microT' and float(code1[3])!=0. and "LP-AN-ARM" not in methcode: labfield=float(code1[3])*1e-6
                    if code1[2]=='mT' and methcode!="LP-IRM":
                        demag="AF"
                        if code1[4]=='microT' and float(code1[3])!=0.: labfield=float(code1[3])*1e-6
                    if code1[4]=='microT' and labfield!=0. and meas_type!="LT-IRM":
                        phi,theta=0.,-90.
                        if demag=="T": meas_type="LT-T-I"
                        if demag=="AF": meas_type="LT-AF-I"
                        MagRec["treatment_dc_field"]='%8.3e'%(labfield)
                        MagRec["treatment_dc_field_phi"]='%7.1f'%(phi)
                        MagRec["treatment_dc_field_theta"]='%7.1f'%(theta)
                    if code1[4]=='' or labfield==0. and meas_type!="LT-IRM":
                        if demag=='T':meas_type="LT-T-Z"
                        if demag=="AF":meas_type="LT-AF-Z"
                        MagRec["treatment_dc_field"]='0'
                if syn==0:
                    MagRec["er_specimen_name"]=rec[0]
                    MagRec["er_synthetic_name"]=""
                    MagRec["er_site_name"]=""
                    if specnum!=0:
                        MagRec["er_sample_name"]=rec[0][:specnum]
                    else:
                        MagRec["er_sample_name"]=rec[0]
                    if samp_infile and Samps: # if samp_infile was provided AND yielded sample data
                        samp=pmag.get_dictitem(Samps,'er_sample_name',MagRec['er_sample_name'],'T')
                        if len(samp)>0:
                            MagRec["er_location_name"]=samp[0]["er_location_name"]
                            MagRec["er_site_name"]=samp[0]["er_site_name"]
                        else:
                            MagRec['er_location_name']=''
                            MagRec["er_site_name"]=''
                    elif int(samp_con)!=6:
                        site=pmag.parse_site(MagRec['er_sample_name'],samp_con,Z)
                        MagRec["er_site_name"]=site
                    if MagRec['er_site_name']=="":
                        print('No site name found for: ',MagRec['er_specimen_name'],MagRec['er_sample_name'])
                    if MagRec["er_location_name"]=="":
                        print('no location name for: ',MagRec["er_specimen_name"])
                else:
                    MagRec["er_specimen_name"]=rec[0]
                    if specnum!=0:
                        MagRec["er_sample_name"]=rec[0][:specnum]
                    else:
                        MagRec["er_sample_name"]=rec[0]
                    MagRec["er_site_name"]=""
                    MagRec["er_synthetic_name"]=MagRec["er_specimen_name"]
                    SynRec["er_synthetic_name"]=MagRec["er_specimen_name"]
                    site=pmag.parse_site(MagRec['er_sample_name'],samp_con,Z)
                    SynRec["synthetic_parent_sample"]=site
                    SynRec["er_citation_names"]="This study"
                    SynRec["synthetic_institution"]=institution
                    SynRec["synthetic_type"]=syntype
                    SynRecs.append(SynRec)
                if float(rec[1])==0:
                    pass
                elif demag=="AF":
                    if methcode != "LP-AN-ARM":
                        MagRec["treatment_ac_field"]='%8.3e' %(float(rec[1])*1e-3) # peak field in tesla
                        if meas_type=="LT-AF-Z": MagRec["treatment_dc_field"]='0'
                    else: # AARM experiment
                        if treat[1][0]=='0':
                            meas_type="LT-AF-Z:LP-AN-ARM:"
                            MagRec["treatment_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
                            MagRec["treatment_dc_field"]='%8.3e'%(0)
                            if labfield!=0 and methcode!="LP-AN-ARM": print("Warning - inconsistency in mag file with lab field - overriding file with 0")
                        else:
                            meas_type="LT-AF-I:LP-AN-ARM"
                            ipos=int(treat[0])-1
                            MagRec["treatment_dc_field_phi"]='%7.1f' %(dec[ipos])
                            MagRec["treatment_dc_field_theta"]='%7.1f'% (inc[ipos])
                            MagRec["treatment_dc_field"]='%8.3e'%(labfield)
                            MagRec["treatment_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
                elif demag=="T" and methcode == "LP-AN-TRM":
                    MagRec["treatment_temp"]='%8.3e' % (float(treat[0])+273.) # temp in kelvin
                    if treat[1][0]=='0':
                        meas_type="LT-T-Z:LP-AN-TRM"
                        MagRec["treatment_dc_field"]='%8.3e'%(0)
                        MagRec["treatment_dc_field_phi"]='0'
                        MagRec["treatment_dc_field_theta"]='0'
                    else:
                        MagRec["treatment_dc_field"]='%8.3e'%(labfield)
                        if treat[1][0]=='7': # alteration check as final measurement
                                meas_type="LT-PTRM-I:LP-AN-TRM"
                        else:
                                meas_type="LT-T-I:LP-AN-TRM"

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


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


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

                    MagRec["treatment_temp"]='%8.3e' % (float(treat[0])+273.) # temp in kelvin
                    if treat[1][0]=='0':
                        meas_type="LT-T-Z:LP-CR-TRM"
                        MagRec["treatment_dc_field"]='%8.3e'%(0)
                        MagRec["treatment_dc_field_phi"]='0'
                        MagRec["treatment_dc_field_theta"]='0'
                    else:
                        MagRec["treatment_dc_field"]='%8.3e'%(labfield)
                        if treat[1][0]=='7': # alteration check as final measurement
                                meas_type="LT-PTRM-I:LP-CR-TRM"
                        else:
                                meas_type="LT-T-I:LP-CR-TRM"
                        MagRec["treatment_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                        MagRec["treatment_dc_field_theta"]='%7.1f' % (theta) # labfield theta

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


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



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

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

    SYNTAX
        cit_magic.py [command line options]

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

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

    NOTES:
         Sample naming convention:
        [1] XXXXY: where XXXX is an arbitrary length site designation and Y
            is the single character sample designation.  e.g., TG001a is the
            first sample from site TG001.    [default]
        [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
        [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
        [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
        [5] site name = sample name
        [6] site name entered in site_name column in the orient.txt format input file  -- NOT CURRENTLY SUPPORTED
        [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
        NB: all others you will have to either customize your
            self or e-mail [email protected] for help.
    """
    #
    #initialize variables
    norm='cc'
    samp_con,Z='3',1
    meas_file='measurements.txt'
    spec_file='specimens.txt'
    samp_file='samples.txt'
    site_file='sites.txt'
    Specs,Samps,Sites,Locs=[],[],[],[]
    MeasRecs=[]
    specnum,units,locname=0,"1","unknown"
    citation="This study"
    dir_path='.'
    args=sys.argv
    if command_line:
        if '-WD' in args:
            ind=args.index("-WD")
            dir_path=args[ind+1]
        if "-h" in args:
            print(main.__doc__)
            return False
        if "-usr" in args:
            ind=args.index("-usr")
            user=args[ind+1]
        if '-F' in args:
            ind=args.index("-F")
            meas_file=args[ind+1]
        if '-Fsp' in args:
            ind=args.index("-Fsp")
            spec_file=args[ind+1]
        if '-Fsa' in args:
            ind=args.index("-Fsa")
            samp_file=args[ind+1]
        if '-Fsi' in args:   # LORI addition
            ind=args.index("-Fsi")
            site_file=args[ind+1]
        if '-loc' in args:
            ind=args.index("-loc")
            locname=args[ind+1]
        if '-mcd' in args:
            ind=args.index("-mcd")
            methods=args[ind+1]
        else:
            methods='SO-MAG'
        if '-spc' in args:
            ind=args.index("-spc")
            specnum=-int(args[ind+1])
        if '-n' in args:
            ind=args.index("-n")
            norm=args[ind+1]
        if "-A" in args:
            avg=1
        else:
            avg=0
        if "-ncn" in args:
            ind=args.index("-ncn")
            samp_con=sys.argv[ind+1]
            if "4" in samp_con:
                if "-" not in samp_con:
                    print("option [4] must be in form 4-Z where Z is an integer")
                    return False, "naming convention option [4] must be in form 4-Z where Z is an integer"
                else:
                    Z=samp_con.split("-")[1]
                    samp_con="4"
        if '-f' in args:
            ind=args.index("-f")
            magfile=args[ind+1]
        if '-ID' in args:
            ind = args.index('-ID')
            input_dir_path = args[ind+1]
        else:
            input_dir_path = dir_path
        output_dir_path = dir_path
        if '-dm' in args:
            dmi = args.index('-dm')
            data_model = args[dmi+1]

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

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

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

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

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

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

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

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

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

    return True, meas_file
Example #31
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