def kly4s(infile, specnum=0, locname="unknown", inst='SIO-KLY4S',
          samp_con="1", or_con='3', user='', measfile='measurements.txt',
          aniso_outfile='rmag_anisotropy.txt', samp_infile='', spec_infile='',
          spec_outfile='specimens.txt', azdip_infile='', dir_path='.',
          input_dir_path='', data_model_num=3, samp_outfile='samples.txt',
          site_outfile='sites.txt'):
    """

    converts files generated by SIO kly4S labview program to MagIC formated

    Parameters
    ----------
    infile :  str
        input file name
    specnum : int
        number of characters to designate a specimen, default 0
    locname : str
        location name, default "unknown"
    samp_con : str
        sample/site naming convention, default '1', see info below
    or_con : number
        orientation convention, default '3', see info below
    user : str
        user name, default ""
    measfile : str
        output measurement file name, default "measurements.txt"
    aniso_outfile : str
        output anisotropy file name, default "rmag_anisotropy.txt", data model 2 only
    samp_infile : str
        existing sample infile (not required), default ""
    spec_infile : str
        existing site infile (not required), default ""
    spec_outfile : str
        output specimen file name, default "specimens.txt"
    azdip_infile : str
        AZDIP file with orientations, will create sample output file
    dir_path : str
        output directory, default "."
    input_dir_path : str
        input file directory IF different from dir_path, default ""
    data_model_num : int
        MagIC data model 2 or 3, default 3
    samp_outfile : str
        sample output filename, default "samples.txt"
    site_outfile : str
        site output filename, default "sites.txt"

    Returns
    --------
    type - Tuple : (True or False indicating if conversion was sucessful, meas_file name written)


    Info
    ----------
    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 ltauxe@ucsd.edu for help.

   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


    """

    # initialize variables
    # not used: #cont=0
    input_dir_path, output_dir_path = pmag.fix_directories(input_dir_path, dir_path)
    data_model_num = int(float(data_model_num))
    ask = 0
    Z = 1
    AniRecs, SpecRecs, SampRecs, MeasRecs = [], [], [], []
    AppSpec = 0

    # set defaults for MagIC 2
    if data_model_num == 2:
        if measfile == 'measurements.txt':
            measfile = 'magic_measurements.txt'
        if spec_outfile == 'specimens.txt':
            spec_outfile = 'er_specimens.txt'

    # format variables
    specnum = int(specnum)
    samp_con = str(samp_con)
    or_con = str(or_con)
    if azdip_infile:
        azdip_infile = os.path.join(input_dir_path, azdip_infile)
        azfile = open(azdip_infile, 'r')
        AzDipDat = azfile.readlines()
        azfile.close()
    amsfile = os.path.join(input_dir_path, infile)
    if spec_infile:
        spec_infile = os.path.join(input_dir_path, spec_infile)
        AppSpec = 1
    else:
        spec_outfile = os.path.join(output_dir_path, spec_outfile)
        AppSpec = 0
    if samp_infile:
        samp_infile = os.path.join(input_dir_path, samp_infile)
    measfile = os.path.join(output_dir_path, measfile)
    anisfile = os.path.join(output_dir_path, aniso_outfile)

    # set column names for MagIC 3
    spec_name_col = 'specimen'  #
    samp_name_col = 'sample'   #
    site_name_col = 'site'   #
    loc_name_col = 'location' #
    citation_col = 'citations'
    method_col = 'method_codes'
    site_description_col = 'description'
    expedition_col = 'expedition_name'
    instrument_col = 'instrument_codes'
    experiment_col = 'experiments'
    analyst_col = 'analysts'
    quality_col = 'quality'
    aniso_quality_col = 'result_quality'
    meas_standard_col = 'standard'
    meas_description_col = 'description'
    aniso_type_col = 'aniso_type'
    aniso_unit_col = 'aniso_s_unit'
    aniso_n_col = 'aniso_s_n_measurements'
    azimuth_col = 'azimuth'
    spec_volume_col = 'volume'
    samp_dip_col = 'dip'
    bed_dip_col = 'bed_dip'
    bed_dip_direction_col = 'bed_dip_direction'
    chi_vol_col = 'susc_chi_volume'
    aniso_sigma_col = 'aniso_s_sigma'
    aniso_unit_col = 'aniso_s_unit'
    aniso_tilt_corr_col = 'aniso_tilt_correction'
    meas_table_name = 'measurements'
    spec_table_name = 'specimens'
    samp_table_name = 'samples'
    site_table_name = 'sites'
    #
    meas_name_col = 'measurement'
    meas_time_col = 'timestamp'
    meas_ac_col = 'meas_field_ac'
    meas_temp_col = "meas_temp"

    # set column names for MagIC 2
    if data_model_num == 2:
        spec_name_col = 'er_specimen_name'
        samp_name_col = 'er_sample_name'
        site_name_col = 'er_site_name'
        loc_name_col = 'er_location_name'
        citation_col = 'er_citation_names'
        method_col = 'magic_method_codes'
        site_description_col = 'site_description'
        expedition_col = 'er_expedition_name'
        instrument_col = 'magic_instrument_codes'
        experiment_col = 'magic_experiment_names'
        analyst_col = 'er_analyst_mail_names'
        quality_col = 'measurement_flag'
        aniso_quality_col = 'anisotropy_flag'
        meas_standard_col = 'measurement_standard'
        meas_description_col = 'measurement_description'
        aniso_type_col = 'anisotropy_type'
        aniso_unit_col = 'anisotropy_unit'
        aniso_n_col = 'anisotropy_n'
        azimuth_col = 'sample_azimuth'
        spec_volume_col = 'specimen_volume'
        samp_dip_col = 'sample_dip'
        bed_dip_col = 'sample_bed_dip'
        bed_dip_direction_col = 'sample_bed_dip_direction'
        chi_vol_col = 'measurement_chi_volume'
        aniso_sigma_col = 'anisotropy_sigma'
        aniso_unit_col = 'anisotropy_unit'
        aniso_tilt_corr_col = 'anisotropy_tilt_correction'
        meas_table_name = 'magic_measurements'
        spec_table_name = 'er_specimens'
        samp_table_name = 'er_samples'
        site_table_name = 'er_sites'
        #
        meas_name_col = 'measurement_number'
        meas_time_col = 'measurement_date'
        meas_ac_col = 'measurement_lab_field_ac'
        meas_temp_col = "measurement_temp"

    # validate variables
    if "4" in samp_con[0]:
        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[0]:
        if "-" not in samp_con:
            print("option [7] must be in form 7-Z where Z is an integer")
            return False, "option [7] must be in form 7-Z where Z is an integer"
        else:
            Z = samp_con.split("-")[1]
            samp_con = "7"

    try:
        file_input = open(amsfile, 'r')
    except:
        print('Error opening file: ', amsfile)
        return False, 'Error opening file: {}'.format(amsfile)

    # parse file
    SpecRecs, speclist = [], []
    if AppSpec == 1:
        try:
            SpecRecs, filetype = pmag.magic_read(
                spec_infile)  # append new records to existing
            if len(SpecRecs) > 0:
                for spec in SpecRecs:
                    if spec[spec_name_col] not in speclist:
                        speclist.append(spec[spec_name_col])
        except IOError:
            print('trouble opening ', spec_infile)
    Data = file_input.readlines()
    file_input.close()
    samps = []
    if samp_infile:
        samps, file_type = pmag.magic_read(samp_infile)
        SO_methods = []
        for rec in samps:
            if "magic_method_codes" in list(rec.keys()):
                methlist = rec["magic_method_codes"].replace(
                    " ", "").split(":")
                for meth in methlist:
                    if "SO" in meth and "SO-POM" not in meth and "SO-GT5" not in meth and "SO-ASC" not in meth and "SO-BAD" not in meth:
                        if meth not in SO_methods:
                            SO_methods.append(meth)
    #
        SO_priorities = pmag.set_priorities(SO_methods, ask)
    for line in Data:
        rec = line.split()
        if len(rec) > 0:
            AniRec, SpecRec, SampRec, SiteRec, MeasRec = {}, {}, {}, {}, {}
            specname = rec[0]
            if specnum != 0:
                sampname = specname[:-specnum]
            else:
                sampname = specname
            site = pmag.parse_site(sampname, samp_con, Z)
            AniRec[loc_name_col] = locname
            AniRec[citation_col] = "This study"
            AniRec[instrument_col] = inst
            method_codes = ['LP-X', 'AE-H', 'LP-AN-MS']
            AniRec[experiment_col] = specname + ":" + "LP-AN-MS"
            AniRec[analyst_col] = user
            AniRec[site_name_col] = site
            AniRec[samp_name_col] = sampname
            AniRec[spec_name_col] = specname
            labaz, labdip, bed_dip_direction, bed_dip = "", "", "", ""
            if azdip_infile:
                for key in list(AniRec.keys()):
                    SampRec[key] = AniRec[key]
                for oline in AzDipDat:  # look for exact match first
                    orec = oline.replace('\n', '').split()
                    if orec[0].upper() in specname.upper():  # we have a match
                        labaz, labdip = pmag.orient(
                            float(orec[1]), float(orec[2]), or_con)
                        bed_dip_direction = float(
                            orec[3]) - 90.  # assume dip to right of strike
                        bed_dip = float(orec[4])
                        break
                if labaz == "":  # found no exact match - now look at sample level
                    for oline in AzDipDat:
                        orec = oline.split()
                        if orec[0].upper() == sampname.upper():  # we have a match
                            labaz, labdip = pmag.orient(
                                float(orec[1]), float(orec[2]), or_con)
                            bed_dip_direction = float(
                                orec[3]) - 90.  # assume dip to right of strike
                            bed_dip = float(orec[4])
                            break
                if labaz == "":  # found no exact match - now look at sample level
                    print('found no orientation data - will use specimen coordinates')
                    #raw_input("<return> to continue")
                else:
                    for key in list(AniRec.keys()):
                        SampRec[key] = AniRec[key]
                    SampRec[azimuth_col] = '%7.1f' % (labaz)
                    SampRec[dip_col] = '%7.1f' % (labdip)
                    SampRec[bed_dip_direction_col] = '%7.1f' % (
                        bed_dip_direction)
                    SampRec[bed_dip_col] = '%7.1f' % (bed_dip)
                    SampRecs.append(SampRec)
            elif samp_infile:
                redo, p = 1, 0
                orient = {}
                if len(SO_methods) == 1:
                    az_type = SO_methods[0]
                    orient = pmag.find_samp_rec(
                        AniRec[samp_name_col], samps, az_type)
                    if orient[azimuth_col] != "":
                        method_codes.append(az_type)
                    else:
                        print("no orientation data for ",
                              AniRec[samp_name_col], labaz)
                        orient[azimuth_col] = ""
                        orient[dip_col] = ""
                        orient[bed_dip_direction_col] = ""
                        orient[dip_col] = ""
                        noorient = 1
                        method_codes.append("SO-NO")
                        redo = 0
                    redo = 0
                while redo == 1:
                    if p >= len(SO_priorities):
                        print("no orientation data for ",
                              AniRec[samp_name_col], labaz)
                        orient[azimuth_col] = ""
                        orient[dip_col] = ""
                        orient[bed_dip_direction_col] = ""
                        orient[bed_dip_col] = ""
                        noorient = 1
                        method_codes.append("SO-NO")
                        redo = 0
                    else:
                        az_type = SO_methods[SO_methods.index(
                            SO_priorities[p])]
                        orient = pmag.find_samp_rec(
                            AniRec[samp_name_col], samps, az_type)
                        if orient[azimuth_col] != "":
                            method_codes.append(az_type)
                            redo = 0
                        noorient = 0
                    p += 1
                if orient[azimuth_col] != "":
                    labaz = float(orient[azimuth_col])
                if orient[dip_col] != "":
                    labdip = float(orient[dip_col])
                if dip_direction_col in list(orient.keys()) and orient[dip_direction_col] != "":
                    bed_dip_direction = float(
                        orient[dip_direction_col])
                if dip_col in list(orient.keys()) and orient[dip_col] != "":
                    sample_bed_dip = float(orient[dip_col])
            for key in list(AniRec.keys()):
                SpecRec[key] = AniRec[key]
            for key in list(AniRec.keys()):
                MeasRec[key] = AniRec[key]
            AniRec[aniso_type_col] = "AMS"
            AniRec[aniso_n_col] = "192"
            s1_val = rec[1]
            s2_val = rec[2]
            s3_val = rec[3]
            s4_val = rec[4]
            s5_val = rec[5]
            s6_val = rec[6]
            if data_model_num == 2:
                AniRec['anisotropy_s1'] = s1_val
                AniRec['anisotropy_s2'] = s2_val
                AniRec['anisotropy_s3'] = s3_val
                AniRec['anisotropy_s4'] = s4_val
                AniRec['anisotropy_s5'] = s5_val
                AniRec['anisotropy_s6'] = s6_val
            else:
                vals = [s1_val, s2_val, s3_val, s4_val, s5_val, s6_val]
                AniRec['aniso_s'] = ":".join([val.strip() for val in vals])
            AniRec[aniso_sigma_col] = rec[7]
            AniRec[aniso_tilt_corr_col] = '-1'
            AniRec[aniso_unit_col] = 'Normalized by trace'
            SpecRec[spec_volume_col] = '%8.3e' % (
                1e-6 * float(rec[12]))  # volume from cc to m^3
            MeasRec[quality_col] = 'g'  # good
            MeasRec[meas_standard_col] = 'u'  # unknown
            date = rec[14].split('/')
            if int(date[2]) > 80:
                date[2] = '19' + date[2]
            else:
                date[2] = '20' + date[2]
            datetime = date[2] + ':' + date[0] + ':' + date[1] + ":"
            datetime = datetime + rec[15]
            MeasRec[meas_name_col] = '1'
            MeasRec[meas_time_col] = datetime
            MeasRec[meas_ac_col] = '%8.3e' % (
                4 * np.pi * 1e-7 * float(rec[11]))  # convert from A/m to T
            MeasRec[meas_temp_col] = "300"  # assumed room T in kelvin
            MeasRec[chi_vol_col] = rec[8]
            MeasRec[meas_description_col] = 'Bulk measurement'
            MeasRec[method_col] = 'LP-X'
            # remove keys that aren't valid in MagIC 2.5
            if data_model_num == 2:
                for remove_key in ['magic_instrument_codes', 'magic_experiment_names']:
                    if remove_key in SpecRec:
                        val = SpecRec.pop(remove_key)
                #for remove_key in ['magic_experiment_name']:
                #    if remove_key in AniRec:
                #        AniRec.pop(remove_key)
                MeasRec['magic_experiment_name'] = AniRec.get('magic_experiment_names', '')
                if 'magic_experiment_names' in MeasRec:
                    MeasRec.pop('magic_experiment_names')
            else:
                MeasRec['experiment'] = AniRec.get('experiments', '')
                if 'experiments' in MeasRec:
                    MeasRec.pop('experiments')
            #
            if SpecRec[spec_name_col] not in speclist:  # add to list
                speclist.append(SpecRec[spec_name_col])
                SpecRecs.append(SpecRec)
            MeasRecs.append(MeasRec)
            methods = ""
            for meth in method_codes:
                methods = methods + meth + ":"
            # get rid of annoying spaces in Anthony's export files
            AniRec[method_col] = methods.strip()
            AniRecs.append(AniRec)
            if labaz != "":  # have orientation info
                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(s1_val))
                sbar.append(float(s2_val))
                sbar.append(float(s3_val))
                sbar.append(float(s4_val))
                sbar.append(float(s5_val))
                sbar.append(float(s6_val))
                sbarg = pmag.dosgeo(sbar, labaz, labdip)
                if data_model_num == 2:
                    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])
                else:
                    AniRecG['aniso_s'] = ":".join([str(a) for a in sbarg])
                AniRecG[aniso_tilt_corr_col] = '0'
                AniRecs.append(AniRecG)
                if bed_dip != "" and bed_dip != 0:  # have tilt correction
                    sbart = pmag.dostilt(sbarg, bed_dip_direction, bed_dip)
                    if data_model_num == 2:
                        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])
                    else:
                        AniRecT['aniso_s'] = ":".join([str(a) for a in sbart])
                    AniRecT[aniso_tilt_corr_col] = '100'
                    AniRecs.append(AniRecT)

    # for MagIC 2, anisotropy records go in rmag_anisotropy
    if data_model_num == 2:
        pmag.magic_write(anisfile, AniRecs, 'rmag_anisotropy')
        print("anisotropy data saved in ", anisfile)
    # for MagIC 3, anisotropy records go in specimens
    else:
        full_SpecRecs = []
        SampRecs = []
        SiteRecs = []
        spec_list = []
        samp_list = []
        site_list = []
        for rec in SpecRecs:
            full_SpecRecs.append(rec)
            spec_name = rec[spec_name_col]
            samp_name = rec.get(samp_name_col, '')
            site_name = rec.get(site_name_col, '')
            loc_name = rec.get(loc_name_col, '')
            if spec_name not in spec_list:
                ani_recs = pmag.get_dictitem(AniRecs, spec_name_col, spec_name, 'T')
                full_SpecRecs.extend(ani_recs)
                if (samp_name not in samp_list) and (samp_name):
                    samp_list.append(samp_name)
                    SampRecs.append({samp_name_col: samp_name, site_name_col: site_name})
                    if (site_name not in site_list) and (site_name):
                        SiteRecs.append({site_name_col: site_name, loc_name_col: loc_name})
        full_SpecRecs, keys = pmag.fillkeys(full_SpecRecs)
        SpecRecs = full_SpecRecs
        print('anisotropy data added to specimen records')
    if AppSpec:
        pmag.magic_write(spec_infile, SpecRecs, spec_table_name)
        print('specimen information appended to {}'.format(spec_infile))
    else:
        pmag.magic_write(spec_outfile, SpecRecs, spec_table_name)
        print('specimen information written to new file: {}'.format(spec_outfile))
    pmag.magic_write(measfile, MeasRecs, meas_table_name)
    print('measurement data saved in ', measfile)
    if azdip_infile:
        sampfile = 'er_samples.txt'
        pmag.magic_write(sampfile, SampRecs, samp_table_name)
        print('sample data saved in ', sampfile)
    if data_model_num == 3:
        if not azdip_infile:
            if SampRecs:
                pmag.magic_write(samp_outfile, SampRecs, samp_table_name)
        if SiteRecs:
            pmag.magic_write(site_outfile, SiteRecs, site_table_name)
    return True, measfile
