-
Notifications
You must be signed in to change notification settings - Fork 121
/
LoadNexusProcessed.cpp
2387 lines (2122 loc) · 86.6 KB
/
LoadNexusProcessed.cpp
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidDataHandling/LoadNexusProcessed.h"
#include "MantidAPI/AlgorithmFactory.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/BinEdgeAxis.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidAPI/RegisterFileLoader.h"
#include "MantidAPI/Run.h"
#include "MantidAPI/TextAxis.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/WorkspaceHistory.h"
#include "MantidDataHandling/ISISRunLogs.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidDataObjects/LeanElasticPeaksWorkspace.h"
#include "MantidDataObjects/Peak.h"
#include "MantidDataObjects/PeakNoShapeFactory.h"
#include "MantidDataObjects/PeakShapeEllipsoidFactory.h"
#include "MantidDataObjects/PeakShapeSphericalFactory.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidDataObjects/RebinnedOutput.h"
#include "MantidGeometry/Instrument/Goniometer.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/DateAndTime.h"
#include "MantidKernel/MultiThreaded.h"
#include "MantidKernel/StringTokenizer.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidNexus/NexusClasses.h"
#include "MantidNexus/NexusFileIO.h"
#include <boost/regex.hpp>
#include <nexus/NeXusException.hpp>
#include <map>
#include <memory>
#include <string>
#include <vector>
namespace Mantid {
namespace DataHandling {
// Register the algorithm into the algorithm factory
DECLARE_NEXUS_FILELOADER_ALGORITHM(LoadNexusProcessed)
using namespace Mantid::NeXus;
using namespace DataObjects;
using namespace Kernel;
using namespace API;
using Geometry::Instrument_const_sptr;
using Mantid::Types::Event::TofEvent;
using Types::Core::DateAndTime;
namespace {
// Helper typedef
using IntArray = std::vector<int>;
// Struct to contain spectrum information.
struct SpectraInfo {
// Number of spectra
int nSpectra{0};
// Do we have any spectra
bool hasSpectra{false};
// Contains spectrum numbers for each workspace index
IntArray spectraNumbers;
// Index of the detector in the workspace.
IntArray detectorIndex;
// Number of detectors associated with each spectra
IntArray detectorCount;
// Detector list contains a list of all of the detector numbers
IntArray detectorList;
};
// Helper typdef.
using SpectraInfo_optional = boost::optional<SpectraInfo>;
/**
* Extract ALL the detector, spectrum number and workspace index mapping
* information.
* @param mtd_entry
* @param logger
* @return
*/
SpectraInfo extractMappingInfo(NXEntry &mtd_entry, Logger &logger) {
SpectraInfo spectraInfo;
// Instrument information
if (!mtd_entry.containsGroup("instrument")) {
logger.information() << "No NXinstrument group called `instrument` under "
"NXEntry. The workspace will not "
"contain any detector information.\n";
return spectraInfo;
}
NXInstrument inst = mtd_entry.openNXInstrument("instrument");
if (!inst.containsGroup("detector")) {
logger.information() << "Detector block not found. The workspace will not "
"contain any detector information.\n";
return spectraInfo;
}
// Populate the spectra-detector map
NXDetector detgroup = inst.openNXDetector("detector");
// Spectra block - Contains spectrum numbers for each workspace index
// This might not exist so wrap and check. If it doesn't exist create a
// default mapping
try {
NXInt spectra_block = detgroup.openNXInt("spectra");
spectra_block.load();
spectraInfo.spectraNumbers = spectra_block.vecBuffer();
spectraInfo.nSpectra = spectra_block.dim0();
spectraInfo.hasSpectra = true;
} catch (std::runtime_error &) {
spectraInfo.hasSpectra = false;
}
// Read necessary arrays from the file
// Detector list contains a list of all of the detector numbers. If it not
// present then we can't update the spectra
// map
try {
NXInt detlist_group = detgroup.openNXInt("detector_list");
detlist_group.load();
spectraInfo.detectorList = detlist_group.vecBuffer();
} catch (std::runtime_error &) {
logger.information() << "detector_list block not found. The workspace will "
"not contain any detector information.\n";
return spectraInfo;
}
// Detector count contains the number of detectors associated with each
// spectra
NXInt det_count = detgroup.openNXInt("detector_count");
det_count.load();
spectraInfo.detectorCount = det_count.vecBuffer();
// Detector index - contains the index of the detector in the workspace
NXInt det_index = detgroup.openNXInt("detector_index");
det_index.load();
spectraInfo.nSpectra = det_index.dim0();
spectraInfo.detectorIndex = det_index.vecBuffer();
return spectraInfo;
}
/**
* Is this file from a well-formed multiperiod group workspace.
* @param nWorkspaceEntries : Number of entries in the group workspace
* @param sampleWS : Sample workspace to inspect the logs of
* @param log : Information logger object
* @return True only if multiperiod.
*/
bool isMultiPeriodFile(int nWorkspaceEntries, const Workspace_sptr &sampleWS, Logger &log) {
bool isMultiPeriod = false;
if (ExperimentInfo_sptr expInfo = std::dynamic_pointer_cast<ExperimentInfo>(sampleWS)) {
const std::string nPeriodsLogEntryName = "nperiods";
const Run &run = expInfo->run();
if (run.hasProperty(nPeriodsLogEntryName)) {
const auto nPeriods = run.getPropertyValueAsType<int>(nPeriodsLogEntryName);
if (nPeriods == nWorkspaceEntries) {
isMultiPeriod = true;
log.information("Loading as MultiPeriod group workspace.");
}
}
}
return isMultiPeriod;
}
} // namespace
/// Default constructor
LoadNexusProcessed::LoadNexusProcessed()
: m_shared_bins(false), m_xbins(0), m_axis1vals(), m_list(false), m_interval(false), m_spec_min(0),
m_spec_max(Mantid::EMPTY_INT()), m_spec_list(), m_filtered_spec_idxs(), m_nexusFile() {}
/// Destructor defined here so that NeXus::File can be forward declared
/// in header
LoadNexusProcessed::~LoadNexusProcessed() {}
/**
* Return the confidence with with this algorithm can load the file
* @param descriptor A descriptor for the file
* @returns An integer specifying the confidence level. 0 indicates it will not
* be used
*/
int LoadNexusProcessed::confidence(Kernel::NexusDescriptor &descriptor) const {
if (descriptor.pathExists("/mantid_workspace_1"))
return 80;
else
return 0;
}
void LoadNexusProcessed::readSpectraToDetectorMapping(NXEntry &mtd_entry, Mantid::API::MatrixWorkspace &ws) {
readInstrumentGroup(mtd_entry, ws);
}
/** Initialisation method.
*
*/
void LoadNexusProcessed::init() {
// Declare required input parameters for algorithm
const std::vector<std::string> exts{".nxs", ".nx5", ".xml"};
declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Load, exts),
"The name of the Nexus file to read, as a full or relative path.");
declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("OutputWorkspace", "", Direction::Output),
"The name of the workspace to be created as the output of "
"the algorithm. A workspace of this name will be created "
"and stored in the Analysis Data Service. For multiperiod "
"files, one workspace may be generated for each period. "
"Currently only one workspace can be saved at a time so "
"multiperiod Mantid files are not generated.");
// optional
auto mustBePositive = std::make_shared<BoundedValidator<int>>();
mustBePositive->setLower(0);
// Use a static cast as MSVC sometimes gets confused and casts as int64
declareProperty("SpectrumMin", static_cast<int>(1), mustBePositive, "Number of first spectrum to read.");
declareProperty("SpectrumMax", static_cast<int>(Mantid::EMPTY_INT()), mustBePositive,
"Number of last spectrum to read.");
declareProperty(std::make_unique<ArrayProperty<int>>("SpectrumList"), "List of spectrum numbers to read.");
declareProperty("EntryNumber", static_cast<int>(0), mustBePositive,
"0 indicates that every entry is loaded, into a separate "
"workspace within a group. "
"A positive number identifies one entry to be loaded, into "
"one workspace");
declareProperty("LoadHistory", true, "If true, the workspace history will be loaded");
declareProperty(std::make_unique<PropertyWithValue<bool>>("FastMultiPeriod", true, Direction::Input),
"For multiperiod workspaces. Copy instrument, parameter and x-data "
"rather than loading it directly for each workspace. Y, E and log "
"information is always loaded.");
}
/**
* Loading specifically for mulitperiod group workspaces
* @param root : NXRoot ref
* @param entryName : Entry name to load.
* @param tempMatrixWorkspace : Template workspace to base the next multiperiod
* entry off.
* @param nWorkspaceEntries : N entries in the file
* @param p : index + 1 being processed.
* @return Next multiperiod group workspace
*/
Workspace_sptr LoadNexusProcessed::doAccelleratedMultiPeriodLoading(NXRoot &root, const std::string &entryName,
MatrixWorkspace_sptr &tempMatrixWorkspace,
const size_t nWorkspaceEntries, const size_t p) {
MatrixWorkspace_sptr periodWorkspace = WorkspaceFactory::Instance().create(tempMatrixWorkspace);
const size_t nHistograms = periodWorkspace->getNumberHistograms();
for (size_t i = 0; i < nHistograms; ++i) {
periodWorkspace->setSharedX(i, tempMatrixWorkspace->sharedX(i));
}
// We avoid using `openEntry` or similar here because they're just wrappers
// around `open`. `open` is slow for large multiperiod datasets, because it
// does a search upon the entire HDF5 tree. `openLocal` is *much* quicker, as
// it only searches the current group. It does, however, require that the
// parent group is currently open.
// Words of Warning: While the openLocal construct is an optimization,
// it is very dangerous. Forgetting to close an entry of an NXEntry in a
// completely unrelated part of the code can result in us opening the
// wrong NXEntry here!
NXEntry mtdEntry(root, entryName);
mtdEntry.openLocal();
NXData wsEntry(mtdEntry, "workspace");
if (!wsEntry.openLocal()) {
std::stringstream buffer;
buffer << "Group entry " << p - 1
<< " is not a workspace 2D. Retry with "
"FastMultiPeriod option set off.\n";
throw std::runtime_error(buffer.str());
}
if (wsEntry.isValid("frac_area")) {
std::stringstream buffer;
buffer << "Group entry " << p - 1
<< " has fractional area present. Try "
"reloading with FastMultiPeriod set "
"off.\n";
throw std::runtime_error(buffer.str());
}
NXDataSetTyped<double> data(wsEntry, "values");
data.openLocal();
NXDataSetTyped<double> errors(wsEntry, "errors");
errors.openLocal();
const int nChannels = data.dim1();
int blockSize = 8; // Read block size. Set to 8 for efficiency. i.e. read
// 8 histograms at a time.
int nFullBlocks =
static_cast<int>(nHistograms) / blockSize; // Truncated number of full blocks to read. Remainder removed
const int readOptimumStop = (nFullBlocks * blockSize);
const int readStop = m_spec_max - 1;
const int finalBlockSize = readStop - readOptimumStop;
int wsIndex = 0;
int histIndex = m_spec_min - 1;
for (; histIndex < readStop;) {
if (histIndex >= readOptimumStop) {
blockSize = finalBlockSize;
}
data.load(blockSize, histIndex);
errors.load(blockSize, histIndex);
double *dataStart = data();
double *dataEnd = dataStart + nChannels;
double *errorStart = errors();
double *errorEnd = errorStart + nChannels;
int final(histIndex + blockSize);
while (histIndex < final) {
auto &Y = periodWorkspace->mutableY(wsIndex);
Y.assign(dataStart, dataEnd);
dataStart += nChannels;
dataEnd += nChannels;
auto &E = periodWorkspace->mutableE(wsIndex);
E.assign(errorStart, errorEnd);
errorStart += nChannels;
errorEnd += nChannels;
++wsIndex;
++histIndex;
}
}
// We always start one layer too deep
// go from /workspace_{n}/{something} -> /workspace_{n}
m_nexusFile->closeGroup();
// Now move to the correct period group
// /workspace_{n} -> /workspace_{n+1}
m_nexusFile->closeGroup();
m_nexusFile->openGroup(entryName, "NXentry");
try {
// This loads logs, sample, and instrument.
periodWorkspace->loadSampleAndLogInfoNexus(m_nexusFile.get());
} catch (std::exception &e) {
g_log.information("Error loading Instrument section of nxs file");
g_log.information(e.what());
}
// We make sure to close the current entries. Failing to do this can cause
// strange off-by-one errors when loading the spectra.
wsEntry.close();
mtdEntry.close();
const double fractionComplete = double(p - 1) / double(nWorkspaceEntries);
progress(fractionComplete, "Loading multiperiod entry");
return periodWorkspace;
}
//-------------------------------------------------------------------------------------------------
/** Executes the algorithm. Reading in the file and creating and populating
* the output workspace
*
* @throw runtime_error Thrown if algorithm cannot execute
*/
void LoadNexusProcessed::exec() {
API::Workspace_sptr tempWS;
int nWorkspaceEntries = 0;
// Start scoped block
{
progress(0, "Opening file...");
// Throws an approriate exception if there is a problem with file access
const std::string filename = getPropertyValue("Filename");
NXRoot root(filename);
// "Open" the same file but with the C++ interface
m_nexusFile = std::make_unique<::NeXus::File>(root.m_fileID);
// Find out how many first level entries there are
// Cast down to int as another property later on is an int
nWorkspaceEntries = static_cast<int>((root.groups().size()));
// Check for an entry number property
int entrynumber = getProperty("EntryNumber");
Property const *const entryNumberProperty = this->getProperty("EntryNumber");
bool bDefaultEntryNumber = entryNumberProperty->isDefault();
if (!bDefaultEntryNumber && entrynumber > nWorkspaceEntries) {
g_log.error() << "Invalid entry number specified. File only contains " << nWorkspaceEntries << " entries.\n";
throw std::invalid_argument("Invalid entry number specified.");
}
const std::string basename = "mantid_workspace_";
std::ostringstream os;
if (bDefaultEntryNumber) {
// Set the entry number to 1 if not provided.
entrynumber = 1;
}
os << basename << entrynumber;
const std::string targetEntryName = os.str();
// Take the first real workspace obtainable. We need it even if loading
// groups.
tempWS = loadEntry(root, targetEntryName, 0, 1);
if (nWorkspaceEntries == 1 || !bDefaultEntryNumber) {
// We have what we need.
applyLogFiltering(tempWS);
setProperty("OutputWorkspace", tempWS);
} else {
// We already know that this is a group workspace. Is it a true
// multiperiod workspace.
const bool bFastMultiPeriod = this->getProperty("FastMultiPeriod");
const bool bIsMultiPeriod = isMultiPeriodFile(nWorkspaceEntries, tempWS, g_log);
Property *specListProp = this->getProperty("SpectrumList");
m_list = !specListProp->isDefault();
// Load all first level entries
auto wksp_group = std::make_shared<WorkspaceGroup>();
// This forms the name of the group
std::string base_name = getPropertyValue("OutputWorkspace");
// First member of group should be the group itself, for some reason!
// load names of each of the workspaces. Note that if we have duplicate
// names then we don't select them
auto names = extractWorkspaceNames(root, static_cast<size_t>(nWorkspaceEntries));
// remove existing workspace and replace with the one being loaded
bool wsExists = AnalysisDataService::Instance().doesExist(base_name);
if (wsExists) {
Algorithm_sptr alg = AlgorithmManager::Instance().createUnmanaged("DeleteWorkspace");
alg->initialize();
alg->setChild(true);
alg->setProperty("Workspace", base_name);
alg->execute();
}
base_name += "_";
const std::string prop_name = "OutputWorkspace_";
MatrixWorkspace_sptr tempMatrixWorkspace = std::dynamic_pointer_cast<Workspace2D>(tempWS);
bool bAccelleratedMultiPeriodLoading = false;
if (tempMatrixWorkspace) {
// We only accelerate for simple scenarios for now. Spectrum lists are
// too complicated to bother with.
bAccelleratedMultiPeriodLoading = bIsMultiPeriod && bFastMultiPeriod && !m_list;
// Strip out any loaded logs. That way we don't pay for copying that
// information around.
tempMatrixWorkspace->mutableRun().clearLogs();
}
if (bAccelleratedMultiPeriodLoading) {
g_log.information("Accelerated multiperiod loading");
} else {
g_log.information("Individual group loading");
}
for (int p = 1; p <= nWorkspaceEntries; ++p) {
const auto indexStr = std::to_string(p);
// decide what the workspace should be called
std::string wsName = buildWorkspaceName(names[p], base_name, p);
Workspace_sptr local_workspace;
/*
For multiperiod workspaces we can accelerate the loading by making
resonable assumptions about the differences between the workspaces
Only Y, E and log data entries should vary. Therefore we can clone our
temp workspace, and overwrite those things we are interested in.
*/
if (bAccelleratedMultiPeriodLoading) {
local_workspace =
doAccelleratedMultiPeriodLoading(root, basename + indexStr, tempMatrixWorkspace, nWorkspaceEntries, p);
} else // Fall-back for generic loading
{
const auto nWorkspaceEntries_d = static_cast<double>(nWorkspaceEntries);
local_workspace = loadEntry(root, basename + indexStr, static_cast<double>(p - 1) / nWorkspaceEntries_d,
1. / nWorkspaceEntries_d);
}
applyLogFiltering(local_workspace);
declareProperty(
std::make_unique<WorkspaceProperty<API::Workspace>>(prop_name + indexStr, wsName, Direction::Output));
wksp_group->addWorkspace(local_workspace);
setProperty(prop_name + indexStr, local_workspace);
}
// The group is the root property value
setProperty("OutputWorkspace", std::static_pointer_cast<Workspace>(wksp_group));
}
root.close();
} // All file resources should be scoped to here. All previous file handles
// must be cleared to release locks
loadNexusGeometry(*tempWS, nWorkspaceEntries, g_log, std::string(getProperty("Filename")));
m_axis1vals.clear();
} // namespace DataHandling
/**
* Decides what to call a child of a group workspace.
*
* This function builds the workspace name based on either a workspace name
* which was stored in the file or the base name.
*
* @param name :: The name loaded from the file (possibly the empty string if
*none was loaded)
* @param baseName :: The name group workspace
* @param wsIndex :: The current index of this workspace
*
* @return The name of the workspace
*/
std::string LoadNexusProcessed::buildWorkspaceName(const std::string &name, const std::string &baseName,
size_t wsIndex) {
std::string wsName;
std::string index = std::to_string(wsIndex);
if (!name.empty()) {
wsName = name;
} else {
// we have a common stem so rename accordingly
boost::smatch results;
const boost::regex exp(".*_(\\d+$)");
// if we have a common name stem then name is <OutputWorkspaceName>_n
if (boost::regex_search(name, results, exp)) {
wsName = baseName + std::string(results[1].first, results[1].second);
} else {
// if the name property wasn't defined just use <OutputWorkspaceName>_n
wsName = baseName + index;
}
}
correctForWorkspaceNameClash(wsName);
return wsName;
}
/**
* Append an index to the name if it already exists in the AnalysisDataService
*
* @param wsName :: Name to call the workspace
*/
void LoadNexusProcessed::correctForWorkspaceNameClash(std::string &wsName) {
bool noClash(false);
for (int i = 0; !noClash; ++i) {
std::string wsIndex; // dont use an index if there is no other
// workspace
if (i > 0) {
wsIndex = "_" + std::to_string(i);
}
bool wsExists = AnalysisDataService::Instance().doesExist(wsName + wsIndex);
if (!wsExists) {
wsName += wsIndex;
noClash = true;
}
}
}
/**
* Extract the workspace names from the file (if any are stored)
*
* @param root :: the root for the NeXus document
* @param nWorkspaceEntries :: the number of workspace entries
*/
std::vector<std::string> LoadNexusProcessed::extractWorkspaceNames(NXRoot &root, size_t nWorkspaceEntries) {
std::vector<std::string> names(nWorkspaceEntries + 1);
for (size_t p = 1; p <= nWorkspaceEntries; ++p) {
auto period = std::to_string(p);
names[p] = loadWorkspaceName(root, "mantid_workspace_" + period);
}
// Check that there are no duplicates in the workspace name
// This can cause severe problems
auto it = std::unique(names.begin(), names.end());
if (it != names.end()) {
auto size = names.size();
names.clear();
names.resize(size);
}
return names;
}
/**
* Load the workspace name, if the attribute exists
*
* @param root :: Root of NeXus file
* @param entry_name :: Entry in NeXus file to look at
* @return The workspace name. If none found an empty string is returned.
*/
std::string LoadNexusProcessed::loadWorkspaceName(NXRoot &root, const std::string &entry_name) {
NXEntry mtd_entry = root.openEntry(entry_name);
std::string workspaceName = std::string();
try {
workspaceName = mtd_entry.getString("workspace_name");
} catch (std::runtime_error &) {
}
mtd_entry.close();
return workspaceName;
}
//-------------------------------------------------------------------------------------------------
/**
* Load an event_workspace field
*
* @param wksp_cls Nexus data for "event_workspace"
* @param xbins bins on the "X" axis
* @param progressStart algorithm progress (from 0)
* @param progressRange progress made after loading an entry
*
* @return event_workspace object with data
*/
API::MatrixWorkspace_sptr LoadNexusProcessed::loadEventEntry(NXData &wksp_cls, NXDouble &xbins,
const double &progressStart, const double &progressRange) {
NXDataSetTyped<int64_t> indices_data = wksp_cls.openNXDataSet<int64_t>("indices");
indices_data.load();
size_t numspec = indices_data.dim0() - 1;
// process optional spectrum parameters, if set
checkOptionalProperties(numspec);
// Actual number of spectra in output workspace (if only a user-specified
// range and/or list was going to be loaded)
numspec = calculateWorkspaceSize(numspec, true);
int num_xbins = xbins.dim0();
if (xbins.rank() == 2) {
num_xbins = xbins.dim1();
}
if (num_xbins < 2)
num_xbins = 2;
EventWorkspace_sptr ws = std::dynamic_pointer_cast<EventWorkspace>(
WorkspaceFactory::Instance().create("EventWorkspace", numspec, num_xbins, num_xbins - 1));
// Set the YUnit label
ws->setYUnit(indices_data.attributes("units"));
std::string unitLabel = indices_data.attributes("unit_label");
if (unitLabel.empty())
unitLabel = indices_data.attributes("units");
ws->setYUnitLabel(unitLabel);
// Handle optional fields.
// TODO: Handle inconsistent sizes
std::vector<int64_t> pulsetimes;
if (wksp_cls.isValid("pulsetime")) {
NXDataSetTyped<int64_t> pulsetime = wksp_cls.openNXDataSet<int64_t>("pulsetime");
pulsetime.load();
pulsetimes = pulsetime.vecBuffer();
}
std::vector<double> tofs;
if (wksp_cls.isValid("tof")) {
NXDouble tof = wksp_cls.openNXDouble("tof");
tof.load();
tofs = tof.vecBuffer();
}
std::vector<float> error_squareds;
if (wksp_cls.isValid("error_squared")) {
NXFloat error_squared = wksp_cls.openNXFloat("error_squared");
error_squared.load();
error_squareds = error_squared.vecBuffer();
}
std::vector<float> weights;
if (wksp_cls.isValid("weight")) {
NXFloat weight = wksp_cls.openNXFloat("weight");
weight.load();
weights = weight.vecBuffer();
}
// What type of event lists?
EventType type = TOF;
if (!tofs.empty() && !pulsetimes.empty() && !weights.empty() && !error_squareds.empty())
type = WEIGHTED;
else if ((!tofs.empty() && !weights.empty() && !error_squareds.empty()))
type = WEIGHTED_NOTIME;
else if (!pulsetimes.empty() && !tofs.empty())
type = TOF;
else
throw std::runtime_error("Could not figure out the type of event list!");
// indices of events
std::vector<int64_t> indices = indices_data.vecBuffer();
// Create all the event lists
auto max = static_cast<int64_t>(m_filtered_spec_idxs.size());
Progress progress(this, progressStart, progressStart + progressRange, max);
PARALLEL_FOR_NO_WSP_CHECK()
for (int64_t j = 0; j < max; ++j) {
PARALLEL_START_INTERUPT_REGION
size_t wi = m_filtered_spec_idxs[j] - 1;
int64_t index_start = indices[wi];
int64_t index_end = indices[wi + 1];
if (index_end >= index_start) {
EventList &el = ws->getSpectrum(j);
el.switchTo(type);
// Allocate all the required memory
el.reserve(index_end - index_start);
el.clearDetectorIDs();
for (int64_t i = index_start; i < index_end; i++)
switch (type) {
case TOF:
el.addEventQuickly(TofEvent(tofs[i], DateAndTime(pulsetimes[i])));
break;
case WEIGHTED:
el.addEventQuickly(WeightedEvent(tofs[i], DateAndTime(pulsetimes[i]), weights[i], error_squareds[i]));
break;
case WEIGHTED_NOTIME:
el.addEventQuickly(WeightedEventNoTime(tofs[i], weights[i], error_squareds[i]));
break;
}
// Set the X axis
if (this->m_shared_bins)
el.setHistogram(this->m_xbins);
else {
MantidVec x(xbins.dim1());
for (int i = 0; i < xbins.dim1(); i++)
x[i] = xbins(static_cast<int>(wi), i);
// Workspace and el was just created, so we can just set a new histogram
// We can move x as it is not longer used after this point
el.setHistogram(HistogramData::BinEdges(std::move(x)));
}
}
progress.report();
PARALLEL_END_INTERUPT_REGION
}
PARALLEL_CHECK_INTERUPT_REGION
return ws;
}
//-------------------------------------------------------------------------------------------------
/**
* Load a numeric column to the TableWorkspace.
* @param tableData :: Table data to load from
* @param dataSetName :: Name of the data set to use to get column data
* @param tableWs :: Workspace to add column to
* @param columnType :: Name of the column type to create
*/
template <typename ColumnType, typename NexusType>
void LoadNexusProcessed::loadNumericColumn(const Mantid::NeXus::NXData &tableData, const std::string &dataSetName,
const API::ITableWorkspace_sptr &tableWs, const std::string &columnType) {
NXDataSetTyped<NexusType> data = tableData.openNXDataSet<NexusType>(dataSetName);
std::string columnTitle = data.attributes("name");
if (!columnTitle.empty()) {
data.load();
auto length = static_cast<size_t>(data.dim0());
auto rowCount = tableWs->rowCount();
// check that the row count is OK
if (rowCount == 0) {
tableWs->setRowCount(length);
} else if (rowCount != length) {
throw std::runtime_error("Columns have different sizes.");
}
// copy the data
auto column = tableWs->addColumn(columnType, columnTitle);
for (size_t i = 0; i < length; i++) {
column->cell<ColumnType>(i) = static_cast<ColumnType>(*(data() + i));
}
}
}
//-------------------------------------------------------------------------------------------------
/**
* Load a table
*/
API::Workspace_sptr LoadNexusProcessed::loadTableEntry(NXEntry &entry) {
API::ITableWorkspace_sptr workspace;
workspace = Mantid::API::WorkspaceFactory::Instance().createTable("TableWorkspace");
NXData nx_tw = entry.openNXData("table_workspace");
int columnNumber = 1;
do {
std::string dataSetName = "column_" + std::to_string(columnNumber);
NXInfo info = nx_tw.getDataSetInfo(dataSetName);
if (info.stat == NX_ERROR) {
// Assume we done last column of table
break;
}
if (info.rank == 1) {
if (info.type == NX_FLOAT64) {
loadNumericColumn<double, double>(nx_tw, dataSetName, workspace, "double");
} else if (info.type == NX_INT32) {
loadNumericColumn<int, int32_t>(nx_tw, dataSetName, workspace, "int");
} else if (info.type == NX_UINT32) {
loadNumericColumn<uint32_t, uint32_t>(nx_tw, dataSetName, workspace, "uint");
} else if (info.type == NX_INT64) {
loadNumericColumn<int64_t, int64_t>(nx_tw, dataSetName, workspace, "long64");
} else if (info.type == NX_UINT64) {
loadNumericColumn<size_t, uint64_t>(nx_tw, dataSetName, workspace, "size_t");
} else if (info.type == NX_FLOAT32) {
loadNumericColumn<float, float>(nx_tw, dataSetName, workspace, "float");
} else if (info.type == NX_UINT8) {
loadNumericColumn<bool, bool>(nx_tw, dataSetName, workspace, "bool");
} else {
throw std::logic_error("Column with Nexus data type " + std::to_string(info.type) + " cannot be loaded.");
}
} else if (info.rank == 2) {
if (info.type == NX_CHAR) {
NXChar data = nx_tw.openNXChar(dataSetName);
std::string columnTitle = data.attributes("name");
if (!columnTitle.empty()) {
workspace->addColumn("str", columnTitle);
int nRows = info.dims[0];
workspace->setRowCount(nRows);
const int maxStr = info.dims[1];
data.load();
for (int iR = 0; iR < nRows; ++iR) {
auto &cellContents = workspace->cell<std::string>(iR, columnNumber - 1);
auto startPoint = data() + maxStr * iR;
cellContents.assign(startPoint, startPoint + maxStr);
boost::trim_right(cellContents);
}
}
} else if (info.type == NX_INT32) {
loadVectorColumn<int>(nx_tw, dataSetName, workspace, "vector_int");
} else if (info.type == NX_FLOAT64) {
auto data = nx_tw.openNXDouble(dataSetName);
if (data.attributes("interpret_as") == "V3D") {
loadV3DColumn(data, workspace);
} else {
loadVectorColumn<double>(nx_tw, dataSetName, workspace, "vector_double");
}
}
}
columnNumber++;
} while (true);
return std::static_pointer_cast<API::Workspace>(workspace);
}
/**
* Loads a vector column to the TableWorkspace.
* @param tableData :: Table data to load from
* @param dataSetName :: Name of the data set to use to get column data
* @param tableWs :: Workspace to add column to
* @param columnType :: Name of the column type to create
*/
template <typename Type>
void LoadNexusProcessed::loadVectorColumn(const NXData &tableData, const std::string &dataSetName,
const ITableWorkspace_sptr &tableWs, const std::string &columnType) {
NXDataSetTyped<Type> data = tableData.openNXDataSet<Type>(dataSetName);
std::string columnTitle = data.attributes("name");
if (!columnTitle.empty()) {
tableWs->addColumn(columnType, columnTitle);
NXInfo info = tableData.getDataSetInfo(dataSetName);
const size_t rowCount = info.dims[0];
const size_t blockSize = info.dims[1];
// This might've been done already, but doing it twice should't do any harm
tableWs->setRowCount(rowCount);
data.load();
for (size_t i = 0; i < rowCount; ++i) {
auto &cell = tableWs->cell<std::vector<Type>>(i, tableWs->columnCount() - 1);
Type *from = data() + blockSize * i;
cell.assign(from, from + blockSize);
std::ostringstream rowSizeAttrName;
rowSizeAttrName << "row_size_" << i;
// This is ugly, but I can only get attribute as a string using the API
std::istringstream rowSizeStr(data.attributes(rowSizeAttrName.str()));
int rowSize;
rowSizeStr >> rowSize;
cell.resize(rowSize);
}
}
}
/**
* Loads a V3D column to the TableWorkspace.
* @param data :: Table data to load from
* @param tableWs :: Workspace to add column to
*/
void LoadNexusProcessed::loadV3DColumn(Mantid::NeXus::NXDouble &data, const API::ITableWorkspace_sptr &tableWs) {
std::string columnTitle = data.attributes("name");
if (!columnTitle.empty()) {
ColumnVector<V3D> col = tableWs->addColumn("V3D", columnTitle);
const int rowCount = data.dim0();
// This might've been done already, but doing it twice should't do any harm
tableWs->setRowCount(rowCount);
data.load();
for (int i = 0; i < rowCount; ++i) {
auto &cell = col[i];
cell(data(i, 0), data(i, 1), data(i, 2));
}
}
}
/**
* @brief Load LeanElasticPeakWorkspace
*
* @param entry
* @return API::Workspace_sptr
*/
API::Workspace_sptr LoadNexusProcessed::loadLeanElasticPeaksEntry(NXEntry &entry) {
g_log.notice("Load as LeanElasticPeaks");
// API::IPeaksWorkspace_sptr workspace;
API::ITableWorkspace_sptr tWorkspace;
// PeaksWorkspace_sptr workspace;
tWorkspace = Mantid::API::WorkspaceFactory::Instance().createTable("LeanElasticPeaksWorkspace");
IPeaksWorkspace_sptr peakWS = std::dynamic_pointer_cast<LeanElasticPeaksWorkspace>(tWorkspace);
NXData nx_tw = entry.openNXData("peaks_workspace");
int columnNumber = 1;
int numberPeaks = 0;
std::vector<std::string> columnNames;
do {
std::string str = "column_" + std::to_string(columnNumber);
NXInfo info = nx_tw.getDataSetInfo(str);
if (info.stat == NX_ERROR) {
// Assume we done last column of table
break;
}
// store column names
columnNames.emplace_back(str);
// determine number of peaks
// here we assume that a peaks_table has always one column of doubles
if (info.type == NX_FLOAT64) {
NXDouble nxDouble = nx_tw.openNXDouble(str);
std::string columnTitle = nxDouble.attributes("name");
if (!columnTitle.empty() && numberPeaks == 0) {
numberPeaks = nxDouble.dim0();
}
}
columnNumber++;
} while (true);
// Get information from all but data group
std::string parameterStr;
// Hop to the right point /mantid_workspace_1
try {
m_nexusFile->openPath(entry.path()); // This is
} catch (std::runtime_error &re) {
throw std::runtime_error("Error while opening a path in a Peaks entry in a "
"Nexus processed file. "
"This path is wrong: " +
entry.path() + ". Lower level error description: " + re.what());
}
try {
// This loads logs, sample, and instrument.
peakWS->loadExperimentInfoNexus(getPropertyValue("Filename"), m_nexusFile.get(), parameterStr);
// Populate the instrument parameters in this workspace
peakWS->readParameterMap(parameterStr);
} catch (std::exception &e) {
g_log.information("Error loading Instrument section of nxs file");
g_log.information(e.what());
}
// Coordinates - Older versions did not have the separate field but used a log
// value
const std::string peaksWSName = "peaks_workspace";
try {
m_nexusFile->openGroup(peaksWSName, "NXentry");
} catch (std::runtime_error &re) {
throw std::runtime_error("Error while opening a peaks workspace in a Nexus processed file. "
"Cannot open gropu " +
peaksWSName + ". Lower level error description: " + re.what());
}