-
Notifications
You must be signed in to change notification settings - Fork 0
/
adsIOrt.cc
525 lines (373 loc) · 10.9 KB
/
adsIOrt.cc
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
/*
-------------------------------------------------------------------------
OBJECT NAME: adsIOrt.cc
FULL NAME: Real-time ADS Data File Class
DESCRIPTION:
COPYRIGHT: University Corporation for Atmospheric Research, 1999
-------------------------------------------------------------------------
*/
#include "adsIOrt.h"
#include <unistd.h>
/* -------------------------------------------------------------------- */
ADS_rtFile::ADS_rtFile()
{
char host[80], *p;
if ((projDir = getenv("PROJ_DIR")) == NULL)
{
fprintf(stderr, "adsIOrt: PROJ_DIR undefined, fatal.\n");
exit(1);
}
gethostname(host, 80);
if ((p = strchr(host, '.')) ) *p = '\0';
sprintf(rtFileFileName, "%s/hosts/%s/rtdata.filename", projDir, host);
fpSync = fp2D = fpGrey = fpMCR = fpAVAPS = NULL;
checkFileName();
// Length of headers in each file. Used to compute offsets.
headOffset = 20 + (2 * hdr->HeaderLength());
} /* END CONSTRUCTOR */
/* -------------------------------------------------------------------- */
bool ADS_rtFile::FirstSyncRecord(char buff[])
{
checkFileName();
if (fpSync)
rewind(fpSync);
return(NextSyncRecord(buff));
} /* END FIRSTSYNCRECORD */
/* -------------------------------------------------------------------- */
bool ADS_rtFile::NextSyncRecord(char buff[])
{
checkFileName();
if (fpSync == NULL)
return(false);
do
{
if (NextPhysicalRecord(physRecord, fpSync, hdr->lrLength()) <= 0)
return(false);
}
while (ntohs(((ushort *)physRecord)[0]) != SDI_WORD);
memcpy((void *)buff, (void *)physRecord, hdr->lrLength());
return(true);
} /* END NEXTSYNCRECORD */
/* -------------------------------------------------------------------- */
bool ADS_rtFile::LastSyncRecord(char buff[], int minusN)
{
checkFileName();
if (fpSync == NULL)
return(false);
int nRecs = ((fileLengthSync() - headOffset) / hdr->lrLength()) - minusN;
if (nRecs < 0)
nRecs = 0;
fseek(fpSync, headOffset + (nRecs * hdr->lrLength()), SEEK_SET);
return(NextSyncRecord(buff));
} /* END FIRSTSYNCRECORD */
/* -------------------------------------------------------------------- */
bool ADS_rtFile::FirstMCRRecord(Mcr_rec *buff)
{
checkFileName();
if (fpMCR)
rewind(fpMCR);
return(NextMCRRecord(buff));
} /* END FIRSTMCRRECORD */
/* -------------------------------------------------------------------- */
bool ADS_rtFile::NextMCRRecord(Mcr_rec *buff)
{
checkFileName();
if (fpMCR == NULL)
return(false);
if (ftell(fpMCR) > 1009430000)
{
int len;
char fileName[512];
strcpy(fileName, fileBaseName);
strcat(fileName, ".rawMCRm");
fclose(fpMCR);
len = strlen(fileName)-1;
while ((fpMCR = fopen(fileName, "rb")) == NULL)
--fileName[len];
}
do
{
if (NextPhysicalRecord(physRecord, fpMCR, sizeof(Mcr_rec)) <= 0)
return(false);
}
while (strncmp(physRecord, "MCR-", 4));
memcpy((void *)buff, (void *)physRecord, sizeof(Mcr_rec));
return(true);
} /* END NEXTMCRRECORD */
/* -------------------------------------------------------------------- */
bool ADS_rtFile::LastMCRRecord(Mcr_rec *buff)
{
checkFileName();
if (fpMCR == NULL)
return(false);
int nRecs = (fileLengthMCR() - headOffset) / sizeof(Mcr_rec);
fseek(fpMCR, headOffset + (nRecs * sizeof(Mcr_rec)), SEEK_SET);
return(NextMCRRecord(buff));
} /* END LASTMCRRECORD */
/* -------------------------------------------------------------------- */
bool ADS_rtFile::FirstPMS2dRecord(P2d_rec *buff)
{
checkFileName();
if (fp2D)
rewind(fp2D);
return(NextPMS2dRecord(buff));
} /* END FIRSTPMS2DRECORD */
/* -------------------------------------------------------------------- */
bool ADS_rtFile::NextPMS2dRecord(P2d_rec *buff)
{
bool done = false;
int size;
checkFileName();
if (fp2D == NULL)
return(false);
do
{
if ((size = NextPhysicalRecord(physRecord, fp2D, sizeof(P2d_rec))) <= 0)
return(false);
if (physRecord[1] == '1' || physRecord[1] == '2')
{
char c = physRecord[0];
if (c == 'C' || c == 'P' || c == 'H')
done = true;
}
}
while (!done);
memcpy((void *)buff, (void *)physRecord, size);
return(true);
} /* END NEXTPMS2DRECORD */
/* -------------------------------------------------------------------- */
bool ADS_rtFile::LastPMS2dRecord(P2d_rec *buff, int minusN)
{
checkFileName();
if (fp2D == NULL)
return(false);
int nRecs = (fileLength2D() - headOffset) / sizeof(P2d_rec) - minusN;
if (nRecs < 0)
nRecs = 0;
fseek(fp2D, headOffset + (nRecs * sizeof(P2d_rec)), SEEK_SET);
//fprintf(stderr, "adsIOrt.cc: %d %d\n", nRecs, ftell(fp2D));
return(NextPMS2dRecord(buff));
} /* END LASTPMS2DRECORD */
/* -------------------------------------------------------------------- */
bool ADS_rtFile::FirstGreyRecord(char buff[])
{
checkFileName();
if (fpGrey)
rewind(fpGrey);
return(NextGreyRecord(buff));
} /* END FIRSTGREYRECORD */
/* -------------------------------------------------------------------- */
bool ADS_rtFile::NextGreyRecord(char buff[])
{
bool done = false;
int size;
checkFileName();
if (fpGrey == NULL)
return(false);
do
{
if ((size = NextPhysicalRecord(physRecord, fpGrey, sizeof(P2d_rec))) <= 0)
return(false);
if (physRecord[1] == '1' || physRecord[1] == '2')
{
char c = physRecord[0];
if (c == 'C' || c == 'P' || c == 'H' || c == 'G')
done = true;
}
}
while (!done);
memcpy((void *)buff, (void *)physRecord, size);
return(true);
} /* END NEXTGREYRECORD */
/* -------------------------------------------------------------------- */
bool ADS_rtFile::LastGreyRecord(char buff[])
{
checkFileName();
if (fpGrey == NULL)
return(false);
// Greyscale has variable length records, so formulas don't work.
int pos = fileLengthGrey() - 1024; // Back off 1k.
if (pos < headOffset)
pos = headOffset;
fseek(fpGrey, pos, SEEK_SET);
return(NextGreyRecord(buff));
} /* END LASTGREYRECORD */
/* -------------------------------------------------------------------- */
bool ADS_rtFile::FirstAvapsRecord(char *buff)
{
checkFileName();
if (fpAVAPS)
rewind(fpAVAPS);
return(NextAvapsRecord(buff));
}
/* -------------------------------------------------------------------- */
bool ADS_rtFile::NextAvapsRecord(char *buff)
{
char temp[1024];
checkFileName();
if (fpAVAPS == NULL)
return(false);
temp[0] = '\0';
if (fgets(temp, 1024, fpAVAPS))
{
strcpy(buff, temp);
return(true);
}
return(false);
}
/* -------------------------------------------------------------------- */
bool ADS_rtFile::LastAvapsRecord(char *buff)
{
checkFileName();
if (fpAVAPS)
fseek(fpAVAPS, 0, SEEK_END);
return(NextAvapsRecord(buff));
}
/* -------------------------------------------------------------------- */
int ADS_rtFile::NextPhysicalRecord(char buff[], FILE *fp, int lrLen)
{
long cp, ep;
int rc, idWord;
int size = sizeof(short);
// Check for new data, if not enough has been written bail out.
cp = ftell(fp);
fseek(fp, 0, SEEK_END);
ep = ftell(fp);
fseek(fp, cp, SEEK_SET);
if (ep - cp < lrLen)
return(0);
// Read, hopefully, ID word.
rc = fread(buff, size, 1, fp);
if (rc != 1)
return(0);
idWord = ntohs(*((ushort *)buff));
switch (idWord)
{
case SDI_WORD:
size = hdr->lrLength() - sizeof(short);
break;
case ADS_WORD:
size = 18;
break;
case HDR_WORD:
size = hdr->HeaderLength() - sizeof(short);
break;
case PMS2DC1: case PMS2DC2: // PMS 2D
case PMS2DP1: case PMS2DP2:
case PMS2DH1: case PMS2DH2: // HVPS
size = sizeof(P2d_rec) - sizeof(short);
break;
case 0x4d43: // MCR
size = sizeof(Mcr_rec) - sizeof(short);
break;
case PMS2DG1: case PMS2DG2: // GrayScale
/* Because Greyscale physical records are variable length, we will treat
* each particle as a record from disk based files.
*/
size = sizeof(GreyParticle) - sizeof(short);
break;
default:
printf("NextPhysicalRecord: didn't land on a record, reading 2 more.\n");
printf(" byte = %d\n", idWord);
size = 0;
}
if (fread(&buff[sizeof(short)], size, 1, fp) != 1)
size = 0;
if (idWord == PMS2DG1 || idWord == PMS2DG2)
{
size = ntohs(((GreyParticle *)buff)->slice_cnt) * 16;
fread(&buff[sizeof(GreyParticle)], size, 1, fp);
size += sizeof(GreyParticle);
}
else
size += sizeof(short);
return(size);
} /* END NEXTPHYSICALRECORD */
/* -------------------------------------------------------------------- */
ADS_rtFile::~ADS_rtFile()
{
closeFiles();
} /* END DESTRUCTOR */
/* -------------------------------------------------------------------- */
void ADS_rtFile::readFileName()
{
FILE *fpNameFile;
/* The real-time file name base is stored in the following file.
* Read this name, so we know what files to open for sync, 2d, mcr, etc.
*/
if ((fpNameFile = fopen(rtFileFileName, "r")) == NULL)
{
fprintf(stderr, "adsIOrt: Open of %s failed, fatal.\n", rtFileFileName);
exit(1);
}
fgets(fileBaseName, 256, fpNameFile);
fileBaseName[strlen(fileBaseName)-1] = '\0';
fclose(fpNameFile);
} /* END READFILENAME */
/* -------------------------------------------------------------------- */
void ADS_rtFile::checkFileName()
{
stat(rtFileFileName, &fileInfo);
if (fileInfo.st_mtime != lastMod)
{
fprintf(stderr, "adsIOrt: realtime data restarted, resetting files.\n");
lastMod = fileInfo.st_mtime;
readFileName();
closeFiles();
openFiles();
resetFiles();
}
} /* END CHECKFILENAME */
/* -------------------------------------------------------------------- */
void ADS_rtFile::openFiles()
{
char fileName[256];
strcpy(fileName, fileBaseName);
strcat(fileName, ".ads");
while ((fpSync = fopen(fileName, "rb")) == NULL)
sleep(1);
sleep(3);
hdr = new Header(fileName);
strcpy(fileName, fileBaseName);
strcat(fileName, ".2d");
fp2D = fopen(fileName, "rb");
strcpy(fileName, fileBaseName);
strcat(fileName, ".rawMCRa");
fpMCR = fopen(fileName, "rb");
strcpy(fileName, fileBaseName);
strcat(fileName, ".Grey");
fpGrey = fopen(fileName, "rb");
strcpy(fileName, fileBaseName);
strcat(fileName, ".avaps");
fpAVAPS = fopen(fileName, "rb");
} /* END OPENFILES */
/* -------------------------------------------------------------------- */
void ADS_rtFile::closeFiles()
{
if (fpSync)
fclose(fpSync);
if (fp2D)
fclose(fp2D);
if (fpGrey)
fclose(fpGrey);
if (fpMCR)
fclose(fpMCR);
if (fpAVAPS)
fclose(fpAVAPS);
fpSync = fp2D = fpGrey = fpMCR = fpAVAPS = NULL;
} /* END CLOSEFILES */
/* -------------------------------------------------------------------- */
void ADS_rtFile::resetFiles()
{
if (fpSync)
rewind(fpSync);
if (fp2D)
rewind(fp2D);
if (fpGrey)
rewind(fpGrey);
if (fpMCR)
rewind(fpMCR);
if (fpAVAPS)
rewind(fpAVAPS);
} /* END RESETFILE */
/* END ADSIORT.CC */