-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradar_to_hdf_1.c
1107 lines (987 loc) · 38.3 KB
/
radar_to_hdf_1.c
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
/*
NASA/TRMM, Code 910.1.
This is the TRMM Office Radar Software Library.
Copyright (C) 1996, 1997
Mike Kolander
Space Applications Corporation
Vienna, Virginia
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_LIBTSDISTK
/******************************************************************
Writes one VOS from a RSL radar structure into one 1B-51/1C-51 HDF
file.
A 1B-51/1C-51 HDF file contains multiple VOS's recorded by a radar
site during a 1-hour time period, and the HDF file is named using
the date/hour of the constituent VOS's.
Functions defined herein perform tasks preparatory to building a
TSDIS toolkit 'L1B_1C_GV' structure, the contents of which are
written into the HDF file by the TSDIS toolkit. Construction of the
toolkit 'L1B_1C_GV' structure is done via the subroutines defined
in RSL file 'radar_to_hdf_2.c'.
-----------------------------------------------------------------
Libraries required for execution of this code :
-ltsdistk : TSDIS toolkit
-lmfhdf -ldf -ljpeg -lz : HDF
-lrsl : rsl
-lm : C math
-----------------------------------------------------------------
*******************************************************************/
#include <math.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
/* TSDIS toolkit function and structure definitions. */
#include "IO.h"
#include "IO_GV.h"
/* RSL function and structure definitions. */
#include "rsl.h"
/* Parameter definitions for 1B-51 and 1C-51 HDF
file handling applications using the TSDIS toolkit. */
#include "toolkit_1BC-51_appl.h"
/*************************************************************/
/* */
/* Function Prototypes */
/* */
/*************************************************************/
void RSL_set_tkMetaDataString(char *string, int param);
void RSL_set_hdf_qc_parameters(float *inParm);
void radarVolumesSave(Volume *v[MAX_RADAR_VOLUMES], Radar *radar);
void radarVolumesRestore(Radar *radar, Volume *v[MAX_RADAR_VOLUMES]);
int nextVolume(Radar *radar, int last_volume);
Volume *maskBuild(Volume *cv, Volume *ucv);
void metaDataWrite(IO_HANDLE *granuleHandle, Radar *radar,
char *hdfFileName, int fileAccessMode);
int tkVosDimensions(VosSize *vs, Radar *radar);
Ray *first_ray_in_sweep(Sweep *sweep);
int rslVosDimensions(VosSize *vs, Radar *radar, float maxRange);
int L1GVtemplateInit(VosSize *vs, Radar *radar, char *hdfFileName,
float maxRange);
static int hdfFileOpen(IO_HANDLE *granuleHandle, VosSize *vs,
char *hdfFileName, Radar *radar);
int radarPrep1B51(Radar *radar);
int radarPrep1C51(Radar *radar);
int nullGranuleCreate(char *hdfFileName, IO_HANDLE *granuleHandle,
Radar *radar);
int RSL_radar_to_hdf(Radar *radar, char *hdfFileName);
extern L1B_1C_GV *gvl1Build(Radar *radar, float *qcParm, VosSize *vs,
int productID);
extern int radar_verbose_flag;
/* The 1st non-NULL ray in each volume is widely used. Hence global. */
Ray *first_ray_in_volume[MAX_RADAR_VOLUMES];
/*************************************************************/
/* */
/* RSL_set_tkMetaDataString */
/* */
/*************************************************************/
static struct
{
char GenInputDate[128];
char AlgorithmVersion[128];
int ProductVersion;
char SoftwareVersion[128];
} tkMetaDataString;
void RSL_set_tkMetaDataString(char *string, int param)
{
/* Allows the application 'level_1' to store metadata strings into
the tkMetaDataString buffers for later insertion by RSL function
'metaDataWrite()' into the HDF file.
Call **before** RSL_radar_to_hdf().
*/
#define CP_TKMETA(s, str) \
memset(tkMetaDataString.s, '\0', sizeof(tkMetaDataString.s));\
strncpy(tkMetaDataString.s, str, sizeof(tkMetaDataString.s)-1)
if (param == TK_GEN_DATE_INPUT_FILES) {
CP_TKMETA(GenInputDate, string);
/* 1C-51 kludge...
To decide whether or not to write a VOS into a 1C-51 HDF file
(See 'hdf1C51Create()' in file 'level_1.c'), I need to know
something about the times of the VOSs already in the file;
ie, if no satellite overpass, the file is to contain just one
VOS from each half-hour. The only efficient way to do this is
by encoding time info into a metadata field.
The 'GenInputDate' metaData string contains info indicating
which hourly time slots are filled by the VOSs contained in the
HDF file. (See function 'metaDataWrite()' in this file for details.)
Called by 'hdfFilePeek()' in file 'level_1.c'.
*/
}
else if (param == TK_ALGORITHM_VERSION) {
CP_TKMETA(AlgorithmVersion, string);
} else if (param == TK_SOFTWARE_VERSION) {
CP_TKMETA(SoftwareVersion, string);
if (radar_verbose_flag)
fprintf(stderr, "TK_SOFTWARE_VERSION = <%s>\n", string);
} else if (param == TK_PRODUCT_VERSION) {
sscanf(string, "%d", &tkMetaDataString.ProductVersion);
} else {
fprintf(stderr, "RSL_set_tkMetaDataString: Unknown param==%d!!\n", param);
}
}
/*************************************************************/
/* */
/* RSL_set_hdf_qc_parameters */
/* */
/*************************************************************/
static float qcParm[NUMBER_QC_PARAMS] =
{
NOVAL_FLOAT, NOVAL_FLOAT, NOVAL_FLOAT, NOVAL_FLOAT, NOVAL_FLOAT,
NOVAL_FLOAT, NOVAL_FLOAT, NOVAL_FLOAT, NOVAL_FLOAT, NOVAL_FLOAT
};
void RSL_set_hdf_qc_parameters(float *inParm)
{
/* Stores 1C-51 QC parameters for later insertion into 1C-51 HDF
file. Call **before** RSL_radar_to_hdf().
*/
int j;
for (j=0; j<NUMBER_QC_PARAMS; j++)
qcParm[j] = inParm[j];
}
/*************************************************************/
/* */
/* radarVolumesSave */
/* */
/*************************************************************/
void radarVolumesSave(Volume *v[MAX_RADAR_VOLUMES], Radar *radar)
{
/* Save array of radar volume pointers. */
int j;
for (j=0; j<radar->h.nvolumes; j++)
v[j] = radar->v[j];
}
/*************************************************************/
/* */
/* radarVolumesRestore */
/* */
/*************************************************************/
void radarVolumesRestore(Radar *radar, Volume *v[MAX_RADAR_VOLUMES])
{
/* Restore array of radar volume pointers. */
int j;
for (j=0; j<radar->h.nvolumes; j++)
radar->v[j] = v[j];
}
/*************************************************************/
/* */
/* nextVolume */
/* */
/*************************************************************/
int nextVolume(Radar *radar, int last_volume)
/* Find the index of the next volume in the radar structure, given
the index of the last volume found.
Returns:
index of next volume, if success.
-1 if failure.
*/
{
int j;
for (j=last_volume+1; j<radar->h.nvolumes; j++)
if (radar->v[j] != NULL)
return(j); /* Found volume. Return the index. */
return(-1); /* No volume found. Return bogus index. */
}
/*************************************************************/
/* */
/* maskBuild */
/* */
/*************************************************************/
Volume *maskBuild(Volume *cv, Volume *ucv)
{
/* Create a mask volume. A mask value, in conjunction with a corresponding
corrected reflectivity value, enables the future recovery of a
uncorrected value from a 1C-51 HDF file, since the original, uncorrected
reflectivity values are not stored in 1C-51 HDF files.
This function used for both reflectivity 'DZ' and differential reflectivity
'ZD'.
cv: corrected volume pointer. (CZ or CD)
ucv: uncorrected volume pointer. (DZ or ZD)
mv: mask volume pointer. (MZ or MD)
*/
Volume *mv;
int sindex, rindex, bindex;
mv = RSL_copy_volume(cv);
mv->h.f = MZ_F; /* MZ_F identical to MD_F. Can use either. */
mv->h.invf = MZ_INVF; /* MZ_INVF identical to MD_INVF. Can use either. */
for (sindex=0; sindex<cv->h.nsweeps; sindex++)
{
if (cv->sweep[sindex] == NULL) continue;
mv->sweep[sindex]->h.f = MZ_F;
mv->sweep[sindex]->h.invf = MZ_INVF;
for (rindex=0; rindex<cv->sweep[sindex]->h.nrays; rindex++)
{
if (cv->sweep[sindex]->ray[rindex] == NULL) continue;
mv->sweep[sindex]->ray[rindex]->h.f = MZ_F;
mv->sweep[sindex]->ray[rindex]->h.invf = MZ_INVF;
for (bindex=0; bindex<cv->sweep[sindex]->ray[rindex]->h.nbins; bindex++)
/* Has the Z_value been corrected by the QC algorithm? */
if (cv->sweep[sindex]->ray[rindex]->range[bindex] == cv->h.invf(BADVAL))
mv->sweep[sindex]->ray[rindex]->range[bindex] = 1;
else /* Uncorrected Z_value. */
mv->sweep[sindex]->ray[rindex]->range[bindex] = 0;
} /* end for (rindex=0; ... */
} /* end for (sindex=0; ... */
return(mv);
}
/*************************************************************/
/* */
/* metaDataWrite */
/* */
/*************************************************************/
void metaDataWrite(IO_HANDLE *granuleHandle, Radar *radar,
char *hdfFileName, int fileAccessMode)
{
/* Write out some metadata values into toolkit structures. */
/*
* YOU REALLY DON'T KNOW, A PRIORI, WHAT DATATYPE THE METADATA ITEM IS.
* TRIAL and ERROR is how we figured some of them out.
*
* THEY SHOULD ALL BE STRINGS!!!!!
*/
char buf[1024];
int intVal, vindex;
float floatVal;
DATE_STR tkdate;
TIME_STR tktime;
vindex = nextVolume(radar, -1); /* Find 1st non-NULL rsl volume. */
/* ----------------- Core MetaData ---------------*/
/* Begin/end date of the HDF granule. */
tkdate.tkyear = (short) radar->h.year;
tkdate.tkmonth = (short) radar->h.month;
tkdate.tkday = (short) radar->h.day;
TKwriteMetadataInt(granuleHandle, TK_BEGIN_DATE, &tkdate);
TKwriteMetadataInt(granuleHandle, TK_END_DATE, &tkdate);
/* Begin time of the HDF granule. */
tktime.tkhour = (int8) radar->h.hour;
tktime.tkminute = (int8) 0;
tktime.tksecond = (int8) 0;
TKwriteMetadataInt(granuleHandle, TK_BEGIN_TIME, &tktime);
/* End time of the HDF granule. */
tktime.tkhour = (int8) radar->h.hour;
tktime.tkminute = (int8) 59;
tktime.tksecond = (int8) 59;
TKwriteMetadataInt(granuleHandle, TK_END_TIME, &tktime);
/* Longitude & Bounding Coordinates */
floatVal = radar->h.lond + radar->h.lonm/60.0 + radar->h.lons/3600.0;
TKwriteMetadataFloat(granuleHandle, TK_CENTER_POINT_LON, &floatVal);
floatVal = floatVal - 2.0;
TKwriteMetadataFloat(granuleHandle, TK_WEST_BOUND_COORD, &floatVal);
floatVal = floatVal + 4.0;
TKwriteMetadataFloat(granuleHandle, TK_EAST_BOUND_COORD, &floatVal);
/* Latitude & Bounding Coordinates */
floatVal = radar->h.latd + radar->h.latm/60.0 + radar->h.lats/3600.0;
TKwriteMetadataFloat(granuleHandle, TK_CENTER_POINT_LAT, &floatVal);
floatVal = floatVal - 2.0;
TKwriteMetadataFloat(granuleHandle, TK_SOUTH_BOUND_COORD, &floatVal);
floatVal = floatVal + 4.0;
TKwriteMetadataFloat(granuleHandle, TK_NORTH_BOUND_COORD, &floatVal);
TKwriteMetadataChar(granuleHandle, TK_CONTACT, "Danny Rosenfeld");
/* ----------------- PS MetaData ---------------*/
TKwriteMetadataChar(granuleHandle, TK_ALGORITHM_VERSION,
tkMetaDataString.AlgorithmVersion);
TKwriteMetadataInt(granuleHandle, TK_PRODUCT_VERSION,
&tkMetaDataString.ProductVersion);
TKwriteMetadataChar(granuleHandle, TK_SOFTWARE_VERSION,
tkMetaDataString.SoftwareVersion);
TKwriteMetadataChar(granuleHandle, TK_MAX_VALID_CHANNEL, "70 dBz");
TKwriteMetadataChar(granuleHandle, TK_MIN_VALID_CHANNEL, "-20 dBz");
if (radar->h.nvolumes > 0)
{
floatVal = first_ray_in_volume[vindex]->h.wavelength;
}
else /* No radar volumes. Creating empty granule. */
{
TKwriteMetadataChar(granuleHandle, TK_ANOMALY_FLAG, "EMPTY: REASON UNKNOWN");
floatVal = 0.0;
}
TKwriteMetadataFloat(granuleHandle, TK_RADAR_WAVELENGTH, &floatVal);
floatVal = -20.0; /* MIN_REFL_THRESHOLD */
TKwriteMetadataFloat(granuleHandle, TK_MIN_REF_THRESHOLD, &floatVal);
TKwriteMetadataChar(granuleHandle, TK_RADAR_NAME, radar->h.radar_name);
TKwriteMetadataChar(granuleHandle, TK_RADAR_CITY, radar->h.city);
TKwriteMetadataChar(granuleHandle, TK_RADAR_STATE, radar->h.state);
TKwriteMetadataChar(granuleHandle, TK_RADAR_COUNTRY, radar->h.country);
intVal = (int)TKgetNvos(granuleHandle);
TKwriteMetadataInt(granuleHandle, TK_NUM_VOS, &intVal);
TKwriteMetadataFloat(granuleHandle, TK_GV_DZCAL, &qcParm[ZCAL]);
floatVal = X; /* Mask scale factor */
TKwriteMetadataFloat(granuleHandle, TK_GV_L1C_SCALE, &floatVal);
floatVal = 0.0; /* Correction for gaseous two-way attenuation */
TKwriteMetadataFloat(granuleHandle, TK_GV_ALPHA, &floatVal);
TKwriteMetadataChar(granuleHandle, TK_INPUT_FILES, "8mm tape files");
TKwriteMetadataChar(granuleHandle, TK_DATA_CENTER_SRC, radar->h.radar_name);
/* I really, really hate this 1C-51 kludge, but...
To decide (within function 'hdf1C51Create') whether or not to write
a VOS into a 1C-51 file, I need to know something about the times
of the VOSs already in the file; ie, if no satellite overpass, the
file is to contain just one VOS from each half-hour.
The only efficient way to do this is by encoding info into a metadata
field.
So... I use the 'GEN_DATE_INPUT_FILES' metaData field. The intial
value of this field is the string 'unKNOWN'. When a VOS from the
first half_of_the_hour is written into the file, change the first
char 'u' to upper_case. When a VOS from the second half_of_the_hour
is written into the file, change the second char 'n' to upper_case.
The final string, after both time slots are filled, is entirely
upper_case: 'UNKOWN'.
*/
if (granuleHandle->productID == TK_L1C_GV) /* 1C-51 file? */
{
/* If new file, then intitialize 'GEN_DATE_INPUT_FILES' string.
If file exists, then get the existing string from the file. */
if (fileAccessMode == TK_NEW_FILE) strcpy(buf, "unKNOWN");
else strcpy(buf, tkMetaDataString.GenInputDate);
if (radar->h.nvolumes > 0) /* Don't mess with an empty granule. */
{
if (radar->h.minute < 30) /* 1st half_of_the_hour? */
buf[0] = (char) toupper((int)buf[0]); /* Convert char to upper_case */
else /* 2nd half_of_the_hour */
buf[1] = (char) toupper((int)buf[1]); /* Convert char to upper_case */
}
}
else /* 1B-51 file */
{
strcpy(buf, "UNKNOWN");
}
TKwriteMetadataChar(granuleHandle, TK_GEN_DATE_INPUT_FILES, buf);
}
/*************************************************************/
/* */
/* newPhysicalSweep */
/* */
/*************************************************************/
int newPhysicalSweep(Sweep *phys_sweep, Sweep *sweep)
{
/* Checks if the rsl 'sweep' belongs to a different physical sweep
than 'phys_sweep'.
Returns: 1, if sweep is a new physical sweep
0, if not new physical sweep
-1, if error.
*/
int nrays, iray;
/* If elevations don't match, new sweep. */
if (phys_sweep->h.elev != sweep->h.elev) return(1);
/* Check the first 50 rays of both sweeps.
* If azimuths don't match, new sweep.
*/
nrays = 50;
if (sweep->h.nrays < nrays) nrays = sweep->h.nrays;
if (phys_sweep->h.nrays < nrays) nrays = phys_sweep->h.nrays;
for (iray=0; iray<nrays; iray++)
{
if (sweep->ray[iray] == NULL) continue;
if (phys_sweep->ray[iray] == NULL) continue;
if (phys_sweep->ray[iray]->h.azimuth != sweep->ray[iray]->h.azimuth)
return(1); /* New physical sweep */
}
/* No new physical sweep. */
return(0);
}
/*************************************************************/
/* */
/* tkVosDimensions */
/* */
/*************************************************************/
int tkVosDimensions(VosSize *vs, Radar *radar)
{
/* Logically reconfigures the VOS in the rsl radar structure into
a sequence of physical sweeps as required for the toolkit
L1GV structure.
Records required toolkit gvl1 dimensions in the 'VosSize->tk'
structure. When we later load the toolkit gvl1 structure with
actual data values, vs->tk.ncell[tk_sindex][pindex] will be zero
for those data types not collected by the radar during physical
sweep 'tk_sindex'.
The algorithm used herein to locate physical sweeps is as simple
as possible. It works for all VOSs processed to date. However,
if more complicated radar scanning regimes turn up, this function
will surely require an overhaul and additional complexity.
Algorithm: Within the rsl radar structure, walks horizontally
across the radar volumes at one sweep level, checking for elev
and azim values different from the last physical sweep located.
Then increments the sweep level and repeats at the next rsl sweep
level.
Returns: OK, if success.
<0, if error.
*/
int pindex, sindex, tk_sindex, status;
Sweep *sweep;
/* Note: 'vs->tk' structure contains all zeroes upon entry into this
function. */
sindex = 0; /* rsl sweep index */
tk_sindex = -1; /* toolkit sweep index */
while (sindex < vs->rsl.maxNsweep) /* for each rsl sweep... */
{
for (pindex=0; pindex<vs->tk.nparm; pindex++) /* for each rsl volume */
{
if (sindex >= vs->rsl.nsweep[pindex]) continue;
sweep = vs->rsl.v[pindex]->sweep[sindex];
/* No null sweep ptrs allowed in rsl ptr array. */
if (sweep == NULL) return(QUIT);
/* Check for a new physical sweep. */
if (tk_sindex == -1)
{
tk_sindex++;
vs->tk.nray[tk_sindex] = vs->rsl.nray[pindex][sindex];
vs->rsl.sweep[tk_sindex] = sweep;
}
else if ((status=newPhysicalSweep(vs->rsl.sweep[tk_sindex], sweep)))
{
if (status < 0) return(status);
tk_sindex++;
if (tk_sindex >= MAX_SWEEP)
{
if (radar_verbose_flag)
fprintf(stderr, "tkVosDimensions(): Too many toolkit sweeps.\n");
return(QUIT);
}
vs->tk.nray[tk_sindex] = vs->rsl.nray[pindex][sindex];
vs->rsl.sweep[tk_sindex] = sweep;
} /* end if (newPhysicalSweep */
vs->tk.ncell[tk_sindex][pindex] = vs->rsl.ncell[pindex][sindex];
} /* end for (pindex=0;... */
sindex++;
} /* end while (sindex < vs->rsl.maxNsweep) */
vs->tk.nsweep = tk_sindex + 1;
return(OK);
}
/*************************************************************/
/* */
/* first_ray_in_sweep */
/* */
/*************************************************************/
Ray *first_ray_in_sweep(Sweep *sweep)
{
/*
* Return the first non-NULL ray in the sweep.
* Returns NULL, if error.
*/
int iray;
if (sweep == NULL) return(NULL);
for (iray=0; iray<sweep->h.nrays; iray++)
{
if (sweep->ray[iray] != NULL)
return(sweep->ray[iray]);
}
return(NULL);
}
/*************************************************************/
/* */
/* rslVosDimensions */
/* */
/*************************************************************/
int rslVosDimensions(VosSize *vs, Radar *radar, float maxRange)
{
/* Scopes out all dimensions of the VOS contained in the rsl
radar structure. Records dimensions in the 'VosSize->rsl'
structure.
The ncell values may be less than the actual number of
bins in the radar structure, since the 1B-51/1C-51 standards
call for range truncation. Truncates range as necessary.
For 1B-51: Max range is the lesser of:
1: max_range from radar structure, and
2: 230 km
For 1C-51: Max range is the lesser of:
1: max_range from radar structure, and
2: 200 km
Returns OK, or
<0, if error.
*/
/* Must differentiate between rsl and vosSize array indices,
since the rsl arrays may contain NULL elements. */
int ivolume, isweep, iray; /* Indices for rsl arrays. */
int Vindex, Sindex; /* Indices for non-NULL rsl array elements. */
int nrays_in_sweep;
float maxRangeActual = 0.0;
Ray *ray;
Ray *longest_ray_in_sweep;
/* Initialize the 'VosSize' structure. */
memset(vs, '\0', sizeof(VosSize));
vs->rsl.sweep[0] = NULL;
/*
if (radar_verbose_flag)
fprintf(stderr, "RSL VOS Dimensions...\n");
*/
Vindex = -1;
for (ivolume=0; ivolume<radar->h.nvolumes; ivolume++)
{
if (radar->v[ivolume] == NULL) continue;
Vindex++;
vs->rsl.v[Vindex] = radar->v[ivolume];
Sindex = -1;
for (isweep=0; isweep<radar->v[ivolume]->h.nsweeps; isweep++)
{
if (radar->v[ivolume]->sweep[isweep] == NULL) continue;
Sindex++;
longest_ray_in_sweep = first_ray_in_sweep(radar->v[ivolume]->sweep[isweep]);
if (longest_ray_in_sweep == NULL)
{
fprintf(stderr, "rslVosDimensions(): no rays in sweep:%d ???\n\n",
isweep);
return(QUIT);
}
nrays_in_sweep = 0;
if (Sindex == 0) /* First sweep in volume? */
first_ray_in_volume[ivolume] = longest_ray_in_sweep;
/*
* Go thru the entire sweep to find the number of
* rays, and the longest ray.
*/
for (iray=0; iray<radar->v[ivolume]->sweep[isweep]->h.nrays; iray++)
{
if (radar->v[ivolume]->sweep[isweep]->ray[iray] == NULL) continue;
ray = radar->v[ivolume]->sweep[isweep]->ray[iray];
nrays_in_sweep++;
if (ray->h.nbins > longest_ray_in_sweep->h.nbins)
longest_ray_in_sweep = ray;
} /* end for (iray=0;... */
if (Sindex == 0) /* 1st sweep of this volume? */
{
/* Find max range (km) of data in this sweep. */
maxRangeActual = (longest_ray_in_sweep->h.range_bin1 +
longest_ray_in_sweep->h.nbins *
longest_ray_in_sweep->h.gate_size) / 1000.0; /*km*/
if (maxRangeActual > maxRange+0.5) /* Truncate range at maxRange km */
vs->rsl.ncell[Vindex][Sindex] = (int)
((maxRange*1000.0 - longest_ray_in_sweep->h.range_bin1) /
longest_ray_in_sweep->h.gate_size);
else
vs->rsl.ncell[Vindex][Sindex] = (int)
((maxRangeActual*1000.0 - longest_ray_in_sweep->h.range_bin1) /
longest_ray_in_sweep->h.gate_size);
if (vs->rsl.ncell[Vindex][Sindex] > MAX_CELL)
{
fprintf(stderr, "rslVosDimensions(): ncell[parm%d]=%d > MAX_CELL=%d ",
Vindex, vs->rsl.ncell[Vindex][Sindex], MAX_CELL);
fprintf(stderr, " gate_size:%d\n", longest_ray_in_sweep->h.gate_size);
return(QUIT);
}
} /* end if (Sindex == 0) */
else /* Not the 1st sweep of volume. */
{
if (longest_ray_in_sweep->h.nbins > vs->rsl.ncell[Vindex][0])
vs->rsl.ncell[Vindex][Sindex] = vs->rsl.ncell[Vindex][0];
else
vs->rsl.ncell[Vindex][Sindex] = longest_ray_in_sweep->h.nbins;
}
vs->rsl.nray[Vindex][Sindex] = nrays_in_sweep;
if (vs->rsl.nray[Vindex][Sindex] > vs->rsl.maxNray)
{
vs->rsl.maxNray = vs->rsl.nray[Vindex][Sindex];
if (vs->rsl.maxNray > MAX_RAY)
{
fprintf(stderr, "rslVosDimensions(): v[%d]->sweep[%d].nray=%d > MAX_RAY=%d\n",
ivolume, isweep, vs->rsl.nray[Vindex][Sindex], MAX_RAY);
return(QUIT);
}
}
} /* end for (isweep=0;... */
vs->rsl.nsweep[Vindex] = Sindex + 1;
if (vs->rsl.nsweep[Vindex] > vs->rsl.maxNsweep)
{
vs->rsl.maxNsweep = vs->rsl.nsweep[Vindex];
if (vs->rsl.maxNsweep > MAX_SWEEP)
{
fprintf(stderr, "rslVosDimensions(): v[%d].nsweep=%d > MAX_SWEEP=%d\n",
ivolume, vs->rsl.nsweep[Vindex], MAX_SWEEP);
return(QUIT);
}
}
/*
if (radar_verbose_flag)
{
fprintf(stderr,
" vIndex:%2d nsweeps:%d cellSize(m):%4d ncells:%d maxRng(km):%.1f\n",
ivolume, vs->rsl.nsweep[Vindex], longest_ray_in_sweep->h.gate_size,
longest_ray_in_sweep->h.nbins, maxRangeActual);
}
*/
} /* for (ivolume=0;... */
vs->tk.nparm = Vindex + 1;
if ((vs->tk.nparm == 0) || (vs->tk.nparm > MAX_PARM))
{
fprintf(stderr, "rslVosDimensions(): Invalid nparm=%d\n", vs->tk.nparm);
return(QUIT);
}
return(OK);
}
/*************************************************************/
/* */
/* L1GVtemplateInit */
/* */
/*************************************************************/
int L1GVtemplateInit(VosSize *vs, Radar *radar, char *hdfFileName,
float maxRange)
{
/* Create a toolkit 'Level_1B_1C_GV' template_node for the VOS
contained in the radar structure. To do this:
1: Based on the data contained in the rsl structure, initialize
the following toolkit arrays:
'TKnparm' : no. of volumes in radar structure (DZ, VR, ZD, etc).
'TKnsweep': max no. of sweeps/volume, over all volumes.
'TKnray' : max no. of rays per sweep, over all volumes.
'TKncell': max no. of cells (bins) per ray, over all volumes, all sweeps.
These values are parameters for the 'TKsetL1GVtemplate()'
toolkit function call.
2: Call TKsetL1GVtemplate().
Returns:
OK, if success.
<0, if failure.
*/
int pindex, status;
int32 TKnparm[MAX_VOS], TKnsweep[MAX_VOS], TKnray[MAX_VOS];
int32 TKncell[MAX_VOS][MAX_PARM];
/* Scope out all dimensions of the VOS contained in the rsl structure. */
status = rslVosDimensions(vs, radar, maxRange);
if (status < 0) return(status);
/* Set the toolkit VOS dimensions. These are different from the rsl
dimensions. The toolkit L1GV structure is organized as a
sequence of physical sweeps, while RSL is organized as a
sequence of logical "volumes".
*/
status = tkVosDimensions(vs, radar);
if (status < 0) return(status);
/* Fill required toolkit array values for TKsetL1GVtemplate() call. */
TKnparm[0] = vs->tk.nparm;
TKnsweep[0] = vs->tk.nsweep;
TKnray[0] = vs->rsl.maxNray;
/*
if (radar_verbose_flag)
fprintf(stderr, "Toolkit VOS Dimensions...\n");
*/
for (pindex=0; pindex<vs->tk.nparm; pindex++)
{
TKncell[0][pindex] = vs->rsl.ncell[pindex][0];
/*
if (radar_verbose_flag)
fprintf(stderr, " pIndex:%d nsweep:%d nray:%d ncell:%d\n", pindex,
(int)TKnsweep[0], (int)TKnray[0], (int)TKncell[0][pindex]);
*/
}
/* Create a toolkit template_node for this VOS. */
status = TKsetL1GVtemplate(1, TKnparm, TKncell, TKnray, TKnsweep, hdfFileName);
if (status != TK_SUCCESS)
{
fprintf(stderr, "L1GVtemplateInit(): *****TKsetL1GVtemplate() error\n");
return(ABORT);
}
return(OK); /* Successful template_node creation. */
}
/*************************************************************/
/* */
/* hdfFileOpen */
/* */
/*************************************************************/
static int hdfFileOpen(IO_HANDLE *granuleHandle, VosSize *vs,
char *hdfFileName, Radar *radar)
{
/* Create a toolkit template node for the new VOS, and then open
a HDF file in which to write the VOS.
Returns:
OK, if success.
<0, if failure.
*/
char fileAccessMode;
int productID, status;
float maxRange;
struct stat buf;
/* Based on the product desired, set the maximum range of radar data
values for the HDF file.
For 1B-51: Max range = 230.0 km.
For 1C-51: Max range = 200.0 km
*/
productID = (int)granuleHandle->productID;
if (productID == TK_L1B_GV)
maxRange = MAX_RANGE_1B51;
else if (productID == TK_L1C_GV)
maxRange = MAX_RANGE_1C51;
else
{
fprintf(stderr, "hdfFileOpen(): Invalid product type\n");
return(ABORT);
}
/* Create a L1BGV template node for the new VOS. */
/*
if (radar_verbose_flag)
fprintf(stderr, "\n****** Creating toolkit template_node for VOS ...\n");
*/
status = L1GVtemplateInit(vs, radar, hdfFileName, maxRange);
if (status < 0) return(status);
/* Determine if the HDF file to which this VOS belongs already exists.*/
if (stat(hdfFileName, &buf) == 0)
{
/* The HDF file already exists. We will append this VOS to it. */
fileAccessMode = TK_APPEND;
if (radar_verbose_flag)
fprintf(stderr, "\n****** Opening HDF file: %s to append VOS ...\n",
hdfFileName);
}
else /* The HDF file does not exist. We must create it. */
{
fileAccessMode = TK_NEW_FILE;
if (radar_verbose_flag)
fprintf(stderr, "\n****** Opening new HDF file: %s to write VOS ...\n",
hdfFileName);
}
/* Finally, open the HDF file. */
status = TKopen(hdfFileName, productID, fileAccessMode, granuleHandle);
if (status != TK_SUCCESS)
{
if (radar_verbose_flag)
fprintf(stderr, "level_1(): ***** TKopen() error\n");
return(ABORT);
}
/* Find the slot number in the HDF granule for this VOS. */
vs->vos_num = TKgetNvos(granuleHandle) - 1;
/* Write metadata fields into HDF file. */
metaDataWrite(granuleHandle, radar, hdfFileName, fileAccessMode);
return(OK);
}
/*************************************************************/
/* */
/* radarPrep1B51 */
/* */
/*************************************************************/
int radarPrep1B51(Radar *radar)
{
/*
Prepare the RSL radar structure for 1B-51 processing:
Null the pointers to RSL volumes not required for 1B-51.
Returns OK.
*/
int j;
/* For 1B-51, we need only the DZ, VR, and ZD data volumes. NULL the
pointers to all other volumes. */
for (j=0; j<radar->h.nvolumes; j++)
{
if (radar->v[j] == NULL)
continue;
if ((j == DZ_INDEX) || (j == VR_INDEX) || (j == ZD_INDEX))
continue;
radar->v[j] = NULL;
} /* end for (j=0; ... */
return(OK);
}
/*************************************************************/
/* */
/* radarPrep1C51 */
/* */
/*************************************************************/
int radarPrep1C51(Radar *radar)
{
/*
Prepare the RSL radar structure for 1C-51 processing.
1. Check that the necessary RSL volumes exist.
2. Create mask volumes.
3. Null the pointers to RSL volumes not required for 1C-51.
Returns:
OK, if success.
<0, if failure.
*/
int j;
if (radar->v[DZ_INDEX] != NULL) /* Is there a DZ volume? */
{
if (radar->v[CZ_INDEX] == NULL) /* DZ exists, hence CZ should exist. */
{
fprintf(stderr, "RSL_radar_to_hdf(): CZ volume expected but not found.\n");
return(QUIT);
}
else /* Both DZ and CZ volumes exist. */
{
/* Construct mask volume MZ */
radar->v[MZ_INDEX] = maskBuild(radar->v[CZ_INDEX], radar->v[DZ_INDEX]);
/* We're now finished with the RSL CZ data, so let the CZ volume point
to the uncorrected volume DZ. We will later use it,
in combination with the mask volume MZ, to create the HDF CZ volume.
(The HDF CZ values differ from the RSL CZ values.)
*/
radar->v[CZ_INDEX] = radar->v[DZ_INDEX];
}
} /* end if (radar->v[DZ_INDEX] != NULL) */
/* For 1C-51, we need only the following volumes:
CZ: QC'ed reflectivity, which we will later obtain from DZ and MZ
MZ: mask to obtain DZ from CZ
NULL the pointers to all other volumes.
*/
for (j=0; j<radar->h.nvolumes; j++)
{
if (radar->v[j] == NULL)
continue;
if ((j == CZ_INDEX) || (j == MZ_INDEX))
continue;
radar->v[j] = NULL;
} /* end for (j=0; ... */
return(OK);
}
/*************************************************************/
/* */
/* nullGranuleCreate */
/* */
/*************************************************************/
int nullGranuleCreate(char *hdfFileName, IO_HANDLE *granuleHandle,
Radar *radar)
{
/*
Create an HDF file containing an empty granule.
*/
int status;
struct stat buf;
/* Following arrays required by toolkit function 'TKsetL1GVtemplate()'*/
int32 TKnparm[MAX_VOS], TKnsweep[MAX_VOS], TKnray[MAX_VOS];
int32 TKncell[MAX_VOS][MAX_PARM];
/* Check if this HDF file already exists. If it exists, abort. */
if (stat(hdfFileName, &buf) == 0)
{
if (radar_verbose_flag)
fprintf(stderr, "\nnullGranuleCreate(): File %s already exists.\n",
hdfFileName);
return(ABORT);
}
/* Fill toolkit array values for subsequent TKsetL1GVtemplate() call. */
TKnparm[0] = TKnsweep[0] = TKnray[0] = TKncell[0][0] = 0;
/* Create a L1 GV template_node. */
status = TKsetL1GVtemplate(0, TKnparm, TKncell, TKnray, TKnsweep, hdfFileName);
if (status != TK_SUCCESS)
{
fprintf(stderr, "nullGranuleCreate(): ***** TKsetL1GVtemplate() error\n");
return(ABORT);
}
/* Open the HDF file. */
if (radar_verbose_flag)
fprintf(stderr, "\n\n****** Opening new HDF file: %s for empty granule...\n",
hdfFileName);
status = TKopen(hdfFileName, TK_L1C_GV, TK_NEW_FILE, granuleHandle);
if (status != TK_SUCCESS)
{
if (radar_verbose_flag)
fprintf(stderr, "nullGranuleCreate(): ***** TKopen() error\n");
return(ABORT);
}
/* Write metadata fields into HDF file. */
metaDataWrite(granuleHandle, radar, hdfFileName, TK_NEW_FILE);
/* Close the HDF file */
if (radar_verbose_flag)
fprintf(stderr, "\n****** Closing HDF file: %s ...\n\n", hdfFileName);
status = TKclose(granuleHandle);
if (status != TK_SUCCESS)
{
if (radar_verbose_flag)
fprintf(stderr, "nullGranuleCreate(): ***** TKclose() error\n");
return(ABORT);
}
return(OK);