Exemple #1
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 = nb.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 = nb.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 = nb.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 = nb.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))
Exemple #2
0
 def test_get_min_max_lat_lon(self):
     site_container = nb.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 = nb.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 = nb.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"))
Exemple #3
0
 def test_add_empty_magic_table(self):
     con = nb.Contribution(self.directory, read_tables=['specimens'],
                           dmodel=DMODEL)
     self.assertEqual(set(['specimens']), set(con.tables.keys()))
     con.add_empty_magic_table('samples')
     self.assertEqual(set(['specimens', 'samples']), set(con.tables.keys()))
     self.assertEqual(0, len(con.tables['samples'].df))
Exemple #4
0
 def get_wd_data(self):
     wait = wx.BusyInfo(
         'Reading in data from current working directory, please wait...')
     wx.Yield()
     print '-I- Read in any available data from working directory'
     self.contribution = nb.Contribution(self.WD, dmodel=self.data_model)
     del wait
Exemple #5
0
    def test_find_missing_items(self):
        for table in self.con.tables:
            self.assertEqual(set(), self.con.find_missing_items(table))

        self.con.tables['sites'].delete_row(0)
        missing = self.con.find_missing_items('sites')
        self.assertEqual(set(['hz05']), missing)

        con = nb.Contribution(PROJECT_WD)
        for table in con.tables:
            self.assertEqual(set(), con.find_missing_items(table))

        directory = os.path.join(WD, 'data_files', '3_0', 'McMurdo')
        con = nb.Contribution(directory)
        for table in con.tables:
            self.assertEqual(set(), con.find_missing_items(table))
Exemple #6
0
 def on_change_dir_button(self, event):
     """
     create change directory frame
     """
     currentDirectory = self.WD #os.getcwd()
     change_dir_dialog = wx.DirDialog(self.panel,
                                      "Choose your working directory to create or edit a MagIC contribution:",
                                      defaultPath=currentDirectory,
                                      style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON | wx.DD_CHANGE_DIR)
     result = change_dir_dialog.ShowModal()
     if result == wx.ID_CANCEL:
         return
     if result == wx.ID_OK:
         self.WD = change_dir_dialog.GetPath()
         self.dir_path.SetValue(self.WD)
     change_dir_dialog.Destroy()
     wait = wx.BusyInfo('Initializing data object in new directory, please wait...')
     wx.Yield()
     print '-I- Initializing magic data object'
     # make new contribution object, but reuse old data_model
     self.contribution = nb.Contribution(self.WD, dmodel=self.data_model)
     self.edited = False
     print '-I- Read in any available data from working directory'
     print '-I- Initializing headers'
     del wait
Exemple #7
0
 def test_propagate_average_up(self):
     directory = os.path.join('data_files', '3_0', 'McMurdo')
     con = nb.Contribution(directory, read_tables=['sites', 'samples'])
     con.tables['sites'].df.drop(['lat', 'lon'],
                                 axis='columns',
                                 inplace=True)
     con.tables['samples'].df.loc['mc01a', 'lat'] = -60.
     # test basic function
     con.propagate_average_up()
     self.assertTrue(
         all(con.tables['sites'].df[['lat', 'lon']].values.ravel()))
     self.assertEqual([-75.61875],
                      con.tables['sites'].df.loc['mc01', 'lat'].unique())
     # make sure does not overwrite existing values
     con = nb.Contribution(directory, read_tables=['sites', 'samples'])
     con.tables['sites'].df.loc['mc01', 'lon'] = 12
     con.propagate_average_up()
     self.assertEqual([12], con.tables['sites'].df.loc['mc01',
                                                       'lon'].unique())
     self.assertNotIn('new_lat', con.tables['sites'].df.columns)
     self.assertNotIn('new_lon', con.tables['sites'].df.columns)
     # make sure works with only some sample data available
     con = nb.Contribution(directory, read_tables=['sites', 'samples'])
     con.tables['samples'].df.drop(['lon'], axis='columns', inplace=True)
     con.propagate_average_up()
     # fails gracefully?
     con = nb.Contribution(directory, read_tables=['sites', 'samples'])
     con.tables['samples'].df.drop(['site'], axis='columns', inplace=True)
     con.tables['sites'].df.loc['mc01', 'lat'] = ''
     con.propagate_average_up()
     # fails gracefully?
     con = nb.Contribution(directory,
                           read_tables=['sites', 'samples'],
                           custom_filenames={'samples': '_samples.txt'})
     res = con.propagate_average_up()
     self.assertIsNone(res)
     # fails gracefully?
     res = con.propagate_average_up(target_df_name='samples',
                                    source_df_name='sites')
     self.assertIsNone(res)
     # fails gracefully?
     res = con.propagate_average_up(target_df_name='sites',
                                    source_df_name='specimens')
     self.assertIsNone(res)
Exemple #8
0
 def on_clear(self, event):
     """
     initialize window to allow user to empty the working directory
     """
     dia = pmag_menu_dialogs.ClearWD(self.parent, self.parent.WD)
     clear = dia.do_clear()
     if clear:
         print('-I- Clear data object')
         self.contribution = nb.Contribution(self.WD, dmodel=self.data_model)
         self.edited = False
Exemple #9
0
 def test_propagate_name_down(self):
     directory = os.path.join(WD, 'data_files', 'Measurement_Import',
                              'CIT_magic', 'PI47')
     con = nb.Contribution(directory)
     self.assertNotIn('location', con.tables['measurements'].df.columns)
     # need to actually test this
     con.propagate_name_down('sample', 'measurements')
     con.propagate_name_down('site', 'measurements')
     con.propagate_name_down('location', 'measurements')
     self.assertIn('location', con.tables['measurements'].df.columns)
Exemple #10
0
 def get_wd_data(self):
     """
     Show dialog to get user input for which directory
     to set as working directory.
     """
     wait = wx.BusyInfo('Reading in data from current working directory, please wait...')
     #wx.Yield()
     print('-I- Read in any available data from working directory')
     self.contribution = nb.Contribution(self.WD, dmodel=self.data_model)
     del wait
Exemple #11
0
    def test_kly4s_with_valid_infile_data_model3(self):
        in_dir = os.path.join(WD, 'data_files', 'Measurement_Import',
                              'kly4s_magic')
        program_ran, outfile = ipmag.kly4s_magic('KLY4S_magic_example.dat',
                                                 output_dir_path=WD,
                                                 input_dir_path=in_dir,
                                                 data_model_num=3)

        con = nb.Contribution(WD)
        self.assertEqual(['measurements', 'samples', 'sites', 'specimens'],
                         sorted(con.tables))
