示例#1
0
def pca(data, selected_dots, system, approx_mode):
    print(data, selected_dots, system, approx_mode)
    df = data
    n = len(selected_dots)
    col_D = "Dg"
    col_I = "Ig"

    if system == "stratigraphic":
        col_D = "Ds"
        col_I = "Is"

    first_col_name = "T"
    if "M" in df.columns:
        first_col_name = "M"

    df["quality"] = "g"
    data_to_pca = df[[first_col_name, col_D, col_I, "MAG", "quality"]].values.tolist()
    answer = pmag.domean(data_to_pca, 0, n-1, approx_mode)
    return answer["specimen_inc"], answer["specimen_dec"], answer["specimen_mad"]
示例#2
0
def main():
    """
    NAME
        zeq_magic.py

    DESCRIPTION
        reads in magic_measurements formatted file, makes plots of remanence decay
        during demagnetization experiments.  Reads in prior interpretations saved in
        a pmag_specimens formatted file [and  allows re-interpretations of best-fit lines
        and planes and saves (revised or new) interpretations in a pmag_specimens file.
        interpretations are saved in the coordinate system used. Also allows judicious editting of
        measurements to eliminate "bad" measurements.  These are marked as such in the magic_measurements
        input file.  they are NOT deleted, just ignored. ] Bracketed part not yet implemented

    SYNTAX
        zeq_magic.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f  MEASFILE: sets measurements format input file, default: measurements.txt
        -fsp SPECFILE: sets specimens format file with prior interpreations, default: specimens.txt
        -fsa SAMPFILE: sets samples format file sample=>site information, default: samples.txt
        -fsi SITEFILE: sets sites format file with site=>location informationprior interpreations, default: samples.txt
        -Fp PLTFILE: sets filename for saved plot, default is name_type.fmt (where type is zijd, eqarea or decay curve)
        -crd [s,g,t]: sets coordinate system,  g=geographic, t=tilt adjusted, default: specimen coordinate system
        -spc SPEC  plots single specimen SPEC, saves plot with specified format
              with optional -dir settings and quits
        -dir [L,P,F][beg][end]: sets calculation type for principal component analysis, default is none
             beg: starting step for PCA calculation
             end: ending step for PCA calculation
             [L,P,F]: calculation type for line, plane or fisher mean
             must be used with -spc option
        -fmt FMT: set format of saved plot [png,svg,jpg]
        -A:  suppresses averaging of  replicate measurements, default is to average
        -sav: saves all plots without review
    SCREEN OUTPUT:
        Specimen, N, a95, StepMin, StepMax, Dec, Inc, calculation type

    """
    # initialize some variables
    doave, e, b = 1, 0, 0  # average replicates, initial end and beginning step
    intlist = ['magn_moment', 'magn_volume', 'magn_mass', 'magnitude']
    plots, coord = 0, 's'
    noorient = 0
    version_num = pmag.get_version()
    verbose = pmagplotlib.verbose
    calculation_type, fmt = "", "svg"
    user, spec_keys, locname = "", [], ''
    geo, tilt, ask = 0, 0, 0
    PriorRecs = []  # empty list for prior interpretations
    backup = 0
    specimen = ""  # can skip everything and just plot one specimen with bounds e,b
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    dir_path = pmag.get_named_arg_from_sys("-WD", default_val=os.getcwd())
    meas_file = pmag.get_named_arg_from_sys("-f",
                                            default_val="measurements.txt")
    spec_file = pmag.get_named_arg_from_sys("-fsp",
                                            default_val="specimens.txt")
    samp_file = pmag.get_named_arg_from_sys("-fsa", default_val="samples.txt")
    site_file = pmag.get_named_arg_from_sys("-fsi", default_val="sites.txt")
    #meas_file = os.path.join(dir_path, meas_file)
    #spec_file = os.path.join(dir_path, spec_file)
    #samp_file = os.path.join(dir_path, samp_file)
    #site_file = os.path.join(dir_path, site_file)
    plot_file = pmag.get_named_arg_from_sys("-Fp", default_val="")
    crd = pmag.get_named_arg_from_sys("-crd", default_val="s")
    if crd == "s":
        coord = "-1"
    elif crd == "t":
        coord = "100"
    else:
        coord = "0"
    fmt = pmag.get_named_arg_from_sys("-fmt", "svg")
    specimen = pmag.get_named_arg_from_sys("-spc", default_val="")
    beg_pca, end_pca = "", ""
    if '-dir' in sys.argv:
        ind = sys.argv.index('-dir')
        direction_type = sys.argv[ind + 1]
        beg_pca = int(sys.argv[ind + 2])
        end_pca = int(sys.argv[ind + 3])
        if direction_type == 'L':
            calculation_type = 'DE-BFL'
        if direction_type == 'P':
            calculation_type = 'DE-BFP'
        if direction_type == 'F':
            calculation_type = 'DE-FM'
    if '-A' in sys.argv:
        doave = 0
    if '-sav' in sys.argv:
        plots, verbose = 1, 0
    #
    first_save = 1
    fnames = {
        'measurements': meas_file,
        'specimens': spec_file,
        'samples': samp_file,
        'sites': site_file
    }
    contribution = nb.Contribution(
        dir_path,
        custom_filenames=fnames,
        read_tables=['measurements', 'specimens', 'samples', 'sites'])
    #
    #   import  specimens

    specimen_cols = [
        'analysts', 'aniso_ftest', 'aniso_ftest12', 'aniso_ftest23', 'aniso_s',
        'aniso_s_mean', 'aniso_s_n_measurements', 'aniso_s_sigma',
        'aniso_s_unit', 'aniso_tilt_correction', 'aniso_type', 'aniso_v1',
        'aniso_v2', 'aniso_v3', 'citations', 'description', 'dir_alpha95',
        'dir_comp', 'dir_dec', 'dir_inc', 'dir_mad_free', 'dir_n_measurements',
        'dir_tilt_correction', 'experiments', 'geologic_classes',
        'geologic_types', 'hyst_bc', 'hyst_bcr', 'hyst_mr_moment',
        'hyst_ms_moment', 'int_abs', 'int_b', 'int_b_beta', 'int_b_sigma',
        'int_corr', 'int_dang', 'int_drats', 'int_f', 'int_fvds', 'int_gamma',
        'int_mad_free', 'int_md', 'int_n_measurements', 'int_n_ptrm', 'int_q',
        'int_rsc', 'int_treat_dc_field', 'lithologies', 'meas_step_max',
        'meas_step_min', 'meas_step_unit', 'method_codes', 'sample',
        'software_packages', 'specimen'
    ]
    if 'specimens' in contribution.tables:
        #        contribution.propagate_name_down('sample','measurements')
        spec_container = contribution.tables['specimens']
        prior_spec_data = spec_container.get_records_for_code(
            'LP-DIR', strict_match=False
        )  # look up all prior directional interpretations
#
#  tie sample names to measurement data
#
    else:
        spec_container, prior_spec_data = None, []

#
#   import samples  for orientation info
#
    if 'samples' in contribution.tables:
        #        contribution.propagate_name_down('site','measurements')
        contribution.propagate_cols(
            col_names=['azimuth', 'dip', 'orientation_flag'],
            target_df_name='measurements',
            source_df_name='samples')
#
# define figure numbers for equal area, zijderveld,
#  and intensity vs. demagnetiztion step respectively
#
    ZED = {}
    ZED['eqarea'], ZED['zijd'], ZED['demag'] = 1, 2, 3
    pmagplotlib.plot_init(ZED['eqarea'], 6, 6)
    pmagplotlib.plot_init(ZED['zijd'], 6, 6)
    pmagplotlib.plot_init(ZED['demag'], 6, 6)
    #    save_pca=0
    angle, direction_type, setangle = "", "", 0
    #   create measurement dataframe
    #
    meas_container = contribution.tables['measurements']
    meas_data = meas_container.df
    #
    meas_data = meas_data[meas_data['method_codes'].str.contains(
        'LT-NO|LT-AF-Z|LT-T-Z|LT-M-Z') == True]  # fish out steps for plotting
    meas_data = meas_data[meas_data['method_codes'].str.contains(
        'AN|ARM|LP-TRM|LP-PI-ARM') == False]  # strip out unwanted experiments
    intensity_types = [
        col_name for col_name in meas_data.columns if col_name in intlist
    ]
    # plot first intensity method found - normalized to initial value anyway -
    # doesn't matter which used
    int_key = intensity_types[0]
    # get all the non-null intensity records of the same type
    meas_data = meas_data[meas_data[int_key].notnull()]
    if 'flag' not in meas_data.columns:
        meas_data['flag'] = 'g'  # set the default flag to good
# need to treat LP-NO specially  for af data, treatment should be zero,
# otherwise 273.
    meas_data['treatment'] = meas_data['treat_ac_field'].where(
        cond=meas_data['treat_ac_field'] != '0', other=meas_data['treat_temp'])
    meas_data['ZI'] = 1  # initialize these to one
    meas_data['instrument_codes'] = ""  # initialize these to blank
    #   for unusual case of microwave power....
    if 'treat_mw_power' in meas_data.columns:
        meas_data.loc[
            meas_data.treat_mw_power != 0,
            'treatment'] = meas_data.treat_mw_power * meas_data.treat_mw_time
#
# get list of unique specimen names from measurement data
#
# this is a list of all the specimen names
    specimen_names = meas_data.specimen.unique()
    specimen_names = specimen_names.tolist()
    specimen_names.sort()
    #
    # set up new DataFrame for this sessions specimen interpretations
    #
    data_container = nb.MagicDataFrame(dtype='specimens',
                                       columns=specimen_cols)
    # this is for interpretations from this session
    current_spec_data = data_container.df
    locname = 'LookItUp'
    if specimen == "":
        k = 0
    else:
        k = specimen_names.index(specimen)
    # let's look at the data now
    while k < len(specimen_names):
        # set the current specimen for plotting
        this_specimen = specimen_names[k]
        if verbose and this_specimen != "":
            print(this_specimen, k + 1, 'out of ', len(specimen_names))
        if setangle == 0:
            angle = ""
        this_specimen_measurements = meas_data[
            meas_data['specimen'].str.contains(
                this_specimen) == True]  # fish out this specimen
        this_specimen_measurements = this_specimen_measurements[
            this_specimen_measurements['flag'].str.contains(
                'g') == True]  # fish out this specimen
        if len(this_specimen_measurements) != 0:  # if there are measurements
            #
            #    set up datablock [[treatment,dec, inc, int, direction_type],[....]]
            #
            #
            # figure out the method codes
            #
            units, methods, title = "", "", this_specimen
            # this is a list of all the specimen method codes`
            meas_meths = this_specimen_measurements.method_codes.unique()
            tr = pd.to_numeric(this_specimen_measurements.treatment).tolist()
            if set(tr) == set([0]):
                k += 1
                continue
            for m in meas_meths:
                if 'LT-AF-Z' in m:
                    units = 'T'  # units include tesla
                    tr[0] = 0
                if 'LT-T-Z' in m:
                    units = units + ":K"  # units include kelvin
                if 'LT-M-Z' in m:
                    units = units + ':J'  # units include joules
                    tr[0] = 0
                units = units.strip(':')  # strip off extra colons
                if 'LP-' in m:
                    methods = methods + ":" + m
            decs = pd.to_numeric(this_specimen_measurements.dir_dec).tolist()
            incs = pd.to_numeric(this_specimen_measurements.dir_inc).tolist()
            #
            #    fix the coordinate system
            #
            if coord != '-1':  # need to transform coordinates to geographic

                azimuths = pd.to_numeric(this_specimen_measurements.azimuth
                                         ).tolist()  # get the azimuths
                # get the azimuths
                dips = pd.to_numeric(this_specimen_measurements.dip).tolist()
                dirs = [decs, incs, azimuths, dips]
                # this transposes the columns and rows of the list of lists
                dirs_geo = np.array(list(map(list, list(zip(*dirs)))))
                decs, incs = pmag.dogeo_V(dirs_geo)
                if coord == '100':  # need to do tilt correction too
                    bed_dip_dirs = pd.to_numeric(
                        this_specimen_measurements.bed_dip_dir).tolist(
                        )  # get the azimuths
                    bed_dips = pd.to_numeric(this_specimen_measurements.bed_dip
                                             ).tolist()  # get the azimuths
                    dirs = [decs, incs, bed_dip_dirs, bed_dips]
                    # this transposes the columns and rows of the list of lists
                    dirs_tilt = np.array(list(map(list, list(zip(*dirs)))))
                    decs, incs = pmag.dotilt_V(dirs_tilt)
                    title = title + '_t'
                else:
                    title = title + '_g'
            if angle == "":
                angle = decs[0]
            ints = pd.to_numeric(this_specimen_measurements[int_key]).tolist()
            ZI = this_specimen_measurements.ZI.tolist()
            flags = this_specimen_measurements.flag.tolist()
            codes = this_specimen_measurements.instrument_codes.tolist()
            datalist = [tr, decs, incs, ints, ZI, flags, codes]
            # this transposes the columns and rows of the list of lists
            datablock = list(map(list, list(zip(*datalist))))
            pmagplotlib.plotZED(ZED, datablock, angle, title, units)
            if verbose:
                pmagplotlib.drawFIGS(ZED)
#
#     collect info for current_specimen_interpretation dictionary
#
            if beg_pca == "" and len(prior_spec_data) != 0:
                #
                #     find prior interpretation
                #
                prior_specimen_interpretations = prior_spec_data[
                    prior_spec_data['specimen'].str.contains(
                        this_specimen) == True]
                beg_pcas = pd.to_numeric(prior_specimen_interpretations.
                                         meas_step_min.values).tolist()
                end_pcas = pd.to_numeric(prior_specimen_interpretations.
                                         meas_step_max.values).tolist()
                spec_methods = prior_specimen_interpretations.method_codes.tolist(
                )
                # step through all prior interpretations and plot them
                for ind in range(len(beg_pcas)):
                    spec_meths = spec_methods[ind].split(':')
                    for m in spec_meths:
                        if 'DE-BFL' in m:
                            calculation_type = 'DE-BFL'  # best fit line
                        if 'DE-BFP' in m:
                            calculation_type = 'DE-BFP'  # best fit plane
                        if 'DE-FM' in m:
                            calculation_type = 'DE-FM'  # fisher mean
                        if 'DE-BFL-A' in m:
                            calculation_type = 'DE-BFL-A'  # anchored best fit line
                    start, end = tr.index(beg_pcas[ind]), tr.index(
                        end_pcas[ind]
                    )  # getting the starting and ending points
                    # calculate direction/plane
                    mpars = pmag.domean(datablock, start, end,
                                        calculation_type)
                    if mpars["specimen_direction_type"] != "Error":
                        # put it on the plot
                        pmagplotlib.plotDir(ZED, mpars, datablock, angle)
                        if verbose:
                            pmagplotlib.drawFIGS(ZED)
            else:
                start, end = int(beg_pca), int(end_pca)
                # calculate direction/plane
                mpars = pmag.domean(datablock, start, end, calculation_type)
                if mpars["specimen_direction_type"] != "Error":
                    # put it on the plot
                    pmagplotlib.plotDir(ZED, mpars, datablock, angle)
                    if verbose:
                        pmagplotlib.drawFIGS(ZED)
            if plots == 1 or specimen != "":
                if plot_file == "":
                    basename = title
                else:
                    basename = plot_file
                files = {}
                for key in list(ZED.keys()):
                    files[key] = basename + '_' + key + '.' + fmt
                pmagplotlib.saveP(ZED, files)
                if specimen != "":
                    sys.exit()
            if verbose:
                recnum = 0
                for plotrec in datablock:
                    if units == 'T':
                        print('%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' %
                              (plotrec[5], recnum, plotrec[0] * 1e3, " mT",
                               plotrec[3], plotrec[1], plotrec[2], plotrec[6]))
                    if units == "K":
                        print('%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' %
                              (plotrec[5], recnum, plotrec[0] - 273, ' C',
                               plotrec[3], plotrec[1], plotrec[2], plotrec[6]))
                    if units == "J":
                        print('%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' %
                              (plotrec[5], recnum, plotrec[0], ' J',
                               plotrec[3], plotrec[1], plotrec[2], plotrec[6]))
                    if 'K' in units and 'T' in units:
                        if plotrec[0] >= 1.:
                            print('%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' %
                                  (plotrec[5], recnum, plotrec[0] - 273, ' C',
                                   plotrec[3], plotrec[1], plotrec[2],
                                   plotrec[6]))
                        if plotrec[0] < 1.:
                            print('%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' %
                                  (plotrec[5], recnum, plotrec[0] * 1e3, " mT",
                                   plotrec[3], plotrec[1], plotrec[2],
                                   plotrec[6]))
                    recnum += 1
            # we have a current interpretation
            elif mpars["specimen_direction_type"] != "Error":
                #
                # create a new specimen record for the interpreation for this
                # specimen
                this_specimen_interpretation = {
                    col: ""
                    for col in specimen_cols
                }
                #               this_specimen_interpretation["analysts"]=user
                this_specimen_interpretation['software_packages'] = version_num
                this_specimen_interpretation['specimen'] = this_specimen
                this_specimen_interpretation["method_codes"] = calculation_type
                this_specimen_interpretation["meas_step_unit"] = units
                this_specimen_interpretation["meas_step_min"] = tr[start]
                this_specimen_interpretation["meas_step_max"] = tr[end]
                this_specimen_interpretation["dir_dec"] = '%7.1f' % (
                    mpars['specimen_dec'])
                this_specimen_interpretation["dir_inc"] = '%7.1f' % (
                    mpars['specimen_inc'])
                this_specimen_interpretation["dir_dang"] = '%7.1f' % (
                    mpars['specimen_dang'])
                this_specimen_interpretation["dir_n_measurements"] = '%i' % (
                    mpars['specimen_n'])
                this_specimen_interpretation["dir_tilt_correction"] = coord
                methods = methods.replace(" ", "")
                if "T" in units:
                    methods = methods + ":LP-DIR-AF"
                if "K" in units:
                    methods = methods + ":LP-DIR-T"
                if "J" in units:
                    methods = methods + ":LP-DIR-M"
                this_specimen_interpretation["method_codes"] = methods.strip(
                    ':')
                this_specimen_interpretation[
                    "experiments"] = this_specimen_measurements.experiment.unique(
                    )[0]
                #
                #   print some stuff
                #
                if calculation_type != 'DE-FM':
                    this_specimen_interpretation["dir_mad_free"] = '%7.1f' % (
                        mpars['specimen_mad'])
                    this_specimen_interpretation["dir_alpha95"] = ''
                    if verbose:
                        if units == 'K':
                            print(
                                '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %s \n'
                                %
                                (this_specimen_interpretation["specimen"],
                                 int(this_specimen_interpretation[
                                     "dir_n_measurements"]),
                                 float(this_specimen_interpretation[
                                     "dir_mad_free"]),
                                 float(
                                     this_specimen_interpretation["dir_dang"]),
                                 float(this_specimen_interpretation[
                                     "meas_step_min"]) - 273,
                                 float(this_specimen_interpretation[
                                     "meas_step_max"]) - 273,
                                 float(
                                     this_specimen_interpretation["dir_dec"]),
                                 float(
                                     this_specimen_interpretation["dir_inc"]),
                                 calculation_type))
                        elif units == 'T':
                            print(
                                '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %s \n'
                                %
                                (this_specimen_interpretation["specimen"],
                                 int(this_specimen_interpretation[
                                     "dir_n_measurements"]),
                                 float(this_specimen_interpretation[
                                     "dir_mad_free"]),
                                 float(
                                     this_specimen_interpretation["dir_dang"]),
                                 float(this_specimen_interpretation[
                                     "meas_step_min"]) * 1e3,
                                 float(this_specimen_interpretation[
                                     "meas_step_max"]) * 1e3,
                                 float(
                                     this_specimen_interpretation["dir_dec"]),
                                 float(
                                     this_specimen_interpretation["dir_inc"]),
                                 calculation_type))
                        elif 'T' in units and 'K' in units:
                            if float(this_specimen_interpretation[
                                    'meas_step_min']) < 1.0:
                                min = float(this_specimen_interpretation[
                                    'meas_step_min']) * 1e3
                            else:
                                min = float(this_specimen_interpretation[
                                    'meas_step_min']) - 273
                            if float(this_specimen_interpretation[
                                    'meas_step_max']) < 1.0:
                                max = float(this_specimen_interpretation[
                                    'meas_step_max']) * 1e3
                            else:
                                max = float(this_specimen_interpretation[
                                    'meas_step_max']) - 273
                            print(
                                '%s %i %7.1f %i %i %7.1f %7.1f %7.1f %s \n' %
                                (this_specimen_interpretation["specimen"],
                                 int(this_specimen_interpretation[
                                     "dir_n_measurements"]),
                                 float(this_specimen_interpretation[
                                     "dir_mad_free"]),
                                 float(
                                     this_specimen_interpretation["dir_dang"]),
                                 min, max,
                                 float(
                                     this_specimen_interpretation["dir_dec"]),
                                 float(
                                     this_specimen_interpretation["dir_inc"]),
                                 calculation_type))
                        else:
                            print(
                                '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %s \n'
                                %
                                (this_specimen_interpretation["specimen"],
                                 int(this_specimen_interpretation[
                                     "dir_n_measurements"]),
                                 float(this_specimen_interpretation[
                                     "dir_mad_free"]),
                                 float(
                                     this_specimen_interpretation["dir_dang"]),
                                 float(this_specimen_interpretation[
                                     "meas_step_min"]),
                                 float(this_specimen_interpretation[
                                     "meas_step_max"]),
                                 float(
                                     this_specimen_interpretation["dir_dec"]),
                                 float(
                                     this_specimen_interpretation["dir_inc"]),
                                 calculation_type))
                else:
                    this_specimen_interpretation["dir_alpha95"] = '%7.1f' % (
                        mpars['specimen_alpha95'])
                    this_specimen_interpretation["dir_mad_free"] = ''
                    if verbose:
                        if 'K' in units:
                            print(
                                '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %s \n'
                                %
                                (this_specimen_interpretation["specimen"],
                                 int(this_specimen_interpretation[
                                     "dir_n_measurments"]),
                                 float(this_specimen_interpretation[
                                     "dir_mad_free"]),
                                 float(
                                     this_specimen_interpretation["dir_dang"]),
                                 float(this_specimen_interpretation[
                                     "meas_step_min"]) - 273,
                                 float(this_specimen_interpretation[
                                     "meas_step_max"]) - 273,
                                 float(
                                     this_specimen_interpretation["dir_dec"]),
                                 float(
                                     this_specimen_interpretation["dir_inc"]),
                                 calculation_type))
                        elif 'T' in units:
                            print(
                                '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %s \n'
                                %
                                (this_specimen_interpretation["specimen"],
                                 int(this_specimen_interpretation[
                                     "dir_n_measurements"]),
                                 float(this_specimen_interpretation[
                                     "dir_alpha95"]),
                                 float(
                                     this_specimen_interpretation["dir_dang"]),
                                 float(this_specimen_interpretation[
                                     "meas_step_min"]) * 1e3,
                                 float(this_specimen_interpretation[
                                     "meas_step_max"]) * 1e3,
                                 float(
                                     this_specimen_interpretation["dir_dec"]),
                                 float(
                                     this_specimen_interpretation["dir_inc"]),
                                 calculation_type))
                        elif 'T' in units and 'K' in units:
                            if float(this_specimen_interpretation[
                                    'meas_step_min']) < 1.0:
                                min = float(this_specimen_interpretation[
                                    'meas_step_min']) * 1e3
                            else:
                                min = float(this_specimen_interpretation[
                                    'meas_step_min']) - 273
                            if float(this_specimen_interpretation[
                                    'meas_step_max']) < 1.0:
                                max = float(this_specimen_interpretation[
                                    'meas_step_max']) * 1e3
                            else:
                                max = float(this_specimen_interpretation[
                                    'meas_step_max']) - 273
                            print('%s %i %7.1f %i %i %7.1f %7.1f %s \n' % (
                                this_specimen_interpretation["specimen"],
                                int(this_specimen_interpretation[
                                    "dir_n_measurements"]),
                                float(
                                    this_specimen_interpretation["dir_alpha95"]
                                ), min, max,
                                float(this_specimen_interpretation["dir_dec"]),
                                float(this_specimen_interpretation["dir_inc"]),
                                calculation_type))
                        else:
                            print(
                                '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %s \n' %
                                (this_specimen_interpretation["specimen"],
                                 int(this_specimen_interpretation[
                                     "dir_n_measurements"]),
                                 float(this_specimen_interpretation[
                                     "dir_alpha95"]),
                                 float(this_specimen_interpretation[
                                     "meas_step_min"]),
                                 float(this_specimen_interpretation[
                                     "meas_step_max"]),
                                 float(
                                     this_specimen_interpretation["dir_dec"]),
                                 float(
                                     this_specimen_interpretation["dir_inc"]),
                                 calculation_type))
                if verbose:
                    saveit = input("Save this interpretation? [y]/n \n")
#   START HERE
#
#         if len(current_spec_data)==0: # no interpretations yet for this session
#             print "no current interpretation"
#             beg_pca,end_pca="",""
#             calculation_type=""
# get the ones that meet the current coordinate system
        else:
            print("no data")
        if verbose:
            input('Ready for next specimen  ')
        k += 1
