ReductionControllerTest.cpp 6.92 KB
Newer Older
legoc's avatar
legoc committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 * 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() {

}

}