def getvec_geocentric(gh, lat, lon):
    """
    from pmagpy - itype = 2 and alt = 6371.004
    Evaluates the vector at a given latitude and longitude for a specified
    set of coefficients
    Parameters
    ----------
    gh : a list of gauss coefficients
    lat : latitude of location
    long : longitude of location
    Returns
    -------
    vec : direction in [dec, inc, intensity]
    """
    sv = []
    pad = 120 - len(gh)
    for x in range(pad):
        gh.append(0.)
    for x in range(len(gh)):
        sv.append(0.)
    #! convert to colatitude for MB routine
    itype = 2
    colat = 90. - lat
    date, alt = 2000., 6371.004  # use a dummy date and altitude
    x, y, z, f = pmag.magsyn(gh, sv, date, date, itype, alt, colat, lon)
    vec = pmag.cart2dir([x, y, z])
    vec[2] = f
    return vec
Exemplo n.º 2
0
def main():
    """
    NAME
        igrf.py
    DESCRIPTION
        This program calculates igrf field values 
    using the routine of Malin and  Barraclough (1981) 
    based on d/igrfs from 1900 to 2010.
    between 1900 and 1000BCE, it uses CALS3K.4, ARCH3K.1 
    Prior to 1000BCE, it uses PFM9k or CALS10k-4b
    Calculates reference field vector at  specified location and time.
  
    SYNTAX
       igrf.py [-h] [-i] -f FILE  [< filename]
    OPTIONS:
       -h prints help message and quits
       -i for interactive data entry
       -f FILE  specify file name with input data 
       -F FILE  specify output file name
       -ages MIN MAX INCR: specify age minimum in years (+/- AD), maximum and increment, default is line by line
       -loc LAT LON;  specify location, default is line by line
       -alt ALT;  specify altitude in km, default is sealevel (0)
       -plt; make a plot of the time series
       -sav, saves plot and quits
       -fmt [pdf,jpg,eps,svg]  specify format for output figure  (default is svg)
       -mod [arch3k,cals3k,pfm9k,hfm10k,cals10k_2,shadif14k,cals10k] specify model for 3ka to 1900 AD, default is cals10k
             NB:  program uses IGRF12 for dates 1900 to 2015.
    
    INPUT FORMAT 
      interactive entry:
           date: decimal year
           alt:  altitude in km
           lat: positive north
           lon: positive east
       for file entry:
           space delimited string: date  alt   lat long
    OUTPUT  FORMAT
        Declination Inclination Intensity (nT) date alt lat long
    MODELS:  ARCH3K: (Korte et al., 2009);CALS3K (Korte & Contable, 2011); CALS10k (is .1b of Korte et al., 2011); PFM9K (Nilsson et al., 2014); HFM10k (is HFM.OL1.A1 of Constable et al., 2016); CALS10k_2 (is cals10k.2 of Constable et al., 2016), SHADIF14k (SHA.DIF.14K of Pavon-Carrasco et al., 2014).
    """
    plot, fmt = 0, 'svg'
    plt = 0
    if '-fmt' in sys.argv:
        ind = sys.argv.index('-fmt')
        fmt = sys.argv[ind + 1]
    if len(sys.argv) != 0 and '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-mod' in sys.argv:
        ind = sys.argv.index('-mod')
        mod = sys.argv[ind + 1]
    else:
        mod = 'cals10k'
    if '-f' in sys.argv:
        ind = sys.argv.index('-f')
        file = sys.argv[ind + 1]
        input = numpy.loadtxt(file)
    elif '-i' in sys.argv:
        while 1:
            try:
                line = []
                line.append(float(input("Decimal year: <cntrl-D to quit> ")))
                alt = input("Elevation in km [0] ")
                if alt == "": alt = "0"
                line.append(float(alt))
                line.append(float(input("Latitude (positive north) ")))
                line.append(float(input("Longitude (positive east) ")))
                if mod == '':
                    x, y, z, f = pmag.doigrf(line[3] % 360., line[2], line[1],
                                             line[0])
                else:
                    x, y, z, f = pmag.doigrf(line[3] % 360.,
                                             line[2],
                                             line[1],
                                             line[0],
                                             mod=mod)
                Dir = pmag.cart2dir((x, y, z))
                print('%8.2f %8.2f %8.0f' % (Dir[0], Dir[1], f))
            except EOFError:
                print("\n Good-bye\n")
                sys.exit()
    elif '-ages' in sys.argv:
        ind = sys.argv.index('-ages')
        agemin = float(sys.argv[ind + 1])
        agemax = float(sys.argv[ind + 2])
        ageincr = float(sys.argv[ind + 3])
        if '-loc' in sys.argv:
            ind = sys.argv.index('-loc')
            lat = float(sys.argv[ind + 1])
            lon = float(sys.argv[ind + 2])
        else:
            print("must specify lat/lon if using age range option")
            sys.exit()
        if '-alt' in sys.argv:
            ind = sys.argv.index('-alt')
            alt = float(sys.argv[ind + 1])
        else:
            alt = 0
        ages = numpy.arange(agemin, agemax, ageincr)
        lats = numpy.ones(len(ages)) * lat
        lons = numpy.ones(len(ages)) * lon
        alts = numpy.ones(len(ages)) * alt
        input = numpy.array([ages, alts, lats, lons]).transpose()
    else:
        input = numpy.loadtxt(sys.stdin, dtype=numpy.float)
    if '-F' in sys.argv:
        ind = sys.argv.index('-F')
        outfile = sys.argv[ind + 1]
        out = open(outfile, 'w')
    else:
        outfile = ""
    if '-sav' in sys.argv: plot = 1
    if '-plt' in sys.argv:
        plt = 1
        import matplotlib
        matplotlib.use("TkAgg")
        import pylab
        pylab.ion()
        Ages, Decs, Incs, Ints, VADMs = [], [], [], [], []
    for line in input:
        #if mod=='':
        #    x,y,z,f=pmag.doigrf(line[3]%360.,line[2],line[1],line[0])
        #else:
        #    x,y,z,f=pmag.doigrf(line[3]%360.,line[2],line[1],line[0],mod=mod)
        x, y, z, f = pmag.doigrf(line[3] % 360.,
                                 line[2],
                                 line[1],
                                 line[0],
                                 mod=mod)
        Dir = pmag.cart2dir((x, y, z))
        if outfile != "":
            out.write('%8.2f %8.2f %8.0f %7.1f %7.1f %7.1f %7.1f\n' %
                      (Dir[0], Dir[1], f, line[0], line[1], line[2], line[3]))
        elif plt:
            Ages.append(line[0])
            if Dir[0] > 180: Dir[0] = Dir[0] - 360.0
            Decs.append(Dir[0])
            Incs.append(Dir[1])
            Ints.append(f * 1e-3)
            VADMs.append(pmag.b_vdm(f * 1e-9, line[2]) * 1e-21)
        else:
            print('%8.2f %8.2f %8.0f %7.1f %7.1f %7.1f %7.1f' %
                  (Dir[0], Dir[1], f, line[0], line[1], line[2], line[3]))
    if plt:
        fig = pylab.figure(num=1, figsize=(7, 9))
        fig.add_subplot(411)
        pylab.plot(Ages, Decs)
        pylab.ylabel('Declination ($^{\circ}$)')
        fig.add_subplot(412)
        pylab.plot(Ages, Incs)
        pylab.ylabel('Inclination ($^{\circ}$)')
        fig.add_subplot(413)
        pylab.plot(Ages, Ints)
        pylab.ylabel('Intensity ($\mu$T)')
        fig.add_subplot(414)
        pylab.plot(Ages, VADMs)
        pylab.ylabel('VADMs (ZAm$^2$)')
        pylab.xlabel('Ages')
        if plot == 0:
            pylab.draw()
            ans = input("S[a]ve to save figure, <Return>  to quit  ")
            if ans == 'a':
                pylab.savefig('igrf.' + fmt)
                print('Figure saved as: ', 'igrf.' + fmt)
        else:
            pylab.savefig('igrf.' + fmt)
            print('Figure saved as: ', 'igrf.' + fmt)
        sys.exit()
