3 This is the TRMM Office Radar Software Library.
4 Copyright (C) 1996, 1997
6 Space Applications Corporation
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with this library; if not, write to the Free
21 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * Volume functions coded in this file:
26 * Default DZ_F and DZ_INVF. For VR, SW, CZ, ZT, DR and LR too.
27 * Volume *RSL_new_volume(int max_sweeps);
28 * Sweep *RSL_new_sweep(int max_rays);
29 * Ray *RSL_new_ray(int max_bins);
30 * Ray *RSL_clear_ray(Ray *r);
31 * Sweep *RSL_clear_sweep(Sweep *s);
32 * Volume *RSL_clear_volume(Volume *v);
33 * void RSL_free_ray(Ray *r);
34 * void RSL_free_sweep(Sweep *s);
35 * void RSL_free_volume(Volume *v);
36 * Ray *RSL_copy_ray(Ray *r);
37 * Sweep *RSL_copy_sweep(Sweep *s);
38 * Volume *RSL_copy_volume(Volume *v);
39 * Ray *RSL_get_ray_from_sweep(Sweep *s, float azim);
40 * float RSL_get_value_from_sweep(Sweep *s, float azim, float r);
41 * float RSL_get_value_from_ray(Ray *ray, float r);
42 * float RSL_get_value_at_h(Volume *v, float azim, float grnd_r, float h);
43 * Sweep *RSL_get_sweep(Volume *v, float elev);
44 * Ray *RSL_get_ray(Volume *v, float elev, float azimuth);
45 * float RSL_get_value(Volume *v, float elev, float azimuth, float range);
46 * Ray *RSL_get_ray_above(Volume *v, Ray *current_ray);
47 * Ray *RSL_get_ray_below(Volume *v, Ray *current_ray);
48 * Ray *RSL_get_matching_ray(Volume *v, Ray *ray);
49 * int RSL_get_sweep_index_from_volume
51 * See image_gen.c for the Volume image generation functions.
53 * See doc/rsl_index.html for HTML formatted documentation.
55 * All routines herein coded, unless otherwise stated, by:
57 * Space Applications Corporation
60 * Dennis Flanigan, Jr.
61 * flanigan@lance.colostate.edu
68 int strcasecmp(const char *s1, const char *s2);
73 #define bin_azimuth(x, dx) (float)((float)x/dx)
74 #define bin_elevation(x, dx) (float)((float)x/dx)
75 #define bin_range(x, dx) (float)((float)x/dx)
77 extern int radar_verbose_flag;
79 /* Internal storage conversion functions. These may be any conversion and
80 * may be dynamically defined; based on the input data conversion. If you
81 * change any of the reserved values, ie. values that cannot be converted
82 * from/to internal storage (BADVAL, APFLAG, etc), be sure to change all
83 * functions, both XX_F and XX_INVF to handle it. It is best to have the
84 * reserved values stored at 0, 1, 2, on up. That way you merely need to
85 * provide an offset to the actual conversion function. See 'rsl.h'
86 * for the definition of the reserved values. Currently: BADVAL, RFVAL,
89 * The conversion functions may NOT be macros.
92 #ifdef USE_TWO_BYTE_PRECISION
93 #define F_FACTOR 100.0
94 #define F_DR_FACTOR 1000.0
95 #define F_DZ_RANGE_OFFSET 50
98 #define F_DR_FACTOR 10.0
99 #define F_DZ_RANGE_OFFSET 32
102 /* #define F_VR_OFFSET 63.5 */
103 #define F_VR_OFFSET 127.0
104 #define F_DR_OFFSET 12.0
106 /* IMPORTANT: This is the offset from reserved values. This
107 * number must be exactly (or >=) the number of
108 * reserved values in XX_F and XX_INVF.
110 * You must change nsig_to_radar.c where F_OFFSET is used for optimization.
115 float DZ_F(Range x) {
116 if (x >= F_OFFSET) /* This test works when Range is unsigned. */
117 return (((float)x-F_OFFSET)/F_FACTOR - F_DZ_RANGE_OFFSET); /* Default wsr88d. */
118 if (x == 0) return BADVAL;
119 if (x == 1) return RFVAL;
120 if (x == 2) return APFLAG;
121 if (x == 3) return NOECHO;
122 return BADVAL; /* Can't get here, but quiets the compiler. */
125 float VR_F(Range x) {
127 if (x >= F_OFFSET) { /* This test works when Range is unsigned. */
128 val = (((float)x-F_OFFSET)/F_FACTOR - F_VR_OFFSET); /* Default wsr88d coding. */
129 /* fprintf(stderr, "x=%d, val=%f\n", x, val); */
132 if (x == 0) return BADVAL;
133 if (x == 1) return RFVAL;
134 if (x == 2) return APFLAG;
135 if (x == 3) return NOECHO;
136 return BADVAL; /* Can't get here, but quiets the compiler. */
139 float DR_F(Range x) { /* Differential reflectivity */
141 if (x >= F_OFFSET) { /* This test works when Range is unsigned. */
142 val = (((float)x-F_OFFSET)/F_DR_FACTOR - F_DR_OFFSET);
145 if (x == 0) return BADVAL;
146 if (x == 1) return RFVAL;
147 if (x == 2) return APFLAG;
148 if (x == 3) return NOECHO;
149 return BADVAL; /* Can't get here, but quiets the compiler. */
152 float LR_F(Range x) {/* From MCTEX */
153 if (x >= F_OFFSET) /* This test works when Range is unsigned. */
154 return (float) (x - 250.)/6.;
155 if (x == 0) return BADVAL;
156 if (x == 1) return RFVAL;
157 if (x == 2) return APFLAG;
158 if (x == 3) return NOECHO;
162 float HC_F(Range x) { /* HydroClass (Sigmet) */
163 if (x == 0) return BADVAL;
167 /****************************
168 Sigmet RhoHV : one_byte values
169 > RohHV = sqrt((N-1)/253)
176 *******************************/
177 float RH_F(Range x) {
178 if (x == 0) return BADVAL;
179 /* return (float)(sqrt((double)((x-1.0)/253.0))); */
180 return (float)(x-1) / 65533.;
183 /*****************************
184 Sigmet PhiDP : one_byte values
185 > PhiDP (mod 180) = 180 * ((N-1)/254)
186 > The range is from 0 to 180 degrees in steps of 0.71 as follows.
192 ******************************/
193 float PH_F(Range x) {
194 if (x == 0) return BADVAL;
195 /*return (float)(180.0*((x-1.0)/254.0));*/
196 return (360.*(x-1.))/65534.;
199 /* TODO: Should this be 5. cm. instead of 0.5? Or maybe 10. cm.? */
200 float rsl_kdp_wavelen = 0.5; /* Default radar wavelen = .5 cm. See
201 * nsig_to_radar.c for the code that sets this.
203 /* KD_F for 1 or 2 byte. */
206 /****** Commented-out code for 1-byte Sigmet native data format.
209 if (rsl_kdp_wavelen == 0.0) return BADVAL;
212 -0.25 * pow((double)600.0,(double)((127-x)/126.0))
216 0.25 * pow((double)600.0,(double)((x-129)/126.0))
221 if (x == 1) return RFVAL;
222 if (x == 2) return APFLAG;
223 if (x == 3) return NOECHO;
227 if (x == 0) return BADVAL;
228 return (x-32768.)/100.;
231 float NP_F(Range x) { /* Normalized Coherent Power (DORADE) */
232 if (x == 0) return BADVAL;
233 return (float)x / 100.;
236 /* Signal Quality Index */
237 float SQ_F(Range x) {
238 if (x == 0) return BADVAL;
239 return (float)(x-1) / 65533.;
244 if (x >= F_OFFSET) return (float)(x);
245 if (x == 0) return BADVAL;
246 if (x == 1) return RFVAL;
247 if (x == 2) return APFLAG;
248 if (x == 3) return NOECHO;
252 float SW_F(Range x) { return VR_F(x); }
253 float CZ_F(Range x) { return DZ_F(x); }
254 float ZT_F(Range x) { return DZ_F(x); }
255 float ZD_F(Range x) { return DR_F(x); } /* Differential reflectivity */
256 float CD_F(Range x) { return DR_F(x); } /* Differential reflectivity */
257 float XZ_F(Range x) { return DZ_F(x); }
258 float MZ_F(Range x) { return (float)x; } /* DZ Mask */
259 float MD_F(Range x) { return MZ_F(x); } /* ZD Mask */
260 float ZE_F(Range x) { return DZ_F(x); }
261 float VE_F(Range x) { return VR_F(x); }
262 float DM_F(Range x) { return DZ_F(x); }
263 float DX_F(Range x) { return DZ_F(x); }
264 float CH_F(Range x) { return DZ_F(x); }
265 float AH_F(Range x) { return DZ_F(x); }
266 float CV_F(Range x) { return DZ_F(x); }
267 float AV_F(Range x) { return DZ_F(x); }
268 float VS_F(Range x) { return VR_F(x); }
269 float VL_F(Range x) { return VR_F(x); }
270 float VG_F(Range x) { return VR_F(x); }
271 float VT_F(Range x) { return VR_F(x); }
272 float VC_F(Range x) { return VR_F(x); }
277 /* Unfortunately, floats are stored differently than ints/shorts. So,
278 * we cannot simply set up a switch statement and we must test for
279 * all the special cases first. We must test for exactness.
281 Range DZ_INVF(float x)
283 if (x == BADVAL) return (Range)0;
284 if (x == RFVAL) return (Range)1;
285 if (x == APFLAG) return (Range)2;
286 if (x == NOECHO) return (Range)3;
287 if (x < -F_DZ_RANGE_OFFSET) return (Range)0;
288 return (Range)(F_FACTOR*(x+F_DZ_RANGE_OFFSET)+.5 + F_OFFSET); /* Default wsr88d. */
291 Range VR_INVF(float x)
293 if (x == BADVAL) return (Range)0;
294 if (x == RFVAL) return (Range)1;
295 if (x == APFLAG) return (Range)2;
296 if (x == NOECHO) return (Range)3;
297 if (x < -F_VR_OFFSET) return (Range)0;
298 return (Range)(F_FACTOR*(x+F_VR_OFFSET)+.5 + F_OFFSET); /* Default wsr88d coding. */
301 Range DR_INVF(float x) /* Differential reflectivity */
303 if (x == BADVAL) return (Range)0;
304 if (x == RFVAL) return (Range)1;
305 if (x == APFLAG) return (Range)2;
306 if (x == NOECHO) return (Range)3;
307 if (x < -F_DR_OFFSET) return (Range)0;
308 return (Range)(F_DR_FACTOR*(x + F_DR_OFFSET) + F_OFFSET + 0.5);
311 Range HC_INVF(float x) /* HydroClass (Sigmet) */
313 if (x == BADVAL) return (Range)0;
314 return (Range)(x + 0.5); /* Round */
317 Range LR_INVF(float x) /* MCTEX */
319 if (x == BADVAL) return (Range)0;
320 if (x == RFVAL) return (Range)1;
321 if (x == APFLAG) return (Range)2;
322 if (x == NOECHO) return (Range)3;
323 return (Range)((6.*x + 250) + 0.5); /* Round */
326 /**************************
327 Sigmet RhoHV : one_byte values
328 > RohHV = sqrt((N-1)/253)
335 ****************************/
336 /* RH_INVF for 1 or 2 byte data. */
337 Range RH_INVF(float x) {
338 if (x == BADVAL) return (Range)0;
339 /* return (Range)(x * x * 253.0 + 1.0 + 0.5); */
340 return (Range)(x * 65533. + 1. +.5);
343 /******************************
344 Sigmet PhiDP : one_byte values
345 > PhiDP (mod 180) = 180 * ((N-1)/254)
346 > The range is from 0 to 180 degrees in steps of 0.71 as follows.
352 *******************************/
353 Range PH_INVF(float x) {
354 if (x == BADVAL) return (Range)0;
355 /* return (Range)((x / 180.0) * 254.0 + 1.0 + 0.5); */
356 return (Range)(x*65534./360. + 1.0 + 0.5);
360 /* KD_INVF for 1 or 2 byte data. */
361 Range KD_INVF(float x) {
362 if (x == BADVAL) return (Range)0;
363 /****** Commented-out code for 1-byte Sigmet native data format.
364 if (x == RFVAL) return (Range)1;
365 if (x == APFLAG) return (Range)2;
366 if (x == NOECHO) return (Range)3;
367 if (rsl_kdp_wavelen == 0.0) return (Range)0;
370 126 * (log((double)-x) - log((double)(0.25/rsl_kdp_wavelen))) /
375 126 * (log((double)x) - log((double)0.25/rsl_kdp_wavelen)) /
383 return (Range)(x * 100. + 32768. + 0.5);
387 /* Signal Quality Index */
388 Range SQ_INVF(float x)
390 if (x == BADVAL) return (Range)0;
391 return (Range)(x * 65533. + 1. +.5);
395 Range NP_INVF(float x) /* Normalized Coherent Power (DORADE) */
397 if (x == BADVAL) return (0);
398 return (Range)(x * 100.);
401 Range TI_INVF(float x) /* MCTEX */
403 if (x == BADVAL) return (Range)0;
404 if (x == RFVAL) return (Range)1;
405 if (x == APFLAG) return (Range)2;
406 if (x == NOECHO) return (Range)3;
411 Range SW_INVF(float x) { return VR_INVF(x); }
412 Range CZ_INVF(float x) { return DZ_INVF(x); }
413 Range ZT_INVF(float x) { return DZ_INVF(x); }
414 Range ZD_INVF(float x) { return DR_INVF(x); } /* Differential reflectivity */
415 Range CD_INVF(float x) { return DR_INVF(x); } /* Differential reflectivity */
416 Range XZ_INVF(float x) { return DZ_INVF(x); }
417 Range MZ_INVF(float x) { return (Range)x; } /* DZ Mask */
418 Range MD_INVF(float x) { return MZ_INVF(x); } /* ZD Mask */
419 Range ZE_INVF(float x) { return DZ_INVF(x); }
420 Range VE_INVF(float x) { return VR_INVF(x); }
421 Range DM_INVF(float x) { return DZ_INVF(x); }
422 Range DX_INVF(float x) { return DZ_INVF(x); }
423 Range CH_INVF(float x) { return DZ_INVF(x); }
424 Range AH_INVF(float x) { return DZ_INVF(x); }
425 Range CV_INVF(float x) { return DZ_INVF(x); }
426 Range AV_INVF(float x) { return DZ_INVF(x); }
427 Range VS_INVF(float x) { return VR_INVF(x); }
428 Range VL_INVF(float x) { return VR_INVF(x); }
429 Range VG_INVF(float x) { return VR_INVF(x); }
430 Range VT_INVF(float x) { return VR_INVF(x); }
431 Range VC_INVF(float x) { return VR_INVF(x); }
435 /**********************************************************************/
436 /* M E M O R Y M A N A G E M E N T R O U T I N E S */
437 /**********************************************************************/
438 /**********************************************************************/
444 /**********************************************************************/
445 Volume *RSL_new_volume(int max_sweeps)
448 * A volume consists of a header section and an array of sweeps.
451 v = (Volume *)calloc(1, sizeof(Volume));
452 if (v == NULL) perror("RSL_new_volume");
453 v->sweep = (Sweep **) calloc(max_sweeps, sizeof(Sweep*));
454 if (v->sweep == NULL) perror("RSL_new_volume, Sweep*");
455 v->h.nsweeps = max_sweeps; /* A default setting. */
460 * The 'Sweep_list' structure is internal to RSL. It maintains a list
461 * of sweeps allocated and it contains pointers to a hash table of Rays
462 * separately for each sweep. There is no reason to access this internal
463 * structure except when optimizing new RSL routines that access Rays.
464 * Otherwise, the RSL interfaces should suffice.
466 * The hash table is a means of finding rays, by azimuth, quickly.
467 * To find a ray is simple: use the hash function to get close
468 * to the ray, if not right on it the first time. Collisions of rays in
469 * the hash table are handled by a link list of rays from a hash entry.
470 * Typically, the first ray of the sweep is not the ray with the smallest
471 * azimuth angle. We are confident that the order of Rays in the Sweep
472 * is by azimuth angle, but that cannot be guarenteed. Therefore, this
473 * hash scheme is required.
475 * The 'Sweep_list' contains the address of the memory allocated to
476 * sweep. The list is sorted by addresses. There is no
477 * memory limit to the number of sweeps. If the number of sweeps exceeds
478 * the current allocation for the Sweep_list, then a new Sweep_list is
479 * allocated, which is bigger, and the old list copied to it.
481 * Sweep_list is at least as long as the number of sweeps allocated.
490 * By design of RSL, this should be "#define STATIC static"
492 * It is OK to "#define STATIC static", but, if you do, then
493 * the examples (run by run_tests in examples/) will fail for
494 * those programs that test these data structures. I normally,
495 * don't set this #define, for that reason.
499 STATIC int RSL_max_sweeps = 0; /* Initial allocation for sweep_list.
500 * RSL_new_sweep will allocate the space first
503 STATIC int RSL_nsweep_addr = 0; /* A count of sweeps in the table. */
504 STATIC Sweep_list *RSL_sweep_list = NULL;
505 STATIC int RSL_nextents = 0;
507 void FREE_HASH_NODE(Azimuth_hash *node)
509 if (node == NULL) return;
510 FREE_HASH_NODE(node->next); /* Tail recursive link list removal. */
514 void FREE_HASH_TABLE(Hash_table *table)
517 if (table == NULL) return;
518 for (i=0; i<table->nindexes; i++)
519 FREE_HASH_NODE(table->indexes[i]); /* A possible linked list of Rays. */
520 free(table->indexes);
524 void REMOVE_SWEEP(Sweep *s)
528 /* Find where it goes, split the list and slide the tail down one. */
529 for (i=0; i<RSL_nsweep_addr; i++)
530 if (s == RSL_sweep_list[i].s_addr) break;
532 if (i == RSL_nsweep_addr) return; /* Not found. */
533 /* This sweep is at 'i'. */
534 /* Deallocate the memory for the hash table. */
535 FREE_HASH_TABLE(RSL_sweep_list[i].hash);
538 for (j=i; j<RSL_nsweep_addr; j++)
539 RSL_sweep_list[j] = RSL_sweep_list[j+1];
541 RSL_sweep_list[RSL_nsweep_addr].s_addr = NULL;
542 RSL_sweep_list[RSL_nsweep_addr].hash = NULL;
546 int INSERT_SWEEP(Sweep *s)
548 Sweep_list *new_list;
551 if (RSL_nsweep_addr >= RSL_max_sweeps) { /* Current list is too small. */
553 new_list = (Sweep_list *) calloc(100*RSL_nextents, sizeof(Sweep_list));
554 if (new_list == NULL) {
555 perror("INSERT_SWEEP");
558 /* Copy the old list to the new one. */
559 for (i=0; i<RSL_max_sweeps; i++) new_list[i] = RSL_sweep_list[i];
560 RSL_max_sweeps = 100*RSL_nextents;
561 free(RSL_sweep_list);
562 RSL_sweep_list = new_list;
564 /* Find where it goes, split the list and slide the tail down one. */
565 for (i=0; i<RSL_nsweep_addr; i++)
566 if (s < RSL_sweep_list[i].s_addr) break;
568 /* This sweep goes at 'i'. But first we must split the list. */
569 for (j=RSL_nsweep_addr; j>i; j--)
570 RSL_sweep_list[j] = RSL_sweep_list[j-1];
572 RSL_sweep_list[i].s_addr = s;
573 RSL_sweep_list[i].hash = NULL;
578 int SWEEP_INDEX(Sweep *s)
580 /* Locate the sweep in the RSL_sweep_list. Return the index. */
581 /* Simple linear search; but this will be a binary search. */
583 for (i=0; i<RSL_nsweep_addr; i++)
584 if (s == RSL_sweep_list[i].s_addr) return i;
588 Sweep *RSL_new_sweep(int max_rays)
591 * A sweep consists of a header section and an array of rays.
594 s = (Sweep *)calloc(1, sizeof(Sweep));
595 if (s == NULL) perror("RSL_new_sweep");
597 s->ray = (Ray **) calloc(max_rays, sizeof(Ray*));
598 if (s->ray == NULL) perror("RSL_new_sweep, Ray*");
599 s->h.nrays = max_rays; /* A default setting. */
603 Ray *RSL_new_ray(int max_bins)
606 * A ray consists of a header section and an array of Range types (floats).
609 r = (Ray *)calloc(1, sizeof(Ray));
610 if (r == NULL) perror("RSL_new_ray");
611 r->range = (Range *) calloc(max_bins, sizeof(Range));
612 if (r->range == NULL) perror("RSL_new_ray, Range");
613 r->h.nbins = max_bins; /* A default setting. */
614 /* fprintf(stderr,"range[0] = %x, range[%d] = %x\n", &r->range[0], max_bins-1, &r->range[max_bins-1]);*/
618 /**********************************************************************/
624 /**********************************************************************/
625 Ray *RSL_clear_ray(Ray *r)
627 if (r == NULL) return r;
628 memset(r->range, 0, sizeof(Range)*r->h.nbins);
631 Sweep *RSL_clear_sweep(Sweep *s)
634 if (s == NULL) return s;
635 for (i=0; i<s->h.nrays; i++) {
636 RSL_clear_ray(s->ray[i]);
640 Volume *RSL_clear_volume(Volume *v)
643 if (v == NULL) return v;
644 for (i=0; i<v->h.nsweeps; i++) {
645 RSL_clear_sweep(v->sweep[i]);
649 /**********************************************************************/
655 /**********************************************************************/
656 void RSL_free_ray(Ray *r)
658 if (r == NULL) return;
659 if (r->range) free(r->range);
662 void RSL_free_sweep(Sweep *s)
665 if (s == NULL) return;
666 for (i=0; i<s->h.nrays; i++) {
667 RSL_free_ray(s->ray[i]);
669 if (s->ray) free(s->ray);
670 REMOVE_SWEEP(s); /* Remove from internal Sweep list. */
673 void RSL_free_volume(Volume *v)
676 if (v == NULL) return;
678 for (i=0; i<v->h.nsweeps; i++)
680 RSL_free_sweep(v->sweep[i]);
682 if (v->sweep) free(v->sweep);
686 /**********************************************************************/
692 /**********************************************************************/
693 Ray *RSL_copy_ray(Ray *r)
697 if (r == NULL) return NULL;
698 new_ray = RSL_new_ray(r->h.nbins);
700 memcpy(new_ray->range, r->range, r->h.nbins*sizeof(Range));
703 Sweep *RSL_copy_sweep(Sweep *s)
708 if (s == NULL) return NULL;
709 n_sweep = RSL_new_sweep(s->h.nrays);
710 if (n_sweep == NULL) return NULL;
713 for (i=0; i<s->h.nrays; i++) {
714 n_sweep->ray[i] = RSL_copy_ray(s->ray[i]);
721 Volume *RSL_copy_volume(Volume *v)
726 if (v == NULL) return NULL;
727 new_vol = RSL_new_volume(v->h.nsweeps);
730 for (i=0; i<v->h.nsweeps; i++) {
731 new_vol->sweep[i] = RSL_copy_sweep(v->sweep[i]);
737 /**********************************************************************/
738 /**********************************************************************/
739 /* G E N E R A L F U N C T I O N S */
740 /**********************************************************************/
741 /**********************************************************************/
743 double angle_diff(float x, float y)
746 d = fabs((double)(x - y));
747 if (d > 180) d = 360 - d;
751 /**********************************************************************/
753 /* RSL_get_next_cwise_ray */
754 /* Dennis Flanigan */
755 /* Mods by John Merritt 10/20/95 */
756 /**********************************************************************/
757 Ray *RSL_get_next_cwise_ray(Sweep *s, Ray *ray)
759 /* The fastest way to do this is to gain access to the hash table
760 * which maintains a linked list of sorted rays.
762 Hash_table *hash_table;
763 Azimuth_hash *closest;
767 if (s == NULL) return NULL;
768 if (ray == NULL) return NULL;
769 /* Find a non-NULL index close to hindex that we want. */
770 hash_table = hash_table_for_sweep(s);
771 if (hash_table == NULL) return NULL; /* Nada. */
772 ray_angle = ray->h.azimuth;
773 hindex = hash_bin(hash_table,ray_angle);
775 /* Find hash entry with closest Ray */
776 closest = the_closest_hash(hash_table->indexes[hindex],ray_angle);
778 return closest->ray_high->ray;
781 /**********************************************************************/
783 /* RSL_get_next_ccwise_ray */
785 /**********************************************************************/
786 Ray *RSL_get_next_ccwise_ray(Sweep *s, Ray *ray)
788 /* The fastest way to do this is to gain access to the hash table
789 * which maintains a linked list of sorted rays.
791 Hash_table *hash_table;
792 Azimuth_hash *closest;
796 if (s == NULL) return NULL;
797 if (ray == NULL) return NULL;
798 /* Find a non-NULL index close to hindex that we want. */
799 hash_table = hash_table_for_sweep(s);
800 if (hash_table == NULL) return NULL; /* Nada. */
801 ray_angle = ray->h.azimuth;
802 hindex = hash_bin(hash_table,ray_angle);
804 /* Find hash entry with closest Ray */
805 closest = the_closest_hash(hash_table->indexes[hindex],ray_angle);
807 return closest->ray_low->ray;
811 /******************************************
815 * Dennis Flanigan,Jr. 5/17/95 *
816 ******************************************/
817 double cwise_angle_diff(float x,float y)
819 /* Returns the clockwise angle difference of x to y.
820 * If x = 345 and y = 355 return 10.
821 * If x = 345 and y = 335 return 350
830 /******************************************
832 * ccwise_angle_diff *
834 * Dennis Flanigan,Jr. 5/17/95 *
835 ******************************************/
836 double ccwise_angle_diff(float x,float y)
838 /* Returns the counterclockwise angle differnce of x to y.
839 * If x = 345 and y = 355 return 350.
840 * If x = 345 and y = 335 return 10
849 /*****************************************
853 * Dennis Flanigan,Jr. 4/29/95 *
854 *****************************************/
855 Azimuth_hash *the_closest_hash(Azimuth_hash *hash, float ray_angle)
857 /* Return the hash pointer with the minimum ray angle difference. */
859 double clow,chigh,cclow;
860 Azimuth_hash *high,*low;
862 if (hash == NULL) return NULL;
864 /* Set low pointer to hash index with ray angle just below
865 * requested angle and high pointer to just above requested
869 /* set low and high pointers to initial search locations*/
871 high = hash->ray_high;
873 /* Search until clockwise angle to high is less then clockwise
877 clow = cwise_angle_diff(ray_angle,low->ray->h.azimuth);
878 chigh = cwise_angle_diff(ray_angle,high->ray->h.azimuth);
879 cclow = ccwise_angle_diff(ray_angle,low->ray->h.azimuth);
881 while((chigh > clow) && (clow != 0))
886 high = low->ray_high; /* Not the same low as line before ! */
891 high = low->ray_high; /* Not the same low as line before ! */
894 clow = cwise_angle_diff(ray_angle,low->ray->h.azimuth);
895 chigh = cwise_angle_diff(ray_angle,high->ray->h.azimuth);
896 cclow = ccwise_angle_diff(ray_angle,low->ray->h.azimuth);
910 /*******************************************************************/
912 /* get_closest_sweep_index */
914 /* Dennis Flanigan, Jr. 5/15/95 */
915 /*******************************************************************/
916 int get_closest_sweep_index(Volume *v,float sweep_angle)
920 float delta_angle = 91;
923 if(v == NULL) return -1;
927 for (i=0; i<v->h.nsweeps; i++)
930 if (s == NULL) continue;
931 check_angle = fabs((double)(s->h.elev - sweep_angle));
933 if(check_angle <= delta_angle)
935 delta_angle = check_angle;
947 /********************************************************************/
949 /* RSL_get_closest_sweep */
951 /* Dennis Flanigan, Jr. 5/15/95 */
952 /********************************************************************/
953 Sweep *RSL_get_closest_sweep(Volume *v,float sweep_angle,float limit)
955 /* Find closest sweep to requested angle. Assume PPI sweep for
956 * now. Meaning: sweep_angle represents elevation angle from
963 if (v == NULL) return NULL;
965 if((ci = get_closest_sweep_index(v,sweep_angle)) < 0)
972 delta_angle = fabs((double)(s->h.elev - sweep_angle));
974 if( delta_angle <= limit)
984 /**********************************************************************/
985 /* These are more specific routines to make coding hierarchical. */
987 /* done 4/7/95 Ray *RSL_get_ray_from_sweep */
988 /* done 3/31 float RSL_get_value_from_sweep */
989 /* done 3/31 float RSL_get_value_from_ray */
990 /* done 4/1 float RSL_get_value_at_h */
992 /**********************************************************************/
993 Ray *RSL_get_ray_from_sweep(Sweep *s, float ray_angle)
995 /* Locate the Ray * for ray_angle in the sweep. */
998 if (s == NULL) return NULL;
999 if (ray_angle < 0) ray_angle += 360.0; /* Only positive angles. */
1000 if (ray_angle >= 360) ray_angle -= 360;
1002 return RSL_get_closest_ray_from_sweep(s,ray_angle,s->h.horz_half_bw);
1005 /**********************************************
1009 * Dennis Flanigan, Jr. 4/27/95 *
1010 **********************************************/
1011 int hash_bin(Hash_table *table,float angle)
1013 /* Internal Routine to calculate the hashing bin index
1019 res = 360.0/table->nindexes;
1020 hash = (int)(angle/res + res/2.0);/*Centered about bin.*/
1022 if(hash >= table->nindexes) hash = hash - table->nindexes;
1024 /* Could test see which direction is closer, but
1027 while(table->indexes[hash] == NULL) {
1029 if(hash >= table->nindexes) hash = 0;
1035 Hash_table *hash_table_for_sweep(Sweep *s)
1040 if (i==-1) { /* Obviously, an unregistered sweep. Most likely the
1041 * result of pointer assignments.
1043 i = INSERT_SWEEP(s);
1046 if (RSL_sweep_list[i].hash == NULL) { /* First time. Construct the table. */
1047 RSL_sweep_list[i].hash = construct_sweep_hash_table(s);
1050 return RSL_sweep_list[i].hash;
1053 /*********************************************************************/
1055 /* RSL_get_closest_ray_from_sweep */
1057 /* Dennis Flanigan 4/30/95 */
1058 /*********************************************************************/
1059 Ray *RSL_get_closest_ray_from_sweep(Sweep *s,float ray_angle, float limit)
1062 * Return closest Ray in Sweep within limit (angle) specified
1063 * in parameter list. Assume PPI mode.
1066 Hash_table *hash_table;
1067 Azimuth_hash *closest;
1070 if (s == NULL) return NULL;
1071 /* Find a non-NULL index close to hindex that we want. */
1072 hash_table = hash_table_for_sweep(s);
1073 if (hash_table == NULL) return NULL; /* Nada. */
1075 hindex = hash_bin(hash_table,ray_angle);
1077 /* Find hash entry with closest Ray */
1078 closest = the_closest_hash(hash_table->indexes[hindex],ray_angle);
1080 /* Is closest ray within limit parameter ? If
1081 * so return ray, else return NULL.
1084 close_diff = angle_diff(ray_angle,closest->ray->h.azimuth);
1086 if(close_diff <= limit) return closest->ray;
1092 /*********************************************************************/
1094 /* Rsl_get_value_from_sweep */
1096 /*********************************************************************/
1097 float RSL_get_value_from_sweep(Sweep *s, float azim, float r)
1099 /* Locate the polar point (r,azim) in the sweep. */
1101 if (s == NULL) return BADVAL;
1102 ray = RSL_get_ray_from_sweep(s, azim);
1103 if (ray == NULL) return BADVAL;
1104 return RSL_get_value_from_ray(ray, r);
1108 /*********************************************************************/
1110 /* RSL_get_range_of_range_index */
1111 /* D. Flanigan 8/18/95 */
1112 /*********************************************************************/
1113 float RSL_get_range_of_range_index(Ray *ray, int index)
1115 if (ray == NULL) return 0.0;
1116 if (index >= ray->h.nbins) return 0.0;
1117 return ray->h.range_bin1/1000.0 + index*ray->h.gate_size/1000.0;
1121 /************************************/
1122 /* RSL_get_value_from_ray */
1124 /* Updated 4/4/95 D. Flanigan */
1126 /************************************/
1127 float RSL_get_value_from_ray(Ray *ray, float r)
1134 if (ray == NULL) return BADVAL;
1136 if(ray->h.gate_size == 0)
1138 if(radar_verbose_flag)
1140 fprintf(stderr,"RSL_get_value_from_ray: ray->h.gate_size == 0\n");
1145 /* range_bin1 is range to center of first bin */
1146 bin_index = (int)(((rm - ray->h.range_bin1)/ray->h.gate_size) + 0.5);
1148 /* Bin indexes go from 0 to nbins - 1 */
1149 if (bin_index >= ray->h.nbins || bin_index < 0) return BADVAL;
1151 return ray->h.f(ray->range[bin_index]);
1155 /*********************************************************************/
1157 /* RSL_get_value_at_h */
1159 /*********************************************************************/
1160 float RSL_get_value_at_h(Volume *v, float azim, float grnd_r, float h)
1164 RSL_get_slantr_and_elev(grnd_r, h, &r, &elev);
1165 return RSL_get_value(v, elev, azim, r);
1169 /**********************************************************************/
1170 /* These take a Volume and return the appropriate structure. */
1172 /* done 4/21/95 Sweep *RSL_get_sweep */
1173 /* done 4/1 Ray *RSL_get_ray */
1174 /* done 4/1 float *RSL_get_value */
1175 /* done 5/3 Ray *RSL_get_ray_above */
1176 /* done 5/3 Ray *RSL_get_ray_below */
1177 /* done 5/12 Ray *RSL_get_ray_from_other_volume */
1179 /**********************************************************************/
1183 /*********************************************************************/
1187 /* Updated 5/15/95 Dennis Flanigan, Jr. */
1188 /*********************************************************************/
1189 Sweep *RSL_get_sweep(Volume *v, float sweep_angle)
1191 /* Return a sweep with +/- 1/2 beam_width of 'elev', if found. */
1194 if (v == NULL) return NULL;
1195 while(v->sweep[i] == NULL) i++;
1197 return RSL_get_closest_sweep(v,sweep_angle,v->sweep[i]->h.vert_half_bw);
1201 /*********************************************************************/
1205 /*********************************************************************/
1206 Ray *RSL_get_ray(Volume *v, float elev, float azimuth)
1208 /* Locate 'elev' and 'azimuth' in the Volume v by a simple +/- epsilon on
1209 * the elevation angle and azimuth angle.
1213 * 1. Locate sweep using azimuth; call RSL_get_sweep.
1214 * 2. Call RSL_get_ray_from_sweep
1217 return RSL_get_ray_from_sweep( RSL_get_sweep( v, elev ), azimuth );
1220 /*********************************************************************/
1224 /*********************************************************************/
1225 float RSL_get_value(Volume *v, float elev, float azimuth, float range)
1227 /* Locate 'elev' and 'azimuth' and '<range' in the Volume v
1228 * by a simple +/- epsilon on the elevation angle and azimuth angle
1233 * 1. Locate sweep using 'elev'.
1234 * 2. Call RSL_get_value_from_sweep
1236 return RSL_get_value_from_sweep ( RSL_get_sweep (v, elev), azimuth, range );
1239 /*********************************************************************/
1241 /* RSL_get_ray_above */
1243 /* Updated 5/15/95, Dennis Flanigan, Jr. */
1244 /*********************************************************************/
1245 Ray *RSL_get_ray_above(Volume *v, Ray *current_ray)
1249 if (v == NULL) return NULL;
1250 if (current_ray == NULL) return NULL;
1252 /* Find index of current Sweep */
1253 if(( i = get_closest_sweep_index(v,current_ray->h.elev)) < 0) return NULL;
1256 while( i < v->h.nsweeps)
1258 if(v->sweep[i] != NULL) break;
1262 if(i >= v->h.nsweeps) return NULL;
1264 return RSL_get_ray_from_sweep(v->sweep[i], current_ray->h.azimuth);
1268 /*********************************************************************/
1270 /* RSL_get_ray_below */
1272 /* Updated 5/15/95, Dennis Flanigan, Jr. */
1273 /*********************************************************************/
1274 Ray *RSL_get_ray_below(Volume *v, Ray *current_ray)
1278 if (v == NULL) return NULL;
1279 if (current_ray == NULL) return NULL;
1281 /* Find index of current Sweep */
1282 if(( i = get_closest_sweep_index(v,current_ray->h.elev)) < 0) return NULL;
1287 if(v->sweep[i] != NULL) break;
1291 if(i < 0) return NULL;
1293 return RSL_get_ray_from_sweep(v->sweep[i], current_ray->h.azimuth);
1296 /*********************************************************************/
1298 /* RSL_get_matching_ray */
1300 /*********************************************************************/
1301 Ray *RSL_get_matching_ray(Volume *v, Ray *ray)
1305 * Locate the closest matching ray in the Volume 'v' to 'ray'.
1306 * Typically, use this function when finding a similiar ray in another
1309 if (v == NULL) return NULL;
1310 if (ray == NULL) return NULL;
1312 return RSL_get_ray(v, ray->h.elev, ray->h.azimuth);
1315 /*********************************************************************/
1317 /* RSL_get_first_ray_of_sweep */
1318 /* RSL_get_first_ray_of_volume */
1320 /*********************************************************************/
1321 Ray *RSL_get_first_ray_of_sweep(Sweep *s)
1323 /* Because a sorting of azimuth angles may have been performed,
1324 * we need to test on the ray_num member and look for the smallest
1329 int smallest_ray_num;
1332 smallest_ray_num = 9999999;
1333 if (s == NULL) return r;
1334 for (i=0; i<s->h.nrays; i++)
1336 if (s->ray[i]->h.ray_num <= 1) return s->ray[i];
1337 if (s->ray[i]->h.ray_num < smallest_ray_num) {
1339 smallest_ray_num = r->h.ray_num;
1345 Ray *RSL_get_first_ray_of_volume(Volume *v)
1348 if (v == NULL) return NULL;
1349 for (i=0; i<v->h.nsweeps; i++)
1350 if (v->sweep[i]) return RSL_get_first_ray_of_sweep(v->sweep[i]);
1354 /*********************************************************************/
1356 /* RSL_get_first_sweep_of_volume */
1358 /*********************************************************************/
1359 Sweep *RSL_get_first_sweep_of_volume(Volume *v)
1362 if (v == NULL) return NULL;
1363 for (i=0; i<v->h.nsweeps; i++)
1364 if (RSL_get_first_ray_of_sweep(v->sweep[i])) return v->sweep[i];
1368 #define N_SPECIAL_NAMES 2
1370 * Unfortunately in C, there is no way around initializing static
1371 * arrays by specifying repetition.
1373 * There is another solution and that is to have RSL_new_radar set
1374 * a flag indicating if the application has called 'RSL_select_fields'
1375 * prior to calling the ingest routine. I choose the static = {...}; method
1379 /* Could be static and force use of 'rsl_query_field' */
1380 int rsl_qfield[MAX_RADAR_VOLUMES] = {
1391 /*********************************************************************/
1393 /* RSL_select_fields */
1395 /*********************************************************************/
1396 void RSL_select_fields(char *field_type, ...)
1400 * field_type = Case insensitive:
1401 * "all" - default, if never this routine is never called.
1402 * "none" - No fields are ingestd. Useful for getting header
1404 * "dz" - Ingest DZ volume.
1405 * "vr" - Ingest VR volume.
1406 * ... - Just list additional fields.
1408 * The last argument must be NULL. This signals this routine
1409 * when to stop parsing the field types.
1411 * Action or side-effect:
1412 * A second call to this fuction overrides any previous settings.
1413 * In other words, multiple calls are not additive. So, to get both
1414 * DZ and VR volumes, use:
1415 * RSL_select_fields("dz", "vr"); - Read both DZ and VR.
1417 * RSL_select_fields("dz"); - Read only DZ.
1418 * RSL_select_fields("vr"); - Read only VR, no DZ.
1420 * An RSL hidden array is set to flag which fields are selected.
1421 * This array is examined inside all ingest code. It is not available
1422 * to the application.
1429 for (i=0; i<MAX_RADAR_VOLUMES; i++) rsl_qfield[i] = 0;
1431 /* # arguments, should be <= MAX_RADAR_VOLUMES, but we can handle
1432 * typo's and redundancies. Each is processed in the order they
1436 c_field = field_type;
1437 va_start(ap, field_type);
1439 if (radar_verbose_flag) fprintf(stderr,"Selected fields for ingest:");
1441 /* CHECK EACH FIELD. This is a fancier case statement than C provides. */
1442 if (radar_verbose_flag) fprintf(stderr," %s", c_field);
1443 if (strcasecmp(c_field, "all") == 0) {
1444 for (i=0; i<MAX_RADAR_VOLUMES; i++) rsl_qfield[i] = 1;
1445 } else if (strcasecmp(c_field, "none") == 0) {
1446 for (i=0; i<MAX_RADAR_VOLUMES; i++) rsl_qfield[i] = 0;
1449 for (i=0; i<MAX_RADAR_VOLUMES; i++)
1450 if (strcasecmp(c_field, RSL_ftype[i]) == 0) {
1452 break; /* Break the for loop. */
1455 if (i == MAX_RADAR_VOLUMES) {
1456 if (radar_verbose_flag)
1457 fprintf(stderr, "\nRSL_select_fields: Invalid field name <<%s>> specified.\n", c_field);
1460 c_field = va_arg(ap, char *);
1463 if (radar_verbose_flag) fprintf(stderr,"\n");
1469 int rsl_query_field(char *c_field)
1473 * RSL interface, for library code developers, to rsl ingest code,
1474 * which is intended to be part of RSL ingest code, which access
1475 * the hidden array 'rsl_qfield' and reports if that field is to
1478 * Return 1 if YES, meaning yes ingest this field type.
1483 * All ingest code is meant to use this routine to decide whether
1484 * or not to allocate memory for a field type. For data formats
1485 * that are very large, this will help optimize the ingest on
1486 * small memory machines and hopefully avoid unnessary swapping.
1488 * LASSEN is a good example where there may be 10 or 12 input field
1489 * types, but the application only wants 2 or 3 of them.
1491 * The application interface is RSL_select_fields.
1495 /* Quiet the compilier when -pedantic. :-) */
1496 RSL_f_list[0] = RSL_f_list[0];
1497 RSL_invf_list[0] = RSL_invf_list[0];
1499 for (i=0; i<MAX_RADAR_VOLUMES; i++)
1500 if (strcasecmp(c_field, RSL_ftype[i]) == 0) {
1502 break; /* Break the for loop. */
1505 if (i == MAX_RADAR_VOLUMES) { /* We should never see this message for
1506 * properly written ingest code.
1508 fprintf(stderr, "rsl_query_field: Invalid field name <<%s>> specified.\n", c_field);
1511 /* 'i' is the index. Is it set? */
1512 return rsl_qfield[i];
1516 /* Could be static and force use of 'rsl_query_sweep' */
1517 int *rsl_qsweep = NULL; /* If NULL, then read all sweeps. Otherwise,
1518 * read what is on the list.
1520 #define RSL_MAX_QSWEEP 500 /* It'll be rediculious to have more. :-) */
1521 int rsl_qsweep_max = RSL_MAX_QSWEEP;
1523 /*********************************************************************/
1525 /* RSL_read_these_sweeps */
1527 /*********************************************************************/
1528 void RSL_read_these_sweeps(char *csweep, ...)
1534 /* "all", "none", "0", "1", "2", "3", ... */
1536 /* # arguments, should be <= 'max # sweeps expected', but, what is it?
1537 * We can handle typo's and redundancies. Each is processed in the
1538 * order they appear.
1542 va_start(ap, csweep);
1544 rsl_qsweep_max = -1;
1545 if (rsl_qsweep == NULL)
1546 rsl_qsweep = (int *)calloc(RSL_MAX_QSWEEP, sizeof(int));
1548 /* else Clear the array - a second call to this function over-rides
1549 * any previous settings. This holds even if the second call has
1553 for(i = 0;i< RSL_MAX_QSWEEP; i++)
1557 if (radar_verbose_flag) fprintf(stderr,"Selected sweeps for ingest:");
1558 for (;c_sweep; c_sweep = va_arg(ap, char *))
1560 /* CHECK EACH FIELD. This is a fancier case statement than C provides. */
1561 if (radar_verbose_flag) fprintf(stderr," %s", c_sweep);
1562 if (strcasecmp(c_sweep, "all") == 0) {
1563 for (i=0; i<RSL_MAX_QSWEEP; i++) rsl_qsweep[i] = 1;
1564 rsl_qsweep_max = RSL_MAX_QSWEEP;
1565 } else if (strcasecmp(c_sweep, "none") == 0) {
1566 /* Commented this out to save runtime -GJW
1567 * rsl_qsweep[] already initialized to 0 above.
1569 * for (i=0; i<RSL_MAX_QSWEEP; i++) rsl_qsweep[i] = 0;
1570 * rsl_qsweep_max = -1;
1573 i = sscanf(c_sweep,"%d", &isweep);
1574 if (i == 0) { /* No match, bad argument. */
1575 if (radar_verbose_flag) fprintf(stderr,"\nRSL_read_these_sweeps: bad parameter %s. Ignoring.\n", c_sweep);
1579 if (isweep < 0 || isweep > RSL_MAX_QSWEEP) {
1580 if (radar_verbose_flag) fprintf(stderr,"\nRSL_read_these_sweeps: parameter %s not in [0,%d). Ignoring.\n", c_sweep, RSL_MAX_QSWEEP);
1584 if (isweep > rsl_qsweep_max) rsl_qsweep_max = isweep;
1585 rsl_qsweep[isweep] = 1;
1589 if (radar_verbose_flag) fprintf(stderr,"\n");
1594 void RSL_fix_time (Ray *ray)
1598 /* Fixes possible overflow values in month, day, year, hh, mm, ss */
1599 /* Normally, ss should be the overflow. This code ensures end of
1600 * month, year and century are handled correctly by using the Unix
1603 if (ray == NULL) return;
1604 memset(&the_time, 0, sizeof(struct tm));
1605 the_time.tm_sec = ray->h.sec;
1606 fsec = ray->h.sec - the_time.tm_sec;
1607 the_time.tm_min = ray->h.minute;
1608 the_time.tm_hour = ray->h.hour;
1609 the_time.tm_mon = ray->h.month - 1;
1610 the_time.tm_year = ray->h.year - 1900;
1611 the_time.tm_mday = ray->h.day;
1612 the_time.tm_isdst = -1;
1613 (void) mktime(&the_time);
1614 /* The time is fixed. */
1615 ray->h.sec = the_time.tm_sec;
1617 ray->h.minute = the_time.tm_min;
1618 ray->h.hour = the_time.tm_hour;
1619 ray->h.month = the_time.tm_mon + 1;
1620 ray->h.year = the_time.tm_year + 1900;
1621 ray->h.day = the_time.tm_mday;
1625 /*********************************************************************/
1627 /* RSL_add_dbz_offset_to_ray */
1629 /*********************************************************************/
1631 Add the calibration factor 'dbz_offset' to each ray bin which
1632 contains a valid value.
1634 void RSL_add_dbz_offset_to_ray(Ray *r, float dbz_offset)
1639 if (r == NULL) return;
1640 for (ibin=0; ibin<r->h.nbins; ibin++)
1642 val = r->h.f(r->range[ibin]);
1643 if ( val >= (float)NOECHO ) continue; /* Invalid value */
1644 r->range[ibin] = r->h.invf(val + dbz_offset);
1648 /*********************************************************************/
1650 /* RSL_add_dbz_offset_to_sweep */
1652 /*********************************************************************/
1653 void RSL_add_dbz_offset_to_sweep(Sweep *s, float dbz_offset)
1656 if (s == NULL) return;
1657 for (iray=0; iray<s->h.nrays; iray++)
1658 RSL_add_dbz_offset_to_ray(s->ray[iray], dbz_offset);
1661 /*********************************************************************/
1663 /* RSL_add_dbz_offset_to_volume */
1665 /*********************************************************************/
1666 void RSL_add_dbz_offset_to_volume(Volume *v, float dbz_offset)
1669 if (v == NULL) return;
1670 for (isweep=0; isweep<v->h.nsweeps; isweep++)
1671 RSL_add_dbz_offset_to_sweep(v->sweep[isweep], dbz_offset);