Ejemplo n.º 1
0
 def test_get_min_max_lat_lon(self):
     site_container = cb.MagicDataFrame(dtype='sites')
     site_container.add_row('site1', {
         'lat': 10,
         'lon': 4,
         'location': 'location1'
     })
     site_container.add_row('site2', {
         'lat': 10.2,
         'lon': 5,
         'location': 'location1'
     })
     site_container.add_row('site3', {
         'lat': 20,
         'lon': '15',
         'location': 'location2'
     })
     site_container.add_row('site4', {'lat': None, 'location': 'location1'})
     loc_container = cb.MagicDataFrame(
         dtype='locations',
         columns=['lat_n', 'lat_s', 'lon_e', 'lon_w', 'location'])
     site_container.df
     loc_container.add_row('location1', {})
     loc_container.add_row('location2', {})
     con = cb.Contribution(".", read_tables=['images'])
     con.tables['sites'] = site_container
     con.tables['locations'] = loc_container
     con.get_min_max_lat_lon()
     self.assertEqual(10., con.tables['locations'].df.loc['location1',
                                                          'lat_s'])
     self.assertEqual(15., con.tables['locations'].df.loc['location2',
                                                          'lon_e'])
     os.remove(os.path.join(".", "locations.txt"))
Ejemplo n.º 2
0
 def test_delete_row(self):
     magic_df = cb.MagicDataFrame(os.path.join(PROJECT_WD, 'sites.txt'),
                                  dmodel=DMODEL)
     old_len = len(magic_df.df)
     magic_df.delete_row(5)
     self.assertEqual(old_len - 1, len(magic_df.df))
     self.assertEqual('3', magic_df.df.iloc[5].name)
Ejemplo n.º 3
0
 def test_all_to_str(self):
     directory = os.path.join(WD, 'data_files', '3_0', 'McMurdo')
     fname = os.path.join(directory, "sites.txt")
     sites = cb.MagicDataFrame(fname)
     sites.df.loc['mc01', 'age'] = 1.18003000000000001
     sites.all_to_str()
     self.assertEqual('1.18003', sites.df.loc['mc01', 'age'].values[0])
Ejemplo n.º 4
0
 def test_add_row(self):
     magic_df = cb.MagicDataFrame(os.path.join(PROJECT_WD, 'sites.txt'),
                                  dmodel=DMODEL)
     old_len = len(magic_df.df)
     magic_df.add_row('new_site', {'new_col': 'new_val'})
     self.assertEqual('new_val', magic_df.df.iloc[-1]['new_col'])
     self.assertEqual(old_len + 1, len(magic_df.df))
Ejemplo n.º 5
0
 def test_add_blank_row(self):
     magic_df = cb.MagicDataFrame(os.path.join(PROJECT_WD, 'sites.txt'),
                                  dmodel=DMODEL)
     old_len = len(magic_df.df)
     magic_df.add_blank_row('blank_site')
     self.assertIn('blank_site', magic_df.df.index)
     self.assertEqual(old_len + 1, len(magic_df.df))
Ejemplo n.º 6
0
    def test_get_thellier_gui_meas_mapping(self):
        # MagIC 3 --> 2 with 'treat_step_num'
        meas_data3_0 = cb.MagicDataFrame(self.magic_file).df
        meas_data3_0.iloc[0, meas_data3_0.columns.get_loc('measurement')] = 'custom'
        meas_data2_5 = map_magic.convert_meas_df_thellier_gui(meas_data3_0, output=2)
        self.assertEqual('custom',
                         meas_data2_5.iloc[0, meas_data2_5.columns.get_loc('measurement')])
        self.assertEqual(1,
                         meas_data2_5.iloc[0, meas_data2_5.columns.get_loc('measurement_number')])

        # and back
        meas_data3_again = map_magic.convert_meas_df_thellier_gui(meas_data2_5, output=3)
        self.assertEqual('custom',
                         meas_data3_again.iloc[0, meas_data3_again.columns.get_loc('measurement')])
        self.assertEqual(1,
                         meas_data3_again.iloc[0, meas_data3_again.columns.get_loc('treat_step_num')])

        # MagIC 3 --> 2 without 'treat_step_num'
        del meas_data3_0['treat_step_num']
        self.assertEqual('custom',
                         meas_data3_0.iloc[0, meas_data3_0.columns.get_loc('measurement')])
        meas_data2_5 = map_magic.convert_meas_df_thellier_gui(meas_data3_0, output=2)
        self.assertIn('measurement_number', meas_data2_5.columns)
        self.assertEqual('custom',
                 meas_data2_5.iloc[0, meas_data2_5.columns.get_loc('measurement')])
        self.assertEqual('custom',
                         meas_data2_5.iloc[0, meas_data2_5.columns.get_loc('measurement_number')])

        # and back to 3
        meas_data3_0_again = map_magic.convert_meas_df_thellier_gui(meas_data2_5, output=3)
        self.assertEqual('custom',
                         meas_data3_0_again.iloc[0, meas_data2_5.columns.get_loc('measurement')])
        self.assertNotIn('treat_step_num', meas_data3_0_again.columns)
Ejemplo n.º 7
0
 def test_meas_dataframe(self):
     meas_file = os.path.join(WD, "data_files", "3_0", "McMurdo",
                              "measurements.txt")
     df = pd.read_table(meas_file, skiprows=[0])
     self.assertNotIn('sequence', df.columns)
     magic_df = cb.MagicDataFrame(meas_file)
     self.assertIn('sequence', magic_df.df.columns)
Ejemplo n.º 8
0
 def test_get_first_non_null_value(self):
     magic_df = cb.MagicDataFrame(os.path.join(PROJECT_WD, 'sites.txt'),
                                  dmodel=DMODEL)
     res = magic_df.get_first_non_null_value('1', 'bed_dip_direction')
     self.assertEqual(135, res)
     magic_df.df.loc['1', 'bed_dip_direction'] = None
     res = magic_df.get_first_non_null_value('1', 'bed_dip_direction')
     self.assertTrue(pd.isnull(res))
