Beispiel #1
0
def convert(**kwargs):
    version_num = pmag.get_version()

    user = kwargs.get('user', '')
    dir_path = kwargs.get('dir_path', '.')
    input_dir_path = kwargs.get('input_dir_path', dir_path)
    output_dir_path = dir_path
    meas_file = kwargs.get('meas_file', 'measurements.txt')
    spec_file = kwargs.get('spec_file', 'specimens.txt') # specimen outfile
    samp_file = kwargs.get('samp_file', 'samples.txt')
    site_file = kwargs.get('site_file', 'sites.txt') # site outfile
    loc_file = kwargs.get('loc_file', 'locations.txt') # Loc outfile
    mag_file = kwargs.get('mag_file', '') #required
    location = kwargs.get('location', 'unknown')
    site = kwargs.get('site', '')
    samp_con = kwargs.get('samp_con', '1')
    specnum = int(kwargs.get('specnum', 0))
    timezone = kwargs.get('timestamp', 'US/Pacific')
    append = kwargs.get('append', False)
    noave = kwargs.get('noave', False) # default False means DO average
    meth_code = kwargs.get('meth_code', "LP-NO")
    volume = float(kwargs.get('volume', 0))
    if not volume: volume = 0.025**3 #default volume is a 2.5 cm cube, translated to meters cubed
    else: volume *= 1e-6 #convert cm^3 to m^3

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

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


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

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

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

    data['dir_dec'] = direction[0]
    data['dir_inc'] = direction[1]
    data['magn_moment'] = old_div(direction[2], 1000)  # the data are in EMU - this converts to Am^2
    data['magn_volume'] = old_div((old_div(direction[2], 1000)), volume) # EMU  - data converted to A/m

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

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

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

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

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

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

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

    return True, meas_file
Beispiel #2
0
def convert(**kwargs):

    # initialize some stuff
    demag = "N"
    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
    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')
    mag_file = kwargs.get('mag_file', '')
    site = kwargs.get('site', 'unknown')
    expedition = kwargs.get('expedition', 'unknown')
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')
    noave = kwargs.get('noave', False)  # default means DO average
    meth_code = kwargs.get('meth_code', "LP-NO")
    volume = kwargs.get('volume', 2.5**
                        3) * 1e-6  #default volume is a 2.5cm cube

    meth_code = meth_code + ":FS-C-DRILL-IODP:SP-SS-C:SO-V"
    meth_code = meth_code.strip(":")
    if not mag_file:
        print("-W- You must provide an IODP_jr6 format file")
        return False, "You must provide an IODP_jr6 format file"

    mag_file = os.path.join(input_dir_path, mag_file)

    # validate variables
    if not os.path.isfile(mag_file):
        print(
            'The input file you provided: {} does not exist.\nMake sure you have specified the correct filename AND correct input directory name.'
            .format(mag_file))
        return False, 'The input file you provided: {} does not exist.\nMake sure you have specified the correct filename AND correct input directory name.'.format(
            mag_file)

    # parse data
    temp = os.path.join(output_dir_path, 'temp.txt')
    fix_separation(mag_file, temp)
    infile = open(temp, 'r')
    lines = infile.readlines()
    infile.close()
    try:
        os.remove(temp)
    except OSError:
        print("problem with temp file")
    citations = "This Study"
    MeasRecs, SpecRecs, SampRecs, SiteRecs, LocRecs = [], [], [], [], []
    for line in lines:
        MeasRec, SpecRec, SampRec, SiteRec, LocRec = {}, {}, {}, {}, {}
        line = line.split()
        spec_text_id = line[0]
        specimen = spec_text_id
        for dem in ['-', '_']:
            if dem in spec_text_id:
                sample = dem.join(spec_text_id.split(dem)[:-1])
                break
        location = expedition + site

        if specimen != "" and specimen not in [
                x['specimen'] if 'specimen' in list(x.keys()) else ""
                for x in SpecRecs
        ]:
            SpecRec['specimen'] = specimen
            SpecRec['sample'] = sample
            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'] = line[6]
            SampRec['dip'] = line[7]
            SampRec['bed_dip_direction'] = line[8]
            SampRec['bed_dip'] = line[9]
            SampRec['method_codes'] = meth_code
            SampRecs.append(SampRec)
        if site != "" and site not in [
                x['site'] if 'site' in list(x.keys()) else "" for x in SiteRecs
        ]:
            SiteRec['site'] = site
            SiteRec['location'] = location
            SiteRec['citations'] = 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
        MeasRec["citations"] = citations
        MeasRec['software_packages'] = version_num
        MeasRec["treat_temp"] = '%8.3e' % (273)  # room temp in kelvin
        MeasRec["meas_temp"] = '%8.3e' % (273)  # room temp in kelvin
        MeasRec["quality"] = 'g'
        MeasRec["standard"] = 'u'
        MeasRec["treat_step_num"] = '1'
        MeasRec["treat_ac_field"] = '0'

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

        MeasRec["magn_moment"] = str(moment)
        MeasRec["magn_volume"] = str(
            magn_volume)  #str(direction[2] * (10.0 ** expon))
        MeasRec["dir_dec"] = '%7.1f' % (direction[0])
        MeasRec["dir_inc"] = '%7.1f' % (direction[1])

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

        MeasRec['method_codes'] = meas_type
        MeasRecs.append(MeasRec.copy())

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

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

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

    return (True, meas_file)
Beispiel #3
0
def convert(**kwargs):
    # initialize some stuff
    dec=[315,225,180,135,45,90,270,270,270,90,180,180,0,0,0]
    inc=[0,0,0,0,0,-45,-45,0,45,45,45,-45,-90,-45,45]
    tdec=[0,90,0,180,270,0,0,90,0]
    tinc=[0,0,90,0,0,-90,0,0,90]
    demag="N"
    trm=0
    irm=0

    user = kwargs.get('user', '')
    dir_path = kwargs.get('dir_path', '.')
    input_dir_path = kwargs.get('input_dir_path', dir_path)
    output_dir_path = dir_path
    meas_file = kwargs.get('meas_file', 'measurements.txt')
    spec_file = kwargs.get('spec_file', 'specimens.txt') # specimen outfile
    samp_file = kwargs.get('samp_file', 'samples.txt')
    site_file = kwargs.get('site_file', 'sites.txt') # site outfile
    loc_file = kwargs.get('loc_file', 'locations.txt') # site outfile
    magfile = kwargs.get('magfile', '')
    labfield = int(kwargs.get('labfield', 0)) *1e-6
    phi = int(kwargs.get('phi', 0))
    theta = int(kwargs.get('theta', 0))
    peakfield = int(kwargs.get('peakfield', 0))*1e-3
    specnum = int(kwargs.get('specnum', 0))
    location = kwargs.get('location', 'unknown')
    noave = kwargs.get('noave', False) # 0 means "do average", is default
    samp_con = kwargs.get('samp_con', '1')
    codelist = kwargs.get('codelist', '')
    coil = kwargs.get('coil', '')
    arm_labfield = kwargs.get('arm_labfield', 50e-6)
    trm_peakT = kwargs.get('trm_peakT', 600+273)
    mv = kwargs.get('mv', 'v')

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

    if specnum!=0:specnum=-specnum

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

    codes=codelist.split(':')
    if "AF" in codes:
        demag='AF'
        if not labfield: methcode="LT-AF-Z"
        if labfield: methcode="LT-AF-I"
    if "T" in codes:
        demag="T"
        if not labfield: methcode="LT-T-Z"
        if labfield: methcode="LT-T-I"
    if "I" in codes:
        methcode="LP-IRM"
        irmunits="mT"
    if "S" in codes:
        demag="S"
        methcode="LP-PI-TRM:LP-PI-ALT-AFARM"
        trm_labfield=labfield
        # should use arm_labfield and trm_peakT as well, but these values are currently never asked for
    if "G" in codes: methcode="LT-AF-G"
    if "D" in codes: methcode="LT-AF-D"
    if "TRM" in codes:
        demag="T"
        trm=1

    if coil:
        methcode="LP-IRM"
        irmunits="V"
        if coil not in ["1","2","3"]:
            print('not a valid coil specification')
            return False, 'not a valid coil specification'

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

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

        #fill measurements
        MeasRec["treat_temp"]='%8.3e' % (273) # room temp in kelvin
        MeasRec["meas_temp"]='%8.3e' % (273) # room temp in kelvin
        MeasRec["treat_ac_field"]='0'
        MeasRec["treat_dc_field"]='0'
        MeasRec["treat_dc_field_phi"]='0'
        MeasRec["treat_dc_field_theta"]='0'
        meas_type="LT-NO"
        MeasRec["quality"]='g'
        MeasRec["standard"]='u'
        MeasRec["treat_step_num"]='1'
        MeasRec["specimen"]=specimen
