]> Pileus Git - ~andy/rsl/blob - uf_to_radar.c
e87b68d4d1bfcee179e005115bc7b9e9bf94b8fa
[~andy/rsl] / uf_to_radar.c
1 /*
2     NASA/TRMM, Code 910.1.
3     This is the TRMM Office Radar Software Library.
4     Copyright (C) 1996, 1997
5             John H. Merritt
6             Space Applications Corporation
7             Vienna, Virginia
8
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.
13
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.
18
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.
22 */
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27
28 /* This allows us to use RSL_ftype, RSL_f_list, RSL_invf_list from rsl.h. */
29 #define USE_RSL_VARS
30 #include "rsl.h"
31
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.
36                                  */
37
38 #define UF_MORE 0
39 #define UF_DONE 1
40
41 static float (*f)(Range x);
42 static Range (*invf)(float x);
43
44 Volume *reset_nsweeps_in_volume(Volume *volume)
45 {
46   int i;
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;
51       break;
52     }
53   return volume;
54 }
55 Radar *reset_nsweeps_in_all_volumes(Radar *radar)
56 {
57   int i;
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]);
61   return radar;
62 }
63
64 Volume *copy_sweeps_into_volume(Volume *new_volume, Volume *old_volume)
65 {
66   int i;
67   int nsweeps;
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);
77   return new_volume;
78 }
79
80 void swap2(short *buf, int n)
81 {
82   short *end_addr;
83   end_addr = buf + n;
84   while (buf < end_addr) {
85     swap_2_bytes(buf);
86     buf++;
87   }
88 }
89
90
91 /********************************************************************/
92 /*********************************************************************/
93 /*                                                                   */
94 /*                  uf_into_radar                                    */
95 /*                                                                   */
96 /*  By: John Merritt                                                 */
97 /*      Space Applications Corporation                               */
98 /*      August 26, 1994                                              */
99 /*********************************************************************/
100 int uf_into_radar(UF_buffer uf, Radar **the_radar)
101 {
102   
103 /* Missing data flag : -32768 when a signed short. */
104 #define UF_NO_DATA 0X8000
105   
106   /* Any convensions may be observed, however, Radial Velocity must be VE. */
107   /* Typically:
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.
115    *
116    * These fields may appear in any order in the UF file.
117    *
118    * RETURN:
119    *   UF_DONE if we're done with the UF ingest.
120    *   UF_MORE if we need more UF data.
121    */
122   
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
127    * buffer.
128    */
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. */
134   
135   /* The length of each header. */
136   int len_data, len_lu;
137   
138   int current_fh_index; 
139   float scale_factor;
140   
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. */
145   short proj_name[4];
146   Ray *ray;
147   Sweep *sweep;
148   Radar *radar;
149   float x;
150   short missing_data;
151   Volume *new_volume;
152   int nbins;
153   extern int rsl_qfield[];
154   extern int *rsl_qsweep; /* See RSL_read_these_sweeps in volume.c */
155   extern int rsl_qsweep_max;
156
157   radar = *the_radar;
158
159 /*
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).
163  */
164
165
166 /* Set up all the UF pointers. */
167   uf_ma = uf;
168   uf_lu = uf + uf_ma[3] - 1;
169   uf_dh = uf + uf_ma[4] - 1;
170
171   nfields =  uf_dh[0];
172   isweep = uf_ma[9] - 1;
173
174   if (rsl_qsweep != NULL) {
175     if (isweep > rsl_qsweep_max) return UF_DONE;
176     if (rsl_qsweep[isweep] == 0) return UF_MORE;
177   }
178
179
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.
186  */
187 #ifdef STICKYSOLVED
188   if (radar == NULL) radar = RSL_new_radar(nfields);
189   /* Sticky solution here. */
190 #else
191   if (radar == NULL) {
192     radar = RSL_new_radar(MAX_RADAR_VOLUMES);
193     *the_radar = radar;
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);
198   }
199   
200 #endif
201
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;
206     else {
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;
211     }
212     need_scan_mode = 0;
213   }
214
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.
220  */
221
222   for (i=0; i<nfields; i++) {
223     if (little_endian()) swap_2_bytes(&uf_dh[3+2*i]); /* Unswap. */
224     ifield = -1;
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) {
228         ifield = j;
229         break;
230       }
231     }
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]);
235       continue;
236     }
237
238     f = RSL_f_list[ifield];
239     invf = RSL_invf_list[ifield];
240
241     /* Do we place the data into this volume? */
242     if (radar->v[ifield] == NULL) continue; /* Nope. */
243
244     if (isweep >= radar->v[ifield]->h.nsweeps) { /* Exceeded sweep limit.
245                                                   * Allocate more sweeps.
246                                                   * Copy all previous sweeps.
247                                                   */
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;
253     }
254
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
260                                                      * ray encountered.
261                                                      */
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;
268     }
269     
270     
271
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;
276     nbins = uf_fh[5];
277     radar->v[ifield]->sweep[isweep]->ray[iray] = RSL_new_ray(nbins);
278     ray   = radar->v[ifield]->sweep[isweep]->ray[iray];
279     sweep->h.nrays += 1;
280
281
282     if (ray) {
283         /* 
284          * ---- Beginning of MANDATORY HEADER BLOCK.
285          */
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);
292         
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.
296        */
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) {
302         ray->h.year += 1900;
303         if (ray->h.year < 1980) ray->h.year += 100; /* Year >= 2000. */
304       }
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;
311
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.
316        */
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;
322       }
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];
330
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;
347       }
348       /*
349        * ---- End of MANDATORY HEADER BLOCK.
350        */
351       
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);
357
358
359       /* ---- Local Use Header (if present) was checked during Mandatory
360        *      Header processing above.
361        */
362       
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];
368
369       ray->h.nbins      = uf_fh[5];
370       ray->h.pulse_width  = uf_fh[6]/(RSL_SPEED_OF_LIGHT/1.0e6);
371
372         if (strncmp((char *)proj_name, "MCTEX", 5) == 0)  /* MCTEX? */
373         {
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;
379         }
380         else  /* Not MCTEX */
381         {
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 */
386         }           
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;
393         }
394           
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 */
401       }
402       else {
403         radar->v[ifield]->h.calibr_const  = 0.0;
404       }
405       if (uf_fh[17] == (short)UF_NO_DATA) x = 0;
406       else x = uf_fh[17] / 1000000.0;  /* PRT in seconds. */
407       if (x != 0) {
408         ray->h.prf = 1/x;
409         ray->h.unam_rng = RSL_SPEED_OF_LIGHT / (2.0 * ray->h.prf * 1000.0);
410       }
411       else {
412         ray->h.prf = 0.0;
413         ray->h.unam_rng = 0.0;
414       }
415
416       if (VR_INDEX == ifield || VE_INDEX == ifield) {
417         ray->h.nyq_vel = uf_fh[19] / scale_factor;
418       }
419       
420       /* ---- End of FIELD HEADER. */
421       
422       ray->h.f = f;
423       ray->h.invf = invf;
424
425       /* ---- Begining of FIELD DATA. */
426       uf_data = uf+uf_fh[0] - 1;
427
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 */
432         else {
433           if(uf_data[m] == missing_data)
434             ray->range[m] = invf(NOECHO); /* NOECHO */
435           else
436             ray->range[m] = invf((float)uf_data[m]/scale_factor);
437         }
438       }
439     }
440   }
441   return UF_MORE;
442 }
443
444
445 /*********************************************************************/
446 /*                                                                   */
447 /*                  swap_uf_buffer                                   */
448 /*                                                                   */
449 /*  By: John Merritt                                                 */
450 /*      Space Applications Corporation                               */
451 /*      October 4, 1994                                              */
452 /*********************************************************************/
453 void swap_uf_buffer(UF_buffer uf)
454 {
455   short *addr_end;
456
457   addr_end = uf + sizeof(UF_buffer)/sizeof(short);
458   while (uf < addr_end)
459     swap_2_bytes(uf++);
460 }
461
462 enum UF_type {NOT_UF, TRUE_UF, TWO_BYTE_UF, FOUR_BYTE_UF};
463
464
465 /*********************************************************************/
466 /*                                                                   */
467 /*                  RSL_uf_to_radar_fp                               */
468 /*                                                                   */
469 /*  By: John Merritt                                                 */
470 /*      Space Applications Corporation                               */
471 /*      September 22, 1995                                           */
472 /*********************************************************************/
473 Radar *RSL_uf_to_radar_fp(FILE *fp)
474 {
475   union {
476     char buf[6];
477     short sword;
478     int word;
479   } magic;
480   Radar *radar;
481   int nbytes;
482   short sbytes;
483   UF_buffer uf;
484   enum UF_type uf_type;
485 #define NEW_BUFSIZ 16384
486
487
488   radar = NULL;
489   /* setvbuf(fp,NULL,_IOFBF,(size_t)NEW_BUFSIZ); * Faster i/o? */
490   if (fread(magic.buf, sizeof(char), 6, fp) <= 0) return NULL;
491 /*
492  * Check for fortran record length delimeters, NCAR kludge.
493  */
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;
498   
499   switch (uf_type) {
500   case FOUR_BYTE_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. */
503     nbytes = magic.word;
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);
513       
514       (void)fread(uf, sizeof(char), nbytes, fp);
515       if (little_endian()) swap_uf_buffer(uf);
516       
517       (void)fread(&nbytes, sizeof(int), 1, fp);
518       
519       if (uf_into_radar(uf, &radar) == UF_DONE) break;
520     }
521     break;
522
523   case TWO_BYTE_UF:
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);
536       
537       (void)fread(uf, sizeof(char), sbytes, fp);
538       if (little_endian()) swap_uf_buffer(uf);
539       
540       (void)fread(&sbytes, sizeof(short), 1, fp);
541       
542       if (uf_into_radar(uf, &radar) == UF_DONE) break;
543     }
544     break;
545
546   case TRUE_UF:
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. */
551
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);
560       
561       (void)fread(&uf[2], sizeof(short), sbytes-2, fp);  /* Have words 1,2. */
562       if (little_endian()) swap_uf_buffer(uf);
563       
564       if (uf_into_radar(uf, &radar) == UF_DONE) break;
565     }
566     break;
567     
568   case NOT_UF: return NULL; break;
569   }
570   radar = reset_nsweeps_in_all_volumes(radar);
571   radar = RSL_prune_radar(radar);
572
573   return radar;
574 }
575
576
577 /*********************************************************************/
578 /*                                                                   */
579 /*                  RSL_uf_to_radar                                  */
580 /*                                                                   */
581 /*  By: John Merritt                                                 */
582 /*      Space Applications Corporation                               */
583 /*      September 22, 1995                                           */
584 /*********************************************************************/
585 Radar *RSL_uf_to_radar(char *infile)
586 {
587 /*
588  * This routine ingests a UF file and fills the Radar structure.
589  * This routine allocates space via the system routine malloc.
590  *
591  * If *infile is NULL, read from stdin.
592  */
593   FILE *fp;
594   Radar *radar;
595   
596   radar = NULL;
597   if (infile == NULL) {
598     int save_fd;
599     save_fd = dup(0);
600     fp = fdopen(save_fd, "r");
601   }  else if ((fp = fopen(infile, "r")) == NULL) {
602     perror(infile);
603     return radar;
604   }
605   fp = uncompress_pipe(fp); /* Transparently gunzip. */
606   radar = RSL_uf_to_radar_fp(fp);
607   rsl_pclose(fp);
608     
609   return radar;
610 }