Ejemplo n.º 9
0
 def test_delete_rows(self):
     magic_df = cb.MagicDataFrame(os.path.join(PROJECT_WD, 'sites.txt'),
                                  dmodel=DMODEL)
     cond = magic_df.df['description'].str.contains('VGP').astype(bool)
     # delete all rows that aren't described as VGPs
     magic_df.delete_rows(-cond)
     for descr in magic_df.df['description'].values:
         self.assertTrue('VGP' in descr)
Ejemplo n.º 10
0
 def test_update_record(self):
     magic_df = cb.MagicDataFrame(os.path.join(PROJECT_WD, 'sites.txt'),
                                  dmodel=DMODEL)
     cond = magic_df.df['lithologies'] == 'Basalt'
     magic_df.update_record('2',
                            new_data={'description': 'updated'},
                            condition=cond)
     self.assertIn('updated', magic_df.df.loc['2', 'description'].values)
Ejemplo n.º 11
0
 def test_sites_only_propagation(self):
     """
     Make sure propagation works correclty with limited tables provided
     """
     directory = os.path.join(WD, 'data_files', '3_0', 'McMurdo')
     con = cb.Contribution(directory,
                           dmodel=DMODEL,
                           read_tables=['sites'],
                           custom_filenames={
                               'locations': '_locations.txt',
                               'samples': '_samples.txt'
                           })
     self.assertEqual(['sites'], list(con.tables.keys()))
     con.propagate_all_tables_info()
     self.assertEqual(sorted(['samples', 'sites', 'locations']),
                      sorted(con.tables.keys()))
     for fname in ['_locations.txt', '_samples.txt']:
         os.remove(os.path.join(directory, fname))
     #
     con = cb.Contribution(directory,
                           dmodel=DMODEL,
                           read_tables=['sites'],
                           custom_filenames={
                               'locations': '_locations.txt',
                               'samples': '_samples.txt'
                           })
     samp_df = pd.DataFrame(index=['mc01b'],
                            columns=['sample', 'site'],
                            data=[['mc01b', 'fake site']])
     samp_df = cb.MagicDataFrame(dtype='samples', df=samp_df)
     con.tables['samples'] = samp_df
     self.assertEqual('fake site', con.tables['samples'].df.loc['mc01b',
                                                                'site'])
     con.propagate_all_tables_info()
     self.assertEqual(sorted(['samples', 'sites', 'locations']),
                      sorted(con.tables.keys()))
     # mc01b does not update b/c sample_df value trumps value from sites table
     self.assertEqual('fake site', con.tables['samples'].df.loc['mc01b',
                                                                'site'])
     # however, additional samples should be added
     self.assertIn('mc01d', con.tables['samples'].df.index)
     for fname in ['_locations.txt', '_samples.txt']:
         os.remove(os.path.join(directory, fname))
     #
     con = cb.Contribution(self.directory,
                           dmodel=DMODEL,
                           read_tables=['sites'],
                           custom_filenames={
                               'locations': '_locations.txt',
                               'samples': '_samples.txt'
                           })
     self.assertEqual(['sites'], list(con.tables.keys()))
     con.propagate_all_tables_info()
     self.assertEqual(sorted(['sites', 'locations']),
                      sorted(con.tables.keys()))
     for fname in ['_locations.txt']:  # no samples available this time
         os.remove(os.path.join(self.directory, fname))
Ejemplo n.º 12
0
 def test_get_name(self):
     magic_df = cb.MagicDataFrame(os.path.join(PROJECT_WD, 'sites.txt'),
                                  dmodel=DMODEL)
     val = magic_df.get_name('description')
     self.assertEqual('VGP:Site 1', val)
     df_slice = magic_df.df.iloc[10:20]
     val = magic_df.get_name('description', df_slice)
     self.assertEqual('VGP:Site 4', val)
     index_names = ['21', '22']
     val = magic_df.get_name('description', index_names=index_names)
     self.assertEqual('VGP:Site 21', val)
Ejemplo n.º 13
0
 def test_update_row(self):
     magic_df = cb.MagicDataFrame(os.path.join(PROJECT_WD, 'sites.txt'),
                                  dmodel=DMODEL)
     self.assertEqual('Lava Flow', magic_df.df.iloc[3]['geologic_types'])
     magic_df.update_row(3, {
         'geologic_types': 'other type',
         'new_col': 'new_val'
     })
     self.assertEqual('other type', magic_df.df.iloc[3]['geologic_types'])
     self.assertIn('new_col', magic_df.df.columns)
     self.assertEqual('new_val', magic_df.df.iloc[3]['new_col'])
Ejemplo n.º 14
0
 def test_atrm_success(self):
     res, outfile = ipmag.atrm_magic(
         'atrm_measurements3.txt',
         self.atrm_WD,
         input_spec_file='orig_specimens.txt',
         output_spec_file='custom_specimens.txt')
     self.assertTrue(res)
     self.assertEqual(outfile,
                      os.path.join(self.atrm_WD, 'custom_specimens.txt'))
     # check that samples are there from input specimen file
     df = cb.MagicDataFrame(outfile)
     self.assertTrue(any(df.df['sample']))
Ejemplo n.º 15
0
 def test_convert_to_pmag_data_list(self):
     magic_df = cb.MagicDataFrame(os.path.join(PROJECT_WD, 'sites.txt'),
                                  dmodel=DMODEL)
     lst = magic_df.convert_to_pmag_data_list('lst')
     self.assertEqual(list, type(lst))
     self.assertEqual(dict, type(lst[0]))
     self.assertEqual('1', str(lst[0]['site']))
     #
     dct = magic_df.convert_to_pmag_data_list("dict")
     self.assertEqual(dict, type(dct))
     self.assertEqual(dict, type(dct[list(dct.keys())[0]]))
     self.assertEqual('1', str(dct['1']['site']))
