示例#1
0
def save_redo(SpecRecs,inspec):
    SpecRecs,keys=pmag.fillkeys(SpecRecs)
    pmag.magic_write(inspec,SpecRecs,'pmag_specimens')
示例#2
0
def main(command_line=True, **kwargs):
    """
    NAME
        iodp_dscr_magic.py

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


    SYNTAX
        iodp_descr_magic.py [command line options]

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

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

    # format variables

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

    DESCRIPTION
        plots anisotropy data with either bootstrap or hext ellipses

    SYNTAX
        aniso_magic.py [-h] [command line options]
    OPTIONS
        -h plots help message and quits
        -usr USER: set the user name
        -f AFILE, specify rmag_anisotropy formatted file for input
        -F RFILE, specify rmag_results formatted file for output
        -x Hext [1963] and bootstrap
        -B DON'T do bootstrap, do Hext
        -par Tauxe [1998] parametric bootstrap
        -v plot bootstrap eigenvectors instead of ellipses
        -sit plot by site instead of entire file
        -crd [s,g,t] coordinate system, default is specimen (g=geographic, t=tilt corrected)
        -P don't make any plots - just make rmag_results table
        -sav don't make the rmag_results table - just save all the plots
        -fmt [svg, jpg, eps] format for output images, pdf default
        -gtc DEC INC  dec,inc of pole to great circle [down(up) in green (cyan)
        -d Vi DEC INC; Vi (1,2,3) to compare to direction DEC INC
        -n N; specifies the number of bootstraps - default is 1000
    DEFAULTS
       AFILE:  rmag_anisotropy.txt
       RFILE:  rmag_results.txt
       plot bootstrap ellipses of Constable & Tauxe [1987]
    NOTES
       minor axis: circles
       major axis: triangles
       principal axis: squares
       directions are plotted on the lower hemisphere
       for bootstrapped eigenvector components: Xs: blue, Ys: red, Zs: black
"""
#
    dir_path = "."
    version_num = pmag.get_version()
    verbose = pmagplotlib.verbose
    args = sys.argv
    ipar, ihext, ivec, iboot, imeas, isite, iplot, vec = 0, 0, 0, 1, 1, 0, 1, 0
    hpars, bpars, PDir = [], [], []
    CS, crd = '-1', 's'
    nb = 1000
    fmt = 'pdf'
    ResRecs = []
    orlist = []
    outfile, comp, Dir, gtcirc, PDir = 'rmag_results.txt', 0, [], 0, []
    infile = 'rmag_anisotropy.txt'
    if "-h" in args:
        print(main.__doc__)
        sys.exit()
    if '-WD' in args:
        ind = args.index('-WD')
        dir_path = args[ind+1]
    if '-n' in args:
        ind = args.index('-n')
        nb = int(args[ind+1])
    if '-usr' in args:
        ind = args.index('-usr')
        user = args[ind+1]
    else:
        user = ""
    if '-B' in args:
        iboot, ihext = 0, 1
    if '-par' in args:
        ipar = 1
    if '-x' in args:
        ihext = 1
    if '-v' in args:
        ivec = 1
    if '-sit' in args:
        isite = 1
    if '-P' in args:
        iplot = 0
    if '-f' in args:
        ind = args.index('-f')
        infile = args[ind+1]
    if '-F' in args:
        ind = args.index('-F')
        outfile = args[ind+1]
    if '-crd' in sys.argv:
        ind = sys.argv.index('-crd')
        crd = sys.argv[ind+1]
        if crd == 'g':
            CS = '0'
        if crd == 't':
            CS = '100'
    if '-fmt' in args:
        ind = args.index('-fmt')
        fmt = args[ind+1]
    if '-sav' in args:
        plots = 1
        verbose = 0
    else:
        plots = 0
    if '-gtc' in args:
        ind = args.index('-gtc')
        d, i = float(args[ind+1]), float(args[ind+2])
        PDir.append(d)
        PDir.append(i)
    if '-d' in args:
        comp = 1
        ind = args.index('-d')
        vec = int(args[ind+1])-1
        Dir = [float(args[ind+2]), float(args[ind+3])]
#
# set up plots
#
    if infile[0] != '/':
        infile = dir_path+'/'+infile
    if outfile[0] != '/':
        outfile = dir_path+'/'+outfile
    ANIS = {}
    initcdf, inittcdf = 0, 0
    ANIS['data'], ANIS['conf'] = 1, 2
    if iboot == 1:
        ANIS['tcdf'] = 3
        if iplot == 1:
            inittcdf = 1
            pmagplotlib.plot_init(ANIS['tcdf'], 5, 5)
        if comp == 1 and iplot == 1:
            initcdf = 1
            ANIS['vxcdf'], ANIS['vycdf'], ANIS['vzcdf'] = 4, 5, 6
            pmagplotlib.plot_init(ANIS['vxcdf'], 5, 5)
            pmagplotlib.plot_init(ANIS['vycdf'], 5, 5)
            pmagplotlib.plot_init(ANIS['vzcdf'], 5, 5)
    if iplot == 1:
        pmagplotlib.plot_init(ANIS['conf'], 5, 5)
        pmagplotlib.plot_init(ANIS['data'], 5, 5)
# read in the data
    data, ifiletype = pmag.magic_read(infile)
    for rec in data:  # find all the orientation systems
        if 'anisotropy_tilt_correction' not in rec.keys():
            rec['anisotropy_tilt_correction'] = '-1'
        if rec['anisotropy_tilt_correction'] not in orlist:
            orlist.append(rec['anisotropy_tilt_correction'])
    if CS not in orlist:
        if len(orlist) > 0:
            CS = orlist[0]
        else:
            CS = '-1'
        if CS == '-1':
            crd = 's'
        if CS == '0':
            crd = 'g'
        if CS == '100':
            crd = 't'
        if verbose:
            print("desired coordinate system not available, using available: ", crd)
    if isite == 1:
        sitelist = []
        for rec in data:
            if rec['er_site_name'] not in sitelist:
                sitelist.append(rec['er_site_name'])
        sitelist.sort()
        plt = len(sitelist)
    else:
        plt = 1
    k = 0
    while k < plt:
        site = ""
        sdata, Ss = [], []  # list of S format data
        Locs, Sites, Samples, Specimens, Cits = [], [], [], [], []
        if isite == 0:
            sdata = data
        else:
            site = sitelist[k]
            for rec in data:
                if rec['er_site_name'] == site:
                    sdata.append(rec)
        anitypes = []
        csrecs = pmag.get_dictitem(
            sdata, 'anisotropy_tilt_correction', CS, 'T')
        for rec in csrecs:
            if rec['anisotropy_type'] not in anitypes:
                anitypes.append(rec['anisotropy_type'])
            if rec['er_location_name'] not in Locs:
                Locs.append(rec['er_location_name'])
            if rec['er_site_name'] not in Sites:
                Sites.append(rec['er_site_name'])
            if rec['er_sample_name'] not in Samples:
                Samples.append(rec['er_sample_name'])
            if rec['er_specimen_name'] not in Specimens:
                Specimens.append(rec['er_specimen_name'])
            if rec['er_citation_names'] not in Cits:
                Cits.append(rec['er_citation_names'])
            s = []
            s.append(float(rec["anisotropy_s1"]))
            s.append(float(rec["anisotropy_s2"]))
            s.append(float(rec["anisotropy_s3"]))
            s.append(float(rec["anisotropy_s4"]))
            s.append(float(rec["anisotropy_s5"]))
            s.append(float(rec["anisotropy_s6"]))
            if s[0] <= 1.0:
                Ss.append(s)  # protect against crap
            # tau,Vdirs=pmag.doseigs(s)
            ResRec = {}
            ResRec['er_location_names'] = rec['er_location_name']
            ResRec['er_citation_names'] = rec['er_citation_names']
            ResRec['er_site_names'] = rec['er_site_name']
            ResRec['er_sample_names'] = rec['er_sample_name']
            ResRec['er_specimen_names'] = rec['er_specimen_name']
            ResRec['rmag_result_name'] = rec['er_specimen_name'] + \
                ":"+rec['anisotropy_type']
            ResRec["er_analyst_mail_names"] = user
            ResRec["tilt_correction"] = CS
            ResRec["anisotropy_type"] = rec['anisotropy_type']
            if "anisotropy_n" not in rec.keys():
                rec["anisotropy_n"] = "6"
            if "anisotropy_sigma" not in rec.keys():
                rec["anisotropy_sigma"] = "0"
            fpars = pmag.dohext(
                int(rec["anisotropy_n"])-6, float(rec["anisotropy_sigma"]), s)
            ResRec["anisotropy_v1_dec"] = '%7.1f' % (fpars['v1_dec'])
            ResRec["anisotropy_v2_dec"] = '%7.1f' % (fpars['v2_dec'])
            ResRec["anisotropy_v3_dec"] = '%7.1f' % (fpars['v3_dec'])
            ResRec["anisotropy_v1_inc"] = '%7.1f' % (fpars['v1_inc'])
            ResRec["anisotropy_v2_inc"] = '%7.1f' % (fpars['v2_inc'])
            ResRec["anisotropy_v3_inc"] = '%7.1f' % (fpars['v3_inc'])
            ResRec["anisotropy_t1"] = '%10.8f' % (fpars['t1'])
            ResRec["anisotropy_t2"] = '%10.8f' % (fpars['t2'])
            ResRec["anisotropy_t3"] = '%10.8f' % (fpars['t3'])
            ResRec["anisotropy_ftest"] = '%10.3f' % (fpars['F'])
            ResRec["anisotropy_ftest12"] = '%10.3f' % (fpars['F12'])
            ResRec["anisotropy_ftest23"] = '%10.3f' % (fpars['F23'])
            ResRec["result_description"] = 'F_crit: ' + \
                fpars['F_crit']+'; F12,F23_crit: '+fpars['F12_crit']
            ResRec['anisotropy_type'] = pmag.makelist(anitypes)
            ResRecs.append(ResRec)
        if len(Ss) > 1:
            if pmagplotlib.isServer:
                title = "LO:_"+ResRec['er_location_names'] + \
                    '_SI:_'+site+'_SA:__SP:__CO:_'+crd
            else:
                title = ResRec['er_location_names']
                if site:
                    title += "_{}".format(site)
                title += '_{}'.format(crd)
            ResRec['er_location_names'] = pmag.makelist(Locs)
            bpars, hpars = pmagplotlib.plot_anis(
                ANIS, Ss, iboot, ihext, ivec, ipar, title, iplot, comp, vec, Dir, nb)
            if len(PDir) > 0:
                pmagplotlib.plot_circ(ANIS['data'], PDir, 90., 'g')
                pmagplotlib.plot_circ(ANIS['conf'], PDir, 90., 'g')
            if verbose and plots == 0:
                pmagplotlib.draw_figs(ANIS)
            ResRec['er_location_names'] = pmag.makelist(Locs)
            if plots == 1:
                save(ANIS, fmt, title)
            ResRec = {}
            ResRec['er_citation_names'] = pmag.makelist(Cits)
            ResRec['er_location_names'] = pmag.makelist(Locs)
            ResRec['er_site_names'] = pmag.makelist(Sites)
            ResRec['er_sample_names'] = pmag.makelist(Samples)
            ResRec['er_specimen_names'] = pmag.makelist(Specimens)
            ResRec['rmag_result_name'] = pmag.makelist(
                Sites)+":"+pmag.makelist(anitypes)
            ResRec['anisotropy_type'] = pmag.makelist(anitypes)
            ResRec["er_analyst_mail_names"] = user
            ResRec["tilt_correction"] = CS
            if isite == "0":
                ResRec['result_description'] = "Study average using coordinate system: " + CS
            if isite == "1":
                ResRec['result_description'] = "Site average using coordinate system: " + CS
            if hpars != [] and ihext == 1:
                HextRec = {}
                for key in ResRec.keys():
                    HextRec[key] = ResRec[key]   # copy over stuff
                HextRec["anisotropy_v1_dec"] = '%7.1f' % (hpars["v1_dec"])
                HextRec["anisotropy_v2_dec"] = '%7.1f' % (hpars["v2_dec"])
                HextRec["anisotropy_v3_dec"] = '%7.1f' % (hpars["v3_dec"])
                HextRec["anisotropy_v1_inc"] = '%7.1f' % (hpars["v1_inc"])
                HextRec["anisotropy_v2_inc"] = '%7.1f' % (hpars["v2_inc"])
                HextRec["anisotropy_v3_inc"] = '%7.1f' % (hpars["v3_inc"])
                HextRec["anisotropy_t1"] = '%10.8f' % (hpars["t1"])
                HextRec["anisotropy_t2"] = '%10.8f' % (hpars["t2"])
                HextRec["anisotropy_t3"] = '%10.8f' % (hpars["t3"])
                HextRec["anisotropy_hext_F"] = '%7.1f ' % (hpars["F"])
                HextRec["anisotropy_hext_F12"] = '%7.1f ' % (hpars["F12"])
                HextRec["anisotropy_hext_F23"] = '%7.1f ' % (hpars["F23"])
                HextRec["anisotropy_v1_eta_semi_angle"] = '%7.1f ' % (
                    hpars["e12"])
                HextRec["anisotropy_v1_eta_dec"] = '%7.1f ' % (hpars["v2_dec"])
                HextRec["anisotropy_v1_eta_inc"] = '%7.1f ' % (hpars["v2_inc"])
                HextRec["anisotropy_v1_zeta_semi_angle"] = '%7.1f ' % (
                    hpars["e13"])
                HextRec["anisotropy_v1_zeta_dec"] = '%7.1f ' % (
                    hpars["v3_dec"])
                HextRec["anisotropy_v1_zeta_inc"] = '%7.1f ' % (
                    hpars["v3_inc"])
                HextRec["anisotropy_v2_eta_semi_angle"] = '%7.1f ' % (
                    hpars["e12"])
                HextRec["anisotropy_v2_eta_dec"] = '%7.1f ' % (hpars["v1_dec"])
                HextRec["anisotropy_v2_eta_inc"] = '%7.1f ' % (hpars["v1_inc"])
                HextRec["anisotropy_v2_zeta_semi_angle"] = '%7.1f ' % (
                    hpars["e23"])
                HextRec["anisotropy_v2_zeta_dec"] = '%7.1f ' % (
                    hpars["v3_dec"])
                HextRec["anisotropy_v2_zeta_inc"] = '%7.1f ' % (
                    hpars["v3_inc"])
                HextRec["anisotropy_v3_eta_semi_angle"] = '%7.1f ' % (
                    hpars["e12"])
                HextRec["anisotropy_v3_eta_dec"] = '%7.1f ' % (hpars["v1_dec"])
                HextRec["anisotropy_v3_eta_inc"] = '%7.1f ' % (hpars["v1_inc"])
                HextRec["anisotropy_v3_zeta_semi_angle"] = '%7.1f ' % (
                    hpars["e23"])
                HextRec["anisotropy_v3_zeta_dec"] = '%7.1f ' % (
                    hpars["v2_dec"])
                HextRec["anisotropy_v3_zeta_inc"] = '%7.1f ' % (
                    hpars["v2_inc"])
                HextRec["magic_method_codes"] = 'LP-AN:AE-H'
                if verbose:
                    print("Hext Statistics: ")
                    print(
                        " tau_i, V_i_D, V_i_I, V_i_zeta, V_i_zeta_D, V_i_zeta_I, V_i_eta, V_i_eta_D, V_i_eta_I")
                    print(HextRec["anisotropy_t1"], HextRec["anisotropy_v1_dec"], HextRec["anisotropy_v1_inc"], HextRec["anisotropy_v1_eta_semi_angle"], HextRec["anisotropy_v1_eta_dec"],
                          HextRec["anisotropy_v1_eta_inc"], HextRec["anisotropy_v1_zeta_semi_angle"], HextRec["anisotropy_v1_zeta_dec"], HextRec["anisotropy_v1_zeta_inc"])
                    print(HextRec["anisotropy_t2"], HextRec["anisotropy_v2_dec"], HextRec["anisotropy_v2_inc"], HextRec["anisotropy_v2_eta_semi_angle"], HextRec["anisotropy_v2_eta_dec"],
                          HextRec["anisotropy_v2_eta_inc"], HextRec["anisotropy_v2_zeta_semi_angle"], HextRec["anisotropy_v2_zeta_dec"], HextRec["anisotropy_v2_zeta_inc"])
                    print(HextRec["anisotropy_t3"], HextRec["anisotropy_v3_dec"], HextRec["anisotropy_v3_inc"], HextRec["anisotropy_v3_eta_semi_angle"], HextRec["anisotropy_v3_eta_dec"],
                          HextRec["anisotropy_v3_eta_inc"], HextRec["anisotropy_v3_zeta_semi_angle"], HextRec["anisotropy_v3_zeta_dec"], HextRec["anisotropy_v3_zeta_inc"])
                HextRec['magic_software_packages'] = version_num
                ResRecs.append(HextRec)
            if bpars != []:
                BootRec = {}
                for key in ResRec.keys():
                    BootRec[key] = ResRec[key]   # copy over stuff
                BootRec["anisotropy_v1_dec"] = '%7.1f' % (bpars["v1_dec"])
                BootRec["anisotropy_v2_dec"] = '%7.1f' % (bpars["v2_dec"])
                BootRec["anisotropy_v3_dec"] = '%7.1f' % (bpars["v3_dec"])
                BootRec["anisotropy_v1_inc"] = '%7.1f' % (bpars["v1_inc"])
                BootRec["anisotropy_v2_inc"] = '%7.1f' % (bpars["v2_inc"])
                BootRec["anisotropy_v3_inc"] = '%7.1f' % (bpars["v3_inc"])
                BootRec["anisotropy_t1"] = '%10.8f' % (bpars["t1"])
                BootRec["anisotropy_t2"] = '%10.8f' % (bpars["t2"])
                BootRec["anisotropy_t3"] = '%10.8f' % (bpars["t3"])
                BootRec["anisotropy_v1_eta_inc"] = '%7.1f ' % (
                    bpars["v1_eta_inc"])
                BootRec["anisotropy_v1_eta_dec"] = '%7.1f ' % (
                    bpars["v1_eta_dec"])
                BootRec["anisotropy_v1_eta_semi_angle"] = '%7.1f ' % (
                    bpars["v1_eta"])
                BootRec["anisotropy_v1_zeta_inc"] = '%7.1f ' % (
                    bpars["v1_zeta_inc"])
                BootRec["anisotropy_v1_zeta_dec"] = '%7.1f ' % (
                    bpars["v1_zeta_dec"])
                BootRec["anisotropy_v1_zeta_semi_angle"] = '%7.1f ' % (
                    bpars["v1_zeta"])
                BootRec["anisotropy_v2_eta_inc"] = '%7.1f ' % (
                    bpars["v2_eta_inc"])
                BootRec["anisotropy_v2_eta_dec"] = '%7.1f ' % (
                    bpars["v2_eta_dec"])
                BootRec["anisotropy_v2_eta_semi_angle"] = '%7.1f ' % (
                    bpars["v2_eta"])
                BootRec["anisotropy_v2_zeta_inc"] = '%7.1f ' % (
                    bpars["v2_zeta_inc"])
                BootRec["anisotropy_v2_zeta_dec"] = '%7.1f ' % (
                    bpars["v2_zeta_dec"])
                BootRec["anisotropy_v2_zeta_semi_angle"] = '%7.1f ' % (
                    bpars["v2_zeta"])
                BootRec["anisotropy_v3_eta_inc"] = '%7.1f ' % (
                    bpars["v3_eta_inc"])
                BootRec["anisotropy_v3_eta_dec"] = '%7.1f ' % (
                    bpars["v3_eta_dec"])
                BootRec["anisotropy_v3_eta_semi_angle"] = '%7.1f ' % (
                    bpars["v3_eta"])
                BootRec["anisotropy_v3_zeta_inc"] = '%7.1f ' % (
                    bpars["v3_zeta_inc"])
                BootRec["anisotropy_v3_zeta_dec"] = '%7.1f ' % (
                    bpars["v3_zeta_dec"])
                BootRec["anisotropy_v3_zeta_semi_angle"] = '%7.1f ' % (
                    bpars["v3_zeta"])
                BootRec["anisotropy_hext_F"] = ''
                BootRec["anisotropy_hext_F12"] = ''
                BootRec["anisotropy_hext_F23"] = ''
                # regular bootstrap
                BootRec["magic_method_codes"] = 'LP-AN:AE-H:AE-BS'
                if ipar == 1:
                    # parametric bootstrap
                    BootRec["magic_method_codes"] = 'LP-AN:AE-H:AE-BS-P'
                if verbose:
                    print("Boostrap Statistics: ")
                    print(
                        " tau_i, V_i_D, V_i_I, V_i_zeta, V_i_zeta_D, V_i_zeta_I, V_i_eta, V_i_eta_D, V_i_eta_I")
                    print(BootRec["anisotropy_t1"], BootRec["anisotropy_v1_dec"], BootRec["anisotropy_v1_inc"], BootRec["anisotropy_v1_eta_semi_angle"], BootRec["anisotropy_v1_eta_dec"],
                          BootRec["anisotropy_v1_eta_inc"], BootRec["anisotropy_v1_zeta_semi_angle"], BootRec["anisotropy_v1_zeta_dec"], BootRec["anisotropy_v1_zeta_inc"])
                    print(BootRec["anisotropy_t2"], BootRec["anisotropy_v2_dec"], BootRec["anisotropy_v2_inc"], BootRec["anisotropy_v2_eta_semi_angle"], BootRec["anisotropy_v2_eta_dec"],
                          BootRec["anisotropy_v2_eta_inc"], BootRec["anisotropy_v2_zeta_semi_angle"], BootRec["anisotropy_v2_zeta_dec"], BootRec["anisotropy_v2_zeta_inc"])
                    print(BootRec["anisotropy_t3"], BootRec["anisotropy_v3_dec"], BootRec["anisotropy_v3_inc"], BootRec["anisotropy_v3_eta_semi_angle"], BootRec["anisotropy_v3_eta_dec"],
                          BootRec["anisotropy_v3_eta_inc"], BootRec["anisotropy_v3_zeta_semi_angle"], BootRec["anisotropy_v3_zeta_dec"], BootRec["anisotropy_v3_zeta_inc"])
                BootRec['magic_software_packages'] = version_num
                ResRecs.append(BootRec)
            k += 1
            goon = 1
            while goon == 1 and iplot == 1 and verbose:
                if iboot == 1:
                    print("compare with [d]irection ")
                print(
                    " plot [g]reat circle,  change [c]oord. system, change [e]llipse calculation,  s[a]ve plots, [q]uit ")
                if isite == 1:
                    print("  [p]revious, [s]ite, [q]uit, <return> for next ")
                ans = input("")
                if ans == "q":
                    sys.exit()
                if ans == "e":
                    iboot, ipar, ihext, ivec = 1, 0, 0, 0
                    e = input("Do Hext Statistics  1/[0]: ")
                    if e == "1":
                        ihext = 1
                    e = input("Suppress bootstrap 1/[0]: ")
                    if e == "1":
                        iboot = 0
                    if iboot == 1:
                        e = input("Parametric bootstrap 1/[0]: ")
                        if e == "1":
                            ipar = 1
                        e = input("Plot bootstrap eigenvectors:  1/[0]: ")
                        if e == "1":
                            ivec = 1
                        if iplot == 1:
                            if inittcdf == 0:
                                ANIS['tcdf'] = 3
                                pmagplotlib.plot_init(ANIS['tcdf'], 5, 5)
                                inittcdf = 1
                    bpars, hpars = pmagplotlib.plot_anis(
                        ANIS, Ss, iboot, ihext, ivec, ipar, title, iplot, comp, vec, Dir, nb)
                    if verbose and plots == 0:
                        pmagplotlib.draw_figs(ANIS)
                if ans == "c":
                    print("Current Coordinate system is: ")
                    if CS == '-1':
                        print(" Specimen")
                    if CS == '0':
                        print(" Geographic")
                    if CS == '100':
                        print(" Tilt corrected")
                    key = input(
                        " Enter desired coordinate system: [s]pecimen, [g]eographic, [t]ilt corrected ")
                    if key == 's':
                        CS = '-1'
                    if key == 'g':
                        CS = '0'
                    if key == 't':
                        CS = '100'
                    if CS not in orlist:
                        if len(orlist) > 0:
                            CS = orlist[0]
                        else:
                            CS = '-1'
                        if CS == '-1':
                            crd = 's'
                        if CS == '0':
                            crd = 'g'
                        if CS == '100':
                            crd = 't'
                        print(
                            "desired coordinate system not available, using available: ", crd)
                    k -= 1
                    goon = 0
                if ans == "":
                    if isite == 1:
                        goon = 0
                    else:
                        print("Good bye ")
                        sys.exit()
                if ans == 'd':
                    if initcdf == 0:
                        initcdf = 1
                        ANIS['vxcdf'], ANIS['vycdf'], ANIS['vzcdf'] = 4, 5, 6
                        pmagplotlib.plot_init(ANIS['vxcdf'], 5, 5)
                        pmagplotlib.plot_init(ANIS['vycdf'], 5, 5)
                        pmagplotlib.plot_init(ANIS['vzcdf'], 5, 5)
                    Dir, comp = [], 1
                    print("""
                      Input: Vi D I to  compare  eigenvector Vi with direction D/I
                             where Vi=1: principal
                                   Vi=2: major
                                   Vi=3: minor
                                   D= declination of comparison direction
                                   I= inclination of comparison direction""")
                    con = 1
                    while con == 1:
                        try:
                            vdi = input("Vi D I: ").split()
                            vec = int(vdi[0])-1
                            Dir = [float(vdi[1]), float(vdi[2])]
                            con = 0
                        except IndexError:
                            print(" Incorrect entry, try again ")
                    bpars, hpars = pmagplotlib.plot_anis(
                        ANIS, Ss, iboot, ihext, ivec, ipar, title, iplot, comp, vec, Dir, nb)
                    Dir, comp = [], 0
                if ans == 'g':
                    con, cnt = 1, 0
                    while con == 1:
                        try:
                            print(
                                " Input:  input pole to great circle ( D I) to  plot a great circle:   ")
                            di = input(" D I: ").split()
                            PDir.append(float(di[0]))
                            PDir.append(float(di[1]))
                            con = 0
                        except:
                            cnt += 1
                            if cnt < 10:
                                print(
                                    " enter the dec and inc of the pole on one line ")
                            else:
                                print(
                                    "ummm - you are doing something wrong - i give up")
                                sys.exit()
                    pmagplotlib.plot_circ(ANIS['data'], PDir, 90., 'g')
                    pmagplotlib.plot_circ(ANIS['conf'], PDir, 90., 'g')
                    if verbose and plots == 0:
                        pmagplotlib.draw_figs(ANIS)
                if ans == "p":
                    k -= 2
                    goon = 0
                if ans == "q":
                    k = plt
                    goon = 0
                if ans == "s":
                    keepon = 1
                    site = input(" print site or part of site desired: ")
                    while keepon == 1:
                        try:
                            k = sitelist.index(site)
                            keepon = 0
                        except:
                            tmplist = []
                            for qq in range(len(sitelist)):
                                if site in sitelist[qq]:
                                    tmplist.append(sitelist[qq])
                            print(site, " not found, but this was: ")
                            print(tmplist)
                            site = input('Select one or try again\n ')
                            k = sitelist.index(site)
                    goon, ans = 0, ""
                if ans == "a":
                    locs = pmag.makelist(Locs)
                    if pmagplotlib.isServer:  # use server plot naming convention
                        title = "LO:_"+locs+'_SI:__'+'_SA:__SP:__CO:_'+crd
                    else:  # use more readable plot naming convention
                        title = "{}_{}".format(locs, crd)
                    save(ANIS, fmt, title)
                    goon = 0
        else:
            if verbose:
                print('skipping plot - not enough data points')
            k += 1