示例#3
0
def main():
    """
    NAME
        thellier_magic.py

    DESCRIPTION
        plots Thellier-Thellier, allowing interactive setting of bounds
        and customizing of selection criteria.  Saves and reads interpretations
        from a pmag_specimen formatted table, default: thellier_specimens.txt

    SYNTAX
        thellier_magic.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f MEAS, set magic_measurements input file
        -fsp PRIOR, set pmag_specimen prior interpretations file
        -fan ANIS, set rmag_anisotropy file for doing the anisotropy corrections
        -fcr CRIT, set criteria file for grading.
        -fmt [svg,png,jpg], format for images - default is svg
        -sav,  saves plots with out review (default format)
        -spc SPEC, plots single specimen SPEC, saves plot with specified format
            with optional -b bounds adn quits
        -b BEG END: sets  bounds for calculation
           BEG: starting step for slope calculation
           END: ending step for slope calculation
        -z use only z component difference for pTRM calculation

    DEFAULTS
        MEAS: magic_measurements.txt
        REDO: thellier_redo
        CRIT: NONE
        PRIOR: NONE

    OUTPUT
        figures:
            ALL:  numbers refer to temperature steps in command line window
            1) Arai plot:  closed circles are zero-field first/infield
                           open circles are infield first/zero-field
                           triangles are pTRM checks
                           squares are pTRM tail checks
                           VDS is vector difference sum
                           diamonds are bounds for interpretation
            2) Zijderveld plot:  closed (open) symbols are X-Y (X-Z) planes
                                 X rotated to NRM direction
            3) (De/Re)Magnetization diagram:
                           circles are NRM remaining
                           squares are pTRM gained
            4) equal area projections:
               green triangles are pTRM gained direction
                           red (purple) circles are lower(upper) hemisphere of ZI step directions
                           blue (cyan) squares are lower(upper) hemisphere IZ step directions
            5) Optional:  TRM acquisition
            6) Optional: TDS normalization
        command line window:
            list is: temperature step numbers, temperatures (C), Dec, Inc, Int (units of magic_measuements)
                     list of possible commands: type letter followed by return to select option
                     saving of plots creates .svg format files with specimen_name, plot type as name
    """
    #
    #   initializations
    #
    meas_file, critout, inspec = "magic_measurements.txt", "", "thellier_specimens.txt"
    first = 1
    inlt = 0
    version_num = pmag.get_version()
    TDinit, Tinit, field, first_save = 0, 0, -1, 1
    user, comment, AniSpec, locname = "", '', "", ""
    ans, specimen, recnum, start, end = 0, 0, 0, 0, 0
    plots, pmag_out, samp_file, style = 0, "", "", "svg"
    verbose = pmagplotlib.verbose
    fmt = '.' + style
    #
    # default acceptance criteria
    #
    accept = pmag.default_criteria(0)[0]  # set the default criteria
    #
    # parse command line options
    #
    Zdiff, anis = 0, 0
    spc, BEG, END = "", "", ""
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-f' in sys.argv:
        ind = sys.argv.index('-f')
        meas_file = sys.argv[ind + 1]
    if '-fsp' in sys.argv:
        ind = sys.argv.index('-fsp')
        inspec = sys.argv[ind + 1]
    if '-fan' in sys.argv:
        ind = sys.argv.index('-fan')
        anisfile = sys.argv[ind + 1]
        anis = 1
        anis_data, file_type = pmag.magic_read(anisfile)
        if verbose:
            print("Anisotropy data read in from ", anisfile)
    if '-fmt' in sys.argv:
        ind = sys.argv.index('-fmt')
        fmt = '.' + sys.argv[ind + 1]
    if '-dpi' in sys.argv:
        ind = sys.argv.index('-dpi')
        dpi = '.' + sys.argv[ind + 1]
    else:
        dpi = 100
    if '-sav' in sys.argv:
        plots = 1
        verbose = 0
    if '-z' in sys.argv:
        Zdiff = 1
    if '-spc' in sys.argv:
        ind = sys.argv.index('-spc')
        spc = sys.argv[ind + 1]
        if '-b' in sys.argv:
            ind = sys.argv.index('-b')
            BEG = int(sys.argv[ind + 1])
            END = int(sys.argv[ind + 2])
    if '-fcr' in sys.argv:
        ind = sys.argv.index('-fcr')
        critout = sys.argv[ind + 1]
        crit_data, file_type = pmag.magic_read(critout)
        if file_type != 'pmag_criteria':
            if verbose:
                print('bad pmag_criteria file, using no acceptance criteria')
            accept = pmag.default_criteria(1)[0]
        else:
            if verbose:
                print("Acceptance criteria read in from ", critout)
            accept = {
                'pmag_criteria_code': 'ACCEPTANCE',
                'er_citation_names': 'This study'
            }
            for critrec in crit_data:
                if 'sample_int_sigma_uT' in critrec.keys(
                ):  # accommodate Shaar's new criterion
                    critrec['sample_int_sigma'] = '%10.3e' % (
                        eval(critrec['sample_int_sigma_uT']) * 1e-6)
                for key in critrec.keys():
                    if key not in accept.keys() and critrec[key] != '':
                        accept[key] = critrec[key]
    try:
        open(inspec, 'rU')
        PriorRecs, file_type = pmag.magic_read(inspec)
        if file_type != 'pmag_specimens':
            print(file_type)
            print(file_type, inspec, " is not a valid pmag_specimens file ")
            sys.exit()
        for rec in PriorRecs:
            if 'magic_software_packages' not in rec.keys():
                rec['magic_software_packages'] = ""
    except IOError:
        PriorRecs = []
        if verbose:
            print("starting new specimen interpretation file: ", inspec)
    meas_data, file_type = pmag.magic_read(meas_file)
    if file_type != 'magic_measurements':
        print(file_type)
        print(file_type, "This is not a valid magic_measurements file ")
        sys.exit()
    backup = 0
    # define figure numbers for arai, zijderveld and
    #   de-,re-magization diagrams
    AZD = {}
    AZD['deremag'], AZD['zijd'], AZD['arai'], AZD['eqarea'] = 1, 2, 3, 4
    pmagplotlib.plot_init(AZD['arai'], 5, 5)
    pmagplotlib.plot_init(AZD['zijd'], 5, 5)
    pmagplotlib.plot_init(AZD['deremag'], 5, 5)
    pmagplotlib.plot_init(AZD['eqarea'], 5, 5)
    #
    #
    #
    # get list of unique specimen names
    #
    CurrRec = []
    sids = pmag.get_specs(meas_data)
    # get plots for specimen s - default is just to step through arai diagrams
    #
    if spc != "":
        specimen = sids.index(spc)
    while specimen < len(sids):
        methcodes = []

        if verbose:
            print(sids[specimen], specimen + 1, 'of ', len(sids))
        MeasRecs = []
        s = sids[specimen]
        datablock, trmblock, tdsrecs = [], [], []
        PmagSpecRec = {}
        if first == 0:
            for key in keys:
                # make sure all new records have same set of keys
                PmagSpecRec[key] = ""
        PmagSpecRec["er_analyst_mail_names"] = user
        PmagSpecRec["specimen_correction"] = 'u'
        #
        # find the data from the meas_data file for this specimen
        #
        for rec in meas_data:
            if rec["er_specimen_name"] == s:
                MeasRecs.append(rec)
                if "magic_method_codes" not in rec.keys():
                    rec["magic_method_codes"] = ""
                methods = rec["magic_method_codes"].split(":")
                meths = []
                for meth in methods:
                    meths.append(meth.strip())  # take off annoying spaces
                methods = ""
                for meth in meths:
                    if meth.strip() not in methcodes and "LP-" in meth:
                        methcodes.append(meth.strip())
                    methods = methods + meth + ":"
                methods = methods[:-1]
                rec["magic_method_codes"] = methods
                if "LP-PI-TRM" in meths:
                    datablock.append(rec)
                if "LP-TRM" in meths:
                    trmblock.append(rec)
                if "LP-TRM-TD" in meths:
                    tdsrecs.append(rec)
        if len(trmblock) > 2 and inspec != "":
            if Tinit == 0:
                Tinit = 1
                AZD['TRM'] = 5
                pmagplotlib.plot_init(AZD['TRM'], 5, 5)
        elif Tinit == 1:  # clear the TRM figure if not needed
            pmagplotlib.clearFIG(AZD['TRM'])
        if len(tdsrecs) > 2:
            if TDinit == 0:
                TDinit = 1
                AZD['TDS'] = 6
                pmagplotlib.plot_init(AZD['TDS'], 5, 5)
        elif TDinit == 1:  # clear the TDS figure if not needed
            pmagplotlib.clearFIG(AZD['TDS'])
        if len(datablock) < 4:
            if backup == 0:
                specimen += 1
                if verbose:
                    print('skipping specimen - moving forward ', s)
            else:
                specimen -= 1
                if verbose:
                    print('skipping specimen - moving backward ', s)
    #
    #  collect info for the PmagSpecRec dictionary
    #
        else:
            rec = datablock[0]
            PmagSpecRec["er_citation_names"] = "This study"
            PmagSpecRec["er_specimen_name"] = s
            PmagSpecRec["er_sample_name"] = rec["er_sample_name"]
            PmagSpecRec["er_site_name"] = rec["er_site_name"]
            PmagSpecRec["er_location_name"] = rec["er_location_name"]
            locname = rec['er_location_name'].replace('/', '-')
            if "er_expedition_name" in rec.keys():
                PmagSpecRec["er_expedition_name"] = rec["er_expedition_name"]
            if "magic_instrument_codes" not in rec.keys():
                rec["magic_instrument_codes"] = ""
            PmagSpecRec["magic_instrument_codes"] = rec[
                "magic_instrument_codes"]
            PmagSpecRec["measurement_step_unit"] = "K"
            if "magic_experiment_name" not in rec.keys():
                rec["magic_experiment_name"] = ""
            else:
                PmagSpecRec["magic_experiment_names"] = rec[
                    "magic_experiment_name"]

            meths = rec["magic_method_codes"].split()
            # sort data into types
            araiblock, field = pmag.sortarai(datablock, s, Zdiff)
            first_Z = araiblock[0]
            GammaChecks = araiblock[5]
            if len(first_Z) < 3:
                if backup == 0:
                    specimen += 1
                    if verbose:
                        print('skipping specimen - moving forward ', s)
                else:
                    specimen -= 1
                    if verbose:
                        print('skipping specimen - moving backward ', s)
            else:
                backup = 0
                zijdblock, units = pmag.find_dmag_rec(s, meas_data)
                recnum = 0
                if verbose:
                    print("index step Dec   Inc  Int       Gamma")
                    for plotrec in zijdblock:
                        if GammaChecks != "":
                            gamma = ""
                            for g in GammaChecks:
                                if g[0] == plotrec[0] - 273:
                                    gamma = g[1]
                                    break
                        if gamma != "":
                            print('%i     %i %7.1f %7.1f %8.3e %7.1f' %
                                  (recnum, plotrec[0] - 273, plotrec[1],
                                   plotrec[2], plotrec[3], gamma))
                        else:
                            print('%i     %i %7.1f %7.1f %8.3e ' %
                                  (recnum, plotrec[0] - 273, plotrec[1],
                                   plotrec[2], plotrec[3]))
                        recnum += 1
                pmagplotlib.plot_arai_zij(AZD, araiblock, zijdblock, s,
                                          units[0])
                if verbose:
                    pmagplotlib.draw_figs(AZD)
                if len(tdsrecs) > 2:  # a TDS experiment
                    tdsblock = []  # make a list for the TDS  data
                    Mkeys = [
                        'measurement_magnitude', 'measurement_magn_moment',
                        'measurement_magn_volume', 'measuruement_magn_mass'
                    ]
                    mkey, k = "", 0
                    # find which type of intensity
                    while mkey == "" and k < len(Mkeys) - 1:
                        key = Mkeys[k]
                        if key in tdsrecs[0].keys() and tdsrecs[0][key] != "":
                            mkey = key
                        k += 1
                    if mkey == "":
                        break  # get outta here
                    Tnorm = ""
                    for tdrec in tdsrecs:
                        meths = tdrec['magic_method_codes'].split(":")
                        for meth in meths:
                            # strip off potential nasty spaces
                            meth.replace(" ", "")
                        if 'LT-T-I' in meths and Tnorm == "":  # found first total TRM
                            # normalize by total TRM
                            Tnorm = float(tdrec[mkey])
                            # put in the zero step
                            tdsblock.append([273, zijdblock[0][3] / Tnorm, 1.])
                        # found a LP-TRM-TD demag step, now need complementary LT-T-Z from zijdblock
                        if 'LT-T-Z' in meths and Tnorm != "":
                            step = float(tdrec['treatment_temp'])
                            Tint = ""
                            if mkey != "":
                                Tint = float(tdrec[mkey])
                            if Tint != "":
                                for zrec in zijdblock:
                                    if zrec[0] == step:  # found matching
                                        tdsblock.append([
                                            step, zrec[3] / Tnorm, Tint / Tnorm
                                        ])
                                        break
                    if len(tdsblock) > 2:
                        pmagplotlib.plot_tds(AZD['TDS'], tdsblock,
                                             s + ':LP-PI-TDS:')
                        if verbose:
                            pmagplotlib(draw_figs(AZD))
                    else:
                        print("Something wrong here")
                if anis == 1:  # look up anisotropy data for this specimen
                    AniSpec = ""
                    for aspec in anis_data:
                        if aspec["er_specimen_name"] == PmagSpecRec[
                                "er_specimen_name"]:
                            AniSpec = aspec
                            if verbose:
                                print('Found anisotropy record...')
                            break
                if inspec != "":
                    if verbose:
                        print('Looking up saved interpretation....')
                    found = 0
                    for k in range(len(PriorRecs)):
                        try:
                            if PriorRecs[k]["er_specimen_name"] == s:
                                found = 1
                                CurrRec.append(PriorRecs[k])
                                for j in range(len(zijdblock)):
                                    if float(zijdblock[j][0]) == float(
                                            PriorRecs[k]
                                        ["measurement_step_min"]):
                                        start = j
                                    if float(zijdblock[j][0]) == float(
                                            PriorRecs[k]
                                        ["measurement_step_max"]):
                                        end = j
                                pars, errcode = pmag.PintPars(
                                    datablock, araiblock, zijdblock, start,
                                    end, accept)
                                pars['measurement_step_unit'] = "K"
                                pars['experiment_type'] = 'LP-PI-TRM'
                                # put in CurrRec, take out of PriorRecs
                                del PriorRecs[k]
                                if errcode != 1:
                                    pars["specimen_lab_field_dc"] = field
                                    pars["specimen_int"] = -1 * \
                                        field*pars["specimen_b"]
                                    pars["er_specimen_name"] = s
                                    if verbose:
                                        print('Saved interpretation: ')
                                    pars, kill = pmag.scoreit(
                                        pars, PmagSpecRec, accept, '', verbose)
                                    pmagplotlib.plot_b(AZD, araiblock,
                                                       zijdblock, pars)
                                    if verbose:
                                        pmagplotlib.draw_figs(AZD)
                                    if len(trmblock) > 2:
                                        blab = field
                                        best = pars["specimen_int"]
                                        Bs, TRMs = [], []
                                        for trec in trmblock:
                                            Bs.append(
                                                float(
                                                    trec['treatment_dc_field'])
                                            )
                                            TRMs.append(
                                                float(trec[
                                                    'measurement_magn_moment'])
                                            )
                                        # calculate best fit parameters through TRM acquisition data, and get new banc
                                        NLpars = nlt.NLtrm(
                                            Bs, TRMs, best, blab, 0)
                                        Mp, Bp = [], []
                                        for k in range(int(max(Bs) * 1e6)):
                                            Bp.append(float(k) * 1e-6)
                                            # predicted NRM for this field
                                            npred = nlt.TRM(
                                                Bp[-1], NLpars['xopt'][0],
                                                NLpars['xopt'][1])
                                            Mp.append(npred)
                                        pmagplotlib.plot_trm(
                                            AZD['TRM'], Bs, TRMs, Bp, Mp,
                                            NLpars,
                                            trec['magic_experiment_name'])
                                        PmagSpecRec['specimen_int'] = NLpars[
                                            'banc']
                                        if verbose:
                                            print('Banc= ',
                                                  float(NLpars['banc']) * 1e6)
                                            pmagplotlib.draw_figs(AZD)
                                    mpars = pmag.domean(
                                        araiblock[1], start, end, 'DE-BFL')
                                    if verbose:
                                        print(
                                            'pTRM direction= ',
                                            '%7.1f' % (mpars['specimen_dec']),
                                            ' %7.1f' % (mpars['specimen_inc']),
                                            ' MAD:',
                                            '%7.1f' % (mpars['specimen_mad']))
                                    if AniSpec != "":
                                        CpTRM = pmag.Dir_anis_corr([
                                            mpars['specimen_dec'],
                                            mpars['specimen_inc']
                                        ], AniSpec)
                                        AniSpecRec = pmag.doaniscorr(
                                            PmagSpecRec, AniSpec)
                                        if verbose:
                                            print(
                                                'Anisotropy corrected TRM direction= ',
                                                '%7.1f' % (CpTRM[0]),
                                                ' %7.1f' % (CpTRM[1]))
                                            print(
                                                'Anisotropy corrected intensity= ',
                                                float(
                                                    AniSpecRec['specimen_int'])
                                                * 1e6)
                                else:
                                    print('error on specimen ', s)
                        except:
                            pass
                    if verbose and found == 0:
                        print('    None found :(  ')
                if spc != "":
                    if BEG != "":
                        pars, errcode = pmag.PintPars(datablock, araiblock,
                                                      zijdblock, BEG, END,
                                                      accept)
                        pars['measurement_step_unit'] = "K"
                        pars["specimen_lab_field_dc"] = field
                        pars["specimen_int"] = -1 * field * pars["specimen_b"]
                        pars["er_specimen_name"] = s
                        pars['specimen_grade'] = ''  # ungraded
                        pmagplotlib.plot_b(AZD, araiblock, zijdblock, pars)
                        if verbose:
                            pmagplotlib.draw_figs(AZD)
                        if len(trmblock) > 2:
                            if inlt == 0:
                                inlt = 1
                            blab = field
                            best = pars["specimen_int"]
                            Bs, TRMs = [], []
                            for trec in trmblock:
                                Bs.append(float(trec['treatment_dc_field']))
                                TRMs.append(
                                    float(trec['measurement_magn_moment']))
                            # calculate best fit parameters through TRM acquisition data, and get new banc
                            NLpars = nlt.NLtrm(Bs, TRMs, best, blab, 0)
                            #
                            Mp, Bp = [], []
                            for k in range(int(max(Bs) * 1e6)):
                                Bp.append(float(k) * 1e-6)
                                # predicted NRM for this field
                                npred = nlt.TRM(Bp[-1], NLpars['xopt'][0],
                                                NLpars['xopt'][1])
                    files = {}
                    for key in AZD.keys():
                        files[key] = s + '_' + key + fmt
                    pmagplotlib.save_plots(AZD, files, dpi=dpi)
                    sys.exit()
                if verbose:
                    ans = 'b'
                    while ans != "":
                        print("""
               s[a]ve plot, set [b]ounds for calculation, [d]elete current interpretation, [p]revious, [s]ample, [q]uit:
               """)
                        ans = input('Return for next specimen \n')
                        if ans == "":
                            specimen += 1
                        if ans == "d":
                            save_redo(PriorRecs, inspec)
                            CurrRec = []
                            pmagplotlib.plot_arai_zij(AZD, araiblock,
                                                      zijdblock, s, units[0])
                            if verbose:
                                pmagplotlib.draw_figs(AZD)
                        if ans == 'a':
                            files = {}
                            for key in AZD.keys():
                                files[key] = "LO:_"+locname+'_SI:_'+PmagSpecRec['er_site_name'] + \
                                    '_SA:_' + \
                                    PmagSpecRec['er_sample_name'] + \
                                    '_SP:_'+s+'_CO:_s_TY:_'+key+fmt
                            pmagplotlib.save_plots(AZD, files)
                            ans = ""
                        if ans == 'q':
                            print("Good bye")
                            sys.exit()
                        if ans == 'p':
                            specimen = specimen - 1
                            backup = 1
                            ans = ""
                        if ans == 's':
                            keepon = 1
                            spec = input(
                                'Enter desired specimen name (or first part there of): '
                            )
                            while keepon == 1:
                                try:
                                    specimen = sids.index(spec)
                                    keepon = 0
                                except:
                                    tmplist = []
                                    for qq in range(len(sids)):
                                        if spec in sids[qq]:
                                            tmplist.append(sids[qq])
                                    print(specimen,
                                          " not found, but this was: ")
                                    print(tmplist)
                                    spec = input('Select one or try again\n ')
                            ans = ""
                        if ans == 'b':
                            if end == 0 or end >= len(zijdblock):
                                end = len(zijdblock) - 1
                            GoOn = 0
                            while GoOn == 0:
                                answer = input(
                                    'Enter index of first point for calculation: ['
                                    + str(start) + ']  ')
                                try:
                                    start = int(answer)
                                    answer = input(
                                        'Enter index  of last point for calculation: ['
                                        + str(end) + ']  ')
                                    end = int(answer)
                                    if start >= 0 and start < len(
                                            zijdblock
                                    ) - 2 and end > 0 and end < len(
                                            zijdblock) or start >= end:
                                        GoOn = 1
                                    else:
                                        print("Bad endpoints - try again! ")
                                        start, end = 0, len(zijdblock)
                                except ValueError:
                                    print("Bad endpoints - try again! ")
                                    start, end = 0, len(zijdblock)
                            s = sids[specimen]
                            pars, errcode = pmag.PintPars(
                                datablock, araiblock, zijdblock, start, end,
                                accept)
                            pars['measurement_step_unit'] = "K"
                            pars["specimen_lab_field_dc"] = field
                            pars["specimen_int"] = -1 * field * pars[
                                "specimen_b"]
                            pars["er_specimen_name"] = s
                            pars, kill = pmag.scoreit(pars, PmagSpecRec,
                                                      accept, '', 0)
                            PmagSpecRec['specimen_scat'] = pars[
                                'specimen_scat']
                            PmagSpecRec['specimen_frac'] = '%5.3f' % (
                                pars['specimen_frac'])
                            PmagSpecRec['specimen_gmax'] = '%5.3f' % (
                                pars['specimen_gmax'])
                            PmagSpecRec["measurement_step_min"] = '%8.3e' % (
                                pars["measurement_step_min"])
                            PmagSpecRec["measurement_step_max"] = '%8.3e' % (
                                pars["measurement_step_max"])
                            PmagSpecRec["measurement_step_unit"] = "K"
                            PmagSpecRec["specimen_int_n"] = '%i' % (
                                pars["specimen_int_n"])
                            PmagSpecRec["specimen_lab_field_dc"] = '%8.3e' % (
                                pars["specimen_lab_field_dc"])
                            PmagSpecRec["specimen_int"] = '%9.4e ' % (
                                pars["specimen_int"])
                            PmagSpecRec["specimen_b"] = '%5.3f ' % (
                                pars["specimen_b"])
                            PmagSpecRec["specimen_q"] = '%5.1f ' % (
                                pars["specimen_q"])
                            PmagSpecRec["specimen_f"] = '%5.3f ' % (
                                pars["specimen_f"])
                            PmagSpecRec["specimen_fvds"] = '%5.3f' % (
                                pars["specimen_fvds"])
                            PmagSpecRec["specimen_b_beta"] = '%5.3f' % (
                                pars["specimen_b_beta"])
                            PmagSpecRec["specimen_int_mad"] = '%7.1f' % (
                                pars["specimen_int_mad"])
                            PmagSpecRec["specimen_Z"] = '%7.1f' % (
                                pars["specimen_Z"])
                            PmagSpecRec["specimen_gamma"] = '%7.1f' % (
                                pars["specimen_gamma"])
                            PmagSpecRec["specimen_grade"] = pars[
                                "specimen_grade"]
                            if pars["method_codes"] != "":
                                tmpcodes = pars["method_codes"].split(":")
                                for t in tmpcodes:
                                    if t.strip() not in methcodes:
                                        methcodes.append(t.strip())
                            PmagSpecRec["specimen_dec"] = '%7.1f' % (
                                pars["specimen_dec"])
                            PmagSpecRec["specimen_inc"] = '%7.1f' % (
                                pars["specimen_inc"])
                            PmagSpecRec["specimen_tilt_correction"] = '-1'
                            PmagSpecRec["specimen_direction_type"] = 'l'
                            # this is redundant, but helpful - won't be imported
                            PmagSpecRec["direction_type"] = 'l'
                            PmagSpecRec["specimen_int_dang"] = '%7.1f ' % (
                                pars["specimen_int_dang"])
                            PmagSpecRec["specimen_drats"] = '%7.1f ' % (
                                pars["specimen_drats"])
                            PmagSpecRec["specimen_drat"] = '%7.1f ' % (
                                pars["specimen_drat"])
                            PmagSpecRec["specimen_int_ptrm_n"] = '%i ' % (
                                pars["specimen_int_ptrm_n"])
                            PmagSpecRec["specimen_rsc"] = '%6.4f ' % (
                                pars["specimen_rsc"])
                            PmagSpecRec["specimen_md"] = '%i ' % (int(
                                pars["specimen_md"]))
                            if PmagSpecRec["specimen_md"] == '-1':
                                PmagSpecRec["specimen_md"] = ""
                            PmagSpecRec["specimen_b_sigma"] = '%5.3f ' % (
                                pars["specimen_b_sigma"])
                            if "IE-TT" not in methcodes:
                                methcodes.append("IE-TT")
                            methods = ""
                            for meth in methcodes:
                                methods = methods + meth + ":"
                            PmagSpecRec["magic_method_codes"] = methods[:-1]
                            PmagSpecRec["specimen_description"] = comment
                            PmagSpecRec[
                                "magic_software_packages"] = version_num
                            pmagplotlib.plot_arai_zij(AZD, araiblock,
                                                      zijdblock, s, units[0])
                            pmagplotlib.plot_b(AZD, araiblock, zijdblock, pars)
                            if verbose:
                                pmagplotlib.draw_figs(AZD)
                            if len(trmblock) > 2:
                                blab = field
                                best = pars["specimen_int"]
                                Bs, TRMs = [], []
                                for trec in trmblock:
                                    Bs.append(float(
                                        trec['treatment_dc_field']))
                                    TRMs.append(
                                        float(trec['measurement_magn_moment']))
                                # calculate best fit parameters through TRM acquisition data, and get new banc
                                NLpars = nlt.NLtrm(Bs, TRMs, best, blab, 0)
                                Mp, Bp = [], []
                                for k in range(int(max(Bs) * 1e6)):
                                    Bp.append(float(k) * 1e-6)
                                    # predicted NRM for this field
                                    npred = nlt.TRM(Bp[-1], NLpars['xopt'][0],
                                                    NLpars['xopt'][1])
                                    Mp.append(npred)
                                pmagplotlib.plot_trm(
                                    AZD['TRM'], Bs, TRMs, Bp, Mp, NLpars,
                                    trec['magic_experiment_name'])
                                if verbose:
                                    print(
                                        'Non-linear TRM corrected intensity= ',
                                        float(NLpars['banc']) * 1e6)
                            if verbose:
                                pmagplotlib.draw_figs(AZD)
                            pars["specimen_lab_field_dc"] = field
                            pars["specimen_int"] = -1 * field * pars[
                                "specimen_b"]
                            pars, kill = pmag.scoreit(pars, PmagSpecRec,
                                                      accept, '', verbose)
                            saveit = input(
                                "Save this interpretation? [y]/n \n")
                            if saveit != 'n':
                                # put back an interpretation
                                PriorRecs.append(PmagSpecRec)
                                specimen += 1
                                save_redo(PriorRecs, inspec)
                            ans = ""
                elif plots == 1:
                    specimen += 1
                    if fmt != ".pmag":
                        files = {}
                        for key in AZD.keys():
                            files[key] = "LO:_"+locname+'_SI:_'+PmagSpecRec['er_site_name']+'_SA:_' + \
                                PmagSpecRec['er_sample_name'] + \
                                '_SP:_'+s+'_CO:_s_TY:_'+key+'_'+fmt
                        if pmagplotlib.isServer:
                            black = '#000000'
                            purple = '#800080'
                            titles = {}
                            titles['deremag'] = 'DeReMag Plot'
                            titles['zijd'] = 'Zijderveld Plot'
                            titles['arai'] = 'Arai Plot'
                            AZD = pmagplotlib.add_borders(
                                AZD, titles, black, purple)
                        pmagplotlib.save_plots(AZD, files, dpi=dpi)
    #                   pmagplotlib.combineFigs(s,files,3)
                    else:  # save in pmag format
                        script = "grep " + s + " output.mag | thellier -mfsi"
                        script = script + ' %8.4e' % (field)
                        min = '%i' % ((pars["measurement_step_min"] - 273))
                        Max = '%i' % ((pars["measurement_step_max"] - 273))
                        script = script + " " + min + " " + Max
                        script = script + " |plotxy;cat mypost >>thellier.ps\n"
                        pltf.write(script)
                        pmag.domagicmag(outf, MeasRecs)
        if len(CurrRec) > 0:
            for rec in CurrRec:
                PriorRecs.append(rec)
        CurrRec = []
    if plots != 1 and verbose:
        ans = input(" Save last plot? 1/[0] ")
        if ans == "1":
            if fmt != ".pmag":
                files = {}
                for key in AZD.keys():
                    files[key] = s + '_' + key + fmt
                pmagplotlib.save_plots(AZD, files, dpi=dpi)
        else:
            print("\n Good bye\n")
            sys.exit()
        if len(CurrRec) > 0:
            PriorRecs.append(CurrRec)  # put back an interpretation
        if len(PriorRecs) > 0:
            save_redo(PriorRecs, inspec)
            print('Updated interpretations saved in ', inspec)
    if verbose:
        print("Good bye")
示例#4
0
def main():
    """
    NAME
        thellier_magic.py
    
    DESCRIPTION
        plots Thellier-Thellier, allowing interactive setting of bounds
        and customizing of selection criteria.  Saves and reads interpretations
        from a pmag_specimen formatted table, default: thellier_specimens.txt

    SYNTAX 
        thellier_magic.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f MEAS, set magic_measurements input file
        -fsp PRIOR, set pmag_specimen prior interpretations file
        -fan ANIS, set rmag_anisotropy file for doing the anisotropy corrections
        -fcr CRIT, set criteria file for grading.  
        -fmt [svg,png,jpg], format for images - default is svg
        -sav,  saves plots with out review (default format)
        -spc SPEC, plots single specimen SPEC, saves plot with specified format
            with optional -b bounds adn quits
        -b BEG END: sets  bounds for calculation
           BEG: starting step for slope calculation
           END: ending step for slope calculation
        -z use only z component difference for pTRM calculation
        
    DEFAULTS
        MEAS: magic_measurements.txt
        REDO: thellier_redo
        CRIT: NONE
        PRIOR: NONE
  
    OUTPUT 
        figures:
            ALL:  numbers refer to temperature steps in command line window
            1) Arai plot:  closed circles are zero-field first/infield
                           open circles are infield first/zero-field
                           triangles are pTRM checks
                           squares are pTRM tail checks
                           VDS is vector difference sum
                           diamonds are bounds for interpretation
            2) Zijderveld plot:  closed (open) symbols are X-Y (X-Z) planes
                                 X rotated to NRM direction
            3) (De/Re)Magnetization diagram:
                           circles are NRM remaining
                           squares are pTRM gained
            4) equal area projections:
 			   green triangles are pTRM gained direction
                           red (purple) circles are lower(upper) hemisphere of ZI step directions 
                           blue (cyan) squares are lower(upper) hemisphere IZ step directions 
            5) Optional:  TRM acquisition
            6) Optional: TDS normalization
        command line window:
            list is: temperature step numbers, temperatures (C), Dec, Inc, Int (units of magic_measuements)
                     list of possible commands: type letter followed by return to select option
                     saving of plots creates .svg format files with specimen_name, plot type as name
    """ 
#
#   initializations
#
    meas_file,critout,inspec="magic_measurements.txt","","thellier_specimens.txt"
    first=1
    inlt=0
    version_num=pmag.get_version()
    TDinit,Tinit,field,first_save=0,0,-1,1
    user,comment,AniSpec,locname="",'',"",""
    ans,specimen,recnum,start,end=0,0,0,0,0
    plots,pmag_out,samp_file,style=0,"","","svg"
    verbose=pmagplotlib.verbose 
    fmt='.'+style
#
# default acceptance criteria
#
    accept=pmag.default_criteria(0)[0] # set the default criteria
