Commit 4e221b4e authored by Anthony Lim's avatar Anthony Lim
Browse files

refs #26726 made ALFView's load robust

parent fb852eda
......@@ -8,53 +8,115 @@
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidGeometry/Instrument.h"
#include "MantidKernel/Unit.h"
#include <utility>
namespace {
const std::string tmpName = "ALF_tmp";
const std::string instrumentName = "ALF";
const std::string wsName = "ALFData";
} // namespace
namespace MantidQt {
namespace CustomInterfaces {
namespace Direct {
/*
*Loads data for use in ALFView
* Loads data, normalise to current and then converts to d spacing
* @param name:: string name for ALF data
*/
void loadData(const std::string& name) {
Mantid::API::IAlgorithm_sptr alg =
Mantid::API::AlgorithmManager::Instance().create("Load");
alg->initialize();
alg->setProperty("Filename", name);
alg->setProperty("OutputWorkspace", "ALF");
alg->execute();
// check it is a valid ALF run number
Mantid::API::MatrixWorkspace_const_sptr ws =
Mantid::API::AnalysisDataService::Instance().retrieveWS<Mantid::API::MatrixWorkspace>("ALF");
auto da = ws->getNEvents();
auto sadfas = ws->size();
auto gfgds = ws->readX(0);
if( ws->getNEvents() != 0){
return;
}
namespace CustomInterfaces {
namespace Direct {
void loadEmptyInstrument() {
Mantid::API::IAlgorithm_sptr alg =
Mantid::API::AlgorithmManager::Instance().create("LoadEmptyInstrument");
alg->initialize();
alg->setProperty("OutputWorkspace", wsName);
alg->setProperty("InstrumentName", instrumentName);
alg->execute();
}
/*
* Loads data for use in ALFView
* Loads data, normalise to current and then converts to d spacing
* @param name:: string name for ALF data
* @return int:: the run number
*/
int loadData(const std::string &name) {
Mantid::API::IAlgorithm_sptr alg =
Mantid::API::AlgorithmManager::Instance().create("Load");
alg->initialize();
alg->setProperty("Filename", name);
alg->setProperty("OutputWorkspace", tmpName); // write to tmp ws
alg->execute();
Mantid::API::MatrixWorkspace_sptr ws =
Mantid::API::AnalysisDataService::Instance()
.retrieveWS<Mantid::API::MatrixWorkspace>(tmpName);
return ws->getRunNumber();
}
/*
* Checks loaded data is from ALF
* Loads data, normalise to current and then converts to d spacing
* @return pair<bool,bool>:: If the instrument is ALF, if it is d-spacing
*/
std::pair<bool, bool> isDataValid() {
Mantid::API::MatrixWorkspace_sptr ws =
Mantid::API::AnalysisDataService::Instance()
.retrieveWS<Mantid::API::MatrixWorkspace>(tmpName);
bool isItALF = false;
bool isItDSpace = false;
if (ws->getInstrument()->getName() == instrumentName) {
isItALF = true;
}
auto axis = ws->getAxis(0);
auto unit = axis->unit()->unitID();
if (unit == "dSpacing") {
isItDSpace = true;
}
return std::make_pair(isItALF, isItDSpace);
}
/*
* Transforms ALF data; normalise to current and then converts to d spacing
* If already d-space does nothing.
*/
void transformData() {
Mantid::API::IAlgorithm_sptr normAlg =
Mantid::API::AlgorithmManager::Instance().create("NormaliseByCurrent");
normAlg->initialize();
normAlg->setProperty("InputWorkspace", "ALF");
normAlg->setProperty("OutputWorkspace", "ALF");
normAlg->setProperty("InputWorkspace", wsName);
normAlg->setProperty("OutputWorkspace", wsName);
normAlg->execute();
Mantid::API::IAlgorithm_sptr dSpacingAlg =
Mantid::API::AlgorithmManager::Instance().create("ConvertUnits");
dSpacingAlg->initialize();
dSpacingAlg->setProperty("InputWorkspace", "ALF");
dSpacingAlg->setProperty("InputWorkspace", wsName);
dSpacingAlg->setProperty("Target", "dSpacing");
dSpacingAlg->setProperty("OutputWorkspace", "ALF");
dSpacingAlg->setProperty("OutputWorkspace", wsName);
dSpacingAlg->execute();
}
void rename() {
Mantid::API::AnalysisDataService::Instance().rename(tmpName, wsName);
}
void remove() { Mantid::API::AnalysisDataService::Instance().remove(tmpName); }
int currentRun() {
try {
Mantid::API::MatrixWorkspace_sptr ws =
Mantid::API::AnalysisDataService::Instance()
.retrieveWS<Mantid::API::MatrixWorkspace>(wsName);
return ws->getRunNumber();
} catch(...) {
return -999; // special error code
}
}
} // namespace Direct
} // namespace CustomInterfaces
} // namespace MantidQt
\ No newline at end of file
} // namespace MantidQt
......@@ -12,8 +12,13 @@ namespace MantidQt {
namespace CustomInterfaces {
namespace Direct {
void loadData(const std::string &name);
void loadEmptyInstrument();
int loadData(const std::string &name);
std::pair<bool,bool> isDataValid();
void transformData();
void rename();
void remove();
int currentRun();
} // namespace Direct
} // namespace CustomInterfaces
} // namespace MantidQt
......
......@@ -5,15 +5,17 @@
// & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
#include "ALFView_presenter.h"
#include "ALFView_view.h"
#include "ALFView_model.h"
#include "MantidQtWidgets/Common/FunctionBrowser.h"
#include "ALFView_view.h"
#include "MantidAPI/FileFinder.h"
#include "MantidQtWidgets/Common/FunctionBrowser.h"
#include "MantidQtWidgets/InstrumentView/InstrumentWidget.h"
#include "MantidQtWidgets/InstrumentView/InstrumentWidgetPickTab.h"
#include "MantidQtWidgets/Plotting/PreviewPlot.h"
#include "MantidAPI/FileFinder.h"
#include <math.h>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLineEdit>
......@@ -22,61 +24,79 @@
#include <QSplitter>
#include <QVBoxLayout>
#include <QWidget>
#include <math.h>
#include <string>
namespace MantidQt {
namespace CustomInterfaces {
using namespace Mantid::Kernel;
using namespace Mantid::API;
DECLARE_SUBWINDOW(ALFView)
using namespace Mantid;
using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace MantidQt::MantidWidgets;
using namespace MantidQt::CustomInterfaces;
using Mantid::API::Workspace_sptr;
DECLARE_SUBWINDOW(ALFView)
/// static logger
Mantid::Kernel::Logger g_log("ALFTest");
Mantid::Kernel::Logger g_log("ALFView");
ALFView::ALFView(QWidget *parent) : UserSubWindow(parent), m_view(nullptr)
{
// set up an empty ALF workspace
IAlgorithm_sptr alg =
AlgorithmManager::Instance().create("LoadEmptyInstrument");
alg->initialize();
alg->setProperty("OutputWorkspace", "ALF");
alg->setProperty("InstrumentName", "ALF");
alg->execute();
ALFView::ALFView(QWidget *parent) : UserSubWindow(parent), m_view(nullptr),m_currentRun(0) {
Direct::loadEmptyInstrument();
}
void ALFView::initLayout() { m_view = new ALFView_view(this);
void ALFView::initLayout() {
m_view = new ALFView_view(this);
this->setCentralWidget(m_view);
connect(m_view, SIGNAL(newRun()), this, SLOT(loadRunNumber()));
connect(m_view, SIGNAL(browsedToRun(const::std::string &)), this, SLOT(loadBrowsedFile(const std::string &)));
connect(m_view, SIGNAL(browsedToRun(std::string)), this,
SLOT(loadBrowsedFile(const std::string)));
}
void ALFView::loadAndAnalysis(const std::string &run) {
int runNumber = Direct::loadData(run);
auto bools = Direct::isDataValid();
if (bools.first) {
Direct::rename();
m_currentRun = runNumber;
} else {
Direct::remove();
}
// if the displayed run number is out of sinc
int das = m_view->getRunNumber();
if (m_view->getRunNumber() != m_currentRun) {
m_view->setRunQuietly(QString::number(m_currentRun));
}
if (bools.first && !bools.second) {
Direct::transformData();
}
}
void ALFView::loadRunNumber() {
const std::string runNumber = "ALF" + std::to_string(m_view->getRunNumber());
std::string filePath;
// add memory of last good run and check alf instrument
try {
filePath = Mantid::API::FileFinder::Instance().findRuns(runNumber)[0];
} catch (...) {
return;
}
void ALFView::loadRunNumber() {
int newRun = m_view->getRunNumber();
const int currentRunInADS = Direct::currentRun();
Direct::loadData(runNumber);
if (currentRunInADS == newRun) {
return;
}
const std::string runNumber = "ALF" + std::to_string(newRun);
std::string filePath;
// check its a valid run number
try {
filePath = Mantid::API::FileFinder::Instance().findRuns(runNumber)[0];
} catch (...) {
m_view->setRunQuietly(QString::number(m_currentRun));
// if file has been deleted we should replace it
if (currentRunInADS == -999) {
loadAndAnalysis("ALF" + std::to_string(m_currentRun));
}
return;
}
loadAndAnalysis(runNumber);
}
void ALFView::loadBrowsedFile(const std::string &fileName) {
void ALFView::loadBrowsedFile(const std::string fileName) {
Direct::loadData(fileName);
}
loadAndAnalysis(fileName);
}
} // namespace CustomInterfaces
} // namespace MantidQt
\ No newline at end of file
......@@ -30,10 +30,12 @@ protected:
private slots:
void loadRunNumber();
void loadBrowsedFile(const std::string &fileName);
void loadBrowsedFile(const std::string);
private:
void loadAndAnalysis(const std::string &run);
ALFView_view *m_view;
int m_currentRun;
};
}// customInterfaces
}// MantidQt
......
......@@ -6,12 +6,12 @@
// SPDX - License - Identifier: GPL - 3.0 +
#include "ALFView_view.h"
#include <QFileDialog>
#include <QGridLayout>
#include <QLineEdit>
#include <QRegExpValidator>
#include <QSplitter>
#include <QVBoxLayout>
#include <QRegExpValidator>
#include <QFileDialog>
namespace MantidQt {
namespace CustomInterfaces {
......@@ -28,7 +28,7 @@ ALFView_view::ALFView_view(QWidget *parent) : QWidget(parent), m_run(nullptr) {
}
void ALFView_view::generateLoadWidget(QWidget *loadBar) {
m_run = new QLineEdit();
m_run = new QLineEdit("0");
m_run->setValidator(new QRegExpValidator(QRegExp("[0-9]*"), m_run));
connect(m_run, SIGNAL(editingFinished()), this, SLOT(runChanged()));
......@@ -42,17 +42,21 @@ void ALFView_view::generateLoadWidget(QWidget *loadBar) {
int ALFView_view::getRunNumber() { return m_run->text().toInt(); }
void ALFView_view::runChanged() {
auto fsafd = 1;
emit newRun();
void ALFView_view::setRunQuietly(const QString runNumber) {
m_run->blockSignals(true);
m_run->setText(runNumber);
m_run->blockSignals(false);
}
void ALFView_view::browse() {
auto file = QFileDialog::getOpenFileName(
this, "Open a file", "directoryToOpen",
"File (*.nxs)");
auto dad = file.toStdString();
emit browsedToRun(file.toStdString());
void ALFView_view::runChanged() { emit newRun(); }
void ALFView_view::browse() {
auto file = QFileDialog::getOpenFileName(this, "Open a file",
"", "File (*.nxs)");
if (file.isEmpty()) {
return;
}
emit browsedToRun(file.toStdString());
}
} // namespace CustomInterfaces
......
......@@ -23,6 +23,7 @@ class ALFView_view : public QWidget {
public:
ALFView_view(QWidget *parent = nullptr);
int getRunNumber();
void setRunQuietly(const QString runNumber);
public slots:
void runChanged();
......@@ -30,7 +31,7 @@ public slots:
signals:
void newRun();
void browsedToRun(const std::string &fileName);
void browsedToRun(std::string);
private:
void generateLoadWidget(QWidget *loadBar);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment