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.
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.
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.
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.
21 /*************************************************************/
23 /* Function: nsig_to_radar.c */
26 /* Applied Research Corporation */
30 /* Modifications by: */
32 /* Space Applications Corporation */
36 /* Started: 08 AUG 96 */
38 /* Derived from Paul Kucera's nsig_to_radar.c */
40 /*************************************************************/
54 extern int radar_verbose_flag;
55 extern int rsl_qfield[]; /* See RSL_select_fields */
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
71 static float (*f)(Range x);
72 static Range (*invf)(float x);
76 void get_extended_header_info(NSIG_Sweep **nsig_sweep, int xh_size, int iray,
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,
83 float *lat, float *lon, int *alt, float *rvc,
84 float *vel_east, float *vel_north, float *vel_up)
86 static NSIG_Ext_header_ver1 xh;
89 *msec = *azm = *elev = *pitch = *roll = *heading =
90 *azm_rate = *elev_rate = *pitch_rate = *roll_rate = *heading_rate =
91 *lat = *lon = *alt = *rvc = 0;
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;
98 /* printf("...extended header itype=%d, nparams=%d\n", itype, nparams); */
99 if (itype == nparams) return; /* No extended header. */
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. */
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);
123 *heading_rate = nsig_from_bang(xh.heading_rate);
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 */
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;
160 int sweep_year, sweep_day, sweep_month;
161 int sweep_hour, sweep_minute, sweep_second;
163 int z_flag_unc, z_flag_cor, v_flag, w_flag, speckle;
168 float prf, wave, beam_width;
169 float vert_half_bw, horz_half_bw;
171 float rng_first_bin, freq;
172 float max_vel, sweep_rate, azim_rate;
176 float sqi, log, csr, sig, cal_dbz;
177 char radar_type[50], state[2], city[15];
179 NSIG_Product_file *prod_file;
181 int data_mask, nrays;
183 int nparams, nsweeps;
184 NSIG_Sweep **nsig_sweep;
187 unsigned short nsig_u2byte; /* New for 2-byte data types, Aug 2009 */
190 float azm, elev, pitch, roll, heading, azm_rate, elev_rate,
191 pitch_rate, roll_rate, heading_rate,
193 int alt; /* Altitude */
194 float rvc; /* Radial correction velocity m/s */
195 float vel_east, vel_north, vel_up; /* Platform velocity vectors m/sec */
198 extern int *rsl_qsweep; /* See RSL_read_these_sweeps in volume.c */
199 extern int rsl_qsweep_max;
200 extern float rsl_kdp_wavelen;
203 if (radar_verbose_flag)
204 fprintf(stderr, "open file: %s\n", filename);
206 /** Opening nsig file **/
207 if((fp = nsig_open(filename)) == NULL) return NULL;
210 sprintf(radar_type, "nsig2");
211 radar_number = 22; /** Arbitrary number given to nsig2 data **/
213 sprintf(radar_type, "nsig");
214 radar_number = 21; /* What are these suppose to be? */
221 prod_file = (NSIG_Product_file *)calloc(1, sizeof(NSIG_Product_file));
223 n = nsig_read_record(fp, (char *)&prod_file->rec1);
224 nsig_endianess(&prod_file->rec1);
225 if (radar_verbose_flag)
226 fprintf(stderr, "Read %d bytes for rec1.\n", n);
228 id = NSIG_I2(prod_file->rec1.struct_head.id);
229 if (radar_verbose_flag)
230 fprintf(stderr, "ID = %d\n", (int)id);
231 if (id != 7 && id != 27) { /* testing: Use 27 for Version 2 data */
232 fprintf(stderr, "File is not a SIGMET version 1 nor version 2 raw product file.\n");
236 n = nsig_read_record(fp, (char *)&prod_file->rec2);
237 if (radar_verbose_flag)
238 fprintf(stderr, "Read %d bytes for rec2.\n", n);
240 /** Test for scan mode -- If scan is a RHI will return NULL **/
241 /** because RSL can't handle RHI's. In the future, replace **/
242 /** NULL will a routine to convert RHI's to RSL Format **/
243 ant_scan_mode =NSIG_I2(prod_file->rec2.task_config.scan_info.ant_scan_mode);
244 if(ant_scan_mode == 2)
246 if (radar_verbose_flag)
247 fprintf(stderr, "RHI scan detected. Unable to process, returning NULL.\n");
251 /* Count the bits set in 'data_mask' to determine the number
252 * of parameters present.
254 xh_size = NSIG_I2(prod_file->rec2.ingest_head.size_ext_ray_headers);
255 nrays = NSIG_I2(prod_file->rec2.ingest_head.num_rays);
256 if (radar_verbose_flag)
257 fprintf(stderr, "Expecting %d rays in each sweep.\n", nrays);
259 memmove(&masks[0], prod_file->rec2.task_config.dsp_info.data_mask_cur.mask_word_0,
261 memmove(&masks[1], &prod_file->rec2.task_config.dsp_info.data_mask_cur.mask_word_1,
264 for (j=0; j < 5; j++) {
265 data_mask = masks[j];
267 nparams += (data_mask >> i) & 0x1;
270 memmove(&data_mask, prod_file->rec2.task_config.dsp_info.data_mask, sizeof(fourb));
271 for (nparams=i=0; i<32; i++)
272 nparams += (data_mask >> i) & 0x1;
275 /* Number of sweeps */
276 nsweeps = NSIG_I2(prod_file->rec2.task_config.scan_info.num_swp);
280 memmove(site_name, prod_file->rec1.prod_end.site_name, sizeof(prod_file->rec1.prod_end.site_name));
281 site_name[sizeof(site_name)-1] = '\0';
282 if (radar_verbose_flag) {
283 fprintf(stderr, "nparams = %d, nsweeps = %d\n", nparams, nsweeps);
284 fprintf(stderr, "Site name = <%s>\n", site_name);
287 /* nsig_sweep = nsig_read_sweep(fp, prod_file)
289 * Use: nsig_sweep[i]->ray[j]->range
291 * where 'range' is [0..nbins-1]
295 * All the information you need is in:
297 * .struct_head, .prod_config .prod_end
299 * .struct_head, .ingest_head, .task_config .device_stat,
301 * nsig_sweep[0..nparams-1] 'nparams' is the true number
302 * of parameters present. You
303 * must check the 'id' (or type)
304 * to determine the field type.
305 * So far seen, nparams <= 6.
306 * nsig_sweep[i]->bhdr <NSIG_Raw_prod_bhdr>
307 * nsig_sweep[i]->idh <NSIG_Ingest_data_header>
308 * nsig_sweep[i]->ray[j] <NSIG_Ray *>
311 * For extended header access, you'll typically use nsig_sweep[0]
312 * (double check the id) and the ray data allocated (nsig_ray->range)
313 * is a pointer to the extended header, either v0 or v1.
314 * You can typecast the pointer to NSIG_Ext_header_ver0 or
315 * NSIG_Ext_header_ver1, as you like. To determine which
316 * version of the extended headers you have use:
317 * xh_size <= 20 for version 0, else version 1.
319 * xh_size = NSIG_I2(prod_file->rec2.ingest_head.size_ext_ray_headers)
322 * NSIG_I2(nsig_sweep[i]->idh.num_rays_act); -- # of rays. (j)
323 * NSIG_I2(nsig_sweep[i]->ray[j]->h.num_bins); -- # of bins in a ray.
325 * NSIG_I2(x), NSIG_I4(x) - Convert data, x, to floating point.
327 * IMPORTANT NOTE: It must be known whether or not to perform
328 * byte-swapping. To determine this, call
329 * 'nsig_endianess'. It returns 0 for no-swapping
330 * and 1 for swapping. Additionally, it transparently
331 * initializes the nsig library to automatically
332 * swap when using NSIG_I2 or NSIG_I4.
333 * The function 'nsig_read_sweep' automatically
334 * calls 'nsig_endianess', too.
337 sea_lvl_hgt = NSIG_I2(prod_file->rec1.prod_end.grnd_sea_ht);
339 if (radar_verbose_flag)
340 fprintf(stderr, "sea: %d\n", sea_lvl_hgt);
341 if (radar_verbose_flag)
342 fprintf(stderr, "site_name: %s", site_name);
344 /** Determine beamwidth from input variables (not saved in nsig file) **/
345 if(strncmp(site_name,"mit",3) == 0 || strncmp(site_name,"MIT",3) == 0)
346 beam_width = MIT_BEAMWIDTH;
347 else if(strncmp(site_name,"tog",3) == 0 || strncmp(site_name,"TOG",3) == 0)
348 beam_width = TOG_BEAMWIDTH;
349 else if(strncmp(site_name,"kwa",3) == 0 || strncmp(site_name,"KWA",3) == 0)
350 beam_width = KWA_BEAMWIDTH;
352 beam_width = DEFAULT_BEAMWIDTH;
354 if (radar_verbose_flag)
355 fprintf(stderr, "beamwidth: %f\n", beam_width);
357 vert_half_bw = beam_width/2.0;
358 horz_half_bw = beam_width/2.0;
360 /** Reading date and time **/
361 month = NSIG_I2(prod_file->rec2.ingest_head.start_time.month);
362 year = NSIG_I2(prod_file->rec2.ingest_head.start_time.year);
363 day = NSIG_I2(prod_file->rec2.ingest_head.start_time.day);
364 sec = NSIG_I4(prod_file->rec2.ingest_head.start_time.sec);
366 /* converting seconds since mid to time of day */
369 tmp = (tmp - hour) * 60.0;
371 second = (tmp - minute) * 60.0;
373 /** records of the nsig file. **/
375 pw = (NSIG_I4(prod_file->rec1.prod_end.pulse_wd))/100.0; /* pulse width */
376 prf = NSIG_I4(prod_file->rec1.prod_end.prf); /* pulse repetition frequency */
377 wave = (NSIG_I4(prod_file->rec1.prod_end.wavelen))/100.0; /* wavelength (cm) */
378 rsl_kdp_wavelen = wave; /* EXTERNAL (volume.c) This sets KD_F and KD_INVF
379 * to operate with the proper wavelength.
381 numbins = NSIG_I4(prod_file->rec1.prod_end.num_bin); /* # bins in ray */
382 rng_first_bin = (float)NSIG_I4(prod_file->rec1.prod_end.rng_f_bin)/100.0;
383 rng_last_bin = (float)NSIG_I4(prod_file->rec1.prod_end.rng_l_bin)/100.0;
384 bin_space = ((rng_last_bin-rng_first_bin)/numbins); /*rng res (m)*/
386 numsweep = NSIG_I2(prod_file->rec2.task_config.scan_info.num_swp); /* # sweeps in volume */
387 num_samples = NSIG_I2(prod_file->rec1.prod_end.num_samp);
388 sweep_rate = 3.0; /** Approximate value -- info not stored **/
389 azim_rate = sweep_rate*360.0/60.0;
390 max_vel = wave*prf/(100.0*4.0);
391 freq = (299793000.0/wave)*1.0e-4; /** freq in MHZ **/
393 sqi = NSIG_I2(prod_file->rec2.task_config.calib_info.sqi)/256.0;
394 log = NSIG_I2(prod_file->rec2.task_config.calib_info.noise)/16.0;
395 csr = NSIG_I2(prod_file->rec2.task_config.calib_info.clutr_corr)/(-16.0);
396 sig = NSIG_I2(prod_file->rec2.task_config.calib_info.power)/16.0;
397 cal_dbz = NSIG_I2(prod_file->rec2.task_config.calib_info.cal_ref)/16.0;
398 z_flag_unc = NSIG_I2(prod_file->rec2.task_config.calib_info.z_flag_unc);
399 z_flag_cor = NSIG_I2(prod_file->rec2.task_config.calib_info.z_flag_cor);
400 v_flag = NSIG_I2(prod_file->rec2.task_config.calib_info.v_flag);
401 w_flag = NSIG_I2(prod_file->rec2.task_config.calib_info.w_flag);
402 speckle = NSIG_I2(prod_file->rec2.task_config.calib_info.speckle);
404 /** Verbose calibration information **/
405 if (radar_verbose_flag)
407 fprintf(stderr, "LOG = %5.2f\n", log);
408 fprintf(stderr, "SQI = %5.2f\n", sqi);
409 fprintf(stderr, "CSR = %5.2f\n", csr);
410 fprintf(stderr, "SIG = %5.2f\n", sig);
411 fprintf(stderr, "Calibration reflectivity: %5.2f dBZ\n", cal_dbz);
412 fprintf(stderr, "ZT flags: %d\n", z_flag_unc); /** can find these **/
413 fprintf(stderr, "DZ flags: %d\n", z_flag_cor); /** defn in the **/
414 fprintf(stderr, "VR flags: %d\n", v_flag); /** SIGMET Doc **/
415 fprintf(stderr, "SW flags: %d\n", w_flag);
416 fprintf(stderr, "Flags: -3856 = SQI thresholding\n");
417 fprintf(stderr, " -21846 = LOG thresholding\n");
418 fprintf(stderr, " -24416 = LOG & SQI thresholding\n");
419 fprintf(stderr, " -24516 = LOG & SQI & SIG thresholding\n");
420 fprintf(stderr, "speckle remover: %d\n", speckle);
423 if (radar_verbose_flag)
424 fprintf(stderr, "vel: %f prf: %f\n", max_vel, prf);
426 /** Extracting Latitude and Longitude from nsig file **/
427 lat = nsig_from_fourb_ang(prod_file->rec2.ingest_head.lat_rad);
428 lon = nsig_from_fourb_ang(prod_file->rec2.ingest_head.lon_rad);
429 if(lat > 180.0) lat -= 360.0;
430 if(lon > 180.0) lon -= 360.0;
431 if (radar_verbose_flag)
432 fprintf(stderr, "nsig_to_radar: lat %f, lon %f\n", lat, lon);
433 /** Latitude deg, min, sec **/
435 tmp = (lat - latd) * 60.0;
437 lats = (int)((tmp - latm) * 60.0);
438 /** Longitude deg, min, sec **/
440 tmp = (lon - lond) * 60.0;
442 lons = (int)((tmp - lonm) * 60.0);
444 /** Allocating memory for radar structure **/
445 radar = RSL_new_radar(MAX_RADAR_VOLUMES);
448 fprintf(stderr, "nsig_to_radar: radar is NULL\n");
452 /** Filling Radar Header **/
453 radar->h.month = month;
455 radar->h.year = year; /* Year 2000 compliant. */
456 radar->h.hour = hour;
457 radar->h.minute = minute;
458 radar->h.sec = second;
459 sprintf(radar->h.radar_type, "%s", radar_type);
460 radar->h.number = radar_number;
461 memmove(radar->h.name, site_name, sizeof(radar->h.name));
462 memmove(radar->h.radar_name, site_name, sizeof(radar->h.radar_name));
463 memmove(radar->h.city, city, sizeof(radar->h.city));
464 memmove(radar->h.state, state, sizeof(radar->h.state));
465 radar->h.latd = latd;
466 radar->h.latm = latm;
467 radar->h.lats = lats;
468 radar->h.lond = lond;
469 radar->h.lonm = lonm;
470 radar->h.lons = lons;
471 radar->h.height = (int)sea_lvl_hgt;
472 radar->h.spulse = (int)(pw*1000);
473 radar->h.lpulse = (int)(pw*1000);
475 if (radar_verbose_flag) {
477 fprintf(stderr, "\nSIGMET version 2 raw product file.\n");
479 fprintf(stderr, "\nSIGMET version 1 raw product file.\n");
481 fprintf(stderr, "Date: %2.2d/%2.2d/%4.4d %2.2d:%2.2d:%f\n",
482 radar->h.month, radar->h.day, radar->h.year,
483 radar->h.hour, radar->h.minute, radar->h.sec);
484 fprintf(stderr, "Name: ");
485 for (i=0; i<sizeof(radar->h.name); i++)
486 fprintf(stderr, "%c", radar->h.name[i]);
487 fprintf(stderr, "\n");
488 fprintf(stderr, "Lat/lon (%d %d' %d'', %d %d' %d'')\n",
489 radar->h.latd, radar->h.latm, radar->h.lats,
490 radar->h.lond, radar->h.lonm, radar->h.lons);
493 /** Converting data **/
494 if (radar_verbose_flag) fprintf(stderr, "Expecting %d sweeps.\n", numsweep);
495 for(i = 0; i < numsweep; i++)
497 nsig_sweep = nsig_read_sweep(fp, prod_file);
498 if (nsig_sweep == NULL) { /* EOF possibility */
502 if (rsl_qsweep != NULL) {
503 if (i > rsl_qsweep_max) break;
504 if (rsl_qsweep[i] == 0) continue;
506 if (radar_verbose_flag)
507 fprintf(stderr, "Read sweep # %d\n", i);
508 /* The whole sweep is 'nsig_sweep' ... pretty slick.
510 * nsig_sweep[itype] -- [0..nparams], if non-null.
512 for (itype=0; itype<nparams; itype++) {
513 if (nsig_sweep[itype] == NULL) continue;
515 /** Reading date and time **/
516 sweep_month = NSIG_I2(nsig_sweep[itype]->idh.time.month);
517 sweep_year = NSIG_I2(nsig_sweep[itype]->idh.time.year);
518 sweep_day = NSIG_I2(nsig_sweep[itype]->idh.time.day);
519 sweep_sec = NSIG_I4(nsig_sweep[itype]->idh.time.sec);
521 msec = NSIG_I2(nsig_sweep[itype]->idh.time.msec);
522 /* printf("....... msec == %d\n", msec); */
524 /* converting seconds since mid to time of day */
525 tmp = sweep_sec/3600.0;
526 sweep_hour = (int)tmp;
527 tmp = (tmp - sweep_hour) * 60.0;
528 sweep_minute = (int)tmp;
529 sweep_second = sweep_sec - (sweep_hour*3600 + sweep_minute*60);
531 num_rays = NSIG_I2(nsig_sweep[itype]->idh.num_rays_exp);
533 data_type = NSIG_I2(nsig_sweep[itype]->idh.data_type);
575 case NSIG_DTB_PHIDP: /* SRB 990127 */
580 case NSIG_DTB_RHOHV: /* SRB 000414 */
596 case NSIG_DTB_PHIDP2:
601 case NSIG_DTB_RHOHV2:
612 case NSIG_DTB_HCLASS:
613 case NSIG_DTB_HCLASS2:
619 fprintf(stderr,"Unknown field type: %d Skipping it.\n", data_type);
623 if (radar_verbose_flag)
624 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));
626 if (data_type != NSIG_DTB_EXH) {
627 if ((radar->v[ifield] == NULL)) {
628 if (rsl_qfield[ifield]) {
629 radar->v[ifield] = RSL_new_volume(numsweep);
630 radar->v[ifield]->h.f = f;
631 radar->v[ifield]->h.invf = invf;
633 /* Skip this field, because, the user does not want it. */
637 if (radar->v[ifield]->sweep[i] == NULL)
638 radar->v[ifield]->sweep[i] = RSL_new_sweep(num_rays);
641 continue; /* Skip the actual extended header processing.
642 * This is different than getting it, so that
643 * the information is available for the other
644 * fields when filling the RSL ray headers.
647 /** DATA conversion time **/
648 sweep = radar->v[ifield]->sweep[i];
650 sweep->h.invf = invf;
651 sweep->h.sweep_num = i;
652 sweep->h.beam_width = beam_width;
653 sweep->h.vert_half_bw = vert_half_bw;
654 sweep->h.horz_half_bw = horz_half_bw;
655 elev = nsig_from_bang(nsig_sweep[itype]->idh.fix_ang);
656 sweep->h.elev = elev;
658 for(j = 0; j < num_rays; j++)
660 ray_p = nsig_sweep[itype]->ray[j];
661 if (ray_p == NULL) continue;
662 bin_num = NSIG_I2(ray_p->h.num_bins);
664 /* Load extended header information, if available.
665 * We need to pass the entire nsig_sweep and search for
666 * the extended header field (it may not be data_type==0).
668 get_extended_header_info(nsig_sweep, xh_size, j, nparams,
670 &pitch, &roll, &heading,
671 &azm_rate, &elev_rate,
672 &pitch_rate, &roll_rate, &heading_rate,
673 &lat, &lon, &alt, &rvc,
674 &vel_east, &vel_north, &vel_up);
677 if (radar->v[ifield]->sweep[i]->ray[j] == NULL)
678 radar->v[ifield]->sweep[i]->ray[j] = RSL_new_ray(bin_num);
679 ray = radar->v[ifield]->sweep[i]->ray[j];
682 /** Ray is at nsig_sweep[itype].ray->... **/
683 /** Loading nsig data into data structure **/
685 ray->h.month = sweep_month;
686 ray->h.day = sweep_day;
687 ray->h.year = sweep_year; /* Year 2000 compliant. */
688 ray->h.hour = sweep_hour;
689 ray->h.minute = sweep_minute;
690 if (msec == 0) { /* No extended header */
691 ray->h.sec = NSIG_I2(ray_p->h.sec) + sweep_second;
692 elev = sweep->h.elev;
694 ray->h.sec = sweep_second + msec/1000.0;
696 /* add time ... handles end of min,hour,month,year and century. */
697 if (ray->h.sec >= 60) /* Should I fix the time no matter what? */
698 RSL_fix_time(ray); /* Repair second overflow. */
702 ray->h.range_bin1 = (int)rng_first_bin;
703 ray->h.gate_size = (int)(bin_space+.5); /* Nearest int */
704 ray->h.vel_res = bin_space;
705 ray->h.sweep_rate = sweep_rate;
706 ray->h.prf = (int)prf;
708 ray->h.unam_rng = 299793000.0 / (2.0 * prf * 1000.0); /* km */
710 ray->h.unam_rng = 0.0;
711 ray->h.fix_angle = (float)sweep->h.elev;
712 ray->h.azim_rate = azim_rate;
713 ray->h.pulse_count = (float)num_samples;
714 ray->h.pulse_width = pw;
715 ray->h.beam_width = beam_width;
716 ray->h.frequency = freq / 1000.0; /* GHz */
717 ray->h.wavelength = wave/100.0; /* meters */
718 ray->h.nyq_vel = max_vel; /* m/s */
719 if (elev == 0.) elev = sweep->h.elev;
721 /* Compute mean azimuth angle for ray. */
722 az1 = nsig_from_bang(ray_p->h.beg_azm);
723 az2 = nsig_from_bang(ray_p->h.end_azm);
724 /* printf("az1, %f, az2 %f\n", az1, az2); */
726 if((az1 - az2) > 180.0) az2 += 360.0;
730 if((az2 - az1) > 180.0) az1 += 360.0;
732 az1 = (az1 + az2) / 2.0;
733 if (az1 > 360) az1 -= 360;
734 ray->h.azimuth = az1;
736 /* From the extended header information, we learn the following. */
737 ray->h.pitch = pitch;
739 ray->h.heading = heading;
740 ray->h.pitch_rate = pitch_rate;
741 ray->h.roll_rate = roll_rate;
742 ray->h.heading_rate = heading_rate;
747 ray->h.vel_east = vel_east;
748 ray->h.vel_north = vel_north;
749 ray->h.vel_up = vel_up;
751 /* printf("Processing sweep[%d]->ray[%d]: %d %f %f %f %f %f %f %f %f %d nbins=%d, bin1=%d gate=%d\n",
752 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);
754 /* TODO: ingest data header contains a value for bits-per-bin.
755 * This might be of use to allocate an array for ray->range with
756 * either 1-byte or 2-byte elements. Then there's no need for
757 * memmove() whenever we need 2 bytes.
760 if (data_type == NSIG_DTB_EXH) continue;
762 for(k = 0; k < bin_num; k++) {
766 if (ray_p->range[k] == 0) ray_data = NSIG_NO_ECHO;
767 else ray_data = (float)((ray_p->range[k]-64.0)/2.0);
769 /* Simplified the velocity conversion for NSIG_DTB_VEL, using
770 * formula from IRIS Programmer's Manual. BLK, Oct 9 2009.
773 if (ray_p->range[k] == 0) ray_data = NSIG_NO_ECHO;
774 else ray_data = (float)((ray_p->range[k]-128.0)/127.0)*max_vel;
778 if (ray_p->range[k] == 0) ray_data = NSIG_NO_ECHO;
779 else ray_data =(float)((ray_p->range[k])/256.0)*max_vel;
783 if (ray_p->range[k] == 0) ray_data = NSIG_NO_ECHO;
784 else ray_data = (float)((ray_p->range[k]-128.0)/16.0);
788 if (ray_p->range[k] == 0 || ray_p->range[k] == 255 ||
789 rsl_kdp_wavelen == 0.0) {
790 ray_data = NSIG_NO_ECHO;
793 if (ray_p->range[k] < 128)
795 pow((double)600.0,(double)((127-ray_p->range[k])/126.0))) /
797 else if (ray_p->range[k] > 128)
799 pow((double)600.0,(double)((ray_p->range[k]-129)/126.0))) /
806 if (ray_p->range[k] == 0 || ray_p->range[k] == 255)
807 ray_data = NSIG_NO_ECHO;
809 ray_data = 180.0*((ray_p->range[k]-1.0)/254.0);
813 if (ray_p->range[k] == 0 || ray_p->range[k] == 255)
814 ray_data = NSIG_NO_ECHO;
816 ray_data = sqrt((double)((ray_p->range[k]-1.0)/253.0));
819 case NSIG_DTB_HCLASS:
820 if (ray_p->range[k] == 0 || ray_p->range[k] == 255)
821 ray_data = NSIG_NO_ECHO;
823 ray_data = ray_p->range[k];
827 if (ray_p->range[k] == 0) ray_data = NSIG_NO_ECHO;
828 else ray_data = (float)sqrt((ray_p->range[k]-1.0)/253.0);
832 if (ray_p->range[k] == 0) ray_data = NSIG_NO_ECHO;
834 incr=75./127.; /* (+|- 75m/s) / 254 values */
835 ray_data = (float)(ray_p->range[k]-128)*incr;
845 memmove(&nsig_u2byte, &ray_p->range[2*k], 2);
846 nsig_u2byte = NSIG_I2(&nsig_u2byte);
847 if (nsig_u2byte == 0 || nsig_u2byte == 65535)
848 ray_data = NSIG_NO_ECHO2;
849 else ray_data = (float)(nsig_u2byte-32768)/100.;
853 memmove(&nsig_u2byte, &ray_p->range[2*k], 2);
854 nsig_u2byte = NSIG_I2(&nsig_u2byte);
855 if (nsig_u2byte == 0 || nsig_u2byte == 65535)
856 ray_data = NSIG_NO_ECHO2;
857 else ray_data = (float)nsig_u2byte/100.;
860 case NSIG_DTB_PHIDP2:
861 memmove(&nsig_u2byte, &ray_p->range[2*k], 2);
862 nsig_u2byte = NSIG_I2(&nsig_u2byte);
863 if (nsig_u2byte == 0 || nsig_u2byte == 65535)
864 ray_data = NSIG_NO_ECHO;
866 ray_data = 360.*(nsig_u2byte-1)/65534.;
870 case NSIG_DTB_RHOHV2:
871 memmove(&nsig_u2byte, &ray_p->range[2*k], 2);
872 nsig_u2byte = NSIG_I2(&nsig_u2byte);
873 if (nsig_u2byte == 0 || nsig_u2byte == 65535)
874 ray_data = NSIG_NO_ECHO2;
875 else ray_data = (float)(nsig_u2byte-1)/65533.;
878 case NSIG_DTB_HCLASS2:
879 memmove(&nsig_u2byte, &ray_p->range[2*k], 2);
880 nsig_u2byte = NSIG_I2(&nsig_u2byte);
881 if (nsig_u2byte == 0 || nsig_u2byte == 65535)
882 ray_data = NSIG_NO_ECHO2;
884 ray_data = nsig_u2byte;
887 if (ray_data == NSIG_NO_ECHO || ray_data == NSIG_NO_ECHO2)
888 ray->range[k] = ray->h.invf(BADVAL);
890 ray->range[k] = ray->h.invf(ray_data);
893 if (data_type == NSIG_DTB_KDP)
894 printf("v[%d]->sweep[%d]->ray[%d]->range[%d] = %f, %d, %f\n",
895 ifield, i, j, k, ray->h.f(ray->range[k]),
896 (int)ray_p->range[k], ray_data);
901 nsig_free_sweep(nsig_sweep);
904 /* Do not reset radar->h.nvolumes. It is already set properly. */
905 if (radar_verbose_flag)
906 fprintf(stderr, "Max index of radar->v[0..%d]\n", radar->h.nvolumes);
909 /** close nsig file **/
912 radar = RSL_prune_radar(radar);
913 /** return radar pointer **/