#
# parse command line options
#
    Zdiff,anis=0,0
    spc,BEG,END="","",""
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        meas_file=sys.argv[ind+1]
    if '-fsp' in sys.argv:
        ind=sys.argv.index('-fsp')
        inspec=sys.argv[ind+1]
    if '-fan' in sys.argv:
        ind=sys.argv.index('-fan')
        anisfile=sys.argv[ind+1]
        anis=1
        anis_data,file_type=pmag.magic_read(anisfile)
        if verbose: print "Anisotropy data read in from ", anisfile
    if '-fmt' in sys.argv:
        ind=sys.argv.index('-fmt')
        fmt='.'+sys.argv[ind+1]
    if '-dpi' in sys.argv:
        ind=sys.argv.index('-dpi')
        dpi='.'+sys.argv[ind+1]
    else: dpi=100
    if '-sav' in sys.argv: 
        plots=1
        verbose=0
    if '-z' in sys.argv: Zdiff=1
    if '-spc' in sys.argv:
        ind=sys.argv.index('-spc')
        spc=sys.argv[ind+1]
        if '-b' in sys.argv:
            ind=sys.argv.index('-b')
            BEG=int(sys.argv[ind+1])
            END=int(sys.argv[ind+2])
    if '-fcr' in sys.argv:
        ind=sys.argv.index('-fcr')
        critout=sys.argv[ind+1]
        crit_data,file_type=pmag.magic_read(critout)
        if file_type!='pmag_criteria':
            if verbose: print 'bad pmag_criteria file, using no acceptance criteria'
            accept=pmag.default_criteria(1)[0]
        else:
            if verbose: print "Acceptance criteria read in from ", critout
            accept={'pmag_criteria_code':'ACCEPTANCE','er_citation_names':'This study'}
            for critrec in crit_data:
                if 'sample_int_sigma_uT' in critrec.keys(): # accommodate Shaar's new criterion
                    critrec['sample_int_sigma']='%10.3e'%(eval(critrec['sample_int_sigma_uT'])*1e-6)
                for key in critrec.keys():
                    if key not in accept.keys() and critrec[key]!='':
                        accept[key]=critrec[key]
    try:
        open(inspec,'rU')
        PriorRecs,file_type=pmag.magic_read(inspec)
        if file_type != 'pmag_specimens':
            print file_type
            print file_type,inspec," is not a valid pmag_specimens file " 
            sys.exit()
        for rec in PriorRecs:
            if 'magic_software_packages' not in rec.keys():rec['magic_software_packages']=""
    except IOError:
        PriorRecs=[]
        if verbose:print "starting new specimen interpretation file: ",inspec
    meas_data,file_type=pmag.magic_read(meas_file)
    if file_type != 'magic_measurements':
        print file_type
        print file_type,"This is not a valid magic_measurements file " 
        sys.exit()
    backup=0
    # define figure numbers for arai, zijderveld and 
    #   de-,re-magization diagrams
    AZD={}
    AZD['deremag'], AZD['zijd'],AZD['arai'],AZD['eqarea']=1,2,3,4
    pmagplotlib.plot_init(AZD['arai'],5,5)
    pmagplotlib.plot_init(AZD['zijd'],5,5)
    pmagplotlib.plot_init(AZD['deremag'],5,5)
    pmagplotlib.plot_init(AZD['eqarea'],5,5)
    #
    #
    #
    # get list of unique specimen names
    #
    CurrRec=[]
    sids=pmag.get_specs(meas_data)
    # get plots for specimen s - default is just to step through arai diagrams
    #
    if spc!="": specimen =sids.index(spc)
    while specimen < len(sids):
        methcodes=[]
       
        if verbose:
            print sids[specimen],specimen+1, 'of ', len(sids)
        MeasRecs=[]
        s=sids[specimen]
        datablock,trmblock,tdsrecs=[],[],[]
        PmagSpecRec={}
        if first==0:
           for key in keys:PmagSpecRec[key]="" # make sure all new records have same set of keys
        PmagSpecRec["er_analyst_mail_names"]=user
        PmagSpecRec["specimen_correction"]='u'
    #
    # find the data from the meas_data file for this specimen
    #
        for rec in meas_data:
            if rec["er_specimen_name"]==s:
                MeasRecs.append(rec)
                if "magic_method_codes" not in rec.keys():
                    rec["magic_method_codes"]=""
                methods=rec["magic_method_codes"].split(":")
                meths=[]
                for meth in methods:
                    meths.append(meth.strip()) # take off annoying spaces
                methods=""
                for meth in meths:
                    if meth.strip() not in methcodes and "LP-" in meth:methcodes.append(meth.strip())
                    methods=methods+meth+":"
                methods=methods[:-1]
                rec["magic_method_codes"]=methods 
                if "LP-PI-TRM" in meths: datablock.append(rec)
                if "LP-TRM" in meths: trmblock.append(rec)
                if "LP-TRM-TD" in meths: tdsrecs.append(rec)
        if len(trmblock)>2 and inspec!="":
            if Tinit==0:
                Tinit=1
                AZD['TRM']=5
                pmagplotlib.plot_init(AZD['TRM'],5,5)
        elif Tinit==1: # clear the TRM figure if not needed
            pmagplotlib.clearFIG(AZD['TRM'])
        if len(tdsrecs)>2:
            if TDinit==0:
                TDinit=1
                AZD['TDS']=6
                pmagplotlib.plot_init(AZD['TDS'],5,5)
        elif TDinit==1: # clear the TDS figure if not needed
            pmagplotlib.clearFIG(AZD['TDS'])
        if len(datablock) <4:
           if backup==0:
               specimen+=1
               if verbose:
                   print 'skipping specimen - moving forward ', s
           else:
               specimen-=1
               if verbose:
                   print 'skipping specimen - moving backward ', s
    #
    #  collect info for the PmagSpecRec dictionary
    #
        else:
           rec=datablock[0]
           PmagSpecRec["er_citation_names"]="This study"
           PmagSpecRec["er_specimen_name"]=s
           PmagSpecRec["er_sample_name"]=rec["er_sample_name"]
           PmagSpecRec["er_site_name"]=rec["er_site_name"]
           PmagSpecRec["er_location_name"]=rec["er_location_name"]
           locname=rec['er_location_name'].replace('/','-')
           if "er_expedition_name" in rec.keys():PmagSpecRec["er_expedition_name"]=rec["er_expedition_name"]
           if "magic_instrument_codes" not in rec.keys():rec["magic_instrument_codes"]=""
           PmagSpecRec["magic_instrument_codes"]=rec["magic_instrument_codes"]
           PmagSpecRec["measurement_step_unit"]="K"
           if "magic_experiment_name" not in rec.keys():
               rec["magic_experiment_name"]=""
           else:
               PmagSpecRec["magic_experiment_names"]=rec["magic_experiment_name"]
    
           meths=rec["magic_method_codes"].split()
       # sort data into types
           araiblock,field=pmag.sortarai(datablock,s,Zdiff)
           first_Z=araiblock[0]
           GammaChecks=araiblock[5]
           if len(first_Z)<3:
               if backup==0:
                   specimen+=1
                   if verbose:
                       print 'skipping specimen - moving forward ', s
               else:
                   specimen-=1
                   if verbose:
                       print 'skipping specimen - moving backward ', s
           else:
               backup=0
               zijdblock,units=pmag.find_dmag_rec(s,meas_data)
               recnum=0
               if verbose:
                   print "index step Dec   Inc  Int       Gamma"
                   for plotrec in zijdblock:
                       if GammaChecks!="":
                           gamma=""
                           for g in GammaChecks:
                               if g[0]==plotrec[0]-273:
                                   gamma=g[1]
                                   break
                       if gamma!="":
                           print '%i     %i %7.1f %7.1f %8.3e %7.1f' % (recnum,plotrec[0]-273,plotrec[1],plotrec[2],plotrec[3],gamma)
                       else:
                           print '%i     %i %7.1f %7.1f %8.3e ' % (recnum,plotrec[0]-273,plotrec[1],plotrec[2],plotrec[3])
                       recnum += 1
               pmagplotlib.plotAZ(AZD,araiblock,zijdblock,s,units[0])
               if verbose:pmagplotlib.drawFIGS(AZD)
               if len(tdsrecs)>2: # a TDS experiment
                   tdsblock=[] # make a list for the TDS  data
                   Mkeys=['measurement_magnitude','measurement_magn_moment','measurement_magn_volume','measuruement_magn_mass']
                   mkey,k="",0
                   while mkey=="" and k<len(Mkeys)-1: # find which type of intensity
                       key= Mkeys[k]
                       if key in tdsrecs[0].keys() and tdsrecs[0][key]!="": mkey=key
                       k+=1
                   if mkey=="":break # get outta here
                   Tnorm=""
                   for tdrec in tdsrecs:
                       meths=tdrec['magic_method_codes'].split(":")
                       for meth in meths: meth.replace(" ","") # strip off potential nasty spaces
                       if  'LT-T-I' in meths and Tnorm=="": # found first total TRM 
                           Tnorm=float(tdrec[mkey]) # normalize by total TRM 
                           tdsblock.append([273,zijdblock[0][3]/Tnorm,1.]) # put in the zero step
                       if  'LT-T-Z' in meths and Tnorm!="": # found a LP-TRM-TD demag step, now need complementary LT-T-Z from zijdblock
                           step=float(tdrec['treatment_temp'])
                           Tint=""
                           if mkey!="":
                               Tint=float(tdrec[mkey])
                           if Tint!="":
                               for zrec in zijdblock:
                                   if zrec[0]==step:  # found matching
                                       tdsblock.append([step,zrec[3]/Tnorm,Tint/Tnorm])
                                       break
                   if len(tdsblock)>2: 
                       pmagplotlib.plotTDS(AZD['TDS'],tdsblock,s+':LP-PI-TDS:')
                       if verbose:pmagplotlib(drawFIGS(AZD)) 
                   else: 
                       print "Something wrong here"
               if anis==1:   # look up anisotropy data for this specimen
                   AniSpec=""
                   for aspec in anis_data:
                       if aspec["er_specimen_name"]==PmagSpecRec["er_specimen_name"]:
                           AniSpec=aspec
                           if verbose: print 'Found anisotropy record...'
                           break
               if inspec !="":
                   if verbose: print 'Looking up saved interpretation....'
                   found = 0
                   for k in range(len(PriorRecs)):
                       try:
                         if PriorRecs[k]["er_specimen_name"]==s:
                           found =1
                           CurrRec.append(PriorRecs[k])
                           for j in range(len(zijdblock)):
                               if float(zijdblock[j][0])==float(PriorRecs[k]["measurement_step_min"]):start=j
                               if float(zijdblock[j][0])==float(PriorRecs[k]["measurement_step_max"]):end=j
                           pars,errcode=pmag.PintPars(datablock,araiblock,zijdblock,start,end,accept)
                           pars['measurement_step_unit']="K"
                           pars['experiment_type']='LP-PI-TRM'
                           del PriorRecs[k]  # put in CurrRec, take out of PriorRecs
                           if errcode!=1:
                               pars["specimen_lab_field_dc"]=field
                               pars["specimen_int"]=-1*field*pars["specimen_b"]
                               pars["er_specimen_name"]=s
                               if verbose:
                                   print 'Saved interpretation: '
                               pars,kill=pmag.scoreit(pars,PmagSpecRec,accept,'',verbose)
                               pmagplotlib.plotB(AZD,araiblock,zijdblock,pars)
                               if verbose:pmagplotlib.drawFIGS(AZD)
                               if len(trmblock)>2:
                                   blab=field
                                   best=pars["specimen_int"]
                                   Bs,TRMs=[],[]
                                   for trec in trmblock:
                                       Bs.append(float(trec['treatment_dc_field']))
                                       TRMs.append(float(trec['measurement_magn_moment']))
                                   NLpars=nlt.NLtrm(Bs,TRMs,best,blab,0) # calculate best fit parameters through TRM acquisition data, and get new banc
                                   Mp,Bp=[],[]
                                   for k in  range(int(max(Bs)*1e6)):
                                       Bp.append(float(k)*1e-6)
                                       npred=nlt.TRM(Bp[-1],NLpars['xopt'][0],NLpars['xopt'][1]) # predicted NRM for this field
                                       Mp.append(npred)
                                   pmagplotlib.plotTRM(AZD['TRM'],Bs,TRMs,Bp,Mp,NLpars,trec['magic_experiment_name'])
                                   PmagSpecRec['specimen_int']=NLpars['banc'] 
                                   if verbose:
                                       print 'Banc= ',float(NLpars['banc'])*1e6
                                       pmagplotlib.drawFIGS(AZD)
                               mpars=pmag.domean(araiblock[1],start,end,'DE-BFL')
                               if verbose:
                                       print 'pTRM direction= ','%7.1f'%(mpars['specimen_dec']),' %7.1f'%(mpars['specimen_inc']),' MAD:','%7.1f'%(mpars['specimen_mad'])
                               if AniSpec!="":
                                   CpTRM=pmag.Dir_anis_corr([mpars['specimen_dec'],mpars['specimen_inc']],AniSpec)
                                   AniSpecRec=pmag.doaniscorr(PmagSpecRec,AniSpec)
                                   if verbose:
                                       print 'Anisotropy corrected TRM direction= ','%7.1f'%(CpTRM[0]),' %7.1f'%(CpTRM[1])
                                       print 'Anisotropy corrected intensity= ',float(AniSpecRec['specimen_int'])*1e6
                           else:
                               print 'error on specimen ',s
                       except:
                         pass
                   if verbose and found==0: print  '    None found :(  ' 
               if spc!="":
                   if BEG!="": 
                       pars,errcode=pmag.PintPars(datablock,araiblock,zijdblock,BEG,END,accept)
                       pars['measurement_step_unit']="K"
                       pars["specimen_lab_field_dc"]=field
                       pars["specimen_int"]=-1*field*pars["specimen_b"]
                       pars["er_specimen_name"]=s
                       pars['specimen_grade']='' # ungraded
                       pmagplotlib.plotB(AZD,araiblock,zijdblock,pars)
                       if verbose:pmagplotlib.drawFIGS(AZD)
                       if len(trmblock)>2:
                           if inlt==0:
                               inlt=1
                           blab=field
                           best=pars["specimen_int"]
                           Bs,TRMs=[],[]
                           for trec in trmblock:
                               Bs.append(float(trec['treatment_dc_field']))
                               TRMs.append(float(trec['measurement_magn_moment']))
                           NLpars=nlt.NLtrm(Bs,TRMs,best,blab,0) # calculate best fit parameters through TRM acquisition data, and get new banc
    #
                           Mp,Bp=[],[]
                           for k in  range(int(max(Bs)*1e6)):
                               Bp.append(float(k)*1e-6)
                               npred=nlt.TRM(Bp[-1],NLpars['xopt'][0],NLpars['xopt'][1]) # predicted NRM for this field
                   files={}
                   for key in AZD.keys():
                       files[key]=s+'_'+key+fmt 
                   pmagplotlib.saveP(AZD,files,dpi=dpi)
                   sys.exit()
               if verbose:
                   ans='b'
                   while ans != "":
                       print """
               s[a]ve plot, set [b]ounds for calculation, [d]elete current interpretation, [p]revious, [s]ample, [q]uit:
               """
                       ans=raw_input('Return for next specimen \n')
                       if ans=="": 
                           specimen +=1
                       if ans=="d": 
                           save_redo(PriorRecs,inspec)
                           CurrRec=[]
                           pmagplotlib.plotAZ(AZD,araiblock,zijdblock,s,units[0])
                           if verbose:pmagplotlib.drawFIGS(AZD)
                       if ans=='a':
                           files={}
                           for key in AZD.keys():
                               files[key]="LO:_"+locname+'_SI:_'+PmagSpecRec['er_site_name']+'_SA:_'+PmagSpecRec['er_sample_name']+'_SP:_'+s+'_CO:_s_TY:_'+key+fmt
                           pmagplotlib.saveP(AZD,files)
                           ans=""
                       if ans=='q':
                           print "Good bye"
                           sys.exit()
                       if ans=='p':
                           specimen =specimen -1
                           backup = 1
                           ans=""
                       if ans=='s':
                           keepon=1
                           spec=raw_input('Enter desired specimen name (or first part there of): ')
                           while keepon==1:
                               try:
                                   specimen =sids.index(spec)
                                   keepon=0
                               except:
                                   tmplist=[]
                                   for qq in range(len(sids)):
                                       if spec in sids[qq]:tmplist.append(sids[qq])
                                   print specimen," not found, but this was: "
                                   print tmplist
                                   spec=raw_input('Select one or try again\n ')
                           ans=""
                       if  ans=='b':
                           if end==0 or end >=len(zijdblock):end=len(zijdblock)-1
                           GoOn=0
                           while GoOn==0:
                               answer=raw_input('Enter index of first point for calculation: ['+str(start)+']  ')
                               try:
                                   start=int(answer)
                                   answer=raw_input('Enter index  of last point for calculation: ['+str(end)+']  ')
                                   end=int(answer)
                                   if start >=0 and start <len(zijdblock)-2 and end >0 and end <len(zijdblock) or start>=end:
                                       GoOn=1
                                   else:
                                       print "Bad endpoints - try again! "
                                       start,end=0,len(zijdblock)
                               except ValueError:
                                   print "Bad endpoints - try again! "
                                   start,end=0,len(zijdblock)
                           s=sids[specimen] 
                           pars,errcode=pmag.PintPars(datablock,araiblock,zijdblock,start,end,accept)
                           pars['measurement_step_unit']="K"
                           pars["specimen_lab_field_dc"]=field
                           pars["specimen_int"]=-1*field*pars["specimen_b"]
                           pars["er_specimen_name"]=s
                           pars,kill=pmag.scoreit(pars,PmagSpecRec,accept,'',0)
                           PmagSpecRec['specimen_scat']=pars['specimen_scat']
                           PmagSpecRec['specimen_frac']='%5.3f'%(pars['specimen_frac'])
                           PmagSpecRec['specimen_gmax']='%5.3f'%(pars['specimen_gmax'])
                           PmagSpecRec["measurement_step_min"]='%8.3e' % (pars["measurement_step_min"])
                           PmagSpecRec["measurement_step_max"]='%8.3e' % (pars["measurement_step_max"])
                           PmagSpecRec["measurement_step_unit"]="K"
                           PmagSpecRec["specimen_int_n"]='%i'%(pars["specimen_int_n"])
                           PmagSpecRec["specimen_lab_field_dc"]='%8.3e'%(pars["specimen_lab_field_dc"])
                           PmagSpecRec["specimen_int"]='%9.4e '%(pars["specimen_int"])
                           PmagSpecRec["specimen_b"]='%5.3f '%(pars["specimen_b"])
                           PmagSpecRec["specimen_q"]='%5.1f '%(pars["specimen_q"])
                           PmagSpecRec["specimen_f"]='%5.3f '%(pars["specimen_f"])
                           PmagSpecRec["specimen_fvds"]='%5.3f'%(pars["specimen_fvds"])
                           PmagSpecRec["specimen_b_beta"]='%5.3f'%(pars["specimen_b_beta"])
                           PmagSpecRec["specimen_int_mad"]='%7.1f'%(pars["specimen_int_mad"])
                           PmagSpecRec["specimen_Z"]='%7.1f'%(pars["specimen_Z"])
                           PmagSpecRec["specimen_gamma"]='%7.1f'%(pars["specimen_gamma"])
                           PmagSpecRec["specimen_grade"]=pars["specimen_grade"]
                           if pars["method_codes"]!="":
                               tmpcodes=pars["method_codes"].split(":")
                               for t in tmpcodes:
                                   if t.strip() not in methcodes:methcodes.append(t.strip())
                           PmagSpecRec["specimen_dec"]='%7.1f'%(pars["specimen_dec"])
                           PmagSpecRec["specimen_inc"]='%7.1f'%(pars["specimen_inc"])
                           PmagSpecRec["specimen_tilt_correction"]='-1'
                           PmagSpecRec["specimen_direction_type"]='l'
                           PmagSpecRec["direction_type"]='l' # this is redundant, but helpful - won't be imported
                           PmagSpecRec["specimen_int_dang"]='%7.1f '%(pars["specimen_int_dang"])
                           PmagSpecRec["specimen_drats"]='%7.1f '%(pars["specimen_drats"])
                           PmagSpecRec["specimen_drat"]='%7.1f '%(pars["specimen_drat"])
                           PmagSpecRec["specimen_int_ptrm_n"]='%i '%(pars["specimen_int_ptrm_n"])
                           PmagSpecRec["specimen_rsc"]='%6.4f '%(pars["specimen_rsc"])
                           PmagSpecRec["specimen_md"]='%i '%(int(pars["specimen_md"]))
                           if PmagSpecRec["specimen_md"]=='-1':PmagSpecRec["specimen_md"]=""
                           PmagSpecRec["specimen_b_sigma"]='%5.3f '%(pars["specimen_b_sigma"])
                           if "IE-TT" not in  methcodes:methcodes.append("IE-TT")
                           methods=""
                           for meth in methcodes:
                               methods=methods+meth+":"
                           PmagSpecRec["magic_method_codes"]=methods[:-1]
                           PmagSpecRec["specimen_description"]=comment
                           PmagSpecRec["magic_software_packages"]=version_num
                           pmagplotlib.plotAZ(AZD,araiblock,zijdblock,s,units[0])
                           pmagplotlib.plotB(AZD,araiblock,zijdblock,pars)
                           if verbose:pmagplotlib.drawFIGS(AZD)
                           if len(trmblock)>2:
                               blab=field
                               best=pars["specimen_int"]
                               Bs,TRMs=[],[]
                               for trec in trmblock:
                                   Bs.append(float(trec['treatment_dc_field']))
                                   TRMs.append(float(trec['measurement_magn_moment']))
                               NLpars=nlt.NLtrm(Bs,TRMs,best,blab,0) # calculate best fit parameters through TRM acquisition data, and get new banc
                               Mp,Bp=[],[]
                               for k in  range(int(max(Bs)*1e6)):
                                   Bp.append(float(k)*1e-6)
                                   npred=nlt.TRM(Bp[-1],NLpars['xopt'][0],NLpars['xopt'][1]) # predicted NRM for this field
                                   Mp.append(npred)
                               pmagplotlib.plotTRM(AZD['TRM'],Bs,TRMs,Bp,Mp,NLpars,trec['magic_experiment_name'])
                               if verbose:
                                   print 'Non-linear TRM corrected intensity= ',float(NLpars['banc'])*1e6
                           if verbose:pmagplotlib.drawFIGS(AZD)
                           pars["specimen_lab_field_dc"]=field
                           pars["specimen_int"]=-1*field*pars["specimen_b"]
                           pars,kill=pmag.scoreit(pars,PmagSpecRec,accept,'',verbose)
                           saveit=raw_input("Save this interpretation? [y]/n \n")
                           if saveit!='n':
                               PriorRecs.append(PmagSpecRec) # put back an interpretation
                               specimen+=1
                               save_redo(PriorRecs,inspec)
                           ans=""
               elif plots==1:
                   specimen+=1
                   if fmt != ".pmag":
                       files={}
                       for key in AZD.keys():
                           files[key]="LO:_"+locname+'_SI:_'+PmagSpecRec['er_site_name']+'_SA:_'+PmagSpecRec['er_sample_name']+'_SP:_'+s+'_CO:_s_TY:_'+key+'_'+fmt
                       if pmagplotlib.isServer:
                           black     = '#000000'
                           purple    = '#800080'
                           titles={}
                           titles['deremag']='DeReMag Plot'
                           titles['zijd']='Zijderveld Plot'
                           titles['arai']='Arai Plot'
                           AZD = pmagplotlib.addBorders(AZD,titles,black,purple)
                       pmagplotlib.saveP(AZD,files,dpi=dpi)
    #                   pmagplotlib.combineFigs(s,files,3)
                   else:  # save in pmag format 
                       script="grep "+s+" output.mag | thellier -mfsi"
                       script=script+' %8.4e'%(field)
                       min='%i'%((pars["measurement_step_min"]-273))
                       Max='%i'%((pars["measurement_step_max"]-273))
                       script=script+" "+min+" "+Max
                       script=script+" |plotxy;cat mypost >>thellier.ps\n"
                       pltf.write(script)
                       pmag.domagicmag(outf,MeasRecs)
        if len(CurrRec)>0:
            for rec in CurrRec:
                PriorRecs.append(rec)
        CurrRec=[]
    if plots!=1 and verbose:
        ans=raw_input(" Save last plot? 1/[0] ")
        if ans=="1":
            if fmt != ".pmag":
                files={}
                for key in AZD.keys():
                    files[key]=s+'_'+key+fmt
                pmagplotlib.saveP(AZD,files,dpi=dpi)
        else:
            print "\n Good bye\n"
            sys.exit()
        if len(CurrRec)>0:PriorRecs.append(CurrRec) # put back an interpretation
        if len(PriorRecs)>0:
            save_redo(PriorRecs,inspec)
            print 'Updated interpretations saved in ',inspec
    if verbose:
        print "Good bye"
示例#5
0
def main():
    """
    NAME
        zeq_magic.py

    DESCRIPTION
        reads in magic_measurements formatted file, makes plots of remanence decay
        during demagnetization experiments.  Reads in prior interpretations saved in 
        a pmag_specimens formatted file and  allows re-interpretations of best-fit lines
        and planes and saves (revised or new) interpretations in a pmag_specimens file.  
        interpretations are saved in the coordinate system used. Also allows judicious editting of
        measurements to eliminate "bad" measurements.  These are marked as such in the magic_measurements
        input file.  they are NOT deleted, just ignored. 

    SYNTAX
        zeq_magic.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f  MEASFILE: sets magic_measurements format input file, default: magic_measurements.txt
        -fsp SPECFILE: sets pmag_specimens format file with prior interpreations, default: zeq_specimens.txt
        -Fp PLTFILE: sets filename for saved plot, default is name_type.fmt (where type is zijd, eqarea or decay curve)
        -crd [s,g,t]: sets coordinate system,  g=geographic, t=tilt adjusted, default: specimen coordinate system
        -fsa SAMPFILE: sets er_samples format file with orientation information, default: er_samples.txt
        -spc SPEC  plots single specimen SPEC, saves plot with specified format 
              with optional -dir settings and quits
        -dir [L,P,F][beg][end]: sets calculation type for principal component analysis, default is none
             beg: starting step for PCA calculation
             end: ending step for PCA calculation
             [L,P,F]: calculation type for line, plane or fisher mean
             must be used with -spc option
        -fmt FMT: set format of saved plot [png,svg,jpg]
        -A:  suppresses averaging of  replicate measurements, default is to average
        -sav: saves all plots without review
    SCREEN OUTPUT:
        Specimen, N, a95, StepMin, StepMax, Dec, Inc, calculation type

    """
    # initialize some variables
    doave,e,b=1,0,0 # average replicates, initial end and beginning step
    plots,coord=0,'s'
    noorient=0
    version_num=pmag.get_version()
    verbose=pmagplotlib.verbose
    beg_pca,end_pca,direction_type="","",'l'
    calculation_type,fmt="","svg"
    user,spec_keys,locname="",[],''
    plot_file=""
    sfile=""
    plot_file=""
    PriorRecs=[] # empty list for prior interpretations
    backup=0
    specimen="" # can skip everything and just plot one specimen with bounds e,b
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-WD' in sys.argv:
        ind=sys.argv.index('-WD')
        dir_path=sys.argv[ind+1]
    else:
        dir_path='.'
    inspec=dir_path+'/'+'zeq_specimens.txt'
    meas_file,geo,tilt,ask,samp_file=dir_path+'/magic_measurements.txt',0,0,0,dir_path+'/er_samples.txt'
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        meas_file=dir_path+'/'+sys.argv[ind+1]
    if '-fsp' in sys.argv:
        ind=sys.argv.index('-fsp')
        inspec=dir_path+'/'+sys.argv[ind+1]
    if '-fsa' in sys.argv:
        ind=sys.argv.index('-fsa')
        samp_file=dir_path+'/'+sys.argv[ind+1]
        sfile='ok'
    if '-crd' in sys.argv:
        ind=sys.argv.index('-crd')
        coord=sys.argv[ind+1]
        if coord=='g' or coord=='t':
            samp_data,file_type=pmag.magic_read(samp_file)
            if file_type=='er_samples':sfile='ok'
            geo=1
            if coord=='t':tilt=1
    if '-spc' in sys.argv:
        ind=sys.argv.index('-spc')
        specimen=sys.argv[ind+1]
        if '-dir' in sys.argv:
            ind=sys.argv.index('-dir')
            direction_type=sys.argv[ind+1]
            beg_pca=int(sys.argv[ind+2])
            end_pca=int(sys.argv[ind+3])
            if direction_type=='L':calculation_type='DE-BFL'
            if direction_type=='P':calculation_type='DE-BFP'
            if direction_type=='F':calculation_type='DE-FM'
        if '-Fp' in sys.argv: 
            ind=sys.argv.index('-Fp')
            plot_file=dir_path+'/'+sys.argv[ind+1]
    if '-A' in sys.argv: doave=0
    if '-sav' in sys.argv: 
        plots=1
        verbose=0
    if '-fmt' in sys.argv:
        ind=sys.argv.index('-fmt')
        fmt=sys.argv[ind+1]
    #
    first_save=1
    meas_data,file_type=pmag.magic_read(meas_file)
    changeM,changeS=0,0 # check if data or interpretations have changed
    if file_type != 'magic_measurements':
        print file_type
        print file_type,"This is not a valid magic_measurements file " 
        sys.exit()
    for rec in  meas_data:
        if  "magic_method_codes" not in rec.keys(): rec["magic_method_codes"]=""
        methods=""
        tmp=rec["magic_method_codes"].replace(" ","").split(":")
        for meth in tmp:
            methods=methods+meth+":"
        rec["magic_method_codes"]=methods[:-1]  # get rid of annoying spaces in Anthony's export files 
        if "magic_instrument_codes" not in rec.keys() :rec["magic_instrument_codes"]=""
    PriorSpecs=[]
    PriorRecs,file_type=pmag.magic_read(inspec)
    if len(PriorRecs)==0: 
        if verbose:print "starting new file ",inspec
    for Rec in PriorRecs:
        if 'magic_software_packages' not in Rec.keys():Rec['magic_software_packages']=""
        if Rec['er_specimen_name'] not in PriorSpecs:
            if 'specimen_comp_name' not in Rec.keys():Rec['specimen_comp_name']="A"
            PriorSpecs.append(Rec['er_specimen_name'])
        else:
            if 'specimen_comp_name' not in Rec.keys():Rec['specimen_comp_name']="A"
        if "magic_method_codes" in Rec.keys():
            methods=[]
            tmp=Rec["magic_method_codes"].replace(" ","").split(":")
            for meth in tmp:
                methods.append(meth)
            if 'DE-FM' in methods:
                Rec['calculation_type']='DE-FM' # this won't be imported but helps
            if 'DE-BFL' in methods:
                Rec['calculation_type']='DE-BFL'
            if 'DE-BFL-A' in methods:
                Rec['calculation_type']='DE-BFL-A'
            if 'DE-BFL-O' in methods:
                Rec['calculation_type']='DE-BFL-O'
            if 'DE-BFP' in methods:
                Rec['calculation_type']='DE-BFP'
        else:
            Rec['calculation_type']='DE-BFL' # default is to assume a best-fit line
    #
    # get list of unique specimen names
    #
    sids=pmag.get_specs(meas_data)
    #
    #  set up plots, angle sets X axis to horizontal,  direction_type 'l' is best-fit line
    # direction_type='p' is great circle
    #     
    #
    # draw plots for sample s - default is just to step through zijderveld diagrams
    #
    #
    # define figure numbers for equal area, zijderveld,  
    #  and intensity vs. demagnetiztion step respectively
    ZED={}
    ZED['eqarea'],ZED['zijd'],  ZED['demag']=1,2,3 
    pmagplotlib.plot_init(ZED['eqarea'],5,5)
    pmagplotlib.plot_init(ZED['zijd'],6,5)
    pmagplotlib.plot_init(ZED['demag'],5,5)
    save_pca=0
    if specimen=="":
        k = 0
    else:
        k=sids.index(specimen)
    angle,direction_type="",""
    setangle=0
    CurrRecs=[]
    while k < len(sids):
        CurrRecs=[]
        if setangle==0:angle=""
        method_codes,inst_code=[],""
        s=sids[k]
        PmagSpecRec={}
        PmagSpecRec["er_analyst_mail_names"]=user
        PmagSpecRec['magic_software_packages']=version_num
        PmagSpecRec['specimen_description']=""
        PmagSpecRec['magic_method_codes']=""
        if verbose and  s!="":print s, k , 'out of ',len(sids)
    #
    #  collect info for the PmagSpecRec dictionary
    #
        s_meas=pmag.get_dictitem(meas_data,'er_specimen_name',s,'T') # fish out this specimen
        s_meas=pmag.get_dictitem(s_meas,'magic_method_codes','Z','has') # fish out zero field steps
        if len(s_meas)>0:
          for rec in  s_meas: # fix up a few things for the output record
               PmagSpecRec["magic_instrument_codes"]=rec["magic_instrument_codes"]  # copy over instruments
               PmagSpecRec["er_citation_names"]="This study"
               PmagSpecRec["er_specimen_name"]=s
               PmagSpecRec["er_sample_name"]=rec["er_sample_name"]
               PmagSpecRec["er_site_name"]=rec["er_site_name"]
               PmagSpecRec["er_location_name"]=rec["er_location_name"]
               locname=rec['er_location_name']
               if 'er_expedition_name' in rec.keys(): PmagSpecRec["er_expedition_name"]=rec["er_expedition_name"]
               PmagSpecRec["magic_method_codes"]=rec["magic_method_codes"]
               if "magic_experiment_name" not in rec.keys():
                   PmagSpecRec["magic_experiment_names"]=""
               else:    
                   PmagSpecRec["magic_experiment_names"]=rec["magic_experiment_name"]
               break
    #
    # find the data from the meas_data file for this specimen
    #
          data,units=pmag.find_dmag_rec(s,meas_data)
          PmagSpecRec["measurement_step_unit"]= units
          u=units.split(":")
          if "T" in units:PmagSpecRec["magic_method_codes"]=PmagSpecRec["magic_method_codes"]+":LP-DIR-AF"
          if "K" in units:PmagSpecRec["magic_method_codes"]=PmagSpecRec["magic_method_codes"]+":LP-DIR-T"
          if "J" in units:PmagSpecRec["magic_method_codes"]=PmagSpecRec["magic_method_codes"]+":LP-DIR-M"
    #
    # find prior interpretation
    #
          if len(CurrRecs)==0: # check if already in
            beg_pca,end_pca="",""
            calculation_type=""
            if inspec !="":
              if verbose: print "    looking up previous interpretations..."
              precs=pmag.get_dictitem(PriorRecs,'er_specimen_name',s,'T') # get all the prior recs with this specimen name
              precs=pmag.get_dictitem(precs,'magic_method_codes','LP-DIR','has') # get the directional data
              PriorRecs=pmag.get_dictitem(PriorRecs,'er_specimen_name',s,'F') # take them all out of prior recs
         # get the ones that meet the current coordinate system
              for prec in precs:
                if 'specimen_tilt_correction' not in prec.keys() or prec['specimen_tilt_correction']=='-1':
                    crd='s'
                elif prec['specimen_tilt_correction']=='0':
                    crd='g'
                elif prec['specimen_tilt_correction']=='100':
                    crd='t'
                else:
                    crd='?'
                CurrRec={}
                for key in prec.keys():CurrRec[key]=prec[key]
                CurrRecs.append(CurrRec) # put in CurrRecs
                method_codes= CurrRec["magic_method_codes"].replace(" ","").split(':')
                calculation_type='DE-BFL'
                if 'DE-FM' in method_codes: calculation_type='DE-FM'
                if 'DE-BFP' in method_codes: calculation_type='DE-BFP'
                if 'DE-BFL-A' in method_codes: calculation_type='DE-BFL-A'
                if 'specimen_dang' not in CurrRec.keys():
                    if verbose:print 'Run mk_redo.py and zeq_magic_redo.py to get the specimen_dang values'
                    CurrRec['specimen_dang']=-1
                if calculation_type!='DE-FM' and crd==coord: # not a fisher mean
                    if verbose:print "Specimen  N    MAD    DANG  start     end      dec     inc  type  component coordinates"
                    if units=='K':
                            if verbose:print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f  %s  %s       %s \n' % (CurrRec["er_specimen_name"],int(CurrRec["specimen_n"]),float(CurrRec["specimen_mad"]),float(CurrRec["specimen_dang"]),float(CurrRec["measurement_step_min"])-273,float(CurrRec["measurement_step_max"])-273,float(CurrRec["specimen_dec"]),float(CurrRec["specimen_inc"]),calculation_type,CurrRec['specimen_comp_name'],crd)
                    elif units=='T':
                       if verbose:print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f  %s  %s %s \n' % (CurrRec["er_specimen_name"],int(CurrRec["specimen_n"]),float(CurrRec["specimen_mad"]),float(CurrRec["specimen_dang"]),float(CurrRec["measurement_step_min"])*1e3,float(CurrRec["measurement_step_max"])*1e3,float(CurrRec["specimen_dec"]),float(CurrRec["specimen_inc"]),calculation_type,CurrRec['specimen_comp_name'],crd)
                    elif 'T' in units and 'K' in units:
                            if float(CurrRec['measurement_step_min'])<1.0 :
                                min=float(CurrRec['measurement_step_min'])*1e3
                            else:
                                min=float(CurrRec['measurement_step_min'])-273
                            if float(CurrRec['measurement_step_max'])<1.0 :
                                max=float(CurrRec['measurement_step_max'])*1e3
                            else:
                                max=float(CurrRec['measurement_step_max'])-273
                            if verbose:print '%s %i %7.1f %i %i %7.1f %7.1f %7.1f, %s        %s\n' % (CurrRec["er_specimen_name"],int(CurrRec["specimen_n"]),float(CurrRec["specimen_mad"]),float(CurrRec['specimen_dang']),min,max,float(CurrRec["specimen_dec"]),float(CurrRec["specimen_inc"]),calculation_type,crd)
                    elif 'J' in units:
                       if verbose:print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f  %s  %s       %s \n' % (CurrRec["er_specimen_name"],int(CurrRec["specimen_n"]),float(CurrRec["specimen_mad"]),float(CurrRec['specimen_dang']),float(CurrRec["measurement_step_min"]),float(CurrRec["measurement_step_max"]),float(CurrRec["specimen_dec"]),float(CurrRec["specimen_inc"]),calculation_type,CurrRec['specimen_comp_name'],crd)
                elif calculation_type=='DE-FM' and crd==coord: # fisher mean
                    if verbose:print "Specimen  a95 DANG   start     end      dec     inc  type  component coordinates"
                    if units=='K':
                         if verbose:print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f  %s  %s       %s \n' % (CurrRec["er_specimen_name"],int(CurrRec["specimen_n"]),float(CurrRec["specimen_alpha95"]),float(CurrRec["measurement_step_min"])-273,float(CurrRec["measurement_step_max"])-273,float(CurrRec["specimen_dec"]),float(CurrRec["specimen_inc"]),calculation_type,CurrRec['specimen_comp_name'],crd)
                    elif units=='T':
                          if verbose:print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f  %s  %s       %s \n' % (CurrRec["er_specimen_name"],int(CurrRec["specimen_n"]),float(CurrRec["specimen_alpha95"]),float(CurrRec["measurement_step_min"])*1e3,float(CurrRec["measurement_step_max"])*1e3,float(CurrRec["specimen_dec"]),float(CurrRec["specimen_inc"]),calculation_type,CurrRec['specimen_comp_name'],crd)
                    elif 'T' in units and 'K' in units:
                            if float(CurrRec['measurement_step_min'])<1.0 :
                                min=float(CurrRec['measurement_step_min'])*1e3
                            else:
                                min=float(CurrRec['measurement_step_min'])-273
                            if float(CurrRec['measurement_step_max'])<1.0 :
                                max=float(CurrRec['measurement_step_max'])*1e3
                            else:
                                max=float(CurrRec['measurement_step_max'])-273
                            if verbose:print '%s %i %7.1f %i %i %7.1f %7.1f %s       %s \n' % (CurrRec["er_specimen_name"],int(CurrRec["specimen_n"]),float(CurrRec["specimen_alpha95"]),min,max,float(CurrRec["specimen_dec"]),float(CurrRec["specimen_inc"]),calculation_type,crd)
                    elif 'J' in units:
                       if verbose:print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %s %s       %s \n' % (CurrRec["er_specimen_name"],int(CurrRec["specimen_n"]),float(CurrRec["specimen_mad"]),float(CurrRec["measurement_step_min"]),float(CurrRec["measurement_step_max"]),float(CurrRec["specimen_dec"]),float(CurrRec["specimen_inc"]),calculation_type,CurrRec['specimen_comp_name'],crd)
              if len(CurrRecs)==0:beg_pca,end_pca="",""
          datablock=data
          noskip=1
          if len(datablock) <3: 
            noskip=0
            if backup==0:
                k+=1
            else:
                k-=1
            if len(CurrRecs)>0:
                for rec in CurrRecs:
                    PriorRecs.append(rec)
            CurrRecs=[]
          else:
            backup=0 
          if noskip:
        #
        # find replicate measurements at given treatment step and average them
        #