Ejemplo n.º 16
0
 def test_init_with_data(self):
     data = [{
         'specimen': 'spec1',
         'sample': 'samp1'
     }, {
         'specimen': 'spec2',
         'sample': 'samp2'
     }]
     magic_df = cb.MagicDataFrame(dtype='specimens', data=data)
     self.assertEqual(len(magic_df.df), 2)
     self.assertEqual(magic_df.dtype, 'specimens')
     self.assertEqual('specimen name', magic_df.df.index.name)
     self.assertEqual(['spec1', 'spec2'], sorted(magic_df.df.index))
Ejemplo n.º 17
0
 def test_front_and_backfill(self):
     magic_df = cb.MagicDataFrame(os.path.join(PROJECT_WD, 'sites.txt'),
                                  dmodel=DMODEL)
     directions = magic_df.df.loc['1', 'bed_dip_direction']
     self.assertEqual(
         sorted(directions,
                key=lambda x, y=0: int(x - y)
                if (isinstance(x, float) or isinstance(x, int)) and
                (isinstance(y, float) or isinstance(y, int)) else -1),
         [None, 135, 135])
     magic_df.front_and_backfill(cols=['bed_dip_direction'])
     directions = magic_df.df.loc['1', 'bed_dip_direction']
     self.assertEqual(sorted(directions), [135, 135, 135])
Ejemplo n.º 18
0
 def test_drop_stub_rows(self):
     magic_df = cb.MagicDataFrame(os.path.join(PROJECT_WD, 'sites.txt'),
                                  dmodel=DMODEL)
     self.assertEqual(3, len(magic_df.df.loc['1']))
     magic_df.add_row('1', {'site': '1', 'location': 'new_loc'})
     magic_df.add_row('1', {
         'site': '1',
         'location': 'new_loc',
         'citations': 'real citation'
     })
     self.assertEqual(5, len(magic_df.df.loc['1']))
     magic_df.drop_stub_rows(['site', 'location'])
     self.assertEqual(4, len(magic_df.df.loc['1']))
Ejemplo n.º 19
0
 def test_get_di_block(self):
     magic_df = cb.MagicDataFrame(os.path.join(PROJECT_WD, 'sites.txt'),
                                  dmodel=DMODEL)
     di_block = magic_df.get_di_block(df_slice='all')
     self.assertEqual([289.8, 43.6], di_block[0])
     di_block = magic_df.get_di_block(do_index=True, item_names=['1', '2'])
     self.assertEqual([289.8, 43.6], di_block[0])
     self.assertEqual(4, len(di_block))
     magic_df.df.loc['2', 'method_codes'] = 'fake_code'
     di_block = magic_df.get_di_block(do_index=True,
                                      item_names=['1', '2'],
                                      excl=['fake_code'])
     self.assertEqual(2, len(di_block))
Ejemplo n.º 20
0
    def __init__(self, WD, parent, contribution=None):
        SIZE = wx.DisplaySize()
        SIZE = (SIZE[0] * .95, SIZE[1] * .95)

        wx.Frame.__init__(self,
                          parent,
                          wx.ID_ANY,
                          size=SIZE,
                          name='ErMagicBuilder')
        self.parent = parent
        self.main_frame = self.Parent
        self.panel = wx.ScrolledWindow(self)
        self.panel.SetScrollbars(1, 1, 1, 1)
        if sys.platform in ['win32', 'win64']:
            self.panel.SetScrollbars(20, 20, 50, 50)
        os.chdir(WD)
        self.WD = os.getcwd()
        self.site_lons = []
        self.site_lats = []

        # if ErMagic data object was not passed in,
        # create one based on the working directory

        if not contribution:
            self.contribution = cb.Contribution(self.WD)
        else:
            self.contribution = contribution

        # first propagate from measurements
        self.contribution.propagate_measurement_info()
        # then propagate from other tables
        # (i.e., if sites are in specimens or samples but not measurements)
        self.contribution.propagate_all_tables_info()
        # then add in blank tables if any are missing
        self.table_list = [
            "specimens", "samples", "sites", "locations", "ages"
        ]
        for table in self.table_list:
            if table not in self.contribution.tables:
                new_table = cb.MagicDataFrame(
                    dtype=table, dmodel=self.contribution.data_model)
                self.contribution.tables[table] = new_table

        self.SetTitle("Earth-Ref Magic Builder")
        self.InitUI()
        # hide mainframe, bind close event so that it closes the current window not the mainframe
        self.parent.Hide()
        self.parent.Bind(
            wx.EVT_MENU,
            lambda event: self.parent.menubar.on_quit(event, self),
            self.parent.menubar.file_quit)
Ejemplo n.º 21
0
 def test_get_records_for_code(self):
     magic_df = cb.MagicDataFrame(os.path.join(PROJECT_WD, 'sites.txt'),
                                  dmodel=DMODEL)
     results = magic_df.get_records_for_code('LP-DC2')
     self.assertEqual(87, len(results))
     #
     magic_df.df.loc['1', 'method_codes'] = 'LP-NEW'
     results = magic_df.get_records_for_code('LP', strict_match=False)
     self.assertEqual(89, len(results))
     #
     df_slice = magic_df.df.head()
     results = magic_df.get_records_for_code('LP-DC2',
                                             use_slice=True,
                                             sli=df_slice)
     self.assertEqual(1, len(results))