#        if mv=='v': MeasRec["susc_chi_volume"]=susc_chi_volume
#        else: MeasRec["susc_chi_mass"]=susc_chi_mass
        MeasRec["dir_csd"]=rec[3]
        MeasRec["magn_moment"]='%10.3e'% (float(rec[4])*1e-7)
        MeasRec["dir_dec"]=rec[5]
        MeasRec["dir_inc"]=rec[6]
        MeasRec["citations"]="This study"
        if demag=="AF":
            if methcode != "LP-AN-ARM":
                MeasRec["treat_ac_field"]='%8.3e' %(float(rec[1])*1e-3) # peak field in tesla
                meas_type="LT-AF-Z"
                MeasRec["treat_dc_field"]='0'
            else: # AARM experiment
                if treat[1][0]=='0':
                    meas_type="LT-AF-Z"
                    MeasRec["treat_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
                else:
                    meas_type="LT-AF-I"
                    ipos=int(treat[0])-1
                    MeasRec["treat_dc_field_phi"]='%7.1f' %(dec[ipos])
                    MeasRec["treat_dc_field_theta"]='%7.1f'% (inc[ipos])
                    MeasRec["treat_dc_field"]='%8.3e'%(labfield)
                    MeasRec["treat_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
        elif demag=="T":
            if rec[1][0]==".":rec[1]="0"+rec[1]
            treat=rec[1].split('.')
            if len(treat)==1:treat.append('0')
            MeasRec["treat_temp"]='%8.3e' % (float(rec[1])+273.) # temp in kelvin
            meas_type="LT-T-Z"
            MeasRec["treat_temp"]='%8.3e' % (float(treat[0])+273.) # temp in kelvin
            if trm==0:  # demag=T and not trmaq
                if treat[1][0]=='0':
                    meas_type="LT-T-Z"
                else:
                    MeasRec["treat_dc_field"]='%8.3e' % (labfield) # labfield in tesla (convert from microT)
                    MeasRec["treat_dc_field_phi"]='%7.1f' % (phi) # labfield phi
                    MeasRec["treat_dc_field_theta"]='%7.1f' % (theta) # labfield theta
                    if treat[1][0]=='1':meas_type="LT-T-I" # in-field thermal step
                    if treat[1][0]=='2':
                        meas_type="LT-PTRM-I" # pTRM check
                        pTRM=1
                    if treat[1][0]=='3':
                        MeasRec["treat_dc_field"]='0'  # this is a zero field step
                        meas_type="LT-PTRM-MD" # pTRM tail check
            else:
                meas_type="LT-T-I" # trm acquisition experiment
        MeasRec['method_codes']=meas_type
        MeasRecs.append(MeasRec)

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

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

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

    return True, meas_file
Beispiel #4
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
Beispiel #5
0
def convert(**kwargs):
    """

    """

    #get kwargs
    dir_path = kwargs.get('dir_path', '.')
    input_dir_path = kwargs.get('input_dir_path', dir_path)
    output_dir_path = dir_path
    meas_file = kwargs.get('meas_file', 'measurements.txt')
    mag_file = kwargs.get('mag_file')
    spec_file = kwargs.get('spec_file', 'specimens.txt')
    samp_file = kwargs.get('samp_file', 'samples.txt')
    site_file = kwargs.get('site_file', 'sites.txt')
    loc_file = kwargs.get('loc_file', 'locations.txt')
    lat = kwargs.get('lat', 0)
    lon = kwargs.get('lon', 0)
    specnum = int(kwargs.get('specnum', 0))
    samp_con = kwargs.get('samp_con', '1')
    location = kwargs.get('location', 'unknown')
    noave = kwargs.get('noave', 0) # default (0) means DO average
    meth_code = kwargs.get('meth_code', "LP-NO")
    version_num=pmag.get_version()

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

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

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

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

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

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

    SpecRecs.append(SpecRec)
    SampRecs.append(SampRec)
    SiteRecs.append(SiteRec)
    LocRecs.append(LocRec)
    for k in range(3,len(data)): # read in data
      line=data[k]
      rec=line.split()
      if len(rec)>1: # skip blank lines at bottom
        MeasRec={}
        MeasRec['description']='Date: '+date+' '+time
        MeasRec["citations"]="This study"
        MeasRec['software_packages']=version_num
        MeasRec["treat_temp"]='%8.3e' % (273) # room temp in kelvin
        MeasRec["meas_temp"]='%8.3e' % (273) # room temp in kelvin
        MeasRec["quality"]='g'
        MeasRec["standard"]='u'
        MeasRec["treat_step_num"]='1'
        MeasRec["specimen"]=specimen
        if rec[0]=='NRM':
            meas_type="LT-NO"
        elif rec[0][0]=='M' or rec[0][0]=='H':
            meas_type="LT-AF-Z"
        elif rec[0][0]=='T':
            meas_type="LT-T-Z"
        else:
            print("measurement type unknown")
            return False, "measurement type unknown"
        X=[float(rec[1]),float(rec[2]),float(rec[3])]
        Vec=pmag.cart2dir(X)
        MeasRec["magn_moment"]='%10.3e'% (Vec[2]) # Am^2
        MeasRec["magn_volume"]=rec[4] # A/m
        MeasRec["dir_dec"]='%7.1f'%(Vec[0])
        MeasRec["dir_inc"]='%7.1f'%(Vec[1])
        MeasRec["treat_ac_field"]='0'
        if meas_type!='LT-NO':
            treat=float(rec[0][1:])
        else:
            treat=0
        if meas_type=="LT-AF-Z":
            MeasRec["treat_ac_field"]='%8.3e' %(treat*1e-3) # convert from mT to tesla
        elif meas_type=="LT-T-Z":
            MeasRec["treat_temp"]='%8.3e' % (treat+273.) # temp in kelvin
        MeasRec['method_codes']=meas_type
        MeasRecs.append(MeasRec)

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

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

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

    return True, meas_file
Beispiel #6
0
def convert(**kwargs):
    #
    # initialize variables
    #
    bed_dip,bed_dip_dir="",""
    sclass,lithology,_type="","",""
    DecCorr=0.
    months=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']

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

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

    if ID:
        input_dir_path = ID
    else:
        input_dir_path = dir_path

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

    if not mag_file:
        print("mag file is required input")
        return False, "mag file is required input"
    output_dir_path = dir_path
    mag_file = os.path.join(input_dir_path, mag_file)

    samplist=[]
    try:
        SampRecs,file_type=pmag.magic_read(samp_file)
    except:
        SampRecs=[]
    MeasRecs,SpecRecs,SiteRecs,LocRecs=[],[],[],[]
    try:
        f=open(mag_file,'br')
        input=str(f.read()).strip("b '")
        f.close()
    except Exception as ex:
        print('ex', ex)
        print("bad mag file")
        return False, "bad mag file"
    firstline,date=1,""
    d=input.split('\\xcd')
    for line in d:
        rec=line.split('\\x00')
        if firstline==1:
            firstline=0
            spec,vol="",1
            el=51
            while line[el:el+1]!="\\": spec=spec+line[el];el+=1
            # check for bad sample name
            test=spec.split('.')
            date=""
            if len(test)>1:
                spec=test[0]
                kk=24
                while line[kk]!='\\x01' and line[kk]!='\\x00':
                    kk+=1
                vcc=line[24:kk]
                el=10
                while rec[el].strip()!='':el+=1
                date,comments=rec[el+7],[]
            else:
                el=9
                while rec[el]!='\\x01':el+=1
                vcc,date,comments=rec[el-3],rec[el+7],[]
            specname=spec.lower()
            print('importing ',specname)
            el+=8
            while rec[el].isdigit()==False:
                comments.append(rec[el])
                el+=1
            while rec[el]=="":el+=1
            az=float(rec[el])
            el+=1
            while rec[el]=="":el+=1
            pl=float(rec[el])
            el+=1
            while rec[el]=="":el+=1
            bed_dip_dir=float(rec[el])
            el+=1
            while rec[el]=="":el+=1
            bed_dip=float(rec[el])
            el+=1
            while rec[el]=="":el+=1
            if rec[el]=='\\x01':
                bed_dip=180.-bed_dip
                el+=1
                while rec[el]=="":el+=1
            fold_az=float(rec[el])
            el+=1
            while rec[el]=="":el+=1
            fold_pl=rec[el]
            el+=1
            while rec[el]=="":el+=1
            if rec[el]!="" and rec[el]!='\\x02' and rec[el]!='\\x01':
                deccorr=float(rec[el])
                az+=deccorr
                bed_dip_dir+=deccorr
                fold_az+=deccorr
                if bed_dip_dir>=360:bed_dip_dir=bed_dip_dir-360.
                if az>=360.:az=az-360.
                if fold_az>=360.:fold_az=fold_az-360.
            else:
                deccorr=0
            if specnum!=0:
                sample=specname[:specnum]
            else:
                sample=specname

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

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

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

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

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

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

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

    con.write_table_to_file('specimens', custom_name=spec_file)
    con.write_table_to_file('samples', custom_name=samp_file)
    con.write_table_to_file('sites', custom_name=site_file)
    con.write_table_to_file('locations', custom_name=loc_file)
    con.write_table_to_file('measurements', custom_name=meas_file)
    return True, meas_file
Beispiel #7
0
def convert(**kwargs):
    """
    EXAMPLE DOCSTRING for function (you would usually put the discription here)

    Parameters
    -----------
    user : colon delimited list of analysts (default : "")
    magfile : input magnetometer file (required)

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

    #get parameters from kwargs.get(parameter_name, default_value)
    user = kwargs.get('user', '')
    magfile = kwargs.get('magfile')

    #do any extra formating you need to variables here

    #open magfile to start reading data
    try:
        infile=open(magfile,'r')
    except Exception as ex:
        print(("bad file path: ", magfile))
        return False, "bad file path"

    #Depending on the dataset you may need to read in all data here put it in a list of dictionaries or something here. If you do just replace the "for line in infile.readlines():" bellow with "for d in data:" where data is the structure you put your data into

    #define the lists that hold each line of data for their respective tables
    SpecRecs,SampRecs,SiteRecs,LocRecs,MeasRecs=[],[],[],[],[]

    #itterate over the contence of the file
    for line in infile.readlines():
        MeasRec,SpecRec,SampRec,SiteRec,LocRec={},{},{},{},{}

        #extract data from line and put it in variables

        #fill this line of the Specimen table using above variables
        if specimen!="" and specimen not in [x['specimen'] if 'specimen' in list(x.keys()) else "" for x in SpecRecs]:
            SpecRec['analysts']=user
            SpecRecs.append(SpecRec)
        #fill this line of the Sample table using above variables
        if sample!="" and sample not in [x['sample'] if 'sample' in list(x.keys()) else "" for x in SampRecs]:
            SampRec['analysts']=user
            SampRecs.append(SampRec)
        #fill this line of the Site table using above variables
        if site!="" and site not in [x['site'] if 'site' in list(x.keys()) else "" for x in SiteRecs]:
            SiteRec['analysts']=user
            SiteRecs.append(SiteRec)
        #fill this line of the Location table using above variables
        if location!="" and location not in [x['location'] if 'location' in list(x.keys()) else "" for x in LocRecs]:
            LocRec['analysts']=user
            LocRecs.append(LocRec)

        #Fill this line of Meas Table using data in line
        MeasRec['analysts']=user
        MeasRecs.append(MeasRec)

    #close your file object so Python3 doesn't throw an annoying warning
    infile.close()

    #open a Contribution object
    con = nb.Contribution(output_dir_path,read_tables=[])

    #Create Magic Tables and add to a contribution
    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts=pmag.measurements_methods3(MeasRecs,noave) #figures out method codes for measuremet data
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    #write to file
    con.write_table_to_file('specimens', custom_name=spec_file)
    con.write_table_to_file('samples', custom_name=samp_file)
    con.write_table_to_file('sites', custom_name=site_file)
    con.write_table_to_file('locations', custom_name=loc_file)
    meas_file = con.write_table_to_file('measurements', custom_name=meas_file)

    return True, meas_file
Beispiel #8
0
def convert(**kwargs):

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

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

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

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

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

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

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

    version_num = pmag.get_version()

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                MeasRecs.append(MeasRec)

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

            if demag == "T":

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return True, meas_file
Beispiel #9
0
def convert(**kwargs):

    # initialize some stuff
    demag="N"
    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
    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')
    mag_file = kwargs.get('mag_file', '')
    site = kwargs.get('site', 'unknown')
    expedition = kwargs.get('expedition', 'unknown')
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')
    noave = kwargs.get('noave', False) # default means DO average
    meth_code = kwargs.get('meth_code', "LP-NO")
    volume = kwargs.get('volume', 2.5**3)*1e-6#default volume is a 2.5cm cube

    meth_code=meth_code+":FS-C-DRILL-IODP:SP-SS-C:SO-V"
    meth_code=meth_code.strip(":")
    if not mag_file:
        print("-W- You must provide an IODP_jr6 format file")
        return False, "You must provide an IODP_jr6 format file"

    mag_file = os.path.join(input_dir_path, mag_file)

    # validate variables
    if not os.path.isfile(mag_file):
        print('The input file you provided: {} does not exist.\nMake sure you have specified the correct filename AND correct input directory name.'.format(mag_file))
        return False, 'The input file you provided: {} does not exist.\nMake sure you have specified the correct filename AND correct input directory name.'.format(mag_file)

    # parse data
    temp = os.path.join(output_dir_path, 'temp.txt')
    fix_separation(mag_file, temp)
    infile = open(temp, 'r')
    lines = infile.readlines()
    infile.close()
    try: os.remove(temp)
    except OSError: print("problem with temp file")
    citations="This Study"
    MeasRecs,SpecRecs,SampRecs,SiteRecs,LocRecs=[],[],[],[],[]
    for line in lines:
        MeasRec,SpecRec,SampRec,SiteRec,LocRec={},{},{},{},{}
        line = line.split()
        spec_text_id = line[0]
        specimen = spec_text_id
        for dem in ['-','_']:
            if dem in spec_text_id:
                sample=dem.join(spec_text_id.split(dem)[:-1]); break
        location = expedition + site

        if specimen!="" and specimen not in [x['specimen'] if 'specimen' in list(x.keys()) else "" for x in SpecRecs]:
            SpecRec['specimen'] = specimen
            SpecRec['sample'] = sample
            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']=line[6]
            SampRec['dip']=line[7]
            SampRec['bed_dip_direction']=line[8]
            SampRec['bed_dip']=line[9]
            SampRec['method_codes']=meth_code
            SampRecs.append(SampRec)
        if site!="" and site not in [x['site'] if 'site' in list(x.keys()) else "" for x in SiteRecs]:
            SiteRec['site'] = site
            SiteRec['location'] = location
            SiteRec['citations']= 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
        MeasRec["citations"]=citations
        MeasRec['software_packages']=version_num
        MeasRec["treat_temp"]='%8.3e' % (273) # room temp in kelvin
        MeasRec["meas_temp"]='%8.3e' % (273) # room temp in kelvin
        MeasRec["quality"]='g'
        MeasRec["standard"]='u'
        MeasRec["treat_step_num"]='1'
        MeasRec["treat_ac_field"]='0'

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

        MeasRec["magn_moment"]=str(moment)
        MeasRec["magn_volume"]=str(magn_volume)#str(direction[2] * (10.0 ** expon))
        MeasRec["dir_dec"]='%7.1f'%(direction[0])
        MeasRec["dir_inc"]='%7.1f'%(direction[1])

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

        MeasRec['method_codes']=meas_type
        MeasRecs.append(MeasRec.copy())

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

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

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

    return (True, meas_file)
Beispiel #10
0
def convert(**kwargs):
    """

    """

    #get kwargs
    dir_path = kwargs.get('dir_path', '.')
    input_dir_path = kwargs.get('input_dir_path', dir_path)
    output_dir_path = dir_path
    meas_file = kwargs.get('meas_file', 'measurements.txt')
    mag_file = kwargs.get('mag_file')
    spec_file = kwargs.get('spec_file', 'specimens.txt')
    samp_file = kwargs.get('samp_file', 'samples.txt')
    site_file = kwargs.get('site_file', 'sites.txt')
    loc_file = kwargs.get('loc_file', 'locations.txt')
    lat = kwargs.get('lat', 0)
    lon = kwargs.get('lon', 0)
    specnum = int(kwargs.get('specnum', 0))
    samp_con = kwargs.get('samp_con', '1')
    location = kwargs.get('location', 'unknown')
    noave = kwargs.get('noave', 0)  # default (0) means DO average
    meth_code = kwargs.get('meth_code', "LP-NO")
    version_num = pmag.get_version()

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

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

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

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

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

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

    SpecRecs.append(SpecRec)
    SampRecs.append(SampRec)
    SiteRecs.append(SiteRec)
    LocRecs.append(LocRec)
    for k in range(3, len(data)):  # read in data
        line = data[k]
        rec = line.split()
        if len(rec) > 1:  # skip blank lines at bottom
            MeasRec = {}
            MeasRec['description'] = 'Date: ' + date + ' ' + time
            MeasRec["citations"] = "This study"
            MeasRec['software_packages'] = version_num
            MeasRec["treat_temp"] = '%8.3e' % (273)  # room temp in kelvin
            MeasRec["meas_temp"] = '%8.3e' % (273)  # room temp in kelvin
            MeasRec["quality"] = 'g'
            MeasRec["standard"] = 'u'
            MeasRec["treat_step_num"] = '1'
            MeasRec["specimen"] = specimen
            if rec[0] == 'NRM':
                meas_type = "LT-NO"
            elif rec[0][0] == 'M' or rec[0][0] == 'H':
                meas_type = "LT-AF-Z"
            elif rec[0][0] == 'T':
                meas_type = "LT-T-Z"
            else:
                print("measurement type unknown")
                return False, "measurement type unknown"
            X = [float(rec[1]), float(rec[2]), float(rec[3])]
            Vec = pmag.cart2dir(X)
            MeasRec["magn_moment"] = '%10.3e' % (Vec[2])  # Am^2
            MeasRec["magn_volume"] = rec[4]  # A/m
            MeasRec["dir_dec"] = '%7.1f' % (Vec[0])
            MeasRec["dir_inc"] = '%7.1f' % (Vec[1])
            MeasRec["treat_ac_field"] = '0'
            if meas_type != 'LT-NO':
                treat = float(rec[0][1:])
            else:
                treat = 0
            if meas_type == "LT-AF-Z":
                MeasRec["treat_ac_field"] = '%8.3e' % (
                    treat * 1e-3)  # convert from mT to tesla
            elif meas_type == "LT-T-Z":
                MeasRec["treat_temp"] = '%8.3e' % (treat + 273.
                                                   )  # temp in kelvin
            MeasRec['method_codes'] = meas_type
            MeasRecs.append(MeasRec)

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

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

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

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

    version_num = pmag.get_version()
    dir_path = kwargs.get('dir_path', '.')
    mag_file = kwargs.get('mag_file')
    input_dir_path = kwargs.get('input_dir_path', dir_path)
    if input_dir_path == dir_path:
        mag_file = pmag.resolve_file_name(mag_file, dir_path)
        input_dir_path = os.path.split(mag_file)[0]
    output_dir_path = dir_path
    user = kwargs.get('user', '')
    meas_file = kwargs.get('meas_file', 'measurements.txt')  # outfile
    spec_file = kwargs.get('spec_file', 'specimens.txt')  # specimen outfile
    samp_file = kwargs.get('samp_file', 'samples.txt')  # sample outfile
    site_file = kwargs.get('site_file', 'sites.txt')  # site outfile
    loc_file = kwargs.get('loc_file', 'locations.txt')  # location outfile
    specnum = kwargs.get('specnum', 1)
    samp_con = kwargs.get('samp_con', '1')
    location = kwargs.get('location', 'unknown')
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')
    noave = kwargs.get('noave', 0)  # default (0) means DO average
    meth_code = kwargs.get('meth_code', "LP-NO")
    volume = float(kwargs.get('volume', 2.5)) * 1e-6
    timezone = kwargs.get('timestamp', 'UTC')

    # format variables
    mag_file = os.path.join(input_dir_path, mag_file)
    if specnum != 0: specnum = -int(specnum)
    if samp_con.startswith("4"):
        if "-" not in samp_con:
            print("option [4] must be in form 4-Z where Z is an integer")
            return False, "naming convention option [4] must be in form 4-Z where Z is an integer"
        else:
            Z = samp_con.split("-")[1]
            samp_con = "4"
    elif samp_con.startswith("7"):
        if "-" not in samp_con:
            print("option [7] must be in form 7-Z where Z is an integer")
            return False, "naming convention option [7] must be in form 7-Z where Z is an integer"
        else:
            Z = samp_con.split("-")[1]
            samp_con = "7"
    else:
        Z = 1

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

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

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

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

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

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

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

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

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

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

        #end of data while loop

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

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

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

    return True, meas_file
Beispiel #12
0
def convert(**kwargs):

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

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

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

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

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

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

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

    version_num=pmag.get_version()

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                MeasRecs.append(MeasRec)

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


            if demag=="T":

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return True, meas_file
Beispiel #13
0
def convert(**kwargs):
    version_num = pmag.get_version()

    user = kwargs.get('user', '')
    dir_path = kwargs.get('dir_path', '.')
    input_dir_path = kwargs.get('input_dir_path', dir_path)
    output_dir_path = dir_path
    meas_file = kwargs.get('meas_file', 'measurements.txt')
    spec_file = kwargs.get('spec_file', 'specimens.txt')  # specimen outfile
    samp_file = kwargs.get('samp_file', 'samples.txt')
    site_file = kwargs.get('site_file', 'sites.txt')  # site outfile
    loc_file = kwargs.get('loc_file', 'locations.txt')  # Loc outfile
    mag_file = kwargs.get('mag_file')  #required
    location = kwargs.get('location', 'unknown')
    site = kwargs.get('site', '')
    samp_con = kwargs.get('samp_con', '1')
    specnum = int(kwargs.get('specnum', 0))
    timezone = kwargs.get('timestamp', 'US/Pacific')
    noave = kwargs.get('noave', False)  # default False means DO average
    meth_code = kwargs.get('meth_code', "LP-NO")
    volume = float(kwargs.get('volume', 0))
    if not volume:
        volume = 0.025**3  #default volume is a 2.5 cm cube, translated to meters cubed
    else:
        volume *= 1e-6  #convert cm^3 to m^3

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

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

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

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

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

    data['dir_dec'] = direction[0]
    data['dir_inc'] = direction[1]
    data['magn_moment'] = old_div(
        direction[2], 1000)  # the data are in EMU - this converts to Am^2
    data['magn_volume'] = old_div((old_div(direction[2], 1000)),
                                  volume)  # EMU  - data converted to A/m

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

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

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

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

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

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

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

    return True, meas_file
Beispiel #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
Beispiel #15
0
def convert(**kwargs):

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

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

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

    # parse data

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

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

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

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

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


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

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

    data.close()

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

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

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

    return True, meas_file
Beispiel #16
0
def convert(**kwargs):
    # initialize some stuff
    dec = [315, 225, 180, 135, 45, 90, 270, 270, 270, 90, 180, 180, 0, 0, 0]
    inc = [0, 0, 0, 0, 0, -45, -45, 0, 45, 45, 45, -45, -90, -45, 45]
    tdec = [0, 90, 0, 180, 270, 0, 0, 90, 0]
    tinc = [0, 0, 90, 0, 0, -90, 0, 0, 90]
    demag = "N"
    trm = 0
    irm = 0

    user = kwargs.get('user', '')
    dir_path = kwargs.get('dir_path', '.')
    input_dir_path = kwargs.get('input_dir_path', dir_path)
    output_dir_path = dir_path
    meas_file = kwargs.get('meas_file', 'measurements.txt')
    spec_file = kwargs.get('spec_file', 'specimens.txt')  # specimen outfile
    samp_file = kwargs.get('samp_file', 'samples.txt')
    site_file = kwargs.get('site_file', 'sites.txt')  # site outfile
    loc_file = kwargs.get('loc_file', 'locations.txt')  # site outfile
    magfile = kwargs.get('magfile', '')
    labfield = int(kwargs.get('labfield', 0)) * 1e-6
    phi = int(kwargs.get('phi', 0))
    theta = int(kwargs.get('theta', 0))
    peakfield = int(kwargs.get('peakfield', 0)) * 1e-3
    specnum = int(kwargs.get('specnum', 0))
    location = kwargs.get('location', 'unknown')
    noave = kwargs.get('noave', False)  # 0 means "do average", is default
    samp_con = kwargs.get('samp_con', '1')
    codelist = kwargs.get('codelist', '')
    coil = kwargs.get('coil', '')
    arm_labfield = kwargs.get('arm_labfield', 50e-6)
    trm_peakT = kwargs.get('trm_peakT', 600 + 273)
    mv = kwargs.get('mv', 'v')

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

    if specnum != 0: specnum = -specnum

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

    codes = codelist.split(':')
    if "AF" in codes:
        demag = 'AF'
        if not labfield: methcode = "LT-AF-Z"
        if labfield: methcode = "LT-AF-I"
    if "T" in codes:
        demag = "T"
        if not labfield: methcode = "LT-T-Z"
        if labfield: methcode = "LT-T-I"
    if "I" in codes:
        methcode = "LP-IRM"
        irmunits = "mT"
    if "S" in codes:
        demag = "S"
        methcode = "LP-PI-TRM:LP-PI-ALT-AFARM"
        trm_labfield = labfield
        # should use arm_labfield and trm_peakT as well, but these values are currently never asked for
    if "G" in codes: methcode = "LT-AF-G"
    if "D" in codes: methcode = "LT-AF-D"
    if "TRM" in codes:
        demag = "T"
        trm = 1

    if coil:
        methcode = "LP-IRM"
        irmunits = "V"
        if coil not in ["1", "2", "3"]:
            print('not a valid coil specification')
            return False, 'not a valid coil specification'

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

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

        #fill measurements
        MeasRec["treat_temp"] = '%8.3e' % (273)  # room temp in kelvin
        MeasRec["meas_temp"] = '%8.3e' % (273)  # room temp in kelvin
        MeasRec["treat_ac_field"] = '0'
        MeasRec["treat_dc_field"] = '0'
        MeasRec["treat_dc_field_phi"] = '0'
        MeasRec["treat_dc_field_theta"] = '0'
        meas_type = "LT-NO"
        MeasRec["quality"] = 'g'
        MeasRec["standard"] = 'u'
        MeasRec["treat_step_num"] = '1'
        MeasRec["specimen"] = specimen
        #        if mv=='v': MeasRec["susc_chi_volume"]=susc_chi_volume
        #        else: MeasRec["susc_chi_mass"]=susc_chi_mass
        MeasRec["dir_csd"] = rec[3]
        MeasRec["magn_moment"] = '%10.3e' % (float(rec[4]) * 1e-7)
        MeasRec["dir_dec"] = rec[5]
        MeasRec["dir_inc"] = rec[6]
        MeasRec["citations"] = "This study"
        if demag == "AF":
            if methcode != "LP-AN-ARM":
                MeasRec["treat_ac_field"] = '%8.3e' % (float(rec[1]) * 1e-3
                                                       )  # peak field in tesla
                meas_type = "LT-AF-Z"
                MeasRec["treat_dc_field"] = '0'
            else:  # AARM experiment
                if treat[1][0] == '0':
                    meas_type = "LT-AF-Z"
                    MeasRec["treat_ac_field"] = '%8.3e' % (
                        peakfield)  # peak field in tesla
                else:
                    meas_type = "LT-AF-I"
                    ipos = int(treat[0]) - 1
                    MeasRec["treat_dc_field_phi"] = '%7.1f' % (dec[ipos])
                    MeasRec["treat_dc_field_theta"] = '%7.1f' % (inc[ipos])
                    MeasRec["treat_dc_field"] = '%8.3e' % (labfield)
                    MeasRec["treat_ac_field"] = '%8.3e' % (
                        peakfield)  # peak field in tesla
        elif demag == "T":
            if rec[1][0] == ".": rec[1] = "0" + rec[1]
            treat = rec[1].split('.')
            if len(treat) == 1: treat.append('0')
            MeasRec["treat_temp"] = '%8.3e' % (float(rec[1]) + 273.
                                               )  # temp in kelvin
            meas_type = "LT-T-Z"
            MeasRec["treat_temp"] = '%8.3e' % (float(treat[0]) + 273.
                                               )  # temp in kelvin
            if trm == 0:  # demag=T and not trmaq
                if treat[1][0] == '0':
                    meas_type = "LT-T-Z"
                else:
                    MeasRec["treat_dc_field"] = '%8.3e' % (
                        labfield)  # labfield in tesla (convert from microT)
                    MeasRec["treat_dc_field_phi"] = '%7.1f' % (
                        phi)  # labfield phi
                    MeasRec["treat_dc_field_theta"] = '%7.1f' % (
                        theta)  # labfield theta
                    if treat[1][0] == '1':
                        meas_type = "LT-T-I"  # in-field thermal step
                    if treat[1][0] == '2':
                        meas_type = "LT-PTRM-I"  # pTRM check
                        pTRM = 1
                    if treat[1][0] == '3':
                        MeasRec[
                            "treat_dc_field"] = '0'  # this is a zero field step
                        meas_type = "LT-PTRM-MD"  # pTRM tail check
            else:
                meas_type = "LT-T-I"  # trm acquisition experiment
        MeasRec['method_codes'] = meas_type
        MeasRecs.append(MeasRec)

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

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

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

    return True, meas_file
Beispiel #17
0
def main(**kwargs):

    version_num = pmag.get_version()
    dir_path = kwargs.get('dir_path', '.')
    input_dir_path = kwargs.get('input_dir_path', dir_path)
    output_dir_path = dir_path
    user = kwargs.get('user', '')
    meas_file = kwargs.get('meas_file', 'measurements.txt')  # outfile
    spec_file = kwargs.get('spec_file', 'specimens.txt')  # specimen outfile
    samp_file = kwargs.get('samp_file', 'samples.txt')  # sample outfile
    site_file = kwargs.get('site_file', 'sites.txt')  # site outfile
    loc_file = kwargs.get('loc_file', 'locations.txt')  # location outfile
    mag_file = kwargs.get('mag_file')
    specnum = kwargs.get('specnum', 1)
    samp_con = kwargs.get('samp_con', '1')
    location = kwargs.get('location', 'unknown')
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')
    noave = kwargs.get('noave', 0)  # default (0) means DO average
    meth_code = kwargs.get('meth_code', "LP-NO")
    volume = float(kwargs.get('volume', 2.5)) * 1e-6
    JR = kwargs.get('JR', False)
    if JR:
        if meth_code == "LP-NO":
            meth_code = ""
        meth_code = meth_code + ":FS-C-DRILL-IODP:SP-SS-C:SO-V"
        meth_code = meth_code.strip(":")
        samp_con = '5'

    # format variables
    tmp_file = mag_file.split(os.extsep)[0] + os.extsep + 'tmp'
    mag_file = os.path.join(input_dir_path, mag_file)
    if specnum != 0: specnum = -int(specnum)
    if samp_con.startswith("4"):
        if "-" not in samp_con:
            print("option [4] must be in form 4-Z where Z is an integer")
            return False, "naming convention option [4] must be in form 4-Z where Z is an integer"
        else:
            Z = samp_con.split("-")[1]
            samp_con = "4"
    elif samp_con.startswith("7"):
        if "-" not in samp_con:
            print("option [7] must be in form 7-Z where Z is an integer")
            return False, "naming convention option [7] must be in form 7-Z where Z is an integer"
        else:
            Z = samp_con.split("-")[1]
            samp_con = "7"
    else:
        Z = 1

    # parse data
    # fix .jr6 file so that there are spaces between all the columns.
    pre_data = open(mag_file, 'r')
    tmp_data = open(tmp_file, 'w')
    if samp_con != '2': fixed_data = pre_data.read().replace('-', ' -')
    else:
        fixed_data = ""
        for line in pre_data.readlines():
            entries = line.split()
            if len(entries) < 2: continue
            fixed_line = entries[0] + ' ' + reduce(
                lambda x, y: x + ' ' + y,
                [x.replace('-', ' -') for x in entries[1:]])
            fixed_data += fixed_line + os.linesep
    tmp_data.write(fixed_data)
    tmp_data.close()
    pre_data.close()

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

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

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

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

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

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

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

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

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

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

    # parse data

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

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

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

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

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

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

    data.close()

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

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

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

    return True, meas_file
Beispiel #19
0
def convert(**kwargs):

    # unpack keyword args
    user = kwargs.get('user', '')
    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')
    magfile = kwargs.get('magfile', '')
    labfield = float(kwargs.get('labfield', 0))*1e-6
    labfield_phi = float(kwargs.get('labfield_phi', 0))
    labfield_theta = float(kwargs.get('labfield_theta', 0))
    experiment = kwargs.get('experiment', '')
    cooling_times_list = kwargs.get('cooling_times_list', [])
    sample_nc = kwargs.get('sample_nc', [1, 0])
    site_nc = kwargs.get('site_nc', [1, 0])
    location = kwargs.get('location', 'unknown')
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')
    noave = kwargs.get('noave', False) # False is default, means do average
    WD = kwargs.get('WD', '.')
    output_dir_path=WD

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

    if not experiment:
        print("-exp is required option. Please provide experiment type of: Demag, PI, ATRM n (n of positions), CR (see below for format), NLT")
        print(__doc__)
        return False, "-exp is required option"

    if experiment=='ATRM':
        if command_line:
            ind=sys.argv.index("ATRM")
            atrm_n_pos=int(sys.argv[ind+1])
        else:
            atrm_n_pos = 6

    if experiment=='AARM':
        if command_line:
            ind=sys.argv.index("AARM")
            aarm_n_pos=int(sys.argv[ind+1])
        else:
            aarm_n_pos = 6

    if  experiment=='CR':
        if command_line:
            ind=sys.argv.index("CR")
            cooling_times=sys.argv[ind+1]
            cooling_times_list=cooling_times.split(',')
        # if not command line, cooling_times_list is already set

    #--------------------------------------
    # read data from generic file
    #--------------------------------------

    mag_data=read_generic_file(magfile,not noave)

    #--------------------------------------
    # for each specimen get the data, and translate it to MagIC format
    #--------------------------------------

    MeasRecs,SpecRecs,SampRecs,SiteRecs,LocRecs=[],[],[],[],[]
    specimens_list=sorted(mag_data.keys())
    for specimen in specimens_list:
        measurement_running_number=0
        this_specimen_treatments=[] # a list of all treatments
        MeasRecs_this_specimen=[]
        LP_this_specimen=[] # a list of all lab protocols
        IZ,ZI=0,0 # counter for IZ and ZI steps

        for meas_line in mag_data[specimen]:

            #------------------
            # trivial MeasRec data
            #------------------

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

            specimen=meas_line['specimen']
            sample=get_upper_level_name(specimen,sample_nc)
            site=get_upper_level_name(sample,site_nc)
            sample_method_codes=""
            azimuth,dip,DipDir,Dip="","","",""

            MeasRec['citations']="This study"
            MeasRec["specimen"]=specimen
            MeasRec['analysts']=user
            MeasRec["instrument_codes"]=""
            MeasRec["quality"]='g'
            MeasRec["treat_step_num"]="%i"%measurement_running_number
            MeasRec["magn_moment"]='%10.3e'%(float(meas_line["moment"])*1e-3) # in Am^2
            MeasRec["meas_temp"]='273.' # room temp in kelvin

            #------------------
            #  decode treatments from treatment column in the generic file
            #------------------

            treatment=[]
            treatment_code=str(meas_line['treatment']).split(".")
            treatment.append(float(treatment_code[0]))
            if len(treatment_code)==1:
                treatment.append(0)
            else:
                treatment.append(float(treatment_code[1]))

            #------------------
            #  lab field direction
            #------------------

            if experiment in ['PI','NLT','CR']:

                if float(treatment[1])==0:
                    MeasRec["treat_dc_field"]="0"
                    MeasRec["treat_dc_field_phi"]="0"
                    MeasRec["treat_dc_field_theta"]="0"
                elif not labfield:
                    print("-W- WARNING: labfield (-dc) is a required argument for this experiment type")
                    return False, "labfield (-dc) is a required argument for this experiment type"

                else:
                    MeasRec["treat_dc_field"]='%8.3e'%(float(labfield))
                    MeasRec["treat_dc_field_phi"]="%.2f"%(float(labfield_phi))
                    MeasRec["treat_dc_field_theta"]="%.2f"%(float(labfield_theta))
            else:
                MeasRec["treat_dc_field"]=""
                MeasRec["treat_dc_field_phi"]=""
                MeasRec["treat_dc_field_theta"]=""

            #------------------
            # treatment temperature/peak field
            #------------------

            if experiment == 'Demag':
                if meas_line['treatment_type']=='A':
                    MeasRec['treat_temp']="273."
                    MeasRec["treat_ac_field"]="%.3e"%(treatment[0]*1e-3)
                elif meas_line['treatment_type']=='N':
                    MeasRec['treat_temp']="273."
                    MeasRec["treat_ac_field"]=""
                else:
                    MeasRec['treat_temp']="%.2f"%(treatment[0]+273.)
                    MeasRec["treat_ac_field"]=""
            else:
                    MeasRec['treat_temp']="%.2f"%(treatment[0]+273.)
                    MeasRec["treat_ac_field"]=""


            #---------------------
            # Lab treatment
            # Lab protocol
            #---------------------

            #---------------------
            # Lab treatment and lab protocoal for NRM:
            #---------------------

            if float(meas_line['treatment'])==0:
                LT="LT-NO"
                LP="" # will be filled later after finishing reading all measurements line

            #---------------------
            # Lab treatment and lab protocoal for paleointensity experiment
            #---------------------

            elif experiment =='PI':
                LP="LP-PI-TRM"
                if treatment[1]==0:
                    LT="LT-T-Z"
                elif  treatment[1]==1 or treatment[1]==10: # infield
                    LT="LT-T-I"
                elif treatment[1]==2 or treatment[1]==20:  # pTRM check
                    LT="LT-PTRM-I"
                    LP=LP+":"+"LP-PI-ALT-PTRM"
                elif treatment[1]==3 or treatment[1]==30: # Tail check
                    LT="LT-PTRM-MD"
                    LP=LP+":"+"LP-PI-BT-MD"
                elif treatment[1]==4 or treatment[1]==40: # Additivity check
                    LT="LT-PTRM-AC"
                    LP=LP+":"+"LP-PI-BT-MD"
                else:
                    print("-E- unknown measurement code specimen %s treatmemt %s"%(meas_line['specimen'],meas_line['treatment']))
                    MeasRec={}
                    continue
                # save all treatment in a list
                # we will use this later to distinguidh between ZI / IZ / and IZZI

                this_specimen_treatments.append(float(meas_line['treatment']))
                if LT=="LT-T-Z":
                    if float(treatment[0]+0.1) in this_specimen_treatments:
                        LP=LP+":"+"LP-PI-IZ"
                if LT=="LT-T-I":
                    if float(treatment[0]+0.0) in this_specimen_treatments:
                        LP=LP+":"+"LP-PI-ZI"
            #---------------------
            # Lab treatment and lab protocoal for demag experiment
            #---------------------

            elif "Demag" in experiment:
                if meas_line['treatment_type']=='A':
                    LT="LT-AF-Z"
                    LP="LP-DIR-AF"
                else:
                    LT="LT-T-Z"
                    LP="LP-DIR-T"

            #---------------------
            # Lab treatment and lab protocoal for ATRM experiment
            #---------------------

            elif experiment in ['ATRM','AARM']:

                if experiment=='ATRM':
                    LP="LP-AN-TRM"
                    n_pos=atrm_n_pos
                    if n_pos!=6:
                        print("the program does not support ATRM in %i position."%n_pos)
                        continue

                if experiment=='AARM':
                    LP="LP-AN-ARM"
                    n_pos=aarm_n_pos
                    if n_pos!=6:
                        print("the program does not support AARM in %i position."%n_pos)
                        continue

                if treatment[1]==0:
                    if experiment=='ATRM':
                        LT="LT-T-Z"
                        MeasRec['treat_temp']="%.2f"%(treatment[0]+273.)
                        MeasRec["treat_ac_field"]=""

                    else:
                        LT="LT-AF-Z"
                        MeasRec['treat_temp']="273."
                        MeasRec["treat_ac_field"]="%.3e"%(treatment[0]*1e-3)
                    MeasRec["treat_dc_field"]='0'
                    MeasRec["treat_dc_field_phi"]='0'
                    MeasRec["treat_dc_field_theta"]='0'
                else:
                    if experiment=='ATRM':
                        if float(treatment[1])==70 or float(treatment[1])==7: # alteration check as final measurement
                            LT="LT-PTRM-I"
                        else:
                            LT="LT-T-I"
                    else:
                        LT="LT-AF-I"
                    MeasRec["treat_dc_field"]='%8.3e'%(float(labfield))

                    # find the direction of the lab field in two ways:

                    # (1) using the treatment coding (XX.1=+x, XX.2=+y, XX.3=+z, XX.4=-x, XX.5=-y, XX.6=-z)
                    tdec=[0,90,0,180,270,0,0,90,0]
                    tinc=[0,0,90,0,0,-90,0,0,90]
                    if treatment[1] < 10:
                        ipos_code=int(treatment[1])-1
                    else:
                        ipos_code=int(old_div(treatment[1],10))-1

                    # (2) using the magnetization
                    if meas_line["dec_s"]!="":
                        DEC=float(meas_line["dec_s"])
                        INC=float(meas_line["inc_s"])
                    elif meas_line["dec_g"]!="":
                        DEC=float(meas_line["dec_g"])
                        INC=float(meas_line["inc_g"])
                    elif meas_line["dec_t"]!="":
                        DEC=float(meas_line["dec_t"])
                        INC=float(meas_line["inc_t"])
                    if DEC<0 and DEC>-359:
                        DEC=360.+DEC

                    if INC < 45 and INC > -45:
                        if DEC>315  or DEC<45: ipos_guess=0
                        if DEC>45 and DEC<135: ipos_guess=1
                        if DEC>135 and DEC<225: ipos_guess=3
                        if DEC>225 and DEC<315: ipos_guess=4
                    else:
                        if INC >45: ipos_guess=2
                        if INC <-45: ipos_guess=5
                    # prefer the guess over the code
                    ipos=ipos_guess
                    # check it
                    if treatment[1]!= 7 and treatment[1]!= 70:
                        if ipos_guess!=ipos_code:
                            print("-W- WARNING: check specimen %s step %s, anistropy measurements, coding does not match the direction of the lab field"%(specimen,meas_line['treatment']))
                    MeasRec["treat_dc_field_phi"]='%7.1f' %(tdec[ipos])
                    MeasRec["treat_dc_field_theta"]='%7.1f'% (tinc[ipos])

            #---------------------
            # Lab treatment and lab protocoal for cooling rate experiment
            #---------------------

            elif experiment == "CR":

                cooling_times_list
                LP="LP-CR-TRM"
                MeasRec["treat_temp"]='%8.3e' % (float(treatment[0])+273.) # temp in kelvin

                if treatment[1]==0:
                    LT="LT-T-Z"
                    MeasRec["treat_dc_field"]="0"
                    MeasRec["treat_dc_field_phi"]='0'
                    MeasRec["treat_dc_field_theta"]='0'
                else:
                    if treatment[1]==7: # alteration check as final measurement
                            LT="LT-PTRM-I"
                    else:
                            LT="LT-T-I"
                    MeasRec["treat_dc_field"]='%8.3e'%(labfield)
                    MeasRec["treat_dc_field_phi"]='%7.1f' % (labfield_phi) # labfield phi
                    MeasRec["treat_dc_field_theta"]='%7.1f' % (labfield_theta) # labfield theta

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


            #---------------------
            # Lab treatment and lab protocoal for NLT experiment
            #---------------------

            elif 'NLT' in experiment :
                print("Dont support yet NLT rate experiment file. Contact [email protected]")

            #---------------------
            # method_codes for this measurement only
            # LP will be fixed after all measurement lines are read
            #---------------------

            MeasRec["method_codes"]=LT+":"+LP

            #--------------------
            # deal with specimen orientation and different coordinate system
            #--------------------

            found_s,found_geo,found_tilt=False,False,False
            if "dec_s" in list(meas_line.keys()) and "inc_s" in list(meas_line.keys()):
                if meas_line["dec_s"]!="" and meas_line["inc_s"]!="":
                    found_s=True
                MeasRec["dir_dec"]=meas_line["dec_s"]
                MeasRec["dir_inc"]=meas_line["inc_s"]
            if "dec_g" in list(meas_line.keys()) and "inc_g" in list(meas_line.keys()):
                if meas_line["dec_g"]!="" and meas_line["inc_g"]!="":
                    found_geo=True
            if "dec_t" in list(meas_line.keys()) and "inc_t" in list(meas_line.keys()):
                if meas_line["dec_t"]!="" and meas_line["inc_t"]!="":
                    found_tilt=True

            #-----------------------------
            # specimen coordinates: no
            # geographic coordinates: yes
            #-----------------------------

            if found_geo and not found_s:
                MeasRec["dir_dec"]=meas_line["dec_g"]
                MeasRec["dir_inc"]=meas_line["inc_g"]
                azimuth="0"
                dip="0"

            #-----------------------------
            # specimen coordinates: no
            # geographic coordinates: no
            #-----------------------------
            if not found_geo and not found_s:
                print("-E- ERROR: sample %s does not have dec_s/inc_s or dec_g/inc_g. Ignore specimen %s "%(sample,specimen))
                break

            #-----------------------------
            # specimen coordinates: yes
            # geographic coordinates: yes
            #
            # commant: Ron, this need to be tested !!
            #-----------------------------
            if found_geo and found_s:
                cdec,cinc=float(meas_line["dec_s"]),float(meas_line["inc_s"])
                gdec,ginc=float(meas_line["dec_g"]),float(meas_line["inc_g"])
                az,pl=pmag.get_azpl(cdec,cinc,gdec,ginc)
                azimuth="%.1f"%az
                dip="%.1f"%pl

            #-----------------------------
            # specimen coordinates: yes
            # geographic coordinates: no
            #-----------------------------
            if not found_geo and found_s and "Demag" in experiment:
                print("-W- WARNING: missing dip or azimuth for sample %s"%sample)

            #-----------------------------
            # tilt-corrected coordinates: yes
            # geographic coordinates: no
            #-----------------------------
            if found_tilt and not found_geo:
                    print("-E- ERROR: missing geographic data for sample %s. Ignoring tilt-corrected data "%sample)

            #-----------------------------
            # tilt-corrected coordinates: yes
            # geographic coordinates: yes
            #-----------------------------
            if found_tilt and found_geo:
                dec_geo,inc_geo=float(meas_line["dec_g"]),float(meas_line["inc_g"])
                dec_tilt,inc_tilt=float(meas_line["dec_t"]),float(meas_line["inc_t"])
                if dec_geo==dec_tilt and inc_geo==inc_tilt:
                    DipDir,Dip=0.,0.
                else:
                    DipDir,Dip=pmag.get_tilt(dec_geo,inc_geo,dec_tilt,inc_tilt)

            #-----------------------------
            # samples method codes
            # geographic coordinates: no
            #-----------------------------
            if found_tilt or found_geo:
                sample_method_codes="SO-NO"


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

            MeasRecs_this_specimen.append(MeasRec)
            measurement_running_number+=1
            #-------

        #-------
        # after reading all the measurements lines for this specimen
        # 1) add experiments
        # 2) fix method_codes with the correct lab protocol
        #-------
        LP_this_specimen=[]
        for MeasRec in MeasRecs_this_specimen:
            method_codes=MeasRec["method_codes"].split(":")
            for code in method_codes:
                if "LP" in code and code not in LP_this_specimen:
                    LP_this_specimen.append(code)
        # check IZ/ZI/IZZI
        if "LP-PI-ZI" in   LP_this_specimen and "LP-PI-IZ" in   LP_this_specimen:
            LP_this_specimen.remove("LP-PI-ZI")
            LP_this_specimen.remove("LP-PI-IZ")
            LP_this_specimen.append("LP-PI-BT-IZZI")

        # add the right LP codes and fix experiment name
        for MeasRec in MeasRecs_this_specimen:
            MeasRec["experiments"]=MeasRec["specimen"]+":"+":".join(LP_this_specimen)
            method_codes=MeasRec["method_codes"].split(":")
            LT=""
            for code in method_codes:
                if code[:3]=="LT-":
                    LT=code;
                    break
            MeasRec["method_codes"]=LT+":"+":".join(LP_this_specimen)
            MeasRecs.append(MeasRec)

    #--
    # write tables to file
    #--

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

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

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

    return True, meas_file
Beispiel #20
0
def convert(**kwargs):
    # initialize defaults
    version_num=pmag.get_version()
    citations="This study"
    dir_path,demag='.','NRM'
    depth_method='a'

    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', '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', '')

    # 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
    MeasRecs,SpecRecs,SampRecs,SiteRecs,LocRecs=[],[],[],[],[]
    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,'r').readlines()
            keys=file_input[0].replace('\n','').split(',') # splits on underscores
            keys=[k.strip('"') for k in keys]
            if "Interval Top (cm) on SHLF" in keys:interval_key="Interval Top (cm) on SHLF"
            elif " Interval Bot (cm) on SECT" in keys:interval_key=" Interval Bot (cm) on SECT"
            elif "Offset (cm)" in keys: interval_key="Offset (cm)"
            else: print("couldn't find interval or offset amount")
            if "Top Depth (m)" in keys:depth_key="Top Depth (m)"
            elif "CSF-A Top (m)" in keys:depth_key="CSF-A Top (m)"
            elif "Depth CSF-A (m)" in keys:depth_key="Depth CSF-A (m)"
            else: print("couldn't find depth")
            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=""; print("couldn't find composite depth")
            if "Demag level (mT)" in keys:demag_key="Demag level (mT)"
            elif "Demag Level (mT)" in keys: demag_key="Demag Level (mT)"
            elif "Treatment Value" in keys: demag_key="Treatment Value"
            else: print("couldn't find demag type")
            if "Inclination (Tray- and Bkgrd-Corrected) (deg)" in keys:inc_key="Inclination (Tray- and Bkgrd-Corrected) (deg)"
            elif "Inclination background + tray corrected  (deg)" in keys:inc_key="Inclination background + tray corrected  (deg)"
            elif "Inclination background + tray corrected  (\xc2\xb0)" in keys:inc_key="Inclination background + tray corrected  (\xc2\xb0)"
            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 (Tray- and Bkgrd-Corrected) (deg)" in keys:dec_key="Declination (Tray- and Bkgrd-Corrected) (deg)"
            elif "Declination background + tray corrected (deg)" in keys:dec_key="Declination background + tray corrected (deg)"
            elif "Declination background + tray corrected (\xc2\xb0)" in keys:dec_key="Declination background + tray corrected (\xc2\xb0)"
            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 (Tray- and Bkgrd-Corrected) (A/m)" in keys:int_key="Intensity (Tray- and Bkgrd-Corrected) (A/m)"
            elif "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")
            if "Core Type" in keys:
                core_type="Core Type"
            elif "Type" in keys: core_type="Type"
            else: print("couldn't find core type")
            if 'Run Number' in keys: run_number_key='Run Number'
            elif 'Test No.' in keys: run_number_key='Test No.'
            else: print("couldn't find run number")
            if 'Test Changed On' in keys: date_key='Test Changed On'
            elif "Timestamp (UTC)" in keys: date_key="Timestamp (UTC)"
            else: print("couldn't find timestamp")
            if "Section" in keys: sect_key="Section"
            elif "Sect" in keys: sect_key="Sect"
            else: print("couldn't find section number")
            if 'Section Half' in keys: half_key='Section Half'
            elif "A/W" in keys: half_key="A/W"
            else: print("couldn't find half number")
            if "Text ID" in keys: text_id="Text ID"
            elif "Text Id" in keys: text_id="Text Id"
            else: print("couldn't find ID number")
            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].strip(""" " ' """)
                if 'Exp' in list(InRec.keys()) and InRec['Exp']!="": test=1 # get rid of pesky blank lines (why is this a thing?)
                if not test: continue
                run_number=""
                inst="IODP-SRM"
                volume='15.59' # set default volume to this
                if 'Sample Area (cm?)' in list(InRec.keys()) and  InRec['Sample Area (cm?)']!= "": volume=InRec['Sample Area (cm?)']
                MeasRec,SpecRec,SampRec,SiteRec,LocRec={},{},{},{},{}
                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])
                sample = expedition+'-'+location+'-'+InRec['Core']+InRec[core_type]
                site = expedition+'-'+location

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

                if specimen!="" and specimen not in [x['specimen'] if 'specimen' in list(x.keys()) else "" for x in SpecRecs]:
                    SpecRec['specimen'] = specimen
                    SpecRec['sample'] = sample
                    SpecRec['citations']=citations
                    SpecRec['volume'] = volume
                    SpecRec['specimen_alternatives']=InRec[text_id]
                    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['core_depth']=InRec[depth_key]
                    if comp_depth_key!='':
                        SampRec['composite_depth']=InRec[comp_depth_key]
                    if "SHLF" not in InRec[text_id]:
                        SampRec['method_codes']='FS-C-DRILL-IODP:SP-SS-C:SO-V'
                    else:
                        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
                MeasRec['magic_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"
                if run_number_key in list(InRec.keys()) and 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=mmddyy[0]+':'+mmddyy[1]+":"+mmddyy[2] +':' +datestamp[1]+":0"
                if '-' in datestamp[0]:
                    mmddyy=datestamp[0].split('-') # break into month day year
                    date=mmddyy[0]+':'+mmddyy[1]+":"+mmddyy[2] +':' +datestamp[1]+":0"
                if len(date.split(":")) > 6: date=date[:-2]
                try: utc_dt = datetime.datetime.strptime(date, "%m:%d:%Y:%H:%M:%S")
                except ValueError:
                    utc_dt = datetime.datetime.strptime(date, "%Y:%m:%d:%H:%M:%S")
                MeasRec['timestamp']=utc_dt.strftime("%Y-%m-%dT%H:%M:%S")+"Z"
                MeasRec["method_codes"]='LT-NO'
                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"]=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['Treatment Value'])+273 # convert C => K
                        MeasRec["treat_temp"]=treatment_value
                    elif 'Alternating Frequency' in InRec['Treatment Type']:
                        MeasRec['method_codes'] = 'LT-AF-Z'
                        inst=inst+':IODP-DTECH' # measured on shipboard Dtech D2000
                        treatment_value=float(InRec[demag_key])*1e-3 # convert mT => T
                        MeasRec["treat_ac_field"]=treatment_value # AF demag in treat mT => T
                    elif 'Thermal' in InRec['Treatment Type']:
                        MeasRec['method_codes'] = 'LT-T-Z'
                        inst=inst+':IODP-TDS' # measured on shipboard Schonstedt thermal demagnetizer
                        treatment_value=float(InRec[demag_key])+273 # convert C => K
                        MeasRec["treat_temp"]='%8.3e'%(treatment_value) #
                elif InRec[demag_key]!="0":
                    MeasRec['method_codes'] = 'LT-AF-Z'
                    inst=inst+':IODP-SRM-AF' # measured on shipboard in-line 2G AF
                    try: treatment_value=float(InRec[demag_key])*1e-3 # convert mT => T
                    except ValueError: print("Couldn't determine treatment value was given treatment value of %s and demag key %s; setting to blank you will have to manually correct this (or fix it)"%(InRec[demag_key], demag_key)); treatment_value=''
                    MeasRec["treat_ac_field"]=treatment_value # AF demag in treat mT => T
                MeasRec["standard"]='u' # assume all data are "good"
                vol=float(volume)*1e-6 # convert from cc to m^3
                if run_number!="":
                    MeasRec['external_database_ids']={'LIMS':run_number}
                else:
                    MeasRec['external_database_ids']=""
                MeasRec['dir_inc']=InRec[inc_key]
                MeasRec['dir_dec']=InRec[dec_key]
                intens = InRec[int_key]
                try: MeasRec['magn_moment']='%8.3e'%(float(intens)*vol) # convert intensity from A/m to Am^2 using vol
                except ValueError: print("couldn't find magnetic moment for specimen %s and int_key %s; leaving this field blank you'll have to fix this manually"%(specimen, int_key)); MeasRec['magn_moment']=''
                MeasRec['instrument_codes']=inst
                MeasRec['treat_step_num']='1'
                MeasRec['dir_csd']='0'
                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: int(round(x['treat_ac_field']-y['treat_ac_field'])))
    MeasOuts=pmag.measurements_methods3(MeasSort,noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

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

    return (True, meas_file)
Beispiel #21
0
def convert(**kwargs):

    version_num = pmag.get_version()
    dir_path = kwargs.get('dir_path', '.')
    input_dir_path = kwargs.get('input_dir_path', dir_path)
    output_dir_path = dir_path
    user = kwargs.get('user', '')
    meas_file = kwargs.get('meas_file', 'measurements.txt')  # outfile
    spec_file = kwargs.get('spec_file', 'specimens.txt')  # specimen outfile
    samp_file = kwargs.get('samp_file', 'samples.txt')  # sample outfile
    site_file = kwargs.get('site_file', 'sites.txt')  # site outfile
    loc_file = kwargs.get('loc_file', 'locations.txt')  # location outfile
    mag_file = kwargs.get('mag_file')
    specnum = kwargs.get('specnum', 1)
    samp_con = kwargs.get('samp_con', '1')
    location = kwargs.get('location', 'unknown')
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')
    noave = kwargs.get('noave', 0)  # default (0) means DO average
    meth_code = kwargs.get('meth_code', "LP-NO")
    volume = float(kwargs.get('volume', 2.5)) * 1e-6
    timezone = kwargs.get('timestamp', 'UTC')

    # format variables
    mag_file = os.path.join(input_dir_path, mag_file)
    if specnum != 0: specnum = -int(specnum)
    if samp_con.startswith("4"):
        if "-" not in samp_con:
            print("option [4] must be in form 4-Z where Z is an integer")
            return False, "naming convention option [4] must be in form 4-Z where Z is an integer"
        else:
            Z = samp_con.split("-")[1]
            samp_con = "4"
    elif samp_con.startswith("7"):
        if "-" not in samp_con:
            print("option [7] must be in form 7-Z where Z is an integer")
            return False, "naming convention option [7] must be in form 7-Z where Z is an integer"
        else:
            Z = samp_con.split("-")[1]
            samp_con = "7"
    else:
        Z = 1

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

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

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

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

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

            specimenDec = parsedLine[2]
            specimenInc = parsedLine[3]
            line = data.readline()
            line = data.readline()
            parsedLine = line.split()
            geographicDec = parsedLine[1]
            geographicInc = parsedLine[2]

            # Add data to various MagIC data tables.
            specimen = sampleName
            if specnum != 0: sample = specimen[:specnum]
            else: sample = specimen
            site = pmag.parse_site(sample, samp_con, Z)

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

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

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

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

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

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

    data.close()

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

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

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

    return True, meas_file
Beispiel #22
0
def convert(**kwargs):
    # initialize defaults
    version_num=pmag.get_version()
    citations="This study"
    dir_path,demag='.','NRM'
    depth_method='a'

    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', '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', '')

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

    # parsing the data
    MeasRecs,SpecRecs,SampRecs,SiteRecs,LocRecs=[],[],[],[],[]
    file_found = False
    for f in filelist: # parse each file
        year_warning = True
        if f[-3:].lower()=='csv':
            print('processing:', f)
            file_found = True
            # get correct full filename and read data
            fname = pmag.resolve_file_name(f, input_dir_path)
            full_file = open(fname)
            file_input=full_file.readlines()
            full_file.close()
            keys=file_input[0].replace('\n','').split(',') # splits on underscores
            keys=[k.strip('"') for k in keys]
            if "Interval Top (cm) on SHLF" in keys:interval_key="Interval Top (cm) on SHLF"
            elif " Interval Bot (cm) on SECT" in keys:interval_key=" Interval Bot (cm) on SECT"
            elif "Offset (cm)" in keys: interval_key="Offset (cm)"
            else: print("couldn't find interval or offset amount")
            if "Top Depth (m)" in keys:depth_key="Top Depth (m)"
            elif "CSF-A Top (m)" in keys:depth_key="CSF-A Top (m)"
            elif "Depth CSF-A (m)" in keys:depth_key="Depth CSF-A (m)"
            else: print("couldn't find depth")
            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=""; print("couldn't find composite depth")
            if "Demag level (mT)" in keys:demag_key="Demag level (mT)"
            elif "Demag Level (mT)" in keys: demag_key="Demag Level (mT)"
            elif "Treatment Value" in keys: demag_key="Treatment Value"
            else: print("couldn't find demag type")
            if "Inclination (Tray- and Bkgrd-Corrected) (deg)" in keys:inc_key="Inclination (Tray- and Bkgrd-Corrected) (deg)"
            elif "Inclination background + tray corrected  (deg)" in keys:inc_key="Inclination background + tray corrected  (deg)"
            elif "Inclination background + tray corrected  (\xc2\xb0)" in keys:inc_key="Inclination background + tray corrected  (\xc2\xb0)"
            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 (Tray- and Bkgrd-Corrected) (deg)" in keys:dec_key="Declination (Tray- and Bkgrd-Corrected) (deg)"
            elif "Declination background + tray corrected (deg)" in keys:dec_key="Declination background + tray corrected (deg)"
            elif "Declination background + tray corrected (\xc2\xb0)" in keys:dec_key="Declination background + tray corrected (\xc2\xb0)"
            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 (Tray- and Bkgrd-Corrected) (A/m)" in keys:int_key="Intensity (Tray- and Bkgrd-Corrected) (A/m)"
            elif "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")
            if "Core Type" in keys:
                core_type="Core Type"
            elif "Type" in keys: core_type="Type"
            else: print("couldn't find core type")
            if 'Run Number' in keys: run_number_key='Run Number'
            elif 'Test No.' in keys: run_number_key='Test No.'
            else: print("couldn't find run number")
            if 'Test Changed On' in keys: date_key='Test Changed On'
            elif "Timestamp (UTC)" in keys: date_key="Timestamp (UTC)"
            else: print("couldn't find timestamp")
            if "Section" in keys: sect_key="Section"
            elif "Sect" in keys: sect_key="Sect"
            else: print("couldn't find section number")
            if 'Section Half' in keys: half_key='Section Half'
            elif "A/W" in keys: half_key="A/W"
            else: print("couldn't find half number")
            if "Text ID" in keys: text_id="Text ID"
            elif "Text Id" in keys: text_id="Text Id"
            else: print("couldn't find ID number")
            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].strip(""" " ' """)
                if 'Exp' in list(InRec.keys()) and InRec['Exp']!="": test=1 # get rid of pesky blank lines (why is this a thing?)
                if not test: continue
                run_number=""
                inst="IODP-SRM"
                volume='15.59' # set default volume to this
                if 'Sample Area (cm?)' in list(InRec.keys()) and  InRec['Sample Area (cm?)']!= "": volume=InRec['Sample Area (cm?)']
                MeasRec,SpecRec,SampRec,SiteRec,LocRec={},{},{},{},{}
                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])
                sample = expedition+'-'+location+'-'+InRec['Core']+InRec[core_type]
                site = expedition+'-'+location

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

                if specimen!="" and specimen not in [x['specimen'] if 'specimen' in list(x.keys()) else "" for x in SpecRecs]:
                    SpecRec['specimen'] = specimen
                    SpecRec['sample'] = sample
                    SpecRec['citations']=citations
                    SpecRec['volume'] = volume
                    SpecRec['specimen_alternatives']=InRec[text_id]
                    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['core_depth']=InRec[depth_key]
                    if comp_depth_key!='':
                        SampRec['composite_depth']=InRec[comp_depth_key]
                    if "SHLF" not in InRec[text_id]:
                        SampRec['method_codes']='FS-C-DRILL-IODP:SP-SS-C:SO-V'
                    else:
                        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
                MeasRec['magic_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"
                if run_number_key in list(InRec.keys()) and 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(mmddyy[2])==1: mmddyy[2] = '0'+mmddyy[2] # make 2 characters
                    if len(datestamp[1])==1: datestamp[1]='0'+datestamp[1] # make 2 characters
                    hour, minute = datestamp[1].split(':')
                    if len(hour) == 1:
                        hour = '0' + hour
                    date=mmddyy[0]+':'+mmddyy[1]+":"+mmddyy[2] +':' + hour + ":" + minute + ":00"
                    #date=mmddyy[2] + ':'+mmddyy[0]+":"+mmddyy[1] +':' + hour + ":" + minute + ":00"
                if '-' in datestamp[0]:
                    mmddyy=datestamp[0].split('-') # break into month day year
                    date=mmddyy[0]+':'+mmddyy[1]+":"+mmddyy[2] +':' +datestamp[1]+":0"
                if len(date.split(":")) > 6: date=date[:-2]
                # try with month:day:year
                try:
                    utc_dt = datetime.datetime.strptime(date, "%m:%d:%Y:%H:%M:%S")
                except ValueError:
                # try with year:month:day
                    try:
                        utc_dt = datetime.datetime.strptime(date, "%Y:%m:%d:%H:%M:%S")
                    except ValueError:
                # if all else fails, assume the year is in the third position
                # and try padding with '20'
                        new_date = pad_year(date, ind=2, warn=year_warning, fname=os.path.split(f)[1])
                        utc_dt = datetime.datetime.strptime(new_date, "%m:%d:%Y:%H:%M:%S")
                        # only give warning once per csv file
                        year_warning = False
                MeasRec['timestamp']=utc_dt.strftime("%Y-%m-%dT%H:%M:%S")+"Z"
                MeasRec["method_codes"]='LT-NO'
                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"]=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['Treatment Value'])+273 # convert C => K
                        MeasRec["treat_temp"]=treatment_value
                    elif 'Alternating Frequency' in InRec['Treatment Type']:
                        MeasRec['method_codes'] = 'LT-AF-Z'
                        inst=inst+':IODP-DTECH' # measured on shipboard Dtech D2000
                        treatment_value=float(InRec[demag_key])*1e-3 # convert mT => T
                        MeasRec["treat_ac_field"]=treatment_value # AF demag in treat mT => T
                    elif 'Thermal' in InRec['Treatment Type']:
                        MeasRec['method_codes'] = 'LT-T-Z'
                        inst=inst+':IODP-TDS' # measured on shipboard Schonstedt thermal demagnetizer
                        treatment_value=float(InRec[demag_key])+273 # convert C => K
                        MeasRec["treat_temp"]='%8.3e'%(treatment_value) #
                elif InRec[demag_key]!="0":
                    MeasRec['method_codes'] = 'LT-AF-Z'
                    inst=inst+':IODP-SRM-AF' # measured on shipboard in-line 2G AF
                    try: treatment_value=float(InRec[demag_key])*1e-3 # convert mT => T
                    except ValueError: print("Couldn't determine treatment value was given treatment value of %s and demag key %s; setting to blank you will have to manually correct this (or fix it)"%(InRec[demag_key], demag_key)); treatment_value=''
                    MeasRec["treat_ac_field"]=treatment_value # AF demag in treat mT => T
                MeasRec["standard"]='u' # assume all data are "good"
                vol=float(volume)*1e-6 # convert from cc to m^3
                if run_number!="":
                    MeasRec['external_database_ids']={'LIMS':run_number}
                else:
                    MeasRec['external_database_ids']=""
                MeasRec['dir_inc']=InRec[inc_key]
                MeasRec['dir_dec']=InRec[dec_key]
                intens = InRec[int_key]
                try: MeasRec['magn_moment']='%8.3e'%(float(intens)*vol) # convert intensity from A/m to Am^2 using vol
                except ValueError: print("couldn't find magnetic moment for specimen %s and int_key %s; leaving this field blank you'll have to fix this manually"%(specimen, int_key)); MeasRec['magn_moment']=''
                MeasRec['instrument_codes']=inst
                MeasRec['treat_step_num']='1'
                MeasRec['dir_csd']='0'
                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,key=lambda x: (x['specimen'], float(x['treat_ac_field'])))
    #MeasSort=sorted(MeasRecs,key=lambda x: float(x['treat_ac_field']))
    #MeasOuts=pmag.measurements_methods3(MeasSort,noave)
    MeasOuts=pmag.measurements_methods3(MeasRecs,noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

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

    return (True, meas_file)
Beispiel #23
0
def convert(**kwargs):

    # initialize some stuff
    methcode="LP-NO"
    phi,theta,peakfield,labfield=0,0,0,0
    pTRM,MD=0,0
    dec=[315,225,180,135,45,90,270,270,270,90,180,180,0,0,0]
    inc=[0,0,0,0,0,-45,-45,0,45,45,45,-45,-90,-45,45]
    tdec=[0,90,0,180,270,0,0,90,0]
    tinc=[0,0,90,0,0,-90,0,0,90]
    missing=1
    demag="N"
    citations='This study'
    fmt='old'
    Samps=[]
    trm=0
    irm=0

    #get args
    user = kwargs.get('user', '')
    dir_path = kwargs.get('dir_path', '.')
    output_dir_path = dir_path
    meas_file = kwargs.get('meas_file', 'measurements.txt')
    spec_file = kwargs.get('spec_file', 'specimens.txt') # specimen outfile
    samp_file = kwargs.get('samp_file', 'samples.txt') # sample outfile
    site_file = kwargs.get('site_file', 'sites.txt') # site outfile
    loc_file = kwargs.get('loc_file', 'locations.txt') # location outfile
    mag_file = kwargs.get('mag_file', '')
    labfield = kwargs.get('labfield', '')
    if labfield:
        labfield = float(labfield) *1e-6
    else:
        labfield = 0
    phi = kwargs.get('phi', 0)
    if phi:
        phi = float(phi)
    else:
        phi = 0
    theta = kwargs.get('theta', 0)
    if theta:
        theta=float(theta)
    else:
        theta = 0
    peakfield = kwargs.get('peakfield', 0)
    if peakfield:
        peakfield=float(peakfield) *1e-3
    else:
        peakfield = 0
    specnum = kwargs.get('specnum', 0)
    samp_con = kwargs.get('samp_con', '1')
    location = kwargs.get('location', 'unknown')
    samp_infile = kwargs.get('samp_infile', '')
    syn = kwargs.get('syn', 0)
    institution = kwargs.get('institution', '')
    syntype = kwargs.get('syntype', '')
    inst = kwargs.get('inst', '')
    noave = kwargs.get('noave', 0)
    codelist = kwargs.get('codelist', '')
    coil = kwargs.get('coil', '')
    cooling_rates = kwargs.get('cooling_rates', '')
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')
    timezone = kwargs.get('timezone', 'UTC')

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

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

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

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

    for line in lines:
        instcode=""
        if len(line)>2:
            MeasRec,SpecRec,SampRec,SiteRec,LocRec={},{},{},{},{}
            MeasRec['software_packages']=version_num
            MeasRec["description"]=""
            MeasRec["treat_temp"]='%8.3e' % (273) # room temp in kelvin
            MeasRec["meas_temp"]='%8.3e' % (273) # room temp in kelvin
            MeasRec["treat_ac_field"]='0'
            MeasRec["treat_dc_field"]='0'
            MeasRec["treat_dc_field_phi"]='0'
            MeasRec["treat_dc_field_theta"]='0'
            meas_type="LT-NO"
            rec=line.split()
            try: float(rec[0]); print("No specimen name for line #%d in the measurement file"%lines.index(line)); continue
            except ValueError: pass
            if rec[1]==".00":rec[1]="0.00"
            treat=rec[1].split('.')
            if methcode=="LP-IRM":
                if irmunits=='mT':
                    labfield=float(treat[0])*1e-3
                else:
                    labfield=pmag.getfield(irmunits,coil,treat[0])
                if rec[1][0]!="-":
                    phi,theta=0.,90.
                else:
                    phi,theta=0.,-90.
                meas_type="LT-IRM"
                MeasRec["treat_dc_field"]='%8.3e'%(labfield)
                MeasRec["treat_dc_field_phi"]='%7.1f'%(phi)
                MeasRec["treat_dc_field_theta"]='%7.1f'%(theta)
            if len(rec)>6:
              code1=rec[6].split(';') # break e.g., 10/15/02;7:45 indo date and time
              if len(code1)==2: # old format with AM/PM
                missing=0
                code2=code1[0].split('/') # break date into mon/day/year
                code3=rec[7].split(';') # break e.g., AM;C34;200  into time;instr/axes/measuring pos;number of measurements
                yy=int(code2[2])
                if yy <90:
                    yyyy=str(2000+yy)
                else: yyyy=str(1900+yy)
                mm=int(code2[0])
                if mm<10:
                    mm="0"+str(mm)
                else: mm=str(mm)
                dd=int(code2[1])
                if dd<10:
                    dd="0"+str(dd)
                else: dd=str(dd)
                time=code1[1].split(':')
                hh=int(time[0])
                if code3[0]=="PM":hh=hh+12
                if hh<10:
                    hh="0"+str(hh)
                else: hh=str(hh)
                min=int(time[1])
                if min<10:
                   min= "0"+str(min)
                else: min=str(min)
                dt=yyyy+":"+mm+":"+dd+":"+hh+":"+min+":00"
                local = pytz.timezone(timezone)
                naive = datetime.datetime.strptime(dt, "%Y:%m:%d:%H:%M:%S")
                local_dt = local.localize(naive, is_dst=None)
                utc_dt = local_dt.astimezone(pytz.utc)
                MeasRec["timestamp"]=utc_dt.strftime("%Y-%m-%dT%H:%M:%S")+"Z"
                if inst=="":
                    if code3[1][0]=='C':instcode='SIO-bubba'
                    if code3[1][0]=='G':instcode='SIO-flo'
                else:
                    instcode=''
                MeasRec["meas_n_orient"]=code3[1][2]
              elif len(code1)>2: # newest format (cryo7 or later)
                if "LP-AN-ARM" not in methcode:labfield=0
                fmt='new'
                date=code1[0].split('/') # break date into mon/day/year
                yy=int(date[2])
                if yy <90:
                    yyyy=str(2000+yy)
                else: yyyy=str(1900+yy)
                mm=int(date[0])
                if mm<10:
                    mm="0"+str(mm)
                else: mm=str(mm)
                dd=int(date[1])
                if dd<10:
                    dd="0"+str(dd)
                else: dd=str(dd)
                time=code1[1].split(':')
                hh=int(time[0])
                if hh<10:
                    hh="0"+str(hh)
                else: hh=str(hh)
                min=int(time[1])
                if min<10:
                   min= "0"+str(min)
                else:
                    min=str(min)
                dt=yyyy+":"+mm+":"+dd+":"+hh+":"+min+":00"
                local = pytz.timezone(timezone)
                naive = datetime.datetime.strptime(dt, "%Y:%m:%d:%H:%M:%S")
                local_dt = local.localize(naive, is_dst=None)
                utc_dt = local_dt.astimezone(pytz.utc)
                MeasRec["timestamp"]=utc_dt.strftime("%Y-%m-%dT%H:%M:%S")+"Z"
                if inst=="":
                    if code1[6][0]=='C':
                        instcode='SIO-bubba'
                    if code1[6][0]=='G':
                        instcode='SIO-flo'
                else:
                    instcode=''
                if len(code1)>1:
                    MeasRec["meas_n_orient"]=code1[6][2]
                else:
                    MeasRec["meas_n_orient"]=code1[7]   # takes care of awkward format with bubba and flo being different
                if user=="":user=code1[5]
                if code1[2][-1]=='C':
                    demag="T"
                    if code1[4]=='microT' and float(code1[3])!=0. and "LP-AN-ARM" not in methcode: labfield=float(code1[3])*1e-6
                if code1[2]=='mT' and methcode!="LP-IRM":
                    demag="AF"
                    if code1[4]=='microT' and float(code1[3])!=0.: labfield=float(code1[3])*1e-6
                if code1[4]=='microT' and labfield!=0. and meas_type!="LT-IRM":
                    phi,theta=0.,-90.
                    if demag=="T": meas_type="LT-T-I"
                    if demag=="AF": meas_type="LT-AF-I"
                    MeasRec["treat_dc_field"]='%8.3e'%(labfield)
                    MeasRec["treat_dc_field_phi"]='%7.1f'%(phi)
                    MeasRec["treat_dc_field_theta"]='%7.1f'%(theta)
                if code1[4]=='' or labfield==0. and meas_type!="LT-IRM":
                    if demag=='T':meas_type="LT-T-Z"
                    if demag=="AF":meas_type="LT-AF-Z"
                    MeasRec["treat_dc_field"]='0'
            if syn==0:
                specimen=rec[0]
                MeasRec["specimen"]=specimen
                if specnum!=0:
                    sample=rec[0][:specnum]
                else:
                    sample=rec[0]
                if samp_infile and Samps: # if samp_infile was provided AND yielded sample data
                    samp=pmag.get_dictitem(Samps,'sample',sample,'T')
                    if len(samp)>0:
                        location=samp[0]["location"]
                        site=samp[0]["site"]
                    else:
                        location=''
                        site=''
                else:
                    site=pmag.parse_site(sample,samp_con,Z)
                if location!='' and location not in [x['location'] if 'location' in list(x.keys()) else '' for x in LocRecs]:
                    LocRec['location'] = location
                    LocRec['lat_n'] = lat
                    LocRec['lat_s'] = lat
                    LocRec['lon_e'] = lon
                    LocRec['lon_w'] = lon
                    LocRecs.append(LocRec)
                if site!='' and site not in [x['site'] if 'site' in list(x.keys()) else '' for x in SiteRecs]:
                    SiteRec['location'] = location
                    SiteRec['site'] = site
                    SiteRec['lat'] = lat
                    SiteRec['lon'] = lon
                    SiteRecs.append(SiteRec)
                if sample!='' and sample not in [x['sample'] if 'sample' in list(x.keys()) else '' for x in SampRecs]:
                    SampRec['site'] = site
                    SampRec['sample'] = sample
                    SampRecs.append(SampRec)
                if specimen!='' and specimen not in [x['specimen'] if 'specimen' in list(x.keys()) else '' for x in SpecRecs]:
                    SpecRec["specimen"]=specimen
                    SpecRec['sample'] = sample
                    SpecRecs.append(SpecRec)
            else:
                specimen=rec[0]
                MeasRec["specimen"]=specimen
                if specnum!=0:
                    sample=rec[0][:specnum]
                else:
                    sample=rec[0]
                site=pmag.parse_site(sample,samp_con,Z)
                if location!='' and location not in [x['location'] if 'location' in list(x.keys()) else '' for x in LocRecs]:
                    LocRec['location'] = location
                    LocRec['lat_n'] = lat
                    LocRec['lat_s'] = lat
                    LocRec['lon_e'] = lon
                    LocRec['lon_w'] = lon
                    LocRecs.append(LocRec)
                if site!='' and site not in [x['site'] if 'site' in list(x.keys()) else '' for x in SiteRecs]:
                    SiteRec['location'] = location
                    SiteRec['site'] = site
                    SiteRec['lat'] = lat
                    SiteRec['lon'] = lon
                    SiteRecs.append(SiteRec)
                if sample!='' and sample not in [x['sample'] if 'sample' in list(x.keys()) else '' for x in SampRecs]:
                    SampRec['site'] = site
                    SampRec['sample'] = sample
                    SampRecs.append(SampRec)
                if specimen!='' and specimen not in [x['specimen'] if 'specimen' in list(x.keys()) else '' for x in SpecRecs]:
                    SpecRec["specimen"]=specimen
                    SpecRec['sample'] = sample
                    SpecRecs.append(SpecRec)
                SampRec["institution"]=institution
                SampRec["material_type"]=syntype
            if float(rec[1])==0:
                pass
            elif demag=="AF":
                if methcode != "LP-AN-ARM":
                    MeasRec["treat_ac_field"]='%8.3e' %(float(rec[1])*1e-3) # peak field in tesla
                    if meas_type=="LT-AF-Z": MeasRec["treat_dc_field"]='0'
                else: # AARM experiment
                    if treat[1][0]=='0':
                        meas_type="LT-AF-Z:LP-AN-ARM:"
                        MeasRec["treat_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
                        MeasRec["treat_dc_field"]='%8.3e'%(0)
                        if labfield!=0 and methcode!="LP-AN-ARM": print("Warning - inconsistency in mag file with lab field - overriding file with 0")
                    else:
                        meas_type="LT-AF-I:LP-AN-ARM"
                        ipos=int(treat[0])-1
                        MeasRec["treat_dc_field_phi"]='%7.1f' %(dec[ipos])
                        MeasRec["treat_dc_field_theta"]='%7.1f'% (inc[ipos])
                        MeasRec["treat_dc_field"]='%8.3e'%(labfield)
                        MeasRec["treat_ac_field"]='%8.3e' %(peakfield) # peak field in tesla
            elif demag=="T" and methcode == "LP-AN-TRM":
                MeasRec["treat_temp"]='%8.3e' % (float(treat[0])+273.) # temp in kelvin
                if treat[1][0]=='0':
                    meas_type="LT-T-Z:LP-AN-TRM"
                    MeasRec["treat_dc_field"]='%8.3e'%(0)
                    MeasRec["treat_dc_field_phi"]='0'
                    MeasRec["treat_dc_field_theta"]='0'
                else:
                    MeasRec["treat_dc_field"]='%8.3e'%(labfield)
                    if treat[1][0]=='7': # alteration check as final measurement
                            meas_type="LT-PTRM-I:LP-AN-TRM"
                    else:
                            meas_type="LT-T-I:LP-AN-TRM"

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


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


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

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

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


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

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

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

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

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

    Parameters
    -----------
    dir_path : directory to output files to (default : current directory)
    user : colon delimited list of analysts (default : "")
    magfile : magnetometer file (.sam) to convert to MagIC (required)
    meas_file : measurement file name to output (default : measurements.txt)
    spec_file : specimen file name to output (default : specimens.txt)
    samp_file : sample file name to output (default : samples.txt)
    site_file : site file name to output (default : site.txt)
    loc_file : location file name to output (default : locations.txt)
    locname : location name
    methods : colon delimited list of sample method codes. full list here (https://www2.earthref.org/MagIC/method-codes) (default : SO-MAG
    specnum : number of terminal characters that identify a specimen
    norm : is volume or mass normalization using cgs or si units (options : cc,m3,g,kg) (default : cc)
    oersted : demag step vales are in Oersted
    noave : average measurement data or not. False is average, True is don't average. (default : False)
    samp_con : sample naming convention options as follows:
        [1] XXXXY: where XXXX is an arbitrary length site designation and Y
            is the single character sample designation.  e.g., TG001a is the
            first sample from site TG001.    [default]
        [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
        [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
        [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
        [5] site name = sample name
        [6] site name entered in sitename column in the orient.txt format input file  -- NOT CURRENTLY SUPPORTED
        [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
    input_dir_path : if you did not supply a full path with magfile you can put the directory the magfile is in here
    meas_n_orient : Number of different orientations in measurement (default : 8)
    labfield : DC_FIELD in microTesla (default : 0)
    phi : DC_PHI in degrees (default : 0)
    theta : DC_THETA in degrees (default : 0)

    Returns
    -----------
    type - Tuple : (True or False indicating if conversion was sucessful, meas_file name written)
    """
    dir_path = kwargs.get('dir_path', '.')
    user = kwargs.get('user', '')
    meas_file = kwargs.get('meas_file', 'measurements.txt') # outfile
    spec_file = kwargs.get('spec_file', 'specimens.txt') # specimen outfile
    samp_file = kwargs.get('samp_file', 'samples.txt') # sample outfile
    site_file = kwargs.get('site_file', 'sites.txt') # site outfile
    loc_file = kwargs.get('loc_file', 'locations.txt') # location outfile
    locname = kwargs.get('locname', 'unknown')
    sitename = kwargs.get('sitename', '')
    methods = kwargs.get('methods', ['SO-MAG'])
    specnum = -int(kwargs.get('specnum', 0))
    norm = kwargs.get('norm', 'cc')
    oersted = kwargs.get('oersted', False) # mT (oe/10) is the default value
    noave = kwargs.get('noave', False)  # False means do average
    samp_con = kwargs.get('samp_con', '3')
    magfile = kwargs.get('magfile', '')
    input_dir_path = kwargs.get('input_dir_path',os.path.split(magfile)[0])
    meas_n_orient = kwargs.get('meas_n_orient','8')
    output_dir_path = dir_path
    try:
        DC_FIELD = float(kwargs.get('labfield',0))*1e-6
        DC_PHI = float(kwargs.get('phi',0))
        DC_THETA = float(kwargs.get('theta',0))
    except ValueError: raise ValueError('problem with your dc parameters. please provide a labfield in microTesla and a phi and theta in degrees.')
    yn = ''
    if DC_FIELD==0 and DC_PHI==0 and DC_THETA==0: GET_DC_PARAMS=True
    else: GET_DC_PARAMS=False
    if locname=='' or locname==None: locname = 'unknown'
    if "4" in samp_con:
        if "-" not in samp_con:
            print("option [4] must be in form 4-Z where Z is an integer")
            return False, "naming convention option [4] must be in form 4-Z where Z is an integer"
        else:
            Z=samp_con.split("-")[1]
            samp_con="4"
    elif "7" in samp_con:
        if "-" not in samp_con:
            print("option [7] must be in form 7-Z where Z is an integer")
            return False, "naming convention option [7] must be in form 7-Z where Z is an integer"
        else:
            Z=samp_con.split("-")[1]
            samp_con="7"
    else: Z=1

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

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

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

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

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

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

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

    Parameters
    -----------
    dir_path : directory to output files to (default : current directory)
    user : colon delimited list of analysts (default : "")
    magfile : magnetometer file (.sam) to convert to MagIC (required)
    meas_file : measurement file name to output (default : measurements.txt)
    spec_file : specimen file name to output (default : specimens.txt)
    samp_file : sample file name to output (default : samples.txt)
    site_file : site file name to output (default : site.txt)
    loc_file : location file name to output (default : locations.txt)
    locname : location name
    methods : colon delimited list of sample method codes. full list here (https://www2.earthref.org/MagIC/method-codes) (default : SO-MAG
    specnum : number of terminal characters that identify a specimen
    norm : is volume or mass normalization using cgs or si units (options : cc,m3,g,kg) (default : cc)
    noave : average measurement data or not. False is average, True is don't average. (default : False)
    samp_con : sample naming convention options as follows:
        [1] XXXXY: where XXXX is an arbitrary length site designation and Y
            is the single character sample designation.  e.g., TG001a is the
            first sample from site TG001.    [default]
        [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
        [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
        [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
        [5] site name = sample name
        [6] site name entered in sitename column in the orient.txt format input file  -- NOT CURRENTLY SUPPORTED
        [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY
    input_dir_path : if you did not supply a full path with magfile you can put the directory the magfile is in here
    meas_n_orient : Number of different orientations in measurement (default : 8)
    labfield : DC_FIELD in microTesla (default : 0)
    phi : DC_PHI in degrees (default : 0)
    theta : DC_THETA in degrees (default : 0)

    Returns
    -----------
    type - Tuple : (True or False indicating if conversion was sucessful, meas_file name written)
    """
    dir_path = kwargs.get('dir_path', '.')
    user = kwargs.get('user', '')
    meas_file = kwargs.get('meas_file', 'measurements.txt') # outfile
    spec_file = kwargs.get('spec_file', 'specimens.txt') # specimen outfile
    samp_file = kwargs.get('samp_file', 'samples.txt') # sample outfile
    site_file = kwargs.get('site_file', 'sites.txt') # site outfile
    loc_file = kwargs.get('loc_file', 'locations.txt') # location outfile
    locname = kwargs.get('locname', 'unknown')
    sitename = kwargs.get('sitename', '')
    methods = kwargs.get('methods', ['SO-MAG'])
    specnum = -int(kwargs.get('specnum', 0))
    norm = kwargs.get('norm', 'cc')
    noave = kwargs.get('noave', False)  # False means do average
    samp_con = kwargs.get('samp_con', '3')
    magfile = kwargs.get('magfile', '')
    input_dir_path = kwargs.get('input_dir_path',os.path.split(magfile)[0])
    meas_n_orient = kwargs.get('meas_n_orient','8')
    output_dir_path = dir_path
    try:
        DC_FIELD = float(kwargs.get('labfield',0))*1e-6
        DC_PHI = float(kwargs.get('phi',0))
        DC_THETA = float(kwargs.get('theta',0))
    except ValueError: raise ValueError('problem with your dc parameters. please provide a labfield in microTesla and a phi and theta in degrees.')
    yn = ''
    if DC_FIELD==0 and DC_PHI==0 and DC_THETA==0: GET_DC_PARAMS=True
    else: GET_DC_PARAMS=False
    if locname=='' or locname==None: locname = 'unknown'
    if "4" in samp_con:
        if "-" not in samp_con:
            print("option [4] must be in form 4-Z where Z is an integer")
            return False, "naming convention option [4] must be in form 4-Z where Z is an integer"
        else:
            Z=samp_con.split("-")[1]
            samp_con="4"
    elif "7" in samp_con:
        if "-" not in samp_con:
            print("option [7] must be in form 7-Z where Z is an integer")
            return False, "naming convention option [7] must be in form 7-Z where Z is an integer"
        else:
            Z=samp_con.split("-")[1]
            samp_con="7"
    else: Z=1

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

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

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

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

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

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

    return True, meas_file
Beispiel #26
0
def convert(**kwargs):
    #
    # initialize variables
    #
    bed_dip, bed_dip_dir = "", ""
    sclass, lithology, _type = "", "", ""
    DecCorr = 0.
    months = [
        'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct',
        'Nov', 'Dec'
    ]

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

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

    if ID:
        input_dir_path = ID
    else:
        input_dir_path = dir_path

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

    if not mag_file:
        print("mag file is required input")
        return False, "mag file is required input"
    output_dir_path = dir_path
    mag_file = os.path.join(input_dir_path, mag_file)

    samplist = []
    try:
        SampRecs, file_type = pmag.magic_read(samp_file)
    except:
        SampRecs = []
    MeasRecs, SpecRecs, SiteRecs, LocRecs = [], [], [], []
    try:
        f = open(mag_file, 'br')
        input = str(f.read()).strip("b '")
        f.close()
    except Exception as ex:
        print('ex', ex)
        print("bad mag file")
        return False, "bad mag file"
    firstline, date = 1, ""
    d = input.split('\\xcd')
    for line in d:
        rec = line.split('\\x00')
        if firstline == 1:
            firstline = 0
            spec, vol = "", 1
            el = 51
            while line[el:el + 1] != "\\":
                spec = spec + line[el]
                el += 1
            # check for bad sample name
            test = spec.split('.')
            date = ""
            if len(test) > 1:
                spec = test[0]
                kk = 24
                while line[kk] != '\\x01' and line[kk] != '\\x00':
                    kk += 1
                vcc = line[24:kk]
                el = 10
                while rec[el].strip() != '':
                    el += 1
                date, comments = rec[el + 7], []
            else:
                el = 9
                while rec[el] != '\\x01':
                    el += 1
                vcc, date, comments = rec[el - 3], rec[el + 7], []
            specname = spec.lower()
            print('importing ', specname)
            el += 8
            while rec[el].isdigit() == False:
                comments.append(rec[el])
                el += 1
            while rec[el] == "":
                el += 1
            az = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            pl = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            bed_dip_dir = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            bed_dip = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            if rec[el] == '\\x01':
                bed_dip = 180. - bed_dip
                el += 1
                while rec[el] == "":
                    el += 1
            fold_az = float(rec[el])
            el += 1
            while rec[el] == "":
                el += 1
            fold_pl = rec[el]
            el += 1
            while rec[el] == "":
                el += 1
            if rec[el] != "" and rec[el] != '\\x02' and rec[el] != '\\x01':
                deccorr = float(rec[el])
                az += deccorr
                bed_dip_dir += deccorr
                fold_az += deccorr
                if bed_dip_dir >= 360: bed_dip_dir = bed_dip_dir - 360.
                if az >= 360.: az = az - 360.
                if fold_az >= 360.: fold_az = fold_az - 360.
            else:
                deccorr = 0
            if specnum != 0:
                sample = specname[:specnum]
            else:
                sample = specname

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

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

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

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

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

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

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

    con.write_table_to_file('specimens', custom_name=spec_file)
    con.write_table_to_file('samples', custom_name=samp_file)
    con.write_table_to_file('sites', custom_name=site_file)
    con.write_table_to_file('locations', custom_name=loc_file)
    con.write_table_to_file('measurements', custom_name=meas_file)
    return True, meas_file
Beispiel #27
0
def convert(**kwargs):
    """
    EXAMPLE DOCSTRING for function (you would usually put the discription here)

    Parameters
    -----------
    user : colon delimited list of analysts (default : "")
    magfile : input magnetometer file (required)

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

    #get parameters from kwargs.get(parameter_name, default_value)
    user = kwargs.get('user', '')
    magfile = kwargs.get('magfile')

    #do any extra formating you need to variables here

    #open magfile to start reading data
    try:
        infile=open(magfile,'r')
    except Exception as ex:
        print(("bad file path: ", magfile))
        return False, "bad file path"

    #Depending on the dataset you may need to read in all data here put it in a list of dictionaries or something here. If you do just replace the "for line in infile.readlines():" bellow with "for d in data:" where data is the structure you put your data into

    #define the lists that hold each line of data for their respective tables
    SpecRecs,SampRecs,SiteRecs,LocRecs,MeasRecs=[],[],[],[],[]

    #itterate over the contence of the file
    for line in infile.readlines():
        MeasRec,SpecRec,SampRec,SiteRec,LocRec={},{},{},{},{}

        #extract data from line and put it in variables

        #fill this line of the Specimen table using above variables
        if specimen!="" and specimen not in [x['specimen'] if 'specimen' in list(x.keys()) else "" for x in SpecRecs]:
            SpecRec['analysts']=user
            SpecRecs.append(SpecRec)
        #fill this line of the Sample table using above variables
        if sample!="" and sample not in [x['sample'] if 'sample' in list(x.keys()) else "" for x in SampRecs]:
            SampRec['analysts']=user
            SampRecs.append(SampRec)
        #fill this line of the Site table using above variables
        if site!="" and site not in [x['site'] if 'site' in list(x.keys()) else "" for x in SiteRecs]:
            SiteRec['analysts']=user
            SiteRecs.append(SiteRec)
        #fill this line of the Location table using above variables
        if location!="" and location not in [x['location'] if 'location' in list(x.keys()) else "" for x in LocRecs]:
            LocRec['analysts']=user
            LocRecs.append(LocRec)

        #Fill this line of Meas Table using data in line
        MeasRec['analysts']=user
        MeasRecs.append(MeasRec)

    #close your file object so Python3 doesn't throw an annoying warning
    infile.close()

    #open a Contribution object
    con = nb.Contribution(output_dir_path,read_tables=[])

    #Create Magic Tables and add to a contribution
    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts=pmag.measurements_methods3(MeasRecs,noave) #figures out method codes for measuremet data
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

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

    return True, meas_file
Beispiel #28
0
def main(**kwargs):

    version_num=pmag.get_version()
    dir_path = kwargs.get('dir_path', '.')
    input_dir_path = kwargs.get('input_dir_path', dir_path)
    output_dir_path = dir_path
    user = kwargs.get('user', '')
    meas_file = kwargs.get('meas_file', 'measurements.txt') # outfile
    spec_file = kwargs.get('spec_file', 'specimens.txt') # specimen outfile
    samp_file = kwargs.get('samp_file', 'samples.txt') # sample outfile
    site_file = kwargs.get('site_file', 'sites.txt') # site outfile
    loc_file = kwargs.get('loc_file', 'locations.txt') # location outfile
    mag_file = kwargs.get('mag_file')
    specnum = kwargs.get('specnum', 1)
    samp_con = kwargs.get('samp_con', '1')
    location = kwargs.get('location', 'unknown')
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')
    noave = kwargs.get('noave', 0) # default (0) means DO average
    meth_code = kwargs.get('meth_code', "LP-NO")
    volume = float(kwargs.get('volume', 2.5))* 1e-6
    JR = kwargs.get('JR', False)
    if JR:
        if meth_code == "LP-NO":
            meth_code = ""
        meth_code=meth_code+":FS-C-DRILL-IODP:SP-SS-C:SO-V"
        meth_code=meth_code.strip(":")
        samp_con='5'

    # format variables
    tmp_file = mag_file.split(os.extsep)[0]+os.extsep+'tmp'
    mag_file = os.path.join(input_dir_path, mag_file)
    if specnum!=0: specnum=-int(specnum)
    if samp_con.startswith("4"):
        if "-" not in samp_con:
            print("option [4] must be in form 4-Z where Z is an integer")
            return False, "naming convention option [4] must be in form 4-Z where Z is an integer"
        else:
            Z=samp_con.split("-")[1]
            samp_con="4"
    elif samp_con.startswith("7"):
        if "-" not in samp_con:
            print("option [7] must be in form 7-Z where Z is an integer")
            return False, "naming convention option [7] must be in form 7-Z where Z is an integer"
        else:
            Z=samp_con.split("-")[1]
            samp_con="7"
    else: Z=1

    # parse data
    # fix .jr6 file so that there are spaces between all the columns.
    pre_data=open(mag_file, 'r')
    tmp_data=open(tmp_file, 'w')
    if samp_con!='2': fixed_data=pre_data.read().replace('-',' -')
    else:
        fixed_data=""
        for line in pre_data.readlines():
            entries=line.split()
            if len(entries)<2: continue
            fixed_line = entries[0] + ' ' + reduce(lambda x,y: x+' '+y, [x.replace('-',' -') for x in entries[1:]])
            fixed_data += fixed_line+os.linesep
    tmp_data.write(fixed_data)
    tmp_data.close()
    pre_data.close()

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

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

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

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

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

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

    return True, meas_file