Exemple #12
0
 def test_add_magic_table(self):
     con = nb.Contribution(self.directory,
                           read_tables=['specimens'],
                           dmodel=DMODEL)
     self.assertEqual(set(['specimens']), set(con.tables.keys()))
     con.add_magic_table('samples')
     self.assertEqual(set(['specimens', 'samples']), set(con.tables.keys()))
     self.assertGreater(len(con.tables['samples'].df), 0)
     con.add_magic_table('unknown', 'sites.txt')
     self.assertEqual(set(['specimens', 'samples', 'sites']),
                      set(con.tables.keys()))
     self.assertGreater(len(con.tables['sites'].df), 0)
Exemple #13
0
 def test_propagate_name_down_fail(self):
     """fail gracefully"""
     directory = os.path.join(WD, 'data_files', 'Measurement_Import',
                              'CIT_magic', 'PI47')
     con = nb.Contribution(directory)
     self.assertNotIn('sample', con.tables['measurements'].df.columns)
     self.assertNotIn('location', con.tables['measurements'].df.columns)
     # missing link:
     del con.tables['samples'].df['site']
     meas_df = con.propagate_location_to_measurements()
     self.assertIn('sample', con.tables['measurements'].df.columns)
     self.assertNotIn('location', meas_df.columns)
Exemple #14
0
    def on_convert_3(self, event):
        dia = pw.UpgradeDialog(None)
        dia.Center()
        res = dia.ShowModal()
        if res == wx.ID_CANCEL:
            webbrowser.open("https://www2.earthref.org/MagIC/upgrade", new=2)
            return
        ## more nicely styled way, but doesn't link to earthref
        #msg = "This tool is meant for relatively simple upgrades (for instance, a measurement file, a sample file, and a criteria file).\nIf you have a more complex contribution to upgrade, and you want maximum accuracy, use the upgrade tool at https://www2.earthref.org/MagIC/upgrade.\n\nDo you want to continue?"
        #result = pw.warning_with_override(msg)
        #if result == wx.ID_NO:
            #webbrowser.open("https://www2.earthref.org/MagIC/upgrade", new=2)
            #return
        # turn files from 2.5 --> 3.0 (rough translation)
        meas, upgraded, no_upgrade = pmag.convert_directory_2_to_3('magic_measurements.txt',
                                                                   input_dir=self.WD, output_dir=self.WD,
                                                                   data_model=self.contribution.data_model)
        if not meas:
            wx.MessageBox('2.5 --> 3.0 failed. Do you have a magic_measurements.txt file in your working directory?',
                          'Info', wx.OK | wx.ICON_INFORMATION)
            return

        # create a contribution
        self.contribution = nb.Contribution(self.WD)
        # make skeleton files with specimen, sample, site, location data
        self.contribution.propagate_measurement_info()
        #
        # note what DIDN'T upgrade
        #no_upgrade = []
        #for fname in os.listdir(self.WD):
        #    if 'rmag' in fname:
        #        no_upgrade.append(fname)
        #    elif fname in ['pmag_results.txt', 'pmag_criteria.txt',
        #                   'er_synthetics.txt', 'er_images.txt',
        #                   'er_plots.txt', 'er_ages.txt']:
        #        no_upgrade.append(fname)

        # pop up
        upgraded_string = ", ".join(upgraded)
        if no_upgrade:
            no_upgrade_string = ", ".join(no_upgrade)
            msg = '2.5 --> 3.0 translation completed!\n\nThese 3.0 format files were created: {}.\n\nHowever, these 2.5 format files could not be upgraded: {}.\n\nTo convert all 2.5 files, use the MagIC upgrade tool: https://www2.earthref.org/MagIC/upgrade\n'.format(upgraded_string, no_upgrade_string)
            if 'criteria.txt' in upgraded:
                msg += '\nNote: Please check your criteria file for completeness and accuracy, as not all 2.5 files will be fully upgraded.'
            if 'pmag_criteria.txt' in no_upgrade:
                msg += '\nNote: Not all criteria files can be upgraded, even on the MagIC site.  You may need to recreate an old pmag_criteria file from scratch in Thellier GUI or Demag GUI.'
            wx.MessageBox(msg, 'Warning', wx.OK | wx.ICON_INFORMATION)
        else:
            msg = '2.5 --> 3.0 translation completed!\nThese files were converted: {}'.format(upgraded_string)
            wx.MessageBox(msg, 'Info', wx.OK | wx.ICON_INFORMATION)
Exemple #15
0
 def on_clear(self, event):
     """
     initialize window to allow user to empty the working directory
     """
     dia = pmag_menu_dialogs.ClearWD(self.parent, self.parent.WD)
     clear = dia.do_clear()
     if clear:
         # clear directory, but use previously acquired data_model
         if self.data_model_num == 2.5:
             self.parent.er_magic = builder.ErMagicBuilder(
                 self.parent.WD, self.parent.er_magic.data_model)
         elif self.data_model_num == 3:
             self.parent.contribution = nb.Contribution(
                 self.parent.WD, dmodel=self.parent.contribution.data_model)
Exemple #16
0
 def test_propagate_cols_up(self):
     directory = os.path.join('data_files', '3_0', 'McMurdo')
     con = nb.Contribution(directory,
                           read_tables=['sites', 'samples'],
                           custom_filenames={'locations': '_locations.txt'})
     con.tables['samples'].df.loc['mc01a', 'lithologies'] = 'other:Trachyte'
     ind = con.tables['samples'].df.columns.get_loc('lithologies')
     con.tables['samples'].df.iloc[2, ind] = None
     con.tables['samples'].df.iloc[3, ind] = np.nan
     con.tables['samples'].df.iloc[4, ind] = ''
     con.tables['sites'].df.loc['mc01', 'lithologies'] = ''
     con.tables['sites'].df[:10][['lithologies', 'geologic_types']]
     cols = ['lithologies', 'geologic_types']
     con.propagate_cols_up(cols, 'sites', 'samples')
     self.assertEqual(
         'Other:Trachyte',
         con.tables['sites'].df.loc['mc01', 'lithologies'].unique()[0])
     self.assertEqual(
         'Basalt', con.tables['sites'].df.loc['mc02',
                                              'lithologies'].unique()[0])
     self.assertTrue(all(con.tables['sites'].df['lithologies']))
     # fail gracefully
     con = nb.Contribution(directory, read_tables=['sites'])
     con.propagate_cols_up(cols, 'sites', 'samples')
