def save_wave_to_disk(self, waveform=None): """ Called in order to save in-memory waveforms to disk. Parameters: :wave: a particular wave to save, if none is passed then all waves in memory are saved. """ if waveform: try: wave_directory = Config.get('Export', 'waveform_dir') if not os.path.exists(wave_directory): os.makedirs(wave_directory) day_directory = os.path.join(wave_directory, date.today().isoformat()) if not os.path.exists(day_directory): os.makedirs(day_directory) default_file = 'Capture' + datetime.now().strftime('%m-%d-%H-%M-%S') + '.csv' default_file = os.path.join(day_directory, default_file).replace('\\', '/') file_name = QtWidgets.QFileDialog.getSaveFileName(self.main_window, 'Save As', default_file)[0] with WaveformCsvFile(waveform, file_name) as file: file.write() self.logger.info('Waveform saved to ' + file_name) self.update_status('Waveform saved to ' + file_name) except Exception as e: self.logger.error(e) else: wave_count = self.db_session.query(Waveform).count() if wave_count: try: wave_directory = Config.get('Export', 'waveform_dir') if not os.path.exists(wave_directory): os.makedirs(wave_directory) day_directory = os.path.join(wave_directory, date.today().isoformat()) if not os.path.exists(day_directory): os.makedirs(day_directory) default_file = 'Capture' + datetime.now().strftime('%m-%d-%H-%M-%S') + '.csv' default_file = os.path.join(day_directory, default_file).replace('\\', '/') file_name = QtWidgets.QFileDialog.getSaveFileName(self.main_window, 'Save As', default_file)[0] with WaveformCsvFile(self.db_session.query(Waveform), file_name) as file: file.write() self.logger.info("%d waveforms saved to %s", wave_count, file_name) self.update_status('Waveforms saved to ' + file_name) except Exception as e: self.logger.error(e) else: self.update_status('No Waveforms to Save')
def create_new_database_file(identifier): """ Create a new database file for a new data acquisition session. :param identifier: a unique identifier to be prepended to the default file name. :return: a path to the newly created file. """ database_dir = Config.get('Database', 'database_dir') if not os.path.exists(database_dir): os.makedirs(database_dir) if identifier: database_file = identifier + Config.get('Database', 'database_file') else: database_file = Config.get('Database', 'database_file') database_path = os.path.join(database_dir, database_file) open(database_path, 'w').close() return database_path
def load_database(self): """ Connect to an old database file, and load its waves into memory if it is valid. """ try: default_file = Config.get('Database', 'database_dir') database_path = QtWidgets.QFileDialog.getOpenFileName(self.main_window, 'Open', default_file)[0] # Occurs if user hits cancel if database_path is '': return self.update_status('Loading waves from ' + database_path) self.logger.info('Disconnecting from database') # clear old session if self.db_session: self.db_session.close() self.db_session = None # reset GUI self.reset() self.update_status('Loading waves from ' + database_path) self.logger.info('Loading waves from ' + database_path) # make new connection self.database = Database(database_path) if self.database.is_setup: self.db_session = self.database.session() # get waves loaded_waves = self.db_session.query(Waveform).all() if not len(loaded_waves): raise RuntimeError('Database contained no waves.') # display waves to user. [self.wave_column.add_wave(wave) for wave in loaded_waves] try: self.plot_wave(loaded_waves[-1]) except ValueError as e: self.logger.info(e) except Exception as e: self.logger.error(e) self.histogram_options.update_properties(loaded_waves[-1]) self.update_histogram() self.update_status('Wave loading complete.') except Exception as e: self.logger.error(e) self.update_status('Failed to load waves from ' + database_path)
def main(): print("Initializing ScopeOut...") logger = logging.getLogger('ScopeOut') logger.setLevel(logging.DEBUG) # create file handler which logs even debug messages log_dir = Config.get('Logging', 'log_dir') if not os.path.exists(log_dir): os.makedirs(log_dir) log_path = os.path.join(log_dir, Config.get('Logging', 'log_file')) fh = logging.FileHandler(log_path) fh.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setLevel(logging.ERROR) # create formatter and add it to the handlers formatter = logging.Formatter('%(asctime)s - %(name)s:%(lineno)d - %(levelname)s - %(message)s') fh.setFormatter(formatter) ch.setFormatter(formatter) # add the handlers to the logger logger.addHandler(fh) logger.addHandler(ch) logger.info("Initializing ScopeOut...") app = ThreadedClient(sys.argv) logger.info("ScopeOut initialization completed") # Enable keyboard shortcuts to kill from command line signal.signal(signal.SIGINT, signal.SIG_DFL) return app.exec_()
def make_properties_file(): wave_directory = Config.get('Export', 'waveform_dir') if not os.path.exists(wave_directory): os.makedirs(wave_directory) day_directory = os.path.join(wave_directory, date.today().isoformat()) if not os.path.exists(day_directory): os.makedirs(day_directory) default_file = 'Properties' + datetime.now().strftime('%m-%d-%H-%M-%S') + '.csv' default_file = os.path.join(day_directory, default_file).replace('\\', '/') file_name = QtWidgets.QFileDialog.getSaveFileName(self.main_window, 'Save As', default_file)[0] return file_name
def save_plot_to_disk(self): """ Save the currently displayed plot to disk. """ plot_directory = Config.get('Export', 'plot_dir') if not os.path.exists(plot_directory): os.makedirs(plot_directory) day_directory = os.path.join(plot_directory, date.today().isoformat()) if not os.path.exists(day_directory): os.makedirs(day_directory) default_file = 'Plot' + datetime.now().strftime('%m-%d-%H-%M-%S') + '.png' default_file = os.path.join(day_directory, default_file).replace('\\', '/') file_name = QtWidgets.QFileDialog.getSaveFileName(self.main_window, 'Save As', default_file)[0] if self.plot.save_plot(file_name): self.update_status("Plot saved successfully") else: self.update_status("Error occurred while saving plot. Check log for details.")