def __init__(self, inst): if isinstance(inst, str): self.instrument_string = inst else: self.instrument_string = SANSInstrument.to_string(inst)
def get_instrument_paths_for_sans_file(file_name): """ Gets the Instrument Definition File (IDF) path and the Instrument Parameter Path (IPF) path associated with a file. :param file_name: the file name is a name fo a SANS data file, e.g. SANS2D0001234 :return: the IDF path and the IPF path """ def get_file_location(path): return os.path.dirname(path) def get_ipf_equivalent_name(path): # If XXX_Definition_Yyy.xml is the IDF name, then the equivalent IPF name is: XXX_Parameters_Yyy.xml base_file_name = os.path.basename(path) return base_file_name.replace("Definition", "Parameters") def get_ipf_standard_name(path): # If XXX_Definition_Yyy.xml is the IDF name, then the standard IPF name is: XXX_Parameters.xml base_file_name = os.path.basename(path) elements = base_file_name.split("_") return elements[0] + "_Parameters.xml" def check_for_files(directory, path): # Check if XXX_Parameters_Yyy.xml exists in the same folder ipf_equivalent_name = get_ipf_equivalent_name(path) ipf_equivalent = os.path.join(directory, ipf_equivalent_name) if os.path.exists(ipf_equivalent): return ipf_equivalent # Check if XXX_Parameters.xml exists in the same folder ipf_standard_name = get_ipf_standard_name(path) ipf_standard = os.path.join(directory, ipf_standard_name) if os.path.exists(ipf_standard): return ipf_standard # Does not seem to be in the folder return None def get_ipf_for_rule_1(path): # Check if can be found in the same folder directory = get_file_location(path) return check_for_files(directory, path) def get_ipf_for_rule_2(path): # Check if can be found in the instrument folder directory = ConfigService.getInstrumentDirectory() return check_for_files(directory, path) # Get the measurement date file_information_factory = SANSFileInformationFactory() file_information = file_information_factory.create_sans_file_information(file_name) measurement_time = file_information.get_date() # For some odd reason the __str__ method of DateAndTime adds a space which we need to strip here. It seems # to be on purpose though since the export method is called IS08601StringPlusSpace --> hence we need to strip it # ourselves measurement_time_as_string = str(measurement_time).strip() # Get the instrument instrument = file_information.get_instrument() instrument_as_string = SANSInstrument.to_string(instrument) # Get the idf file path idf_path = ExperimentInfo.getInstrumentFilename(instrument_as_string, measurement_time_as_string) idf_path = os.path.normpath(idf_path) if not os.path.exists(idf_path): raise RuntimeError("SANSFileInformation: The instrument definition file {0} does not seem to " "exist.".format(str(idf_path))) # Get the ipf path. This is slightly more complicated. See the Mantid documentation for the naming rules. Currently # they are: # 1. If the IDF is not in the instrument folder and there is another X_Parameters.xml in the same folder, # this one in the same folder will be used instead of any parameter file in the instrument folder. # 2. If you want one parameter file for your IDF file, name your IDF file X_Definition_Yyy.xml and the parameter # file X_Parameters_Yyy.xml , where Yyy is any combination a characters you find appropriate. If your IDF # file is not in the instrument folder, the parameter file can be in either the same folder or in the instrument # folder, but it can only be in the instrument folder, if the same folder has no X_Parameters.xml or # X_Parameters_Yyy.xml file. If there is no X_Parameters_Yyy.xml file, X_Parameters.xml would be used. ipf_rule1 = get_ipf_for_rule_1(idf_path) if ipf_rule1: return idf_path, ipf_rule1 ipf_rule2 = get_ipf_for_rule_2(idf_path) if ipf_rule2: return idf_path, ipf_rule2 raise RuntimeError("SANSFileInformation: There does not seem to be a corresponding instrument parameter file " "available for {0}".format(str(idf_path)))
def get_instrument_paths_for_sans_file(file_name=None, file_information=None): """ Gets the Instrument Definition File (IDF) path and the Instrument Parameter Path (IPF) path associated with a file. :param file_name: the file name is a name fo a SANS data file, e.g. SANS2D0001234. This or the file_information has to be specified. :param file_information: a file_information object. either this or the file_name has to be specified. :return: the IDF path and the IPF path """ def get_file_location(path): return os.path.dirname(path) def get_ipf_equivalent_name(path): # If XXX_Definition_Yyy.xml is the IDF name, then the equivalent IPF name is: XXX_Parameters_Yyy.xml base_file_name = os.path.basename(path) return base_file_name.replace(DEFINITION, PARAMETERS) def get_ipf_standard_name(path): # If XXX_Definition_Yyy.xml is the IDF name, then the standard IPF name is: XXX_Parameters.xml base_file_name = os.path.basename(path) elements = base_file_name.split("_") return elements[0] + PARAMETERS_XML_SUFFIX def check_for_files(directory, path): # Check if XXX_Parameters_Yyy.xml exists in the same folder ipf_equivalent_name = get_ipf_equivalent_name(path) ipf_equivalent = os.path.join(directory, ipf_equivalent_name) if os.path.exists(ipf_equivalent): return ipf_equivalent # Check if XXX_Parameters.xml exists in the same folder ipf_standard_name = get_ipf_standard_name(path) ipf_standard = os.path.join(directory, ipf_standard_name) if os.path.exists(ipf_standard): return ipf_standard # Does not seem to be in the folder return None def get_ipf_for_rule_1(path): # Check if can be found in the same folder directory = get_file_location(path) return check_for_files(directory, path) def get_ipf_for_rule_2(path): # Check if can be found in the instrument folder directory = ConfigService.getInstrumentDirectory() return check_for_files(directory, path) # Get the measurement date if not isinstance(file_information, SANSFileInformation): file_information_factory = SANSFileInformationFactory() file_information = file_information_factory.create_sans_file_information(file_name) measurement_time = file_information.get_date() # For some odd reason the __str__ method of DateAndTime adds a space which we need to strip here. It seems # to be on purpose though since the export method is called IS08601StringPlusSpace --> hence we need to strip it # ourselves measurement_time_as_string = str(measurement_time).strip() # Get the instrument instrument = file_information.get_instrument() instrument_as_string = SANSInstrument.to_string(instrument) # Get the idf file path # IMPORTANT NOTE: I profiled the call to ExperimentInfo.getInstrumentFilename and it dominates # the state creation. Ironically this routine is exported from C++. The problem is # that we are performing XML parsing on the C++ side, which is costly. There is a # movement currently towards making the IDF redundant and storing instrument info # as native nexus information. # TODO for optimization: Add the IDF path to a global cache layer which takes the # instrument name and the from-to dates idf_path = ExperimentInfo.getInstrumentFilename(instrument_as_string, measurement_time_as_string) idf_path = os.path.normpath(idf_path) if not os.path.exists(idf_path): raise RuntimeError("SANSFileInformation: The instrument definition file {0} does not seem to " "exist.".format(str(idf_path))) # Get the ipf path. This is slightly more complicated. See the Mantid documentation for the naming rules. Currently # they are: # 1. If the IDF is not in the instrument folder and there is another X_Parameters.xml in the same folder, # this one in the same folder will be used instead of any parameter file in the instrument folder. # 2. If you want one parameter file for your IDF file, name your IDF file X_Definition_Yyy.xml and the parameter # file X_Parameters_Yyy.xml , where Yyy is any combination a characters you find appropriate. If your IDF # file is not in the instrument folder, the parameter file can be in either the same folder or in the instrument # folder, but it can only be in the instrument folder, if the same folder has no X_Parameters.xml or # X_Parameters_Yyy.xml file. If there is no X_Parameters_Yyy.xml file, X_Parameters.xml would be used. ipf_rule1 = get_ipf_for_rule_1(idf_path) if ipf_rule1: return idf_path, ipf_rule1 ipf_rule2 = get_ipf_for_rule_2(idf_path) if ipf_rule2: return idf_path, ipf_rule2 raise RuntimeError("SANSFileInformation: There does not seem to be a corresponding instrument parameter file " "available for {0}".format(str(idf_path)))
def get_instrument_paths_for_sans_file(file_name=None, file_information=None): """ Gets the Instrument Definition File (IDF) path and the Instrument Parameter Path (IPF) path associated with a file. :param file_name: the file name is a name fo a SANS data file, e.g. SANS2D0001234. This or the file_information has to be specified. :param file_information: a file_information object. either this or the file_name has to be specified. :return: the IDF path and the IPF path """ def get_file_location(path): return os.path.dirname(path) def get_ipf_equivalent_name(path): # If XXX_Definition_Yyy.xml is the IDF name, then the equivalent IPF name is: XXX_Parameters_Yyy.xml base_file_name = os.path.basename(path) return base_file_name.replace(DEFINITION, PARAMETERS) def get_ipf_standard_name(path): # If XXX_Definition_Yyy.xml is the IDF name, then the standard IPF name is: XXX_Parameters.xml base_file_name = os.path.basename(path) elements = base_file_name.split("_") return elements[0] + PARAMETERS_XML_SUFFIX def check_for_files(directory, path): # Check if XXX_Parameters_Yyy.xml exists in the same folder ipf_equivalent_name = get_ipf_equivalent_name(path) ipf_equivalent = os.path.join(directory, ipf_equivalent_name) if os.path.exists(ipf_equivalent): return ipf_equivalent # Check if XXX_Parameters.xml exists in the same folder ipf_standard_name = get_ipf_standard_name(path) ipf_standard = os.path.join(directory, ipf_standard_name) if os.path.exists(ipf_standard): return ipf_standard # Does not seem to be in the folder return None def get_ipf_for_rule_1(path): # Check if can be found in the same folder directory = get_file_location(path) return check_for_files(directory, path) def get_ipf_for_rule_2(path): # Check if can be found in the instrument folder directory = ConfigService.getInstrumentDirectory() return check_for_files(directory, path) # Get the measurement date if not isinstance(file_information, SANSFileInformation): file_information_factory = SANSFileInformationFactory() file_information = file_information_factory.create_sans_file_information( file_name) measurement_time = file_information.get_date() # For some odd reason the __str__ method of DateAndTime adds a space which we need to strip here. It seems # to be on purpose though since the export method is called IS08601StringPlusSpace --> hence we need to strip it # ourselves measurement_time_as_string = str(measurement_time).strip() # Get the instrument instrument = file_information.get_instrument() instrument_as_string = SANSInstrument.to_string(instrument) # Get the idf file path # IMPORTANT NOTE: I profiled the call to ExperimentInfo.getInstrumentFilename and it dominates # the state creation. Ironically this routine is exported from C++. The problem is # that we are performing XML parsing on the C++ side, which is costly. There is a # movement currently towards making the IDF redundant and storing instrument info # as native nexus information. # TODO for optimization: Add the IDF path to a global cache layer which takes the # instrument name and the from-to dates idf_path = ExperimentInfo.getInstrumentFilename( instrument_as_string, measurement_time_as_string) idf_path = os.path.normpath(idf_path) if not os.path.exists(idf_path): raise RuntimeError( "SANSFileInformation: The instrument definition file {0} does not seem to " "exist.".format(str(idf_path))) # Get the ipf path. This is slightly more complicated. See the Mantid documentation for the naming rules. Currently # they are: # 1. If the IDF is not in the instrument folder and there is another X_Parameters.xml in the same folder, # this one in the same folder will be used instead of any parameter file in the instrument folder. # 2. If you want one parameter file for your IDF file, name your IDF file X_Definition_Yyy.xml and the parameter # file X_Parameters_Yyy.xml , where Yyy is any combination a characters you find appropriate. If your IDF # file is not in the instrument folder, the parameter file can be in either the same folder or in the instrument # folder, but it can only be in the instrument folder, if the same folder has no X_Parameters.xml or # X_Parameters_Yyy.xml file. If there is no X_Parameters_Yyy.xml file, X_Parameters.xml would be used. ipf_rule1 = get_ipf_for_rule_1(idf_path) if ipf_rule1: return idf_path, ipf_rule1 ipf_rule2 = get_ipf_for_rule_2(idf_path) if ipf_rule2: return idf_path, ipf_rule2 raise RuntimeError( "SANSFileInformation: There does not seem to be a corresponding instrument parameter file " "available for {0}".format(str(idf_path)))