#            step_meth,avedata=pmag.vspec(data)
#            if len(avedata) != len(datablock):
#                if doave==1: 
#                    method_codes.append("DE-VM")
#                    datablock=avedata
#        #
        # do geo or stratigraphic correction now
        #
            if geo==1:
        #
        # find top priority orientation method
                orient,az_type=pmag.get_orient(samp_data,PmagSpecRec["er_sample_name"])
                if az_type=='SO-NO':
                    if verbose: print "no orientation data for ",s 
                    orient["sample_azimuth"]=0
                    orient["sample_dip"]=0
                    noorient=1
                    method_codes.append("SO-NO")
                    orient["sample_azimuth"]=0
                    orient["sample_dip"]=0
                    orient["sample_bed_dip_azimuth"]=0
                    orient["sample_bed_dip"]=0
                    noorient=1
                    method_codes.append("SO-NO")
                else: 
                    noorient=0
        #
        #  if stratigraphic selected,  get stratigraphic correction
        #
                tiltblock,geoblock=[],[]
                for rec in datablock:
                    d_geo,i_geo=pmag.dogeo(rec[1],rec[2],float(orient["sample_azimuth"]),float(orient["sample_dip"]))
                    geoblock.append([rec[0],d_geo,i_geo,rec[3],rec[4],rec[5],rec[6]])
                    if tilt==1 and "sample_bed_dip" in orient.keys() and float(orient['sample_bed_dip'])!=0: 
                        d_tilt,i_tilt=pmag.dotilt(d_geo,i_geo,float(orient["sample_bed_dip_direction"]),float(orient["sample_bed_dip"]))
                        tiltblock.append([rec[0],d_tilt,i_tilt,rec[3],rec[4],rec[5],rec[6]])
                    if tilt==1: plotblock=tiltblock
                    if geo==1 and tilt==0:plotblock=geoblock
            if geo==0 and tilt==0: plotblock=datablock
    #
    # set the end pca point to last point  if not set
            if e==0 or e>len(plotblock)-1: e=len(plotblock)-1
            if angle=="": angle=plotblock[0][1] # rotate to NRM declination
            title=s+'_s'
            if geo==1 and tilt==0 and noorient!=1:title=s+'_g'
            if tilt==1 and noorient!=1:title=s+'_t'
            pmagplotlib.plotZED(ZED,plotblock,angle,title,units)
            if verbose:pmagplotlib.drawFIGS(ZED)
            if len(CurrRecs)!=0:
                for prec in CurrRecs:
                    if 'calculation_type' not in prec.keys():
                        calculation_type=''
                    else:
                        calculation_type=prec["calculation_type"]
                    direction_type=prec["specimen_direction_type"]
                    if calculation_type !="":
                        beg_pca,end_pca="",""
                        for j in range(len(datablock)):
                            if data[j][0]==float(prec["measurement_step_min"]):beg_pca=j
                            if data[j][0]==float(prec["measurement_step_max"]):end_pca=j
                        if beg_pca=="" or end_pca=="":  
                            if verbose:
                                print "something wrong with prior interpretation "
                            break
                    if calculation_type!="":
                        if beg_pca=="":beg_pca=0
                        if end_pca=="":end_pca=len(plotblock)-1
                        if geo==1 and tilt==0:
                            mpars=pmag.domean(geoblock,beg_pca,end_pca,calculation_type)
                            if mpars["specimen_direction_type"]!="Error":
                                pmagplotlib.plotDir(ZED,mpars,geoblock,angle)
                                if verbose:pmagplotlib.drawFIGS(ZED)
                        if geo==1 and tilt==1:
                            mpars=pmag.domean(tiltblock,beg_pca,end_pca,calculation_type)
                            if mpars["specimen_direction_type"]!="Error":
                                pmagplotlib.plotDir(ZED,mpars,tiltblock,angle)
                                if verbose:pmagplotlib.drawFIGS(ZED)
                        if geo==0 and tilt==0: 
                            mpars=pmag.domean(datablock,beg_pca,end_pca,calculation_type)
                        if mpars["specimen_direction_type"]!="Error":
                                pmagplotlib.plotDir(ZED,mpars,plotblock,angle)
                                if verbose:pmagplotlib.drawFIGS(ZED)
    #
    # print out data for this sample to screen
    #
            recnum=0
            for plotrec in plotblock:
                if units=='T' and verbose: print '%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' % (plotrec[5], recnum,plotrec[0]*1e3," mT",plotrec[3],plotrec[1],plotrec[2],plotrec[6])
                if units=="K" and verbose: print '%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' % (plotrec[5], recnum,plotrec[0]-273,' C',plotrec[3],plotrec[1],plotrec[2],plotrec[6])
                if units=="J" and verbose: print '%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' % (plotrec[5], recnum,plotrec[0],' J',plotrec[3],plotrec[1],plotrec[2],plotrec[6])
                if 'K' in units and 'T' in units:
                    if plotrec[0]>=1. and verbose: print '%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' % (plotrec[5], recnum,plotrec[0]-273,' C',plotrec[3],plotrec[1],plotrec[2],plotrec[6])
                    if plotrec[0]<1. and  verbose: print '%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' % (plotrec[5], recnum,plotrec[0]*1e3," mT",plotrec[3],plotrec[1],plotrec[2],plotrec[6])
                recnum += 1
            if specimen!="":
                if plot_file=="":
                    basename=locname+'_'+s
                else:
                    basename=plot_file
                files={}
                for key in ZED.keys():
                    files[key]=basename+'_'+key+'.'+fmt 
                pmagplotlib.saveP(ZED,files)
                sys.exit()
            else:  # interactive
              if plots==0:
                ans='b'
                k+=1
                changeS=0
                while ans != "":
                    if len(CurrRecs)==0:
                        print """
                g/b: indicates  good/bad measurement.  "bad" measurements excluded from calculation

                set s[a]ve plot, [b]ounds for pca and calculate, [p]revious, [s]pecimen, 
                 change [h]orizontal projection angle,   change [c]oordinate systems, 
                 [e]dit data,  [q]uit: 
                """
                    else:
                        print """
                g/b: indicates  good/bad measurement.  "bad" measurements excluded from calculation

                 set s[a]ve plot, [b]ounds for pca and calculate, [p]revious, [s]pecimen, 
                 change [h]orizontal projection angle,   change [c]oordinate systems, 
                 [d]elete current interpretation(s), [e]dit data,   [q]uit: 
                """
                    ans=raw_input('<Return>  for  next specimen \n')
                    setangle=0
                    if ans=='d': # delete this interpretation
                        CurrRecs=[]
                        k-=1 # replot same specimen
                        ans=""
                        changeS=1
                    if  ans=='q': 
                        if changeM==1:
                            ans=raw_input('Save changes to magic_measurements.txt? y/[n] ')
                            if ans=='y':
                                pmag.magic_write(meas_file,meas_data,'magic_measurements')
                        print "Good bye"
                        sys.exit()
                    if  ans=='a':
                        if plot_file=="":
                            basename=locname+'_'+s+'_'
                        else:
                            basename=plot_file
                        files={}
                        for key in ZED.keys():
                            files[key]=basename+'_'+coord+'_'+key+'.'+fmt 
                        pmagplotlib.saveP(ZED,files)
                        ans=""
                    if  ans=='p':
                        k-=2
                        ans=""
                        backup=1
                    if ans=='c':
                        k-=1 # replot same block
                        if tilt==0 and geo ==1:print "You  are currently viewing geographic  coordinates "
                        if tilt==1 and geo ==1:print "You  are currently viewing stratigraphic  coordinates "
                        if tilt==0 and geo ==0: print "You  are currently viewing sample coordinates "
                        print "\n Which coordinate system do you wish to view? "
                        coord=raw_input(" <Return>  specimen, [g] geographic, [t] tilt corrected ")
                        if coord=="g":geo,tilt=1,0
                        if coord=="t":
                            geo=1
                            tilt=1
                        if coord=="":
                            coord='s'
                            geo=0
                            tilt=0
                        if geo==1 and sfile=="":
                            samp_file=raw_input(" Input er_samples file for sample orientations [er_samples.txt] " )
                            if samp_file=="":samp_file="er_samples.txt"
                            samp_data,file_type=pmag.magic_read(samp_file)
                            if file_type != 'er_samples':
                               print file_type
                               print "This is not a valid er_samples file - coordinate system not changed" 
                            else:
                               sfile="ok"
                        ans=""
                    if ans=='s':
                        keepon=1
                        sample=raw_input('Enter desired specimen name (or first part there of): ')
                        while keepon==1:
                            try:
                                k =sids.index(sample)
                                keepon=0
                            except:
                                tmplist=[]
                                for qq in range(len(sids)):
                                    if sample in sids[qq]:tmplist.append(sids[qq])
                                print sample," not found, but this was: "
                                print tmplist
                                sample=raw_input('Select one or try again\n ')
                        angle,direction_type="",""
                        setangle=0
                        ans=""
                    if ans=='h':
                        k-=1
                        angle=raw_input("Enter desired  declination for X axis 0-360 ")
                        angle=float(angle)
                        if angle==0:angle=0.001
                        s=sids[k]
                        setangle=1
                        ans=""
                    if  ans=='e':
                        k-=1
                        ans=""
                        recnum=0
                        for plotrec in plotblock:
                            if plotrec[0]<=200 and verbose: print '%s: %i  %7.1f %s  %8.3e %7.1f %7.1f ' % (plotrec[5], recnum,plotrec[0]*1e3," mT",plotrec[3],plotrec[1],plotrec[2])
                            if plotrec[0]>200 and verbose: print '%s: %i  %7.1f %s  %8.3e %7.1f %7.1f ' % (plotrec[5], recnum,plotrec[0]-273,' C',plotrec[3],plotrec[1],plotrec[2])
                            recnum += 1
                        answer=raw_input('Enter index of point to change from bad to good or vice versa:  ')
                        try: 
                                ind=int(answer)
                                meas_data=pmag.mark_dmag_rec(s,ind,meas_data)
                                changeM=1
                        except:
                                'bad entry, try again'
                    if  ans=='b':
                        if end_pca=="":end_pca=len(plotblock)-1
                        if beg_pca=="":beg_pca=0
                        k-=1   # stay on same sample until through
                        GoOn=0
                        while GoOn==0:
                            print 'Enter index of first point for pca: ','[',beg_pca,']'
                            answer=raw_input('return to keep default  ')
                            if answer != "":
                                beg_pca=int(answer)
                            print 'Enter index  of last point for pca: ','[',end_pca,']'
                            answer=raw_input('return to keep default  ')
                            try:
                                end_pca=int(answer) 
                                if plotblock[beg_pca][5]=='b' or plotblock[end_pca][5]=='b': 
                                    print "Can't select 'bad' measurement for PCA bounds -try again"
                                    end_pca=len(plotblock)-1
                                    beg_pca=0
                                elif beg_pca >=0 and beg_pca<=len(plotblock)-2 and end_pca>0 and end_pca<len(plotblock): 
                                    GoOn=1
                                else:
                                    print beg_pca,end_pca, " are bad entry of indices - try again"
                                    end_pca=len(plotblock)-1
                                    beg_pca=0
                            except:
                                print beg_pca,end_pca, " are bad entry of indices - try again"
                                end_pca=len(plotblock)-1
                                beg_pca=0
                        GoOn=0
                        while GoOn==0:
                            if calculation_type!="":
                                print "Prior calculation type = ",calculation_type
                            ct=raw_input('Enter new Calculation Type: best-fit line,  plane or fisher mean [l]/p/f :  ' )
                            if ct=="" or ct=="l": 
                                direction_type="l"
                                calculation_type="DE-BFL"
                                GoOn=1
                            elif ct=='p':
                                direction_type="p"
                                calculation_type="DE-BFP"
                                GoOn=1
                            elif ct=='f':
                                direction_type="l"
                                calculation_type="DE-FM"
                                GoOn=1
                            else: 
                                print "bad entry of calculation type: try again. "
                        pmagplotlib.plotZED(ZED,plotblock,angle,s,units)
                        if verbose:pmagplotlib.drawFIGS(ZED)
                        if geo==1 and tilt==0:
                            mpars=pmag.domean(geoblock,beg_pca,end_pca,calculation_type)
                            if mpars['specimen_direction_type']=='Error':break
                            PmagSpecRec["specimen_dec"]='%7.1f ' %(mpars["specimen_dec"])
                            PmagSpecRec["specimen_inc"]='%7.1f ' %(mpars["specimen_inc"])
                            if "SO-NO" not in method_codes:
                                PmagSpecRec["specimen_tilt_correction"]='0'
                                method_codes.append("DA-DIR-GEO")
                            else:
                                PmagSpecRec["specimen_tilt_correction"]='-1'
                            pmagplotlib.plotDir(ZED,mpars,geoblock,angle)
                            if verbose:pmagplotlib.drawFIGS(ZED)
                        if geo==1 and  tilt==1:
                            mpars=pmag.domean(tiltblock,beg_pca,end_pca,calculation_type)
                            if mpars['specimen_direction_type']=='Error':break
                            PmagSpecRec["specimen_dec"]='%7.1f ' %(mpars["specimen_dec"])
                            PmagSpecRec["specimen_inc"]='%7.1f ' %(mpars["specimen_inc"])
                            if "SO-NO" not in method_codes:
                                PmagSpecRec["specimen_tilt_correction"]='100'
                                method_codes.append("DA-DIR-TILT")
                            else:
                                PmagSpecRec["specimen_tilt_correction"]='-1'
                            pmagplotlib.plotDir(ZED,mpars,tiltblock,angle)
                            if verbose:pmagplotlib.drawFIGS(ZED)
                        if geo==0 and tilt==0: 
                            mpars=pmag.domean(datablock,beg_pca,end_pca,calculation_type)
                            if mpars['specimen_direction_type']=='Error':break
                            PmagSpecRec["specimen_dec"]='%7.1f ' %(mpars["specimen_dec"])
                            PmagSpecRec["specimen_inc"]='%7.1f ' %(mpars["specimen_inc"])
                            PmagSpecRec["specimen_tilt_correction"]='-1'
                            pmagplotlib.plotDir(ZED,mpars,plotblock,angle)
                            if verbose:pmagplotlib.drawFIGS(ZED)
                        PmagSpecRec["measurement_step_min"]='%8.3e ' %(mpars["measurement_step_min"])
                        PmagSpecRec["measurement_step_max"]='%8.3e ' %(mpars["measurement_step_max"])
                        PmagSpecRec["specimen_correction"]='u'
                        PmagSpecRec["specimen_dang"]='%7.1f ' %(mpars['specimen_dang'])
                        print 'DANG: ',PmagSpecRec["specimen_dang"]
                        if calculation_type!='DE-FM':
                            PmagSpecRec["specimen_mad"]='%7.1f ' %(mpars["specimen_mad"])
                            PmagSpecRec["specimen_alpha95"]=""
                        else:
                            PmagSpecRec["specimen_alpha95"]='%7.1f ' %(mpars["specimen_alpha95"])
                            PmagSpecRec["specimen_mad"]=""
                        PmagSpecRec["specimen_n"]='%i ' %(mpars["specimen_n"])
                        PmagSpecRec["specimen_direction_type"]=direction_type
                        PmagSpecRec["calculation_type"]=calculation_type # redundant and won't be imported - just for convenience
                        method_codes=PmagSpecRec["magic_method_codes"].split(':')
                        if len(method_codes) != 0:
                            methstring=""
                            for meth in method_codes:
                                ctype=meth.split('-')
                                if 'DE' not in ctype:methstring=methstring+ ":" +meth # don't include old direction estimation methods
                        methstring=methstring+':'+calculation_type
                        PmagSpecRec["magic_method_codes"]= methstring.strip(':')
                        print 'Method codes: ',PmagSpecRec['magic_method_codes']
                        if calculation_type!='DE-FM':
                            if units=='K': 
                                print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f, %s \n' % (PmagSpecRec["er_specimen_name"],int(PmagSpecRec["specimen_n"]),float(PmagSpecRec["specimen_mad"]),float(PmagSpecRec["specimen_dang"]),float(PmagSpecRec["measurement_step_min"])-273,float(PmagSpecRec["measurement_step_max"])-273,float(PmagSpecRec["specimen_dec"]),float(PmagSpecRec["specimen_inc"]),calculation_type)
                            elif units== 'T':
                                print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f, %s \n' % (PmagSpecRec["er_specimen_name"],int(PmagSpecRec["specimen_n"]),float(PmagSpecRec["specimen_mad"]),float(PmagSpecRec["specimen_dang"]),float(PmagSpecRec["measurement_step_min"])*1e3,float(PmagSpecRec["measurement_step_max"])*1e3,float(PmagSpecRec["specimen_dec"]),float(PmagSpecRec["specimen_inc"]),calculation_type)
                            elif 'T' in units and 'K' in units:
                                if float(PmagSpecRec['measurement_step_min'])<1.0 :
                                    min=float(PmagSpecRec['measurement_step_min'])*1e3
                                else:
                                    min=float(PmagSpecRec['measurement_step_min'])-273
                                if float(PmagSpecRec['measurement_step_max'])<1.0 :
                                    max=float(PmagSpecRec['measurement_step_max'])*1e3
                                else:
                                    max=float(PmagSpecRec['measurement_step_max'])-273
                                print '%s %i %7.1f %i %i %7.1f %7.1f %7.1f, %s \n' % (PmagSpecRec["er_specimen_name"],int(PmagSpecRec["specimen_n"]),float(PmagSpecRec["specimen_mad"]),float(PmagSpecRec["specimen_dang"]),min,max,float(PmagSpecRec["specimen_dec"]),float(PmagSpecRec["specimen_inc"]),calculation_type)
                            else:
                                print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f, %s \n' % (PmagSpecRec["er_specimen_name"],int(PmagSpecRec["specimen_n"]),float(PmagSpecRec["specimen_mad"]),float(PmagSpecRec["specimen_dang"]),float(PmagSpecRec["measurement_step_min"]),float(PmagSpecRec["measurement_step_max"]),float(PmagSpecRec["specimen_dec"]),float(PmagSpecRec["specimen_inc"]),calculation_type)
                        else:
                            if 'K' in units:
                                print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f, %s \n' % (PmagSpecRec["er_specimen_name"],int(PmagSpecRec["specimen_n"]),float(PmagSpecRec["specimen_alpha95"]),float(PmagSpecRec["specimen_dang"]),float(PmagSpecRec["measurement_step_min"])-273,float(PmagSpecRec["measurement_step_max"])-273,float(PmagSpecRec["specimen_dec"]),float(PmagSpecRec["specimen_inc"]),calculation_type)
                            elif 'T' in units:
                                print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f, %s \n' % (PmagSpecRec["er_specimen_name"],int(PmagSpecRec["specimen_n"]),float(PmagSpecRec["specimen_alpha95"]),float(PmagSpecRec["specimen_dang"]),float(PmagSpecRec["measurement_step_min"])*1e3,float(PmagSpecRec["measurement_step_max"])*1e3,float(PmagSpecRec["specimen_dec"]),float(PmagSpecRec["specimen_inc"]),calculation_type)
                            elif 'T' in units and 'K' in units:
                                if float(PmagSpecRec['measurement_step_min'])<1.0 :
                                    min=float(PmagSpecRec['measurement_step_min'])*1e3
                                else:
                                    min=float(PmagSpecRec['measurement_step_min'])-273
                                if float(PmagSpecRec['measurement_step_max'])<1.0 :
                                    max=float(PmagSpecRec['measurement_step_max'])*1e3
                                else:
                                    max=float(PmagSpecRec['measurement_step_max'])-273
                                print '%s %i %7.1f %i %i %7.1f %7.1f, %s \n' % (PmagSpecRec["er_specimen_name"],int(PmagSpecRec["specimen_n"]),float(PmagSpecRec["specimen_alpha95"]),min,max,float(PmagSpecRec["specimen_dec"]),float(PmagSpecRec["specimen_inc"]),calculation_type)
                            else:
                                print '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f, %s \n' % (PmagSpecRec["er_specimen_name"],int(PmagSpecRec["specimen_n"]),float(PmagSpecRec["specimen_alpha95"]),float(PmagSpecRec["measurement_step_min"]),float(PmagSpecRec["measurement_step_max"]),float(PmagSpecRec["specimen_dec"]),float(PmagSpecRec["specimen_inc"]),calculation_type)
                        saveit=raw_input("Save this interpretation? [y]/n \n")
                        if saveit!="n":
                            changeS=1
#
# put in details
#
                            angle,direction_type,setangle="","",0
                            if len(CurrRecs)>0:
                                replace=raw_input(" [0] add new component, or [1] replace existing interpretation(s) [default is replace] ")
                                if replace=="1" or replace=="":
                                    CurrRecs=[]
                                    PmagSpecRec['specimen_comp_name']='A'
                                    CurrRecs.append(PmagSpecRec)
                                else:
                                    print 'These are the current component names for this specimen: '
                                    for trec in CurrRecs:print trec['specimen_comp_name']
                                    compnum=raw_input("Enter new component name: ")
                                    PmagSpecRec['specimen_comp_name']=compnum
                                    print "Adding new component: ",PmagSpecRec['specimen_comp_name']
                                    CurrRecs.append(PmagSpecRec)
                            else:
                                PmagSpecRec['specimen_comp_name']='A'
                                CurrRecs.append(PmagSpecRec)
                            k+=1 
                            ans=""
                        else:
                            ans=""
              else:  # plots=1
                  k+=1
                  files={}
                  locname.replace('/','-')
                  print PmagSpecRec
                  for key in ZED.keys():
                      files[key]="LO:_"+locname+'_SI:_'+PmagSpecRec['er_site_name']+'_SA:_'+PmagSpecRec['er_sample_name']+'_SP:_'+s+'_CO:_'+coord+'_TY:_'+key+'_.'+fmt
                  if pmagplotlib.isServer:
                      black     = '#000000'
                      purple    = '#800080'
                      titles={}
                      titles['demag']='DeMag Plot'
                      titles['zijd']='Zijderveld Plot'
                      titles['eqarea']='Equal Area Plot'
                      ZED = pmagplotlib.addBorders(ZED,titles,black,purple)
                  pmagplotlib.saveP(ZED,files)
            if len(CurrRecs)>0:
                for rec in CurrRecs: PriorRecs.append(rec)
            if changeS==1:
                if len(PriorRecs)>0:
                    save_redo(PriorRecs,inspec)
                else:
                    os.system('rm '+inspec)
            CurrRecs,beg_pca,end_pca=[],"","" # next up
            changeS=0
        else: k+=1 # skip record - not enough data
    if changeM==1:
        pmag.magic_write(meas_file,meas_data,'magic_measurements')
示例#6
0
def main():
    """
    NAME
        zeq_magic_redo.py
   
    DESCRIPTION
        Calculate principal components through demagnetization data using bounds and calculation type stored in "redo" file
  
    SYNTAX
        zeq_magic_redo.py [command line options]

    OPTIONS
        -h prints help message
        -usr USER:   identify user, default is ""
        -f: specify input file, default is magic_measurements.txt
        -F: specify output file, default is zeq_specimens.txt
        -fre  REDO: specify redo file, default is "zeq_redo"
        -fsa  SAMPFILE: specify er_samples format file, default is "er_samples.txt"
        -A : don't average replicate measurements, default is yes
        -crd [s,g,t] : 
             specify coordinate system [s,g,t]  [default is specimen coordinates]
                 are specimen, geographic, and tilt corrected respectively
             NB: you must have a SAMPFILE in this directory to rotate from specimen coordinates
        -leg:  attaches "Recalculated from original measurements; supercedes published results. " to comment field
    INPUTS
        zeq_redo format file is:
        specimen_name calculation_type[DE-BFL,DE-BFL-A,DE-BFL-O,DE-BFP,DE-FM]  step_min step_max component_name[A,B,C]
    """
    dir_path='.'
    INCL=["LT-NO","LT-AF-Z","LT-T-Z","LT-M-Z"] # looking for demag data
    beg,end,pole,geo,tilt,askave,save=0,0,[],0,0,0,0
    user,doave,comment= "",1,""
    geo,tilt=0,0
    version_num=pmag.get_version()
    args=sys.argv
    if '-WD' in args:
        ind=args.index('-WD')
        dir_path=args[ind+1]
    meas_file,pmag_file,mk_file= dir_path+"/"+"magic_measurements.txt",dir_path+"/"+"zeq_specimens.txt",dir_path+"/"+"zeq_redo"
    samp_file,coord=dir_path+"/"+"er_samples.txt",""
    if "-h" in args:
        print(main.__doc__)
        sys.exit()
    if "-usr" in args:
        ind=args.index("-usr")
        user=sys.argv[ind+1]
    if "-A" in args:doave=0
    if "-leg" in args: comment="Recalculated from original measurements; supercedes published results. "
    if "-f" in args:
        ind=args.index("-f")
        meas_file=dir_path+'/'+sys.argv[ind+1]
    if "-F" in args:
        ind=args.index("-F")
        pmag_file=dir_path+'/'+sys.argv[ind+1]
    if "-fre" in args:
        ind=args.index("-fre")
        mk_file=dir_path+"/"+args[ind+1]
    try:
        mk_f=open(mk_file,'r')
    except:
        print("Bad redo file")
        sys.exit()
    mkspec,skipped=[],[]
    speclist=[]
    for line in mk_f.readlines():
        tmp=line.split()
        mkspec.append(tmp)
        speclist.append(tmp[0])
    if "-fsa" in args:
        ind=args.index("-fsa")
        samp_file=dir_path+'/'+sys.argv[ind+1]
    if "-crd" in args:
        ind=args.index("-crd")
        coord=sys.argv[ind+1]
        if coord=="g":geo,tilt=1,0
        if coord=="t":geo,tilt=1,1
#
# now get down to bidness
    if geo==1:
        samp_data,file_type=pmag.magic_read(samp_file)
        if file_type != 'er_samples':
            print(file_type)
            print("This is not a valid er_samples file ") 
            sys.exit()
    #
    #
    #

    meas_data,file_type=pmag.magic_read(meas_file)
    if file_type != 'magic_measurements':
        print(file_type)
        print(file_type,"This is not a valid magic_measurements file ") 
        sys.exit()
    #
    # sort the specimen names
    #
    k = 0
    print('Processing ',len(speclist), ' specimens - please wait')
    PmagSpecs=[]
    while k < len(speclist):
        s=speclist[k]
        recnum=0
        PmagSpecRec={}
        method_codes,inst_codes=[],[]
    # find the data from the meas_data file for this sample
    #
    #  collect info for the PmagSpecRec dictionary
    #
        meas_meth=[]
        spec=pmag.get_dictitem(meas_data,'er_specimen_name',s,'T')   
        if len(spec)==0:
            print('no data found for specimen:  ',s)
            print('delete from zeq_redo input file...., then try again')
        else: 
          for rec in  spec: # copy of vital stats to PmagSpecRec from first spec record in demag block
           skip=1
           methods=rec["magic_method_codes"].split(":")
           if len(set(methods) & set(INCL))>0:
                   PmagSpecRec["er_analyst_mail_names"]=user
                   PmagSpecRec["magic_software_packages"]=version_num
                   PmagSpecRec["er_specimen_name"]=s
                   PmagSpecRec["er_sample_name"]=rec["er_sample_name"]
                   PmagSpecRec["er_site_name"]=rec["er_site_name"]
                   PmagSpecRec["er_location_name"]=rec["er_location_name"]
                   if "er_expedition_name" in list(rec.keys()):PmagSpecRec["er_expedition_name"]=rec["er_expedition_name"]
                   PmagSpecRec["er_citation_names"]="This study"
                   if "magic_experiment_name" not in list(rec.keys()): rec["magic_experiment_name"]=""
                   PmagSpecRec["magic_experiment_names"]=rec["magic_experiment_name"]
                   if "magic_instrument_codes" not in list(rec.keys()): rec["magic_instrument_codes"]=""
                   inst=rec['magic_instrument_codes'].split(":")
                   for I in inst:
                       if I not in inst_codes:  # copy over instruments
                           inst_codes.append(I)
                   meths=rec["magic_method_codes"].split(":")
                   for meth in meths:
                       if meth.strip() not in meas_meth:meas_meth.append(meth)
                   if "LP-DIR-AF" in meas_meth or "LT-AF-Z" in meas_meth: 
                       PmagSpecRec["measurement_step_unit"]="T"
                       if "LP-DIR-AF" not in method_codes:method_codes.append("LP-DIR-AF") 
                   if "LP-DIR-T" in meas_meth or "LT-T-Z" in meas_meth: 
                       PmagSpecRec["measurement_step_unit"]="K"
                       if "LP-DIR-T" not in method_codes:method_codes.append("LP-DIR-T") 
                   if "LP-DIR-M" in meas_meth or "LT-M-Z" in meas_meth: 
                       PmagSpecRec["measurement_step_unit"]="J"
                       if "LP-DIR-M" not in method_codes:method_codes.append("LP-DIR-M") 
    #
    #
        datablock,units=pmag.find_dmag_rec(s,spec) # fish out the demag data for this specimen
    #
        if len(datablock) <2 or s not in speclist : 
            k+=1
#            print 'skipping ', s,len(datablock)
        else:
        #
        # find replicate measurements at given treatment step and average them
        #