#   put rmag_results stuff here
    if len(ResRecs) > 0:
        ResOut, keylist = pmag.fillkeys(ResRecs)
        pmag.magic_write(outfile, ResOut, 'rmag_results')
    if verbose:
        print(" Good bye ")
示例#5
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)
示例#6
0
def main(command_line=True, **kwargs):
    """
    NAME
        iodp_srm_magic.py
 
    DESCRIPTION
        converts IODP LIMS and LORE SRM archive half sample format files to magic_measurements format files


    SYNTAX
        iodp_srm_magic.py [command line options]

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

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

    # format variables

    meas_file = os.path.join(output_dir_path, meas_file)
    spec_file = os.path.join(output_dir_path, spec_file)
    Specs,file_type = pmag.magic_read(spec_file)
    samp_file = os.path.join(output_dir_path, samp_file)
    ErSamps,file_type = pmag.magic_read(samp_file)
    site_file = os.path.join(output_dir_path, site_file)
    if csv_file=="":
        filelist=os.listdir(input_dir_path) # read in list of files to import
    else:
        csv_file = os.path.join(input_dir_path, csv_file)
        filelist=[csv_file]

    
    # parsing the data
    specimens,samples,sites=[],[],[]
    MagRecs,SpecRecs,SampRecs,SiteRecs=[],[],[],[]
    for samp in ErSamps:
        if samp['er_sample_name'] not in samples:
            samples.append(samp['er_sample_name'])
            SampRecs.append(samp)
    file_found = False
    for f in filelist: # parse each file
        if f[-3:].lower()=='csv':
            file_found = True
            print 'processing: ',f
            full_file = os.path.join(input_dir_path, f)
            file_input=open(full_file,'rU').readlines()
            keys=file_input[0].replace('\n','').split(',') # splits on underscores
            if "Interval Top (cm) on SHLF" in keys:interval_key="Interval Top (cm) on SHLF"
            if " Interval Bot (cm) on SECT" in keys:interval_key=" Interval Bot (cm) on SECT"
            if "Offset (cm)" in keys: interval_key="Offset (cm)"
            if "Top Depth (m)" in keys:depth_key="Top Depth (m)"
            if "CSF-A Top (m)" in keys:depth_key="CSF-A Top (m)" 
            if "Depth CSF-A (m)" in keys:depth_key="Depth CSF-A (m)"
            if "CSF-B Top (m)" in keys: 
                comp_depth_key="CSF-B Top (m)" # use this model if available 
            elif "Depth CSF-B (m)" in keys:
                comp_depth_key="Depth CSF-B (m)"
            else:
                comp_depth_key=""
            if "Demag level (mT)" in keys:demag_key="Demag level (mT)"
            if "Demag Level (mT)" in keys: demag_key="Demag Level (mT)"
            if "Inclination (Tray- and Bkgrd-Corrected) (deg)" in keys:inc_key="Inclination (Tray- and Bkgrd-Corrected) (deg)"
            if "Inclination background + tray corrected  (deg)" in keys:inc_key="Inclination background + tray corrected  (deg)"
            if "Inclination background + tray corrected  (\xc2\xb0)" in keys:inc_key="Inclination background + tray corrected  (\xc2\xb0)"
            if "Inclination background &amp; tray corrected (deg)" in keys:inc_key="Inclination background &amp; tray corrected (deg)"
            if "Declination (Tray- and Bkgrd-Corrected) (deg)" in keys:dec_key="Declination (Tray- and Bkgrd-Corrected) (deg)"
            if "Declination background + tray corrected (deg)" in keys:dec_key="Declination background + tray corrected (deg)"
            if "Declination background + tray corrected (\xc2\xb0)" in keys:dec_key="Declination background + tray corrected (\xc2\xb0)"
            if "Declination background &amp; tray corrected (deg)" in keys:dec_key="Declination background &amp; tray corrected (deg)"
            if "Intensity (Tray- and Bkgrd-Corrected) (A/m)" in keys:int_key="Intensity (Tray- and Bkgrd-Corrected) (A/m)"
            if "Intensity background + tray corrected  (A/m)" in keys:int_key="Intensity background + tray corrected  (A/m)"
            if "Intensity background &amp; tray corrected (A/m)" in keys:int_key="Intensity background &amp; tray corrected (A/m)"
            if "Core Type" in keys:
                core_type="Core Type"
            else: core_type="Type" 
            if 'Run Number' in keys: run_number_key='Run Number'
            if 'Test No.' in keys: run_number_key='Test No.'
            if 'Test Changed On' in keys: date_key='Test Changed On'
            if "Timestamp (UTC)" in keys: date_key="Timestamp (UTC)"
            if "Section" in keys: sect_key="Section"
            if "Sect" in keys: sect_key="Sect"
            if 'Section Half' in keys: half_key='Section Half'
            if "A/W" in keys: half_key="A/W"
            if "Text ID" in keys: text_id="Text ID"
            if "Text Id" in keys: text_id="Text Id"
            for line in file_input[1:]:
              InRec={}
              test=0
              recs=line.split(',')
              for k in range(len(keys)):
                  if len(recs)==len(keys): 
                      InRec[keys[k]]=line.split(',')[k]
              if InRec['Exp']!="": test=1 # get rid of pesky blank lines
              if test==1:
                run_number=""
                inst="IODP-SRM"
                volume='15.59' # set default volume to this
                MagRec,SpecRec,SampRec,SiteRec={},{},{},{}
                expedition=InRec['Exp']
                location=InRec['Site']+InRec['Hole']
# Maintain backward compatibility for the ever-changing LIMS format (Argh!)
                while len(InRec['Core'])<3:
                    InRec['Core']='0'+InRec['Core']
                if "Last Tray Measurment" in InRec.keys() and "SHLF" not in InRec[text_id] or 'dscr' in csv_file :  # assume discrete sample
                    specimen=expedition+'-'+location+'-'+InRec['Core']+InRec[core_type]+"-"+InRec[sect_key]+'-'+InRec[half_key]+'-'+str(InRec[interval_key])
                else: # mark as continuous measurements
                    specimen=expedition+'-'+location+'-'+InRec['Core']+InRec[core_type]+"_"+InRec[sect_key]+InRec[half_key]+'-'+str(InRec[interval_key])
                SpecRec['er_expedition_name']=expedition
                SpecRec['er_location_name']=location
                SpecRec['er_site_name']=specimen
                SpecRec['er_citation_names']=citation
                for key in SpecRec.keys():SampRec[key]=SpecRec[key]
                for key in SpecRec.keys():SiteRec[key]=SpecRec[key]
                SampRec['sample_azimuth']='0'
                SampRec['sample_dip']='0'
                SampRec['sample_core_depth']=InRec[depth_key]
                if comp_depth_key!='':
                    SampRec['sample_composite_depth']=InRec[comp_depth_key]
                if "SHLF" not in InRec[text_id]: 
                    SampRec['magic_method_codes']='FS-C-DRILL-IODP:SP-SS-C:SO-V'
                else:
                    SampRec['magic_method_codes']='FS-C-DRILL-IODP:SO-V'
                SpecRec['er_specimen_name']=specimen
                SpecRec['er_sample_name']=specimen
                SampRec['er_sample_name']=specimen
                SampRec['er_specimen_names']=specimen
                SiteRec['er_specimen_names']=specimen

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

    DESCRIPTION
        plots anisotropy data with either bootstrap or hext ellipses

    SYNTAX
        aniso_magic.py [-h] [command line options]
    OPTIONS
        -h plots help message and quits
        -usr USER: set the user name
        -f AFILE, specify rmag_anisotropy formatted file for input
        -F RFILE, specify rmag_results formatted file for output
        -x Hext [1963] and bootstrap
        -B DON'T do bootstrap, do Hext
        -par Tauxe [1998] parametric bootstrap
        -v plot bootstrap eigenvectors instead of ellipses
        -sit plot by site instead of entire file
        -crd [s,g,t] coordinate system, default is specimen (g=geographic, t=tilt corrected)
        -P don't make any plots - just make rmag_results table
        -sav don't make the rmag_results table - just save all the plots
        -fmt [svg, jpg, eps] format for output images, pdf default
        -gtc DEC INC  dec,inc of pole to great circle [down(up) in green (cyan)
        -d Vi DEC INC; Vi (1,2,3) to compare to direction DEC INC
        -nb N; specifies the number of bootstraps - default is 1000
    DEFAULTS
       AFILE:  rmag_anisotropy.txt
       RFILE:  rmag_results.txt
       plot bootstrap ellipses of Constable & Tauxe [1987]
    NOTES
       minor axis: circles
       major axis: triangles
       principal axis: squares
       directions are plotted on the lower hemisphere
       for bootstrapped eigenvector components: Xs: blue, Ys: red, Zs: black
"""
    #
    dir_path = "."
    version_num = pmag.get_version()
    verbose = pmagplotlib.verbose
    args = sys.argv
    ipar, ihext, ivec, iboot, imeas, isite, iplot, vec = 0, 0, 0, 1, 1, 0, 1, 0
    hpars, bpars, PDir = [], [], []
    CS, crd = '-1', 's'
    nb = 1000
    fmt = 'pdf'
    ResRecs = []
    orlist = []
    outfile, comp, Dir, gtcirc, PDir = 'rmag_results.txt', 0, [], 0, []
    infile = 'rmag_anisotropy.txt'
    if "-h" in args:
        print(main.__doc__)
        sys.exit()
    if '-WD' in args:
        ind = args.index('-WD')
        dir_path = args[ind + 1]
    if '-nb' in args:
        ind = args.index('-nb')
        nb = int(args[ind + 1])
    if '-usr' in args:
        ind = args.index('-usr')
        user = args[ind + 1]
    else:
        user = ""
    if '-B' in args: iboot, ihext = 0, 1
    if '-par' in args: ipar = 1
    if '-x' in args: ihext = 1
    if '-v' in args: ivec = 1
    if '-sit' in args: isite = 1
    if '-P' in args: iplot = 0
    if '-f' in args:
        ind = args.index('-f')
        infile = args[ind + 1]
    if '-F' in args:
        ind = args.index('-F')
        outfile = args[ind + 1]
    if '-crd' in sys.argv:
        ind = sys.argv.index('-crd')
        crd = sys.argv[ind + 1]
        if crd == 'g': CS = '0'
        if crd == 't': CS = '100'
    if '-fmt' in args:
        ind = args.index('-fmt')
        fmt = args[ind + 1]
    if '-sav' in args:
        plots = 1
        verbose = 0
    else:
        plots = 0
    if '-gtc' in args:
        ind = args.index('-gtc')
        d, i = float(args[ind + 1]), float(args[ind + 2])
        PDir.append(d)
        PDir.append(i)
    if '-d' in args:
        comp = 1
        ind = args.index('-d')
        vec = int(args[ind + 1]) - 1
        Dir = [float(args[ind + 2]), float(args[ind + 3])]
#
# set up plots
#
    if infile[0] != '/': infile = dir_path + '/' + infile
    if outfile[0] != '/': outfile = dir_path + '/' + outfile
    ANIS = {}
    initcdf, inittcdf = 0, 0
    ANIS['data'], ANIS['conf'] = 1, 2
    if iboot == 1:
        ANIS['tcdf'] = 3
        if iplot == 1:
            inittcdf = 1
            pmagplotlib.plot_init(ANIS['tcdf'], 5, 5)
        if comp == 1 and iplot == 1:
            initcdf = 1
            ANIS['vxcdf'], ANIS['vycdf'], ANIS['vzcdf'] = 4, 5, 6
            pmagplotlib.plot_init(ANIS['vxcdf'], 5, 5)
            pmagplotlib.plot_init(ANIS['vycdf'], 5, 5)
            pmagplotlib.plot_init(ANIS['vzcdf'], 5, 5)
    if iplot == 1:
        pmagplotlib.plot_init(ANIS['conf'], 5, 5)
        pmagplotlib.plot_init(ANIS['data'], 5, 5)