Exemple #17
0
 def test_SUFAR4_succeed_data_model3(self):
     input_dir = os.path.join(WD, 'data_files', 'Measurement_Import',
                              'SUFAR_asc_magic')
     infile = 'sufar4-asc_magic_example.txt'
     program_ran, outfile = ipmag.SUFAR4_magic(infile,
                                               input_dir_path=input_dir)
     self.assertTrue(program_ran)
     self.assertEqual(outfile, os.path.join('.', 'measurements.txt'))
     with open(outfile, 'r') as ofile:
         lines = ofile.readlines()
         self.assertEqual(292, len(lines))
         self.assertEqual('measurements', lines[0].split('\t')[1].strip())
     con = nb.Contribution(WD)
     self.assertEqual(
         sorted(con.tables),
         sorted(['measurements', 'specimens', 'samples', 'sites']))
Exemple #18
0
 def test3_with_contribution(self):
     dir_path = os.path.join(WD, 'data_files', '3_0', 'Megiddo')
     con = nb.Contribution(directory=dir_path)
     outfile, error_message, errors, all_errors = ipmag.upload_magic3(
         contribution=con)
     msg = "file validation has failed.  You may run into problems if you try to upload this file."
     self.assertEqual(error_message, msg)
     # delete any upload file that was partially created
     import re
     pattern = re.compile('\w*[.]\w*[.]\w*[20]\d{2}\w*.txt$')
     possible_files = os.listdir(dir_path)
     files = []
     for f in possible_files:
         if pattern.match(f):
             files.append(f)
     pmag.remove_files(files, dir_path)
Exemple #19
0
 def test_init_empty(self):
     tables = [
         'measurements', 'specimens', 'samples', 'sites', 'locations',
         'ages', 'criteria', 'contribution'
     ]
     files = os.listdir(WD)
     for table in tables:
         fname = table + ".txt"
         if table in files:
             try:
                 os.remove(os.path.join(WD, fname))
             except OSError:
                 print(
                     "error when removing files for empty directory test in test_new_builder"
                 )
     con = nb.Contribution(WD, dmodel=DMODEL)
     self.assertEqual(0, len(con.tables))
    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.panel = wx.Panel(self)
        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 = nb.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 = nb.MagicDataFrame(
                    dtype=table, dmodel=self.contribution.data_model)
                self.contribution.tables[table] = new_table

        self.SetTitle("Earth-Ref Magic Builder")
        self.InitUI()
Exemple #21
0
 def test_propagate_cols_up_old(self):
     directory = os.path.join(WD, 'data_files', '3_0', 'McMurdo')
     con = nb.Contribution(directory,
                           dmodel=DMODEL,
                           read_tables=['sites', 'samples'])
     con.tables['sites'].df.loc[:, 'lithologies'] = None
     con.tables['sites'].df.loc[:, 'geologic_types'] = 'your type'
     con.tables['samples'].df.loc[:, 'geologic_types'] = 'my_type'
     con.propagate_cols(['lithologies', 'geologic_types'],
                        'sites',
                        'samples',
                        down=False)
     self.assertEqual(
         'Basalt', con.tables['sites'].get_first_non_null_value(
             'mc50', 'lithologies'))
     self.assertEqual(
         'your type', con.tables['sites'].get_first_non_null_value(
             'mc50', 'geologic_types'))
Exemple #22
0
    def on_convert_3(self, event):
        # turn files from 2.5 --> 3.0 (rough translation)
        res = pmag.convert_directory_2_to_3('magic_measurements.txt',
                                            input_dir=self.WD,
                                            output_dir=self.WD)
        if not res:
            wx.MessageBox(
                '2.5 --> 3.0 failed. Do you have a magic_measurements.txt file in your working directory?',
                'Info', wx.OK | wx.ICON_INFORMATION)
            return

        # create a contribution
        self.contribution = nb.Contribution(self.WD)
        # make skeleton files with specimen, sample, site, location data
        self.contribution.propagate_measurement_info()
        # pop up
        wx.MessageBox('2.5 --> 3.0 translation completed!', 'Info',
                      wx.OK | wx.ICON_INFORMATION)
Exemple #23
0
    def on_convert_3(self, event):
        # turn files from 2.5 --> 3.0 (rough translation)
        meas, upgraded, no_upgrade = pmag.convert_directory_2_to_3(
            'magic_measurements.txt',
            input_dir=self.WD,
            output_dir=self.WD,
            data_model=self.contribution.data_model)
        if not meas:
            wx.MessageBox(
                '2.5 --> 3.0 failed. Do you have a magic_measurements.txt file in your working directory?',
                'Info', wx.OK | wx.ICON_INFORMATION)
            return

        # create a contribution
        self.contribution = nb.Contribution(self.WD)
        # make skeleton files with specimen, sample, site, location data
        self.contribution.propagate_measurement_info()
        #
        # note what DIDN'T upgrade
        #no_upgrade = []
        #for fname in os.listdir(self.WD):
        #    if 'rmag' in fname:
        #        no_upgrade.append(fname)
        #    elif fname in ['pmag_results.txt', 'pmag_criteria.txt',
        #                   'er_synthetics.txt', 'er_images.txt',
        #                   'er_plots.txt', 'er_ages.txt']:
        #        no_upgrade.append(fname)

        # pop up
        upgraded_string = ", ".join(upgraded)
        if no_upgrade:
            no_upgrade_string = ", ".join(no_upgrade)
            msg = '2.5 --> 3.0 translation completed!\n\nThese 3.0 format files were created: {}.\n\nHowever, these 2.5 format files could not be upgraded: {}.\n\nTo convert all 2.5 files, use the MagIC upgrade tool: https://www2.earthref.org/MagIC/upgrade\n'.format(
                upgraded_string, no_upgrade_string)
            if 'criteria.txt' in upgraded:
                msg += '\nNote: Please check your criteria file for completeness and accuracy, as not all 2.5 files will be fully upgraded.'
            if 'pmag_criteria.txt' in no_upgrade:
                msg += '\nNote: Not all criteria files can be upgraded, even on the MagIC site.  You may need to recreate an old pmag_criteria file from scratch in Thellier GUI or Demag GUI.'
            wx.MessageBox(msg, 'Warning', wx.OK | wx.ICON_INFORMATION)
        else:
            msg = '2.5 --> 3.0 translation completed!\nThese files were converted: {}'.format(
                upgraded_string)
            wx.MessageBox(msg, 'Info', wx.OK | wx.ICON_INFORMATION)
