ICP  1
nexuswriter.h
Go to the documentation of this file.
1 #ifndef NEXUSWRITER_H
2 #define NEXUSWRITER_H
3 
4 typedef void (*nx_error_t)(void* arg, char* text);
5 
6 #include "napi.h"
7 #include "NeXusFile.hpp"
8 #include "NeXusStream.hpp"
9 
10 #include "selogger.h"
11 
12 extern const char* detector_base;
13 extern const char* detector_format;
14 
15 class IXNeXusFile : public ISIS::Base
16 {
17  NXhandle m_fileid;
18  int m_options;
22  void* m_error_arg;
23 
24 public:
25 
26  int clone(const IXNeXusFile& orig);
27  IXNeXusFile* clone();
28 
29  int flush()
30  {
31  return NXflush(&m_fileid);
32  }
33 
34  template<typename T>
35  int writeData(const char* data_name, const std::vector<T>& value)
36  {
37  if (value.size() > 0)
38  {
39  return writeData(data_name, &(value[0]), static_cast<int>(value.size()));
40  }
41  else
42  {
43  return writeData(data_name, static_cast<T*>(NULL), 0);
44  }
45  }
46 
47  static bool string_len_lt(const std::string& s1, const std::string& s2)
48  {
49  return s1.size() < s2.size();
50  }
51 
52  template<>
53  int writeData(const char* data_name, const std::vector<std::string>& value)
54  {
55  int dims_array[2];
56  std::vector<std::string>::const_iterator it = std::max_element(value.begin(), value.end(), string_len_lt);
57  if (value.size() > 0 && it != value.end())
58  {
59  dims_array[0] = static_cast<int>(value.size());
60  dims_array[1] = static_cast<int>(it->size());
61  int n = dims_array[0] * dims_array[1];
62  char* char_array = new char[n];
63  memset(char_array, ' ', n);
64  for(int i=0; i<dims_array[0]; i++)
65  {
66  strncpy(char_array + i * dims_array[1], value[i].c_str(), value[i].size());
67  }
68  writeData(data_name, char_array, dims_array[0], dims_array[1]);
69  delete[] char_array;
70  return 0;
71  }
72  else
73  {
74  return 0;
75  }
76  }
77 
78 
79  enum Options { None = 0x0, WriteDummyOnNULL = 0x1, Read = 0x2, Write = 0x4, HDF4 = 0x8, HDF5 = 0x10, Update = 0x20 };
80  enum NXReturnCodes { OK = NX_OK, Error = NX_ERROR };
81  IXNeXusFile(nx_error_t error_func = NULL, void* error_arg = NULL);
82  void setErrorReporter(nx_error_t error_func, void* error_arg);
83  int open(const char* filename, int mode, int type = IXNeXusFile::HDF5);
84  int close();
85  int closeData() { return NXclosedata(m_fileid); }
86  int openData(const char* name) { return NXopendata(m_fileid, name); }
87  int getLink(const char* name, const char* nxclass, NXlink& link);
88  int makeLink(const char* name, const char* nxclass, NXlink& link);
89  int getLink(const char* name, NXlink& link) { return getLink(name, NULL, link); }
90  int makeLink(const char* name, NXlink& link) { return makeLink(name, NULL, link); }
91  int writeData(const char* data_name, int value);
92  int writeData(const char* data_name, unsigned value) { return writeData(data_name, (int)value); }
93  int writeData(const char* data_name, long value) { return writeData(data_name, (int)value); }
94  int writeData(const char* data_name, unsigned long value) { return writeData(data_name, (long)value); }
95  int writeData(const char* data_name, float value);
96  int writeData(const char* data_name, double value);
97  int writeData(const char* data_name, int64_t value);
98  int writeData(const char* data_name, uint64_t value);
99  int writeData(const std::string& data_name, const std::string& value)
100  {
101  return writeData(data_name.c_str(), value.c_str());
102  }
103  int writeAttribute(const std::string& data_name, const std::string& attr_name, const std::string& value)
104  {
105  return writeAttribute(data_name.c_str(), attr_name.c_str(), value.c_str());
106  }
107 
108  int writeAttribute(const char* data_name, const char* attr_name, int value);
109  int writeAttribute(const char* data_name, const char* attr_name, float value);
110  int writeAttribute(const char* data_name, const char* attr_name, double value);
111  int writeData(const char* data_name, const int64_t* value, const int* dims_array, int ndims);
112  int writeData(const char* data_name, const uint64_t* value, const int* dims_array, int ndims);
113  int writeData(const char* data_name, const int* value, const int* dims_array, int ndims);
114  int writeData(const char* data_name, const unsigned int* value, const int* dims_array, int ndims);
115  int writeData(const char* data_name, const float* value, const int* dims_array, int ndims);
116  int writeData(const char* data_name, const double* value, const int* dims_array, int ndims);
117  int writeData(const char* data_name, const int64_t* value, int len);
118  int writeData(const char* data_name, const uint64_t* value, int len);
119  int writeData(const char* data_name, const int* value, int len);
120  int writeData(const char* data_name, const unsigned int* value, int len);
121  int writeData(const char* data_name, const float* value, int len);
122  int writeData(const char* data_name, const double* value, int len);
123  int writeData(const char* data_name, const int* value, int dim1, int dim2);
124  int writeData(const char* data_name, const unsigned long* value, int dim1, int dim2) { return writeData(data_name, (const int*)value, dim1, dim2); }
125  int writeData(const char* data_name, const float* value, int dim1, int dim2);
126  int writeData(const char* data_name, const double* value, int dim1, int dim2);
127  int writeData(const char* data_name, const char* value);
128  int writeData(const char* data_name, const char* value, int len);
129  int writeData(const char* data_name, const char* value, int dim1, int dim2);
130  int makeData(const char* data_name, int nxtype, const int* dims_array, int ndims);
131  int makeDataChunked(const char* data_name, int nxtype, const int* dims_array, const int* chunk_array, int ndims);
132  int writeSlabList(const char* data_name, const unsigned long* value, const int* dims_array, int ndims, const int* slab_start, const int* slab_size, const std::vector<int>& slab_list);
133  int addSlabList(const char* data_name, const unsigned long* value, const int* dims_array, int ndims, int dest_start[], const int* slab_start, const int* slab_size, const std::vector<int>& slab_list);
134  int makeDataSlab(const char* data_name, const unsigned long* value, const int* dims_array, int ndims);
135  int writeSlab(const char* data_name, const int* value, const int* dims_array, int ndims, const int* slab_start, const int* slab_size);
136  int writeSlab(const char* data_name, const unsigned long* value, const int* dims_array, int ndims, const int* slab_start, const int* slab_size) { return writeSlab(data_name, (const int*)value, dims_array, ndims, slab_start, slab_size); }
137  int addSlab(const char* data_name, const int* value, const int* dims_array, int ndims, const int* slab_start, const int* slab_size, const int* dest_start);
138  int addSlab(const char* data_name, const unsigned long* value, const int* dims_array, int ndims, const int* slab_start, const int* slab_size, const int* dest_start);
139  int addSlab(const char* data_name, const float* value, const int* dims_array, int ndims, const int* slab_start, const int* slab_size, const int* dest_start);
140  int addSlab(const int* value, const int* dims_array, int ndims, const int* slab_start, const int* slab_size, const int* dest_start);
141  int addSlab(const unsigned long* value, const int* dims_array, int ndims, const int* slab_start, const int* slab_size, const int* dest_start);
142  int addSlab(const float* value, const int* dims_array, int ndims, const int* slab_start, const int* slab_size, const int* dest_start);
143  int addSlab(const int* value, const int64_t* dims_array, int ndims, const int64_t* slab_start, const int64_t* slab_size, const int64_t* dest_start);
144  int addSlab(const float* value, const int64_t* dims_array, int ndims, const int64_t* slab_start, const int64_t* slab_size, const int64_t* dest_start);
145  int writeAttribute(const char* data_name, const char* attr_name, const int* value, int len);
146  int writeAttribute(const char* data_name, const char* attr_name, const float* value, int len);
147  int writeAttribute(const char* data_name, const char* attr_name, const double* value, int len);
148  int writeAttribute(const char* data_name, const char* attr_name, const char* value, int len);
149  int writeAttribute(const char* data_name, const char* attr_name, const char* value);
150  int addLog(const char* nxname, const char* filename,
151  const char* log_name, const char* log_units, time_t ref_time);
152  template<typename T>
153  int addLog(const char* nxname, const std::vector<T>& values, const char* value_units,
154  const std::vector<float>& time, const char* time_units, time_t ref_time);
155 
156 // int writeMuonFile(const char* filename, const ISISCRPT_STRUCT* crpt);
157  int makeNewGroup(const char* name, const char* nxclass, bool open_group = true);
158  int makeNewGroup(const std::string& name, const std::string& nxclass, bool open_group = true) { return makeNewGroup(name.c_str(), nxclass.c_str(), open_group); }
159 
160  int closeGroup();
161  const char* ISOtime(time_t time);
162  time_t unixTime(const char* isotime);
163  int enableOptions(Options options) { return (m_options |= options); }
164  void setSpectraChunkSize(int nsp) { m_spectra_chunk = nsp; }
165  void enableCompression(int comp_level = NX_COMP_LZW) { m_compression_type = comp_level; }
166  void disableCompression() { m_compression_type = NX_COMP_NONE; }
167  bool testOptions(Options options) { return ((m_options & options) != 0); }
168 
169  int readData(const char* data_name, std::string& value);
170  int readData(const char* data_name, int& value);
171  int readData(const char* data_name, float& value);
172  int listEntries(std::list<std::string>& entries, bool groups_only = false);
173  int openGroup(const char* group_name, const char* group_class);
174  NXhandle fileid() { return m_fileid; }
175 };
176 
177 class ISISRAW;
178 struct ISISCRPT_STRUCT;
179 
180 class NeXusWriter : public ISIS::Base
181 {
182 public:
183  NeXusWriter() : Base("NeXusWriter") , m_run_number_digits(8) { }
184  void setRunNumberDigits(int digits) { m_run_number_digits = digits; }
185  int writeMuonFile(const char* filename, const ISISCRPT_STRUCT* crpt, const isisU32_t* raw_data, isisU32_t raw_data_size, const seblock_map_t& blocks, const Poco::Path& inst_settings_dir, const std::string& inst_xml_file, const std::string& inst_parameter_map_file, DAEstatus& dstatus);
186  int writeISISNeXus(const char* filename, bool logs_only, const ISISCRPT_STRUCT* crpt, const isisU32_t* raw_data, isisU32_t raw_data_size, const ISISRAW* iraw, const seblock_map_t& blocks, const std::string& inst_xml_file, const std::string& inst_parameter_map_file, DAEstatus& dstatus, bool update = false);
187  int writeISISNeXus(const char* filename, bool logs_only, const ISISCRPT_STRUCT* crpt, const isisU32_t* raw_data, isisU32_t raw_data_size, const ISISRAW* iraw, const seblock_map_t& blocks, const std::string& events_nxfile, const std::string& inst_xml_file, const std::string& inst_parameter_map_file, DAEstatus& dstatus);
188  int addMissingMuonLogs(const char* filename, const char* logs_path, const char* new_comment, const char* new_sample_name, DAEstatus& dstatus);
189 private:
190  static void readExternalFile(const std::string& file_name, std::string& file_contents);
193 };
194 
195 template <typename T>
196 class NXlogWriter : public ISIS::Base
197 {
198 private:
199  time_t m_offset;
200  std::string m_value_units;
201  std::vector<float> m_time;
202  std::vector<T> m_value;
203 public:
204  NXlogWriter(const std::string& value_units, time_t offset) : m_offset(offset), m_value_units(value_units) { setLoggerName("NXlogWriter"); }
205  void addValue(float time, T value) { m_time.push_back(time); m_value.push_back(value); }
206  void write(IXNeXusFile* file, const std::string& name) { file->addLog(name.c_str(), m_value, m_value_units.c_str(), m_time, "second", m_offset); }
207  void setOffset(time_t offset) { m_offset = offset; }
208 };
209 
210 #endif /* NEXUSWRITER_H */
int m_spectra_chunk
Definition: nexuswriter.h:20
NXhandle fileid()
Definition: nexuswriter.h:174
int writeData(const char *data_name, unsigned long value)
Definition: nexuswriter.h:94
int openGroup(const char *group_name, const char *group_class)
nx_error_t m_error_func
Definition: nexuswriter.h:21
void setErrorReporter(nx_error_t error_func, void *error_arg)
const char * ISOtime(time_t time)
int listEntries(std::list< std::string > &entries, bool groups_only=false)
int readData(const char *data_name, std::string &value)
IXNeXusFile * clone()
NXlogWriter(const std::string &value_units, time_t offset)
Definition: nexuswriter.h:204
int open(const char *filename, int mode, int type=IXNeXusFile::HDF5)
int makeNewGroup(const std::string &name, const std::string &nxclass, bool open_group=true)
Definition: nexuswriter.h:158
int getLink(const char *name, NXlink &link)
Definition: nexuswriter.h:89
int makeNewGroup(const char *name, const char *nxclass, bool open_group=true)
int flush()
Definition: nexuswriter.h:29
int makeData(const char *data_name, int nxtype, const int *dims_array, int ndims)
unsigned long isisU32_t
Definition: isisvme_types.h:8
int writeSlabList(const char *data_name, const unsigned long *value, const int *dims_array, int ndims, const int *slab_start, const int *slab_size, const std::vector< int > &slab_list)
std::map< std::string, SEBLOCK > seblock_map_t
Definition: selogger.h:128
int writeData(const char *data_name, const std::vector< std::string > &value)
Definition: nexuswriter.h:53
int addLog(const char *nxname, const char *filename, const char *log_name, const char *log_units, time_t ref_time)
void addValue(float time, T value)
Definition: nexuswriter.h:205
time_t m_offset
Definition: nexuswriter.h:199
NXhandle m_fileid
Definition: nexuswriter.h:17
time_t unixTime(const char *isotime)
const char * detector_format
int makeDataSlab(const char *data_name, const unsigned long *value, const int *dims_array, int ndims)
int makeDataChunked(const char *data_name, int nxtype, const int *dims_array, const int *chunk_array, int ndims)
std::vector< float > m_time
Definition: nexuswriter.h:201
int addMissingMuonLogs(const char *filename, const char *logs_path, const char *new_comment, const char *new_sample_name, DAEstatus &dstatus)
int closeGroup()
static bool string_len_lt(const std::string &s1, const std::string &s2)
Definition: nexuswriter.h:47
void disableCompression()
Definition: nexuswriter.h:166
std::vector< T > m_value
Definition: nexuswriter.h:202
int writeSlab(const char *data_name, const int *value, const int *dims_array, int ndims, const int *slab_start, const int *slab_size)
void * m_error_arg
Definition: nexuswriter.h:22
int writeAttribute(const std::string &data_name, const std::string &attr_name, const std::string &value)
Definition: nexuswriter.h:103
int writeData(const char *data_name, const unsigned long *value, int dim1, int dim2)
Definition: nexuswriter.h:124
void setSpectraChunkSize(int nsp)
Definition: nexuswriter.h:164
static void readExternalFile(const std::string &file_name, std::string &file_contents)
void(* nx_error_t)(void *arg, char *text)
Definition: nexuswriter.h:4
void setRunNumberDigits(int digits)
Definition: nexuswriter.h:184
int enableOptions(Options options)
Definition: nexuswriter.h:163
void setOffset(time_t offset)
Definition: nexuswriter.h:207
int writeMuonFile(const char *filename, const ISISCRPT_STRUCT *crpt, const isisU32_t *raw_data, isisU32_t raw_data_size, const seblock_map_t &blocks, const Poco::Path &inst_settings_dir, const std::string &inst_xml_file, const std::string &inst_parameter_map_file, DAEstatus &dstatus)
const char * detector_base
int getLink(const char *name, const char *nxclass, NXlink &link)
int writeISISNeXus(const char *filename, bool logs_only, const ISISCRPT_STRUCT *crpt, const isisU32_t *raw_data, isisU32_t raw_data_size, const ISISRAW *iraw, const seblock_map_t &blocks, const std::string &inst_xml_file, const std::string &inst_parameter_map_file, DAEstatus &dstatus, bool update=false)
int m_run_number_digits
Definition: nexuswriter.h:191
int makeLink(const char *name, NXlink &link)
Definition: nexuswriter.h:90
void write(IXNeXusFile *file, const std::string &name)
Definition: nexuswriter.h:206
int writeSlab(const char *data_name, const unsigned long *value, const int *dims_array, int ndims, const int *slab_start, const int *slab_size)
Definition: nexuswriter.h:136
Base(const std::string &logger_name="UNKNOWN")
Definition: IsisBase.h:13
NeXusWriter(const NeXusWriter &)
Definition: nexuswriter.h:192
int openData(const char *name)
Definition: nexuswriter.h:86
IXNeXusFile(nx_error_t error_func=NULL, void *error_arg=NULL)
bool testOptions(Options options)
Definition: nexuswriter.h:167
int writeData(const std::string &data_name, const std::string &value)
Definition: nexuswriter.h:99
void setLoggerName(const std::string &logger_name)
Definition: IsisBase.h:17
int writeData(const char *data_name, const std::vector< T > &value)
Definition: nexuswriter.h:35
int addSlabList(const char *data_name, const unsigned long *value, const int *dims_array, int ndims, int dest_start[], const int *slab_start, const int *slab_size, const std::vector< int > &slab_list)
void enableCompression(int comp_level=NX_COMP_LZW)
Definition: nexuswriter.h:165
int makeLink(const char *name, const char *nxclass, NXlink &link)
int addSlab(const char *data_name, const int *value, const int *dims_array, int ndims, const int *slab_start, const int *slab_size, const int *dest_start)
std::string m_value_units
Definition: nexuswriter.h:200
int writeData(const char *data_name, unsigned value)
Definition: nexuswriter.h:92
int m_compression_type
Definition: nexuswriter.h:19
int closeData()
Definition: nexuswriter.h:85
int writeData(const char *data_name, long value)
Definition: nexuswriter.h:93