# read in the data
    data, ifiletype = pmag.magic_read(infile)
    for rec in data:  # find all the orientation systems
        if 'anisotropy_tilt_correction' not in rec.keys():
            rec['anisotropy_tilt_correction'] = '-1'
        if rec['anisotropy_tilt_correction'] not in orlist:
            orlist.append(rec['anisotropy_tilt_correction'])
    if CS not in orlist:
        if len(orlist) > 0:
            CS = orlist[0]
        else:
            CS = '-1'
        if CS == '-1': crd = 's'
        if CS == '0': crd = 'g'
        if CS == '100': crd = 't'
        if verbose:
            print("desired coordinate system not available, using available: ",
                  crd)
    if isite == 1:
        sitelist = []
        for rec in data:
            if rec['er_site_name'] not in sitelist:
                sitelist.append(rec['er_site_name'])
        sitelist.sort()
        plt = len(sitelist)
    else:
        plt = 1
    k = 0
    while k < plt:
        site = ""
        sdata, Ss = [], []  # list of S format data
        Locs, Sites, Samples, Specimens, Cits = [], [], [], [], []
        if isite == 0:
            sdata = data
        else:
            site = sitelist[k]
            for rec in data:
                if rec['er_site_name'] == site: sdata.append(rec)
        anitypes = []
        csrecs = pmag.get_dictitem(sdata, 'anisotropy_tilt_correction', CS,
                                   'T')
        for rec in csrecs:
            if rec['anisotropy_type'] not in anitypes:
                anitypes.append(rec['anisotropy_type'])
            if rec['er_location_name'] not in Locs:
                Locs.append(rec['er_location_name'])
            if rec['er_site_name'] not in Sites:
                Sites.append(rec['er_site_name'])
            if rec['er_sample_name'] not in Samples:
                Samples.append(rec['er_sample_name'])
            if rec['er_specimen_name'] not in Specimens:
                Specimens.append(rec['er_specimen_name'])
            if rec['er_citation_names'] not in Cits:
                Cits.append(rec['er_citation_names'])
            s = []
            s.append(float(rec["anisotropy_s1"]))
            s.append(float(rec["anisotropy_s2"]))
            s.append(float(rec["anisotropy_s3"]))
            s.append(float(rec["anisotropy_s4"]))
            s.append(float(rec["anisotropy_s5"]))
            s.append(float(rec["anisotropy_s6"]))
            if s[0] <= 1.0: Ss.append(s)  # protect against crap
            #tau,Vdirs=pmag.doseigs(s)
            ResRec = {}
            ResRec['er_location_names'] = rec['er_location_name']
            ResRec['er_citation_names'] = rec['er_citation_names']
            ResRec['er_site_names'] = rec['er_site_name']
            ResRec['er_sample_names'] = rec['er_sample_name']
            ResRec['er_specimen_names'] = rec['er_specimen_name']
            ResRec['rmag_result_name'] = rec['er_specimen_name'] + ":" + rec[
                'anisotropy_type']
            ResRec["er_analyst_mail_names"] = user
            ResRec["tilt_correction"] = CS
            ResRec["anisotropy_type"] = rec['anisotropy_type']
            if "anisotropy_n" not in rec.keys(): rec["anisotropy_n"] = "6"
            if "anisotropy_sigma" not in rec.keys():
                rec["anisotropy_sigma"] = "0"
            fpars = pmag.dohext(
                int(rec["anisotropy_n"]) - 6, float(rec["anisotropy_sigma"]),
                s)
            ResRec["anisotropy_v1_dec"] = '%7.1f' % (fpars['v1_dec'])
            ResRec["anisotropy_v2_dec"] = '%7.1f' % (fpars['v2_dec'])
            ResRec["anisotropy_v3_dec"] = '%7.1f' % (fpars['v3_dec'])
            ResRec["anisotropy_v1_inc"] = '%7.1f' % (fpars['v1_inc'])
            ResRec["anisotropy_v2_inc"] = '%7.1f' % (fpars['v2_inc'])
            ResRec["anisotropy_v3_inc"] = '%7.1f' % (fpars['v3_inc'])
            ResRec["anisotropy_t1"] = '%10.8f' % (fpars['t1'])
            ResRec["anisotropy_t2"] = '%10.8f' % (fpars['t2'])
            ResRec["anisotropy_t3"] = '%10.8f' % (fpars['t3'])
            ResRec["anisotropy_ftest"] = '%10.3f' % (fpars['F'])
            ResRec["anisotropy_ftest12"] = '%10.3f' % (fpars['F12'])
            ResRec["anisotropy_ftest23"] = '%10.3f' % (fpars['F23'])
            ResRec["result_description"] = 'F_crit: ' + fpars[
                'F_crit'] + '; F12,F23_crit: ' + fpars['F12_crit']
            ResRec['anisotropy_type'] = pmag.makelist(anitypes)
            ResRecs.append(ResRec)
        if len(Ss) > 1:
            if pmagplotlib.isServer:
                title = "LO:_" + ResRec[
                    'er_location_names'] + '_SI:_' + site + '_SA:__SP:__CO:_' + crd
            else:
                title = ResRec['er_location_names']
                if site:
                    title += "_{}".format(site)
                title += '_{}'.format(crd)
            ResRec['er_location_names'] = pmag.makelist(Locs)
            bpars, hpars = pmagplotlib.plotANIS(ANIS, Ss, iboot, ihext, ivec,
                                                ipar, title, iplot, comp, vec,
                                                Dir, nb)
            if len(PDir) > 0:
                pmagplotlib.plotC(ANIS['data'], PDir, 90., 'g')
                pmagplotlib.plotC(ANIS['conf'], PDir, 90., 'g')
            if verbose and plots == 0: pmagplotlib.drawFIGS(ANIS)
            ResRec['er_location_names'] = pmag.makelist(Locs)
            if plots == 1:
                save(ANIS, fmt, title)
            ResRec = {}
            ResRec['er_citation_names'] = pmag.makelist(Cits)
            ResRec['er_location_names'] = pmag.makelist(Locs)
            ResRec['er_site_names'] = pmag.makelist(Sites)
            ResRec['er_sample_names'] = pmag.makelist(Samples)
            ResRec['er_specimen_names'] = pmag.makelist(Specimens)
            ResRec['rmag_result_name'] = pmag.makelist(
                Sites) + ":" + pmag.makelist(anitypes)
            ResRec['anisotropy_type'] = pmag.makelist(anitypes)
            ResRec["er_analyst_mail_names"] = user
            ResRec["tilt_correction"] = CS
            if isite == "0":
                ResRec[
                    'result_description'] = "Study average using coordinate system: " + CS
            if isite == "1":
                ResRec[
                    'result_description'] = "Site average using coordinate system: " + CS
            if hpars != [] and ihext == 1:
                HextRec = {}
                for key in ResRec.keys():
                    HextRec[key] = ResRec[key]  # copy over stuff
                HextRec["anisotropy_v1_dec"] = '%7.1f' % (hpars["v1_dec"])
                HextRec["anisotropy_v2_dec"] = '%7.1f' % (hpars["v2_dec"])
                HextRec["anisotropy_v3_dec"] = '%7.1f' % (hpars["v3_dec"])
                HextRec["anisotropy_v1_inc"] = '%7.1f' % (hpars["v1_inc"])
                HextRec["anisotropy_v2_inc"] = '%7.1f' % (hpars["v2_inc"])
                HextRec["anisotropy_v3_inc"] = '%7.1f' % (hpars["v3_inc"])
                HextRec["anisotropy_t1"] = '%10.8f' % (hpars["t1"])
                HextRec["anisotropy_t2"] = '%10.8f' % (hpars["t2"])
                HextRec["anisotropy_t3"] = '%10.8f' % (hpars["t3"])
                HextRec["anisotropy_hext_F"] = '%7.1f ' % (hpars["F"])
                HextRec["anisotropy_hext_F12"] = '%7.1f ' % (hpars["F12"])
                HextRec["anisotropy_hext_F23"] = '%7.1f ' % (hpars["F23"])
                HextRec["anisotropy_v1_eta_semi_angle"] = '%7.1f ' % (
                    hpars["e12"])
                HextRec["anisotropy_v1_eta_dec"] = '%7.1f ' % (hpars["v2_dec"])
                HextRec["anisotropy_v1_eta_inc"] = '%7.1f ' % (hpars["v2_inc"])
                HextRec["anisotropy_v1_zeta_semi_angle"] = '%7.1f ' % (
                    hpars["e13"])
                HextRec["anisotropy_v1_zeta_dec"] = '%7.1f ' % (
                    hpars["v3_dec"])
                HextRec["anisotropy_v1_zeta_inc"] = '%7.1f ' % (
                    hpars["v3_inc"])
                HextRec["anisotropy_v2_eta_semi_angle"] = '%7.1f ' % (
                    hpars["e12"])
                HextRec["anisotropy_v2_eta_dec"] = '%7.1f ' % (hpars["v1_dec"])
                HextRec["anisotropy_v2_eta_inc"] = '%7.1f ' % (hpars["v1_inc"])
                HextRec["anisotropy_v2_zeta_semi_angle"] = '%7.1f ' % (
                    hpars["e23"])
                HextRec["anisotropy_v2_zeta_dec"] = '%7.1f ' % (
                    hpars["v3_dec"])
                HextRec["anisotropy_v2_zeta_inc"] = '%7.1f ' % (
                    hpars["v3_inc"])
                HextRec["anisotropy_v3_eta_semi_angle"] = '%7.1f ' % (
                    hpars["e12"])
                HextRec["anisotropy_v3_eta_dec"] = '%7.1f ' % (hpars["v1_dec"])
                HextRec["anisotropy_v3_eta_inc"] = '%7.1f ' % (hpars["v1_inc"])
                HextRec["anisotropy_v3_zeta_semi_angle"] = '%7.1f ' % (
                    hpars["e23"])
                HextRec["anisotropy_v3_zeta_dec"] = '%7.1f ' % (
                    hpars["v2_dec"])
                HextRec["anisotropy_v3_zeta_inc"] = '%7.1f ' % (
                    hpars["v2_inc"])
                HextRec["magic_method_codes"] = 'LP-AN:AE-H'
                if verbose:
                    print("Hext Statistics: ")
                    print(
                        " tau_i, V_i_D, V_i_I, V_i_zeta, V_i_zeta_D, V_i_zeta_I, V_i_eta, V_i_eta_D, V_i_eta_I"
                    )
                    print(HextRec["anisotropy_t1"],
                          HextRec["anisotropy_v1_dec"],
                          HextRec["anisotropy_v1_inc"],
                          HextRec["anisotropy_v1_eta_semi_angle"],
                          HextRec["anisotropy_v1_eta_dec"],
                          HextRec["anisotropy_v1_eta_inc"],
                          HextRec["anisotropy_v1_zeta_semi_angle"],
                          HextRec["anisotropy_v1_zeta_dec"],
                          HextRec["anisotropy_v1_zeta_inc"])
                    print(HextRec["anisotropy_t2"],
                          HextRec["anisotropy_v2_dec"],
                          HextRec["anisotropy_v2_inc"],
                          HextRec["anisotropy_v2_eta_semi_angle"],
                          HextRec["anisotropy_v2_eta_dec"],
                          HextRec["anisotropy_v2_eta_inc"],
                          HextRec["anisotropy_v2_zeta_semi_angle"],
                          HextRec["anisotropy_v2_zeta_dec"],
                          HextRec["anisotropy_v2_zeta_inc"])
                    print(HextRec["anisotropy_t3"],
                          HextRec["anisotropy_v3_dec"],
                          HextRec["anisotropy_v3_inc"],
                          HextRec["anisotropy_v3_eta_semi_angle"],
                          HextRec["anisotropy_v3_eta_dec"],
                          HextRec["anisotropy_v3_eta_inc"],
                          HextRec["anisotropy_v3_zeta_semi_angle"],
                          HextRec["anisotropy_v3_zeta_dec"],
                          HextRec["anisotropy_v3_zeta_inc"])
                HextRec['magic_software_packages'] = version_num
                ResRecs.append(HextRec)
            if bpars != []:
                BootRec = {}
                for key in ResRec.keys():
                    BootRec[key] = ResRec[key]  # copy over stuff
                BootRec["anisotropy_v1_dec"] = '%7.1f' % (bpars["v1_dec"])
                BootRec["anisotropy_v2_dec"] = '%7.1f' % (bpars["v2_dec"])
                BootRec["anisotropy_v3_dec"] = '%7.1f' % (bpars["v3_dec"])
                BootRec["anisotropy_v1_inc"] = '%7.1f' % (bpars["v1_inc"])
                BootRec["anisotropy_v2_inc"] = '%7.1f' % (bpars["v2_inc"])
                BootRec["anisotropy_v3_inc"] = '%7.1f' % (bpars["v3_inc"])
                BootRec["anisotropy_t1"] = '%10.8f' % (bpars["t1"])
                BootRec["anisotropy_t2"] = '%10.8f' % (bpars["t2"])
                BootRec["anisotropy_t3"] = '%10.8f' % (bpars["t3"])
                BootRec["anisotropy_v1_eta_inc"] = '%7.1f ' % (
                    bpars["v1_eta_inc"])
                BootRec["anisotropy_v1_eta_dec"] = '%7.1f ' % (
                    bpars["v1_eta_dec"])
                BootRec["anisotropy_v1_eta_semi_angle"] = '%7.1f ' % (
                    bpars["v1_eta"])
                BootRec["anisotropy_v1_zeta_inc"] = '%7.1f ' % (
                    bpars["v1_zeta_inc"])
                BootRec["anisotropy_v1_zeta_dec"] = '%7.1f ' % (
                    bpars["v1_zeta_dec"])
                BootRec["anisotropy_v1_zeta_semi_angle"] = '%7.1f ' % (
                    bpars["v1_zeta"])
                BootRec["anisotropy_v2_eta_inc"] = '%7.1f ' % (
                    bpars["v2_eta_inc"])
                BootRec["anisotropy_v2_eta_dec"] = '%7.1f ' % (
                    bpars["v2_eta_dec"])
                BootRec["anisotropy_v2_eta_semi_angle"] = '%7.1f ' % (
                    bpars["v2_eta"])
                BootRec["anisotropy_v2_zeta_inc"] = '%7.1f ' % (
                    bpars["v2_zeta_inc"])
                BootRec["anisotropy_v2_zeta_dec"] = '%7.1f ' % (
                    bpars["v2_zeta_dec"])
                BootRec["anisotropy_v2_zeta_semi_angle"] = '%7.1f ' % (
                    bpars["v2_zeta"])
                BootRec["anisotropy_v3_eta_inc"] = '%7.1f ' % (
                    bpars["v3_eta_inc"])
                BootRec["anisotropy_v3_eta_dec"] = '%7.1f ' % (
                    bpars["v3_eta_dec"])
                BootRec["anisotropy_v3_eta_semi_angle"] = '%7.1f ' % (
                    bpars["v3_eta"])
                BootRec["anisotropy_v3_zeta_inc"] = '%7.1f ' % (
                    bpars["v3_zeta_inc"])
                BootRec["anisotropy_v3_zeta_dec"] = '%7.1f ' % (
                    bpars["v3_zeta_dec"])
                BootRec["anisotropy_v3_zeta_semi_angle"] = '%7.1f ' % (
                    bpars["v3_zeta"])
                BootRec["anisotropy_hext_F"] = ''
                BootRec["anisotropy_hext_F12"] = ''
                BootRec["anisotropy_hext_F23"] = ''
                BootRec[
                    "magic_method_codes"] = 'LP-AN:AE-H:AE-BS'  # regular bootstrap
                if ipar == 1:
                    BootRec[
                        "magic_method_codes"] = 'LP-AN:AE-H:AE-BS-P'  # parametric bootstrap
                if verbose:
                    print("Boostrap Statistics: ")
                    print(
                        " tau_i, V_i_D, V_i_I, V_i_zeta, V_i_zeta_D, V_i_zeta_I, V_i_eta, V_i_eta_D, V_i_eta_I"
                    )
                    print(BootRec["anisotropy_t1"],
                          BootRec["anisotropy_v1_dec"],
                          BootRec["anisotropy_v1_inc"],
                          BootRec["anisotropy_v1_eta_semi_angle"],
                          BootRec["anisotropy_v1_eta_dec"],
                          BootRec["anisotropy_v1_eta_inc"],
                          BootRec["anisotropy_v1_zeta_semi_angle"],
                          BootRec["anisotropy_v1_zeta_dec"],
                          BootRec["anisotropy_v1_zeta_inc"])
                    print(BootRec["anisotropy_t2"],
                          BootRec["anisotropy_v2_dec"],
                          BootRec["anisotropy_v2_inc"],
                          BootRec["anisotropy_v2_eta_semi_angle"],
                          BootRec["anisotropy_v2_eta_dec"],
                          BootRec["anisotropy_v2_eta_inc"],
                          BootRec["anisotropy_v2_zeta_semi_angle"],
                          BootRec["anisotropy_v2_zeta_dec"],
                          BootRec["anisotropy_v2_zeta_inc"])
                    print(BootRec["anisotropy_t3"],
                          BootRec["anisotropy_v3_dec"],
                          BootRec["anisotropy_v3_inc"],
                          BootRec["anisotropy_v3_eta_semi_angle"],
                          BootRec["anisotropy_v3_eta_dec"],
                          BootRec["anisotropy_v3_eta_inc"],
                          BootRec["anisotropy_v3_zeta_semi_angle"],
                          BootRec["anisotropy_v3_zeta_dec"],
                          BootRec["anisotropy_v3_zeta_inc"])
                BootRec['magic_software_packages'] = version_num
                ResRecs.append(BootRec)
            k += 1
            goon = 1
            while goon == 1 and iplot == 1 and verbose:
                if iboot == 1: print("compare with [d]irection ")
                print(
                    " plot [g]reat circle,  change [c]oord. system, change [e]llipse calculation,  s[a]ve plots, [q]uit "
                )
                if isite == 1:
                    print("  [p]revious, [s]ite, [q]uit, <return> for next ")
                ans = input("")
                if ans == "q":
                    sys.exit()
                if ans == "e":
                    iboot, ipar, ihext, ivec = 1, 0, 0, 0
                    e = input("Do Hext Statistics  1/[0]: ")
                    if e == "1": ihext = 1
                    e = input("Suppress bootstrap 1/[0]: ")
                    if e == "1": iboot = 0
                    if iboot == 1:
                        e = input("Parametric bootstrap 1/[0]: ")
                        if e == "1": ipar = 1
                        e = input("Plot bootstrap eigenvectors:  1/[0]: ")
                        if e == "1": ivec = 1
                        if iplot == 1:
                            if inittcdf == 0:
                                ANIS['tcdf'] = 3
                                pmagplotlib.plot_init(ANIS['tcdf'], 5, 5)
                                inittcdf = 1
                    bpars, hpars = pmagplotlib.plotANIS(
                        ANIS, Ss, iboot, ihext, ivec, ipar, title, iplot, comp,
                        vec, Dir, nb)
                    if verbose and plots == 0: pmagplotlib.drawFIGS(ANIS)
                if ans == "c":
                    print("Current Coordinate system is: ")
                    if CS == '-1': print(" Specimen")
                    if CS == '0': print(" Geographic")
                    if CS == '100': print(" Tilt corrected")
                    key = input(
                        " Enter desired coordinate system: [s]pecimen, [g]eographic, [t]ilt corrected "
                    )
                    if key == 's': CS = '-1'
                    if key == 'g': CS = '0'
                    if key == 't': CS = '100'
                    if CS not in orlist:
                        if len(orlist) > 0:
                            CS = orlist[0]
                        else:
                            CS = '-1'
                        if CS == '-1': crd = 's'
                        if CS == '0': crd = 'g'
                        if CS == '100': crd = 't'
                        print(
                            "desired coordinate system not available, using available: ",
                            crd)
                    k -= 1
                    goon = 0
                if ans == "":
                    if isite == 1:
                        goon = 0
                    else:
                        print("Good bye ")
                        sys.exit()
                if ans == 'd':
                    if initcdf == 0:
                        initcdf = 1
                        ANIS['vxcdf'], ANIS['vycdf'], ANIS['vzcdf'] = 4, 5, 6
                        pmagplotlib.plot_init(ANIS['vxcdf'], 5, 5)
                        pmagplotlib.plot_init(ANIS['vycdf'], 5, 5)
                        pmagplotlib.plot_init(ANIS['vzcdf'], 5, 5)
                    Dir, comp = [], 1
                    print("""
                      Input: Vi D I to  compare  eigenvector Vi with direction D/I
                             where Vi=1: principal
                                   Vi=2: major
                                   Vi=3: minor
                                   D= declination of comparison direction
                                   I= inclination of comparison direction""")
                    con = 1
                    while con == 1:
                        try:
                            vdi = input("Vi D I: ").split()
                            vec = int(vdi[0]) - 1
                            Dir = [float(vdi[1]), float(vdi[2])]
                            con = 0
                        except IndexError:
                            print(" Incorrect entry, try again ")
                    bpars, hpars = pmagplotlib.plotANIS(
                        ANIS, Ss, iboot, ihext, ivec, ipar, title, iplot, comp,
                        vec, Dir, nb)
                    Dir, comp = [], 0
                if ans == 'g':
                    con, cnt = 1, 0
                    while con == 1:
                        try:
                            print(
                                " Input:  input pole to great circle ( D I) to  plot a great circle:   "
                            )
                            di = input(" D I: ").split()
                            PDir.append(float(di[0]))
                            PDir.append(float(di[1]))
                            con = 0
                        except:
                            cnt += 1
                            if cnt < 10:
                                print(
                                    " enter the dec and inc of the pole on one line "
                                )
                            else:
                                print(
                                    "ummm - you are doing something wrong - i give up"
                                )
                                sys.exit()
                    pmagplotlib.plotC(ANIS['data'], PDir, 90., 'g')
                    pmagplotlib.plotC(ANIS['conf'], PDir, 90., 'g')
                    if verbose and plots == 0: pmagplotlib.drawFIGS(ANIS)
                if ans == "p":
                    k -= 2
                    goon = 0
                if ans == "q":
                    k = plt
                    goon = 0
                if ans == "s":
                    keepon = 1
                    site = input(" print site or part of site desired: ")
                    while keepon == 1:
                        try:
                            k = sitelist.index(site)
                            keepon = 0
                        except:
                            tmplist = []
                            for qq in range(len(sitelist)):
                                if site in sitelist[qq]:
                                    tmplist.append(sitelist[qq])
                            print(site, " not found, but this was: ")
                            print(tmplist)
                            site = input('Select one or try again\n ')
                            k = sitelist.index(site)
                    goon, ans = 0, ""
                if ans == "a":
                    locs = pmag.makelist(Locs)
                    if pmagplotlib.isServer:  # use server plot naming convention
                        title = "LO:_" + locs + '_SI:__' + '_SA:__SP:__CO:_' + crd
                    else:  # use more readable plot naming convention
                        title = "{}_{}".format(locs, crd)
                    save(ANIS, fmt, title)
                    goon = 0
        else:
            if verbose: print('skipping plot - not enough data points')
            k += 1
#   put rmag_results stuff here
    if len(ResRecs) > 0:
        ResOut, keylist = pmag.fillkeys(ResRecs)
        pmag.magic_write(outfile, ResOut, 'rmag_results')
    if verbose:
        print(" Good bye ")
示例#9
0
def main():
    """
    NAME
        replace_AC_specimens.py
    
    DESCRIPTION
        finds  anisotropy corrected data and 
        replaces that specimen with it.
        puts in pmag_specimen format file
    
    SYNTAX
        replace_AC_specimens.py [command line options]

    OPTIONS
        -h prints help message and quits
        -i allows interactive setting of file names
        -fu TFILE uncorrected pmag_specimen format file with thellier interpretations
            created by thellier_magic_redo.py
        -fc AFILE anisotropy corrected pmag_specimen format file
            created by thellier_magic_redo.py
        -F FILE pmag_specimens format output file 

    DEFAULTS
        TFILE: thellier_specimens.txt
        AFILE: AC_specimens.txt
        FILE: TorAC_specimens.txt
    """
    dir_path='.'
    tspec="thellier_specimens.txt"
    aspec="AC_specimens.txt"
    ofile="TorAC_specimens.txt"
    critfile="pmag_criteria.txt"
    ACSamplist,Samplist,sigmin=[],[],10000
    GoodSamps,SpecOuts=[],[]
# get arguments from command line
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-fu' in sys.argv:
        ind=sys.argv.index('-fu')
        tspec=sys.argv[ind+1]
    if '-fc' in sys.argv:
        ind=sys.argv.index('-fc')
        aspec=sys.argv[ind+1]
    if '-F' in sys.argv:
        ind=sys.argv.index('-F')
        ofile=sys.argv[ind+1]
    if '-WD' in sys.argv:
        ind=sys.argv.index('-WD')
        dir_path=sys.argv[ind+1]
         
    # read in pmag_specimens file
    tspec=dir_path+'/'+tspec
    aspec=dir_path+'/'+aspec
    ofile=dir_path+'/'+ofile
    Specs,file_type=pmag.magic_read(tspec)
    Specs,file_type=pmag.magic_read(tspec)
    Speclist=pmag.get_specs(Specs)
    ACSpecs,file_type=pmag.magic_read(aspec)
    ACspeclist=pmag.get_specs(ACSpecs)
    for spec in Specs:
            if spec["er_sample_name"] not in Samplist:Samplist.append(spec["er_sample_name"])
    for spec in ACSpecs:
            if spec["er_sample_name"] not in ACSamplist:ACSamplist.append(spec["er_sample_name"])
    #
    for samp in Samplist:
        useAC,Ints,ACInts,GoodSpecs,AC,UC=0,[],[],[],[],[]
        for spec in Specs:
            if spec["er_sample_name"].lower()==samp.lower():
                    UC.append(spec)
        if samp in ACSamplist:
            for spec in ACSpecs:
                if spec["er_sample_name"].lower()==samp.lower():
                        AC.append(spec)
        if len(AC)>0:
            AClist=[]
            for spec in AC: 
                SpecOuts.append(spec)
                AClist.append(spec['er_specimen_name'])
                print('using AC: ',spec['er_specimen_name'],'%7.1f'%(1e6*float(spec['specimen_int'])))
            for spec in UC: 
                if spec['er_specimen_name'] not in AClist:
                   SpecOuts.append(spec)
#                   print 'using UC: ',spec['er_specimen_name'],'%7.1f'%(1e6*float(spec['specimen_int']))
        else:
            for spec in UC: 
                SpecOuts.append(spec)
#                print 'using UC: ',spec['er_specimen_name'],'%7.1f'%(1e6*float(spec['specimen_int']))
    SpecOuts,keys=pmag.fillkeys(SpecOuts)
    pmag.magic_write(ofile,SpecOuts,'pmag_specimens')
    print('thellier data assessed for AC correction put in ', ofile)
