def test_all_files_are_created(self): files = ['locations.txt', 'sites.txt', 'samples.txt', 'specimens.txt', 'measurements.txt', 'contribution.txt'] pmag.remove_files(files, self.download_dir) ipmag.download_magic('magic_contribution_16533.txt', dir_path=self.download_dir, input_dir_path=self.download_dir) output_files = os.listdir(self.download_dir) for f in files: self.assertIn(f, output_files)
def main(): """ NAME download_magic.py DESCRIPTION unpacks a magic formatted smartbook .txt file from the MagIC database into the tab delimited MagIC format txt files for use with the MagIC-Py programs. SYNTAX download_magic.py command line options] INPUT takes either the upload.txt file created by upload_magic.py or a file downloaded from the MagIC database (http://earthref.org/MagIC) OPTIONS -h prints help message and quits -i allows interactive entry of filename -f FILE specifies input file name -sep write location data to separate subdirectories (Location_*), (default False) -O do not overwrite duplicate Location_* directories while downloading -DM data model (2 or 3, default 3) """ if '-h' in sys.argv: print(main.__doc__) sys.exit() if '-WD' in sys.argv: ind = sys.argv.index('-WD') dir_path = sys.argv[ind + 1] # interactive entry if '-i' in sys.argv: infile = input("Magic txt file for unpacking? ") dir_path = '.' input_dir_path = '.' # non-interactive else: infile = pmag.get_named_arg("-f", reqd=True) # if -O flag is present, overwrite is False overwrite = pmag.get_flag_arg_from_sys("-O", true=False, false=True) # if -sep flag is present, sep is True sep = pmag.get_flag_arg_from_sys("-sep", true=True, false=False) data_model = pmag.get_named_arg("-DM", default_val=3, reqd=False) dir_path = pmag.get_named_arg("-WD", default_val=".", reqd=False) input_dir_path = pmag.get_named_arg("-ID", default_val=".", reqd=False) #if '-ID' not in sys.argv and '-WD' in sys.argv: # input_dir_path = dir_path if "-WD" not in sys.argv and "-ID" not in sys.argv: input_dir_path = os.path.split(infile)[0] if not input_dir_path: input_dir_path = "." ipmag.download_magic(infile, dir_path, input_dir_path, overwrite, True, data_model, sep)
def main(): """ NAME download_magic.py DESCRIPTION unpacks a magic formatted smartbook .txt file from the MagIC database into the tab delimited MagIC format txt files for use with the MagIC-Py programs. SYNTAX download_magic.py command line options] INPUT takes either the upload.txt file created by upload_magic.py or a file downloaded from the MagIC database (http://earthref.org/MagIC) OPTIONS -h prints help message and quits -i allows interactive entry of filename -f FILE specifies input file name -sep write location data to separate subdirectories (Location_*), (default False) -O do not overwrite duplicate Location_* directories while downloading -DM data model (2 or 3, default 3) """ if '-h' in sys.argv: print(main.__doc__) sys.exit() if '-WD' in sys.argv: ind=sys.argv.index('-WD') dir_path=sys.argv[ind+1] # interactive entry if '-i' in sys.argv: infile=input("Magic txt file for unpacking? ") dir_path = '.' input_dir_path = '.' # non-interactive else: infile = pmag.get_named_arg("-f", reqd=True) # if -O flag is present, overwrite is False overwrite = pmag.get_flag_arg_from_sys("-O", true=False, false=True) # if -sep flag is present, sep is True sep = pmag.get_flag_arg_from_sys("-sep", true=True, false=False) data_model = pmag.get_named_arg("-DM", default_val=3, reqd=False) dir_path = pmag.get_named_arg("-WD", default_val=".", reqd=False) input_dir_path = pmag.get_named_arg("-ID", default_val=".", reqd=False) #if '-ID' not in sys.argv and '-WD' in sys.argv: # input_dir_path = dir_path if "-WD" not in sys.argv and "-ID" not in sys.argv: input_dir_path = os.path.split(infile)[0] if not input_dir_path: input_dir_path = "." ipmag.download_magic(infile, dir_path, input_dir_path, overwrite, True, data_model, sep)
def test_all_files_are_created(self): files = [ 'locations.txt', 'sites.txt', 'samples.txt', 'specimens.txt', 'measurements.txt', 'contribution.txt' ] pmag.remove_files(files, self.download_dir) ipmag.download_magic('magic_contribution_15143.txt', dir_path=self.download_dir, input_dir_path=self.download_dir) output_files = os.listdir(self.download_dir) for f in files: self.assertIn(f, output_files)
def on_unpack(self, event): dlg = wx.FileDialog( None, message = "choose txt file to unpack", defaultDir=self.WD, defaultFile="", style=wx.OPEN #| wx.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 {}".format(f, self.WD, input_dir) # 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): text = "Successfully ran download_magic.py program.\nMagIC files were saved in your working directory.\nSee Terminal/Command Prompt 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)
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 {}".format(f, self.WD, input_dir) # 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): 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)
def test_depth_propagation(self): dir_path = os.path.join(WD, 'data_files', 'core_depthplot') #con = cb.Contribution(dir_path) #self.assertNotIn('core_depth', con.tables['sites'].df.index) #con.propagate_cols(['core_depth'], 'sites', 'samples', down=False) #self.assertIn('core_depth', con.tables['sites'].df.columns) #self.assertEqual(con.tables['sites'].df.loc['15-1-013', 'core_depth'], 55.23) # outfile, error_message, errors, all_errors = ipmag.upload_magic(dir_path=dir_path) print('mv {} {}'.format(outfile, WD)) os.system('mv {} {}'.format(outfile, WD)) outfile = os.path.join(WD, os.path.split(outfile)[1]) ipmag.download_magic(outfile) con = cb.Contribution(WD) self.assertIn('core_depth', con.tables['sites'].df.columns) self.assertEqual(con.tables['sites'].df.loc['15-1-013', 'core_depth'], 55.23)
def test_depth_propagation(self): dir_path = os.path.join(WD, 'data_files', 'core_depthplot') #con = cb.Contribution(dir_path) #self.assertNotIn('core_depth', con.tables['sites'].df.index) #con.propagate_cols(['core_depth'], 'sites', 'samples', down=False) #self.assertIn('core_depth', con.tables['sites'].df.columns) #self.assertEqual(con.tables['sites'].df.loc['15-1-013', 'core_depth'], 55.23) # outfile, error_message, errors, all_errors = ipmag.upload_magic3( dir_path=dir_path) print('mv {} {}'.format(outfile, WD)) os.system('mv {} {}'.format(outfile, WD)) outfile = os.path.join(WD, os.path.split(outfile)[1]) ipmag.download_magic(outfile) con = cb.Contribution(WD) self.assertIn('core_depth', con.tables['sites'].df.columns) self.assertEqual(con.tables['sites'].df.loc['15-1-013', 'core_depth'], 55.23)
def main(): """ NAME download_magic.py DESCRIPTION unpacks a magic formatted smartbook .txt file from the MagIC database into the tab delimited MagIC format txt files for use with the MagIC-Py programs. SYNTAX download_magic.py command line options] INPUT takes either the upload.txt file created by upload_magic.py or the file exported by the MagIC v2.2 console software (downloaded from the MagIC database or output by the Console on your PC). OPTIONS -h prints help message and quits -i allows interactive entry of filename -f FILE specifies input file name -O do not overwrite duplicate Location_* directories while downloading """ if '-h' in sys.argv: print main.__doc__ sys.exit() if '-WD' in sys.argv: ind = sys.argv.index('-WD') dir_path = sys.argv[ind + 1] # interactive entry if '-i' in sys.argv: infile = raw_input("Magic txt file for unpacking? ") dir_path = '.' input_dir_path = '.' # non-interactive else: dataframe = extractor.command_line_dataframe([['O', False, True]]) checked_args = extractor.extract_and_check_args(sys.argv, dataframe) infile, dir_path, input_dir_path, overwrite = extractor.get_vars( ['f', 'WD', 'ID', 'O'], checked_args) data_model = float(pmag.get_named_arg_from_sys("-DM", 2.5)) ipmag.download_magic(infile, dir_path, input_dir_path, overwrite, data_model)
def main(): """ NAME download_magic.py DESCRIPTION unpacks a magic formatted smartbook .txt file from the MagIC database into the tab delimited MagIC format txt files for use with the MagIC-Py programs. SYNTAX download_magic.py command line options] INPUT takes either the upload.txt file created by upload_magic.py or the file exported by the MagIC v2.2 console software (downloaded from the MagIC database or output by the Console on your PC). OPTIONS -h prints help message and quits -i allows interactive entry of filename -f FILE specifies input file name -O do not overwrite duplicate Location_* directories while downloading """ if '-h' in sys.argv: print main.__doc__ sys.exit() if '-WD' in sys.argv: ind=sys.argv.index('-WD') dir_path=sys.argv[ind+1] # interactive entry if '-i' in sys.argv: infile=raw_input("Magic txt file for unpacking? ") dir_path = '.' input_dir_path = '.' # non-interactive else: dataframe = extractor.command_line_dataframe([['O', False, True]]) checked_args = extractor.extract_and_check_args(sys.argv, dataframe) infile, dir_path, input_dir_path, overwrite = extractor.get_vars(['f', 'WD', 'ID', 'O'], checked_args) data_model = float(pmag.get_named_arg_from_sys("-DM", 2.5)) ipmag.download_magic(infile, dir_path, input_dir_path, overwrite, data_model)
def on_btn_unpack(self, event): """ Create dialog to choose a file to unpack with download magic. Then run download_magic and create self.contribution. """ def magic_download_dia(warn=""): dia = pw.TextDialog( self, "Download from MagIC\nusing contribution id or DOI", "MagIC id/DOI", warn) res = dia.ShowModal() magic_id = dia.text_ctrl.return_value() if res == wx.ID_CANCEL: return wx.ID_CANCEL if res == wx.ID_OK: return magic_id else: return False dlg = pw.ChooseOne( self, "Download from MagIC", "Unpack previous downloaded file", text= "You can unpack a downloaded file from MagIC, or download a file from MagIC directly using the contribution id or DOI.", title="") dlg.Centre() res = dlg.ShowModal() # try to download directly from MagIC if res == wx.ID_YES: magic_id = True warning = "" while magic_id: magic_id = magic_download_dia(warning) # if magic id was blank if magic_id == "": warning = "You must provide a MagIC contribution id or DOI" magic_id = True continue # if user canceled the download if magic_id == wx.ID_CANCEL: return # if everything looks good, try to download if len(str(magic_id)) < 8: # use contribution id status, stuff = ipmag.download_magic_from_id(magic_id) f = "magic_contribution_{}.txt".format(magic_id) else: # use DOI status, stuff = ipmag.download_magic_from_doi(magic_id) f = "magic_contribution.txt" if not status: warning = stuff if status: break if not os.path.exists(os.path.join(self.WD, f)): os.rename(os.path.join(os.getcwd(), f), os.path.join(self.WD, f)) input_dir = self.WD # try to unpack a previously downloaded file if res == wx.ID_NO: 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 {}".format( f, self.WD, input_dir) # run as module: print("-I- running python script:\n %s" % (outstring)) wait = wx.BusyInfo("Please wait, working...") wx.SafeYield() 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." return 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) return self.contribution = cb.Contribution(self.WD) # make a success pop-up dlg = wx.MessageDialog( self, caption="Success", message= "You can now add orientation information or metadata, or open one of the analysis tools", style=wx.OK) dlg.ShowModal()