def SaveIqAscii(reducer=None, process=''): """ Old command for backward compatibility """ output_dir = os.path.expanduser('~') msg = "SaveIqAscii is not longer used:\n " msg += "Please use 'SaveIq' instead\n " msg += "Your output files are currently in %s" % output_dir Logger.get("CommandInterface").warning(msg) ReductionSingleton().reduction_properties["OutputDirectory"] = output_dir ReductionSingleton().reduction_properties["ProcessInfo"] = str(process)
def find_data(file, instrument='', allow_multiple=False): """ Finds a file path for the specified data set, which can either be: - a run number - an absolute path - a file name @param file: file name or part of a file name @param instrument: if supplied, FindNeXus will be tried as a last resort """ # First, assume a file name file = str(file).strip() # If we allow multiple files, users may use ; as a separator, # which is incompatible with the FileFinder n_files = 1 if allow_multiple: file=file.replace(';',',') toks = file.split(',') n_files = len(toks) instrument = str(instrument) file_path = FileFinder.getFullPath(file) if os.path.isfile(file_path): return file_path # Second, assume a run number and pass the instrument name as a hint try: # FileFinder doesn't like dashes... instrument=instrument.replace('-','') f = FileFinder.findRuns(instrument+file) if os.path.isfile(f[0]): if allow_multiple: # Mantid returns its own list object type, so make a real list out if it if len(f)==n_files: return [i for i in f] else: return f[0] except: # FileFinder couldn't make sense of the the supplied information pass # Third, assume a run number, without instrument name to take care of list of full paths try: f = FileFinder.findRuns(file) if os.path.isfile(f[0]): if allow_multiple: # Mantid returns its own list object type, so make a real list out if it if len(f)==n_files: return [i for i in f] else: return f[0] except: # FileFinder couldn't make sense of the the supplied information pass # If we didn't find anything, raise an exception Logger.get('find_data').error("\n\nCould not find a file for %s: check your reduction parameters\n\n" % str(file)) raise RuntimeError, "Could not find a file for %s" % str(file)
def __init__(self, name="", facility=""): self.instrument_name = name self.facility_name = facility self._observers = [] self._output_directory = os.path.expanduser('~') if HAS_MANTID: config = ConfigService.Instance() try: head, tail = os.path.split(config.getUserFilename()) if os.path.isdir(head): self._output_directory = head except: Logger.get("scripter").debug("Could not get user filename")
def __init__(self, filepath): if not _os.path.isfile(filepath): raise ValueError("PluginLoader expects a single filename. '%s' does not point to an existing file" % filepath) if not filepath.endswith(self.extension): raise ValueError("PluginLoader expects a filename ending with .py. '%s' does not have a .py extension" % filepath) self._filepath = filepath self._logger = Logger.get("PluginLoader")
def test_logger_creation_does_not_raise_an_error(self): logger = Logger.get("LoggerTest") self.assertTrue(isinstance(logger, Logger)) attrs = ['fatal', 'error','warning','notice', 'information', 'debug'] for att in attrs: if not hasattr(logger, att): self.fail("Logger object does not have the required attribute '%s'" % att)
def __init__(self, data_file, workspace_name=None): self.errors = [] if HAS_MANTID: try: if workspace_name is None: self.data_ws = "__raw_data_file" else: self.data_ws = str(workspace_name) api.HFIRLoad(Filename=str(data_file), OutputWorkspace=self.data_ws) ws = AnalysisDataService.retrieve(self.data_ws) x = ws.dataX(0) self.wavelength = (x[0]+x[1])/2.0 self.wavelength_spread = x[1]-x[0] self.sample_detector_distance = ws.getRun().getProperty("sample_detector_distance").value self.sample_thickness = ws.getRun().getProperty("sample-thickness").value self.beam_diameter = ws.getRun().getProperty("beam-diameter").value Logger.get("hfir_data_proxy").information("Loaded data file: %s" % data_file) except: Logger.get("hfir_data_proxy").error("Error loading data file:\n%s" % sys.exc_value) self.errors.append("Error loading data file:\n%s" % sys.exc_value)
def pre_process(self): """ Reduction steps that are meant to be executed only once per set of data files. After this is executed, all files will go through the list of reduction steps. """ Logger.get("Reducer").information("Setting up reduction options") if self.setup_algorithm is not None: alg = AlgorithmManager.create(self.setup_algorithm) alg.initialize() props = [p.name for p in alg.getProperties()] for key in self.reduction_properties.keys(): if key in props: try: alg.setProperty(key, self.reduction_properties[key]) except: msg = "Error setting %s=%s" % (key, str(self.reduction_properties[key])) msg += "\n %s" % sys.exc_value Logger.get("Reducer").error(msg) else: Logger.get("Reducer").warning("Setup algorithm has no %s property" % key) if "ReductionProperties" in props: alg.setPropertyValue("ReductionProperties", self.get_reduction_table_name()) alg.execute()
def reduce(self): """ Go through the list of reduction steps """ t_0 = time.time() self.output_workspaces = [] # Log text self.log_text = "%s reduction - %s\n" % (self.instrument_name, time.ctime()) self.log_text += "Mantid Python API v2\n" # Go through the list of steps that are common to all data files self.pre_process() if self.reduction_algorithm is None: Logger.get("Reducer").error("A reduction algorithm wasn't set: stopping") return for ws in self._data_files.keys(): alg = AlgorithmManager.create(self.reduction_algorithm) alg.initialize() props = [p.name for p in alg.getProperties()] # Check whether the data is already available or needs to be loaded if self._data_files[ws] is not None: datafile = self._data_files[ws] if type(datafile)==list: datafile=','.join(datafile) if "Filename" in props: alg.setPropertyValue("Filename", datafile) else: msg = "Can't set the Filename property on %s" % self.reduction_algorithm Logger.get("Reducer").error(msg) else: if "InputWorkspace" in props: alg.setPropertyValue("InputWorkspace", ws) else: msg = "Can't set the InputWorkspace property on %s" % self.reduction_algorithm Logger.get("Reducer").error(msg) if "ReductionProperties" in props: alg.setPropertyValue("ReductionProperties", self.get_reduction_table_name()) if "OutputWorkspace" in props: alg.setPropertyValue("OutputWorkspace", ws) alg.execute() if "OutputMessage" in props: self.log_text += alg.getProperty("OutputMessage").value+'\n' #any clean up, possibly removing workspaces self.post_process() # Determine which directory to use output_dir = self._data_path if self._output_path is not None: if os.path.isdir(self._output_path): output_dir = self._output_path else: output_dir = os.path.expanduser('~') self.log_text += "Reduction completed in %g sec\n" % (time.time()-t_0) log_path = os.path.join(output_dir,"%s_reduction.log" % self.instrument_name) self.log_text += "Log saved to %s" % log_path # Write the log to file f = open(log_path, 'a') f.write("\n-------------------------------------------\n") f.write(self.log_text) f.close() return self.log_text