Ejemplo n.º 22
0
 def test_with_numeric_measurement_name(self):
     # set up meas df with numeric measurement names
     meas_data3 = cb.MagicDataFrame(self.magic_file).df
     del meas_data3['treat_step_num']
     meas_names = range(1001, len(meas_data3) + 1001)
     meas_data3['measurement'] = meas_names
     # convert from MagIC 3 --> MagIC 2.5
     meas_data2 = map_magic.convert_meas_df_thellier_gui(meas_data3, output=2)
     self.assertEqual(1001,
                      meas_data2.iloc[0, meas_data2.columns.get_loc('measurement')])
     self.assertEqual(1001,
                      meas_data2.iloc[0, meas_data2.columns.get_loc('measurement_number')])
     # and back
     meas_data3_again = map_magic.convert_meas_df_thellier_gui(meas_data2, output=3)
     self.assertNotIn('treat_step_num', meas_data3_again.columns)
     self.assertEqual(1001,
                      meas_data3_again.iloc[0, meas_data3_again.columns.get_loc('measurement')])
Ejemplo n.º 23
0
    def test_convert_to_pmag_list(self):
        # np.nan and None should both be converted to a string
        directory = os.path.join(WD, 'data_files', '3_0', 'Megiddo')
        fname = os.path.join(directory, "sites.txt")
        df = cb.MagicDataFrame(fname)

        df.df.loc['mgq04t1', 'age_high'] = np.nan
        df.df.loc['mgq04t1', 'age_low'] = None
        for val in df.df.loc['mgq04t1', 'age_high'].values:
            self.assertTrue(np.isnan(val))

        for val in df.df.loc['mgq04t1', 'age_low'].values:
            self.assertTrue(val is None)

        lst = df.convert_to_pmag_data_list()
        relevant_lst = pmag.get_dictitem(lst, 'site', 'mgq04t1', 'T')
        # make sure np.nan/None values are converted to ''
        for i in relevant_lst:
            self.assertEqual(i['age_high'], '')
            self.assertEqual(i['age_low'], '')
        # make sure numeric values are string-i-fied
        self.assertEqual(str, type(relevant_lst[0]['age']))
