]> Pileus Git - ~andy/rsl/blob - nsig_to_radar.c
RSL v1.42
[~andy/rsl] / nsig_to_radar.c
1 /*
2     NASA/TRMM, Code 910.1.
3     This is the TRMM Office Radar Software Library.
4     Copyright (C) 1996  Paul A. Kucera of Applied Research Corporation,
5                         Landover, Maryland, a NASA/GSFC on-site contractor.
6
7     This library is free software; you can redistribute it and/or
8     modify it under the terms of the GNU Library General Public
9     License as published by the Free Software Foundation; either
10     version 2 of the License, or (at your option) any later version.
11
12     This library is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15     Library General Public License for more details.
16
17     You should have received a copy of the GNU Library General Public
18     License along with this library; if not, write to the Free
19     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20         */
21 /*************************************************************/
22 /*                                                           */
23 /*    Function: nsig_to_radar.c                              */
24 /*                                                           */
25 /*    Paul A. Kucera                                         */
26 /*    Applied Research Corporation                           */
27 /*    NASA/GSFC                                              */
28 /*    TRMM/Code 910.1                                        */
29 /*                                                           */
30 /*    Modifications by:                                      */
31 /*    John H. Merritt                                        */
32 /*    Space Applications Corporation                         */
33 /*    NASA/GSFC                                              */
34 /*    TRMM/Code 910.1                                        */
35 /*                                                           */
36 /*    Started: 08 AUG 96                                     */
37 /*                                                           */
38 /*    Derived from Paul Kucera's nsig_to_radar.c             */
39 /*    Copyright 1996                                         */
40 /*************************************************************/
41
42 #include<stdio.h>
43 #include<unistd.h>
44 #include<sys/types.h>
45 #include<sys/stat.h>
46 #include<fcntl.h>
47 #include<stdlib.h>
48 #include<math.h>
49 #include<string.h>
50
51 #include"nsig.h"
52 #include"rsl.h"
53
54 extern int radar_verbose_flag;
55 extern int rsl_qfield[]; /* See RSL_select_fields */
56
57    /*  We need this entry for various things esp in Ray_header  */
58 #define MISSING_HEADER_DATA -9999
59    /*  The following is speed of light _in_ _air_ !  */
60 #define SPEED_OF_LIGHT 299702547
61 #define MIT_BEAMWIDTH      1.65
62 #define TOG_BEAMWIDTH      1.65
63 #define KWA_BEAMWIDTH      1.0
64 #define DEFAULT_BEAMWIDTH  1.0
65 #define NSIG_NO_DATA       -1
66 #define MAX_NSIG_SWEEPS    30
67 #define MAX_NSIG_RAYS      400
68 #define NSIG_NO_ECHO       -32.0
69 #define NSIG_NO_ECHO2     -999.0
70
71 static float (*f)(Range x);
72 static Range (*invf)(float x);
73
74 extern FILE *file;
75
76 void  get_extended_header_info(NSIG_Sweep **nsig_sweep, int xh_size, int iray,
77                                int nparams,
78                                int *msec, float *azm, float *elev,
79                                float *pitch, float *roll, float *heading,
80                                float *azm_rate, float *elev_rate,
81                                float *pitch_rate, float *roll_rate,
82                                float *heading_rate,
83                                float *lat, float *lon, int *alt, float *rvc,
84                                float *vel_east, float *vel_north, float *vel_up)
85 {
86   static NSIG_Ext_header_ver1 xh;
87   int data_type, itype;
88
89   *msec = *azm = *elev = *pitch = *roll = *heading =
90         *azm_rate = *elev_rate = *pitch_rate = *roll_rate = *heading_rate =
91         *lat = *lon = *alt = *rvc = 0;
92
93   /* Determine where 'itype' for extended header is. */
94   for (itype = 0; itype<nparams; itype++) {
95         data_type = NSIG_I2(nsig_sweep[itype]->idh.data_type);
96     if (data_type == NSIG_DTB_EXH) break;
97   }
98   /*  printf("...extended header itype=%d, nparams=%d\n", itype, nparams); */
99   if (itype == nparams) return;  /* No extended header. */
100
101   /* Version 1. */
102   if (nsig_sweep[itype]->ray[iray] == NULL) return;
103   if (nsig_sweep[itype]->ray[iray]->range == NULL) return;
104   memmove(&xh, nsig_sweep[itype]->ray[iray]->range, sizeof(xh));
105   *msec = NSIG_I4(xh.msec);
106   /*   printf("...extended header msec= %d\n", *msec); */
107   if (xh_size <= 20) /* Stop, only have version 0. */
108     return;
109
110   /* Version 1 processing. */
111   *azm = nsig_from_bang(xh.azm);
112   *elev = nsig_from_bang(xh.elev);
113   *pitch = nsig_from_bang(xh.pitch);
114   *roll   = nsig_from_bang(xh.roll);
115   *heading = nsig_from_bang(xh.heading);
116   *azm_rate = nsig_from_bang(xh.azm_rate);
117   *elev_rate = nsig_from_bang(xh.elev_rate);
118   *pitch_rate = nsig_from_bang(xh.pitch_rate);
119   *roll_rate   = nsig_from_bang(xh.roll_rate);
120 #ifdef NSIG_VER2
121
122 #else
123   *heading_rate = nsig_from_bang(xh.heading_rate);
124 #endif
125   *lat = nsig_from_fourb_ang(xh.lat);
126   *lon = nsig_from_fourb_ang(xh.lon);
127   if(*lat > 180.0) *lat -= 360.0;
128   if(*lon > 180.0) *lon -= 360.0;
129   *alt       = NSIG_I2(xh.alt);
130   *rvc       = NSIG_I2(xh.rad_vel_cor)/100.0; /* cm to m */
131   *vel_east  = NSIG_I2(xh.vel_e)/100.0; /* cm to m */
132   *vel_north = NSIG_I2(xh.vel_n)/100.0; /* cm to m */
133   *vel_up    = NSIG_I2(xh.vel_u)/100.0; /* cm to m */
134   return;
135 }
136
137 /** Main code **/
138 Radar *
139 #ifdef NSIG_VER2
140 RSL_nsig2_to_radar
141 #else
142 RSL_nsig_to_radar
143 #endif
144 (char *filename)
145 {
146   FILE *fp;
147   /* RSL structures */
148   Radar                    *radar;
149   Ray                      *ray;
150   
151   int i, j, k, n;
152   int year, month, day;
153   int hour, minute, sec;
154   int numbins, numsweep;
155   int num_rays, sea_lvl_hgt;
156   int radar_number, num_samples;
157   int latd, latm, lats, lond, lonm, lons;
158   int data_type;
159   int bin_num;
160   int sweep_year, sweep_day, sweep_month;
161   int sweep_hour, sweep_minute, sweep_second;
162   int sweep_sec;
163   int z_flag_unc, z_flag_cor, v_flag, w_flag, speckle;
164   int ant_scan_mode;
165   float second;
166   float pw;
167   float bin_space;
168   float prf, prf2, wave, beam_width;
169   int prf_mode;
170   float prf_modes[] = {1.0, 2.0/3.0, 3.0/4.0, 4.0/5.0};
171   float vert_half_bw, horz_half_bw;
172   float rng_last_bin;
173   float rng_first_bin, freq;
174   float max_vel, sweep_rate, azim_rate;
175   float ray_data;
176   float az1, az2;
177   double tmp;
178   float sqi, log, csr, sig, cal_dbz;
179   char radar_type[50], state[2], city[15];
180   char site_name[16];
181   NSIG_Product_file *prod_file;
182   short id;
183   int data_mask, nrays;
184   int masks[5];
185   int nparams, nsweeps;
186   NSIG_Sweep **nsig_sweep;
187   NSIG_Ray *ray_p;
188   int itype, ifield;
189   unsigned short nsig_2byte;   /* New for 2-byte data types, Aug 2009 */
190   twob nsig_twob;
191   Sweep *sweep;
192   int msec;
193   float azm, elev, pitch, roll, heading, azm_rate, elev_rate,
194     pitch_rate, roll_rate, heading_rate,
195     lat, lon;
196   int alt;  /* Altitude */
197   float rvc;  /* Radial correction velocity m/s */
198   float vel_east, vel_north, vel_up; /* Platform velocity vectors m/sec */
199   int xh_size;
200   float incr;
201   extern int *rsl_qsweep; /* See RSL_read_these_sweeps in volume.c */
202   extern int rsl_qsweep_max;
203   extern float rsl_kdp_wavelen;
204
205   radar = NULL;
206   if (radar_verbose_flag)
207     fprintf(stderr, "open file: %s\n", filename);
208   
209   /** Opening nsig file **/
210   if((fp = nsig_open(filename)) == NULL) return NULL;
211   
212 #ifdef NSIG_VER2
213   sprintf(radar_type, "nsig2");
214   radar_number = 22;  /** Arbitrary number given to nsig2 data **/
215 #else
216   sprintf(radar_type, "nsig");
217   radar_number = 21;  /* What are these suppose to be? */
218 #endif
219   sprintf(state,"NA");
220   sprintf(city,"NA");
221   
222   /* MAINLINE CODE */
223   
224   prod_file = (NSIG_Product_file *)calloc(1, sizeof(NSIG_Product_file));
225
226   n = nsig_read_record(fp, (char *)&prod_file->rec1);
227   nsig_endianess(&prod_file->rec1);
228   if (radar_verbose_flag)
229     fprintf(stderr, "Read %d bytes for rec1.\n", n);
230
231   id = NSIG_I2(prod_file->rec1.struct_head.id);
232   if (radar_verbose_flag)
233     fprintf(stderr, "ID = %d\n", (int)id);
234   if (id != 7 && id != 27) { /* testing: Use 27 for Version 2 data */
235     fprintf(stderr, "File is not a SIGMET version 1 nor version 2 raw product file.\n");
236     return NULL;
237   }
238
239   n = nsig_read_record(fp, (char *)&prod_file->rec2);
240   if (radar_verbose_flag)
241     fprintf(stderr, "Read %d bytes for rec2.\n", n);
242
243   /* Count the bits set in 'data_mask' to determine the number
244    * of parameters present.
245    */
246   xh_size = NSIG_I2(prod_file->rec2.ingest_head.size_ext_ray_headers);
247   nrays = NSIG_I2(prod_file->rec2.ingest_head.num_rays);
248   if (radar_verbose_flag)
249     fprintf(stderr, "Expecting %d rays in each sweep.\n", nrays);
250 #ifdef NSIG_VER2 
251   memmove(&masks[0], prod_file->rec2.task_config.dsp_info.data_mask_cur.mask_word_0,
252     sizeof(fourb));
253   memmove(&masks[1], &prod_file->rec2.task_config.dsp_info.data_mask_cur.mask_word_1,
254     4*sizeof(fourb));
255   nparams = 0;
256   for (j=0; j < 5; j++) {
257     data_mask = masks[j];
258     for (i=0; i<32; i++)
259       nparams += (data_mask >> i) & 0x1;
260   }
261 #else
262   memmove(&data_mask, prod_file->rec2.task_config.dsp_info.data_mask, sizeof(fourb));
263   for (nparams=i=0; i<32; i++)
264     nparams += (data_mask >> i) & 0x1;
265 #endif
266
267   /* Number of sweeps */
268   nsweeps = NSIG_I2(prod_file->rec2.task_config.scan_info.num_swp);
269   
270
271
272    memmove(site_name, prod_file->rec1.prod_end.site_name, sizeof(prod_file->rec1.prod_end.site_name));
273    site_name[sizeof(site_name)-1] = '\0';
274   if (radar_verbose_flag) {
275     fprintf(stderr, "nparams = %d, nsweeps = %d\n", nparams, nsweeps);
276     fprintf(stderr, "Site name = <%s>\n", site_name);
277   }
278
279     /* nsig_sweep = nsig_read_sweep(fp, prod_file)
280      *
281      * Use: nsig_sweep[i]->ray[j]->range
282      *
283      * where 'range' is [0..nbins-1]
284      */
285
286     /*
287      * All the information you need is in:
288      *    prod_file->rec1
289      *        .struct_head, .prod_config .prod_end
290      *    prod_file->rec2
291      *        .struct_head, .ingest_head, .task_config .device_stat,
292      *        .dsp1, .dsp2
293      *    nsig_sweep[0..nparams-1]  'nparams' is the true number
294      *                              of parameters present.  You
295      *                              must check the 'id' (or type)
296      *                              to determine the field type.
297      *                              So far seen, nparams <= 6.
298      *    nsig_sweep[i]->bhdr     <NSIG_Raw_prod_bhdr>
299      *    nsig_sweep[i]->idh      <NSIG_Ingest_data_header>
300      *    nsig_sweep[i]->ray[j]   <NSIG_Ray *>
301      *
302      * Note:
303      *    For extended header access, you'll typically use nsig_sweep[0]
304      *    (double check the id) and the ray data allocated (nsig_ray->range)
305      *    is a pointer to the extended header, either v0 or v1.
306      *    You can typecast the pointer to NSIG_Ext_header_ver0 or
307      *    NSIG_Ext_header_ver1, as you like.  To determine which
308      *    version of the extended headers you have use:
309      *      xh_size <= 20 for version 0, else version 1.
310      *    Access:
311      *      xh_size = NSIG_I2(prod_file->rec2.ingest_head.size_ext_ray_headers)
312      *    
313      * Functions:
314      *    NSIG_I2(nsig_sweep[i]->idh.num_rays_act);   -- # of rays. (j)
315      *    NSIG_I2(nsig_sweep[i]->ray[j]->h.num_bins); -- # of bins in a ray.
316      *
317      *    NSIG_I2(x), NSIG_I4(x)   - Convert data, x, to floating point.
318      *
319      *    IMPORTANT NOTE: It must be known whether or not to perform
320      *                    byte-swapping.  To determine this, call
321      *                    'nsig_endianess'.  It returns 0 for no-swapping
322      *                    and 1 for swapping.  Additionally, it transparently
323      *                    initializes the nsig library to automatically
324      *                    swap when using NSIG_I2 or NSIG_I4.
325      *                    The function 'nsig_read_sweep' automatically
326      *                    calls 'nsig_endianess', too.
327      */
328
329    sea_lvl_hgt = NSIG_I2(prod_file->rec1.prod_end.grnd_sea_ht);
330
331    if (radar_verbose_flag)
332      fprintf(stderr, "sea: %d\n", sea_lvl_hgt);
333    if (radar_verbose_flag)
334      fprintf(stderr, "site_name: %s", site_name);
335    
336    /** Determine beamwidth from input variables (not saved in nsig file) **/
337    if(strncmp(site_name,"mit",3) == 0 || strncmp(site_name,"MIT",3) == 0)
338      beam_width = MIT_BEAMWIDTH;
339    else if(strncmp(site_name,"tog",3) == 0 || strncmp(site_name,"TOG",3) == 0)
340      beam_width = TOG_BEAMWIDTH;
341    else if(strncmp(site_name,"kwa",3) == 0 || strncmp(site_name,"KWA",3) == 0)
342      beam_width = KWA_BEAMWIDTH;
343    else
344      beam_width = DEFAULT_BEAMWIDTH;
345
346    if (radar_verbose_flag)
347      fprintf(stderr, "beamwidth: %f\n", beam_width);
348    
349    vert_half_bw = beam_width/2.0;
350    horz_half_bw = beam_width/2.0;
351    
352    /** Reading date and time **/
353    month = NSIG_I2(prod_file->rec2.ingest_head.start_time.month);
354    year = NSIG_I2(prod_file->rec2.ingest_head.start_time.year);
355    day = NSIG_I2(prod_file->rec2.ingest_head.start_time.day);
356    sec = NSIG_I4(prod_file->rec2.ingest_head.start_time.sec);
357
358    /* converting seconds since mid to time of day */
359    tmp = sec/3600.0;
360    hour = (int)tmp;
361    tmp = (tmp - hour) * 60.0;
362    minute = (int)tmp;
363    second = (tmp - minute) * 60.0;
364
365    /** records of the nsig file.                             **/
366    num_rays = 0;
367    pw = (NSIG_I4(prod_file->rec1.prod_end.pulse_wd))/100.0; /* pulse width */
368    prf = NSIG_I4(prod_file->rec1.prod_end.prf);  /* pulse repetition frequency */
369    prf_mode = NSIG_I2(prod_file->rec2.task_config.dsp_info.prf_mode);
370    prf2 = prf * prf_modes[prf_mode];
371    wave = (NSIG_I4(prod_file->rec1.prod_end.wavelen))/100.0; /* wavelength (cm) */
372    rsl_kdp_wavelen = wave;  /* EXTERNAL (volume.c) This sets KD_F and KD_INVF
373                              * to operate with the proper wavelength.
374                              */
375    numbins = NSIG_I4(prod_file->rec1.prod_end.num_bin);   /* # bins in ray */
376    rng_first_bin = (float)NSIG_I4(prod_file->rec1.prod_end.rng_f_bin)/100.0;
377    rng_last_bin = (float)NSIG_I4(prod_file->rec1.prod_end.rng_l_bin)/100.0;
378    bin_space = ((rng_last_bin-rng_first_bin)/numbins); /*rng res (m)*/
379    
380    numsweep = NSIG_I2(prod_file->rec2.task_config.scan_info.num_swp); /* # sweeps in volume */
381    num_samples = NSIG_I2(prod_file->rec1.prod_end.num_samp);
382    sweep_rate = 3.0; /** Approximate value -- info not stored **/
383    azim_rate = sweep_rate*360.0/60.0;
384    if (prf_mode != 0)
385    {
386         float max_vel1 = wave*prf/(100.0*4.0);
387         float max_vel2 = wave*prf2/(100.0*4.0);
388
389         max_vel = (max_vel1 * max_vel2)/(max_vel1-max_vel2);
390    }
391    else 
392    {
393         max_vel = wave*prf/(100.0*4.0);
394    }
395
396    freq = (299793000.0/wave)*1.0e-4; /** freq in MHZ **/
397
398    sqi = NSIG_I2(prod_file->rec2.task_config.calib_info.sqi)/256.0;
399    log = NSIG_I2(prod_file->rec2.task_config.calib_info.noise)/16.0;
400    csr = NSIG_I2(prod_file->rec2.task_config.calib_info.clutr_corr)/(-16.0);
401    sig = NSIG_I2(prod_file->rec2.task_config.calib_info.power)/16.0;
402    cal_dbz = NSIG_I2(prod_file->rec2.task_config.calib_info.cal_ref)/16.0;
403    z_flag_unc = NSIG_I2(prod_file->rec2.task_config.calib_info.z_flag_unc);
404    z_flag_cor = NSIG_I2(prod_file->rec2.task_config.calib_info.z_flag_cor);
405    v_flag = NSIG_I2(prod_file->rec2.task_config.calib_info.v_flag);
406    w_flag = NSIG_I2(prod_file->rec2.task_config.calib_info.w_flag);
407    speckle = NSIG_I2(prod_file->rec2.task_config.calib_info.speckle);
408
409    /** Verbose calibration information **/
410    if (radar_verbose_flag)
411       {
412       fprintf(stderr, "LOG = %5.2f\n", log);
413       fprintf(stderr, "SQI = %5.2f\n", sqi);
414       fprintf(stderr, "CSR = %5.2f\n", csr);
415       fprintf(stderr, "SIG = %5.2f\n", sig);
416       fprintf(stderr, "Calibration reflectivity: %5.2f dBZ\n", cal_dbz);
417       fprintf(stderr, "ZT flags: %d\n", z_flag_unc);  /** can find these **/
418       fprintf(stderr, "DZ flags: %d\n", z_flag_cor);  /** defn in the    **/
419       fprintf(stderr, "VR flags: %d\n", v_flag);      /** SIGMET Doc     **/
420       fprintf(stderr, "SW flags: %d\n", w_flag);
421       fprintf(stderr, "Flags: -3856  = SQI thresholding\n");
422       fprintf(stderr, "       -21846 = LOG thresholding\n");
423       fprintf(stderr, "       -24416 = LOG & SQI thresholding\n");
424       fprintf(stderr, "       -24516 = LOG & SQI & SIG thresholding\n");
425       fprintf(stderr, "speckle remover: %d\n", speckle);
426       }
427    
428    if (radar_verbose_flag)
429      fprintf(stderr, "vel: %f prf: %f prf2: %f\n", max_vel, prf, prf2);
430    
431    /** Extracting Latitude and Longitude from nsig file **/
432    lat = nsig_from_fourb_ang(prod_file->rec2.ingest_head.lat_rad);
433    lon = nsig_from_fourb_ang(prod_file->rec2.ingest_head.lon_rad);
434    if(lat > 180.0) lat -= 360.0;
435    if(lon > 180.0) lon -= 360.0;
436    if (radar_verbose_flag)
437      fprintf(stderr, "nsig_to_radar: lat %f, lon %f\n", lat, lon);
438    /** Latitude deg, min, sec **/
439    latd = (int)lat;
440    tmp = (lat - latd) * 60.0;
441    latm = (int)tmp;
442    lats = (int)((tmp - latm) * 60.0);
443    /** Longitude deg, min, sec **/
444    lond = (int)lon;
445    tmp = (lon - lond) * 60.0;
446    lonm = (int)tmp;
447    lons = (int)((tmp - lonm) * 60.0);
448    
449    /** Allocating memory for radar structure **/
450    radar = RSL_new_radar(MAX_RADAR_VOLUMES);
451    if (radar == NULL) 
452       {
453       fprintf(stderr, "nsig_to_radar: radar is NULL\n");
454       return NULL;
455       }
456
457    /** Filling Radar Header **/
458    radar->h.month = month;
459    radar->h.day = day;
460    radar->h.year = year; /* Year 2000 compliant. */
461    radar->h.hour = hour;
462    radar->h.minute = minute;
463    radar->h.sec = second;
464    sprintf(radar->h.radar_type, "%s", radar_type);
465    radar->h.number = radar_number;
466    memmove(radar->h.name, site_name, sizeof(radar->h.name));
467    memmove(radar->h.radar_name, site_name, sizeof(radar->h.radar_name));
468    memmove(radar->h.city, city, sizeof(radar->h.city));
469    memmove(radar->h.state, state, sizeof(radar->h.state));
470    radar->h.latd = latd;
471    radar->h.latm = latm;
472    radar->h.lats = lats;
473    radar->h.lond = lond;
474    radar->h.lonm = lonm;
475    radar->h.lons = lons;
476    radar->h.height = (int)sea_lvl_hgt;
477    radar->h.spulse = (int)(pw*1000);
478    radar->h.lpulse = (int)(pw*1000);
479
480    if (radar_verbose_flag) {
481 #ifdef NSIG_VER2
482      fprintf(stderr, "\nSIGMET version 2 raw product file.\n");
483 #else
484      fprintf(stderr, "\nSIGMET version 1 raw product file.\n");
485 #endif
486      fprintf(stderr, "Date: %2.2d/%2.2d/%4.4d %2.2d:%2.2d:%f\n",
487              radar->h.month, radar->h.day, radar->h.year,
488              radar->h.hour, radar->h.minute, radar->h.sec);
489      fprintf(stderr, "Name: ");
490      for (i=0; i<sizeof(radar->h.name); i++)
491        fprintf(stderr, "%c", radar->h.name[i]);
492      fprintf(stderr, "\n");
493      fprintf(stderr, "Lat/lon (%d %d' %d'', %d %d' %d'')\n",
494              radar->h.latd, radar->h.latm, radar->h.lats,
495              radar->h.lond, radar->h.lonm, radar->h.lons);
496    }
497
498    /** Converting data **/
499    if (radar_verbose_flag) fprintf(stderr, "Expecting %d sweeps.\n", numsweep);
500    for(i = 0; i < numsweep; i++)
501       {
502         nsig_sweep = nsig_read_sweep(fp, prod_file);
503         if (nsig_sweep == NULL) { /* EOF possibility */
504           if (feof(fp)) break;
505           else continue;
506         }
507         if (rsl_qsweep != NULL) {
508           if (i > rsl_qsweep_max) break;
509           if (rsl_qsweep[i] == 0) continue;
510         }
511         if (radar_verbose_flag)
512           fprintf(stderr, "Read sweep # %d\n", i);
513     /* The whole sweep is 'nsig_sweep' ... pretty slick.
514          *
515          * nsig_sweep[itype]  -- [0..nparams], if non-null.
516          */
517     for (itype=0; itype<nparams; itype++) {
518       if (nsig_sweep[itype] == NULL) continue;
519           
520       /** Reading date and time **/
521       sweep_month = NSIG_I2(nsig_sweep[itype]->idh.time.month);
522       sweep_year = NSIG_I2(nsig_sweep[itype]->idh.time.year);
523       sweep_day = NSIG_I2(nsig_sweep[itype]->idh.time.day);
524       sweep_sec = NSIG_I4(nsig_sweep[itype]->idh.time.sec);
525 #ifdef NSIG_VER2
526       msec      = NSIG_I2(nsig_sweep[itype]->idh.time.msec);
527       /*      printf("....... msec == %d\n", msec); */
528 #endif
529       /* converting seconds since mid to time of day */
530       tmp = sweep_sec/3600.0;
531       sweep_hour = (int)tmp;
532       tmp = (tmp - sweep_hour) * 60.0;
533       sweep_minute = (int)tmp;
534       sweep_second = sweep_sec - (sweep_hour*3600 + sweep_minute*60);
535
536       num_rays = NSIG_I2(nsig_sweep[itype]->idh.num_rays_exp);
537
538       data_type = NSIG_I2(nsig_sweep[itype]->idh.data_type);
539
540       ifield = 0;
541       switch (data_type) {
542       case NSIG_DTB_EXH: 
543           ifield = -1; 
544           break;
545       case NSIG_DTB_UCR:
546       case NSIG_DTB_UCR2:
547         ifield = ZT_INDEX;
548         f      = ZT_F; 
549         invf   = ZT_INVF;
550         break;
551       case NSIG_DTB_CR:
552       case NSIG_DTB_CR2:
553         ifield = DZ_INDEX;
554         f      = DZ_F; 
555         invf   = DZ_INVF;
556         break;
557       case NSIG_DTB_VEL:
558       case NSIG_DTB_VEL2:
559         ifield = VR_INDEX;
560         f      = VR_F; 
561         invf   = VR_INVF;
562         break;
563       case NSIG_DTB_WID:
564       case NSIG_DTB_WID2:
565         ifield = SW_INDEX;
566         f      = SW_F; 
567         invf   = SW_INVF;
568         break;
569       case NSIG_DTB_ZDR:             
570       case NSIG_DTB_ZDR2:
571         ifield = DR_INDEX;
572         f      = DR_F; 
573         invf   = DR_INVF;
574         break;
575       case NSIG_DTB_KDP:
576         ifield = KD_INDEX;
577         f      = KD_F; 
578         invf   = KD_INVF;
579         break;
580       case NSIG_DTB_PHIDP:     /* SRB 990127 */
581         ifield = PH_INDEX;
582         f      = PH_F; 
583         invf   = PH_INVF;
584         break;
585       case NSIG_DTB_RHOHV:     /* SRB 000414 */
586         ifield = RH_INDEX;
587         f      = RH_F; 
588         invf   = RH_INVF;
589         break;
590       case NSIG_DTB_VELC:
591       case NSIG_DTB_VELC2:
592         ifield = VC_INDEX;
593         f      = VC_F; 
594         invf   = VC_INVF;
595         break;
596       case NSIG_DTB_KDP2:
597         ifield = KD_INDEX;
598         f      = KD_F; 
599         invf   = KD_INVF;
600         break;
601       case NSIG_DTB_PHIDP2:
602         ifield = PH_INDEX;
603         f      = PH_F; 
604         invf   = PH_INVF;
605         break;
606       case NSIG_DTB_RHOHV2:
607         ifield = RH_INDEX;
608         f      = RH_F; 
609         invf   = RH_INVF;
610         break;
611       case NSIG_DTB_SQI:
612       case NSIG_DTB_SQI2:
613         ifield = SQ_INDEX;
614         f      = SQ_F; 
615         invf   = SQ_INVF;
616         break;
617       case NSIG_DTB_HCLASS:
618       case NSIG_DTB_HCLASS2:
619         ifield = HC_INDEX;
620         f      = HC_F; 
621         invf   = HC_INVF;
622       case NSIG_DTB_DBZ2:
623         ifield = CZ_INDEX;
624         f      = CZ_F; 
625         invf   = CZ_INVF;
626       case NSIG_DTB_ZDRC2:
627         ifield = ZD_INDEX;
628         f      = ZD_F; 
629         invf   = ZD_INVF;
630         break;
631       default:
632         fprintf(stderr,"Unknown field type: %d  Skipping it.\n", data_type);
633         continue;
634       }
635
636       if (radar_verbose_flag)
637         fprintf(stderr, "     nsig_sweep[%d], data_type = %d, rays(expected) = %d, nrays(actual) = %d\n", itype, data_type, num_rays, NSIG_I2(nsig_sweep[itype]->idh.num_rays_act));
638
639       if (data_type != NSIG_DTB_EXH) {
640         if ((radar->v[ifield] == NULL)) {
641           if (rsl_qfield[ifield]) {
642              radar->v[ifield] = RSL_new_volume(numsweep);
643              radar->v[ifield]->h.f = f;
644              radar->v[ifield]->h.invf = invf;
645            } else {
646              /* Skip this field, because, the user does not want it. */
647              continue;
648            }
649         }
650          if (radar->v[ifield]->sweep[i] == NULL)
651            radar->v[ifield]->sweep[i] = RSL_new_sweep(num_rays);
652          } 
653       else
654       continue;    /* Skip the actual extended header processing.
655                     * This is different than getting it, so that
656                     * the information is available for the other
657                     * fields when filling the RSL ray headers.
658                     */
659
660       /** DATA conversion time **/
661       sweep = radar->v[ifield]->sweep[i];
662       sweep->h.f = f;
663       sweep->h.invf = invf;
664       sweep->h.sweep_num = i;
665       sweep->h.beam_width = beam_width;
666       sweep->h.vert_half_bw = vert_half_bw;
667       sweep->h.horz_half_bw = horz_half_bw;
668       elev = nsig_from_bang(nsig_sweep[itype]->idh.fix_ang);
669       sweep->h.elev = elev;
670       
671       for(j = 0; j < num_rays; j++)
672         {
673           ray_p = nsig_sweep[itype]->ray[j];
674           if (ray_p == NULL) continue;
675           bin_num = NSIG_I2(ray_p->h.num_bins);
676
677           /* Load extended header information, if available.
678            * We need to pass the entire nsig_sweep and search for
679            * the extended header field (it may not be data_type==0).
680            */
681           get_extended_header_info(nsig_sweep, xh_size, j, nparams,
682                        &msec, &azm, &elev,
683                        &pitch, &roll, &heading,
684                        &azm_rate, &elev_rate,
685                        &pitch_rate, &roll_rate, &heading_rate,
686                        &lat, &lon, &alt, &rvc,
687                        &vel_east, &vel_north, &vel_up);
688           
689
690           if (radar->v[ifield]->sweep[i]->ray[j] == NULL)
691             radar->v[ifield]->sweep[i]->ray[j] = RSL_new_ray(bin_num);
692           ray = radar->v[ifield]->sweep[i]->ray[j];
693           ray->h.f = f;
694           ray->h.invf = invf;
695           /** Ray is at nsig_sweep[itype].ray->... **/
696           /** Loading nsig data into data structure **/
697                   
698           ray->h.month  = sweep_month;
699           ray->h.day    = sweep_day;
700           ray->h.year   = sweep_year; /* Year 2000 compliant. */
701           ray->h.hour   = sweep_hour;
702           ray->h.minute = sweep_minute;
703           if (msec == 0) { /* No extended header */
704             ray->h.sec  = NSIG_I2(ray_p->h.sec) + sweep_second;
705             elev = sweep->h.elev;
706           } else
707             ray->h.sec  = sweep_second + msec/1000.0;
708
709           /* add time ... handles end of min,hour,month,year and century. */
710           if (ray->h.sec >= 60) /* Should I fix the time no matter what? */
711             RSL_fix_time(ray);  /* Repair second overflow. */
712
713           ray->h.ray_num    = j;
714           ray->h.elev_num   = i;
715           ray->h.range_bin1 = (int)rng_first_bin;
716           ray->h.gate_size  = (int)(bin_space+.5); /* Nearest int */
717           ray->h.vel_res    = bin_space;
718           ray->h.sweep_rate = sweep_rate;
719           ray->h.prf        = (int)prf;
720             if (prf != 0)
721               ray->h.unam_rng = 299793000.0 / (2.0 * prf * 1000.0);  /* km */
722             else
723               ray->h.unam_rng = 0.0;
724           ray->h.prf2 = (int) prf2;
725           ray->h.fix_angle = (float)sweep->h.elev;
726           ray->h.azim_rate  = azim_rate;
727           ray->h.pulse_count = num_samples;
728           ray->h.pulse_width = pw;
729           ray->h.beam_width  = beam_width;
730           ray->h.frequency   = freq / 1000.0;  /* GHz */
731           ray->h.wavelength  = wave/100.0;     /* meters */
732           ray->h.nyq_vel     = max_vel;        /* m/s */
733           if (elev == 0.) elev = sweep->h.elev;
734           ray->h.elev        = (nsig_from_bang(ray_p->h.end_elev)+nsig_from_bang(ray_p->h.beg_elev))/2.0;
735           /* Compute mean azimuth angle for ray. */
736           az1 = nsig_from_bang(ray_p->h.beg_azm);
737           az2 = nsig_from_bang(ray_p->h.end_azm);
738           /*          printf("az1, %f, az2 %f\n", az1, az2); */
739           if(az1 > az2)
740             if((az1 - az2) > 180.0) az2 += 360.0;
741             else
742               ;
743           else
744             if((az2 - az1) > 180.0) az1 += 360.0;
745
746           az1 = (az1 + az2) / 2.0;
747           if (az1 > 360) az1 -= 360;
748           ray->h.azimuth     = az1;
749
750           /* From the extended header information, we learn the following. */
751           ray->h.pitch        = pitch;
752           ray->h.roll         = roll;
753           ray->h.heading      = heading;
754           ray->h.pitch_rate   = pitch_rate;
755           ray->h.roll_rate    = roll_rate;
756           ray->h.heading_rate = heading_rate;
757           ray->h.lat          = lat;
758           ray->h.lon          = lon;
759           ray->h.alt          = alt;
760           ray->h.rvc          = rvc;
761           ray->h.vel_east     = vel_east;
762           ray->h.vel_north    = vel_north;
763           ray->h.vel_up       = vel_up;
764
765           /*          printf("Processing sweep[%d]->ray[%d]: %d %f %f %f %f %f %f %f %f %d nbins=%d, bin1=%d gate=%d\n",
766                  i, j, msec, ray->h.sec, ray->h.azimuth, ray->h.elev, ray->h.pitch, ray->h.roll, ray->h.heading, ray->h.lat, ray->h.lon, ray->h.alt, ray->h.nbins, ray->h.range_bin1, ray->h.gate_size);
767                  */
768           /* TODO: ingest data header contains a value for bits-per-bin.
769            * This might be of use to allocate an array for ray->range with
770            * either 1-byte or 2-byte elements.  Then there's no need for
771            * memmove() whenever we need 2 bytes.
772            */
773
774           if (data_type == NSIG_DTB_EXH) continue;
775           ray_data = 0;
776           for(k = 0; k < bin_num; k++) {
777             switch(data_type) {
778             case NSIG_DTB_UCR:
779             case NSIG_DTB_CR:
780               if (ray_p->range[k] == 0) ray_data = NSIG_NO_ECHO;
781               else ray_data = (float)((ray_p->range[k]-64.0)/2.0);
782               break;
783             /* Simplified the velocity conversion for NSIG_DTB_VEL, using
784              * formula from IRIS Programmer's Manual. BLK, Oct 9 2009.
785              */
786             case NSIG_DTB_VEL:
787               if (ray_p->range[k] == 0) ray_data = NSIG_NO_ECHO;
788               else ray_data = (float)((ray_p->range[k]-128.0)/127.0)*max_vel;
789               break;
790               
791             case NSIG_DTB_WID:
792               if (ray_p->range[k] == 0) ray_data = NSIG_NO_ECHO;
793               else ray_data =(float)((ray_p->range[k])/256.0)*max_vel;
794               break;
795               
796             case NSIG_DTB_ZDR:
797               if (ray_p->range[k] == 0) ray_data = NSIG_NO_ECHO;
798               else ray_data = (float)((ray_p->range[k]-128.0)/16.0);
799               break;
800
801             case NSIG_DTB_KDP:
802                 if (ray_p->range[k] == 0 || ray_p->range[k] == 255 ||
803                     rsl_kdp_wavelen == 0.0) {
804                   ray_data = NSIG_NO_ECHO;
805                   break;
806                 }
807                 if (ray_p->range[k] < 128)
808                   ray_data = (-0.25 *
809                     pow((double)600.0,(double)((127-ray_p->range[k])/126.0))) /
810                       rsl_kdp_wavelen;
811                 else if (ray_p->range[k] > 128)
812                   ray_data = (0.25 *
813                     pow((double)600.0,(double)((ray_p->range[k]-129)/126.0))) /
814                       rsl_kdp_wavelen;
815                 else
816                   ray_data = 0.0;
817                 break;
818
819             case NSIG_DTB_PHIDP:
820               if (ray_p->range[k] == 0 || ray_p->range[k] == 255) 
821                 ray_data = NSIG_NO_ECHO;
822               else
823                 ray_data = 180.0*((ray_p->range[k]-1.0)/254.0);
824               break;
825
826             case NSIG_DTB_RHOHV:
827               if (ray_p->range[k] == 0 || ray_p->range[k] == 255) 
828                 ray_data = NSIG_NO_ECHO;
829               else 
830                 ray_data = sqrt((double)((ray_p->range[k]-1.0)/253.0));
831               break;
832
833             case NSIG_DTB_HCLASS:
834               if (ray_p->range[k] == 0 || ray_p->range[k] == 255) 
835                 ray_data = NSIG_NO_ECHO;
836               else
837                 ray_data = ray_p->range[k];
838               break;
839
840             case NSIG_DTB_SQI:
841               if (ray_p->range[k] == 0) ray_data = NSIG_NO_ECHO;
842               else ray_data = (float)sqrt((ray_p->range[k]-1.0)/253.0);
843               break;
844
845             case NSIG_DTB_VELC:
846               if (ray_p->range[k] == 0) ray_data = NSIG_NO_ECHO;
847               else {
848                 incr=75./127.;  /*  (+|- 75m/s) / 254 values */
849                 ray_data = (float)(ray_p->range[k]-128)*incr;
850               }
851               break;
852
853             case NSIG_DTB_UCR2:
854             case NSIG_DTB_CR2:
855             case NSIG_DTB_VEL2:
856             case NSIG_DTB_VELC2:
857             case NSIG_DTB_ZDR2:
858             case NSIG_DTB_KDP2:
859               memmove(nsig_twob, &ray_p->range[2*k], 2);
860               nsig_2byte = NSIG_I2(nsig_twob);
861               if (nsig_2byte == 0 || nsig_2byte == 65535)
862                 ray_data = NSIG_NO_ECHO2;
863               else ray_data = (float)(nsig_2byte-32768)/100.;
864               break;
865
866             case NSIG_DTB_WID2:
867               memmove(nsig_twob, &ray_p->range[2*k], 2);
868               nsig_2byte = NSIG_I2(nsig_twob);
869               if (nsig_2byte == 0 || nsig_2byte == 65535)
870                 ray_data = NSIG_NO_ECHO2;
871               else ray_data = (float)nsig_2byte/100.;
872               break;
873
874             case NSIG_DTB_PHIDP2:
875               memmove(nsig_twob, &ray_p->range[2*k], 2);
876               nsig_2byte = NSIG_I2(nsig_twob);
877               if (nsig_2byte == 0 || nsig_2byte == 65535)
878                 ray_data = NSIG_NO_ECHO;
879               else
880                 ray_data = 360.*(nsig_2byte-1)/65534.;
881               break;
882
883             case NSIG_DTB_SQI2:
884             case NSIG_DTB_RHOHV2:
885               memmove(nsig_twob, &ray_p->range[2*k], 2);
886               nsig_2byte = NSIG_I2(nsig_twob);
887               if (nsig_2byte == 0 || nsig_2byte == 65535)
888                 ray_data = NSIG_NO_ECHO2;
889               else ray_data = (float)(nsig_2byte-1)/65533.;
890               break;
891
892             case NSIG_DTB_HCLASS2:
893               memmove(nsig_twob, &ray_p->range[2*k], 2);
894               nsig_2byte = NSIG_I2(nsig_twob);
895               if (nsig_2byte == 0 || nsig_2byte == 65535)
896                 ray_data = NSIG_NO_ECHO2;
897               else
898                 ray_data = nsig_2byte;
899             }
900
901             if (ray_data == NSIG_NO_ECHO || ray_data == NSIG_NO_ECHO2)
902               ray->range[k] = ray->h.invf(BADVAL);
903             else
904               ray->range[k] = ray->h.invf(ray_data);
905
906             /*
907             if (data_type == NSIG_DTB_KDP)
908             printf("v[%d]->sweep[%d]->ray[%d]->range[%d] = %f, %d, %f\n", 
909                    ifield, i, j, k, ray->h.f(ray->range[k]), 
910                    (int)ray_p->range[k], ray_data);
911             */
912           }
913         }
914         }
915         nsig_free_sweep(nsig_sweep);
916       }
917
918    /* Do not reset radar->h.nvolumes. It is already set properly. */
919    if (radar_verbose_flag)
920      fprintf(stderr, "Max index of radar->v[0..%d]\n", radar->h.nvolumes);
921    
922
923    /** close nsig file **/
924    nsig_close(fp);
925
926    radar = RSL_prune_radar(radar);
927    /** return radar pointer **/
928    return radar;
929 }