BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
GUI::View::ImportDataUtils Namespace Reference

Description

Provides utility methods to import data files.

Functions

QString Import1dData (RealDataItem *realDataItem, const AbstractDataLoader *selectedLoader)
 Imports 1D data into the given item. More...
 
std::unique_ptr< Datafield > Import2dData (const QString &fileName, IOFactory::LoaderSelector loader)
 Imports 2D data, stores them as Datafield, and returns owning pointer. More...
 

Function Documentation

◆ Import1dData()

QString GUI::View::ImportDataUtils::Import1dData ( RealDataItem realDataItem,
const AbstractDataLoader selectedLoader 
)

Imports 1D data into the given item.

Returns an error text if a fatal error occurred (discard item). "Empty string" means "no fatal error" => imported item should be kept. The imported item still can have errors, but they might be solvable by different import settings or by using a different data loader. Argument selectedLoader is the one which was selected in the open-file-dialog (or null if none selected).

Definition at line 62 of file ImportDataUtils.cpp.

64 {
65  const QString fileName = realDataItem->nativeFileName();
66  const std::string fileNameStdString = fileName.toStdString();
67 
68  if (selectedLoader == nullptr) {
69  if (DataUtils::Format::isCompressed(fileNameStdString)
70  || DataUtils::Format::isIntFile(fileNameStdString)
71  || DataUtils::Format::isTiffFile(fileNameStdString)) {
72  try {
73  ImportDataInfo info(ImportKnownData(fileName), Coords::QSPACE);
74  if (info) {
75  realDataItem->setImportData(std::move(info));
76  return QString();
77  }
78  } catch (std::exception& ex) {
79  // If it is not tiff but e.g. dat.gz, it could be tried with CSV import
80  const bool tryWithLoaders =
81  (DataUtils::Format::isIntFile(fileNameStdString)
82  && !DataUtils::Format::isCompressed(
83  fileNameStdString)); // #baimport support compressed
84 
85  if (!tryWithLoaders)
86  // import is not possible
87  return QString::fromLatin1(ex.what());
88  }
89  }
90  }
91 
92  // -- try with selected CSV loader. If none selected, try with QRE loader
93 
94  QFile file(fileName);
95  const bool ok = file.open(QFile::ReadOnly | QIODevice::Text);
96  if (!ok)
97  return "File could not be opened.";
98 
99  QByteArray fileContent = file.readAll();
100  file.close();
101 
102  if (fileContent.isEmpty())
103  return "The imported file is empty.";
104 
105  if (DataUtils::Format::isCompressed(fileNameStdString)) {
106  // #baimport implement decompress
107  }
108 
109  AbstractDataLoader* loader = nullptr;
110  if (selectedLoader == nullptr)
111  loader = new QREDataLoader();
112  else
113  loader = selectedLoader->clone();
115  loader->setRealDataItem(realDataItem);
116  realDataItem->setDataLoader(loader);
117  QApplication::setOverrideCursor(Qt::WaitCursor);
118  loader->setFileContents(fileContent);
119  loader->guessSettings();
120  loader->processContents();
121  QApplication::restoreOverrideCursor();
122 
123  return QString();
124 }
Abstract base class for all data loaders (classes to import real data).
virtual void guessSettings()
Guess appropriate settings (for example the separator in a CSV file). Is called only once,...
virtual void setFileContents(const QByteArray &fileContent)=0
Sets the file contents to be imported. If the file was a compressed file, then the decompressed conte...
virtual void processContents()=0
Process the file contents. Can be called more than once, e.g. if the import settings have changed....
virtual void initWithDefaultImportSettings()
Set import settings to defaults.
virtual AbstractDataLoader * clone() const =0
Create a complete clone, including all internal states.
void setRealDataItem(RealDataItem *item)
Define the real data item on which the import shall work.
Carries information about loaded data.
Loader for experimental reflectometry CSV files that contain three columns with Q,...
Definition: QREDataLoader.h:29
void setImportData(ImportDataInfo info)
Sets imported data to underlying item. Creates it if not existing. This is used for 1D import....
QString nativeFileName() const
void setDataLoader(AbstractDataLoader *loader)
Takes ownership of loader.

References AbstractDataLoader::clone(), AbstractDataLoader::guessSettings(), AbstractDataLoader::initWithDefaultImportSettings(), RealDataItem::nativeFileName(), AbstractDataLoader::processContents(), RealDataItem::setDataLoader(), AbstractDataLoader::setFileContents(), RealDataItem::setImportData(), and AbstractDataLoader::setRealDataItem().

Referenced by RealDataSelectorWidget::importData1D().

Here is the call graph for this function:

◆ Import2dData()

std::unique_ptr< Datafield > GUI::View::ImportDataUtils::Import2dData ( const QString &  fileName,
IOFactory::LoaderSelector  loader 
)

Imports 2D data, stores them as Datafield, and returns owning pointer.

Definition at line 57 of file ImportDataUtils.cpp.

58 {
59  return ImportKnownData(fileName, loader);
60 }

Referenced by RealDataSelectorWidget::importData2D().