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