#            step_meth,avedata=pmag.vspec(data)
#
#            if len(avedata) != len(datablock):
#                if doave==1: 
#                    method_codes.append("DE-VM")
#                    datablock=avedata
        #
        # do geo or stratigraphic correction now
        #
            if geo==1 or tilt==1:
       # find top priority orientation method
                orient,az_type=pmag.get_orient(samp_data,PmagSpecRec["er_sample_name"])
                if az_type not in method_codes:method_codes.append(az_type)
        #
        #  if tilt selected,  get stratigraphic correction
        #
                tiltblock,geoblock=[],[]
                for rec in datablock:
                    if "sample_azimuth" in list(orient.keys()) and orient["sample_azimuth"]!="":
                        d_geo,i_geo=pmag.dogeo(rec[1],rec[2],float(orient["sample_azimuth"]),float(orient["sample_dip"]))
                        geoblock.append([rec[0],d_geo,i_geo,rec[3],rec[4],rec[5]])
                        if tilt==1 and "sample_bed_dip_direction" in list(orient.keys()): 
                            d_tilt,i_tilt=pmag.dotilt(d_geo,i_geo,float(orient["sample_bed_dip_direction"]),float(orient["sample_bed_dip"]))
                            tiltblock.append([rec[0],d_tilt,i_tilt,rec[3],rec[4],rec[5]])
                        elif tilt==1:
                            if PmagSpecRec["er_sample_name"] not in skipped:
                                print('no tilt correction for ', PmagSpecRec["er_sample_name"],' skipping....')
                                skipped.append(PmagSpecRec["er_sample_name"])
                    else:
                        if PmagSpecRec["er_sample_name"] not in skipped:
                            print('no geographic correction for ', PmagSpecRec["er_sample_name"],' skipping....')
                            skipped.append(PmagSpecRec["er_sample_name"])
    #
    #	get beg_pca, end_pca, pca
            if PmagSpecRec['er_sample_name'] not in skipped:
                compnum=-1
                for spec in mkspec:
                    if spec[0]==s:
                        CompRec={}
                        for key in list(PmagSpecRec.keys()):CompRec[key]=PmagSpecRec[key]
                        compnum+=1
                        calculation_type=spec[1]
                        beg=float(spec[2])
                        end=float(spec[3])
                        if len(spec)>4:
                            comp_name=spec[4]
                        else:
                            comp_name=string.uppercase[compnum]
                        CompRec['specimen_comp_name']=comp_name
                        if beg < float(datablock[0][0]):beg=float(datablock[0][0])
                        if end > float(datablock[-1][0]):end=float(datablock[-1][0])
                        for l  in range(len(datablock)):
                            if datablock[l][0]==beg:beg_pca=l
                            if datablock[l][0]==end:end_pca=l
                        if geo==1 and tilt==0:
                            mpars=pmag.domean(geoblock,beg_pca,end_pca,calculation_type)
                            if mpars["specimen_direction_type"]!="Error":
                                CompRec["specimen_dec"]='%7.1f ' %(mpars["specimen_dec"])
                                CompRec["specimen_inc"]='%7.1f ' %(mpars["specimen_inc"])
                                CompRec["specimen_tilt_correction"]='0'
                        if geo==1 and tilt==1:
                            mpars=pmag.domean(tiltblock,beg_pca,end_pca,calculation_type)
                            if mpars["specimen_direction_type"]!="Error":
                                CompRec["specimen_dec"]='%7.1f ' %(mpars["specimen_dec"])
                                CompRec["specimen_inc"]='%7.1f ' %(mpars["specimen_inc"])
                                CompRec["specimen_tilt_correction"]='100'
                        if geo==0 and tilt==0: 
                            mpars=pmag.domean(datablock,beg_pca,end_pca,calculation_type)
                            if mpars["specimen_direction_type"]!="Error":
                                CompRec["specimen_dec"]='%7.1f ' %(mpars["specimen_dec"])
                                CompRec["specimen_inc"]='%7.1f ' %(mpars["specimen_inc"])
                                CompRec["specimen_tilt_correction"]='-1'
                        if mpars["specimen_direction_type"]=="Error": 
                            pass
                        else: 
                            CompRec["measurement_step_min"]='%8.3e '%(datablock[beg_pca][0])
                            try:
                                CompRec["measurement_step_max"]='%8.3e '%(datablock[end_pca][0] )
                            except:
                                print('error in end_pca ',PmagSpecRec['er_specimen_name'])
                            CompRec["specimen_correction"]='u'
                            if calculation_type!='DE-FM':
                                CompRec["specimen_mad"]='%7.1f '%(mpars["specimen_mad"])
                                CompRec["specimen_alpha95"]=""
                            else:
                                CompRec["specimen_mad"]=""
                                CompRec["specimen_alpha95"]='%7.1f '%(mpars["specimen_alpha95"])
                            CompRec["specimen_n"]='%i '%(mpars["specimen_n"])
                            CompRec["specimen_dang"]='%7.1f '%(mpars["specimen_dang"])
                            CompMeths=[]
                            for meth in method_codes:
                                if meth not in CompMeths:CompMeths.append(meth)
                            if calculation_type not in CompMeths:CompMeths.append(calculation_type)
                            if geo==1: CompMeths.append("DA-DIR-GEO")
                            if tilt==1: CompMeths.append("DA-DIR-TILT")
                            if "DE-BFP" not in calculation_type:
                                CompRec["specimen_direction_type"]='l'
                            else:
                                CompRec["specimen_direction_type"]='p'
                            CompRec["magic_method_codes"]=""
                            if len(CompMeths) != 0:
                                methstring=""
                                for meth in CompMeths:
                                    methstring=methstring+ ":" +meth
                                CompRec["magic_method_codes"]=methstring.strip(':')
                            CompRec["specimen_description"]=comment
                            if len(inst_codes) != 0:
                                inststring=""
                                for inst in inst_codes:
                                    inststring=inststring+ ":" +inst
                                CompRec["magic_instrument_codes"]=inststring.strip(':')
                            PmagSpecs.append(CompRec)
            k+=1
    pmag.magic_write(pmag_file,PmagSpecs,'pmag_specimens')
    print("Recalculated specimen data stored in ",pmag_file)
示例#7
0
def main():
    """
    NAME
        zeq_magic.py

    DESCRIPTION
        reads in magic_measurements formatted file, makes plots of remanence decay
        during demagnetization experiments.  Reads in prior interpretations saved in
        a pmag_specimens formatted file [and  allows re-interpretations of best-fit lines
        and planes and saves (revised or new) interpretations in a pmag_specimens file.
        interpretations are saved in the coordinate system used. Also allows judicious editting of
        measurements to eliminate "bad" measurements.  These are marked as such in the magic_measurements
        input file.  they are NOT deleted, just ignored. ] Bracketed part not yet implemented

    SYNTAX
        zeq_magic.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f  MEASFILE: sets measurements format input file, default: measurements.txt
        -fsp SPECFILE: sets specimens format file with prior interpreations, default: specimens.txt
        -fsa SAMPFILE: sets samples format file sample=>site information, default: samples.txt
        -fsi SITEFILE: sets sites format file with site=>location informationprior interpreations, default: samples.txt
        -Fp PLTFILE: sets filename for saved plot, default is name_type.fmt (where type is zijd, eqarea or decay curve)
        -crd [s,g,t]: sets coordinate system,  g=geographic, t=tilt adjusted, default: specimen coordinate system
        -spc SPEC  plots single specimen SPEC, saves plot with specified format
              with optional -dir settings and quits
        -dir [L,P,F][beg][end]: sets calculation type for principal component analysis, default is none
             beg: starting step for PCA calculation
             end: ending step for PCA calculation
             [L,P,F]: calculation type for line, plane or fisher mean
             must be used with -spc option
        -fmt FMT: set format of saved plot [png,svg,jpg]
        -A:  suppresses averaging of  replicate measurements, default is to average
        -sav: saves all plots without review
    SCREEN OUTPUT:
        Specimen, N, a95, StepMin, StepMax, Dec, Inc, calculation type

    """
    # initialize some variables
    doave, e, b = 1, 0, 0  # average replicates, initial end and beginning step
    intlist = ['magn_moment', 'magn_volume', 'magn_mass', 'magnitude']
    plots, coord = 0, 's'
    noorient = 0
    version_num = pmag.get_version()
    verbose = pmagplotlib.verbose
    calculation_type, fmt = "", "svg"
    spec_keys = []
    geo, tilt, ask = 0, 0, 0
    PriorRecs = []  # empty list for prior interpretations
    backup = 0
    specimen = ""  # can skip everything and just plot one specimen with bounds e,b
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    dir_path = pmag.get_named_arg("-WD", default_val=os.getcwd())
    meas_file = pmag.get_named_arg("-f", default_val="measurements.txt")
    spec_file = pmag.get_named_arg("-fsp", default_val="specimens.txt")
    samp_file = pmag.get_named_arg("-fsa", default_val="samples.txt")
    site_file = pmag.get_named_arg("-fsi", default_val="sites.txt")
    #meas_file = os.path.join(dir_path, meas_file)
    #spec_file = os.path.join(dir_path, spec_file)
    #samp_file = os.path.join(dir_path, samp_file)
    #site_file = os.path.join(dir_path, site_file)
    plot_file = pmag.get_named_arg("-Fp", default_val="")
    crd = pmag.get_named_arg("-crd", default_val="s")
    if crd == "s":
        coord = "-1"
    elif crd == "t":
        coord = "100"
    else:
        coord = "0"
    saved_coord = coord
    fmt = pmag.get_named_arg("-fmt", "svg")
    specimen = pmag.get_named_arg("-spc", default_val="")
    #if specimen: # just save plot and exit
    #    plots, verbose = 1, 0
    beg_pca, end_pca = "", ""
    if '-dir' in sys.argv:
        ind = sys.argv.index('-dir')
        direction_type = sys.argv[ind + 1]
        beg_pca = int(sys.argv[ind + 2])
        end_pca = int(sys.argv[ind + 3])
        if direction_type == 'L':
            calculation_type = 'DE-BFL'
        if direction_type == 'P':
            calculation_type = 'DE-BFP'
        if direction_type == 'F':
            calculation_type = 'DE-FM'
    if '-A' in sys.argv:
        doave = 0
    if '-sav' in sys.argv:
        plots, verbose = 1, 0
    #
    first_save = 1
    fnames = {
        'measurements': meas_file,
        'specimens': spec_file,
        'samples': samp_file,
        'sites': site_file
    }
    contribution = cb.Contribution(
        dir_path,
        custom_filenames=fnames,
        read_tables=['measurements', 'specimens', 'samples', 'sites'])
    #
    #   import  specimens

    if 'measurements' not in contribution.tables:
        print('-W- No measurements table found in your working directory')
        return

    specimen_cols = [
        'analysts', 'aniso_ftest', 'aniso_ftest12', 'aniso_ftest23', 'aniso_s',
        'aniso_s_mean', 'aniso_s_n_measurements', 'aniso_s_sigma',
        'aniso_s_unit', 'aniso_tilt_correction', 'aniso_type', 'aniso_v1',
        'aniso_v2', 'aniso_v3', 'citations', 'description', 'dir_alpha95',
        'dir_comp', 'dir_dec', 'dir_inc', 'dir_mad_free', 'dir_n_measurements',
        'dir_tilt_correction', 'experiments', 'geologic_classes',
        'geologic_types', 'hyst_bc', 'hyst_bcr', 'hyst_mr_moment',
        'hyst_ms_moment', 'int_abs', 'int_b', 'int_b_beta', 'int_b_sigma',
        'int_corr', 'int_dang', 'int_drats', 'int_f', 'int_fvds', 'int_gamma',
        'int_mad_free', 'int_md', 'int_n_measurements', 'int_n_ptrm', 'int_q',
        'int_rsc', 'int_treat_dc_field', 'lithologies', 'meas_step_max',
        'meas_step_min', 'meas_step_unit', 'method_codes', 'sample',
        'software_packages', 'specimen'
    ]
    if 'specimens' in contribution.tables:
        contribution.propagate_name_down('sample', 'measurements')
        # add location/site info to measurements table for naming plots
        if pmagplotlib.isServer:
            contribution.propagate_name_down('site', 'measurements')
            contribution.propagate_name_down('location', 'measurements')
        spec_container = contribution.tables['specimens']
        if 'method_codes' not in spec_container.df.columns:
            spec_container.df['method_codes'] = None
        prior_spec_data = spec_container.get_records_for_code(
            'LP-DIR', strict_match=False
        )  # look up all prior directional interpretations
#
#  tie sample names to measurement data
#
    else:
        spec_container, prior_spec_data = None, []

#
#   import samples  for orientation info
#
    if 'samples' in contribution.tables:
        samp_container = contribution.tables['samples']
        samps = samp_container.df
        samp_data = samps.to_dict(
            'records'
        )  # convert to list of dictionaries for use with get_orient
    else:
        samp_data = []
    #if ('samples' in contribution.tables) and ('specimens' in contribution.tables):
    #    #        contribution.propagate_name_down('site','measurements')
    #    contribution.propagate_cols(col_names=[
    #                                'azimuth', 'dip', 'orientation_quality','bed_dip','bed_dip_direction'], target_df_name='measurements', source_df_name='samples')
##
# define figure numbers for equal area, zijderveld,
#  and intensity vs. demagnetiztion step respectively
#
    ZED = {}
    ZED['eqarea'], ZED['zijd'], ZED['demag'] = 1, 2, 3
    pmagplotlib.plot_init(ZED['eqarea'], 6, 6)
    pmagplotlib.plot_init(ZED['zijd'], 6, 6)
    pmagplotlib.plot_init(ZED['demag'], 6, 6)
    #    save_pca=0
    angle, direction_type, setangle = "", "", 0
    #   create measurement dataframe
    #
    meas_container = contribution.tables['measurements']
    meas_data = meas_container.df
    #
    meas_data = meas_data[meas_data['method_codes'].str.contains(
        'LT-NO|LT-AF-Z|LT-T-Z|LT-M-Z') == True]  # fish out steps for plotting
    meas_data = meas_data[meas_data['method_codes'].str.contains(
        'AN|ARM|LP-TRM|LP-PI-ARM') == False]  # strip out unwanted experiments
    intensity_types = [
        col_name for col_name in meas_data.columns if col_name in intlist
    ]
    intensity_types = [
        col_name for col_name in intensity_types if any(meas_data[col_name])
    ]
    if not len(intensity_types):
        print('-W- No intensity columns found')
        return
    # plot first non-empty intensity method found - normalized to initial value anyway -
    # doesn't matter which used
    int_key = intensity_types[0]
    # get all the non-null intensity records of the same type
    meas_data = meas_data[meas_data[int_key].notnull()]
    if 'quality' not in meas_data.columns:
        meas_data['quality'] = 'g'  # set the default flag to good
# need to treat LP-NO specially  for af data, treatment should be zero,
# otherwise 273.
#meas_data['treatment'] = meas_data['treat_ac_field'].where(
#    cond=meas_data['treat_ac_field'] != 0, other=meas_data['treat_temp'])
    meas_data['treatment'] = meas_data['treat_ac_field'].where(
        cond=meas_data['treat_ac_field'].astype(bool),
        other=meas_data['treat_temp'])
    meas_data['ZI'] = 1  # initialize these to one
    meas_data['instrument_codes'] = ""  # initialize these to blank
    #   for unusual case of microwave power....
    if 'treat_mw_power' in meas_data.columns:
        meas_data.loc[
            (meas_data.treat_mw_power != 0) & (meas_data.treat_mw_power) &
            (meas_data.treat_mw_time),
            'treatment'] = meas_data.treat_mw_power * meas_data.treat_mw_time
#
# get list of unique specimen names from measurement data
#
# this is a list of all the specimen names
    specimen_names = meas_data.specimen.unique()
    specimen_names = specimen_names.tolist()
    specimen_names.sort()
    #
    # set up new DataFrame for this sessions specimen interpretations
    #
    data_container = cb.MagicDataFrame(dtype='specimens',
                                       columns=specimen_cols)
    # this is for interpretations from this session
    current_spec_data = data_container.df
    if specimen == "":
        k = 0
    else:
        k = specimen_names.index(specimen)
    # let's look at the data now
    while k < len(specimen_names):
        mpars = {"specimen_direction_type": "Error"}
        # set the current specimen for plotting
        this_specimen = specimen_names[k]
        # reset beginning/end pca if plotting more than one specimen
        if not specimen:
            beg_pca, end_pca = "", ""
        if verbose and this_specimen != "":
            print(this_specimen, k + 1, 'out of ', len(specimen_names))
        if setangle == 0:
            angle = ""
        this_specimen_measurements = meas_data[
            meas_data['specimen'].str.contains(this_specimen).astype(
                bool)]  # fish out this specimen
        this_specimen_measurements = this_specimen_measurements[
            -this_specimen_measurements['quality'].str.contains('b').astype(
                bool)]  # remove bad measurements
        if len(this_specimen_measurements) != 0:  # if there are measurements
            meas_list = this_specimen_measurements.to_dict(
                'records')  # get a list of dictionaries
            this_sample = ""
            if coord != '-1' and 'sample' in meas_list[0].keys(
            ):  # look up sample name
                this_sample = pmag.get_dictitem(meas_list, 'specimen',
                                                this_specimen, 'T')
                if len(this_sample) > 0:
                    this_sample = this_sample[0]['sample']
            #
            #    set up datablock [[treatment,dec, inc, int, direction_type],[....]]
            #
            #
            # figure out the method codes
            #
            units, methods, title = "", "", this_specimen

            if pmagplotlib.isServer:
                try:
                    loc = this_specimen_measurements.loc[:,
                                                         'location'].values[0]
                except:
                    loc = ""
                try:
                    site = this_specimen_measurements.loc[:, 'site'].values[0]
                except:
                    site = ""
                try:
                    samp = this_specimen_measurements.loc[:,
                                                          'sample'].values[0]
                except:
                    samp = ""
                title = "LO:_{}_SI:_{}_SA:_{}_SP:_{}_".format(
                    loc, site, samp, this_specimen)
            # this is a list of all the specimen method codes
            meas_meths = this_specimen_measurements.method_codes.unique()
            tr = pd.to_numeric(this_specimen_measurements.treatment).tolist()
            if any(cb.is_null(treat, False) for treat in tr):
                print(
                    '-W- Missing required values in measurements.treatment for {}, skipping'
                    .format(this_specimen))
                if specimen:
                    return
                k += 1
                continue
            if set(tr) == set([0]):
                print(
                    '-W- Missing required values in measurements.treatment for {}, skipping'
                    .format(this_specimen))
                if specimen:
                    return
                k += 1
                continue
            for m in meas_meths:
                if 'LT-AF-Z' in m and 'T' not in units:
                    units = 'T'  # units include tesla
                    tr[0] = 0
                if 'LT-T-Z' in m and 'K' not in units:
                    units = units + ":K"  # units include kelvin
                if 'LT-M-Z' in m and 'J' not in units:
                    units = units + ':J'  # units include joules
                    tr[0] = 0
                units = units.strip(':')  # strip off extra colons
                if 'LP-' in m:
                    methods = methods + ":" + m
            decs = pd.to_numeric(this_specimen_measurements.dir_dec).tolist()
            incs = pd.to_numeric(this_specimen_measurements.dir_inc).tolist()

            #
            #    fix the coordinate system
            #
            # revert to original coordinate system
            coord = saved_coord
            if coord != '-1':  # need to transform coordinates to geographic
                # get the azimuth
                or_info, az_type = pmag.get_orient(samp_data,
                                                   this_sample,
                                                   data_model=3)
                if 'azimuth' in or_info.keys() and cb.not_null(
                        or_info['azimuth']):
                    #azimuths = pd.to_numeric(
                    #    this_specimen_measurements.azimuth).tolist()
                    #dips = pd.to_numeric(this_specimen_measurements.dip).tolist()
                    azimuths = len(decs) * [or_info['azimuth']]
                    dips = len(decs) * [or_info['dip']]
                # if azimuth/dip is missing, plot using specimen coordinates instead
                else:
                    azimuths, dips = [], []
                if any([cb.is_null(az) for az in azimuths if az != 0]):
                    coord = '-1'
                    print("-W- Couldn't find azimuth and dip for {}".format(
                        this_specimen))
                    print("    Plotting with specimen coordinates instead")
                elif any([cb.is_null(dip) for dip in dips if dip != 0]):
                    coord = '-1'
                    print("-W- Couldn't find azimuth and dip for {}".format(
                        this_specimen))
                    print("    Plotting with specimen coordinates instead")
                else:
                    coord = saved_coord
                # if azimuth and dip were found, continue with geographic coordinates
                if coord != "-1" and len(azimuths) > 0:
                    dirs = [decs, incs, azimuths, dips]
                    # this transposes the columns and rows of the list of lists
                    dirs_geo = np.array(list(map(list, list(zip(*dirs)))))
                    decs, incs = pmag.dogeo_V(dirs_geo)
                    if coord == '100' and 'bed_dip_direction' in or_info.keys(
                    ) and or_info[
                            'bed_dip_direction'] != "":  # need to do tilt correction too
                        bed_dip_dirs = len(decs) * [
                            or_info['bed_dip_direction']
                        ]
                        bed_dips = len(decs) * [or_info['bed_dip']]
                        #bed_dip_dirs = pd.to_numeric(
                        #    this_specimen_measurements.bed_dip_direction).tolist()  # get the azimuths
                        #bed_dips = pd.to_numeric(
                        #    this_specimen_measurements.bed_dip).tolist()  # get the azimuths

                        dirs = [decs, incs, bed_dip_dirs, bed_dips]
                        ## this transposes the columns and rows of the list of lists
                        dirs_tilt = np.array(list(map(list, list(zip(*dirs)))))
                        decs, incs = pmag.dotilt_V(dirs_tilt)
                        if pmagplotlib.isServer:
                            title = title + "CO:_t_"
                        else:
                            title = title + '_t'
                    else:
                        if pmagplotlib.isServer:
                            title = title + "CO:_g_"
                        else:
                            title = title + '_g'
            if angle == "":
                angle = decs[0]
            ints = pd.to_numeric(this_specimen_measurements[int_key]).tolist()
            ZI = this_specimen_measurements.ZI.tolist()
            flags = this_specimen_measurements.quality.tolist()
            codes = this_specimen_measurements.instrument_codes.tolist()
            datalist = [tr, decs, incs, ints, ZI, flags, codes]
            # this transposes the columns and rows of the list of lists
            datablock = list(map(list, list(zip(*datalist))))
            pmagplotlib.plot_zed(ZED, datablock, angle, title, units)
            if verbose and not set_env.IS_WIN:
                pmagplotlib.draw_figs(ZED)
#
#     collect info for current_specimen_interpretation dictionary
#

#
#     find prior interpretation
#
            prior_specimen_interpretations = []
            if len(prior_spec_data):
                prior_specimen_interpretations = prior_spec_data[
                    prior_spec_data['specimen'].str.contains(
                        this_specimen) == True]
            if (beg_pca == "") and (len(prior_specimen_interpretations) != 0):
                if len(prior_specimen_interpretations) > 0:
                    beg_pcas = pd.to_numeric(prior_specimen_interpretations.
                                             meas_step_min.values).tolist()
                    end_pcas = pd.to_numeric(prior_specimen_interpretations.
                                             meas_step_max.values).tolist()
                    spec_methods = prior_specimen_interpretations.method_codes.tolist(
                    )
                    # step through all prior interpretations and plot them
                    for ind in range(len(beg_pcas)):
                        spec_meths = spec_methods[ind].split(':')
                        for m in spec_meths:
                            if 'DE-BFL' in m:
                                calculation_type = 'DE-BFL'  # best fit line
                            if 'DE-BFP' in m:
                                calculation_type = 'DE-BFP'  # best fit plane
                            if 'DE-FM' in m:
                                calculation_type = 'DE-FM'  # fisher mean
                            if 'DE-BFL-A' in m:
                                calculation_type = 'DE-BFL-A'  # anchored best fit line
                        if len(beg_pcas) != 0:
                            try:
                                # getting the starting and ending points
                                start, end = tr.index(beg_pcas[ind]), tr.index(
                                    end_pcas[ind])
                                mpars = pmag.domean(datablock, start, end,
                                                    calculation_type)
                            except ValueError:
                                print(
                                    '-W- Specimen record contains invalid start/stop bounds:'
                                )
                                mpars['specimen_direction_type'] = "Error"
                        # calculate direction/plane
                            if mpars["specimen_direction_type"] != "Error":
                                # put it on the plot
                                pmagplotlib.plot_dir(ZED, mpars, datablock,
                                                     angle)
                                if verbose and not set_env.IS_WIN:
                                    pmagplotlib.draw_figs(ZED)
### SKIP if no prior interpretation - this section should not be used:
#            else:
#                try:
#                    start, end = int(beg_pca), int(end_pca)
#                except ValueError:
#                    beg_pca = 0
#                    end_pca = len(datablock) - 1
#                    start, end = int(beg_pca), int(end_pca)
#            #    # calculate direction/plane
#                try:
#                    mpars = pmag.domean(datablock, start, end, calculation_type)
#                except Exception as ex:
#                    print('-I- Problem with {}'.format(this_specimen))
#                    print('   ', ex)
#                    print('    Skipping')
#                    continue
#                    k += 1
#                if mpars["specimen_direction_type"] != "Error":
#                    # put it on the plot
#                    pmagplotlib.plot_dir(ZED, mpars, datablock, angle)
#                    if verbose:
#                        pmagplotlib.draw_figs(ZED)

            if plots == 1 or specimen != "":
                if plot_file == "":
                    basename = title
                else:
                    basename = plot_file
                files = {}
                for key in list(ZED.keys()):
                    files[key] = basename + '_' + key + '.' + fmt
                    if pmagplotlib.isServer:
                        files[key] = basename + "TY:_{}_.".format(key) + fmt
                pmagplotlib.save_plots(ZED, files)
                if specimen != "":
                    sys.exit()
            if verbose:
                recnum = 0
                for plotrec in datablock:
                    if units == 'T':
                        print('%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' %
                              (plotrec[5], recnum, plotrec[0] * 1e3, " mT",
                               plotrec[3], plotrec[1], plotrec[2], plotrec[6]))
                    if units == "K":
                        print('%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' %
                              (plotrec[5], recnum, plotrec[0] - 273, ' C',
                               plotrec[3], plotrec[1], plotrec[2], plotrec[6]))
                    if units == "J":
                        print('%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' %
                              (plotrec[5], recnum, plotrec[0], ' J',
                               plotrec[3], plotrec[1], plotrec[2], plotrec[6]))
                    if 'K' in units and 'T' in units:
                        if plotrec[0] >= 1.:
                            print('%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' %
                                  (plotrec[5], recnum, plotrec[0] - 273, ' C',
                                   plotrec[3], plotrec[1], plotrec[2],
                                   plotrec[6]))
                        if plotrec[0] < 1.:
                            print('%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' %
                                  (plotrec[5], recnum, plotrec[0] * 1e3, " mT",
                                   plotrec[3], plotrec[1], plotrec[2],
                                   plotrec[6]))
                    recnum += 1
            # we have a current interpretation
            elif mpars["specimen_direction_type"] != "Error":
                #
                # create a new specimen record for the interpreation for this
                # specimen
                this_specimen_interpretation = {
                    col: ""
                    for col in specimen_cols
                }
                #               this_specimen_interpretation["analysts"]=user
                this_specimen_interpretation['software_packages'] = version_num
                this_specimen_interpretation['specimen'] = this_specimen
                this_specimen_interpretation["method_codes"] = calculation_type
                this_specimen_interpretation["meas_step_unit"] = units
                this_specimen_interpretation["meas_step_min"] = tr[start]
                this_specimen_interpretation["meas_step_max"] = tr[end]
                this_specimen_interpretation["dir_dec"] = '%7.1f' % (
                    mpars['specimen_dec'])
                this_specimen_interpretation["dir_inc"] = '%7.1f' % (
                    mpars['specimen_inc'])
                this_specimen_interpretation["dir_dang"] = '%7.1f' % (
                    mpars['specimen_dang'])
                this_specimen_interpretation["dir_n_measurements"] = '%i' % (
                    mpars['specimen_n'])
                this_specimen_interpretation["dir_tilt_correction"] = coord
                methods = methods.replace(" ", "")
                if "T" in units:
                    methods = methods + ":LP-DIR-AF"
                if "K" in units:
                    methods = methods + ":LP-DIR-T"
                if "J" in units:
                    methods = methods + ":LP-DIR-M"
                this_specimen_interpretation["method_codes"] = methods.strip(
                    ':')
                this_specimen_interpretation[
                    "experiments"] = this_specimen_measurements.experiment.unique(
                    )[0]
                #
                #   print some stuff
                #
                if calculation_type != 'DE-FM':
                    this_specimen_interpretation["dir_mad_free"] = '%7.1f' % (
                        mpars['specimen_mad'])
                    this_specimen_interpretation["dir_alpha95"] = ''
                    if verbose:
                        if units == 'K':
                            print(
                                '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %s \n'
                                %
                                (this_specimen_interpretation["specimen"],
                                 int(this_specimen_interpretation[
                                     "dir_n_measurements"]),
                                 float(this_specimen_interpretation[
                                     "dir_mad_free"]),
                                 float(
                                     this_specimen_interpretation["dir_dang"]),
                                 float(this_specimen_interpretation[
                                     "meas_step_min"]) - 273,
                                 float(this_specimen_interpretation[
                                     "meas_step_max"]) - 273,
                                 float(
                                     this_specimen_interpretation["dir_dec"]),
                                 float(
                                     this_specimen_interpretation["dir_inc"]),
                                 calculation_type))
                        elif units == 'T':
                            print(
                                '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %s \n'
                                %
                                (this_specimen_interpretation["specimen"],
                                 int(this_specimen_interpretation[
                                     "dir_n_measurements"]),
                                 float(this_specimen_interpretation[
                                     "dir_mad_free"]),
                                 float(
                                     this_specimen_interpretation["dir_dang"]),
                                 float(this_specimen_interpretation[
                                     "meas_step_min"]) * 1e3,
                                 float(this_specimen_interpretation[
                                     "meas_step_max"]) * 1e3,
                                 float(
                                     this_specimen_interpretation["dir_dec"]),
                                 float(
                                     this_specimen_interpretation["dir_inc"]),
                                 calculation_type))
                        elif 'T' in units and 'K' in units:
                            if float(this_specimen_interpretation[
                                    'meas_step_min']) < 1.0:
                                min = float(this_specimen_interpretation[
                                    'meas_step_min']) * 1e3
                            else:
                                min = float(this_specimen_interpretation[
                                    'meas_step_min']) - 273
                            if float(this_specimen_interpretation[
                                    'meas_step_max']) < 1.0:
                                max = float(this_specimen_interpretation[
                                    'meas_step_max']) * 1e3
                            else:
                                max = float(this_specimen_interpretation[
                                    'meas_step_max']) - 273
                            print(
                                '%s %i %7.1f %i %i %7.1f %7.1f %7.1f %s \n' %
                                (this_specimen_interpretation["specimen"],
                                 int(this_specimen_interpretation[
                                     "dir_n_measurements"]),
                                 float(this_specimen_interpretation[
                                     "dir_mad_free"]),
                                 float(
                                     this_specimen_interpretation["dir_dang"]),
                                 min, max,
                                 float(
                                     this_specimen_interpretation["dir_dec"]),
                                 float(
                                     this_specimen_interpretation["dir_inc"]),
                                 calculation_type))
                        else:
                            print(
                                '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %s \n'
                                %
                                (this_specimen_interpretation["specimen"],
                                 int(this_specimen_interpretation[
                                     "dir_n_measurements"]),
                                 float(this_specimen_interpretation[
                                     "dir_mad_free"]),
                                 float(
                                     this_specimen_interpretation["dir_dang"]),
                                 float(this_specimen_interpretation[
                                     "meas_step_min"]),
                                 float(this_specimen_interpretation[
                                     "meas_step_max"]),
                                 float(
                                     this_specimen_interpretation["dir_dec"]),
                                 float(
                                     this_specimen_interpretation["dir_inc"]),
                                 calculation_type))
                else:
                    this_specimen_interpretation["dir_alpha95"] = '%7.1f' % (
                        mpars['specimen_alpha95'])
                    this_specimen_interpretation["dir_mad_free"] = ''
                    if verbose:
                        if 'K' in units:
                            print(
                                '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %s \n'
                                %
                                (this_specimen_interpretation["specimen"],
                                 int(this_specimen_interpretation[
                                     "dir_n_measurments"]),
                                 float(this_specimen_interpretation[
                                     "dir_mad_free"]),
                                 float(
                                     this_specimen_interpretation["dir_dang"]),
                                 float(this_specimen_interpretation[
                                     "meas_step_min"]) - 273,
                                 float(this_specimen_interpretation[
                                     "meas_step_max"]) - 273,
                                 float(
                                     this_specimen_interpretation["dir_dec"]),
                                 float(
                                     this_specimen_interpretation["dir_inc"]),
                                 calculation_type))
                        elif 'T' in units:
                            print(
                                '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %s \n'
                                %
                                (this_specimen_interpretation["specimen"],
                                 int(this_specimen_interpretation[
                                     "dir_n_measurements"]),
                                 float(this_specimen_interpretation[
                                     "dir_alpha95"]),
                                 float(
                                     this_specimen_interpretation["dir_dang"]),
                                 float(this_specimen_interpretation[
                                     "meas_step_min"]) * 1e3,
                                 float(this_specimen_interpretation[
                                     "meas_step_max"]) * 1e3,
                                 float(
                                     this_specimen_interpretation["dir_dec"]),
                                 float(
                                     this_specimen_interpretation["dir_inc"]),
                                 calculation_type))
                        elif 'T' in units and 'K' in units:
                            if float(this_specimen_interpretation[
                                    'meas_step_min']) < 1.0:
                                min = float(this_specimen_interpretation[
                                    'meas_step_min']) * 1e3
                            else:
                                min = float(this_specimen_interpretation[
                                    'meas_step_min']) - 273
                            if float(this_specimen_interpretation[
                                    'meas_step_max']) < 1.0:
                                max = float(this_specimen_interpretation[
                                    'meas_step_max']) * 1e3
                            else:
                                max = float(this_specimen_interpretation[
                                    'meas_step_max']) - 273
                            print('%s %i %7.1f %i %i %7.1f %7.1f %s \n' % (
                                this_specimen_interpretation["specimen"],
                                int(this_specimen_interpretation[
                                    "dir_n_measurements"]),
                                float(
                                    this_specimen_interpretation["dir_alpha95"]
                                ), min, max,
                                float(this_specimen_interpretation["dir_dec"]),
                                float(this_specimen_interpretation["dir_inc"]),
                                calculation_type))
                        else:
                            print(
                                '%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %s \n' %
                                (this_specimen_interpretation["specimen"],
                                 int(this_specimen_interpretation[
                                     "dir_n_measurements"]),
                                 float(this_specimen_interpretation[
                                     "dir_alpha95"]),
                                 float(this_specimen_interpretation[
                                     "meas_step_min"]),
                                 float(this_specimen_interpretation[
                                     "meas_step_max"]),
                                 float(
                                     this_specimen_interpretation["dir_dec"]),
                                 float(
                                     this_specimen_interpretation["dir_inc"]),
                                 calculation_type))
                if verbose:
                    saveit = input("Save this interpretation? [y]/n \n")
        else:
            print("no data", this_specimen)
        if verbose:
            pmagplotlib.draw_figs(ZED)
            #res = input('  <return> for next specimen, [q]uit  ')
            res = input("S[a]ve plots, [q]uit, or <return> to continue  ")
            if res == 'a':
                files = {
                    plot_type: this_specimen + "_" + plot_type + "." + fmt
                    for plot_type in ZED
                }
                pmagplotlib.save_plots(ZED, files)
                print("")
            if res == 'q':
                return
        k += 1