示例#10
0
def main():
    """
    NAME
	specimens_results_magic.py

    DESCRIPTION
	combines pmag_specimens.txt file with age, location, acceptance criteria and
	outputs pmag_results table along with other MagIC tables necessary for uploading to the database

    SYNTAX
	specimens_results_magic.py [command line options]

    OPTIONS
	-h prints help message and quits
	-usr USER:   identify user, default is ""
	-f: specimen input magic_measurements format file, default is "magic_measurements.txt"
	-fsp: specimen input pmag_specimens format file, default is "pmag_specimens.txt"
	-fsm: sample input er_samples format file, default is "er_samples.txt"
	-fsi: specimen input er_sites format file, default is "er_sites.txt"
	-fla: specify a file with paleolatitudes for calculating VADMs, default is not to calculate VADMS
               format is:  site_name paleolatitude (space delimited file)
	-fa AGES: specify er_ages format file with age information
	-crd [s,g,t,b]:   specify coordinate system
	    (s, specimen, g geographic, t, tilt corrected, b, geographic and tilt corrected)
	    Default is to assume geographic
	    NB: only the tilt corrected data will appear on the results table, if both g and t are selected.
        -cor [AC:CR:NL]: colon delimited list of required data adjustments for all specimens 
            included in intensity calculations (anisotropy, cooling rate, non-linear TRM)
            unless specified, corrections will not be applied
        -pri [TRM:ARM] colon delimited list of priorities for anisotropy correction (-cor must also be set to include AC). default is TRM, then ARM 
	-age MIN MAX UNITS:   specify age boundaries and units
	-exc:  use exiting selection criteria (in pmag_criteria.txt file), default is default criteria
	-C: no acceptance criteria
	-aD:  average directions per sample, default is NOT
	-aI:  average multiple specimen intensities per sample, default is by site 
	-aC:  average all components together, default is NOT
	-pol:  calculate polarity averages
	-sam:  save sample level vgps and v[a]dms, default is by site
	-xSi:  skip the site level intensity calculation
	-p: plot directions and look at intensities by site, default is NOT
	    -fmt: specify output for saved images, default is svg (only if -p set)
	-lat: use present latitude for calculating VADMs, default is not to calculate VADMs
	-xD: skip directions
	-xI: skip intensities
    OUPUT
	writes pmag_samples, pmag_sites, pmag_results tables
    """
    # set defaults
    Comps = []  # list of components
    version_num = pmag.get_version()
    args = sys.argv
    DefaultAge = ["none"]
    skipdirs, coord, excrit, custom, vgps, average, Iaverage, plotsites, opt = 1, 0, 0, 0, 0, 0, 0, 0, 0
    get_model_lat = 0  # this skips VADM calculation altogether, when get_model_lat=1, uses present day
    fmt = 'svg'
    dir_path = "."
    model_lat_file = ""
    Caverage = 0
    infile = 'pmag_specimens.txt'
    measfile = "magic_measurements.txt"
    sampfile = "er_samples.txt"
    sitefile = "er_sites.txt"
    agefile = "er_ages.txt"
    specout = "er_specimens.txt"
    sampout = "pmag_samples.txt"
    siteout = "pmag_sites.txt"
    resout = "pmag_results.txt"
    critout = "pmag_criteria.txt"
    instout = "magic_instruments.txt"
    sigcutoff, OBJ = "", ""
    noDir, noInt = 0, 0
    polarity = 0
    coords = ['0']
    Dcrit, Icrit, nocrit = 0, 0, 0
    corrections = []
    nocorrection = ['DA-NL', 'DA-AC', 'DA-CR']
    priorities = ['DA-AC-ARM',
                  'DA-AC-TRM']  # priorities for anisotropy correction
    # get command line stuff
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if '-WD' in args:
        ind = args.index("-WD")
        dir_path = args[ind + 1]
    if '-cor' in args:
        ind = args.index('-cor')
        cors = args[ind + 1].split(':')  # list of required data adjustments
        for cor in cors:
            nocorrection.remove('DA-' + cor)
            corrections.append('DA-' + cor)
    if '-pri' in args:
        ind = args.index('-pri')
        priorities = args[ind + 1].split(
            ':')  # list of required data adjustments
        for p in priorities:
            p = 'DA-AC-' + p
    if '-f' in args:
        ind = args.index("-f")
        measfile = args[ind + 1]
    if '-fsp' in args:
        ind = args.index("-fsp")
        infile = args[ind + 1]
    if '-fsi' in args:
        ind = args.index("-fsi")
        sitefile = args[ind + 1]
    if "-crd" in args:
        ind = args.index("-crd")
        coord = args[ind + 1]
        if coord == 's': coords = ['-1']
        if coord == 'g': coords = ['0']
        if coord == 't': coords = ['100']
        if coord == 'b': coords = ['0', '100']
    if "-usr" in args:
        ind = args.index("-usr")
        user = sys.argv[ind + 1]
    else:
        user = ""
    if "-C" in args: Dcrit, Icrit, nocrit = 1, 1, 1  # no selection criteria
    if "-sam" in args: vgps = 1  # save sample level VGPS/VADMs
    if "-xSi" in args:
        nositeints = 1  # skip site level intensity
    else:
        nositeints = 0
    if "-age" in args:
        ind = args.index("-age")
        DefaultAge[0] = args[ind + 1]
        DefaultAge.append(args[ind + 2])
        DefaultAge.append(args[ind + 3])
    Daverage, Iaverage, Caverage = 0, 0, 0
    if "-aD" in args: Daverage = 1  # average by sample directions
    if "-aI" in args: Iaverage = 1  # average by sample intensities
    if "-aC" in args:
        Caverage = 1  # average all components together ???  why???
    if "-pol" in args: polarity = 1  # calculate averages by polarity
    if '-xD' in args: noDir = 1
    if '-xI' in args:
        noInt = 1
    elif "-fla" in args:
        if '-lat' in args:
            print "you should set a paleolatitude file OR use present day lat - not both"
            sys.exit()
        ind = args.index("-fla")
        model_lat_file = dir_path + '/' + args[ind + 1]
        get_model_lat = 2
        mlat = open(model_lat_file, 'rU')
        ModelLats = []
        for line in mlat.readlines():
            ModelLat = {}
            tmp = line.split()
            ModelLat["er_site_name"] = tmp[0]
            ModelLat["site_model_lat"] = tmp[1]
            ModelLat["er_sample_name"] = tmp[0]
            ModelLat["sample_lat"] = tmp[1]
            ModelLats.append(ModelLat)
        get_model_lat = 2
    elif '-lat' in args:
        get_model_lat = 1
    if "-p" in args:
        plotsites = 1
        if "-fmt" in args:
            ind = args.index("-fmt")
            fmt = args[ind + 1]
        if noDir == 0:  # plot by site - set up plot window
            import pmagplotlib
            EQ = {}
            EQ['eqarea'] = 1
            pmagplotlib.plot_init(
                EQ['eqarea'], 5, 5)  # define figure 1 as equal area projection
            pmagplotlib.plotNET(
                EQ['eqarea']
            )  # I don't know why this has to be here, but otherwise the first plot never plots...
            pmagplotlib.drawFIGS(EQ)
    if '-WD' in args:
        infile = dir_path + '/' + infile
        measfile = dir_path + '/' + measfile
        instout = dir_path + '/' + instout
        sampfile = dir_path + '/' + sampfile
        sitefile = dir_path + '/' + sitefile
        agefile = dir_path + '/' + agefile
        specout = dir_path + '/' + specout
        sampout = dir_path + '/' + sampout
        siteout = dir_path + '/' + siteout
        resout = dir_path + '/' + resout
        critout = dir_path + '/' + critout
    if "-exc" in args:  # use existing pmag_criteria file
        if "-C" in args:
            print 'you can not use both existing and no criteria - choose either -exc OR -C OR neither (for default)'
            sys.exit()
        crit_data, file_type = pmag.magic_read(critout)
        print "Acceptance criteria read in from ", critout
    else:  # use default criteria (if nocrit set, then get really loose criteria as default)
        crit_data = pmag.default_criteria(nocrit)
        if nocrit == 0:
            print "Acceptance criteria are defaults"
        else:
            print "No acceptance criteria used "
    accept = {}
    for critrec in crit_data:
        for key in critrec.keys():
            # need to migrate specimen_dang to specimen_int_dang for intensity data using old format
            if 'IE-SPEC' in critrec.keys() and 'specimen_dang' in critrec.keys(
            ) and 'specimen_int_dang' not in critrec.keys():
                critrec['specimen_int_dang'] = critrec['specimen_dang']
                del critrec['specimen_dang']
# need to get rid of ron shaars sample_int_sigma_uT
            if 'sample_int_sigma_uT' in critrec.keys():
                critrec['sample_int_sigma'] = '%10.3e' % (
                    eval(critrec['sample_int_sigma_uT']) * 1e-6)
            if key not in accept.keys() and critrec[key] != '':
                accept[key] = critrec[key]
    #
    #
    if "-exc" not in args and "-C" not in args:
        print "args", args
        pmag.magic_write(critout, [accept], 'pmag_criteria')
        print "\n Pmag Criteria stored in ", critout, '\n'
#
# now we're done slow dancing
#
    SiteNFO, file_type = pmag.magic_read(
        sitefile)  # read in site data - has the lats and lons
    SampNFO, file_type = pmag.magic_read(
        sampfile)  # read in site data - has the lats and lons
    height_nfo = pmag.get_dictitem(SiteNFO, 'site_height', '',
                                   'F')  # find all the sites with height info.
    if agefile != "":
        AgeNFO, file_type = pmag.magic_read(
            agefile)  # read in the age information
    Data, file_type = pmag.magic_read(
        infile)  # read in specimen interpretations
    IntData = pmag.get_dictitem(Data, 'specimen_int', '',
                                'F')  # retrieve specimens with intensity data
    comment, orient = "", []
    samples, sites = [], []
    for rec in Data:  # run through the data filling in missing keys and finding all components, coordinates available
        # fill in missing fields, collect unique sample and site names
        if 'er_sample_name' not in rec.keys():
            rec['er_sample_name'] = ""
        elif rec['er_sample_name'] not in samples:
            samples.append(rec['er_sample_name'])
        if 'er_site_name' not in rec.keys():
            rec['er_site_name'] = ""
        elif rec['er_site_name'] not in sites:
            sites.append(rec['er_site_name'])
        if 'specimen_int' not in rec.keys(): rec['specimen_int'] = ''
        if 'specimen_comp_name' not in rec.keys(
        ) or rec['specimen_comp_name'] == "":
            rec['specimen_comp_name'] = 'A'
        if rec['specimen_comp_name'] not in Comps:
            Comps.append(rec['specimen_comp_name'])
        rec['specimen_tilt_correction'] = rec[
            'specimen_tilt_correction'].strip('\n')
        if "specimen_tilt_correction" not in rec.keys():
            rec["specimen_tilt_correction"] = "-1"  # assume sample coordinates
        if rec["specimen_tilt_correction"] not in orient:
            orient.append(rec["specimen_tilt_correction"]
                          )  # collect available coordinate systems
        if "specimen_direction_type" not in rec.keys():
            rec["specimen_direction_type"] = 'l'  # assume direction is line - not plane
        if "specimen_dec" not in rec.keys():
            rec["specimen_direction_type"] = ''  # if no declination, set direction type to blank
        if "specimen_n" not in rec.keys(): rec["specimen_n"] = ''  # put in n
        if "specimen_alpha95" not in rec.keys():
            rec["specimen_alpha95"] = ''  # put in alpha95
        if "magic_method_codes" not in rec.keys():
            rec["magic_method_codes"] = ''
    #
    # start parsing data into SpecDirs, SpecPlanes, SpecInts
    SpecInts, SpecDirs, SpecPlanes = [], [], []
    samples.sort()  # get sorted list of samples and sites
    sites.sort()
    if noInt == 0:  # don't skip intensities
        IntData = pmag.get_dictitem(
            Data, 'specimen_int', '',
            'F')  # retrieve specimens with intensity data
        if nocrit == 0:  # use selection criteria
            for rec in IntData:  # do selection criteria
                kill = pmag.grade(rec, accept, 'specimen_int')
                if len(kill) == 0:
                    SpecInts.append(
                        rec
                    )  # intensity record to be included in sample, site calculations
        else:
            SpecInts = IntData[:]  # take everything - no selection criteria
# check for required data adjustments
        if len(corrections) > 0 and len(SpecInts) > 0:
            for cor in corrections:
                SpecInts = pmag.get_dictitem(
                    SpecInts, 'magic_method_codes', cor,
                    'has')  # only take specimens with the required corrections
        if len(nocorrection) > 0 and len(SpecInts) > 0:
            for cor in nocorrection:
                SpecInts = pmag.get_dictitem(
                    SpecInts, 'magic_method_codes', cor, 'not'
                )  # exclude the corrections not specified for inclusion
# take top priority specimen of its name in remaining specimens (only one per customer)
        PrioritySpecInts = []
        specimens = pmag.get_specs(SpecInts)  # get list of uniq specimen names
        for spec in specimens:
            ThisSpecRecs = pmag.get_dictitem(
                SpecInts, 'er_specimen_name', spec,
                'T')  # all the records for this specimen
            if len(ThisSpecRecs) == 1:
                PrioritySpecInts.append(ThisSpecRecs[0])
            elif len(ThisSpecRecs) > 1:  # more than one
                prec = []
                for p in priorities:
                    ThisSpecRecs = pmag.get_dictitem(
                        SpecInts, 'magic_method_codes', p,
                        'has')  # all the records for this specimen
                    if len(ThisSpecRecs) > 0: prec.append(ThisSpecRecs[0])
                PrioritySpecInts.append(prec[0])  # take the best one
        SpecInts = PrioritySpecInts  # this has the first specimen record
    if noDir == 0:  # don't skip directions
        AllDirs = pmag.get_dictitem(
            Data, 'specimen_direction_type', '',
            'F')  # retrieve specimens with directed lines and planes
        Ns = pmag.get_dictitem(
            AllDirs, 'specimen_n', '',
            'F')  # get all specimens with specimen_n information
        if nocrit != 1:  # use selection criteria
            for rec in Ns:  # look through everything with specimen_n for "good" data
                kill = pmag.grade(rec, accept, 'specimen_dir')
                if len(kill) == 0:  # nothing killed it
                    SpecDirs.append(rec)
        else:  # no criteria
            SpecDirs = AllDirs[:]  # take them all
# SpecDirs is now the list of all specimen directions (lines and planes) that pass muster
#
    PmagSamps, SampDirs = [], [
    ]  # list of all sample data and list of those that pass the DE-SAMP criteria
    PmagSites, PmagResults = [], [
    ]  # list of all site data and selected results
    SampInts = []
    for samp in samples:  # run through the sample names
        if Daverage == 1:  #  average by sample if desired
            SampDir = pmag.get_dictitem(
                SpecDirs, 'er_sample_name', samp,
                'T')  # get all the directional data for this sample
            if len(SampDir) > 0:  # there are some directions
                for coord in coords:  # step through desired coordinate systems
                    CoordDir = pmag.get_dictitem(
                        SampDir, 'specimen_tilt_correction', coord,
                        'T')  # get all the directions for this sample
                    if len(CoordDir
                           ) > 0:  # there are some with this coordinate system
                        if Caverage == 0:  # look component by component
                            for comp in Comps:
                                CompDir = pmag.get_dictitem(
                                    CoordDir, 'specimen_comp_name', comp, 'T'
                                )  # get all directions from this component
                                if len(CompDir) > 0:  # there are some
                                    PmagSampRec = pmag.lnpbykey(
                                        CompDir, 'sample', 'specimen'
                                    )  # get a sample average from all specimens
                                    PmagSampRec["er_location_name"] = CompDir[0][
                                        'er_location_name']  # decorate the sample record
                                    PmagSampRec["er_site_name"] = CompDir[0][
                                        'er_site_name']
                                    PmagSampRec["er_sample_name"] = samp
                                    PmagSampRec[
                                        "er_citation_names"] = "This study"
                                    PmagSampRec["er_analyst_mail_names"] = user
                                    PmagSampRec[
                                        'magic_software_packages'] = version_num
                                    if nocrit != 1:
                                        PmagSampRec[
                                            'pmag_criteria_codes'] = "ACCEPT"
                                    if agefile != "":
                                        PmagSampRec = pmag.get_age(
                                            PmagSampRec, "er_site_name",
                                            "sample_inferred_", AgeNFO,
                                            DefaultAge)
                                    site_height = pmag.get_dictitem(
                                        height_nfo, 'er_site_name',
                                        PmagSampRec['er_site_name'], 'T')
                                    if len(site_height) > 0:
                                        PmagSampRec[
                                            "sample_height"] = site_height[0][
                                                'site_height']  # add in height if available
                                    PmagSampRec['sample_comp_name'] = comp
                                    PmagSampRec[
                                        'sample_tilt_correction'] = coord
                                    PmagSampRec[
                                        'er_specimen_names'] = pmag.get_list(
                                            CompDir, 'er_specimen_name'
                                        )  # get a list of the specimen names used
                                    PmagSampRec[
                                        'magic_method_codes'] = pmag.get_list(
                                            CompDir, 'magic_method_codes'
                                        )  # get a list of the methods used
                                    if nocrit != 1:  # apply selection criteria
                                        kill = pmag.grade(
                                            PmagSampRec, accept, 'sample_dir')
                                    else:
                                        kill = []
                                    if len(kill) == 0:
                                        SampDirs.append(PmagSampRec)
                                        if vgps == 1:  # if sample level VGP info desired, do that now
                                            PmagResRec = pmag.getsampVGP(
                                                PmagSampRec, SiteNFO)
                                            if PmagResRec != "":
                                                PmagResults.append(PmagResRec)
                                        PmagSamps.append(PmagSampRec)
                        if Caverage == 1:  # average all components together  basically same as above
                            PmagSampRec = pmag.lnpbykey(
                                CoordDir, 'sample', 'specimen')
                            PmagSampRec["er_location_name"] = CoordDir[0][
                                'er_location_name']
                            PmagSampRec["er_site_name"] = CoordDir[0][
                                'er_site_name']
                            PmagSampRec["er_sample_name"] = samp
                            PmagSampRec["er_citation_names"] = "This study"
                            PmagSampRec["er_analyst_mail_names"] = user
                            PmagSampRec[
                                'magic_software_packages'] = version_num
                            if nocrit != 1:
                                PmagSampRec['pmag_criteria_codes'] = ""
                            if agefile != "":
                                PmagSampRec = pmag.get_age(
                                    PmagSampRec, "er_site_name",
                                    "sample_inferred_", AgeNFO, DefaultAge)
                            site_height = pmag.get_dictitem(
                                height_nfo, 'er_site_name', site, 'T')
                            if len(site_height) > 0:
                                PmagSampRec["sample_height"] = site_height[0][
                                    'site_height']  # add in height if available
                            PmagSampRec['sample_tilt_correction'] = coord
                            PmagSampRec['sample_comp_name'] = pmag.get_list(
                                CoordDir,
                                'specimen_comp_name')  # get components used
                            PmagSampRec['er_specimen_names'] = pmag.get_list(
                                CoordDir, 'er_specimen_name'
                            )  # get specimne names averaged
                            PmagSampRec['magic_method_codes'] = pmag.get_list(
                                CoordDir,
                                'magic_method_codes')  # assemble method codes
                            if nocrit != 1:  # apply selection criteria
                                kill = pmag.grade(PmagSampRec, accept,
                                                  'sample_dir')
                                if len(kill) == 0:  # passes the mustard
                                    SampDirs.append(PmagSampRec)
                                    if vgps == 1:
                                        PmagResRec = pmag.getsampVGP(
                                            PmagSampRec, SiteNFO)
                                        if PmagResRec != "":
                                            PmagResults.append(PmagResRec)
                            else:  # take everything
                                SampDirs.append(PmagSampRec)
                                if vgps == 1:
                                    PmagResRec = pmag.getsampVGP(
                                        PmagSampRec, SiteNFO)
                                    if PmagResRec != "":
                                        PmagResults.append(PmagResRec)
                            PmagSamps.append(PmagSampRec)
        if Iaverage == 1:  #  average by sample if desired
            SampI = pmag.get_dictitem(
                SpecInts, 'er_sample_name', samp,
                'T')  # get all the intensity data for this sample
            if len(SampI) > 0:  # there are some
                PmagSampRec = pmag.average_int(
                    SampI, 'specimen', 'sample')  # get average intensity stuff
                PmagSampRec[
                    "sample_description"] = "sample intensity"  # decorate sample record
                PmagSampRec["sample_direction_type"] = ""
                PmagSampRec['er_site_name'] = SampI[0]["er_site_name"]
                PmagSampRec['er_sample_name'] = samp
                PmagSampRec['er_location_name'] = SampI[0]["er_location_name"]
                PmagSampRec["er_citation_names"] = "This study"
                PmagSampRec["er_analyst_mail_names"] = user
                if agefile != "":
                    PmagSampRec = pmag.get_age(PmagSampRec, "er_site_name",
                                               "sample_inferred_", AgeNFO,
                                               DefaultAge)
                site_height = pmag.get_dictitem(height_nfo, 'er_site_name',
                                                PmagSampRec['er_site_name'],
                                                'T')
                if len(site_height) > 0:
                    PmagSampRec["sample_height"] = site_height[0][
                        'site_height']  # add in height if available
                PmagSampRec['er_specimen_names'] = pmag.get_list(
                    SampI, 'er_specimen_name')
                PmagSampRec['magic_method_codes'] = pmag.get_list(
                    SampI, 'magic_method_codes')
                if nocrit != 1:  # apply criteria!
                    kill = pmag.grade(PmagSampRec, accept, 'sample_int')
                    if len(kill) == 0:
                        PmagSampRec['pmag_criteria_codes'] = "ACCEPT"
                        SampInts.append(PmagSampRec)
                        PmagSamps.append(PmagSampRec)
                    else:
                        PmagSampRec = {}  # sample rejected
                else:  # no criteria
                    SampInts.append(PmagSampRec)
                    PmagSamps.append(PmagSampRec)
                    PmagSampRec['pmag_criteria_codes'] = ""
                if vgps == 1 and get_model_lat != 0 and PmagSampRec != {}:  #
                    if get_model_lat == 1:  # use sample latitude
                        PmagResRec = pmag.getsampVDM(PmagSampRec, SampNFO)
                        del (PmagResRec['model_lat']
                             )  # get rid of the model lat key
                    elif get_model_lat == 2:  # use model latitude
                        PmagResRec = pmag.getsampVDM(PmagSampRec, ModelLats)
                        if PmagResRec != {}:
                            PmagResRec['magic_method_codes'] = PmagResRec[
                                'magic_method_codes'] + ":IE-MLAT"
                    if PmagResRec != {}:
                        PmagResRec['er_specimen_names'] = PmagSampRec[
                            'er_specimen_names']
                        PmagResRec['er_sample_names'] = PmagSampRec[
                            'er_sample_name']
                        PmagResRec['pmag_criteria_codes'] = 'ACCEPT'
                        PmagResRec['average_int_sigma_perc'] = PmagSampRec[
                            'sample_int_sigma_perc']
                        PmagResRec['average_int_sigma'] = PmagSampRec[
                            'sample_int_sigma']
                        PmagResRec['average_int_n'] = PmagSampRec[
                            'sample_int_n']
                        PmagResRec['vadm_n'] = PmagSampRec['sample_int_n']
                        PmagResRec['data_type'] = 'i'
                        PmagResults.append(PmagResRec)
    if len(PmagSamps) > 0:
        TmpSamps, keylist = pmag.fillkeys(
            PmagSamps)  # fill in missing keys from different types of records
        pmag.magic_write(sampout, TmpSamps,
                         'pmag_samples')  # save in sample output file
        print ' sample averages written to ', sampout

