BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
ImportDataInfo.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Support/IO/ImportDataInfo.cpp
6 //! @brief Implements ImportDataInfo helper struct
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include "Base/Axis/Frame.h"
17 #include "Base/Axis/IAxis.h"
18 #include "Device/Coord/AxisNames.h"
19 #include "Device/Data/Datafield.h"
20 #include "GUI/Support/IO/IOUtils.h"
21 #include "GUI/Util/CoordName.h"
22 #include "GUI/Util/Error.h"
23 #include <memory>
24 
25 namespace {
26 
27 std::vector<Coords> specularUnits()
28 {
29  std::vector<Coords> result;
30  for (const auto& pair : DataUtils::AxisNames::specAxis)
31  result.push_back(pair.first);
32  return result;
33 }
34 
35 // map: data rank --> available units
36 std::map<size_t, std::vector<Coords>> available_units = {{1u, specularUnits()},
37  {2u, {Coords::NBINS}}};
38 
39 } // namespace
40 
42  : m_data(std::move(other.m_data))
43  , m_coords(other.m_coords)
44 {
45 }
46 
47 ImportDataInfo::ImportDataInfo(std::unique_ptr<Datafield> data, Coords units)
48  : m_data(units == Coords::NBINS && data ? GUI::Util::IO::binifyAxes(*data) : std::move(data))
49  , m_coords(units)
50 {
51  checkValidity();
52 }
53 
54 ImportDataInfo::ImportDataInfo(std::unique_ptr<Datafield> data, const QString& units_label)
55  : m_data(std::move(data))
56  , m_coords(GUI::Util::CoordName::coordFromName(units_label))
57 {
58  checkValidity();
59 }
60 
61 ImportDataInfo::ImportDataInfo(Datafield&& data, Coords units)
62  : ImportDataInfo(std::make_unique<Datafield>(std::move(data)), units)
63 {
64 }
65 
67 
68 ImportDataInfo::operator bool() const
69 {
70  return static_cast<bool>(m_data);
71 }
72 
73 std::unique_ptr<Datafield> ImportDataInfo::clonedData() const&
74 {
75  if (!m_data)
76  return nullptr;
77  return std::unique_ptr<Datafield>(m_data->clone());
78 }
79 
80 std::unique_ptr<Datafield> ImportDataInfo::intensityData() &&
81 {
82  return std::move(m_data);
83 }
84 
86 {
87  if (!m_data)
88  return 0;
89  return m_data->rank();
90 }
91 
93 {
95 }
96 
97 QString ImportDataInfo::axisLabel(size_t axis_index) const
98 {
99  if (!m_data)
100  return "";
101 
102  const size_t rank = m_data->rank();
103  if (rank == 2)
104  return axis_index == 0 ? "X [nbins]" : "Y [nbins]";
105  if (rank == 1) {
106  if (axis_index > 0)
107  return "Signal [a.u.]";
108  return QString::fromStdString(DataUtils::AxisNames::specAxis.at(m_coords));
109  }
110  throw Error("Error in ImportDataInfo::axisLabel: unsupported data type");
111 }
112 
114 {
115  if (!m_data)
116  return;
117  auto iter = available_units.find(m_data->rank());
118  if (iter == available_units.end())
119  throw Error("Error in ImportDataInfo constructor: unsupported data type");
120  for (auto& value : iter->second)
121  if (m_coords == value)
122  return;
123 
124  throw Error("Error in ImportDataInfo constructor: inacceptable units passed.");
125 }
Defines namespace GUI::Util::CoordName.
Defines error class.
Defines namespace GUI::Util::IO.
Defines ImportDataInfo helper struct.
@ other
The unit has no enum value defined in here (e.g. when defined as an explicit string)
Carries information about loaded data.
size_t dataRank() const
Returns number of dimensions.
QString unitsLabel() const
std::unique_ptr< Datafield > clonedData() const &
QString axisLabel(size_t axis_index) const
std::unique_ptr< Datafield > m_data
ImportDataInfo(std::unique_ptr< Datafield > data, Coords units)
std::unique_ptr< Datafield > intensityData() &&
const Coords m_coords
Coords coordFromName(const QString &name)
Returns domain axes units type from their GUI name.
Definition: CoordName.cpp:46
QString nameFromCoord(Coords units)
Returns axes units names from their domain counterpart.
Definition: CoordName.cpp:36
std::unique_ptr< Datafield > binifyAxes(const Datafield &data)
Creates Datafield with bin-valued axes.
Definition: IOUtils.cpp:22
Definition: def.cpp:20