Exemplo n.º 3
0
def main(command_line=True, **kwargs):
    """
    NAME
        pmd_magic.py
 
    DESCRIPTION
        converts PMD (Enkin)  format files to magic_measurements format files

    SYNTAX
        pmd_magic.py [command line options]

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

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

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


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

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

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

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

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

    SYNTAX
        bgc_magic.py [command line options]

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

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

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

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

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

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

    ErSampRec = {}

    # parse data

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

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

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

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

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

    # Configure the magic_measurements table

    for rowNum, row in data.iterrows():
        MagRec = {}
        MagRec['measurement_description'] = 'Date: ' + str(row['Date']) + ' Time: ' + str(row['Time'])
        MagRec["er_citation_names"] = "This study"
        MagRec['er_location_name'] = er_location_name
        MagRec['er_site_name'] = er_site_name
        MagRec['er_sample_name'] = sample_name
        MagRec['magic_software_packages'] = version_num
        MagRec["treatment_temp"] = '%8.3e' % (273) # room temp in kelvin
        MagRec["measurement_temp"] = '%8.3e' % (273) # room temp in kelvin
        MagRec["measurement_flag"] = 'g'
        MagRec["measurement_standard"] = 'u'
        MagRec["measurement_number"] = rowNum
        MagRec["er_specimen_name"] = sample_name
        MagRec["treatment_ac_field"] = '0'
        if row['DM Val'] == '0':
            meas_type = "LT-NO"
        elif int(row['DM Type']) > 0.0:
            meas_type = "LT-AF-Z"
            treat = float(row['DM Val'])
            MagRec["treatment_ac_field"] = '%8.3e' %(treat*1e-3) # convert from mT to tesla
        elif int(row['DM Type']) == -1:
            meas_type = "LT-T-Z"
            treat = float(row['DM Val'])
            MagRec["treatment_temp"] = '%8.3e' % (treat+273.) # temp in kelvin
        else:
            print("measurement type unknown:", row['DM Type'], " in row ", rowNum)
        MagRec["measurement_magn_moment"] = str(row['measurement_magn_moment'])
        MagRec["measurement_magn_volume"] = str(row['measurement_magn_volume'])
        MagRec["measurement_dec"] = str(row['measurement_dec'])
        MagRec["measurement_inc"] = str(row['measurement_inc'])
        MagRec['magic_method_codes'] = meas_type
        MagRec['measurement_csd'] = '0.0' # added due to magic.write error
        MagRec['measurement_positions'] = '1' # added due to magic.write error
        MagRecs.append(MagRec.copy())
    pmag.magic_write(samp_file, SampOuts, 'er_samples')
    print("sample orientations put in ", samp_file)
    MagOuts = pmag.measurements_methods(MagRecs, noave)
    pmag.magic_write(meas_file, MagOuts, 'magic_measurements')
    print("results put in ", meas_file)
    return True, meas_file
Exemplo n.º 5
0
    def average_duplicates(duplicates):
        '''
        avarage replicate measurements.
        '''
        carts_s,carts_g,carts_t=[],[],[]
        for rec in duplicates:
            moment=float(rec['moment'])
            if 'dec_s' in list(rec.keys()) and 'inc_s' in list(rec.keys()):
                if rec['dec_s']!="" and rec['inc_s']!="":
                    dec_s=float(rec['dec_s'])
                    inc_s=float(rec['inc_s'])
                    cart_s=pmag.dir2cart([dec_s,inc_s,moment])
                    carts_s.append(cart_s)
            if 'dec_g' in list(rec.keys()) and 'inc_g' in list(rec.keys()):
                if rec['dec_g']!="" and rec['inc_g']!="":
                    dec_g=float(rec['dec_g'])
                    inc_g=float(rec['inc_g'])
                    cart_g=pmag.dir2cart([dec_g,inc_g,moment])
                    carts_g.append(cart_g)
            if 'dec_t' in list(rec.keys()) and 'inc_t' in list(rec.keys()):
                if rec['dec_t']!="" and rec['inc_t']!="":
                    dec_t=float(rec['dec_t'])
                    inc_t=float(rec['inc_t'])
                    cart_t=pmag.dir2cart([dec_t,inc_t,moment])
                    carts_t.append(cart_t)
        if len(carts_s)>0:
            carts=scipy.array(carts_s)
            x_mean=scipy.mean(carts[:,0])
            y_mean=scipy.mean(carts[:,1])
            z_mean=scipy.mean(carts[:,2])
            mean_dir=pmag.cart2dir([x_mean,y_mean,z_mean])
            mean_dec_s="%.2f"%mean_dir[0]
            mean_inc_s="%.2f"%mean_dir[1]
            mean_moment="%10.3e"%mean_dir[2]
        else:
            mean_dec_s,mean_inc_s="",""
        if len(carts_g)>0:
            carts=scipy.array(carts_g)
            x_mean=scipy.mean(carts[:,0])
            y_mean=scipy.mean(carts[:,1])
            z_mean=scipy.mean(carts[:,2])
            mean_dir=pmag.cart2dir([x_mean,y_mean,z_mean])
            mean_dec_g="%.2f"%mean_dir[0]
            mean_inc_g="%.2f"%mean_dir[1]
            mean_moment="%10.3e"%mean_dir[2]
        else:
            mean_dec_g,mean_inc_g="",""

        if len(carts_t)>0:
            carts=scipy.array(carts_t)
            x_mean=scipy.mean(carts[:,0])
            y_mean=scipy.mean(carts[:,1])
            z_mean=scipy.mean(carts[:,2])
            mean_dir=pmag.cart2dir([x_mean,y_mean,z_mean])
            mean_dec_t="%.2f"%mean_dir[0]
            mean_inc_t="%.2f"%mean_dir[1]
            mean_moment="%10.3e"%mean_dir[2]
        else:
            mean_dec_t,mean_inc_t="",""

        meanrec={}
        for key in list(duplicates[0].keys()):
            if key in ['dec_s','inc_s','dec_g','inc_g','dec_t','inc_t','moment']:
                continue
            else:
                meanrec[key]=duplicates[0][key]
        meanrec['dec_s']=mean_dec_s
        meanrec['dec_g']=mean_dec_g
        meanrec['dec_t']=mean_dec_t
        meanrec['inc_s']=mean_inc_s
        meanrec['inc_g']=mean_inc_g
        meanrec['inc_t']=mean_inc_t
        meanrec['moment']=mean_moment
        return meanrec
Exemplo n.º 6
0
def main(command_line=True, **kwargs):
    """
    NAME
        utrecht_magic.py

    DESCRIPTION
        converts Utrecht magnetometer data files to magic_measurements files

    SYNTAX
        utrecht_magic.py [command line options]

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

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

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

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


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

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

    # parse data

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

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

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

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

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

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

# write out the data to MagIC data files
    pmag.magic_write(spec_file, SpecOuts, 'er_specimens')
    pmag.magic_write(samp_file, SampOuts, 'er_samples')
    pmag.magic_write(site_file, SiteOuts, 'er_sites')
#    MagOuts = pmag.measurements_methods(MagRecs, noave)
#    pmag.magic_write(meas_file, MagOuts, 'magic_measurements')
    pmag.magic_write(meas_file, MagRecs, 'magic_measurements')
    print("results put in ", meas_file)
    print("exit!")
    return True, meas_file