示例#8
0
def main():
    """
    NAME
        zeq_magic.py

    DESCRIPTION
        reads in magic_measurements formatted file, makes plots of remanence decay
        during demagnetization experiments.  Reads in prior interpretations saved in
        a pmag_specimens formatted file [and  allows re-interpretations of best-fit lines
        and planes and saves (revised or new) interpretations in a pmag_specimens file.
        interpretations are saved in the coordinate system used. Also allows judicious editting of
        measurements to eliminate "bad" measurements.  These are marked as such in the magic_measurements
        input file.  they are NOT deleted, just ignored. ] Bracketed part not yet implemented

    SYNTAX
        zeq_magic.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f  MEASFILE: sets measurements format input file, default: measurements.txt
        -fsp SPECFILE: sets specimens format file with prior interpreations, default: specimens.txt
        -fsa SAMPFILE: sets samples format file sample=>site information, default: samples.txt
        -fsi SITEFILE: sets sites format file with site=>location informationprior interpreations, default: samples.txt
        -Fp PLTFILE: sets filename for saved plot, default is name_type.fmt (where type is zijd, eqarea or decay curve)
        -crd [s,g,t]: sets coordinate system,  g=geographic, t=tilt adjusted, default: specimen coordinate system
        -spc SPEC  plots single specimen SPEC, saves plot with specified format
              with optional -dir settings and quits
        -dir [L,P,F][beg][end]: sets calculation type for principal component analysis, default is none
             beg: starting step for PCA calculation
             end: ending step for PCA calculation
             [L,P,F]: calculation type for line, plane or fisher mean
             must be used with -spc option
        -fmt FMT: set format of saved plot [png,svg,jpg]
        -A:  suppresses averaging of  replicate measurements, default is to average
        -sav: saves all plots without review
    SCREEN OUTPUT:
        Specimen, N, a95, StepMin, StepMax, Dec, Inc, calculation type

    """
    # initialize some variables
    doave, e, b = 1, 0, 0  # average replicates, initial end and beginning step
    intlist = ['magn_moment', 'magn_volume', 'magn_mass', 'magnitude']
    plots, coord = 0, 's'
    noorient = 0
    version_num = pmag.get_version()
    verbose = pmagplotlib.verbose
    calculation_type, fmt = "", "svg"
    user, spec_keys, locname = "", [], ''
    geo, tilt, ask = 0, 0, 0
    PriorRecs = []  # empty list for prior interpretations
    backup = 0
    specimen = ""  # can skip everything and just plot one specimen with bounds e,b
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    dir_path = pmag.get_named_arg_from_sys("-WD", default_val=os.getcwd())
    meas_file = pmag.get_named_arg_from_sys(
        "-f", default_val="measurements.txt")
    spec_file = pmag.get_named_arg_from_sys(
        "-fsp", default_val="specimens.txt")
    samp_file = pmag.get_named_arg_from_sys("-fsa", default_val="samples.txt")
    site_file = pmag.get_named_arg_from_sys("-fsi", default_val="sites.txt")
    #meas_file = os.path.join(dir_path, meas_file)
    #spec_file = os.path.join(dir_path, spec_file)
    #samp_file = os.path.join(dir_path, samp_file)
    #site_file = os.path.join(dir_path, site_file)
    plot_file = pmag.get_named_arg_from_sys("-Fp", default_val="")
    crd = pmag.get_named_arg_from_sys("-crd", default_val="s")
    if crd == "s":
        coord = "-1"
    elif crd == "t":
        coord = "100"
    else:
        coord = "0"
    fmt = pmag.get_named_arg_from_sys("-fmt", "svg")
    specimen = pmag.get_named_arg_from_sys("-spc", default_val="")
    beg_pca, end_pca = "", ""
    if '-dir' in sys.argv:
        ind = sys.argv.index('-dir')
        direction_type = sys.argv[ind + 1]
        beg_pca = int(sys.argv[ind + 2])
        end_pca = int(sys.argv[ind + 3])
        if direction_type == 'L':
            calculation_type = 'DE-BFL'
        if direction_type == 'P':
            calculation_type = 'DE-BFP'
        if direction_type == 'F':
            calculation_type = 'DE-FM'
    if '-A' in sys.argv:
        doave = 0
    if '-sav' in sys.argv:
        plots, verbose = 1, 0
    #
    first_save = 1
    fnames = {'measurements': meas_file, 'specimens': spec_file,
              'samples': samp_file, 'sites': site_file}
    contribution = nb.Contribution(dir_path, custom_filenames=fnames, read_tables=[
                                   'measurements', 'specimens', 'samples', 'sites'])
#
#   import  specimens

    specimen_cols = ['analysts', 'aniso_ftest', 'aniso_ftest12', 'aniso_ftest23', 'aniso_s', 'aniso_s_mean', 'aniso_s_n_measurements', 'aniso_s_sigma', 'aniso_s_unit', 'aniso_tilt_correction', 'aniso_type', 'aniso_v1', 'aniso_v2', 'aniso_v3', 'citations', 'description', 'dir_alpha95', 'dir_comp', 'dir_dec', 'dir_inc', 'dir_mad_free', 'dir_n_measurements', 'dir_tilt_correction', 'experiments', 'geologic_classes',
                     'geologic_types', 'hyst_bc', 'hyst_bcr', 'hyst_mr_moment', 'hyst_ms_moment', 'int_abs', 'int_b', 'int_b_beta', 'int_b_sigma', 'int_corr', 'int_dang', 'int_drats', 'int_f', 'int_fvds', 'int_gamma', 'int_mad_free', 'int_md', 'int_n_measurements', 'int_n_ptrm', 'int_q', 'int_rsc', 'int_treat_dc_field', 'lithologies', 'meas_step_max', 'meas_step_min', 'meas_step_unit', 'method_codes', 'sample', 'software_packages', 'specimen']
    if 'specimens' in contribution.tables:
        #        contribution.propagate_name_down('sample','measurements')
        spec_container = contribution.tables['specimens']
        if 'method_codes' not in spec_container.df.columns:
            spec_container.df['method_codes'] = None
        prior_spec_data = spec_container.get_records_for_code(
            'LP-DIR', strict_match=False)  # look up all prior directional interpretations
#
#  tie sample names to measurement data
#
    else:
        spec_container, prior_spec_data = None, []

#
#   import samples  for orientation info
#
    if ('samples' in contribution.tables) and ('specimens' in contribution.tables):
        #        contribution.propagate_name_down('site','measurements')
        contribution.propagate_cols(col_names=[
                                    'azimuth', 'dip', 'orientation_quality'], target_df_name='measurements', source_df_name='samples')
#
# define figure numbers for equal area, zijderveld,
#  and intensity vs. demagnetiztion step respectively
#
    ZED = {}
    ZED['eqarea'], ZED['zijd'],  ZED['demag'] = 1, 2, 3
    pmagplotlib.plot_init(ZED['eqarea'], 6, 6)
    pmagplotlib.plot_init(ZED['zijd'], 6, 6)
    pmagplotlib.plot_init(ZED['demag'], 6, 6)
#    save_pca=0
    angle, direction_type, setangle = "", "", 0
#   create measurement dataframe
#
    meas_container = contribution.tables['measurements']
    meas_data = meas_container.df
#
    meas_data = meas_data[meas_data['method_codes'].str.contains(
        'LT-NO|LT-AF-Z|LT-T-Z|LT-M-Z') == True]  # fish out steps for plotting
    meas_data = meas_data[meas_data['method_codes'].str.contains(
        'AN|ARM|LP-TRM|LP-PI-ARM') == False]  # strip out unwanted experiments
    intensity_types = [
        col_name for col_name in meas_data.columns if col_name in intlist]
    intensity_types = [
        col_name for col_name in intensity_types if any(meas_data[col_name])]
    if not len(intensity_types):
        print('-W- No intensity columns found')
        return
    # plot first non-empty intensity method found - normalized to initial value anyway -
    # doesn't matter which used
    int_key = intensity_types[0]
    # get all the non-null intensity records of the same type
    meas_data = meas_data[meas_data[int_key].notnull()]
    if 'flag' not in meas_data.columns:
        meas_data['flag'] = 'g'  # set the default flag to good
# need to treat LP-NO specially  for af data, treatment should be zero,
# otherwise 273.
    meas_data['treatment'] = meas_data['treat_ac_field'].where(
        cond=meas_data['treat_ac_field'] != 0, other=meas_data['treat_temp'])
    meas_data['ZI'] = 1  # initialize these to one
    meas_data['instrument_codes'] = ""  # initialize these to blank
#   for unusual case of microwave power....
    if 'treat_mw_power' in meas_data.columns:
        meas_data.loc[
            (meas_data.treat_mw_power != 0) &
            (meas_data.treat_mw_power) &
            (meas_data.treat_mw_time),
             'treatment'] = meas_data.treat_mw_power * meas_data.treat_mw_time
#
# get list of unique specimen names from measurement data
#
    # this is a list of all the specimen names
    specimen_names = meas_data.specimen.unique()
    specimen_names = specimen_names.tolist()
    specimen_names.sort()
#
# set up new DataFrame for this sessions specimen interpretations
#
    data_container = nb.MagicDataFrame(
        dtype='specimens', columns=specimen_cols)
    # this is for interpretations from this session
    current_spec_data = data_container.df
    locname = 'LookItUp'
    if specimen == "":
        k = 0
    else:
        k = specimen_names.index(specimen)
    # let's look at the data now
    while k < len(specimen_names):
        mpars = None
        # set the current specimen for plotting
        this_specimen = specimen_names[k]
        # reset beginning/end pca if plotting more than one specimen
        if not specimen:
            beg_pca, end_pca = "", ""
        if verbose and this_specimen != "":
            print(this_specimen, k + 1, 'out of ', len(specimen_names))
        if setangle == 0:
            angle = ""
        this_specimen_measurements = meas_data[meas_data['specimen'].str.contains(
            this_specimen) == True]  # fish out this specimen
        this_specimen_measurements = this_specimen_measurements[this_specimen_measurements['flag'].str.contains(
            'g') == True]  # fish out this specimen
        if len(this_specimen_measurements) != 0:  # if there are measurements
            #
            #    set up datablock [[treatment,dec, inc, int, direction_type],[....]]
            #
            #
            # figure out the method codes
            #
            units, methods, title = "", "", this_specimen
            # this is a list of all the specimen method codes`
            meas_meths = this_specimen_measurements.method_codes.unique()
            tr = pd.to_numeric(this_specimen_measurements.treatment).tolist()
            if set(tr) == set([0]):
                k += 1
                continue
            for m in meas_meths:
                if 'LT-AF-Z' in m:
                    units = 'T'  # units include tesla
                    tr[0] = 0
                if 'LT-T-Z' in m:
                    units = units + ":K"  # units include kelvin
                if 'LT-M-Z' in m:
                    units = units + ':J'  # units include joules
                    tr[0] = 0
                units = units.strip(':')  # strip off extra colons
                if 'LP-' in m:
                    methods = methods + ":" + m
            decs = pd.to_numeric(this_specimen_measurements.dir_dec).tolist()
            incs = pd.to_numeric(this_specimen_measurements.dir_inc).tolist()
#
#    fix the coordinate system
#
            if coord != '-1':  # need to transform coordinates to geographic

                azimuths = pd.to_numeric(
                    this_specimen_measurements.azimuth).tolist()  # get the azimuths
                # get the azimuths
                dips = pd.to_numeric(this_specimen_measurements.dip).tolist()
                dirs = [decs, incs, azimuths, dips]
                # this transposes the columns and rows of the list of lists
                dirs_geo = np.array(list(map(list, list(zip(*dirs)))))
                decs, incs = pmag.dogeo_V(dirs_geo)
                if coord == '100':  # need to do tilt correction too
                    bed_dip_dirs = pd.to_numeric(
                        this_specimen_measurements.bed_dip_dir).tolist()  # get the azimuths
                    bed_dips = pd.to_numeric(
                        this_specimen_measurements.bed_dip).tolist()  # get the azimuths
                    dirs = [decs, incs, bed_dip_dirs, bed_dips]
                    # this transposes the columns and rows of the list of lists
                    dirs_tilt = np.array(list(map(list, list(zip(*dirs)))))
                    decs, incs = pmag.dotilt_V(dirs_tilt)
                    title = title + '_t'
                else:
                    title = title + '_g'
            if angle == "":
                angle = decs[0]
            ints = pd.to_numeric(this_specimen_measurements[int_key]).tolist()
            ZI = this_specimen_measurements.ZI.tolist()
            flags = this_specimen_measurements.flag.tolist()
            codes = this_specimen_measurements.instrument_codes.tolist()
            datalist = [tr, decs, incs, ints, ZI, flags, codes]
            # this transposes the columns and rows of the list of lists
            datablock = list(map(list, list(zip(*datalist))))
            pmagplotlib.plotZED(ZED, datablock, angle, title, units)
            if verbose:
                pmagplotlib.drawFIGS(ZED)
#
#     collect info for current_specimen_interpretation dictionary
#

#
#     find prior interpretation
#

            prior_specimen_interpretations = prior_spec_data[prior_spec_data['specimen'].str.contains(this_specimen) == True]

            if (beg_pca == "") and (len(prior_specimen_interpretations) != 0):
                if len(prior_specimen_interpretations)>0:
                    beg_pcas = pd.to_numeric(
                        prior_specimen_interpretations.meas_step_min.values).tolist()
                    end_pcas = pd.to_numeric(
                        prior_specimen_interpretations.meas_step_max.values).tolist()
                    spec_methods = prior_specimen_interpretations.method_codes.tolist()
                # step through all prior interpretations and plot them
                    for ind in range(len(beg_pcas)):
                        spec_meths = spec_methods[ind].split(':')
                        for m in spec_meths:
                            if 'DE-BFL' in m:
                                calculation_type = 'DE-BFL'  # best fit line
                            if 'DE-BFP' in m:
                                calculation_type = 'DE-BFP'  # best fit plane
                            if 'DE-FM' in m:
                                calculation_type = 'DE-FM'  # fisher mean
                            if 'DE-BFL-A' in m:
                                calculation_type = 'DE-BFL-A'  # anchored best fit line
                        start, end = tr.index(beg_pcas[ind]), tr.index(
                            end_pcas[ind])  # getting the starting and ending points
                    # calculate direction/plane
                        mpars = pmag.domean(
                            datablock, start, end, calculation_type)
                        if mpars["specimen_direction_type"] != "Error":
                            # put it on the plot
                            pmagplotlib.plotDir(ZED, mpars, datablock, angle)
                            if verbose:
                                pmagplotlib.drawFIGS(ZED)
            else:
                try:
                    start, end = int(beg_pca), int(end_pca)
                except ValueError:
                    beg_pca = 0
                    end_pca = len(datablock) - 1
                    start, end = int(beg_pca), int(end_pca)
                # calculate direction/plane
                try:
                    mpars = pmag.domean(datablock, start, end, calculation_type)
                except Exception as ex:
                    print('-I- Problem with {}'.format(this_specimen))
                    print('   ', ex)
                    print('    Skipping')
                    k += 1
                    continue
                if mpars["specimen_direction_type"] != "Error":
                    # put it on the plot
                    pmagplotlib.plotDir(ZED, mpars, datablock, angle)
                    if verbose:
                        pmagplotlib.drawFIGS(ZED)
            if plots == 1 or specimen != "":
                if plot_file == "":
                    basename = title
                else:
                    basename = plot_file
                files = {}
                for key in list(ZED.keys()):
                    files[key] = basename + '_' + key + '.' + fmt
                pmagplotlib.saveP(ZED, files)
                if specimen != "":
                    sys.exit()
            if verbose:
                recnum = 0
                for plotrec in datablock:
                    if units == 'T':
                        print('%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' % (
                            plotrec[5], recnum, plotrec[0] * 1e3, " mT", plotrec[3], plotrec[1], plotrec[2], plotrec[6]))
                    if units == "K":
                        print('%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' % (
                            plotrec[5], recnum, plotrec[0] - 273, ' C', plotrec[3], plotrec[1], plotrec[2], plotrec[6]))
                    if units == "J":
                        print('%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' % (
                            plotrec[5], recnum, plotrec[0], ' J', plotrec[3], plotrec[1], plotrec[2], plotrec[6]))
                    if 'K' in units and 'T' in units:
                        if plotrec[0] >= 1.:
                            print('%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' % (
                                plotrec[5], recnum, plotrec[0] - 273, ' C', plotrec[3], plotrec[1], plotrec[2], plotrec[6]))
                        if plotrec[0] < 1.:
                            print('%s: %i  %7.1f %s  %8.3e %7.1f %7.1f %s' % (
                                plotrec[5], recnum, plotrec[0] * 1e3, " mT", plotrec[3], plotrec[1], plotrec[2], plotrec[6]))
                    recnum += 1
            # we have a current interpretation
            elif mpars["specimen_direction_type"] != "Error":
                #
                # create a new specimen record for the interpreation for this
                # specimen
                this_specimen_interpretation = {
                    col: "" for col in specimen_cols}
#               this_specimen_interpretation["analysts"]=user
                this_specimen_interpretation['software_packages'] = version_num
                this_specimen_interpretation['specimen'] = this_specimen
                this_specimen_interpretation["method_codes"] = calculation_type
                this_specimen_interpretation["meas_step_unit"] = units
                this_specimen_interpretation["meas_step_min"] = tr[start]
                this_specimen_interpretation["meas_step_max"] = tr[end]
                this_specimen_interpretation["dir_dec"] = '%7.1f' % (
                    mpars['specimen_dec'])
                this_specimen_interpretation["dir_inc"] = '%7.1f' % (
                    mpars['specimen_inc'])
                this_specimen_interpretation["dir_dang"] = '%7.1f' % (
                    mpars['specimen_dang'])
                this_specimen_interpretation["dir_n_measurements"] = '%i' % (
                    mpars['specimen_n'])
                this_specimen_interpretation["dir_tilt_correction"] = coord
                methods = methods.replace(" ", "")
                if "T" in units:
                    methods = methods + ":LP-DIR-AF"
                if "K" in units:
                    methods = methods + ":LP-DIR-T"
                if "J" in units:
                    methods = methods + ":LP-DIR-M"
                this_specimen_interpretation["method_codes"] = methods.strip(
                    ':')
                this_specimen_interpretation["experiments"] = this_specimen_measurements.experiment.unique()[
                    0]
#
#   print some stuff
#
                if calculation_type != 'DE-FM':
                    this_specimen_interpretation["dir_mad_free"] = '%7.1f' % (
                        mpars['specimen_mad'])
                    this_specimen_interpretation["dir_alpha95"] = ''
                    if verbose:
                        if units == 'K':
                            print('%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %s \n' % (this_specimen_interpretation["specimen"], int(this_specimen_interpretation["dir_n_measurements"]), float(this_specimen_interpretation["dir_mad_free"]), float(this_specimen_interpretation["dir_dang"]), float(
                                this_specimen_interpretation["meas_step_min"]) - 273, float(this_specimen_interpretation["meas_step_max"]) - 273, float(this_specimen_interpretation["dir_dec"]), float(this_specimen_interpretation["dir_inc"]), calculation_type))
                        elif units == 'T':
                            print('%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %s \n' % (this_specimen_interpretation["specimen"], int(this_specimen_interpretation["dir_n_measurements"]), float(this_specimen_interpretation["dir_mad_free"]), float(this_specimen_interpretation["dir_dang"]), float(
                                this_specimen_interpretation["meas_step_min"]) * 1e3, float(this_specimen_interpretation["meas_step_max"]) * 1e3, float(this_specimen_interpretation["dir_dec"]), float(this_specimen_interpretation["dir_inc"]), calculation_type))
                        elif 'T' in units and 'K' in units:
                            if float(this_specimen_interpretation['meas_step_min']) < 1.0:
                                min = float(
                                    this_specimen_interpretation['meas_step_min']) * 1e3
                            else:
                                min = float(
                                    this_specimen_interpretation['meas_step_min']) - 273
                            if float(this_specimen_interpretation['meas_step_max']) < 1.0:
                                max = float(
                                    this_specimen_interpretation['meas_step_max']) * 1e3
                            else:
                                max = float(
                                    this_specimen_interpretation['meas_step_max']) - 273
                            print('%s %i %7.1f %i %i %7.1f %7.1f %7.1f %s \n' % (this_specimen_interpretation["specimen"], int(this_specimen_interpretation["dir_n_measurements"]), float(this_specimen_interpretation["dir_mad_free"]), float(
                                this_specimen_interpretation["dir_dang"]), min, max, float(this_specimen_interpretation["dir_dec"]), float(this_specimen_interpretation["dir_inc"]), calculation_type))
                        else:
                            print('%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %s \n' % (this_specimen_interpretation["specimen"], int(this_specimen_interpretation["dir_n_measurements"]), float(this_specimen_interpretation["dir_mad_free"]), float(this_specimen_interpretation["dir_dang"]), float(
                                this_specimen_interpretation["meas_step_min"]), float(this_specimen_interpretation["meas_step_max"]), float(this_specimen_interpretation["dir_dec"]), float(this_specimen_interpretation["dir_inc"]), calculation_type))
                else:
                    this_specimen_interpretation["dir_alpha95"] = '%7.1f' % (
                        mpars['specimen_alpha95'])
                    this_specimen_interpretation["dir_mad_free"] = ''
                    if verbose:
                        if 'K' in units:
                            print('%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %s \n' % (this_specimen_interpretation["specimen"], int(this_specimen_interpretation["dir_n_measurments"]), float(this_specimen_interpretation["dir_mad_free"]), float(this_specimen_interpretation["dir_dang"]), float(
                                this_specimen_interpretation["meas_step_min"]) - 273, float(this_specimen_interpretation["meas_step_max"]) - 273, float(this_specimen_interpretation["dir_dec"]), float(this_specimen_interpretation["dir_inc"]), calculation_type))
                        elif 'T' in units:
                            print('%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %7.1f %s \n' % (this_specimen_interpretation["specimen"], int(this_specimen_interpretation["dir_n_measurements"]), float(this_specimen_interpretation["dir_alpha95"]), float(this_specimen_interpretation["dir_dang"]), float(
                                this_specimen_interpretation["meas_step_min"]) * 1e3, float(this_specimen_interpretation["meas_step_max"]) * 1e3, float(this_specimen_interpretation["dir_dec"]), float(this_specimen_interpretation["dir_inc"]), calculation_type))
                        elif 'T' in units and 'K' in units:
                            if float(this_specimen_interpretation['meas_step_min']) < 1.0:
                                min = float(
                                    this_specimen_interpretation['meas_step_min']) * 1e3
                            else:
                                min = float(
                                    this_specimen_interpretation['meas_step_min']) - 273
                            if float(this_specimen_interpretation['meas_step_max']) < 1.0:
                                max = float(
                                    this_specimen_interpretation['meas_step_max']) * 1e3
                            else:
                                max = float(
                                    this_specimen_interpretation['meas_step_max']) - 273
                            print('%s %i %7.1f %i %i %7.1f %7.1f %s \n' % (this_specimen_interpretation["specimen"], int(this_specimen_interpretation["dir_n_measurements"]), float(
                                this_specimen_interpretation["dir_alpha95"]), min, max, float(this_specimen_interpretation["dir_dec"]), float(this_specimen_interpretation["dir_inc"]), calculation_type))
                        else:
                            print('%s %i %7.1f %7.1f %7.1f %7.1f %7.1f %s \n' % (this_specimen_interpretation["specimen"], int(this_specimen_interpretation["dir_n_measurements"]), float(this_specimen_interpretation["dir_alpha95"]), float(
                                this_specimen_interpretation["meas_step_min"]), float(this_specimen_interpretation["meas_step_max"]), float(this_specimen_interpretation["dir_dec"]), float(this_specimen_interpretation["dir_inc"]), calculation_type))
                if verbose:
                    saveit = input("Save this interpretation? [y]/n \n")
#   START HERE
#
#         if len(current_spec_data)==0: # no interpretations yet for this session
#             print "no current interpretation"
#             beg_pca,end_pca="",""
#             calculation_type=""
# get the ones that meet the current coordinate system
        else:
            print("no data")
        if verbose:
            res = input('  <return> for next specimen, [q]uit  ')
            if res == 'q':
                return
        k += 1
