Skip to content

Commit

Permalink
re #10489 functionality in and works at least on windows, needs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NickDraper committed Nov 4, 2014
1 parent 54b2932 commit f74b812
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 97 deletions.
59 changes: 32 additions & 27 deletions Code/Mantid/Framework/API/src/ExperimentInfo.cpp
Expand Up @@ -817,7 +817,7 @@ namespace API
std::string instrument(Kernel::ConfigService::Instance().getInstrument(instrumentName).name());

// Get the search directory for XML instrument definition files (IDFs)
std::string directoryName = Kernel::ConfigService::Instance().getInstrumentDirectory();
std::vector<std::string> directoryNames = Kernel::ConfigService::Instance().getInstrumentDirectories();

boost::regex regex(instrument+"_Definition.*\\.xml", boost::regex_constants::icase);
Poco::DirectoryIterator end_iter;
Expand All @@ -826,39 +826,44 @@ namespace API
std::string mostRecentIDF; // store most recently starting matching IDF if found, else most recently starting IDF.
DateAndTime refDate("1900-01-31 23:59:00"); // used to help determine the most recently starting IDF, if none match
DateAndTime refDateGoodFile("1900-01-31 23:59:00"); // used to help determine the most recently starting matching IDF
for ( Poco::DirectoryIterator dir_itr(directoryName); dir_itr != end_iter; ++dir_itr )
for ( auto instDirs_itr = directoryNames.begin(); instDirs_itr != directoryNames.end(); ++instDirs_itr)
{
if ( !Poco::File(dir_itr->path() ).isFile() ) continue;

std::string l_filenamePart = Poco::Path(dir_itr->path()).getFileName();
if ( regex_match(l_filenamePart, regex) )
//This will iterate around the directories from user ->etc ->install, and find the first beat file
std::string directoryName = *instDirs_itr;
for ( Poco::DirectoryIterator dir_itr(directoryName); dir_itr != end_iter; ++dir_itr )
{
g_log.debug() << "Found file: '" << dir_itr->path() << "'\n";
std::string validFrom, validTo;
getValidFromTo(dir_itr->path(), validFrom, validTo);
g_log.debug() << "File '" << dir_itr->path() << " valid dates: from '" << validFrom << "' to '" << validTo << "'\n";
DateAndTime from(validFrom);
// Use a default valid-to date if none was found.
DateAndTime to;
if (validTo.length() > 0)
to.setFromISO8601(validTo);
else
to.setFromISO8601("2100-01-01T00:00:00");
if ( !Poco::File(dir_itr->path() ).isFile() ) continue;

if ( from <= d && d <= to )
std::string l_filenamePart = Poco::Path(dir_itr->path()).getFileName();
if ( regex_match(l_filenamePart, regex) )
{
if( from > refDateGoodFile )
{ // We'd found a matching file more recently starting than any other matching file found
foundGoodFile = true;
refDateGoodFile = from;
g_log.debug() << "Found file: '" << dir_itr->path() << "'\n";
std::string validFrom, validTo;
getValidFromTo(dir_itr->path(), validFrom, validTo);
g_log.debug() << "File '" << dir_itr->path() << " valid dates: from '" << validFrom << "' to '" << validTo << "'\n";
DateAndTime from(validFrom);
// Use a default valid-to date if none was found.
DateAndTime to;
if (validTo.length() > 0)
to.setFromISO8601(validTo);
else
to.setFromISO8601("2100-01-01T00:00:00");

if ( from <= d && d <= to )
{
if( from > refDateGoodFile )
{ // We'd found a matching file more recently starting than any other matching file found
foundGoodFile = true;
refDateGoodFile = from;
mostRecentIDF = dir_itr->path();
}
}
if ( !foundGoodFile && ( from > refDate ) )
{ // Use most recently starting file, in case we don't find a matching file.
refDate = from;
mostRecentIDF = dir_itr->path();
}
}
if ( !foundGoodFile && ( from > refDate ) )
{ // Use most recently starting file, in case we don't find a matching file.
refDate = from;
mostRecentIDF = dir_itr->path();
}
}
}
g_log.debug() << "IDF selected is " << mostRecentIDF << std::endl;
Expand Down
Expand Up @@ -87,7 +87,7 @@ namespace Mantid
/// Algorithm's name for identification overriding a virtual method
virtual const std::string name() const { return "LoadInstrument";};
///Summary of algorithms purpose
virtual const std::string summary() const {return "Loads an Instrument Definition File (IDF) into a workspace. After the IDF has been read this algorithm will attempt to run the Child Algorithm LoadParameterFile; where if IDF filename is of the form IDENTIFIER_Definition.xml then the instrument parameters in the file named IDENTIFIER_Parameters.xml would be loaded (in the directory specified by the parameterDefinition.directory Mantid property).";}
virtual const std::string summary() const {return "Loads an Instrument Definition File (IDF) into a workspace. After the IDF has been read this algorithm will attempt to run the Child Algorithm LoadParameterFile; where if IDF filename is of the form IDENTIFIER_Definition.xml then the instrument parameters in the file named IDENTIFIER_Parameters.xml would be loaded.";}

/// Algorithm's version for identification overriding a virtual method
virtual int version() const { return 1;};
Expand Down
30 changes: 18 additions & 12 deletions Code/Mantid/Framework/DataHandling/src/LoadIDFFromNexus.cpp
Expand Up @@ -79,19 +79,25 @@ void LoadIDFFromNexus::exec()
if ( parameterString.empty() )
{
// Create the 'fallback' parameter file name to look for
const std::string directory = ConfigService::Instance().getString("parameterDefinition.directory");
std::vector<std::string> directoryNames = ConfigService::Instance().getInstrumentDirectories();
const std::string instrumentName = localWorkspace->getInstrument()->getName();
const std::string paramFile = directory + instrumentName + "_Parameters.xml";

try {
// load and also populate instrument parameters from this 'fallback' parameter file
Algorithm_sptr loadParamAlg = createChildAlgorithm("LoadParameterFile");
loadParamAlg->setProperty("Filename", paramFile);
loadParamAlg->setProperty("Workspace", localWorkspace);
loadParamAlg->execute();
g_log.notice() << "Instrument parameter file: " << paramFile << " has been loaded" << std::endl;
} catch ( std::runtime_error& ) {
g_log.debug() << "Instrument parameter file: " << paramFile << " not found or un-parsable. ";
for ( auto instDirs_itr = directoryNames.begin(); instDirs_itr != directoryNames.end(); ++instDirs_itr)
{
//This will iterate around the directories from user ->etc ->install, and find the first beat file
std::string directoryName = *instDirs_itr;
const std::string paramFile = directoryName + instrumentName + "_Parameters.xml";

try {
// load and also populate instrument parameters from this 'fallback' parameter file
Algorithm_sptr loadParamAlg = createChildAlgorithm("LoadParameterFile");
loadParamAlg->setProperty("Filename", paramFile);
loadParamAlg->setProperty("Workspace", localWorkspace);
loadParamAlg->execute();
g_log.notice() << "Instrument parameter file: " << paramFile << " has been loaded" << std::endl;
break; //stop at the first one
} catch ( std::runtime_error& ) {
g_log.debug() << "Instrument parameter file: " << paramFile << " not found or un-parsable. ";
}
}
}
else
Expand Down
15 changes: 9 additions & 6 deletions Code/Mantid/Framework/DataHandling/src/LoadInstrument.cpp
Expand Up @@ -202,14 +202,17 @@ namespace Mantid
{
// Not found, so search the other places were it may occur
Kernel::ConfigServiceImpl & configService = Kernel::ConfigService::Instance();
std::string directoryName = configService.getString("parameterDefinition.directory");
if (directoryName.empty())
std::vector<std::string> directoryNames = configService.getInstrumentDirectories();

for ( auto instDirs_itr = directoryNames.begin(); instDirs_itr != directoryNames.end(); ++instDirs_itr)
{
// This is the assumed deployment directory for parameter files, where we need to be
// relative to the directory of the executable, not the current working directory.
directoryName = Poco::Path(configService.getPropertiesDir()).resolve("../instrument").toString();
//This will iterate around the directories from user ->etc ->install, and find the first beat file
std::string directoryName = *instDirs_itr;
fullPathParamIDF = getFullPathParamIDF( directoryName );
//stop when you find the first one
if (!fullPathParamIDF.empty())
break;
}
fullPathParamIDF = getFullPathParamIDF( directoryName );
}

if(!fullPathParamIDF.empty()) {
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/Kernel/CMakeLists.txt
Expand Up @@ -364,7 +364,7 @@ set_property ( TARGET Kernel PROPERTY FOLDER "MantidFramework" )

target_link_libraries ( Kernel ${MANTIDLIBS} ${GSL_LIBRARIES} ${NEXUS_LIBRARIES} )
if ( WIN32 )
target_link_libraries ( Kernel Psapi.lib ) # For memory usage queries
target_link_libraries ( Kernel Psapi.lib shell32.lib ) # For memory usage queries
endif()

# Add the unit tests directory
Expand Down
12 changes: 11 additions & 1 deletion Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h
Expand Up @@ -158,6 +158,8 @@ namespace Mantid
std::string getCurrentDir() const;
/// Returns the system's temp directory
std::string getTempDir();
/// Returns the system's appdata directory
std::string getAppDataDir();
//Return the executable path
std::string getDirectoryOfExecutable() const;
//Return the full path to the executable
Expand All @@ -184,6 +186,8 @@ namespace Mantid
/// Get the list of user search paths
const std::vector<std::string>& getUserSearchDirs() const;
/// Get instrument search directory
const std::vector<std::string>& getInstrumentDirectories() const;
/// Get instrument search directory
const std::string getInstrumentDirectory() const;
//@}

Expand Down Expand Up @@ -249,13 +253,17 @@ namespace Mantid
/// Create the storage of the data search directories
void cacheDataSearchPaths();
/// Create the storage of the user search directories
void cacheUserSearchPaths();
void cacheUserSearchPaths();
/// Create the storage of the instrument directories
void cacheInstrumentPaths();
/// Returns true if the path is in the data search list
bool isInDataSearchList(const std::string & path) const;
/// Empty the list of facilities, deleting the FacilityInfo objects in the process
void clearFacilities();
/// Set the PV_PLUGIN_PATH to point at this version of Mantid.
void setParaViewPluginPath() const;
/// Verifies the directory exists and add it to the back of the directory list if valid
bool addDirectoryifExists(const std::string& directoryName, std::vector<std::string>& directoryList);

// Forward declaration of inner class
template <class T>
Expand Down Expand Up @@ -288,6 +296,8 @@ namespace Mantid
std::vector<std::string> m_DataSearchDirs;
/// Store a list of user search paths
std::vector<std::string> m_UserSearchDirs;
/// Store a list of instrument directory paths
std::vector<std::string> m_InstrumentDirs;
/// A map of facilities to instruments
std::map<std::string,std::vector<std::string> > m_instr_prefixes;

Expand Down

0 comments on commit f74b812

Please sign in to comment.