]> Pileus Git - ~andy/rsl/blob - radar_to_uf.c
RSL v1.43
[~andy/rsl] / radar_to_uf.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 <time.h>
26 #include <stdlib.h>
27
28 #define USE_RSL_VARS
29 #include "rsl.h"
30 extern int radar_verbose_flag;
31 /* Missing data flag : -32768 when a signed short. */
32 #define UF_NO_DATA 0X8000
33
34
35 /* Field names. Any convensions may be observed. */
36 /* Typically:
37  *    DZ = Reflectivity (dBZ).
38  *    VR = Radial Velocity.
39  *    SW = Spectrum Width.
40  *    CZ = Corrected Reflectivity. (Quality controlled: AP removed, etc.)
41  *    ZT = Total Reflectivity (dB(mW)).  Becomes UZ in UF files.
42  *    DR = Differential Reflectivity.
43  *    LR = Another DR (LDR).
44  *    ZD = Tina Johnson use this one.
45  *    DM = Received power.
46  *    RH = Rho coefficient.
47  *    PH = Phi (MCTEX parameter).
48  *    XZ = X-band reflectivity.
49  *    CD = Corrected ZD.
50  *    MZ = DZ mask for 1C-51 HDF.
51  *    MD = ZD mask for 1C-51 HDF.
52  *    ZE = Edited reflectivity.
53  *    VE = Edited velocity.
54  *    KD = KDP wavelength*deg/km
55  *    TI = TIME (units unknown).
56  * These fields may appear in any order in the UF file.
57  * There are more fields than appear here.  See rsl.h.
58  */
59
60
61 /* Changed old buffer size (16384) for larger dualpol files.  BLK 5/20/2011 */
62 typedef short UF_buffer[20000]; /* Bigger than documented 4096. */
63
64 void swap_uf_buffer(UF_buffer uf);
65 void swap2(short *buf, int n);
66
67 /**********************************************************************/
68 /*                                                                    */
69 /*                     RSL_radar_to_uf_fp                             */
70 /*                                                                    */
71 /*  By: John Merritt                                                  */
72 /*      Space Applications Corporation                                */
73 /*      May  20, 1994                                                 */
74 /**********************************************************************/
75 void RSL_radar_to_uf_fp(Radar *r, FILE *fp)
76
77 {
78   /*
79    * 1. Fill the UF buffers with data from the Radar structure.
80    * 2. Write to a stream.  Assume open and leave it so.
81    */
82
83
84   UF_buffer uf;
85
86 /* These are pointers to various locations within the UF buffer 'uf'.
87  * They are used to index the different components of the UF structure in
88  * a manor consistant with the UF documentation.  For instance, uf_ma[1]
89  * will be equivalenced to the second word (2 bytes/each) of the UF
90  * buffer.
91  */
92   short *uf_ma;  /* Mandatory header block. */
93   short *uf_op;  /* Optional header block.  */
94   short *uf_lu;  /* Local Use header block.  */
95   short *uf_dh;  /* Data header.  */
96   short *uf_fh;  /* Field header. */
97   short *uf_data; /* Data. */
98
99 /* The length of each header. */
100   int len_ma, len_op, len_lu, len_dh, len_fh, len_data;
101
102 /* Booleans to flag inclusion of headers. */
103   int q_op, q_lu, q_dh, q_fh;
104
105   int current_fh_index; 
106   int scale_factor;
107   int rec_len, save_rec_len;
108   int nfield;
109   float vr_az;
110   int max_field_names;
111
112   struct tm *tm;
113   time_t the_time;
114
115   int i,j,k,m;
116   int degree, minute;
117   float second;
118
119   int uf_sweep_mode = 1;  /* default PPI */
120
121 /* Here are the arrays for each field type.  Each dimension is the number
122  * of fields in the radar structure.  I do this because the radar organization
123  * is by volumes (field types) and the UF demands that each ray contain
124  * all the field types.
125  */
126   Volume **volume;
127   Sweep  **sweep;
128   Ray     *ray;
129   int     *nsweeps;
130   int nvolumes, maxsweeps, nrays;
131   int true_nvolumes;
132   int sweep_num, ray_num, rec_num;
133   float x;
134
135   if (r == NULL) {
136     fprintf(stderr, "radar_to_uf_fp: radar pointer NULL\n");
137     return;
138   }
139
140 /* Do all the headers first time around.  Then, prune OP and LU. */
141   q_op = q_lu = q_dh = q_fh = 1;
142
143   memset(&uf, 0, sizeof(uf)); /* Init to 0 or NULL for pointers. */
144
145   sweep_num = ray_num = rec_num = 0;
146   true_nvolumes = nvolumes = maxsweeps = nrays = 0;
147
148   /* PPI and RHI are enum constants defined in rsl.h */
149   if (r->h.scan_mode == PPI) uf_sweep_mode = 1;
150   else if (r->h.scan_mode == RHI) uf_sweep_mode = 3;
151
152 /*
153  * The organization of the Radar structure is by volumes, then sweeps, then
154  * rays, then gates.  This is different from the UF file organization.
155  * The UF format wants sweeps, rays, then gates for all field types (volumes).
156  * So, we have to do a back flip, here.  This is achieved by maintaining 
157  * an array of volume pointers and sweep pointers, each dimensioned by 
158  * 'nvolumes', which contains the data for the different field types; this
159  * is our innermost loop.  The variables are 'volume[i]' and 'sweep[i]' where
160  * 'i' is the volume index.
161  *
162  * In other words, we are getting all the field types together, when we
163  * are looping on the number of rays in a sweep, so we can load the UF_buffer
164  * appropriately.
165  */
166
167   nvolumes = r->h.nvolumes;
168   volume   = (Volume **) calloc(nvolumes, sizeof(Volume *));
169   sweep    = (Sweep  **) calloc(nvolumes, sizeof(Sweep  *));
170   nsweeps  = (int *)     calloc(nvolumes, sizeof(int));
171
172 /* Get the the number of sweeps in the radar structure.  This will be
173  * the main controlling loop variable.
174  */
175   for (i=0; i<nvolumes; i++) {
176     volume[i] = r->v[i];
177     if(volume[i]) {
178       nsweeps[i] = volume[i]->h.nsweeps;
179       if (nsweeps[i] > maxsweeps) maxsweeps = nsweeps[i];
180       true_nvolumes++;
181     }
182   }
183
184   if (radar_verbose_flag) {
185     fprintf(stderr,"True number of volumes for UF is %d\n", true_nvolumes);
186     fprintf(stderr,"Maximum #   of volumes for UF is %d\n", nvolumes);
187   }
188
189   max_field_names = sizeof(RSL_ftype) / 4;
190
191 /*--------
192  *   LOOP for all sweeps (typically 11 or 16 for wsr88d data.
193  *
194  */
195   for (i=0; i<maxsweeps; i++) {
196     /* Get the array of volume and sweep pointers; one for each field type. */
197     nrays = 0;
198     for (k=0; k<nvolumes; k++) {
199       if (volume[k]) sweep[k] = volume[k]->sweep[i];
200       
201       /* Check if we really can access this sweep.  Paul discovered that
202        * if the actual number of sweeps is less than the maximum that we
203        * could be chasing a bad pointer (a NON-NULL garbage pointer).
204        */
205       if (i >= nsweeps[k]) sweep[k] = NULL;
206
207       if (sweep[k]) if (sweep[k]->h.nrays > nrays) nrays = sweep[k]->h.nrays;
208     }
209
210     sweep_num++;  /* I guess it will be ok to count NULL sweeps. */
211     ray_num = 0;
212   if (radar_verbose_flag) 
213     fprintf(stderr,"Processing sweep %d for %d rays.", i, nrays);
214   if (radar_verbose_flag)
215     if (little_endian()) fprintf(stderr," ... On Little endian.\n");
216     else fprintf(stderr,"\n");
217
218
219 /* Now LOOP for all rays within this particular sweep (i).
220  *    Get all the field types together for the ray, see ray[k], and
221  *    fill the UF data buffer appropriately.
222  */
223     for (j=0; j<nrays; j++) {
224       memset(uf, 0, sizeof(uf));
225       nfield = 0;
226       ray_num++;  /* And counting, possibly, NULL rays. */
227       current_fh_index = 0;
228
229
230       /* Find any ray for header information. It does not matter which
231        * ray, since the information for the MANDITORY, OPTIONAL, and LOCAL
232        * USE headers is common to any field type ray.
233        */
234       ray = NULL;
235       for (k=0; k<nvolumes; k++) {
236         if (sweep[k])
237           if (j < sweep[k]->h.nrays)
238             if (sweep[k]->ray)
239               if ((ray = sweep[k]->ray[j])) break;
240       }
241
242       /* If there is no such ray, then continue on to the next ray. */
243       if (ray) {
244 /*
245                   fprintf(stderr,"Ray: %.4d, Time: %2.2d:%2.2d:%f  %.2d/%.2d/%.4d\n", ray_num, ray->h.hour, ray->h.minute, ray->h.sec, ray->h.month, ray->h.day, ray->h.year);
246 */
247
248         /* 
249          * ---- Begining of MANDITORY HEADER BLOCK.
250          */
251         uf_ma = uf;
252         memcpy(&uf_ma[0], "UF", 2);
253         if (little_endian()) memcpy(&uf_ma[0], "FU", 2);
254         uf_ma[1]  = 0;  /* Not known yet. */
255         uf_ma[2]  = 0;  /* Not known yet. Really, I do. */
256         uf_ma[3]  = 0;  /* Not known yet. */
257         uf_ma[4]  = 0;  /* Not known yet. */
258
259         uf_ma[6]  = 1;
260         uf_ma[7]  = ray_num;
261         uf_ma[8 ] = 1;
262         uf_ma[9 ] = sweep_num;
263         memcpy(&uf_ma[10], r->h.radar_name, 8);
264         if (little_endian()) swap2(&uf_ma[10], 8/2);
265         memcpy(&uf_ma[14], r->h.name, 8);
266         if (little_endian()) swap2(&uf_ma[14], 8/2);
267         /* Convert decimal lat/lon to d:m:s */
268
269         if (ray->h.lat != 0.0) {
270           degree = (int)ray->h.lat;
271           minute = (int)((ray->h.lat - degree) * 60);
272           second = (ray->h.lat - degree - minute/60.0) * 3600.0;
273         } else {
274           degree = r->h.latd;
275           minute = r->h.latm;
276           second = r->h.lats;
277         }
278         uf_ma[18] = degree;
279         uf_ma[19] = minute;
280         if (second > 0.0) uf_ma[20] = second*64 + 0.5;
281         else uf_ma[20] = second*64 - 0.5;
282
283         if (ray->h.lon != 0.0) {
284           degree = (int)ray->h.lon;
285           minute = (int)((ray->h.lon - degree) * 60);
286           second = (ray->h.lon - degree - minute/60.0) * 3600.0;
287         } else {
288           degree = r->h.lond;
289           minute = r->h.lonm;
290           second = r->h.lons;
291         }
292         uf_ma[21] = degree;
293         uf_ma[22] = minute;
294         if (second > 0.0) uf_ma[23] = second*64 + 0.5;
295         else uf_ma[23] = second*64 - 0.5;
296         if (ray->h.alt != 0) 
297           uf_ma[24] = ray->h.alt;
298         else
299           uf_ma[24] = r->h.height;
300
301         uf_ma[25] = ray->h.year % 100; /* By definition: not year 2000 compliant. */
302         uf_ma[26] = ray->h.month;
303         uf_ma[27] = ray->h.day;
304         uf_ma[28] = ray->h.hour;
305         uf_ma[29] = ray->h.minute;
306         uf_ma[30] = ray->h.sec;
307         memcpy(&uf_ma[31], "UT", 2);
308         if (little_endian()) memcpy(&uf_ma[31], "TU", 2);
309         if (ray->h.azimuth > 0) uf_ma[32] = ray->h.azimuth*64 + 0.5;
310         else uf_ma[32] = ray->h.azimuth*64 - 0.5;
311         uf_ma[33] = ray->h.elev*64 + 0.5;
312         uf_ma[34] = uf_sweep_mode;
313         if (ray->h.fix_angle != 0.)
314              uf_ma[35] = ray->h.fix_angle*64.0 + 0.5;
315         else uf_ma[35] = sweep[k]->h.elev*64.0 + 0.5;
316         uf_ma[36] = ray->h.sweep_rate*(360.0/60.0)*64.0 + 0.5;
317         
318         the_time = time(NULL);
319         tm = gmtime(&the_time);
320         
321         uf_ma[37] = tm->tm_year % 100; /* Same format as data year */
322         uf_ma[38] = tm->tm_mon+1;
323         uf_ma[39] = tm->tm_mday;
324         memcpy(&uf_ma[40], "RSL" RSL_VERSION_STR, 8);
325         if (little_endian()) swap2(&uf_ma[40], 8/2);
326         uf_ma[44] = (signed short)UF_NO_DATA;
327         len_ma = 45;
328         uf_ma[2] = len_ma+1;
329         /*
330          * ---- End of MANDITORY HEADER BLOCK.
331          */
332         
333         /* ---- Begining of OPTIONAL HEADER BLOCK. */
334         len_op = 0;
335         if (q_op) {
336           q_op = 0;  /* Only once. */
337           uf_op = uf+len_ma;
338           memcpy(&uf_op[0], "TRMMGVUF", 8);
339           if (little_endian()) swap2(&uf_op[0], 8/2);
340           uf_op[4] = (signed short)UF_NO_DATA;
341           uf_op[5] = (signed short)UF_NO_DATA;
342           uf_op[6] = ray->h.hour;
343           uf_op[7] = ray->h.minute;
344           uf_op[8] = ray->h.sec;
345           memcpy(&uf_op[9], "RADAR_UF", 8);
346           if (little_endian()) swap2(&uf_op[9], 8/2);
347           uf_op[13] = 2;
348           len_op = 14;
349         }
350         /* ---- End of OPTIONAL HEADER BLOCK. */
351         
352         /* ---- Begining of LOCAL USE HEADER BLOCK. */
353         q_lu = 0;
354
355         /* TODO: Code within "#ifdef LUHDR_VR_AZ" below should be removed
356          * once testing of merge_split_cuts is completed.
357          */
358 /* 7/25/2011 There's really no need to remove this code after merge_split_cuts.
359  * We can continue using it for exact VR azimuth in RSL.  Non-RSL programs can
360  * use the mandatory header azimuth, which is exact for DZ and a close
361  * approximation for VR when merge-split-cuts is applied.
362  */
363 /* Preprocessor directives are for testing. */
364 #define LUHDR_VR_AZ
365 #ifdef LUHDR_VR_AZ
366         /* If DZ and VR azimuths are different, store VR azimuth in Local Use
367          * Header. This is done for WSR-88D split cuts.
368          */
369         if (sweep[DZ_INDEX] && sweep[VR_INDEX]) {
370             if (sweep[DZ_INDEX]->ray[j] && sweep[VR_INDEX]->ray[j]) {
371             vr_az = sweep[VR_INDEX]->ray[j]->h.azimuth;
372             if (sweep[DZ_INDEX]->ray[j]->h.azimuth != vr_az)
373                 q_lu = 1; /* Set to use Local Use Header block. */
374             }
375         }
376 #endif
377         len_lu = 0;
378         if (q_lu) {
379           /* Store azimuth for WSR-88D VR ray in Local Use Header. */
380           uf_lu = uf+len_ma+len_op;
381           memcpy(&uf_lu[0], "AZ", 2);
382           if (little_endian()) memcpy(&uf_lu[0], "ZA", 2);
383           if (vr_az > 0) uf_lu[1] = vr_az*64 + 0.5;
384           else uf_lu[1] = vr_az*64 - 0.5;
385           len_lu = 2;
386         }
387         /* ---- End  of LOCAL USE HEADER BLOCK. */
388
389
390        /* Here is where we loop on each field type.  We need to keep
391         * track of how many FIELD HEADER and FIELD DATA sections, one
392         * for each field type, we fill.  The variable that tracks this
393         * index into 'uf' is 'current_fh_index'.  It is bumped by
394         * the length of the FIELD HEADER and FIELD DATA for each field
395         * type encountered.  Field types expected are: Reflectivity,
396         * Velocity, and Spectrum width; this is a typicial list but it
397         * is not restricted to it.
398         */
399         
400          for (k=0; k<nvolumes; k++) {
401           if (sweep[k])
402             if (j < sweep[k]->h.nrays && sweep[k]->ray[j])
403               ray = sweep[k]->ray[j];
404             else
405               ray = NULL;
406           else ray = NULL;
407
408           if (ray) {
409             /* ---- Begining of DATA HEADER. */
410             nfield++;
411             if (q_dh) {
412               len_dh = 2*true_nvolumes + 3;
413               uf_dh = uf+len_ma+len_op+len_lu;
414               uf_dh[0] = nfield;
415               uf_dh[1] = 1;
416               uf_dh[2] = nfield;
417               /* 'nfield' indexes the field number.
418                * 'k' indexes the particular field from the volume.
419                *  RSL_ftype contains field names and is defined in rsl.h.
420                */
421               if (k > max_field_names-1) {
422                 fprintf(stderr,
423                   "RSL_uf_to_radar: No field name for volume index %d\n", k);
424                 fprintf(stderr,"RSL_ftype must be updated in rsl.h for new field.\n");
425                 fprintf(stderr,"Quitting now.\n");
426                 return;
427               }
428               memcpy(&uf_dh[3+2*(nfield-1)], RSL_ftype[k], 2);
429               if (little_endian()) swap2(&uf_dh[3+2*(nfield-1)], 2/2);
430               if (current_fh_index == 0) current_fh_index = len_ma+len_op+len_lu+len_dh;
431               uf_dh[4+2*(nfield-1)] = current_fh_index + 1;
432             }
433             /* ---- End of DATA HEADER. */
434             
435             /* ---- Begining of FIELD HEADER. */
436             if (q_fh) {
437               uf_fh = uf+current_fh_index;
438               if (k != PH_INDEX) scale_factor = 100;
439               else scale_factor = 10;
440               uf_fh[1] = scale_factor;
441               uf_fh[2] = ray->h.range_bin1/1000.0;
442               uf_fh[3] = ray->h.range_bin1 - (1000*uf_fh[2]);
443               uf_fh[4] = ray->h.gate_size;
444               uf_fh[5] = ray->h.nbins;
445               uf_fh[6] = ray->h.pulse_width*(RSL_SPEED_OF_LIGHT/1.0e6);
446               uf_fh[7] = sweep[k]->h.beam_width*64.0 + 0.5;
447               uf_fh[8] = sweep[k]->h.beam_width*64.0 + 0.5;
448               uf_fh[9] = ray->h.frequency*64.0 + 0.5; /* Bandwidth (mHz). */
449               uf_fh[10] = 0; /* Horizontal polarization. */ 
450               uf_fh[11] = ray->h.wavelength*64.0*100.0; /* m to cm. */
451               uf_fh[12] = ray->h.pulse_count;
452               memcpy(&uf_fh[13], "  ", 2);
453               uf_fh[14] = (signed short)UF_NO_DATA;
454               uf_fh[15] = (signed short)UF_NO_DATA;
455               if (k == DZ_INDEX || k == ZT_INDEX) {
456                 uf_fh[16] = volume[k]->h.calibr_const*100.0 + 0.5;
457               }
458               else {
459                 memcpy(&uf_fh[16], "  ", 2);
460               }
461               if (ray->h.prf != 0)
462                 uf_fh[17] = 1.0/ray->h.prf*1000000.0; /* Pulse repetition time(msec)  = 1/prf */
463               else
464                 uf_fh[17] = (signed short)UF_NO_DATA; /* Pulse repetition time  = 1/prf */
465               uf_fh[18] = 16;
466               if (VR_INDEX == k || VE_INDEX == k) {
467                 uf_fh[19] = scale_factor*ray->h.nyq_vel;
468                 uf_fh[20] = 1;
469                 len_fh = 21;
470               } else {
471                 len_fh = 19;
472               }
473               
474               uf_fh[0] = current_fh_index + len_fh + 1;
475               /* ---- End of FIELD HEADER. */
476               
477               /* ---- Begining of FIELD DATA. */
478               uf_data = uf+len_fh+current_fh_index;
479               len_data = ray->h.nbins;
480               for (m=0; m<len_data; m++) {
481                 x = ray->h.f(ray->range[m]);
482                 if (x == BADVAL || x == RFVAL || x == APFLAG || x == NOECHO)
483                   uf_data[m] = (signed short)UF_NO_DATA;
484                 else
485                   uf_data[m] = scale_factor * x;
486               }
487               
488               current_fh_index += (len_fh+len_data);
489             }
490           }
491         /* ---- End of FIELD DATA. */
492         }
493         /* Fill in some infomation we didn't know.  Like, buffer length,
494          * record number, etc.
495          */
496         rec_num++;
497         uf_ma[1] = current_fh_index;
498         uf_ma[3] = len_ma + len_op + 1;
499         uf_ma[4] = len_ma + len_op + len_lu + 1;
500         uf_ma[5] = rec_num;
501         
502         /* WRITE the UF buffer. */
503         rec_len =(int)uf_ma[1]*2;
504         save_rec_len = rec_len;  /* We destroy 'rec_len' when making it
505                         big endian on a little endian machine. */
506         if (little_endian()) swap_4_bytes(&rec_len);
507         (void)fwrite(&rec_len, sizeof(int), 1, fp);
508         if (little_endian()) swap_uf_buffer(uf);
509         (void)fwrite(uf, sizeof(char), save_rec_len, fp);
510         (void)fwrite(&rec_len, sizeof(int), 1, fp);
511       } /* if (ray) */
512     }
513   }
514 }
515
516 /**********************************************************************/
517 /*                                                                    */
518 /*                     RSL_radar_to_uf                                */
519 /*                                                                    */
520 /**********************************************************************/
521 void RSL_radar_to_uf(Radar *r, char *outfile)
522 {
523   FILE *fp;
524   if (r == NULL) {
525     fprintf(stderr, "radar_to_uf: radar pointer NULL\n");
526     return;
527   }
528
529   if ((fp = fopen(outfile, "w")) == NULL) {
530     perror(outfile);
531     return;
532   }
533
534   RSL_radar_to_uf_fp(r, fp);
535   fclose(fp);
536 }
537
538 /**********************************************************************/
539 /*                                                                    */
540 /*                     RSL_radar_to_uf_gzip                           */
541 /*                                                                    */
542 /**********************************************************************/
543 void RSL_radar_to_uf_gzip(Radar *r, char *outfile)
544 {
545   FILE *fp;
546   if (r == NULL) {
547     fprintf(stderr, "radar_to_uf_gzip: radar pointer NULL\n");
548     return;
549   }
550
551   if ((fp = fopen(outfile, "w")) == NULL) {
552     perror(outfile);
553     return;
554   }
555
556   fp = compress_pipe(fp);
557   RSL_radar_to_uf_fp(r, fp);
558   rsl_pclose(fp);
559 }