Mantid
Loading...
Searching...
No Matches
isisraw2.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#include "isisraw2.h"
8#include "byte_rel_comp.h"
9#include <cstdio>
10#include <exception>
11
13#include "MantidKernel/Logger.h"
14#include <boost/lexical_cast.hpp>
15
16namespace {
19} // namespace
20
22ISISRAW2::ISISRAW2() : ISISRAW(nullptr, false), ndes(0), outbuff(nullptr), m_bufferSize(0) {
23 // Determine the size of the output buffer to create from the config service.
24 g_log.debug("Determining ioRaw buffer size\n");
25 auto bufferSizeConfigVal = Mantid::Kernel::ConfigService::Instance().getValue<int>("loadraw.readbuffer.size");
26
27 if (!bufferSizeConfigVal.is_initialized()) {
28 m_bufferSize = 200000;
29 g_log.debug() << "loadraw.readbuffer.size not found, setting to " << m_bufferSize << "\n";
30 } else {
31 m_bufferSize = bufferSizeConfigVal.get();
32 g_log.debug() << "loadraw.readbuffer.size set to " << m_bufferSize << "\n";
33 }
34}
35
43int ISISRAW2::ioRAW(FILE *file, bool from_file, bool read_data) {
44 (void)read_data; // Avoid compiler warning
45
46 int i;
47 fpos_t add_pos, dhdr_pos;
48 if (!from_file) {
49 add.ad_run = 32;
50 add.ad_inst = add.ad_run + 94;
51 add.ad_se = add.ad_inst + 70 + 2 * i_mon + (5 + i_use) * i_det;
52 add.ad_dae = add.ad_se + 66 + e_nse * 32;
53 add.ad_tcb = add.ad_dae + 65 + 5 * i_det;
54 add.ad_user = add.ad_tcb + 288 + (t_ntc1 + 1);
55 add.ad_data = add.ad_user + 2 + u_len;
56 add.ad_log = 0; // we don't know it yet
57 add.ad_end = 0;
58 }
59 ISISRAW::ioRAW(file, &hdr, 1, from_file);
60 ISISRAW::ioRAW(file, &frmt_ver_no, 1, from_file);
61 fgetpos(file, &add_pos);
62 ISISRAW::ioRAW(file, &add, 1, from_file);
63 ISISRAW::ioRAW(file, &data_format, 3, from_file);
64 ISISRAW::ioRAW(file, r_title, 80, from_file);
65 ISISRAW::ioRAW(file, &user, 1, from_file);
66 ISISRAW::ioRAW(file, &rpb, 1, from_file);
67 ISISRAW::ioRAW(file, &ver3, 1, from_file);
68 ISISRAW::ioRAW(file, i_inst, 8, from_file);
69 ISISRAW::ioRAW(file, &ivpb, 1, from_file);
70 ISISRAW::ioRAW(file, &i_det, 3, from_file);
71 ISISRAW::ioRAW(file, &mdet, i_mon, from_file);
72 ISISRAW::ioRAW(file, &monp, i_mon, from_file);
73 ISISRAW::ioRAW(file, &spec, i_det, from_file);
74 ISISRAW::ioRAW(file, &delt, i_det, from_file);
75 ISISRAW::ioRAW(file, &len2, i_det, from_file);
76 ISISRAW::ioRAW(file, &code, i_det, from_file);
77 ISISRAW::ioRAW(file, &tthe, i_det, from_file);
78 ISISRAW::ioRAW(file, &ut, i_use * i_det, from_file);
79 ISISRAW::ioRAW(file, &ver4, 1, from_file);
80 ISISRAW::ioRAW(file, &spb, 1, from_file);
81 ISISRAW::ioRAW(file, &e_nse, 1, from_file);
82 ISISRAW::ioRAW(file, &e_seblock, e_nse, from_file);
83 ISISRAW::ioRAW(file, &ver5, 1, from_file);
84 ISISRAW::ioRAW(file, &daep, 1, from_file);
85 ISISRAW::ioRAW(file, &crat, i_det, from_file);
86 ISISRAW::ioRAW(file, &modn, i_det, from_file);
87 ISISRAW::ioRAW(file, &mpos, i_det, from_file);
88 ISISRAW::ioRAW(file, &timr, i_det, from_file);
89 ISISRAW::ioRAW(file, &udet, i_det, from_file);
90 ISISRAW::ioRAW(file, &ver6, 267, from_file);
91 ISISRAW::ioRAW(file, &(t_tcp1[0][0]), 20, from_file);
92 ISISRAW::ioRAW(file, &t_pre1, 1, from_file);
93 ISISRAW::ioRAW(file, &t_tcb1, t_ntc1 + 1, from_file);
94 ISISRAW::ioRAW(file, &ver7, 1, from_file);
95 // it appear that the VMS ICP traditionally sets u_len to 1 regardless
96 // of its real size; thus we cannot rely on it for reading and must instead
97 // use section offsets
98 i = 0;
99 ISISRAW::ioRAW(file, &i, 1, from_file);
100 if (from_file) {
101 u_len = add.ad_data - add.ad_user - 2;
102
103 if (u_len < 0 || (add.ad_data < add.ad_user + 2)) {
104 // this will/would be used for memory allocation
105 g_log.error() << "Error in u_len value read from file, it would be " << u_len
106 << "; where it is calculated as "
107 "u_len = ad_data - ad_user - 2, where ad_data: "
108 << add.ad_data << ", ad_user: " << add.ad_user << "\n";
109 throw std::runtime_error("Inconsistent value for the field u_len found");
110 }
111 }
112
113 ISISRAW::ioRAW(file, &u_dat, u_len, from_file);
114 ISISRAW::ioRAW(file, &ver8, 1, from_file);
115 fgetpos(file, &dhdr_pos);
116 ISISRAW::ioRAW(file, &dhdr, 1, from_file);
117
118 if (!outbuff)
119 outbuff = new char[m_bufferSize];
120 ndes = t_nper * (t_nsp1 + 1);
121 ISISRAW::ioRAW(file, &ddes, ndes, true);
122 if (!dat1)
123 dat1 = new uint32_t[t_ntc1 + 1]; // space for just one spectrum
124 // so when we round up words we get a zero written
125 memset(outbuff, 0, m_bufferSize);
126
127 return 0; // stop reading here
128}
129
133void ISISRAW2::skipData(FILE *file, int i) {
134 if (i < ndes) {
135 int zero = fseek(file, 4 * ddes[i].nwords, SEEK_CUR);
136 if (0 != zero) {
137 g_log.warning() << "Failed to skip data from file, with value: " << i << "\n";
138 }
139 }
140}
141
146bool ISISRAW2::readData(FILE *file, int i) {
147 if (i >= ndes)
148 return false;
149 int nwords = 4 * ddes[i].nwords;
150 if (nwords > m_bufferSize) {
151 g_log.debug() << "Overflow error, nwords > buffer size. nwords = " << nwords << ", buffer=" << m_bufferSize << "\n";
152 throw std::overflow_error("LoadRaw input file buffer too small for "
153 "selected data. Try increasing the "
154 "\"loadraw.readbuffer.size\" user property.");
155 }
156 int res = ISISRAW::ioRAW(file, outbuff, nwords, true);
157 if (res != 0)
158 return false;
159 byte_rel_expn(outbuff, nwords, 0, reinterpret_cast<int *>(dat1), t_ntc1 + 1);
160 return true;
161}
162
164 if (outbuff)
165 delete[] outbuff;
166}
167
170 if (outbuff)
171 delete[] outbuff;
172}
int byte_rel_expn(const char *data_in, int n_in, int n_from, int *data_out, int n_out)
~ISISRAW2() override
Definition: isisraw2.cpp:163
int m_bufferSize
Definition: isisraw2.h:27
char * outbuff
output buffer
Definition: isisraw2.h:26
int ndes
ndes
Definition: isisraw2.h:24
ISISRAW2()
No arg Constructor.
Definition: isisraw2.cpp:22
void skipData(FILE *file, int i)
Skip data.
Definition: isisraw2.cpp:133
int ioRAW(FILE *file, bool from_file, bool read_data=true) override
Loads the headers of the file, leaves the file pointer at a specific position.
Definition: isisraw2.cpp:43
bool readData(FILE *file, int i)
Read data.
Definition: isisraw2.cpp:146
void clear()
Clears the output buffer.
Definition: isisraw2.cpp:169
isis raw file.
Definition: isisraw.h:272
HDR_STRUCT hdr
header block (80 bytes)
Definition: isisraw.h:282
float * ut
nuse UT* user tables (total size NUSE*NDET) ut01=phi
Definition: isisraw.h:307
int i_use
number of user defined UTn tables NUSE
Definition: isisraw.h:298
SPB_STRUCT spb
sample parameter block (64*4 bytes)
Definition: isisraw.h:310
float * delt
hold off table (size NDET)
Definition: isisraw.h:303
int * modn
module number for each detector (size NDET)
Definition: isisraw.h:319
float t_tcp1[5][4]
time channel parameters
Definition: isisraw.h:332
int e_nse
number of controlled SEPs NSEP
Definition: isisraw.h:311
char i_inst[8]
instrument name
Definition: isisraw.h:294
uint32_t * dat1
compressed data for (NTC1+1)*(NSP1+1)*NPER values
Definition: isisraw.h:346
int i_det
number of detectors NDET
Definition: isisraw.h:296
DAEP_STRUCT daep
DAE parameter block (size 64*4 bytes)
Definition: isisraw.h:316
DDES_STRUCT * ddes
(NSP1+1)*NPER items, totoal size (NSP1+1)*NPER*2*4 bytes
Definition: isisraw.h:345
int * code
code for UTn tables (size NDET)
Definition: isisraw.h:305
int t_nper
number of periods
Definition: isisraw.h:327
DHDR_STRUCT dhdr
size 32*4 bytes
Definition: isisraw.h:342
int * t_tcb1
time channel boundaries in clock pulses (size NTC1+1)
Definition: isisraw.h:334
int t_nsp1
number of spectra in time regime 1
Definition: isisraw.h:329
int * mdet
detector number for monitors (size NMON)
Definition: isisraw.h:300
int ver3
instrument section version number (=2)
Definition: isisraw.h:293
int ver5
DAE section version number (=2)
Definition: isisraw.h:315
int ver7
user version number (=1)
Definition: isisraw.h:337
int * timr
time regime for each detector (size NDET)
Definition: isisraw.h:321
USER_STRUCT user
user information (8*20 bytes)
Definition: isisraw.h:290
int i_mon
number of monitors NMON
Definition: isisraw.h:297
int * crat
crate number for each detector (size NDET)
Definition: isisraw.h:318
virtual int ioRAW(FILE *file, bool from_file, bool read_data=true)
stuff
Definition: isisraw.cpp:401
int ver4
SE section version number (=2)
Definition: isisraw.h:309
int ver8
data version number (=2)
Definition: isisraw.h:341
float * u_dat
user defined data (ULEN, max size 400 words)
Definition: isisraw.h:339
int data_format
data section format (0 = by TC, 1 = by spectrum)
Definition: isisraw.h:285
char r_title[80]
run title
Definition: isisraw.h:289
int * mpos
module position for each detector (size NDET)
Definition: isisraw.h:320
int ver6
TCB section version number (=1)
Definition: isisraw.h:324
IVPB_STRUCT ivpb
instrument parameter block (64*4 bytes)
Definition: isisraw.h:295
int * udet
user detector number for each detector (size NDET)
Definition: isisraw.h:322
int * monp
prescale value for each monitor (size NMON)
Definition: isisraw.h:301
int u_len
length of user data section
Definition: isisraw.h:338
RPB_STRUCT rpb
run parameter block (32*4 bytes)
Definition: isisraw.h:291
int * spec
spectrum number table (size NDET)
Definition: isisraw.h:302
SE_STRUCT * e_seblock
NSEP SE parameter blocks (total size NSEP*32*4 bytes)
Definition: isisraw.h:313
struct ADD_STRUCT add
9*4 bytes
Definition: isisraw.h:284
int t_ntc1
number of time channels in time regime 1
Definition: isisraw.h:330
float * tthe
2theta scattering angle (size NDET)
Definition: isisraw.h:306
float * len2
L2 table (size NDET)
Definition: isisraw.h:304
int t_pre1
prescale for 32MHz clock
Definition: isisraw.h:333
int frmt_ver_no
format version number VER1 (=2)
Definition: isisraw.h:283
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition: Logger.h:52
void debug(const std::string &msg)
Logs at debug level.
Definition: Logger.cpp:114
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
Kernel::Logger g_log("ExperimentInfo")
static logger object
int ad_dae
ad_se + 66 + nse*32
Definition: isisraw.h:39
int ad_user
ad_tcb + 288 + (ntc1 + 1)
Definition: isisraw.h:41
int ad_data
ad_user + 2 + ulen
Definition: isisraw.h:42
int ad_tcb
ad_dae + 65 + 5*ndet
Definition: isisraw.h:40
int ad_run
32 (1+offset_of(ver2))
Definition: isisraw.h:36
int ad_end
1+end of file
Definition: isisraw.h:44
int ad_inst
ad_run + 94
Definition: isisraw.h:37
int ad_log
ad_data + 33 +
Definition: isisraw.h:43
int ad_se
ad_inst + 70 + 2*nmon+(5+nuse)*ndet
Definition: isisraw.h:38
int nwords
number of compressed words in spectrum
Definition: isisraw.h:247