diff --git a/Code/Mantid/Framework/Algorithms/src/CreateCalFileByNames.cpp b/Code/Mantid/Framework/Algorithms/src/CreateCalFileByNames.cpp index 256fa0cb0b2d2e9c68c107bdc9be815485c85962..3ada5cd5b0848e6ea752cf3f04f0bf5da85c4a4f 100644 --- a/Code/Mantid/Framework/Algorithms/src/CreateCalFileByNames.cpp +++ b/Code/Mantid/Framework/Algorithms/src/CreateCalFileByNames.cpp @@ -78,13 +78,7 @@ namespace Mantid instshort=instshort+"_Definition.xml"; // Determine the search directory for XML instrument definition files (IDFs) - std::string directoryName = Kernel::ConfigService::Instance().getString("instrumentDefinition.directory"); - if ( directoryName.empty() ) - { - // This is the assumed deployment directory for IDFs, where we need to be relative to the - // directory of the executable, not the current working directory. - directoryName = Poco::Path(Mantid::Kernel::ConfigService::Instance().getBaseDir()).resolve("../Instrument").toString(); - } + std::string directoryName = Kernel::ConfigService::Instance().getInstrumentDirectory(); // Set up the DOM parser and parse xml file DOMParser pParser; diff --git a/Code/Mantid/Framework/Algorithms/src/CreateDummyCalFile.cpp b/Code/Mantid/Framework/Algorithms/src/CreateDummyCalFile.cpp index 147d712ee8135c7705e2ebdfe23c5d419de5c2ca..d027a85219a0020c12881d1497f81a1c15d045d9 100644 --- a/Code/Mantid/Framework/Algorithms/src/CreateDummyCalFile.cpp +++ b/Code/Mantid/Framework/Algorithms/src/CreateDummyCalFile.cpp @@ -82,15 +82,8 @@ namespace Mantid std::string instshort=instname; std::transform(instshort.begin(),instshort.end(),instshort.begin(),toupper); instshort=instshort+"_Definition.xml"; - // Determine the search directory for XML instrument definition files (IDFs) - std::string directoryName = Kernel::ConfigService::Instance().getString("instrumentDefinition.directory"); - if ( directoryName.empty() ) - { - // This is the assumed deployment directory for IDFs, where we need to be relative to the - // directory of the executable, not the current working directory. - directoryName = Poco::Path(Mantid::Kernel::ConfigService::Instance().getBaseDir()).resolve("../Instrument").toString(); - } + std::string directoryName = Kernel::ConfigService::Instance().getInstrumentDirectory(); // Set up the DOM parser and parse xml file DOMParser pParser; diff --git a/Code/Mantid/Framework/Algorithms/src/ReadGroupsFromFile.cpp b/Code/Mantid/Framework/Algorithms/src/ReadGroupsFromFile.cpp index 4726952461c3ed3515821fdddbac89c2eccba7ef..d3edec6a579129c1d3a4443b86cfa00b5bdaac6f 100644 --- a/Code/Mantid/Framework/Algorithms/src/ReadGroupsFromFile.cpp +++ b/Code/Mantid/Framework/Algorithms/src/ReadGroupsFromFile.cpp @@ -22,258 +22,256 @@ namespace Mantid { -namespace Algorithms -{ - -// Register the class into the algorithm factory -DECLARE_ALGORITHM(ReadGroupsFromFile) - -using namespace Kernel; -using API::WorkspaceProperty; -using API::MatrixWorkspace_sptr; -using API::MatrixWorkspace; -using API::FileProperty; - -ReadGroupsFromFile::ReadGroupsFromFile():API::Algorithm(),calibration() -{ -} -/** Initialisation method. Declares properties to be used in algorithm. - * - */ -void ReadGroupsFromFile::init() -{ - - // The name of the instrument - declareProperty("InstrumentName","", - "The name of the instrument. This needs to be have been loaded during\n" - "this Mantid session either by loading a dataset from that instrument\n" - "or calling LoadEmptyInstrument" ); - // The calibration file that contains the grouping information - std::vector<std::string> exts; - exts.push_back(".cal"); - exts.push_back(".xml"); - declareProperty(new FileProperty("GroupingFileName","", FileProperty::Load, exts), - "The CalFile containing the grouping you want to visualize" ); - // Flag to consider unselected detectors in the cal file - std::vector<std::string> select; - select.push_back("True"); - select.push_back("False"); - declareProperty("ShowUnselected", "True", new ListValidator(select), - "Whether to show detectors that are not in any group (default yes)" ); - // The output worksapce (2D) that will contain the group information - declareProperty( - new API::WorkspaceProperty<DataObjects::Workspace2D>("OutputWorkspace","",Direction::Output), - "The name of the output workspace" ); -} - -/** Executes the algorithm - * - * @throw Exception::FileError If the grouping file cannot be opened or read successfully - * @throw runtime_error If unable to run one of the sub-algorithms successfully - */ -void ReadGroupsFromFile::exec() -{ - // Construct the full filename from the symbol of the instrument ( - const std::string instname=getProperty("InstrumentName"); - //std::string instshort=instname.substr(0,3); - std::string instshort=instname; - std::transform(instshort.begin(),instshort.end(),instshort.begin(),toupper); - instshort=instshort+"_Definition.xml"; + namespace Algorithms + { - DataObjects::Workspace2D_sptr localWorkspace = loadEmptyInstrument(instshort); + // Register the class into the algorithm factory + DECLARE_ALGORITHM(ReadGroupsFromFile) - const std::string groupfile=getProperty("GroupingFilename"); + using namespace Kernel; + using API::WorkspaceProperty; + using API::MatrixWorkspace_sptr; + using API::MatrixWorkspace; + using API::FileProperty; - if ( ! groupfile.empty() ) - { - std::string filename(groupfile); - std::transform(filename.begin(), filename.end(), filename.begin(), tolower); - if ( filename.find(".xml") != std::string::npos ) + ReadGroupsFromFile::ReadGroupsFromFile():API::Algorithm(),calibration() { - readXMLGroupingFile(groupfile); } - else + /** Initialisation method. Declares properties to be used in algorithm. + * + */ + void ReadGroupsFromFile::init() { - readGroupingFile(groupfile); + + // The name of the instrument + declareProperty("InstrumentName","", + "The name of the instrument. This needs to be have been loaded during\n" + "this Mantid session either by loading a dataset from that instrument\n" + "or calling LoadEmptyInstrument" ); + // The calibration file that contains the grouping information + std::vector<std::string> exts; + exts.push_back(".cal"); + exts.push_back(".xml"); + declareProperty(new FileProperty("GroupingFileName","", FileProperty::Load, exts), + "The CalFile containing the grouping you want to visualize" ); + // Flag to consider unselected detectors in the cal file + std::vector<std::string> select; + select.push_back("True"); + select.push_back("False"); + declareProperty("ShowUnselected", "True", new ListValidator(select), + "Whether to show detectors that are not in any group (default yes)" ); + // The output worksapce (2D) that will contain the group information + declareProperty( + new API::WorkspaceProperty<DataObjects::Workspace2D>("OutputWorkspace","",Direction::Output), + "The name of the output workspace" ); } - } - // Get the instrument. - const int nHist=localWorkspace->getNumberHistograms(); - API::Axis* specAxis=localWorkspace->getAxis(1); - // Get the spectra to detector map - const API::SpectraDetectorMap& spectramap=localWorkspace->spectraMap(); + /** Executes the algorithm + * + * @throw Exception::FileError If the grouping file cannot be opened or read successfully + * @throw runtime_error If unable to run one of the sub-algorithms successfully + */ + void ReadGroupsFromFile::exec() + { + // Construct the full filename from the symbol of the instrument ( + const std::string instname=getProperty("InstrumentName"); + //std::string instshort=instname.substr(0,3); + std::string instshort=instname; + std::transform(instshort.begin(),instshort.end(),instshort.begin(),toupper); + instshort=instshort+"_Definition.xml"; - // Determine whether the user wants to see unselected detectors or not - const std::string su=getProperty("ShowUnselected"); - bool showunselected=(!su.compare("True")); - bool success=false; - - for (int i=0;i<nHist;i++) + DataObjects::Workspace2D_sptr localWorkspace = loadEmptyInstrument(instshort); + + const std::string groupfile=getProperty("GroupingFilename"); + + if ( ! groupfile.empty() ) + { + std::string filename(groupfile); + std::transform(filename.begin(), filename.end(), filename.begin(), tolower); + if ( filename.find(".xml") != std::string::npos ) { - int spec=specAxis->spectraNo(i); - std::vector<int> dets=spectramap.getDetectors(spec); - if (dets.empty()) // Nothing - { - localWorkspace->dataY(i)[0]=0.0; - continue; - } - calmap::const_iterator it=calibration.find(dets[0]); - if (it==calibration.end()) //Could not find the detector - { - localWorkspace->dataY(i)[0]=0.0; - continue; - } - if (showunselected) - { - if (((*it).second).second==0) - localWorkspace->dataY(i)[0]=0.0; - else - localWorkspace->dataY(i)[0]=static_cast<double>(((*it).second).first); - } - else - localWorkspace->dataY(i)[0]=static_cast<double>(((*it).second).first); - if (!success) success=true; //At least one detector is found in the cal file - + readXMLGroupingFile(groupfile); } - progress(1); - - calibration.clear(); - if (!success) //Do some cleanup + else { - localWorkspace.reset(); - throw std::runtime_error("Fail to found a detector in "+groupfile+" existing in instrument "+instshort); + readGroupingFile(groupfile); } - setProperty("OutputWorkspace",localWorkspace); - return; -} + } -DataObjects::Workspace2D_sptr ReadGroupsFromFile::loadEmptyInstrument(const std::string& instrument_xml_name) -{ - // Determine the search directory for XML instrument definition files (IDFs) - std::string directoryName = Kernel::ConfigService::Instance().getString("instrumentDefinition.directory"); - if ( directoryName.empty() ) + // Get the instrument. + const int nHist=localWorkspace->getNumberHistograms(); + API::Axis* specAxis=localWorkspace->getAxis(1); + // Get the spectra to detector map + const API::SpectraDetectorMap& spectramap=localWorkspace->spectraMap(); + + // Determine whether the user wants to see unselected detectors or not + const std::string su=getProperty("ShowUnselected"); + bool showunselected=(!su.compare("True")); + bool success=false; + + for (int i=0;i<nHist;i++) + { + int spec=specAxis->spectraNo(i); + std::vector<int> dets=spectramap.getDetectors(spec); + if (dets.empty()) // Nothing { - // This is the assumed deployment directory for IDFs, where we need to be relative to the - // directory of the executable, not the current working directory. - directoryName = Poco::Path(Mantid::Kernel::ConfigService::Instance().getBaseDir()).resolve("../Instrument").toString(); + localWorkspace->dataY(i)[0]=0.0; + continue; } - API::IAlgorithm_sptr loadInst = createSubAlgorithm("LoadEmptyInstrument"); - loadInst->setPropertyValue("Filename", directoryName+'/'+instrument_xml_name); - - // Now execute the sub-algorithm. Catch and log any error, but don't stop. - try + calmap::const_iterator it=calibration.find(dets[0]); + if (it==calibration.end()) //Could not find the detector { - loadInst->execute(); + localWorkspace->dataY(i)[0]=0.0; + continue; } - catch (std::runtime_error&) + if (showunselected) { - g_log.error("Unable to successfully run LoadEmptyInstrument sub-algorithm"); + if (((*it).second).second==0) + localWorkspace->dataY(i)[0]=0.0; + else + localWorkspace->dataY(i)[0]=static_cast<double>(((*it).second).first); } - progress(0.3); - // Get back the output workspace - return loadInst->getProperty("OutputWorkspace"); -} + else + localWorkspace->dataY(i)[0]=static_cast<double>(((*it).second).first); + if (!success) success=true; //At least one detector is found in the cal file + + } + progress(1); + + calibration.clear(); + if (!success) //Do some cleanup + { + localWorkspace.reset(); + throw std::runtime_error("Fail to found a detector in "+groupfile+" existing in instrument "+instshort); + } + setProperty("OutputWorkspace",localWorkspace); + return; + } + /** + * Load an empty instrument + * @param instrument_xml_name The name of the instrument definition file + * @returns A pointer to a Workspace2D containing the loaded instrument + */ + DataObjects::Workspace2D_sptr ReadGroupsFromFile::loadEmptyInstrument(const std::string& instrument_xml_name) + { + // Determine the search directory for XML instrument definition files (IDFs) + std::string directoryName = Kernel::ConfigService::Instance().getInstrumentDirectory(); + API::IAlgorithm_sptr loadInst = createSubAlgorithm("LoadEmptyInstrument"); + loadInst->setPropertyValue("Filename", directoryName+'/'+instrument_xml_name); + + // Now execute the sub-algorithm. Catch and log any error, but don't stop. + try + { + loadInst->execute(); + } + catch (std::runtime_error&) + { + g_log.error("Unable to successfully run LoadEmptyInstrument sub-algorithm"); + } + progress(0.3); + // Get back the output workspace + return loadInst->getProperty("OutputWorkspace"); + } -void ReadGroupsFromFile::readGroupingFile(const std::string& filename) -{ - std::ifstream grFile(filename.c_str()); - if (!grFile.is_open()) - { - g_log.error() << "Unable to open grouping file " << filename << std::endl; - throw Exception::FileError("Error reading .cal file",filename); - } - calibration.clear(); - std::string str; - while(getline(grFile,str)) - { - // Comment, not read - if (str.empty() || str[0] == '#') continue; - std::istringstream istr(str); - int n,udet,sel,group; - double offset; - istr >> n >> udet >> offset >> sel >> group; - calibration[udet]=std::make_pair<int,int>(group,sel); - } - grFile.close(); - progress(0.7); - return; -} + void ReadGroupsFromFile::readGroupingFile(const std::string& filename) + { + std::ifstream grFile(filename.c_str()); + if (!grFile.is_open()) + { + g_log.error() << "Unable to open grouping file " << filename << std::endl; + throw Exception::FileError("Error reading .cal file",filename); + } + calibration.clear(); + std::string str; + while(getline(grFile,str)) + { + // Comment, not read + if (str.empty() || str[0] == '#') continue; + std::istringstream istr(str); + int n,udet,sel,group; + double offset; + istr >> n >> udet >> offset >> sel >> group; + calibration[udet]=std::make_pair<int,int>(group,sel); + } + grFile.close(); + progress(0.7); + return; + } -/** -* Reads detctor ids for groups from an XML grouping file, such as one created by the SpatialGrouping algorithm. -* @param filename path and name of input file -* @throws FileError is there is a problem with the XML file -*/ -void ReadGroupsFromFile::readXMLGroupingFile(const std::string& filename) -{ - Poco::XML::DOMParser xmlParser; - Poco::XML::Document* file; - try - { - file = xmlParser.parse(filename); - } - catch ( ... ) - { - throw Kernel::Exception::FileError("Unable to parse file: ", filename); - } + /** + * Reads detctor ids for groups from an XML grouping file, such as one created by the SpatialGrouping algorithm. + * @param filename path and name of input file + * @throws FileError is there is a problem with the XML file + */ + void ReadGroupsFromFile::readXMLGroupingFile(const std::string& filename) + { + Poco::XML::DOMParser xmlParser; + Poco::XML::Document* file; + try + { + file = xmlParser.parse(filename); + } + catch ( ... ) + { + throw Kernel::Exception::FileError("Unable to parse file: ", filename); + } - Poco::XML::Element* root = file->documentElement(); + Poco::XML::Element* root = file->documentElement(); - if ( ! root->hasChildNodes() ) - { - throw Kernel::Exception::FileError("No root element in XML grouping file: ", filename); - } + if ( ! root->hasChildNodes() ) + { + throw Kernel::Exception::FileError("No root element in XML grouping file: ", filename); + } - Poco::XML::NodeList* groups = root->getElementsByTagName("group"); + Poco::XML::NodeList* groups = root->getElementsByTagName("group"); - if ( groups->length() == 0 ) - { - throw Kernel::Exception::FileError("XML group file contains no group elements:", filename); - } + if ( groups->length() == 0 ) + { + throw Kernel::Exception::FileError("XML group file contains no group elements:", filename); + } - unsigned int nGroups = groups->length(); - for ( unsigned int i = 0; i < nGroups; i++ ) - { - // Get the "detids" element from the grouping file - Poco::XML::Element* elem = static_cast<Poco::XML::Element*>(groups->item(i)); - Poco::XML::Element* group = elem->getChildElement("detids"); + unsigned int nGroups = groups->length(); + for ( unsigned int i = 0; i < nGroups; i++ ) + { + // Get the "detids" element from the grouping file + Poco::XML::Element* elem = static_cast<Poco::XML::Element*>(groups->item(i)); + Poco::XML::Element* group = elem->getChildElement("detids"); - if ( ! group ) - { - throw Mantid::Kernel::Exception::FileError("XML Group File, group contains no <detids> element:", filename); - } + if ( ! group ) + { + throw Mantid::Kernel::Exception::FileError("XML Group File, group contains no <detids> element:", filename); + } - std::string ids = group->getAttribute("val"); + std::string ids = group->getAttribute("val"); - Poco::StringTokenizer data(ids, ",", Poco::StringTokenizer::TOK_TRIM); + Poco::StringTokenizer data(ids, ",", Poco::StringTokenizer::TOK_TRIM); - if ( data.begin() != data.end() ) - { - for ( Poco::StringTokenizer::Iterator it = data.begin(); it != data.end(); ++it ) - { - // cast the string to an int - int detID; - try - { - detID = boost::lexical_cast<int>(*it); - } catch ( boost::bad_lexical_cast & ) - { - throw Mantid::Kernel::Exception::FileError("Could cast string to integer in input XML file", filename); - } + if ( data.begin() != data.end() ) + { + for ( Poco::StringTokenizer::Iterator it = data.begin(); it != data.end(); ++it ) + { + // cast the string to an int + int detID; + try + { + detID = boost::lexical_cast<int>(*it); + } catch ( boost::bad_lexical_cast & ) + { + throw Mantid::Kernel::Exception::FileError("Could cast string to integer in input XML file", filename); + } - if ( calibration.find(detID) == calibration.end() ) - { - // add detector to a group - calibration[detID] = std::pair<int,int>(i+1, 1); - } + if ( calibration.find(detID) == calibration.end() ) + { + // add detector to a group + calibration[detID] = std::pair<int,int>(i+1, 1); + } + } + } } - } - } - progress(0.7); -} + progress(0.7); + } -} // namespace Algorithm + } // namespace Algorithm } // namespace Mantid diff --git a/Code/Mantid/Framework/DataHandling/src/LoadEmptyInstrument.cpp b/Code/Mantid/Framework/DataHandling/src/LoadEmptyInstrument.cpp index 5a495f987278f6199e64f76a8f5edf5c30a8b8ec..b239fcd0f401b23940b264e00136b5859c831923 100644 --- a/Code/Mantid/Framework/DataHandling/src/LoadEmptyInstrument.cpp +++ b/Code/Mantid/Framework/DataHandling/src/LoadEmptyInstrument.cpp @@ -115,13 +115,7 @@ namespace Mantid { const std::string filename = getPropertyValue("Filename"); // Determine the search directory for XML instrument definition files (IDFs) - std::string directoryName = Kernel::ConfigService::Instance().getString("instrumentDefinition.directory"); - if ( directoryName.empty() ) - { - // This is the assumed deployment directory for IDFs, where we need to be relative to the - // directory of the executable, not the current working directory. - directoryName = Poco::Path(Mantid::Kernel::ConfigService::Instance().getBaseDir()).resolve("../Instrument").toString(); - } + std::string directoryName = Kernel::ConfigService::Instance().getInstrumentDirectory(); const std::string::size_type stripPath = filename.find_last_of("\\/"); std::string fullPathIDF; diff --git a/Code/Mantid/Framework/DataHandling/src/LoadInstrument.cpp b/Code/Mantid/Framework/DataHandling/src/LoadInstrument.cpp index d0096c25effb63c769d15b326bcf270ad4ebae39..eb42073507055a0de11307d47513c69627ae2afa 100644 --- a/Code/Mantid/Framework/DataHandling/src/LoadInstrument.cpp +++ b/Code/Mantid/Framework/DataHandling/src/LoadInstrument.cpp @@ -1732,14 +1732,13 @@ namespace Mantid { g_log.debug("Loading the parameter definition..."); // Determine the search directory for XML parameter definition files - std::string directoryName = Kernel::ConfigService::Instance().getString( - "parameterDefinition.directory"); + Kernel::ConfigServiceImpl & configService = Kernel::ConfigService::Instance(); + std::string directoryName = configService.getString("parameterDefinition.directory"); if (directoryName.empty()) { // 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(Mantid::Kernel::ConfigService::Instance().getBaseDir()).resolve( - "../Instrument").toString(); + directoryName = Poco::Path(configService.getPropertiesDir()).resolve("../Instrument").toString(); } // Remove the path from the filename diff --git a/Code/Mantid/Framework/DataHandling/src/ManagedRawFileWorkspace2D.cpp b/Code/Mantid/Framework/DataHandling/src/ManagedRawFileWorkspace2D.cpp index a46ffa21051f7b3d79c88efce3b4153f604f81da..4e29e1ea3f166c1f6cc8320720af614709e4dec3 100644 --- a/Code/Mantid/Framework/DataHandling/src/ManagedRawFileWorkspace2D.cpp +++ b/Code/Mantid/Framework/DataHandling/src/ManagedRawFileWorkspace2D.cpp @@ -312,7 +312,7 @@ namespace Mantid std::string path = Kernel::ConfigService::Instance().getString("ManagedWorkspace.FilePath"); if( path.empty() || !Poco::File(path).exists() || !Poco::File(path).canWrite() ) { - path = Mantid::Kernel::ConfigService::Instance().getBaseDir(); + path = Mantid::Kernel::ConfigService::Instance().getUserPropertiesDir(); g_log.debug() << "Temporary file written to " << path << std::endl; } if ( ( *(path.rbegin()) != '/' ) && ( *(path.rbegin()) != '\\' ) ) diff --git a/Code/Mantid/Framework/DataObjects/src/ManagedWorkspace2D.cpp b/Code/Mantid/Framework/DataObjects/src/ManagedWorkspace2D.cpp index 8eb8e9049a792427e5cb4695da8b5b6c18bfe008..c31d562798e96ea995cf18e6b70b118871b6e92c 100644 --- a/Code/Mantid/Framework/DataObjects/src/ManagedWorkspace2D.cpp +++ b/Code/Mantid/Framework/DataObjects/src/ManagedWorkspace2D.cpp @@ -88,7 +88,7 @@ void ManagedWorkspace2D::init(const int &NVectors, const int &XLength, const int std::string path = Kernel::ConfigService::Instance().getString("ManagedWorkspace.FilePath"); if( path.empty() || !Poco::File(path).exists() || !Poco::File(path).canWrite() ) { - path = Kernel::ConfigService::Instance().getOutputDir(); + path = Kernel::ConfigService::Instance().getUserPropertiesDir(); g_log.debug() << "Temporary file written to " << path << std::endl; } // Append a slash if necessary diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h index c22d72370816a2ddc30d48b49f2b1701f46aae74..2240cd13b62b7958b7afc038fc2156e246d1f726 100644 --- a/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h +++ b/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h @@ -142,10 +142,10 @@ namespace Mantid std::string getTempDir(); //@} - /// Returns the directory where the framework libraries lie - std::string getBaseDir() const; - /// Returns a directory to use as a default output directory - std::string getOutputDir() const; + /// Returns the directory where the Mantid.properties file is found. + std::string getPropertiesDir() const; + /// Returns a directory to use to write out Mantid information. Needs to be writable + std::string getUserPropertiesDir() const; /// Get the list of search paths const std::vector<std::string>& getDataSearchDirs() const; /// Get the list of user search paths diff --git a/Code/Mantid/Framework/Kernel/src/ConfigService.cpp b/Code/Mantid/Framework/Kernel/src/ConfigService.cpp index 7e114ea188f70041e7c1f86ef0ec52b61d78a3eb..179dbb333c248ec492166b10b6d360fec182e536 100644 --- a/Code/Mantid/Framework/Kernel/src/ConfigService.cpp +++ b/Code/Mantid/Framework/Kernel/src/ConfigService.cpp @@ -132,35 +132,16 @@ ConfigServiceImpl::ConfigServiceImpl() : Poco::LoggingFactory::defaultFactory().registerChannelClass("SignalChannel", new Poco::Instantiator< Poco::SignalChannel, Poco::Channel>); - // Define a directory that serves as the 'application directory'. This is where we expect to find the Mantid.properties file - // It cannot simply be the current directory since the application may be called from a different place, .i.e. - // on Linux where the bin directory is in the path and the app is run from a different location. - // We have two scenarios: - // 1) The framework is compiled into an executable and its directory is then considered as the base or, - // 2) The framework is in a stand-alone library and is created from within another executing application - // e.g. Python or Matlab (only two at the moment). We can only use the current directory here as there - // is no programmatic way of determing where the library that contains this class is. - - // A MANTID environmental variable might solve all of this?? - - std::string callingApplication = Poco::Path(getPathToExecutable()).getFileName(); - // the cases used in the names varies on different systems so we do this case insensitive - std::transform(callingApplication.begin(), callingApplication.end(), callingApplication.begin(), - tolower); - + // Define the directory to search for the Mantid.properties file. if (Poco::Environment::has("MANTIDPATH")) { // Here we have to follow the convention of the rest of this code and add a trailing slash. // Note: adding it to the MANTIDPATH itself will make other parts of the code crash. m_strBaseDir = Poco::Environment::get("MANTIDPATH") + "/"; } - else if (callingApplication.find("python") != std::string::npos || callingApplication.find("matlab") - != std::string::npos) - { - m_strBaseDir = Poco::Path::current(); - } else { + // Use the path of the executable m_strBaseDir = Mantid::Kernel::getDirectoryOfExecutable(); } @@ -180,14 +161,14 @@ ConfigServiceImpl::ConfigServiceImpl() : m_ConfigPaths.insert(std::make_pair("mantidqt.python_interfaces_directory", true)); //attempt to load the default properties file that resides in the directory of the executable - updateConfig(getBaseDir() + m_properties_file_name, false, false); + updateConfig(getPropertiesDir() + m_properties_file_name, false, false); //and then append the user properties updateConfig(getUserFilename(), true, true); updateFacilities(); g_log.debug() << "ConfigService created." << std::endl; - g_log.debug() << "Configured base directory of application as " << getBaseDir() << std::endl; + g_log.debug() << "Configured Mantid.properties directory of application as " << getPropertiesDir() << std::endl; g_log.information() << "This is Mantid Version " << MANTID_VERSION << std::endl; } @@ -231,7 +212,7 @@ void ConfigServiceImpl::loadConfig(const std::string& filename, const bool appen // check if we have failed to open the file if ((!good) || (temp == "")) { - if (filename == getOutputDir() + m_user_properties_file_name) + if (filename == getUserPropertiesDir() + m_user_properties_file_name) { //write out a fresh file createUserPropertiesFile(); @@ -300,9 +281,9 @@ void ConfigServiceImpl::configureLogging() { //Ensure that the logging directory exists Poco::Path logpath(getString("logging.channels.fileChannel.path")); - if (logpath.toString().empty() || getOutputDir() != getBaseDir()) + if (logpath.toString().empty() || getUserPropertiesDir() != getPropertiesDir()) { - std::string logfile = getOutputDir() + "mantid.log"; + std::string logfile = getUserPropertiesDir() + "mantid.log"; logpath.assign(logfile); m_pConf->setString("logging.channels.fileChannel.path", logfile); } @@ -331,9 +312,7 @@ void ConfigServiceImpl::convertRelativeToAbsolute() if (m_ConfigPaths.empty()) return; - std::string execdir(getBaseDir()); m_AbsolutePaths.clear(); - std::map<std::string, bool>::const_iterator send = m_ConfigPaths.end(); for (std::map<std::string, bool>::const_iterator sitr = m_ConfigPaths.begin(); sitr != send; ++sitr) { @@ -356,7 +335,7 @@ void ConfigServiceImpl::convertRelativeToAbsolute() std::string ConfigServiceImpl::makeAbsolute(const std::string & dir, const std::string & key) const { std::string converted; - const std::string execdir(getBaseDir()); + const std::string propFileDir(getPropertiesDir()); // If we have a list, chop it up and convert each one if (dir.find_first_of(";,") != std::string::npos) { @@ -399,7 +378,7 @@ std::string ConfigServiceImpl::makeAbsolute(const std::string & dir, const std:: } if (is_relative) { - converted = Poco::Path(execdir).resolve(dir).toString(); + converted = Poco::Path(propFileDir).resolve(dir).toString(); } else { @@ -513,7 +492,7 @@ void ConfigServiceImpl::createUserPropertiesFile() const { try { - std::fstream filestr((getOutputDir() + m_user_properties_file_name).c_str(), std::fstream::out); + std::fstream filestr((getUserPropertiesDir() + m_user_properties_file_name).c_str(), std::fstream::out); filestr << "# This file can be used to override any properties for this installation." << std::endl; filestr @@ -534,7 +513,7 @@ void ConfigServiceImpl::createUserPropertiesFile() const filestr.close(); } catch (std::runtime_error& ex) { - g_log.warning() << "Unable to write out user.properties file to " << getOutputDir() + g_log.warning() << "Unable to write out user.properties file to " << getUserPropertiesDir() << m_user_properties_file_name << " error: " << ex.what() << std::endl; } @@ -810,7 +789,7 @@ int ConfigServiceImpl::getValue(const std::string& keyName, T& out) */ std::string ConfigServiceImpl::getUserFilename() const { - return getOutputDir() + m_user_properties_file_name; + return getUserPropertiesDir() + m_user_properties_file_name; } /** Searches for the string within the environment variables and returns the @@ -879,22 +858,23 @@ std::string ConfigServiceImpl::getTempDir() } /** - * Gets the directory that we consider to be the bse directory. Basically, this is the - * executable directory when running normally or the current directory on startup when - * running through Python on the command line + * Gets the directory that we consider to be the directory containing the Mantid.properties file. + * Basically, this is the either the directory pointed to by MANTIDPATH or the directory of the current + * executable if this is not set. * @returns The directory to consider as the base directory, including a trailing slash */ -std::string ConfigServiceImpl::getBaseDir() const +std::string ConfigServiceImpl::getPropertiesDir() const { return m_strBaseDir; } /** - * Return the directory that Mantid should use for writing files. A trailing slash is appended + * Return the directory that Mantid should use for writing any files it needs so that + * this is kept separated to user saved files. A trailing slash is appended * so that filenames can more easily be concatenated with this * @return the directory that Mantid should use for writing files */ -std::string ConfigServiceImpl::getOutputDir() const +std::string ConfigServiceImpl::getUserPropertiesDir() const { #ifdef _WIN32 return m_strBaseDir; @@ -938,7 +918,7 @@ const std::string ConfigServiceImpl::getInstrumentDirectory() const { // This is the assumed deployment directory for IDFs, where we need to be relative to the // directory of the executable, not the current working directory. - directoryName = Poco::Path(getBaseDir()).resolve("../Instrument").toString(); + directoryName = Poco::Path(getPropertiesDir()).resolve("../Instrument").toString(); } if ( !Poco::File(directoryName).isDirectory() ) diff --git a/Code/Mantid/Framework/Nexus/src/SaveNexusProcessed.cpp b/Code/Mantid/Framework/Nexus/src/SaveNexusProcessed.cpp index c053b9583c971355ef8b14bd7fa0e1c15ec973c8..8f45ef02aa0281167a253ef6d74e7a8bc6a57d55 100644 --- a/Code/Mantid/Framework/Nexus/src/SaveNexusProcessed.cpp +++ b/Code/Mantid/Framework/Nexus/src/SaveNexusProcessed.cpp @@ -141,13 +141,8 @@ namespace NeXus std::transform(instrumentName.begin(), instrumentName.end(), instrumentName.begin(), toupper); std::string instrumentXml(instrumentName+"_Definition.xml"); // Determine the search directory for XML instrument definition files (IDFs) - std::string directoryName = Kernel::ConfigService::Instance().getString("instrumentDefinition.directory"); - if ( directoryName.empty() ) - { - // This is the assumed deployment directory for IDFs, where we need to be relative to the - // directory of the executable, not the current working directory. - directoryName = Poco::Path(Mantid::Kernel::ConfigService::Instance().getBaseDir()).resolve("../Instrument").toString(); - } + std::string directoryName = Kernel::ConfigService::Instance().getInstrumentDirectory(); + Poco::File file(directoryName+"/"+instrumentXml); if(!file.exists()) instrumentXml="NoXmlFileFound"; diff --git a/Code/Mantid/Framework/PythonAPI/src/SimplePythonAPI.cpp b/Code/Mantid/Framework/PythonAPI/src/SimplePythonAPI.cpp index 57bb567d7897e5d1f94efed8fa9ec596896c1f8a..d15c0b5496e78af198736abd1b25d075523d28bc 100644 --- a/Code/Mantid/Framework/PythonAPI/src/SimplePythonAPI.cpp +++ b/Code/Mantid/Framework/PythonAPI/src/SimplePythonAPI.cpp @@ -35,7 +35,8 @@ namespace Mantid */ std::string SimplePythonAPI::getModuleFilename() { - return Poco::Path(Mantid::Kernel::ConfigService::Instance().getOutputDir()).append(Poco::Path(g_module_name)).toString(); + Poco::Path userPropDir(Mantid::Kernel::ConfigService::Instance().getUserPropertiesDir()); + return userPropDir.append(Poco::Path(g_module_name)).toString(); } /** diff --git a/Code/Mantid/Framework/TestHelpers/src/ComponentCreationHelper.cpp b/Code/Mantid/Framework/TestHelpers/src/ComponentCreationHelper.cpp index fedd8ad7380ea80424d105a5759b38d4328a211e..886d5fcf0ebc34353d4bcc51e9daa63c3f671332 100644 --- a/Code/Mantid/Framework/TestHelpers/src/ComponentCreationHelper.cpp +++ b/Code/Mantid/Framework/TestHelpers/src/ComponentCreationHelper.cpp @@ -370,15 +370,7 @@ Workspace2D_sptr SANSInstrumentCreationHelper::createSANSInstrumentWorkspace(std Workspace2D_sptr workspace) { // Determine the search directory for XML instrument definition files (IDFs) - std::string directoryName = Mantid::Kernel::ConfigService::Instance().getString( - "instrumentDefinition.directory"); - if (directoryName.empty()) - { - // This is the assumed deployment directory for IDFs, where we need to be relative to the - // directory of the executable, not the current working directory. - directoryName = Poco::Path(Mantid::Kernel::ConfigService::Instance().getBaseDir()).resolve( - "../Instrument").toString(); - } + std::string directoryName = Mantid::Kernel::ConfigService::Instance().getInstrumentDirectory(); // For Nexus Mantid processed, Instrument XML file name is read from nexus std::string instrumentID = inst_name; diff --git a/Code/Mantid/MantidPlot/src/PythonScripting.cpp b/Code/Mantid/MantidPlot/src/PythonScripting.cpp index d8c012f699a60193f092b4374d1e4104ffca6a6a..080d1b57c4f7ec2082c1143f09d0c4bcdd76ce9b 100644 --- a/Code/Mantid/MantidPlot/src/PythonScripting.cpp +++ b/Code/Mantid/MantidPlot/src/PythonScripting.cpp @@ -70,7 +70,7 @@ PythonScripting::PythonScripting(ApplicationWindow *parent) #if defined(Q_OS_DARWIN) const std::string sipLocation = "/Applications/MantidPlot.app/Contents/MacOS"; #else - const std::string sipLocation = Mantid::Kernel::ConfigService::Instance().getBaseDir(); + const std::string sipLocation = Mantid::Kernel::ConfigService::Instance().getPropertiesDir(); #endif // MG: The documentation claims that if the third argument to setenv is non zero then it will update the // environment variable. What this seems to mean is that it actually overwrites it. So here we'll have @@ -113,14 +113,14 @@ bool PythonScripting::start() Py_InitializeEx(0); // Add in Mantid paths. - QDir mantidbin(QString::fromStdString(Mantid::Kernel::ConfigService::Instance().getBaseDir())); + QDir mantidbin(QString::fromStdString(Mantid::Kernel::ConfigService::Instance().getPropertiesDir())); QString pycode = "import sys\n" "mantidbin = '" + mantidbin.absolutePath() + "'\n" + "if not mantidbin in sys.path:\n" "\tsys.path.insert(0,mantidbin)\n"; - QDir mantidoutput(QString::fromStdString(Mantid::Kernel::ConfigService::Instance().getOutputDir())); + QDir mantidoutput(QString::fromStdString(Mantid::Kernel::ConfigService::Instance().getUserPropertiesDir())); if( mantidoutput != mantidbin ) { pycode += QString("sys.path.insert(1,'") + mantidoutput.absolutePath() + QString("')\n"); diff --git a/Code/Mantid/MantidQt/MantidWidgets/src/UserFunctionDialog.cpp b/Code/Mantid/MantidQt/MantidWidgets/src/UserFunctionDialog.cpp index 3cb2752ea382bb0d6785115d4a48297e23257600..404482f820d8e755cdea8f4c7c0bb3317419e6fd 100644 --- a/Code/Mantid/MantidQt/MantidWidgets/src/UserFunctionDialog.cpp +++ b/Code/Mantid/MantidQt/MantidWidgets/src/UserFunctionDialog.cpp @@ -78,7 +78,7 @@ void UserFunctionDialog::loadFunctions() setFunction ("Base","rint","rint(x)","Round to nearest integer"); setFunction ("Built-in","Gauss","h*exp(-s*(x-c)^2)"); setFunction ("Built-in","ExpDecay","h*exp(-x/t)"); - QFile funFile(QString::fromStdString (Mantid::Kernel::ConfigService::Instance().getBaseDir()) + "Mantid.user.functions"); + QFile funFile(QString::fromStdString (Mantid::Kernel::ConfigService::Instance().getPropertiesDir()) + "Mantid.user.functions"); if (funFile.exists() && funFile.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in(&funFile); @@ -359,7 +359,7 @@ void UserFunctionDialog::saveFunction() void UserFunctionDialog::saveToFile() { - QFile funFile(QString::fromStdString (Mantid::Kernel::ConfigService::Instance().getBaseDir()) + "Mantid.user.functions"); + QFile funFile(QString::fromStdString (Mantid::Kernel::ConfigService::Instance().getPropertiesDir()) + "Mantid.user.functions"); if (funFile.open(QIODevice::WriteOnly | QIODevice::Text)) { QMap<QString,QString>::const_iterator it = m_funs.begin();