示例#9
0
def main():
    """
    NAME
        thellier_magic.py

    DESCRIPTION
        plots Thellier-Thellier data in version 3.0 format
        Reads saved interpretations from a specimen formatted table, default: specimens.txt

    SYNTAX
        thellier_magic.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f MEAS, set measurements input file, default is 'measurements.txt'
        -fsp PRIOR, set specimens.txt prior interpretations file, default is 'specimens.txt'
        -fcr CRIT, set criteria file for grading.  # not yet implemented
        -fmt [svg,png,jpg], format for images - default is svg
        -sav,  saves plots with out review (in format specified by -fmt key or default)
        -spc SPEC, plots single specimen SPEC, saves plot with specified format
            with optional -b bounds and quits
        -b BEG END: sets  bounds for calculation
           BEG: starting step number for slope calculation
           END: ending step number for slope calculation
        -z use only z component difference for pTRM calculation

    OUTPUT
        figures:
            ALL:  numbers refer to temperature steps in command line window
            1) Arai plot:  closed circles are zero-field first/infield
                           open circles are infield first/zero-field
                           triangles are pTRM checks
                           squares are pTRM tail checks
                           VDS is vector difference sum
                           diamonds are bounds for interpretation
            2) Zijderveld plot:  closed (open) symbols are X-Y (X-Z) planes
                                 X rotated to NRM direction
            3) (De/Re)Magnetization diagram:
                           circles are NRM remaining
                           squares are pTRM gained
            4) equal area projections:
               green triangles are pTRM gained direction
                           red (purple) circles are lower(upper) hemisphere of ZI step directions
                           blue (cyan) squares are lower(upper) hemisphere IZ step directions
            5) Optional:  TRM acquisition
            6) Optional: TDS normalization
        command line window:
            list is: temperature step numbers, temperatures (C), Dec, Inc, Int (units of measuements)
                     list of possible commands: type letter followed by return to select option
                     saving of plots creates image files with specimen, plot type as name
    """
    #
    #   initializations
    #
    version_num = pmag.get_version()
    verbose = pmagplotlib.verbose
    #
    # default acceptance criteria
    #
    accept = pmag.default_criteria(0)[0]  # set the default criteria
    #
    # parse command line options
    #
    plots, fmt, Zdiff = 0, 'svg', 0
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    dir_path = pmag.get_named_arg_from_sys("-WD", default_val=os.getcwd())
    meas_file = pmag.get_named_arg_from_sys("-f",
                                            default_val="measurements.txt")
    spec_file = pmag.get_named_arg_from_sys("-fsp",
                                            default_val="specimens.txt")
    crit_file = pmag.get_named_arg_from_sys("-fcr", default_val="criteria.txt")
    spec_file = os.path.join(dir_path, spec_file)
    meas_file = os.path.join(dir_path, meas_file)
    crit_file = os.path.join(dir_path, crit_file)
    fmt = pmag.get_named_arg_from_sys("-fmt", "svg")
    if '-sav' in sys.argv: plots, verbose = 1, 0
    if '-z' in sys.argv: Zdiff = 1
    specimen = pmag.get_named_arg_from_sys("-spc", default_val="")
    if '-b' in sys.argv:
        ind = sys.argv.index('-b')
        start = int(sys.argv[ind + 1])
        end = int(sys.argv[ind + 2])
    else:
        start, end = "", ""
    fnames = {
        'measurements': meas_file,
        'specimens': spec_file,
        'criteria': crit_file
    }
    contribution = nb.Contribution(
        dir_path,
        custom_filenames=fnames,
        read_tables=['measurements', 'specimens', 'criteria'])
    #
    #   import  prior interpretations  from specimen file
    #
    specimen_cols = [
        'analysts', 'aniso_ftest', 'aniso_ftest12', 'aniso_ftest23', 'aniso_s',
        'aniso_s_mean', 'aniso_s_n_measurements', 'aniso_s_sigma',
        'aniso_s_unit', 'aniso_tilt_correction', 'aniso_type', 'aniso_v1',
        'aniso_v2', 'aniso_v3', 'citations', 'description', 'dir_alpha95',
        'dir_comp', 'dir_dec', 'dir_inc', 'dir_mad_free', 'dir_n_measurements',
        'dir_tilt_correction', 'experiments', 'geologic_classes',
        'geologic_types', 'hyst_bc', 'hyst_bcr', 'hyst_mr_moment',
        'hyst_ms_moment', 'int_abs', 'int_b', 'int_b_beta', 'int_b_sigma',
        'int_corr', 'int_dang', 'int_drats', 'int_f', 'int_fvds', 'int_gamma',
        'int_mad_free', 'int_md', 'int_n_measurements', 'int_n_ptrm', 'int_q',
        'int_rsc', 'int_treat_dc_field', 'lithologies', 'meas_step_max',
        'meas_step_min', 'meas_step_unit', 'method_codes', 'sample',
        'software_packages', 'specimen'
    ]
    if 'specimens' in contribution.tables:
        spec_container = contribution.tables['specimens']
        prior_spec_data = spec_container.get_records_for_code(
            'LP-PI-TRM',
            strict_match=False)  # look up all prior intensity interpretations
    else:
        spec_container, prior_spec_data = None, []
    backup = 0
    #
    Mkeys = ['magn_moment', 'magn_volume', 'magn_mass']
    #
    #   create measurement dataframe
    #
    meas_container = contribution.tables['measurements']
    meas_data = meas_container.df
    #
    meas_data['method_codes'] = meas_data['method_codes'].str.replace(
        " ", "")  # get rid of nasty spaces
    meas_data = meas_data[meas_data['method_codes'].str.contains(
        'LP-PI-TRM|LP-TRM|LP-TRM-TD') ==
                          True]  # fish out zero field steps for plotting
    intensity_types = [
        col_name for col_name in meas_data.columns if col_name in Mkeys
    ]
    int_key = intensity_types[
        0]  # plot first intensity method found - normalized to initial value anyway - doesn't matter which used
    meas_data = meas_data[meas_data[int_key].notnull(
    )]  # get all the non-null intensity records of the same type
    if 'flag' not in meas_data.columns:
        meas_data['flag'] = 'g'  # set the default flag to good
    meas_data = meas_data[meas_data['flag'].str.contains('g') ==
                          True]  # only the 'good' measurements
    thel_data = meas_data[meas_data['method_codes'].str.contains('LP-PI-TRM')
                          == True]  # get all the Thellier data
    trm_data = meas_data[meas_data['method_codes'].str.contains('LP-TRM') ==
                         True]  # get all the TRM acquisition data
    td_data = meas_data[meas_data['method_codes'].str.contains('LP-TRM-TD') ==
                        True]  # get all the TD data
    anis_data = meas_data[meas_data['method_codes'].str.contains('LP-AN') ==
                          True]  # get all the anisotropy data
    #
    # get list of unique specimen names from measurement data
    #
    specimen_names = meas_data.specimen.unique(
    )  # this is a Series of all the specimen names
    specimen_names = specimen_names.tolist()  # turns it into a list
    specimen_names.sort()  # sorts by specimen name
    #
    # set up new DataFrame for this sessions specimen interpretations
    #
    spec_container = nb.MagicDataFrame(dtype='specimens',
                                       columns=specimen_cols)
    current_spec_data = spec_container.df  # this is for interpretations from this session
    if specimen == "":  # do all specimens
        k = 0
    else:
        k = specimen_names.index(specimen)  # just do this one
    # define figure numbers for arai, zijderveld and
    #   de-,re-magnetization diagrams
    AZD = {}
    AZD['deremag'], AZD['zijd'], AZD['arai'], AZD['eqarea'] = 1, 2, 3, 4
    pmagplotlib.plot_init(AZD['arai'], 5, 5)
    pmagplotlib.plot_init(AZD['zijd'], 5, 5)
    pmagplotlib.plot_init(AZD['deremag'], 5, 5)
    pmagplotlib.plot_init(AZD['eqarea'], 5, 5)
    if len(trm_data) > 0:
        AZD['TRM'] = 5
        pmagplotlib.plot_init(AZD['TRM'], 5, 5)
    if len(td_data) > 0:
        AZD['TDS'] = 6
        pmagplotlib.plot_init(AZD['TDS'], 5, 5)
    #
    while k < len(specimen_names):
        this_specimen = specimen_names[
            k]  # set the current specimen for plotting
        if verbose and this_specimen != "":
            print(this_specimen, k + 1, 'out of ', len(specimen_names))
        #
        #    set up datablocks
        #
        thelblock = thel_data[thel_data['specimen'].str.contains(this_specimen)
                              == True]  # fish out this specimen
        trmblock = trm_data[trm_data['specimen'].str.contains(this_specimen) ==
                            True]  # fish out this specimen
        tdsrecs = td_data[td_data['specimen'].str.contains(this_specimen) ==
                          True]  # fish out this specimen
        anisblock = anis_data[anis_data['specimen'].str.contains(this_specimen)
                              == True]  # fish out the anisotropy data
        prior_specimen_interpretations = prior_spec_data[
            prior_spec_data['specimen'].str.contains(
                this_specimen) == True]  # fish out prior interpretation
        #
        # sort data into types
        #
        araiblock, field = pmag.sortarai(thelblock,
                                         this_specimen,
                                         Zdiff,
                                         version=3)
        first_Z = araiblock[0]
        GammaChecks = araiblock[5]
        if len(first_Z) < 3:
            if backup == 0:
                k += 1
                if verbose:
                    print('skipping specimen - moving forward ', this_specimen)
            else:
                k -= 1
                if verbose:
                    print('skipping specimen - moving backward ',
                          this_specimen)
        else:
            backup = 0
            zijdblock, units = pmag.find_dmag_rec(this_specimen,
                                                  thelblock,
                                                  version=3)
            if start == "" and len(prior_specimen_interpretations) > 0:
                if verbose: print('Looking up saved interpretation....')
                #
                # get prior interpretation steps
                #
                beg_int = pd.to_numeric(prior_specimen_interpretations.
                                        meas_step_min.values).tolist()[0]
                end_int = pd.to_numeric(prior_specimen_interpretations.
                                        meas_step_max.values).tolist()[0]
            else:
                beg_int, end_int = "", ""
            recnum = 0
            if verbose: print("index step Dec   Inc  Int       Gamma")
            for plotrec in zijdblock:
                if plotrec[0] == beg_int:
                    start = recnum  # while we are at it, collect these bounds
                if plotrec[0] == end_int: end = recnum
                if verbose:
                    if GammaChecks != "":
                        gamma = ""
                        for g in GammaChecks:
                            if g[0] == plotrec[0] - 273:
                                gamma = g[1]
                                break
                    if gamma != "":
                        print('%i     %i %7.1f %7.1f %8.3e %7.1f' %
                              (recnum, plotrec[0] - 273, plotrec[1],
                               plotrec[2], plotrec[3], gamma))
                    else:
                        print('%i     %i %7.1f %7.1f %8.3e ' %
                              (recnum, plotrec[0] - 273, plotrec[1],
                               plotrec[2], plotrec[3]))
                recnum += 1
            for fig in list(AZD.keys()):
                pmagplotlib.clearFIG(AZD[fig])  # clear all figures
            pmagplotlib.plotAZ(AZD, araiblock, zijdblock, this_specimen,
                               units[0])
            if verbose: pmagplotlib.drawFIGS(AZD)
            pars, errcode = pmag.PintPars(thelblock,
                                          araiblock,
                                          zijdblock,
                                          start,
                                          end,
                                          accept,
                                          version=3)
            pars['measurement_step_unit'] = "K"
            pars['experiment_type'] = 'LP-PI-TRM'
            #
            # work on saving interpretations stuff later
            #
            if errcode != 1:  # no problem in PintPars
                pars["specimen_lab_field_dc"] = field
                pars["specimen_int"] = -1 * field * pars["specimen_b"]
                pars["er_specimen_name"] = this_specimen
                #pars,kill=pmag.scoreit(pars,this_specimen_interpretation,accept,'',verbose) # deal with this later
                pars["specimen_grade"] = 'None'
                pars['measurement_step_min'] = pars['meas_step_min']
                pars['measurement_step_max'] = pars['meas_step_max']
                if pars['measurement_step_unit'] == 'K':
                    outstr = "specimen     Tmin  Tmax  N  lab_field  B_anc  b  q  f(coe)  Fvds  beta  MAD  Dang  Drats  Nptrm  Grade  R  MD%  sigma  Gamma_max \n"
                    pars_out = (this_specimen, (pars["meas_step_min"] - 273),
                                (pars["meas_step_max"] -
                                 273), (pars["specimen_int_n"]),
                                1e6 * (pars["specimen_lab_field_dc"]),
                                1e6 * (pars["specimen_int"]),
                                pars["specimen_b"], pars["specimen_q"],
                                pars["specimen_f"], pars["specimen_fvds"],
                                pars["specimen_b_beta"], pars["int_mad_free"],
                                pars["int_dang"], pars["int_drats"],
                                pars["int_n_ptrm"], pars["specimen_grade"],
                                np.sqrt(pars["specimen_rsc"]),
                                int(pars["int_md"]), pars["specimen_b_sigma"],
                                pars['specimen_gamma'])
                    outstring = '%s %4.0f %4.0f %i %4.1f %4.1f %5.3f %5.1f %5.3f %5.3f %5.3f  %7.1f %7.1f %7.1f %s %s %6.3f %i %5.3f %7.1f' % pars_out + '\n'
                elif pars['measurement_step_unit'] == 'J':
                    outstr = "specimen     Wmin  Wmax  N  lab_field  B_anc  b  q  f(coe)  Fvds  beta  MAD  Dang  Drats  Nptrm  Grade  R  MD%  sigma  ThetaMax DeltaMax GammaMax\n"
                    pars_out = (
                        this_specimen, (pars["meas_step_min"]),
                        (pars["meas_step_max"]), (pars["specimen_int_n"]),
                        1e6 * (pars["specimen_lab_field_dc"]),
                        1e6 * (pars["specimen_int"]), pars["specimen_b"],
                        pars["specimen_q"], pars["specimen_f"],
                        pars["specimen_fvds"], pars["specimen_b_beta"],
                        pars["specimen_int_mad"], pars["specimen_int_dang"],
                        pars["specimen_drats"], pars["specimen_int_ptrm_n"],
                        pars["specimen_grade"], np.sqrt(pars["specimen_rsc"]),
                        int(pars["specimen_md"]), pars["specimen_b_sigma"],
                        pars["specimen_theta"], pars["specimen_delta"],
                        pars["specimen_gamma"])
                    outstring = '%s %4.0f %4.0f %i %4.1f %4.1f %5.3f %5.1f %5.3f %5.3f %5.3f  %7.1f %7.1f %7.1f %s %s %6.3f %i %5.3f %7.1f %7.1f %7.1f' % pars_out + '\n'
                print(outstr)
                print(outstring)
                pmagplotlib.plotB(AZD, araiblock, zijdblock, pars)
                mpars = pmag.domean(araiblock[1], start, end, 'DE-BFL')
                if verbose:
                    pmagplotlib.drawFIGS(AZD)
                    print('pTRM direction= ',
                          '%7.1f' % (mpars['specimen_dec']),
                          ' %7.1f' % (mpars['specimen_inc']), ' MAD:',
                          '%7.1f' % (mpars['specimen_mad']))
            if len(anisblock) > 0:  # this specimen has anisotropy data
                if verbose:
                    print('Found anisotropy record... but ignoring for now ')
            if plots == 1:
                if fmt != "pmag":
                    files = {}
                    for key in list(AZD.keys()):
                        files[
                            key] = 'SP:_' + this_specimen + '_TY:_' + key + '_' + '.' + fmt
                    if pmagplotlib.isServer:
                        black = '#000000'
                        purple = '#800080'
                        titles = {}
                        titles['deremag'] = 'DeReMag Plot'
                        titles['zijd'] = 'Zijderveld Plot'
                        titles['arai'] = 'Arai Plot'
                        titles['TRM'] = 'TRM Acquisition data'
                        AZD = pmagplotlib.addBorders(AZD, titles, black,
                                                     purple)
                    pmagplotlib.saveP(AZD, files)
                else:  # save in pmag format
                    print('pmag format no longer supported')
                    #script="grep "+this_specimen+" output.mag | thellier -mfsi"
                    #script=script+' %8.4e'%(field)
                    #min='%i'%((pars["measurement_step_min"]-273))
                    #Max='%i'%((pars["measurement_step_max"]-273))
                    #script=script+" "+min+" "+Max
                    #script=script+" |plotxy;cat mypost >>thellier.ps\n"
                    #pltf.write(script)
                    #pmag.domagicmag(outf,MeasRecs)
            if specimen != "": sys.exit()  # syonara
            if verbose:
                ans = input('Return for next specimen, q to quit:  ')
                if ans == 'q': sys.exit()
            k += 1  # moving on
示例#10
0
def main():
    """
    NAME
       zeq.py
  
    DESCRIPTION
       plots demagnetization data. The equal area projection has the X direction (usually North in geographic coordinates)
          to the top.  The red line is the X axis of the Zijderveld diagram.  Solid symbols are lower hemisphere. 
          The solid (open) symbols in the Zijderveld diagram are X,Y (X,Z) pairs.  The demagnetization diagram plots the
          fractional remanence remaining after each step. The green line is the fraction of the total remaence removed 
          between each step.        

    INPUT FORMAT
       takes specimen_name treatment intensity declination inclination  in space
 delimited file

    SYNTAX
        zeq.py [command line options

    OPTIONS
        -f FILE for reading from command line
        -u [mT,C] specify units of mT OR C, default is unscaled
    """
    if '-h' in sys.argv: # check if help is needed
        print main.__doc__
        sys.exit() # graceful quit
    else:
        if '-f' in sys.argv:
            ind=sys.argv.index('-f')
            file=sys.argv[ind+1]
        else:
            print main.__doc__
            sys.exit()
        if '-u' in sys.argv:
            ind=sys.argv.index('-u')
            units=sys.argv[ind+1]
            if units=="C":SIunits="K"
            if units=="mT":SIunits="T"
        else:
            units="U"
            SIunits="U"
    f=open(file,'rU')
    data=f.readlines()
#
    datablock= [] # set up list for data
    fmt='svg' # default image format
    s="" # initialize specimen name
    angle=0.
    for line in data:   # read in the data from standard input
        rec=line.split() # split each line on space to get records
        if angle=="":angle=float(rec[3])
        if s=="":s=rec[0]
        if units=='mT':datablock.append([float(rec[1])*1e-3,float(rec[3]),float(rec[4]),1e-3*float(rec[2]),'','g']) # treatment, dec, inc, int # convert to T and Am^2 (assume emu)
        if units=='C':datablock.append([float(rec[1])+273.,float(rec[3]),float(rec[4]),1e-3*float(rec[2]),'','g']) # treatment, dec, inc, int, convert to K and Am^2, assume emu
        if units=='U':datablock.append([float(rec[1]),float(rec[3]),float(rec[4]),float(rec[2]),'','g']) # treatment, dec, inc, int, using unscaled units 
# define figure numbers in a dictionary for equal area, zijderveld,  
#  and intensity vs. demagnetiztion step respectively
    ZED={}
    ZED['eqarea'],ZED['zijd'],  ZED['demag']=1,2,3 
    pmagplotlib.plot_init(ZED['eqarea'],5,5) # initialize plots
    pmagplotlib.plot_init(ZED['zijd'],5,5)
    pmagplotlib.plot_init(ZED['demag'],5,5)
#
#
    pmagplotlib.plotZED(ZED,datablock,angle,s,SIunits) # plot the data
    pmagplotlib.drawFIGS(ZED)
    while 1:

#
# print out data for this sample to screen
#
        recnum=0
        for plotrec in datablock:
            if units=='mT':print '%i  %7.1f %8.3e %7.1f %7.1f ' % (recnum,plotrec[0]*1e3,plotrec[3],plotrec[1],plotrec[2])
            if units=='C':print '%i  %7.1f %8.3e %7.1f %7.1f ' % (recnum,plotrec[0]-273.,plotrec[3],plotrec[1],plotrec[2])
            if units=='U':print '%i  %7.1f %8.3e %7.1f %7.1f ' % (recnum,plotrec[0],plotrec[3],plotrec[1],plotrec[2])
            recnum += 1
        end_pca=len(datablock)-1 # initialize end_pca, beg_pca to first and last measurement
        beg_pca=0
        ans=raw_input(" s[a]ve plot, [b]ounds for pca and calculate, change [h]orizontal projection angle, [q]uit:   ")
        if ans =='q':
            sys.exit() 
        if  ans=='a':
            files={}
            for key in ZED.keys():
                files[key]=s+'_'+key+'.'+fmt 
            pmagplotlib.saveP(ZED,files)
        if ans=='h':
            angle=float(raw_input(" Declination to project onto horizontal axis? "))
            pmagplotlib.plotZED(ZED,datablock,angle,s,SIunits) # plot the data
        if ans=='b':
            GoOn=0
            while GoOn==0: # keep going until reasonable bounds are set
                print 'Enter index of first point for pca: ','[',beg_pca,']'
                answer=raw_input('return to keep default  ')
                if answer != "":beg_pca=int(answer)
                print 'Enter index  of last point for pca: ','[',end_pca,']'
                answer=raw_input('return to keep default  ')
                if answer != "":
                    end_pca=int(answer) 
                if beg_pca >=0 and beg_pca<=len(datablock)-2 and end_pca>0 and end_pca<len(datablock): 
                    GoOn=1
                else:
                    print "Bad entry of indices - try again"
                    end_pca=len(datablock)-1
                    beg_pca=0
            GoOn=0
            while GoOn==0:
                ct=raw_input('Enter Calculation Type: best-fit line,  plane or fisher mean [l]/p/f :  ' )
                if ct=="" or ct=="l": 
                    calculation_type="DE-BFL"
                    GoOn=1 # all good
                elif ct=='p':
                    calculation_type="DE-BFP"
                    GoOn=1 # all good
                elif ct=='f':
                    calculation_type="DE-FM"
                    GoOn=1 # all good
                else: 
                    print "bad entry of calculation type: try again. " # keep going
            pmagplotlib.plotZED(ZED,datablock,angle,s,SIunits) # plot the data
            mpars=pmag.domean(datablock,beg_pca,end_pca,calculation_type) # get best-fit direction/great circle
            pmagplotlib.plotDir(ZED,mpars,datablock,angle) # plot the best-fit direction/great circle
            print 'Specimen, calc_type, N, min, max, MAD, dec, inc'
            if units=='mT':print '%s %s %i  %6.2f %6.2f %6.1f %7.1f %7.1f' % (s,calculation_type,mpars["specimen_n"],mpars["measurement_step_min"]*1e3,mpars["measurement_step_max"]*1e3,mpars["specimen_mad"],mpars["specimen_dec"],mpars["specimen_inc"])
            if units=='C':print '%s %s %i  %6.2f %6.2f %6.1f %7.1f %7.1f' % (s,calculation_type,mpars["specimen_n"],mpars["measurement_step_min"]-273,mpars["measurement_step_max"]-273,mpars["specimen_mad"],mpars["specimen_dec"],mpars["specimen_inc"])
            if units=='U':print '%s %s %i  %6.2f %6.2f %6.1f %7.1f %7.1f' % (s,calculation_type,mpars["specimen_n"],mpars["measurement_step_min"],mpars["measurement_step_max"],mpars["specimen_mad"],mpars["specimen_dec"],mpars["specimen_inc"])
示例#11
0
def main():
    """
    NAME
       zeq.py
  
    DESCRIPTION
       plots demagnetization data. The equal area projection has the X direction (usually North in geographic coordinates)
          to the top.  The red line is the X axis of the Zijderveld diagram.  Solid symbols are lower hemisphere. 
          The solid (open) symbols in the Zijderveld diagram are X,Y (X,Z) pairs.  The demagnetization diagram plots the
          fractional remanence remaining after each step. The green line is the fraction of the total remaence removed 
          between each step.        

    INPUT FORMAT
       takes specimen_name treatment intensity declination inclination  in space
 delimited file

    SYNTAX
        zeq.py [command line options

    OPTIONS
        -f FILE for reading from command line
        -u [mT,C] specify units of mT OR C, default is unscaled
        -sav save figure and quit
        -fmt [svg,jpg,png,pdf] set figure format [default is svg]
        -beg [step number] treatment step for beginning of PCA calculation, 0 is default
        -end [step number] treatment step for end of PCA calculation, last step is default
        -ct [l,p,f] Calculation Type: best-fit line,  plane or fisher mean; line is default

    """
    files, fmt, plot = {}, 'svg', 0
    end_pca, beg_pca = "", ""
    calculation_type = 'DE-BFL'
    if '-h' in sys.argv:  # check if help is needed
        print(main.__doc__)
        sys.exit()  # graceful quit
    else:
        if '-f' in sys.argv:
            ind = sys.argv.index('-f')
            file = sys.argv[ind + 1]
        else:
            print(main.__doc__)
            sys.exit()
        if '-u' in sys.argv:
            ind = sys.argv.index('-u')
            units = sys.argv[ind + 1]
            if units == "C": SIunits = "K"
            if units == "mT": SIunits = "T"
        else:
            units = "U"
            SIunits = "U"
    if '-sav' in sys.argv: plot = 1
    if '-ct' in sys.argv:
        ind = sys.argv.index('-ct')
        ct = sys.argv[ind + 1]
        if ct == 'f': calculation_type = 'DE-FM'
        if ct == 'p': calculation_type = 'DE-BFP'
    if '-fmt' in sys.argv:
        ind = sys.argv.index('-fmt')
        fmt = sys.argv[ind + 1]
    if '-beg' in sys.argv:
        ind = sys.argv.index('-beg')
        beg_pca = int(sys.argv[ind + 1])
    if '-end' in sys.argv:
        ind = sys.argv.index('-end')
        end_pca = int(sys.argv[ind + 1])
    f = open(file, 'r')
    data = f.readlines()
    #
    datablock = []  # set up list for data
    s = ""  # initialize specimen name
    angle = 0.
    for line in data:  # read in the data from standard input
        rec = line.split()  # split each line on space to get records
        if angle == "": angle = float(rec[3])
        if s == "": s = rec[0]
        if units == 'mT':
            datablock.append([
                float(rec[1]) * 1e-3,
                float(rec[3]),
                float(rec[4]), 1e-3 * float(rec[2]), '', 'g'
            ])  # treatment, dec, inc, int # convert to T and Am^2 (assume emu)
        if units == 'C':
            datablock.append([
                float(rec[1]) + 273.,
                float(rec[3]),
                float(rec[4]), 1e-3 * float(rec[2]), '', 'g'
            ])  # treatment, dec, inc, int, convert to K and Am^2, assume emu
        if units == 'U':
            datablock.append([
                float(rec[1]),
                float(rec[3]),
                float(rec[4]),
                float(rec[2]), '', 'g'
            ])  # treatment, dec, inc, int, using unscaled units


# define figure numbers in a dictionary for equal area, zijderveld,
#  and intensity vs. demagnetiztion step respectively
    ZED = {}
    ZED['eqarea'], ZED['zijd'], ZED['demag'] = 1, 2, 3
    pmagplotlib.plot_init(ZED['eqarea'], 5, 5)  # initialize plots
    pmagplotlib.plot_init(ZED['zijd'], 5, 5)
    pmagplotlib.plot_init(ZED['demag'], 5, 5)
    #
    #
    pmagplotlib.plotZED(ZED, datablock, angle, s, SIunits)  # plot the data
    if plot == 0: pmagplotlib.drawFIGS(ZED)
    #
    # print out data for this sample to screen
    #
    recnum = 0
    for plotrec in datablock:
        if units == 'mT':
            print(
                '%i  %7.1f %8.3e %7.1f %7.1f ' %
                (recnum, plotrec[0] * 1e3, plotrec[3], plotrec[1], plotrec[2]))
        if units == 'C':
            print('%i  %7.1f %8.3e %7.1f %7.1f ' %
                  (recnum, plotrec[0] - 273., plotrec[3], plotrec[1],
                   plotrec[2]))
        if units == 'U':
            print('%i  %7.1f %8.3e %7.1f %7.1f ' %
                  (recnum, plotrec[0], plotrec[3], plotrec[1], plotrec[2]))
        recnum += 1
    if plot == 0:
        while 1:
            if beg_pca != "" and end_pca != "" and calculation_type != "":
                pmagplotlib.plotZED(ZED, datablock, angle, s,
                                    SIunits)  # plot the data
                mpars = pmag.domean(
                    datablock, beg_pca, end_pca,
                    calculation_type)  # get best-fit direction/great circle
                pmagplotlib.plotDir(
                    ZED, mpars, datablock,
                    angle)  # plot the best-fit direction/great circle
                print('Specimen, calc_type, N, min, max, MAD, dec, inc')
                if units == 'mT':
                    print('%s %s %i  %6.2f %6.2f %6.1f %7.1f %7.1f' %
                          (s, calculation_type, mpars["specimen_n"],
                           mpars["measurement_step_min"] * 1e3,
                           mpars["measurement_step_max"] * 1e3,
                           mpars["specimen_mad"], mpars["specimen_dec"],
                           mpars["specimen_inc"]))
                if units == 'C':
                    print('%s %s %i  %6.2f %6.2f %6.1f %7.1f %7.1f' %
                          (s, calculation_type, mpars["specimen_n"],
                           mpars["measurement_step_min"] - 273,
                           mpars["measurement_step_max"] - 273,
                           mpars["specimen_mad"], mpars["specimen_dec"],
                           mpars["specimen_inc"]))
                if units == 'U':
                    print(
                        '%s %s %i  %6.2f %6.2f %6.1f %7.1f %7.1f' %
                        (s, calculation_type, mpars["specimen_n"],
                         mpars["measurement_step_min"],
                         mpars["measurement_step_max"], mpars["specimen_mad"],
                         mpars["specimen_dec"], mpars["specimen_inc"]))
            if end_pca == "":
                end_pca = len(
                    datablock
                ) - 1  # initialize end_pca, beg_pca to first and last measurement
            if beg_pca == "": beg_pca = 0
            ans = input(
                " s[a]ve plot, [b]ounds for pca and calculate, change [h]orizontal projection angle, [q]uit:   "
            )
            if ans == 'q':
                sys.exit()
            if ans == 'a':
                files = {}
                for key in list(ZED.keys()):
                    files[key] = s + '_' + key + '.' + fmt
                pmagplotlib.saveP(ZED, files)
            if ans == 'h':
                angle = float(
                    input(" Declination to project onto horizontal axis? "))
                pmagplotlib.plotZED(ZED, datablock, angle, s,
                                    SIunits)  # plot the data

            if ans == 'b':
                GoOn = 0
                while GoOn == 0:  # keep going until reasonable bounds are set
                    print('Enter index of first point for pca: ', '[', beg_pca,
                          ']')
                    answer = input('return to keep default  ')
                    if answer != "": beg_pca = int(answer)
                    print('Enter index  of last point for pca: ', '[', end_pca,
                          ']')
                    answer = input('return to keep default  ')
                    if answer != "":
                        end_pca = int(answer)
                    if beg_pca >= 0 and beg_pca <= len(
                            datablock) - 2 and end_pca > 0 and end_pca < len(
                                datablock):
                        GoOn = 1
                    else:
                        print("Bad entry of indices - try again")
                        end_pca = len(datablock) - 1
                        beg_pca = 0
                GoOn = 0
                while GoOn == 0:
                    ct = input(
                        'Enter Calculation Type: best-fit line,  plane or fisher mean [l]/p/f :  '
                    )
                    if ct == "" or ct == "l":
                        calculation_type = "DE-BFL"
                        GoOn = 1  # all good
                    elif ct == 'p':
                        calculation_type = "DE-BFP"
                        GoOn = 1  # all good
                    elif ct == 'f':
                        calculation_type = "DE-FM"
                        GoOn = 1  # all good
                    else:
                        print("bad entry of calculation type: try again. "
                              )  # keep going
                    pmagplotlib.plotZED(ZED, datablock, angle, s,
                                        SIunits)  # plot the data
                    mpars = pmag.domean(
                        datablock, beg_pca, end_pca, calculation_type
                    )  # get best-fit direction/great circle
                    pmagplotlib.plotDir(
                        ZED, mpars, datablock,
                        angle)  # plot the best-fit direction/great circle
                    print('Specimen, calc_type, N, min, max, MAD, dec, inc')
                    if units == 'mT':
                        print('%s %s %i  %6.2f %6.2f %6.1f %7.1f %7.1f' %
                              (s, calculation_type, mpars["specimen_n"],
                               mpars["measurement_step_min"] * 1e3,
                               mpars["measurement_step_max"] * 1e3,
                               mpars["specimen_mad"], mpars["specimen_dec"],
                               mpars["specimen_inc"]))
                    if units == 'C':
                        print('%s %s %i  %6.2f %6.2f %6.1f %7.1f %7.1f' %
                              (s, calculation_type, mpars["specimen_n"],
                               mpars["measurement_step_min"] - 273,
                               mpars["measurement_step_max"] - 273,
                               mpars["specimen_mad"], mpars["specimen_dec"],
                               mpars["specimen_inc"]))
                    if units == 'U':
                        print('%s %s %i  %6.2f %6.2f %6.1f %7.1f %7.1f' %
                              (s, calculation_type, mpars["specimen_n"],
                               mpars["measurement_step_min"],
                               mpars["measurement_step_max"],
                               mpars["specimen_mad"], mpars["specimen_dec"],
                               mpars["specimen_inc"]))
            pmagplotlib.drawFIGS(ZED)
    else:
        print(beg_pca, end_pca)
        if beg_pca != "" and end_pca != "":
            pmagplotlib.plotZED(ZED, datablock, angle, s,
                                SIunits)  # plot the data
            mpars = pmag.domean(
                datablock, beg_pca, end_pca,
                calculation_type)  # get best-fit direction/great circle
            pmagplotlib.plotDir(
                ZED, mpars, datablock,
                angle)  # plot the best-fit direction/great circle
            print('Specimen, calc_type, N, min, max, MAD, dec, inc')
            if units == 'mT':
                print('%s %s %i  %6.2f %6.2f %6.1f %7.1f %7.1f' %
                      (s, calculation_type, mpars["specimen_n"],
                       mpars["measurement_step_min"] * 1e3,
                       mpars["measurement_step_max"] * 1e3,
                       mpars["specimen_mad"], mpars["specimen_dec"],
                       mpars["specimen_inc"]))
            if units == 'C':
                print('%s %s %i  %6.2f %6.2f %6.1f %7.1f %7.1f' %
                      (s, calculation_type, mpars["specimen_n"],
                       mpars["measurement_step_min"] - 273,
                       mpars["measurement_step_max"] - 273,
                       mpars["specimen_mad"], mpars["specimen_dec"],
                       mpars["specimen_inc"]))
            if units == 'U':
                print('%s %s %i  %6.2f %6.2f %6.1f %7.1f %7.1f' %
                      (s, calculation_type, mpars["specimen_n"],
                       mpars["measurement_step_min"],
                       mpars["measurement_step_max"], mpars["specimen_mad"],
                       mpars["specimen_dec"], mpars["specimen_inc"]))
        files = {}
        for key in list(ZED.keys()):
            files[key] = s + '_' + key + '.' + fmt
        pmagplotlib.saveP(ZED, files)
示例#12
0
def main():
    """
    NAME
        zeq_magic_redo.py
   
    DESCRIPTION
        Calculate principal components through demagnetization data using bounds and calculation type stored in "redo" file
  
    SYNTAX
        zeq_magic_redo.py [command line options]

    OPTIONS
        -h prints help message
        -usr USER:   identify user, default is ""
        -f: specify input file, default is magic_measurements.txt
        -F: specify output file, default is zeq_specimens.txt
        -fre  REDO: specify redo file, default is "zeq_redo"
        -fsa  SAMPFILE: specify er_samples format file, default is "er_samples.txt"
        -A : don't average replicate measurements, default is yes
        -crd [s,g,t] : 
             specify coordinate system [s,g,t]  [default is specimen coordinates]
                 are specimen, geographic, and tilt corrected respectively
             NB: you must have a SAMPFILE in this directory to rotate from specimen coordinates
        -leg:  attaches "Recalculated from original measurements; supercedes published results. " to comment field
    INPUTS
        zeq_redo format file is:
        specimen_name calculation_type[DE-BFL,DE-BFL-A,DE-BFL-O,DE-BFP,DE-FM]  step_min step_max component_name[A,B,C]
    """
    dir_path = '.'
    INCL = ["LT-NO", "LT-AF-Z", "LT-T-Z", "LT-M-Z"]  # looking for demag data
    beg, end, pole, geo, tilt, askave, save = 0, 0, [], 0, 0, 0, 0
    user, doave, comment = "", 1, ""
    geo, tilt = 0, 0
    version_num = pmag.get_version()
    args = sys.argv
    if '-WD' in args:
        ind = args.index('-WD')
        dir_path = args[ind + 1]
    meas_file, pmag_file, mk_file = dir_path + "/" + "magic_measurements.txt", dir_path + "/" + "zeq_specimens.txt", dir_path + "/" + "zeq_redo"
    samp_file, coord = dir_path + "/" + "er_samples.txt", ""
    if "-h" in args:
        print(main.__doc__)
        sys.exit()
    if "-usr" in args:
        ind = args.index("-usr")
        user = sys.argv[ind + 1]
    if "-A" in args: doave = 0
    if "-leg" in args:
        comment = "Recalculated from original measurements; supercedes published results. "
    if "-f" in args:
        ind = args.index("-f")
        meas_file = dir_path + '/' + sys.argv[ind + 1]
    if "-F" in args:
        ind = args.index("-F")
        pmag_file = dir_path + '/' + sys.argv[ind + 1]
    if "-fre" in args:
        ind = args.index("-fre")
        mk_file = dir_path + "/" + args[ind + 1]
    try:
        mk_f = open(mk_file, 'r')
    except:
        print("Bad redo file")
        sys.exit()
    mkspec, skipped = [], []
    speclist = []
    for line in mk_f.readlines():
        tmp = line.split()
        mkspec.append(tmp)
        speclist.append(tmp[0])
    if "-fsa" in args:
        ind = args.index("-fsa")
        samp_file = dir_path + '/' + sys.argv[ind + 1]
    if "-crd" in args:
        ind = args.index("-crd")
        coord = sys.argv[ind + 1]
        if coord == "g": geo, tilt = 1, 0
        if coord == "t": geo, tilt = 1, 1