Exemplo n.º 7
0
def main(command_line=True, **kwargs):
    """
    NAME
        old_iodp_jr6_magic.py
 
    DESCRIPTION
        converts shipboard .jr6 format files to magic_measurements format files

    SYNTAX
        old_iodp_jr6_magic.py [command line options]

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


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

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


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


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

    # validate variables
    if not mag_file:
        print "You must provide an IODP_jr6 format file"
        return False, "You must provide an IODP_jr6 format file"
    if not os.path.exists(mag_file):
        print 'The input file you provided: {} does not exist.\nMake sure you have specified the correct filename AND correct input directory name.'.format(os.path.join(input_dir_path, mag_file))
        return False, 'The input file you provided: {} does not exist.\nMake sure you have specified the correct filename AND correct input directory name.'.format(magfile)
    if not os.path.exists(samp_file):
        print 'samp_file', samp_file
        print "Your input directory:\n{}\nmust contain an er_samples.txt file, or you must explicitly provide one".format(input_dir_path)
        return False, "Your input directory:\n{}\nmust contain an er_samples.txt file, or you must explicitly provide one".format(input_dir_path)
    
    # parse data
    temp = os.path.join(output_dir_path, 'temp.txt')
    fix_separation(mag_file, temp)
    #os.rename('temp.txt', mag_file)
    #data = open(mag_file, 'rU').readlines()
    data=pd.read_csv(temp, delim_whitespace=True,header=None)
    os.remove(temp)
    samples,filetype = pmag.magic_read(samp_file)
    data.columns=['specname','step','negz','y','x','expon','sample_azimuth','sample_dip','sample_bed_dip_direction','sample_bed_dip','bed_dip_dir2','bed_dip2','param1','param2','param3','param4','measurement_csd']
    cart=np.array([data['x'],data['y'],-data['negz']]).transpose()
    dir= pmag.cart2dir(cart).transpose()
    data['measurement_dec']=dir[0]
    data['measurement_inc']=dir[1]
    data['measurement_magn_volume']=dir[2]*(10.0**data['expon']) # A/m  - data in A/m
    data['measurement_flag']='g'
    data['measurement_standard']='u'
    data['measurement_number']='1'
    data['measurement_temp']='273'
    data['er_location_name']=er_location_name
    for rowNum, row in data.iterrows():
        MagRec={}
        spec_text_id=row['specname'].split('_')[1]
        SampRecs=pmag.get_dictitem(samples,'er_sample_alternatives',spec_text_id,'has') # retrieve sample record for this specimen
        if len(SampRecs)>0: # found one
            MagRec['er_specimen_name']=SampRecs[0]['er_sample_name']
            MagRec['er_sample_name']=MagRec['er_specimen_name']
            MagRec['er_site_name']=MagRec['er_specimen_name']
            MagRec["er_citation_names"]="This study"
            MagRec['er_location_name']=er_location_name
            MagRec['magic_software_packages']=version_num
            MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
            MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
            MagRec["measurement_flag"]='g'
            MagRec["measurement_standard"]='u'
            MagRec["measurement_number"]='1'
            MagRec["treatment_ac_field"]='0'
            volume=float(SampRecs[0]['sample_volume'])
            moment=row['measurement_magn_volume'] * volume 
            MagRec["measurement_magn_moment"]=str(moment)
            MagRec["measurement_magn_volume"]=str(row['measurement_magn_volume'])
            MagRec["measurement_dec"]='%7.1f'%(row['measurement_dec'])
            MagRec["measurement_inc"]='%7.1f'%(row['measurement_inc'])
            if row['step'] == 'NRM':
                meas_type="LT-NO"
            elif row['step'][0:2] == 'AD':
                meas_type="LT-AF-Z"
                treat=float(row['step'][2:])
                MagRec["treatment_ac_field"]='%8.3e' %(treat*1e-3) # convert from mT to tesla
            elif row['step'][0] == 'TD':
                meas_type="LT-T-Z"
                treat=float(row['step'][2:])
                MagRec["treatment_temp"]='%8.3e' % (treat+273.) # temp in kelvin
            elif row['step'][0:3]=='ARM': # 
                meas_type="LT-AF-I"
                treat=float(row['step'][3:])
                MagRec["treatment_ac_field"]='%8.3e' %(treat*1e-3) # convert from mT to tesla
                MagRec["treatment_dc_field"]='%8.3e' %(50e-6) # assume 50uT DC field
                MagRec["measurement_description"]='Assumed DC field - actual unknown'
            elif row['step'][0:3]=='IRM': # 
                meas_type="LT-IRM"
                treat=float(row['step'][3:])
                MagRec["treatment_dc_field"]='%8.3e' %(treat*1e-3) # convert from mT to tesla
            else:
                print 'unknown treatment type for ',row
                return False, 'unknown treatment type for ',row
            MagRec['magic_method_codes']=meas_type
            MagRecs.append(MagRec.copy())
        else:
            print 'sample name not found: ',row['specname']
    MagOuts=pmag.measurements_methods(MagRecs,noave)
    file_created, error_message = pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    if file_created:
        return True, meas_file
    else:
        return False, 'Results not written to file'
Exemplo n.º 8
0
def main():
    """
    NAME
        cart_dir.py
    
    DESCRIPTION
      converts cartesian coordinates to geomagnetic elements
    
    INPUT (COMMAND LINE ENTRY) 
           x1 x2  x3
        if only two columns, assumes magnitude of unity
    OUTPUT
           declination inclination magnitude
    
    SYNTAX
        cart_dir.py [command line options] [< filename]
    
    OPTIONS 
        -h prints help message and quits
        -i for interactive data entry
        -f FILE to specify input filename
        -F OFILE to specify output filename (also prints to screen)
    
    """
    ofile = ""
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-F' in sys.argv:
        ind = sys.argv.index('-F')
        ofile = sys.argv[ind + 1]
        outfile = open(ofile, 'w')
    if '-i' in sys.argv:
        cont = 1
        while cont == 1:
            cart = []
            try:
                ans = input('X: [ctrl-D  to quit] ')
                cart.append(float(ans))
                ans = input('Y: ')
                cart.append(float(ans))
                ans = input('Z: ')
                cart.append(float(ans))
            except:
                print("\n Good-bye \n")
                sys.exit()
            dir = pmag.cart2dir(
                cart)  # send dir to dir2cart and spit out result
            print('%7.1f %7.1f %10.3e' % (dir[0], dir[1], dir[2]))
    elif '-f' in sys.argv:
        ind = sys.argv.index('-f')
        file = sys.argv[ind + 1]
        inp = numpy.loadtxt(file)  # read from a file
    else:
        inp = numpy.loadtxt(sys.stdin,
                            dtype=numpy.float)  # read from standard input
    dir = pmag.cart2dir(inp)
    if len(dir.shape) == 1:
        line = dir
        print('%7.1f %7.1f %10.3e' % (line[0], line[1], line[2]))
        if ofile != "":
            outstring = '%7.1f %7.1f %10.8e\n' % (line[0], line[1], line[2])
            outfile.write(outstring)
    else:
        for line in dir:
            print('%7.1f %7.1f %10.3e' % (line[0], line[1], line[2]))
            if ofile != "":
                outstring = '%7.1f %7.1f %10.8e\n' % (line[0], line[1],
                                                      line[2])
                outfile.write(outstring)
Exemplo n.º 9
0
def main(command_line=True, **kwargs):
    """
    NAME
        bgc_magic.py

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

    SYNTAX
        bgc_magic.py [command line options]

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

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

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

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

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

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

    ErSampRec = {}

    # parse data

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

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

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

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

    # Configure the er_sample table

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

    # Configure the magic_measurements table

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

    DESCRIPTION
        converts Utrecht magnetometer data files to magic_measurements files

    SYNTAX
        utrecht_magic.py [command line options]

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

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

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

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

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

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

    # parse data

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

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

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

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

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

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


# write out the data to MagIC data files
    pmag.magic_write(spec_file, SpecOuts, 'er_specimens')
    pmag.magic_write(samp_file, SampOuts, 'er_samples')
    pmag.magic_write(site_file, SiteOuts, 'er_sites')
    #    MagOuts = pmag.measurements_methods(MagRecs, noave)
    #    pmag.magic_write(meas_file, MagOuts, 'magic_measurements')
    pmag.magic_write(meas_file, MagRecs, 'magic_measurements')
    print("results put in ", meas_file)
    print("exit!")
    return True, meas_file
Exemplo n.º 14
0
def plotMT(datablock, s, num, units, norm):
    Ints = []
    for plotrec in datablock:
        Ints.append(plotrec[3])
    Ints.sort()
    T, M, Tv, recnum = [], [], [], 0
    Mex, Tex, Vdif = [], [], []
    recbak = []
    for rec in datablock:
        if rec[5] == 'g':
            if units == "T":
                T.append(rec[0] * 1e3)
                Tv.append(rec[0] * 1e3)
                if recnum > 0: Tv.append(rec[0] * 1e3)
            elif units == "U":
                T.append(rec[0])
                Tv.append(rec[0])
                if recnum > 0: Tv.append(rec[0])
            elif units == "K":
                T.append(rec[0] - 273)
                Tv.append(rec[0] - 273)
                if recnum > 0: Tv.append(rec[0] - 273)
            elif "T" in units and "K" in units:
                if rec[0] < 1.:
                    T.append(rec[0] * 1e3)
                    Tv.append(rec[0] * 1e3)
                else:
                    T.append(rec[0] - 273)
                    Tv.append(rec[0] - 273)
                    if recnum > 0: Tv.append(rec[0] - 273)
            else:
                T.append(rec[0])
                Tv.append(rec[0])
                if recnum > 0: Tv.append(rec[0])
            if norm == 1:
                M.append(rec[3] / Ints[-1])
            else:
                M.append(rec[3])
            if recnum > 0 and len(rec) > 0 and len(recbak) > 0:
                v = []
                if recbak[0] != rec[0]:
                    V0 = pmag.dir2cart([recbak[1], recbak[2], recbak[3]])
                    V1 = pmag.dir2cart([rec[1], rec[2], rec[3]])
                    for el in range(3):
                        v.append(abs(V1[el] - V0[el]))
                    vdir = pmag.cart2dir(v)
                    Vdif.append(vdir[2] / Ints[-1])  # append vector difference
                    Vdif.append(vdir[2] / Ints[-1])  #
            recbak = []
            for el in rec:
                recbak.append(el)
            delta = .02 * M[0]
            if num == 1:
                if recnum % 2 == 0:
                    plt.text(T[-1] + delta,
                             M[-1],
                             ' ' * 3 + str(int(datablock[recnum][0] - 273)) +
                             '$\degree$C',
                             fontsize=9)
            recnum += 1
        else:
            if rec[0] < 200: Tex.append(rec[0] * 1e3)
            if rec[0] >= 200: Tex.append(rec[0] - 273)
            Mex.append(rec[3] / Ints[-1])
            recnum += 1
        MTlist = T
        MTlisty = M
    if len(Mex) > 0 and len(Tex) > 0:
        plt.scatter(Tex, Mex, marker='d', color='k')
    if len(Vdif) > 0:
        Vdif.append(vdir[2] / Ints[-1])  #
        Vdif.append(0)
    Tv.append(Tv[-1])
    plt.plot(T, M)
    plt.plot(T, M, 'ro')
    if len(Tv) == len(Vdif) and norm == 1: plt.plot(Tv, Vdif, 'g-')
    if units == "T":
        plt.xlabel("Step (mT)")
    elif units == "K":
        plt.xlabel("Step (C)")
    elif units == "J":
        plt.xlabel("Step (J)")
    else:
        plt.xlabel("Step [mT,C]")
    if norm == 1: plt.ylabel("Fractional Magnetization")
    if norm == 0: plt.ylabel("Magnetization")
    plt.axvline(0, color='k')
    plt.axhline(0, color='k')
    tstring = s
    plt.title(tstring)