Exemple #24
0
    def on_upload_file(self, event):
        if not hasattr(self, "contribution"):
            self.contribution = nb.Contribution(self.WD, dmodel=self.data_model)
        wait = wx.BusyInfo('Validating data, please wait...')
        wx.Yield()
        res, error_message, has_problems, all_failing_items = ipmag.upload_magic3(dir_path=self.WD,
                                                                                  vocab=self.contribution.vocab)
        self.failing_items = all_failing_items
        if has_problems:
            self.highlight_problems(has_problems)
        if not has_problems:
            self.validation_mode = set()
            self.message.SetLabel('Validated!')
            self.bSizer_msg.ShowItems(False)
            self.hbox.Fit(self)
            # do alert that your file passed
            dlg = wx.MessageDialog(self,caption="Message:", message="Your contribution has passed validations!\nGo to https://www.earthref.org/MagIC to upload:\n{}".format(res), style=wx.OK)
            dlg.ShowModal()

        del wait
Exemple #25
0
    def get_wd_data(self):
        self.edited = False
        self.validation_mode = False
        self.reset_highlights()

        wait = wx.BusyInfo('Reading in data from current working directory, please wait...')
        wx.Yield()
        print('-I- Read in any available data from working directory')
        self.contribution = nb.Contribution(self.WD, dmodel=self.data_model)
        # propagate names from measurements into other tables
        if "measurements" in self.contribution.tables:
            self.contribution.propagate_measurement_info()
        # propagate names from any table into other tables
        # (i.e., site name from samples)
        self.contribution.propagate_all_tables_info()
        # extract average lats/lons from sites table
        self.contribution.get_min_max_lat_lon()
        # extract age info from ages table and put into other tables
        self.contribution.propagate_ages()
        # finish up
        self.edited = False
        del wait
Exemple #26
0
    def on_unpack(self, event):
        dlg = wx.FileDialog(
            None, message = "choose txt file to unpack",
            defaultDir=self.WD,
            defaultFile="",
            style=wx.FD_OPEN #| wx.FD_CHANGE_DIR
            )
        if dlg.ShowModal() == wx.ID_OK:
            FILE = dlg.GetPath()
            input_dir, f = os.path.split(FILE)
        else:
            return False

        outstring="download_magic.py -f {} -WD {} -ID {} -DM {}".format(f, self.WD, input_dir, self.data_model_num)

        # run as module:
        print("-I- running python script:\n %s"%(outstring))
        wait = wx.BusyInfo("Please wait, working...")
        wx.Yield()
        ex = None
        try:
            if ipmag.download_magic(f, self.WD, input_dir, overwrite=True, data_model=self.data_model):
                text = "Successfully ran download_magic.py program.\nMagIC files were saved in your working directory.\nSee Terminal/message window for details."
            else:
                text = "Something went wrong.  Make sure you chose a valid file downloaded from the MagIC database and try again."

        except Exception as ex:
            text = "Something went wrong.  Make sure you chose a valid file downloaded from the MagIC database and try again."
            del wait
            dlg = wx.MessageDialog(self, caption="Saved", message=text, style=wx.OK)
            result = dlg.ShowModal()
            if result == wx.ID_OK:
                dlg.Destroy()
            if ex:
                raise(ex)
        self.contribution = nb.Contribution(self.WD)
Exemple #27
0
def main():
    """
    NAME
        quick_hyst.py

    DESCRIPTION
        makes plots of hysteresis data

    SYNTAX
        quick_hyst.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f: specify input file, default is measurements.txt
        -spc SPEC: specify specimen name to plot and quit
        -sav save all plots and quit
        -fmt [png,svg,eps,jpg]
    """
    args = sys.argv
    if "-h" in args:
        print(main.__doc__)
        sys.exit()
    plots = 0
    pltspec = ""
    verbose = pmagplotlib.verbose
    #version_num = pmag.get_version()
    dir_path = pmag.get_named_arg_from_sys('-WD', '.')
    dir_path = os.path.realpath(dir_path)
    meas_file = pmag.get_named_arg_from_sys('-f', 'measurements.txt')
    fmt = pmag.get_named_arg_from_sys('-fmt', 'png')
    if '-sav' in args:
        verbose = 0
        plots = 1
    if '-spc' in args:
        ind = args.index("-spc")
        pltspec = args[ind + 1]
        verbose = 0
        plots = 1
    #
    con = nb.Contribution(dir_path,
                          read_tables=['measurements'],
                          custom_filenames={'measurements': meas_file})
    # get as much name data as possible (used for naming plots)
    if not 'measurements' in con.tables:
        print("-W- No measurement file found")
        return
    con.propagate_location_to_measurements()

    if 'measurements' not in con.tables:
        print(main.__doc__)
        print('bad file')
        sys.exit()
    meas_container = con.tables['measurements']
    #meas_df = meas_container.df

    #
    # initialize some variables
    # define figure numbers for hyst,deltaM,DdeltaM curves
    HystRecs = []
    HDD = {}
    HDD['hyst'] = 1
    pmagplotlib.plot_init(HDD['hyst'], 5, 5)
    #
    # get list of unique experiment names and specimen names
    #
    sids = []
    hyst_data = meas_container.get_records_for_code('LP-HYS')
    #experiment_names = hyst_data['experiment_name'].unique()
    if not len(hyst_data):
        print("-W- No hysteresis data found")
        return
    sids = hyst_data['specimen'].unique()

    # if 'treat_temp' is provided, use that value, otherwise assume 300
    hyst_data['treat_temp'].where(hyst_data['treat_temp'].notnull(),
                                  '300',
                                  inplace=True)
    # start at first specimen, or at provided specimen ('-spc')
    k = 0
    if pltspec != "":
        try:
            print(sids)
            k = list(sids).index(pltspec)
        except ValueError:
            print('-W- No specimen named: {}.'.format(pltspec))
            print('-W- Please provide a valid specimen name')
            return
    intlist = ['magn_moment', 'magn_volume', 'magn_mass']

    while k < len(sids):
        locname, site, sample, synth = '', '', '', ''
        s = sids[k]
        if verbose:
            print(s, k + 1, 'out of ', len(sids))
        # B, M for hysteresis, Bdcd,Mdcd for irm-dcd data
        B, M = [], []
        # get all measurements for this specimen
        spec = hyst_data[hyst_data['specimen'] == s]
        # get names
        if 'location' in spec:
            locname = spec['location'][0]
        if 'site' in spec:
            site = spec['sample'][0]
        if 'sample' in spec:
            sample = spec['sample'][0]

        # get all records with non-blank values in any intlist column
        # find intensity data
        for int_column in intlist:
            if int_column in spec.columns:
                int_col = int_column
                break
        meas_data = spec[spec[int_column].notnull()]
        if len(meas_data) == 0:
            break
        #
        c = ['k-', 'b-', 'c-', 'g-', 'm-', 'r-', 'y-']
        cnum = 0
        Temps = []
        xlab, ylab, title = '', '', ''
        Temps = meas_data['treat_temp'].unique()
        for t in Temps:
            print('working on t: ', t)
            t_data = meas_data[meas_data['treat_temp'] == t]
            m = int_col
            B = t_data['meas_field_dc'].astype(float).values
            M = t_data[m].astype(float).values
            # now plot the hysteresis curve(s)
            #
            if len(B) > 0:
                B = numpy.array(B)
                M = numpy.array(M)
                if t == Temps[-1]:
                    xlab = 'Field (T)'
                    ylab = m
                    title = 'Hysteresis: ' + s
                if t == Temps[0]:
                    pmagplotlib.clearFIG(HDD['hyst'])
                pmagplotlib.plotXY(HDD['hyst'],
                                   B,
                                   M,
                                   sym=c[cnum],
                                   xlab=xlab,
                                   ylab=ylab,
                                   title=title)
                pmagplotlib.plotXY(HDD['hyst'], [1.1 * B.min(), 1.1 * B.max()],
                                   [0, 0],
                                   sym='k-',
                                   xlab=xlab,
                                   ylab=ylab,
                                   title=title)
                pmagplotlib.plotXY(HDD['hyst'], [0, 0],
                                   [1.1 * M.min(), 1.1 * M.max()],
                                   sym='k-',
                                   xlab=xlab,
                                   ylab=ylab,
                                   title=title)
                if verbose:
                    pmagplotlib.drawFIGS(HDD)
                cnum += 1
                if cnum == len(c):
                    cnum = 0

