Commit 40293ff2 authored by legoc's avatar legoc
Browse files

Added test for Mantid reduction

parent 6dac8a53
<module name="mantidreductiontest">
<controller class="mantid::ReductionControllerTest"/>
<include path="$(NOMAD_HOME)/../NomadModules/src"/>
</module>
/*
* Nomad Instrument Control Software
*
* Copyright 2011 Institut Laue-Langevin
*
* Licensed under the EUPL, Version 1.1 only (the "License");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
#include "ReductionControllerTest.h"
#include "controllers/common/family/Families.h"
#include <boost/lexical_cast.hpp>
#include <common/base/Date.h>
#include <fstream>
#include <iostream>
#include <napi.h>
namespace mantid {
using namespace std;
using namespace common;
using namespace boost;
using namespace cameo;
const std::string ReductionControllerTest::TYPE = "mantid_reduction_test";
const std::string ReductionControllerTest::REMOTE_APPLICATION = "responder-server";
const std::string ReductionControllerTest::RESPONDER = "responder";
ReductionControllerTest::ReductionControllerTest(const std::string& name) :
ExperimentController(name), controller::Stoppable(this) {
setFamily(family::ACQUISITION, family::COUNT);
serverEndpoint.init(this, SAVE, "cameo_server");
initialized.init(this, NOSAVE, "initialized");
fileDirectory.init(this, SAVE, "file_directory");
filename.init(this, SAVE, "filename");
x.init(this, NOSAVE, "x");
data.init(this, NOSAVE, "data");
}
ReductionControllerTest::ReductionControllerTest(const ReductionControllerTest& controller) :
ExperimentController(controller), controller::Stoppable(this) {
}
ReductionControllerTest::~ReductionControllerTest() {
// Stop the remote application.
m_remoteApplication->stop();
// Wait for the termination
application::State state = m_remoteApplication->waitFor();
cout << "Remote application " << m_remoteApplication->getName() << " terminated with state " << application::toString(state) << endl;
}
void ReductionControllerTest::postConfiguration() {
if (serverEndpoint() == "") {
m_server.reset(new Server(application::This::getServer().getEndpoint()));
} else {
m_server.reset(new Server(serverEndpoint()));
}
initialized = initApplication();
}
bool ReductionControllerTest::initApplication() {
// Do not initialize if it is already done
if (initialized()) {
return true;
}
// Start the remote server
if (!m_server->isAvailable(1000)) {
cout << "Remote server is not available" << endl;
return false;
}
cout << "Remote server is connected" << endl;
m_remoteApplication = m_server->connect(REMOTE_APPLICATION);
if (m_remoteApplication->exists()) {
// The application exists from a previous server session
m_remoteApplication->kill();
application::State state = m_remoteApplication->waitFor();
cout << "Terminated remote application with state " << application::toString(state) << endl;
}
m_remoteApplication = m_server->start(REMOTE_APPLICATION);
if (!m_remoteApplication->exists()) {
cout << "No remote application" << endl;
return false;
}
// Application initialized
initialized = true;
return true;
}
void ReductionControllerTest::readFile(const std::string& nexusFileName, std::string& fileContent) {
streampos size;
ifstream file(nexusFileName.c_str(), ios::in | ios::binary | ios::ate);
if (file.is_open()) {
size = file.tellg();
// Reserve the string.
fileContent.resize(size);
// Get the allocated array.
char * array = (char *) fileContent.data();
file.seekg(0, ios::beg);
file.read(array, size);
file.close();
} else {
cerr << "File " << nexusFileName << " cannot be read." << endl;
}
}
void ReductionControllerTest::writeFile(const std::string& nexusFileName, const std::string& fileContent) {
ofstream file;
file.open(nexusFileName, ios::binary);
file << fileContent;
file.close();
}
void ReductionControllerTest::readProcessedFile() {
NXhandle fileId;
// Open the file.
int res = NXopen("processed.nxs", NXACC_RDWR, &fileId);
if (res != NX_OK) {
cerr << "Cannot open processed.nxs." << endl;
return;
}
res = NXopengroup(fileId, "mantid_workspace_1", "NXentry");
if (res != NX_OK) {
cerr << "Cannot open group mantid_workspace_1." << endl;
return;
}
res = NXopengroup(fileId, "workspace", "NXdata");
if (res != NX_OK) {
cerr << "Cannot open group workspace" << endl;
return;
}
// Read X data.
int rank = 1;
int dims[2];
int dataType;
NXopendata(fileId, "axis1");
NXgetinfo(fileId, &rank, dims, &dataType);
if (dataType != NX_FLOAT64) {
cerr << "Problem with the type of data of axis1." << endl;
return;
}
int xSize = dims[0];
delete [] x.get();
double * xData = new double[xSize];
NXgetdata(fileId, xData);
x.set(xData);
// We set the size - 1 to have the same size than the data.
x.setSize(xSize - 1);
// Close the data.
NXclosedata(fileId);
// Read values data.
rank = 2;
NXopendata(fileId, "values");
NXgetinfo(fileId, &rank, dims, &dataType);
if (dataType != NX_FLOAT64) {
cerr << "Problem with the type of data of values." << endl;
return;
}
if (dims[1] + 1 != xSize) {
cerr << "Problem with the size " << dims[1] << " of values." << endl;
return;
}
int dataSize = dims[1];
delete [] data.get();
double * values = new double[dataSize];
NXgetdata(fileId, values);
data.set(values);
data.setSize(dataSize);
NXclosedata(fileId);
NXclosegroup(fileId);
NXclosegroup(fileId);
NXclose(&fileId);
// Check the data.
int index = 0;
while (index < dataSize) {
if (!isfinite(values[index])) {
break;
}
++index;
}
cout << "Forced sizes to " << index << endl;
// x.setSize(index);
// data.setSize(index);
int fixedSize = 280;
x.setSize(index < fixedSize ? index : fixedSize);
data.setSize(index < fixedSize ? index : fixedSize);
x.sendEvent();
data.sendEvent();
cout << "Processing finished." << endl;
}
void ReductionControllerTest::start() {
if (!initApplication()) {
return;
}
// Send a simple message with the content of the Nexus data file.
string fileContent;
// Read the file.
readFile(fileDirectory() + "/" + filename(), fileContent);
// Create the requester
auto_ptr<application::Requester> requester = application::Requester::create(*m_remoteApplication, RESPONDER);
if (requester.get() == 0) {
cout << "requester error" << endl;
return;
}
// Start date.
Date begin;
// Send the file content to the server.
requester->sendBinary(fileContent);
// Wait for the response from the server.
string response;
requester->receiveBinary(response);
// Print the duration.
Date end;
double ms = (end - begin).getMilliseconds();
cout << "File processed in " << ms << " ms" << endl;
// Write the file to disc.
writeFile("processed.nxs", response);
// Read the file.
readProcessedFile();
// Remove the file.
remove("processed.nxs");
}
void ReductionControllerTest::stop() {
}
}
/*
* Nomad Instrument Control Software
*
* Copyright 2011 Institut Laue-Langevin
*
* Licensed under the EUPL, Version 1.1 only (the "License");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
#ifndef MANTID_REDUCTIONCONTROLLERTEST_H
#define MANTID_REDUCTIONCONTROLLERTEST_H
#include <Controller.h>
#include <cameo/cameo.h>
namespace mantid {
class ReductionControllerTest : public ExperimentController,
public controller::Stoppable {
public:
//! Type of controller
static const std::string TYPE;
ReductionControllerTest(const std::string& name);
ReductionControllerTest(const ReductionControllerTest& controller);
virtual ~ReductionControllerTest();
virtual void postConfiguration();
virtual void start();
virtual void stop();
Property<std::string> serverEndpoint;
Property<bool> initialized;
Property<string> fileDirectory;
Property<string> filename;
ArrayProperty<float64> x;
ArrayProperty<float64> data;
private:
bool initApplication();
void readFile(const std::string& nexusFileName, std::string& fileContent);
void writeFile(const std::string& nexusFileName, const std::string& fileContent);
void readProcessedFile();
static const std::string REMOTE_APPLICATION;
static const std::string RESPONDER;
std::auto_ptr<cameo::Server> m_server;
std::auto_ptr<cameo::application::Instance> m_remoteApplication;
};
}
#endif
<plugin>
<controller type="mantid_reduction_test" role="mantid_reduction_test1"/>
<number_of_lines nb_lines="1"/>
<text role="mantid_reduction_test1" property="filename" prefix="Filename"/>
</plugin>
<plotdatas>
<plotdata key="data" color="1F96C0" plugins="SETUP">
<dataX p_role="mantid_reduction_test1" p_name="x"/>
<dataY p_role="mantid_reduction_test1" p_name="data"/>
</plotdata>
</plotdatas>
<controller_plugin_config type="mantid_reduction_test">
<image key="ACQUISITION"/>
<settings view="mantid_reduction_testView.xml"/>
<command view="mantid_reduction_testCommandView.xml"/>
</controller_plugin_config>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<controller type="mantid_reduction_test">
<property name="x" type="doublearray">
</property>
<property name="data" type="doublearray">
</property>
</controller>
<plugin>
<controller type="mantid_reduction_test" role="mantid_reduction_test1"/>
<text role="mantid_reduction_test1" property="filename" prefix="Filename"/>
<newLine/>
<plot_launcher role="mantid_reduction_test1" data="data" title="data" height="20"/>
</plugin>
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