/
Dorade.cpp
1768 lines (1542 loc) · 45 KB
/
Dorade.cpp
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
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Dorade.cpp
*
* Copyright 2011 Michael Bell
* All rights reserved.
*
*/
#include "Dorade.h"
#include <stdlib.h> //exit(1)
Dorade::Dorade()
{
swap_bytes = false;
ssptr = new sswb_info;
vptr = new vold_info;
rptr = new radd_info;
cptr = new celv_info;
cfptr = new cfac_info;
pptr = new parm_info[40];
sptr = new swib_info;
ryptr = new ryib_info[1500];
aptr = new asib_info[1500];
dptr = new rdat_info*[40];
for (int i=0; i<40; i++) {
dptr[i] = new rdat_info[1500]; //<-- might produce the issue
}
rangles = new radar_angles[1500];
rkptr = new rktb_info;
rtptr = new rot_table_entry[1500];
refIndex = velIndex = swIndex = 0;
ref_fld = "DBZ";
vel_fld = "VG";
sw_fld = "SW";
}
Dorade::Dorade(const QString& swpFilename)
{
swap_bytes = false;
ssptr = new sswb_info;
vptr = new vold_info;
rptr = new radd_info;
cptr = new celv_info;
cfptr = new cfac_info;
pptr = new parm_info[40];
sptr = new swib_info;
ryptr = new ryib_info[1500];
aptr = new asib_info[1500];
dptr = new rdat_info*[40];
for (int i=0; i<40; i++) {
dptr[i] = new rdat_info[1500];
}
rangles = new radar_angles[1500];
rkptr = new rktb_info;
rtptr = new rot_table_entry[1500];
refIndex = velIndex = swIndex = 0;
ref_fld = "DBZ";
vel_fld = "VG";
sw_fld = "SW";
setFilename(swpFilename);
}
Dorade::~Dorade()
{
delete ssptr;
delete vptr;
delete rptr;
delete cptr;
delete cfptr;
delete[] pptr;
delete sptr;
delete[] ryptr;
delete[] aptr;
for (int i=0; i<40; i++) {
delete[] dptr[i];
}
delete[] dptr;
delete[] rangles;
delete rkptr;
delete[] rtptr;
}
bool Dorade::readSwpfile()
{
// Check the byte order
if (!machineBigEndian()) {
swap_bytes = true;
}
// Read in a dorade file
QByteArray ba = filename.toLocal8Bit();
const char* ccfilename = ba.constData();
sweepread(ccfilename, ssptr, vptr, rptr, cptr,
cfptr, pptr, sptr, ryptr, aptr, dptr);
return true;
}
bool Dorade::readSwpfile(const QString& refname, const QString& velname, const QString& swname)
{
ref_fld = refname;
vel_fld = velname;
sw_fld = swname;
readSwpfile();
return true;
}
bool Dorade::writeSwpfile()
{
// Check the byte order
if (!machineBigEndian()) {
swap_bytes = true;
}
QByteArray ba = filename.toLocal8Bit();
const char* ccfilename = ba.constData();
int flag = 0;
sweepwrite(ccfilename, ssptr, vptr, rptr, cptr,
cfptr, pptr, sptr, ryptr, aptr, dptr, flag);
return true;
}
bool Dorade::writeSwpfile(const QString& newfilename)
{
// Check the byte order
if (!machineBigEndian()) {
swap_bytes = true;
}
// Add a suffix to indicate we've modified the file
filename = newfilename;
QByteArray ba = filename.toLocal8Bit();
const char* ccfilename = ba.constData();
int flag = 0;
sweepwrite(ccfilename, ssptr, vptr, rptr, cptr,
cfptr, pptr, sptr, ryptr, aptr, dptr, flag);
return true;
}
bool Dorade::writeDoradefile(const QString& doradeFilename)
{
// Check the byte order
if (!machineBigEndian()) {
swap_bytes = true;
}
// Change the output file to a dorade file
filename = doradeFilename;
QByteArray ba = filename.toLocal8Bit();
const char* ccfilename = ba.constData();
int flag = 1;
sweepwrite(ccfilename, ssptr, vptr, rptr, cptr,
cfptr, pptr, sptr, ryptr, aptr, dptr, flag);
return true;
}
QString Dorade::getFilename()
{
return filename;
}
void Dorade::setFilename(const QString& newname)
{
filename = newname;
}
QString Dorade::getRadarname()
{
QStringList pathparts = filename.split("/");
QStringList fileparts = pathparts.last().split(".");
QString radarname = fileparts[2];
return radarname;
}
float Dorade::getAzimuth(int& ray)
{
if ((ray >= 0) and (ray<(sptr->num_rays))) {
return ryptr[ray].azimuth;
} else {
return -999;
}
}
float Dorade::getElevation(int& ray)
{
if ((ray >= 0) and (ray<(sptr->num_rays))) {
return ryptr[ray].elevation;
} else {
return -999;
}
}
float* Dorade::getReflectivity(int& ray)
{
if ((ray >= 0) and (ray<(sptr->num_rays))) {
return dptr[refIndex][ray].data;
} else {
return 0;
}
}
float* Dorade::getRadialVelocity(int& ray)
{
if ((ray >= 0) and (ray<(sptr->num_rays))) {
return dptr[velIndex][ray].data ;
} else {
return 0;
}
}
float* Dorade::getSpectrumWidth(int &ray)
{
if ((ray >= 0) and (ray<(sptr->num_rays))) {
return dptr[swIndex][ray].data;
} else {
return 0;
}
}
float* Dorade::getRayData(int &ray, const QString& field)
{
int fldIndex = -1;
for (int j=0; j<(rptr->num_param_desc); j++) {
QString fieldName(pptr[j].parm_name);
if (fieldName.size() > 8) fieldName.resize(8);
fieldName.remove(QRegExp("[\\s+]"));
if (field == fieldName) {
// Match
fldIndex = j;
break;
}
}
if (fldIndex < 0) {
// No match
return NULL;
}
if (ray<(sptr->num_rays)) {
return dptr[fldIndex][ray].data;
} else {
return NULL;
}
}
int Dorade::getNumRays()
{
return sptr->num_rays;
}
int Dorade::getNumGates()
{
return cptr->total_gates;
}
float* Dorade::getGateSpacing()
{
return cptr->gate_spacing;
}
float Dorade::getRadarAltAGL()
{
return (aptr->alt_agl + cfptr->c_alt_agl);
}
float Dorade::getRadarAltMSL()
{
return (aptr->alt_msl + cfptr->c_alt_msl);
}
float Dorade::getRadarLat()
{
return aptr->lat;
}
float Dorade::getRadarLon()
{
return aptr->lon;
}
float Dorade::getRadarAltAGL(const int& ray)
{
if (ray < 0) return -999.;
if (ray > sptr->num_rays) return -999.;
// return aptr[ray].alt_agl;
return (aptr[ray].alt_agl + cfptr->c_alt_msl); /* c_alt_msl contains c_alt_msl or c_alt_agl
depending on the cns_eldo_cai input altitude
(1: pressure or msl or 2: geopotential or agl);
c_alt_agl is always zero in cfac file created
by cns_eldo_cai (RV)
*/
}
float Dorade::getRadarAltMSL(const int& ray)
{
if (ray < 0) return -999.;
if (ray > sptr->num_rays) return -999.;
return (aptr[ray].alt_msl + cfptr->c_alt_msl);
}
float Dorade::getRadarLat(const int& ray)
{
if (ray < 0) return -999.;
if (ray > sptr->num_rays) return -999.;
return aptr[ray].lat;
}
float Dorade::getRadarLon(const int& ray)
{
if (ray < 0) return -999.;
if (ray > sptr->num_rays) return -999.;
return aptr[ray].lon;
}
float Dorade::getFLwind_u(const int& ray)
{
if (ray < 0) return -999.;
if (ray > sptr->num_rays) return -999.;
return aptr[ray].ew_horiz_wind;
}
float Dorade::getFLwind_v(const int& ray)
{
if (ray < 0) return -999.;
if (ray > sptr->num_rays) return -999.;
return aptr[ray].ns_horiz_wind;
}
float Dorade::getHeading(const int& ray)
{
if (ray < 0) return -999.;
if (ray > sptr->num_rays) return -999.;
return aptr[ray].head;
}
float Dorade::getBeamwidthDeg()
{
float bw = (rptr->horiz_beam_width + rptr->vert_beam_width) * 0.5;
return bw;
}
float Dorade::getNyquistVelocity()
{
return rptr->unambig_vel;
}
QDateTime Dorade::getRayTime(int& ray)
{
int year = vptr->year;
int jd = ryptr[ray].julian_day;
QDate date = QDate(year,1,1);
date = date.addDays(jd-1);
int hour = ryptr[ray].hour;
int min = ryptr[ray].min;
int sec = ryptr[ray].sec;
int msec = ryptr[ray].msec;
QTime time = QTime(hour, min, sec, msec);
return QDateTime(date, time, Qt::UTC);
}
asib_info* Dorade::getAircraftBlock(const int& ray)
{
return &aptr[ray];
}
cfac_info* Dorade::getCfacBlock()
{
return cfptr;
}
ryib_info* Dorade::getRyibBlock(const int& ray)
{
return &ryptr[ray];
}
void Dorade::recalculateAirborneAngles()
{
if (rptr->scan_mode == 9) {
// Airborne data, need to calculate ground relative azimuth and elevation
for (int i=0; i < sptr->num_rays; i++) {
calcAirborneAngles(&aptr[i], cfptr, &ryptr[i], &rangles[i]);
}
}
}
double Dorade::getAircraftVelocity(const int& ray)
{
double vr = 0;
if ((ray >= 0) and (ray < sptr->num_rays))
vr = calcAircraftMotion(&aptr[ray], cfptr, &rangles[ray]);
return vr;
}
bool Dorade::copyField(const QString& oldFieldName, const QString& newFieldName,
const QString& newFieldDesc, const QString& newFieldUnits)
{
// Copy a field over and rename it
int oldIndex = -1;
int newIndex = rptr->num_param_desc;
for (int j=0; j<(rptr->num_param_desc); j++) {
QString fieldName = pptr[j].parm_name;
if (fieldName.size() > 8) fieldName.resize(8);
fieldName.remove(QRegExp("[\\s+]"));
if (oldFieldName == fieldName) {
// Match
oldIndex = j;
break;
}
}
if ((oldIndex < 0) or (oldIndex > newIndex)) {
// No match
return false;
}
// Increment rptr & sswb
rptr->num_param_desc++;
ssptr->num_params++;
// Copy parm info, rename it
pptr[newIndex] = pptr[oldIndex];
memset(pptr[newIndex].parm_name,' ',8);
for (int c=0; c < 8; c++) {
if (c < newFieldName.size()) {
QByteArray ba = newFieldName.toLocal8Bit();
pptr[newIndex].parm_name[c] = ba.at(c);
}
}
memset(pptr[newIndex].parm_desc,' ',40);
for (int c=0; c < 40; c++) {
if (c < newFieldDesc.size()) {
QByteArray ba = newFieldDesc.toLocal8Bit();
pptr[newIndex].parm_desc[c] = ba.at(c);
}
}
memset(pptr[newIndex].parm_unit,' ',8);
for (int c=0; c < 8; c++) {
if (c < newFieldUnits.size()) {
QByteArray ba = newFieldUnits.toLocal8Bit();
pptr[newIndex].parm_unit[c] = ba.at(c);
}
}
// Copy the RDAT blocks
for (int i=0; i<(sptr->num_rays); i++) {
dptr[newIndex][i] = dptr[oldIndex][i];
memset(dptr[newIndex][i].parm_name,' ',8);
for (int c=0; c < 8; c++) {
if (c < newFieldName.size()) {
QByteArray ba = newFieldName.toLocal8Bit();
dptr[newIndex][i].parm_name[c] = ba.at(c);
}
}
}
return true;
}
bool Dorade::copyField(const QString& oldFieldName, const QString& newFieldName)
{
// Copy a field over and rename it
int oldIndex = -1;
int newIndex = rptr->num_param_desc;
bool existing = false;
for (int j=0; j<(rptr->num_param_desc); j++) {
QString fieldName = pptr[j].parm_name;
if (fieldName.size() > 8) fieldName.resize(8);
fieldName.remove(QRegExp("[\\s+]"));
if (newFieldName == fieldName) {
// Match existing field
existing = true;
newIndex = j;
}
if (oldFieldName == fieldName) {
// Match
oldIndex = j;
}
}
if ((oldIndex < 0) or (oldIndex > rptr->num_param_desc)) {
// No match
return false;
}
if (!existing) {
// Increment rptr & sswb
rptr->num_param_desc++;
ssptr->num_params++;
// Copy parm info, rename it
pptr[newIndex] = pptr[oldIndex];
memset(pptr[newIndex].parm_name,' ',8);
for (int c=0; c < 8; c++) {
if (c < newFieldName.size()) {
QByteArray ba = newFieldName.toLocal8Bit();
pptr[newIndex].parm_name[c] = ba.at(c);
}
}
}
// Copy the RDAT blocks
for (int i=0; i<(sptr->num_rays); i++) {
dptr[newIndex][i] = dptr[oldIndex][i];
memset(dptr[newIndex][i].parm_name,' ',8);
for (int c=0; c < 8; c++) {
if (c < newFieldName.size()) {
QByteArray ba = newFieldName.toLocal8Bit();
dptr[newIndex][i].parm_name[c] = ba.at(c);
}
}
}
return true;
}
/* Private routines */
/**************************************************/
bool Dorade::machineBigEndian(){
union {
unsigned char byte[4];
int val;
}word;
word.val = 0;
word.byte[3] = 0x01;
return word.val == 1;
}
/**************************************************/
int short Dorade::swap2(char *ov) /* swap integer*2 */
{
union {
int short newval;
char nv[2];
}u;
u.nv[1] = *ov++; u.nv[0] = *ov++;
return(u.newval);
}
/**************************************************/
int Dorade::swap4(char *ov ) /* swap integer*4 */
{
union {
int newval;
char nv[4];
}u;
u.nv[3] = *ov++; u.nv[2] = *ov++; u.nv[1] = *ov++; u.nv[0] = *ov++;
return(u.newval);
}
/**************************************************/
void Dorade::sweepread(const char swp_fname[],struct sswb_info *ssptr, struct vold_info *vptr,
struct radd_info *rptr,struct celv_info *cptr,
struct cfac_info *cfptr,struct parm_info *pptr,
struct swib_info *sptr,struct ryib_info *ryptr,
struct asib_info *aptr, struct rdat_info **dptr)
{
/****************************************************/
/* THIS SUBROUTINE READS THE DESCRIPTORS FROM A */
/* SWEEP FILE & PASSES THE VOLD, RADD, CFAC, PARM, */
/* SWIB BACK TO THE FORTRAN PROGRAM */
/* swp_fname=name of sweep file */
/* *vptr=pointer to vold descriptor */
/* *rptr=pointer to radd descriptor */
/* *cptr=pointer to cfac descriptor */
/* *pptr=pointer to parm descriptor */
/* *sptr=pointer to swib descriptor */
/* *ryptr=pointer to ryib descriptor */
/* *aptr=pointer to asib descriptor */
/* *dptr=pointer to rdat descriptor */
/* tot_gates=total number of gates */
/* arr=array to hold gate spacing */
/* *sptr=pointer of swib descriptor */
/****************************************************/
FILE *fp;
int i=0;
char identifier[IDENT_LEN]; //defined in read_dorade.h (RV)
int desc_len;
int fld_num=0;
int match;
int found=false;
int beam=-1;
i = 0;
/* ADD NULL CHARACTER TO END OF STRING
for (unsigned int i=0;i<strlen(swp_fname);i++) {
if (isspace(swp_fname[i])) {break;}
else {len++;}
}
swp_fname[len]='\0'; */
/* READ THE SWEEP FILE */
/* OPEN THE SWEEP FILE */
if ( (fp = fopen(swp_fname,"rb"))==NULL) {
printf("Can't open %s\n",swp_fname);
}
while ( !feof(fp) ) {
/* READ THE DESCRIPTOR IDENTIFIER */
//printf ("at %d\n",ftell(fp));
if ( (fread(identifier,sizeof(char),IDENT_LEN,fp)) != IDENT_LEN) {
printf("sweep file read error..can't read identifier\n");
exit(-1);
}
/* READ THE DESCRIPTOR LENGTH */
desc_len=read_int(fp);
// printf ("reading %s at %d\n",identifier,ftell(fp));
// printf("desc_len: %d\n",desc_len);
if ( (strncmp(identifier,"SSWB",IDENT_LEN)) == 0) {
/* READ THE SSWB DESCRIPTOR */
/*printf ("reading vold\n");*/
read_sswb(fp,ssptr);
} else if ( (strncmp(identifier,"VOLD",IDENT_LEN)) == 0) {
/* READ THE VOLUME DESCRIPTOR */
/*printf ("reading vold\n");*/
read_vold(fp,vptr);
} else if ( (strncmp(identifier,"RADD",IDENT_LEN)) == 0) {
/* READ THE RADAR DESCRIPTOR */
/*printf ("reading radd\n");*/
read_radd(fp,rptr);
if (rptr->num_param_desc > MAX_NUM_PARMS) {
printf ("WARNING: NUMBER OF PARAMETERS ");
printf ("GREATER THAN MAX_NUM_PARMS\n");
printf ("NUMBER OF PARAMETERS: %d\n",rptr->num_param_desc);
printf ("MAX_NUM_PARMS: %d\n",MAX_NUM_PARMS);
printf ("SEE READ_DORADE.H\n");
}
} else if ( (strncmp(identifier,"CFAC",IDENT_LEN)) == 0) {
/* READ THE CFAC DESCRIPTOR */
/*printf ("reading cfac\n");*/
read_cfac(fp,cfptr);
} else if ( (strncmp(identifier,"PARM",IDENT_LEN)) == 0) {
/* READ THE PARAMETER DESCRIPTOR */
/*printf ("reading parm\n");*/
read_parm(fp,pptr);
*pptr++;
} else if ( (strncmp(identifier,"CELV",IDENT_LEN)) == 0) {
/*printf ("reading CELV\n");*/
/* CHECK & MAKE SURE NUMBER OF GATES DOESN'T
EXCEED LIMIT */
read_celv(fp,cptr,desc_len);
if (cptr->total_gates>MAX_GATES) {
printf ("WARNING: NUMBER OF GATES ");
printf ("GREATER THAN MAX_GATES\n");
printf ("SEE READ_DORADE.H\n");
printf ("NUMBER OF GATES: %d\n",(int)cptr->total_gates);
printf ("MAX_GATES: %d\n",MAX_GATES);
}
} else if ( (strncmp(identifier,"SWIB",IDENT_LEN)) == 0) {
/* READ THE SWEEP INFO DESCRIPTOR */
/*printf ("reading swib\n");*/
read_swib(fp,sptr);
if (sptr->num_rays > MAX_BEAMS) {
printf ("WARNING: NUMBER OF BEAMS ");
printf ("GREATER THAN MAX_BEAMS\n");
printf ("SEE READ_DORADE.H\n");
printf ("NUMBER OF BEAMS: %d\n",(int)sptr->num_rays);
printf ("MAX_BEAMS: %d\n",MAX_BEAMS);
}
} else if ( (strncmp(identifier,"RYIB",IDENT_LEN)) == 0) {
/* READ THE RAY INFO DESCRIPTOR */
/*printf ("reading ryib\n");*/
beam++;
read_ryib(fp,&ryptr[beam]);
fld_num=0;
/* GO BACK TO FIRST PARAMETER DESCRIPTOR */
for (i=0;i<rptr->num_param_desc;i++) {*pptr--;}
/* DID WE FIND THE FIELD??
if (beam==2 && found==FALSE) {
printf ("%s NOT FOUND..\n",fld_name);
printf ("VALID FIELD NAMES:\n");
for (i=0;i<rptr->num_param_desc;i++) {
printf ("%s\n",pptr->parm_name);
*pptr++;
}
printf ("EXITING..\n");
exit(-1);
}
*/
} else if ( (strncmp(identifier,"ASIB",IDENT_LEN)) == 0) {
/* READ THE PLATFORM INFO DESCRIPTOR */
/*printf ("reading asib\n");*/
read_asib(fp,&aptr[beam]);
//*aptr++;
} else if ( (strncmp(identifier,"RDAT",IDENT_LEN)) == 0) {
/* READ THE DATA DESCRIPTOR */
// printf ("reading rdat %s %d:\n", fld_name, desc_len);
match=false;
read_rdat(fp,fld_num,desc_len,&match,
pptr->parm_type,beam,cptr->total_gates,
rptr->compress_flag,pptr->baddata_flag,
pptr->scale_fac,&dptr[fld_num][beam]);
fld_num++;
*pptr++;
if (match==true) {
// *dptr++;
found=match;
}
} else if ( (strncmp(identifier,"NULL",IDENT_LEN)) == 0) {
skip_bytes(fp,desc_len-(IDENT_LEN+sizeof(int)));
} else if ( (strncmp(identifier,"RKTB",IDENT_LEN)) == 0) {
/* READ THE ROTATION ANGLE DESCRIPTOR */
/*printf ("reading rktb\n");*/
rktb_size = desc_len;
read_rktb(fp);
// That should be the end
break;
} else {
// printf ("Dorade.cpp HOLA1\n");
// printf("Identifier: %s\n",identifier);
// printf("desc_len: %d\n",desc_len);
// // printf("ftell: %s\n",ftell(fp)); // <-- segmentation fault with ftell(fp) (RV)
// printf ("Dorade.cpp HOLA2\n");
// exit(1);
skip_bytes(fp,desc_len-(IDENT_LEN+sizeof(int))); //<--"Seek Error..aborting" (RV)
} /* endif */
} /* endwhile */
fclose(fp);
if (rptr->scan_mode == 9) {
// Airborne data, need to calculate ground relative azimuth and elevation
for (int i=0; i < sptr->num_rays; i++) {
calcAirborneAngles(&aptr[i], cfptr, &ryptr[i], &rangles[i]);
}
}
}
/**************************************************/
void Dorade::sweepwrite(const char swp_fname[],struct sswb_info *ssptr,struct vold_info *vptr,
struct radd_info *rptr,struct celv_info *cptr,
struct cfac_info *cfptr,struct parm_info *pptr,
struct swib_info *sptr,struct ryib_info *ryptr,
struct asib_info *aptr, struct rdat_info **dptr, int doradeFlag)
{
FILE *fp;
char* identifier;
QString block;
QByteArray blocka;
// int desc_len;
long desc_len; // see if fixes reorder problem(RV)
identifier = new char[4];
if (doradeFlag) {
if ( (fp = fopen(swp_fname,"ab"))==NULL) {
printf("Can't open %s\n",swp_fname);
}
} else {
if ( (fp = fopen(swp_fname,"wb"))==NULL) {
printf("Can't open %s\n",swp_fname);
}
}
/* Write it out one block at a time */
if (!doradeFlag) {
/* SSWB */
block = "SSWB";
blocka = block.toLocal8Bit();
identifier = blocka.data();
if ( (fwrite(identifier,sizeof(char),IDENT_LEN,fp)) != IDENT_LEN) {
printf("sweep file read error..can't read identifier\n");
exit(-1);
}
desc_len = sizeof(sswb_info)+8;
if ( (fwrite(&desc_len,sizeof(int),1,fp)) != 1) {
printf("sweep file read error..\n");
}
if ( fwrite ((char *)ssptr,sizeof (struct sswb_info),1,fp) !=1 ) {
puts("ERROR WRITING SSWB DESCRIPTOR\n");
exit(-1);
}
}
/* VOLD */
block = "VOLD";
blocka = block.toLocal8Bit();
identifier = blocka.data();
if ( (fwrite(identifier,sizeof(char),IDENT_LEN,fp)) != IDENT_LEN) {
printf("sweep file read error..can't read dentifier\n");
exit(-1);
}
desc_len = sizeof(vold_info)+8;
if ( (fwrite(&desc_len,sizeof(int),1,fp)) != 1) {
printf("sweep file read error..\n");
}
if ( fwrite ((char *)vptr,sizeof (struct vold_info),1,fp) !=1 ) {
puts("ERROR WRITING VOLUME DESCRIPTOR\n");
exit(-1);
}
/* RADD */
block = "RADD";
blocka = block.toLocal8Bit();
identifier = blocka.data();
if ( (fwrite(identifier,sizeof(char),IDENT_LEN,fp)) != IDENT_LEN) {
printf("sweep file read error..can't read identifier\n");
exit(-1);
}
desc_len = sizeof(radd_info)+8;
if ( (fwrite(&desc_len,sizeof(int),1,fp)) != 1) {
printf("sweep file read error..\n");
}
// Try uncompressed data
//rptr->compress_flag = 0;
rptr->compress_flag = 1;
if ( fwrite ((char *)rptr,sizeof (struct radd_info),1,fp) !=1 ) {
puts("ERROR WRITING VOLUME DESCRIPTOR\n");
exit(-1);
}
/* PARAMETERS */
/* GO BACK TO FIRST PARAMETER DESCRIPTOR */
//for (int i=0; i<rptr->num_param_desc; i++) {*pptr--;}
for (int i=0; i<rptr->num_param_desc; i++) {
block = "PARM";
blocka = block.toLocal8Bit();
identifier = blocka.data();
if ( (fwrite(identifier,sizeof(char),IDENT_LEN,fp)) != IDENT_LEN) {
printf("sweep file read error..can't read identifier\n");
exit(-1);
}
desc_len = sizeof(parm_info)+8;
if ( (fwrite(&desc_len,sizeof(int),1,fp)) != 1) {
printf("sweep file read error..\n");
}
if ( fwrite ((char *)&pptr[i],sizeof (struct parm_info),1,fp) !=1 ) {
puts("ERROR WRITING VOLUME DESCRIPTOR\n");
exit(-1);
}
}
/* CELV */
block = "CELV";
blocka = block.toLocal8Bit();
identifier = blocka.data();
if ( (fwrite(identifier,sizeof(char),IDENT_LEN,fp)) != IDENT_LEN) {
printf("sweep file read error..can't read identifier\n");
exit(-1);
}
desc_len = sizeof(celv_info)+8;
if ( (fwrite(&desc_len,sizeof(int),1,fp)) != 1) {
printf("sweep file read error..\n");
}
if ( fwrite ((char *)cptr,sizeof (struct celv_info),1,fp) !=1 ) {
puts("ERROR WRITING VOLUME DESCRIPTOR\n");
exit(-1);
}
/* CFAC */
block = "CFAC";
blocka = block.toLocal8Bit();
identifier = blocka.data();
if ( (fwrite(identifier,sizeof(char),IDENT_LEN,fp)) != IDENT_LEN) {
printf("sweep file read error..can't read identifier\n");
exit(-1);
}
desc_len = sizeof(cfac_info) + 8;
if ( (fwrite(&desc_len,sizeof(int),1,fp)) != 1) {
printf("sweep file read error..\n");
}
if ( fwrite ((char *)cfptr,sizeof (struct cfac_info),1,fp) !=1 ) {
puts("ERROR WRITING VOLUME DESCRIPTOR\n");
exit(-1);
}
/* SWIB */
block = "SWIB";
blocka = block.toLocal8Bit();
identifier = blocka.data();
if ( (fwrite(identifier,sizeof(char),IDENT_LEN,fp)) != IDENT_LEN) {
printf("sweep file read error..can't read identifier\n");
exit(-1);
}
desc_len = sizeof(swib_info)+8;
if ( (fwrite(&desc_len,sizeof(int),1,fp)) != 1) {
printf("sweep file read error..\n");
}
if ( fwrite ((char *)sptr,sizeof (struct swib_info),1,fp) !=1 ) {
puts("ERROR WRITING VOLUME DESCRIPTOR\n");
exit(-1);
}
/* RAYS */
for (int i=0; i<(sptr->num_rays); i++) {
// Update RKTB
rtptr[i].offset = ftell(fp);
/* RYIB */
block = "RYIB";
blocka = block.toLocal8Bit();
identifier = blocka.data();
if ( (fwrite(identifier,sizeof(char),IDENT_LEN,fp)) != IDENT_LEN) {
printf("sweep file read error..can't read identifier\n");
exit(-1);
}
desc_len = sizeof(ryib_info)+8;
if ( (fwrite(&desc_len,sizeof(int),1,fp)) != 1) {
printf("sweep file read error..\n");
}
if ( fwrite ((char *)&ryptr[i],sizeof (struct ryib_info),1,fp) !=1 ) {
puts("ERROR WRITING VOLUME DESCRIPTOR\n");
exit(-1);
}
/* ASIB */
block = "ASIB";
blocka = block.toLocal8Bit();
identifier = blocka.data();
if ( (fwrite(identifier,sizeof(char),IDENT_LEN,fp)) != IDENT_LEN) {
printf("sweep file read error..can't read identifier\n");
exit(-1);
}
desc_len = sizeof(asib_info)+8;
if ( (fwrite(&desc_len,sizeof(int),1,fp)) != 1) {
printf("sweep file read error..\n");
}
if ( fwrite ((char *)&aptr[i],sizeof (struct asib_info),1,fp) !=1 ) {
puts("ERROR WRITING VOLUME DESCRIPTOR\n");
exit(-1);
}
short arr_com[MAX_GATES], arr_uncom[MAX_GATES];
unsigned int num_words;
for (int j=0; j<(rptr->num_param_desc); j++) {
/* RDAT */
/* RESCALE THE DATA */
int arrsize = cptr->total_gates;
int baddata_flag = pptr[j].baddata_flag;
float scale_fac = pptr[j].scale_fac;
for (int k=0;k<arrsize;k++) {
arr_uncom[k]=0;
if (dptr[j][i].data[k] != baddata_flag) {
arr_uncom[k] = dptr[j][i].data[k]*scale_fac;
} else {
arr_uncom[k] = dptr[j][i].data[k];
}
//printf("%d\n",arr_uncom[k]);
}
/* COMPRESS A RAY OF DATA */
num_words=dd_compress((unsigned short *)&arr_uncom,(unsigned short *)&arr_com,(unsigned short)baddata_flag,arrsize);
block = "RDAT";
blocka = block.toLocal8Bit();
identifier = blocka.data();
if ( (fwrite(identifier,sizeof(char),IDENT_LEN,fp)) != IDENT_LEN) {
printf("sweep file read error..can't read identifier\n");
exit(-1);
}
//desc_len = PARM_NAME_LEN + num_words*sizeof(short)+8;
unsigned int bytes = INTS_TO_BYTES(SHORTS_TO_INTS(num_words));
desc_len = bytes + PARM_NAME_LEN + 8;
// Uncompressed
//desc_len = PARM_NAME_LEN + arrsize*sizeof(short)+8;
if ( (fwrite(&desc_len,sizeof(int),1,fp)) != 1) {
printf("sweep file read error..\n");
}
if ( fwrite ((char *)&dptr[j][i].parm_name,sizeof(char),PARM_NAME_LEN,fp) != PARM_NAME_LEN ) {
puts("ERROR WRITING RDAT DESCRIPTOR\n");
exit(-1);