def kly4s ( infile , specnum = 0 , locname = "unknown" , inst = 'SIO-KLY4S' , samp_con = "1" , or_con = '3' , user = '' , measfile = 'measurements.txt' , aniso_outfile = 'rmag_anisotropy.txt' , samp_infile = '' , spec_infile = '' , spec_outfile = 'specimens.txt' , azdip_infile = '' , dir_path = '.' , input_dir_path = '' , data_model_num = 3 , samp_outfile = 'samples.txt' , site_outfile = 'sites.txt' ) : input_dir_path , output_dir_path = pmag . fix_directories ( input_dir_path , dir_path ) data_model_num = int ( float ( data_model_num ) ) ask = 0 Z = 1 AniRecs , SpecRecs , SampRecs , MeasRecs = [ ] , [ ] , [ ] , [ ] AppSpec = 0 if data_model_num == 2 : if measfile == 'measurements.txt' : measfile = 'magic_measurements.txt' if spec_outfile == 'specimens.txt' : spec_outfile = 'er_specimens.txt' specnum = int ( specnum ) samp_con = str ( samp_con ) or_con = str ( or_con ) if azdip_infile : azdip_infile = os . path . join ( input_dir_path , azdip_infile ) azfile = open ( azdip_infile , 'r' ) AzDipDat = azfile . readlines ( ) azfile . close ( ) amsfile = os . path . join ( input_dir_path , infile ) if spec_infile : spec_infile = os . path . join ( input_dir_path , spec_infile ) AppSpec = 1 else : spec_outfile = os . path . join ( output_dir_path , spec_outfile ) AppSpec = 0 if samp_infile : samp_infile = os . path . join ( input_dir_path , samp_infile ) measfile = os . path . join ( output_dir_path , measfile ) anisfile = os . path . join ( output_dir_path , aniso_outfile ) spec_name_col = 'specimen' samp_name_col = 'sample' site_name_col = 'site' loc_name_col = 'location' citation_col = 'citations' method_col = 'method_codes' site_description_col = 'description' expedition_col = 'expedition_name' instrument_col = 'instrument_codes' experiment_col = 'experiments' analyst_col = 'analysts' quality_col = 'quality' aniso_quality_col = 'result_quality' meas_standard_col = 'standard' meas_description_col = 'description' aniso_type_col = 'aniso_type' aniso_unit_col = 'aniso_s_unit' aniso_n_col = 'aniso_s_n_measurements' azimuth_col = 'azimuth' spec_volume_col = 'volume' samp_dip_col = 'dip' bed_dip_col = 'bed_dip' bed_dip_direction_col = 'bed_dip_direction' chi_vol_col = 'susc_chi_volume' aniso_sigma_col = 'aniso_s_sigma' aniso_unit_col = 'aniso_s_unit' aniso_tilt_corr_col = 'aniso_tilt_correction' meas_table_name = 'measurements' spec_table_name = 'specimens' samp_table_name = 'samples' site_table_name = 'sites' meas_name_col = 'measurement' meas_time_col = 'timestamp' meas_ac_col = 'meas_field_ac' meas_temp_col = "meas_temp" if data_model_num == 2 : spec_name_col = 'er_specimen_name' samp_name_col = 'er_sample_name' site_name_col = 'er_site_name' loc_name_col = 'er_location_name' citation_col = 'er_citation_names' method_col = 'magic_method_codes' site_description_col = 'site_description' expedition_col = 'er_expedition_name' instrument_col = 'magic_instrument_codes' experiment_col = 'magic_experiment_names' analyst_col = 'er_analyst_mail_names' quality_col = 'measurement_flag' aniso_quality_col = 'anisotropy_flag' meas_standard_col = 'measurement_standard' meas_description_col = 'measurement_description' aniso_type_col = 'anisotropy_type' aniso_unit_col = 'anisotropy_unit' aniso_n_col = 'anisotropy_n' azimuth_col = 'sample_azimuth' spec_volume_col = 'specimen_volume' samp_dip_col = 'sample_dip' bed_dip_col = 'sample_bed_dip' bed_dip_direction_col = 'sample_bed_dip_direction' chi_vol_col = 'measurement_chi_volume' aniso_sigma_col = 'anisotropy_sigma' aniso_unit_col = 'anisotropy_unit' aniso_tilt_corr_col = 'anisotropy_tilt_correction' meas_table_name = 'magic_measurements' spec_table_name = 'er_specimens' samp_table_name = 'er_samples' site_table_name = 'er_sites' meas_name_col = 'measurement_number' meas_time_col = 'measurement_date' meas_ac_col = 'measurement_lab_field_ac' meas_temp_col = "measurement_temp" if "4" in samp_con [ 0 ] : 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 [ 0 ] : if "-" not in samp_con : print ( "option [7] must be in form 7-Z where Z is an integer" ) return False , "option [7] must be in form 7-Z where Z is an integer" else : Z = samp_con . split ( "-" ) [ 1 ] samp_con = "7" try : file_input = open ( amsfile , 'r' ) except : print ( 'Error opening file: ' , amsfile ) return False , 'Error opening file: {}' . format ( amsfile ) SpecRecs , speclist = [ ] , [ ] if AppSpec == 1 : try : SpecRecs , filetype = pmag . magic_read ( spec_infile ) if len ( SpecRecs ) > 0 : for spec in SpecRecs : if spec [ spec_name_col ] not in speclist : speclist . append ( spec [ spec_name_col ] ) except IOError : print ( 'trouble opening ' , spec_infile ) Data = file_input . readlines ( ) file_input . close ( ) samps = [ ] if samp_infile : samps , file_type = pmag . magic_read ( samp_infile ) SO_methods = [ ] for rec in samps : if "magic_method_codes" in list ( rec . keys ( ) ) : methlist = rec [ "magic_method_codes" ] . replace ( " " , "" ) . split ( ":" ) for meth in methlist : if "SO" in meth and "SO-POM" not in meth and "SO-GT5" not in meth and "SO-ASC" not in meth and "SO-BAD" not in meth : if meth not in SO_methods : SO_methods . append ( meth ) SO_priorities = pmag . set_priorities ( SO_methods , ask ) for line in Data : rec = line . split ( ) if len ( rec ) > 0 : AniRec , SpecRec , SampRec , SiteRec , MeasRec = { } , { } , { } , { } , { } specname = rec [ 0 ] if specnum != 0 : sampname = specname [ : - specnum ] else : sampname = specname site = pmag . parse_site ( sampname , samp_con , Z ) AniRec [ loc_name_col ] = locname AniRec [ citation_col ] = "This study" AniRec [ instrument_col ] = inst method_codes = [ 'LP-X' , 'AE-H' , 'LP-AN-MS' ] AniRec [ experiment_col ] = specname + ":" + "LP-AN-MS" AniRec [ analyst_col ] = user AniRec [ site_name_col ] = site AniRec [ samp_name_col ] = sampname AniRec [ spec_name_col ] = specname labaz , labdip , bed_dip_direction , bed_dip = "" , "" , "" , "" if azdip_infile : for key in list ( AniRec . keys ( ) ) : SampRec [ key ] = AniRec [ key ] for oline in AzDipDat : orec = oline . replace ( '\n' , '' ) . split ( ) if orec [ 0 ] . upper ( ) in specname . upper ( ) : labaz , labdip = pmag . orient ( float ( orec [ 1 ] ) , float ( orec [ 2 ] ) , or_con ) bed_dip_direction = float ( orec [ 3 ] ) - 90. bed_dip = float ( orec [ 4 ] ) break if labaz == "" : for oline in AzDipDat : orec = oline . split ( ) if orec [ 0 ] . upper ( ) == sampname . upper ( ) : labaz , labdip = pmag . orient ( float ( orec [ 1 ] ) , float ( orec [ 2 ] ) , or_con ) bed_dip_direction = float ( orec [ 3 ] ) - 90. bed_dip = float ( orec [ 4 ] ) break if labaz == "" : print ( 'found no orientation data - will use specimen coordinates' ) else : for key in list ( AniRec . keys ( ) ) : SampRec [ key ] = AniRec [ key ] SampRec [ azimuth_col ] = '%7.1f' % ( labaz ) SampRec [ dip_col ] = '%7.1f' % ( labdip ) SampRec [ bed_dip_direction_col ] = '%7.1f' % ( bed_dip_direction ) SampRec [ bed_dip_col ] = '%7.1f' % ( bed_dip ) SampRecs . append ( SampRec ) elif samp_infile : redo , p = 1 , 0 orient = { } if len ( SO_methods ) == 1 : az_type = SO_methods [ 0 ] orient = pmag . find_samp_rec ( AniRec [ samp_name_col ] , samps , az_type ) if orient [ azimuth_col ] != "" : method_codes . append ( az_type ) else : print ( "no orientation data for " , AniRec [ samp_name_col ] , labaz ) orient [ azimuth_col ] = "" orient [ dip_col ] = "" orient [ bed_dip_direction_col ] = "" orient [ dip_col ] = "" noorient = 1 method_codes . append ( "SO-NO" ) redo = 0 redo = 0 while redo == 1 : if p >= len ( SO_priorities ) : print ( "no orientation data for " , AniRec [ samp_name_col ] , labaz ) orient [ azimuth_col ] = "" orient [ dip_col ] = "" orient [ bed_dip_direction_col ] = "" orient [ bed_dip_col ] = "" noorient = 1 method_codes . append ( "SO-NO" ) redo = 0 else : az_type = SO_methods [ SO_methods . index ( SO_priorities [ p ] ) ] orient = pmag . find_samp_rec ( AniRec [ samp_name_col ] , samps , az_type ) if orient [ azimuth_col ] != "" : method_codes . append ( az_type ) redo = 0 noorient = 0 p += 1 if orient [ azimuth_col ] != "" : labaz = float ( orient [ azimuth_col ] ) if orient [ dip_col ] != "" : labdip = float ( orient [ dip_col ] ) if dip_direction_col in list ( orient . keys ( ) ) and orient [ dip_direction_col ] != "" : bed_dip_direction = float ( orient [ dip_direction_col ] ) if dip_col in list ( orient . keys ( ) ) and orient [ dip_col ] != "" : sample_bed_dip = float ( orient [ dip_col ] ) for key in list ( AniRec . keys ( ) ) : SpecRec [ key ] = AniRec [ key ] for key in list ( AniRec . keys ( ) ) : MeasRec [ key ] = AniRec [ key ] AniRec [ aniso_type_col ] = "AMS" AniRec [ aniso_n_col ] = "192" s1_val = rec [ 1 ] s2_val = rec [ 2 ] s3_val = rec [ 3 ] s4_val = rec [ 4 ] s5_val = rec [ 5 ] s6_val = rec [ 6 ] if data_model_num == 2 : AniRec [ 'anisotropy_s1' ] = s1_val AniRec [ 'anisotropy_s2' ] = s2_val AniRec [ 'anisotropy_s3' ] = s3_val AniRec [ 'anisotropy_s4' ] = s4_val AniRec [ 'anisotropy_s5' ] = s5_val AniRec [ 'anisotropy_s6' ] = s6_val else : vals = [ s1_val , s2_val , s3_val , s4_val , s5_val , s6_val ] AniRec [ 'aniso_s' ] = ":" . join ( [ val . strip ( ) for val in vals ] ) AniRec [ aniso_sigma_col ] = rec [ 7 ] AniRec [ aniso_tilt_corr_col ] = '-1' AniRec [ aniso_unit_col ] = 'Normalized by trace' SpecRec [ spec_volume_col ] = '%8.3e' % ( 1e-6 * float ( rec [ 12 ] ) ) MeasRec [ quality_col ] = 'g' MeasRec [ meas_standard_col ] = 'u' date = rec [ 14 ] . split ( '/' ) if int ( date [ 2 ] ) > 80 : date [ 2 ] = '19' + date [ 2 ] else : date [ 2 ] = '20' + date [ 2 ] datetime = date [ 2 ] + ':' + date [ 0 ] + ':' + date [ 1 ] + ":" datetime = datetime + rec [ 15 ] MeasRec [ meas_name_col ] = '1' MeasRec [ meas_time_col ] = datetime MeasRec [ meas_ac_col ] = '%8.3e' % ( 4 * np . pi * 1e-7 * float ( rec [ 11 ] ) ) MeasRec [ meas_temp_col ] = "300" MeasRec [ chi_vol_col ] = rec [ 8 ] MeasRec [ meas_description_col ] = 'Bulk measurement' MeasRec [ method_col ] = 'LP-X' if data_model_num == 2 : for remove_key in [ 'magic_instrument_codes' , 'magic_experiment_names' ] : if remove_key in SpecRec : val = SpecRec . pop ( remove_key ) MeasRec [ 'magic_experiment_name' ] = AniRec . get ( 'magic_experiment_names' , '' ) if 'magic_experiment_names' in MeasRec : MeasRec . pop ( 'magic_experiment_names' ) else : MeasRec [ 'experiment' ] = AniRec . get ( 'experiments' , '' ) if 'experiments' in MeasRec : MeasRec . pop ( 'experiments' ) if SpecRec [ spec_name_col ] not in speclist : speclist . append ( SpecRec [ spec_name_col ] ) SpecRecs . append ( SpecRec ) MeasRecs . append ( MeasRec ) methods = "" for meth in method_codes : methods = methods + meth + ":" AniRec [ method_col ] = methods . strip ( ) AniRecs . append ( AniRec ) if labaz != "" : 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 ( s1_val ) ) sbar . append ( float ( s2_val ) ) sbar . append ( float ( s3_val ) ) sbar . append ( float ( s4_val ) ) sbar . append ( float ( s5_val ) ) sbar . append ( float ( s6_val ) ) sbarg = pmag . dosgeo ( sbar , labaz , labdip ) if data_model_num == 2 : 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 ] ) else : AniRecG [ 'aniso_s' ] = ":" . join ( [ str ( a ) for a in sbarg ] ) AniRecG [ aniso_tilt_corr_col ] = '0' AniRecs . append ( AniRecG ) if bed_dip != "" and bed_dip != 0 : sbart = pmag . dostilt ( sbarg , bed_dip_direction , bed_dip ) if data_model_num == 2 : 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 ] ) else : AniRecT [ 'aniso_s' ] = ":" . join ( [ str ( a ) for a in sbart ] ) AniRecT [ aniso_tilt_corr_col ] = '100' AniRecs . append ( AniRecT ) if data_model_num == 2 : pmag . magic_write ( anisfile , AniRecs , 'rmag_anisotropy' ) print ( "anisotropy data saved in " , anisfile ) else : full_SpecRecs = [ ] SampRecs = [ ] SiteRecs = [ ] spec_list = [ ] samp_list = [ ] site_list = [ ] for rec in SpecRecs : full_SpecRecs . append ( rec ) spec_name = rec [ spec_name_col ] samp_name = rec . get ( samp_name_col , '' ) site_name = rec . get ( site_name_col , '' ) loc_name = rec . get ( loc_name_col , '' ) if spec_name not in spec_list : ani_recs = pmag . get_dictitem ( AniRecs , spec_name_col , spec_name , 'T' ) full_SpecRecs . extend ( ani_recs ) if ( samp_name not in samp_list ) and ( samp_name ) : samp_list . append ( samp_name ) SampRecs . append ( { samp_name_col : samp_name , site_name_col : site_name } ) if ( site_name not in site_list ) and ( site_name ) : SiteRecs . append ( { site_name_col : site_name , loc_name_col : loc_name } ) full_SpecRecs , keys = pmag . fillkeys ( full_SpecRecs ) SpecRecs = full_SpecRecs print ( 'anisotropy data added to specimen records' ) if AppSpec : pmag . magic_write ( spec_infile , SpecRecs , spec_table_name ) print ( 'specimen information appended to {}' . format ( spec_infile ) ) else : pmag . magic_write ( spec_outfile , SpecRecs , spec_table_name ) print ( 'specimen information written to new file: {}' . format ( spec_outfile ) ) pmag . magic_write ( measfile , MeasRecs , meas_table_name ) print ( 'measurement data saved in ' , measfile ) if azdip_infile : sampfile = 'er_samples.txt' pmag . magic_write ( sampfile , SampRecs , samp_table_name ) print ( 'sample data saved in ' , sampfile ) if data_model_num == 3 : if not azdip_infile : if SampRecs : pmag . magic_write ( samp_outfile , SampRecs , samp_table_name ) if SiteRecs : pmag . magic_write ( site_outfile , SiteRecs , site_table_name ) return True , measfile
converts files generated by SIO kly4S labview program to MagIC formated

    Parameters
    ----------
    infile :  str
        input file name
    specnum : int
        number of characters to designate a specimen, default 0
    locname : str
        location name, default "unknown"
    samp_con : str
        sample/site naming convention, default '1', see info below
    or_con : number
        orientation convention, default '3', see info below
    user : str
        user name, default ""
    measfile : str
        output measurement file name, default "measurements.txt"
    aniso_outfile : str
        output anisotropy file name, default "rmag_anisotropy.txt", data model 2 only
    samp_infile : str
        existing sample infile (not required), default ""
    spec_infile : str
        existing site infile (not required), default ""
    spec_outfile : str
        output specimen file name, default "specimens.txt"
    azdip_infile : str
        AZDIP file with orientations, will create sample output file
    dir_path : str
        output directory, default "."
    input_dir_path : str
        input file directory IF different from dir_path, default ""
    data_model_num : int
        MagIC data model 2 or 3, default 3
    samp_outfile : str
        sample output filename, default "samples.txt"
    site_outfile : str
        site output filename, default "sites.txt"

    Returns
    --------
    type - Tuple : (True or False indicating if conversion was sucessful, meas_file name written)


    Info
    ----------
    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 ltauxe@ucsd.edu for help.

   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