def _onSaveCalibFile(self, event): """ Saves a calibration file (*csv) containing the time range and the corresponding trigger delay for streak camera calibration. """ logging.debug("Save trigger delay calibration file for temporal acquisition.") dialog = wx.FileDialog(self.panel, message="Choose a filename and destination to save the calibration file. " "It is advisory to include the SEM voltage into the filename.", defaultDir=self._calib_path, defaultFile="", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT, wildcard="csv files (*.csv)|*.csv") # Show the dialog and check whether is was accepted or cancelled if dialog.ShowModal() != wx.ID_OK: return # get selected path + filename and update default directory self._calib_path = dialog.GetDirectory() path = dialog.GetPath() filename = dialog.GetFilename() # check if filename is provided with the correct extension if os.path.splitext(filename)[1] != ".csv": filename += ".csv" path += ".csv" # get a copy of the triggerDelay dict from MD tr2d = self.streak_delay.getMetadata()[model.MD_TIME_RANGE_TO_DELAY] calibration.write_trigger_delay_csv(path, tr2d) # update txt displayed in GUI self._onUpdateTriggerDelayGUI(filename)
def test_write_read_trigger_delays(self): TRIG_DELAY_FILE = "test-trig-delays.csv" # Make sure the file is not present try: os.remove(TRIG_DELAY_FILE) except Exception: pass # no such file # Write the file calibration.write_trigger_delay_csv(TRIG_DELAY_FILE, TIME_RANGE_TO_DELAY_EX) # Read back tr2d = calibration.read_trigger_delay_csv( TRIG_DELAY_FILE, set(TIME_RANGE_TO_DELAY_EX.keys()), [0, 0.1]) self.assertEqual(TIME_RANGE_TO_DELAY_EX, tr2d) # Read with wrong hardware with self.assertRaises(ValueError): tr2d = calibration.read_trigger_delay_csv( TRIG_DELAY_FILE, {1e-09, 7e-9}, # Wrong time ranges [0, 0.1]) with self.assertRaises(ValueError): tr2d = calibration.read_trigger_delay_csv( TRIG_DELAY_FILE, set(TIME_RANGE_TO_DELAY_EX.keys()), [0, 1e-6]) # Out of delay range # Try overwriting calibration.write_trigger_delay_csv(TRIG_DELAY_FILE, TIME_RANGE_TO_DELAY_EX) os.remove(TRIG_DELAY_FILE)