Exemplo n.º 15
0
def main():
    """
    NAME
        cart_dir.py
    
    DESCRIPTION
      converts cartesian coordinates to geomagnetic elements
    
    INPUT (COMMAND LINE ENTRY) 
           x1 x2  x3
        if only two columns, assumes magnitude of unity
    OUTPUT
           declination inclination magnitude
    
    SYNTAX
        cart_dir.py [command line options] [< filename]
    
    OPTIONS 
        -h prints help message and quits
        -i for interactive data entry
        -f FILE to specify input filename
        -F OFILE to specify output filename (also prints to screen)
    
    """
    ofile=""
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-F' in sys.argv:
        ind=sys.argv.index('-F')
        ofile=sys.argv[ind+1]
        outfile=open(ofile,'w')
    if '-i' in sys.argv:
        cont=1
        while cont==1:
            cart=[]
            try:
                ans=raw_input('X: [ctrl-D  to quit] ')
                cart.append(float(ans))
                ans=raw_input('Y: ')
                cart.append(float(ans))
                ans=raw_input('Z: ')
                cart.append(float(ans))
            except:
                print "\n Good-bye \n"
                sys.exit()
            dir= pmag.cart2dir(cart)  # send dir to dir2cart and spit out result
            print '%7.1f %7.1f %10.3e'%(dir[0],dir[1],dir[2])
    elif '-f' in sys.argv:
        ind=sys.argv.index('-f')
        file=sys.argv[ind+1]
        input=numpy.loadtxt(file) # read from a file
    else:
        input = numpy.loadtxt(sys.stdin,dtype=numpy.float)  # read from standard input
    dir=pmag.cart2dir(input)
    if len(dir.shape)==1:
        line=dir 
        print '%7.1f %7.1f %10.3e'%(line[0],line[1],line[2])
        if ofile!="":
           outstring='%7.1f %7.1f %10.8e\n' %(line[0],line[1],line[2]) 
           outfile.write(outstring)
    else: 
        for line in dir:
            print '%7.1f %7.1f %10.3e'%(line[0],line[1],line[2])
            if ofile!="":
               outstring='%7.1f %7.1f %10.8e\n' %(line[0],line[1],line[2]) 
               outfile.write(outstring)
Exemplo n.º 16
0
def main():
    """
    NAME
        igrf.py
    DESCRIPTION
        This program calculates igrf field values 
    using the routine of Malin and  Barraclough (1981) 
    based on d/igrfs from 1900 to 2010.
    between 1900 and 1000BCE, it uses CALS3K.4, ARCH3K.1 
    Prior to 1000BCE, it uses PFM9k or CALS10k-4b
    Calculates reference field vector at  specified location and time.
  
    SYNTAX
       igrf.py [-h] [-i] -f FILE  [< filename]
    OPTIONS:
       -h prints help message and quits
       -i for interactive data entry
       -f FILE  specify file name with input data 
       -fgh FILE specify file with custom field coefficients in format:  l m g h
       -F FILE  specify output file name
       -ages MIN MAX INCR: specify age minimum in years (+/- AD), maximum and increment, default is line by line
       -loc LAT LON;  specify location, default is line by line
       -alt ALT;  specify altitude in km, default is sealevel (0)
       -plt; make a plot of the time series
       -sav, saves plot and quits
       -fmt [pdf,jpg,eps,svg]  specify format for output figure  (default is svg)
       -mod [arch3k,cals3k,pfm9k,hfm10k,cals10k_2,shadif14k,cals10k] specify model for 3ka to 1900 AD, default is cals10k
             NB:  program uses IGRF12 for dates 1900 to 2015.
    
    INPUT FORMAT 
      interactive entry:
           date: decimal year
           alt:  altitude in km
           lat: positive north
           lon: positive east
       for file entry:
           space delimited string: date  alt   lat long
    OUTPUT  FORMAT
        Declination Inclination Intensity (nT) date alt lat long
    MODELS:  ARCH3K: (Korte et al., 2009);CALS3K (Korte & Contable, 2011); CALS10k (is .1b of Korte et al., 2011); PFM9K (Nilsson et al., 2014); HFM10k (is HFM.OL1.A1 of Constable et al., 2016); CALS10k_2 (is cals10k.2 of Constable et al., 2016), SHADIF14k (SHA.DIF.14K of Pavon-Carrasco et al., 2014).
    """
    plot,fmt=0,'svg'
    mod,alt,plt,lat,lon='cals10k',0,0,0,0
    if '-loc' in sys.argv:
        ind=sys.argv.index('-loc')
        lat=float(sys.argv[ind+1])
        lon=float(sys.argv[ind+2])
    if '-alt' in sys.argv:
        ind=sys.argv.index('-alt')
        alt=float(sys.argv[ind+1])
    if '-fmt' in sys.argv:
        ind=sys.argv.index('-fmt')
        fmt=sys.argv[ind+1]
    if len(sys.argv)!=0 and '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-mod' in sys.argv:
        ind=sys.argv.index('-mod')
        mod=sys.argv[ind+1]
    if '-fgh' in sys.argv:
        ind=sys.argv.index('-fgh')
        ghfile=sys.argv[ind+1]
        lmgh=numpy.loadtxt(ghfile)
        gh=[]
        lmgh=numpy.loadtxt(ghfile).transpose()
        gh.append(lmgh[2][0])
        for i in range(1,lmgh.shape[1]):
            gh.append(lmgh[2][i])
            gh.append(lmgh[3][i])
        mod='custom'
        inp=[[0,alt,lat,lon]]
    elif '-f' in sys.argv:
        ind=sys.argv.index('-f')
        file=sys.argv[ind+1]
        inp=numpy.loadtxt(file)
    elif '-i' in sys.argv:
        while 1:
          try:
            line=[]
            if mod!='custom':
                line.append(float(input("Decimal year: <cntrl-D to quit> ")))
            else:
                line.append(0)
            alt=input("Elevation in km [0] ")
            if alt=="":alt="0"
            line.append(float(alt))
            line.append(float(input("Latitude (positive north) ")))
            line.append(float(input("Longitude (positive east) ")))
            if mod=='':
                x,y,z,f=pmag.doigrf(line[3]%360.,line[2],line[1],line[0])
            elif mod=='custom':
                x,y,z,f = pmag.docustom(line[3]%360.,line[2], line[1], gh)
            else:
                x,y,z,f=pmag.doigrf(line[3]%360.,line[2],line[1],line[0],mod=mod)
            Dir=pmag.cart2dir((x,y,z))
            print('%8.2f %8.2f %8.0f'%(Dir[0],Dir[1],f))           
          except EOFError:
            print("\n Good-bye\n")
            sys.exit()
    elif '-ages' in sys.argv:
        ind=sys.argv.index('-ages')
        agemin=float(sys.argv[ind+1])
        agemax=float(sys.argv[ind+2])
        ageincr=float(sys.argv[ind+3])
        ages=numpy.arange(agemin,agemax,ageincr)
        lats=numpy.ones(len(ages))*lat
        lons=numpy.ones(len(ages))*lon
        alts=numpy.ones(len(ages))*alt
        inp=numpy.array([ages,alts,lats,lons]).transpose()
    else:
        inp=numpy.loadtxt(sys.stdin,dtype=numpy.float)
    if '-F' in sys.argv:
        ind=sys.argv.index('-F')
        outfile=sys.argv[ind+1]
        out=open(outfile,'w')
    else:outfile=""
    if '-sav' in sys.argv:plot=1
    if '-plt' in sys.argv:
        plt=1
        import matplotlib
        matplotlib.use("TkAgg")
        import pylab
        pylab.ion()
        Ages,Decs,Incs,Ints,VADMs=[],[],[],[],[]
    for line in inp:
        if mod!='custom':
            x,y,z,f=pmag.doigrf(line[3]%360.,line[2],line[1],line[0],mod=mod)
        else:
            x,y,z,f = pmag.docustom(line[3]%360.,line[2], line[1], gh)
        Dir=pmag.cart2dir((x,y,z))
        if outfile!="":
            out.write('%8.2f %8.2f %8.0f %7.1f %7.1f %7.1f %7.1f\n'%(Dir[0],Dir[1],f,line[0],line[1],line[2],line[3]))           
        elif plt:
            Ages.append(line[0])
            if Dir[0]>180: Dir[0]=Dir[0]-360.0
            Decs.append(Dir[0])
            Incs.append(Dir[1])
            Ints.append(f*1e-3)
            VADMs.append(pmag.b_vdm(f*1e-9,line[2])*1e-21)
        else:
            print('%8.2f %8.2f %8.0f %7.1f %7.1f %7.1f %7.1f'%(Dir[0],Dir[1],f,line[0],line[1],line[2],line[3]))           
    if plt:
        fig=pylab.figure(num=1,figsize=(7,9))
        fig.add_subplot(411)
        pylab.plot(Ages,Decs)
        pylab.ylabel('Declination ($^{\circ}$)')
        fig.add_subplot(412)
        pylab.plot(Ages,Incs)
        pylab.ylabel('Inclination ($^{\circ}$)')
        fig.add_subplot(413)
        pylab.plot(Ages,Ints)
        pylab.ylabel('Intensity ($\mu$T)')
        fig.add_subplot(414)
        pylab.plot(Ages,VADMs)
        pylab.ylabel('VADMs (ZAm$^2$)')
        pylab.xlabel('Ages')
        if plot==0:
            pylab.draw()
            ans=input("S[a]ve to save figure, <Return>  to quit  ")
            if ans=='a':
                pylab.savefig('igrf.'+fmt)
                print('Figure saved as: ','igrf.'+fmt)
        else: 
            pylab.savefig('igrf.'+fmt)
            print('Figure saved as: ','igrf.'+fmt)
        sys.exit()