#
#create site averages from specimens or samples as specified
#
    for site in sites:
        if Daverage == 0:
            key, dirlist = 'specimen', SpecDirs  # if specimen averages at site level desired
        if Daverage == 1:
            key, dirlist = 'sample', SampDirs  # if sample averages at site level desired
        tmp = pmag.get_dictitem(dirlist, 'er_site_name', site,
                                'T')  # get all the sites with  directions
        tmp1 = pmag.get_dictitem(
            tmp, key + '_tilt_correction', coords[-1],
            'T')  # use only the last coordinate if Caverage==0
        sd = pmag.get_dictitem(
            SiteNFO, 'er_site_name', site,
            'T')  # fish out site information (lat/lon, etc.)
        if len(sd) > 0:
            sitedat = sd[0]
            if Caverage == 0:  # do component wise averaging
                for comp in Comps:
                    siteD = pmag.get_dictitem(tmp1, key + '_comp_name', comp,
                                              'T')  # get all components comp
                    if len(
                            siteD
                    ) > 0:  # there are some for this site and component name
                        PmagSiteRec = pmag.lnpbykey(
                            siteD, 'site', key)  # get an average for this site
                        PmagSiteRec[
                            'site_comp_name'] = comp  # decorate the site record
                        PmagSiteRec["er_location_name"] = siteD[0][
                            'er_location_name']
                        PmagSiteRec["er_site_name"] = siteD[0]['er_site_name']
                        PmagSiteRec['site_tilt_correction'] = coords[-1]
                        PmagSiteRec['site_comp_name'] = pmag.get_list(
                            siteD, key + '_comp_name')
                        if Daverage == 1:
                            PmagSiteRec['er_sample_names'] = pmag.get_list(
                                siteD, 'er_sample_name')
                        else:
                            PmagSiteRec['er_specimen_names'] = pmag.get_list(
                                siteD, 'er_specimen_name')


# determine the demagnetization code (DC3,4 or 5) for this site
                        AFnum = len(
                            pmag.get_dictitem(siteD, 'magic_method_codes',
                                              'LP-DIR-AF', 'has'))
                        Tnum = len(
                            pmag.get_dictitem(siteD, 'magic_method_codes',
                                              'LP-DIR-T', 'has'))
                        DC = 3
                        if AFnum > 0: DC += 1
                        if Tnum > 0: DC += 1
                        PmagSiteRec['magic_method_codes'] = pmag.get_list(
                            siteD,
                            'magic_method_codes') + ':' + 'LP-DC' + str(DC)
                        PmagSiteRec['magic_method_codes'].strip(":")
                        if plotsites == 1:
                            print PmagSiteRec['er_site_name']
                            pmagplotlib.plotSITE(EQ['eqarea'], PmagSiteRec,
                                                 siteD,
                                                 key)  # plot and list the data
                            pmagplotlib.drawFIGS(EQ)
                        PmagSites.append(PmagSiteRec)
            else:  # last component only
                siteD = tmp1[:]  # get the last orientation system specified
                if len(siteD) > 0:  # there are some
                    PmagSiteRec = pmag.lnpbykey(
                        siteD, 'site', key)  # get the average for this site
                    PmagSiteRec["er_location_name"] = siteD[0][
                        'er_location_name']  # decorate the record
                    PmagSiteRec["er_site_name"] = siteD[0]['er_site_name']
                    PmagSiteRec['site_comp_name'] = comp
                    PmagSiteRec['site_tilt_correction'] = coords[-1]
                    PmagSiteRec['site_comp_name'] = pmag.get_list(
                        siteD, key + '_comp_name')
                    PmagSiteRec['er_specimen_names'] = pmag.get_list(
                        siteD, 'er_specimen_name')
                    PmagSiteRec['er_sample_names'] = pmag.get_list(
                        siteD, 'er_sample_name')
                    AFnum = len(
                        pmag.get_dictitem(siteD, 'magic_method_codes',
                                          'LP-DIR-AF', 'has'))
                    Tnum = len(
                        pmag.get_dictitem(siteD, 'magic_method_codes',
                                          'LP-DIR-T', 'has'))
                    DC = 3
                    if AFnum > 0: DC += 1
                    if Tnum > 0: DC += 1
                    PmagSiteRec['magic_method_codes'] = pmag.get_list(
                        siteD, 'magic_method_codes') + ':' + 'LP-DC' + str(DC)
                    PmagSiteRec['magic_method_codes'].strip(":")
                    if Daverage == 0:
                        PmagSiteRec['site_comp_name'] = pmag.get_list(
                            siteD, key + '_comp_name')
                    if plotsites == 1:
                        pmagplotlib.plotSITE(EQ['eqarea'], PmagSiteRec, siteD,
                                             key)
                        pmagplotlib.drawFIGS(EQ)
                    PmagSites.append(PmagSiteRec)
        else:
            print 'site information not found in er_sites for site, ', site, ' site will be skipped'
    for PmagSiteRec in PmagSites:  # now decorate each dictionary some more, and calculate VGPs etc. for results table
        PmagSiteRec["er_citation_names"] = "This study"
        PmagSiteRec["er_analyst_mail_names"] = user
        PmagSiteRec['magic_software_packages'] = version_num
        if agefile != "":
            PmagSiteRec = pmag.get_age(PmagSiteRec, "er_site_name",
                                       "site_inferred_", AgeNFO, DefaultAge)
        PmagSiteRec['pmag_criteria_codes'] = 'ACCEPT'
        if 'site_n_lines' in PmagSiteRec.keys(
        ) and 'site_n_planes' in PmagSiteRec.keys() and PmagSiteRec[
                'site_n_lines'] != "" and PmagSiteRec['site_n_planes'] != "":
            if int(PmagSiteRec["site_n_planes"]) > 0:
                PmagSiteRec["magic_method_codes"] = PmagSiteRec[
                    'magic_method_codes'] + ":DE-FM-LP"
            elif int(PmagSiteRec["site_n_lines"]) > 2:
                PmagSiteRec["magic_method_codes"] = PmagSiteRec[
                    'magic_method_codes'] + ":DE-FM"
            kill = pmag.grade(PmagSiteRec, accept, 'site_dir')
            if len(kill) == 0:
                PmagResRec = {
                }  # set up dictionary for the pmag_results table entry
                PmagResRec['data_type'] = 'i'  # decorate it a bit
                PmagResRec['magic_software_packages'] = version_num
                PmagSiteRec[
                    'site_description'] = 'Site direction included in results table'
                PmagResRec['pmag_criteria_codes'] = 'ACCEPT'
                dec = float(PmagSiteRec["site_dec"])
                inc = float(PmagSiteRec["site_inc"])
                if 'site_alpha95' in PmagSiteRec.keys(
                ) and PmagSiteRec['site_alpha95'] != "":
                    a95 = float(PmagSiteRec["site_alpha95"])
                else:
                    a95 = 180.
                sitedat = pmag.get_dictitem(
                    SiteNFO, 'er_site_name', PmagSiteRec['er_site_name'],
                    'T')[0]  # fish out site information (lat/lon, etc.)
                lat = float(sitedat['site_lat'])
                lon = float(sitedat['site_lon'])
                plong, plat, dp, dm = pmag.dia_vgp(
                    dec, inc, a95, lat, lon)  # get the VGP for this site
                if PmagSiteRec['site_tilt_correction'] == '-1':
                    C = ' (spec coord) '
                if PmagSiteRec['site_tilt_correction'] == '0':
                    C = ' (geog. coord) '
                if PmagSiteRec['site_tilt_correction'] == '100':
                    C = ' (strat. coord) '
                PmagResRec["pmag_result_name"] = "VGP Site: " + PmagSiteRec[
                    "er_site_name"]  # decorate some more
                PmagResRec[
                    "result_description"] = "Site VGP, coord system = " + str(
                        coord) + ' component: ' + comp
                PmagResRec['er_site_names'] = PmagSiteRec['er_site_name']
                PmagResRec['pmag_criteria_codes'] = 'ACCEPT'
                PmagResRec['er_citation_names'] = 'This study'
                PmagResRec['er_analyst_mail_names'] = user
                PmagResRec["er_location_names"] = PmagSiteRec[
                    "er_location_name"]
                if Daverage == 1:
                    PmagResRec["er_sample_names"] = PmagSiteRec[
                        "er_sample_names"]
                else:
                    PmagResRec["er_specimen_names"] = PmagSiteRec[
                        "er_specimen_names"]
                PmagResRec["tilt_correction"] = PmagSiteRec[
                    'site_tilt_correction']
                PmagResRec["pole_comp_name"] = PmagSiteRec['site_comp_name']
                PmagResRec["average_dec"] = PmagSiteRec["site_dec"]
                PmagResRec["average_inc"] = PmagSiteRec["site_inc"]
                PmagResRec["average_alpha95"] = PmagSiteRec["site_alpha95"]
                PmagResRec["average_n"] = PmagSiteRec["site_n"]
                PmagResRec["average_n_lines"] = PmagSiteRec["site_n_lines"]
                PmagResRec["average_n_planes"] = PmagSiteRec["site_n_planes"]
                PmagResRec["vgp_n"] = PmagSiteRec["site_n"]
                PmagResRec["average_k"] = PmagSiteRec["site_k"]
                PmagResRec["average_r"] = PmagSiteRec["site_r"]
                PmagResRec["average_lat"] = '%10.4f ' % (lat)
                PmagResRec["average_lon"] = '%10.4f ' % (lon)
                if agefile != "":
                    PmagResRec = pmag.get_age(PmagResRec, "er_site_names",
                                              "average_", AgeNFO, DefaultAge)
                site_height = pmag.get_dictitem(height_nfo, 'er_site_name',
                                                site, 'T')
                if len(site_height) > 0:
                    PmagResRec["average_height"] = site_height[0][
                        'site_height']
                PmagResRec["vgp_lat"] = '%7.1f ' % (plat)
                PmagResRec["vgp_lon"] = '%7.1f ' % (plong)
                PmagResRec["vgp_dp"] = '%7.1f ' % (dp)
                PmagResRec["vgp_dm"] = '%7.1f ' % (dm)
                PmagResRec["magic_method_codes"] = PmagSiteRec[
                    "magic_method_codes"]
                if PmagSiteRec['site_tilt_correction'] == '0':
                    PmagSiteRec['magic_method_codes'] = PmagSiteRec[
                        'magic_method_codes'] + ":DA-DIR-GEO"
                if PmagSiteRec['site_tilt_correction'] == '100':
                    PmagSiteRec['magic_method_codes'] = PmagSiteRec[
                        'magic_method_codes'] + ":DA-DIR-TILT"
                PmagSiteRec['site_polarity'] = ""
                if polarity == 1:  # assign polarity based on angle of pole lat to spin axis - may want to re-think this sometime
                    angle = pmag.angle([0, 0], [0, (90 - plat)])
                    if angle <= 55.: PmagSiteRec["site_polarity"] = 'n'
                    if angle > 55. and angle < 125.:
                        PmagSiteRec["site_polarity"] = 't'
                    if angle >= 125.: PmagSiteRec["site_polarity"] = 'r'
                PmagResults.append(PmagResRec)
    if polarity == 1:
        crecs = pmag.get_dictitem(PmagSites, 'site_tilt_correction', '100',
                                  'T')  # find the tilt corrected data
        if len(crecs) < 2:
            crecs = pmag.get_dictitem(
                PmagSites, 'site_tilt_correction', '0',
                'T')  # if there aren't any, find the geographic corrected data
        if len(crecs) > 2:  # if there are some,
            comp = pmag.get_list(
                crecs,
                'site_comp_name').split(':')[0]  # find the first component
            crecs = pmag.get_dictitem(
                crecs, 'site_comp_name', comp,
                'T')  # fish out all of the first component
            precs = []
            for rec in crecs:
                precs.append({
                    'dec': rec['site_dec'],
                    'inc': rec['site_inc'],
                    'name': rec['er_site_name'],
                    'loc': rec['er_location_name']
                })
            polpars = pmag.fisher_by_pol(
                precs)  # calculate average by polarity
            for mode in polpars.keys(
            ):  # hunt through all the modes (normal=A, reverse=B, all=ALL)
                PolRes = {}
                PolRes['er_citation_names'] = 'This study'
                PolRes[
                    "pmag_result_name"] = "Polarity Average: Polarity " + mode  #
                PolRes["data_type"] = "a"
                PolRes["average_dec"] = '%7.1f' % (polpars[mode]['dec'])
                PolRes["average_inc"] = '%7.1f' % (polpars[mode]['inc'])
                PolRes["average_n"] = '%i' % (polpars[mode]['n'])
                PolRes["average_r"] = '%5.4f' % (polpars[mode]['r'])
                PolRes["average_k"] = '%6.0f' % (polpars[mode]['k'])
                PolRes["average_alpha95"] = '%7.1f' % (
                    polpars[mode]['alpha95'])
                PolRes['er_site_names'] = polpars[mode]['sites']
                PolRes['er_location_names'] = polpars[mode]['locs']
                PolRes['magic_software_packages'] = version_num
                PmagResults.append(PolRes)

    if noInt != 1 and nositeints != 1:
        for site in sites:  # now do intensities for each site
            if plotsites == 1: print site
            if Iaverage == 0:
                key, intlist = 'specimen', SpecInts  # if using specimen level data
            if Iaverage == 1:
                key, intlist = 'sample', PmagSamps  # if using sample level data
            Ints = pmag.get_dictitem(
                intlist, 'er_site_name', site,
                'T')  # get all the intensities  for this site
            if len(Ints) > 0:  # there are some
                PmagSiteRec = pmag.average_int(
                    Ints, key,
                    'site')  # get average intensity stuff for site table
                PmagResRec = pmag.average_int(
                    Ints, key,
                    'average')  # get average intensity stuff for results table
                if plotsites == 1:  # if site by site examination requested - print this site out to the screen
                    for rec in Ints:
                        print rec['er_' + key + '_name'], ' %7.1f' % (
                            1e6 * float(rec[key + '_int']))
                    if len(Ints) > 1:
                        print 'Average: ', '%7.1f' % (1e6 * float(
                            PmagResRec['average_int'])), 'N: ', len(Ints)
                        print 'Sigma: ', '%7.1f' % (
                            1e6 * float(PmagResRec['average_int_sigma'])
                        ), 'Sigma %: ', PmagResRec['average_int_sigma_perc']
                    raw_input('Press any key to continue\n')
                er_location_name = Ints[0]["er_location_name"]
                PmagSiteRec[
                    "er_location_name"] = er_location_name  # decorate the records
                PmagSiteRec["er_citation_names"] = "This study"
                PmagResRec["er_location_names"] = er_location_name
                PmagResRec["er_citation_names"] = "This study"
                PmagSiteRec["er_analyst_mail_names"] = user
                PmagResRec["er_analyst_mail_names"] = user
                PmagResRec["data_type"] = 'i'
                if Iaverage == 0:
                    PmagSiteRec['er_specimen_names'] = pmag.get_list(
                        Ints, 'er_specimen_name')  # list of all specimens used
                    PmagResRec['er_specimen_names'] = pmag.get_list(
                        Ints, 'er_specimen_name')
                PmagSiteRec['er_sample_names'] = pmag.get_list(
                    Ints, 'er_sample_name')  # list of all samples used
                PmagResRec['er_sample_names'] = pmag.get_list(
                    Ints, 'er_sample_name')
                PmagSiteRec['er_site_name'] = site
                PmagResRec['er_site_names'] = site
                PmagSiteRec['magic_method_codes'] = pmag.get_list(
                    Ints, 'magic_method_codes')
                PmagResRec['magic_method_codes'] = pmag.get_list(
                    Ints, 'magic_method_codes')
                kill = pmag.grade(PmagSiteRec, accept, 'site_int')
                if nocrit == 1 or len(kill) == 0:
                    b, sig = float(PmagResRec['average_int']), ""
                    if (PmagResRec['average_int_sigma']) != "":
                        sig = float(PmagResRec['average_int_sigma'])
                    sdir = pmag.get_dictitem(PmagResults, 'er_site_names',
                                             site,
                                             'T')  # fish out site direction
                    if len(sdir) > 0 and sdir[-1][
                            'average_inc'] != "":  # get the VDM for this record using last average inclination (hope it is the right one!)
                        inc = float(sdir[0]['average_inc'])  #
                        mlat = pmag.magnetic_lat(
                            inc)  # get magnetic latitude using dipole formula
                        PmagResRec["vdm"] = '%8.3e ' % (pmag.b_vdm(
                            b, mlat))  # get VDM with magnetic latitude
                        PmagResRec["vdm_n"] = PmagResRec['average_int_n']
                        if 'average_int_sigma' in PmagResRec.keys(
                        ) and PmagResRec['average_int_sigma'] != "":
                            vdm_sig = pmag.b_vdm(
                                float(PmagResRec['average_int_sigma']), mlat)
                            PmagResRec["vdm_sigma"] = '%8.3e ' % (vdm_sig)
                        else:
                            PmagResRec["vdm_sigma"] = ""
                    mlat = ""  # define a model latitude
                    if get_model_lat == 1:  # use present site latitude
                        mlats = pmag.get_dictitem(SiteNFO, 'er_site_name',
                                                  site, 'T')
                        if len(mlats) > 0: mlat = mlats[0]['site_lat']
                    elif get_model_lat == 2:  # use a model latitude from some plate reconstruction model (or something)
                        mlats = pmag.get_dictitem(ModelLats, 'er_site_name',
                                                  site, 'T')
                        if len(mlats) > 0:
                            PmagResRec['model_lat'] = mlats[0][
                                'site_model_lat']
                        mlat = PmagResRec['model_lat']
                    if mlat != "":
                        PmagResRec["vadm"] = '%8.3e ' % (
                            pmag.b_vdm(b, float(mlat))
                        )  # get the VADM using the desired latitude
                        if sig != "":
                            vdm_sig = pmag.b_vdm(
                                float(PmagResRec['average_int_sigma']),
                                float(mlat))
                            PmagResRec["vadm_sigma"] = '%8.3e ' % (vdm_sig)
                            PmagResRec["vadm_n"] = PmagResRec['average_int_n']
                        else:
                            PmagResRec["vadm_sigma"] = ""
                    sitedat = pmag.get_dictitem(
                        SiteNFO, 'er_site_name', PmagSiteRec['er_site_name'],
                        'T')  # fish out site information (lat/lon, etc.)
                    if len(sitedat) > 0:
                        sitedat = sitedat[0]
                        PmagResRec['average_lat'] = sitedat['site_lat']
                        PmagResRec['average_lon'] = sitedat['site_lon']
                    else:
                        PmagResRec['average_lon'] = 'UNKNOWN'
                        PmagResRec['average_lon'] = 'UNKNOWN'
                    PmagResRec['magic_software_packages'] = version_num
                    PmagResRec["pmag_result_name"] = "V[A]DM: Site " + site
                    PmagResRec["result_description"] = "V[A]DM of site"
                    PmagResRec["pmag_criteria_codes"] = "ACCEPT"
                    if agefile != "":
                        PmagResRec = pmag.get_age(PmagResRec, "er_site_names",
                                                  "average_", AgeNFO,
                                                  DefaultAge)
                    site_height = pmag.get_dictitem(height_nfo, 'er_site_name',
                                                    site, 'T')
                    if len(site_height) > 0:
                        PmagResRec["average_height"] = site_height[0][
                            'site_height']
                    PmagSites.append(PmagSiteRec)
                    PmagResults.append(PmagResRec)
    if len(PmagSites) > 0:
        Tmp, keylist = pmag.fillkeys(PmagSites)
        pmag.magic_write(siteout, Tmp, 'pmag_sites')
        print ' sites written to ', siteout
    else:
        print "No Site level table"
    if len(PmagResults) > 0:
        TmpRes, keylist = pmag.fillkeys(PmagResults)
        pmag.magic_write(resout, TmpRes, 'pmag_results')
        print ' results written to ', resout
    else:
        print "No Results level table"