Ejemplo n.º 24
0
def main():
    """
    NAME
        make_magic_plots.py

    DESCRIPTION
        inspects magic directory for available data and makes plots

    SYNTAX
        make_magic_plots.py [command line options]

    INPUT
        magic files

    OPTIONS
        -h prints help message and quits
        -f FILE specifies input file name
        -fmt [png,eps,svg,jpg,pdf] specify format, default is png
    """
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    # reset log files
    for fname in ['log.txt', 'errors.txt']:
        f = os.path.join(os.getcwd(), fname)
        if os.path.exists(f):
            os.remove(f)
    image_recs = []
    dirlist = ['./']
    dir_path = os.getcwd()
    #
    if '-fmt' in sys.argv:
        ind = sys.argv.index("-fmt")
        fmt = sys.argv[ind + 1]
    else:
        fmt = 'png'
    if '-f' in sys.argv:
        ind = sys.argv.index("-f")
        filelist = [sys.argv[ind + 1]]
    else:
        filelist = os.listdir(dir_path)
    ## initialize some variables
    samp_file = 'samples.txt'
    meas_file = 'measurements.txt'
    #loc_key = 'location'
    loc_file = 'locations.txt'
    method_key = 'method_codes'
    dec_key = 'dir_dec'
    inc_key = 'dir_inc'
    tilt_corr_key = "dir_tilt_correction"
    aniso_tilt_corr_key = "aniso_tilt_correction"
    hyst_bcr_key = "hyst_bcr"
    hyst_mr_key = "hyst_mr_moment"
    hyst_ms_key = "hyst_ms_moment"
    hyst_bc_key = "hyst_bc"
    Mkeys = ['magnitude', 'magn_moment', 'magn_volume', 'magn_mass']
    results_file = 'sites.txt'
    hyst_file = 'specimens.txt'
    aniso_file = 'specimens.txt'
    # create contribution and propagate data throughout
    full_con = cb.Contribution()
    full_con.propagate_location_to_measurements()
    full_con.propagate_location_to_specimens()
    full_con.propagate_location_to_samples()
    if not full_con.tables:
        print('-E- No MagIC tables could be found in this directory')
        error_log("No MagIC tables found")
        return
    # try to get the contribution id for error logging
    con_id = ""
    if 'contribution' in full_con.tables:
        if 'id' in full_con.tables['contribution'].df.columns:
            con_id = full_con.tables['contribution'].df.iloc[0]['id']
    # check to see if propagation worked, otherwise you can't plot by location
    lowest_table = None
    for table in full_con.ancestry:
        if table in full_con.tables:
            lowest_table = table
            break

    do_full_directory = False
    # check that locations propagated down to the lowest table in the contribution
    if 'location' in full_con.tables[lowest_table].df.columns:
        if 'locations' not in full_con.tables:
            info_log(
                'location names propagated to {}, but could not be validated'.
                format(lowest_table))
        # are there any locations in the lowest table?
        elif not all(full_con.tables[lowest_table].df['location'].isnull()):
            locs = full_con.tables['locations'].df.index.unique()
            lowest_locs = full_con.tables[lowest_table].df['location'].unique()
            incorrect_locs = set(lowest_locs).difference(set(locs))
            # are they actual locations?
            if not incorrect_locs:
                info_log(
                    'location names propagated to {}'.format(lowest_table))
            else:
                do_full_directory = True
                error_log(
                    'location names did not propagate fully to {} table (looks like there are some naming inconsistencies between tables)'
                    .format(lowest_table),
                    con_id=con_id)
        else:
            do_full_directory = True
            error_log(
                'could not propagate location names down to {} table'.format(
                    lowest_table),
                con_id=con_id)
    else:
        do_full_directory = True
        error_log('could not propagate location names down to {} table'.format(
            lowest_table),
                  con_id=con_id)

    all_data = {}
    all_data['measurements'] = full_con.tables.get('measurements', None)
    all_data['specimens'] = full_con.tables.get('specimens', None)
    all_data['samples'] = full_con.tables.get('samples', None)
    all_data['sites'] = full_con.tables.get('sites', None)
    all_data['locations'] = full_con.tables.get('locations', None)
    if 'locations' in full_con.tables:
        locations = full_con.tables['locations'].df.index.unique()
    else:
        locations = ['']
    dirlist = [
        loc for loc in locations if cb.not_null(loc, False) and loc != 'nan'
    ]
    if not dirlist:
        dirlist = ["./"]
    if do_full_directory:
        dirlist = ["./"]

    # plot the whole contribution as one location
    if dirlist == ["./"]:
        error_log('plotting the entire contribution as one location',
                  con_id=con_id)
        for fname in os.listdir("."):
            if fname.endswith(".txt"):
                shutil.copy(fname, "tmp_" + fname)

    # if possible, go through all data by location
    # use tmp_*.txt files to separate out by location

    for loc in dirlist:
        print('\nworking on: ', loc)

        def get_data(dtype, loc_name):
            """
            Extract data of type dtype for location loc_name.
            Write tmp_dtype.txt files if possible.
            """
            if cb.not_null(all_data[dtype], False):
                data_container = all_data[dtype]
                if loc_name == "./":
                    data_df = data_container.df
                else:
                    # awkward workaround for chars like "(" and "?" that break in regex
                    try:
                        data_df = data_container.df[data_container.df[
                            'location'].astype(str).str.contains(loc_name,
                                                                 na=False)]
                    except:  #sre_constants.error:
                        data_df = data_container.df[
                            data_container.df['location'] == loc_name]

                data = data_container.convert_to_pmag_data_list(df=data_df)
                res = data_container.write_magic_file(
                    'tmp_{}.txt'.format(dtype), df=data_df)
                if not res:
                    return [], []
                return data, data_df
            return [], []

        meas_data, meas_df = get_data('measurements', loc)
        spec_data, spec_df = get_data('specimens', loc)
        samp_data, samp_df = get_data('samples', loc)
        site_data, site_df = get_data('sites', loc)
        loc_data, loc_df = get_data('locations', loc)

        con = cb.Contribution(read_tables=[])
        con.tables['measurements'] = cb.MagicDataFrame(df=meas_df,
                                                       dtype="measurements")
        con.tables['specimens'] = cb.MagicDataFrame(df=spec_df,
                                                    dtype="specimens")
        con.tables['samples'] = cb.MagicDataFrame(df=samp_df, dtype="samples")
        con.tables['sites'] = cb.MagicDataFrame(df=site_df, dtype="sites")
        con.tables['locations'] = cb.MagicDataFrame(df=loc_df,
                                                    dtype="locations")

        if loc == "./":  # if you can't sort by location, do everything together
            con = full_con
            try:
                meas_data = con.tables[
                    'measurements'].convert_to_pmag_data_list()
            except KeyError:
                meas_data = None
            try:
                spec_data = con.tables['specimens'].convert_to_pmag_data_list()
            except KeyError:
                spec_data = None
            try:
                samp_data = con.tables['samples'].convert_to_pmag_data_list()
            except KeyError:
                samp_data = None
            try:
                site_data = con.tables['sites'].convert_to_pmag_data_list()
            except KeyError:
                site_data = None

        crd = 's'
        if 'samples' in con.tables:
            if 'azimuth' in con.tables['samples'].df.columns:
                if any(con.tables['samples'].df['azimuth'].dropna()):
                    crd = 'g'
        if crd == 's':
            print('using specimen coordinates')
        else:
            print('using geographic coordinates')
        if meas_file in filelist and meas_data:  # start with measurement data
            print('working on plotting measurements data')
            data = meas_data
            file_type = 'measurements'
            # looking for  zeq_magic possibilities
            # get all non blank method codes
            AFZrecs = pmag.get_dictitem(data, method_key, 'LT-AF-Z', 'has')
            # get all non blank method codes
            TZrecs = pmag.get_dictitem(data, method_key, 'LT-T-Z', 'has')
            # get all non blank method codes
            MZrecs = pmag.get_dictitem(data, method_key, 'LT-M-Z', 'has')
            # get all dec measurements
            Drecs = pmag.get_dictitem(data, dec_key, '', 'F')
            # get all inc measurements
            Irecs = pmag.get_dictitem(data, inc_key, '', 'F')
            for key in Mkeys:
                Mrecs = pmag.get_dictitem(data, key, '',
                                          'F')  # get intensity data
                if len(Mrecs) > 0:
                    break
            # potential for stepwise demag curves
            if len(AFZrecs) > 0 or len(TZrecs) > 0 or len(MZrecs) > 0 and len(
                    Drecs) > 0 and len(Irecs) > 0 and len(Mrecs) > 0:
                #CMD = 'zeq_magic.py -f tmp_measurements.txt -fsp tmp_specimens.txt -fsa tmp_samples.txt -fsi tmp_sites.txt -sav -fmt ' + fmt + ' -crd ' + crd + " -new"
                CMD = "ipmag.zeq_magic(crd={}, n_plots='all', contribution={}, image_records=True)".format(
                    crd, con)
                print(CMD)
                info_log(CMD, loc)
                res, outfiles, zeq_images = ipmag.zeq_magic(crd=crd,
                                                            n_plots='all',
                                                            contribution=con,
                                                            image_records=True)
                image_recs.extend(zeq_images)
            # looking for  thellier_magic possibilities
            if len(pmag.get_dictitem(data, method_key, 'LP-PI-TRM',
                                     'has')) > 0:
                #CMD = 'thellier_magic.py -f tmp_measurements.txt -fsp tmp_specimens.txt -sav -fmt ' + fmt
                CMD = "ipmag.thellier_magic(n_specs='all', fmt='png', contribution={}, image_records=True)".format(
                    con)
                print(CMD)
                info_log(CMD, loc)
                res, outfiles, thellier_images = ipmag.thellier_magic(
                    n_specs='all',
                    fmt="png",
                    contribution=con,
                    image_records=True)
                image_recs.extend(thellier_images)
            # looking for hysteresis possibilities
            if len(pmag.get_dictitem(data, method_key, 'LP-HYS',
                                     'has')) > 0:  # find hyst experiments
                # check for reqd columns
                missing = check_for_reqd_cols(data, ['treat_temp'])
                if missing:
                    error_log(
                        'LP-HYS method code present, but required column(s) [{}] missing'
                        .format(", ".join(missing)),
                        loc,
                        "quick_hyst.py",
                        con_id=con_id)
                else:
                    #CMD = 'quick_hyst.py -f tmp_measurements.txt -sav -fmt ' + fmt
                    CMD = "ipmag.quick_hyst(fmt='png', n_plots='all', contribution={}, image_records=True)".format(
                        con)
                    print(CMD)
                    info_log(CMD, loc)
                    res, outfiles, quick_hyst_recs = ipmag.quick_hyst(
                        fmt="png",
                        n_plots='all',
                        contribution=con,
                        image_records=True)
                    image_recs.extend(quick_hyst_recs)
            # equal area plots of directional data
            # at measurement level (by specimen)
            if data:
                missing = check_for_reqd_cols(data, ['dir_dec', 'dir_inc'])
                if not missing:
                    #CMD = "eqarea_magic.py -f tmp_measurements.txt -obj spc -sav -no-tilt -fmt " + fmt
                    CMD = "ipmag.eqarea_magic(fmt='png', n_plots='all', ignore_tilt=True, plot_by='spc', contribution={}, source_table='measurements', image_records=True)".format(
                        con)
                    print(CMD)
                    info_log(CMD, loc, "eqarea_magic.py")
                    res, outfiles, eqarea_spc_images = ipmag.eqarea_magic(
                        fmt="png",
                        n_plots='all',
                        ignore_tilt=True,
                        plot_by="spc",
                        contribution=con,
                        source_table="measurements",
                        image_records=True)
                    image_recs.extend(eqarea_spc_images)

        else:
            if VERBOSE:
                print('-I- No measurement data found')

        # site data
        if results_file in filelist and site_data:
            print('-I- result file found', results_file)
            data = site_data
            file_type = 'sites'
            print('-I- working on site directions')
            print('number of datapoints: ', len(data), loc)
            dec_key = 'dir_dec'
            inc_key = 'dir_inc'
            int_key = 'int_abs'
            SiteDIs = pmag.get_dictitem(data, dec_key, "", 'F')  # find decs
            SiteDIs = pmag.get_dictitem(SiteDIs, inc_key, "",
                                        'F')  # find decs and incs
            dir_data_found = len(SiteDIs)
            print('{} Dec/inc pairs found'.format(dir_data_found))
            if SiteDIs:
                # then convert tilt_corr_key to correct format
                old_SiteDIs = SiteDIs
                SiteDIs = []
                for rec in old_SiteDIs:
                    if tilt_corr_key not in rec:
                        rec[tilt_corr_key] = "0"
                    # make sure tilt_corr_key is a correct format
                    try:
                        rec[tilt_corr_key] = str(int(float(
                            rec[tilt_corr_key])))
                    except ValueError:
                        rec[tilt_corr_key] = "0"
                    SiteDIs.append(rec)

                print('number of individual directions: ', len(SiteDIs))
                # tilt corrected coordinates
                SiteDIs_t = pmag.get_dictitem(SiteDIs,
                                              tilt_corr_key,
                                              '100',
                                              'T',
                                              float_to_int=True)
                print('number of tilt corrected directions: ', len(SiteDIs_t))
                SiteDIs_g = pmag.get_dictitem(
                    SiteDIs, tilt_corr_key, '0', 'T',
                    float_to_int=True)  # geographic coordinates
                print('number of geographic  directions: ', len(SiteDIs_g))
                SiteDIs_s = pmag.get_dictitem(
                    SiteDIs, tilt_corr_key, '-1', 'T',
                    float_to_int=True)  # sample coordinates
                print('number of sample  directions: ', len(SiteDIs_s))
                SiteDIs_x = pmag.get_dictitem(SiteDIs, tilt_corr_key, '',
                                              'T')  # no coordinates
                print('number of no coordinates  directions: ', len(SiteDIs_x))
                if len(SiteDIs_t) > 0 or len(SiteDIs_g) > 0 or len(
                        SiteDIs_s) > 0 or len(SiteDIs_x) > 0:
                    CRD = ""
                    if len(SiteDIs_t) > 0:
                        CRD = ' -crd t'
                        crd = "t"
                    elif len(SiteDIs_g) > 0:
                        CRD = ' -crd g'
                        crd = "g"
                    elif len(SiteDIs_s) > 0:
                        CRD = ' -crd s'
                        crd = "s"
                    #CMD = 'eqarea_magic.py -f tmp_sites.txt -fsp tmp_specimens.txt -fsa tmp_samples.txt -flo tmp_locations.txt -sav -fmt ' + fmt + CRD
                    CMD = "ipmag.eqarea_magic(crd={}, fmt='png', n_plots='all', contribution={}, source_table='sites')".format(
                        crd, con)
                    print(CMD)
                    info_log(CMD, loc)
                    res, outfiles, eqarea_site_recs = ipmag.eqarea_magic(
                        crd=crd,
                        fmt="png",
                        n_plots='all',
                        contribution=con,
                        source_table="sites",
                        image_records=True)
                    image_recs.extend(eqarea_site_recs)
                else:
                    if dir_data_found:
                        error_log(
                            '{} dec/inc pairs found, but no equal area plots were made'
                            .format(dir_data_found),
                            loc,
                            "equarea_magic.py",
                            con_id=con_id)
            #
            print('-I- working on VGP map')
            VGPs = pmag.get_dictitem(SiteDIs, 'vgp_lat', "",
                                     'F')  # are there any VGPs?
            if len(VGPs) > 0:  # YES!
                #CMD = 'vgpmap_magic.py -f tmp_sites.txt -prj moll -res c -sym ro 5 -sav -fmt png'
                CMD = "ipmag.vgpmap_magic(proj='moll', sym='ro', size=5, fmt='png', contribution={})".format(
                    con)
                print(CMD)
                info_log(CMD, loc, 'vgpmap_magic.py')
                res, outfiles, vgpmap_recs = ipmag.vgpmap_magic(
                    proj='moll',
                    sym='ro',
                    size=5,
                    fmt="png",
                    contribution=con,
                    image_records=True)
                image_recs.extend(vgpmap_recs)
            else:
                print('-I- No vgps found')

            print('-I- Look for intensities')
            # is there any intensity data?
            if site_data:
                if int_key in site_data[0].keys():
                    # old way, wasn't working right:
                    #CMD = 'magic_select.py  -key ' + int_key + ' 0. has -F tmp1.txt -f tmp_sites.txt'
                    Selection = pmag.get_dictkey(site_data, int_key, dtype="f")
                    selection = [i * 1e6 for i in Selection if i != 0]
                    loc = loc.replace(" ", "_")
                    if loc == "./":
                        loc_name = ""
                    else:
                        loc_name = loc
                    histfile = 'LO:_' + loc_name + \
                        '_TY:_intensities_histogram:_.' + fmt
                    CMD = "histplot.py -twin -b 1 -xlab 'Intensity (uT)' -sav -f intensities.txt -F " + histfile
                    CMD = "ipmag.histplot(data=selection, outfile=histfile, xlab='Intensity (uT)', binsize=1, norm=-1, save_plots=True)".format(
                        histfile)
                    info_log(CMD, loc)
                    print(CMD)
                    ipmag.histplot(data=selection,
                                   outfile=histfile,
                                   xlab="Intensity (uT)",
                                   binsize=1,
                                   norm=-1,
                                   save_plots=True)
                    histplot_rec = {
                        'file': histfile,
                        'type': 'Other',
                        'title': 'Intensity histogram',
                        'software_packages': version.version,
                        'keywords': "",
                        'timestamp': datetime.date.today().isoformat()
                    }
                    image_recs.append(histplot_rec)
                else:
                    print('-I- No intensities found')
            else:
                print('-I- No intensities found')

        ##
        if hyst_file in filelist and spec_data:
            print('working on hysteresis', hyst_file)
            data = spec_data
            file_type = 'specimens'
            hdata = pmag.get_dictitem(data, hyst_bcr_key, '', 'F')
            hdata = pmag.get_dictitem(hdata, hyst_mr_key, '', 'F')
            hdata = pmag.get_dictitem(hdata, hyst_ms_key, '', 'F')
            # there are data for a dayplot
            hdata = pmag.get_dictitem(hdata, hyst_bc_key, '', 'F')
            if len(hdata) > 0:
                CMD = "ipmag.dayplot_magic(save=True, fmt='png', contribution={}, image_records=True)".format(
                    con)
                info_log(CMD, loc)
                print(CMD)
                res, outfiles, dayplot_recs = ipmag.dayplot_magic(
                    save=True, fmt='png', contribution=con, image_records=True)
                image_recs.extend(dayplot_recs)
            else:
                print('no hysteresis data found')
        if aniso_file in filelist and spec_data:  # do anisotropy plots if possible
            print('working on anisotropy', aniso_file)
            data = spec_data
            file_type = 'specimens'

            # make sure there is some anisotropy data
            if not data:
                print('No anisotropy data found')
            elif 'aniso_s' not in data[0]:
                print('No anisotropy data found')
            else:
                # get specimen coordinates
                if aniso_tilt_corr_key not in data[0]:
                    sdata = data
                else:
                    sdata = pmag.get_dictitem(data,
                                              aniso_tilt_corr_key,
                                              '-1',
                                              'T',
                                              float_to_int=True)
                # get specimen coordinates
                gdata = pmag.get_dictitem(data,
                                          aniso_tilt_corr_key,
                                          '0',
                                          'T',
                                          float_to_int=True)
                # get specimen coordinates
                tdata = pmag.get_dictitem(data,
                                          aniso_tilt_corr_key,
                                          '100',
                                          'T',
                                          float_to_int=True)
                if len(sdata) > 3:
                    CMD = "ipmag.aniso_magic(iboot=0, ihext=1, crd='s', fmt='png', contribution={})".format(
                        con)
                    print(CMD)
                    info_log(CMD, loc)
                    res, files, aniso_recs = ipmag.aniso_magic(
                        iboot=0,
                        ihext=1,
                        crd="s",
                        fmt="png",
                        contribution=con,
                        image_records=True)
                    image_recs.extend(aniso_recs)
                if len(gdata) > 3:
                    CMD = "ipmag.aniso_magic(iboot=0, ihext=1, crd='g', fmt='png', contribution={})".format(
                        con)
                    print(CMD)
                    info_log(CMD, loc)
                    res, files, aniso_recs = ipmag.aniso_magic(
                        iboot=0,
                        ihext=1,
                        crd="g",
                        fmt="png",
                        contribution=con,
                        image_records=True)
                    image_recs.extend(aniso_recs)
                if len(tdata) > 3:
                    CMD = "ipmag.aniso_magic(iboot=0, ihext=1, crd='g', fmt='png', contribution={})".format(
                        con)
                    print(CMD)
                    info_log(CMD, loc)
                    res, files, aniso_recs = ipmag.aniso_magic(
                        iboot=0,
                        ihext=1,
                        crd="t",
                        fmt="png",
                        contribution=con,
                        image_records=True)
                    image_recs.extend(aniso_recs)

        # remove temporary files
        for fname in glob.glob('tmp*.txt'):
            os.remove(fname)

    # now we need full contribution data
    if loc_file in filelist and loc_data:
        #data, file_type = pmag.magic_read(loc_file)  # read in location data
        data = loc_data
        print('-I- working on pole map')
        poles = pmag.get_dictitem(data, 'pole_lat', "",
                                  'F')  # are there any poles?
        poles = pmag.get_dictitem(poles, 'pole_lon', "",
                                  'F')  # are there any poles?
        if len(poles) > 0:  # YES!
            CMD = 'polemap_magic.py -sav -fmt png -rev gv 40'
            CMD = 'ipmag.polemap_magic(flip=True, rsym="gv", rsymsize=40, fmt="png", contribution={})'.format(
                full_con)
            print(CMD)
            info_log(CMD, "all locations", "polemap_magic.py")
            res, outfiles, polemap_recs = ipmag.polemap_magic(
                flip=True,
                rsym="gv",
                rsymsize=40,
                fmt="png",
                contribution=full_con,
                image_records=True)
            image_recs.extend(polemap_recs)
        else:
            print('-I- No poles found')

    if image_recs:
        new_image_file = os.path.join(dir_path, 'new_images.txt')
        old_image_file = os.path.join(dir_path, 'images.txt')
        pmag.magic_write(new_image_file, image_recs, 'images')
        if os.path.exists(old_image_file):
            ipmag.combine_magic([old_image_file, new_image_file],
                                outfile=old_image_file,
                                magic_table="images",
                                dir_path=dir_path)
        else:
            os.rename(new_image_file, old_image_file)
    if set_env.isServer:
        thumbnails.make_thumbnails(dir_path)