Exemplo n.º 17
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
Exemplo n.º 18
0
def average_duplicates(duplicates):
    '''
    avarage replicate measurements.
    '''
    carts_s,carts_g,carts_t=[],[],[]
    for rec in duplicates:
        moment=float(rec['moment'])
        if 'dec_s' in list(rec.keys()) and 'inc_s' in list(rec.keys()):
            if rec['dec_s']!="" and rec['inc_s']!="":
                dec_s=float(rec['dec_s'])
                inc_s=float(rec['inc_s'])
                cart_s=pmag.dir2cart([dec_s,inc_s,moment])
                carts_s.append(cart_s)
        if 'dec_g' in list(rec.keys()) and 'inc_g' in list(rec.keys()):
            if rec['dec_g']!="" and rec['inc_g']!="":
                dec_g=float(rec['dec_g'])
                inc_g=float(rec['inc_g'])
                cart_g=pmag.dir2cart([dec_g,inc_g,moment])
                carts_g.append(cart_g)
        if 'dec_t' in list(rec.keys()) and 'inc_t' in list(rec.keys()):
            if rec['dec_t']!="" and rec['inc_t']!="":
                dec_t=float(rec['dec_t'])
                inc_t=float(rec['inc_t'])
                cart_t=pmag.dir2cart([dec_t,inc_t,moment])
                carts_t.append(cart_t)
    if len(carts_s)>0:
        carts=scipy.array(carts_s)
        x_mean=scipy.mean(carts[:,0])
        y_mean=scipy.mean(carts[:,1])
        z_mean=scipy.mean(carts[:,2])
        mean_dir=pmag.cart2dir([x_mean,y_mean,z_mean])
        mean_dec_s="%.2f"%mean_dir[0]
        mean_inc_s="%.2f"%mean_dir[1]
        mean_moment="%10.3e"%mean_dir[2]
    else:
        mean_dec_s,mean_inc_s="",""
    if len(carts_g)>0:
        carts=scipy.array(carts_g)
        x_mean=scipy.mean(carts[:,0])
        y_mean=scipy.mean(carts[:,1])
        z_mean=scipy.mean(carts[:,2])
        mean_dir=pmag.cart2dir([x_mean,y_mean,z_mean])
        mean_dec_g="%.2f"%mean_dir[0]
        mean_inc_g="%.2f"%mean_dir[1]
        mean_moment="%10.3e"%mean_dir[2]
    else:
        mean_dec_g,mean_inc_g="",""

    if len(carts_t)>0:
        carts=scipy.array(carts_t)
        x_mean=scipy.mean(carts[:,0])
        y_mean=scipy.mean(carts[:,1])
        z_mean=scipy.mean(carts[:,2])
        mean_dir=pmag.cart2dir([x_mean,y_mean,z_mean])
        mean_dec_t="%.2f"%mean_dir[0]
        mean_inc_t="%.2f"%mean_dir[1]
        mean_moment="%10.3e"%mean_dir[2]
    else:
        mean_dec_t,mean_inc_t="",""

    meanrec={}
    for key in list(duplicates[0].keys()):
        if key in ['dec_s','inc_s','dec_g','inc_g','dec_t','inc_t','moment']:
            continue
        else:
            meanrec[key]=duplicates[0][key]
    meanrec['dec_s']=mean_dec_s
    meanrec['dec_g']=mean_dec_g
    meanrec['dec_t']=mean_dec_t
    meanrec['inc_s']=mean_inc_s
    meanrec['inc_g']=mean_inc_g
    meanrec['inc_t']=mean_inc_t
    meanrec['moment']=mean_moment
    return meanrec
 def calculate_aniso_parameters(B,K):
 
     aniso_parameters={}
     S_bs=dot(B,K)
     
     # normalize by trace
     trace=S_bs[0]+S_bs[1]+S_bs[2]
     S_bs=S_bs/trace
     s1,s2,s3,s4,s5,s6=S_bs[0],S_bs[1],S_bs[2],S_bs[3],S_bs[4],S_bs[5]
     s_matrix=[[s1,s4,s6],[s4,s2,s5],[s6,s5,s3]]
     
     # calculate eigen vector,
     t,evectors=eig(s_matrix)
     # sort vectors
     t=list(t)
     t1=max(t)
     ix_1=t.index(t1)
     t3=min(t)
     ix_3=t.index(t3)
     for tt in range(3):
         if t[tt]!=t1 and t[tt]!=t3:
             t2=t[tt]
             ix_2=t.index(t2)
             
     v1=[evectors[0][ix_1],evectors[1][ix_1],evectors[2][ix_1]]
     v2=[evectors[0][ix_2],evectors[1][ix_2],evectors[2][ix_2]]
     v3=[evectors[0][ix_3],evectors[1][ix_3],evectors[2][ix_3]]
 
 
     DIR_v1=pmag.cart2dir(v1)
     DIR_v2=pmag.cart2dir(v2)
     DIR_v3=pmag.cart2dir(v3)
 
                         
     aniso_parameters['anisotropy_s1']="%f"%s1
     aniso_parameters['anisotropy_s2']="%f"%s2
     aniso_parameters['anisotropy_s3']="%f"%s3
     aniso_parameters['anisotropy_s4']="%f"%s4
     aniso_parameters['anisotropy_s5']="%f"%s5
     aniso_parameters['anisotropy_s6']="%f"%s6
     aniso_parameters['anisotropy_degree']="%f"%(t1/t3)
     aniso_parameters['anisotropy_t1']="%f"%t1
     aniso_parameters['anisotropy_t2']="%f"%t2
     aniso_parameters['anisotropy_t3']="%f"%t3
     aniso_parameters['anisotropy_v1_dec']="%.1f"%DIR_v1[0]
     aniso_parameters['anisotropy_v1_inc']="%.1f"%DIR_v1[1]
     aniso_parameters['anisotropy_v2_dec']="%.1f"%DIR_v2[0]
     aniso_parameters['anisotropy_v2_inc']="%.1f"%DIR_v2[1]
     aniso_parameters['anisotropy_v3_dec']="%.1f"%DIR_v3[0]
     aniso_parameters['anisotropy_v3_inc']="%.1f"%DIR_v3[1]
 
     # modified from pmagpy:
     if len(K)/3==9 or len(K)/3==6 or len(K)/3==15:
         n_pos=len(K)/3
         tmpH = Matrices[n_pos]['tmpH']
         a=s_matrix
         S=0.
         comp=zeros((n_pos*3),'f')
         for i in range(n_pos):
             for j in range(3):
                 index=i*3+j
                 compare=a[j][0]*tmpH[i][0]+a[j][1]*tmpH[i][1]+a[j][2]*tmpH[i][2]
                 comp[index]=compare
         for i in range(n_pos*3):
             d=K[i]/trace - comp[i] # del values
             S+=d*d
         nf=float(n_pos*3-6) # number of degrees of freedom
         if S >0: 
             sigma=math.sqrt(S/nf)
         hpars=pmag.dohext(nf,sigma,[s1,s2,s3,s4,s5,s6])
         
         aniso_parameters['anisotropy_sigma']="%f"%sigma
         aniso_parameters['anisotropy_ftest']="%f"%hpars["F"]
         aniso_parameters['anisotropy_ftest12']="%f"%hpars["F12"]
         aniso_parameters['anisotropy_ftest23']="%f"%hpars["F23"]
         aniso_parameters['result_description']="Critical F: %s"%(hpars['F_crit'])
         aniso_parameters['anisotropy_F_crit']="%f"%float(hpars['F_crit'])
         aniso_parameters['anisotropy_n']=n_pos
         
     return(aniso_parameters)
