diff --git a/Code/Mantid/API/inc/MantidAPI/Progress.h b/Code/Mantid/API/inc/MantidAPI/Progress.h index 15bd1f01491add7319567f72f2fb83aafd677a9a..5a778695ef064697b5b563fb053a42bca7979cfa 100644 --- a/Code/Mantid/API/inc/MantidAPI/Progress.h +++ b/Code/Mantid/API/inc/MantidAPI/Progress.h @@ -5,24 +5,6 @@ // Includes //---------------------------------------------------------------------- #include "MantidKernel/System.h" -//#include "MantidAPI/IAlgorithm.h" -//#include "MantidAPI/AlgorithmManager.h" -//#include "MantidAPI/WorkspaceFactory.h" -//#include "MantidAPI/WorkspaceOpOverloads.h" -//#include "MantidKernel/PropertyManager.h" -//#include "MantidKernel/Property.h" -//#include "MantidAPI/WorkspaceProperty.h" -//#include "MantidKernel/Logger.h" -//#include "MantidKernel/Exception.h" - -//#include <boost/shared_ptr.hpp> -//#include <Poco/ActiveMethod.h> -//#include <Poco/NotificationCenter.h> -//#include <Poco/Notification.h> -//#include <Poco/NObserver.h> -//#include <string> -//#include <vector> -//#include <map> namespace Mantid { diff --git a/Code/Mantid/Algorithms/Algorithms.vcproj b/Code/Mantid/Algorithms/Algorithms.vcproj index 8658caffccf7d957c8b30798154239b29f7a7398..98593d7200078fcd8c8832cac73d3da6bb0ffbcf 100644 --- a/Code/Mantid/Algorithms/Algorithms.vcproj +++ b/Code/Mantid/Algorithms/Algorithms.vcproj @@ -118,7 +118,7 @@ FavorSizeOrSpeed="1" OmitFramePointers="true" WholeProgramOptimization="true" - AdditionalIncludeDirectories="inc;../API/inc;../Geometry/inc;../Kernel/inc;../DataObjects/inc;../../Third_Party/include" + AdditionalIncludeDirectories="inc;../API/inc;../Geometry/inc;../Kernel/inc;../DataObjects/inc;../CurveFitting/inc;../../Third_Party/include" PreprocessorDefinitions="_USE_MATH_DEFINES;WIN32;_WINDOWS;POCO_DLL;WINVER=0x0500;IN_MANTID_ALGORITHMS=1;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;_SCL_SECURE_NO_DEPRECATE;GSL_DLL;NOMINMAX" RuntimeLibrary="2" OpenMP="true" diff --git a/Code/Mantid/Geometry/Geometry.vcproj b/Code/Mantid/Geometry/Geometry.vcproj index 4fe3501f68a940978b2dd3e98234c90a93d78312..a764021dbe567a782efb311cfdea5d17ebdf9d58 100644 --- a/Code/Mantid/Geometry/Geometry.vcproj +++ b/Code/Mantid/Geometry/Geometry.vcproj @@ -629,11 +629,11 @@ > </File> <File - RelativePath=".\inc\MantidGeometry\Matrix.h" + RelativePath=".\auxinc\Matrix.h" > </File> <File - RelativePath=".\auxinc\Matrix.h" + RelativePath=".\inc\MantidGeometry\Matrix.h" > </File> <File @@ -680,6 +680,10 @@ RelativePath=".\inc\MantidGeometry\Parameter.h" > </File> + <File + RelativePath=".\inc\MantidGeometry\ParameterFactory.h" + > + </File> <File RelativePath=".\inc\MantidGeometry\ParameterMap.h" > @@ -757,11 +761,11 @@ > </File> <File - RelativePath=".\auxinc\RotCounter.h" + RelativePath=".\inc\MantidGeometry\RotCounter.h" > </File> <File - RelativePath=".\inc\MantidGeometry\RotCounter.h" + RelativePath=".\auxinc\RotCounter.h" > </File> <File diff --git a/Code/Mantid/Geometry/inc/MantidGeometry/Parameter.h b/Code/Mantid/Geometry/inc/MantidGeometry/Parameter.h index 32bc02ecb2dde13580531bd489ebada92c0f859a..4bea249715a8ba7f79f6e658000193b3881bab44 100644 --- a/Code/Mantid/Geometry/inc/MantidGeometry/Parameter.h +++ b/Code/Mantid/Geometry/inc/MantidGeometry/Parameter.h @@ -15,6 +15,13 @@ namespace Mantid { + +namespace Kernel +{ +template <class C, class Base> +class Instantiator; +} + namespace Geometry { @@ -49,10 +56,11 @@ class DLLExport Parameter { public: /// Constructor - Parameter( const std::string& name):m_name(name){} + ///Parameter( const std::string& name):m_name(name){} /// Virtual destructor virtual ~Parameter(){} + const std::string& type() const{return m_type;}; /// Parameter name const std::string& name() const{return m_name;}; @@ -73,12 +81,16 @@ public: template<class T> void set(const T& t); +protected: + friend class ParameterFactory; + /// Constructor + Parameter():m_name(""){} private: + /// The type of the property + std::string m_type; /// The name of the property - const std::string m_name; + std::string m_name; std::string m_str_value; ///< Parameter value as a string - /// Private default constructor ? - Parameter(); }; /// Templated class for parameters of type \c Type @@ -86,8 +98,6 @@ template<class Type> class DLLExport ParameterType:public Parameter { public: - /// Constructor - ParameterType(const std::string& name):Parameter(name){} /// Returns the value of the property as a string std::string asString() const { @@ -119,6 +129,10 @@ public: /// Get the value of the parameter const Type& operator()()const{return m_value;} +protected: + friend class Kernel::Instantiator<ParameterType<Type>,Parameter>; + /// Constructor + ParameterType():Parameter(){} private: Type m_value;///< Value }; @@ -155,13 +169,14 @@ typedef DLLExport ParameterType<V3D> ParameterV3D; /// Parameter of type Quat typedef DLLExport ParameterType<Quat> ParameterQuat; -/// Cannot convert from std::string to Geometry::Quat. -inline std::istream& operator>>(std::istream&,Quat&) -{ - throw std::runtime_error("Cannot convert from std::string to Geometry::Quat."); -} - } // namespace Geometry } // namespace Mantid +#define DECLARE_PARAMETER(classname,classtype) \ + namespace { \ + Mantid::Kernel::RegistrationHelper register_par_##classname( \ + ((Mantid::Geometry::ParameterFactory::subscribe< Mantid::Geometry::ParameterType<classtype> >(#classname)) \ + , 0)); \ + } + #endif /*MANTID_GEOMETRY_PARAMETER_H_*/ diff --git a/Code/Mantid/Geometry/inc/MantidGeometry/ParameterFactory.h b/Code/Mantid/Geometry/inc/MantidGeometry/ParameterFactory.h new file mode 100644 index 0000000000000000000000000000000000000000..adaa895faa1a875291618c41b923ae469276df0e --- /dev/null +++ b/Code/Mantid/Geometry/inc/MantidGeometry/ParameterFactory.h @@ -0,0 +1,123 @@ +#ifndef MANTID_GEOMETRY_PARAMETERFACTORY_H_ +#define MANTID_GEOMETRY_PARAMETERFACTORY_H_ + +//---------------------------------------------------------------------- +// Includes +//---------------------------------------------------------------------- +#include <vector> +#include "MantidKernel/System.h" +//#include "MantidKernel/DynamicFactory.h" +#include "MantidKernel/Instantiator.h" +#include "MantidKernel/SingletonHolder.h" +#include "MantidKernel/Logger.h" + +namespace Mantid +{ + +namespace Kernel +{ + class Logger; +} + +namespace Geometry +{ + +//---------------------------------------------------------------------- +// Forward declaration +//---------------------------------------------------------------------- + + class Parameter; + +/** The AlgorithmFactory class creates parameters for the instrument ParameterMap. + It inherits most of its implementation from the Dynamic Factory base class. + It is implemented as a singleton class. + + @author Roman Tolchenov, Tessella plc + @date 19/05/2009 + + Copyright © 2007 STFC Rutherford Appleton Laboratories + + This file is part of Mantid. + + Mantid is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + Mantid is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + + File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid> +*/ +//class DLLExport ParameterFactoryImpl : public Kernel::DynamicFactory<Parameter> +// { +// public: +// /// Creates a new instance of parameter of type className +// Parameter* create(const std::string& className, const std::string& name) const; +// template<class C> +// void subscribe(const std::string& className); +// private: +// friend struct Mantid::Kernel::CreateUsingNew<ParameterFactoryImpl>; +// +// /// Private Constructor for singleton class +// ParameterFactoryImpl(): Kernel::DynamicFactory<Parameter>(), g_log(Kernel::Logger::get("ParameterFactory")){} +// /// Private copy constructor - NO COPY ALLOWED +// ParameterFactoryImpl(const ParameterFactoryImpl&); +// /// Private assignment operator - NO ASSIGNMENT ALLOWED +// ParameterFactoryImpl& operator = (const ParameterFactoryImpl&); +// ///Private Destructor +// virtual ~ParameterFactoryImpl(){} +// ///static reference to the logger class +// Kernel::Logger& g_log; +// +// }; +// +// ///Forward declaration of a specialisation of SingletonHolder for AlgorithmFactoryImpl (needed for dllexport/dllimport) and a typedef for it. +//#ifdef _WIN32 +//// this breaks new namespace declaraion rules; need to find a better fix +// template class DLLExport Mantid::Kernel::SingletonHolder<ParameterFactoryImpl>; +//#endif /* _WIN32 */ +// typedef DLLExport Mantid::Kernel::SingletonHolder<ParameterFactoryImpl> ParameterFactory; +// + + class DLLExport ParameterFactory + { + public: + /// A typedef for the instantiator + typedef Kernel::AbstractInstantiator<Parameter> AbstractFactory; + /// A typedef for the map of registered classes + typedef std::map<std::string, AbstractFactory*> FactoryMap; + template<class C> + static void subscribe(const std::string& className); + static Parameter* create(const std::string& className, const std::string& name); + private: + ParameterFactory(); + ParameterFactory(const ParameterFactory&); + ParameterFactory& operator=(const ParameterFactory&); + /// The map holding the registered class names and their instantiators + static FactoryMap s_map; + }; + + template<class C> + void ParameterFactory::subscribe(const std::string& className) + { + typename FactoryMap::iterator it = s_map.find(className); + if (!className.empty() && it == s_map.end()) + { + s_map[className] = new Kernel::Instantiator<C, Parameter>; + } + else + { + throw std::runtime_error("Parameter type" + className + " is already registered.\n"); + } + } + +} // namespace Geometry +} // namespace Mantid + +#endif /*MANTID_GEOMETRY_PARAMETERFACTORY_H_*/ diff --git a/Code/Mantid/Geometry/inc/MantidGeometry/ParameterMap.h b/Code/Mantid/Geometry/inc/MantidGeometry/ParameterMap.h index 72fb055d7123e3d1753f18e4cd80fa553b705fe5..8e1c2956e3579cf2a0a3a09d07342117ff60e6d8 100644 --- a/Code/Mantid/Geometry/inc/MantidGeometry/ParameterMap.h +++ b/Code/Mantid/Geometry/inc/MantidGeometry/ParameterMap.h @@ -4,6 +4,7 @@ #include "MantidKernel/System.h" #include "MantidKernel/Logger.h" #include "MantidGeometry/Parameter.h" +#include "MantidGeometry/ParameterFactory.h" #include "MantidGeometry/IComponent.h" #ifndef HAS_UNORDERED_MAP_H @@ -74,32 +75,38 @@ public: int size() const {return m_map.size();} ///Copy Contructor ParameterMap(const ParameterMap& copy); + /// Clears the map + void clear(){m_map.clear();} - /// Templated method for adding a parameter providing its value as a string - template<class T> - void addTypeString(const IComponent* comp,const std::string& name, const std::string& value) + /// Method for adding a parameter providing its value as a string + void add(const std::string& type,const IComponent* comp,const std::string& name, const std::string& value) { - ParameterType<T> *param = new ParameterType<T>(name); + Parameter* param = ParameterFactory::create(type,name); param->fromString(value); m_map.insert(std::make_pair(comp,boost::shared_ptr<Parameter>(param))); } - /// Templated method for adding a parameter providing its value of a particular type + /// Method for adding a parameter providing its value of a particular type template<class T> - void addType(const IComponent* comp,const std::string& name, const T& value) + void add(const std::string& type,const IComponent* comp,const std::string& name, const T& value) { - ParameterType<T> *param = new ParameterType<T>(name); + ParameterType<T> *param = dynamic_cast<ParameterType<T> *>(ParameterFactory::create(type,name)); + if (!param) + { + reportError("Error in adding parameter: incompatible types"); + throw std::runtime_error("Error in adding parameter: incompatible types"); + } param->setValue(value); m_map.insert(std::make_pair(comp,boost::shared_ptr<Parameter>(param))); } /// Method is provided to add a parameter of any custom type which must be created with 'new' operator. /// ParameterMap takes the owneship of the parameter. e.g. AMap.add(new CustomParameter,"comp","name","value"); - void add(Parameter* param,const IComponent* comp,const std::string& name, const std::string& value) - { - param->fromString(value); - m_map.insert(std::make_pair(comp,boost::shared_ptr<Parameter>(param))); - } + //void add(Parameter* param,const IComponent* comp,const std::string& name, const std::string& value) + //{ + // param->fromString(value); + // m_map.insert(std::make_pair(comp,boost::shared_ptr<Parameter>(param))); + //} /// The same as above except that the caller is resposible for setting the parameter's value. /// e.g. CustomParameter* param = new CustomParameter; @@ -122,66 +129,66 @@ public: @param name Name for the new parameter @param value Parameter value as a string */ - void addDouble(const IComponent* comp,const std::string& name, const std::string& value){addTypeString<double>(comp,name,value);} + void addDouble(const IComponent* comp,const std::string& name, const std::string& value){add("double",comp,name,value);} /** Adds a double value to the parameter map. @param comp Component to which the new parameter is related @param name Name for the new parameter @param value Parameter value as a double */ - void addDouble(const IComponent* comp,const std::string& name, double value){addType(comp,name,value);} + void addDouble(const IComponent* comp,const std::string& name, double value){add("double",comp,name,value);} /** Adds an int value to the parameter map. @param comp Component to which the new parameter is related @param name Name for the new parameter @param value Parameter value as a string */ - void addInt(const IComponent* comp,const std::string& name, const std::string& value){addTypeString<int>(comp,name,value);} + void addInt(const IComponent* comp,const std::string& name, const std::string& value){add("int",comp,name,value);} /** Adds an int value to the parameter map. @param comp Component to which the new parameter is related @param name Name for the new parameter @param value Parameter value as an int */ - void addInt(const IComponent* comp,const std::string& name, int value){addType(comp,name,value);} + void addInt(const IComponent* comp,const std::string& name, int value){add("int",comp,name,value);} /** Adds a bool value to the parameter map. @param comp Component to which the new parameter is related @param name Name for the new parameter @param value Parameter value as a string */ - void addBool(const IComponent* comp,const std::string& name, const std::string& value){addTypeString<bool>(comp,name,value);} + void addBool(const IComponent* comp,const std::string& name, const std::string& value){add("bool",comp,name,value);} /** Adds a bool value to the parameter map. @param comp Component to which the new parameter is related @param name Name for the new parameter @param value Parameter value as a bool */ - void addBool(const IComponent* comp,const std::string& name, bool value){addType(comp,name,value);} + void addBool(const IComponent* comp,const std::string& name, bool value){add("bool",comp,name,value);} /** Adds a std::string value to the parameter map. @param comp Component to which the new parameter is related @param name Name for the new parameter @param value Parameter value */ - void addString(const IComponent* comp,const std::string& name, const std::string& value){addTypeString<std::string>(comp,name,value);} + void addString(const IComponent* comp,const std::string& name, const std::string& value){add<std::string>("string",comp,name,value);} /** Adds a V3D value to the parameter map. @param comp Component to which the new parameter is related @param name Name for the new parameter @param value Parameter value as a string */ - void addV3D(const IComponent* comp,const std::string& name, const std::string& value){addTypeString<V3D>(comp,name,value);} + void addV3D(const IComponent* comp,const std::string& name, const std::string& value){add("V3D",comp,name,value);} /** Adds a V3D value to the parameter map. @param comp Component to which the new parameter is related @param name Name for the new parameter @param value Parameter value as a V3D */ - void addV3D(const IComponent* comp,const std::string& name, const V3D& value){addType(comp,name,value);} + void addV3D(const IComponent* comp,const std::string& name, const V3D& value){add("V3D",comp,name,value);} /** Adds a Quat value to the parameter map. @param comp Component to which the new parameter is related @param name Name for the new parameter @param value Parameter value as a Quat */ - void addQuat(const IComponent* comp,const std::string& name, const Quat& value){addType(comp,name,value);} + void addQuat(const IComponent* comp,const std::string& name, const Quat& value){add("Quat",comp,name,value);} /// Return the value of a parameter as a string. std::string getString(const IComponent* comp,const std::string& name); @@ -226,9 +233,18 @@ public: /// Returns a vector with all parameter names for componenet comp std::vector<std::string> nameList(const IComponent* comp)const; + /// Returns a string with all component names, parameter names and values + std::string asString()const; + + /** Populates the map from a string containing triplets of <comp name>,<param name>,<param value> separated by + semicolons, e.g. "monitor1,pos,V3D,[0,0,1.0];detector_bank2,pos,V3D,[1,2,0]" + */ + void fromString(const std::string& str); + private: ///Assignment operator ParameterMap& operator=(const ParameterMap& rhs); + void reportError(const std::string& str); /// insternal parameter map instance pmap m_map; diff --git a/Code/Mantid/Geometry/inc/MantidGeometry/Quat.h b/Code/Mantid/Geometry/inc/MantidGeometry/Quat.h index 01dfc10bc094426906422d9de44039fd6a2cdb55..44ae4cd7ead7e898320c5a1ba23ae29f4cde1350 100644 --- a/Code/Mantid/Geometry/inc/MantidGeometry/Quat.h +++ b/Code/Mantid/Geometry/inc/MantidGeometry/Quat.h @@ -105,6 +105,7 @@ namespace Mantid const double& operator[](int) const; double& operator[](int); void printSelf(std::ostream&) const; + void readPrinted(std::istream&); private: /// Internal value double w; @@ -117,6 +118,8 @@ namespace Mantid }; DLLExport std::ostream& operator<<(std::ostream&, const Quat&); + DLLExport std::istream& operator>>(std::istream&,Quat& q); + } // Namespace Mantid diff --git a/Code/Mantid/Geometry/inc/MantidGeometry/V3D.h b/Code/Mantid/Geometry/inc/MantidGeometry/V3D.h index 455ea6caca986d548ca0712a6a0a43eec24399e3..a2444a7deb98877d3c2b522e8bd8fd123f62ff0c 100644 --- a/Code/Mantid/Geometry/inc/MantidGeometry/V3D.h +++ b/Code/Mantid/Geometry/inc/MantidGeometry/V3D.h @@ -105,6 +105,7 @@ namespace Mantid double angle(const V3D&) const; // Send to a stream void printSelf(std::ostream&) const; + void readPrinted(std::istream&); void read(std::istream&); void write(std::ostream&) const; diff --git a/Code/Mantid/Geometry/src/Parameter.cpp b/Code/Mantid/Geometry/src/Parameter.cpp index fafe88b0b39dcfb4998afda23ebcdc25fc708bd1..cfce9a0db949203b752f0728097fda3b75b01004 100644 --- a/Code/Mantid/Geometry/src/Parameter.cpp +++ b/Code/Mantid/Geometry/src/Parameter.cpp @@ -1,4 +1,5 @@ #include "MantidGeometry/Parameter.h" +#include "MantidGeometry/ParameterFactory.h" #include <sstream> namespace Mantid @@ -6,7 +7,27 @@ namespace Mantid namespace Geometry { + ParameterFactory::FactoryMap ParameterFactory::s_map; + + Parameter* ParameterFactory::create(const std::string& className, const std::string& name) + { + Parameter* p = NULL; + FactoryMap::const_iterator it = s_map.find(className); + if (it != s_map.end()) + p = it->second->createUnwrappedInstance(); + else + throw std::runtime_error("ParameterFactory:"+ className + " is not registered.\n"); + p->m_name = name; + p->m_type = className; + return p; + } } // Namespace Geometry } // Namespace Mantid +DECLARE_PARAMETER(int,int) +DECLARE_PARAMETER(double,double) +DECLARE_PARAMETER(str,std::string) +DECLARE_PARAMETER(V3D,Mantid::Geometry::V3D) +DECLARE_PARAMETER(Quat,Mantid::Geometry::Quat) + diff --git a/Code/Mantid/Geometry/src/ParameterMap.cpp b/Code/Mantid/Geometry/src/ParameterMap.cpp index 840f3153aef26214c371cdfa57daa4dfeff92575..e4534cda0c36e2af31308fdea0edeebdd4dbf3b4 100644 --- a/Code/Mantid/Geometry/src/ParameterMap.cpp +++ b/Code/Mantid/Geometry/src/ParameterMap.cpp @@ -73,6 +73,20 @@ std::vector<std::string> ParameterMap::nameList(const IComponent* comp)const } +std::string ParameterMap::asString()const +{ + std::stringstream out; + for(pmap_it it=m_map.begin();it!=m_map.end();it++) + { + boost::shared_ptr<Parameter> p = it->second; + if (p) + { + out << (it->first)->getName() << ';' << p->type()<< ';' << p->name() << ';' << p->asString() << '|'; + } + } + return out.str(); +} + /** Create or adjust "pos" parameter for a component * Assumed that name either equals "x", "y" or "z" otherwise this method will not add/modify "pos" parameter @@ -144,6 +158,15 @@ void ParameterMap::addRotationParam(const IComponent* comp, const double deg) addQuat(comp, "rot", quat); } +void ParameterMap::reportError(const std::string& str) +{ + g_log.error(str); +} + +void ParameterMap::fromString(const std::string& str) +{ + +} } // Namespace Geometry diff --git a/Code/Mantid/Geometry/src/Quat.cpp b/Code/Mantid/Geometry/src/Quat.cpp index 6aabab46905802655867ca803e6a0c4f94170c0b..c5bf78903aba105aada4ceb4363c176a4fbecc18 100644 --- a/Code/Mantid/Geometry/src/Quat.cpp +++ b/Code/Mantid/Geometry/src/Quat.cpp @@ -441,6 +441,33 @@ void Quat::printSelf(std::ostream& os) const } +/** Read data from a stream in the format returned by printSelf ("[w,a,b,c]"). + * @param IX :: Input Stream + * @throw std::runtime_error if the input is of wrong format +*/ +void Quat::readPrinted(std::istream& IX) +{ + std::string in; + std::getline(IX,in); + size_t i = in.find_first_of('['); + if (i == std::string::npos) throw std::runtime_error("Wrong format for Quat input: "+in); + size_t j = in.find_last_of(']'); + if (j == std::string::npos || j < i + 8) throw std::runtime_error("Wrong format for Quat input: "+in); + + size_t c1 = in.find_first_of(','); + size_t c2 = in.find_first_of(',',c1+1); + size_t c3 = in.find_first_of(',',c2+1); + if (c1 == std::string::npos || c2 == std::string::npos || c3 == std::string::npos) + throw std::runtime_error("Wrong format for Quat input: ["+in+"]"); + + w = atof(in.substr(i+1,c1-i-1).c_str()); + a = atof(in.substr(c1+1,c2-c1-1).c_str()); + b = atof(in.substr(c2+1,c3-c2-1).c_str()); + c = atof(in.substr(c3+1,j-c3-1).c_str()); + + return; +} + /** Prints a string representation * @param os the stream to output to * @param q the quat to output @@ -452,6 +479,12 @@ std::ostream& operator<<(std::ostream& os,const Quat& q) return os; } +std::istream& operator>>(std::istream& ins,Quat& q) +{ + q.readPrinted(ins); + return ins; +} + } // Namespace Geometry } // Namespce Mantid diff --git a/Code/Mantid/Geometry/src/V3D.cpp b/Code/Mantid/Geometry/src/V3D.cpp index b0f71110a85b0e9c89cadc2eb758446222f5d051..4546dd0738399adca9bce174e5263f2583f03110 100644 --- a/Code/Mantid/Geometry/src/V3D.cpp +++ b/Code/Mantid/Geometry/src/V3D.cpp @@ -597,7 +597,7 @@ V3D::write(std::ostream& OX) const } /** - Prints a text representation of itself + Prints a text representation of itself in format "[x,y,z]" \param os the Stream to output to */ void @@ -607,6 +607,32 @@ V3D::printSelf(std::ostream& os) const return; } +/*! + Read data from a stream in the format returned by printSelf ("[x,y,z]"). + \param IX :: Input Stream + \throw std::runtime_error if the input is of wrong format +*/ +void +V3D::readPrinted(std::istream& IX) +{ + std::string in; + std::getline(IX,in); + size_t i = in.find_first_of('['); + if (i == std::string::npos) throw std::runtime_error("Wrong format for V3D input: "+in); + size_t j = in.find_last_of(']'); + if (j == std::string::npos || j < i + 6) throw std::runtime_error("Wrong format for V3D input: "+in); + + size_t c1 = in.find_first_of(','); + size_t c2 = in.find_first_of(',',c1+1); + if (c1 == std::string::npos || c2 == std::string::npos) throw std::runtime_error("Wrong format for V3D input: ["+in+"]"); + + x = atof(in.substr(i+1,c1-i-1).c_str()); + y = atof(in.substr(c1+1,c2-c1-1).c_str()); + z = atof(in.substr(c2+1,j-c2-1).c_str()); + + return; +} + /** Prints a text representation of itself \param os the Stream to output to @@ -629,7 +655,7 @@ operator>>(std::istream& IX,V3D& A) \return Current state of stream */ { - A.read(IX); + A.readPrinted(IX); return IX; } diff --git a/Code/Mantid/Nexus/inc/MantidNexus/NexusFileIO.h b/Code/Mantid/Nexus/inc/MantidNexus/NexusFileIO.h index 0fac48038cca07b2b0988b647ccd439e6ae7aec8..e9c8a9334b9be07635c452bc38a4078422d2461f 100644 --- a/Code/Mantid/Nexus/inc/MantidNexus/NexusFileIO.h +++ b/Code/Mantid/Nexus/inc/MantidNexus/NexusFileIO.h @@ -88,6 +88,10 @@ namespace Mantid const int& m_spec_min, const int& m_spec_max); /// read spectra map information bool readNexusProcessedSpectraMap(API::SpectraDetectorMap& spectraMap, const int& m_spec_min, const int& m_spec_max); + /// write instrument parameters + bool writeNexusParameterMap(API::MatrixWorkspace_sptr ws); + /// read instrument parameters + bool readNexusParameterMap(API::MatrixWorkspace_sptr ws); /// close the nexus file int closeNexusFile(); diff --git a/Code/Mantid/Nexus/src/LoadMuonNexus.cpp b/Code/Mantid/Nexus/src/LoadMuonNexus.cpp index c52d6fe0b250480e5da4e7954e94bd0db3738c5c..a5155cfff61892aa04f5aab089e84bd2ffb7f9c5 100644 --- a/Code/Mantid/Nexus/src/LoadMuonNexus.cpp +++ b/Code/Mantid/Nexus/src/LoadMuonNexus.cpp @@ -8,6 +8,7 @@ #include "MantidKernel/ArrayProperty.h" #include "MantidKernel/FileValidator.h" #include "MantidGeometry/Detector.h" +#include "MantidAPI/Progress.h" #include "Poco/Path.h" @@ -117,6 +118,7 @@ namespace Mantid m_spec_max = m_numberOfSpectra; // was +1? } + API::Progress progress(this,0.,1.,m_numberOfPeriods * total_specs); // Loop over the number of periods in the Nexus file, putting each period in a separate workspace for (int period = 0; period < m_numberOfPeriods; ++period) { @@ -133,6 +135,7 @@ namespace Mantid int histToRead = i + period*total_specs; loadData(timeChannelsVec,counter,histToRead,nxload,lengthIn-1,localWorkspace ); // added -1 for NeXus counter++; + progress.report(); } // Read in the spectra in the optional list parameter, if set if (m_list) @@ -141,6 +144,7 @@ namespace Mantid { loadData(timeChannelsVec,counter,m_spec_list[i],nxload,lengthIn-1, localWorkspace ); counter++; + progress.report(); } } // Just a sanity check diff --git a/Code/Mantid/Nexus/src/LoadNeXus.cpp b/Code/Mantid/Nexus/src/LoadNeXus.cpp index 7ca2a57fa391e38d92e7af7a091d38dab00e0261..61a2a715345c25e667eebea7b78373026786b4cf 100644 --- a/Code/Mantid/Nexus/src/LoadNeXus.cpp +++ b/Code/Mantid/Nexus/src/LoadNeXus.cpp @@ -112,7 +112,7 @@ namespace NeXus void LoadNexus::runLoadMuonNexus() { - IAlgorithm_sptr loadMuonNexus = createSubAlgorithm("LoadMuonNexus"); + IAlgorithm_sptr loadMuonNexus = createSubAlgorithm("LoadMuonNexus",0.,1.); // Pass through the same input filename loadMuonNexus->setPropertyValue("Filename",m_filename); // Set the workspace property @@ -172,7 +172,7 @@ namespace NeXus void LoadNexus::runLoadNexusProcessed() { - IAlgorithm_sptr loadNexusPro = createSubAlgorithm("LoadNexusProcessed"); + IAlgorithm_sptr loadNexusPro = createSubAlgorithm("LoadNexusProcessed",0.,1.); // Pass through the same input filename loadNexusPro->setPropertyValue("Filename",m_filename); // Set the workspace property @@ -233,7 +233,7 @@ namespace NeXus void LoadNexus::runLoadIsisNexus() { - IAlgorithm_sptr loadNexusPro = createSubAlgorithm("LoadISISNexus"); + IAlgorithm_sptr loadNexusPro = createSubAlgorithm("LoadISISNexus",0.,1.); // Pass through the same input filename loadNexusPro->setPropertyValue("Filename",m_filename); // Set the workspace property diff --git a/Code/Mantid/Nexus/src/LoadNexusProcessed.cpp b/Code/Mantid/Nexus/src/LoadNexusProcessed.cpp index f6faf247c1fde2d82f9d11cc45750257a7001c08..e797a76cb771e6c58f984340d10b3a90dbed52f9 100644 --- a/Code/Mantid/Nexus/src/LoadNexusProcessed.cpp +++ b/Code/Mantid/Nexus/src/LoadNexusProcessed.cpp @@ -11,6 +11,7 @@ #include "MantidKernel/FileValidator.h" #include "MantidKernel/ConfigService.h" #include "MantidKernel/System.h" +#include "MantidAPI/Progress.h" #include "MantidNexus/NexusFileIO.h" #include "Poco/Path.h" @@ -131,7 +132,7 @@ void LoadNexusProcessed::exec() { localWorkspace->getAxis(0)->unit() = UnitFactory::Instance().create(m_axes.substr(0, colon)); } catch (std::runtime_error&) - { + { g_log.warning("Unable to set Axis(0) units"); } } @@ -144,6 +145,7 @@ void LoadNexusProcessed::exec() if (m_uniformbounds) nexusFile->getXValues(xValues.access(), 0); int counter = 0; + API::Progress progress(this,0.,1.,m_numberofspectra); for (int i = 1; i <= m_numberofspectra; ++i) { //int histToRead = i + period*(m_numberOfSpectra+1); @@ -161,8 +163,7 @@ void LoadNexusProcessed::exec() localWorkspace->getAxis(1)->spectraNo(counter) = i; // ++counter; - //if (++histCurrent % 100 == 0) progress(double(histCurrent)/total_specs); dont understand setting of histCurrent - interruption_point(); + progress.report(); } } @@ -180,6 +181,7 @@ void LoadNexusProcessed::exec() boost::shared_ptr<IInstrument> localInstrument = localWorkspace->getInstrument(); SpectraDetectorMap& spectraMap = localWorkspace->mutableSpectraMap(); nexusFile->readNexusProcessedSpectraMap(spectraMap, m_spec_min, m_spec_max); + nexusFile->readNexusParameterMap(localWorkspace); // Assign the result to the output workspace property std::string outputWorkspace = "OutputWorkspace"; setProperty(outputWorkspace, localWorkspace); diff --git a/Code/Mantid/Nexus/src/NexusFileIO.cpp b/Code/Mantid/Nexus/src/NexusFileIO.cpp index 316e168d94e4937392e954b27c7c3bc667fd6744..a76aab365480b030c026594ce28e012f01682805 100644 --- a/Code/Mantid/Nexus/src/NexusFileIO.cpp +++ b/Code/Mantid/Nexus/src/NexusFileIO.cpp @@ -20,6 +20,7 @@ #include "MantidKernel/FileValidator.h" #include "MantidAPI/SpectraDetectorMap.h" +#include <boost/tokenizer.hpp> #include <boost/shared_ptr.hpp> #include "Poco/File.h" @@ -1319,5 +1320,67 @@ namespace NeXus status=NXclosegroup(fileID); // close instrument group return(true); } + + + bool NexusFileIO::writeNexusParameterMap(API::MatrixWorkspace_sptr ws) + { + /*! Writes the instrument parameter map if not empty. Must be called inside NXentry group. + @param ws The workspace + @return true for OK, false for error + */ + + boost::shared_ptr<Geometry::ParameterMap> params = ws->InstrumentParameters(); + std::string str = params->asString(); + return writeNxNote("instrument_parameter_map","","","",str); + + } + + bool NexusFileIO::readNexusParameterMap(API::MatrixWorkspace_sptr ws) + { + /*! Reads the instrument parameter map from the data string in "instrument_parameter_map" group (NXnote). + Must be called inside NXentry group. + @param ws The workspace + @return true for OK, false for error + */ + + if (NX_ERROR == NXopengroup(fileID,"instrument_parameter_map","NXnote")) return false; + // + std::string value; + std::vector<std::string> avalues,attributes; + + if( ! readNxText( "data", value, attributes, avalues) ) + return(false); + + boost::shared_ptr<Geometry::ParameterMap> params = ws->InstrumentParameters(); + params->clear(); + IInstrument_sptr instr = ws->getBaseInstrument(); + + typedef boost::tokenizer<boost::char_separator<char> > tokenizer; + + boost::char_separator<char> sep("|"); + tokenizer parTok(value, sep); + int index=0; + // Iterate over the properties + for (tokenizer::iterator it = parTok.begin(); it != parTok.end(); ++it) + { + if ( ! it->empty() ) + { + tokenizer itemTok(*it,boost::char_separator<char>(";")); + tokenizer::iterator i = itemTok.begin(); + std::string compName = *i++; + std::string type = *i++; + std::string name = *i++; + std::string val = *i; + Geometry::IComponent* comp = instr->getComponentByName(compName).get(); + if (!comp) continue; + params->add(type,comp,name,val); + } + } + + NXclosegroup(fileID); + return(true); + + } + } // namespace NeXus } // namespace Mantid diff --git a/Code/Mantid/Nexus/src/SaveNexusProcessed.cpp b/Code/Mantid/Nexus/src/SaveNexusProcessed.cpp index 2a96537a92392e7552f6a33a29811506b1275f3a..e623c8f0df234f7ba1a8d07a0265a4653c74dde6 100644 --- a/Code/Mantid/Nexus/src/SaveNexusProcessed.cpp +++ b/Code/Mantid/Nexus/src/SaveNexusProcessed.cpp @@ -101,6 +101,8 @@ namespace NeXus boost::shared_ptr<IInstrument> instrument = m_inputWorkspace->getInstrument(); nexusFile->writeNexusInstrument(instrument); + nexusFile->writeNexusParameterMap(m_inputWorkspace); + // write XML source file name, if it exists - otherwise write "NoNameAvailable" std::string instrumentName=instrument->getName(); if(instrumentName != "")