Newer
Older
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
#include <exception>
#include <iostream>
#include "isisraw2.h"
#include "vms_convert.h"
#include "byte_rel_comp.h"
#define SUCCESS 0
#define FAILURE 1
/// stuff
int ISISRAW2::addItems()
{
static const int hdr_size = sizeof(hdr) / sizeof(char);
static const int rrpb_size = sizeof(rpb) / sizeof(float);
static const int irpb_size = sizeof(rpb) / sizeof(int);
m_char_items.addItem("HDR", (const char*)&hdr, false, &hdr_size);
m_real_items.addItem("RRPB", (float*)&rpb, false, &rrpb_size);
m_int_items.addItem("IRPB", (int*)&rpb, false, &irpb_size);
return 0;
}
// create one bound to a CRPT
/// stuff
ISISRAW2::ISISRAW2() : m_crpt(NULL),m_ntc1(0),m_nsp1(0), m_nper(0),
mdet(0),monp(0),spec(0),delt(0),len2(0),code(0),
tthe(0),ut(0),crat(0),modn(0),mpos(0),timr(0),udet(0),
t_tcb1(0),u_dat(0),ddes(0),dat1(0),m_file(0),outbuff(0)
{
e_nse = 0;
e_seblock = 0;
u_len = 0;
logsect.nlines = 0;
logsect.lines = 0;
addItems();
//updateFromCRPT();
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, bool from_file, bool read_data)
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
{
m_file = file;
int i;
fpos_t add_pos, dhdr_pos;
if (!from_file)
{
add.ad_run = 32;
add.ad_inst = add.ad_run + 94;
add.ad_se = add.ad_inst + 70 + 2*i_mon + (5+i_use)*i_det;
add.ad_dae = add.ad_se + 66 + e_nse*32;
add.ad_tcb = add.ad_dae + 65 + 5*i_det;
add.ad_user = add.ad_tcb + 288 + (t_ntc1 + 1);
add.ad_data = add.ad_user + 2 + u_len;
add.ad_log = 0; // we don't know it yet
add.ad_end = 0;
}
ioRAW(file, &hdr, 1, from_file);
ioRAW(file, &frmt_ver_no, 1, from_file);
fgetpos(file, &add_pos);
ioRAW(file, &add, 1, from_file);
ioRAW(file, &data_format, 3, from_file);
ioRAW(file, r_title, 80, from_file);
ioRAW(file, &user, 1, from_file);
ioRAW(file, &rpb, 1, from_file);
ioRAW(file, &ver3, 1, from_file);
ioRAW(file, i_inst, 8, from_file);
ioRAW(file, &ivpb, 1, from_file);
ioRAW(file, &i_det, 3, from_file);
ioRAW(file, &mdet, i_mon, from_file);
ioRAW(file, &monp, i_mon, from_file);
ioRAW(file, &spec, i_det, from_file);
ioRAW(file, &delt, i_det, from_file);
ioRAW(file, &len2, i_det, from_file);
ioRAW(file, &code, i_det, from_file);
ioRAW(file, &tthe, i_det, from_file);
ioRAW(file, &ut, i_use * i_det, from_file);
ioRAW(file, &ver4, 1, from_file);
ioRAW(file, &spb, 1, from_file);
ioRAW(file, &e_nse, 1, from_file);
ioRAW(file, &e_seblock, e_nse, from_file);
ioRAW(file, &ver5, 1, from_file);
ioRAW(file, &daep, 1, from_file);
ioRAW(file, &crat, i_det, from_file);
ioRAW(file, &modn, i_det, from_file);
ioRAW(file, &mpos, i_det, from_file);
ioRAW(file, &timr, i_det, from_file);
ioRAW(file, &udet, i_det, from_file);
ioRAW(file, &ver6, 267, from_file);
ioRAW(file, &(t_tcp1[0][0]), 20, from_file);
ioRAW(file, &t_pre1, 1, from_file);
ioRAW(file, &t_tcb1, t_ntc1+1, from_file);
ioRAW(file, &ver7, 1, from_file);
// it appear that the VMS ICP traditionally sets u_len to 1 regardless
// of its real size; thus we cannot rely on it for reading and must instead
// use section offsets
i = 0;
ioRAW(file, &i, 1, from_file);
// ioRAW(file, &u_len, 1, from_file);
if (from_file)
{
u_len = add.ad_data - add.ad_user - 2;
}
ioRAW(file, &u_dat, u_len, from_file);
ioRAW(file, &ver8, 1, from_file);
fgetpos(file, &dhdr_pos);
ioRAW(file, &dhdr, 1, from_file);
int outbuff_size = 100000;
outbuff = new char[outbuff_size];
ndes = t_nper * (t_nsp1+1);
ioRAW(file, &ddes, ndes, true);
dat1 = new uint32_t[ t_ntc1+1 ]; // space for just one spectrum
memset(outbuff, 0, outbuff_size); // so when we round up words we get a zero written
return 0; // stop reading here
}
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
void ISISRAW2::skipData(int i)
{
if (i < ndes)
fseek(m_file,4*ddes[i].nwords,SEEK_CUR);
}
void ISISRAW2::readData(int i)
{
if (i >= ndes) return;
int nwords = 4*ddes[i].nwords;
ioRAW(m_file, outbuff, nwords, true);
byte_rel_expn(outbuff, nwords, 0, (int*)dat1, t_ntc1+1);
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, HDR_STRUCT* s, int len, bool from_file)
{
ioRAW(file, (char*)s, sizeof(HDR_STRUCT) * len, from_file);
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, ADD_STRUCT* s, int len, bool from_file)
{
ioRAW(file, (int*)s, (sizeof(ADD_STRUCT) * len / sizeof(int)), from_file);
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, USER_STRUCT* s, int len, bool from_file)
{
ioRAW(file, (char*)s, sizeof(USER_STRUCT) * len, from_file);
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, RPB_STRUCT* s, int len, bool from_file)
{
int i;
for(i=0; i<len; i++)
{
ioRAW(file, &(s[i].r_dur), 7, from_file);
ioRAW(file, &(s[i].r_gd_prtn_chrg), 2, from_file);
ioRAW(file, &(s[i].r_goodfrm), 7, from_file);
ioRAW(file, s[i].r_enddate, 20, from_file);
ioRAW(file, &(s[i].r_prop), 11, from_file);
}
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, IVPB_STRUCT* s, int len, bool from_file)
{
int i;
for(i=0; i<len; i++)
{
ioRAW(file, &(s[i].i_chfreq), 3, from_file);
ioRAW(file, &(s[i].delay_c1), 14, from_file);
ioRAW(file, &(s[i].i_xsect), 2, from_file);
ioRAW(file, &(s[i].i_posn), 3, from_file);
ioRAW(file, &(s[i].i_l1), 1, from_file);
ioRAW(file, &(s[i].i_rfreq), 1, from_file);
ioRAW(file, &(s[i].i_renergy), 2, from_file);
ioRAW(file, &(s[i].i_rslit), 2, from_file);
ioRAW(file, &(s[i].i_xcen), 2, from_file);
ioRAW(file, &(s[i].i_bestop), 1, from_file);
ioRAW(file, &(s[i].i_radbest), 4, from_file);
ioRAW(file, s[i].spare, 29, from_file);
}
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, SPB_STRUCT* s, int len, bool from_file)
{
int i;
for(i=0; i<len; i++)
{
ioRAW(file, &(s[i].e_posn), 3, from_file);
ioRAW(file, &(s[i].e_thick), 16, from_file);
ioRAW(file, s[i].e_name, 40, from_file);
ioRAW(file, &(s[i].e_equip), 35, from_file);
}
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, SE_STRUCT* s, int len, bool from_file)
{
int i;
for(i=0; i<len; i++)
{
ioRAW(file, s[i].sep_name, 8, from_file);
ioRAW(file, &(s[i].sep_value), 2, from_file);
ioRAW(file, s[i].sep_units, 8, from_file);
ioRAW(file, &(s[i].sep_low_trip), 7, from_file);
ioRAW(file, &(s[i].sep_stable), 2, from_file);
ioRAW(file, &(s[i].sep_cam_addr), 17, from_file);
}
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, DAEP_STRUCT* s, int len, bool from_file)
{
ioRAW(file, (int*)s, sizeof(DAEP_STRUCT) * len / sizeof(int), from_file);
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, DHDR_STRUCT* s, int len, bool from_file)
{
int i;
for(i=0; i<len; i++)
{
ioRAW(file, &(s[i].d_comp), 3, from_file);
ioRAW(file, &(s[i].d_crdata), 2, from_file);
ioRAW(file, &(s[i].d_exp_filesize), 27, from_file);
}
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, DDES_STRUCT* s, int len, bool from_file)
{
int i;
for(i=0; i<len; i++)
{
ioRAW(file, &(s[i].nwords), 2, from_file);
}
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, LOG_STRUCT* s, int len, bool from_file)
{
int i;
for(i=0; i<len; i++)
{
ioRAW(file, &(s[i].ver), 2, from_file);
ioRAW(file, &(s[i].lines), s[i].nlines, from_file);
}
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, LOG_LINE* s, int len, bool from_file)
{
char padding[5];
memset(padding, ' ', sizeof(padding));
int i, nbytes_rounded;
for(i=0; i<len; i++)
{
ioRAW(file, &(s[i].len), 1, from_file);
nbytes_rounded = 4 * (1 + (s[i].len-1)/4);
ioRAW(file, &(s[i].data), s[i].len, from_file);
ioRAW(file, padding, nbytes_rounded - s[i].len, from_file);
}
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, char* s, int len, bool from_file)
{
size_t n;
if ( (len <= 0) || (s == 0) )
{
return 0;
}
if (from_file)
{
n = fread(s, sizeof(char), len, file);
}
else
{
n = fwrite(s, sizeof(char), len, file);
}
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, int* s, int len, bool from_file)
{
size_t n;
if ( (len <= 0) || (s == 0) )
{
return 0;
}
if (from_file)
{
n = fread(s, sizeof(int), len, file);
}
else
{
n = fwrite(s, sizeof(int), len, file);
}
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, uint32_t* s, int len, bool from_file)
{
size_t n;
if ( (len <= 0) || (s == 0) )
{
return 0;
}
if (from_file)
{
n = fread(s, sizeof(uint32_t), len, file);
}
else
{
n = fwrite(s, sizeof(uint32_t), len, file);
}
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, float* s, int len, bool from_file)
{
size_t n;
int errcode = 0;
if ( (len <= 0) || (s == 0) )
{
return 0;
}
if (from_file)
{
n = fread(s, sizeof(float), len, file);
vaxf_to_local(s, &len, &errcode);
}
else
{
local_to_vaxf(s, &len, &errcode);
n = fwrite(s, sizeof(float), len, file);
vaxf_to_local(s, &len, &errcode);
}
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, char** s, int len, bool from_file)
{
if (from_file)
{
if (len > 0)
{
*s = new char[len];
ioRAW(file, *s, len, from_file);
}
else
{
*s = 0;
}
}
else
{
if (*s != 0)
{
ioRAW(file, *s, len, from_file);
}
}
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, int** s, int len, bool from_file)
{
if (from_file)
{
if (len > 0)
{
*s = new int[len];
ioRAW(file, *s, len, from_file);
}
else
{
*s = 0;
}
}
else
{
if (*s != 0)
{
ioRAW(file, *s, len, from_file);
}
}
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, uint32_t** s, int len, bool from_file)
{
if (from_file)
{
if (len > 0)
{
*s = new uint32_t[len];
ioRAW(file, *s, len, from_file);
}
else
{
*s = 0;
}
}
else
{
if (*s != 0)
{
ioRAW(file, *s, len, from_file);
}
}
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, float** s, int len, bool from_file)
{
if (from_file)
{
if (len > 0)
{
*s = new float[len];
ioRAW(file, *s, len, from_file);
}
else
{
*s = 0;
}
}
else
{
if (*s != 0)
{
ioRAW(file, *s, len, from_file);
}
}
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, SE_STRUCT** s, int len, bool from_file)
{
if (from_file)
{
if (len > 0)
{
*s = new SE_STRUCT[len];
ioRAW(file, *s, len, from_file);
}
else
{
*s = 0;
}
}
else
{
if (*s != 0)
{
ioRAW(file, *s, len, from_file);
}
}
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, DDES_STRUCT** s, int len, bool from_file)
{
if (from_file)
{
if (len > 0)
{
*s = new DDES_STRUCT[len];
ioRAW(file, *s, len, from_file);
}
else
{
*s = 0;
}
}
else
{
if (*s != 0)
{
ioRAW(file, *s, len, from_file);
}
}
return 0;
}
/// stuff
int ISISRAW2::ioRAW(FILE* file, LOG_LINE** s, int len, bool from_file)
{
if (from_file)
{
if (len > 0)
{
*s = new LOG_LINE[len];
ioRAW(file, *s, len, from_file);
}
else
{
*s = 0;
}
}
else
{
if (*s != 0)
{
ioRAW(file, *s, len, from_file);
}
}
return 0;
}
/// stuff
int ISISRAW2::size_check()
{
static int size_check_array[] = {
sizeof(HDR_STRUCT), 80,
sizeof(ADD_STRUCT), 9*4,
sizeof(USER_STRUCT), 8*20,
sizeof(RPB_STRUCT), 32*4,
sizeof(IVPB_STRUCT), 64*4,
sizeof(SPB_STRUCT), 64*4,
sizeof(SE_STRUCT), 32*4,
sizeof(DAEP_STRUCT), 64*4,
sizeof(DHDR_STRUCT), 32*4,
sizeof(DDES_STRUCT), 2*4
};
for(unsigned i=0; i<sizeof(size_check_array)/sizeof(int); i += 2)
{
if (size_check_array[i] != size_check_array[i+1])
{
std::cerr << "size check failed" << std::endl;
}
}
return 0;
}
/// stuff
int ISISRAW2::vmstime(char* timbuf, int len, time_t time_value)
{
/*
* get time in VMS format 01-JAN-1970 00:00:00
*/
int i, n;
struct tm *tmstruct = NULL;
#ifdef MS_VISUAL_STUDIO
errno_t err = localtime_s( tmstruct, &time_value );
if (err)
{
return FAILURE;
}
#else //_WIN32
tmstruct = localtime((time_t*)&time_value);
#endif //_WIN32
n = strftime(timbuf, len, "%d-%b-%Y %H:%M:%S", tmstruct);
for(i=0; i<n; i++)
{
timbuf[i] = toupper(timbuf[i]);
}
return SUCCESS;
}
/// stuff
int ISISRAW2::readFromFile(const char* filename, bool read_data)
{
#ifdef MS_VISUAL_STUDIO
FILE* input_file=NULL;
if(fopen_s( &input_file, filename, "rb" ) !=0 )
{
return -1;
}
#else //_WIN32
FILE* input_file = fopen(filename,"rb");
#endif //_WIN32
if (input_file != NULL)
{
ioRAW(input_file, true, read_data);
fclose(input_file);
return 0;
}
else
{
return -1;
}
}
/// stuff
int ISISRAW2::writeToFile(const char* filename)
{
unsigned char zero_pad[512];
int npad;
long pos;
memset(zero_pad, 0, sizeof(zero_pad));
remove(filename);
#ifdef MS_VISUAL_STUDIO
FILE* output_file=NULL;
if(fopen_s( &output_file, filename, "w+bc" ) !=0 )
{
return -1;
}
#else //_WIN32
FILE* output_file = fopen(filename,"w+bc");
#endif //_WIN32
if (output_file != NULL)
{
ioRAW(output_file, false,0);
fflush(output_file);
// we need to pad to a multiple of 512 bytes for VMS compatibility
fseek(output_file, 0, SEEK_END);
pos = ftell(output_file);
if (pos % 512 > 0)
{
npad = 512 - pos % 512;
fwrite(zero_pad, 1, npad, output_file);
}
fclose(output_file);
return 0;
}
else
{
return -1;
}
}
/// stuff
int ISISRAW2::printInfo(std::ostream& os)
{
int i;
long offset;
os << "INST section at " << add.ad_inst << " 0x" << std::hex << 4*add.ad_inst << std::dec << std::endl;
os << "SE section at " << add.ad_se << " 0x" << std::hex << 4*add.ad_se << std::dec << std::endl;
os << "Dae section at " << add.ad_dae << " 0x" << std::hex << 4*add.ad_dae << std::dec << std::endl;
os << "Tcb section at " << add.ad_tcb << " 0x" << std::hex << 4*add.ad_tcb << std::dec << std::endl;
os << "User section at " << add.ad_user << " 0x" << std::hex << 4*add.ad_user << std::dec << std::endl;
os << "Data section at " << add.ad_data << " 0x" << std::hex << 4*add.ad_data << std::dec << std::endl;
os << "Log section at " << add.ad_log << " 0x" << std::hex << 4*add.ad_log << std::dec << std::endl;
os << "End section at " << add.ad_end << " 0x" << std::hex << 4*add.ad_end << std::dec << std::endl;
os << "User data len " << u_len << std::endl;
os << "Compression is " << (dhdr.d_comp == 0 ? "NONE" : "BYTE-RELATIVE") << std::endl;
os << "Compression ratio of data = " << dhdr.d_crdata << std::endl;
os << "Offsets of spectrum data" << std::endl;
offset = add.ad_data;
for(i=0; i < ((t_nsp1+1) * t_nper); i++)
{
os << i << " " << ddes[i].nwords << " words at offset " << ddes[i].offset << std::endl;
}
return 0;
}
/// stuff
ISISRAW2::~ISISRAW2()
{
//fclose(m_file);
delete[] outbuff;
delete[] dat1;
delete[] ut;
delete[] mdet;
delete[] monp;
delete[] spec;
delete[] delt;
delete[] len2;
delete[] code;
delete[] tthe;
delete[] e_seblock;
delete[] crat;
delete[] modn;
delete[] mpos;
delete[] timr;
delete[] udet;
delete[] t_tcb1;
delete[] u_dat;
delete[] ddes;
for(int i=0; i<logsect.nlines; i++)
{
delete[] logsect.lines[i].data;
}
delete[] logsect.lines;
}
// rtcb1 is of size t_ntc1+1
int ISISRAW2::getTimeChannels(float* rtcb1, int n)
{
if (n != t_ntc1+1)
{
return -1;
}
float extra;
if (frmt_ver_no > 1)
{
extra = 4 * daep.a_delay; // add on frame sync delay
}
else
{
extra = 0.0; // old files did not have this
}
int i;
for(i=0; i<t_ntc1+1; i++)
{
rtcb1[i] = t_tcb1[i] * t_pre1 / 32.0 + extra;
}
return 0;
}