Exemplo n.º 20
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
Exemplo n.º 21
0
def main(command_line=True, **kwargs):
    """
    NAME
        utrecht_magic.py

    DESCRIPTION
        converts Utrecht magnetometer data files to magic_measurements files

    SYNTAX
        utrecht_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -f FILE: specify  input file, or
        -F FILE: specify output file, default is magic_measurements.txt
        -Fsa: specify er_samples format file for appending, default is new er_samples.txt (Not working yet)
        -loc LOCNAME : specify location/study name
        -site SITENAME : specify site name
        -lat latitude of samples
        -lon longitude of samples
        -A: don't average replicate measurements
        -mcd [SO-MAG,SO-SUN,SO-SIGHT...] supply how these samples were oriented

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

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

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

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

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

    ErSampRec = {}

    # parse data

    # Open up the Utrecht file and read the header information
    print 'mag_file in utrecht_file', mag_file
    data = open(mag_file, 'rU')
    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
    line = data.readline()
    while line != '"END"' :
        line_items = line.split(',')
        sample_name=line_items[0]
        sample_name=sample_name.replace("\"","")
        print "sample_name=", sample_name
        free_string=line_items[1]
        free_string=free_string.replace("\"","")
        print "free_string=", free_string
        dec=line_items[2]
        print "dec=", dec
        inc=line_items[3]
        print "inc=", inc
        volume=float(line_items[4])
        volume=volume * 1e-6 # enter volume in cm^3, convert to m^3
        print "volume=", volume
        bed_plane=line_items[5]
        print "bed_plane=", bed_plane
        bed_tilt=line_items[6]
        print "bed_tilt=", bed_tilt

        # Configure et er_samples table
        ErSampRec['er_sample_name'] = sample_name
        ErSampRec['sample_azimuth'] = dec
        ErSampRec['sample_dip'] = inc
        ErSampRec['sample_bed_dip_direction'] = bed_plane
        ErSampRec['sample_bed_tilt'] = bed_tilt
        ErSampRec['sample_lat'] = sample_lat
        ErSampRec['sample_lon'] = sample_lon
        ErSampRec['magic_method_codes'] = meth_code
        ErSampRec['er_location_name'] = er_location_name
        ErSampRec['er_site_name'] = er_site_name
        ErSampRec['er_citation_names'] = 'This study'
        SampOuts.append(ErSampRec.copy())
        
        line = data.readline()
        line = line.rstrip("\n")
        items = line.split(",")
    #    exit()
        while line != '9999' :
            print line
            step=items[0]
            step=step.split('.')
            step_value=step[0]
            step_type = ""
            if len(step) == 2:
                step_type=step[1]
            A=float(items[1])
            B=float(items[2])
            C=float(items[3])
#  convert to MagIC coordinates
            Z=-A
            X=-B
            Y=C
            cart = np.array([X, Y, Z]).transpose()
            direction = pmag.cart2dir(cart).transpose()
            measurement_dec = direction[0]
            measurement_inc = direction[1]
            measurement_magn_moment = direction[2] * 1.0e-12 # the data are in pico-Am^2 - this converts to Am^2
            measurement_magn_volume = direction[2] * 1.0e-12 / volume # data volume normalized - converted to A/m
            print "measurement_magn_moment=", measurement_magn_moment
            print "measurement_magn_volume=", measurement_magn_volume
            error = items[4]
            date=items[5]
            date=date.strip('"')
            date=date.split("-")
            print date
            time=items[6]
            time=time.strip('"')
            time=time.split(":")
            print time
            date_time = date[0] + ":" + date[1] + ":" + date[2] + ":" + time[0] + ":" + time[1] + ":" + "0.0"   
            print date_time

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

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

# write out the data to MagIC data files
    pmag.magic_write(samp_file, SampOuts, 'er_samples')
    print "sample orientations put in ", samp_file
#    MagOuts = pmag.measurements_methods(MagRecs, noave)
#    pmag.magic_write(meas_file, MagOuts, 'magic_measurements')
    pmag.magic_write(meas_file, MagRecs, 'magic_measurements')
    print "results put in ", meas_file
    print "exit!"
    return True, meas_file
Exemplo n.º 22
0
def main(command_line=True, **kwargs):
    """
    NAME
        pmd_magic.py
 
    DESCRIPTION
        converts PMD (Enkin)  format files to magic_measurements format files

    SYNTAX
        pmd_magic.py [command line options]

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

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

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

    #
    # get command line arguments
    #

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

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

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

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

    SYNTAX
        jr6_jr6_magic.py [command line options]

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

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

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

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

    ErSampRec,ErSiteRec={},{}

    # parse data

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

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

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

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

    # Configure the er_sample table        

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

    # Configure the magic_measurements table

    for rowNum, row in data.iterrows():
        MagRec={}
#        MagRec['measurement_description']='Date: '+date
        MagRec["er_citation_names"]="This study"
        MagRec['er_location_name']=er_location_name
        MagRec['er_site_name']=row['er_site_name']
        MagRec['er_sample_name']=row['er_sample_name']
        MagRec['magic_software_packages']=version_num
        MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
        MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
        MagRec["measurement_flag"]='g'
        MagRec["measurement_standard"]='u'
        MagRec["measurement_number"]='1'
        MagRec["er_specimen_name"]=row['er_specimen_name']
        MagRec["treatment_ac_field"]='0'
        if row['step'] == 'NRM':
            meas_type="LT-NO"
        elif row['step'][0:2] == 'AD':
            meas_type="LT-AF-Z"
            treat=float(row['step'][2:])
            MagRec["treatment_ac_field"]='%8.3e' %(treat*1e-3) # convert from mT to tesla
        elif row['step'][0] == 'TD':
            meas_type="LT-T-Z"
            treat=float(row['step'][2:])
            MagRec["treatment_temp"]='%8.3e' % (treat+273.) # temp in kelvin
        else: # need to add IRM, and ARM options
            print "measurement type unknown", row['step']
            return False, "measurement type unknown"
        MagRec["measurement_magn_moment"]=str(row['measurement_magn_moment'])
        MagRec["measurement_magn_volume"]=str(row['measurement_magn_volume'])
        MagRec["measurement_dec"]=str(row['measurement_dec'])
        MagRec["measurement_inc"]=str(row['measurement_inc'])
        MagRec['magic_method_codes']=meas_type
        MagRecs.append(MagRec.copy())
    pmag.magic_write(samp_file,SampOuts,'er_samples')
    print "sample orientations put in ",samp_file
    MagOuts=pmag.measurements_methods(MagRecs,noave)
    pmag.magic_write(meas_file,MagOuts,'magic_measurements')
    print "results put in ",meas_file
    print "exit!"
    return True, meas_file
