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 typedef short UF_buffer[16384]; /* Some UF files are bigger than 4096
34 * that the UF doc's specify.
40 static float (*f)(Range x);
41 static Range (*invf)(float x);
43 Volume *reset_nsweeps_in_volume(Volume *volume)
46 if (volume == NULL) return NULL;
47 for (i=volume->h.nsweeps; i>0; i--)
48 if (volume->sweep[i-1] != NULL) {
49 volume->h.nsweeps = i;
54 Radar *reset_nsweeps_in_all_volumes(Radar *radar)
57 if (radar == NULL) return NULL;
58 for (i=0; i<radar->h.nvolumes; i++)
59 radar->v[i] = reset_nsweeps_in_volume(radar->v[i]);
63 Volume *copy_sweeps_into_volume(Volume *new_volume, Volume *old_volume)
67 if (old_volume == NULL) return new_volume;
68 if (new_volume == NULL) return new_volume;
69 nsweeps = new_volume->h.nsweeps; /* Save before copying old header. */
70 new_volume->h = old_volume->h;
71 new_volume->h.nsweeps = nsweeps;
72 for (i=0; i<old_volume->h.nsweeps; i++)
73 new_volume->sweep[i] = old_volume->sweep[i]; /* Just copy pointers. */
74 /* Free the old sweep array, not the pointers to sweeps. */
75 free(old_volume->sweep);
79 void swap2(short *buf, int n)
83 while (buf < end_addr) {
90 /********************************************************************/
91 /*********************************************************************/
95 /* By: John Merritt */
96 /* Space Applications Corporation */
98 /*********************************************************************/
99 int uf_into_radar(UF_buffer uf, Radar **the_radar)
102 /* Missing data flag : -32768 when a signed short. */
103 #define UF_NO_DATA 0X8000
105 /* Any convensions may be observed, however, Radial Velocity must be VE. */
107 * DM = Reflectivity (dB(mW)).
108 * DZ = Reflectivity (dBZ).
109 * VR = Radial Velocity.
110 * CZ = Corrected Reflectivity. (Quality controlled: AP removed, etc.)
111 * SW = Spectrum Width.
112 * DR = Differential Reflectivity.
113 * XZ = X-band Reflectivity.
115 * These fields may appear in any order in the UF file.
118 * UF_DONE if we're done with the UF ingest.
119 * UF_MORE if we need more UF data.
122 /* These are pointers to various locations within the UF buffer 'uf'.
123 * They are used to index the different components of the UF structure in
124 * a manor consistant with the UF documentation. For instance, uf_ma[1]
125 * will be equivalenced to the second word (2 bytes/each) of the UF
128 short *uf_ma; /* Mandatory header block. */
129 short *uf_lu; /* Local Use header block. */
130 short *uf_dh; /* Data header. */
131 short *uf_fh; /* Field header. */
132 short *uf_data; /* Data. */
134 /* The length of each header. */
135 int len_data, len_lu;
137 int current_fh_index;
140 int nfields, isweep, ifield, iray, i, j, m;
141 static int pulled_time_from_first_ray = 0;
142 static int need_scan_mode = 1;
143 char *field_type; /* For printing the field type upon error. */
152 extern int rsl_qfield[];
153 extern int *rsl_qsweep; /* See RSL_read_these_sweeps in volume.c */
154 extern int rsl_qsweep_max;
159 * The organization of the Radar structure is by volumes, then sweeps, then
160 * rays, then gates. This is different from the UF file organization.
161 * The UF format is sweeps, rays, then gates for all field types (volumes).
165 /* Set up all the UF pointers. */
167 uf_lu = uf + uf_ma[3] - 1;
168 uf_dh = uf + uf_ma[4] - 1;
171 isweep = uf_ma[9] - 1;
173 if (rsl_qsweep != NULL) {
174 if (isweep > rsl_qsweep_max) return UF_DONE;
175 if (rsl_qsweep[isweep] == 0) return UF_MORE;
179 /* Here is a sticky part. We must make sure that if we encounter any
180 * additional fields that were not previously present, that we are able
181 * to load them. This will require us to copy the entire radar structure
182 * and whack off the old one. But, we must be sure that it really is a
183 * new field. This is not so trivial as a couple of lines of code; I will
184 * have to think about this a little bit more. See STICKYSOLVED below.
187 if (radar == NULL) radar = RSL_new_radar(nfields);
188 /* Sticky solution here. */
191 radar = RSL_new_radar(MAX_RADAR_VOLUMES);
193 pulled_time_from_first_ray = 0;
194 for (i=0; i<MAX_RADAR_VOLUMES; i++)
195 if (rsl_qfield[i]) /* See RSL_select_fields in volume.c */
196 radar->v[i] = RSL_new_volume(20);
201 if (need_scan_mode) {
202 /* PPI and RHI are enum constants defined in rsl.h */
203 if (uf_ma[34] == 1) radar->h.scan_mode = PPI;
204 else if (uf_ma[34] == 3) radar->h.scan_mode = RHI;
206 fprintf(stderr,"Warning: UF sweep mode = %d\n", uf_ma[34]);
207 fprintf(stderr," Expected 1 or 3 (PPI or RHI)\n");
208 fprintf(stderr," Setting radar->h.scan_mode to PPI\n");
209 radar->h.scan_mode = PPI;
214 /* For LITTLE ENDIAN:
215 * WE "UNSWAP" character strings. Because there are so few of them,
216 * it is easier to catch them here. The entire UF buffer is swapped prior
217 * to entry to here, therefore, undo-ing these swaps; sets the
218 * character strings right.
221 for (i=0; i<nfields; i++) {
222 if (little_endian()) swap_2_bytes(&uf_dh[3+2*i]); /* Unswap. */
224 field_type = (char *)&uf_dh[3+2*i];
225 for (j=0; j<MAX_RADAR_VOLUMES; j++) {
226 if (strncmp(field_type, RSL_ftype[j], 2) == 0) {
231 if (ifield < 0) { /* DON'T know how to handle this yet. */
232 fprintf(stderr, "Unknown field type %c%c\n", (char)field_type[0],
233 (char)field_type[1]);
237 f = RSL_f_list[ifield];
238 invf = RSL_invf_list[ifield];
240 /* Do we place the data into this volume? */
241 if (radar->v[ifield] == NULL) continue; /* Nope. */
243 if (isweep >= radar->v[ifield]->h.nsweeps) { /* Exceeded sweep limit.
244 * Allocate more sweeps.
245 * Copy all previous sweeps.
247 if (radar_verbose_flag)
248 fprintf(stderr,"Exceeded sweep allocation of %d. Adding 20 more.\n", isweep);
249 new_volume = RSL_new_volume(radar->v[ifield]->h.nsweeps+20);
250 new_volume = copy_sweeps_into_volume(new_volume, radar->v[ifield]);
251 radar->v[ifield] = new_volume;
254 if (radar->v[ifield]->sweep[isweep] == NULL) {
255 if (radar_verbose_flag)
256 fprintf(stderr,"Allocating new sweep for field %d, isweep %d\n", ifield, isweep);
257 radar->v[ifield]->sweep[isweep] = RSL_new_sweep(1000);
258 radar->v[ifield]->sweep[isweep]->h.nrays = 0; /* Increment this for each
261 radar->v[ifield]->h.f = f;
262 radar->v[ifield]->h.invf = invf;
263 radar->v[ifield]->sweep[isweep]->h.f = f;
264 radar->v[ifield]->sweep[isweep]->h.invf = invf;
265 radar->v[ifield]->sweep[isweep]->h.sweep_num = uf_ma[9];
266 radar->v[ifield]->sweep[isweep]->h.elev = uf_ma[35] / 64.0;
271 current_fh_index = uf_dh[4+2*i];
272 uf_fh = uf + current_fh_index - 1;
273 sweep = radar->v[ifield]->sweep[isweep];
274 iray = sweep->h.nrays;
276 radar->v[ifield]->sweep[isweep]->ray[iray] = RSL_new_ray(nbins);
277 ray = radar->v[ifield]->sweep[isweep]->ray[iray];
283 * ---- Beginning of MANDATORY HEADER BLOCK.
285 ray->h.ray_num = uf_ma[7];
286 if (little_endian()) swap2(&uf_ma[10], 8);
287 memcpy(radar->h.radar_name, &uf_ma[10], 8);
288 if (little_endian()) swap2(&uf_ma[10], 8/2);
289 memcpy(radar->h.name, &uf_ma[14], 8);
290 if (little_endian()) swap2(&uf_ma[14], 8/2);
292 /* All components of lat/lon are the same sign. If not, then
293 * what ever wrote the UF was in error. A simple RSL program
294 * can repair the damage, however, not here.
296 ray->h.lat = uf_ma[18] + uf_ma[19]/60.0 + uf_ma[20]/64.0/3600;
297 ray->h.lon = uf_ma[21] + uf_ma[22]/60.0 + uf_ma[23]/64.0/3600;
298 ray->h.alt = uf_ma[24];
299 ray->h.year = uf_ma[25];
300 if (ray->h.year < 1900) {
302 if (ray->h.year < 1980) ray->h.year += 100; /* Year >= 2000. */
304 ray->h.month = uf_ma[26];
305 ray->h.day = uf_ma[27];
306 ray->h.hour = uf_ma[28];
307 ray->h.minute = uf_ma[29];
308 ray->h.sec = uf_ma[30];
309 ray->h.azimuth = uf_ma[32] / 64.0;
311 /* If Local Use Header is present and contains azimuth, use that
312 * azimuth for VR and SW. This is for WSR-88D, which runs separate
313 * scans for DZ and VR/SW at the lower elevations, which means DZ
314 * VR/SW and have different azimuths in the "same" ray.
316 len_lu = uf_ma[4] - uf_ma[3];
317 if (len_lu == 2 && (ifield == VR_INDEX || ifield == SW_INDEX)) {
318 if (strncmp((char *)uf_lu,"ZA",2) == 0 ||
319 strncmp((char *)uf_lu,"AZ",2) == 0)
320 ray->h.azimuth = uf_lu[1] / 64.0;
322 if (ray->h.azimuth < 0.) ray->h.azimuth += 360.; /* make it 0 to 360. */
323 ray->h.elev = uf_ma[33] / 64.0;
324 ray->h.elev_num = sweep->h.sweep_num;
325 ray->h.fix_angle = sweep->h.elev = uf_ma[35] / 64.0;
326 ray->h.azim_rate = uf_ma[36] / 64.0;
327 ray->h.sweep_rate = ray->h.azim_rate * (60.0/360.0);
328 missing_data = uf_ma[44];
330 if (pulled_time_from_first_ray == 0) {
331 radar->h.height = uf_ma[24];
332 radar->h.latd = uf_ma[18];
333 radar->h.latm = uf_ma[19];
334 radar->h.lats = uf_ma[20] / 64.0;
335 radar->h.lond = uf_ma[21];
336 radar->h.lonm = uf_ma[22];
337 radar->h.lons = uf_ma[23] / 64.0;
338 radar->h.year = ray->h.year;
339 radar->h.month = ray->h.month;
340 radar->h.day = ray->h.day;
341 radar->h.hour = ray->h.hour;
342 radar->h.minute = ray->h.minute;
343 radar->h.sec = ray->h.sec;
344 strcpy(radar->h.radar_type, "uf");
345 pulled_time_from_first_ray = 1;
348 * ---- End of MANDATORY HEADER BLOCK.
351 /* ---- Optional header used for MCTEX files. */
352 /* If this is a MCTEX file, the first 4 words following the
353 mandatory header contain the string 'MCTEX'. */
354 memcpy(proj_name, (short *)(uf + uf_ma[2] - 1), 8);
355 if (little_endian()) swap2(proj_name, 4);
358 /* ---- Local Use Header (if present) was checked during Mandatory
359 * Header processing above.
362 /* ---- Begining of FIELD HEADER. */
363 uf_fh = uf+current_fh_index - 1;
364 scale_factor = uf_fh[1];
365 ray->h.range_bin1 = uf_fh[2] * 1000.0 + uf_fh[3];
366 ray->h.gate_size = uf_fh[4];
368 ray->h.nbins = uf_fh[5];
369 ray->h.pulse_width = uf_fh[6]/(RSL_SPEED_OF_LIGHT/1.0e6);
371 if (strncmp((char *)proj_name, "MCTEX", 5) == 0) /* MCTEX? */
373 /* The beamwidth values are not correct in Mctex UF files. */
374 ray->h.beam_width = 1.0;
375 sweep->h.beam_width = ray->h.beam_width;
376 sweep->h.horz_half_bw = ray->h.beam_width/2.0;
377 sweep->h.vert_half_bw = ray->h.beam_width/2.0;
381 ray->h.beam_width = uf_fh[7] / 64.0;
382 sweep->h.beam_width = uf_fh[7] / 64.0;
383 sweep->h.horz_half_bw = uf_fh[7] / 128.0; /* DFF 4/4/95 */
384 sweep->h.vert_half_bw = uf_fh[8] / 128.0; /* DFF 4/4/95 */
386 /* fprintf (stderr, "uf_fh[7] = %d, [8] = %d\n", (int)uf_fh[7], (int)uf_fh[8]); */
387 if((int)uf_fh[7] == -32768) {
388 ray->h.beam_width = 1;
389 sweep->h.beam_width = 1;
390 sweep->h.horz_half_bw = .5;
391 sweep->h.vert_half_bw = .5;
394 ray->h.frequency = uf_fh[9] / 64.0;
395 ray->h.wavelength = uf_fh[11] / 64.0 / 100.0; /* cm to m. */
396 ray->h.pulse_count = uf_fh[12];
397 if (ifield == DZ_INDEX || ifield == ZT_INDEX) {
398 radar->v[ifield]->h.calibr_const = uf_fh[16] / 100.0;
399 /* uf value scaled by 100 */
402 radar->v[ifield]->h.calibr_const = 0.0;
404 if (uf_fh[17] == (short)UF_NO_DATA) x = 0;
405 else x = uf_fh[17] / 1000000.0; /* PRT in seconds. */
408 ray->h.unam_rng = RSL_SPEED_OF_LIGHT / (2.0 * ray->h.prf * 1000.0);
412 ray->h.unam_rng = 0.0;
415 if (VR_INDEX == ifield || VE_INDEX == ifield) {
416 ray->h.nyq_vel = uf_fh[19] / scale_factor;
419 /* ---- End of FIELD HEADER. */
424 /* ---- Begining of FIELD DATA. */
425 uf_data = uf+uf_fh[0] - 1;
427 len_data = ray->h.nbins; /* Known because of RSL_new_ray. */
428 for (m=0; m<len_data; m++) {
429 if (uf_data[m] == (short)UF_NO_DATA)
430 ray->range[m] = invf(BADVAL); /* BADVAL */
432 if(uf_data[m] == missing_data)
433 ray->range[m] = invf(NOECHO); /* NOECHO */
435 ray->range[m] = invf((float)uf_data[m]/scale_factor);
444 /*********************************************************************/
448 /* By: John Merritt */
449 /* Space Applications Corporation */
450 /* October 4, 1994 */
451 /*********************************************************************/
452 void swap_uf_buffer(UF_buffer uf)
456 addr_end = uf + sizeof(UF_buffer)/sizeof(short);
457 while (uf < addr_end)
461 enum UF_type {NOT_UF, TRUE_UF, TWO_BYTE_UF, FOUR_BYTE_UF};
464 /*********************************************************************/
466 /* RSL_uf_to_radar_fp */
468 /* By: John Merritt */
469 /* Space Applications Corporation */
470 /* September 22, 1995 */
471 /*********************************************************************/
472 Radar *RSL_uf_to_radar_fp(FILE *fp)
483 enum UF_type uf_type;
484 #define NEW_BUFSIZ 16384
488 setvbuf(fp,NULL,_IOFBF,(size_t)NEW_BUFSIZ); /* Faster i/o? */
489 if (fread(magic.buf, sizeof(char), 6, fp) <= 0) return NULL;
491 * Check for fortran record length delimeters, NCAR kludge.
493 if (strncmp("UF", magic.buf, 2) == 0) uf_type = TRUE_UF;
494 else if (strncmp("UF", &magic.buf[2], 2) == 0) uf_type = TWO_BYTE_UF;
495 else if (strncmp("UF", &magic.buf[4], 2) == 0) uf_type = FOUR_BYTE_UF;
496 else uf_type = NOT_UF;
500 if (radar_verbose_flag) fprintf(stderr,"UF file with 4 byte FORTRAN record delimeters.\n");
501 /* Handle first record specially, since we needed magic information. */
503 if (little_endian()) swap_4_bytes(&nbytes);
504 memcpy(uf, &magic.buf[4], 2);
505 (void)fread(&uf[1], sizeof(char), nbytes-2, fp);
506 if (little_endian()) swap_uf_buffer(uf);
507 (void)fread(&nbytes, sizeof(int), 1, fp);
508 if (uf_into_radar(uf, &radar) == UF_DONE) break;
509 /* Now the rest of the file. */
510 while(fread(&nbytes, sizeof(int), 1, fp) > 0) {
511 if (little_endian()) swap_4_bytes(&nbytes);
513 (void)fread(uf, sizeof(char), nbytes, fp);
514 if (little_endian()) swap_uf_buffer(uf);
516 (void)fread(&nbytes, sizeof(int), 1, fp);
518 if (uf_into_radar(uf, &radar) == UF_DONE) break;
523 if (radar_verbose_flag) fprintf(stderr,"UF file with 2 byte FORTRAN record delimeters.\n");
524 /* Handle first record specially, since we needed magic information. */
525 sbytes = magic.sword;
526 if (little_endian()) swap_2_bytes(&sbytes);
527 memcpy(uf, &magic.buf[2], 4);
528 (void)fread(&uf[2], sizeof(char), sbytes-4, fp);
529 if (little_endian()) swap_uf_buffer(uf);
530 (void)fread(&sbytes, sizeof(short), 1, fp);
531 uf_into_radar(uf, &radar);
532 /* Now the rest of the file. */
533 while(fread(&sbytes, sizeof(short), 1, fp) > 0) {
534 if (little_endian()) swap_2_bytes(&sbytes);
536 (void)fread(uf, sizeof(char), sbytes, fp);
537 if (little_endian()) swap_uf_buffer(uf);
539 (void)fread(&sbytes, sizeof(short), 1, fp);
541 if (uf_into_radar(uf, &radar) == UF_DONE) break;
546 if (radar_verbose_flag) fprintf(stderr,"UF file with no FORTRAN record delimeters. Good.\n");
547 /* Handle first record specially, since we needed magic information. */
548 memcpy(&sbytes, &magic.buf[2], 2); /* Record length is in word #2. */
549 if (little_endian()) swap_2_bytes(&sbytes); /* # of 2 byte words. */
551 memcpy(uf, &magic.buf[0], 6);
552 (void)fread(&uf[3], sizeof(short), sbytes-3, fp);
553 if (little_endian()) swap_uf_buffer(uf);
554 uf_into_radar(uf, &radar);
555 /* Now the rest of the file. */
556 while(fread(uf, sizeof(short), 2, fp) > 0) {
557 memcpy(&sbytes, &uf[1], 2); /* Record length is in word #2. */
558 if (little_endian()) swap_2_bytes(&sbytes);
560 (void)fread(&uf[2], sizeof(short), sbytes-2, fp); /* Have words 1,2. */
561 if (little_endian()) swap_uf_buffer(uf);
563 if (uf_into_radar(uf, &radar) == UF_DONE) break;
567 case NOT_UF: return NULL; break;
569 radar = reset_nsweeps_in_all_volumes(radar);
570 radar = RSL_prune_radar(radar);
576 /*********************************************************************/
578 /* RSL_uf_to_radar */
580 /* By: John Merritt */
581 /* Space Applications Corporation */
582 /* September 22, 1995 */
583 /*********************************************************************/
584 Radar *RSL_uf_to_radar(char *infile)
587 * This routine ingests a UF file and fills the Radar structure.
588 * This routine allocates space via the system routine malloc.
590 * If *infile is NULL, read from stdin.
596 if (infile == NULL) {
599 fp = fdopen(save_fd, "r");
600 } else if ((fp = fopen(infile, "r")) == NULL) {
604 fp = uncompress_pipe(fp); /* Transparently gunzip. */
605 radar = RSL_uf_to_radar_fp(fp);