#
        files = {}
        if plots:
            if pltspec != "":
                s = pltspec
            for key in list(HDD.keys()):
                if pmagplotlib.isServer:
                    if synth == '':
                        files[
                            key] = "LO:_" + locname + '_SI:_' + site + '_SA:_' + sample + '_SP:_' + s + '_TY:_' + key + '_.' + fmt
                    else:
                        files[
                            key] = 'SY:_' + synth + '_TY:_' + key + '_.' + fmt
                else:
                    if synth == '':
                        filename = ''
                        for item in [locname, site, sample, s, key]:
                            if item:
                                item = item.replace(' ', '_')
                                filename += item + '_'
                        if filename.endswith('_'):
                            filename = filename[:-1]
                        filename += ".{}".format(fmt)
                        files[key] = filename
                    else:
                        files[key] = "{}_{}.{}".format(synth, key, fmt)

            pmagplotlib.saveP(HDD, files)
            if pltspec != "":
                sys.exit()
        if verbose:
            pmagplotlib.drawFIGS(HDD)
            ans = input(
                "S[a]ve plots, [s]pecimen name, [q]uit, <return> to continue\n "
            )
            if ans == "a":
                files = {}
                for key in list(HDD.keys()):
                    if pmagplotlib.isServer:  # use server plot naming convention
                        files[
                            key] = "LO:_" + locname + '_SI:_' + site + '_SA:_' + sample + '_SP:_' + s + '_TY:_' + key + '_.' + fmt
                    else:  # use more readable plot naming convention
                        filename = ''
                        for item in [locname, site, sample, s, key]:
                            if item:
                                item = item.replace(' ', '_')
                                filename += item + '_'
                        if filename.endswith('_'):
                            filename = filename[:-1]
                        filename += ".{}".format(fmt)
                        files[key] = filename

                pmagplotlib.saveP(HDD, files)
            if ans == '':
                k += 1
            if ans == "p":
                del HystRecs[-1]
                k -= 1
            if ans == 'q':
                print("Good bye")
                sys.exit()
            if ans == 's':
                keepon = 1
                specimen = input(
                    'Enter desired specimen name (or first part there of): ')
                while keepon == 1:
                    try:
                        k = sids.index(specimen)
                        keepon = 0
                    except:
                        tmplist = []
                        for qq in range(len(sids)):
                            if specimen in sids[qq]:
                                tmplist.append(sids[qq])
                        print(specimen, " not found, but this was: ")
                        print(tmplist)
                        specimen = input('Select one or try again\n ')
                        k = sids.index(specimen)
        else:
            k += 1
        if len(B) == 0:
            if verbose:
                print('skipping this one - no hysteresis data')
            k += 1