Exemplo n.º 25
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
Exemplo n.º 26
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)
Exemplo n.º 27
0
    def calculate_aniso_parameters(B, K):

        aniso_parameters = {}
        S_bs = dot(B, K)

        # normalize by trace
        trace = S_bs[0] + S_bs[1] + S_bs[2]
        S_bs = S_bs / trace
        s1, s2, s3, s4, s5, s6 = S_bs[0], S_bs[1], S_bs[2], S_bs[3], S_bs[
            4], S_bs[5]
        s_matrix = [[s1, s4, s6], [s4, s2, s5], [s6, s5, s3]]

        # calculate eigen vector,
        t, evectors = eig(s_matrix)
        # sort vectors
        t = list(t)
        t1 = max(t)
        ix_1 = t.index(t1)
        t3 = min(t)
        ix_3 = t.index(t3)
        for tt in range(3):
            if t[tt] != t1 and t[tt] != t3:
                t2 = t[tt]
                ix_2 = t.index(t2)

        v1 = [evectors[0][ix_1], evectors[1][ix_1], evectors[2][ix_1]]
        v2 = [evectors[0][ix_2], evectors[1][ix_2], evectors[2][ix_2]]
        v3 = [evectors[0][ix_3], evectors[1][ix_3], evectors[2][ix_3]]

        DIR_v1 = pmag.cart2dir(v1)
        DIR_v2 = pmag.cart2dir(v2)
        DIR_v3 = pmag.cart2dir(v3)

        aniso_parameters['anisotropy_s1'] = "%f" % s1
        aniso_parameters['anisotropy_s2'] = "%f" % s2
        aniso_parameters['anisotropy_s3'] = "%f" % s3
        aniso_parameters['anisotropy_s4'] = "%f" % s4
        aniso_parameters['anisotropy_s5'] = "%f" % s5
        aniso_parameters['anisotropy_s6'] = "%f" % s6
        aniso_parameters['anisotropy_degree'] = "%f" % (t1 / t3)
        aniso_parameters['anisotropy_t1'] = "%f" % t1
        aniso_parameters['anisotropy_t2'] = "%f" % t2
        aniso_parameters['anisotropy_t3'] = "%f" % t3
        aniso_parameters['anisotropy_v1_dec'] = "%.1f" % DIR_v1[0]
        aniso_parameters['anisotropy_v1_inc'] = "%.1f" % DIR_v1[1]
        aniso_parameters['anisotropy_v2_dec'] = "%.1f" % DIR_v2[0]
        aniso_parameters['anisotropy_v2_inc'] = "%.1f" % DIR_v2[1]
        aniso_parameters['anisotropy_v3_dec'] = "%.1f" % DIR_v3[0]
        aniso_parameters['anisotropy_v3_inc'] = "%.1f" % DIR_v3[1]

        # modified from pmagpy:
        if len(K) / 3 == 9 or len(K) / 3 == 6 or len(K) / 3 == 15:
            n_pos = len(K) / 3
            tmpH = Matrices[n_pos]['tmpH']
            a = s_matrix
            S = 0.
            comp = zeros((n_pos * 3), 'f')
            for i in range(n_pos):
                for j in range(3):
                    index = i * 3 + j
                    compare = a[j][0] * tmpH[i][0] + a[j][1] * tmpH[i][1] + a[
                        j][2] * tmpH[i][2]
                    comp[index] = compare
            for i in range(n_pos * 3):
                d = K[i] / trace - comp[i]  # del values
                S += d * d
            nf = float(n_pos * 3 - 6)  # number of degrees of freedom
            if S > 0:
                sigma = math.sqrt(S / nf)
            hpars = pmag.dohext(nf, sigma, [s1, s2, s3, s4, s5, s6])

            aniso_parameters['anisotropy_sigma'] = "%f" % sigma
            aniso_parameters['anisotropy_ftest'] = "%f" % hpars["F"]
            aniso_parameters['anisotropy_ftest12'] = "%f" % hpars["F12"]
            aniso_parameters['anisotropy_ftest23'] = "%f" % hpars["F23"]
            aniso_parameters['result_description'] = "Critical F: %s" % (
                hpars['F_crit'])
            aniso_parameters['anisotropy_F_crit'] = "%f" % float(
                hpars['F_crit'])
            aniso_parameters['anisotropy_n'] = n_pos

        return (aniso_parameters)
Exemplo n.º 28
0
def main(command_line=True, **kwargs):
    """
    NAME
        iodp_jr6_magic.py

    DESCRIPTION
        converts shipboard .jr6 format files to magic_measurements format files

    SYNTAX
        iodp_jr6_magic.py [command line options]

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

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

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


# initialize some stuff

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

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

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

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

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

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

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

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

        else:
            print('sample name not found: ', row['specname'])
    MagOuts = pmag.measurements_methods(MagRecs, noave)
    file_created, error_message = pmag.magic_write(meas_file, MagOuts,
                                                   'magic_measurements')
    if file_created:
        return True, meas_file
    else:
        return False, 'Results not written to file'
Exemplo n.º 29
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
Exemplo n.º 30
0
def main(command_line=True, **kwargs):
    """
    NAME
        utrecht_magic.py

    DESCRIPTION
        converts Utrecht magnetometer data files to magic_measurements files

    SYNTAX
        utrecht_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -f FILE: specify  input file, or
        -F FILE: specify output file, default is magic_measurements.txt
        -Fsa: specify er_samples format file for appending, default is new er_samples.txt (Not working yet)
        -loc LOCNAME : specify location/study name
        -site SITENAME : specify site name
        -lat latitude of samples
        -lon longitude of samples
        -A: don't average replicate measurements
        -mcd [SO-MAG,SO-SUN,SO-SIGHT...] supply how these samples were oriented

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

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

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

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

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

    ErSampRec = {}

    # parse data

    # Open up the Utrecht file and read the header information
    print 'mag_file in utrecht_file', mag_file
    data = open(mag_file, 'rU')
    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
    line = data.readline()
    while line != '"END"':
        line_items = line.split(',')
        sample_name = line_items[0]
        sample_name = sample_name.replace("\"", "")
        print "sample_name=", sample_name
        free_string = line_items[1]
        free_string = free_string.replace("\"", "")
        print "free_string=", free_string
        dec = line_items[2]
        print "dec=", dec
        inc = line_items[3]
        print "inc=", inc
        volume = float(line_items[4])
        volume = volume * 1e-6  # enter volume in cm^3, convert to m^3
        print "volume=", volume
        bed_plane = line_items[5]
        print "bed_plane=", bed_plane
        bed_tilt = line_items[6]
        print "bed_tilt=", bed_tilt

        # Configure et er_samples table
        ErSampRec['er_sample_name'] = sample_name
        ErSampRec['sample_azimuth'] = dec
        ErSampRec['sample_dip'] = inc
        ErSampRec['sample_bed_dip_direction'] = bed_plane
        ErSampRec['sample_bed_tilt'] = bed_tilt
        ErSampRec['sample_lat'] = sample_lat
        ErSampRec['sample_lon'] = sample_lon
        ErSampRec['magic_method_codes'] = meth_code
        ErSampRec['er_location_name'] = er_location_name
        ErSampRec['er_site_name'] = er_site_name
        ErSampRec['er_citation_names'] = 'This study'
        SampOuts.append(ErSampRec.copy())

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

            MagRec = {}

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

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


# write out the data to MagIC data files
    pmag.magic_write(samp_file, SampOuts, 'er_samples')
    print "sample orientations put in ", samp_file
    #    MagOuts = pmag.measurements_methods(MagRecs, noave)
    #    pmag.magic_write(meas_file, MagOuts, 'magic_measurements')
    pmag.magic_write(meas_file, MagRecs, 'magic_measurements')
    print "results put in ", meas_file
    print "exit!"
    return True, meas_file
