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.
28 /* This allows us to use RSL_ftype, RSL_f_list, RSL_invf_list from rsl.h. */
32 extern int radar_verbose_flag;
33 /* Changed old buffer size (16384) for larger dualpol files. BLK 5/18/2011 */
34 typedef short UF_buffer[20000]; /* Some UF files are bigger than 4096
35 * that the UF doc's specify.
41 static float (*f)(Range x);
42 static Range (*invf)(float x);
44 Volume *reset_nsweeps_in_volume(Volume *volume)
47 if (volume == NULL) return NULL;
48 for (i=volume->h.nsweeps; i>0; i--)
49 if (volume->sweep[i-1] != NULL) {
50 volume->h.nsweeps = i;
55 Radar *reset_nsweeps_in_all_volumes(Radar *radar)
58 if (radar == NULL) return NULL;
59 for (i=0; i<radar->h.nvolumes; i++)
60 radar->v[i] = reset_nsweeps_in_volume(radar->v[i]);
64 Volume *copy_sweeps_into_volume(Volume *new_volume, Volume *old_volume)
68 if (old_volume == NULL) return new_volume;
69 if (new_volume == NULL) return new_volume;
70 nsweeps = new_volume->h.nsweeps; /* Save before copying old header. */
71 new_volume->h = old_volume->h;
72 new_volume->h.nsweeps = nsweeps;
73 for (i=0; i<old_volume->h.nsweeps; i++)
74 new_volume->sweep[i] = old_volume->sweep[i]; /* Just copy pointers. */
75 /* Free the old sweep array, not the pointers to sweeps. */
76 free(old_volume->sweep);
80 void swap2(short *buf, int n)
84 while (buf < end_addr) {
91 /********************************************************************/
92 /*********************************************************************/
96 /* By: John Merritt */
97 /* Space Applications Corporation */
99 /*********************************************************************/
100 int uf_into_radar(UF_buffer uf, Radar **the_radar)
103 /* Missing data flag : -32768 when a signed short. */
104 #define UF_NO_DATA 0X8000
106 /* Any convensions may be observed, however, Radial Velocity must be VE. */
108 * DM = Reflectivity (dB(mW)).
109 * DZ = Reflectivity (dBZ).
110 * VR = Radial Velocity.
111 * CZ = Corrected Reflectivity. (Quality controlled: AP removed, etc.)
112 * SW = Spectrum Width.
113 * DR = Differential Reflectivity.
114 * XZ = X-band Reflectivity.
116 * These fields may appear in any order in the UF file.
119 * UF_DONE if we're done with the UF ingest.
120 * UF_MORE if we need more UF data.
123 /* These are pointers to various locations within the UF buffer 'uf'.
124 * They are used to index the different components of the UF structure in
125 * a manor consistant with the UF documentation. For instance, uf_ma[1]
126 * will be equivalenced to the second word (2 bytes/each) of the UF
129 short *uf_ma; /* Mandatory header block. */
130 short *uf_lu; /* Local Use header block. */
131 short *uf_dh; /* Data header. */
132 short *uf_fh; /* Field header. */
133 short *uf_data; /* Data. */
135 /* The length of each header. */
136 int len_data, len_lu;
138 int current_fh_index;
141 int nfields, isweep, ifield, iray, i, j, m;
142 static int pulled_time_from_first_ray = 0;
143 static int need_scan_mode = 1;
144 char *field_type; /* For printing the field type upon error. */
153 extern int rsl_qfield[];
154 extern int *rsl_qsweep; /* See RSL_read_these_sweeps in volume.c */
155 extern int rsl_qsweep_max;
160 * The organization of the Radar structure is by volumes, then sweeps, then
161 * rays, then gates. This is different from the UF file organization.
162 * The UF format is sweeps, rays, then gates for all field types (volumes).
166 /* Set up all the UF pointers. */
168 uf_lu = uf + uf_ma[3] - 1;
169 uf_dh = uf + uf_ma[4] - 1;
172 isweep = uf_ma[9] - 1;
174 if (rsl_qsweep != NULL) {
175 if (isweep > rsl_qsweep_max) return UF_DONE;
176 if (rsl_qsweep[isweep] == 0) return UF_MORE;
180 /* Here is a sticky part. We must make sure that if we encounter any
181 * additional fields that were not previously present, that we are able
182 * to load them. This will require us to copy the entire radar structure
183 * and whack off the old one. But, we must be sure that it really is a
184 * new field. This is not so trivial as a couple of lines of code; I will
185 * have to think about this a little bit more. See STICKYSOLVED below.
188 if (radar == NULL) radar = RSL_new_radar(nfields);
189 /* Sticky solution here. */
192 radar = RSL_new_radar(MAX_RADAR_VOLUMES);
194 pulled_time_from_first_ray = 0;
195 for (i=0; i<MAX_RADAR_VOLUMES; i++)
196 if (rsl_qfield[i]) /* See RSL_select_fields in volume.c */
197 radar->v[i] = RSL_new_volume(20);
202 if (need_scan_mode) {
203 /* PPI and RHI are enum constants defined in rsl.h */
204 if (uf_ma[34] == 1) radar->h.scan_mode = PPI;
205 else if (uf_ma[34] == 3) radar->h.scan_mode = RHI;
207 fprintf(stderr,"Warning: UF sweep mode = %d\n", uf_ma[34]);
208 fprintf(stderr," Expected 1 or 3 (PPI or RHI)\n");
209 fprintf(stderr," Setting radar->h.scan_mode to PPI\n");
210 radar->h.scan_mode = PPI;
215 /* For LITTLE ENDIAN:
216 * WE "UNSWAP" character strings. Because there are so few of them,
217 * it is easier to catch them here. The entire UF buffer is swapped prior
218 * to entry to here, therefore, undo-ing these swaps; sets the
219 * character strings right.
222 for (i=0; i<nfields; i++) {
223 if (little_endian()) swap_2_bytes(&uf_dh[3+2*i]); /* Unswap. */
225 field_type = (char *)&uf_dh[3+2*i];
226 for (j=0; j<MAX_RADAR_VOLUMES; j++) {
227 if (strncmp(field_type, RSL_ftype[j], 2) == 0) {
232 if (ifield < 0) { /* DON'T know how to handle this yet. */
233 fprintf(stderr, "Unknown field type %c%c\n", (char)field_type[0],
234 (char)field_type[1]);
238 f = RSL_f_list[ifield];
239 invf = RSL_invf_list[ifield];
241 /* Do we place the data into this volume? */
242 if (radar->v[ifield] == NULL) continue; /* Nope. */
244 if (isweep >= radar->v[ifield]->h.nsweeps) { /* Exceeded sweep limit.
245 * Allocate more sweeps.
246 * Copy all previous sweeps.
248 if (radar_verbose_flag)
249 fprintf(stderr,"Exceeded sweep allocation of %d. Adding 20 more.\n", isweep);
250 new_volume = RSL_new_volume(radar->v[ifield]->h.nsweeps+20);
251 new_volume = copy_sweeps_into_volume(new_volume, radar->v[ifield]);
252 radar->v[ifield] = new_volume;
255 if (radar->v[ifield]->sweep[isweep] == NULL) {
256 if (radar_verbose_flag)
257 fprintf(stderr,"Allocating new sweep for field %d, isweep %d\n", ifield, isweep);
258 radar->v[ifield]->sweep[isweep] = RSL_new_sweep(1000);
259 radar->v[ifield]->sweep[isweep]->h.nrays = 0; /* Increment this for each
262 radar->v[ifield]->h.f = f;
263 radar->v[ifield]->h.invf = invf;
264 radar->v[ifield]->sweep[isweep]->h.f = f;
265 radar->v[ifield]->sweep[isweep]->h.invf = invf;
266 radar->v[ifield]->sweep[isweep]->h.sweep_num = uf_ma[9];
267 radar->v[ifield]->sweep[isweep]->h.elev = uf_ma[35] / 64.0;
272 current_fh_index = uf_dh[4+2*i];
273 uf_fh = uf + current_fh_index - 1;
274 sweep = radar->v[ifield]->sweep[isweep];
275 iray = sweep->h.nrays;
277 radar->v[ifield]->sweep[isweep]->ray[iray] = RSL_new_ray(nbins);
278 ray = radar->v[ifield]->sweep[isweep]->ray[iray];
284 * ---- Beginning of MANDATORY HEADER BLOCK.
286 ray->h.ray_num = uf_ma[7];
287 if (little_endian()) swap2(&uf_ma[10], 8);
288 memcpy(radar->h.radar_name, &uf_ma[10], 8);
289 if (little_endian()) swap2(&uf_ma[10], 8/2);
290 memcpy(radar->h.name, &uf_ma[14], 8);
291 if (little_endian()) swap2(&uf_ma[14], 8/2);
293 /* All components of lat/lon are the same sign. If not, then
294 * what ever wrote the UF was in error. A simple RSL program
295 * can repair the damage, however, not here.
297 ray->h.lat = uf_ma[18] + uf_ma[19]/60.0 + uf_ma[20]/64.0/3600;
298 ray->h.lon = uf_ma[21] + uf_ma[22]/60.0 + uf_ma[23]/64.0/3600;
299 ray->h.alt = uf_ma[24];
300 ray->h.year = uf_ma[25];
301 if (ray->h.year < 1900) {
303 if (ray->h.year < 1980) ray->h.year += 100; /* Year >= 2000. */
305 ray->h.month = uf_ma[26];
306 ray->h.day = uf_ma[27];
307 ray->h.hour = uf_ma[28];
308 ray->h.minute = uf_ma[29];
309 ray->h.sec = uf_ma[30];
310 ray->h.azimuth = uf_ma[32] / 64.0;
312 /* If Local Use Header is present and contains azimuth, use that
313 * azimuth for VR and SW. This is for WSR-88D, which runs separate
314 * scans for DZ and VR/SW at the lower elevations, which means DZ
315 * VR/SW and have different azimuths in the "same" ray.
317 len_lu = uf_ma[4] - uf_ma[3];
318 if (len_lu == 2 && (ifield == VR_INDEX || ifield == SW_INDEX)) {
319 if (strncmp((char *)uf_lu,"ZA",2) == 0 ||
320 strncmp((char *)uf_lu,"AZ",2) == 0)
321 ray->h.azimuth = uf_lu[1] / 64.0;
323 if (ray->h.azimuth < 0.) ray->h.azimuth += 360.; /* make it 0 to 360. */
324 ray->h.elev = uf_ma[33] / 64.0;
325 ray->h.elev_num = sweep->h.sweep_num;
326 ray->h.fix_angle = sweep->h.elev = uf_ma[35] / 64.0;
327 ray->h.azim_rate = uf_ma[36] / 64.0;
328 ray->h.sweep_rate = ray->h.azim_rate * (60.0/360.0);
329 missing_data = uf_ma[44];
331 if (pulled_time_from_first_ray == 0) {
332 radar->h.height = uf_ma[24];
333 radar->h.latd = uf_ma[18];
334 radar->h.latm = uf_ma[19];
335 radar->h.lats = uf_ma[20] / 64.0;
336 radar->h.lond = uf_ma[21];
337 radar->h.lonm = uf_ma[22];
338 radar->h.lons = uf_ma[23] / 64.0;
339 radar->h.year = ray->h.year;
340 radar->h.month = ray->h.month;
341 radar->h.day = ray->h.day;
342 radar->h.hour = ray->h.hour;
343 radar->h.minute = ray->h.minute;
344 radar->h.sec = ray->h.sec;
345 strcpy(radar->h.radar_type, "uf");
346 pulled_time_from_first_ray = 1;
349 * ---- End of MANDATORY HEADER BLOCK.
352 /* ---- Optional header used for MCTEX files. */
353 /* If this is a MCTEX file, the first 4 words following the
354 mandatory header contain the string 'MCTEX'. */
355 memcpy(proj_name, (short *)(uf + uf_ma[2] - 1), 8);
356 if (little_endian()) swap2(proj_name, 4);
359 /* ---- Local Use Header (if present) was checked during Mandatory
360 * Header processing above.
363 /* ---- Begining of FIELD HEADER. */
364 uf_fh = uf+current_fh_index - 1;
365 scale_factor = uf_fh[1];
366 ray->h.range_bin1 = uf_fh[2] * 1000.0 + uf_fh[3];
367 ray->h.gate_size = uf_fh[4];
369 ray->h.nbins = uf_fh[5];
370 ray->h.pulse_width = uf_fh[6]/(RSL_SPEED_OF_LIGHT/1.0e6);
372 if (strncmp((char *)proj_name, "MCTEX", 5) == 0) /* MCTEX? */
374 /* The beamwidth values are not correct in Mctex UF files. */
375 ray->h.beam_width = 1.0;
376 sweep->h.beam_width = ray->h.beam_width;
377 sweep->h.horz_half_bw = ray->h.beam_width/2.0;
378 sweep->h.vert_half_bw = ray->h.beam_width/2.0;
382 ray->h.beam_width = uf_fh[7] / 64.0;
383 sweep->h.beam_width = uf_fh[7] / 64.0;
384 sweep->h.horz_half_bw = uf_fh[7] / 128.0; /* DFF 4/4/95 */
385 sweep->h.vert_half_bw = uf_fh[8] / 128.0; /* DFF 4/4/95 */
387 /* fprintf (stderr, "uf_fh[7] = %d, [8] = %d\n", (int)uf_fh[7], (int)uf_fh[8]); */
388 if((int)uf_fh[7] == -32768) {
389 ray->h.beam_width = 1;
390 sweep->h.beam_width = 1;
391 sweep->h.horz_half_bw = .5;
392 sweep->h.vert_half_bw = .5;
395 ray->h.frequency = uf_fh[9] / 64.0;
396 ray->h.wavelength = uf_fh[11] / 64.0 / 100.0; /* cm to m. */
397 ray->h.pulse_count = uf_fh[12];
398 if (ifield == DZ_INDEX || ifield == ZT_INDEX) {
399 radar->v[ifield]->h.calibr_const = uf_fh[16] / 100.0;
400 /* uf value scaled by 100 */
403 radar->v[ifield]->h.calibr_const = 0.0;
405 if (uf_fh[17] == (short)UF_NO_DATA) x = 0;
406 else x = uf_fh[17] / 1000000.0; /* PRT in seconds. */
409 ray->h.unam_rng = RSL_SPEED_OF_LIGHT / (2.0 * ray->h.prf * 1000.0);
413 ray->h.unam_rng = 0.0;
416 if (VR_INDEX == ifield || VE_INDEX == ifield) {
417 ray->h.nyq_vel = uf_fh[19] / scale_factor;
420 /* ---- End of FIELD HEADER. */
425 /* ---- Begining of FIELD DATA. */
426 uf_data = uf+uf_fh[0] - 1;
428 len_data = ray->h.nbins; /* Known because of RSL_new_ray. */
429 for (m=0; m<len_data; m++) {
430 if (uf_data[m] == (short)UF_NO_DATA)
431 ray->range[m] = invf(BADVAL); /* BADVAL */
433 if(uf_data[m] == missing_data)
434 ray->range[m] = invf(NOECHO); /* NOECHO */
436 ray->range[m] = invf((float)uf_data[m]/scale_factor);
445 /*********************************************************************/
449 /* By: John Merritt */
450 /* Space Applications Corporation */
451 /* October 4, 1994 */
452 /*********************************************************************/
453 void swap_uf_buffer(UF_buffer uf)
457 addr_end = uf + sizeof(UF_buffer)/sizeof(short);
458 while (uf < addr_end)
462 enum UF_type {NOT_UF, TRUE_UF, TWO_BYTE_UF, FOUR_BYTE_UF};
465 /*********************************************************************/
467 /* RSL_uf_to_radar_fp */
469 /* By: John Merritt */
470 /* Space Applications Corporation */
471 /* September 22, 1995 */
472 /*********************************************************************/
473 Radar *RSL_uf_to_radar_fp(FILE *fp)
484 enum UF_type uf_type;
485 #define NEW_BUFSIZ 16384
489 /* setvbuf(fp,NULL,_IOFBF,(size_t)NEW_BUFSIZ); * Faster i/o? */
490 if (fread(magic.buf, sizeof(char), 6, fp) <= 0) return NULL;
492 * Check for fortran record length delimeters, NCAR kludge.
494 if (strncmp("UF", magic.buf, 2) == 0) uf_type = TRUE_UF;
495 else if (strncmp("UF", &magic.buf[2], 2) == 0) uf_type = TWO_BYTE_UF;
496 else if (strncmp("UF", &magic.buf[4], 2) == 0) uf_type = FOUR_BYTE_UF;
497 else uf_type = NOT_UF;
501 if (radar_verbose_flag) fprintf(stderr,"UF file with 4 byte FORTRAN record delimeters.\n");
502 /* Handle first record specially, since we needed magic information. */
504 if (little_endian()) swap_4_bytes(&nbytes);
505 memcpy(uf, &magic.buf[4], 2);
506 (void)fread(&uf[1], sizeof(char), nbytes-2, fp);
507 if (little_endian()) swap_uf_buffer(uf);
508 (void)fread(&nbytes, sizeof(int), 1, fp);
509 if (uf_into_radar(uf, &radar) == UF_DONE) break;
510 /* Now the rest of the file. */
511 while(fread(&nbytes, sizeof(int), 1, fp) > 0) {
512 if (little_endian()) swap_4_bytes(&nbytes);
514 (void)fread(uf, sizeof(char), nbytes, fp);
515 if (little_endian()) swap_uf_buffer(uf);
517 (void)fread(&nbytes, sizeof(int), 1, fp);
519 if (uf_into_radar(uf, &radar) == UF_DONE) break;
524 if (radar_verbose_flag) fprintf(stderr,"UF file with 2 byte FORTRAN record delimeters.\n");
525 /* Handle first record specially, since we needed magic information. */
526 sbytes = magic.sword;
527 if (little_endian()) swap_2_bytes(&sbytes);
528 memcpy(uf, &magic.buf[2], 4);
529 (void)fread(&uf[2], sizeof(char), sbytes-4, fp);
530 if (little_endian()) swap_uf_buffer(uf);
531 (void)fread(&sbytes, sizeof(short), 1, fp);
532 uf_into_radar(uf, &radar);
533 /* Now the rest of the file. */
534 while(fread(&sbytes, sizeof(short), 1, fp) > 0) {
535 if (little_endian()) swap_2_bytes(&sbytes);
537 (void)fread(uf, sizeof(char), sbytes, fp);
538 if (little_endian()) swap_uf_buffer(uf);
540 (void)fread(&sbytes, sizeof(short), 1, fp);
542 if (uf_into_radar(uf, &radar) == UF_DONE) break;
547 if (radar_verbose_flag) fprintf(stderr,"UF file with no FORTRAN record delimeters. Good.\n");
548 /* Handle first record specially, since we needed magic information. */
549 memcpy(&sbytes, &magic.buf[2], 2); /* Record length is in word #2. */
550 if (little_endian()) swap_2_bytes(&sbytes); /* # of 2 byte words. */
552 memcpy(uf, &magic.buf[0], 6);
553 (void)fread(&uf[3], sizeof(short), sbytes-3, fp);
554 if (little_endian()) swap_uf_buffer(uf);
555 uf_into_radar(uf, &radar);
556 /* Now the rest of the file. */
557 while(fread(uf, sizeof(short), 2, fp) > 0) {
558 memcpy(&sbytes, &uf[1], 2); /* Record length is in word #2. */
559 if (little_endian()) swap_2_bytes(&sbytes);
561 (void)fread(&uf[2], sizeof(short), sbytes-2, fp); /* Have words 1,2. */
562 if (little_endian()) swap_uf_buffer(uf);
564 if (uf_into_radar(uf, &radar) == UF_DONE) break;
568 case NOT_UF: return NULL; break;
570 radar = reset_nsweeps_in_all_volumes(radar);
571 radar = RSL_prune_radar(radar);
577 /*********************************************************************/
579 /* RSL_uf_to_radar */
581 /* By: John Merritt */
582 /* Space Applications Corporation */
583 /* September 22, 1995 */
584 /*********************************************************************/
585 Radar *RSL_uf_to_radar(char *infile)
588 * This routine ingests a UF file and fills the Radar structure.
589 * This routine allocates space via the system routine malloc.
591 * If *infile is NULL, read from stdin.
597 if (infile == NULL) {
600 fp = fdopen(save_fd, "r");
601 } else if ((fp = fopen(infile, "r")) == NULL) {
605 fp = uncompress_pipe(fp); /* Transparently gunzip. */
606 radar = RSL_uf_to_radar_fp(fp);