Exemple #28
0
def main():
    """
    NAME
        zeq_magic.py

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

    SYNTAX
        zeq_magic.py [command line options]

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

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

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

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

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

    DESCRIPTION
       makes equal area projections from declination/inclination data

    SYNTAX
        eqarea_magic.py [command line options]

    INPUT
       takes magic formatted sites, samples, specimens, or measurements

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input magic format file from magic, default='sites.txt'
         supported types=[measurements, specimens, samples, sites]
        -fsp FILE: specify specimen file name, (required if you want to plot measurements by sample)
                default='specimens.txt'
        -fsa FILE: specify sample file name, (required if you want to plot specimens by site)
                default='samples.txt'
        -fsi FILE: specify site file name, default='sites.txt'

        -obj OBJ: specify  level of plot  [all, sit, sam, spc], default is all
        -crd [s,g,t]: specify coordinate system, [s]pecimen, [g]eographic, [t]ilt adjusted
                default is geographic, unspecified assumed geographic
        -fmt [svg,png,jpg] format for output plots
        -ell [F,K,B,Be,Bv] plot Fisher, Kent, Bingham, Bootstrap ellipses or Boostrap eigenvectors
        -c plot as colour contour
        -sav save plot and quit quietly
    NOTE
        all: entire file; sit: site; sam: sample; spc: specimen
    """
    # initialize some default variables
    FIG = {} # plot dictionary
    FIG['eqarea'] = 1 # eqarea is figure 1
    plotE = 0
    plt = 0  # default to not plotting
    verbose = pmagplotlib.verbose
    # extract arguments from sys.argv
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    dir_path = pmag.get_named_arg_from_sys("-WD", default_val=".")
    pmagplotlib.plot_init(FIG['eqarea'],5,5)
    in_file = pmag.get_named_arg_from_sys("-f", default_val="sites.txt")
    in_file = pmag.resolve_file_name(in_file, dir_path)
    if "-WD" not in sys.argv:
        dir_path = os.path.split(in_file)[0]
    #full_in_file = os.path.join(dir_path, in_file)
    plot_by = pmag.get_named_arg_from_sys("-obj", default_val="all").lower()
    spec_file = pmag.get_named_arg_from_sys("-fsp", default_val="specimens.txt")
    samp_file = pmag.get_named_arg_from_sys("-fsa", default_val="samples.txt")
    site_file = pmag.get_named_arg_from_sys("-fsi", default_val="sites.txt")
    if plot_by == 'all':
        plot_key = 'all'
    elif plot_by == 'sit':
        plot_key = 'site'
    elif plot_by == 'sam':
        plot_key = 'sample'
    elif plot_by == 'spc':
        plot_key = 'specimen'
    else:
        plot_by = 'all'
        plot_key = 'all'
    if '-c' in sys.argv:
        contour = 1
    else:
        contour = 0
    if '-sav' in sys.argv:
        plt = 1
        verbose = 0
    if '-ell' in sys.argv:
        plotE = 1
        ind = sys.argv.index('-ell')
        ell_type = sys.argv[ind+1]
        ell_type = pmag.get_named_arg_from_sys("-ell", "F")
        dist = ell_type.upper()
        # if dist type is unrecognized, use Fisher
        if dist not in ['F', 'K', 'B', 'BE', 'BV']:
            dist = 'F'
        if dist == "BV":
            FIG['bdirs'] = 2
            pmagplotlib.plot_init(FIG['bdirs'],5,5)
    crd = pmag.get_named_arg_from_sys("-crd", default_val="g")
    if crd == "s":
        coord = "-1"
    elif crd == "t":
        coord = "100"
    else:
        coord = "0"

    fmt = pmag.get_named_arg_from_sys("-fmt", "svg")

    dec_key = 'dir_dec'
    inc_key = 'dir_inc'
    tilt_key = 'dir_tilt_correction'
    #Dir_type_keys=['','site_direction_type','sample_direction_type','specimen_direction_type']

    #
    fnames = {"specimens": spec_file, "samples": samp_file, 'sites': site_file}
    contribution = nb.Contribution(dir_path, custom_filenames=fnames,
                                   single_file=in_file)

    try:
        contribution.propagate_location_to_samples()
        contribution.propagate_location_to_specimens()
        contribution.propagate_location_to_measurements()
    except KeyError as ex:
        pass

    # the object that contains the DataFrame + useful helper methods:
    table_name = list(contribution.tables.keys())[0]
    data_container = contribution.tables[table_name]
    # the actual DataFrame:
    data = data_container.df

    if plot_key != "all" and plot_key not in data.columns:
        print("-E- You can't plot by {} with the data provided".format(plot_key))
        return

    # add tilt key into DataFrame columns if it isn't there already
    if tilt_key not in data.columns:
        data.loc[:, tilt_key] = None

    if verbose:
        print(len(data), ' records read from ', in_file)

    # find desired dec,inc data:
    dir_type_key = ''
    #
    # get plotlist if not plotting all records
    #
    plotlist=[]
    if plot_key != "all":
        # return all where plot_key is not blank
        if plot_key not in data.columns:
            print('Can\'t plot by "{}".  That header is not in infile: {}'.format(plot_key, in_file))
            return
        plots = data[data[plot_key].notnull()]
        plotlist = plots[plot_key].unique() # grab unique values
    else:
        plotlist.append('All')

    for plot in plotlist:
        if verbose:
            print(plot)
        if plot == 'All':
            # plot everything at once
            plot_data = data
        else:
            # pull out only partial data
            plot_data = data[data[plot_key] == plot]

        DIblock = []
        GCblock = []
        # SLblock, SPblock = [], []
        title = plot
        mode = 1
        k = 0


        if dec_key not in plot_data.columns:
            print("-W- No dec/inc data")
            continue
        # get all records where dec & inc values exist
        plot_data = plot_data[plot_data[dec_key].notnull() & plot_data[inc_key].notnull()]
        if plot_data.empty:
            continue
        # this sorting out is done in get_di_bock
        #if coord == '0':  # geographic, use records with no tilt key (or tilt_key 0)
        #    cond1 = plot_data[tilt_key].fillna('') == coord
        #    cond2 = plot_data[tilt_key].isnull()
        #    plot_data = plot_data[cond1 | cond2]
        #else:  # not geographic coordinates, use only records with correct tilt_key
        #    plot_data = plot_data[plot_data[tilt_key] == coord]

        # get metadata for naming the plot file
        locations = data_container.get_name('location', df_slice=plot_data)
        site = data_container.get_name('site', df_slice=plot_data)
        sample = data_container.get_name('sample', df_slice=plot_data)
        specimen = data_container.get_name('specimen', df_slice=plot_data)

        # make sure method_codes is in plot_data
        if 'method_codes' not in plot_data.columns:
            plot_data['method_codes'] = ''

        # get data blocks
        DIblock = data_container.get_di_block(df_slice=plot_data,
                                              tilt_corr=coord, excl=['DE-BFP'])
        #SLblock = [[ind, row['method_codes']] for ind, row in plot_data.iterrows()]
        # get great circles
        great_circle_data = data_container.get_records_for_code('DE-BFP', incl=True,
                                                                use_slice=True, sli=plot_data)

        if len(great_circle_data) > 0:
            gc_cond = great_circle_data[tilt_key] == coord
            GCblock = [[float(row[dec_key]), float(row[inc_key])] for ind, row in great_circle_data[gc_cond].iterrows()]
            #SPblock = [[ind, row['method_codes']] for ind, row in great_circle_data[gc_cond].iterrows()]

        if len(DIblock) > 0:
            if contour == 0:
                pmagplotlib.plotEQ(FIG['eqarea'], DIblock, title)
            else:
                pmagplotlib.plotEQcont(FIG['eqarea'], DIblock)
        else:
            pmagplotlib.plotNET(FIG['eqarea'])
        if len(GCblock)>0:
            for rec in GCblock:
                pmagplotlib.plotC(FIG['eqarea'], rec, 90., 'g')
        if len(DIblock) == 0 and len(GCblock) == 0:
            if verbose:
                print("no records for plotting")
            continue
            #sys.exit()
        if plotE == 1:
            ppars = pmag.doprinc(DIblock) # get principal directions
            nDIs, rDIs, npars, rpars = [], [], [], []
            for rec in DIblock:
                angle=pmag.angle([rec[0],rec[1]],[ppars['dec'],ppars['inc']])
                if angle>90.:
                    rDIs.append(rec)
                else:
                    nDIs.append(rec)
            if dist=='B': # do on whole dataset
                etitle="Bingham confidence ellipse"
                bpars=pmag.dobingham(DIblock)
                for key in list(bpars.keys()):
                    if key!='n' and verbose: print("    ",key, '%7.1f'%(bpars[key]))
                    if key=='n' and verbose: print("    ",key, '       %i'%(bpars[key]))
                npars.append(bpars['dec'])
                npars.append(bpars['inc'])
                npars.append(bpars['Zeta'])
                npars.append(bpars['Zdec'])
                npars.append(bpars['Zinc'])
                npars.append(bpars['Eta'])
                npars.append(bpars['Edec'])
                npars.append(bpars['Einc'])
            if dist=='F':
                etitle="Fisher confidence cone"
                if len(nDIs)>2:
                    fpars=pmag.fisher_mean(nDIs)
                    for key in list(fpars.keys()):
                        if key!='n' and verbose: print("    ",key, '%7.1f'%(fpars[key]))
                        if key=='n' and verbose: print("    ",key, '       %i'%(fpars[key]))
                    mode+=1
                    npars.append(fpars['dec'])
                    npars.append(fpars['inc'])
                    npars.append(fpars['alpha95']) # Beta
                    npars.append(fpars['dec'])
                    isign=old_div(abs(fpars['inc']),fpars['inc'])
                    npars.append(fpars['inc']-isign*90.) #Beta inc
                    npars.append(fpars['alpha95']) # gamma
                    npars.append(fpars['dec']+90.) # Beta dec
                    npars.append(0.) #Beta inc
                if len(rDIs)>2:
                    fpars=pmag.fisher_mean(rDIs)
                    if verbose: print("mode ",mode)
                    for key in list(fpars.keys()):
                        if key!='n' and verbose: print("    ",key, '%7.1f'%(fpars[key]))
                        if key=='n' and verbose: print("    ",key, '       %i'%(fpars[key]))
                    mode+=1
                    rpars.append(fpars['dec'])
                    rpars.append(fpars['inc'])
                    rpars.append(fpars['alpha95']) # Beta
                    rpars.append(fpars['dec'])
                    isign=old_div(abs(fpars['inc']),fpars['inc'])
                    rpars.append(fpars['inc']-isign*90.) #Beta inc
                    rpars.append(fpars['alpha95']) # gamma
                    rpars.append(fpars['dec']+90.) # Beta dec
                    rpars.append(0.) #Beta inc
            if dist=='K':
                etitle="Kent confidence ellipse"
                if len(nDIs)>3:
                    kpars=pmag.dokent(nDIs,len(nDIs))
                    if verbose: print("mode ",mode)
                    for key in list(kpars.keys()):
                        if key!='n' and verbose: print("    ",key, '%7.1f'%(kpars[key]))
                        if key=='n' and verbose: print("    ",key, '       %i'%(kpars[key]))
                    mode+=1
                    npars.append(kpars['dec'])
                    npars.append(kpars['inc'])
                    npars.append(kpars['Zeta'])
                    npars.append(kpars['Zdec'])
                    npars.append(kpars['Zinc'])
                    npars.append(kpars['Eta'])
                    npars.append(kpars['Edec'])
                    npars.append(kpars['Einc'])
                if len(rDIs)>3:
                    kpars=pmag.dokent(rDIs,len(rDIs))
                    if verbose: print("mode ",mode)
                    for key in list(kpars.keys()):
                        if key!='n' and verbose: print("    ",key, '%7.1f'%(kpars[key]))
                        if key=='n' and verbose: print("    ",key, '       %i'%(kpars[key]))
                    mode+=1
                    rpars.append(kpars['dec'])
                    rpars.append(kpars['inc'])
                    rpars.append(kpars['Zeta'])
                    rpars.append(kpars['Zdec'])
                    rpars.append(kpars['Zinc'])
                    rpars.append(kpars['Eta'])
                    rpars.append(kpars['Edec'])
                    rpars.append(kpars['Einc'])
            else: # assume bootstrap
                if dist=='BE':
                    if len(nDIs)>5:
                        BnDIs=pmag.di_boot(nDIs)
                        Bkpars=pmag.dokent(BnDIs,1.)
                        if verbose: print("mode ",mode)
                        for key in list(Bkpars.keys()):
                            if key!='n' and verbose: print("    ",key, '%7.1f'%(Bkpars[key]))
                            if key=='n' and verbose: print("    ",key, '       %i'%(Bkpars[key]))
                        mode+=1
                        npars.append(Bkpars['dec'])
                        npars.append(Bkpars['inc'])
                        npars.append(Bkpars['Zeta'])
                        npars.append(Bkpars['Zdec'])
                        npars.append(Bkpars['Zinc'])
                        npars.append(Bkpars['Eta'])
                        npars.append(Bkpars['Edec'])
                        npars.append(Bkpars['Einc'])
                    if len(rDIs)>5:
                        BrDIs=pmag.di_boot(rDIs)
                        Bkpars=pmag.dokent(BrDIs,1.)
                        if verbose: print("mode ",mode)
                        for key in list(Bkpars.keys()):
                            if key!='n' and verbose: print("    ",key, '%7.1f'%(Bkpars[key]))
                            if key=='n' and verbose: print("    ",key, '       %i'%(Bkpars[key]))
                        mode+=1
                        rpars.append(Bkpars['dec'])
                        rpars.append(Bkpars['inc'])
                        rpars.append(Bkpars['Zeta'])
                        rpars.append(Bkpars['Zdec'])
                        rpars.append(Bkpars['Zinc'])
                        rpars.append(Bkpars['Eta'])
                        rpars.append(Bkpars['Edec'])
                        rpars.append(Bkpars['Einc'])
                    etitle="Bootstrapped confidence ellipse"
                elif dist=='BV':
                    sym={'lower':['o','c'],'upper':['o','g'],'size':3,'edgecolor':'face'}
                    if len(nDIs)>5:
                        BnDIs=pmag.di_boot(nDIs)
                        pmagplotlib.plotEQsym(FIG['bdirs'],BnDIs,'Bootstrapped Eigenvectors', sym)
                    if len(rDIs)>5:
                        BrDIs=pmag.di_boot(rDIs)
                        if len(nDIs)>5:  # plot on existing plots
                            pmagplotlib.plotDIsym(FIG['bdirs'],BrDIs,sym)
                        else:
                            pmagplotlib.plotEQ(FIG['bdirs'],BrDIs,'Bootstrapped Eigenvectors')
            if dist=='B':
                if len(nDIs)> 3 or len(rDIs)>3: pmagplotlib.plotCONF(FIG['eqarea'],etitle,[],npars,0)
            elif len(nDIs)>3 and dist!='BV':
                pmagplotlib.plotCONF(FIG['eqarea'],etitle,[],npars,0)
                if len(rDIs)>3:
                    pmagplotlib.plotCONF(FIG['eqarea'],etitle,[],rpars,0)
            elif len(rDIs)>3 and dist!='BV':
                pmagplotlib.plotCONF(FIG['eqarea'],etitle,[],rpars,0)

        for key in list(FIG.keys()):
            files = {}
            filename = pmag.get_named_arg_from_sys('-fname')
            if filename: # use provided filename
                filename+= '.' + fmt
            elif pmagplotlib.isServer: # use server plot naming convention
                filename='LO:_'+locations+'_SI:_'+site+'_SA:_'+sample+'_SP:_'+specimen+'_CO:_'+crd+'_TY:_'+key+'_.'+fmt
            elif plot_key == 'all':
                filename = 'all'
                if 'location' in plot_data.columns:
                    locs = plot_data['location'].unique()
                    loc_string = "_".join([loc.replace(' ', '_') for loc in locs])
                    filename += "_" + loc_string
                filename += "_" + crd + "_" + key
                filename += ".{}".format(fmt)
            else: # use more readable naming convention
                filename = ''
                # fix this if plot_by is location , for example
                use_names = {'location': [locations], 'site': [locations, site],
                             'sample': [locations, site, sample],
                             'specimen': [locations, site, sample, specimen]}
                use = use_names[plot_key]
                use.extend([crd, key])
                for item in use: #[locations, site, sample, specimen, crd, key]:
                    if item:
                        item = item.replace(' ', '_')
                        filename += item + '_'
                if filename.endswith('_'):
                    filename = filename[:-1]
                filename += ".{}".format(fmt)

            files[key]=filename

        if pmagplotlib.isServer:
            black     = '#000000'
            purple    = '#800080'
            titles={}
            titles['eq']='Equal Area Plot'
            FIG = pmagplotlib.addBorders(FIG,titles,black,purple)
            pmagplotlib.saveP(FIG,files)

        if plt:
            pmagplotlib.saveP(FIG,files)
            continue
        if verbose:
            pmagplotlib.drawFIGS(FIG)
            ans=input(" S[a]ve to save plot, [q]uit, Return to continue:  ")
            if ans == "q":
                sys.exit()
            if ans == "a":
                pmagplotlib.saveP(FIG,files)
        continue