示例#11
0
def main(command_line=True, **kwargs):
    """
    NAME
        iodp_dscr_magic.py

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


    SYNTAX
        iodp_descr_magic.py [command line options]

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

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

    # format variables

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

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


    SYNTAX
        iodp_srm_magic.py [command line options]

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

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

    # format variables

    meas_file = os.path.join(output_dir_path, meas_file)
    spec_file = os.path.join(output_dir_path, spec_file)
    Specs,file_type = pmag.magic_read(spec_file)
    samp_file = os.path.join(output_dir_path, samp_file)
    ErSamps,file_type = pmag.magic_read(samp_file)
    site_file = os.path.join(output_dir_path, site_file)
    if csv_file=="":
        filelist=os.listdir(input_dir_path) # read in list of files to import
    else:
        csv_file = os.path.join(input_dir_path, csv_file)
        filelist=[csv_file]


    # parsing the data
    specimens,samples,sites=[],[],[]
    MagRecs,SpecRecs,SampRecs,SiteRecs=[],[],[],[]
    for samp in ErSamps:
        if samp['er_sample_name'] not in samples:
            samples.append(samp['er_sample_name'])
            SampRecs.append(samp)
    file_found = False
    for f in filelist: # parse each file
        if f[-3:].lower()=='csv':
            file_found = True
            print('processing: ',f)
            full_file = os.path.join(input_dir_path, f)
            with open(full_file, 'r') as fin:
                file_input = fin.readlines()
            keys=file_input[0].replace('\n','').split(',') # splits on underscores
            if "Interval Top (cm) on SHLF" in keys:interval_key="Interval Top (cm) on SHLF"
            if " Interval Bot (cm) on SECT" in keys:interval_key=" Interval Bot (cm) on SECT"
            if "Offset (cm)" in keys: interval_key="Offset (cm)"
            if "Top Depth (m)" in keys:depth_key="Top Depth (m)"
            if "CSF-A Top (m)" in keys:depth_key="CSF-A Top (m)"
            if "Depth CSF-A (m)" in keys:depth_key="Depth CSF-A (m)"
            if "CSF-B Top (m)" in keys:
                comp_depth_key="CSF-B Top (m)" # use this model if available
            elif "Depth CSF-B (m)" in keys:
                comp_depth_key="Depth CSF-B (m)"
            else:
                comp_depth_key=""
            if "Demag level (mT)" in keys:demag_key="Demag level (mT)"
            if "Demag Level (mT)" in keys: demag_key="Demag Level (mT)"
            if "Inclination (Tray- and Bkgrd-Corrected) (deg)" in keys:inc_key="Inclination (Tray- and Bkgrd-Corrected) (deg)"
            if "Inclination background + tray corrected  (deg)" in keys:inc_key="Inclination background + tray corrected  (deg)"
            if "Inclination background + tray corrected  (\xc2\xb0)" in keys:inc_key="Inclination background + tray corrected  (\xc2\xb0)"
            if "Inclination background &amp; tray corrected (deg)" in keys:inc_key="Inclination background &amp; tray corrected (deg)"
            if "Declination (Tray- and Bkgrd-Corrected) (deg)" in keys:dec_key="Declination (Tray- and Bkgrd-Corrected) (deg)"
            if "Declination background + tray corrected (deg)" in keys:dec_key="Declination background + tray corrected (deg)"
            if "Declination background + tray corrected (\xc2\xb0)" in keys:dec_key="Declination background + tray corrected (\xc2\xb0)"
            if "Declination background &amp; tray corrected (deg)" in keys:dec_key="Declination background &amp; tray corrected (deg)"
            if "Intensity (Tray- and Bkgrd-Corrected) (A/m)" in keys:int_key="Intensity (Tray- and Bkgrd-Corrected) (A/m)"
            if "Intensity background + tray corrected  (A/m)" in keys:int_key="Intensity background + tray corrected  (A/m)"
            if "Intensity background &amp; tray corrected (A/m)" in keys:int_key="Intensity background &amp; tray corrected (A/m)"
            if "Core Type" in keys:
                core_type="Core Type"
            else: core_type="Type"
            if 'Run Number' in keys: run_number_key='Run Number'
            if 'Test No.' in keys: run_number_key='Test No.'
            if 'Test Changed On' in keys: date_key='Test Changed On'
            if "Timestamp (UTC)" in keys: date_key="Timestamp (UTC)"
            if "Section" in keys: sect_key="Section"
            if "Sect" in keys: sect_key="Sect"
            if 'Section Half' in keys: half_key='Section Half'
            if "A/W" in keys: half_key="A/W"
            if "Text ID" in keys: text_id="Text ID"
            if "Text Id" in keys: text_id="Text Id"
            for line in file_input[1:]:
              InRec={}
              test=0
              recs=line.split(',')
              for k in range(len(keys)):
                  if len(recs)==len(keys):
                      InRec[keys[k]]=line.split(',')[k]
              if InRec['Exp']!="": test=1 # get rid of pesky blank lines
              if test==1:
                run_number=""
                inst="IODP-SRM"
                volume='15.59' # set default volume to this
                MagRec,SpecRec,SampRec,SiteRec={},{},{},{}
                expedition=InRec['Exp']
                location=InRec['Site']+InRec['Hole']
# Maintain backward compatibility for the ever-changing LIMS format (Argh!)
                while len(InRec['Core'])<3:
                    InRec['Core']='0'+InRec['Core']
                if "Last Tray Measurment" in list(InRec.keys()) and "SHLF" not in InRec[text_id] or 'dscr' in csv_file :  # assume discrete sample
                    specimen=expedition+'-'+location+'-'+InRec['Core']+InRec[core_type]+"-"+InRec[sect_key]+'-'+InRec[half_key]+'-'+str(InRec[interval_key])
                else: # mark as continuous measurements
                    specimen=expedition+'-'+location+'-'+InRec['Core']+InRec[core_type]+"_"+InRec[sect_key]+InRec[half_key]+'-'+str(InRec[interval_key])
                SpecRec['er_expedition_name']=expedition
                SpecRec['er_location_name']=location
                SpecRec['er_site_name']=specimen
                SpecRec['er_citation_names']=citation
                for key in list(SpecRec.keys()):SampRec[key]=SpecRec[key]
                for key in list(SpecRec.keys()):SiteRec[key]=SpecRec[key]
                SampRec['sample_azimuth']='0'
                SampRec['sample_dip']='0'
                SampRec['sample_core_depth']=InRec[depth_key]
                if comp_depth_key!='':
                    SampRec['sample_composite_depth']=InRec[comp_depth_key]
                if "SHLF" not in InRec[text_id]:
                    SampRec['magic_method_codes']='FS-C-DRILL-IODP:SP-SS-C:SO-V'
                else:
                    SampRec['magic_method_codes']='FS-C-DRILL-IODP:SO-V'
                SpecRec['er_specimen_name']=specimen
                SpecRec['er_sample_name']=specimen
                SampRec['er_sample_name']=specimen
                SampRec['er_specimen_names']=specimen
                SiteRec['er_specimen_names']=specimen

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

    DESCRIPTION
    combines pmag_specimens.txt file with age, location, acceptance criteria and
    outputs pmag_results table along with other MagIC tables necessary for uploading to the database

    SYNTAX
    specimens_results_magic.py [command line options]

    OPTIONS
    -h prints help message and quits
    -usr USER:   identify user, default is ""
    -f: specimen input magic_measurements format file, default is "magic_measurements.txt"
    -fsp: specimen input pmag_specimens format file, default is "pmag_specimens.txt"
    -fsm: sample input er_samples format file, default is "er_samples.txt"
    -fsi: specimen input er_sites format file, default is "er_sites.txt"
    -fla: specify a file with paleolatitudes for calculating VADMs, default is not to calculate VADMS
               format is:  site_name paleolatitude (space delimited file)
    -fa AGES: specify er_ages format file with age information
    -crd [s,g,t,b]:   specify coordinate system
        (s, specimen, g geographic, t, tilt corrected, b, geographic and tilt corrected)
        Default is to assume geographic
        NB: only the tilt corrected data will appear on the results table, if both g and t are selected.
        -cor [AC:CR:NL]: colon delimited list of required data adjustments for all specimens
            included in intensity calculations (anisotropy, cooling rate, non-linear TRM)
            unless specified, corrections will not be applied
        -pri [TRM:ARM] colon delimited list of priorities for anisotropy correction (-cor must also be set to include AC). default is TRM, then ARM
    -age MIN MAX UNITS:   specify age boundaries and units
    -exc:  use exiting selection criteria (in pmag_criteria.txt file), default is default criteria
    -C: no acceptance criteria
    -aD:  average directions per sample, default is NOT
    -aI:  average multiple specimen intensities per sample, default is by site
    -aC:  average all components together, default is NOT
    -pol:  calculate polarity averages
    -sam:  save sample level vgps and v[a]dms, default is by site
    -xSi:  skip the site level intensity calculation
    -p: plot directions and look at intensities by site, default is NOT
        -fmt: specify output for saved images, default is svg (only if -p set)
    -lat: use present latitude for calculating VADMs, default is not to calculate VADMs
    -xD: skip directions
    -xI: skip intensities
    OUPUT
    writes pmag_samples, pmag_sites, pmag_results tables
    """
# set defaults
    Comps=[] # list of components
    version_num=pmag.get_version()
    args=sys.argv
    DefaultAge=["none"]
    skipdirs,coord,excrit,custom,vgps,average,Iaverage,plotsites,opt=1,0,0,0,0,0,0,0,0
    get_model_lat=0 # this skips VADM calculation altogether, when get_model_lat=1, uses present day
    fmt='svg'
    dir_path="."
    model_lat_file=""
    Caverage=0
    infile='pmag_specimens.txt'
    measfile="magic_measurements.txt"
    sampfile="er_samples.txt"
    sitefile="er_sites.txt"
    agefile="er_ages.txt"
    specout="er_specimens.txt"
    sampout="pmag_samples.txt"
    siteout="pmag_sites.txt"
    resout="pmag_results.txt"
    critout="pmag_criteria.txt"
    instout="magic_instruments.txt"
    sigcutoff,OBJ="",""
    noDir,noInt=0,0
    polarity=0
    coords=['0']
    Dcrit,Icrit,nocrit=0,0,0
    corrections=[]
    nocorrection=['DA-NL','DA-AC','DA-CR']
    priorities=['DA-AC-ARM','DA-AC-TRM'] # priorities for anisotropy correction
# get command line stuff
    if "-h" in args:
        print(main.__doc__)
        sys.exit()
    if '-WD' in args:
        ind=args.index("-WD")
        dir_path=args[ind+1]
    if '-cor' in args:
        ind=args.index('-cor')
        cors=args[ind+1].split(':') # list of required data adjustments
        for cor in cors:
            nocorrection.remove('DA-'+cor)
            corrections.append('DA-'+cor)
    if '-pri' in args:
        ind=args.index('-pri')
        priorities=args[ind+1].split(':') # list of required data adjustments
        for p in priorities:
            p='DA-AC-'+p
    if '-f' in args:
        ind=args.index("-f")
        measfile=args[ind+1]
    if '-fsp' in args:
        ind=args.index("-fsp")
        infile=args[ind+1]
    if '-fsi' in args:
        ind=args.index("-fsi")
        sitefile=args[ind+1]
    if "-crd" in args:
        ind=args.index("-crd")
        coord=args[ind+1]
        if coord=='s':coords=['-1']
        if coord=='g':coords=['0']
        if coord=='t':coords=['100']
        if coord=='b':coords=['0','100']
    if "-usr" in args:
        ind=args.index("-usr")
        user=sys.argv[ind+1]
    else: user=""
    if "-C" in args: Dcrit,Icrit,nocrit=1,1,1 # no selection criteria
    if "-sam" in args: vgps=1 # save sample level VGPS/VADMs
    if "-xSi" in args:
        nositeints=1 # skip site level intensity
    else:
        nositeints=0
    if "-age" in args:
        ind=args.index("-age")
        DefaultAge[0]=args[ind+1]
        DefaultAge.append(args[ind+2])
        DefaultAge.append(args[ind+3])
    Daverage,Iaverage,Caverage=0,0,0
    if "-aD" in args: Daverage=1 # average by sample directions
    if "-aI" in args: Iaverage=1 # average by sample intensities
    if "-aC" in args: Caverage=1 # average all components together ???  why???
    if "-pol" in args: polarity=1 # calculate averages by polarity
    if '-xD' in args:noDir=1
    if '-xI' in args:
        noInt=1
    elif "-fla" in args:
        if '-lat' in args:
            print("you should set a paleolatitude file OR use present day lat - not both")
            sys.exit()
        ind=args.index("-fla")
        model_lat_file=dir_path+'/'+args[ind+1]
        get_model_lat=2
        mlat=open(model_lat_file,'r')
        ModelLats=[]
        for line in mlat.readlines():
            ModelLat={}
            tmp=line.split()
            ModelLat["er_site_name"]=tmp[0]
            ModelLat["site_model_lat"]=tmp[1]
            ModelLat["er_sample_name"]=tmp[0]
            ModelLat["sample_lat"]=tmp[1]
            ModelLats.append(ModelLat)
        get_model_lat=2
    elif '-lat' in args:
        get_model_lat=1
    if "-p" in args:
        plotsites=1
        if "-fmt" in args:
            ind=args.index("-fmt")
            fmt=args[ind+1]
        if noDir==0: # plot by site - set up plot window
            import pmagplotlib
            EQ={}
            EQ['eqarea']=1
            pmagplotlib.plot_init(EQ['eqarea'],5,5) # define figure 1 as equal area projection
            pmagplotlib.plotNET(EQ['eqarea']) # I don't know why this has to be here, but otherwise the first plot never plots...
            pmagplotlib.drawFIGS(EQ)
    if '-WD' in args:
        infile=dir_path+'/'+infile
        measfile=dir_path+'/'+measfile
        instout=dir_path+'/'+instout
        sampfile=dir_path+'/'+sampfile
        sitefile=dir_path+'/'+sitefile
        agefile=dir_path+'/'+agefile
        specout=dir_path+'/'+specout
        sampout=dir_path+'/'+sampout
        siteout=dir_path+'/'+siteout
        resout=dir_path+'/'+resout
        critout=dir_path+'/'+critout
    if "-exc" in args: # use existing pmag_criteria file
        if "-C" in args:
            print('you can not use both existing and no criteria - choose either -exc OR -C OR neither (for default)')
            sys.exit()
        crit_data,file_type=pmag.magic_read(critout)
        print("Acceptance criteria read in from ", critout)
    else  : # use default criteria (if nocrit set, then get really loose criteria as default)
        crit_data=pmag.default_criteria(nocrit)
        if nocrit==0:
            print("Acceptance criteria are defaults")
        else:
            print("No acceptance criteria used ")
    accept={}
    for critrec in crit_data:
        for key in list(critrec.keys()):
# need to migrate specimen_dang to specimen_int_dang for intensity data using old format
            if 'IE-SPEC' in list(critrec.keys()) and 'specimen_dang' in list(critrec.keys()) and 'specimen_int_dang' not in list(critrec.keys()):
                critrec['specimen_int_dang']=critrec['specimen_dang']
                del critrec['specimen_dang']
# need to get rid of ron shaars sample_int_sigma_uT
            if 'sample_int_sigma_uT' in list(critrec.keys()):
                critrec['sample_int_sigma']='%10.3e'%(eval(critrec['sample_int_sigma_uT'])*1e-6)
            if key not in list(accept.keys()) and critrec[key]!='':
                accept[key]=critrec[key]
    #
    #
    if "-exc" not in args and "-C" not in args:
        print("args",args)
        pmag.magic_write(critout,[accept],'pmag_criteria')
        print("\n Pmag Criteria stored in ",critout,'\n')
#
# now we're done slow dancing
#
    SiteNFO,file_type=pmag.magic_read(sitefile) # read in site data - has the lats and lons
    SampNFO,file_type=pmag.magic_read(sampfile) # read in site data - has the lats and lons
    height_nfo=pmag.get_dictitem(SiteNFO,'site_height','','F') # find all the sites with height info.
    if agefile !="":AgeNFO,file_type=pmag.magic_read(agefile) # read in the age information
    Data,file_type=pmag.magic_read(infile) # read in specimen interpretations
    IntData=pmag.get_dictitem(Data,'specimen_int','','F') # retrieve specimens with intensity data
    comment,orient="",[]
    samples,sites=[],[]
    for rec in Data: # run through the data filling in missing keys and finding all components, coordinates available
# fill in missing fields, collect unique sample and site names
        if 'er_sample_name' not in list(rec.keys()):
            rec['er_sample_name']=""
        elif rec['er_sample_name'] not in samples:
            samples.append(rec['er_sample_name'])
        if 'er_site_name' not in list(rec.keys()):
            rec['er_site_name']=""
        elif rec['er_site_name'] not in sites:
            sites.append(rec['er_site_name'])
        if 'specimen_int' not in list(rec.keys()):rec['specimen_int']=''
        if 'specimen_comp_name' not in list(rec.keys()) or rec['specimen_comp_name']=="":rec['specimen_comp_name']='A'
        if rec['specimen_comp_name'] not in Comps:Comps.append(rec['specimen_comp_name'])
        rec['specimen_tilt_correction']=rec['specimen_tilt_correction'].strip('\n')
        if "specimen_tilt_correction" not in list(rec.keys()): rec["specimen_tilt_correction"]="-1" # assume sample coordinates
        if rec["specimen_tilt_correction"] not in orient: orient.append(rec["specimen_tilt_correction"])  # collect available coordinate systems
        if "specimen_direction_type" not in list(rec.keys()): rec["specimen_direction_type"]='l'  # assume direction is line - not plane
        if "specimen_dec" not in list(rec.keys()): rec["specimen_direction_type"]=''  # if no declination, set direction type to blank
        if "specimen_n" not in list(rec.keys()): rec["specimen_n"]=''  # put in n
        if "specimen_alpha95" not in list(rec.keys()): rec["specimen_alpha95"]=''  # put in alpha95
        if "magic_method_codes" not in list(rec.keys()): rec["magic_method_codes"]=''
     #
     # start parsing data into SpecDirs, SpecPlanes, SpecInts
    SpecInts,SpecDirs,SpecPlanes=[],[],[]
    samples.sort() # get sorted list of samples and sites
    sites.sort()
    if noInt==0: # don't skip intensities
        IntData=pmag.get_dictitem(Data,'specimen_int','','F') # retrieve specimens with intensity data
        if nocrit==0: # use selection criteria
            for rec in IntData: # do selection criteria
                kill=pmag.grade(rec,accept,'specimen_int')
                if len(kill)==0: SpecInts.append(rec) # intensity record to be included in sample, site calculations
        else:
            SpecInts=IntData[:] # take everything - no selection criteria
    # check for required data adjustments
        if len(corrections)>0 and len(SpecInts)>0:
            for cor in corrections:
                SpecInts=pmag.get_dictitem(SpecInts,'magic_method_codes',cor,'has') # only take specimens with the required corrections
        if len(nocorrection)>0 and len(SpecInts)>0:
            for cor in nocorrection:
                SpecInts=pmag.get_dictitem(SpecInts,'magic_method_codes',cor,'not') # exclude the corrections not specified for inclusion
# take top priority specimen of its name in remaining specimens (only one per customer)
        PrioritySpecInts=[]
        specimens=pmag.get_specs(SpecInts) # get list of uniq specimen names
        for spec in specimens:
            ThisSpecRecs=pmag.get_dictitem(SpecInts,'er_specimen_name',spec,'T') # all the records for this specimen
            if len(ThisSpecRecs)==1:
                PrioritySpecInts.append(ThisSpecRecs[0])
            elif len(ThisSpecRecs)>1: # more than one
                prec=[]
                for p in priorities:
                    ThisSpecRecs=pmag.get_dictitem(SpecInts,'magic_method_codes',p,'has') # all the records for this specimen
                    if len(ThisSpecRecs)>0:prec.append(ThisSpecRecs[0])
                PrioritySpecInts.append(prec[0]) # take the best one
        SpecInts=PrioritySpecInts # this has the first specimen record
    if noDir==0: # don't skip directions
        AllDirs=pmag.get_dictitem(Data,'specimen_direction_type','','F') # retrieve specimens with directed lines and planes
        Ns=pmag.get_dictitem(AllDirs,'specimen_n','','F')  # get all specimens with specimen_n information
        if nocrit!=1: # use selection criteria
            for rec in Ns: # look through everything with specimen_n for "good" data
                kill=pmag.grade(rec,accept,'specimen_dir')
                if len(kill)==0: # nothing killed it
                    SpecDirs.append(rec)
        else: # no criteria
            SpecDirs=AllDirs[:] # take them all
# SpecDirs is now the list of all specimen directions (lines and planes) that pass muster
#
    PmagSamps,SampDirs=[],[] # list of all sample data and list of those that pass the DE-SAMP criteria
    PmagSites,PmagResults=[],[] # list of all site data and selected results
    SampInts=[]
    for samp in samples: # run through the sample names
        if Daverage==1: #  average by sample if desired
           SampDir=pmag.get_dictitem(SpecDirs,'er_sample_name',samp,'T') # get all the directional data for this sample
           if len(SampDir)>0: # there are some directions
               for coord in coords: # step through desired coordinate systems
                   CoordDir=pmag.get_dictitem(SampDir,'specimen_tilt_correction',coord,'T') # get all the directions for this sample
                   if len(CoordDir)>0: # there are some with this coordinate system
                       if Caverage==0: # look component by component
                           for comp in Comps:
                               CompDir=pmag.get_dictitem(CoordDir,'specimen_comp_name',comp,'T') # get all directions from this component
                               if len(CompDir)>0: # there are some
                                   PmagSampRec=pmag.lnpbykey(CompDir,'sample','specimen') # get a sample average from all specimens
                                   PmagSampRec["er_location_name"]=CompDir[0]['er_location_name'] # decorate the sample record
                                   PmagSampRec["er_site_name"]=CompDir[0]['er_site_name']
                                   PmagSampRec["er_sample_name"]=samp
                                   PmagSampRec["er_citation_names"]="This study"
                                   PmagSampRec["er_analyst_mail_names"]=user
                                   PmagSampRec['magic_software_packages']=version_num
                                   if nocrit!=1:PmagSampRec['pmag_criteria_codes']="ACCEPT"
                                   if agefile != "": PmagSampRec= pmag.get_age(PmagSampRec,"er_site_name","sample_inferred_",AgeNFO,DefaultAge)
                                   site_height=pmag.get_dictitem(height_nfo,'er_site_name',PmagSampRec['er_site_name'],'T')
                                   if len(site_height)>0:PmagSampRec["sample_height"]=site_height[0]['site_height'] # add in height if available
                                   PmagSampRec['sample_comp_name']=comp
                                   PmagSampRec['sample_tilt_correction']=coord
                                   PmagSampRec['er_specimen_names']= pmag.get_list(CompDir,'er_specimen_name') # get a list of the specimen names used
                                   PmagSampRec['magic_method_codes']= pmag.get_list(CompDir,'magic_method_codes') # get a list of the methods used
                                   if nocrit!=1: # apply selection criteria
                                       kill=pmag.grade(PmagSampRec,accept,'sample_dir')
                                   else:
                                       kill=[]
                                   if len(kill)==0:
                                       SampDirs.append(PmagSampRec)
                                       if vgps==1: # if sample level VGP info desired, do that now
                                           PmagResRec=pmag.getsampVGP(PmagSampRec,SiteNFO)
                                           if PmagResRec!="":PmagResults.append(PmagResRec)
                                       PmagSamps.append(PmagSampRec)
                       if Caverage==1: # average all components together  basically same as above
                           PmagSampRec=pmag.lnpbykey(CoordDir,'sample','specimen')
                           PmagSampRec["er_location_name"]=CoordDir[0]['er_location_name']
                           PmagSampRec["er_site_name"]=CoordDir[0]['er_site_name']
                           PmagSampRec["er_sample_name"]=samp
                           PmagSampRec["er_citation_names"]="This study"
                           PmagSampRec["er_analyst_mail_names"]=user
                           PmagSampRec['magic_software_packages']=version_num
                           if nocrit!=1:PmagSampRec['pmag_criteria_codes']=""
                           if agefile != "": PmagSampRec= pmag.get_age(PmagSampRec,"er_site_name","sample_inferred_",AgeNFO,DefaultAge)
                           site_height=pmag.get_dictitem(height_nfo,'er_site_name',site,'T')
                           if len(site_height)>0:PmagSampRec["sample_height"]=site_height[0]['site_height'] # add in height if available
                           PmagSampRec['sample_tilt_correction']=coord
                           PmagSampRec['sample_comp_name']= pmag.get_list(CoordDir,'specimen_comp_name') # get components used
                           PmagSampRec['er_specimen_names']= pmag.get_list(CoordDir,'er_specimen_name') # get specimne names averaged
                           PmagSampRec['magic_method_codes']= pmag.get_list(CoordDir,'magic_method_codes') # assemble method codes
                           if nocrit!=1: # apply selection criteria
                               kill=pmag.grade(PmagSampRec,accept,'sample_dir')
                               if len(kill)==0: # passes the mustard
                                   SampDirs.append(PmagSampRec)
                                   if vgps==1:
                                       PmagResRec=pmag.getsampVGP(PmagSampRec,SiteNFO)
                                       if PmagResRec!="":PmagResults.append(PmagResRec)
                           else: # take everything
                               SampDirs.append(PmagSampRec)
                               if vgps==1:
                                   PmagResRec=pmag.getsampVGP(PmagSampRec,SiteNFO)
                                   if PmagResRec!="":PmagResults.append(PmagResRec)
                           PmagSamps.append(PmagSampRec)
        if Iaverage==1: #  average by sample if desired
           SampI=pmag.get_dictitem(SpecInts,'er_sample_name',samp,'T') # get all the intensity data for this sample
           if len(SampI)>0: # there are some
               PmagSampRec=pmag.average_int(SampI,'specimen','sample') # get average intensity stuff
               PmagSampRec["sample_description"]="sample intensity" # decorate sample record
               PmagSampRec["sample_direction_type"]=""
               PmagSampRec['er_site_name']=SampI[0]["er_site_name"]
               PmagSampRec['er_sample_name']=samp
               PmagSampRec['er_location_name']=SampI[0]["er_location_name"]
               PmagSampRec["er_citation_names"]="This study"
               PmagSampRec["er_analyst_mail_names"]=user
               if agefile != "":   PmagSampRec=pmag.get_age(PmagSampRec,"er_site_name","sample_inferred_", AgeNFO,DefaultAge)
               site_height=pmag.get_dictitem(height_nfo,'er_site_name',PmagSampRec['er_site_name'],'T')
               if len(site_height)>0:PmagSampRec["sample_height"]=site_height[0]['site_height'] # add in height if available
               PmagSampRec['er_specimen_names']= pmag.get_list(SampI,'er_specimen_name')
               PmagSampRec['magic_method_codes']= pmag.get_list(SampI,'magic_method_codes')
               if nocrit!=1:  # apply criteria!
                   kill=pmag.grade(PmagSampRec,accept,'sample_int')
                   if len(kill)==0:
                       PmagSampRec['pmag_criteria_codes']="ACCEPT"
                       SampInts.append(PmagSampRec)
                       PmagSamps.append(PmagSampRec)
                   else:PmagSampRec={} # sample rejected
               else: # no criteria
                   SampInts.append(PmagSampRec)
                   PmagSamps.append(PmagSampRec)
                   PmagSampRec['pmag_criteria_codes']=""
               if vgps==1 and get_model_lat!=0 and PmagSampRec!={}: #
                   if get_model_lat==1: # use sample latitude
                       PmagResRec=pmag.getsampVDM(PmagSampRec,SampNFO)
                       del(PmagResRec['model_lat']) # get rid of the model lat key
                   elif get_model_lat==2: # use model latitude
                       PmagResRec=pmag.getsampVDM(PmagSampRec,ModelLats)
                       if PmagResRec!={}:PmagResRec['magic_method_codes']=PmagResRec['magic_method_codes']+":IE-MLAT"
                   if PmagResRec!={}:
                          PmagResRec['er_specimen_names']=PmagSampRec['er_specimen_names']
                          PmagResRec['er_sample_names']=PmagSampRec['er_sample_name']
                          PmagResRec['pmag_criteria_codes']='ACCEPT'
                          PmagResRec['average_int_sigma_perc']=PmagSampRec['sample_int_sigma_perc']
                          PmagResRec['average_int_sigma']=PmagSampRec['sample_int_sigma']
                          PmagResRec['average_int_n']=PmagSampRec['sample_int_n']
                          PmagResRec['vadm_n']=PmagSampRec['sample_int_n']
                          PmagResRec['data_type']='i'
                          PmagResults.append(PmagResRec)
    if len(PmagSamps)>0:
        TmpSamps,keylist=pmag.fillkeys(PmagSamps) # fill in missing keys from different types of records
        pmag.magic_write(sampout,TmpSamps,'pmag_samples') # save in sample output file
        print(' sample averages written to ',sampout)