Ejemplo n.º 25
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
Ejemplo n.º 26
0
 def test_sort_dataframe_cols(self):
     magic_df = cb.MagicDataFrame(os.path.join(PROJECT_WD, 'sites.txt'),
                                  dmodel=DMODEL)
     self.assertEqual('bed_dip', magic_df.df.columns[0])
     magic_df.sort_dataframe_cols()
     self.assertEqual('site', magic_df.df.columns[0])
Ejemplo n.º 27
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("-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")
    crit_file = pmag.get_named_arg("-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("-fmt", "svg")
    if '-sav' in sys.argv:
        plots, verbose = 1, 0
    if '-z' in sys.argv:
        Zdiff = 1
    specimen = pmag.get_named_arg("-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 = cb.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'
    ]
    spec_container, prior_spec_data = None, []
    if 'specimens' in contribution.tables:
        spec_container = contribution.tables['specimens']
        if 'method_codes' in spec_container.df.columns:
            # look up all prior intensity interpretations
            prior_spec_data = spec_container.get_records_for_code(
                'LP-PI-TRM', strict_match=False)
    backup = 0
    #
    Mkeys = ['magn_moment', 'magn_volume', 'magn_mass']
    #
    #   create measurement dataframe
    #
    if pmagplotlib.isServer:
        contribution.propagate_location_to_measurements()

    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
    ]
    # plot first intensity method found - normalized to initial value anyway -
    # doesn't matter which used

    int_key = meas_container.find_filled_col(intensity_types)
    if not int_key:
        print("-W- No intensity data found, thellier_magic cannot run")
        return

    # get all the non-null intensity records of the same type
    meas_data = meas_data[meas_data[int_key].notnull()]

    if not len(meas_data):
        print("-W- No intensity data found, thellier_magic cannot run")
        return
    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
    #
    # this is a Series of all the specimen names
    specimen_names = meas_data.specimen.unique()
    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 = cb.MagicDataFrame(dtype='specimens',
                                       columns=specimen_cols)
    # this is for interpretations from this session
    current_spec_data = spec_container.df
    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):
        # 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 pmagplotlib.isServer:
            this_specimen_measurements = meas_data[meas_data['specimen'] ==
                                                   this_specimen]
            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 = ""