def convert(**kwargs):

    # initialize some stuff
    demag = "N"
    version_num = pmag.get_version()

    dir_path = kwargs.get('dir_path', '.')
    input_dir_path = kwargs.get('input_dir_path', dir_path)
    output_dir_path = dir_path
    meas_file = kwargs.get('meas_file', 'measurements.txt')
    spec_file = kwargs.get('spec_file', 'specimens.txt')
    samp_file = kwargs.get('samp_file', 'samples.txt')
    site_file = kwargs.get('site_file', 'sites.txt')
    loc_file = kwargs.get('loc_file', 'locations.txt')
    mag_file = kwargs.get('mag_file', '')
    site = kwargs.get('site', 'unknown')
    expedition = kwargs.get('expedition', 'unknown')
    lat = kwargs.get('lat', '')
    lon = kwargs.get('lon', '')
    noave = kwargs.get('noave', False)  # default means DO average
    meth_code = kwargs.get('meth_code', "LP-NO")
    volume = kwargs.get('volume', 2.5**
                        3) * 1e-6  #default volume is a 2.5cm cube

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

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

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

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

        if specimen != "" and specimen not in [
                x['specimen'] if 'specimen' in list(x.keys()) else ""
                for x in SpecRecs
        ]:
            SpecRec['specimen'] = specimen
            SpecRec['sample'] = sample
            SpecRec['volume'] = volume
            SpecRec['citations'] = citations
            SpecRecs.append(SpecRec)
        if sample != "" and sample not in [
                x['sample'] if 'sample' in list(x.keys()) else ""
                for x in SampRecs
        ]:
            SampRec['sample'] = sample
            SampRec['site'] = site
            SampRec['citations'] = citations
            SampRec['azimuth'] = line[6]
            SampRec['dip'] = line[7]
            SampRec['bed_dip_direction'] = line[8]
            SampRec['bed_dip'] = line[9]
            SampRec['method_codes'] = meth_code
            SampRecs.append(SampRec)
        if site != "" and site not in [
                x['site'] if 'site' in list(x.keys()) else "" for x in SiteRecs
        ]:
            SiteRec['site'] = site
            SiteRec['location'] = location
            SiteRec['citations'] = citations
            SiteRec['lat'] = lat
            SiteRec['lon'] = lon
            SiteRecs.append(SiteRec)
        if location != "" and location not in [
                x['location'] if 'location' in list(x.keys()) else ""
                for x in LocRecs
        ]:
            LocRec['location'] = location
            LocRec['citations'] = citations
            LocRec['expedition_name'] = expedition
            LocRec['lat_n'] = lat
            LocRec['lon_e'] = lon
            LocRec['lat_s'] = lat
            LocRec['lon_w'] = lon
            LocRecs.append(LocRec)

        MeasRec['specimen'] = specimen
        MeasRec["citations"] = citations
        MeasRec['software_packages'] = version_num
        MeasRec["treat_temp"] = '%8.3e' % (273)  # room temp in kelvin
        MeasRec["meas_temp"] = '%8.3e' % (273)  # room temp in kelvin
        MeasRec["quality"] = 'g'
        MeasRec["standard"] = 'u'
        MeasRec["treat_step_num"] = '1'
        MeasRec["treat_ac_field"] = '0'

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

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

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

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

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

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

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

    return (True, meas_file)