#
#create site averages from specimens or samples as specified
#
    for site in sites:
        if Daverage==0: key,dirlist='specimen',SpecDirs # if specimen averages at site level desired
        if Daverage==1: key,dirlist='sample',SampDirs # if sample averages at site level desired
        tmp=pmag.get_dictitem(dirlist,'er_site_name',site,'T') # get all the sites with  directions
        tmp1=pmag.get_dictitem(tmp,key+'_tilt_correction',coords[-1],'T') # use only the last coordinate if Caverage==0
        sd=pmag.get_dictitem(SiteNFO,'er_site_name',site,'T') # fish out site information (lat/lon, etc.)
        if len(sd)>0:
            sitedat=sd[0]
            if Caverage==0: # do component wise averaging
                for comp in Comps:
                    siteD=pmag.get_dictitem(tmp1,key+'_comp_name',comp,'T') # get all components comp
                    if len(siteD)>0: # there are some for this site and component name
                        PmagSiteRec=pmag.lnpbykey(siteD,'site',key) # get an average for this site
                        PmagSiteRec['site_comp_name']=comp # decorate the site record
                        PmagSiteRec["er_location_name"]=siteD[0]['er_location_name']
                        PmagSiteRec["er_site_name"]=siteD[0]['er_site_name']
                        PmagSiteRec['site_tilt_correction']=coords[-1]
                        PmagSiteRec['site_comp_name']= pmag.get_list(siteD,key+'_comp_name')
                        if Daverage==1:
                            PmagSiteRec['er_sample_names']= pmag.get_list(siteD,'er_sample_name')
                        else:
                            PmagSiteRec['er_specimen_names']= pmag.get_list(siteD,'er_specimen_name')
        # determine the demagnetization code (DC3,4 or 5) for this site
                        AFnum=len(pmag.get_dictitem(siteD,'magic_method_codes','LP-DIR-AF','has'))
                        Tnum=len(pmag.get_dictitem(siteD,'magic_method_codes','LP-DIR-T','has'))
                        DC=3
                        if AFnum>0:DC+=1
                        if Tnum>0:DC+=1
                        PmagSiteRec['magic_method_codes']= pmag.get_list(siteD,'magic_method_codes')+':'+ 'LP-DC'+str(DC)
                        PmagSiteRec['magic_method_codes'].strip(":")
                        if plotsites==1:
                            print(PmagSiteRec['er_site_name'])
                            pmagplotlib.plotSITE(EQ['eqarea'],PmagSiteRec,siteD,key) # plot and list the data
                            pmagplotlib.drawFIGS(EQ)
                        PmagSites.append(PmagSiteRec)
            else: # last component only
                siteD=tmp1[:] # get the last orientation system specified
                if len(siteD)>0: # there are some
                    PmagSiteRec=pmag.lnpbykey(siteD,'site',key) # get the average for this site
                    PmagSiteRec["er_location_name"]=siteD[0]['er_location_name'] # decorate the record
                    PmagSiteRec["er_site_name"]=siteD[0]['er_site_name']
                    PmagSiteRec['site_comp_name']=comp
                    PmagSiteRec['site_tilt_correction']=coords[-1]
                    PmagSiteRec['site_comp_name']= pmag.get_list(siteD,key+'_comp_name')
                    PmagSiteRec['er_specimen_names']= pmag.get_list(siteD,'er_specimen_name')
                    PmagSiteRec['er_sample_names']= pmag.get_list(siteD,'er_sample_name')
                    AFnum=len(pmag.get_dictitem(siteD,'magic_method_codes','LP-DIR-AF','has'))
                    Tnum=len(pmag.get_dictitem(siteD,'magic_method_codes','LP-DIR-T','has'))
                    DC=3
                    if AFnum>0:DC+=1
                    if Tnum>0:DC+=1
                    PmagSiteRec['magic_method_codes']= pmag.get_list(siteD,'magic_method_codes')+':'+ 'LP-DC'+str(DC)
                    PmagSiteRec['magic_method_codes'].strip(":")
                    if Daverage==0:PmagSiteRec['site_comp_name']= pmag.get_list(siteD,key+'_comp_name')
                    if plotsites==1:
                        pmagplotlib.plotSITE(EQ['eqarea'],PmagSiteRec,siteD,key)
                        pmagplotlib.drawFIGS(EQ)
                    PmagSites.append(PmagSiteRec)
        else:
            print('site information not found in er_sites for site, ',site,' site will be skipped')
    for PmagSiteRec in PmagSites: # now decorate each dictionary some more, and calculate VGPs etc. for results table
        PmagSiteRec["er_citation_names"]="This study"
        PmagSiteRec["er_analyst_mail_names"]=user
        PmagSiteRec['magic_software_packages']=version_num
        if agefile != "": PmagSiteRec= pmag.get_age(PmagSiteRec,"er_site_name","site_inferred_",AgeNFO,DefaultAge)
        PmagSiteRec['pmag_criteria_codes']='ACCEPT'
        if 'site_n_lines' in list(PmagSiteRec.keys()) and 'site_n_planes' in list(PmagSiteRec.keys()) and PmagSiteRec['site_n_lines']!="" and PmagSiteRec['site_n_planes']!="":
            if int(PmagSiteRec["site_n_planes"])>0:
                PmagSiteRec["magic_method_codes"]=PmagSiteRec['magic_method_codes']+":DE-FM-LP"
            elif int(PmagSiteRec["site_n_lines"])>2:
                PmagSiteRec["magic_method_codes"]=PmagSiteRec['magic_method_codes']+":DE-FM"
            kill=pmag.grade(PmagSiteRec,accept,'site_dir')
            if len(kill)==0:
                PmagResRec={} # set up dictionary for the pmag_results table entry
                PmagResRec['data_type']='i' # decorate it a bit
                PmagResRec['magic_software_packages']=version_num
                PmagSiteRec['site_description']='Site direction included in results table'
                PmagResRec['pmag_criteria_codes']='ACCEPT'
                dec=float(PmagSiteRec["site_dec"])
                inc=float(PmagSiteRec["site_inc"])
                if 'site_alpha95' in list(PmagSiteRec.keys()) and PmagSiteRec['site_alpha95']!="":
                    a95=float(PmagSiteRec["site_alpha95"])
                else:a95=180.
                sitedat=pmag.get_dictitem(SiteNFO,'er_site_name',PmagSiteRec['er_site_name'],'T')[0] # fish out site information (lat/lon, etc.)
                lat=float(sitedat['site_lat'])
                lon=float(sitedat['site_lon'])
                plong,plat,dp,dm=pmag.dia_vgp(dec,inc,a95,lat,lon) # get the VGP for this site
                if PmagSiteRec['site_tilt_correction']=='-1':C=' (spec coord) '
                if PmagSiteRec['site_tilt_correction']=='0':C=' (geog. coord) '
                if PmagSiteRec['site_tilt_correction']=='100':C=' (strat. coord) '
                PmagResRec["pmag_result_name"]="VGP Site: "+PmagSiteRec["er_site_name"] # decorate some more
                PmagResRec["result_description"]="Site VGP, coord system = "+str(coord)+' component: '+comp
                PmagResRec['er_site_names']=PmagSiteRec['er_site_name']
                PmagResRec['pmag_criteria_codes']='ACCEPT'
                PmagResRec['er_citation_names']='This study'
                PmagResRec['er_analyst_mail_names']=user
                PmagResRec["er_location_names"]=PmagSiteRec["er_location_name"]
                if Daverage==1:
                    PmagResRec["er_sample_names"]=PmagSiteRec["er_sample_names"]
                else:
                    PmagResRec["er_specimen_names"]=PmagSiteRec["er_specimen_names"]
                PmagResRec["tilt_correction"]=PmagSiteRec['site_tilt_correction']
                PmagResRec["pole_comp_name"]=PmagSiteRec['site_comp_name']
                PmagResRec["average_dec"]=PmagSiteRec["site_dec"]
                PmagResRec["average_inc"]=PmagSiteRec["site_inc"]
                PmagResRec["average_alpha95"]=PmagSiteRec["site_alpha95"]
                PmagResRec["average_n"]=PmagSiteRec["site_n"]
                PmagResRec["average_n_lines"]=PmagSiteRec["site_n_lines"]
                PmagResRec["average_n_planes"]=PmagSiteRec["site_n_planes"]
                PmagResRec["vgp_n"]=PmagSiteRec["site_n"]
                PmagResRec["average_k"]=PmagSiteRec["site_k"]
                PmagResRec["average_r"]=PmagSiteRec["site_r"]
                PmagResRec["average_lat"]='%10.4f ' %(lat)
                PmagResRec["average_lon"]='%10.4f ' %(lon)
                if agefile != "": PmagResRec= pmag.get_age(PmagResRec,"er_site_names","average_",AgeNFO,DefaultAge)
                site_height=pmag.get_dictitem(height_nfo,'er_site_name',site,'T')
                if len(site_height)>0:PmagResRec["average_height"]=site_height[0]['site_height']
                PmagResRec["vgp_lat"]='%7.1f ' % (plat)
                PmagResRec["vgp_lon"]='%7.1f ' % (plong)
                PmagResRec["vgp_dp"]='%7.1f ' % (dp)
                PmagResRec["vgp_dm"]='%7.1f ' % (dm)
                PmagResRec["magic_method_codes"]= PmagSiteRec["magic_method_codes"]
                if PmagSiteRec['site_tilt_correction']=='0':PmagSiteRec['magic_method_codes']=PmagSiteRec['magic_method_codes']+":DA-DIR-GEO"
                if PmagSiteRec['site_tilt_correction']=='100':PmagSiteRec['magic_method_codes']=PmagSiteRec['magic_method_codes']+":DA-DIR-TILT"
                PmagSiteRec['site_polarity']=""
                if polarity==1: # assign polarity based on angle of pole lat to spin axis - may want to re-think this sometime
                    angle=pmag.angle([0,0],[0,(90-plat)])
                    if angle <= 55.: PmagSiteRec["site_polarity"]='n'
                    if angle > 55. and angle < 125.: PmagSiteRec["site_polarity"]='t'
                    if angle >= 125.: PmagSiteRec["site_polarity"]='r'
                PmagResults.append(PmagResRec)
    if polarity==1:
        crecs=pmag.get_dictitem(PmagSites,'site_tilt_correction','100','T') # find the tilt corrected data
        if len(crecs)<2:crecs=pmag.get_dictitem(PmagSites,'site_tilt_correction','0','T') # if there aren't any, find the geographic corrected data
        if len(crecs)>2: # if there are some,
            comp=pmag.get_list(crecs,'site_comp_name').split(':')[0] # find the first component
            crecs=pmag.get_dictitem(crecs,'site_comp_name',comp,'T') # fish out all of the first component
            precs=[]
            for rec in crecs:
                precs.append({'dec':rec['site_dec'],'inc':rec['site_inc'],'name':rec['er_site_name'],'loc':rec['er_location_name']})
            polpars=pmag.fisher_by_pol(precs) # calculate average by polarity
            for mode in list(polpars.keys()): # hunt through all the modes (normal=A, reverse=B, all=ALL)
                PolRes={}
                PolRes['er_citation_names']='This study'
                PolRes["pmag_result_name"]="Polarity Average: Polarity "+mode #
                PolRes["data_type"]="a"
                PolRes["average_dec"]='%7.1f'%(polpars[mode]['dec'])
                PolRes["average_inc"]='%7.1f'%(polpars[mode]['inc'])
                PolRes["average_n"]='%i'%(polpars[mode]['n'])
                PolRes["average_r"]='%5.4f'%(polpars[mode]['r'])
                PolRes["average_k"]='%6.0f'%(polpars[mode]['k'])
                PolRes["average_alpha95"]='%7.1f'%(polpars[mode]['alpha95'])
                PolRes['er_site_names']= polpars[mode]['sites']
                PolRes['er_location_names']= polpars[mode]['locs']
                PolRes['magic_software_packages']=version_num
                PmagResults.append(PolRes)

    if noInt!=1 and nositeints!=1:
      for site in sites: # now do intensities for each site
        if plotsites==1:print(site)
        if Iaverage==0: key,intlist='specimen',SpecInts # if using specimen level data
        if Iaverage==1: key,intlist='sample',PmagSamps # if using sample level data
        Ints=pmag.get_dictitem(intlist,'er_site_name',site,'T') # get all the intensities  for this site
        if len(Ints)>0: # there are some
            PmagSiteRec=pmag.average_int(Ints,key,'site') # get average intensity stuff for site table
            PmagResRec=pmag.average_int(Ints,key,'average') # get average intensity stuff for results table
            if plotsites==1: # if site by site examination requested - print this site out to the screen
                for rec in Ints:print(rec['er_'+key+'_name'],' %7.1f'%(1e6*float(rec[key+'_int'])))
                if len(Ints)>1:
                    print('Average: ','%7.1f'%(1e6*float(PmagResRec['average_int'])),'N: ',len(Ints))
                    print('Sigma: ','%7.1f'%(1e6*float(PmagResRec['average_int_sigma'])),'Sigma %: ',PmagResRec['average_int_sigma_perc'])
                input('Press any key to continue\n')
            er_location_name=Ints[0]["er_location_name"]
            PmagSiteRec["er_location_name"]=er_location_name # decorate the records
            PmagSiteRec["er_citation_names"]="This study"
            PmagResRec["er_location_names"]=er_location_name
            PmagResRec["er_citation_names"]="This study"
            PmagSiteRec["er_analyst_mail_names"]=user
            PmagResRec["er_analyst_mail_names"]=user
            PmagResRec["data_type"]='i'
            if Iaverage==0:
                PmagSiteRec['er_specimen_names']= pmag.get_list(Ints,'er_specimen_name') # list of all specimens used
                PmagResRec['er_specimen_names']= pmag.get_list(Ints,'er_specimen_name')
            PmagSiteRec['er_sample_names']= pmag.get_list(Ints,'er_sample_name') # list of all samples used
            PmagResRec['er_sample_names']= pmag.get_list(Ints,'er_sample_name')
            PmagSiteRec['er_site_name']= site
            PmagResRec['er_site_names']= site
            PmagSiteRec['magic_method_codes']= pmag.get_list(Ints,'magic_method_codes')
            PmagResRec['magic_method_codes']= pmag.get_list(Ints,'magic_method_codes')
            kill=pmag.grade(PmagSiteRec,accept,'site_int')
            if nocrit==1 or len(kill)==0:
                b,sig=float(PmagResRec['average_int']),""
                if(PmagResRec['average_int_sigma'])!="":sig=float(PmagResRec['average_int_sigma'])
                sdir=pmag.get_dictitem(PmagResults,'er_site_names',site,'T') # fish out site direction
                if len(sdir)>0 and  sdir[-1]['average_inc']!="": # get the VDM for this record using last average inclination (hope it is the right one!)
                        inc=float(sdir[0]['average_inc']) #
                        mlat=pmag.magnetic_lat(inc) # get magnetic latitude using dipole formula
                        PmagResRec["vdm"]='%8.3e '% (pmag.b_vdm(b,mlat)) # get VDM with magnetic latitude
                        PmagResRec["vdm_n"]=PmagResRec['average_int_n']
                        if 'average_int_sigma' in list(PmagResRec.keys()) and PmagResRec['average_int_sigma']!="":
                            vdm_sig=pmag.b_vdm(float(PmagResRec['average_int_sigma']),mlat)
                            PmagResRec["vdm_sigma"]='%8.3e '% (vdm_sig)
                        else:
                            PmagResRec["vdm_sigma"]=""
                mlat="" # define a model latitude
                if get_model_lat==1: # use present site latitude
                    mlats=pmag.get_dictitem(SiteNFO,'er_site_name',site,'T')
                    if len(mlats)>0: mlat=mlats[0]['site_lat']
                elif get_model_lat==2: # use a model latitude from some plate reconstruction model (or something)
                    mlats=pmag.get_dictitem(ModelLats,'er_site_name',site,'T')
                    if len(mlats)>0: PmagResRec['model_lat']=mlats[0]['site_model_lat']
                    mlat=PmagResRec['model_lat']
                if mlat!="":
                    PmagResRec["vadm"]='%8.3e '% (pmag.b_vdm(b,float(mlat))) # get the VADM using the desired latitude
                    if sig!="":
                        vdm_sig=pmag.b_vdm(float(PmagResRec['average_int_sigma']),float(mlat))
                        PmagResRec["vadm_sigma"]='%8.3e '% (vdm_sig)
                        PmagResRec["vadm_n"]=PmagResRec['average_int_n']
                    else:
                        PmagResRec["vadm_sigma"]=""
                sitedat=pmag.get_dictitem(SiteNFO,'er_site_name',PmagSiteRec['er_site_name'],'T') # fish out site information (lat/lon, etc.)
                if len(sitedat)>0:
                    sitedat=sitedat[0]
                    PmagResRec['average_lat']=sitedat['site_lat']
                    PmagResRec['average_lon']=sitedat['site_lon']
                else:
                    PmagResRec['average_lon']='UNKNOWN'
                    PmagResRec['average_lon']='UNKNOWN'
                PmagResRec['magic_software_packages']=version_num
                PmagResRec["pmag_result_name"]="V[A]DM: Site "+site
                PmagResRec["result_description"]="V[A]DM of site"
                PmagResRec["pmag_criteria_codes"]="ACCEPT"
                if agefile != "": PmagResRec= pmag.get_age(PmagResRec,"er_site_names","average_",AgeNFO,DefaultAge)
                site_height=pmag.get_dictitem(height_nfo,'er_site_name',site,'T')
                if len(site_height)>0:PmagResRec["average_height"]=site_height[0]['site_height']
                PmagSites.append(PmagSiteRec)
                PmagResults.append(PmagResRec)
    if len(PmagSites)>0:
        Tmp,keylist=pmag.fillkeys(PmagSites)
        pmag.magic_write(siteout,Tmp,'pmag_sites')
        print(' sites written to ',siteout)
    else: print("No Site level table")
    if len(PmagResults)>0:
        TmpRes,keylist=pmag.fillkeys(PmagResults)
        pmag.magic_write(resout,TmpRes,'pmag_results')
        print(' results written to ',resout)
    else: print("No Results level table")
