示例#1
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
示例#2
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
示例#3
0
def main():
    """
    NAME
        huji_sample_magic.py
   
    DESCRIPTION
        takes tab delimited Hebrew University sample file and converts to MagIC formatted tables
 
    SYNTAX
        huji_sample_magic.py [command line options]

    OPTIONS
        -f FILE: specify input file
        -Fsa FILE: specify sample output file, default is: er_samples.txt 
        -Fsi FILE: specify site output file, default is: er_sites.txt 
        -Iso:  import sample orientation info - default is to set sample_az/dip to 0,0
        -ncn NCON:  specify naming convention: default is #1 below
        -mcd: specify sampling method codes as a colon delimited string:  [default is: FS-FD:SO-POM]
             FS-FD field sampling done with a drill
             FS-H field sampling done with hand samples
             FS-LOC-GPS  field location done with GPS
             FS-LOC-MAP  field location done with map
             SO-POM   a Pomeroy orientation device was used
             SO-ASC   an ASC orientation device was used
             SO-MAG   orientation with magnetic compass
        -loc: location name, default="unknown"

    INPUT FORMAT
        Input files must be tab delimited:
            Samp  Az Dip Dip_dir Dip 
        Orientation convention:
             Lab arrow azimuth = mag_azimuth; Lab arrow dip = 90-field_dip
                e.g. field_dip is degrees from horizontal of drill direction
       
         Magnetic declination convention:
             Az is already corrected in file 
    
         Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name = sample name
            [6] site name entered in site_name column in the orient.txt format input file  -- NOT CURRENTLY SUPPORTED
            [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
            NB: all others you will have to either customize your
                self or e-mail [email protected] for help.

    OUTPUT
            output saved in er_samples.txt  will overwrite any existing files 
    """
    #
    # initialize variables
    #
    version_num=pmag.get_version()
    samp_file,or_con,corr = "er_samples.txt","1","1"
    site_file='er_sites.txt'
    args=sys.argv
    date,lat,lon="","",""  # date of sampling, latitude (pos North), longitude (pos East)
    bed_dip,bed_dip_dir="",""
    participantlist=""
    sites=[]   # list of site names
    Lats,Lons=[],[] # list of latitudes and longitudes
    SampRecs,SiteRecs,ImageRecs,imagelist=[],[],[],[]  # lists of Sample records and Site records
    samp_con,Z,average_bedding="1",1,"0"
    newbaseline,newbeddir,newbeddip="","",""
    meths='FS-FD:SO-POM:SO-SUN'
    delta_u="0"
    sclass,lithology,type="","",""
    newclass,newlith,newtype='','',''
    user=""
    or_con='3'
    corr=="3"
    DecCorr=0.
    location_name="unknown"
    ignore=1
    #
    #
    if "-h" in args:
        print(main.__doc__)
        sys.exit()
    if "-f" in args:
        ind=args.index("-f")
        orient_file=sys.argv[ind+1]
    else:
        "Must have orientation file name"
        sys.exit() 
    if "-Fsa" in args:
        ind=args.index("-Fsa")
        samp_file=sys.argv[ind+1]
    if "-ncn" in args:
        ind=args.index("-ncn")
        samp_con=sys.argv[ind+1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print("option [4] must be in form 3-Z where Z is an integer")
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="4"
            print(samp_con, Z)
    if "-mcd" in args:
        ind=args.index("-mcd")
        meths=(sys.argv[ind+1])
    if "-loc" in args:
        ind=args.index("-loc")
        location_name=(sys.argv[ind+1])
    if "-Iso" in args: ignore=0
    #
    # read in file to convert
    #
    azfile=open(orient_file,'r')
    AzDipDat=azfile.readlines()
    azfile.close()
    SampOut=[]
    SiteOut=[]
    for line in AzDipDat[1:]: 
      orec=line.split()
      if len(orec)>1:
        labaz,labdip=pmag.orient(float(orec[1]),float(orec[2]),or_con)
        bed_dip_dir=(orec[3])
        bed_dip=(orec[4])
        SampRec={}
        SiteRec={}
        SampRec["er_location_name"]=location_name
        SampRec["er_citation_names"]="This study"
        SiteRec["er_location_name"]=location_name
        SiteRec["er_citation_names"]="This study"
        SiteRec["site_class"]=""
        SiteRec["site_lithology"]=""
        SiteRec["site_type"]=""
        SiteRec["site_definition"]="s"
        SiteRec["er_citation_names"]="This study"
    #
    # parse information common to all orientation methods
    #
        SampRec["er_sample_name"]=orec[0]
        SampRec["sample_bed_dip_direction"]=orec[3]
        SampRec["sample_bed_dip"]=orec[4]
        SiteRec["site_bed_dip_direction"]=orec[3]
        SiteRec["site_bed_dip"]=orec[4]
        if ignore==0:
            SampRec["sample_dip"]='%7.1f'%(labdip)
            SampRec["sample_azimuth"]='%7.1f'%(labaz)
        else:
            SampRec["sample_dip"]='0'
            SampRec["sample_azimuth"]='0'
        SampRec["sample_lat"]=orec[5]
        SampRec["sample_lon"]=orec[6]
        SiteRec["site_lat"]=orec[5]
        SiteRec["site_lon"]=orec[6]
        methods=meths.split(":")
        SampRec["magic_method_codes"]=meths
        site=pmag.parse_site(orec[0],samp_con,Z) # parse out the site name
        SampRec["er_site_name"]=site
        SampRec['magic_software_packages']=version_num
        SiteRec["er_site_name"]=site
        SiteRec['magic_software_packages']=version_num
        SampOut.append(SampRec)
        SiteOut.append(SiteRec)
    pmag.magic_write(samp_file,SampOut,"er_samples")
    print("Sample info saved in ", samp_file)
    pmag.magic_write(site_file,SiteOut,"er_sites")
    print("Site info saved in ", site_file)
示例#4
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
示例#5
0
def convert(**kwargs):
    # initialize some stuff
    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]
    demag="N"
    trm=0
    irm=0

    user = kwargs.get('user', '')
    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', 'measurements.txt')
    spec_file = kwargs.get('spec_file', 'specimens.txt') # specimen outfile
    samp_file = kwargs.get('samp_file', 'samples.txt')
    site_file = kwargs.get('site_file', 'sites.txt') # site outfile
    loc_file = kwargs.get('loc_file', 'locations.txt') # site outfile
    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))
    location = kwargs.get('location', 'unknown')
    noave = kwargs.get('noave', False) # 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)
    mv = kwargs.get('mv', 'v')

    # format/organize variables
    if magfile:
        try:
            infile=open(os.path.join(input_dir_path,magfile),'r')
        except IOError:
            print("bad mag file name")
            return False, "bad mag file name"
    else:
        print("mag_file field is required option")
        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"
    elif "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"
    else: Z=1

    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('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
    MeasRecs,SpecRecs,SampRecs,SiteRecs,LocRecs=[],[],[],[],[]
    version_num=pmag.get_version()
    # find start of data:
    DIspec=[]
    Data=infile.readlines()
    infile.close()
    for k in range(len(Data)):
        rec=Data[k].split()
        if len(rec)<=2: continue
        if rec[0].upper()=="LAT:" and len(rec)>3: lat,lon=rec[1],rec[3]; continue
        elif rec[0].upper()=="ID": continue
        MeasRec,SpecRec,SampRec,SiteRec,LocRec={},{},{},{},{}
        specimen=rec[0]
        if specnum!=0:
            sample=specimen[:specnum]
        else:
            sample=specimen
        site=pmag.parse_site(sample,samp_con,Z)
        if mv=='v':
            volume = float(rec[12])
            if volume > 0: susc_chi_volume='%10.3e'%(old_div((float(rec[11])*1e-5),volume)) #convert to SI (assume Bartington, 10-5 SI)
            else: susc_chi_volume='%10.3e'%(float(rec[11])*1e-5) #convert to SI (assume Bartington, 10-5 SI)
        else:
            mass = float(rec[12])
            if mass > 0: susc_chi_mass='%10.3e'%(old_div((float(rec[11])*1e-5),mass)) #convert to SI (assume Bartington, 10-5 SI)
            else: susc_chi_mass='%10.3e'%(float(rec[11])*1e-5) #convert to SI (assume Bartington, 10-5 SI)
        print((specimen,sample,site,samp_con,Z))

        #fill tables besides measurements
        if specimen!="" and specimen not in [x['specimen'] if 'specimen' in list(x.keys()) else "" for x in SpecRecs]:
            SpecRec['specimen'] = specimen
            SpecRec['sample'] = sample
            if mv=='v':
                SpecRec["susc_chi_volume"]=susc_chi_volume
                SpecRec["volume"]=volume
            else:
                SpecRec["susc_chi_mass"]=susc_chi_mass
                SpecRec["mass"]=mass
            SpecRecs.append(SpecRec)
        if sample!="" and sample not in [x['sample'] if 'sample' in list(x.keys()) else "" for x in SampRecs]:
            SampRec['sample'] = sample
            SampRec['site'] = site
            SampRecs.append(SampRec)
        if site!="" and site not in [x['site'] if 'site' in list(x.keys()) else "" for x in SiteRecs]:
            SiteRec['site'] = site
            SiteRec['location'] = location
            SiteRec['lat'] = lat
            SiteRec['lon'] = lon
            SiteRecs.append(SiteRec)
        if location!="" and location not in [x['location'] if 'location' in list(x.keys()) else "" for x in LocRecs]:
            LocRec['location']=location
            LocRec['lat_n'] = lat
            LocRec['lon_e'] = lon
            LocRec['lat_s'] = lat
            LocRec['lon_w'] = lon
            LocRecs.append(LocRec)

        #fill measurements
        MeasRec["treat_temp"]='%8.3e' % (273) # room temp in kelvin
        MeasRec["meas_temp"]='%8.3e' % (273) # room temp in kelvin
        MeasRec["treat_ac_field"]='0'
        MeasRec["treat_dc_field"]='0'
        MeasRec["treat_dc_field_phi"]='0'
        MeasRec["treat_dc_field_theta"]='0'
        meas_type="LT-NO"
        MeasRec["quality"]='g'
        MeasRec["standard"]='u'
        MeasRec["treat_step_num"]='1'
        MeasRec["specimen"]=specimen
#        if mv=='v': MeasRec["susc_chi_volume"]=susc_chi_volume
#        else: MeasRec["susc_chi_mass"]=susc_chi_mass
        MeasRec["dir_csd"]=rec[3]
        MeasRec["magn_moment"]='%10.3e'% (float(rec[4])*1e-7)
        MeasRec["dir_dec"]=rec[5]
        MeasRec["dir_inc"]=rec[6]
        MeasRec["citations"]="This study"
        if demag=="AF":
            if methcode != "LP-AN-ARM":
                MeasRec["treat_ac_field"]='%8.3e' %(float(rec[1])*1e-3) # peak field in tesla
                meas_type="LT-AF-Z"
                MeasRec["treat_dc_field"]='0'
            else: # AARM experiment
                if treat[1][0]=='0':
                    meas_type="LT-AF-Z"
                    MeasRec["treat_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
                else:
                    meas_type="LT-AF-I"
                    ipos=int(treat[0])-1
                    MeasRec["treat_dc_field_phi"]='%7.1f' %(dec[ipos])
                    MeasRec["treat_dc_field_theta"]='%7.1f'% (inc[ipos])
                    MeasRec["treat_dc_field"]='%8.3e'%(labfield)
                    MeasRec["treat_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')
            MeasRec["treat_temp"]='%8.3e' % (float(rec[1])+273.) # temp in kelvin
            meas_type="LT-T-Z"
            MeasRec["treat_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:
                    MeasRec["treat_dc_field"]='%8.3e' % (labfield) # labfield in tesla (convert from microT)
                    MeasRec["treat_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                    MeasRec["treat_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':
                        MeasRec["treat_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
        MeasRec['method_codes']=meas_type
        MeasRecs.append(MeasRec)

    con = nb.Contribution(output_dir_path,read_tables=[])

    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts=pmag.measurements_methods3(MeasRecs,noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    con.tables['specimens'].write_magic_file(custom_name=spec_file)
    con.tables['samples'].write_magic_file(custom_name=samp_file)
    con.tables['sites'].write_magic_file(custom_name=site_file)
    con.tables['locations'].write_magic_file(custom_name=loc_file)
    con.tables['measurements'].write_magic_file(custom_name=meas_file)

    return True, meas_file
示例#6
0
def main(command_line=True, **kwargs):
    """
    NAME
        utrecht_magic.py

    DESCRIPTION
        converts Utrecht magnetometer data files to magic_measurements files

    SYNTAX
        utrecht_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -f FILE: specify  input file, or
        -F FILE: specify output file, default is magic_measurements.txt
        -Fsa: specify er_samples format file for appending, default is new er_samples.txt (Not working yet)
        -WD: output directory for MagIC files
        -ncn: Site Naming Convention
         Site to Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2: default] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name = sample name
            [6] site name entered in site_name column in the orient.txt format input file  -- NOT CURRENTLY SUPPORTED
            [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
        -spc: number of characters to remove to generate sample names from specimen names
        -dmy: European date format
        -loc LOCNAME : specify location/study name
        -lat latitude of samples
        -lon longitude of samples
        -A: don't average replicate measurements
        -mcd: [SO-MAG,SO-SUN,SO-SIGHT...] supply how these samples were oriented
        -dc: B PHI THETA: dc lab field (in microTesla), phi,and theta must be input as a tuple "(DC,PHI,THETA)". If not input user will be asked for values, this is advantagious if there are differing dc fields between steps or specimens. Note: this currently only works with the decimal IZZI naming convetion (XXX.0,1,2,3 where XXX is the treatment temperature and 0 is a zero field step, 1 is in field, and 2 is a pTRM check, 3 is a tail check). All other steps are hardcoded dc_field = 0.

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

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

    meas_file='magic_measurements.txt'
    spec_file='er_specimens.txt'
    samp_file='er_samples.txt'
    site_file='er_sites.txt'
    meth_code = ""
    #
    # get command line arguments
    #
    if command_line:
        if '-WD' in sys.argv:
            ind = sys.argv.index('-WD')
            dir_path = sys.argv[ind+1]
        if '-ID' in sys.argv:
            ind = sys.argv.index('-ID')
            input_dir_path = sys.argv[ind+1]
        else:
            input_dir_path = dir_path
        output_dir_path = dir_path
        if "-h" in args:
            print(main.__doc__)
            return False
        if '-F' in args:
            ind = args.index("-F")
            meas_file = args[ind+1]
        if '-Fsp' in args:
            ind=args.index("-Fsp")
            spec_file=args[ind+1]
        if '-Fsa' in args:
            ind = args.index("-Fsa")
            samp_file = args[ind+1]
        if '-Fsi' in args:   # LORI addition
            ind=args.index("-Fsi")
            site_file=args[ind+1]
            #try:
            #    open(samp_file,'r')
            #    ErSamps,file_type=pmag.magic_read(samp_file)
            #    print 'sample information will be appended to ', samp_file
            #except:
            #    print samp_file,' not found: sample information will be stored in new er_samples.txt file'
            #    samp_file = output_dir_path+'/er_samples.txt'
        if '-f' in args:
            ind = args.index("-f")
            mag_file = args[ind+1]
        if "-loc" in args:
            ind = args.index("-loc")
            er_location_name = args[ind+1]
        if "-lat" in args:
            ind = args.index("-lat")
            site_lat = args[ind+1]
        if "-lon" in args:
            ind = args.index("-lon")
            site_lon = args[ind+1]
        if "-A" in args:
            noave = 1
        if "-mcd" in args:
            ind = args.index("-mcd")
            meth_code = args[ind+1]
            #samp_con='5'
        if "-ncn" in args:
            ind=args.index("-ncn")
            samp_con=sys.argv[ind+1]
            if "4" in samp_con:
                if "-" not in samp_con:
                    print("option [4] must be in form 4-Z where Z is an integer")
                    return False, "naming convention option [4] must be in form 4-Z where Z is an integer"
                else:
                    site_num=samp_con.split("-")[1]
                    samp_con="4"
            elif "7" in samp_con:
                if "-" not in samp_con:
                    print("option [7] must be in form 7-Z where Z is an integer")
                    return False, "naming convention option [7] must be in form 7-Z where Z is an integer"
                else:
                    site_num=samp_con.split("-")[1]
                    samp_con="7"
        else: samp_con="1"
        if '-dc' in args:
            ind=args.index('-dc')
            DC_FIELD,DC_PHI,DC_THETA=list(map(float,args[ind+1].strip('( ) [ ]').split(',')))
            DC_FIELD *= 1e-6
            yn=''
            GET_DC_PARAMS=False
        else: DC_FIELD,DC_PHI,DC_THETA=0,0,-90
        if '-spc' in args:
            ind=args.index("-spc")
            specnum=-int(args[ind+1])
        else: specnum = 0
        if '-dmy' in args:
            ind=args.index("-dmy")
            dmy_flag=True
        else: dmy_flag=False


    if not command_line:
        dir_path = kwargs.get('dir_path', '.')
        input_dir_path = kwargs.get('input_dir_path', dir_path)
        output_dir_path = dir_path
        meas_file = kwargs.get('meas_file', 'magic_measurements.txt')
        mag_file = kwargs.get('mag_file')
        spec_file = kwargs.get('spec_file', 'er_specimens.txt') # specimen outfile
        samp_file = kwargs.get('samp_file', 'er_samples.txt')
        site_file = kwargs.get('site_file', 'er_sites.txt') # site outfile
        er_location_name = kwargs.get('location_name', '')
        site_lat = kwargs.get('site_lat', '')
        site_lon = kwargs.get('site_lon', '')
        #oave = kwargs.get('noave', 0) # default (0) means DO average
        meth_code = kwargs.get('meth_code', "LP-NO")
        specnum = -int(kwargs.get('specnum', 0))
        samp_con = kwargs.get('samp_con', '2')
        if "4" in samp_con:
            if "-" not in samp_con:
                print("option [4] must be in form 4-Z where Z is an integer")
                return False, "naming convention option [4] must be in form 4-Z where Z is an integer"
            else:
                site_num=samp_con.split("-")[1]
                samp_con="4"
        elif "7" in samp_con:
            if "-" not in samp_con:
                print("option [7] must be in form 7-Z where Z is an integer")
                return False, "naming convention option [7] must be in form 7-Z where Z is an integer"
            else:
                site_num=samp_con.split("-")[1]
                samp_con="7"
        DC_FIELD,DC_PHI,DC_THETA = list(map(float, kwargs.get('dc_params', (0,0,-90))))
        DC_FIELD *= 1e-6
        noave = kwargs.get('avg', True)
        dmy_flag = kwargs.get('dmy_flag', False)

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

    # parse data

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

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

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

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

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

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

# write out the data to MagIC data files
    pmag.magic_write(spec_file, SpecOuts, 'er_specimens')
    pmag.magic_write(samp_file, SampOuts, 'er_samples')
    pmag.magic_write(site_file, SiteOuts, 'er_sites')
#    MagOuts = pmag.measurements_methods(MagRecs, noave)
#    pmag.magic_write(meas_file, MagOuts, 'magic_measurements')
    pmag.magic_write(meas_file, MagRecs, 'magic_measurements')
    print("results put in ", meas_file)
    print("exit!")
    return True, meas_file
示例#7
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
示例#8
0
def convert(**kwargs):
    """

    """

    #get kwargs
    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', 'measurements.txt')
    mag_file = kwargs.get('mag_file')
    spec_file = kwargs.get('spec_file', 'specimens.txt')
    samp_file = kwargs.get('samp_file', 'samples.txt')
    site_file = kwargs.get('site_file', 'sites.txt')
    loc_file = kwargs.get('loc_file', 'locations.txt')
    lat = kwargs.get('lat', 0)
    lon = kwargs.get('lon', 0)
    specnum = int(kwargs.get('specnum', 0))
    samp_con = kwargs.get('samp_con', '1')
    location = kwargs.get('location', 'unknown')
    noave = kwargs.get('noave', 0)  # default (0) means DO average
    meth_code = kwargs.get('meth_code', "LP-NO")
    version_num = pmag.get_version()

    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"
    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"
    else:
        Z = 1

    # format variables
    mag_file = os.path.join(input_dir_path, mag_file)
    meas_file = os.path.join(output_dir_path, meas_file)
    spec_file = os.path.join(output_dir_path, spec_file)
    samp_file = os.path.join(output_dir_path, samp_file)
    site_file = os.path.join(output_dir_path, site_file)

    # parse data
    data = open(mag_file, 'r').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.
    specimen = rec[0]
    SpecRecs, SampRecs, SiteRecs, LocRecs, MeasRecs = [], [], [], [], []
    SpecRec, SampRec, SiteRec, LocRec = {}, {}, {}, {}  # make a  sample record
    if specnum != 0:
        sample = rec[0][:specnum]
    else:
        sample = rec[0]
    if int(samp_con) < 6:
        site = pmag.parse_site(sample, samp_con, Z)
    else:
        if 'site' in list(SampRec.keys()): site = ErSampREc['site']
        if 'location' in list(SampRec.keys()): location = ErSampREc['location']
    az_ind = rec.index('a=') + 1
    SampRec['sample'] = sample
    SampRec['description'] = comment
    SampRec['azimuth'] = rec[az_ind]
    dip_ind = rec.index('b=') + 1
    dip = -float(rec[dip_ind])
    SampRec['dip'] = '%7.1f' % (dip)
    strike_ind = rec.index('s=') + 1
    SampRec['bed_dip_direction'] = '%7.1f' % (float(rec[strike_ind]) + 90.)
    bd_ind = rec.index('d=') + 1
    SampRec['bed_dip'] = rec[bd_ind]
    v_ind = rec.index('v=') + 1
    vol = rec[v_ind][:-3]
    date = rec[-2]
    time = rec[-1]
    SampRec['method_codes'] = meth_code
    SampRec['site'] = site
    SampRec['citations'] = 'This study'
    SampRec['method_codes'] = 'SO-NO'

    SpecRec['specimen'] = specimen
    SpecRec['sample'] = sample
    SpecRec['citations'] = 'This study'

    SiteRec['site'] = site
    SiteRec['location'] = location
    SiteRec['citations'] = 'This study'
    SiteRec['lat'] = lat
    SiteRec['lon'] = lon

    LocRec['location'] = location
    LocRec['citations'] = 'This study'
    LocRec['lat_n'] = lat
    LocRec['lat_s'] = lat
    LocRec['lon_e'] = lon
    LocRec['lon_w'] = lon

    SpecRecs.append(SpecRec)
    SampRecs.append(SampRec)
    SiteRecs.append(SiteRec)
    LocRecs.append(LocRec)
    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
            MeasRec = {}
            MeasRec['description'] = 'Date: ' + date + ' ' + time
            MeasRec["citations"] = "This study"
            MeasRec['software_packages'] = version_num
            MeasRec["treat_temp"] = '%8.3e' % (273)  # room temp in kelvin
            MeasRec["meas_temp"] = '%8.3e' % (273)  # room temp in kelvin
            MeasRec["quality"] = 'g'
            MeasRec["standard"] = 'u'
            MeasRec["treat_step_num"] = '1'
            MeasRec["specimen"] = specimen
            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)
            MeasRec["magn_moment"] = '%10.3e' % (Vec[2])  # Am^2
            MeasRec["magn_volume"] = rec[4]  # A/m
            MeasRec["dir_dec"] = '%7.1f' % (Vec[0])
            MeasRec["dir_inc"] = '%7.1f' % (Vec[1])
            MeasRec["treat_ac_field"] = '0'
            if meas_type != 'LT-NO':
                treat = float(rec[0][1:])
            else:
                treat = 0
            if meas_type == "LT-AF-Z":
                MeasRec["treat_ac_field"] = '%8.3e' % (
                    treat * 1e-3)  # convert from mT to tesla
            elif meas_type == "LT-T-Z":
                MeasRec["treat_temp"] = '%8.3e' % (treat + 273.
                                                   )  # temp in kelvin
            MeasRec['method_codes'] = meas_type
            MeasRecs.append(MeasRec)

    con = nb.Contribution(output_dir_path, read_tables=[])

    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts = pmag.measurements_methods3(MeasRecs, noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    con.tables['specimens'].write_magic_file(custom_name=spec_file)
    con.tables['samples'].write_magic_file(custom_name=samp_file)
    con.tables['sites'].write_magic_file(custom_name=site_file)
    con.tables['locations'].write_magic_file(custom_name=loc_file)
    con.tables['measurements'].write_magic_file(custom_name=meas_file)

    return True, meas_file
示例#9
0
def main():
    """
    NAME
        umich_magic.py
 
    DESCRIPTION
        converts UMICH .mag format files to magic_measurements format files

    SYNTAX
        umich_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify .mag format input file, required
        -fsa SAMPFILE : specify er_samples.txt file relating samples, site and locations names,default is none
        -F FILE: specify output file, default is magic_measurements.txt
        -spc NUM : specify number of characters to designate a  specimen, default = 0
        -loc LOCNAME : specify location/study name, must have either LOCNAME or SAMPFILE or be a synthetic
        -ncn NCON:  specify naming convention: default is #1 below
        -A: don't average replicate measurements
       Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name same as sample
            [6] site is entered under a separate column -- NOT CURRENTLY SUPPORTED
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.
 
        Format of UMICH .mag files:   
        Spec Treat CSD Intensity Declination Inclination metadata string
        Spec: specimen name
        Treat:  treatment step
            XXX T in Centigrade
            XXX AF in mT
         Intensity assumed to be total moment in 10^3 Am^2 (emu)
         Declination:  Declination in specimen coordinate system
         Inclination:  Declination in specimen coordinate system

         metatdata string:  mm/dd/yy;hh:mm;[dC,mT];xx.xx;UNITS;USER;INST;NMEAS
             hh in 24 hours.  
             dC or mT units of treatment XXX (see Treat above) for thermal or AF respectively
             xx.xxx   DC field
             UNITS of DC field (microT, mT)
             INST:  instrument code, number of axes, number of positions (e.g., G34 is 2G, three axes, 
                    measured in four positions)
             NMEAS: number of measurements in a single position (1,3,200...)
    """
    # initialize some stuff
    dir_path = '.'
    infile_type = "mag"
    noave = 0
    methcode, inst = "", ""
    phi, theta, peakfield, labfield = 0, 0, 0, 0
    pTRM, MD, samp_con, Z = 0, 0, '1', 1
    missing = 1
    demag = "N"
    er_location_name = ""
    citation = 'This study'
    args = sys.argv
    methcode = "LP-NO"
    samp_file, ErSamps = '', []
    specnum = 0
    #
    # get command line arguments
    #
    meas_file = "magic_measurements.txt"
    user = ""
    if '-WD' in args:
        ind = args.index("-WD")
        dir_path = args[ind + 1]
    if "-h" in args:
        print(main.__doc__)
        sys.exit()
    if "-usr" in args:
        ind = args.index("-usr")
        user = args[ind + 1]
    if '-F' in args:
        ind = args.index("-F")
        meas_file = dir_path + '/' + args[ind + 1]
    if '-f' in args:
        ind = args.index("-f")
        magfile = dir_path + '/' + args[ind + 1]
        try:
            input = open(magfile, 'r')
        except:
            print("bad mag file name")
            sys.exit()
    else:
        print("mag_file field is required option")
        print(main.__doc__)
        sys.exit()
    if "-spc" in args:
        ind = args.index("-spc")
        specnum = int(args[ind + 1])
        if specnum != 0: specnum = -specnum
    if "-loc" in args:
        ind = args.index("-loc")
        er_location_name = args[ind + 1]
    if "-fsa" in args:
        ind = args.index("-fsa")
        samp_file = dir_path + '/' + args[ind + 1]
        Samps, file_type = pmag.magic_read(samp_file)
    if "-A" in args: noave = 1
    if "-ncn" in args:
        ind = args.index("-ncn")
        samp_con = sys.argv[ind + 1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print("option [4] must be in form 4-Z where Z is an integer")
                sys.exit()
            else:
                Z = samp_con.split("-")[1]
                samp_con = "4"
            samp_con = sys.argv[ind + 1]
        if "7" in samp_con:
            if "-" not in samp_con:
                print("option [7] must be in form 7-Z where Z is an integer")
                sys.exit()
            else:
                Z = samp_con.split("-")[1]
                samp_con = "7"
    MagRecs, specs = [], []
    version_num = pmag.get_version()
    if infile_type == "mag":
        for line in input.readlines():
            instcode = ""
            if len(line) > 2:
                MagRec = {}
                MagRec['er_location_name'] = er_location_name
                MagRec['magic_software_packages'] = version_num
                MagRec["treatment_temp"] = '%8.3e' % (273
                                                      )  # room temp in kelvin
                MagRec["measurement_temp"] = '%8.3e' % (
                    273)  # room temp in kelvin
                MagRec["treatment_ac_field"] = '0'
                MagRec["treatment_dc_field"] = '0'
                MagRec["treatment_dc_field_phi"] = '0'
                MagRec["treatment_dc_field_theta"] = '0'
                meas_type = "LT-NO"
                rec = line.split()
                labfield = 0
                code1 = rec[6].split(';')
                date = code1[0].split('/')  # break date into mon/day/year
                yy = int(date[2])
                if yy < 90:
                    yyyy = str(2000 + yy)
                else:
                    yyyy = str(1900 + yy)
                mm = int(date[0])
                if mm < 10:
                    mm = "0" + str(mm)
                else:
                    mm = str(mm)
                dd = int(date[1])
                if dd < 10:
                    dd = "0" + str(dd)
                else:
                    dd = str(dd)
                time = code1[1].split(':')
                hh = int(time[0])
                if hh < 10:
                    hh = "0" + str(hh)
                else:
                    hh = str(hh)
                min = int(time[1])
                if min < 10:
                    min = "0" + str(min)
                else:
                    min = str(min)
                MagRec[
                    "measurement_date"] = yyyy + ":" + mm + ":" + dd + ":" + hh + ":" + min + ":00.00"
                MagRec["measurement_time_zone"] = ''
                instcode = ''
                if len(code1) > 1:
                    MagRec["measurement_positions"] = code1[6][2]
                else:
                    MagRec["measurement_positions"] = code1[
                        7]  # takes care of awkward format with bubba and flo being different
                if user == "": user = code1[5]
                if code1[2][-1] == 'C': demag = "T"
                if code1[2] == 'mT': demag = "AF"
                treat = rec[1].split('.')
                if len(treat) == 1: treat.append('0')
                if demag == 'T' and treat != 0:
                    meas_type = "LT-T-Z"
                    MagRec["treatment_temp"] = '%8.3e' % (
                        float(treat[0]) + 273.)  # temp in kelvin
                if demag == "AF":
                    meas_type = "LT-AF-Z"
                    MagRec["treatment_ac_field"] = '%8.3e' % (
                        float(treat[0]) * 1e-3)  # Af field in T
                MagRec["treatment_dc_field"] = '0'
                MagRec["er_specimen_name"] = rec[0]
                if rec[0] not in specs:
                    specs.append(rec[0])  # get a list of specimen names
                experiment = rec[0] + ":"
                MagRec["er_site_name"] = ""
                if specnum != 0:
                    MagRec["er_sample_name"] = rec[0][:specnum]
                else:
                    MagRec["er_sample_name"] = rec[0]
                if "-fsa" in args:
                    for samp in Samps:
                        if samp["er_sample_name"] == MagRec["er_sample_name"]:
                            MagRec["er_location_name"] = samp[
                                "er_location_name"]
                            MagRec["er_site_name"] = samp["er_site_name"]
                            break
                elif int(samp_con) != 6:
                    site = pmag.parse_site(MagRec['er_sample_name'], samp_con,
                                           Z)
                    MagRec["er_site_name"] = site
                if MagRec['er_site_name'] == "":
                    print('No site name found for: ',
                          MagRec['er_specimen_name'], MagRec['er_sample_name'])
                if MagRec["er_location_name"] == "":
                    print('no location name for: ', MagRec["er_specimen_name"])
                if rec[1] == ".00": rec[1] = "0.00"
                MagRec["measurement_csd"] = rec[2]
                MagRec["measurement_magn_moment"] = '%10.3e' % (
                    float(rec[3]) * 1e-3)  # moment in Am^2 (from emu)
                MagRec["measurement_dec"] = rec[4]
                MagRec["measurement_inc"] = rec[5]
                MagRec["magic_instrument_codes"] = instcode
                MagRec["er_analyst_mail_names"] = user
                MagRec["er_citation_names"] = citation
                MagRec["magic_method_codes"] = meas_type
                MagRec["measurement_flag"] = 'g'
                MagRec["er_specimen_name"] = rec[0]
                MagRec["measurement_number"] = '1'
                MagRecs.append(MagRec)
    MagOuts = []
    for spec in specs:  # gather all demag types for this specimen
        SpecRecs, meths, measnum = [], [], 1
        for rec in MagRecs:
            if rec['er_specimen_name'] == spec:
                rec['measurement_number'] = str(measnum)
                measnum += 1
                if rec['magic_method_codes'] not in meths:
                    meths.append(rec['magic_method_codes'])
                SpecRecs.append(rec)
        expname = spec
        if "LT-AF-Z" in meths: expname = expname + ':LP-DIR-AF'
        if "LT-T-Z" in meths: expname = expname + ':LP-DIR-T'
        for rec in SpecRecs:
            rec['magic_experiment_name'] = expname
            MagOuts.append(rec)
    pmag.magic_write(meas_file, MagOuts, 'magic_measurements')
    print("results put in ", meas_file)
示例#10
0
def convert(**kwargs):
    version_num = pmag.get_version()

    user = kwargs.get('user', '')
    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', 'measurements.txt')
    spec_file = kwargs.get('spec_file', 'specimens.txt')  # specimen outfile
    samp_file = kwargs.get('samp_file', 'samples.txt')
    site_file = kwargs.get('site_file', 'sites.txt')  # site outfile
    loc_file = kwargs.get('loc_file', 'locations.txt')  # Loc outfile
    mag_file = kwargs.get('mag_file')  #required
    location = kwargs.get('location', 'unknown')
    site = kwargs.get('site', '')
    samp_con = kwargs.get('samp_con', '1')
    specnum = int(kwargs.get('specnum', 0))
    timezone = kwargs.get('timestamp', 'US/Pacific')
    noave = kwargs.get('noave', False)  # default False 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:
        volume *= 1e-6  #convert cm^3 to m^3

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

    # format variables
    if not os.path.isfile(mag_file):
        print("%s is not a BGC file" % mag_file)
        return False, 'You must provide a BCG format file'
    mag_file = os.path.join(input_dir_path, mag_file)

    # 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(' ')
    specimen = line_items[2]
    specimen = specimen.replace('\n', '')
    line = pre_data.readline()
    line = pre_data.readline()
    line_items = line.split('\t')
    azimuth = float(line_items[1])
    dip = float(line_items[2])
    bed_dip = line_items[3]
    sample_bed_azimuth = line_items[4]
    lon = line_items[5]
    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['dir_dec'] = direction[0]
    data['dir_inc'] = direction[1]
    data['magn_moment'] = old_div(
        direction[2], 1000)  # the data are in EMU - this converts to Am^2
    data['magn_volume'] = old_div((old_div(direction[2], 1000)),
                                  volume)  # EMU  - data converted to A/m

    # Configure the magic_measurements table
    MeasRecs, SpecRecs, SampRecs, SiteRecs, LocRecs = [], [], [], [], []
    for rowNum, row in data.iterrows():
        MeasRec, SpecRec, SampRec, SiteRec, LocRec = {}, {}, {}, {}, {}

        if specnum != 0:
            sample = specimen[:specnum]
        else:
            sample = specimen
        if site == '':
            site = pmag.parse_site(sample, samp_con, Z)

        if specimen != "" and specimen not in [
                x['specimen'] if 'specimen' in list(x.keys()) else ""
                for x in SpecRecs
        ]:
            SpecRec['specimen'] = specimen
            SpecRec['sample'] = sample
            SpecRec['volume'] = volume
            SpecRec['analysts'] = user
            SpecRec['citations'] = 'This study'
            SpecRecs.append(SpecRec)
        if sample != "" and sample not in [
                x['sample'] if 'sample' in list(x.keys()) else ""
                for x in SampRecs
        ]:
            SampRec['sample'] = sample
            SampRec['site'] = site
            SampRec['azimuth'] = azimuth
            SampRec['dip'] = dip
            SampRec['bed_dip_direction'] = sample_bed_azimuth
            SampRec['bed_dip'] = bed_dip
            SampRec['method_codes'] = meth_code
            SampRec['analysts'] = user
            SampRec['citations'] = 'This study'
            SampRecs.append(SampRec)
        if site != "" and site not in [
                x['site'] if 'site' in list(x.keys()) else "" for x in SiteRecs
        ]:
            SiteRec['site'] = site
            SiteRec['location'] = location
            SiteRec['lat'] = lat
            SiteRec['lon'] = lon
            SiteRec['analysts'] = user
            SiteRec['citations'] = 'This study'
            SiteRecs.append(SiteRec)
        if location != "" and location not in [
                x['location'] if 'location' in list(x.keys()) else ""
                for x in LocRecs
        ]:
            LocRec['location'] = location
            LocRec['analysts'] = user
            LocRec['citations'] = 'This study'
            LocRec['lat_n'] = lat
            LocRec['lon_e'] = lon
            LocRec['lat_s'] = lat
            LocRec['lon_w'] = lon
            LocRecs.append(LocRec)

        MeasRec['description'] = 'Date: ' + str(row['Date']) + ' Time: ' + str(
            row['Time'])
        if '.' in row['Date']: datelist = row['Date'].split('.')
        elif '/' in row['Date']: datelist = row['Date'].split('/')
        elif '-' in row['Date']: datelist = row['Date'].split('-')
        else:
            print(
                "unrecogized date formating on one of the measurement entries for specimen %s"
                % specimen)
            datelist = ['', '', '']
        if ':' in row['Time']: timelist = row['Time'].split(':')
        else:
            print(
                "unrecogized time formating on one of the measurement entries for specimen %s"
                % specimen)
            timelist = ['', '', '']
        datelist[2] = '19' + datelist[2] if len(
            datelist[2]) <= 2 else datelist[2]
        dt = ":".join([
            datelist[1], datelist[0], datelist[2], timelist[0], timelist[1],
            timelist[2]
        ])
        local = pytz.timezone(timezone)
        naive = datetime.datetime.strptime(dt, "%m:%d:%Y:%H:%M:%S")
        local_dt = local.localize(naive, is_dst=None)
        utc_dt = local_dt.astimezone(pytz.utc)
        timestamp = utc_dt.strftime("%Y-%m-%dT%H:%M:%S") + "Z"
        MeasRec["timestamp"] = timestamp
        MeasRec["citations"] = "This study"
        MeasRec['software_packages'] = version_num
        MeasRec["treat_temp"] = '%8.3e' % (273)  # room temp in kelvin
        MeasRec["meas_temp"] = '%8.3e' % (273)  # room temp in kelvin
        MeasRec["quality"] = 'g'
        MeasRec["standard"] = 'u'
        MeasRec["treat_step_num"] = rowNum
        MeasRec["specimen"] = specimen
        MeasRec["treat_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'])
            MeasRec["treat_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'])
            MeasRec["treat_temp"] = '%8.3e' % (treat + 273.)  # temp in kelvin
        else:
            print("measurement type unknown:", row['DM Type'], " in row ",
                  rowNum)
        MeasRec["magn_moment"] = str(row['magn_moment'])
        MeasRec["magn_volume"] = str(row['magn_volume'])
        MeasRec["dir_dec"] = str(row['dir_dec'])
        MeasRec["dir_inc"] = str(row['dir_inc'])
        MeasRec['method_codes'] = meas_type
        MeasRec['dir_csd'] = '0.0'  # added due to magic.write error
        MeasRec['meas_n_orient'] = '1'  # added due to magic.write error
        MeasRecs.append(MeasRec.copy())

    con = nb.Contribution(output_dir_path, read_tables=[])

    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts = pmag.measurements_methods3(MeasRecs, noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    con.tables['specimens'].write_magic_file(custom_name=spec_file)
    con.tables['samples'].write_magic_file(custom_name=samp_file)
    con.tables['sites'].write_magic_file(custom_name=site_file)
    con.tables['locations'].write_magic_file(custom_name=loc_file)
    con.tables['measurements'].write_magic_file(custom_name=meas_file)

    return True, meas_file
示例#11
0
def convert(**kwargs):

    user = kwargs.get('user', '')
    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', 'measurements.txt')
    spec_file = kwargs.get('spec_file', 'specimens.txt') # specimen outfile
    samp_file = kwargs.get('samp_file', 'samples.txt')
    site_file = kwargs.get('site_file', 'sites.txt') # site outfile
    loc_file = kwargs.get('loc_file', 'locations.txt') # Loc outfile
    magfile = kwargs.get('magfile', '')
    datafile = kwargs.get('datafile', '')
    specnum = int(kwargs.get('specnum', 0))
    labfield = int(kwargs.get('labfield', 0)) *1e-6
    phi = int(kwargs.get('phi', 0))
    theta = int(kwargs.get('theta', 0))
    peakfield = kwargs.get('peakfield', 0)
    if peakfield:
        peakfield = float(peakfield)*1e-3
    location = kwargs.get('location', '')
    samp_con = kwargs.get('samp_con', '1')
    codelist = kwargs.get('codelist', '')
    CR_cooling_times = kwargs.get('CR_cooling_times', None)
    noave = kwargs.get('noave', False)

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

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

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

    if "TRM" in codes:
        demag="T"
        LPcode="LP-TRM"
        #trm=1

    if "CR" in codes:
        demag="T"
        # dc should be in the code
        if not labfield:
            print("missing lab field option")
            return False, "missing lab field option"

        LPcode="LP-CR-TRM" # TRM in different cooling rates
        if command_line:
            ind=sys.argv.index("-LP")
            CR_cooling_times=sys.argv[ind+2].split(",")

    version_num=pmag.get_version()

    #--------------------------------------
    # Read the file
    # Assumption:
    # 1. different lab protocolsa are in different files
    # 2. measurements are in the correct order
    #--------------------------------------

    Data={}
    line_no=0
    for line in infile.readlines():
        line_no+=1
        this_line_data={}
        line_no+=1
        instcode=""
        if len(line)<2:
            continue
        if line[0]=="#": #HUJI way of marking bad data points
            continue

        rec=line.strip('\n').split()
        specimen=rec[0]
        date=rec[2].split("/")
        hour=rec[3].split(":")
        treatment_type=rec[4]
        treatment=rec[5].split(".")
        dec_core=rec[6]
        inc_core=rec[7]
        moment_emu=float(rec[-1])

        if specimen not in list(Data.keys()):
            Data[specimen]=[]

        # check duplicate treatments:
        # if yes, delete the first and use the second

        if len(Data[specimen])>0:
            if treatment==Data[specimen][-1]['treatment']:
                del(Data[specimen][-1])
                print("-W- Identical treatments in file %s magfile line %i: specimen %s, treatment %s ignoring the first. " %(magfile, line_no, specimen,".".join(treatment)))

        this_line_data={}
        this_line_data['specimen']=specimen
        this_line_data['date']=date
        this_line_data['hour']=hour
        this_line_data['treatment_type']=treatment_type
        this_line_data['treatment']=treatment
        this_line_data['dec_core']=dec_core
        this_line_data['inc_core']=inc_core
        this_line_data['moment_emu']=moment_emu
        this_line_data['azimuth']=''
        this_line_data['dip']=''
        this_line_data['bed_dip_direction']=''
        this_line_data['bed_dip']=''
        this_line_data['lat']=''
        this_line_data['lon']=''
        this_line_data['volume']=''
        Data[specimen].append(this_line_data)
    infile.close()
    print("-I- done reading file %s"%magfile)

    if datafile:
        dinfile = open(datafile)
        for line in dinfile.readlines():
            data = line.split()
            if len(data)<8 or data[0]=='': continue
            elif data[0] in list(Data.keys()):
                for i in range(len(Data[data[0]])):
                    Data[data[0]][i]['azimuth'] = data[1]
                    Data[data[0]][i]['dip'] = data[2]
                    try: Data[data[0]][i]['bed_dip_direction'] = float(data[3])+90
                    except ValueError: Data[data[0]][i]['bed_dip_direction'] = ''
                    Data[data[0]][i]['bed_dip'] = data[4]
                    Data[data[0]][i]['lat'] = data[5]
                    Data[data[0]][i]['lon'] = data[6]
                    Data[data[0]][i]['volume'] = data[7]
            else:
                print("no specimen %s found in magnetometer data file when reading specimen orientation data file, or data file record for specimen too short"%data[0])
        dinfile.close()

    #--------------------------------------
    # Convert to MagIC
    #--------------------------------------

    specimens_list=list(Data.keys())
    specimens_list.sort()

    MeasRecs,SpecRecs,SampRecs,SiteRecs,LocRecs=[],[],[],[],[]
    for specimen in specimens_list:
        for i in range(len(Data[specimen])):
            this_line_data=Data[specimen][i]
            methcode=""
            MeasRec,SpecRec,SampRec,SiteRec,LocRec={},{},{},{},{}
            specimen=this_line_data['specimen']
            if specnum!=0:
                sample=this_line_data['specimen'][:specnum]
            else:
                sample=this_line_data['specimen']
            site=pmag.parse_site(sample,samp_con,Z)
            if not location:
                location=site
            if specimen!="" and specimen not in [x['specimen'] if 'specimen' in list(x.keys()) else "" for x in SpecRecs]:
                SpecRec['specimen'] = specimen
                SpecRec['sample'] = sample
                SpecRecs.append(SpecRec)
            if sample!="" and sample not in [x['sample'] if 'sample' in list(x.keys()) else "" for x in SampRecs]:
                SampRec['sample'] = sample
                SampRec['site'] = site
                SampRec['azimuth'] = this_line_data['azimuth']
                SampRec['dip'] = this_line_data['dip']
                SampRec['bed_dip_direction'] = this_line_data['bed_dip_direction']
                SampRec['bed_dip'] = this_line_data['bed_dip']
                SampRecs.append(SampRec)
            if site!="" and site not in [x['site'] if 'site' in list(x.keys()) else "" for x in SiteRecs]:
                SiteRec['site'] = site
                SiteRec['location'] = location
                SiteRec['lat'] = this_line_data['lat']
                SiteRec['lon'] = this_line_data['lon']
                SiteRecs.append(SiteRec)
            if location!="" and location not in [x['location'] if 'location' in list(x.keys()) else "" for x in LocRecs]:
                LocRec['location']=location
                LocRec['lat_n'] = this_line_data['lat']
                LocRec['lon_e'] = this_line_data['lon']
                LocRec['lat_s'] = this_line_data['lat']
                LocRec['lon_w'] = this_line_data['lon']
                LocRecs.append(LocRec)

            MeasRec['specimen'] = specimen
            MeasRec["meas_temp"]='%8.3e' % (273) # room temp in kelvin
            MeasRec["magn_moment"]='%10.3e'% (float(this_line_data['moment_emu'])*1e-3) # moment in Am^2 (from emu)
            MeasRec["dir_dec"]=this_line_data['dec_core']
            MeasRec["dir_inc"]=this_line_data['inc_core']

            date=this_line_data['date']
            hour=this_line_data['hour']
            if len(date[2])<4 and float(date[2])>=70:
                yyyy="19"+date[2]
            elif len(date[2])<4 and float(date[2])<70:
                yyyy="20"+date[2]
            else: yyyy=date[2]
            if len (date[0])==1:
                date[0]="0"+date[0]
            if len (date[1])==1:
                date[1]="0"+date[1]
            dt=":".join([date[0],date[1],yyyy,hour[0],hour[1],"0"])
            local = pytz.timezone("America/New_York")
            naive = datetime.datetime.strptime(dt, "%m:%d:%Y:%H:%M:%S")
            local_dt = local.localize(naive, is_dst=None)
            utc_dt = local_dt.astimezone(pytz.utc)
            timestamp=utc_dt.strftime("%Y-%m-%dT%H:%M:%S")+"Z"

            MeasRec['analysts']=user
            MeasRec["citations"]="This study"
            MeasRec["instrument_codes"]="HUJI-2G"
            MeasRec["quality"]="g"
            MeasRec["meas_n_orient"]="1"
            MeasRec["standard"]="u"
            MeasRec["description"]=""

            #----------------------------------------
            # AF demag
            # do not support AARM yet
            #----------------------------------------

            if demag=="AF":
                treatment_type=this_line_data['treatment_type']
                # demag in zero field
                if LPcode != "LP-AN-ARM":
                    MeasRec["treat_ac_field"]='%8.3e' %(float(this_line_data['treatment'][0])*1e-3) # peak field in tesla
                    MeasRec["treat_dc_field"]='0'
                    MeasRec["treat_dc_field_phi"]='0'
                    MeasRec["treat_dc_field_theta"]='0'
                    if treatment_type=="N":
                        methcode="LP-DIR-AF:LT-NO"
                    elif treatment_type=="A":
                        methcode="LP-DIR-AF:LT-AF-Z"
                    else:
                        print("ERROR in treatment field line %i... exiting until you fix the problem" %line_no)
                        print(this_line_data)
                        return False, "ERROR in treatment field line %i... exiting until you fix the problem" %line_no
                # AARM experiment
                else:
                    print("Dont supprot AARM in HUJI format yet. sorry... do be DONE")
                MeasRec["method_codes"]=methcode
                MeasRec["experiments"]=specimen+ ":" + LPcode
                MeasRec["treat_step_num"]="%i"%i
                MeasRec["description"]=""

                MeasRecs.append(MeasRec)

            #----------------------------------------
            # Thermal:
            # Thellier experiment: "IZ", "ZI", "IZZI", pTRM checks
            # Thermal demag
            # Thermal cooling rate experiment
            # Thermal NLT
            #----------------------------------------


            if demag=="T":

                treatment=this_line_data['treatment']
                treatment_type=this_line_data['treatment_type']

                #----------------------------------------
                # Thellier experimet
                #----------------------------------------

                if LPcode == "LP-PI-TRM"  : # Thelllier experiment
                    MeasRec["experiments"]=specimen+ ":" + LPcode
                    methcode=LPcode
                    if treatment_type=="N" or ( (treatment[1]=='0' or  treatment[1]=='00') and float(treatment[0])==0):
                            LT_code="LT-NO"
                            MeasRec["treat_dc_field_phi"]='0'
                            MeasRec["treat_dc_field_theta"]='0'
                            MeasRec["treat_dc_field"]='0'
                            MeasRec["treat_temp"]='273.'

                    elif treatment[1]=='0' or  treatment[1]=='00':
                            LT_code="LT-T-Z"
                            MeasRec["treat_dc_field_phi"]='0'
                            MeasRec["treat_dc_field_theta"]='0'
                            MeasRec["treat_dc_field"]='%8.3e'%(0)
                            MeasRec["treat_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin

                            # check if this is ZI or IZ:
                            #  check if the same temperature already measured:
                            methcode="LP-PI-TRM:LP-PI-TRM-ZI"
                            for j in range (0,i):
                                if Data[specimen][j]['treatment'][0] == treatment[0]:
                                    if Data[specimen][j]['treatment'][1] == '1' or Data[specimen][j]['treatment'][1] == '10':
                                        methcode="LP-PI-TRM:LP-PI-TRM-IZ"
                                    else:
                                        methcode="LP-PI-TRM:LP-PI-TRM-ZI"

                    elif treatment[1]=='1' or  treatment[1]=='10':
                            LT_code="LT-T-I"
                            MeasRec["treat_dc_field"]='%8.3e' % (labfield) # labfield in tesla (convert from microT)
                            MeasRec["treat_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                            MeasRec["treat_dc_field_theta"]='%7.1f' % (theta) # labfield theta
                            MeasRec["treat_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin

                            # check if this is ZI or IZ:
                            #  check if the same temperature already measured:
                            methcode="LP-PI-TRM:LP-PI-TRM-IZ"
                            for j in range (0,i):
                                if Data[specimen][j]['treatment'][0] == treatment[0]:
                                    if Data[specimen][j]['treatment'][1] == '0' or Data[specimen][j]['treatment'][1] == '00':
                                        methcode="LP-PI-TRM:LP-PI-TRM-ZI"
                                    else:
                                        methcode="LP-PI-TRM:LP-PI-TRM-IZ"
                    elif treatment[1]=='2' or  treatment[1]=='20':
                            LT_code="LT-PTRM-I"
                            MeasRec["treat_dc_field"]='%8.3e' % (labfield) # labfield in tesla (convert from microT)
                            MeasRec["treat_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                            MeasRec["treat_dc_field_theta"]='%7.1f' % (theta) # labfield theta
                            MeasRec["treat_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin
                            methcode="LP-PI-TRM:LP-PI-TRM-IZ"

                    else:
                            print("ERROR in treatment field line %i... exiting until you fix the problem" %line_no)
                            return False, "ERROR in treatment field line %i... exiting until you fix the problem" %line_no
                    MeasRec["method_codes"]=LT_code+":"+methcode
                    MeasRec["treat_step_num"]="%i"%i
                    MeasRec["description"]=""
                    MeasRecs.append(MeasRec)

                #----------------------------------------
                # demag experimet
                #----------------------------------------

                if LPcode == "LP-DIR-T"  :
                    MeasRec["experiments"]=specimen+ ":" + LPcode
                    methcode=LPcode
                    if treatment_type=="N":
                        LT_code="LT-NO"
                    else:
                        LT_code="LT-T-Z"
                        methcode=LPcode+":"+"LT-T-Z"
                    MeasRec["treat_dc_field_phi"]='0'
                    MeasRec["treat_dc_field_theta"]='0'
                    MeasRec["treat_dc_field"]='%8.3e'%(0)
                    MeasRec["treat_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin
                    MeasRec["method_codes"]=LT_code+":"+methcode
                    MeasRec["treat_step_num"]="%i"%i
                    MeasRec["description"]=""
                    MeasRecs.append(MeasRec)
                    #continue

                #----------------------------------------
                # ATRM measurements
                # The direction of the magnetization is used to determine the
                # direction of the lab field.
                #----------------------------------------

                if LPcode =="LP-AN-TRM":
                    MeasRec["experiments"]=specimen+ ":" + LPcode
                    methcode=LPcode

                    if float(treatment[1])==0:
                        MeasRec["method_codes"]="LP-AN-TRM:LT-T-Z"
                        MeasRec["treat_dc_field_phi"]='0'
                        MeasRec["treat_dc_field_theta"]='0'
                        MeasRec["treat_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin
                        MeasRec["treat_dc_field"]='0'
                    else:
                        if float(treatment[1])==7:
                            # alteration check
                            methcode="LP-AN-TRM:LT-PTRM-I"
                            MeasRec["treat_step_num"]='7'# -z
                        else:
                            MeasRec["method_codes"]="LP-AN-TRM:LT-T-I"
                            inc=float(MeasRec["dir_inc"]);dec=float(MeasRec["dir_dec"])
                            if abs(inc)<45 and (dec<45 or dec>315): # +x
                                tdec,tinc=0,0
                                MeasRec["treat_step_num"]='1'
                            if abs(inc)<45 and (dec<135 and dec>45):
                                tdec,tinc=90,0
                                MeasRec["treat_step_num"]='2' # +y
                            if inc>45 :
                                tdec,tinc=0,90
                                MeasRec["treat_step_num"]='3' # +z
                            if abs(inc)<45 and (dec<225 and dec>135):
                                tdec,tinc=180,0
                                MeasRec["treat_step_num"]='4' # -x
                            if abs(inc)<45 and (dec<315 and dec>225):
                                tdec,tinc=270,0
                                MeasRec["treat_step_num"]='5'# -y
                            if inc<-45 :
                                tdec,tinc=0,-90
                                MeasRec["treat_step_num"]='6'# -z

                        MeasRec["treat_dc_field_phi"]='%7.1f' %(tdec)
                        MeasRec["treat_dc_field_theta"]='%7.1f'% (tinc)
                        MeasRec["treat_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin
                        MeasRec["treat_dc_field"]='%8.3e'%(labfield)
                    MeasRec["description"]=""
                    MeasRecs.append(MeasRec)
                    #continue

                #----------------------------------------
                # NLT measurements
                # or TRM acquisistion experiment
                #----------------------------------------

                if LPcode == "LP-TRM"  :
                    MeasRec["experiments"]=specimen+ ":" + LPcode
                    MeasRec["method_codes"]="LP-TRM:LT-T-I"
                    if float(treatment[1])==0:
                        labfield=0
                    else:
                        labfield=float(float(treatment[1]))*1e-6
                    MeasRec["treat_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin
                    MeasRec["treat_dc_field"]='%8.3e' % (labfield) # labfield in tesla (convert from microT)
                    MeasRec["treat_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                    MeasRec["treat_dc_field_theta"]='%7.1f' % (theta) # labfield theta
                    MeasRec["treat_step_num"]="%i"%i
                    MeasRec["description"]=""
                    MeasRecs.append(MeasRec)
                    #continue

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

                if  LPcode =="LP-CR-TRM":
                    index=int(treatment[1][0])
                    #print index,"index"
                    #print CR_cooling_times,"CR_cooling_times"
                    #print CR_cooling_times[index-1]
                    #print CR_cooling_times[0:index-1]
                    if index==7 or index==70: # alteration check as final measurement
                            meas_type="LT-PTRM-I:LP-CR-TRM"
                            CR_cooling_time=CR_cooling_times[-1]
                    else:
                            meas_type="LT-T-I:LP-CR-TRM"
                            CR_cooling_time=CR_cooling_times[index-1]
                    MeasRec["method_codes"]=meas_type
                    MeasRec["experiments"]=specimen+ ":" + LPcode
                    MeasRec["treat_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin
                    MeasRec["treat_dc_field"]='%8.3e' % (labfield) # labfield in tesla (convert from microT)
                    MeasRec["treat_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                    MeasRec["treat_dc_field_theta"]='%7.1f' % (theta) # labfield theta
                    MeasRec["treat_step_num"]="%i"%index
                    MeasRec["description"]="cooling_rate"+":"+CR_cooling_time+":"+"K/min"
                    #MeasRec["description"]="%.1f minutes per cooling time"%int(CR_cooling_time)
                    MeasRecs.append(MeasRec)
                    #continue

    con = nb.Contribution(output_dir_path,read_tables=[])

    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts=pmag.measurements_methods3(MeasRecs,noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    con.tables['specimens'].write_magic_file(custom_name=spec_file)
    con.tables['samples'].write_magic_file(custom_name=samp_file)
    con.tables['sites'].write_magic_file(custom_name=site_file)
    con.tables['locations'].write_magic_file(custom_name=loc_file)
    con.tables['measurements'].write_magic_file(custom_name=meas_file)

    return True, meas_file
示例#12
0
def convert(**kwargs):
    # initialize some stuff
    citations = 'This study'
    units = 'cgs'
    meth = "LP-HYS"
    version_num = pmag.get_version()
    args = sys.argv
    Samps, Sites = [], []
    noave = 1

    # get args
    user = kwargs.get('user', '')
    dir_path = kwargs.get('dir_path', '.')
    output_dir_path = dir_path
    meas_file = kwargs.get('meas_file', 0)  # measurements outfile
    spec_file = kwargs.get('spec_file', 0)  # specimen outfile
    samp_file = kwargs.get('samp_file', 0)  # sample outfile
    site_file = kwargs.get('site_file', 0)  # site outfile
    loc_file = kwargs.get('loc_file', 0)  # location outfile
    agm_file = kwargs.get('agm_file', '')
    specnum = kwargs.get('specnum', 0)
    specimen = kwargs.get('specimen', 0)
    if not specimen:
        # grab the specimen name from the input file name
        specimen = agm_file.split('.')[0]
    if not meas_file:
        meas_file = specimen + '.magic'
    if not spec_file:
        spec_file = specimen + '_specimens.txt'
    if not samp_file:
        samp_file = specimen + '_samples.txt'
    if not site_file:
        site_file = specimen + '_sites.txt'
    if not loc_file:
        loc_file = specimen + '_locations.txt'
    samp_con = kwargs.get('samp_con', '1')
    location = kwargs.get('location', 'unknown')
    site_infile = kwargs.get('site_infile', '')
    samp_infile = kwargs.get('samp_infile', '')
    spec_infile = kwargs.get('spec_infile', '')
    inst = kwargs.get('ins', '')
    institution = kwargs.get('institution', '')
    syn = kwargs.get('syn', 0)
    syntype = kwargs.get('syntype', '')
    inst = kwargs.get('ins', '')
    bak = kwargs.get('bak', 0)
    units = kwargs.get('units', 0)
    if bak:
        meth = "LP-IRM-DCD"
        output = output_dir_path + "/irm.magic"
    fmt = kwargs.get('new', 0)
    xy = kwargs.get('xy', 0)
    if fmt:
        fmt = 'new'
    if not fmt:
        fmt = 'old'
    if xy:
        fmt = 'xy'
    if specnum != 0:
        specnum = -specnum
    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"
    else:
        Z = 0

    # read stuff in
    if site_infile:
        Sites, file_type = pmag.magic_read(site_infile)
    if samp_infile:
        Samps, file_type = pmag.magic_read(samp_infile)
    if spec_infile:
        Specs, file_type = pmag.magic_read(spec_infile)
    if agm_file:
        Data = pmag.open_file(agm_file)
        if not Data:
            print("you must provide a valid agm_file")
            return False, "you must provide a valid agm_file"
    if not agm_file:
        print(__doc__)
        print("agm_file field is required option")
        return False, "agm_file field is required option"
    if "ASCII" not in Data[0] and fmt != 'xy':
        fmt = 'new'
    measnum, start, end = 1, 0, 0
    if fmt == 'new':  # new Micromag formatted file
        end = 2
        for skip in range(len(Data)):
            line = Data[skip]
            rec = line.strip('\n').strip('\r').split()
            if 'Units' in line:
                units = rec[-1]
            if "Raw" in rec:
                start = skip + 2
            if ("Field" in rec) and ("Moment" in rec) and (not start):
                start = skip + 2
                break
    elif fmt == 'old':
        start = 2
        end = 1

    MeasRecs, SpecRecs, SampRecs, SiteRecs, LocRecs = [], [], [], [], []
    version_num = pmag.get_version()

    ##################################
    # parse data
    stop = len(Data) - end
    for line in Data[start:stop]:  # skip header stuff
        MeasRec, SpecRec, SampRec, SiteRec, LocRec = {}, {}, {}, {}, {}
        # take care of some paper work
        if syn == 0:
            MeasRec["specimen"] = specimen
            if specnum != 0:
                sample = specimen[:specnum]
            else:
                sample = specimen
            if samp_infile and Samps:  # if samp_infile was provided AND yielded sample data
                samp = pmag.get_dictitem(Samps, 'sample', sample, 'T')
                if len(samp) > 0:
                    site = samp[0]["site"]
                else:
                    site = ''
            if site_infile and Sites:  # if samp_infile was provided AND yielded sample data
                sites = pmag.get_dictitem(Sites, 'sample', sample, 'T')
                if len(sites) > 0:
                    site = sites[0]["site"]
                else:
                    site = ''
            else:
                site = pmag.parse_site(sample, samp_con, Z)
            if location != '' and location not in [x['location'] if 'location' in list(x.keys()) else '' for x in LocRecs]:
                LocRec['location'] = location
                LocRecs.append(LocRec)
            if site != '' and site not in [x['site'] if 'site' in list(x.keys()) else '' for x in SiteRecs]:
                SiteRec['location'] = location
                SiteRec['site'] = site
                SiteRecs.append(SiteRec)
            if sample != '' and sample not in [x['sample'] if 'sample' in list(x.keys()) else '' for x in SampRecs]:
                SampRec['site'] = site
                SampRec['sample'] = sample
                SampRecs.append(SampRec)
            if specimen != '' and specimen not in [x['specimen'] if 'specimen' in list(x.keys()) else '' for x in SpecRecs]:
                SpecRec["specimen"] = specimen
                SpecRec['sample'] = sample
                SpecRecs.append(SpecRec)
        else:
            SampRec["material_type"] = syntype
            MeasRec["specimen"] = specimen
            if specnum != 0:
                sample = specimen[:specnum]
            else:
                sample = specimen
            site = pmag.parse_site(sample, samp_con, Z)
            if location != '' and location not in [x['location'] if 'location' in list(x.keys()) else '' for x in LocRecs]:
                LocRec['location'] = location
                LocRecs.append(LocRec)
            if site != '' and site not in [x['site'] if 'site' in list(x.keys()) else '' for x in SiteRecs]:
                SiteRec['location'] = location
                SiteRec['site'] = site
                SiteRecs.append(SiteRec)
            if sample != '' and sample not in [x['sample'] if 'sample' in list(x.keys()) else '' for x in SampRecs]:
                SampRec['site'] = site
                SampRec['sample'] = sample
                SampRecs.append(SampRec)
            if specimen != '' and specimen not in [x['specimen'] if 'specimen' in list(x.keys()) else '' for x in SpecRecs]:
                SpecRec["specimen"] = specimen
                SpecRec['sample'] = sample
                SpecRecs.append(SpecRec)
        MeasRec['instrument_codes'] = inst
        MeasRec['institution'] = institution
        MeasRec['method_codes'] = meth
        MeasRec['experiment'] = specimen + ':' + meth
        if fmt == 'xy':
            rec = list(line.strip('\n').split())
        else:
            rec = list(line.strip('\n').strip('\r').split(','))
        #print (rec)
        if rec[0] != "":
            if units == 'cgs':
                field = float(rec[0]) * 1e-4  # convert from oe to tesla
            else:
                field = float(rec[0])  # field in tesla
            if meth == "LP-HYS":
                MeasRec['meas_field_dc'] = '%10.3e' % (field)
                MeasRec['treat_dc_field'] = '0'
            else:
                MeasRec['meas_field_dc'] = '0'
                MeasRec['treat_dc_field'] = '%10.3e' % (field)
            if units == 'cgs':
                MeasRec['magn_moment'] = '%10.3e' % (
                    float(rec[1]) * 1e-3)  # convert from emu to Am^2
            else:
                MeasRec['magn_moment'] = '%10.3e' % (float(rec[1]))  # Am^2
            MeasRec['treat_temp'] = '273'  # temp in kelvin
            MeasRec['meas_temp'] = '273'  # temp in kelvin
            MeasRec['quality'] = 'g'
            MeasRec['standard'] = 'u'
            MeasRec['treat_step_num'] = '%i' % (measnum)
            MeasRec['measurement'] = specimen + ":" + meth + '%i' % (measnum)
            measnum += 1
            MeasRec['software_packages'] = version_num
            MeasRec['description'] = ""
            MeasRecs.append(MeasRec)
    # we have to relabel LP-HYS method codes.  initial loop is LP-IMT, minor
    # loops are LP-M  - do this in measurements_methods function
    if meth == 'LP-HYS':
        recnum = 0
        while float(MeasRecs[recnum]['meas_field_dc']) < float(MeasRecs[recnum + 1]['meas_field_dc']) and recnum + 1 < len(MeasRecs):  # this is LP-IMAG
            MeasRecs[recnum]['method_codes'] = 'LP-IMAG'
            MeasRecs[recnum]['experiment'] = MeasRecs[recnum]['specimen'] + \
                ":" + 'LP-IMAG'
            recnum += 1
#
    con = nb.Contribution(output_dir_path, read_tables=[])

    # create MagIC tables
    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    # MeasOuts=pmag.measurements_methods3(MeasRecs,noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasRecs)
    # write MagIC tables to file
    con.write_table_to_file('specimens', custom_name=spec_file)
    con.write_table_to_file('samples', custom_name=samp_file)
    con.write_table_to_file('sites', custom_name=site_file)
    con.write_table_to_file('locations', custom_name=loc_file)
    con.write_table_to_file('measurements', custom_name=meas_file)

    return True, meas_file
示例#13
0
def main(command_line=True, **kwargs):
    """
    NAME
        utrecht_magic.py

    DESCRIPTION
        converts Utrecht magnetometer data files to magic_measurements files

    SYNTAX
        utrecht_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -f FILE: specify  input file, or
        -F FILE: specify output file, default is magic_measurements.txt
        -Fsa: specify er_samples format file for appending, default is new er_samples.txt (Not working yet)
        -WD: output directory for MagIC files
        -ncn: Site Naming Convention
         Site to Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2: default] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name = sample name
            [6] site name entered in site_name column in the orient.txt format input file  -- NOT CURRENTLY SUPPORTED
            [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
        -spc: number of characters to remove to generate sample names from specimen names
        -dmy: European date format
        -loc LOCNAME : specify location/study name
        -lat latitude of samples
        -lon longitude of samples
        -A: don't average replicate measurements
        -mcd: [SO-MAG,SO-SUN,SO-SIGHT...] supply how these samples were oriented
        -dc: B PHI THETA: dc lab field (in microTesla), phi,and theta must be input as a tuple "(DC,PHI,THETA)". If not input user will be asked for values, this is advantagious if there are differing dc fields between steps or specimens. Note: this currently only works with the decimal IZZI naming convetion (XXX.0,1,2,3 where XXX is the treatment temperature and 0 is a zero field step, 1 is in field, and 2 is a pTRM check, 3 is a tail check). All other steps are hardcoded dc_field = 0.

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

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

    meas_file = 'magic_measurements.txt'
    spec_file = 'er_specimens.txt'
    samp_file = 'er_samples.txt'
    site_file = 'er_sites.txt'
    meth_code = ""
    #
    # get command line arguments
    #
    if command_line:
        if '-WD' in sys.argv:
            ind = sys.argv.index('-WD')
            dir_path = sys.argv[ind + 1]
        if '-ID' in sys.argv:
            ind = sys.argv.index('-ID')
            input_dir_path = sys.argv[ind + 1]
        else:
            input_dir_path = dir_path
        output_dir_path = dir_path
        if "-h" in args:
            print(main.__doc__)
            return False
        if '-F' in args:
            ind = args.index("-F")
            meas_file = args[ind + 1]
        if '-Fsp' in args:
            ind = args.index("-Fsp")
            spec_file = args[ind + 1]
        if '-Fsa' in args:
            ind = args.index("-Fsa")
            samp_file = args[ind + 1]
        if '-Fsi' in args:  # LORI addition
            ind = args.index("-Fsi")
            site_file = args[ind + 1]
            #try:
            #    open(samp_file,'r')
            #    ErSamps,file_type=pmag.magic_read(samp_file)
            #    print 'sample information will be appended to ', samp_file
            #except:
            #    print samp_file,' not found: sample information will be stored in new er_samples.txt file'
            #    samp_file = output_dir_path+'/er_samples.txt'
        if '-f' in args:
            ind = args.index("-f")
            mag_file = args[ind + 1]
        if "-loc" in args:
            ind = args.index("-loc")
            er_location_name = args[ind + 1]
        if "-lat" in args:
            ind = args.index("-lat")
            site_lat = args[ind + 1]
        if "-lon" in args:
            ind = args.index("-lon")
            site_lon = args[ind + 1]
        if "-A" in args:
            noave = 1
        if "-mcd" in args:
            ind = args.index("-mcd")
            meth_code = args[ind + 1]
            #samp_con='5'
        if "-ncn" in args:
            ind = args.index("-ncn")
            samp_con = sys.argv[ind + 1]
            if "4" in samp_con:
                if "-" not in samp_con:
                    print(
                        "option [4] must be in form 4-Z where Z is an integer")
                    return False, "naming convention option [4] must be in form 4-Z where Z is an integer"
                else:
                    site_num = samp_con.split("-")[1]
                    samp_con = "4"
            elif "7" in samp_con:
                if "-" not in samp_con:
                    print(
                        "option [7] must be in form 7-Z where Z is an integer")
                    return False, "naming convention option [7] must be in form 7-Z where Z is an integer"
                else:
                    site_num = samp_con.split("-")[1]
                    samp_con = "7"
        else:
            samp_con = "1"
        if '-dc' in args:
            ind = args.index('-dc')
            DC_FIELD, DC_PHI, DC_THETA = list(
                map(float, args[ind + 1].strip('( ) [ ]').split(',')))
            DC_FIELD *= 1e-6
            yn = ''
            GET_DC_PARAMS = False
        else:
            DC_FIELD, DC_PHI, DC_THETA = 0, 0, -90
        if '-spc' in args:
            ind = args.index("-spc")
            specnum = -int(args[ind + 1])
        else:
            specnum = 0
        if '-dmy' in args:
            ind = args.index("-dmy")
            dmy_flag = True
        else:
            dmy_flag = False

    if not command_line:
        dir_path = kwargs.get('dir_path', '.')
        input_dir_path = kwargs.get('input_dir_path', dir_path)
        output_dir_path = dir_path
        meas_file = kwargs.get('meas_file', 'magic_measurements.txt')
        mag_file = kwargs.get('mag_file')
        spec_file = kwargs.get('spec_file',
                               'er_specimens.txt')  # specimen outfile
        samp_file = kwargs.get('samp_file', 'er_samples.txt')
        site_file = kwargs.get('site_file', 'er_sites.txt')  # site outfile
        er_location_name = kwargs.get('location_name', '')
        site_lat = kwargs.get('site_lat', '')
        site_lon = kwargs.get('site_lon', '')
        #oave = kwargs.get('noave', 0) # default (0) means DO average
        meth_code = kwargs.get('meth_code', "LP-NO")
        specnum = -int(kwargs.get('specnum', 0))
        samp_con = kwargs.get('samp_con', '2')
        if "4" in samp_con:
            if "-" not in samp_con:
                print("option [4] must be in form 4-Z where Z is an integer")
                return False, "naming convention option [4] must be in form 4-Z where Z is an integer"
            else:
                site_num = samp_con.split("-")[1]
                samp_con = "4"
        elif "7" in samp_con:
            if "-" not in samp_con:
                print("option [7] must be in form 7-Z where Z is an integer")
                return False, "naming convention option [7] must be in form 7-Z where Z is an integer"
            else:
                site_num = samp_con.split("-")[1]
                samp_con = "7"
        DC_FIELD, DC_PHI, DC_THETA = list(
            map(float, kwargs.get('dc_params', (0, 0, -90))))
        DC_FIELD *= 1e-6
        noave = kwargs.get('avg', True)
        dmy_flag = kwargs.get('dmy_flag', False)

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

    # parse data

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

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

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

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

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

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


# write out the data to MagIC data files
    pmag.magic_write(spec_file, SpecOuts, 'er_specimens')
    pmag.magic_write(samp_file, SampOuts, 'er_samples')
    pmag.magic_write(site_file, SiteOuts, 'er_sites')
    #    MagOuts = pmag.measurements_methods(MagRecs, noave)
    #    pmag.magic_write(meas_file, MagOuts, 'magic_measurements')
    pmag.magic_write(meas_file, MagRecs, 'magic_measurements')
    print("results put in ", meas_file)
    print("exit!")
    return True, meas_file
示例#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)
示例#15
0
def main():
    """
    NAME
        mst_magic.py
 
    DESCRIPTION
        converts MsT data (T,M) to magic_measurements format files

    SYNTAX
        mst_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify T,M  format input file, required
        -fsa SFILE: name with sample, site, location information
        -F FILE: specify output file, default is MsT_measurements.txt
        -dc H: specify applied field during measurement, default is 0.5 T
        -syn  : This is a synthetic specimen and has no sample/site/location information
        -spn SPEC: specimen name 
        -spc NUM : specify number of characters to designate a  specimen, default = 0
        -loc LOCNAME : specify location/study name, must have either LOCNAME or SAMPFILE or be a synthetic
        -ncn NCON:  specify naming convention: default is #1 below
       Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name same as sample
            [6] site is entered under a separate column
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.
        INPUT files: 
            T M:  T is in Centigrade and M is uncalibrated magnitude

    """
# initialize some stuff
    samp_con,Z="1","0"
    dir_path='.'
    citation='This study'
    args=sys.argv
    specnum,measnum=0,1
#
# get command line arguments
#
    user=""
    if '-WD' in args:
        ind=args.index("-WD")
        dir_path=args[ind+1]
    meas_file=dir_path+"/MsT_measurements.txt"
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-usr" in args:
        ind=args.index("-usr")
        user=args[ind+1]
    labfield='0.5'
    if "-dc" in args:
        ind=args.index("-dc")
        labfield=args[ind+1]
    if '-F' in args:
        ind=args.index("-F")
        meas_file=dir_path+'/'+args[ind+1]
    if "-fsa" in args:
        ind=args.index("-fsa")
        samp_file=dir_path+'/'+args[ind+1]
        Samps,file_type=pmag.magic_read(samp_file)
    if '-f' in args:
        ind=args.index("-f")
        infile=dir_path+'/'+args[ind+1]
        try:
            input=open(infile,'rU')
        except:
            print "bad mag file name"
            sys.exit()
    else: 
        print main.__doc__
        print "-f  is required option"
        sys.exit()
    if "-spc" in args:
        ind=args.index("-spc")
        specnum=int(args[ind+1])
        if specnum!=0:specnum=-specnum
    er_location_name,syn,specimen_name='unknown',0,'unknown'
    if "-loc" in args:
        ind=args.index("-loc")
        er_location_name=args[ind+1]
    if "-spn" in args:
        ind=args.index("-spn")
        specimen_name=args[ind+1]
    else: 
        print main.__doc__
        print "-spn  is required option"
        sys.exit()
    if "-syn" in args: syn=1
    if "-ncn" in args:
        ind=args.index("-ncn")
        samp_con=sys.argv[ind+1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print "option [4] must be in form 4-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="4"
            samp_con=sys.argv[ind+1]
        if "7" in samp_con:
            if "-" not in samp_con:
                print "option [7] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="7"
    MagRecs,specs=[],[]
    version_num=pmag.get_version()
    data=input.readlines()
    T0=float(data[0].split()[0])
    for line in data:
        instcode=""
        if len(line)>1:
            MagRec={}
            if syn==0: MagRec['er_location_name']=er_location_name
            MagRec['magic_software_packages']=version_num
            MagRec["treatment_dc_field"]=labfield
            rec=line.split()
            T=float(rec[0])
            MagRec["measurment_temp"]='%8.3e' % (float(rec[0])+273.) # temp in kelvin
            if T>T0:
                MagRec["magic_method_codes"]='LP-MW-I'
            elif T<T0:
                MagRec["magic_method_codes"]='LP-MC-I'
                T0=T 
            else:
                print 'skipping repeated temperature step'
                MagRec["magic_method_codes"]=''
            T0=T 
            MagRec["measurement_magnitude"]='%10.3e'% (float(rec[1])) # uncalibrated magnitude
            if syn==0:
                MagRec["er_specimen_name"]=specimen_name
                MagRec["er_site_name"]=""
                if specnum!=0:
                    MagRec["er_sample_name"]=specimen_name[:specnum]
                else:
                    MagRec["er_sample_name"]=specimen_name
                if "-fsa" in args:
                    for samp in Samps:
                        if samp["er_sample_name"] == MagRec["er_sample_name"]:
                            MagRec["er_location_name"]=samp["er_location_name"]
                            MagRec["er_site_name"]=samp["er_site_name"]
                            break
                elif int(samp_con)!=6:
                    site=pmag.parse_site(MagRec['er_sample_name'],samp_con,Z)
                    MagRec["er_site_name"]=site
                if MagRec['er_site_name']=="":
                    print 'No site name found for: ',MagRec['er_specimen_name'],MagRec['er_sample_name']
                if MagRec["er_location_name"]=="":
                    print 'no location name for: ',MagRec["er_specimen_name"] 
            else:
                MagRec["er_synthetic_name"]=specimen_name
                MagRec["er_location_name"]=""
                MagRec["er_sample_name"]=""
                MagRec["er_site_name"]=""
                MagRec["er_specimen_name"]=""
            MagRec["magic_instrument_codes"]=instcode
            MagRec["er_analyst_mail_names"]=user
            MagRec["er_citation_names"]=citation
            MagRec["measurement_flag"]='g'
            MagRec["measurement_number"]=str(measnum)
            measnum+=1
            MagRecs.append(MagRec) 
    for rec in MagRecs: # sort out the measurements by experiment type
        rec['magic_experiment_name']=specimen_name
        if rec['magic_method_codes']=='LP-MW-I':
            rec["magic_experiment_name"]=specimen_name+':LP-MW-I:Curie'
        elif rec['magic_method_codes']=='LP-MC-I':
            rec["magic_experiment_name"]=specimen_name+':LP-MC-I'
    pmag.magic_write(meas_file,MagRecs,'magic_measurements')
    print "results put in ",meas_file
示例#16
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
示例#17
0
def convert(**kwargs):

    # initialize some stuff
    methcode="LP-NO"
    phi,theta,peakfield,labfield=0,0,0,0
    pTRM,MD=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]
    tdec=[0,90,0,180,270,0,0,90,0]
    tinc=[0,0,90,0,0,-90,0,0,90]
    missing=1
    demag="N"
    citations='This study'
    fmt='old'
    Samps=[]
    trm=0
    irm=0

    #get args
    user = kwargs.get('user', '')
    dir_path = kwargs.get('dir_path', '.')
    output_dir_path = dir_path
    meas_file = kwargs.get('meas_file', 'measurements.txt')
    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
    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')
    location = kwargs.get('location', 'unknown')
    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', '')
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')
    timezone = kwargs.get('timezone', 'UTC')

    # 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(__doc__)
            print('not a valid coil specification')
            return False, '{} is not a valid coil specification'.format(coil)
    if mag_file:
        lines = pmag.open_file(mag_file)
        if not lines:
            print("you must provide a valid mag_file")
            return False, "you must provide a valid mag_file"
    if not mag_file:
        print(__doc__)
        print("mag_file field is required option")
        return False, "mag_file field is required option"
    if specnum!=0:
        specnum=-specnum
    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"
    else: Z = 0

    if codelist:
        codes=codelist.split(':')
        if "AF" in codes:
            demag='AF'
            if'-dc' not in sys.argv: methcode="LT-AF-Z"
            if'-dc' in sys.argv: methcode="LT-AF-I"
        if "T" in codes:
            demag="T"
            if '-dc' not in sys.argv: methcode="LT-T-Z"
            if '-dc' in sys.argv: 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=sys.argv.index("CR")
                cooling_rates=sys.argv[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

    MeasRecs,SpecRecs,SampRecs,SiteRecs,LocRecs=[],[],[],[],[]
    version_num=pmag.get_version()

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

    for line in lines:
        instcode=""
        if len(line)>2:
            MeasRec,SpecRec,SampRec,SiteRec,LocRec={},{},{},{},{}
            MeasRec['software_packages']=version_num
            MeasRec["description"]=""
            MeasRec["treat_temp"]='%8.3e' % (273) # room temp in kelvin
            MeasRec["meas_temp"]='%8.3e' % (273) # room temp in kelvin
            MeasRec["treat_ac_field"]='0'
            MeasRec["treat_dc_field"]='0'
            MeasRec["treat_dc_field_phi"]='0'
            MeasRec["treat_dc_field_theta"]='0'
            meas_type="LT-NO"
            rec=line.split()
            try: float(rec[0]); print("No specimen name for line #%d in the measurement file"%lines.index(line)); continue
            except ValueError: pass
            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"
                MeasRec["treat_dc_field"]='%8.3e'%(labfield)
                MeasRec["treat_dc_field_phi"]='%7.1f'%(phi)
                MeasRec["treat_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)
                dt=yyyy+":"+mm+":"+dd+":"+hh+":"+min+":00"
                local = pytz.timezone(timezone)
                naive = datetime.datetime.strptime(dt, "%Y:%m:%d:%H:%M:%S")
                local_dt = local.localize(naive, is_dst=None)
                utc_dt = local_dt.astimezone(pytz.utc)
                MeasRec["timestamp"]=utc_dt.strftime("%Y-%m-%dT%H:%M:%S")+"Z"
                if inst=="":
                    if code3[1][0]=='C':instcode='SIO-bubba'
                    if code3[1][0]=='G':instcode='SIO-flo'
                else:
                    instcode=''
                MeasRec["meas_n_orient"]=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)
                dt=yyyy+":"+mm+":"+dd+":"+hh+":"+min+":00"
                local = pytz.timezone(timezone)
                naive = datetime.datetime.strptime(dt, "%Y:%m:%d:%H:%M:%S")
                local_dt = local.localize(naive, is_dst=None)
                utc_dt = local_dt.astimezone(pytz.utc)
                MeasRec["timestamp"]=utc_dt.strftime("%Y-%m-%dT%H:%M:%S")+"Z"
                if inst=="":
                    if code1[6][0]=='C':
                        instcode='SIO-bubba'
                    if code1[6][0]=='G':
                        instcode='SIO-flo'
                else:
                    instcode=''
                if len(code1)>1:
                    MeasRec["meas_n_orient"]=code1[6][2]
                else:
                    MeasRec["meas_n_orient"]=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"
                    MeasRec["treat_dc_field"]='%8.3e'%(labfield)
                    MeasRec["treat_dc_field_phi"]='%7.1f'%(phi)
                    MeasRec["treat_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"
                    MeasRec["treat_dc_field"]='0'
            if syn==0:
                specimen=rec[0]
                MeasRec["specimen"]=specimen
                if specnum!=0:
                    sample=rec[0][:specnum]
                else:
                    sample=rec[0]
                if samp_infile and Samps: # if samp_infile was provided AND yielded sample data
                    samp=pmag.get_dictitem(Samps,'sample',sample,'T')
                    if len(samp)>0:
                        location=samp[0]["location"]
                        site=samp[0]["site"]
                    else:
                        location=''
                        site=''
                else:
                    site=pmag.parse_site(sample,samp_con,Z)
                if location!='' and location not in [x['location'] if 'location' in list(x.keys()) else '' for x in LocRecs]:
                    LocRec['location'] = location
                    LocRec['lat_n'] = lat
                    LocRec['lat_s'] = lat
                    LocRec['lon_e'] = lon
                    LocRec['lon_w'] = lon
                    LocRecs.append(LocRec)
                if site!='' and site not in [x['site'] if 'site' in list(x.keys()) else '' for x in SiteRecs]:
                    SiteRec['location'] = location
                    SiteRec['site'] = site
                    SiteRec['lat'] = lat
                    SiteRec['lon'] = lon
                    SiteRecs.append(SiteRec)
                if sample!='' and sample not in [x['sample'] if 'sample' in list(x.keys()) else '' for x in SampRecs]:
                    SampRec['site'] = site
                    SampRec['sample'] = sample
                    SampRecs.append(SampRec)
                if specimen!='' and specimen not in [x['specimen'] if 'specimen' in list(x.keys()) else '' for x in SpecRecs]:
                    SpecRec["specimen"]=specimen
                    SpecRec['sample'] = sample
                    SpecRecs.append(SpecRec)
            else:
                specimen=rec[0]
                MeasRec["specimen"]=specimen
                if specnum!=0:
                    sample=rec[0][:specnum]
                else:
                    sample=rec[0]
                site=pmag.parse_site(sample,samp_con,Z)
                if location!='' and location not in [x['location'] if 'location' in list(x.keys()) else '' for x in LocRecs]:
                    LocRec['location'] = location
                    LocRec['lat_n'] = lat
                    LocRec['lat_s'] = lat
                    LocRec['lon_e'] = lon
                    LocRec['lon_w'] = lon
                    LocRecs.append(LocRec)
                if site!='' and site not in [x['site'] if 'site' in list(x.keys()) else '' for x in SiteRecs]:
                    SiteRec['location'] = location
                    SiteRec['site'] = site
                    SiteRec['lat'] = lat
                    SiteRec['lon'] = lon
                    SiteRecs.append(SiteRec)
                if sample!='' and sample not in [x['sample'] if 'sample' in list(x.keys()) else '' for x in SampRecs]:
                    SampRec['site'] = site
                    SampRec['sample'] = sample
                    SampRecs.append(SampRec)
                if specimen!='' and specimen not in [x['specimen'] if 'specimen' in list(x.keys()) else '' for x in SpecRecs]:
                    SpecRec["specimen"]=specimen
                    SpecRec['sample'] = sample
                    SpecRecs.append(SpecRec)
                SampRec["institution"]=institution
                SampRec["material_type"]=syntype
            if float(rec[1])==0:
                pass
            elif demag=="AF":
                if methcode != "LP-AN-ARM":
                    MeasRec["treat_ac_field"]='%8.3e' %(float(rec[1])*1e-3) # peak field in tesla
                    if meas_type=="LT-AF-Z": MeasRec["treat_dc_field"]='0'
                else: # AARM experiment
                    if treat[1][0]=='0':
                        meas_type="LT-AF-Z:LP-AN-ARM:"
                        MeasRec["treat_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
                        MeasRec["treat_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
                        MeasRec["treat_dc_field_phi"]='%7.1f' %(dec[ipos])
                        MeasRec["treat_dc_field_theta"]='%7.1f'% (inc[ipos])
                        MeasRec["treat_dc_field"]='%8.3e'%(labfield)
                        MeasRec["treat_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
            elif demag=="T" and methcode == "LP-AN-TRM":
                MeasRec["treat_temp"]='%8.3e' % (float(treat[0])+273.) # temp in kelvin
                if treat[1][0]=='0':
                    meas_type="LT-T-Z:LP-AN-TRM"
                    MeasRec["treat_dc_field"]='%8.3e'%(0)
                    MeasRec["treat_dc_field_phi"]='0'
                    MeasRec["treat_dc_field_theta"]='0'
                else:
                    MeasRec["treat_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
                    MeasRec["treat_dc_field_phi"]='%7.1f' %(tdec[ipos])
                    MeasRec["treat_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:
                        MeasRec["treat_ac_field"]='%8.3e' % (float(treat[0])*1e-3) # AF field in tesla
                        MeasRec["treat_dc_field"]='0'
                        meas_type="LT-AF-Z" # first AF
                    else:
                        meas_type="LT-NO"
                        MeasRec["treat_ac_field"]='0'
                        MeasRec["treat_dc_field"]='0'
                elif treat[1][1]=='1':
                    if int(treat[0])==0:
                        MeasRec["treat_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
                        MeasRec["treat_dc_field"]='%8.3e'%(arm_labfield)
                        MeasRec["treat_dc_field_phi"]='%7.1f'%(phi)
                        MeasRec["treat_dc_field_theta"]='%7.1f'%(theta)
                        meas_type="LT-AF-I"
                    else:
                        MeasRec["treat_ac_field"]='%8.3e' % ( float(treat[0])*1e-3) # AF field in tesla
                        MeasRec["treat_dc_field"]='0'
                        meas_type="LT-AF-Z"
                elif treat[1][1]=='2':
                    if int(treat[0])==0:
                        MeasRec["treat_ac_field"]='0'
                        MeasRec["treat_dc_field"]='%8.3e'%(trm_labfield)
                        MeasRec["treat_dc_field_phi"]='%7.1f'%(phi)
                        MeasRec["treat_dc_field_theta"]='%7.1f'%(theta)
                        MeasRec["treat_temp"]='%8.3e' % (trm_peakT)
                        meas_type="LT-T-I"
                    else:
                        MeasRec["treat_ac_field"]='%8.3e' % ( float(treat[0])*1e-3) # AF field in tesla
                        MeasRec["treat_dc_field"]='0'
                        meas_type="LT-AF-Z"
                elif treat[1][1]=='3':
                    if int(treat[0])==0:
                        MeasRec["treat_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
                        MeasRec["treat_dc_field"]='%8.3e'%(arm_labfield)
                        MeasRec["treat_dc_field_phi"]='%7.1f'%(phi)
                        MeasRec["treat_dc_field_theta"]='%7.1f'%(theta)
                        meas_type="LT-AF-I"
                    else:
                        MeasRec["treat_ac_field"]='%8.3e' % ( float(treat[0])*1e-3) # AF field in tesla
                        MeasRec["treat_dc_field"]='0'
                        meas_type="LT-AF-Z"


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

                MeasRec["treat_temp"]='%8.3e' % (float(treat[0])+273.) # temp in kelvin
                if treat[1][0]=='0':
                    meas_type="LT-T-Z:LP-CR-TRM"
                    MeasRec["treat_dc_field"]='%8.3e'%(0)
                    MeasRec["treat_dc_field_phi"]='0'
                    MeasRec["treat_dc_field_theta"]='0'
                else:
                    MeasRec["treat_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"
                    MeasRec["treat_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                    MeasRec["treat_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]
                    MeasRec["description"]="cooling_rate"+":"+cooling_time+":"+"K/min"


            elif demag!='N':
              if len(treat)==1:treat.append('0')
              MeasRec["treat_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:
                    MeasRec["treat_dc_field"]='%8.3e' % (labfield) # labfield in tesla (convert from microT)
                    MeasRec["treat_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                    MeasRec["treat_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':
                        MeasRec["treat_dc_field"]='0'  # this is a zero field step
                        meas_type="LT-PTRM-MD" # pTRM tail check
              else:
                labfield=float(treat[1])*1e-6
                MeasRec["treat_dc_field"]='%8.3e' % (labfield) # labfield in tesla (convert from microT)
                MeasRec["treat_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                MeasRec["treat_dc_field_theta"]='%7.1f' % (theta) # labfield theta
                meas_type="LT-T-I:LP-TRM" # trm acquisition experiment

            MeasRec["dir_csd"]=rec[2]
            MeasRec["magn_moment"]='%10.3e'% (float(rec[3])*1e-3) # moment in Am^2 (from emu)
            MeasRec["dir_dec"]=rec[4]
            MeasRec["dir_inc"]=rec[5]
            MeasRec["instrument_codes"]=instcode
            MeasRec["analysts"]=user
            MeasRec["citations"]=citations
            if "LP-IRM-3D" in methcode : meas_type=methcode
            #MeasRec["method_codes"]=methcode.strip(':')
            MeasRec["method_codes"]=meas_type
            MeasRec["quality"]='g'
            if 'std' in rec[0]:
                MeasRec["standard"]='s'
            else:
                MeasRec["standard"]='u'
            MeasRec["treat_step_num"]='1'
            #print MeasRec['treat_temp']
            MeasRecs.append(MeasRec)

    con = nb.Contribution(output_dir_path,read_tables=[])

    # create MagIC tables
    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts=pmag.measurements_methods3(MeasRecs,noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)
    # write MagIC tables to file
    con.tables['specimens'].write_magic_file(custom_name=spec_file)
    con.tables['samples'].write_magic_file(custom_name=samp_file)
    con.tables['sites'].write_magic_file(custom_name=site_file)
    con.tables['locations'].write_magic_file(custom_name=loc_file)
    con.tables['measurements'].write_magic_file(custom_name=meas_file)

    return True, meas_file
示例#18
0
def convert(**kwargs):

    version_num = pmag.get_version()
    dir_path = kwargs.get('dir_path', '.')
    mag_file = kwargs.get('mag_file')
    input_dir_path = kwargs.get('input_dir_path', dir_path)
    if input_dir_path == dir_path:
        mag_file = pmag.resolve_file_name(mag_file, dir_path)
        input_dir_path = os.path.split(mag_file)[0]
    output_dir_path = 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
    specnum = kwargs.get('specnum', 1)
    samp_con = kwargs.get('samp_con', '1')
    location = kwargs.get('location', 'unknown')
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')
    noave = kwargs.get('noave', 0)  # default (0) means DO average
    meth_code = kwargs.get('meth_code', "LP-NO")
    volume = float(kwargs.get('volume', 2.5)) * 1e-6
    timezone = kwargs.get('timestamp', 'UTC')

    # format variables
    mag_file = os.path.join(input_dir_path, mag_file)
    if specnum != 0: specnum = -int(specnum)
    if samp_con.startswith("4"):
        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 samp_con.startswith("7"):
        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"
    else:
        Z = 1

    #create data holders
    MeasRecs, SpecRecs, SampRecs, SiteRecs, LocRecs = [], [], [], [], []

    data = pmag.open_file(mag_file)
    # remove garbage/blank lines
    data = [i for i in data if len(i) >= 5]
    if not len(data):
        print('No data')
        return

    n = 0
    end = False
    # loop through records
    while not end:
        first_line = data[n].split()
        sampleName = first_line[0]
        demagLevel = first_line[2]
        date = first_line[3] + ":0:0:0"
        n += 2
        third_line = data[n].split()
        if not third_line[0].startswith('SPEC.ANGLES'):
            print('third line of a block should start with SPEC.ANGLES')
            print(third_line)
            return
        specimenAngleDec = third_line[1]
        specimenAngleInc = third_line[2]
        n += 4
        while not data[n].startswith('MEAN'):
            n += 1
        mean_line = data[n]
        Mx = mean_line[1]
        My = mean_line[2]
        Mz = mean_line[3]
        n += 1
        precision_line = data[n].split()
        if not precision_line[0].startswith('Modulus'):
            print('precision line should start with "Modulus"')
            return
        splitExp = precision_line[2].split('A')
        intensityVolStr = precision_line[1] + splitExp[0]
        intensityVol = float(intensityVolStr)
        # check and see if Prec is too big and messes with the parcing.
        precisionStr = ''
        if len(precision_line) == 6:  #normal line
            precisionStr = precision_line[5][0:-1]
        else:
            precisionStr = precision_line[4][0:-1]

        precisionPer = float(precisionStr)
        precision = intensityVol * precisionPer / 100

        while not data[n].startswith('SPEC.'):
            n += 1
        specimen_line = data[n].split()
        specimenDec = specimen_line[2]
        specimenInc = specimen_line[3]
        n += 1
        geographic_line = data[n]
        if not geographic_line.startswith('GEOGR'):
            geographic_dec = ''
            geographic_inc = ''
        else:
            geographic_line = geographic_line.split()
            geographicDec = geographic_line[1]
            geographicInc = geographic_line[2]
        # Add data to various MagIC data tables.
        specimen = sampleName
        if specnum != 0: sample = specimen[:specnum]
        else: sample = specimen
        site = pmag.parse_site(sample, samp_con, Z)

        MeasRec, SpecRec, SampRec, SiteRec, LocRec = {}, {}, {}, {}, {}

        if specimen != "" and specimen not in [
                x['specimen'] if 'specimen' in list(x.keys()) else ""
                for x in SpecRecs
        ]:
            SpecRec['specimen'] = specimen
            SpecRec['sample'] = sample
            SpecRec["citations"] = "This study"
            SpecRec["analysts"] = user
            SpecRec['volume'] = volume
            SpecRecs.append(SpecRec)
        if sample != "" and sample not in [
                x['sample'] if 'sample' in list(x.keys()) else ""
                for x in SampRecs
        ]:
            SampRec['sample'] = sample
            SampRec['site'] = site
            SampRec["citations"] = "This study"
            SampRec["analysts"] = user
            SampRec['azimuth'] = specimenAngleDec
            sample_dip = str(float(specimenAngleInc) -
                             90.0)  #convert to magic orientation
            SampRec['dip'] = sample_dip
            SampRec['method_codes'] = meth_code
            SampRecs.append(SampRec)
        if site != "" and site not in [
                x['site'] if 'site' in list(x.keys()) else "" for x in SiteRecs
        ]:
            SiteRec['site'] = site
            SiteRec['location'] = location
            SiteRec["citations"] = "This study"
            SiteRec["analysts"] = user
            SiteRec['lat'] = lat
            SiteRec['lon'] = lon
            SiteRecs.append(SiteRec)
        if location != "" and location not in [
                x['location'] if 'location' in list(x.keys()) else ""
                for x in LocRecs
        ]:
            LocRec['location'] = location
            LocRec["citations"] = "This study"
            LocRec["analysts"] = user
            LocRec['lat_n'] = lat
            LocRec['lon_e'] = lon
            LocRec['lat_s'] = lat
            LocRec['lon_w'] = lon
            LocRecs.append(LocRec)

        local = pytz.timezone(timezone)
        naive = datetime.datetime.strptime(date, "%m-%d-%Y:%H:%M:%S")
        local_dt = local.localize(naive, is_dst=None)
        utc_dt = local_dt.astimezone(pytz.utc)
        timestamp = utc_dt.strftime("%Y-%m-%dT%H:%M:%S") + "Z"
        MeasRec["specimen"] = specimen
        MeasRec["timestamp"] = timestamp
        MeasRec['description'] = ''
        MeasRec["citations"] = "This study"
        MeasRec['software_packages'] = version_num
        MeasRec["treat_temp"] = '%8.3e' % (273)  # room temp in kelvin
        MeasRec["meas_temp"] = '%8.3e' % (273)  # room temp in kelvin
        MeasRec["quality"] = 'g'
        MeasRec["standard"] = 'u'
        MeasRec["treat_step_num"] = '1'
        MeasRec["treat_ac_field"] = '0'
        if demagLevel == 'NRM':
            meas_type = "LT-NO"
        elif demagLevel[0] == 'A':
            if demagLevel[:2] == 'AD':
                treat = float(demagLevel[2:])
            else:
                treat = float(demagLevel[1:])
            meas_type = "LT-AF-Z"
            MeasRec["treat_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:])
            MeasRec["treat_temp"] = '%8.3e' % (treat + 273.)  # temp in kelvin
        else:
            print("measurement type unknown", demagLevel)
            return False, "measurement type unknown"

        MeasRec["magn_moment"] = str(intensityVol * volume)  # Am^2
        MeasRec["magn_volume"] = intensityVolStr  # A/m
        MeasRec["dir_dec"] = specimenDec
        MeasRec["dir_inc"] = specimenInc
        MeasRec['method_codes'] = meas_type
        MeasRecs.append(MeasRec)

        # ignore all the rest of the special characters. Some data files not consistantly formatted.
        n += 1
        while ((len(data[n]) <= 5 and data[n] != '')
               or data[n].startswith('----')):
            n += 1
            if n >= len(data):
                break
        if n >= len(data):
            # we're done!
            end = True

        #end of data while loop

    con = nb.Contribution(output_dir_path, read_tables=[])

    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts = pmag.measurements_methods3(MeasRecs, noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    con.tables['specimens'].write_magic_file(custom_name=spec_file)
    con.tables['samples'].write_magic_file(custom_name=samp_file)
    con.tables['sites'].write_magic_file(custom_name=site_file)
    con.tables['locations'].write_magic_file(custom_name=loc_file)
    con.tables['measurements'].write_magic_file(custom_name=meas_file)

    return True, meas_file
示例#19
0
def convert(**kwargs):
    """
    Converts CIT formated Magnetometer data into MagIC format for Analysis and contribution to the MagIC database

    Parameters
    -----------
    dir_path : directory to output files to (default : current directory)
    user : colon delimited list of analysts (default : "")
    magfile : magnetometer file (.sam) to convert to MagIC (required)
    meas_file : measurement file name to output (default : measurements.txt)
    spec_file : specimen file name to output (default : specimens.txt)
    samp_file : sample file name to output (default : samples.txt)
    site_file : site file name to output (default : site.txt)
    loc_file : location file name to output (default : locations.txt)
    locname : location name
    methods : colon delimited list of sample method codes. full list here (https://www2.earthref.org/MagIC/method-codes) (default : SO-MAG
    specnum : number of terminal characters that identify a specimen
    norm : is volume or mass normalization using cgs or si units (options : cc,m3,g,kg) (default : cc)
    noave : average measurement data or not. False is average, True is don't average. (default : False)
    samp_con : sample naming convention options as follows:
        [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 sitename 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
    input_dir_path : if you did not supply a full path with magfile you can put the directory the magfile is in here
    meas_n_orient : Number of different orientations in measurement (default : 8)
    labfield : DC_FIELD in microTesla (default : 0)
    phi : DC_PHI in degrees (default : 0)
    theta : DC_THETA in degrees (default : 0)

    Returns
    -----------
    type - Tuple : (True or False indicating if conversion was sucessful, meas_file name written)
    """
    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
    locname = kwargs.get('locname', 'unknown')
    sitename = kwargs.get('sitename', '')
    methods = kwargs.get('methods', ['SO-MAG'])
    specnum = -int(kwargs.get('specnum', 0))
    norm = kwargs.get('norm', 'cc')
    noave = kwargs.get('noave', False)  # False means do average
    samp_con = kwargs.get('samp_con', '3')
    magfile = kwargs.get('magfile', '')
    input_dir_path = kwargs.get('input_dir_path',os.path.split(magfile)[0])
    meas_n_orient = kwargs.get('meas_n_orient','8')
    output_dir_path = dir_path
    try:
        DC_FIELD = float(kwargs.get('labfield',0))*1e-6
        DC_PHI = float(kwargs.get('phi',0))
        DC_THETA = float(kwargs.get('theta',0))
    except ValueError: raise ValueError('problem with your dc parameters. please provide a labfield in microTesla and a phi and theta in degrees.')
    yn = ''
    if DC_FIELD==0 and DC_PHI==0 and DC_THETA==0: GET_DC_PARAMS=True
    else: GET_DC_PARAMS=False
    if locname=='' or locname==None: locname = 'unknown'
    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"
    else: Z=1

    #get file names and open magfile to start reading data
    if input_dir_path=='': input_dir_path='.'
    magfile = os.path.join(input_dir_path, magfile)
    FIRST_GET_DC=True
    try:
        file_input=open(magfile,'r')
    except IOError as ex:
        print(("bad sam file name: ", magfile))
        return False, "bad sam file name"
    File = file_input.readlines()
    file_input.close()
    if len(File) == 1: File = File[0].split('\r'); File = [x+"\r\n" for x in File]

    #define initial variables
    SpecRecs,SampRecs,SiteRecs,LocRecs,MeasRecs=[],[],[],[],[]
    sids,ln,format,citations=[],0,'CIT',"This study"
    formats=['CIT','2G','APP','JRA']

    if File[ln].strip()=='CIT': ln+=1
    LocRec={}
    LocRec["location"]=locname
    LocRec["citations"]=citations
    LocRec['analysts']=user
    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["lat_n"]=site_lat
        LocRec["lon_e"]=site_lon
        LocRec["lat_s"]=site_lat
        LocRec["lon_w"]=site_lon
        LocRecs.append(LocRec)
        Cdec=float(line[2])
        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
        if sitename: site=sitename
        else: site=pmag.parse_site(sample,samp_con,Z)
        SpecRec['specimen']=specimen
        SpecRec['sample']=sample
        SpecRec['citations']=citations
        SpecRec['analysts']=user
        SampRec['sample']=sample
        SampRec['site']=site
        SampRec['citations']=citations
        SampRec['method_codes']=methods
        SampRec['azimuth_dec_correction']='%7.1f'%(Cdec)
        SampRec['analysts']=user
        SiteRec['site']=site
        SiteRec['location']=locname
        SiteRec['citations']=citations
        SiteRec['lat']=site_lat
        SiteRec['lon']=site_lon
        SiteRec['analysts']=user
        f=open(os.path.join(input_dir_path,specimen),'r')
        Lines=f.readlines()
        f.close()
        comment=""
        line=Lines[0].split()
        if len(line)>2:
            comment=line[2]
        info=Lines[1].split()
        volmass=float(info[-1])
        if volmass==1.0:
            print('Warning: Specimen volume set to 1.0.')
            print('Warning: If volume/mass really is 1.0, set volume/mass to 1.001')
            print('Warning: specimen method code LP-NOMAG set.')
            SpecRec['weight']=""
            SpecRec['volume']=""
            SpecRec['method_codes']='LP-NOMAG'
        elif norm=="gm":
            SpecRec['volume']=''
            SpecRec['weight']='%10.3e'%volmass*1e-3
        elif norm=="kg":
            SpecRec['volume']=''
            SpecRec['weight']='%10.3e'*volmass
        elif norm=="cc":
            SpecRec['weight']=""
            SpecRec['volume']='%10.3e'%(volmass*1e-6)
        elif norm=="m3":
            SpecRec['weight']=""
            SpecRec['volume']='%10.3e'%(volmass)
        else:
            print('Warning: Unknown normalization unit ', norm, '. Using default of cc')
            SpecRec['weight']=""
            SpecRec['volume']='%10.3e'%(volmass*1e-6)
        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['height']=info[-6]
        else:
            SampRec['height']='0'
        SampRec['azimuth']='%7.1f'%(sample_azimuth)
        SampRec['dip']='%7.1f'%(sample_dip)
        SampRec['bed_dip']='%7.1f'%(dip)
        SampRec['bed_dip_direction']='%7.1f'%(dip_direction)
        SampRec['geologic_classes']=''
        SampRec['geologic_types']=''
        SampRec['lithologies']=''
        if Cdec!=0 or Cdec!="":
            SampRec['method_codes']='SO-CMD-NORTH'
        else:
            SampRec['method_codes']='SO-MAG'
        for line in Lines[2:len(Lines)]:
            if line == '\n': continue
            MeasRec=SpecRec.copy()
            MeasRec.pop('sample')
            MeasRec['analysts']=user
#           Remove volume and weight as they do not exits in the magic_measurement table
            del MeasRec["volume"]
            del MeasRec["weight"]
            if line[3:6]=='   ' : # USGS files have blank for an AF demag value when measurement is the NRM. njarboe
                line = 'NRM' + line[3:]
            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.split()=='': treat='0'
                try: float(treat)
                except ValueError: treat = line.split()[1]
            if treat_type.startswith('NRM'):
                MeasRec['method_codes']='LT-NO'
                MeasRec['meas_temp']='273'
                MeasRec['treat_temp']='273'
                MeasRec['treat_dc_field']='0'
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treat_ac_field']='0'
            elif treat_type.startswith('LT') or treat_type.upper().startswith('LN2'):
                MeasRec['method_codes']='LT-LT-Z'
                MeasRec['meas_temp']='273'
                MeasRec['treat_temp']='77'
                MeasRec['treat_dc_field']='0'
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treat_ac_field']='0'
            elif treat_type.startswith('AF') or treat_type.startswith('MAF'):
                MeasRec['method_codes']='LT-AF-Z'
                MeasRec['meas_temp']='273'
                MeasRec['treat_temp']='273'
                MeasRec['treat_dc_field']='0'
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                if treat.strip() == '':
                    MeasRec['treat_ac_field']='0'
                else:
                    try: MeasRec['treat_ac_field']='%10.3e'%(float(treat)*1e-3)
                    except ValueError as e: print(os.path.join(input_dir_path,specimen)); raise e
            elif treat_type.startswith('ARM'):
                MeasRec['method_codes']="LP-ARM"
                MeasRec['meas_temp']='273'
                MeasRec['treat_temp']='273'
                MeasRec['treat_dc_field']='0'
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                if treat.strip() == '':
                    MeasRec['treat_ac_field']='0'
                else:
                    MeasRec['method_codes']="LP-ARM-AFD"
                    MeasRec['treat_ac_field']='%10.3e'%(float(treat)*1e-3)
            elif treat_type.startswith('IRM'):
                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['method_codes']="LT-IRM"
                MeasRec['meas_temp']='273'
                MeasRec['treat_temp']='273'
                MeasRec['treat_dc_field']='%1.2e'%DC_FIELD
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treat_ac_field']='0'
            elif treat_type.startswith('TT'):
                MeasRec['method_codes']='LT-T-Z'
                MeasRec['meas_temp']='273'
                if treat.strip() == '':
                    MeasRec['treat_temp']='273'
                else:
                    MeasRec['treat_temp']='%7.1f'%(float(treat)+273)
                MeasRec['treat_dc_field']='0'
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treat_ac_field']='0'
            elif line[4] == '0': #assume decimal IZZI format 0 field thus can hardcode the dc fields
                MeasRec['method_codes']='LT-T-Z'
                MeasRec['meas_temp']='273'
                try: MeasRec['treat_temp']=str(int(treat_type) + 273)
                except ValueError as e: print(specimen); raise e
                MeasRec['treat_dc_field']='0'
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treat_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['method_codes']='LT-T-I'
                MeasRec['meas_temp']='273'
                MeasRec['treat_temp']=str(int(treat_type) + 273)
                MeasRec['treat_dc_field']='%1.2e'%DC_FIELD
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treat_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['method_codes']='LT-PTRM-I'
                MeasRec['meas_temp']='273'
                MeasRec['treat_temp']=str(int(treat_type) + 273)
                MeasRec['treat_dc_field']='%1.2e'%DC_FIELD
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treat_ac_field']='0'
            elif line[4] == '3': #assume decimal IZZI format PTRM tail check
                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['method_codes']='LT-PTRM-Z'
                MeasRec['meas_temp']='273'
                MeasRec['treat_temp']=str(int(treat_type) + 273)
                MeasRec['treat_dc_field']='0'
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treat_ac_field']='0'
            else:
                print("trouble with your treatment steps")
            MeasRec['dir_dec']=line[46:51]
            MeasRec['dir_inc']=line[52:58]
#           Some MIT files have and extra digit in the exponent of the magnetude. 
#           That makes those files not compliant with the cit measurement file spec.
#           Not sure if we should just print an error message and exit. For now we accept the file and fix it.
#           The first digit of the exponent, which should always be zero, is cut out of the line if column 39 is not ' ' 
            if line[39] != ' ': line = line[0:37] + line[38:]
            M='%8.2e'%(float(line[31:39])*volmass*1e-3) # convert to Am2
            MeasRec['magn_moment']=M
            MeasRec['dir_csd']='%7.1f'%(eval(line[41:46]))
            MeasRec["meas_n_orient"]=meas_n_orient
            MeasRec['standard']='u'
            if len(line)>60:
                MeasRec['instrument_codes']=line[85:].strip('\n \r \t "')
                MeasRec['magn_x_sigma']='%8.2e'%(float(line[58:67])*1e-8) #(convert e-5emu to Am2)
                MeasRec['magn_y_sigma']='%8.2e'%(float(line[67:76])*1e-8)
                MeasRec['magn_z_sigma']='%8.2e'%(float(line[76:85])*1e-8)
            MeasRecs.append(MeasRec)
        SpecRecs.append(SpecRec)
        if sample not in samples:
            samples.append(sample)
            SampRecs.append(SampRec)
        if site not in sites:
            sites.append(site)
            SiteRecs.append(SiteRec)

    con = nb.Contribution(output_dir_path,read_tables=[])

    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts=pmag.measurements_methods3(MeasRecs,noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    con.tables['specimens'].write_magic_file(custom_name=spec_file)
    con.tables['samples'].write_magic_file(custom_name=samp_file)
    con.tables['sites'].write_magic_file(custom_name=site_file)
    con.tables['locations'].write_magic_file(custom_name=loc_file)
    con.tables['measurements'].write_magic_file(custom_name=meas_file)

    return True, meas_file
示例#20
0
def convert(**kwargs):

    user = kwargs.get('user', '')
    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', 'measurements.txt')
    spec_file = kwargs.get('spec_file', 'specimens.txt')  # specimen outfile
    samp_file = kwargs.get('samp_file', 'samples.txt')
    site_file = kwargs.get('site_file', 'sites.txt')  # site outfile
    loc_file = kwargs.get('loc_file', 'locations.txt')  # Loc outfile
    magfile = kwargs.get('magfile', '')
    datafile = kwargs.get('datafile', '')
    specnum = int(kwargs.get('specnum', 0))
    labfield = int(kwargs.get('labfield', 0)) * 1e-6
    phi = int(kwargs.get('phi', 0))
    theta = int(kwargs.get('theta', 0))
    peakfield = kwargs.get('peakfield', 0)
    if peakfield:
        peakfield = float(peakfield) * 1e-3
    location = kwargs.get('location', '')
    samp_con = kwargs.get('samp_con', '1')
    codelist = kwargs.get('codelist', '')
    CR_cooling_times = kwargs.get('CR_cooling_times', None)
    noave = kwargs.get('noave', False)

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

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

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

    if "TRM" in codes:
        demag = "T"
        LPcode = "LP-TRM"
        #trm=1

    if "CR" in codes:
        demag = "T"
        # dc should be in the code
        if not labfield:
            print("missing lab field option")
            return False, "missing lab field option"

        LPcode = "LP-CR-TRM"  # TRM in different cooling rates
        if command_line:
            ind = sys.argv.index("-LP")
            CR_cooling_times = sys.argv[ind + 2].split(",")

    version_num = pmag.get_version()

    #--------------------------------------
    # Read the file
    # Assumption:
    # 1. different lab protocolsa are in different files
    # 2. measurements are in the correct order
    #--------------------------------------

    Data = {}
    line_no = 0
    for line in infile.readlines():
        line_no += 1
        this_line_data = {}
        line_no += 1
        instcode = ""
        if len(line) < 2:
            continue
        if line[0] == "#":  #HUJI way of marking bad data points
            continue

        rec = line.strip('\n').split()
        specimen = rec[0]
        date = rec[2].split("/")
        hour = rec[3].split(":")
        treatment_type = rec[4]
        treatment = rec[5].split(".")
        dec_core = rec[6]
        inc_core = rec[7]
        moment_emu = float(rec[-1])

        if specimen not in list(Data.keys()):
            Data[specimen] = []

        # check duplicate treatments:
        # if yes, delete the first and use the second

        if len(Data[specimen]) > 0:
            if treatment == Data[specimen][-1]['treatment']:
                del (Data[specimen][-1])
                print(
                    "-W- Identical treatments in file %s magfile line %i: specimen %s, treatment %s ignoring the first. "
                    % (magfile, line_no, specimen, ".".join(treatment)))

        this_line_data = {}
        this_line_data['specimen'] = specimen
        this_line_data['date'] = date
        this_line_data['hour'] = hour
        this_line_data['treatment_type'] = treatment_type
        this_line_data['treatment'] = treatment
        this_line_data['dec_core'] = dec_core
        this_line_data['inc_core'] = inc_core
        this_line_data['moment_emu'] = moment_emu
        this_line_data['azimuth'] = ''
        this_line_data['dip'] = ''
        this_line_data['bed_dip_direction'] = ''
        this_line_data['bed_dip'] = ''
        this_line_data['lat'] = ''
        this_line_data['lon'] = ''
        this_line_data['volume'] = ''
        Data[specimen].append(this_line_data)
    infile.close()
    print("-I- done reading file %s" % magfile)

    if datafile:
        dinfile = open(datafile)
        for line in dinfile.readlines():
            data = line.split()
            if len(data) < 8 or data[0] == '': continue
            elif data[0] in list(Data.keys()):
                for i in range(len(Data[data[0]])):
                    Data[data[0]][i]['azimuth'] = data[1]
                    Data[data[0]][i]['dip'] = data[2]
                    try:
                        Data[data[0]][i]['bed_dip_direction'] = float(
                            data[3]) + 90
                    except ValueError:
                        Data[data[0]][i]['bed_dip_direction'] = ''
                    Data[data[0]][i]['bed_dip'] = data[4]
                    Data[data[0]][i]['lat'] = data[5]
                    Data[data[0]][i]['lon'] = data[6]
                    Data[data[0]][i]['volume'] = data[7]
            else:
                print(
                    "no specimen %s found in magnetometer data file when reading specimen orientation data file, or data file record for specimen too short"
                    % data[0])
        dinfile.close()

    #--------------------------------------
    # Convert to MagIC
    #--------------------------------------

    specimens_list = list(Data.keys())
    specimens_list.sort()

    MeasRecs, SpecRecs, SampRecs, SiteRecs, LocRecs = [], [], [], [], []
    for specimen in specimens_list:
        for i in range(len(Data[specimen])):
            this_line_data = Data[specimen][i]
            methcode = ""
            MeasRec, SpecRec, SampRec, SiteRec, LocRec = {}, {}, {}, {}, {}
            specimen = this_line_data['specimen']
            if specnum != 0:
                sample = this_line_data['specimen'][:specnum]
            else:
                sample = this_line_data['specimen']
            site = pmag.parse_site(sample, samp_con, Z)
            if not location:
                location = site
            if specimen != "" and specimen not in [
                    x['specimen'] if 'specimen' in list(x.keys()) else ""
                    for x in SpecRecs
            ]:
                SpecRec['specimen'] = specimen
                SpecRec['sample'] = sample
                SpecRecs.append(SpecRec)
            if sample != "" and sample not in [
                    x['sample'] if 'sample' in list(x.keys()) else ""
                    for x in SampRecs
            ]:
                SampRec['sample'] = sample
                SampRec['site'] = site
                SampRec['azimuth'] = this_line_data['azimuth']
                SampRec['dip'] = this_line_data['dip']
                SampRec['bed_dip_direction'] = this_line_data[
                    'bed_dip_direction']
                SampRec['bed_dip'] = this_line_data['bed_dip']
                SampRecs.append(SampRec)
            if site != "" and site not in [
                    x['site'] if 'site' in list(x.keys()) else ""
                    for x in SiteRecs
            ]:
                SiteRec['site'] = site
                SiteRec['location'] = location
                SiteRec['lat'] = this_line_data['lat']
                SiteRec['lon'] = this_line_data['lon']
                SiteRecs.append(SiteRec)
            if location != "" and location not in [
                    x['location'] if 'location' in list(x.keys()) else ""
                    for x in LocRecs
            ]:
                LocRec['location'] = location
                LocRec['lat_n'] = this_line_data['lat']
                LocRec['lon_e'] = this_line_data['lon']
                LocRec['lat_s'] = this_line_data['lat']
                LocRec['lon_w'] = this_line_data['lon']
                LocRecs.append(LocRec)

            MeasRec['specimen'] = specimen
            MeasRec["meas_temp"] = '%8.3e' % (273)  # room temp in kelvin
            MeasRec["magn_moment"] = '%10.3e' % (
                float(this_line_data['moment_emu']) * 1e-3
            )  # moment in Am^2 (from emu)
            MeasRec["dir_dec"] = this_line_data['dec_core']
            MeasRec["dir_inc"] = this_line_data['inc_core']

            date = this_line_data['date']
            hour = this_line_data['hour']
            if len(date[2]) < 4 and float(date[2]) >= 70:
                yyyy = "19" + date[2]
            elif len(date[2]) < 4 and float(date[2]) < 70:
                yyyy = "20" + date[2]
            else:
                yyyy = date[2]
            if len(date[0]) == 1:
                date[0] = "0" + date[0]
            if len(date[1]) == 1:
                date[1] = "0" + date[1]
            dt = ":".join([date[0], date[1], yyyy, hour[0], hour[1], "0"])
            local = pytz.timezone("America/New_York")
            naive = datetime.datetime.strptime(dt, "%m:%d:%Y:%H:%M:%S")
            local_dt = local.localize(naive, is_dst=None)
            utc_dt = local_dt.astimezone(pytz.utc)
            timestamp = utc_dt.strftime("%Y-%m-%dT%H:%M:%S") + "Z"

            MeasRec['analysts'] = user
            MeasRec["citations"] = "This study"
            MeasRec["instrument_codes"] = "HUJI-2G"
            MeasRec["quality"] = "g"
            MeasRec["meas_n_orient"] = "1"
            MeasRec["standard"] = "u"
            MeasRec["description"] = ""

            #----------------------------------------
            # AF demag
            # do not support AARM yet
            #----------------------------------------

            if demag == "AF":
                treatment_type = this_line_data['treatment_type']
                # demag in zero field
                if LPcode != "LP-AN-ARM":
                    MeasRec["treat_ac_field"] = '%8.3e' % (
                        float(this_line_data['treatment'][0]) * 1e-3
                    )  # peak field in tesla
                    MeasRec["treat_dc_field"] = '0'
                    MeasRec["treat_dc_field_phi"] = '0'
                    MeasRec["treat_dc_field_theta"] = '0'
                    if treatment_type == "N":
                        methcode = "LP-DIR-AF:LT-NO"
                    elif treatment_type == "A":
                        methcode = "LP-DIR-AF:LT-AF-Z"
                    else:
                        print(
                            "ERROR in treatment field line %i... exiting until you fix the problem"
                            % line_no)
                        print(this_line_data)
                        return False, "ERROR in treatment field line %i... exiting until you fix the problem" % line_no
                # AARM experiment
                else:
                    print(
                        "Dont supprot AARM in HUJI format yet. sorry... do be DONE"
                    )
                MeasRec["method_codes"] = methcode
                MeasRec["experiments"] = specimen + ":" + LPcode
                MeasRec["treat_step_num"] = "%i" % i
                MeasRec["description"] = ""

                MeasRecs.append(MeasRec)

            #----------------------------------------
            # Thermal:
            # Thellier experiment: "IZ", "ZI", "IZZI", pTRM checks
            # Thermal demag
            # Thermal cooling rate experiment
            # Thermal NLT
            #----------------------------------------

            if demag == "T":

                treatment = this_line_data['treatment']
                treatment_type = this_line_data['treatment_type']

                #----------------------------------------
                # Thellier experimet
                #----------------------------------------

                if LPcode == "LP-PI-TRM":  # Thelllier experiment
                    MeasRec["experiments"] = specimen + ":" + LPcode
                    methcode = LPcode
                    if treatment_type == "N" or (
                        (treatment[1] == '0' or treatment[1] == '00')
                            and float(treatment[0]) == 0):
                        LT_code = "LT-NO"
                        MeasRec["treat_dc_field_phi"] = '0'
                        MeasRec["treat_dc_field_theta"] = '0'
                        MeasRec["treat_dc_field"] = '0'
                        MeasRec["treat_temp"] = '273.'

                    elif treatment[1] == '0' or treatment[1] == '00':
                        LT_code = "LT-T-Z"
                        MeasRec["treat_dc_field_phi"] = '0'
                        MeasRec["treat_dc_field_theta"] = '0'
                        MeasRec["treat_dc_field"] = '%8.3e' % (0)
                        MeasRec["treat_temp"] = '%8.3e' % (
                            float(treatment[0]) + 273.)  # temp in kelvin

                        # check if this is ZI or IZ:
                        #  check if the same temperature already measured:
                        methcode = "LP-PI-TRM:LP-PI-TRM-ZI"
                        for j in range(0, i):
                            if Data[specimen][j]['treatment'][0] == treatment[
                                    0]:
                                if Data[specimen][j]['treatment'][
                                        1] == '1' or Data[specimen][j][
                                            'treatment'][1] == '10':
                                    methcode = "LP-PI-TRM:LP-PI-TRM-IZ"
                                else:
                                    methcode = "LP-PI-TRM:LP-PI-TRM-ZI"

                    elif treatment[1] == '1' or treatment[1] == '10':
                        LT_code = "LT-T-I"
                        MeasRec["treat_dc_field"] = '%8.3e' % (
                            labfield
                        )  # labfield in tesla (convert from microT)
                        MeasRec["treat_dc_field_phi"] = '%7.1f' % (
                            phi)  # labfield phi
                        MeasRec["treat_dc_field_theta"] = '%7.1f' % (
                            theta)  # labfield theta
                        MeasRec["treat_temp"] = '%8.3e' % (
                            float(treatment[0]) + 273.)  # temp in kelvin

                        # check if this is ZI or IZ:
                        #  check if the same temperature already measured:
                        methcode = "LP-PI-TRM:LP-PI-TRM-IZ"
                        for j in range(0, i):
                            if Data[specimen][j]['treatment'][0] == treatment[
                                    0]:
                                if Data[specimen][j]['treatment'][
                                        1] == '0' or Data[specimen][j][
                                            'treatment'][1] == '00':
                                    methcode = "LP-PI-TRM:LP-PI-TRM-ZI"
                                else:
                                    methcode = "LP-PI-TRM:LP-PI-TRM-IZ"
                    elif treatment[1] == '2' or treatment[1] == '20':
                        LT_code = "LT-PTRM-I"
                        MeasRec["treat_dc_field"] = '%8.3e' % (
                            labfield
                        )  # labfield in tesla (convert from microT)
                        MeasRec["treat_dc_field_phi"] = '%7.1f' % (
                            phi)  # labfield phi
                        MeasRec["treat_dc_field_theta"] = '%7.1f' % (
                            theta)  # labfield theta
                        MeasRec["treat_temp"] = '%8.3e' % (
                            float(treatment[0]) + 273.)  # temp in kelvin
                        methcode = "LP-PI-TRM:LP-PI-TRM-IZ"

                    else:
                        print(
                            "ERROR in treatment field line %i... exiting until you fix the problem"
                            % line_no)
                        return False, "ERROR in treatment field line %i... exiting until you fix the problem" % line_no
                    MeasRec["method_codes"] = LT_code + ":" + methcode
                    MeasRec["treat_step_num"] = "%i" % i
                    MeasRec["description"] = ""
                    MeasRecs.append(MeasRec)

                #----------------------------------------
                # demag experimet
                #----------------------------------------

                if LPcode == "LP-DIR-T":
                    MeasRec["experiments"] = specimen + ":" + LPcode
                    methcode = LPcode
                    if treatment_type == "N":
                        LT_code = "LT-NO"
                    else:
                        LT_code = "LT-T-Z"
                        methcode = LPcode + ":" + "LT-T-Z"
                    MeasRec["treat_dc_field_phi"] = '0'
                    MeasRec["treat_dc_field_theta"] = '0'
                    MeasRec["treat_dc_field"] = '%8.3e' % (0)
                    MeasRec["treat_temp"] = '%8.3e' % (
                        float(treatment[0]) + 273.)  # temp in kelvin
                    MeasRec["method_codes"] = LT_code + ":" + methcode
                    MeasRec["treat_step_num"] = "%i" % i
                    MeasRec["description"] = ""
                    MeasRecs.append(MeasRec)
                    #continue

                #----------------------------------------
                # ATRM measurements
                # The direction of the magnetization is used to determine the
                # direction of the lab field.
                #----------------------------------------

                if LPcode == "LP-AN-TRM":
                    MeasRec["experiments"] = specimen + ":" + LPcode
                    methcode = LPcode

                    if float(treatment[1]) == 0:
                        MeasRec["method_codes"] = "LP-AN-TRM:LT-T-Z"
                        MeasRec["treat_dc_field_phi"] = '0'
                        MeasRec["treat_dc_field_theta"] = '0'
                        MeasRec["treat_temp"] = '%8.3e' % (
                            float(treatment[0]) + 273.)  # temp in kelvin
                        MeasRec["treat_dc_field"] = '0'
                    else:
                        if float(treatment[1]) == 7:
                            # alteration check
                            methcode = "LP-AN-TRM:LT-PTRM-I"
                            MeasRec["treat_step_num"] = '7'  # -z
                        else:
                            MeasRec["method_codes"] = "LP-AN-TRM:LT-T-I"
                            inc = float(MeasRec["dir_inc"])
                            dec = float(MeasRec["dir_dec"])
                            if abs(inc) < 45 and (dec < 45 or dec > 315):  # +x
                                tdec, tinc = 0, 0
                                MeasRec["treat_step_num"] = '1'
                            if abs(inc) < 45 and (dec < 135 and dec > 45):
                                tdec, tinc = 90, 0
                                MeasRec["treat_step_num"] = '2'  # +y
                            if inc > 45:
                                tdec, tinc = 0, 90
                                MeasRec["treat_step_num"] = '3'  # +z
                            if abs(inc) < 45 and (dec < 225 and dec > 135):
                                tdec, tinc = 180, 0
                                MeasRec["treat_step_num"] = '4'  # -x
                            if abs(inc) < 45 and (dec < 315 and dec > 225):
                                tdec, tinc = 270, 0
                                MeasRec["treat_step_num"] = '5'  # -y
                            if inc < -45:
                                tdec, tinc = 0, -90
                                MeasRec["treat_step_num"] = '6'  # -z

                        MeasRec["treat_dc_field_phi"] = '%7.1f' % (tdec)
                        MeasRec["treat_dc_field_theta"] = '%7.1f' % (tinc)
                        MeasRec["treat_temp"] = '%8.3e' % (
                            float(treatment[0]) + 273.)  # temp in kelvin
                        MeasRec["treat_dc_field"] = '%8.3e' % (labfield)
                    MeasRec["description"] = ""
                    MeasRecs.append(MeasRec)
                    #continue

                #----------------------------------------
                # NLT measurements
                # or TRM acquisistion experiment
                #----------------------------------------

                if LPcode == "LP-TRM":
                    MeasRec["experiments"] = specimen + ":" + LPcode
                    MeasRec["method_codes"] = "LP-TRM:LT-T-I"
                    if float(treatment[1]) == 0:
                        labfield = 0
                    else:
                        labfield = float(float(treatment[1])) * 1e-6
                    MeasRec["treat_temp"] = '%8.3e' % (
                        float(treatment[0]) + 273.)  # temp in kelvin
                    MeasRec["treat_dc_field"] = '%8.3e' % (
                        labfield)  # labfield in tesla (convert from microT)
                    MeasRec["treat_dc_field_phi"] = '%7.1f' % (
                        phi)  # labfield phi
                    MeasRec["treat_dc_field_theta"] = '%7.1f' % (
                        theta)  # labfield theta
                    MeasRec["treat_step_num"] = "%i" % i
                    MeasRec["description"] = ""
                    MeasRecs.append(MeasRec)
                    #continue

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

                if LPcode == "LP-CR-TRM":
                    index = int(treatment[1][0])
                    #print index,"index"
                    #print CR_cooling_times,"CR_cooling_times"
                    #print CR_cooling_times[index-1]
                    #print CR_cooling_times[0:index-1]
                    if index == 7 or index == 70:  # alteration check as final measurement
                        meas_type = "LT-PTRM-I:LP-CR-TRM"
                        CR_cooling_time = CR_cooling_times[-1]
                    else:
                        meas_type = "LT-T-I:LP-CR-TRM"
                        CR_cooling_time = CR_cooling_times[index - 1]
                    MeasRec["method_codes"] = meas_type
                    MeasRec["experiments"] = specimen + ":" + LPcode
                    MeasRec["treat_temp"] = '%8.3e' % (
                        float(treatment[0]) + 273.)  # temp in kelvin
                    MeasRec["treat_dc_field"] = '%8.3e' % (
                        labfield)  # labfield in tesla (convert from microT)
                    MeasRec["treat_dc_field_phi"] = '%7.1f' % (
                        phi)  # labfield phi
                    MeasRec["treat_dc_field_theta"] = '%7.1f' % (
                        theta)  # labfield theta
                    MeasRec["treat_step_num"] = "%i" % index
                    MeasRec[
                        "description"] = "cooling_rate" + ":" + CR_cooling_time + ":" + "K/min"
                    #MeasRec["description"]="%.1f minutes per cooling time"%int(CR_cooling_time)
                    MeasRecs.append(MeasRec)
                    #continue

    con = nb.Contribution(output_dir_path, read_tables=[])

    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts = pmag.measurements_methods3(MeasRecs, noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    con.tables['specimens'].write_magic_file(custom_name=spec_file)
    con.tables['samples'].write_magic_file(custom_name=samp_file)
    con.tables['sites'].write_magic_file(custom_name=site_file)
    con.tables['locations'].write_magic_file(custom_name=loc_file)
    con.tables['measurements'].write_magic_file(custom_name=meas_file)

    return True, meas_file
示例#21
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
示例#22
0
def convert(**kwargs):
    # initialize some stuff
    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]
    demag = "N"
    trm = 0
    irm = 0

    user = kwargs.get('user', '')
    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', 'measurements.txt')
    spec_file = kwargs.get('spec_file', 'specimens.txt')  # specimen outfile
    samp_file = kwargs.get('samp_file', 'samples.txt')
    site_file = kwargs.get('site_file', 'sites.txt')  # site outfile
    loc_file = kwargs.get('loc_file', 'locations.txt')  # site outfile
    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))
    location = kwargs.get('location', 'unknown')
    noave = kwargs.get('noave', False)  # 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)
    mv = kwargs.get('mv', 'v')

    # format/organize variables
    if magfile:
        try:
            infile = open(os.path.join(input_dir_path, magfile), 'r')
        except IOError:
            print("bad mag file name")
            return False, "bad mag file name"
    else:
        print("mag_file field is required option")
        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"
    elif "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"
    else:
        Z = 1

    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('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
    MeasRecs, SpecRecs, SampRecs, SiteRecs, LocRecs = [], [], [], [], []
    version_num = pmag.get_version()
    # find start of data:
    DIspec = []
    Data = infile.readlines()
    infile.close()
    for k in range(len(Data)):
        rec = Data[k].split()
        if len(rec) <= 2: continue
        if rec[0].upper() == "LAT:" and len(rec) > 3:
            lat, lon = rec[1], rec[3]
            continue
        elif rec[0].upper() == "ID":
            continue
        MeasRec, SpecRec, SampRec, SiteRec, LocRec = {}, {}, {}, {}, {}
        specimen = rec[0]
        if specnum != 0:
            sample = specimen[:specnum]
        else:
            sample = specimen
        site = pmag.parse_site(sample, samp_con, Z)
        if mv == 'v':
            volume = float(rec[12])
            if volume > 0:
                susc_chi_volume = '%10.3e' % (old_div(
                    (float(rec[11]) * 1e-5),
                    volume))  #convert to SI (assume Bartington, 10-5 SI)
            else:
                susc_chi_volume = '%10.3e' % (
                    float(rec[11]) * 1e-5
                )  #convert to SI (assume Bartington, 10-5 SI)
        else:
            mass = float(rec[12])
            if mass > 0:
                susc_chi_mass = '%10.3e' % (old_div(
                    (float(rec[11]) * 1e-5),
                    mass))  #convert to SI (assume Bartington, 10-5 SI)
            else:
                susc_chi_mass = '%10.3e' % (
                    float(rec[11]) * 1e-5
                )  #convert to SI (assume Bartington, 10-5 SI)
        print((specimen, sample, site, samp_con, Z))

        #fill tables besides measurements
        if specimen != "" and specimen not in [
                x['specimen'] if 'specimen' in list(x.keys()) else ""
                for x in SpecRecs
        ]:
            SpecRec['specimen'] = specimen
            SpecRec['sample'] = sample
            if mv == 'v':
                SpecRec["susc_chi_volume"] = susc_chi_volume
                SpecRec["volume"] = volume
            else:
                SpecRec["susc_chi_mass"] = susc_chi_mass
                SpecRec["mass"] = mass
            SpecRecs.append(SpecRec)
        if sample != "" and sample not in [
                x['sample'] if 'sample' in list(x.keys()) else ""
                for x in SampRecs
        ]:
            SampRec['sample'] = sample
            SampRec['site'] = site
            SampRecs.append(SampRec)
        if site != "" and site not in [
                x['site'] if 'site' in list(x.keys()) else "" for x in SiteRecs
        ]:
            SiteRec['site'] = site
            SiteRec['location'] = location
            SiteRec['lat'] = lat
            SiteRec['lon'] = lon
            SiteRecs.append(SiteRec)
        if location != "" and location not in [
                x['location'] if 'location' in list(x.keys()) else ""
                for x in LocRecs
        ]:
            LocRec['location'] = location
            LocRec['lat_n'] = lat
            LocRec['lon_e'] = lon
            LocRec['lat_s'] = lat
            LocRec['lon_w'] = lon
            LocRecs.append(LocRec)

        #fill measurements
        MeasRec["treat_temp"] = '%8.3e' % (273)  # room temp in kelvin
        MeasRec["meas_temp"] = '%8.3e' % (273)  # room temp in kelvin
        MeasRec["treat_ac_field"] = '0'
        MeasRec["treat_dc_field"] = '0'
        MeasRec["treat_dc_field_phi"] = '0'
        MeasRec["treat_dc_field_theta"] = '0'
        meas_type = "LT-NO"
        MeasRec["quality"] = 'g'
        MeasRec["standard"] = 'u'
        MeasRec["treat_step_num"] = '1'
        MeasRec["specimen"] = specimen
        #        if mv=='v': MeasRec["susc_chi_volume"]=susc_chi_volume
        #        else: MeasRec["susc_chi_mass"]=susc_chi_mass
        MeasRec["dir_csd"] = rec[3]
        MeasRec["magn_moment"] = '%10.3e' % (float(rec[4]) * 1e-7)
        MeasRec["dir_dec"] = rec[5]
        MeasRec["dir_inc"] = rec[6]
        MeasRec["citations"] = "This study"
        if demag == "AF":
            if methcode != "LP-AN-ARM":
                MeasRec["treat_ac_field"] = '%8.3e' % (float(rec[1]) * 1e-3
                                                       )  # peak field in tesla
                meas_type = "LT-AF-Z"
                MeasRec["treat_dc_field"] = '0'
            else:  # AARM experiment
                if treat[1][0] == '0':
                    meas_type = "LT-AF-Z"
                    MeasRec["treat_ac_field"] = '%8.3e' % (
                        peakfield)  # peak field in tesla
                else:
                    meas_type = "LT-AF-I"
                    ipos = int(treat[0]) - 1
                    MeasRec["treat_dc_field_phi"] = '%7.1f' % (dec[ipos])
                    MeasRec["treat_dc_field_theta"] = '%7.1f' % (inc[ipos])
                    MeasRec["treat_dc_field"] = '%8.3e' % (labfield)
                    MeasRec["treat_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')
            MeasRec["treat_temp"] = '%8.3e' % (float(rec[1]) + 273.
                                               )  # temp in kelvin
            meas_type = "LT-T-Z"
            MeasRec["treat_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:
                    MeasRec["treat_dc_field"] = '%8.3e' % (
                        labfield)  # labfield in tesla (convert from microT)
                    MeasRec["treat_dc_field_phi"] = '%7.1f' % (
                        phi)  # labfield phi
                    MeasRec["treat_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':
                        MeasRec[
                            "treat_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
        MeasRec['method_codes'] = meas_type
        MeasRecs.append(MeasRec)

    con = nb.Contribution(output_dir_path, read_tables=[])

    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts = pmag.measurements_methods3(MeasRecs, noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    con.tables['specimens'].write_magic_file(custom_name=spec_file)
    con.tables['samples'].write_magic_file(custom_name=samp_file)
    con.tables['sites'].write_magic_file(custom_name=site_file)
    con.tables['locations'].write_magic_file(custom_name=loc_file)
    con.tables['measurements'].write_magic_file(custom_name=meas_file)

    return True, meas_file
示例#23
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
示例#24
0
def main():
    """
    NAME
        mst_magic.py
 
    DESCRIPTION
        converts MsT data (T,M) to magic_measurements format files

    SYNTAX
        mst_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify T,M  format input file, required
        -fsa SFILE: name with sample, site, location information
        -F FILE: specify output file, default is MsT_measurements.txt
        -dc H: specify applied field during measurement, default is 0.5 T
        -syn  : This is a synthetic specimen and has no sample/site/location information
        -spn SPEC: specimen name 
        -spc NUM : specify number of characters to designate a  specimen, default = 0
        -loc LOCNAME : specify location/study name, must have either LOCNAME or SAMPFILE or be a synthetic
        -ncn NCON:  specify naming convention: default is #1 below
       Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name same as sample
            [6] site is entered under a separate column -- 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 files: 
            T M:  T is in Centigrade and M is uncalibrated magnitude

    """
    # initialize some stuff
    samp_con, Z = "1", "0"
    dir_path = '.'
    citation = 'This study'
    args = sys.argv
    specnum, measnum = 0, 1
    #
    # get command line arguments
    #
    user = ""
    if '-WD' in args:
        ind = args.index("-WD")
        dir_path = args[ind + 1]
    meas_file = dir_path + "/MsT_measurements.txt"
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-usr" in args:
        ind = args.index("-usr")
        user = args[ind + 1]
    labfield = '0.5'
    if "-dc" in args:
        ind = args.index("-dc")
        labfield = args[ind + 1]
    if '-F' in args:
        ind = args.index("-F")
        meas_file = dir_path + '/' + args[ind + 1]
    if "-fsa" in args:
        ind = args.index("-fsa")
        samp_file = dir_path + '/' + args[ind + 1]
        Samps, file_type = pmag.magic_read(samp_file)
    if '-f' in args:
        ind = args.index("-f")
        infile = dir_path + '/' + args[ind + 1]
        try:
            input = open(infile, 'rU')
        except:
            print "bad mag file name"
            sys.exit()
    else:
        print main.__doc__
        print "-f  is required option"
        sys.exit()
    if "-spc" in args:
        ind = args.index("-spc")
        specnum = int(args[ind + 1])
        if specnum != 0: specnum = -specnum
    er_location_name, syn, specimen_name = 'unknown', 0, 'unknown'
    if "-loc" in args:
        ind = args.index("-loc")
        er_location_name = args[ind + 1]
    if "-spn" in args:
        ind = args.index("-spn")
        specimen_name = args[ind + 1]
    else:
        print main.__doc__
        print "-spn  is required option"
        sys.exit()
    if "-syn" in args: syn = 1
    if "-ncn" in args:
        ind = args.index("-ncn")
        samp_con = sys.argv[ind + 1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print "option [4] must be in form 4-Z where Z is an integer"
                sys.exit()
            else:
                Z = samp_con.split("-")[1]
                samp_con = "4"
            samp_con = sys.argv[ind + 1]
        if "7" in samp_con:
            if "-" not in samp_con:
                print "option [7] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z = samp_con.split("-")[1]
                samp_con = "7"
    MagRecs, specs = [], []
    version_num = pmag.get_version()
    data = input.readlines()
    T0 = float(data[0].split()[0])
    for line in data:
        instcode = ""
        if len(line) > 1:
            MagRec = {}
            if syn == 0: MagRec['er_location_name'] = er_location_name
            MagRec['magic_software_packages'] = version_num
            MagRec["treatment_dc_field"] = labfield
            rec = line.split()
            T = float(rec[0])
            MagRec["measurment_temp"] = '%8.3e' % (float(rec[0]) + 273.
                                                   )  # temp in kelvin
            if T > T0:
                MagRec["magic_method_codes"] = 'LP-MW-I'
            elif T < T0:
                MagRec["magic_method_codes"] = 'LP-MC-I'
                T0 = T
            else:
                print 'skipping repeated temperature step'
                MagRec["magic_method_codes"] = ''
            T0 = T
            MagRec["measurement_magnitude"] = '%10.3e' % (float(
                rec[1]))  # uncalibrated magnitude
            if syn == 0:
                MagRec["er_specimen_name"] = specimen_name
                MagRec["er_site_name"] = ""
                if specnum != 0:
                    MagRec["er_sample_name"] = specimen_name[:specnum]
                else:
                    MagRec["er_sample_name"] = specimen_name
                if "-fsa" in args:
                    for samp in Samps:
                        if samp["er_sample_name"] == MagRec["er_sample_name"]:
                            MagRec["er_location_name"] = samp[
                                "er_location_name"]
                            MagRec["er_site_name"] = samp["er_site_name"]
                            break
                elif int(samp_con) != 6:
                    site = pmag.parse_site(MagRec['er_sample_name'], samp_con,
                                           Z)
                    MagRec["er_site_name"] = site
                if MagRec['er_site_name'] == "":
                    print 'No site name found for: ', MagRec[
                        'er_specimen_name'], MagRec['er_sample_name']
                if MagRec["er_location_name"] == "":
                    print 'no location name for: ', MagRec["er_specimen_name"]
            else:
                MagRec["er_synthetic_name"] = specimen_name
                MagRec["er_location_name"] = ""
                MagRec["er_sample_name"] = ""
                MagRec["er_site_name"] = ""
                MagRec["er_specimen_name"] = ""
            MagRec["magic_instrument_codes"] = instcode
            MagRec["er_analyst_mail_names"] = user
            MagRec["er_citation_names"] = citation
            MagRec["measurement_flag"] = 'g'
            MagRec["measurement_number"] = str(measnum)
            measnum += 1
            MagRecs.append(MagRec)
    for rec in MagRecs:  # sort out the measurements by experiment type
        rec['magic_experiment_name'] = specimen_name
        if rec['magic_method_codes'] == 'LP-MW-I':
            rec["magic_experiment_name"] = specimen_name + ':LP-MW-I:Curie'
        elif rec['magic_method_codes'] == 'LP-MC-I':
            rec["magic_experiment_name"] = specimen_name + ':LP-MC-I'
    pmag.magic_write(meas_file, MagRecs, 'magic_measurements')
    print "results put in ", meas_file
示例#25
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
示例#26
0
def convert(**kwargs):

    # initialize some stuff
    version_num = pmag.get_version()
    MeasRecs,SpecRecs,SampRecs,SiteRecs,LocRecs = [],[],[],[],[]

    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', 'measurements.txt')
    mag_file = kwargs.get('mag_file')
    spec_file = kwargs.get('spec_file', 'specimens.txt') # specimen outfile
    samp_file = kwargs.get('samp_file', 'samples.txt')
    site_file = kwargs.get('site_file', 'sites.txt') # site outfile
    loc_file = kwargs.get('loc_file', 'locations.txt') # site outfile
    location = kwargs.get('location', 'unknown')
    dmy_flag = kwargs.get('dmy_flag', False)
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')
    #oave = kwargs.get('noave', 0) # default (0) means DO average
    meth_code = kwargs.get('meth_code', "LP-NO")
    specnum = -int(kwargs.get('specnum', 0))
    samp_con = kwargs.get('samp_con', '2')
    if "4" in samp_con:
        if "-" not in samp_con:
            print("option [4] must be in form 4-Z where Z is an integer")
            return False, "naming convention option [4] must be in form 4-Z where Z is an integer"
        else:
            site_num=samp_con.split("-")[1]
            samp_con="4"
    elif "7" in samp_con:
        if "-" not in samp_con:
            print("option [7] must be in form 7-Z where Z is an integer")
            return False, "naming convention option [7] must be in form 7-Z where Z is an integer"
        else:
            site_num=samp_con.split("-")[1]
            samp_con="7"
    else: site_num=1
    try:
        DC_FIELD = float(kwargs.get('labfield',0))*1e-6
        DC_PHI = float(kwargs.get('phi',0))
        DC_THETA = float(kwargs.get('theta',0))
    except ValueError: raise ValueError('problem with your dc parameters. please provide a labfield in microTesla and a phi and theta in degrees.')
    noave = kwargs.get('noave', False)
    dmy_flag = kwargs.get('dmy_flag', False)
    meas_n_orient = kwargs.get('meas_n_orient', '8')

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

    # parse data

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

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

        # Configure et er_ tables
        if specnum==0: sample_name = spec_name
        else: sample_name = spec_name[:specnum]
        site = pmag.parse_site(sample_name,samp_con,site_num)
        SpecRec['specimen'] = spec_name
        SpecRec['sample'] = sample_name
        if volume!=0: SpecRec['volume']=volume
        SpecRecs.append(SpecRec)
        if sample_name!="" and sample_name not in [x['sample'] if 'sample' in list(x.keys()) else "" for x in SampRecs]:
            SampRec['sample'] = sample_name
            SampRec['azimuth'] = dec
            SampRec['dip'] = str(float(inc)-90)
            SampRec['bed_dip_direction'] = bed_plane
            SampRec['bed_dip'] = bed_dip
            SampRec['method_codes'] = meth_code
            SampRec['site'] = site
            SampRecs.append(SampRec)
        if site!="" and site not in [x['site'] if 'site' in list(x.keys()) else "" for x in SiteRecs]:
            SiteRec['site'] = site
            SiteRec['location'] = location
            SiteRec['lat'] = lat
            SiteRec['lon'] = lon
            SiteRecs.append(SiteRec)
        if location!="" and location not in [x['location'] if 'location' in list(x.keys()) else "" for x in LocRecs]:
            LocRec['location']=location
            LocRec['lat_n'] = lat
            LocRec['lon_e'] = lon
            LocRec['lat_s'] = lat
            LocRec['lon_w'] = lon
            LocRecs.append(LocRec)

        #measurement data
        line = data.readline()
        line = line.rstrip("\n")
        items = line.split(",")
        while line != '9999':
            step=items[0]
            step=step.split('.')
            step_value=step[0]
            step_type = ""
            if len(step) == 2:
                step_type=step[1]
            if step_type=='5':
                step_value = items[0]
            A=float(items[1])
            B=float(items[2])
            C=float(items[3])
#  convert to MagIC coordinates
            Z=-A
            X=-B
            Y=C
            cart = array([X, Y, Z]).transpose()
            direction = pmag.cart2dir(cart).transpose()
            measurement_dec = direction[0]
            measurement_inc = direction[1]
            magn_moment = direction[2] * 1.0e-12 # the data are in pico-Am^2 - this converts to Am^2
            if volume!=0:
                magn_volume = direction[2] * 1.0e-12 / volume # data volume normalized - converted to A/m
#            print("magn_moment=", magn_moment)
#            print("magn_volume=", magn_volume)
            error = items[4]
            date=items[5]
            date=date.strip('"').replace(' ','')
            if date.count("-") > 0:
                date=date.split("-")
            elif date.count("/") > 0:
                date=date.split("/")
            else: print("date format seperator cannot be identified")
#            print(date)
            time=items[6]
            time=time.strip('"').replace(' ','')
            time=time.split(":")
#            print(time)
            dt = date[0] + ":" + date[1] + ":" + date[2] + ":" + time[0] + ":" + time[1] + ":" + "0"
            local = pytz.timezone("Europe/Amsterdam")
            try:
                if dmy_flag: naive = datetime.datetime.strptime(dt, "%d:%m:%Y:%H:%M:%S")
                else: naive = datetime.datetime.strptime(dt, "%m:%d:%Y:%H:%M:%S")
            except ValueError:
                naive = datetime.datetime.strptime(dt, "%Y:%m:%d:%H:%M:%S")
            local_dt = local.localize(naive, is_dst=None)
            utc_dt = local_dt.astimezone(pytz.utc)
            timestamp=utc_dt.strftime("%Y-%m-%dT%H:%M:%S")+"Z"
#            print(timestamp)

            MeasRec = {}
            MeasRec["timestamp"]=timestamp
            MeasRec["analysts"] = operator
            MeasRec["instrument_codes"] = "Utrecht_" + machine
            MeasRec["description"] = "free string = " + free_string
            MeasRec["citations"] = "This study"
            MeasRec['software_packages'] = version_num
            MeasRec["meas_temp"] = '%8.3e' % (273) # room temp in kelvin
            MeasRec["quality"] = 'g'
            MeasRec["standard"] = 'u'
            MeasRec["experiments"] = location + site + spec_name
            MeasRec["treat_step_num"] = location + site + spec_name + items[0]
            MeasRec["specimen"] = spec_name
            # MeasRec["treat_ac_field"] = '0'
            if AF_or_T.lower() == "th":
                MeasRec["treat_temp"] = '%8.3e' % (float(step_value)+273.) # temp in kelvin
                MeasRec['treat_ac_field']='0'
                lab_treat_type = "T"
            else:
                MeasRec['treat_temp']='273'
                MeasRec['treat_ac_field']='%10.3e'%(float(step_value)*1e-3)
                lab_treat_type = "AF"
            MeasRec['treat_dc_field']='0'
            if step_value == '0':
                meas_type = "LT-NO"
#            print("step_type=", step_type)
            if step_type == '0' or step_type == '00':
                meas_type = "LT-%s-Z"%lab_treat_type
            elif step_type == '1' or step_type == '11':
                meas_type = "LT-%s-I"%lab_treat_type
                MeasRec['treat_dc_field']='%1.2e'%DC_FIELD
            elif step_type == '2' or step_type == '12':
                meas_type = "LT-PTRM-I"
                MeasRec['treat_dc_field']='%1.2e'%DC_FIELD
            elif step_type == '3' or step_type == '13':
                meas_type = "LT-PTRM-Z"
#            print("meas_type=", meas_type)
            MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
            MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
            MeasRec['method_codes'] = meas_type
            MeasRec["magn_moment"] = magn_moment
            if volume!=0: MeasRec["magn_volume"] = magn_volume
            MeasRec["dir_dec"] = measurement_dec
            MeasRec["dir_inc"] = measurement_inc
            MeasRec['dir_csd'] = error
            MeasRec['meas_n_orient'] = meas_n_orient
#            print(MeasRec)
            MeasRecs.append(MeasRec)

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

    data.close()

    con = nb.Contribution(output_dir_path,read_tables=[])

    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts=pmag.measurements_methods3(MeasRecs,noave) #figures out method codes for measuremet data
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    con.tables['specimens'].write_magic_file(custom_name=spec_file)
    con.tables['samples'].write_magic_file(custom_name=samp_file)
    con.tables['sites'].write_magic_file(custom_name=site_file)
    con.tables['locations'].write_magic_file(custom_name=loc_file)
    con.tables['measurements'].write_magic_file(custom_name=meas_file)

    return True, meas_file
示例#27
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
示例#28
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
示例#29
0
def convert(**kwargs):
    #
    # initialize variables
    #
    bed_dip,bed_dip_dir="",""
    sclass,lithology,_type="","",""
    DecCorr=0.
    months=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']

    dir_path = kwargs.get('dir_path', '.')
    mag_file = kwargs.get('mag_file', '')
    meas_file = kwargs.get('meas_file', 'measurements.txt')
    spec_file = kwargs.get('spec_file', 'specimens.txt')
    samp_file = kwargs.get('samp_file', 'samples.txt')
    site_file = kwargs.get('site_file', 'sites.txt')
    loc_file = kwargs.get('loc_file', 'locations.txt')
    or_con = kwargs.get('or_con', '3')
    samp_con = kwargs.get('samp_con', '2')
    corr = kwargs.get('corr', '1')
    gmeths = kwargs.get('gmeths', 'FS-FD:SO-POM')
    location = kwargs.get('location', 'unknown')
    specnum = int(kwargs.get('specnum', 0))
    inst = kwargs.get('inst', '')
    user = kwargs.get('user', '')
    noave = kwargs.get('noave', 0) # default is DO average
    ID = kwargs.get('ID', '')
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')

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

    if ID:
        input_dir_path = ID
    else:
        input_dir_path = dir_path

    if samp_con:
        Z = 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, "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:
            print('Naming convention option [6] not currently supported')
            return False, 'Naming convention option [6] not currently supported'
            #Z=1
            #try:
            #    SampRecs,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"
        #else: Z=1

    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)

    samplist=[]
    try:
        SampRecs,file_type=pmag.magic_read(samp_file)
    except:
        SampRecs=[]
    MeasRecs,SpecRecs,SiteRecs,LocRecs=[],[],[],[]
    try:
        f=open(mag_file,'br')
        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

            methods=gmeths.split(':')
            if deccorr!="0":
                if 'SO-MAG' in methods:del methods[methods.index('SO-MAG')]
                methods.append('SO-CMD-NORTH')
            meths = reduce(lambda x,y: x+':'+y, methods)
            method_codes=meths
            site=pmag.parse_site(sample,samp_con,Z) # parse out the site name
            SpecRec,SampRec,SiteRec,LocRec={},{},{},{}
            SpecRec["specimen"]=specname
            SpecRec["sample"]=sample
            if vcc.strip()!="":vol=float(vcc)*1e-6 # convert to m^3 from cc
            SpecRec["volumne"]='%10.3e'%(vol) #
            SpecRec["geologic_classes"]=sclass
            SpecRec["lithologies"]=lithology
            SpecRec["geologic_types"]=_type
            SpecRecs.append(SpecRec)

            if sample!="" and sample not in [x['sample'] if 'sample' in list(x.keys()) else "" for x in SampRecs]:
                SampRec["sample"]=sample
                SampRec["site"]=site
                labaz,labdip=pmag.orient(az,pl,or_con) # convert to labaz, labpl
                SampRec["bed_dip"]='%7.1f'%(bed_dip)
                SampRec["bed_dip_direction"]='%7.1f'%(bed_dip_dir)
                SampRec["dip"]='%7.1f'%(labdip)
                SampRec["azimuth"]='%7.1f'%(labaz)
                SampRec["azimuth_dec_correction"]='%7.1f'%(deccorr)
                SampRec["geologic_classes"]=sclass
                SampRec["lithologies"]=lithology
                SampRec["geologic_types"]=_type
                SampRec["method_codes"]=method_codes
                SampRecs.append(SampRec)

            if site!="" and site not in [x['site'] if 'site' in list(x.keys()) else "" for x in SiteRecs]:
                SiteRec['site'] = site
                SiteRec['location'] = location
                SiteRec['lat'] = lat
                SiteRec['lon'] = lon
                SiteRec["geologic_classes"]=sclass
                SiteRec["lithologies"]=lithology
                SiteRec["geologic_types"]=_type
                SiteRecs.append(SiteRec)

            if location!="" and location not in [x['location'] if 'location' in list(x.keys()) else "" for x in LocRecs]:
                LocRec['location']=location
                LocRec['lat_n'] = lat
                LocRec['lon_e'] = lon
                LocRec['lat_s'] = lat
                LocRec['lon_w'] = lon
                LocRec["geologic_classes"]=sclass
                LocRec["lithologies"]=lithology
                LocRec["geologic_types"]=_type
                LocRecs.append(LocRec)

        else:
            MeasRec={}
            MeasRec["treat_temp"]='%8.3e' % (273) # room temp in kelvin
            MeasRec["meas_temp"]='%8.3e' % (273) # room temp in kelvin
            MeasRec["treat_ac_field"]='0'
            MeasRec["treat_dc_field"]='0'
            MeasRec["treat_dc_field_phi"]='0'
            MeasRec["treat_dc_field_theta"]='0'
            meas_type="LT-NO"
            MeasRec["quality"]='g'
            MeasRec["standard"]='u'
            MeasRec["treat_step_num"]='1'
            MeasRec["specimen"]=specname
            el,demag=1,''
            treat=rec[el]
            if treat[-1]=='C':
                demag='T'
            elif treat!='NRM':
                demag='AF'
            el+=1
            while rec[el]=="":el+=1
            MeasRec["dir_dec"]=rec[el]
            cdec=float(rec[el])
            el+=1
            while rec[el]=="":el+=1
            MeasRec["dir_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]
            MeasRec["magn_moment"]='%10.3e'% (float(rec[el])*1e-3) # moment in Am^2 (from emu)
            MeasRec["magn_volume"]='%10.3e'% (float(rec[el])*1e-3/vol) # magnetization in A/m
            el=skip(2,el,rec) # skip to xsig
            MeasRec["magn_x_sigma"]='%10.3e'% (float(rec[el])*1e-3) # convert from emu
            el=skip(3,el,rec) # skip to ysig
            MeasRec["magn_y_sigma"]='%10.3e'% (float(rec[el])*1e-3) # convert from emu
            el=skip(3,el,rec) # skip to zsig
            MeasRec["magn_z_sigma"]='%10.3e'% (float(rec[el])*1e-3) # convert from emu
            el+=1 # skip to positions
            MeasRec["meas_n_orient"]=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]
#                    MeasRec['measurement_date']=dstring
            MeasRec["instrument_codes"]=inst
            MeasRec["analysts"]=user
            MeasRec["citations"]="This study"
            MeasRec["method_codes"]=meas_type
            if demag=="AF":
                MeasRec["treat_ac_field"]='%8.3e' %(float(treat[:-2])*1e-3) # peak field in tesla
                meas_type="LT-AF-Z"
                MeasRec["treat_dc_field"]='0'
            elif demag=="T":
                MeasRec["treat_temp"]='%8.3e' % (float(treat[:-1])+273.) # temp in kelvin
                meas_type="LT-T-Z"
            MeasRec['method_codes']=meas_type
            MeasRecs.append(MeasRec)

    con = nb.Contribution(output_dir_path,read_tables=[])

    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts=pmag.measurements_methods3(MeasRecs,noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    con.write_table_to_file('specimens', custom_name=spec_file)
    con.write_table_to_file('samples', custom_name=samp_file)
    con.write_table_to_file('sites', custom_name=site_file)
    con.write_table_to_file('locations', custom_name=loc_file)
    con.write_table_to_file('measurements', custom_name=meas_file)
    return True, meas_file
示例#30
0
def convert(**kwargs):
    """
    Converts CIT formated Magnetometer data into MagIC format for Analysis and contribution to the MagIC database

    Parameters
    -----------
    dir_path : directory to output files to (default : current directory)
    user : colon delimited list of analysts (default : "")
    magfile : magnetometer file (.sam) to convert to MagIC (required)
    meas_file : measurement file name to output (default : measurements.txt)
    spec_file : specimen file name to output (default : specimens.txt)
    samp_file : sample file name to output (default : samples.txt)
    site_file : site file name to output (default : site.txt)
    loc_file : location file name to output (default : locations.txt)
    locname : location name
    methods : colon delimited list of sample method codes. full list here (https://www2.earthref.org/MagIC/method-codes) (default : SO-MAG
    specnum : number of terminal characters that identify a specimen
    norm : is volume or mass normalization using cgs or si units (options : cc,m3,g,kg) (default : cc)
    oersted : demag step vales are in Oersted
    noave : average measurement data or not. False is average, True is don't average. (default : False)
    samp_con : sample naming convention options as follows:
        [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 sitename 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
    input_dir_path : if you did not supply a full path with magfile you can put the directory the magfile is in here
    meas_n_orient : Number of different orientations in measurement (default : 8)
    labfield : DC_FIELD in microTesla (default : 0)
    phi : DC_PHI in degrees (default : 0)
    theta : DC_THETA in degrees (default : 0)

    Returns
    -----------
    type - Tuple : (True or False indicating if conversion was sucessful, meas_file name written)
    """
    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
    locname = kwargs.get('locname', 'unknown')
    sitename = kwargs.get('sitename', '')
    methods = kwargs.get('methods', ['SO-MAG'])
    specnum = -int(kwargs.get('specnum', 0))
    norm = kwargs.get('norm', 'cc')
    oersted = kwargs.get('oersted', False) # mT (oe/10) is the default value
    noave = kwargs.get('noave', False)  # False means do average
    samp_con = kwargs.get('samp_con', '3')
    magfile = kwargs.get('magfile', '')
    input_dir_path = kwargs.get('input_dir_path',os.path.split(magfile)[0])
    meas_n_orient = kwargs.get('meas_n_orient','8')
    output_dir_path = dir_path
    try:
        DC_FIELD = float(kwargs.get('labfield',0))*1e-6
        DC_PHI = float(kwargs.get('phi',0))
        DC_THETA = float(kwargs.get('theta',0))
    except ValueError: raise ValueError('problem with your dc parameters. please provide a labfield in microTesla and a phi and theta in degrees.')
    yn = ''
    if DC_FIELD==0 and DC_PHI==0 and DC_THETA==0: GET_DC_PARAMS=True
    else: GET_DC_PARAMS=False
    if locname=='' or locname==None: locname = 'unknown'
    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"
    else: Z=1

    #get file names and open magfile to start reading data
    if input_dir_path=='': input_dir_path='.'
    magfile = os.path.join(input_dir_path, magfile)
    FIRST_GET_DC=True
    try:
        file_input=open(magfile,'r')
    except IOError as ex:
        print(("bad sam file name: ", magfile))
        return False, "bad sam file name"
    File = file_input.readlines()
    file_input.close()
    if len(File) == 1: File = File[0].split('\r'); File = [x+"\r\n" for x in File]

    #define initial variables
    SpecRecs,SampRecs,SiteRecs,LocRecs,MeasRecs=[],[],[],[],[]
    sids,ln,format,citations=[],0,'CIT',"This study"
    formats=['CIT','2G','APP','JRA']

    if File[ln].strip()=='CIT': ln+=1
    LocRec={}
    LocRec["location"]=locname
    LocRec["citations"]=citations
    LocRec['analysts']=user
    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["lat_n"]=site_lat
        LocRec["lon_e"]=site_lon
        LocRec["lat_s"]=site_lat
        LocRec["lon_w"]=site_lon
        LocRecs.append(LocRec)
        Cdec=float(line[2])
        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
        if sitename: site=sitename
        else: site=pmag.parse_site(sample,samp_con,Z)
        SpecRec['specimen']=specimen
        SpecRec['sample']=sample
        SpecRec['citations']=citations
        SpecRec['analysts']=user
        SampRec['sample']=sample
        SampRec['site']=site
        SampRec['citations']=citations
        SampRec['method_codes']=methods
        SampRec['azimuth_dec_correction']='%7.1f'%(Cdec)
        SampRec['analysts']=user
        SiteRec['site']=site
        SiteRec['location']=locname
        SiteRec['citations']=citations
        SiteRec['lat']=site_lat
        SiteRec['lon']=site_lon
        SiteRec['analysts']=user
        f=open(os.path.join(input_dir_path,specimen),'r')
        Lines=f.readlines()
        f.close()
        comment=""
        line=Lines[0].split()
        if len(line)>2:
            comment=line[2]
        info=Lines[1].split()
        volmass=float(info[-1])
        if volmass==1.0:
            print('Warning: Specimen volume set to 1.0.')
            print('Warning: If volume/mass really is 1.0, set volume/mass to 1.001')
            print('Warning: specimen method code LP-NOMAG set.')
            SpecRec['weight']=""
            SpecRec['volume']=""
            SpecRec['method_codes']='LP-NOMAG'
        elif norm=="gm":
            SpecRec['volume']=''
            SpecRec['weight']='%10.3e'%volmass*1e-3
        elif norm=="kg":
            SpecRec['volume']=''
            SpecRec['weight']='%10.3e'*volmass
        elif norm=="cc":
            SpecRec['weight']=""
            SpecRec['volume']='%10.3e'%(volmass*1e-6)
        elif norm=="m3":
            SpecRec['weight']=""
            SpecRec['volume']='%10.3e'%(volmass)
        else:
            print('Warning: Unknown normalization unit ', norm, '. Using default of cc')
            SpecRec['weight']=""
            SpecRec['volume']='%10.3e'%(volmass*1e-6)
        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['height']=info[-6]
        else:
            SampRec['height']='0'
        SampRec['azimuth']='%7.1f'%(sample_azimuth)
        SampRec['dip']='%7.1f'%(sample_dip)
        SampRec['bed_dip']='%7.1f'%(dip)
        SampRec['bed_dip_direction']='%7.1f'%(dip_direction)
        SampRec['geologic_classes']=''
        SampRec['geologic_types']=''
        SampRec['lithologies']=''
        if Cdec!=0 or Cdec!="":
            SampRec['method_codes']='SO-CMD-NORTH'
        else:
            SampRec['method_codes']='SO-MAG'
        for line in Lines[2:len(Lines)]:
            if line == '\n': continue
            MeasRec=SpecRec.copy()
            MeasRec.pop('sample')
            MeasRec['analysts']=user
#           Remove volume and weight as they do not exits in the magic_measurement table
            del MeasRec["volume"]
            del MeasRec["weight"]
            if line[3:6]=='   ' : # USGS files have blank for an AF demag value when measurement is the NRM. njarboe
                line = 'NRM' + line[3:]
            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.split()=='': treat='0'
                try: float(treat)
                except ValueError: treat = line.split()[1]
            if treat_type.startswith('NRM'):
                MeasRec['method_codes']='LT-NO'
                MeasRec['meas_temp']='273'
                MeasRec['treat_temp']='273'
                MeasRec['treat_dc_field']='0'
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treat_ac_field']='0'
            elif treat_type.startswith('LT') or treat_type.upper().startswith('LN2'):
                MeasRec['method_codes']='LT-LT-Z'
                MeasRec['meas_temp']='273'
                MeasRec['treat_temp']='77'
                MeasRec['treat_dc_field']='0'
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treat_ac_field']='0'
            elif treat_type.startswith('AF') or treat_type.startswith('MAF'):
                MeasRec['method_codes']='LT-AF-Z'
                MeasRec['meas_temp']='273'
                MeasRec['treat_temp']='273'
                MeasRec['treat_dc_field']='0'
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                if treat.strip() == '':
                    MeasRec['treat_ac_field']='0'
                else:
                    try: MeasRec['treat_ac_field']='%10.3e'%(float(treat)*1e-3)
                    except ValueError as e: print(os.path.join(input_dir_path,specimen)); raise e
                if MeasRec['treat_ac_field']!='0':
                    MeasRec['treat_ac_field']='%10.3e'%(float(MeasRec['treat_ac_field'])/10)
            elif treat_type.startswith('ARM'):
                MeasRec['method_codes']="LP-ARM"
                MeasRec['meas_temp']='273'
                MeasRec['treat_temp']='273'
                MeasRec['treat_dc_field']='0'
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                if treat.strip() == '':
                    MeasRec['treat_ac_field']='0'
                else:
                    MeasRec['method_codes']="LP-ARM-AFD"
                    MeasRec['treat_ac_field']='%10.3e'%(float(treat)*1e-3)
            elif treat_type.startswith('IRM'):
                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['method_codes']="LT-IRM"
                MeasRec['meas_temp']='273'
                MeasRec['treat_temp']='273'
                MeasRec['treat_dc_field']='%1.2e'%DC_FIELD
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treat_ac_field']='0'
            elif treat_type.startswith('TT'):
                MeasRec['method_codes']='LT-T-Z'
                MeasRec['meas_temp']='273'
                if treat.strip() == '':
                    MeasRec['treat_temp']='273'
                else:
                    MeasRec['treat_temp']='%7.1f'%(float(treat)+273)
                MeasRec['treat_dc_field']='0'
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treat_ac_field']='0'
            elif line[4] == '0': #assume decimal IZZI format 0 field thus can hardcode the dc fields
                MeasRec['method_codes']='LT-T-Z'
                MeasRec['meas_temp']='273'
                try: MeasRec['treat_temp']=str(int(treat_type) + 273)
                except ValueError as e: print(specimen); raise e
                MeasRec['treat_dc_field']='0'
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treat_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['method_codes']='LT-T-I'
                MeasRec['meas_temp']='273'
                MeasRec['treat_temp']=str(int(treat_type) + 273)
                MeasRec['treat_dc_field']='%1.2e'%DC_FIELD
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treat_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['method_codes']='LT-PTRM-I'
                MeasRec['meas_temp']='273'
                MeasRec['treat_temp']=str(int(treat_type) + 273)
                MeasRec['treat_dc_field']='%1.2e'%DC_FIELD
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treat_ac_field']='0'
            elif line[4] == '3': #assume decimal IZZI format PTRM tail check
                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['method_codes']='LT-PTRM-Z'
                MeasRec['meas_temp']='273'
                MeasRec['treat_temp']=str(int(treat_type) + 273)
                MeasRec['treat_dc_field']='0'
                MeasRec['treat_dc_field_phi'] = '%1.2f'%DC_PHI
                MeasRec['treat_dc_field_theta'] = '%1.2f'%DC_THETA
                MeasRec['treat_ac_field']='0'
            else:
                print("trouble with your treatment steps")
            MeasRec['dir_dec']=line[46:51]
            MeasRec['dir_inc']=line[52:58]
#           Some MIT files have and extra digit in the exponent of the magnetude. 
#           That makes those files not compliant with the cit measurement file spec.
#           Not sure if we should just print an error message and exit. For now we accept the file and fix it.
#           The first digit of the exponent, which should always be zero, is cut out of the line if column 39 is not ' ' 
            if line[39] != ' ': line = line[0:37] + line[38:]
            M='%8.2e'%(float(line[31:39])*volmass*1e-3) # convert to Am2
            MeasRec['magn_moment']=M
            MeasRec['dir_csd']='%7.1f'%(eval(line[41:46]))
            MeasRec["meas_n_orient"]=meas_n_orient
            MeasRec['standard']='u'
            if len(line)>60:
                MeasRec['instrument_codes']=line[85:].strip('\n \r \t "')
                MeasRec['magn_x_sigma']='%8.2e'%(float(line[58:67])*1e-8) #(convert e-5emu to Am2)
                MeasRec['magn_y_sigma']='%8.2e'%(float(line[67:76])*1e-8)
                MeasRec['magn_z_sigma']='%8.2e'%(float(line[76:85])*1e-8)
            MeasRecs.append(MeasRec)
        SpecRecs.append(SpecRec)
        if sample not in samples:
            samples.append(sample)
            SampRecs.append(SampRec)
        if site not in sites:
            sites.append(site)
            SiteRecs.append(SiteRec)

    con = nb.Contribution(output_dir_path,read_tables=[])

    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts=pmag.measurements_methods3(MeasRecs,noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    con.tables['specimens'].write_magic_file(custom_name=spec_file)
    con.tables['samples'].write_magic_file(custom_name=samp_file)
    con.tables['sites'].write_magic_file(custom_name=site_file)
    con.tables['locations'].write_magic_file(custom_name=loc_file)
    con.tables['measurements'].write_magic_file(custom_name=meas_file)

    return True, meas_file
示例#31
0
def main():
    """
    NAME
        umich_magic.py
 
    DESCRIPTION
        converts UMICH .mag format files to magic_measurements format files

    SYNTAX
        umich_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -usr USER:   identify user, default is ""
        -f FILE: specify .mag format input file, required
        -fsa SAMPFILE : specify er_samples.txt file relating samples, site and locations names,default is none
        -F FILE: specify output file, default is magic_measurements.txt
        -spc NUM : specify number of characters to designate a  specimen, default = 0
        -loc LOCNAME : specify location/study name, must have either LOCNAME or SAMPFILE or be a synthetic
        -ncn NCON:  specify naming convention: default is #1 below
        -A: don't average replicate measurements
       Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name same as sample
            [6] site is entered under a separate column -- NOT CURRENTLY SUPPORTED
            [7-Z] [XXXX]YYY:  XXXX is site designation with Z characters with sample name XXXXYYYY
            NB: all others you will have to customize your self
                 or e-mail [email protected] for help.
 
        Format of UMICH .mag files:   
        Spec Treat CSD Intensity Declination Inclination metadata string
        Spec: specimen name
        Treat:  treatment step
            XXX T in Centigrade
            XXX AF in mT
         Intensity assumed to be total moment in 10^3 Am^2 (emu)
         Declination:  Declination in specimen coordinate system
         Inclination:  Declination in specimen coordinate system

         metatdata string:  mm/dd/yy;hh:mm;[dC,mT];xx.xx;UNITS;USER;INST;NMEAS
             hh in 24 hours.  
             dC or mT units of treatment XXX (see Treat above) for thermal or AF respectively
             xx.xxx   DC field
             UNITS of DC field (microT, mT)
             INST:  instrument code, number of axes, number of positions (e.g., G34 is 2G, three axes, 
                    measured in four positions)
             NMEAS: number of measurements in a single position (1,3,200...)
    """
# initialize some stuff
    dir_path='.'
    infile_type="mag"
    noave=0
    methcode,inst="",""
    phi,theta,peakfield,labfield=0,0,0,0
    pTRM,MD,samp_con,Z=0,0,'1',1
    missing=1
    demag="N"
    er_location_name=""
    citation='This study'
    args=sys.argv
    methcode="LP-NO"
    samp_file,ErSamps='',[]
    specnum=0
#
# get command line arguments
#
    meas_file="magic_measurements.txt"
    user=""
    if '-WD' in args:
        ind=args.index("-WD")
        dir_path=args[ind+1]
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-usr" in args:
        ind=args.index("-usr")
        user=args[ind+1]
    if '-F' in args:
        ind=args.index("-F")
        meas_file=dir_path+'/'+args[ind+1]
    if '-f' in args:
        ind=args.index("-f")
        magfile=dir_path+'/'+args[ind+1]
        try:
            input=open(magfile,'rU')
        except:
            print "bad mag file name"
            sys.exit()
    else: 
        print "mag_file field is required option"
        print main.__doc__
        sys.exit()
    if "-spc" in args:
        ind=args.index("-spc")
        specnum=int(args[ind+1])
        if specnum!=0:specnum=-specnum
    if "-loc" in args:
        ind=args.index("-loc")
        er_location_name=args[ind+1]
    if "-fsa" in args:
        ind=args.index("-fsa")
        samp_file=dir_path+'/'+args[ind+1]
        Samps,file_type=pmag.magic_read(samp_file)
    if "-A" in args: noave=1
    if "-ncn" in args:
        ind=args.index("-ncn")
        samp_con=sys.argv[ind+1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print "option [4] must be in form 4-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="4"
            samp_con=sys.argv[ind+1]
        if "7" in samp_con:
            if "-" not in samp_con:
                print "option [7] must be in form 7-Z where Z is an integer"
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="7"
    MagRecs,specs=[],[]
    version_num=pmag.get_version()
    if infile_type=="mag":
        for line in input.readlines():
            instcode=""
            if len(line)>2:
                MagRec={}
                MagRec['er_location_name']=er_location_name
                MagRec['magic_software_packages']=version_num
                MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
                MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
                MagRec["treatment_ac_field"]='0'
                MagRec["treatment_dc_field"]='0'
                MagRec["treatment_dc_field_phi"]='0'
                MagRec["treatment_dc_field_theta"]='0'
                meas_type="LT-NO"
                rec=line.split()
                labfield=0
                code1=rec[6].split(';')
                date=code1[0].split('/') # break date into mon/day/year
                yy=int(date[2])
                if yy <90:
                    yyyy=str(2000+yy)
                else: yyyy=str(1900+yy)
                mm=int(date[0])
                if mm<10:
                    mm="0"+str(mm)
                else: mm=str(mm)
                dd=int(date[1])
                if dd<10:
                    dd="0"+str(dd)
                else: dd=str(dd)
                time=code1[1].split(':')
                hh=int(time[0])
                if hh<10:
                    hh="0"+str(hh)
                else: hh=str(hh)
                min=int(time[1])
                if min<10:
                   min= "0"+str(min)
                else: min=str(min)
                MagRec["measurement_date"]=yyyy+":"+mm+":"+dd+":"+hh+":"+min+":00.00"
                MagRec["measurement_time_zone"]=''
                instcode=''
                if len(code1)>1:
                    MagRec["measurement_positions"]=code1[6][2]
                else:
                    MagRec["measurement_positions"]=code1[7]   # takes care of awkward format with bubba and flo being different
                if user=="":user=code1[5]
                if code1[2][-1]=='C': demag="T"
                if code1[2]=='mT': demag="AF"
                treat=rec[1].split('.')
                if len(treat)==1:treat.append('0')
                if demag=='T' and treat!=0:
                    meas_type="LT-T-Z"
                    MagRec["treatment_temp"]='%8.3e' % (float(treat[0])+273.) # temp in kelvin
                if demag=="AF":
                    meas_type="LT-AF-Z"
                    MagRec["treatment_ac_field"]='%8.3e' % (float(treat[0])*1e-3) # Af field in T
                MagRec["treatment_dc_field"]='0'
                MagRec["er_specimen_name"]=rec[0]
                if rec[0] not in specs:specs.append(rec[0]) # get a list of specimen names
                experiment=rec[0]+":"
                MagRec["er_site_name"]=""
                if specnum!=0:
                    MagRec["er_sample_name"]=rec[0][:specnum]
                else:
                    MagRec["er_sample_name"]=rec[0]
                if "-fsa" in args:
                    for samp in Samps:
                        if samp["er_sample_name"] == MagRec["er_sample_name"]: 
                            MagRec["er_location_name"]=samp["er_location_name"]
                            MagRec["er_site_name"]=samp["er_site_name"]
                            break
                elif int(samp_con)!=6:
                    site=pmag.parse_site(MagRec['er_sample_name'],samp_con,Z)
                    MagRec["er_site_name"]=site
                if MagRec['er_site_name']=="":
                    print 'No site name found for: ',MagRec['er_specimen_name'],MagRec['er_sample_name']
                if MagRec["er_location_name"]=="":
                    print 'no location name for: ',MagRec["er_specimen_name"] 
                if rec[1]==".00":rec[1]="0.00"
                MagRec["measurement_csd"]=rec[2]
                MagRec["measurement_magn_moment"]='%10.3e'% (float(rec[3])*1e-3) # moment in Am^2 (from emu)
                MagRec["measurement_dec"]=rec[4]
                MagRec["measurement_inc"]=rec[5]
                MagRec["magic_instrument_codes"]=instcode
                MagRec["er_analyst_mail_names"]=user
                MagRec["er_citation_names"]=citation
                MagRec["magic_method_codes"]=meas_type
                MagRec["measurement_flag"]='g'
                MagRec["er_specimen_name"]=rec[0]
                MagRec["measurement_number"]='1'
                MagRecs.append(MagRec) 
    MagOuts=[]
    for spec in specs:  # gather all demag types for this specimen
        SpecRecs,meths,measnum=[],[],1
        for rec in MagRecs:
            if rec['er_specimen_name']==spec:
                rec['measurement_number']=str(measnum)
                measnum+=1
                if rec['magic_method_codes'] not in meths:meths.append(rec['magic_method_codes'])
                SpecRecs.append(rec)
        expname=spec
        if "LT-AF-Z" in meths:expname=expname+ ':LP-DIR-AF'
        if "LT-T-Z" in meths:expname=expname+ ':LP-DIR-T'
        for rec in SpecRecs:
            rec['magic_experiment_name']=expname
            MagOuts.append(rec)
    pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    print "results put in ",meas_file
示例#32
0
def convert(**kwargs):
    #
    # initialize variables
    #
    bed_dip, bed_dip_dir = "", ""
    sclass, lithology, _type = "", "", ""
    DecCorr = 0.
    months = [
        'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct',
        'Nov', 'Dec'
    ]

    dir_path = kwargs.get('dir_path', '.')
    mag_file = kwargs.get('mag_file', '')
    meas_file = kwargs.get('meas_file', 'measurements.txt')
    spec_file = kwargs.get('spec_file', 'specimens.txt')
    samp_file = kwargs.get('samp_file', 'samples.txt')
    site_file = kwargs.get('site_file', 'sites.txt')
    loc_file = kwargs.get('loc_file', 'locations.txt')
    or_con = kwargs.get('or_con', '3')
    samp_con = kwargs.get('samp_con', '2')
    corr = kwargs.get('corr', '1')
    gmeths = kwargs.get('gmeths', 'FS-FD:SO-POM')
    location = kwargs.get('location', 'unknown')
    specnum = int(kwargs.get('specnum', 0))
    inst = kwargs.get('inst', '')
    user = kwargs.get('user', '')
    noave = kwargs.get('noave', 0)  # default is DO average
    ID = kwargs.get('ID', '')
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')

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

    if ID:
        input_dir_path = ID
    else:
        input_dir_path = dir_path

    if samp_con:
        Z = 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, "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:
            print('Naming convention option [6] not currently supported')
            return False, 'Naming convention option [6] not currently supported'
            #Z=1
            #try:
            #    SampRecs,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"
        #else: Z=1

    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)

    samplist = []
    try:
        SampRecs, file_type = pmag.magic_read(samp_file)
    except:
        SampRecs = []
    MeasRecs, SpecRecs, SiteRecs, LocRecs = [], [], [], []
    try:
        f = open(mag_file, 'br')
        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

            methods = gmeths.split(':')
            if deccorr != "0":
                if 'SO-MAG' in methods: del methods[methods.index('SO-MAG')]
                methods.append('SO-CMD-NORTH')
            meths = reduce(lambda x, y: x + ':' + y, methods)
            method_codes = meths
            site = pmag.parse_site(sample, samp_con,
                                   Z)  # parse out the site name
            SpecRec, SampRec, SiteRec, LocRec = {}, {}, {}, {}
            SpecRec["specimen"] = specname
            SpecRec["sample"] = sample
            if vcc.strip() != "":
                vol = float(vcc) * 1e-6  # convert to m^3 from cc
            SpecRec["volumne"] = '%10.3e' % (vol)  #
            SpecRec["geologic_classes"] = sclass
            SpecRec["lithologies"] = lithology
            SpecRec["geologic_types"] = _type
            SpecRecs.append(SpecRec)

            if sample != "" and sample not in [
                    x['sample'] if 'sample' in list(x.keys()) else ""
                    for x in SampRecs
            ]:
                SampRec["sample"] = sample
                SampRec["site"] = site
                labaz, labdip = pmag.orient(az, pl,
                                            or_con)  # convert to labaz, labpl
                SampRec["bed_dip"] = '%7.1f' % (bed_dip)
                SampRec["bed_dip_direction"] = '%7.1f' % (bed_dip_dir)
                SampRec["dip"] = '%7.1f' % (labdip)
                SampRec["azimuth"] = '%7.1f' % (labaz)
                SampRec["azimuth_dec_correction"] = '%7.1f' % (deccorr)
                SampRec["geologic_classes"] = sclass
                SampRec["lithologies"] = lithology
                SampRec["geologic_types"] = _type
                SampRec["method_codes"] = method_codes
                SampRecs.append(SampRec)

            if site != "" and site not in [
                    x['site'] if 'site' in list(x.keys()) else ""
                    for x in SiteRecs
            ]:
                SiteRec['site'] = site
                SiteRec['location'] = location
                SiteRec['lat'] = lat
                SiteRec['lon'] = lon
                SiteRec["geologic_classes"] = sclass
                SiteRec["lithologies"] = lithology
                SiteRec["geologic_types"] = _type
                SiteRecs.append(SiteRec)

            if location != "" and location not in [
                    x['location'] if 'location' in list(x.keys()) else ""
                    for x in LocRecs
            ]:
                LocRec['location'] = location
                LocRec['lat_n'] = lat
                LocRec['lon_e'] = lon
                LocRec['lat_s'] = lat
                LocRec['lon_w'] = lon
                LocRec["geologic_classes"] = sclass
                LocRec["lithologies"] = lithology
                LocRec["geologic_types"] = _type
                LocRecs.append(LocRec)

        else:
            MeasRec = {}
            MeasRec["treat_temp"] = '%8.3e' % (273)  # room temp in kelvin
            MeasRec["meas_temp"] = '%8.3e' % (273)  # room temp in kelvin
            MeasRec["treat_ac_field"] = '0'
            MeasRec["treat_dc_field"] = '0'
            MeasRec["treat_dc_field_phi"] = '0'
            MeasRec["treat_dc_field_theta"] = '0'
            meas_type = "LT-NO"
            MeasRec["quality"] = 'g'
            MeasRec["standard"] = 'u'
            MeasRec["treat_step_num"] = '1'
            MeasRec["specimen"] = specname
            el, demag = 1, ''
            treat = rec[el]
            if treat[-1] == 'C':
                demag = 'T'
            elif treat != 'NRM':
                demag = 'AF'
            el += 1
            while rec[el] == "":
                el += 1
            MeasRec["dir_dec"] = rec[el]
            cdec = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            MeasRec["dir_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]
            MeasRec["magn_moment"] = '%10.3e' % (float(rec[el]) * 1e-3
                                                 )  # moment in Am^2 (from emu)
            MeasRec["magn_volume"] = '%10.3e' % (float(rec[el]) * 1e-3 / vol
                                                 )  # magnetization in A/m
            el = skip(2, el, rec)  # skip to xsig
            MeasRec["magn_x_sigma"] = '%10.3e' % (float(rec[el]) * 1e-3
                                                  )  # convert from emu
            el = skip(3, el, rec)  # skip to ysig
            MeasRec["magn_y_sigma"] = '%10.3e' % (float(rec[el]) * 1e-3
                                                  )  # convert from emu
            el = skip(3, el, rec)  # skip to zsig
            MeasRec["magn_z_sigma"] = '%10.3e' % (float(rec[el]) * 1e-3
                                                  )  # convert from emu
            el += 1  # skip to positions
            MeasRec["meas_n_orient"] = 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]
            #                    MeasRec['measurement_date']=dstring
            MeasRec["instrument_codes"] = inst
            MeasRec["analysts"] = user
            MeasRec["citations"] = "This study"
            MeasRec["method_codes"] = meas_type
            if demag == "AF":
                MeasRec["treat_ac_field"] = '%8.3e' % (float(treat[:-2]) * 1e-3
                                                       )  # peak field in tesla
                meas_type = "LT-AF-Z"
                MeasRec["treat_dc_field"] = '0'
            elif demag == "T":
                MeasRec["treat_temp"] = '%8.3e' % (float(treat[:-1]) + 273.
                                                   )  # temp in kelvin
                meas_type = "LT-T-Z"
            MeasRec['method_codes'] = meas_type
            MeasRecs.append(MeasRec)

    con = nb.Contribution(output_dir_path, read_tables=[])

    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts = pmag.measurements_methods3(MeasRecs, noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    con.write_table_to_file('specimens', custom_name=spec_file)
    con.write_table_to_file('samples', custom_name=samp_file)
    con.write_table_to_file('sites', custom_name=site_file)
    con.write_table_to_file('locations', custom_name=loc_file)
    con.write_table_to_file('measurements', custom_name=meas_file)
    return True, meas_file
示例#33
0
def convert(**kwargs):

    version_num = pmag.get_version()
    dir_path = kwargs.get('dir_path', '.')
    input_dir_path = kwargs.get('input_dir_path', dir_path)
    output_dir_path = 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
    mag_file = kwargs.get('mag_file')
    specnum = kwargs.get('specnum', 1)
    samp_con = kwargs.get('samp_con', '1')
    location = kwargs.get('location', 'unknown')
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')
    noave = kwargs.get('noave', 0)  # default (0) means DO average
    meth_code = kwargs.get('meth_code', "LP-NO")
    volume = float(kwargs.get('volume', 2.5)) * 1e-6
    timezone = kwargs.get('timestamp', 'UTC')

    # format variables
    mag_file = os.path.join(input_dir_path, mag_file)
    if specnum != 0: specnum = -int(specnum)
    if samp_con.startswith("4"):
        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 samp_con.startswith("7"):
        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"
    else:
        Z = 1

    #create data holders
    MeasRecs, SpecRecs, SampRecs, SiteRecs, LocRecs = [], [], [], [], []

    # parse data
    data = open(mag_file, 'r')
    line = data.readline()
    line = data.readline()
    line = data.readline()
    while line != '':
        parsedLine = line.split()
        if len(parsedLine) >= 4:
            sampleName = parsedLine[0]
            demagLevel = parsedLine[2]
            date = parsedLine[3] + ":0:0:0"
            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.
            specimen = sampleName
            if specnum != 0: sample = specimen[:specnum]
            else: sample = specimen
            site = pmag.parse_site(sample, samp_con, Z)

            MeasRec, SpecRec, SampRec, SiteRec, LocRec = {}, {}, {}, {}, {}

            if specimen != "" and specimen not in [
                    x['specimen'] if 'specimen' in list(x.keys()) else ""
                    for x in SpecRecs
            ]:
                SpecRec['specimen'] = specimen
                SpecRec['sample'] = sample
                SpecRec["citations"] = "This study"
                SpecRec["analysts"] = user
                SpecRec['volume'] = volume
                SpecRecs.append(SpecRec)
            if sample != "" and sample not in [
                    x['sample'] if 'sample' in list(x.keys()) else ""
                    for x in SampRecs
            ]:
                SampRec['sample'] = sample
                SampRec['site'] = site
                SampRec["citations"] = "This study"
                SampRec["analysts"] = user
                SampRec['azimuth'] = specimenAngleDec
                sample_dip = str(float(specimenAngleInc) -
                                 90.0)  #convert to magic orientation
                SampRec['dip'] = sample_dip
                SampRec['method_codes'] = meth_code
                SampRecs.append(SampRec)
            if site != "" and site not in [
                    x['site'] if 'site' in list(x.keys()) else ""
                    for x in SiteRecs
            ]:
                SiteRec['site'] = site
                SiteRec['location'] = location
                SiteRec["citations"] = "This study"
                SiteRec["analysts"] = user
                SiteRec['lat'] = lat
                SiteRec['lon'] = lon
                SiteRecs.append(SiteRec)
            if location != "" and location not in [
                    x['location'] if 'location' in list(x.keys()) else ""
                    for x in LocRecs
            ]:
                LocRec['location'] = location
                LocRec["citations"] = "This study"
                LocRec["analysts"] = user
                LocRec['lat_n'] = lat
                LocRec['lon_e'] = lon
                LocRec['lat_s'] = lat
                LocRec['lon_w'] = lon
                LocRecs.append(LocRec)

            local = pytz.timezone(timezone)
            naive = datetime.datetime.strptime(date, "%m-%d-%Y:%H:%M:%S")
            local_dt = local.localize(naive, is_dst=None)
            utc_dt = local_dt.astimezone(pytz.utc)
            timestamp = utc_dt.strftime("%Y-%m-%dT%H:%M:%S") + "Z"
            MeasRec["specimen"] = specimen
            MeasRec["timestamp"] = timestamp
            MeasRec['description'] = ''
            MeasRec["citations"] = "This study"
            MeasRec['software_packages'] = version_num
            MeasRec["treat_temp"] = '%8.3e' % (273)  # room temp in kelvin
            MeasRec["meas_temp"] = '%8.3e' % (273)  # room temp in kelvin
            MeasRec["quality"] = 'g'
            MeasRec["standard"] = 'u'
            MeasRec["treat_step_num"] = '1'
            MeasRec["treat_ac_field"] = '0'
            if demagLevel == 'NRM':
                meas_type = "LT-NO"
            elif demagLevel[0] == 'A':
                meas_type = "LT-AF-Z"
                treat = float(demagLevel[1:])
                MeasRec["treat_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:])
                MeasRec["treat_temp"] = '%8.3e' % (treat + 273.
                                                   )  # temp in kelvin
            else:
                print("measurement type unknown", demag_level)
                return False, "measurement type unknown"

            MeasRec["magn_moment"] = str(intensityVol * volume)  # Am^2
            MeasRec["magn_volume"] = intensityVolStr  # A/m
            MeasRec["dir_dec"] = specimenDec
            MeasRec["dir_inc"] = specimenInc
            MeasRec['method_codes'] = meas_type
            MeasRecs.append(MeasRec)

        #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

    data.close()

    con = nb.Contribution(output_dir_path, read_tables=[])

    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts = pmag.measurements_methods3(MeasRecs, noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    con.tables['specimens'].write_magic_file(custom_name=spec_file)
    con.tables['samples'].write_magic_file(custom_name=samp_file)
    con.tables['sites'].write_magic_file(custom_name=site_file)
    con.tables['locations'].write_magic_file(custom_name=loc_file)
    con.tables['measurements'].write_magic_file(custom_name=meas_file)

    return True, meas_file
示例#34
0
def main():
    """
    NAME
        s_magic.py
  
    DESCRIPTION
        converts .s format data to magic_measurements  format.

    SYNTAX
        s_magic.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f SFILE specifies the .s file name
        -sig last column has sigma
        -typ Anisotropy type:  AMS,AARM,ATRM (default is AMS)
        -F RFILE specifies the rmag_anisotropy file name
        -usr USER specify username
        -loc location specify location/study name
        -spc NUM : specify number of characters to 
              designate a  specimen, default = 0
        -spn SPECNAME, this specimen has the name SPECNAME
        -n first column has specimen name 
        -crd [s,g,t], specify coordinate system of data
           s=specimen,g=geographic,t=tilt adjusted, default is 's'
        -ncn NCON: naming conventionconvention NCON
       Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXXYYY:  YYY is sample designation with Z characters from site XXX
            [5] sample = site
            [6] sample, site, location info in er_samples.txt -- NOT CURRENTLY SUPPORTED
            [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
            NB: all others you will have to either customize your
                self or e-mail [email protected] for help.

    
    DEFAULT
        RFILE:  rmag_anisotropy.txt

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

    OUTPUT 
        rmag_anisotropy.txt format file

    NOTE
        because .s files do not have specimen names or location information, the output MagIC files
        will have to be changed prior to importing to data base.   
    """
    sfile,anisfile="","rmag_anisotropy.txt"
    location='unknown'
    user=""
    sitename,specnum='unknown',0
    samp_con,Z="",1
    user=""
    dir_path='.'
    name,sigma,spec=0,0,'unknown'
    type='AMS'
    if '-WD' in sys.argv:
        ind=sys.argv.index('-WD')
        dir_path=sys.argv[ind+1]
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if "-spc" in sys.argv:
        ind=sys.argv.index("-spc")
        specnum=int(sys.argv[ind+1])
        if specnum!=0:specnum=-specnum
    if "-spn" in sys.argv:
        ind=sys.argv.index("-spn")
        spec=sys.argv[ind+1]
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        sfile = sys.argv[ind+1]
    if '-sig' in sys.argv: sigma=1
    if '-typ' in sys.argv: 
        ind=sys.argv.index('-typ')
        type = sys.argv[ind+1]
    if '-F' in sys.argv:
        ind=sys.argv.index('-F')
        anisfile = sys.argv[ind+1]
    if '-usr' in sys.argv:
        ind=sys.argv.index('-usr')
        user = sys.argv[ind+1]
    if '-loc' in sys.argv:
        ind=sys.argv.index('-loc')
        location = sys.argv[ind+1]
    if "-n" in sys.argv:
        name=1
    coord='-1'
    if "-crd" in sys.argv:
        ind=sys.argv.index("-crd")
        coord=sys.argv[ind+1]
        if coord=='s':coord='-1'
        if coord=='g':coord='0'
        if coord=='t':coord='100'
    if "-ncn" in sys.argv:
        ind=sys.argv.index("-ncn")
        samp_con=sys.argv[ind+1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print("option [4] must be in form 4-Z where Z is an integer")
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="4"
        if samp_con=='6':
            Samps,filetype=pmag.magic_read(dirpath+'/er_samples.txt')
    #
    # get down to bidness
    sfile=dir_path+'/'+sfile
    anisfile=dir_path+'/'+anisfile
    input=open(sfile,'r')
    AnisRecs=[]
    linecnt=0
    citation="This study"
    # read in data
    for line in input.readlines():
        AnisRec={}
        rec=line.split()
        if name==1:
            k=1
            spec=rec[0]
        else:
            k=0
        trace=float(rec[k])+float(rec[k+1])+float(rec[k+2])
        s1='%10.9e'%(old_div(float(rec[k]),trace))
        s2='%10.9e'%(old_div(float(rec[k+1]),trace))
        s3='%10.9e'%(old_div(float(rec[k+2]),trace))
        s4='%10.9e'%(old_div(float(rec[k+3]),trace))
        s5='%10.9e'%(old_div(float(rec[k+4]),trace))
        s6='%10.9e'%(old_div(float(rec[k+5]),trace))
        AnisRec["er_citation_names"]=citation
        AnisRec["er_specimen_name"]=spec
        if specnum!=0:
            AnisRec["er_sample_name"]=spec[:specnum]
        else:
            AnisRec["er_sample_name"]=spec
        if samp_con=="6":
            for samp in Samps:
                if samp['er_sample_name']==AnisRec["er_sample_name"]:
                    sitename=samp['er_site_name']
                    location=samp['er_location_name']
        elif samp_con!="":
            sitename=pmag.parse_site(AnisRec['er_sample_name'],samp_con,Z)
        AnisRec["er_location_name"]=location
        AnisRec["er_site_name"]=sitename
        AnisRec["er_anylist_mail_names"]=user
        if type=='AMS':
            AnisRec["anisotropy_type"]="AMS"
            AnisRec["magic_experiment_names"]=spec+":LP-X"
        else:
            AnisRec["anisotropy_type"]=type
            AnisRec["magic_experiment_names"]=spec+":LP-"+type
        AnisRec["anisotropy_s1"]=s1
        AnisRec["anisotropy_s2"]=s2
        AnisRec["anisotropy_s3"]=s3
        AnisRec["anisotropy_s4"]=s4
        AnisRec["anisotropy_s5"]=s5
        AnisRec["anisotropy_s6"]=s6
        if sigma==1: AnisRec["anisotropy_sigma"]='%10.8e'%(old_div(float(rec[k+6]),trace))
        AnisRec["anisotropy_unit"]='SI'
        AnisRec["anisotropy_tilt_correction"]=coord
        AnisRec["magic_method_codes"]='LP-'+type
        AnisRecs.append(AnisRec)
    pmag.magic_write(anisfile,AnisRecs,'rmag_anisotropy')
    print('data saved in ',anisfile) 
示例#35
0
def convert(**kwargs):
    """

    """

    #get kwargs
    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', 'measurements.txt')
    mag_file = kwargs.get('mag_file')
    spec_file = kwargs.get('spec_file', 'specimens.txt')
    samp_file = kwargs.get('samp_file', 'samples.txt')
    site_file = kwargs.get('site_file', 'sites.txt')
    loc_file = kwargs.get('loc_file', 'locations.txt')
    lat = kwargs.get('lat', 0)
    lon = kwargs.get('lon', 0)
    specnum = int(kwargs.get('specnum', 0))
    samp_con = kwargs.get('samp_con', '1')
    location = kwargs.get('location', 'unknown')
    noave = kwargs.get('noave', 0) # default (0) means DO average
    meth_code = kwargs.get('meth_code', "LP-NO")
    version_num=pmag.get_version()

    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"
    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"
    else: Z = 1

    # format variables
    mag_file = os.path.join(input_dir_path,mag_file)
    meas_file = os.path.join(output_dir_path,meas_file)
    spec_file = os.path.join(output_dir_path,spec_file)
    samp_file = os.path.join(output_dir_path,samp_file)
    site_file = os.path.join(output_dir_path,site_file)

    # parse data
    data=open(mag_file,'r').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.
    specimen=rec[0]
    SpecRecs,SampRecs,SiteRecs,LocRecs,MeasRecs = [],[],[],[],[]
    SpecRec,SampRec,SiteRec,LocRec={},{},{},{} # make a  sample record
    if specnum!=0:
        sample=rec[0][:specnum]
    else:
        sample=rec[0]
    if int(samp_con)<6:
        site=pmag.parse_site(sample,samp_con,Z)
    else:
        if 'site' in list(SampRec.keys()):site=ErSampREc['site']
        if 'location' in list(SampRec.keys()):location=ErSampREc['location']
    az_ind=rec.index('a=')+1
    SampRec['sample']=sample
    SampRec['description']=comment
    SampRec['azimuth']=rec[az_ind]
    dip_ind=rec.index('b=')+1
    dip=-float(rec[dip_ind])
    SampRec['dip']='%7.1f'%(dip)
    strike_ind=rec.index('s=')+1
    SampRec['bed_dip_direction']='%7.1f'%(float(rec[strike_ind])+90.)
    bd_ind=rec.index('d=')+1
    SampRec['bed_dip']=rec[bd_ind]
    v_ind=rec.index('v=')+1
    vol=rec[v_ind][:-3]
    date=rec[-2]
    time=rec[-1]
    SampRec['method_codes']=meth_code
    SampRec['site']=site
    SampRec['citations']='This study'
    SampRec['method_codes']='SO-NO'

    SpecRec['specimen'] = specimen
    SpecRec['sample'] = sample
    SpecRec['citations']='This study'

    SiteRec['site'] = site
    SiteRec['location'] = location
    SiteRec['citations']='This study'
    SiteRec['lat'] = lat
    SiteRec['lon']= lon

    LocRec['location'] = location
    LocRec['citations']='This study'
    LocRec['lat_n'] = lat
    LocRec['lat_s'] = lat
    LocRec['lon_e'] = lon
    LocRec['lon_w'] = lon

    SpecRecs.append(SpecRec)
    SampRecs.append(SampRec)
    SiteRecs.append(SiteRec)
    LocRecs.append(LocRec)
    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
        MeasRec={}
        MeasRec['description']='Date: '+date+' '+time
        MeasRec["citations"]="This study"
        MeasRec['software_packages']=version_num
        MeasRec["treat_temp"]='%8.3e' % (273) # room temp in kelvin
        MeasRec["meas_temp"]='%8.3e' % (273) # room temp in kelvin
        MeasRec["quality"]='g'
        MeasRec["standard"]='u'
        MeasRec["treat_step_num"]='1'
        MeasRec["specimen"]=specimen
        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)
        MeasRec["magn_moment"]='%10.3e'% (Vec[2]) # Am^2
        MeasRec["magn_volume"]=rec[4] # A/m
        MeasRec["dir_dec"]='%7.1f'%(Vec[0])
        MeasRec["dir_inc"]='%7.1f'%(Vec[1])
        MeasRec["treat_ac_field"]='0'
        if meas_type!='LT-NO':
            treat=float(rec[0][1:])
        else:
            treat=0
        if meas_type=="LT-AF-Z":
            MeasRec["treat_ac_field"]='%8.3e' %(treat*1e-3) # convert from mT to tesla
        elif meas_type=="LT-T-Z":
            MeasRec["treat_temp"]='%8.3e' % (treat+273.) # temp in kelvin
        MeasRec['method_codes']=meas_type
        MeasRecs.append(MeasRec)

    con = nb.Contribution(output_dir_path,read_tables=[])

    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts=pmag.measurements_methods3(MeasRecs,noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    con.tables['specimens'].write_magic_file(custom_name=spec_file)
    con.tables['samples'].write_magic_file(custom_name=samp_file)
    con.tables['sites'].write_magic_file(custom_name=site_file)
    con.tables['locations'].write_magic_file(custom_name=loc_file)
    con.tables['measurements'].write_magic_file(custom_name=meas_file)

    return True, meas_file
示例#36
0
def convert(**kwargs):
    version_num = pmag.get_version()

    user = kwargs.get('user', '')
    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', 'measurements.txt')
    spec_file = kwargs.get('spec_file', 'specimens.txt') # specimen outfile
    samp_file = kwargs.get('samp_file', 'samples.txt')
    site_file = kwargs.get('site_file', 'sites.txt') # site outfile
    loc_file = kwargs.get('loc_file', 'locations.txt') # Loc outfile
    mag_file = kwargs.get('mag_file', '') #required
    location = kwargs.get('location', 'unknown')
    site = kwargs.get('site', '')
    samp_con = kwargs.get('samp_con', '1')
    specnum = int(kwargs.get('specnum', 0))
    timezone = kwargs.get('timestamp', 'US/Pacific')
    append = kwargs.get('append', False)
    noave = kwargs.get('noave', False) # default False 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: volume *= 1e-6 #convert cm^3 to m^3

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

    # format variables
    mag_file = os.path.join(input_dir_path, mag_file)
    if not os.path.isfile(mag_file):
        print("%s is not a BGC file"%mag_file)
        return False, 'You must provide a BCG format file'


    # 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(' ')
    specimen = line_items[2]
    specimen = specimen.replace('\n', '')
    line = pre_data.readline()
    line = pre_data.readline()
    line_items = line.split('\t')
    azimuth = float(line_items[1])
    dip = float(line_items[2])
    bed_dip = line_items[3]
    sample_bed_azimuth = line_items[4]
    lon = line_items[5]
    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['dir_dec'] = direction[0]
    data['dir_inc'] = direction[1]
    data['magn_moment'] = old_div(direction[2], 1000)  # the data are in EMU - this converts to Am^2
    data['magn_volume'] = old_div((old_div(direction[2], 1000)), volume) # EMU  - data converted to A/m

    # Configure the magic_measurements table
    MeasRecs,SpecRecs,SampRecs,SiteRecs,LocRecs=[],[],[],[],[]
    for rowNum, row in data.iterrows():
        MeasRec,SpecRec,SampRec,SiteRec,LocRec = {},{},{},{},{}

        if specnum!=0:
            sample=specimen[:specnum]
        else:
            sample=specimen
        if site=='':
            site=pmag.parse_site(sample,samp_con,Z)

        if specimen!="" and specimen not in [x['specimen'] if 'specimen' in list(x.keys()) else "" for x in SpecRecs]:
            SpecRec['specimen'] = specimen
            SpecRec['sample'] = sample
            SpecRec['volume'] = volume
            SpecRec['analysts']=user
            SpecRec['citations'] = 'This study'
            SpecRecs.append(SpecRec)
        if sample!="" and sample not in [x['sample'] if 'sample' in list(x.keys()) else "" for x in SampRecs]:
            SampRec['sample'] = sample
            SampRec['site'] = site
            SampRec['azimuth'] = azimuth
            SampRec['dip'] = dip
            SampRec['bed_dip_direction'] = sample_bed_azimuth
            SampRec['bed_dip'] = bed_dip
            SampRec['method_codes'] = meth_code
            SampRec['analysts']=user
            SampRec['citations'] = 'This study'
            SampRecs.append(SampRec)
        if site!="" and site not in [x['site'] if 'site' in list(x.keys()) else "" for x in SiteRecs]:
            SiteRec['site'] = site
            SiteRec['location'] = location
            SiteRec['lat'] = lat
            SiteRec['lon'] = lon
            SiteRec['analysts']=user
            SiteRec['citations'] = 'This study'
            SiteRecs.append(SiteRec)
        if location!="" and location not in [x['location'] if 'location' in list(x.keys()) else "" for x in LocRecs]:
            LocRec['location']=location
            LocRec['analysts']=user
            LocRec['citations'] = 'This study'
            LocRec['lat_n'] = lat
            LocRec['lon_e'] = lon
            LocRec['lat_s'] = lat
            LocRec['lon_w'] = lon
            LocRecs.append(LocRec)

        MeasRec['description'] = 'Date: ' + str(row['Date']) + ' Time: ' + str(row['Time'])
        if '.' in row['Date']: datelist = row['Date'].split('.')
        elif '/' in row['Date']: datelist = row['Date'].split('/')
        elif '-' in row['Date']: datelist = row['Date'].split('-')
        else: print("unrecogized date formating on one of the measurement entries for specimen %s"%specimen); datelist=['','','']
        if ':' in row['Time']: timelist = row['Time'].split(':')
        else: print("unrecogized time formating on one of the measurement entries for specimen %s"%specimen); timelist=['','','']
        datelist[2]='19'+datelist[2] if len(datelist[2])<=2 else datelist[2]
        dt=":".join([datelist[1],datelist[0],datelist[2],timelist[0],timelist[1],timelist[2]])
        local = pytz.timezone(timezone)
        naive = datetime.datetime.strptime(dt, "%m:%d:%Y:%H:%M:%S")
        local_dt = local.localize(naive, is_dst=None)
        utc_dt = local_dt.astimezone(pytz.utc)
        timestamp=utc_dt.strftime("%Y-%m-%dT%H:%M:%S")+"Z"
        MeasRec["timestamp"] = timestamp
        MeasRec["citations"] = "This study"
        MeasRec['software_packages'] = version_num
        MeasRec["treat_temp"] = '%8.3e' % (273) # room temp in kelvin
        MeasRec["meas_temp"] = '%8.3e' % (273) # room temp in kelvin
        MeasRec["quality"] = 'g'
        MeasRec["standard"] = 'u'
        MeasRec["treat_step_num"] = rowNum
        MeasRec["specimen"] = specimen
        MeasRec["treat_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'])
            MeasRec["treat_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'])
            MeasRec["treat_temp"] = '%8.3e' % (treat+273.) # temp in kelvin
        else:
            print("measurement type unknown:", row['DM Type'], " in row ", rowNum)
        MeasRec["magn_moment"] = str(row['magn_moment'])
        MeasRec["magn_volume"] = str(row['magn_volume'])
        MeasRec["dir_dec"] = str(row['dir_dec'])
        MeasRec["dir_inc"] = str(row['dir_inc'])
        MeasRec['method_codes'] = meas_type
        MeasRec['dir_csd'] = '0.0' # added due to magic.write error
        MeasRec['meas_n_orient'] = '1' # added due to magic.write error
        MeasRecs.append(MeasRec.copy())

    con = nb.Contribution(output_dir_path,read_tables=[])

    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts=pmag.measurements_methods3(MeasRecs,noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    con.write_table_to_file('specimens', custom_name=spec_file, append=append)
    con.write_table_to_file('samples', custom_name=samp_file, append=append)
    con.write_table_to_file('sites', custom_name=site_file, append=append)
    con.write_table_to_file('locations', custom_name=loc_file, append=append)
    meas_file = con.write_table_to_file('measurements', custom_name=meas_file, append=append)

    return True, meas_file
示例#37
0
def main():
    """
    NAME
        s_magic.py
  
    DESCRIPTION
        converts .s format data to magic_measurements  format.

    SYNTAX
        s_magic.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f SFILE specifies the .s file name
        -sig last column has sigma
        -typ Anisotropy type:  AMS,AARM,ATRM (default is AMS)
        -F RFILE specifies the rmag_anisotropy file name
        -usr USER specify username
        -loc location specify location/study name
        -spc NUM : specify number of characters to 
              designate a  specimen, default = 0
        -spn SPECNAME, this specimen has the name SPECNAME
        -n first column has specimen name 
        -crd [s,g,t], specify coordinate system of data
           s=specimen,g=geographic,t=tilt adjusted, default is 's'
        -ncn NCON: naming conventionconvention NCON
       Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXXYYY:  YYY is sample designation with Z characters from site XXX
            [5] sample = site
            [6] sample, site, location info in er_samples.txt -- NOT CURRENTLY SUPPORTED
            [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
            NB: all others you will have to either customize your
                self or e-mail [email protected] for help.

    
    DEFAULT
        RFILE:  rmag_anisotropy.txt

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

    OUTPUT 
        rmag_anisotropy.txt format file

    NOTE
        because .s files do not have specimen names or location information, the output MagIC files
        will have to be changed prior to importing to data base.   
    """
    sfile, anisfile = "", "rmag_anisotropy.txt"
    location = 'unknown'
    user = ""
    sitename, specnum = 'unknown', 0
    samp_con, Z = "", 1
    user = ""
    dir_path = '.'
    name, sigma, spec = 0, 0, 'unknown'
    type = 'AMS'
    if '-WD' in sys.argv:
        ind = sys.argv.index('-WD')
        dir_path = sys.argv[ind + 1]
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if "-spc" in sys.argv:
        ind = sys.argv.index("-spc")
        specnum = int(sys.argv[ind + 1])
        if specnum != 0: specnum = -specnum
    if "-spn" in sys.argv:
        ind = sys.argv.index("-spn")
        spec = sys.argv[ind + 1]
    if '-f' in sys.argv:
        ind = sys.argv.index('-f')
        sfile = sys.argv[ind + 1]
    if '-sig' in sys.argv: sigma = 1
    if '-typ' in sys.argv:
        ind = sys.argv.index('-typ')
        type = sys.argv[ind + 1]
    if '-F' in sys.argv:
        ind = sys.argv.index('-F')
        anisfile = sys.argv[ind + 1]
    if '-usr' in sys.argv:
        ind = sys.argv.index('-usr')
        user = sys.argv[ind + 1]
    if '-loc' in sys.argv:
        ind = sys.argv.index('-loc')
        location = sys.argv[ind + 1]
    if "-n" in sys.argv:
        name = 1
    coord = '-1'
    if "-crd" in sys.argv:
        ind = sys.argv.index("-crd")
        coord = sys.argv[ind + 1]
        if coord == 's': coord = '-1'
        if coord == 'g': coord = '0'
        if coord == 't': coord = '100'
    if "-ncn" in sys.argv:
        ind = sys.argv.index("-ncn")
        samp_con = sys.argv[ind + 1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print("option [4] must be in form 4-Z where Z is an integer")
                sys.exit()
            else:
                Z = samp_con.split("-")[1]
                samp_con = "4"
        if samp_con == '6':
            Samps, filetype = pmag.magic_read(dirpath + '/er_samples.txt')
    #
    # get down to bidness
    sfile = dir_path + '/' + sfile
    anisfile = dir_path + '/' + anisfile
    input = open(sfile, 'r')
    AnisRecs = []
    linecnt = 0
    citation = "This study"
    # read in data
    for line in input.readlines():
        AnisRec = {}
        rec = line.split()
        if name == 1:
            k = 1
            spec = rec[0]
        else:
            k = 0
        trace = float(rec[k]) + float(rec[k + 1]) + float(rec[k + 2])
        s1 = '%10.9e' % (old_div(float(rec[k]), trace))
        s2 = '%10.9e' % (old_div(float(rec[k + 1]), trace))
        s3 = '%10.9e' % (old_div(float(rec[k + 2]), trace))
        s4 = '%10.9e' % (old_div(float(rec[k + 3]), trace))
        s5 = '%10.9e' % (old_div(float(rec[k + 4]), trace))
        s6 = '%10.9e' % (old_div(float(rec[k + 5]), trace))
        AnisRec["er_citation_names"] = citation
        AnisRec["er_specimen_name"] = spec
        if specnum != 0:
            AnisRec["er_sample_name"] = spec[:specnum]
        else:
            AnisRec["er_sample_name"] = spec
        if samp_con == "6":
            for samp in Samps:
                if samp['er_sample_name'] == AnisRec["er_sample_name"]:
                    sitename = samp['er_site_name']
                    location = samp['er_location_name']
        elif samp_con != "":
            sitename = pmag.parse_site(AnisRec['er_sample_name'], samp_con, Z)
        AnisRec["er_location_name"] = location
        AnisRec["er_site_name"] = sitename
        AnisRec["er_anylist_mail_names"] = user
        if type == 'AMS':
            AnisRec["anisotropy_type"] = "AMS"
            AnisRec["magic_experiment_names"] = spec + ":LP-X"
        else:
            AnisRec["anisotropy_type"] = type
            AnisRec["magic_experiment_names"] = spec + ":LP-" + type
        AnisRec["anisotropy_s1"] = s1
        AnisRec["anisotropy_s2"] = s2
        AnisRec["anisotropy_s3"] = s3
        AnisRec["anisotropy_s4"] = s4
        AnisRec["anisotropy_s5"] = s5
        AnisRec["anisotropy_s6"] = s6
        if sigma == 1:
            AnisRec["anisotropy_sigma"] = '%10.8e' % (old_div(
                float(rec[k + 6]), trace))
        AnisRec["anisotropy_unit"] = 'SI'
        AnisRec["anisotropy_tilt_correction"] = coord
        AnisRec["magic_method_codes"] = 'LP-' + type
        AnisRecs.append(AnisRec)
    pmag.magic_write(anisfile, AnisRecs, 'rmag_anisotropy')
    print('data saved in ', anisfile)
示例#38
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
示例#39
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
示例#40
0
def main():
    """
    NAME
        susar4-asc_magic.py

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

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

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

    DEFAULTS
        AFILE: rmag_anisotropy.txt
        RFILE: rmag_results.txt
        SFILE: default is to create new er_specimen.txt file
        USER: ""
        LOC: "unknown"
        INST: ""
        SPEC: 0  sample name is same as site (if SPEC is 1, sample is all but last character)
        appends to  'er_specimens.txt, er_samples.txt, er_sites.txt' files
       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.


    """
    citation = 'This study'
    cont = 0
    samp_con, Z = "1", 1
    AniRecSs,AniRecs,SpecRecs,SampRecs,SiteRecs,MeasRecs=[],[],[],[],[],[]
    user, locname, specfile = "", "unknown", "er_specimens.txt"
    isspec, inst, specnum = '0', "", 0
    spin, new = 1, 0
    dir_path = '.'
    if '-WD' in sys.argv:
        ind = sys.argv.index('-WD')
        dir_path = sys.argv[ind + 1]
    aoutput, routput, moutput = dir_path + '/rmag_anisotropy.txt', dir_path + '/rmag_results.txt', dir_path + '/magic_measurements.txt'
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-usr' in sys.argv:
        ind = sys.argv.index('-usr')
        user = sys.argv[ind + 1]
    if "-ncn" in sys.argv:
        ind = sys.argv.index("-ncn")
        samp_con = sys.argv[ind + 1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print("option [4] must be in form 4-Z where Z is an integer")
                sys.exit()
            else:
                Z = samp_con.split("-")[1]
                samp_con = "4"
        if "7" in samp_con:
            if "-" not in samp_con:
                print("option [7] must be in form 7-Z where Z is an integer")
                sys.exit()
            else:
                Z = samp_con.split("-")[1]
                samp_con = "7"
    if '-k15' in sys.argv: spin = 0
    if '-f' in sys.argv:
        ind = sys.argv.index('-f')
        ascfile = dir_path + '/' + sys.argv[ind + 1]
    if '-F' in sys.argv:
        ind = sys.argv.index('-F')
        moutput = dir_path + '/' + sys.argv[ind + 1]
    if '-Fa' in sys.argv:
        ind = sys.argv.index('-Fa')
        aoutput = dir_path + '/' + sys.argv[ind + 1]
    if '-Fr' in sys.argv:
        ind = sys.argv.index('-Fr')
        routput = dir_path + '/' + sys.argv[ind + 1]
    if '-Fs' in sys.argv:
        ind = sys.argv.index('-Fs')
        specfile = dir_path + '/' + sys.argv[ind + 1]
        isspec = '1'
    elif '-loc' in sys.argv:
        ind = sys.argv.index('-loc')
        locname = sys.argv[ind + 1]
    if '-spc' in sys.argv:
        ind = sys.argv.index('-spc')
        specnum = -(int(sys.argv[ind + 1]))
        if specnum != 0: specnum = -specnum
    if isspec == "1":
        specs, file_type = pmag.magic_read(specfile)
    specnames, sampnames, sitenames = [], [], []
    if '-new' not in sys.argv:  # see if there are already specimen,sample, site files lying around
        try:
            SpecRecs, file_type = pmag.magic_read(dir_path +
                                                  '/er_specimens.txt')
            for spec in SpecRecs:
                if spec['er_specimen_name'] not in specnames:
                    specnames.append(samp['er_specimen_name'])
        except:
            SpecRecs, specs = [], []
        try:
            SampRecs, file_type = pmag.magic_read(dir_path + '/er_samples.txt')
            for samp in SampRecs:
                if samp['er_sample_name'] not in sampnames:
                    sampnames.append(samp['er_sample_name'])
        except:
            sampnames, SampRecs = [], []
        try:
            SiteRecs, file_type = pmag.magic_read(dir_path + '/er_sites.txt')
            for site in SiteRecs:
                if site['er_site_names'] not in sitenames:
                    sitenames.append(site['er_site_name'])
        except:
            sitenames, SiteRecs = [], []
    try:
        input = open(ascfile, 'r')
    except:
        print('Error opening file: ', ascfile)
    Data = input.readlines()
    k = 0
    while k < len(Data):
        line = Data[k]
        words = line.split()
        if "ANISOTROPY" in words:  # first line of data for the spec
            MeasRec, AniRec, SpecRec, SampRec, SiteRec = {}, {}, {}, {}, {}
            specname = words[0]
            AniRec['er_specimen_name'] = specname
            if isspec == "1":
                for spec in specs:
                    if spec['er_specimen_name'] == specname:
                        AniRec['er_sample_name'] = spec['er_sample_name']
                        AniRec['er_site_name'] = spec['er_site_name']
                        AniRec['er_location_name'] = spec['er_location_name']
                        break
            elif isspec == "0":
                if specnum != 0:
                    sampname = specname[:specnum]
                else:
                    sampname = specname
                AniRec['er_sample_name'] = sampname
                SpecRec['er_specimen_name'] = specname
                SpecRec['er_sample_name'] = sampname
                SampRec['er_sample_name'] = sampname
                SiteRec['er_sample_name'] = sampname
                SiteRec['site_description'] = 's'
                AniRec['er_site_name'] = pmag.parse_site(
                    AniRec['er_sample_name'], samp_con, Z)
                SpecRec['er_site_name'] = pmag.parse_site(
                    AniRec['er_sample_name'], samp_con, Z)
                SampRec['er_site_name'] = pmag.parse_site(
                    AniRec['er_sample_name'], samp_con, Z)
                SiteRec['er_site_name'] = pmag.parse_site(
                    AniRec['er_sample_name'], samp_con, Z)
                AniRec['er_location_name'] = locname
                SpecRec['er_location_name'] = locname
                SampRec['er_location_name'] = locname
                SiteRec['er_location_name'] = locname
                AniRec['er_citation_names'] = "This study"
                SpecRec['er_citation_names'] = "This study"
                SampRec['er_citation_names'] = "This study"
                SiteRec['er_citation_names'] = "This study"
            AniRec['er_citation_names'] = "This study"
            AniRec['magic_instrument_codes'] = inst
            AniRec['magic_method_codes'] = "LP-X:AE-H:LP-AN-MS"
            AniRec['magic_experiment_names'] = specname + ":" + "LP-AN-MS"
            AniRec['er_analyst_mail_names'] = user
            for key in list(AniRec.keys()):
                MeasRec[key] = AniRec[key]
            MeasRec['measurement_flag'] = 'g'
            AniRec['anisotropy_flag'] = 'g'
            MeasRec['measurement_standard'] = 'u'
            MeasRec[
                'measurement_description'] = 'Bulk sucsecptibility measurement'
            AniRec['anisotropy_type'] = "AMS"
            AniRec['anisotropy_unit'] = "Normalized by trace"
            if spin == 1:
                AniRec['anisotropy_n'] = "192"
            else:
                AniRec['anisotropy_n'] = "15"
        if 'Azi' in words and isspec == '0':
            SampRec['sample_azimuth'] = words[1]
            labaz = float(words[1])
        if 'Dip' in words:
            SampRec['sample_dip'] = '%7.1f' % (-float(words[1]))
            SpecRec['specimen_vol'] = '%8.3e' % (float(
                words[10]) * 1e-6)  # convert actual volume to m^3 from cm^3
            labdip = float(words[1])
        if 'T1' in words and 'F1' in words:
            k += 2  # read in fourth line down
            line = Data[k]
            rec = line.split()
            dd = rec[1].split('/')
            dip_direction = int(dd[0]) + 90
            SampRec['sample_bed_dip_direction'] = '%i' % (dip_direction)
            SampRec['sample_bed_dip'] = dd[1]
            bed_dip = float(dd[1])
        if "Mean" in words:
            k += 4  # read in fourth line down
            line = Data[k]
            rec = line.split()
            MeasRec['measurement_chi_volume'] = rec[1]
            sigma = .01 * float(rec[2]) / 3.
            AniRec['anisotropy_sigma'] = '%7.4f' % (sigma)
            AniRec['anisotropy_unit'] = 'SI'
        if "factors" in words:
            k += 4  # read in second line down
            line = Data[k]
            rec = line.split()
        if "Specimen" in words:  # first part of specimen data
            AniRec['anisotropy_s1'] = '%7.4f' % (old_div(float(
                words[5]), 3.))  # eigenvalues sum to unity - not 3
            AniRec['anisotropy_s2'] = '%7.4f' % (old_div(float(words[6]), 3.))
            AniRec['anisotropy_s3'] = '%7.4f' % (old_div(float(words[7]), 3.))
            k += 1
            line = Data[k]
            rec = line.split()
            AniRec['anisotropy_s4'] = '%7.4f' % (old_div(float(
                rec[5]), 3.))  # eigenvalues sum to unity - not 3
            AniRec['anisotropy_s5'] = '%7.4f' % (old_div(float(rec[6]), 3.))
            AniRec['anisotropy_s6'] = '%7.4f' % (old_div(float(rec[7]), 3.))
            AniRec['anisotropy_tilt_correction'] = '-1'
            AniRecs.append(AniRec)
            AniRecG, AniRecT = {}, {}
            for key in list(AniRec.keys()):
                AniRecG[key] = AniRec[key]
            for key in list(AniRec.keys()):
                AniRecT[key] = AniRec[key]
            sbar = []
            sbar.append(float(AniRec['anisotropy_s1']))
            sbar.append(float(AniRec['anisotropy_s2']))
            sbar.append(float(AniRec['anisotropy_s3']))
            sbar.append(float(AniRec['anisotropy_s4']))
            sbar.append(float(AniRec['anisotropy_s5']))
            sbar.append(float(AniRec['anisotropy_s6']))
            sbarg = pmag.dosgeo(sbar, labaz, labdip)
            AniRecG["anisotropy_s1"] = '%12.10f' % (sbarg[0])
            AniRecG["anisotropy_s2"] = '%12.10f' % (sbarg[1])
            AniRecG["anisotropy_s3"] = '%12.10f' % (sbarg[2])
            AniRecG["anisotropy_s4"] = '%12.10f' % (sbarg[3])
            AniRecG["anisotropy_s5"] = '%12.10f' % (sbarg[4])
            AniRecG["anisotropy_s6"] = '%12.10f' % (sbarg[5])
            AniRecG["anisotropy_tilt_correction"] = '0'
            AniRecs.append(AniRecG)
            if bed_dip != "" and bed_dip != 0:  # have tilt correction
                sbart = pmag.dostilt(sbarg, dip_direction, bed_dip)
                AniRecT["anisotropy_s1"] = '%12.10f' % (sbart[0])
                AniRecT["anisotropy_s2"] = '%12.10f' % (sbart[1])
                AniRecT["anisotropy_s3"] = '%12.10f' % (sbart[2])
                AniRecT["anisotropy_s4"] = '%12.10f' % (sbart[3])
                AniRecT["anisotropy_s5"] = '%12.10f' % (sbart[4])
                AniRecT["anisotropy_s6"] = '%12.10f' % (sbart[5])
                AniRecT["anisotropy_tilt_correction"] = '100'
                AniRecs.append(AniRecT)
            MeasRecs.append(MeasRec)
            if SpecRec['er_specimen_name'] not in specnames:
                SpecRecs.append(SpecRec)
                specnames.append(SpecRec['er_specimen_name'])
            if SampRec['er_sample_name'] not in sampnames:
                SampRecs.append(SampRec)
                sampnames.append(SampRec['er_sample_name'])
            if SiteRec['er_site_name'] not in sitenames:
                SiteRecs.append(SiteRec)
                sitenames.append(SiteRec['er_site_name'])
        k += 1  # skip to next specimen
    pmag.magic_write(aoutput, AniRecs, 'rmag_anisotropy')
    print("anisotropy tensors put in ", aoutput)
    pmag.magic_write(moutput, MeasRecs, 'magic_measurements')
    print("bulk measurements put in ", moutput)
    if isspec == "0":
        SpecOut, keys = pmag.fillkeys(SpecRecs)
        output = dir_path + "/er_specimens.txt"
        pmag.magic_write(output, SpecOut, 'er_specimens')
        print("specimen info put in ", output)
        output = dir_path + "/er_samples.txt"
        SampOut, keys = pmag.fillkeys(SampRecs)
        pmag.magic_write(output, SampOut, 'er_samples')
        print("sample info put in ", output)
        output = dir_path + "/er_sites.txt"
        SiteOut, keys = pmag.fillkeys(SiteRecs)
        pmag.magic_write(output, SiteOut, 'er_sites')
        print("site info put in ", output)
    print(""""
         You can now import your data into the Magic Console and complete data entry,
         for example the site locations, lithologies, etc. plotting can be done with aniso_magic.py
    """)
示例#41
0
def main():
    """
    NAME
        agm_magic.py

    DESCRIPTION
        converts Micromag agm files to magic format

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

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

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

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

    print("results put in ", output)
示例#42
0
def convert(**kwargs):

    # initialize some stuff
    version_num = pmag.get_version()
    MeasRecs, SpecRecs, SampRecs, SiteRecs, LocRecs = [], [], [], [], []

    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', 'measurements.txt')
    mag_file = kwargs.get('mag_file')
    spec_file = kwargs.get('spec_file', 'specimens.txt')  # specimen outfile
    samp_file = kwargs.get('samp_file', 'samples.txt')
    site_file = kwargs.get('site_file', 'sites.txt')  # site outfile
    loc_file = kwargs.get('loc_file', 'locations.txt')  # site outfile
    location = kwargs.get('location', 'unknown')
    dmy_flag = kwargs.get('dmy_flag', False)
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')
    #oave = kwargs.get('noave', 0) # default (0) means DO average
    meth_code = kwargs.get('meth_code', "LP-NO")
    specnum = -int(kwargs.get('specnum', 0))
    samp_con = kwargs.get('samp_con', '2')
    if "4" in samp_con:
        if "-" not in samp_con:
            print("option [4] must be in form 4-Z where Z is an integer")
            return False, "naming convention option [4] must be in form 4-Z where Z is an integer"
        else:
            site_num = samp_con.split("-")[1]
            samp_con = "4"
    elif "7" in samp_con:
        if "-" not in samp_con:
            print("option [7] must be in form 7-Z where Z is an integer")
            return False, "naming convention option [7] must be in form 7-Z where Z is an integer"
        else:
            site_num = samp_con.split("-")[1]
            samp_con = "7"
    else:
        site_num = 1
    try:
        DC_FIELD = float(kwargs.get('labfield', 0)) * 1e-6
        DC_PHI = float(kwargs.get('phi', 0))
        DC_THETA = float(kwargs.get('theta', 0))
    except ValueError:
        raise ValueError(
            'problem with your dc parameters. please provide a labfield in microTesla and a phi and theta in degrees.'
        )
    noave = kwargs.get('noave', False)
    dmy_flag = kwargs.get('dmy_flag', False)
    meas_n_orient = kwargs.get('meas_n_orient', '8')

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

    # parse data

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

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

        # Configure et er_ tables
        if specnum == 0: sample_name = spec_name
        else: sample_name = spec_name[:specnum]
        site = pmag.parse_site(sample_name, samp_con, site_num)
        SpecRec['specimen'] = spec_name
        SpecRec['sample'] = sample_name
        if volume != 0: SpecRec['volume'] = volume
        SpecRecs.append(SpecRec)
        if sample_name != "" and sample_name not in [
                x['sample'] if 'sample' in list(x.keys()) else ""
                for x in SampRecs
        ]:
            SampRec['sample'] = sample_name
            SampRec['azimuth'] = dec
            SampRec['dip'] = str(float(inc) - 90)
            SampRec['bed_dip_direction'] = bed_plane
            SampRec['bed_dip'] = bed_dip
            SampRec['method_codes'] = meth_code
            SampRec['site'] = site
            SampRecs.append(SampRec)
        if site != "" and site not in [
                x['site'] if 'site' in list(x.keys()) else "" for x in SiteRecs
        ]:
            SiteRec['site'] = site
            SiteRec['location'] = location
            SiteRec['lat'] = lat
            SiteRec['lon'] = lon
            SiteRecs.append(SiteRec)
        if location != "" and location not in [
                x['location'] if 'location' in list(x.keys()) else ""
                for x in LocRecs
        ]:
            LocRec['location'] = location
            LocRec['lat_n'] = lat
            LocRec['lon_e'] = lon
            LocRec['lat_s'] = lat
            LocRec['lon_w'] = lon
            LocRecs.append(LocRec)

        #measurement data
        line = data.readline()
        line = line.rstrip("\n")
        items = line.split(",")
        while line != '9999':
            step = items[0]
            step = step.split('.')
            step_value = step[0]
            step_type = ""
            if len(step) == 2:
                step_type = step[1]
            if step_type == '5':
                step_value = items[0]
            A = float(items[1])
            B = float(items[2])
            C = float(items[3])
            #  convert to MagIC coordinates
            Z = -A
            X = -B
            Y = C
            cart = array([X, Y, Z]).transpose()
            direction = pmag.cart2dir(cart).transpose()
            measurement_dec = direction[0]
            measurement_inc = direction[1]
            magn_moment = direction[
                2] * 1.0e-12  # the data are in pico-Am^2 - this converts to Am^2
            if volume != 0:
                magn_volume = direction[
                    2] * 1.0e-12 / volume  # data volume normalized - converted to A/m
#            print("magn_moment=", magn_moment)
#            print("magn_volume=", magn_volume)
            error = items[4]
            date = items[5]
            date = date.strip('"').replace(' ', '')
            if date.count("-") > 0:
                date = date.split("-")
            elif date.count("/") > 0:
                date = date.split("/")
            else:
                print("date format seperator cannot be identified")
            #            print(date)
            time = items[6]
            time = time.strip('"').replace(' ', '')
            time = time.split(":")
            #            print(time)
            dt = date[0] + ":" + date[1] + ":" + date[2] + ":" + time[
                0] + ":" + time[1] + ":" + "0"
            local = pytz.timezone("Europe/Amsterdam")
            try:
                if dmy_flag:
                    naive = datetime.datetime.strptime(dt, "%d:%m:%Y:%H:%M:%S")
                else:
                    naive = datetime.datetime.strptime(dt, "%m:%d:%Y:%H:%M:%S")
            except ValueError:
                naive = datetime.datetime.strptime(dt, "%Y:%m:%d:%H:%M:%S")
            local_dt = local.localize(naive, is_dst=None)
            utc_dt = local_dt.astimezone(pytz.utc)
            timestamp = utc_dt.strftime("%Y-%m-%dT%H:%M:%S") + "Z"
            #            print(timestamp)

            MeasRec = {}
            MeasRec["timestamp"] = timestamp
            MeasRec["analysts"] = operator
            MeasRec["instrument_codes"] = "Utrecht_" + machine
            MeasRec["description"] = "free string = " + free_string
            MeasRec["citations"] = "This study"
            MeasRec['software_packages'] = version_num
            MeasRec["meas_temp"] = '%8.3e' % (273)  # room temp in kelvin
            MeasRec["quality"] = 'g'
            MeasRec["standard"] = 'u'
            MeasRec["experiments"] = location + site + spec_name
            MeasRec["treat_step_num"] = location + site + spec_name + items[0]
            MeasRec["specimen"] = spec_name
            # MeasRec["treat_ac_field"] = '0'
            if AF_or_T.lower() == "th":
                MeasRec["treat_temp"] = '%8.3e' % (float(step_value) + 273.
                                                   )  # temp in kelvin
                MeasRec['treat_ac_field'] = '0'
                lab_treat_type = "T"
            else:
                MeasRec['treat_temp'] = '273'
                MeasRec['treat_ac_field'] = '%10.3e' % (float(step_value) *
                                                        1e-3)
                lab_treat_type = "AF"
            MeasRec['treat_dc_field'] = '0'
            if step_value == '0':
                meas_type = "LT-NO"
#            print("step_type=", step_type)
            if step_type == '0' or step_type == '00':
                meas_type = "LT-%s-Z" % lab_treat_type
            elif step_type == '1' or step_type == '11':
                meas_type = "LT-%s-I" % lab_treat_type
                MeasRec['treat_dc_field'] = '%1.2e' % DC_FIELD
            elif step_type == '2' or step_type == '12':
                meas_type = "LT-PTRM-I"
                MeasRec['treat_dc_field'] = '%1.2e' % DC_FIELD
            elif step_type == '3' or step_type == '13':
                meas_type = "LT-PTRM-Z"


#            print("meas_type=", meas_type)
            MeasRec['treat_dc_field_phi'] = '%1.2f' % DC_PHI
            MeasRec['treat_dc_field_theta'] = '%1.2f' % DC_THETA
            MeasRec['method_codes'] = meas_type
            MeasRec["magn_moment"] = magn_moment
            if volume != 0: MeasRec["magn_volume"] = magn_volume
            MeasRec["dir_dec"] = measurement_dec
            MeasRec["dir_inc"] = measurement_inc
            MeasRec['dir_csd'] = error
            MeasRec['meas_n_orient'] = meas_n_orient
            #            print(MeasRec)
            MeasRecs.append(MeasRec)

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

    data.close()

    con = nb.Contribution(output_dir_path, read_tables=[])

    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts = pmag.measurements_methods3(
        MeasRecs, noave)  #figures out method codes for measuremet data
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    con.tables['specimens'].write_magic_file(custom_name=spec_file)
    con.tables['samples'].write_magic_file(custom_name=samp_file)
    con.tables['sites'].write_magic_file(custom_name=site_file)
    con.tables['locations'].write_magic_file(custom_name=loc_file)
    con.tables['measurements'].write_magic_file(custom_name=meas_file)

    return True, meas_file
示例#43
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
示例#44
0
def main(**kwargs):

    version_num = pmag.get_version()
    dir_path = kwargs.get('dir_path', '.')
    input_dir_path = kwargs.get('input_dir_path', dir_path)
    output_dir_path = 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
    mag_file = kwargs.get('mag_file')
    specnum = kwargs.get('specnum', 1)
    samp_con = kwargs.get('samp_con', '1')
    location = kwargs.get('location', 'unknown')
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')
    noave = kwargs.get('noave', 0)  # default (0) means DO average
    meth_code = kwargs.get('meth_code', "LP-NO")
    volume = float(kwargs.get('volume', 2.5)) * 1e-6
    JR = kwargs.get('JR', False)
    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
    tmp_file = mag_file.split(os.extsep)[0] + os.extsep + 'tmp'
    mag_file = os.path.join(input_dir_path, mag_file)
    if specnum != 0: specnum = -int(specnum)
    if samp_con.startswith("4"):
        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 samp_con.startswith("7"):
        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"
    else:
        Z = 1

    # 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')
    if samp_con != '2': fixed_data = pre_data.read().replace('-', ' -')
    else:
        fixed_data = ""
        for line in pre_data.readlines():
            entries = line.split()
            if len(entries) < 2: continue
            fixed_line = entries[0] + ' ' + reduce(
                lambda x, y: x + ' ' + y,
                [x.replace('-', ' -') for x in entries[1:]])
            fixed_data += fixed_line + os.linesep
    tmp_data.write(fixed_data)
    tmp_data.close()
    pre_data.close()

    if not JR:
        column_names = [
            'specimen', 'step', 'x', 'y', 'z', 'expon', 'azimuth', 'dip',
            'bed_dip_direction', 'bed_dip', 'bed_dip_dir2', 'bed_dip2',
            'param1', 'param2', 'param3', 'param4', 'dir_csd'
        ]
    else:  # measured on the Joides Resolution JR6
        column_names = [
            'specimen', 'step', 'negz', 'y', 'x', 'expon', 'azimuth', 'dip',
            'bed_dip_direction', 'bed_dip', 'bed_dip_dir2', 'bed_dip2',
            'param1', 'param2', 'param3', 'param4', 'dir_csd'
        ]
    data = pd.read_csv(tmp_file,
                       delim_whitespace=True,
                       names=column_names,
                       index_col=False)
    if isinstance(data['x'][0], str):
        column_names = [
            'specimen', 'step', 'step_unit', 'x', 'y', 'z', 'expon', 'azimuth',
            'dip', 'bed_dip_direction', 'bed_dip', 'bed_dip_dir2', 'bed_dip2',
            'param1', 'param2', 'param3', 'param4', 'dir_csd'
        ]
        data = pd.read_csv(tmp_file,
                           delim_whitespace=True,
                           names=column_names,
                           index_col=False)
    if JR: data['z'] = -data['negz']
    cart = np.array([data['x'], data['y'], data['z']]).transpose()
    dir_dat = pmag.cart2dir(cart).transpose()
    data['dir_dec'] = dir_dat[0]
    data['dir_inc'] = dir_dat[1]
    data['magn_moment'] = dir_dat[2] * (
        10.0**
        data['expon']) * volume  # the data are in A/m - this converts to Am^2
    data['magn_volume'] = dir_dat[2] * (10.0**data['expon']
                                        )  # A/m  - data in A/m
    data['dip'] = -data['dip']

    data['specimen']
    # put data into magic tables
    MagRecs, SpecRecs, SampRecs, SiteRecs, LocRecs = [], [], [], [], []
    for rowNum, row in data.iterrows():
        MeasRec, SpecRec, SampRec, SiteRec, LocRec = {}, {}, {}, {}, {}
        specimen = row['specimen']
        if specnum != 0: sample = specimen[:specnum]
        else: sample = specimen
        site = pmag.parse_site(sample, samp_con, Z)
        if specimen != "" and specimen not in [
                x['specimen'] if 'specimen' in list(x.keys()) else ""
                for x in SpecRecs
        ]:
            SpecRec['specimen'] = specimen
            SpecRec['sample'] = sample
            SpecRec["citations"] = "This study"
            SpecRec["analysts"] = user
            SpecRec['volume'] = volume
            SpecRecs.append(SpecRec)
        if sample != "" and sample not in [
                x['sample'] if 'sample' in list(x.keys()) else ""
                for x in SampRecs
        ]:
            SampRec['sample'] = sample
            SampRec['site'] = site
            SampRec["citations"] = "This study"
            SampRec["analysts"] = user
            SampRec['azimuth'] = row['azimuth']
            SampRec['dip'] = row['dip']
            SampRec['bed_dip_direction'] = row['bed_dip_direction']
            SampRec['bed_dip'] = row['bed_dip']
            SampRec['method_codes'] = meth_code
            SampRecs.append(SampRec)
        if site != "" and site not in [
                x['site'] if 'site' in list(x.keys()) else "" for x in SiteRecs
        ]:
            SiteRec['site'] = site
            SiteRec['location'] = location
            SiteRec["citations"] = "This study"
            SiteRec["analysts"] = user
            SiteRec['lat'] = lat
            SiteRec['lon'] = lon
            SiteRecs.append(SiteRec)
        if location != "" and location not in [
                x['location'] if 'location' in list(x.keys()) else ""
                for x in LocRecs
        ]:
            LocRec['location'] = location
            LocRec["citations"] = "This study"
            LocRec["analysts"] = user
            LocRec['lat_n'] = lat
            LocRec['lon_e'] = lon
            LocRec['lat_s'] = lat
            LocRec['lon_w'] = lon
            LocRecs.append(LocRec)
        MeasRec["citations"] = "This study"
        MeasRec["analysts"] = user
        MeasRec["specimen"] = specimen
        MeasRec['software_packages'] = version_num
        MeasRec["treat_temp"] = '%8.3e' % (273)  # room temp in kelvin
        MeasRec["meas_temp"] = '%8.3e' % (273)  # room temp in kelvin
        MeasRec["quality"] = 'g'
        MeasRec["standard"] = 'u'
        MeasRec["treat_step_num"] = '1'
        MeasRec["treat_ac_field"] = '0'
        if row['step'] == 'NRM':
            meas_type = "LT-NO"
        elif 'step_unit' in row and row['step_unit'] == 'C':
            meas_type = "LT-T-Z"
            treat = float(row['step'])
            MeasRec["treat_temp"] = '%8.3e' % (treat + 273.)  # temp in kelvin
        elif row['step'][0:2] == 'AD':
            meas_type = "LT-AF-Z"
            treat = float(row['step'][2:])
            MeasRec["treat_ac_field"] = '%8.3e' % (
                treat * 1e-3)  # convert from mT to tesla
        elif row['step'][0] == 'A':
            meas_type = "LT-AF-Z"
            treat = float(row['step'][1:])
            MeasRec["treat_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:])
            MeasRec["treat_temp"] = '%8.3e' % (treat + 273.)  # temp in kelvin
        elif row['step'][0] == 'T':
            meas_type = "LT-T-Z"
            treat = float(row['step'][1:])
            MeasRec["treat_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"
        MeasRec["magn_moment"] = str(row['magn_moment'])
        MeasRec["magn_volume"] = str(row['magn_volume'])
        MeasRec["dir_dec"] = str(row['dir_dec'])
        MeasRec["dir_inc"] = str(row['dir_inc'])
        MeasRec['method_codes'] = meas_type
        MagRecs.append(MeasRec)

    con = nb.Contribution(output_dir_path, read_tables=[])

    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts = pmag.measurements_methods3(MeasRecs, noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    con.tables['specimens'].write_magic_file(custom_name=spec_file)
    con.tables['samples'].write_magic_file(custom_name=samp_file)
    con.tables['sites'].write_magic_file(custom_name=site_file)
    con.tables['locations'].write_magic_file(custom_name=loc_file)
    con.tables['measurements'].write_magic_file(custom_name=meas_file)

    try:
        os.remove(tmp_file)
    except (OSError, IOError) as e:
        print("couldn't remove temperary fixed JR6 file %s" % tmp_file)

    return True, meas_file
示例#45
0
def main():
    """
    NAME
        huji_sample_magic.py
   
    DESCRIPTION
        takes tab delimited Hebrew University sample file and converts to MagIC formatted tables
 
    SYNTAX
        huji_sample_magic.py [command line options]

    OPTIONS
        -f FILE: specify input file
        -Fsa FILE: specify sample output file, default is: er_samples.txt 
        -Fsi FILE: specify site output file, default is: er_sites.txt 
        -Iso:  import sample orientation info - default is to set sample_az/dip to 0,0
        -ncn NCON:  specify naming convention: default is #1 below
        -mcd: specify sampling method codes as a colon delimited string:  [default is: FS-FD:SO-POM]
             FS-FD field sampling done with a drill
             FS-H field sampling done with hand samples
             FS-LOC-GPS  field location done with GPS
             FS-LOC-MAP  field location done with map
             SO-POM   a Pomeroy orientation device was used
             SO-ASC   an ASC orientation device was used
             SO-MAG   orientation with magnetic compass
        -loc: location name, default="unknown"

    INPUT FORMAT
        Input files must be tab delimited:
            Samp  Az Dip Dip_dir Dip 
        Orientation convention:
             Lab arrow azimuth = mag_azimuth; Lab arrow dip = 90-field_dip
                e.g. field_dip is degrees from horizontal of drill direction
       
         Magnetic declination convention:
             Az is already corrected in file 
    
         Sample naming convention:
            [1] XXXXY: where XXXX is an arbitrary length site designation and Y
                is the single character sample designation.  e.g., TG001a is the
                first sample from site TG001.    [default]
            [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
            [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
            [5] site name = sample name
            [6] site name entered in site_name column in the orient.txt format input file  -- NOT CURRENTLY SUPPORTED
            [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
            NB: all others you will have to either customize your
                self or e-mail [email protected] for help.

    OUTPUT
            output saved in er_samples.txt  will overwrite any existing files 
    """
    #
    # initialize variables
    #
    version_num = pmag.get_version()
    samp_file, or_con, corr = "er_samples.txt", "1", "1"
    site_file = 'er_sites.txt'
    args = sys.argv
    date, lat, lon = "", "", ""  # date of sampling, latitude (pos North), longitude (pos East)
    bed_dip, bed_dip_dir = "", ""
    participantlist = ""
    sites = []  # list of site names
    Lats, Lons = [], []  # list of latitudes and longitudes
    SampRecs, SiteRecs, ImageRecs, imagelist = [], [], [], [
    ]  # lists of Sample records and Site records
    samp_con, Z, average_bedding = "1", 1, "0"
    newbaseline, newbeddir, newbeddip = "", "", ""
    meths = 'FS-FD:SO-POM:SO-SUN'
    delta_u = "0"
    sclass, lithology, type = "", "", ""
    newclass, newlith, newtype = '', '', ''
    user = ""
    or_con = '3'
    corr == "3"
    DecCorr = 0.
    location_name = "unknown"
    ignore = 1
    #
    #
    if "-h" in args:
        print(main.__doc__)
        sys.exit()
    if "-f" in args:
        ind = args.index("-f")
        orient_file = sys.argv[ind + 1]
    else:
        "Must have orientation file name"
        sys.exit()
    if "-Fsa" in args:
        ind = args.index("-Fsa")
        samp_file = sys.argv[ind + 1]
    if "-ncn" in args:
        ind = args.index("-ncn")
        samp_con = sys.argv[ind + 1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print("option [4] must be in form 3-Z where Z is an integer")
                sys.exit()
            else:
                Z = samp_con.split("-")[1]
                samp_con = "4"
            print(samp_con, Z)
    if "-mcd" in args:
        ind = args.index("-mcd")
        meths = (sys.argv[ind + 1])
    if "-loc" in args:
        ind = args.index("-loc")
        location_name = (sys.argv[ind + 1])
    if "-Iso" in args: ignore = 0
    #
    # read in file to convert
    #
    azfile = open(orient_file, 'r')
    AzDipDat = azfile.readlines()
    azfile.close()
    SampOut = []
    SiteOut = []
    for line in AzDipDat[1:]:
        orec = line.split()
        if len(orec) > 1:
            labaz, labdip = pmag.orient(float(orec[1]), float(orec[2]), or_con)
            bed_dip_dir = (orec[3])
            bed_dip = (orec[4])
            SampRec = {}
            SiteRec = {}
            SampRec["er_location_name"] = location_name
            SampRec["er_citation_names"] = "This study"
            SiteRec["er_location_name"] = location_name
            SiteRec["er_citation_names"] = "This study"
            SiteRec["site_class"] = ""
            SiteRec["site_lithology"] = ""
            SiteRec["site_type"] = ""
            SiteRec["site_definition"] = "s"
            SiteRec["er_citation_names"] = "This study"
            #
            # parse information common to all orientation methods
            #
            SampRec["er_sample_name"] = orec[0]
            SampRec["sample_bed_dip_direction"] = orec[3]
            SampRec["sample_bed_dip"] = orec[4]
            SiteRec["site_bed_dip_direction"] = orec[3]
            SiteRec["site_bed_dip"] = orec[4]
            if ignore == 0:
                SampRec["sample_dip"] = '%7.1f' % (labdip)
                SampRec["sample_azimuth"] = '%7.1f' % (labaz)
            else:
                SampRec["sample_dip"] = '0'
                SampRec["sample_azimuth"] = '0'
            SampRec["sample_lat"] = orec[5]
            SampRec["sample_lon"] = orec[6]
            SiteRec["site_lat"] = orec[5]
            SiteRec["site_lon"] = orec[6]
            methods = meths.split(":")
            SampRec["magic_method_codes"] = meths
            site = pmag.parse_site(orec[0], samp_con,
                                   Z)  # parse out the site name
            SampRec["er_site_name"] = site
            SampRec['magic_software_packages'] = version_num
            SiteRec["er_site_name"] = site
            SiteRec['magic_software_packages'] = version_num
            SampOut.append(SampRec)
            SiteOut.append(SiteRec)
    pmag.magic_write(samp_file, SampOut, "er_samples")
    print("Sample info saved in ", samp_file)
    pmag.magic_write(site_file, SiteOut, "er_sites")
    print("Site info saved in ", site_file)
示例#46
0
def main():
    """
    NAME
        susar4-asc_magic.py

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

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

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

    DEFAULTS
        AFILE: rmag_anisotropy.txt
        RFILE: rmag_results.txt
        SFILE: default is to create new er_specimen.txt file
        USER: ""
        LOC: "unknown"
        INST: ""
        SPEC: 0  sample name is same as site (if SPEC is 1, sample is all but last character)
        appends to  'er_specimens.txt, er_samples.txt, er_sites.txt' files
       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.


    """
    citation='This study'
    cont=0
    samp_con,Z="1",1
    AniRecSs,AniRecs,SpecRecs,SampRecs,SiteRecs,MeasRecs=[],[],[],[],[],[]
    user,locname,specfile="","unknown","er_specimens.txt"
    isspec,inst,specnum='0',"",0
    spin,new=1,0
    dir_path='.'
    if '-WD' in sys.argv:
        ind=sys.argv.index('-WD')
        dir_path=sys.argv[ind+1]
    aoutput,routput,moutput=dir_path+'/rmag_anisotropy.txt',dir_path+'/rmag_results.txt',dir_path+'/magic_measurements.txt'
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-usr' in sys.argv:
        ind=sys.argv.index('-usr')
        user=sys.argv[ind+1]
    if "-ncn" in sys.argv:
        ind=sys.argv.index("-ncn")
        samp_con=sys.argv[ind+1]
        if "4" in samp_con:
            if "-" not in samp_con:
                print("option [4] must be in form 4-Z where Z is an integer")
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="4"
        if "7" in samp_con:
            if "-" not in samp_con:
                print("option [7] must be in form 7-Z where Z is an integer")
                sys.exit()
            else:
                Z=samp_con.split("-")[1]
                samp_con="7"
    if '-k15' in sys.argv:spin=0
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        ascfile=dir_path+'/'+sys.argv[ind+1]
    if '-F' in sys.argv:
        ind=sys.argv.index('-F')
        moutput=dir_path+'/'+sys.argv[ind+1]
    if '-Fa' in sys.argv:
        ind=sys.argv.index('-Fa')
        aoutput=dir_path+'/'+sys.argv[ind+1]
    if '-Fr' in sys.argv:
        ind=sys.argv.index('-Fr')
        routput=dir_path+'/'+sys.argv[ind+1]
    if '-Fs' in sys.argv:
        ind=sys.argv.index('-Fs')
        specfile=dir_path+'/'+sys.argv[ind+1]
        isspec='1'
    elif '-loc' in sys.argv:
        ind=sys.argv.index('-loc')
        locname=sys.argv[ind+1]
    if '-spc' in sys.argv:
        ind=sys.argv.index('-spc')
        specnum=-(int(sys.argv[ind+1]))
        if specnum!=0:specnum=-specnum
    if isspec=="1":
        specs,file_type=pmag.magic_read(specfile)
    specnames,sampnames,sitenames=[],[],[]
    if '-new' not in sys.argv: # see if there are already specimen,sample, site files lying around
        try:
            SpecRecs,file_type=pmag.magic_read(dir_path+'/er_specimens.txt')
            for spec in SpecRecs:
                if spec['er_specimen_name'] not in specnames:specnames.append(samp['er_specimen_name'])
        except:
            SpecRecs,specs=[],[]
        try:
            SampRecs,file_type=pmag.magic_read(dir_path+'/er_samples.txt')
            for samp in SampRecs:
                if samp['er_sample_name'] not in sampnames:sampnames.append(samp['er_sample_name'])
        except:
            sampnames,SampRecs=[],[]
        try:
            SiteRecs,file_type=pmag.magic_read(dir_path+'/er_sites.txt')
            for site in SiteRecs:
                if site['er_site_names'] not in sitenames:sitenames.append(site['er_site_name'])
        except:
            sitenames,SiteRecs=[],[]
    try:
        input=open(ascfile,'r')
    except:
        print('Error opening file: ', ascfile)
    Data=input.readlines()
    k=0
    while k<len(Data):
        line = Data[k]
        words=line.split()
        if "ANISOTROPY" in words: # first line of data for the spec
            MeasRec,AniRec,SpecRec,SampRec,SiteRec={},{},{},{},{}
            specname=words[0]
            AniRec['er_specimen_name']=specname
            if isspec=="1":
                for spec in specs:
                    if spec['er_specimen_name']==specname:
                        AniRec['er_sample_name']=spec['er_sample_name']
                        AniRec['er_site_name']=spec['er_site_name']
                        AniRec['er_location_name']=spec['er_location_name']
                        break
            elif isspec=="0":
                if specnum!=0:
                    sampname=specname[:specnum]
                else:
                    sampname=specname
                AniRec['er_sample_name']=sampname
                SpecRec['er_specimen_name']=specname
                SpecRec['er_sample_name']=sampname
                SampRec['er_sample_name']=sampname
                SiteRec['er_sample_name']=sampname
                SiteRec['site_description']='s'
                AniRec['er_site_name']=pmag.parse_site(AniRec['er_sample_name'],samp_con,Z)
                SpecRec['er_site_name']=pmag.parse_site(AniRec['er_sample_name'],samp_con,Z)
                SampRec['er_site_name']=pmag.parse_site(AniRec['er_sample_name'],samp_con,Z)
                SiteRec['er_site_name']=pmag.parse_site(AniRec['er_sample_name'],samp_con,Z)
                AniRec['er_location_name']=locname
                SpecRec['er_location_name']=locname
                SampRec['er_location_name']=locname
                SiteRec['er_location_name']=locname
                AniRec['er_citation_names']="This study"
                SpecRec['er_citation_names']="This study"
                SampRec['er_citation_names']="This study"
                SiteRec['er_citation_names']="This study"
            AniRec['er_citation_names']="This study"
            AniRec['magic_instrument_codes']=inst
            AniRec['magic_method_codes']="LP-X:AE-H:LP-AN-MS"
            AniRec['magic_experiment_names']=specname+":"+"LP-AN-MS"
            AniRec['er_analyst_mail_names']=user
            for key in list(AniRec.keys()):MeasRec[key]=AniRec[key]
            MeasRec['measurement_flag']='g'
            AniRec['anisotropy_flag']='g'
            MeasRec['measurement_standard']='u'
            MeasRec['measurement_description']='Bulk sucsecptibility measurement'
            AniRec['anisotropy_type']="AMS"
            AniRec['anisotropy_unit']="Normalized by trace"
            if spin==1:
                AniRec['anisotropy_n']="192"
            else:
                AniRec['anisotropy_n']="15"
        if 'Azi' in words and isspec=='0':
            SampRec['sample_azimuth']=words[1]
            labaz=float(words[1])
        if 'Dip' in words:
            SampRec['sample_dip']='%7.1f'%(-float(words[1]))
            SpecRec['specimen_vol']='%8.3e'%(float(words[10])*1e-6) # convert actual volume to m^3 from cm^3
            labdip=float(words[1])
        if 'T1' in words and 'F1' in words:
            k+=2 # read in fourth line down
            line=Data[k]
            rec=line.split()
            dd=rec[1].split('/')
            dip_direction=int(dd[0])+90
            SampRec['sample_bed_dip_direction']='%i'%(dip_direction)
            SampRec['sample_bed_dip']=dd[1]
            bed_dip=float(dd[1])
        if "Mean" in words:
            k+=4 # read in fourth line down
            line=Data[k]
            rec=line.split()
            MeasRec['measurement_chi_volume']=rec[1]
            sigma=.01*float(rec[2])/3.
            AniRec['anisotropy_sigma']='%7.4f'%(sigma)
            AniRec['anisotropy_unit']='SI'
        if "factors" in words:
            k+=4 # read in second line down
            line=Data[k]
            rec=line.split()
        if "Specimen" in words:  # first part of specimen data
            AniRec['anisotropy_s1']='%7.4f'%(old_div(float(words[5]),3.)) # eigenvalues sum to unity - not 3
            AniRec['anisotropy_s2']='%7.4f'%(old_div(float(words[6]),3.))
            AniRec['anisotropy_s3']='%7.4f'%(old_div(float(words[7]),3.))
            k+=1
            line=Data[k]
            rec=line.split()
            AniRec['anisotropy_s4']='%7.4f'%(old_div(float(rec[5]),3.)) # eigenvalues sum to unity - not 3
            AniRec['anisotropy_s5']='%7.4f'%(old_div(float(rec[6]),3.))
            AniRec['anisotropy_s6']='%7.4f'%(old_div(float(rec[7]),3.))
            AniRec['anisotropy_tilt_correction']='-1'
            AniRecs.append(AniRec)
            AniRecG,AniRecT={},{}
            for key in list(AniRec.keys()):AniRecG[key]=AniRec[key]
            for key in list(AniRec.keys()):AniRecT[key]=AniRec[key]
            sbar=[]
            sbar.append(float(AniRec['anisotropy_s1']))
            sbar.append(float(AniRec['anisotropy_s2']))
            sbar.append(float(AniRec['anisotropy_s3']))
            sbar.append(float(AniRec['anisotropy_s4']))
            sbar.append(float(AniRec['anisotropy_s5']))
            sbar.append(float(AniRec['anisotropy_s6']))
            sbarg=pmag.dosgeo(sbar,labaz,labdip)
            AniRecG["anisotropy_s1"]='%12.10f'%(sbarg[0])
            AniRecG["anisotropy_s2"]='%12.10f'%(sbarg[1])
            AniRecG["anisotropy_s3"]='%12.10f'%(sbarg[2])
            AniRecG["anisotropy_s4"]='%12.10f'%(sbarg[3])
            AniRecG["anisotropy_s5"]='%12.10f'%(sbarg[4])
            AniRecG["anisotropy_s6"]='%12.10f'%(sbarg[5])
            AniRecG["anisotropy_tilt_correction"]='0'
            AniRecs.append(AniRecG)
            if bed_dip!="" and bed_dip!=0: # have tilt correction
                sbart=pmag.dostilt(sbarg,dip_direction,bed_dip)
                AniRecT["anisotropy_s1"]='%12.10f'%(sbart[0])
                AniRecT["anisotropy_s2"]='%12.10f'%(sbart[1])
                AniRecT["anisotropy_s3"]='%12.10f'%(sbart[2])
                AniRecT["anisotropy_s4"]='%12.10f'%(sbart[3])
                AniRecT["anisotropy_s5"]='%12.10f'%(sbart[4])
                AniRecT["anisotropy_s6"]='%12.10f'%(sbart[5])
                AniRecT["anisotropy_tilt_correction"]='100'
                AniRecs.append(AniRecT)
            MeasRecs.append(MeasRec)
            if SpecRec['er_specimen_name'] not in specnames:
                SpecRecs.append(SpecRec)
                specnames.append(SpecRec['er_specimen_name'])
            if SampRec['er_sample_name'] not in sampnames:
                SampRecs.append(SampRec)
                sampnames.append(SampRec['er_sample_name'])
            if SiteRec['er_site_name'] not in sitenames:
                SiteRecs.append(SiteRec)
                sitenames.append(SiteRec['er_site_name'])
        k+=1 # skip to next specimen
    pmag.magic_write(aoutput,AniRecs,'rmag_anisotropy')
    print("anisotropy tensors put in ",aoutput)
    pmag.magic_write(moutput,MeasRecs,'magic_measurements')
    print("bulk measurements put in ",moutput)
    if isspec=="0":
        SpecOut,keys=pmag.fillkeys(SpecRecs)
        output=dir_path+"/er_specimens.txt"
        pmag.magic_write(output,SpecOut,'er_specimens')
        print("specimen info put in ",output)
        output=dir_path+"/er_samples.txt"
        SampOut,keys=pmag.fillkeys(SampRecs)
        pmag.magic_write(output,SampOut,'er_samples')
        print("sample info put in ",output)
        output=dir_path+"/er_sites.txt"
        SiteOut,keys=pmag.fillkeys(SiteRecs)
        pmag.magic_write(output,SiteOut,'er_sites')
        print("site info put in ",output)
    print(""""
         You can now import your data into the Magic Console and complete data entry,
         for example the site locations, lithologies, etc. plotting can be done with aniso_magic.py
    """)
示例#47
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
示例#48
0
def main(**kwargs):

    version_num=pmag.get_version()
    dir_path = kwargs.get('dir_path', '.')
    input_dir_path = kwargs.get('input_dir_path', dir_path)
    output_dir_path = 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
    mag_file = kwargs.get('mag_file')
    specnum = kwargs.get('specnum', 1)
    samp_con = kwargs.get('samp_con', '1')
    location = kwargs.get('location', 'unknown')
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')
    noave = kwargs.get('noave', 0) # default (0) means DO average
    meth_code = kwargs.get('meth_code', "LP-NO")
    volume = float(kwargs.get('volume', 2.5))* 1e-6
    JR = kwargs.get('JR', False)
    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
    tmp_file = mag_file.split(os.extsep)[0]+os.extsep+'tmp'
    mag_file = os.path.join(input_dir_path, mag_file)
    if specnum!=0: specnum=-int(specnum)
    if samp_con.startswith("4"):
        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 samp_con.startswith("7"):
        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"
    else: Z=1

    # 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')
    if samp_con!='2': fixed_data=pre_data.read().replace('-',' -')
    else:
        fixed_data=""
        for line in pre_data.readlines():
            entries=line.split()
            if len(entries)<2: continue
            fixed_line = entries[0] + ' ' + reduce(lambda x,y: x+' '+y, [x.replace('-',' -') for x in entries[1:]])
            fixed_data += fixed_line+os.linesep
    tmp_data.write(fixed_data)
    tmp_data.close()
    pre_data.close()

    if not JR:
        column_names=['specimen', 'step', 'x', 'y', 'z', 'expon', 'azimuth', 'dip', 'bed_dip_direction', 'bed_dip', 'bed_dip_dir2', 'bed_dip2', 'param1', 'param2', 'param3', 'param4', 'dir_csd']
    else: # measured on the Joides Resolution JR6
        column_names=['specimen', 'step', 'negz', 'y', 'x', 'expon', 'azimuth', 'dip', 'bed_dip_direction', 'bed_dip', 'bed_dip_dir2', 'bed_dip2', 'param1', 'param2', 'param3', 'param4', 'dir_csd']
    data=pd.read_csv(tmp_file, delim_whitespace=True, names=column_names, index_col=False)
    if isinstance(data['x'][0],str):
        column_names=['specimen', 'step', 'step_unit', 'x', 'y', 'z', 'expon', 'azimuth', 'dip', 'bed_dip_direction', 'bed_dip', 'bed_dip_dir2', 'bed_dip2', 'param1', 'param2', 'param3', 'param4', 'dir_csd']
        data=pd.read_csv(tmp_file, delim_whitespace=True, names=column_names, index_col=False)
    if JR: data['z']=-data['negz']
    cart=np.array([data['x'],data['y'],data['z']]).transpose()
    dir_dat= pmag.cart2dir(cart).transpose()
    data['dir_dec']=dir_dat[0]
    data['dir_inc']=dir_dat[1]
    data['magn_moment']=dir_dat[2]*(10.0**data['expon'])*volume # the data are in A/m - this converts to Am^2
    data['magn_volume']=dir_dat[2]*(10.0**data['expon']) # A/m  - data in A/m
    data['dip']=-data['dip']

    data['specimen']
    # put data into magic tables
    MagRecs,SpecRecs,SampRecs,SiteRecs,LocRecs=[],[],[],[],[]
    for rowNum, row in data.iterrows():
        MeasRec,SpecRec,SampRec,SiteRec,LocRec={},{},{},{},{}
        specimen=row['specimen']
        if specnum!=0: sample=specimen[:specnum]
        else: sample=specimen
        site=pmag.parse_site(sample,samp_con,Z)
        if specimen!="" and specimen not in [x['specimen'] if 'specimen' in list(x.keys()) else "" for x in SpecRecs]:
            SpecRec['specimen'] = specimen
            SpecRec['sample'] = sample
            SpecRec["citations"]="This study"
            SpecRec["analysts"]=user
            SpecRec['volume'] = volume
            SpecRecs.append(SpecRec)
        if sample!="" and sample not in [x['sample'] if 'sample' in list(x.keys()) else "" for x in SampRecs]:
            SampRec['sample'] = sample
            SampRec['site'] = site
            SampRec["citations"]="This study"
            SampRec["analysts"]=user
            SampRec['azimuth'] = row['azimuth']
            SampRec['dip'] = row['dip']
            SampRec['bed_dip_direction'] = row['bed_dip_direction']
            SampRec['bed_dip'] = row['bed_dip']
            SampRec['method_codes']=meth_code
            SampRecs.append(SampRec)
        if site!="" and site not in [x['site'] if 'site' in list(x.keys()) else "" for x in SiteRecs]:
            SiteRec['site'] = site
            SiteRec['location'] = location
            SiteRec["citations"]="This study"
            SiteRec["analysts"]=user
            SiteRec['lat'] = lat
            SiteRec['lon'] = lon
            SiteRecs.append(SiteRec)
        if location!="" and location not in [x['location'] if 'location' in list(x.keys()) else "" for x in LocRecs]:
            LocRec['location']=location
            LocRec["citations"]="This study"
            LocRec["analysts"]=user
            LocRec['lat_n'] = lat
            LocRec['lon_e'] = lon
            LocRec['lat_s'] = lat
            LocRec['lon_w'] = lon
            LocRecs.append(LocRec)
        MeasRec["citations"]="This study"
        MeasRec["analysts"]=user
        MeasRec["specimen"]=specimen
        MeasRec['software_packages']=version_num
        MeasRec["treat_temp"]='%8.3e' % (273) # room temp in kelvin
        MeasRec["meas_temp"]='%8.3e' % (273) # room temp in kelvin
        MeasRec["quality"]='g'
        MeasRec["standard"]='u'
        MeasRec["treat_step_num"]='1'
        MeasRec["treat_ac_field"]='0'
        if row['step'] == 'NRM':
            meas_type="LT-NO"
        elif 'step_unit' in row and row['step_unit']=='C':
            meas_type="LT-T-Z"
            treat=float(row['step'])
            MeasRec["treat_temp"]='%8.3e' % (treat+273.) # temp in kelvin
        elif row['step'][0:2] == 'AD':
            meas_type="LT-AF-Z"
            treat=float(row['step'][2:])
            MeasRec["treat_ac_field"]='%8.3e' %(treat*1e-3) # convert from mT to tesla
        elif row['step'][0] == 'A':
            meas_type="LT-AF-Z"
            treat=float(row['step'][1:])
            MeasRec["treat_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:])
            MeasRec["treat_temp"]='%8.3e' % (treat+273.) # temp in kelvin
        elif row['step'][0] == 'T':
            meas_type="LT-T-Z"
            treat=float(row['step'][1:])
            MeasRec["treat_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"
        MeasRec["magn_moment"]=str(row['magn_moment'])
        MeasRec["magn_volume"]=str(row['magn_volume'])
        MeasRec["dir_dec"]=str(row['dir_dec'])
        MeasRec["dir_inc"]=str(row['dir_inc'])
        MeasRec['method_codes']=meas_type
        MagRecs.append(MeasRec)

    con = nb.Contribution(output_dir_path,read_tables=[])

    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts=pmag.measurements_methods3(MeasRecs,noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    con.tables['specimens'].write_magic_file(custom_name=spec_file)
    con.tables['samples'].write_magic_file(custom_name=samp_file)
    con.tables['sites'].write_magic_file(custom_name=site_file)
    con.tables['locations'].write_magic_file(custom_name=loc_file)
    con.tables['measurements'].write_magic_file(custom_name=meas_file)

    try: os.remove(tmp_file)
    except (OSError,IOError) as e: print("couldn't remove temperary fixed JR6 file %s"%tmp_file)

    return True, meas_file