ICP  1
isiscrpt.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 
3 #include "isiscrpt.h"
4 #include "isisraw.h"
5 
6 
11 static const char* inst_abrv_mappings[][2] = {
12  { "SURF", "SRF" },
13  { "INES", "INS" },
14  { "CRISP", "CSP" },
15 // { "ENGINX", "ENG" },
16  { "IRIS", "IRS" },
17 // { "OSIRIS", "OSI" },
18  { "MAPS", "MAP" },
19  { "MARI", "MAR" },
20  { "MERLIN", "MER" },
21  { "TOSCA", "TSC" },
22  { "HRPD", "HRP" },
23 // { "PEARL", "PRL" },
24  { "POLARIS", "POL" },
25  { "SANDALS", "SLS" },
26  { "PRISMA", "PRS" },
27  { "ROTAX", "RTX" },
28  { "VESUVIO", "VES" }, // EVS at the moment
29  { NULL, NULL }
30 };
31 
32 // short names only used for the ISIS VMS RAW files that are generated on neutron instruments
33 static const char* getInstAbrv(const char* inst_name)
34 {
35  static char buffer[256];
36  int i;
37  if (getenv("SHORTNAME") != NULL)
38  {
39  strncpy(buffer, getenv("SHORTNAME"), sizeof(buffer));
40  return buffer;
41  }
42  strncpy(buffer, inst_name, sizeof(buffer));
43  for(i=0; inst_abrv_mappings[i][0] != NULL; i++)
44  {
45  if (stricmp(inst_name, inst_abrv_mappings[i][0]) == 0)
46  {
47  strncpy(buffer, inst_abrv_mappings[i][1], sizeof(buffer));
48  }
49  }
50  NULL_TERMINATE(buffer);
51  return buffer;
52 }
53 
54 ISISCRPT_STRUCT::ISISCRPT_STRUCT(ISISDAE::DAEType dae_type_, const std::string& comp_name)
55 {
56 // TCHAR buffer[MAX_COMPUTERNAME_LENGTH+1];
57 // DWORD nbuffer = MAX_COMPUTERNAME_LENGTH+1;
58  memset(this, 0, sizeof(ISISCRPT_STRUCT));
63  dae_type = dae_type_;
65 // GetComputerName(buffer, &nbuffer);
66  setInstName(comp_name);
67  strncpy(long_title, "long title", sizeof(long_title));
68  time_scaler = 6;
69  ntrg = 1; // number of time regimes (=1)
70  nper = 1; // number of periods
71  nper_daq = 1;
72  nperseq = 0; // number of period sequences
73  run_number = 1;
74  nsp[0] = 2;
75  ntc[0] = 1;
76  persize = (nsp[0] + 1) * (ntc[0] + 1);
77  timr_dae[0] = timr_crpt[0] = timr_file[0] = 1; // without wiring tables, all spectra/detectors go to index 0 and we need a valid Time regime
78 // auto_save_frames = 5000;
79  auto_save_value = 5000;
80  period_output_delay = 20000;
81  software_period = 0; // first
82  update_poll_time = 5;
83  log_nlines = 0;
84  magic = 0x12345678;
85  monitoring.n = 5;
86  crpt_size = sizeof(ISISCRPT_STRUCT);
87 
88  uamp_scale = 1.0;
89 
90 // tcb_maxtime = 400000; // max time dae can handle
91  tcb_maxtime = 16 * 1000 * 1000; // max time dae can handle - set to 16 seconds for TS2
92  tcb_maxdelay = ((1 << 18) - 1); // max delay on frame clock in microseconds, 18 bit register
93 
95 
96  ICPClock* the_clock = g_icp_clock.get();
97  the_clock->getParameters(icp_clock.perf_counter, icp_clock.proc_freq, icp_clock.filetime);
98 
99  markUninitialised(); // as we only have default values
100 }
101 
103 {
105  {
106  clock_frequency = 2;
107  electronics_delay = 0;
108  tcb_mintime = 0.0;
109  }
110  else // ISISDAE::NeutronDAE2 || ISISDAE::NeutronDAE3
111  {
112  clock_frequency = 32; // dae clock frequency (MHz) 32 for DAE2
113  electronics_delay = 7; // get 0.21us for free (7 clock pulses) on neutron dae2
114 // tcb_mintime = 5.0f + (float)electronics_delay / (float)clock_frequency; // lowest time dae can handle
115  tcb_mintime = 5.0; // lowest time dae can handle
116  }
117  tcb_minwidth = 0.25; // minimum channel width dae can handle (0.25)
118  if (getenv("ISISICP_MIN_TCBWIDTH") != NULL)
119  {
120  tcb_minwidth = static_cast<float>(atof(getenv("ISISICP_MIN_TCBWIDTH"))); // minimum channel width dae can handle
121  }
122  return 0;
123 }
124 
125 // copy one string to another and pad out the destination string with spaces
126 // if it is longer
127 int spacePadCopy(char* output, const char* input, int output_size)
128 {
129  int i, n = 0;
130  while( (n < output_size) && (input[n] != '\0') )
131  {
132  output[n] = input[n];
133  n++;
134  }
135  for(i=n; i<output_size; i++)
136  {
137  output[i] = ' ';
138  }
139  return 0;
140 }
141 
142 int spacePadCopyVa(char* output, int output_size, const char* format, ... )
143 {
144  va_list args;
145  int i, n;
146  va_start(args, format);
147  n = vsnprintf(output, output_size, format, args);
148  va_end(args);
149  if (n >= 0)
150  {
151  for(i=n; i<output_size; i++)
152  {
153  output[i] = ' ';
154  }
155  }
156  return 0;
157 }
158 
159 
160 
161 int spaceTrimCopy(char* output, int output_size, const char* input, int input_size)
162 {
163  int n = 0;
164  n = input_size;
165  while( (n > 0) && (input[n-1] == ' ') )
166  {
167  n--;
168  }
169  if (n >= output_size)
170  {
171  n = output_size - 1;
172  }
173  strncpy(output, input, n);
174  output[n] = '\0';
175  return 0;
176 }
177 
178 int ISISCRPT_STRUCT::getCharItem(const char* item_name, long spec, std::string& cvalue, DAEstatus& status) const
179 {
180  int i;
181  char ctemp[256];
182  memset(ctemp, ' ', sizeof(ctemp));
183 #include "isiscrpt_items.h"
184  for(i=0; i< sizeof(char_items) / sizeof(char_item); i++)
185  {
186  if (!strcmp(item_name, char_items[i].name))
187  {
188  cvalue = char_items[i].val;
189  return DAEstatus::Success;
190  }
191  }
192  if (!strcmp(item_name, "HDR"))
193  {
194  strncpy(&(ctemp[0]), this->inst_abrv, 3);
195  sprintf(&(ctemp[3]), "%05d", this->run_number % VMS_RUN_MASK);
196  strncpy(&(ctemp[8]), this->user_name, 20);
197  strncpy(&(ctemp[28]), this->long_title, 24);
198  ISISRAW::vmstime(&(ctemp[52]), 21, this->stop_time); // use 21 rather than 20 to allow NULL termination
199  sprintf(&(ctemp[72]), "%8.2f", this->update_good_uamph);
200  for(i=0; i<80; i++)
201  {
202  if (ctemp[i] == '\0')
203  {
204  ctemp[i] = ' ';
205  }
206  }
207  ctemp[80] = '\0';
208  cvalue = ctemp;
209  return DAEstatus::Success;
210  }
211  if (!strcmp(item_name, "CRPB"))
212  {
213  ISISRAW::vmstime(&(ctemp[64]), 21, this->stop_time); // use 21 rather than 20 to allow NULL termination
214  for(i=0; i<128; i++)
215  {
216  if (ctemp[i] == '\0')
217  {
218  ctemp[i] = ' ';
219  }
220  }
221  ctemp[128] = '\0';
222  cvalue = ctemp;
223  return DAEstatus::Success;
224  }
225  return DAEstatus::Failure;
226 }
227 
228 int ISISCRPT_STRUCT::getCharItem(const char* item_name, std::string& cvalue, DAEstatus& status) const
229 {
230  return getCharItem(item_name, 1, cvalue, status);
231 }
232 
233 int ISISCRPT_STRUCT::getIntItem(const char* item_name, long& lVal, DAEstatus& status) const
234 {
235  std::string sitem_name;
236  std::string tmp_item;
237  size_t n;
238  long spec_no;
239  // handle case of item_spectrum which means we average the array
240  // item[ndet] for that particular spectrum
241  sitem_name = item_name;
242  n = sitem_name.find('_');
243  if (n != std::string::npos)
244  {
245  spec_no = atol(item_name+n+1);
246  tmp_item = sitem_name.substr(0,n);
247  return getIntItem(tmp_item.c_str(), &spec_no, 1, &lVal, status);
248  }
249  else
250  {
251  spec_no = 0;
252  return getIntItem(item_name, &spec_no, 0, &lVal, status);
253  }
254 }
255 
256 // nspec number of 0 means no spec average
257 int ISISCRPT_STRUCT::getIntItem(const char* item_name, const long* spec_array, int nspec, long* lVal, DAEstatus& status) const
258 {
259  int i, j, n;
260  std::string sitem_name;
261  std::string tmp_item;
262  const int* pVal = NULL;
263 // lVal = -1000;
264 #include "isiscrpt_items.h"
265  for(i=0; i< sizeof(int_items) / sizeof(int_item); i++)
266  {
267  if (!strcmp(item_name, int_items[i].name))
268  {
269  for(j=0; j < (nspec == 0 ? 1 : nspec); j++)
270  {
271  lVal[j] = *(int_items[i].pi);
272  }
273  return DAEstatus::Success;
274  }
275  }
276  if (nspec == 0)
277  {
278  return DAEstatus::Failure;
279  }
280  tmp_item = item_name;
281  pVal = NULL;
282  for(i=0; i< sizeof(int_array_items) / sizeof(int_array_item); i++)
283  {
284  if (!tmp_item.compare(int_array_items[i].name))
285  {
286  if (int_array_items[i].dim1 == 0)
287  {
288  n = *(int_array_items[i].dim0);
289  }
290  else
291  {
292  n = *(int_array_items[i].dim0) * *(int_array_items[i].dim1);
293  }
294  if (n == this->ndet)
295  {
296  pVal = int_array_items[i].pi;
297  }
298  }
299  }
300  if (pVal == NULL)
301  {
302  return DAEstatus::Failure;
303  }
304  for(j=0; j<nspec; j++)
305  {
306  lVal[j] = 0;
307  n = 0;
308  for(i=0; i<this->ndet; i++)
309  {
310  if (this->spec[i] == spec_array[j])
311  {
312  lVal[j] += pVal[i];
313  n++;
314  }
315  }
316  if (n > 0)
317  {
318  lVal[j] = lVal[j] / n;
319  }
320  }
321  return DAEstatus::Success;
322 }
323 
324 int ISISCRPT_STRUCT::getRealItem(const char* item_name, const long* spec_array, int nspec, double* dblVal, DAEstatus& status) const
325 {
326  int i, j, n;
327  std::string sitem_name;
328  std::string tmp_item;
329  const float* pVal = NULL;
330 // dblVal = -1000.0;
331 #include "isiscrpt_items.h"
332  for(i=0; i< sizeof(real_items) / sizeof(real_item); i++)
333  {
334  if (!strcmp(item_name, real_items[i].name))
335  {
336  for(j=0; j < (nspec == 0 ? 1 : nspec); j++)
337  {
338  dblVal[j] = *(real_items[i].pf);
339  }
340  return DAEstatus::Success;
341  }
342  }
343  if (nspec == 0)
344  {
345  return DAEstatus::Failure;
346  }
347  tmp_item = item_name;
348  pVal = NULL;
349  for(i=0; i< sizeof(real_array_items) / sizeof(real_array_item); i++)
350  {
351  if (!tmp_item.compare(real_array_items[i].name))
352  {
353  if (real_array_items[i].dim1 == 0)
354  {
355  n = *(real_array_items[i].dim0);
356  }
357  else
358  {
359  n = *(real_array_items[i].dim0) * *(real_array_items[i].dim1);
360  }
361  if (n == this->ndet)
362  {
363  pVal = real_array_items[i].pf;
364  }
365  }
366  }
367  if (pVal == NULL)
368  {
369  return DAEstatus::Failure;
370  }
371  for(j=0; j<nspec; j++)
372  {
373  dblVal[j] = 0.0;
374  n = 0;
375  for(i=0; i<this->ndet; i++)
376  {
377  if (this->spec[i] == spec_array[j])
378  {
379  dblVal[j] += pVal[i];
380  n++;
381  }
382  }
383  if (n > 0)
384  {
385  dblVal[j] = dblVal[j] / (double)n;
386  }
387  }
388  return DAEstatus::Success;
389 }
390 
391 int ISISCRPT_STRUCT::getRealItem(const char* item_name, double& dblVal, DAEstatus& status) const
392 {
393  size_t n;
394  long spec_no;
395  std::string sitem_name;
396  std::string tmp_item;
397  dblVal = -1000.0;
398  // handle case of item_spectrum which means we average the arrat item[ndet] for
399  // that particular spectrum
400  sitem_name = item_name;
401  n = sitem_name.find('_');
402  if (n == std::string::npos)
403  {
404  spec_no = 0;
405  return getRealItem(item_name, &spec_no, 0, &dblVal, status);
406  }
407  else
408  {
409  spec_no = atol(item_name+n+1);
410  tmp_item = sitem_name.substr(0,n);
411  return getRealItem(tmp_item.c_str(), &spec_no, 1, &dblVal, status);
412  }
413 }
414 
415 int ISISCRPT_STRUCT::getIntArrayItemSize(const char* item_name, const long* spec_array, int nspec, int* dims_array, int& ndims, DAEstatus& status) const
416 {
417 #include "isiscrpt_items.h"
418  if ( !strcmp(item_name, "SPECTRUMT0") ) // spectrum including time bin 0
419  {
420  int ntc = 0;
421  for(int i=0; i<nspec; ++i)
422  {
423  ntc = std::max(ntc, this->spectrumNTC(spec_array[i]));
424  }
425  dims_array[0] = ntc + 1;
426  ndims = 1;
427  return DAEstatus::Success;
428  }
429  return DAEstatus::Failure;
430 }
431 
432 int ISISCRPT_STRUCT::getIntArrayItemSize(const char* item_name, int* dims_array, int& ndims, DAEstatus& status) const
433 {
434  int spec_from, spec_to;
435  std::string str;
436  SplitItem split_item;
437  splitItemName(item_name, split_item);
438 #include "isiscrpt_items.h"
439  if ( !strncmp(item_name, "CNT", 3) && (strlen(item_name) >= 5) && (item_name[4] == '[') ) // e.g. CNT1[53:789]
440  {
441  long tr = atol(item_name + 3);
442  str = item_name;
443  if (parseSpectraRange(str, spec_from, spec_to) == DAEstatus::Failure)
444  {
445  return DAEstatus::Failure;
446  }
447  dims_array[0] = spec_to - spec_from + 1;
448  dims_array[1] = (this->ntc[tr-1] + 1);
449  ndims = 2;
450  return DAEstatus::Success;
451  }
452  if ( !strncmp(item_name, "CNT", 3) ) // CNT1
453  {
454  long tr = atol(item_name + 3);
455  dims_array[0] = this->getNumSpectra(tr, true) * this->nper;
456  dims_array[1] = (this->ntc[tr-1] + 1);
457  ndims = 2;
458  return DAEstatus::Success;
459  }
460  if ( !strcmp(item_name, "SPECTRUMT0") ) // spectrum including time bin 0
461  {
462  int ntc = 0;
463  for(int i=0; i<this->ntrg; ++i)
464  {
465  ntc = std::max(ntc, this->ntc[i]);
466  }
467  dims_array[0] = ntc + 1;
468  ndims = 1;
469  return DAEstatus::Success;
470  }
471  if ( !strncmp(item_name, "TCB", 3) && (strlen(item_name) > 3) ) // raw bin boundaries
472  {
473  long tr = atol(item_name + 3);
474  dims_array[0] = this->ntc[tr-1] + 1;
475  ndims = 1;
476  return DAEstatus::Success;
477  }
478  if (!strncmp(item_name, "SPECTRUM_", 9) || !strcmp(item_name, "SPECTRUM"))
479  {
480  dims_array[0] = this->spectrumNTC(split_item.number); // ignore bin 0
481  ndims = 1;
482  return DAEstatus::Success;
483  }
484  // isisraw compatability: 32 word structure of mixed int and real values
485  if (!strcmp(item_name, "IRPB"))
486  {
487  dims_array[0] = 32;
488  ndims = 1;
489  return DAEstatus::Success;
490  }
491  // isisraw compatability: 64 word structure of int values
492  if (!strcmp(item_name, "DAEP"))
493  {
494  dims_array[0] = 64;
495  ndims = 1;
496  return DAEstatus::Success;
497  }
498  for(int i=0; i< sizeof(int_array_items) / sizeof(int_array_item); i++)
499  {
500  if (!strcmp(item_name, int_array_items[i].name))
501  {
502  if (int_array_items[i].dim1 == 0)
503  {
504  dims_array[0] = *(int_array_items[i].dim0);
505  ndims = 1;
506  }
507  else
508  {
509  dims_array[0] = *(int_array_items[i].dim0);
510  dims_array[1] = *(int_array_items[i].dim1);
511  ndims = 2;
512  }
513  return DAEstatus::Success;
514  }
515  }
516  return DAEstatus::Failure;
517 }
518 
519 int ISISCRPT_STRUCT::getRealArrayItemSize(const char* item_name, int* dims_array, int& ndims, DAEstatus& status) const
520 {
521  SplitItem split_item;
522  splitItemName(item_name, split_item);
523 #include "isiscrpt_items.h"
524  if ( (!strncmp(item_name, "RSPECTRUM_", 10)) ||
525  (!strncmp(item_name, "RSPECERR_", 9)) ||
526  (!strcmp(item_name, "RSPECTRUM")) ||
527  (!strcmp(item_name, "RSPECERR")) )
528  {
529  dims_array[0] = spectrumNTC(split_item.number); // no bin 0
530  ndims = 1;
531  return DAEstatus::Success;
532  }
533  if ( !strncmp(item_name, "RTCB_", 5) )
534  {
535  dims_array[0] = spectrumNTC(split_item.number) + 1;
536  ndims = 1;
537  return DAEstatus::Success;
538  }
539  if ( !strncmp(item_name, "RTCB", 4) ) //RTCB1 etc
540  {
541  long tr = atol(item_name+4);
542  dims_array[0] = this->ntc[tr-1] + 1;
543  ndims = 1;
544  return DAEstatus::Success;
545  }
546  if ( !strncmp(item_name, "TIM", 3) ) // TIM1 etc
547  {
548  long tr = atol(item_name+3);
549  dims_array[0] = this->ntc[tr-1] + 1;
550  ndims = 1;
551  return DAEstatus::Success;
552  }
553  // isisraw compatability: 32 word structure of mixed int and real values
554  if (!strcmp(item_name, "RRPB"))
555  {
556  dims_array[0] = 32;
557  ndims = 1;
558  return DAEstatus::Success;
559  }
560  for(int i=0; i< sizeof(real_array_items) / sizeof(real_array_item); i++)
561  {
562  if (!strcmp(item_name, real_array_items[i].name))
563  {
564  if (real_array_items[i].dim1 == 0)
565  {
566  dims_array[0] = *(real_array_items[i].dim0);
567  ndims = 1;
568  }
569  else
570  {
571  dims_array[0] = *(real_array_items[i].dim0);
572  dims_array[1] = *(real_array_items[i].dim1);
573  ndims = 2;
574  }
575  return DAEstatus::Success;
576  }
577  }
578  return DAEstatus::Failure;
579 }
580 
581 int ISISCRPT_STRUCT::getIntArrayItem(const isisU32_t* raw_data, int raw_data_size, const char* item_name, const long* spec_array, int nspec, long* larray, DAEstatus& status) const
582 {
583  SplitItem split_item;
584  splitItemName(item_name, split_item);
585 #include "isiscrpt_items.h"
586  std::string str;
587  int i, j, k, n;
588  time_t now;
589  int spec_from, spec_to;
590  std::string sitem_name = item_name;
591  time(&now);
592  if ( !strncmp(item_name, "CNT", 3) && (strlen(item_name) >= 5) && (item_name[4] == '[') ) // e.g. CNT1[53:789]
593  {
594  long tr = atol(item_name + 3);
595  str = item_name;
596  if (parseSpectraRange(str, spec_from, spec_to) == DAEstatus::Failure)
597  {
598  return DAEstatus::Failure;
599  }
600  n = (this->ntc[tr-1] + 1) * (spec_to - spec_from + 1);
601  uint32_t offset = this->spectrumCRPTOffsetImpl(spec_from, 0, raw_data_size);
602  for(i=0; i<n ; i++)
603  {
604  larray[i] = raw_data[i + offset];
605  }
606  return DAEstatus::Success;
607  }
608  if ( !strncmp(item_name, "CNT", 3) ) // e.g. CNT1
609  {
610  long tr = atol(item_name + 3);
611  n = (this->ntc[tr-1] + 1) * (this->getNumSpectra(tr, true));
612  for(j=0; j<this->nper; ++j)
613  {
614  uint32_t offset = this->trCRPTOffset(tr, j, raw_data_size);
615  if (true)
616  {
617  for(i=0; i<n; ++i)
618  {
619  larray[i+n*j] = raw_data[i+offset];
620  }
621  }
622  else
623  {
624  memset(larray+n*j, 0, n * sizeof(long));
625  }
626  }
627  return DAEstatus::Success;
628  }
629  if ( !strncmp(item_name, "TCB", 3) ) // TCB1 etc
630  {
631  int offset = 0;
632  for(k=0; k<nspec; k++)
633  {
634  int ntc = this->spectrumNTC(spec_array[k]);
635  int tr = this->spectrumCRPTTR(spec_array[k]);
636  for(i=0; i<ntc+1; i++)
637  {
638  larray[i + offset] = this->tcb[tr-1][i];
639  }
640  offset += (ntc + 1);
641  return DAEstatus::Success;
642  }
643  }
644  if (!strcmp(item_name, "SPECTRUMT0")) // spectra including time bin 0
645  {
646  int k = 0, ntcmax = 0;
647  for(j=0; j<nspec; j++)
648  {
649  ntcmax = std::max(ntcmax, this->spectrumNTC(spec_array[j]));
650  }
651  for(j=0; j<nspec; j++)
652  {
653  int ntc = this->spectrumNTC(spec_array[j]);
654  uint32_t offset = this->spectrumCRPTOffsetImpl(spec_array[j], 0, raw_data_size);
655  for(i=0; i < ntc+1; i++)
656  {
657  larray[i + k] = raw_data[offset+i];
658  }
659  k += (ntcmax + 1);
660  }
661  return DAEstatus::Success;
662  }
663  if (!strcmp(item_name, "SPECTRUM")) // spectra without time bin 0
664  {
665  int k = 0;
666  for(j=0; j<nspec; j++)
667  {
668  int ntc = this->spectrumNTC(spec_array[j]);
669  uint32_t offset = this->spectrumCRPTOffsetImpl(spec_array[j], 0, raw_data_size);
670  for(i=0; i<ntc; i++) // offset+1+i as ignore channel 0
671  {
672  larray[i + k] = raw_data[offset+1+i];
673  }
674  k += ntc;
675  }
676  return DAEstatus::Success;
677  }
678  // isisraw compatability: 32 word structure of mixed int and real values
679  if (!strcmp(item_name, "IRPB"))
680  {
681  memset(larray, 0, 32 * nspec * sizeof(int));
682  for(k=0; k<nspec; k++)
683  {
684  n = k * 32;
685  larray[0 + n] = this->update_duration;
686  larray[1 + n] = 1;
687  larray[9 + n] = this->update_good_frames;
688  larray[10 + n] = this->update_raw_frames;
689  larray[12 + n] = this->update_duration;
690  }
691  return DAEstatus::Success;
692  }
693  // isisraw compatability: 64 word structure of int values
694  if (!strcmp(item_name, "DAEP"))
695  {
696  memset(larray, 0, 64 * nspec * sizeof(int));
697  for(k=0; k<nspec; k++)
698  {
699  n = k * 64;
700  larray[0 + n] = 4;
701  larray[1 + n] = larray[0 + n] * (this->nsp[0] + 1) * (this->ntc[0] + 1) * this->nper_daq;
702  larray[3 + n] = this->update_good_ppp_high;
703  larray[4 + n] = this->update_good_ppp_low;
704  larray[5 + n] = this->update_total_ppp_high;
705  larray[6 + n] = this->update_total_ppp_low;
706  larray[23 + n] = this->tcb_delay / 4;
707  if (this->tcb_sync == FrameSyncInternalTest)
708  {
709  larray[24 + n] = 1;
710  }
711  else
712  {
713  larray[24 + n] = 0; // external
714  }
715  if (this->ntrg > 1)
716  {
717  larray[29 + n] = this->ntrg;
718  for(i=0; i<this->ntrg; i++)
719  {
720  larray[30 + i + n] = this->tcb_trdelay[i];
721  }
722  }
723  else
724  {
725  larray[29 + n] = 0;
726  }
727  }
728  return DAEstatus::Success;
729  }
730  for(i=0; i< sizeof(int_array_items) / sizeof(int_array_item); i++)
731  {
732  if (!strcmp(item_name, int_array_items[i].name))
733  {
734  if (int_array_items[i].dim1 == 0)
735  {
736  n = *(int_array_items[i].dim0);
737  }
738  else
739  {
740  n = *(int_array_items[i].dim0) * *(int_array_items[i].dim1);
741  }
742  for(k=0; k<nspec; k++)
743  {
744  for(j=0; j<n; j++)
745  {
746  larray[j + k * n] = int_array_items[i].pi[j];
747  }
748  }
749  return DAEstatus::Success;
750  }
751  }
752  return DAEstatus::Failure;
753 }
754 
755 int ISISCRPT_STRUCT::getIntArrayItem(const isisU32_t* raw_data, int raw_data_size, const char* item_name, long* larray, DAEstatus& status) const
756 {
757  size_t n;
758  long spec_no;
759  std::string sitem_name, tmp_item;
760  sitem_name = item_name;
761  n = sitem_name.find('_');
762  if (n != std::string::npos)
763  {
764  spec_no = atol(item_name+n+1);
765  tmp_item = sitem_name.substr(0,n);
766  return getIntArrayItem(raw_data, raw_data_size, tmp_item.c_str(), &spec_no, 1, larray, status);
767  }
768  else
769  {
770  spec_no = 0;
771  return getIntArrayItem(raw_data, raw_data_size, item_name, &spec_no, 1, larray, status);
772  }
773 }
774 
775 int ISISCRPT_STRUCT::getRealArrayItem(const isisU32_t* raw_data, int raw_data_size, const char* item_name, double* darray, DAEstatus& status) const
776 {
777  return getRealArrayItemHelper<double>(raw_data, raw_data_size, item_name, darray, status);
778 }
779 
780 int ISISCRPT_STRUCT::getRealArrayItem(const isisU32_t* raw_data, int raw_data_size, const char* item_name, float* farray, DAEstatus& status) const
781 {
782  return getRealArrayItemHelper<float>(raw_data, raw_data_size, item_name, farray, status);
783 }
784 
785 int ISISCRPT_STRUCT::getRealArrayItem(const isisU32_t* raw_data, int raw_data_size, const char* item_name, const long* spec_array, int nspec, double* darray, DAEstatus& status) const
786 {
787  return getRealArrayItemHelper<double>(raw_data, raw_data_size, item_name, spec_array, nspec, darray, status);
788 }
789 
790 int ISISCRPT_STRUCT::getRealArrayItem(const isisU32_t* raw_data, int raw_data_size, const char* item_name, const long* spec_array, int nspec, float* farray, DAEstatus& status) const
791 {
792  return getRealArrayItemHelper<float>(raw_data, raw_data_size, item_name, spec_array, nspec, farray, status);
793 }
794 
795 template <typename T>
796 int ISISCRPT_STRUCT::getRealArrayItemHelper(const isisU32_t* raw_data, int raw_data_size, const char* item_name, T* darray, DAEstatus& status) const
797 {
798  size_t n;
799  long spec_no;
800  std::string sitem_name, tmp_item;
801  sitem_name = item_name;
802  n = sitem_name.find('_');
803  if (n != std::string::npos)
804  {
805  spec_no = atol(item_name+n+1);
806  tmp_item = sitem_name.substr(0,n);
807  return getRealArrayItemHelper<T>(raw_data, raw_data_size, tmp_item.c_str(), &spec_no, 1, darray, status);
808  }
809  else
810  {
811  spec_no = 0;
812  return getRealArrayItemHelper<T>(raw_data, raw_data_size, item_name, &spec_no, 1, darray, status);
813  }
814 }
815 
816 template <typename T>
817 int ISISCRPT_STRUCT::getRealArrayItemHelper(const isisU32_t* raw_data, int raw_data_size, const char* item_name, const long* spec_array, int nspec, T* darray, DAEstatus& /*status*/) const
818 {
819 #include "isiscrpt_items.h"
820  int i, j, k, n;
821  std::string sitem_name = item_name;
822 // long sum;
823  if ( !strncmp(item_name, "RTCB", 4) || !strncmp(item_name, "TIM", 3) ) // TCB1 or TIM1 etc
824  {
825  n = 0;
826  for(k=0; k<nspec; k++)
827  {
828  int ntc = this->spectrumNTC(spec_array[k]);
829  int tr = this->spectrumCRPTTR(spec_array[k]);
830  for(i=0; i<ntc+1; i++)
831  {
832  darray[i + n] = this->rtcb[tr-1][i];
833  }
834  n += ntc;
835  }
836  return DAEstatus::Success;
837  }
838  if (!strcmp(item_name, "RSPECTRUM"))
839  {
840  n = 0;
841  for(k=0; k<nspec; k++)
842  {
843  uint32_t offset = this->spectrumCRPTOffsetImpl(spec_array[k], 0, raw_data_size);
844  int ntc = this->spectrumNTC(spec_array[k]);
845  int tr = this->spectrumCRPTTR(spec_array[k]);
846  for(i=0; i<ntc; i++) // n+1+i as ignore time channel 0
847  {
848  darray[i + n] = raw_data[offset+1+i] / (this->rtcb[tr-1][i+1] - this->rtcb[tr-1][i]);
849  }
850  n += ntc;
851  }
852  return DAEstatus::Success;
853  }
854  if (!strcmp(item_name, "RSPECERR"))
855  {
856  n = 0;
857  for(k=0; k<nspec; k++)
858  {
859  uint32_t offset = this->spectrumCRPTOffsetImpl(spec_array[k], 0, raw_data_size);
860  int ntc = this->spectrumNTC(spec_array[k]);
861  int tr = this->spectrumCRPTTR(spec_array[k]);
862  for(i=0; i<ntc; i++) // n+1+i as ignore time channel 0
863  {
864  darray[i + n] = static_cast<T>(sqrt((double)raw_data[offset+1+i]) / (this->rtcb[tr-1][i+1] - this->rtcb[tr-1][i]));
865  }
866  n += ntc;
867  }
868  return DAEstatus::Success;
869  }
870  // isisraw compatability: 32 word structure of mixed int and real values
871  if (!strcmp(item_name, "RRPB"))
872  {
873  memset(darray, 0, 32 * nspec * sizeof(float));
874  for(k=0; k<nspec; k++)
875  {
876  n = k * 32;
877  darray[7 + n] = this->update_good_uamph;
878  darray[8 + n] = this->update_raw_uamph;
879  }
880  return DAEstatus::Success;
881  }
882 
883  for(i=0; i< sizeof(real_array_items) / sizeof(real_array_item); i++)
884  {
885  if (!strcmp(item_name, real_array_items[i].name))
886  {
887  if (real_array_items[i].dim1 == 0)
888  {
889  n = *(real_array_items[i].dim0);
890  }
891  else
892  {
893  n = *(real_array_items[i].dim0) * *(real_array_items[i].dim1);
894  }
895  for(k=0; k<nspec; k++)
896  {
897  for(j=0; j<n; j++)
898  {
899  darray[j + k * n] = real_array_items[i].pf[j];
900  }
901  }
902  return DAEstatus::Success;
903  }
904  }
905  return DAEstatus::Failure;
906 }
907 
908 void ISISCRPT_STRUCT::splitItemName(const std::string& sitem_name, SplitItem& split_item)
909 {
910  int num;
911  size_t n = sitem_name.find('_');
912  if (n != std::string::npos)
913  {
914  num = atol(sitem_name.c_str()+n+1);
915  }
916  else
917  {
918  num = 0;
919  }
920  split_item.item = sitem_name.substr(0,n);
921  split_item.number = num;
922 }
923 
924 
926 int ISISCRPT_STRUCT::getNumSpectra(bool include_spectrum_zero) const
927 {
928  int n = 0;
929  for(int i=0; i<ntrg; ++i)
930  {
931  n += nsp[i];
932  }
933  if (include_spectrum_zero)
934  {
935  n += 1; // only one spectrum zero not per TR at the moment
936  }
937  return n;
938 }
939 
941 int ISISCRPT_STRUCT::getNumSpectra(int tr, bool include_spectrum_zero) const
942 {
943  int n = 0;
944  if (tr >= 1 && tr <= ntrg)
945  {
946  n = nsp[tr - 1];
947  }
948  if (include_spectrum_zero && tr == spectrumCRPTTR(0))
949  {
950  n += 1; // only one spectrum zero not per TR at the moment
951  }
952  return n;
953 }
954 
955 
958 {
959  int n = 0;
960  for(int i=0; i<ntrg; ++i)
961  {
962  n += nsp[i];
963  }
964  return n;
965 }
966 
967 
969 {
970  for(int i=0; i < this->ntrg; ++i)
971  {
972  for(int j=0; j < this->ntrg; ++j)
973  {
974  for(int k=0; k < 1 + this->ntc[j]; ++k)
975  {
976  this->tcb_map[i][j][k] = simpleChannelRebin(k, j+1, i+1);
977  }
978  }
979  }
980  return 0;
981 }
982 
985 int ISISCRPT_STRUCT::channelLookup(int from_chan, int from_tr, int to_tr, float tof_val) const
986 {
987  int simp_chan = this->tcb_map[to_tr-1][from_tr-1][from_chan]; // cached result of simpleChannelRebin()
988  const float* to_rtcb = this->rtcb[to_tr-1];
989  int ntc = this->ntc[to_tr-1];
990  int i = simp_chan;
991  if (from_chan == 0)
992  {
993  return 0;
994  }
995  while(i >= 1 && i < ntc)
996  {
997  if (to_rtcb[i] <= tof_val)
998  {
999  ++i;
1000  continue;
1001  }
1002  if (to_rtcb[i-1] > tof_val)
1003  {
1004  --i;
1005  continue;
1006  }
1007  break;
1008  }
1009  return i;
1010 }
1011 
1015 int ISISCRPT_STRUCT::simpleChannelRebin(int from_chan, int from_tr, int to_tr) const
1016 {
1017  if (from_chan == 0)
1018  {
1019  return 0;
1020  }
1021  const float* from_rtcb = this->rtcb[from_tr-1];
1022  const float* to_rtcb = this->rtcb[to_tr-1];
1023  // we use bin centre as if the old bin centre lies in the new bin, then the old bin probably had
1024  // the most overlap with that new bin's boundaries
1025  float tof_val = (from_rtcb[from_chan-1] + from_rtcb[from_chan]) / 2.0; // bin centre
1026  int ntc = this->ntc[to_tr-1];
1027  int to_chan = ( std::upper_bound(to_rtcb, to_rtcb + ntc, tof_val) - to_rtcb );
1028  if ( to_chan == ntc )
1029  {
1030  return 0; // cannot find it on upper end; upper_bound will return 0 if cannot find on lower end
1031  }
1032 // int to_chan = 0;
1033 // for(int i=0; i<m_crpt->ntc[to_tr-1]; ++i)
1034 // {
1035 // if ( (to_rtcb[i] <= tof_low) )
1036 // {
1037 // to_chan = i + 1;
1038 // }
1039 // }
1040  return to_chan;
1041 }
1042 
1045 int ISISCRPT_STRUCT::randomisedChannelRebin(int from_chan, int from_tr, int to_tr) const
1046 {
1047  if (from_chan == 0)
1048  {
1049  return 0;
1050  }
1051  const float* from_rtcb = this->rtcb[from_tr-1];
1052  const float* to_rtcb = this->rtcb[to_tr-1];
1053  float ran = (float)rand() / (float)(RAND_MAX+1);
1054  float tof_val = from_rtcb[from_chan-1] + ran * (from_rtcb[from_chan] - from_rtcb[from_chan-1]);
1055  int ntc = this->ntc[to_tr-1];
1056  int to_chan = channelLookup(from_chan, from_tr, to_tr, tof_val);
1057 // if ( to_chan != (std::upper_bound(to_rtcb, to_rtcb + ntc, tof_val) - to_rtcb) )
1058 // {
1059 // throw std::runtime_error("error");
1060 // }
1061  if ( to_chan == ntc )
1062  {
1063  return 0; // cannot find it on upper end; upper_bound will return 0 if cannot find on lower end
1064  }
1065  return to_chan;
1066 }
1067 
1068 void ISISCRPT_STRUCT::setInstName(const std::string& comp_name)
1069 {
1070  // strip NDX prefix if present
1071  if (strncmp(comp_name.c_str(), "NDX", 3) == 0)
1072  {
1073  strncpy(inst_name, comp_name.c_str()+3, sizeof(inst_name));
1074  }
1075  else
1076  {
1077  strncpy(inst_name, comp_name.c_str(), sizeof(inst_name));
1078  }
1080  strncpy(inst_abrv, getInstAbrv(inst_name), sizeof(inst_abrv));
1082 }
#define NULL_TERMINATE(__array)
null terminate a char[n] static array
Definition: isiscrpt.h:74
unsigned magic
Definition: isiscrpt.h:495
float uamp_scale
Definition: isiscrpt.h:479
int electronics_delay
Definition: isiscrpt.h:263
const float * pf
Definition: isiscrpt_items.h:9
int getIntArrayItem(const isisU32_t *raw_data, int raw_data_size, const char *item_name, long *larray, DAEstatus &status) const
Definition: isiscrpt.cpp:755
uint32_t trCRPTOffset(int tr, int daq_period, int raw_data_size) const
Definition: isiscrpt.h:651
int timr_crpt[ISISCRPT_MAX_DETECTOR]
Definition: isiscrpt.h:313
int getNumSpectra(bool include_spectrum_zero=false) const
Definition: isiscrpt.cpp:926
const int * pi
Definition: isiscrpt_items.h:3
const int * dim1
Definition: isiscrpt_items.h:8
float update_good_uamph
Definition: isiscrpt.h:372
float rtcb[ISISCRPT_MAX_NTRG][ISISCRPT_MAX_TIMECHANB]
Definition: isiscrpt.h:344
float update_raw_uamph
Definition: isiscrpt.h:371
int tcb_map[ISISCRPT_MAX_NTRG][ISISCRPT_MAX_NTRG][ISISCRPT_MAX_TIMECHANB]
Definition: isiscrpt.h:345
int setDAESpecificParameters()
Definition: isiscrpt.cpp:102
static int vmstime(char *timbuf, int len, time_t time_value)
Definition: isisraw.cpp:1192
uint32_t spectrumCRPTOffsetImpl(int spec, int daq_period) const
Definition: isiscrpt.h:624
void markUninitialised()
Definition: isiscrpt.h:500
int getRealArrayItemHelper(const isisU32_t *raw_data, int raw_data_size, const char *item_name, T *darray, DAEstatus &status) const
Definition: isiscrpt.cpp:796
float tcb_minwidth
Definition: isiscrpt.h:265
unsigned long isisU32_t
Definition: isisvme_types.h:8
char rcsid[ISISCRPT_MAX_RCSID+1]
Definition: isiscrpt.h:239
int period_output_delay
Definition: isiscrpt.h:403
static const int Failure
Definition: DAEstatus.h:141
#define ISISCRPT_VERSION
increment ISISCRPT_VERSION for any ISISCRPT_STRUCT changes that might not be detected by a change of ...
Definition: isiscrpt.h:5
int getCharItem(const char *item_name, std::string &cvalue, DAEstatus &status) const
Definition: isiscrpt.cpp:228
int clock_frequency
Definition: isiscrpt.h:261
static char_item char_items[]
int spaceTrimCopy(char *output, int output_size, const char *input, int input_size)
Definition: isiscrpt.cpp:161
static const int Success
Definition: DAEstatus.h:140
const char * name
Definition: isiscrpt_items.h:9
static const char * getInstAbrv(const char *inst_name)
Definition: isiscrpt.cpp:33
static const char * inst_abrv_mappings[][2]
Definition: isiscrpt.cpp:11
int spacePadCopyVa(char *output, int output_size, const char *format,...)
Definition: isiscrpt.cpp:142
#define RUNSTATUS_SETUP
Definition: isiscrpt.h:49
#define SESTATUS_INRANGE
Definition: isiscrpt.h:62
long update_raw_frames
Definition: isiscrpt.h:377
int timr_dae[ISISCRPT_MAX_DETECTOR]
Definition: isiscrpt.h:312
int persize
size of a period in 32bit words
Definition: isiscrpt.h:348
long update_duration
Definition: isiscrpt.h:382
int channelLookup(int from_chan, int from_tr, int to_tr, float tof_val) const
Definition: isiscrpt.cpp:985
int buildTimeRegimeMap()
Definition: isiscrpt.cpp:968
static real_item real_items[]
int auto_save_value
Definition: isiscrpt.h:287
static int_array_item int_array_items[]
int number
Definition: isiscrpt.h:230
const int * pi
Definition: isiscrpt_items.h:8
int getRealItem(const char *item_name, double &dblVal, DAEstatus &status) const
Definition: isiscrpt.cpp:391
int dae_type
time CRPT unloaded (may not be set if program crashed)
Definition: isiscrpt.h:247
const char * name
Definition: isiscrpt_items.h:8
char long_title[ISISCRPT_TITLE_LEN+1]
Definition: isiscrpt.h:350
long update_good_frames
Definition: isiscrpt.h:376
float tcb_mintime
Definition: isiscrpt.h:266
time_t stop_time
Definition: isiscrpt.h:256
int tcb[ISISCRPT_MAX_NTRG][ISISCRPT_MAX_TIMECHANB]
Definition: isiscrpt.h:343
static int_item int_items[]
const float * pf
Definition: isiscrpt_items.h:4
isisU32_t update_good_ppp_high
Definition: isiscrpt.h:368
isisU32_t update_total_ppp_high
Definition: isiscrpt.h:370
boost::array< int, ISISCRPT_MAX_NTRG > tcb_trdelay
Definition: isiscrpt.h:270
int spec[ISISCRPT_MAX_DETECTOR]
Definition: isiscrpt.h:301
static const char * crpt_rcsid
Definition: isiscrpt.h:7
Poco::SingletonHolder< ICPClock > g_icp_clock
Definition: icputils.cpp:5
int getRealArrayItemSize(const char *item_name, int *dims_array, int &ndims, DAEstatus &status) const
Definition: isiscrpt.cpp:519
int getRealArrayItem(const isisU32_t *raw_data, int raw_data_size, const char *item_name, double *darray, DAEstatus &status) const
Definition: isiscrpt.cpp:775
int spectrumCRPTTR(int spec) const
Definition: isiscrpt.h:794
int update_poll_time
Definition: isiscrpt.h:366
isisU32_t update_good_ppp_low
Definition: isiscrpt.h:367
const int * dim0
Definition: isiscrpt_items.h:8
char inst_abrv[ISISCRPT_INST_ABRV_LEN+1]
Definition: isiscrpt.h:352
int timr_file[ISISCRPT_MAX_DETECTOR]
Definition: isiscrpt.h:314
boost::array< int, ISISCRPT_MAX_NTRG > ntc
number of time channels per CRPT time regime
Definition: isiscrpt.h:346
char user_name[ISISCRPT_USER_NAME_LEN+1]
Definition: isiscrpt.h:353
int simpleChannelRebin(int from_chan, int from_tr, int to_tr) const
Definition: isiscrpt.cpp:1015
char inst_name[ISISCRPT_INST_LEN+1]
Definition: isiscrpt.h:351
void getParameters(LONGLONG &perf_counter, LONGLONG &proc_freq, FILETIME &filetime)
Definition: icputils.h:184
Monitoring monitoring
Definition: isiscrpt.h:472
static void splitItemName(const std::string &sitem_name, SplitItem &split_item)
Definition: isiscrpt.cpp:908
#define VMS_RUN_MASK
need to wrap VMS run numbers to fit into 5 digits
Definition: isisraw.h:7
void setInstName(const std::string &comp_name)
Definition: isiscrpt.cpp:1068
isisU32_t update_total_ppp_low
Definition: isiscrpt.h:369
float tcb_maxtime
Definition: isiscrpt.h:267
const int * dim1
Definition: isiscrpt_items.h:9
FrameSync tcb_sync
Definition: isiscrpt.h:271
int parseSpectraRange(const std::string &spec_range, int &spec_from, int &spec_to)
Definition: icputils.cpp:338
int getHighestSpectrumNumber() const
Definition: isiscrpt.cpp:957
unsigned long crpt_size
Definition: isiscrpt.h:243
int randomisedChannelRebin(int from_chan, int from_tr, int to_tr) const
Definition: isiscrpt.cpp:1045
int getIntItem(const char *item_name, long &lVal, DAEstatus &status) const
Definition: isiscrpt.cpp:233
int spectrumNTC(int spec) const
Definition: isiscrpt.h:804
int getIntArrayItemSize(const char *item_name, int *dims_array, int &ndims, DAEstatus &status) const
Definition: isiscrpt.cpp:432
std::string item
Definition: isiscrpt.h:229
struct ISISCRPT_STRUCT::@9 icp_clock
static real_array_item real_array_items[]
boost::array< int, ISISCRPT_MAX_NTRG > nsp
number of spectra per CRPT time regime
Definition: isiscrpt.h:334
const char * val
Definition: isiscrpt_items.h:2
const int * dim0
Definition: isiscrpt_items.h:9
int software_period
Definition: isiscrpt.h:250
int spacePadCopy(char *output, const char *input, int output_size)
Definition: isiscrpt.cpp:127
DAEType
Definition: isisdae.h:63