#
#    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
        if len(prior_spec_data):
            prior_specimen_interpretations = prior_spec_data[
                prior_spec_data['specimen'].str.contains(
                    this_specimen) == True]  # fish out prior interpretation
        else:
            prior_specimen_interpretations = []

#
# 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 is not "":
                        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
            if 'K' in units:
                u = 'K'
            elif 'J' in units:
                u = 'J'
            pmagplotlib.plot_arai_zij(AZD, araiblock, zijdblock, this_specimen,
                                      u)
            if verbose:
                pmagplotlib.draw_figs(AZD)
            if cb.is_null(start, False) or cb.is_null(end, False):
                if verbose:
                    ans = input('Return for next specimen, q to quit:  ')
                    if ans == 'q':
                        sys.exit()
                k += 1  # moving on
                start, end = "", ""
                continue

            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.plot_b(AZD, araiblock, zijdblock, pars)
                mpars = pmag.domean(araiblock[1], start, end, 'DE-BFL')
                if verbose:
                    pmagplotlib.draw_figs(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()):
                        if pmagplotlib.isServer:
                            files[
                                key] = "LO:_{}_SI:_{}_SA:_{}_SP:_{}_TY:_{}_.{}".format(
                                    loc, site, samp, this_specimen, key, fmt)
                        else:
                            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'
                        titles['eqarea'] = 'Equal Area Plot'
                        AZD = pmagplotlib.add_borders(AZD, titles, black,
                                                      purple)
                    pmagplotlib.save_plots(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()
            start, end = "", ""
            k += 1  # moving on
Ejemplo n.º 28
0
 def test_init_blank(self):
     magic_df = cb.MagicDataFrame()
     self.assertFalse(magic_df.df)
Ejemplo n.º 29
0
 def test_init_with_file(self):
     magic_df = cb.MagicDataFrame(os.path.join(PROJECT_WD, 'sites.txt'),
                                  dmodel=DMODEL)
     self.assertEqual('sites', magic_df.dtype)
     self.assertEqual('1', magic_df.df.index[1])
Ejemplo n.º 30
0
 def test_init_with_dtype(self):
     magic_df = cb.MagicDataFrame(dtype='specimens', dmodel=DMODEL)
     self.assertEqual('specimens', magic_df.dtype)