Mantid
Loading...
Searching...
No Matches
Unit.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#include "MantidKernel/Unit.h"
9
10#include <boost/python/class.hpp>
11#include <boost/python/enum.hpp>
12#include <boost/python/register_ptr_to_python.hpp>
13#include <boost/python/tuple.hpp>
14
17using namespace boost::python;
18
20
21namespace {
26const std::string deprecatedName(const Unit &self) {
27 PyErr_Warn(PyExc_DeprecationWarning, "'name' is deprecated, use 'caption' instead.");
28 return self.caption();
29}
30
35const std::string deprecatedLabel(const Unit &self) {
36 PyErr_Warn(PyExc_DeprecationWarning, "'unit.label()' is deprecated, use 'str(unit.symbol())' instead.");
37 return self.label().ascii();
38}
39
40template <class T> tuple quickConversionWrapper(const Unit &self, const T &destUnitName) {
41 double wavelengthFactor = 0;
42 double wavelengthPower = 0;
43 bool converted = self.quickConversion(destUnitName, wavelengthFactor, wavelengthPower);
44 if (!converted) {
45 throw std::runtime_error("Quick conversion is not possible from unit:" + std::string(self.unitID()) +
46 " to the desired unit.");
47 }
48 return boost::python::make_tuple<double>(wavelengthFactor, wavelengthPower);
49}
50} // namespace
51
53
54 register_ptr_to_python<std::shared_ptr<Unit>>();
55
56 enum_<Mantid::Kernel::UnitParams>("UnitParams")
58 .value("twoTheta", Mantid::Kernel::UnitParams::twoTheta)
63 .value("tzero", Mantid::Kernel::UnitParams::tzero);
64
65 class_<Unit, boost::noncopyable>("Unit", no_init)
66 .def("name", &deprecatedName, arg("self"), "Return the full name of the unit (deprecated, use caption)")
67 .def("caption", &Unit::caption, arg("self"), "Return the full name of the unit")
68 .def("label", &deprecatedLabel, arg("self"),
69 "Returns a plain-text label to be used "
70 "as the symbol for the unit (deprecated, "
71 "use symbol())")
72 .def("symbol", &Unit::label, arg("self"),
73 "Returns a UnitLabel object that holds "
74 "information on the symbol to use for unit")
75 .def("unitID", &Unit::unitID, arg("self"),
76 "Returns the string ID of the unit. This may/may not match "
77 "its name")
78 .def("quickConversion", &quickConversionWrapper<Unit>, (arg("self"), arg("destUnitName")),
79 "Check whether the unit can be converted to another via a "
80 "simple factor")
81 .def("quickConversion", &quickConversionWrapper<std::string>, (arg("self"), arg("destination")),
82 "Check whether the unit can be converted to another via a "
83 "simple factor");
84}
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition: GetPointer.h:17
void export_Unit()
Definition: Unit.cpp:52
const AsciiString & ascii() const
Return an ascii label for unit.
Definition: UnitLabel.cpp:105
The base units (abstract) class.
Definition: Unit.h:41
virtual const UnitLabel label() const =0
A label for the unit to be printed on axes,.
virtual const std::string caption() const =0
The full name of the unit.
virtual const std::string unitID() const =0
The name of the unit.
bool quickConversion(const Unit &destination, double &factor, double &power) const
Is conversion by constant multiplication possible?
Definition: Unit.cpp:62
std::shared_ptr< Unit > Unit_sptr
Shared pointer to the Unit base class.
Definition: Unit.h:229