Exemplo n.º 31
0
def main(command_line=True, **kwargs):
    """
    NAME
        jr6_jr6_magic.py
 
    DESCRIPTION
        converts JR6 .jr6 format files to magic_measurements format files

    SYNTAX
        jr6_jr6_magic.py [command line options]

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

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

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

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

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

    ErSampRec, ErSiteRec = {}, {}

    # parse data

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

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

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

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

    # Configure the er_sample table

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

    # Configure the magic_measurements table

    for rowNum, row in data.iterrows():
        MagRec = {}
        #        MagRec['measurement_description']='Date: '+date
        MagRec["er_citation_names"] = "This study"
        MagRec['er_location_name'] = er_location_name
        MagRec['er_site_name'] = row['er_site_name']
        MagRec['er_sample_name'] = row['er_sample_name']
        MagRec['magic_software_packages'] = version_num
        MagRec["treatment_temp"] = '%8.3e' % (273)  # room temp in kelvin
        MagRec["measurement_temp"] = '%8.3e' % (273)  # room temp in kelvin
        MagRec["measurement_flag"] = 'g'
        MagRec["measurement_standard"] = 'u'
        MagRec["measurement_number"] = '1'
        MagRec["er_specimen_name"] = row['er_specimen_name']
        MagRec["treatment_ac_field"] = '0'
        if row['step'] == 'NRM':
            meas_type = "LT-NO"
        elif row['step'][0:2] == 'AD':
            meas_type = "LT-AF-Z"
            treat = float(row['step'][2:])
            MagRec["treatment_ac_field"] = '%8.3e' % (
                treat * 1e-3)  # convert from mT to tesla
        elif row['step'][0] == 'TD':
            meas_type = "LT-T-Z"
            treat = float(row['step'][2:])
            MagRec["treatment_temp"] = '%8.3e' % (treat + 273.
                                                  )  # temp in kelvin
        else:  # need to add IRM, and ARM options
            print("measurement type unknown", row['step'])
            return False, "measurement type unknown"
        MagRec["measurement_magn_moment"] = str(row['measurement_magn_moment'])
        MagRec["measurement_magn_volume"] = str(row['measurement_magn_volume'])
        MagRec["measurement_dec"] = str(row['measurement_dec'])
        MagRec["measurement_inc"] = str(row['measurement_inc'])
        MagRec['magic_method_codes'] = meas_type
        MagRecs.append(MagRec.copy())
    pmag.magic_write(samp_file, SampOuts, 'er_samples')
    print("sample orientations put in ", samp_file)
    MagOuts = pmag.measurements_methods(MagRecs, noave)
    pmag.magic_write(meas_file, MagOuts, 'magic_measurements')
    print("results put in ", meas_file)
    print("exit!")
    return True, meas_file
Exemplo n.º 32
0
def main():
    """
    NAME
        igrf.py
    DESCRIPTION
        This program calculates igrf field values 
    using the routine of Malin and  Barraclough (1981) 
    based on d/igrfs from 1900 to 2010.
    between 1900 and 1000BCE, it uses CALS3K.4, ARCH3K.1 , or PFM9K
    Prior to 1000BCE, it uses CALS10k-4b
    Calculates reference field vector at  specified location and time.
  
    SYNTAX
       igrf.py [-h] [-i] -f FILE  [< filename]
    OPTIONS:
       -h prints help message and quits
       -i for interactive data entry
       -f FILE  specify file name with input data 
       -F FILE  specify output file name
       -ages MIN MAX INCR: specify age minimum in years (+/- AD), maximum and increment, default is line by line
       -loc LAT LON;  specify location, default is line by line
       -alt ALT;  specify altitude in km, default is sealevel (0)
       -plt; make a plot of the time series
       -sav, saves plot and quits
       -fmt [pdf,jpg,eps,svg]  specify format for output figure  (default is svg)
       -mod [arch3k,cals3k,pfm9k] specify model for 3ka to 1900 AD, default is cals3k.4b
             NB:  program uses IGRF12 for dates 1900 to 2015.
    
    INPUT FORMAT 
      interactive entry:
           date: decimal year
           alt:  altitude in km
           lat: positive north
           lon: positive east
       for file entry:
           space delimited string: date  alt   lat long
    OUTPUT  FORMAT
        Declination Inclination Intensity (nT) date alt lat long
    """
    plot,fmt=0,'svg'
    plt=0
    if '-fmt' in sys.argv:
        ind=sys.argv.index('-fmt')
        fmt=sys.argv[ind+1]
    if len(sys.argv)!=0 and '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-mod' in sys.argv:
        ind=sys.argv.index('-mod')
        mod=sys.argv[ind+1]
    else: mod='cals3k'
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        file=sys.argv[ind+1]
        input=numpy.loadtxt(file)
    elif '-i' in sys.argv:
        while 1:
          try:
            line=[]
            line.append(float(raw_input("Decimal year: <cntrl-D to quit> ")))
            alt=raw_input("Elevation in km [0] ")
            if alt=="":alt="0"
            line.append(float(alt))
            line.append(float(raw_input("Latitude (positive north) ")))
            line.append(float(raw_input("Longitude (positive east) ")))
            if mod=='':
                x,y,z,f=pmag.doigrf(line[3]%360.,line[2],line[1],line[0])
            else:
                x,y,z,f=pmag.doigrf(line[3]%360.,line[2],line[1],line[0],mod=mod)
            Dir=pmag.cart2dir((x,y,z))
            print '%8.2f %8.2f %8.0f'%(Dir[0],Dir[1],f)           
          except EOFError:
            print "\n Good-bye\n"
            sys.exit()
    elif '-ages' in sys.argv:
        ind=sys.argv.index('-ages')
        agemin=float(sys.argv[ind+1])
        agemax=float(sys.argv[ind+2])
        ageincr=float(sys.argv[ind+3])
        if '-loc' in sys.argv:
            ind=sys.argv.index('-loc')
            lat=float(sys.argv[ind+1])
            lon=float(sys.argv[ind+2])
        else: 
            print "must specify lat/lon if using age range option"
            sys.exit()
        if '-alt' in sys.argv:
            ind=sys.argv.index('-alt')
            alt=float(sys.argv[ind+1])
        else: alt=0
        ages=numpy.arange(agemin,agemax,ageincr)
        lats=numpy.ones(len(ages))*lat
        lons=numpy.ones(len(ages))*lon
        alts=numpy.ones(len(ages))*alt
        input=numpy.array([ages,alts,lats,lons]).transpose()
    else:
        input=numpy.loadtxt(sys.stdin,dtype=numpy.float)
    if '-F' in sys.argv:
        ind=sys.argv.index('-F')
        outfile=sys.argv[ind+1]
        out=open(outfile,'w')
    else:outfile=""
    if '-sav' in sys.argv:plot=1
    if '-plt' in sys.argv:
        plt=1
        import matplotlib
        matplotlib.use("TkAgg")
        import pylab
        pylab.ion()
        Ages,Decs,Incs,Ints,VADMs=[],[],[],[],[]
    for line in input:
        if mod=='':
            x,y,z,f=pmag.doigrf(line[3]%360.,line[2],line[1],line[0])
        else:
            x,y,z,f=pmag.doigrf(line[3]%360.,line[2],line[1],line[0],mod=mod)
        Dir=pmag.cart2dir((x,y,z))
        if outfile!="":
            out.write('%8.2f %8.2f %8.0f %7.1f %7.1f %7.1f %7.1f\n'%(Dir[0],Dir[1],f,line[0],line[1],line[2],line[3]))           
        elif plt:
            Ages.append(line[0])
            if Dir[0]>180: Dir[0]=Dir[0]-360.0
            Decs.append(Dir[0])
            Incs.append(Dir[1])
            Ints.append(f*1e-3)
            VADMs.append(pmag.b_vdm(f*1e-9,line[2])*1e-21)
        else:
            print '%8.2f %8.2f %8.0f %7.1f %7.1f %7.1f %7.1f'%(Dir[0],Dir[1],f,line[0],line[1],line[2],line[3])           
    if plt:
        fig=pylab.figure(num=1,figsize=(7,9))
        fig.add_subplot(411)
        pylab.plot(Ages,Decs)
        pylab.ylabel('Declination ($^{\circ}$)')
        fig.add_subplot(412)
        pylab.plot(Ages,Incs)
        pylab.ylabel('Inclination ($^{\circ}$)')
        fig.add_subplot(413)
        pylab.plot(Ages,Ints)
        pylab.ylabel('Intensity ($\mu$T)')
        fig.add_subplot(414)
        pylab.plot(Ages,VADMs)
        pylab.ylabel('VADMs (ZAm$^2$)')
        pylab.xlabel('Ages')
        if plot==0:
            pylab.draw()
            ans=raw_input("S[a]ve to save figure, <Return>  to quit  ")
            if ans=='a':
                pylab.savefig('igrf.'+fmt)
                print 'Figure saved as: ','igrf.'+fmt
        else: 
            pylab.savefig('igrf.'+fmt)
            print 'Figure saved as: ','igrf.'+fmt
        sys.exit()
Exemplo n.º 33
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