#
# now get down to bidness
    if geo == 1:
        samp_data, file_type = pmag.magic_read(samp_file)
        if file_type != 'er_samples':
            print(file_type)
            print("This is not a valid er_samples file ")
            sys.exit()
    #
    #
    #

    meas_data, file_type = pmag.magic_read(meas_file)
    if file_type != 'magic_measurements':
        print(file_type)
        print(file_type, "This is not a valid magic_measurements file ")
        sys.exit()
    #
    # sort the specimen names
    #
    k = 0
    print('Processing ', len(speclist), ' specimens - please wait')
    PmagSpecs = []
    while k < len(speclist):
        s = speclist[k]
        recnum = 0
        PmagSpecRec = {}
        method_codes, inst_codes = [], []
        # find the data from the meas_data file for this sample
        #
        #  collect info for the PmagSpecRec dictionary
        #
        meas_meth = []
        spec = pmag.get_dictitem(meas_data, 'er_specimen_name', s, 'T')
        if len(spec) == 0:
            print('no data found for specimen:  ', s)
            print('delete from zeq_redo input file...., then try again')
        else:
            for rec in spec:  # copy of vital stats to PmagSpecRec from first spec record in demag block
                skip = 1
                methods = rec["magic_method_codes"].split(":")
                if len(set(methods) & set(INCL)) > 0:
                    PmagSpecRec["er_analyst_mail_names"] = user
                    PmagSpecRec["magic_software_packages"] = version_num
                    PmagSpecRec["er_specimen_name"] = s
                    PmagSpecRec["er_sample_name"] = rec["er_sample_name"]
                    PmagSpecRec["er_site_name"] = rec["er_site_name"]
                    PmagSpecRec["er_location_name"] = rec["er_location_name"]
                    if "er_expedition_name" in list(rec.keys()):
                        PmagSpecRec["er_expedition_name"] = rec[
                            "er_expedition_name"]
                    PmagSpecRec["er_citation_names"] = "This study"
                    if "magic_experiment_name" not in list(rec.keys()):
                        rec["magic_experiment_name"] = ""
                    PmagSpecRec["magic_experiment_names"] = rec[
                        "magic_experiment_name"]
                    if "magic_instrument_codes" not in list(rec.keys()):
                        rec["magic_instrument_codes"] = ""
                    inst = rec['magic_instrument_codes'].split(":")
                    for I in inst:
                        if I not in inst_codes:  # copy over instruments
                            inst_codes.append(I)
                    meths = rec["magic_method_codes"].split(":")
                    for meth in meths:
                        if meth.strip() not in meas_meth:
                            meas_meth.append(meth)
                    if "LP-DIR-AF" in meas_meth or "LT-AF-Z" in meas_meth:
                        PmagSpecRec["measurement_step_unit"] = "T"
                        if "LP-DIR-AF" not in method_codes:
                            method_codes.append("LP-DIR-AF")
                    if "LP-DIR-T" in meas_meth or "LT-T-Z" in meas_meth:
                        PmagSpecRec["measurement_step_unit"] = "K"
                        if "LP-DIR-T" not in method_codes:
                            method_codes.append("LP-DIR-T")
                    if "LP-DIR-M" in meas_meth or "LT-M-Z" in meas_meth:
                        PmagSpecRec["measurement_step_unit"] = "J"
                        if "LP-DIR-M" not in method_codes:
                            method_codes.append("LP-DIR-M")
    #
    #
        datablock, units = pmag.find_dmag_rec(
            s, spec)  # fish out the demag data for this specimen
        #
        if len(datablock) < 2 or s not in speclist:
            k += 1


#            print 'skipping ', s,len(datablock)
        else:
            #
            # find replicate measurements at given treatment step and average them
            #
            #            step_meth,avedata=pmag.vspec(data)
            #
            #            if len(avedata) != len(datablock):
            #                if doave==1:
            #                    method_codes.append("DE-VM")
            #                    datablock=avedata
            #
            # do geo or stratigraphic correction now
            #
            if geo == 1 or tilt == 1:
                # find top priority orientation method
                orient, az_type = pmag.get_orient(
                    samp_data, PmagSpecRec["er_sample_name"])
                if az_type not in method_codes: method_codes.append(az_type)
                #
                #  if tilt selected,  get stratigraphic correction
                #
                tiltblock, geoblock = [], []
                for rec in datablock:
                    if "sample_azimuth" in list(
                            orient.keys()) and orient["sample_azimuth"] != "":
                        d_geo, i_geo = pmag.dogeo(
                            rec[1], rec[2], float(orient["sample_azimuth"]),
                            float(orient["sample_dip"]))
                        geoblock.append(
                            [rec[0], d_geo, i_geo, rec[3], rec[4], rec[5]])
                        if tilt == 1 and "sample_bed_dip_direction" in list(
                                orient.keys()):
                            d_tilt, i_tilt = pmag.dotilt(
                                d_geo, i_geo,
                                float(orient["sample_bed_dip_direction"]),
                                float(orient["sample_bed_dip"]))
                            tiltblock.append([
                                rec[0], d_tilt, i_tilt, rec[3], rec[4], rec[5]
                            ])
                        elif tilt == 1:
                            if PmagSpecRec["er_sample_name"] not in skipped:
                                print('no tilt correction for ',
                                      PmagSpecRec["er_sample_name"],
                                      ' skipping....')
                                skipped.append(PmagSpecRec["er_sample_name"])
                    else:
                        if PmagSpecRec["er_sample_name"] not in skipped:
                            print('no geographic correction for ',
                                  PmagSpecRec["er_sample_name"],
                                  ' skipping....')
                            skipped.append(PmagSpecRec["er_sample_name"])
    #
    #	get beg_pca, end_pca, pca
            if PmagSpecRec['er_sample_name'] not in skipped:
                compnum = -1
                for spec in mkspec:
                    if spec[0] == s:
                        CompRec = {}
                        for key in list(PmagSpecRec.keys()):
                            CompRec[key] = PmagSpecRec[key]
                        compnum += 1
                        calculation_type = spec[1]
                        beg = float(spec[2])
                        end = float(spec[3])
                        if len(spec) > 4:
                            comp_name = spec[4]
                        else:
                            comp_name = string.uppercase[compnum]
                        CompRec['specimen_comp_name'] = comp_name
                        if beg < float(datablock[0][0]):
                            beg = float(datablock[0][0])
                        if end > float(datablock[-1][0]):
                            end = float(datablock[-1][0])
                        for l in range(len(datablock)):
                            if datablock[l][0] == beg: beg_pca = l
                            if datablock[l][0] == end: end_pca = l
                        if geo == 1 and tilt == 0:
                            mpars = pmag.domean(geoblock, beg_pca, end_pca,
                                                calculation_type)
                            if mpars["specimen_direction_type"] != "Error":
                                CompRec["specimen_dec"] = '%7.1f ' % (
                                    mpars["specimen_dec"])
                                CompRec["specimen_inc"] = '%7.1f ' % (
                                    mpars["specimen_inc"])
                                CompRec["specimen_tilt_correction"] = '0'
                        if geo == 1 and tilt == 1:
                            mpars = pmag.domean(tiltblock, beg_pca, end_pca,
                                                calculation_type)
                            if mpars["specimen_direction_type"] != "Error":
                                CompRec["specimen_dec"] = '%7.1f ' % (
                                    mpars["specimen_dec"])
                                CompRec["specimen_inc"] = '%7.1f ' % (
                                    mpars["specimen_inc"])
                                CompRec["specimen_tilt_correction"] = '100'
                        if geo == 0 and tilt == 0:
                            mpars = pmag.domean(datablock, beg_pca, end_pca,
                                                calculation_type)
                            if mpars["specimen_direction_type"] != "Error":
                                CompRec["specimen_dec"] = '%7.1f ' % (
                                    mpars["specimen_dec"])
                                CompRec["specimen_inc"] = '%7.1f ' % (
                                    mpars["specimen_inc"])
                                CompRec["specimen_tilt_correction"] = '-1'
                        if mpars["specimen_direction_type"] == "Error":
                            pass
                        else:
                            CompRec["measurement_step_min"] = '%8.3e ' % (
                                datablock[beg_pca][0])
                            try:
                                CompRec["measurement_step_max"] = '%8.3e ' % (
                                    datablock[end_pca][0])
                            except:
                                print('error in end_pca ',
                                      PmagSpecRec['er_specimen_name'])
                            CompRec["specimen_correction"] = 'u'
                            if calculation_type != 'DE-FM':
                                CompRec["specimen_mad"] = '%7.1f ' % (
                                    mpars["specimen_mad"])
                                CompRec["specimen_alpha95"] = ""
                            else:
                                CompRec["specimen_mad"] = ""
                                CompRec["specimen_alpha95"] = '%7.1f ' % (
                                    mpars["specimen_alpha95"])
                            CompRec["specimen_n"] = '%i ' % (
                                mpars["specimen_n"])
                            CompRec["specimen_dang"] = '%7.1f ' % (
                                mpars["specimen_dang"])
                            CompMeths = []
                            for meth in method_codes:
                                if meth not in CompMeths:
                                    CompMeths.append(meth)
                            if calculation_type not in CompMeths:
                                CompMeths.append(calculation_type)
                            if geo == 1: CompMeths.append("DA-DIR-GEO")
                            if tilt == 1: CompMeths.append("DA-DIR-TILT")
                            if "DE-BFP" not in calculation_type:
                                CompRec["specimen_direction_type"] = 'l'
                            else:
                                CompRec["specimen_direction_type"] = 'p'
                            CompRec["magic_method_codes"] = ""
                            if len(CompMeths) != 0:
                                methstring = ""
                                for meth in CompMeths:
                                    methstring = methstring + ":" + meth
                                CompRec[
                                    "magic_method_codes"] = methstring.strip(
                                        ':')
                            CompRec["specimen_description"] = comment
                            if len(inst_codes) != 0:
                                inststring = ""
                                for inst in inst_codes:
                                    inststring = inststring + ":" + inst
                                CompRec[
                                    "magic_instrument_codes"] = inststring.strip(
                                        ':')
                            PmagSpecs.append(CompRec)
            k += 1
    pmag.magic_write(pmag_file, PmagSpecs, 'pmag_specimens')
    print("Recalculated specimen data stored in ", pmag_file)
示例#13
0
文件: pca.py 项目: jbowles100/PmagPy
def main():
    """
    NAME
       pca.py

    DESCRIPTION
       calculates best-fit line/plane through demagnetization data

    INPUT FORMAT
       takes specimen_name treatment intensity declination inclination  in space delimited file

    SYNTAX
       pca.py [command line options][< filename]

    OPTIONS
        -h prints help and quits
        -f FILE
        -dir [L,P,F][BEG][END] specify direction type, beginning and end
          (L:line, P:plane or F:fisher mean of unit vectors)
          BEG: first step (NRM = step zero)
          END: last step (NRM = step zero)
        < filename for reading from standard input
    OUTPUT:
        specimen_name calculation_type N beg end MAD dec inc
        if calculation_type is 'p', dec and inc are pole to plane, otherwise, best-fit direction

    EXAMPLE:
        pca.py -dir L  1 5 <ex3.3
        will calculate best-fit line through demagnetization steps 1 and 5 from file ex5.1

    """
    if '-h' in sys.argv:  # check if help is needed
        print main.__doc__
        sys.exit()  # graceful quit
    if '-f' in sys.argv:
        ind = sys.argv.index('-f')
        file = sys.argv[ind + 1]
        f = open(file, 'rU')
        data = f.readlines()
    else:
        data = sys.stdin.readlines()  # read in data from standard input
    if '-dir' in sys.argv:  #
        ind = sys.argv.index('-dir')
        typ = sys.argv[ind + 1]
        if typ == 'L': calculation_type = 'DE-BFL'
        if typ == 'P': calculation_type = 'DE-BFP'
        if typ == 'F': calculation_type = 'DE-FM'
        beg_pca = int(sys.argv[ind + 2])
        end_pca = int(sys.argv[ind + 3])


#
#
    datablock = []  # set up list for data
    s = ""
    ind = 0
    for line in data:  # read in the data from standard input
        rec = line.split()  # split each line on space to get records
        if s == "":
            s = rec[0]
            print s, calculation_type
        print ind, rec[1], rec[3], rec[4], rec[2]
        ind += 1
        datablock.append(
            [float(rec[1]),
             float(rec[3]),
             float(rec[4]),
             float(rec[2]), '0'])  # treatment,dec,inc,int,dummy
    mpars = pmag.domean(datablock, beg_pca, end_pca, calculation_type)
    if calculation_type == "DE-FM":
        print '%s %s %i  %6.2f %6.2f %6.1f %7.1f %7.1f' % (
            s, calculation_type, mpars["specimen_n"],
            mpars["measurement_step_min"], mpars["measurement_step_max"],
            mpars["specimen_a95"], mpars["specimen_dec"],
            mpars["specimen_inc"])
    else:
        print '%s %s %i  %6.2f %6.2f %6.1f %7.1f %7.1f' % (
            s, calculation_type, mpars["specimen_n"],
            mpars["measurement_step_min"], mpars["measurement_step_max"],
            mpars["specimen_mad"], mpars["specimen_dec"],
            mpars["specimen_inc"])
示例#14
0
def main():
    """
    NAME
        thellier_magic.py

    DESCRIPTION
        plots Thellier-Thellier data in version 3.0 format
        Reads saved interpretations from a specimen formatted table, default: specimens.txt

    SYNTAX
        thellier_magic.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f MEAS, set measurements input file, default is 'measurements.txt'
        -fsp PRIOR, set specimens.txt prior interpretations file, default is 'specimens.txt'
        -fcr CRIT, set criteria file for grading.  # not yet implemented
        -fmt [svg,png,jpg], format for images - default is svg
        -sav,  saves plots with out review (in format specified by -fmt key or default)
        -spc SPEC, plots single specimen SPEC, saves plot with specified format
            with optional -b bounds and quits
        -b BEG END: sets  bounds for calculation
           BEG: starting step number for slope calculation
           END: ending step number for slope calculation
        -z use only z component difference for pTRM calculation

    OUTPUT
        figures:
            ALL:  numbers refer to temperature steps in command line window
            1) Arai plot:  closed circles are zero-field first/infield
                           open circles are infield first/zero-field
                           triangles are pTRM checks
                           squares are pTRM tail checks
                           VDS is vector difference sum
                           diamonds are bounds for interpretation
            2) Zijderveld plot:  closed (open) symbols are X-Y (X-Z) planes
                                 X rotated to NRM direction
            3) (De/Re)Magnetization diagram:
                           circles are NRM remaining
                           squares are pTRM gained
            4) equal area projections:
               green triangles are pTRM gained direction
                           red (purple) circles are lower(upper) hemisphere of ZI step directions
                           blue (cyan) squares are lower(upper) hemisphere IZ step directions
            5) Optional:  TRM acquisition
            6) Optional: TDS normalization
        command line window:
            list is: temperature step numbers, temperatures (C), Dec, Inc, Int (units of measuements)
                     list of possible commands: type letter followed by return to select option
                     saving of plots creates image files with specimen, plot type as name
    """
#
#   initializations
#
    version_num=pmag.get_version()
    verbose=pmagplotlib.verbose
#
# default acceptance criteria
#
    accept=pmag.default_criteria(0)[0] # set the default criteria
#
# parse command line options
#
    plots,fmt,Zdiff=0,'svg',0
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    dir_path = pmag.get_named_arg_from_sys("-WD", default_val=os.getcwd())
    meas_file = pmag.get_named_arg_from_sys("-f", default_val="measurements.txt")
    spec_file=pmag.get_named_arg_from_sys("-fsp", default_val="specimens.txt")
    crit_file=pmag.get_named_arg_from_sys("-fcr", default_val="criteria.txt")
    spec_file=os.path.join(dir_path,spec_file)
    meas_file=os.path.join(dir_path,meas_file)
    crit_file=os.path.join(dir_path,crit_file)
    fmt = pmag.get_named_arg_from_sys("-fmt", "svg")
    if '-sav' in sys.argv: plots,verbose=1,0
    if '-z' in sys.argv: Zdiff=1
    specimen=pmag.get_named_arg_from_sys("-spc",default_val="")
    if '-b' in sys.argv:
        ind=sys.argv.index('-b')
        start=int(sys.argv[ind+1])
        end=int(sys.argv[ind+2])
    else:
        start,end="",""
    fnames = {'measurements': meas_file, 'specimens': spec_file, 'criteria': crit_file}
    contribution = nb.Contribution(dir_path, custom_filenames=fnames, read_tables=['measurements', 'specimens', 'criteria'])
#
#   import  prior interpretations  from specimen file
#
    specimen_cols = ['analysts', 'aniso_ftest', 'aniso_ftest12', 'aniso_ftest23', 'aniso_s', 'aniso_s_mean', 'aniso_s_n_measurements', 'aniso_s_sigma', 'aniso_s_unit', 'aniso_tilt_correction', 'aniso_type', 'aniso_v1', 'aniso_v2', 'aniso_v3', 'citations', 'description', 'dir_alpha95', 'dir_comp', 'dir_dec', 'dir_inc', 'dir_mad_free', 'dir_n_measurements', 'dir_tilt_correction', 'experiments', 'geologic_classes', 'geologic_types', 'hyst_bc', 'hyst_bcr', 'hyst_mr_moment', 'hyst_ms_moment', 'int_abs', 'int_b', 'int_b_beta', 'int_b_sigma', 'int_corr', 'int_dang', 'int_drats', 'int_f', 'int_fvds', 'int_gamma', 'int_mad_free', 'int_md', 'int_n_measurements', 'int_n_ptrm', 'int_q', 'int_rsc', 'int_treat_dc_field', 'lithologies', 'meas_step_max', 'meas_step_min', 'meas_step_unit', 'method_codes', 'sample', 'software_packages', 'specimen']
    if 'specimens' in contribution.tables:
        spec_container = contribution.tables['specimens']
        prior_spec_data=spec_container.get_records_for_code('LP-PI-TRM',strict_match=False) # look up all prior intensity interpretations
    else:
           spec_container, prior_spec_data = None, []
    backup=0
    #
    Mkeys = ['magn_moment', 'magn_volume', 'magn_mass']
#
#   create measurement dataframe
#
    meas_container = contribution.tables['measurements']
    meas_data = meas_container.df
#
    meas_data['method_codes']=meas_data['method_codes'].str.replace(" ","") # get rid of nasty spaces
    meas_data= meas_data[meas_data['method_codes'].str.contains('LP-PI-TRM|LP-TRM|LP-TRM-TD')==True] # fish out zero field steps for plotting
    intensity_types = [col_name for col_name in meas_data.columns if col_name in Mkeys]
    int_key = intensity_types[0] # plot first intensity method found - normalized to initial value anyway - doesn't matter which used
    meas_data = meas_data[meas_data[int_key].notnull()] # get all the non-null intensity records of the same type
    if 'flag' not in meas_data.columns: meas_data['flag'] = 'g' # set the default flag to good
    meas_data = meas_data[meas_data['flag'].str.contains('g')==True] # only the 'good' measurements
    thel_data = meas_data[meas_data['method_codes'].str.contains('LP-PI-TRM')==True] # get all the Thellier data
    trm_data = meas_data[meas_data['method_codes'].str.contains('LP-TRM')==True] # get all the TRM acquisition data
    td_data = meas_data[meas_data['method_codes'].str.contains('LP-TRM-TD')==True] # get all the TD data
    anis_data = meas_data[meas_data['method_codes'].str.contains('LP-AN')==True] # get all the anisotropy data
#
# get list of unique specimen names from measurement data
#
    specimen_names= meas_data.specimen.unique() # this is a Series of all the specimen names
    specimen_names= specimen_names.tolist() # turns it into a list
    specimen_names.sort() # sorts by specimen name
#
# set up new DataFrame for this sessions specimen interpretations
#
    spec_container = nb.MagicDataFrame(dtype='specimens', columns=specimen_cols)
    current_spec_data = spec_container.df # this is for interpretations from this session
    if specimen=="": # do all specimens
        k = 0
    else:
        k=specimen_names.index(specimen) # just do this one
    # define figure numbers for arai, zijderveld and
    #   de-,re-magnetization diagrams
    AZD={}
    AZD['deremag'], AZD['zijd'],AZD['arai'],AZD['eqarea']=1,2,3,4
    pmagplotlib.plot_init(AZD['arai'],5,5)
    pmagplotlib.plot_init(AZD['zijd'],5,5)
    pmagplotlib.plot_init(AZD['deremag'],5,5)
    pmagplotlib.plot_init(AZD['eqarea'],5,5)
    if len(trm_data)>0:
        AZD['TRM']=5
        pmagplotlib.plot_init(AZD['TRM'],5,5)
    if len(td_data)>0:
        AZD['TDS']=6
        pmagplotlib.plot_init(AZD['TDS'],5,5)
    #
    while k < len(specimen_names):
        this_specimen=specimen_names[k] # set the current specimen for plotting
        if verbose and  this_specimen!="":print(this_specimen, k+1 , 'out of ',len(specimen_names))
#
#    set up datablocks
#
        thelblock= thel_data[thel_data['specimen'].str.contains(this_specimen)==True] # fish out this specimen
        trmblock= trm_data[trm_data['specimen'].str.contains(this_specimen)==True] # fish out this specimen
        tdsrecs= td_data[td_data['specimen'].str.contains(this_specimen)==True] # fish out this specimen
        anisblock= anis_data[anis_data['specimen'].str.contains(this_specimen)==True] # fish out the anisotropy data
        prior_specimen_interpretations= prior_spec_data[prior_spec_data['specimen'].str.contains(this_specimen)==True] # fish out prior interpretation
#
# sort data into types
#
        araiblock,field=pmag.sortarai(thelblock,this_specimen,Zdiff,version=3)
        first_Z=araiblock[0]
        GammaChecks=araiblock[5]
        if len(first_Z)<3:
           if backup==0:
                   k+=1
                   if verbose:
                       print('skipping specimen - moving forward ', this_specimen)
           else:
                   k-=1
                   if verbose:
                       print('skipping specimen - moving backward ', this_specimen)
        else:
               backup=0
               zijdblock,units=pmag.find_dmag_rec(this_specimen,thelblock,version=3)
               if start=="" and len(prior_specimen_interpretations)>0:
                   if verbose: print('Looking up saved interpretation....')
#
# get prior interpretation steps
#
                   beg_int=pd.to_numeric(prior_specimen_interpretations.meas_step_min.values).tolist()[0]
                   end_int=pd.to_numeric(prior_specimen_interpretations.meas_step_max.values).tolist()[0]
               else: beg_int,end_int="",""
               recnum=0
               if verbose: print("index step Dec   Inc  Int       Gamma")
               for plotrec in zijdblock:
                   if plotrec[0]==beg_int:start=recnum # while we are at it, collect these bounds
                   if plotrec[0]==end_int:end=recnum
                   if verbose:
                       if GammaChecks!="":
                           gamma=""
                           for g in GammaChecks:
                               if g[0]==plotrec[0]-273:
                                   gamma=g[1]
                                   break
                       if gamma!="":
                           print('%i     %i %7.1f %7.1f %8.3e %7.1f' % (recnum,plotrec[0]-273,plotrec[1],plotrec[2],plotrec[3],gamma))
                       else:
                           print('%i     %i %7.1f %7.1f %8.3e ' % (recnum,plotrec[0]-273,plotrec[1],plotrec[2],plotrec[3]))
                   recnum += 1
               for fig in list(AZD.keys()):pmagplotlib.clearFIG(AZD[fig]) # clear all figures
               pmagplotlib.plotAZ(AZD,araiblock,zijdblock,this_specimen,units[0])
               if verbose:pmagplotlib.drawFIGS(AZD)
               pars,errcode=pmag.PintPars(thelblock,araiblock,zijdblock,start,end,accept,version=3)
               pars['measurement_step_unit']="K"
               pars['experiment_type']='LP-PI-TRM'
#
# work on saving interpretations stuff later
#
               if errcode!=1: # no problem in PintPars
                    pars["specimen_lab_field_dc"]=field
                    pars["specimen_int"]=-1*field*pars["specimen_b"]
                    pars["er_specimen_name"]=this_specimen
                    #pars,kill=pmag.scoreit(pars,this_specimen_interpretation,accept,'',verbose) # deal with this later
                    pars["specimen_grade"]='None'
                    pars['measurement_step_min']=pars['meas_step_min']
                    pars['measurement_step_max']=pars['meas_step_max']
                    if pars['measurement_step_unit']=='K':
                        outstr= "specimen     Tmin  Tmax  N  lab_field  B_anc  b  q  f(coe)  Fvds  beta  MAD  Dang  Drats  Nptrm  Grade  R  MD%  sigma  Gamma_max \n"
                        pars_out= (this_specimen,(pars["meas_step_min"]-273),(pars["meas_step_max"]-273),(pars["specimen_int_n"]),1e6*(pars["specimen_lab_field_dc"]),1e6*(pars["specimen_int"]),pars["specimen_b"],pars["specimen_q"],pars["specimen_f"],pars["specimen_fvds"],pars["specimen_b_beta"],pars["int_mad_free"],pars["int_dang"],pars["int_drats"],pars["int_n_ptrm"],pars["specimen_grade"],np.sqrt(pars["specimen_rsc"]),int(pars["int_md"]), pars["specimen_b_sigma"],pars['specimen_gamma'])
                        outstring= '%s %4.0f %4.0f %i %4.1f %4.1f %5.3f %5.1f %5.3f %5.3f %5.3f  %7.1f %7.1f %7.1f %s %s %6.3f %i %5.3f %7.1f' % pars_out +'\n'
                    elif pars['measurement_step_unit']=='J':
                        outstr= "specimen     Wmin  Wmax  N  lab_field  B_anc  b  q  f(coe)  Fvds  beta  MAD  Dang  Drats  Nptrm  Grade  R  MD%  sigma  ThetaMax DeltaMax GammaMax\n"
                        pars_out= (this_specimen,(pars["meas_step_min"]),(pars["meas_step_max"]),(pars["specimen_int_n"]),1e6*(pars["specimen_lab_field_dc"]),1e6*(pars["specimen_int"]),pars["specimen_b"],pars["specimen_q"],pars["specimen_f"],pars["specimen_fvds"],pars["specimen_b_beta"],pars["specimen_int_mad"],pars["specimen_int_dang"],pars["specimen_drats"],pars["specimen_int_ptrm_n"],pars["specimen_grade"],np.sqrt(pars["specimen_rsc"]),int(pars["specimen_md"]), pars["specimen_b_sigma"],pars["specimen_theta"],pars["specimen_delta"],pars["specimen_gamma"])
                        outstring= '%s %4.0f %4.0f %i %4.1f %4.1f %5.3f %5.1f %5.3f %5.3f %5.3f  %7.1f %7.1f %7.1f %s %s %6.3f %i %5.3f %7.1f %7.1f %7.1f' % pars_out +'\n'
                    print(outstr)
                    print(outstring)
                    pmagplotlib.plotB(AZD,araiblock,zijdblock,pars)
                    mpars=pmag.domean(araiblock[1],start,end,'DE-BFL')
                    if verbose:
                        pmagplotlib.drawFIGS(AZD)
                        print('pTRM direction= ','%7.1f'%(mpars['specimen_dec']),' %7.1f'%(mpars['specimen_inc']),' MAD:','%7.1f'%(mpars['specimen_mad']))
               if len(anisblock)>0:  # this specimen has anisotropy data
                           if verbose: print('Found anisotropy record... but ignoring for now ')
               if plots==1:
                   if fmt != "pmag":
                       files={}
                       for key in list(AZD.keys()):
                           files[key]='SP:_'+this_specimen+'_TY:_'+key+'_'+'.'+fmt
                       if pmagplotlib.isServer:
                           black     = '#000000'
                           purple    = '#800080'
                           titles={}
                           titles['deremag']='DeReMag Plot'
                           titles['zijd']='Zijderveld Plot'
                           titles['arai']='Arai Plot'
                           titles['TRM']='TRM Acquisition data'
                           AZD = pmagplotlib.addBorders(AZD,titles,black,purple)
                       pmagplotlib.saveP(AZD,files)
                   else:  # save in pmag format
                       print('pmag format no longer supported')
                       #script="grep "+this_specimen+" output.mag | thellier -mfsi"
                       #script=script+' %8.4e'%(field)
                       #min='%i'%((pars["measurement_step_min"]-273))
                       #Max='%i'%((pars["measurement_step_max"]-273))
                       #script=script+" "+min+" "+Max
                       #script=script+" |plotxy;cat mypost >>thellier.ps\n"
                       #pltf.write(script)
                       #pmag.domagicmag(outf,MeasRecs)
               if specimen!="": sys.exit() # syonara
               if verbose:
                   ans=input('Return for next specimen, q to quit:  ')
                   if ans=='q':sys.exit()
               k+=1 # moving on
示例#15
0
文件: pca.py 项目: CrabGit334/PmagPy
def main():
    """
    NAME
       pca.py

    DESCRIPTION
       calculates best-fit line/plane through demagnetization data

    INPUT FORMAT
       takes specimen_name treatment intensity declination inclination  in space delimited file

    SYNTAX
       pca.py [command line options][< filename]

    OPTIONS
        -h prints help and quits
        -f FILE
        -dir [L,P,F][BEG][END] specify direction type, beginning and end
          (L:line, P:plane or F:fisher mean of unit vectors)
          BEG: first step (NRM = step zero)
          END: last step (NRM = step zero)
        < filename for reading from standard input
    OUTPUT:
        specimen_name calculation_type N beg end MAD dec inc
        if calculation_type is 'p', dec and inc are pole to plane, otherwise, best-fit direction

    EXAMPLE:
        pca.py -dir L  1 5 <ex3.3
        will calculate best-fit line through demagnetization steps 1 and 5 from file ex5.1

    """
    if '-h' in sys.argv: # check if help is needed
        print(main.__doc__)
        sys.exit() # graceful quit
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        file=sys.argv[ind+1]
        f=open(file,'r')
        data=f.readlines()
    else:
        data=sys.stdin.readlines() # read in data from standard input
    if '-dir' in sys.argv: # 
        ind=sys.argv.index('-dir')
        typ=sys.argv[ind+1]
        if typ=='L': calculation_type='DE-BFL'
        if typ=='P': calculation_type='DE-BFP'
        if typ=='F': calculation_type='DE-FM'
        beg_pca = int(sys.argv[ind+2])
        end_pca = int(sys.argv[ind+3])
#
#
    datablock= [] # set up list for data
    s=""
    ind=0
    for line in data:   # read in the data from standard input
        rec=line.split() # split each line on space to get records
        if s=="":
            s=rec[0]
            print(s, calculation_type)
        print(ind,rec[1],rec[3],rec[4],rec[2])
        ind+=1
        datablock.append([float(rec[1]),float(rec[3]),float(rec[4]),float(rec[2]),'0']) # treatment,dec,inc,int,dummy
    mpars=pmag.domean(datablock,beg_pca,end_pca,calculation_type)
    if calculation_type=="DE-FM":
        print('%s %s %i  %6.2f %6.2f %6.1f %7.1f %7.1f' % (s,calculation_type,mpars["specimen_n"],mpars["measurement_step_min"],mpars["measurement_step_max"],mpars["specimen_a95"],mpars["specimen_dec"],mpars["specimen_inc"]))
    else:
        print('%s %s %i  %6.2f %6.2f %6.1f %7.1f %7.1f' % (s,calculation_type,mpars["specimen_n"],mpars["measurement_step_min"],mpars["measurement_step_max"],mpars["specimen_mad"],mpars["specimen_dec"],mpars["specimen_inc"]))