示例#14
0
def convert(**kwargs):
    # initialize defaults
    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  # rename dir_path after input_dir_path is set
    noave = kwargs.get('noave', False)  # default is DO average
    csv_file = kwargs.get('csv_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')
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')
    volume = kwargs.get('volume', 2.5**
                        3) * 1e-6  #default volume is a 2.5cm cube

    # format variables
    if csv_file == "":
        filelist = os.listdir(
            input_dir_path)  # read in list of files to import
    else:
        csv_file = os.path.join(input_dir_path, csv_file)
        filelist = [csv_file]

    # parsing the data
    file_found, citations = False, "This Study"
    MeasRecs, SpecRecs, SampRecs, SiteRecs, LocRecs = [], [], [], [], []
    for fin in filelist:  # parse each file
        if fin[-3:].lower() == 'csv':
            file_found = True
            print('processing: ', fin)
            indata = open(fin, 'r').readlines()
            keys = indata[0].replace('\n',
                                     '').split(',')  # splits on underscores
            keys = [k.strip('"') for k in keys]
            interval_key = "Offset (cm)"
            if "Treatment Value (mT or \xc2\xb0C)" in keys:
                demag_key = "Treatment Value (mT or \xc2\xb0C)"
            elif "Treatment Value" in keys:
                demag_key = "Treatment Value"
            elif "Treatment Value (mT or &deg;C)" in keys:
                demag_key = "Treatment Value (mT or &deg;C)"
            elif "Demag level (mT)" in keys:
                demag_key = "Demag level (mT)"
            else:
                print("couldn't find demag level")
            if "Treatment type" in keys: treatment_type = "Treatment type"
            elif "Treatment Type" in keys: treatment_type = "Treatment Type"
            else: treatment_type = ""
            run_key = "Test No."
            if "Inclination background + tray corrected  (deg)" in keys:
                inc_key = "Inclination background + tray corrected  (deg)"
            elif "Inclination background &amp; tray corrected (deg)" in keys:
                inc_key = "Inclination background &amp; tray corrected (deg)"
            elif "Inclination background & tray corrected (deg)" in keys:
                inc_key = "Inclination background & tray corrected (deg)"
            elif "Inclination background & drift corrected (deg)" in keys:
                inc_key = "Inclination background & drift corrected (deg)"
            else:
                print("couldn't find inclination")
            if "Declination background + tray corrected (deg)" in keys:
                dec_key = "Declination background + tray corrected (deg)"
            elif "Declination background &amp; tray corrected (deg)" in keys:
                dec_key = "Declination background &amp; tray corrected (deg)"
            elif "Declination background & tray corrected (deg)" in keys:
                dec_key = "Declination background & tray corrected (deg)"
            elif "Declination background & drift corrected (deg)" in keys:
                dec_key = "Declination background & drift corrected (deg)"
            else:
                print("couldn't find declination")
            if "Intensity background + tray corrected  (A/m)" in keys:
                int_key = "Intensity background + tray corrected  (A/m)"
            elif "Intensity background &amp; tray corrected (A/m)" in keys:
                int_key = "Intensity background &amp; tray corrected (A/m)"
            elif "Intensity background & tray corrected (A/m)" in keys:
                int_key = "Intensity background & tray corrected (A/m)"
            elif "Intensity background & drift corrected (A/m)" in keys:
                int_key = "Intensity background & drift corrected (A/m)"
            else:
                print("couldn't find magnetic moment")
            type_val = "Type"
            sect_key = "Sect"
            half_key = "A/W"
            # need to add volume_key to LORE format!
            if "Sample volume (cm^3)" in keys:
                volume_key = "Sample volume (cm^3)"
            elif "Sample volume (cc)" in keys:
                volume_key = "Sample volume (cc)"
            elif "Sample volume (cm&sup3;)" in keys:
                volume_key = "Sample volume (cm&sup3;)"
            elif "Sample volume (cm\xc2\xb3)" in keys:
                volume_key = "Sample volume (cm\xc2\xb3)"
            else:
                volume_key = ""
            for line in indata[1:]:
                InRec = {}
                MeasRec, SpecRec, SampRec, SiteRec, LocRec = {}, {}, {}, {}, {}
                for k in range(len(keys)):
                    InRec[keys[k]] = line.split(',')[k].strip('"')
                inst = "IODP-SRM"
                expedition = InRec['Exp']
                location = InRec['Site'] + InRec['Hole']
                offsets = InRec[interval_key].split(
                    '.'
                )  # maintain consistency with er_samples convention of using top interval
                if len(offsets) == 1:
                    offset = int(offsets[0])
                else:
                    offset = int(offsets[0]) - 1
                #interval=str(offset+1)# maintain consistency with er_samples convention of using top interval
                interval = str(
                    offset
                )  # maintain consistency with er_samples convention of using top interval
                specimen = expedition + '-' + location + '-' + InRec[
                    'Core'] + InRec[type_val] + "-" + InRec[
                        sect_key] + '-' + InRec[half_key] + '-' + str(
                            InRec[interval_key])
                sample = expedition + '-' + location + '-' + InRec[
                    'Core'] + InRec[type_val]
                site = expedition + '-' + location
                if volume_key in list(InRec.keys()): volume = InRec[volume_key]

                if not InRec[dec_key].strip(
                        """ " ' """) or not InRec[inc_key].strip(""" " ' """):
                    print("No dec or inc found for specimen %s, skipping" %
                          specimen)

                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['citations'] = citations
                    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'] = citations
                    SampRec['azimuth'] = '0'
                    SampRec['dip'] = '0'
                    SampRec['method_codes'] = 'FS-C-DRILL-IODP:SO-V'
                    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'] = citations
                    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'] = citations
                    LocRec['expedition_name'] = expedition
                    LocRec['lat_n'] = lat
                    LocRec['lon_e'] = lon
                    LocRec['lat_s'] = lat
                    LocRec['lon_w'] = lon
                    LocRecs.append(LocRec)

                MeasRec['specimen'] = specimen
                # set up measurement record - default is NRM
                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["treat_ac_field"] = '0'
                MeasRec["treat_dc_field"] = '0'
                MeasRec["treat_dc_field_phi"] = '0'
                MeasRec["treat_dc_field_theta"] = '0'
                MeasRec["quality"] = 'g'  # assume all data are "good"
                MeasRec["standard"] = 'u'  # assume all data are "good"
                MeasRec["dir_csd"] = '0'  # assume all data are "good"
                MeasRec["method_codes"] = 'LT-NO'
                sort_by = 'treat_ac_field'  # set default to AF demag
                if treatment_type in list(
                        InRec.keys()) and InRec[treatment_type] != "":
                    if "AF" in InRec[treatment_type].upper():
                        MeasRec['method_codes'] = 'LT-AF-Z'
                        inst = inst + ':IODP-SRM-AF'  # measured on shipboard in-line 2G AF
                        treatment_value = float(InRec[demag_key].strip(
                            '"')) * 1e-3  # convert mT => T
                        MeasRec["treat_ac_field"] = str(
                            treatment_value)  # AF demag in treat mT => T
                    elif "T" in InRec[treatment_type].upper():
                        MeasRec['method_codes'] = 'LT-T-Z'
                        inst = inst + ':IODP-TDS'  # measured on shipboard Schonstedt thermal demagnetizer
                        treatment_value = float(InRec[demag_key].strip(
                            '"')) + 273  # convert C => K
                        MeasRec["treat_temp"] = str(treatment_value)
                    elif "Lowrie" in InRec['Comments']:
                        MeasRec['method_codes'] = 'LP-IRM-3D'
                        treatment_value = float(InRec[demag_key].strip(
                            '"')) + 273.  # convert C => K
                        MeasRec["treat_temp"] = str(treatment_value)
                        MeasRec["treat_ac_field"] = "0"
                        sort_by = 'treat_temp'
                    elif 'Isothermal' in InRec[treatment_type]:
                        MeasRec['method_codes'] = 'LT-IRM'
                        treatment_value = float(InRec[demag_key].strip(
                            '"')) * 1e-3  # convert mT => T
                        MeasRec["treat_dc_field"] = str(treatment_value)
                        MeasRec["treat_ac_field"] = "0"
                        sort_by = 'treat_dc_field'
                elif InRec[demag_key] != "0" and InRec[
                        demag_key] != "":  #Assume AF if there is no Treatment typ info
                    MeasRec['method_codes'] = 'LT-AF-Z'
                    inst = inst + ':IODP-SRM-AF'  # measured on shipboard in-line 2G AF
                    treatment_value = float(
                        InRec[demag_key].strip('"')) * 1e-3  # convert mT => T
                    MeasRec[
                        "treat_ac_field"] = treatment_value  # AF demag in treat mT => T
                MeasRec["standard"] = 'u'  # assume all data are "good"
                vol = float(volume)
                if run_key in list(InRec.keys()):
                    run_number = InRec[run_key]
                    MeasRec['external_database_ids'] = {'LIMS': run_number}
                else:
                    MeasRec['external_database_ids'] = ""
                MeasRec['description'] = 'sample orientation: ' + InRec[
                    'Sample orientation']
                MeasRec['dir_inc'] = InRec[inc_key].strip('"')
                MeasRec['dir_dec'] = InRec[dec_key].strip('"')
                intens = InRec[int_key].strip('"')
                MeasRec['magn_moment'] = '%8.3e' % (
                    float(intens) * vol
                )  # convert intensity from A/m to Am^2 using vol
                MeasRec['instrument_codes'] = inst
                MeasRec['treat_step_num'] = '1'
                MeasRec['meas_n_orient'] = ''
                MeasRecs.append(MeasRec)
    if not file_found:
        print("No .csv files were found")
        return False, "No .csv files were found"

    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)
    MeasSort = sorted(
        MeasRecs,
        lambda x, y=None: int(round(float(x[sort_by]) - float(y[sort_by])))
        if y != None else 0)
    MeasFixed = pmag.measurements_methods3(MeasSort, noave)
    MeasOuts, keys = pmag.fillkeys(MeasFixed)
    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
示例#15
0
def main():
    """
    NAME
        replace_AC_specimens.py
    
    DESCRIPTION
        finds  anisotropy corrected data and 
        replaces that specimen with it.
        puts in pmag_specimen format file
    
    SYNTAX
        replace_AC_specimens.py [command line options]

    OPTIONS
        -h prints help message and quits
        -i allows interactive setting of file names
        -fu TFILE uncorrected pmag_specimen format file with thellier interpretations
            created by thellier_magic_redo.py
        -fc AFILE anisotropy corrected pmag_specimen format file
            created by thellier_magic_redo.py
        -F FILE pmag_specimens format output file 

    DEFAULTS
        TFILE: thellier_specimens.txt
        AFILE: AC_specimens.txt
        FILE: TorAC_specimens.txt
    """
    dir_path = '.'
    tspec = "thellier_specimens.txt"
    aspec = "AC_specimens.txt"
    ofile = "TorAC_specimens.txt"
    critfile = "pmag_criteria.txt"
    ACSamplist, Samplist, sigmin = [], [], 10000
    GoodSamps, SpecOuts = [], []
    # get arguments from command line
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-fu' in sys.argv:
        ind = sys.argv.index('-fu')
        tspec = sys.argv[ind + 1]
    if '-fc' in sys.argv:
        ind = sys.argv.index('-fc')
        aspec = sys.argv[ind + 1]
    if '-F' in sys.argv:
        ind = sys.argv.index('-F')
        ofile = sys.argv[ind + 1]
    if '-WD' in sys.argv:
        ind = sys.argv.index('-WD')
        dir_path = sys.argv[ind + 1]

    # read in pmag_specimens file
    tspec = dir_path + '/' + tspec
    aspec = dir_path + '/' + aspec
    ofile = dir_path + '/' + ofile
    Specs, file_type = pmag.magic_read(tspec)
    Specs, file_type = pmag.magic_read(tspec)
    Speclist = pmag.get_specs(Specs)
    ACSpecs, file_type = pmag.magic_read(aspec)
    ACspeclist = pmag.get_specs(ACSpecs)
    for spec in Specs:
        if spec["er_sample_name"] not in Samplist:
            Samplist.append(spec["er_sample_name"])
    for spec in ACSpecs:
        if spec["er_sample_name"] not in ACSamplist:
            ACSamplist.append(spec["er_sample_name"])
    #
    for samp in Samplist:
        useAC, Ints, ACInts, GoodSpecs, AC, UC = 0, [], [], [], [], []
        for spec in Specs:
            if spec["er_sample_name"].lower() == samp.lower():
                UC.append(spec)
        if samp in ACSamplist:
            for spec in ACSpecs:
                if spec["er_sample_name"].lower() == samp.lower():
                    AC.append(spec)
        if len(AC) > 0:
            AClist = []
            for spec in AC:
                SpecOuts.append(spec)
                AClist.append(spec['er_specimen_name'])
                print('using AC: ', spec['er_specimen_name'],
                      '%7.1f' % (1e6 * float(spec['specimen_int'])))
            for spec in UC:
                if spec['er_specimen_name'] not in AClist:
                    SpecOuts.append(spec)
#                   print 'using UC: ',spec['er_specimen_name'],'%7.1f'%(1e6*float(spec['specimen_int']))
        else:
            for spec in UC:
                SpecOuts.append(spec)


#                print 'using UC: ',spec['er_specimen_name'],'%7.1f'%(1e6*float(spec['specimen_int']))
    SpecOuts, keys = pmag.fillkeys(SpecOuts)
    pmag.magic_write(ofile, SpecOuts, 'pmag_specimens')
    print('thellier data assessed for AC correction put in ', ofile)
示例#16
0
def main():
    """
    NAME
        odp_dcs_magic.py

    DESCRIPTION
        converts ODP discrete sample format files to magic_measurements format files

    SYNTAX
        odp_dsc_magic.py [command line options]

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

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

    Fixed = pmag.measurements_methods(MagOuts, noave)
    pmag.magic_write(meas_file, Fixed, 'magic_measurements')
    print('data stored in ', meas_file)
示例#17
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
    """)
示例#18
0
def save_redo(SpecRecs, inspec):
    SpecRecs, keys = pmag.fillkeys(SpecRecs)
    pmag.magic_write(inspec, SpecRecs, 'pmag_specimens')
示例#19
0
def main():
    """
    NAME
        odp_dcs_magic.py

    DESCRIPTION
        converts ODP discrete sample format files to magic_measurements format files

    SYNTAX
        odp_dsc_magic.py [command line options]

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

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

    Fixed=pmag.measurements_methods(MagOuts,noave)
    pmag.magic_write(meas_file,Fixed,'magic_measurements')
    print('data stored in ',meas_file)
示例#20
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
示例#21
0
def convert(**kwargs):
    # initialize defaults
    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 # rename dir_path after input_dir_path is set
    noave = kwargs.get('noave', False) # default is DO average
    csv_file = kwargs.get('csv_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')
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')
    volume = kwargs.get('volume', 2.5**3)*1e-6#default volume is a 2.5cm cube

    # format variables
    if csv_file=="":
        filelist=os.listdir(input_dir_path) # read in list of files to import
    else:
        csv_file = os.path.join(input_dir_path, csv_file)
        filelist=[csv_file]

    # parsing the data
    file_found,citations = False,"This Study"
    MeasRecs,SpecRecs,SampRecs,SiteRecs,LocRecs=[],[],[],[],[]
    for fin in filelist: # parse each file
        if fin[-3:].lower()=='csv':
            file_found = True
            print('processing: ',fin)
            indata=open(fin,'r').readlines()
            keys=indata[0].replace('\n','').split(',') # splits on underscores
            keys=[k.strip('"') for k in keys]
            interval_key="Offset (cm)"
            if "Treatment Value (mT or \xc2\xb0C)" in keys:demag_key="Treatment Value (mT or \xc2\xb0C)"
            elif "Treatment Value" in keys:demag_key="Treatment Value"
            elif "Treatment Value (mT or &deg;C)" in keys:demag_key="Treatment Value (mT or &deg;C)"
            elif "Demag level (mT)" in keys:demag_key="Demag level (mT)"
            else: print("couldn't find demag level")
            if "Treatment type" in keys:treatment_type="Treatment type"
            elif "Treatment Type" in keys:treatment_type="Treatment Type"
            else: treatment_type=""
            run_key="Test No."
            if "Inclination background + tray corrected  (deg)" in keys: inc_key="Inclination background + tray corrected  (deg)"
            elif "Inclination background &amp; tray corrected (deg)" in keys: inc_key="Inclination background &amp; tray corrected (deg)"
            elif "Inclination background & tray corrected (deg)" in keys:inc_key="Inclination background & tray corrected (deg)"
            elif "Inclination background & drift corrected (deg)" in keys:inc_key="Inclination background & drift corrected (deg)"
            else: print("couldn't find inclination")
            if "Declination background + tray corrected (deg)" in keys: dec_key="Declination background + tray corrected (deg)"
            elif "Declination background &amp; tray corrected (deg)" in keys: dec_key="Declination background &amp; tray corrected (deg)"
            elif "Declination background & tray corrected (deg)" in keys:dec_key="Declination background & tray corrected (deg)"
            elif "Declination background & drift corrected (deg)" in keys:dec_key="Declination background & drift corrected (deg)"
            else: print("couldn't find declination")
            if "Intensity background + tray corrected  (A/m)" in keys: int_key="Intensity background + tray corrected  (A/m)"
            elif "Intensity background &amp; tray corrected (A/m)" in keys: int_key="Intensity background &amp; tray corrected (A/m)"
            elif "Intensity background & tray corrected (A/m)" in keys:int_key="Intensity background & tray corrected (A/m)"
            elif "Intensity background & drift corrected (A/m)" in keys:int_key="Intensity background & drift corrected (A/m)"
            else: print("couldn't find magnetic moment")
            type_val="Type"
            sect_key="Sect"
            half_key="A/W"
# need to add volume_key to LORE format!
            if "Sample volume (cm^3)" in keys:volume_key="Sample volume (cm^3)"
            elif "Sample volume (cc)" in keys:volume_key="Sample volume (cc)"
            elif "Sample volume (cm&sup3;)" in keys:volume_key="Sample volume (cm&sup3;)"
            elif "Sample volume (cm\xc2\xb3)" in keys:volume_key="Sample volume (cm\xc2\xb3)"
            else: volume_key=""
            for line in indata[1:]:
                InRec={}
                MeasRec,SpecRec,SampRec,SiteRec,LocRec={},{},{},{},{}
                for k in range(len(keys)):InRec[keys[k]]=line.split(',')[k].strip('"')
                inst="IODP-SRM"
                expedition=InRec['Exp']
                location=InRec['Site']+InRec['Hole']
                offsets=InRec[interval_key].split('.') # maintain consistency with er_samples convention of using top interval
                if len(offsets)==1:
                    offset=int(offsets[0])
                else:
                    offset=int(offsets[0])-1
                #interval=str(offset+1)# maintain consistency with er_samples convention of using top interval
                interval=str(offset)# maintain consistency with er_samples convention of using top interval
                specimen=expedition+'-'+location+'-'+InRec['Core']+InRec[type_val]+"-"+InRec[sect_key]+'-'+InRec[half_key]+'-'+str(InRec[interval_key])
                sample = expedition+'-'+location+'-'+InRec['Core']+InRec[type_val]
                site = expedition+'-'+location
                if volume_key in list(InRec.keys()): volume=InRec[volume_key]

                if not InRec[dec_key].strip(""" " ' """) or not InRec[inc_key].strip(""" " ' """):
                    print("No dec or inc found for specimen %s, skipping"%specimen)

                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['citations']=citations
                    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']=citations
                    SampRec['azimuth']='0'
                    SampRec['dip']='0'
                    SampRec['method_codes']='FS-C-DRILL-IODP:SO-V'
                    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']=citations
                    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']=citations
                    LocRec['expedition_name']=expedition
                    LocRec['lat_n'] = lat
                    LocRec['lon_e'] = lon
                    LocRec['lat_s'] = lat
                    LocRec['lon_w'] = lon
                    LocRecs.append(LocRec)

                MeasRec['specimen']=specimen
# set up measurement record - default is NRM
                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["treat_ac_field"]='0'
                MeasRec["treat_dc_field"]='0'
                MeasRec["treat_dc_field_phi"]='0'
                MeasRec["treat_dc_field_theta"]='0'
                MeasRec["quality"]='g' # assume all data are "good"
                MeasRec["standard"]='u' # assume all data are "good"
                MeasRec["dir_csd"]='0' # assume all data are "good"
                MeasRec["method_codes"]='LT-NO'
                sort_by='treat_ac_field' # set default to AF demag
                if treatment_type in list(InRec.keys()) and InRec[treatment_type]!="":
                    if "AF" in InRec[treatment_type].upper():
                        MeasRec['method_codes'] = 'LT-AF-Z'
                        inst=inst+':IODP-SRM-AF' # measured on shipboard in-line 2G AF
                        treatment_value=float(InRec[demag_key].strip('"'))*1e-3 # convert mT => T
                        MeasRec["treat_ac_field"]=str(treatment_value) # AF demag in treat mT => T
                    elif "T" in InRec[treatment_type].upper():
                        MeasRec['method_codes'] = 'LT-T-Z'
                        inst=inst+':IODP-TDS' # measured on shipboard Schonstedt thermal demagnetizer
                        treatment_value=float(InRec[demag_key].strip('"'))+273 # convert C => K
                        MeasRec["treat_temp"]=str(treatment_value)
                    elif "Lowrie" in InRec['Comments']:
                        MeasRec['method_codes'] = 'LP-IRM-3D'
                        treatment_value=float(InRec[demag_key].strip('"'))+273. # convert C => K
                        MeasRec["treat_temp"]=str(treatment_value)
                        MeasRec["treat_ac_field"]="0"
                        sort_by='treat_temp'
                    elif 'Isothermal' in InRec[treatment_type]:
                        MeasRec['method_codes'] = 'LT-IRM'
                        treatment_value=float(InRec[demag_key].strip('"'))*1e-3 # convert mT => T
                        MeasRec["treat_dc_field"]=str(treatment_value)
                        MeasRec["treat_ac_field"]="0"
                        sort_by='treat_dc_field'
                elif InRec[demag_key]!="0" and InRec[demag_key]!="": #Assume AF if there is no Treatment typ info
                    MeasRec['method_codes'] = 'LT-AF-Z'
                    inst=inst+':IODP-SRM-AF' # measured on shipboard in-line 2G AF
                    treatment_value=float(InRec[demag_key].strip('"'))*1e-3 # convert mT => T
                    MeasRec["treat_ac_field"]=treatment_value # AF demag in treat mT => T
                MeasRec["standard"]='u' # assume all data are "good"
                vol=float(volume)
                if run_key in list(InRec.keys()):
                    run_number=InRec[run_key]
                    MeasRec['external_database_ids']={'LIMS':run_number}
                else:
                    MeasRec['external_database_ids']=""
                MeasRec['description']='sample orientation: '+InRec['Sample orientation']
                MeasRec['dir_inc']=InRec[inc_key].strip('"')
                MeasRec['dir_dec']=InRec[dec_key].strip('"')
                intens= InRec[int_key].strip('"')
                MeasRec['magn_moment']='%8.3e'%(float(intens)*vol) # convert intensity from A/m to Am^2 using vol
                MeasRec['instrument_codes']=inst
                MeasRec['treat_step_num']='1'
                MeasRec['meas_n_orient']=''
                MeasRecs.append(MeasRec)
    if not file_found:
        print("No .csv files were found")
        return False, "No .csv files were found"

    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)
    MeasSort=sorted(MeasRecs, lambda x,y=None: int(round(float(x[sort_by])-float(y[sort_by]))) if y!=None else 0)
    MeasFixed=pmag.measurements_methods3(MeasSort,noave)
    MeasOuts,keys=pmag.fillkeys(MeasFixed)
    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