3 This is the TRMM Office Radar Software Library.
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * This file contains routines for processing Message Type 31, the digital
26 * radar message type introduced in WSR-88D Level II Build 10. For more
27 * information, see the "Interface Control Document for the RDA/RPG" at the
28 * WSR-88D Radar Operations Center web site.
35 /* Data descriptions in the following data structures are from the "Interface
36 * Control Document for the RDA/RPG", Build 10.0 Draft, WSR-88D Radar
41 short rpg[6]; /* 12 bytes inserted by RPG Communications Mgr. Ignored. */
42 unsigned short msg_size; /* Message size for this segment, in halfwords */
43 unsigned char channel; /* RDA Redundant Channel */
44 unsigned char msg_type; /* Message type. For example, 31 */
45 unsigned short id_seq; /* Msg seq num = 0 to 7FFF, then roll over to 0 */
46 unsigned short msg_date; /* Modified Julian date from 1/1/70 */
47 unsigned int msg_time; /* Packet generation time in ms past midnight */
48 unsigned short num_segs; /* Number of segments for this message */
49 unsigned short seg_num; /* Number of this segment */
54 unsigned int ray_time; /* Data collection time in milliseconds past midnight GMT */
55 unsigned short ray_date; /* Julian date - 2440586.5 (1/01/1970) */
56 unsigned short azm_num ; /* Radial number within elevation scan */
57 float azm; /* Azimuth angle in degrees (0 to 359.956055) */
58 unsigned char compression_code; /* 0 = uncompressed, 1 = BZIP2, 2 = zlib */
59 unsigned char spare; /* for word alignment */
60 unsigned short radial_len; /* radial length in bytes, including data header block */
61 unsigned char azm_res;
62 unsigned char radial_status;
63 unsigned char elev_num;
64 unsigned char sector_cut_num;
65 float elev; /* Elevation angle in degrees (-7.0 to 70.0) */
66 unsigned char radial_spot_blanking;
67 unsigned char azm_indexing_mode;
68 unsigned short data_block_count;
69 /* Data Block Indexes */
70 unsigned int vol_const;
71 unsigned int elev_const;
72 unsigned int radial_const;
79 } Ray_header_m31; /* Called Data Header Block in RDA/RPG document. */
83 unsigned int reserved;
84 unsigned short ngates;
85 short range_first_gate;
86 short range_samp_interval;
87 short thresh_not_overlayed;
89 unsigned char controlflag;
90 unsigned char datasize_bits;
95 #define MAX_RADIAL_LENGTH 14288
98 Ray_header_m31 ray_hdr;
101 unsigned char data[MAX_RADIAL_LENGTH];
105 enum radial_status {START_OF_ELEV, INTERMED_RADIAL, END_OF_ELEV, BEGIN_VOS,
109 void wsr88d_swap_m31_hdr(Wsr88d_msg_hdr *msghdr)
111 swap_2_bytes(&msghdr->msg_size);
112 swap_2_bytes(&msghdr->id_seq);
113 swap_2_bytes(&msghdr->msg_date);
114 swap_4_bytes(&msghdr->msg_time);
115 swap_2_bytes(&msghdr->num_segs);
116 swap_2_bytes(&msghdr->seg_num);
120 void wsr88d_swap_m31_ray_hdr(Ray_header_m31 *ray_hdr)
124 swap_4_bytes(&ray_hdr->ray_time);
125 swap_2_bytes(&ray_hdr->ray_date);
126 swap_2_bytes(&ray_hdr->azm_num);
127 swap_4_bytes(&ray_hdr->azm);
128 swap_2_bytes(&ray_hdr->radial_len);
129 swap_4_bytes(&ray_hdr->elev);
130 swap_2_bytes(&ray_hdr->data_block_count);
131 data_ptr = (int *) &ray_hdr->vol_const;
132 for (; data_ptr <= (int *) &ray_hdr->field6; data_ptr++)
133 swap_4_bytes(data_ptr);
137 void wsr88d_swap_data_hdr(Data_moment_hdr *this_field)
140 halfword = (short *) &this_field->ngates;
141 for (; halfword < (short *) &this_field->controlflag; halfword++)
142 swap_2_bytes(halfword);
143 swap_4_bytes(&this_field->scale);
144 swap_4_bytes(&this_field->offset);
148 float wsr88d_get_angle(short bitfield)
153 float value[13] = {0.043945, 0.08789, 0.17578, 0.35156, .70313, 1.40625,
154 2.8125, 5.625, 11.25, 22.5, 45., 90., 180.};
156 /* Find which bits are set and sum corresponding values to get angle. */
158 bitfield = bitfield >> 3; /* 3 least significant bits aren't used. */
159 for (i = 0; i < 13; i++) {
160 if (bitfield & mask) angle += value[i];
161 bitfield = bitfield >> 1;
167 float wsr88d_get_azim_rate(short bitfield)
172 float value[12] = {0.0109863, 0.021972656, 0.043945, 0.08789, 0.17578,
173 0.35156, .70313, 1.40625, 2.8125, 5.625, 11.25, 22.5};
175 /* Find which bits are set and sum corresponding values to get rate. */
177 bitfield = bitfield >> 3; /* 3 least significant bits aren't used. */
178 for (i = 0; i < 12; i++) {
179 if (bitfield & mask) rate += value[i];
180 bitfield = bitfield >> 1;
182 if (bitfield >> 15) rate = -rate;
186 #define WSR88D_MAX_SWEEPS 20
192 float fixed_angle[WSR88D_MAX_SWEEPS];
193 float azim_rate[WSR88D_MAX_SWEEPS];
194 int waveform[WSR88D_MAX_SWEEPS];
195 int super_res_ctrl[WSR88D_MAX_SWEEPS];
196 int surveil_prf_num[WSR88D_MAX_SWEEPS];
197 int doppler_prf_num[WSR88D_MAX_SWEEPS];
200 static VCP_data vcp_data;
202 void wsr88d_get_vcp_data(short *msgtype5)
204 short azim_rate, fixed_angle, vel_res;
205 short sres_and_survprf; /* super res ctrl and surveil prf, one byte each */
206 short chconf_and_waveform;
209 vcp_data.vcp = (unsigned short) msgtype5[2];
210 vcp_data.num_cuts = msgtype5[3];
211 if (little_endian()) {
212 swap_2_bytes(&vcp_data.vcp);
213 swap_2_bytes(&vcp_data.num_cuts);
215 vel_res = msgtype5[5];
216 if (little_endian()) swap_2_bytes(&vel_res);
217 vel_res = vel_res >> 8;
218 if (vel_res == 2) vcp_data.vel_res = 0.5;
219 else if (vel_res == 4) vcp_data.vel_res = 1.0;
220 else vcp_data.vel_res = 0.0;
221 /* Get elevation related information for each sweep. */
222 for (i=0; i < vcp_data.num_cuts; i++) {
223 fixed_angle = msgtype5[11 + i*23];
224 azim_rate = msgtype5[15 + i*23];
225 chconf_and_waveform = msgtype5[12 + i*23];
226 sres_and_survprf = msgtype5[13 + i*23];
227 vcp_data.doppler_prf_num[i] = msgtype5[23 + i*23];
228 if (little_endian()) {
229 swap_2_bytes(&fixed_angle);
230 swap_2_bytes(&azim_rate);
231 swap_2_bytes(&chconf_and_waveform);
232 swap_2_bytes(&sres_and_survprf);
233 swap_2_bytes(&vcp_data.doppler_prf_num[i]);
235 vcp_data.fixed_angle[i] = wsr88d_get_angle(fixed_angle);
236 vcp_data.azim_rate[i] = wsr88d_get_azim_rate(azim_rate);
237 vcp_data.waveform[i] = chconf_and_waveform & 0xff;
238 vcp_data.super_res_ctrl[i] = sres_and_survprf >> 8;
239 vcp_data.surveil_prf_num[i] = sres_and_survprf & 0xff;
244 void get_wsr88d_unamb_and_nyq_vel(Wsr88d_ray_m31 *wsr88d_ray, float *unamb_rng,
248 short nyq_vel_sh, unamb_rng_sh;
251 dindex = wsr88d_ray->ray_hdr.radial_const;
252 if (strncmp((char *) &wsr88d_ray->data[dindex], "RRAD", 4) == 0) found = 1;
254 dindex = wsr88d_ray->ray_hdr.elev_const;
255 if (strncmp((char *) &wsr88d_ray->data[dindex], "RRAD", 4) == 0)
258 dindex = wsr88d_ray->ray_hdr.vol_const;
259 if (strncmp((char *) &wsr88d_ray->data[dindex], "RRAD", 4) == 0)
264 memcpy(&unamb_rng_sh, &wsr88d_ray->data[dindex+6], 2);
265 memcpy(&nyq_vel_sh, &wsr88d_ray->data[dindex+16], 2);
266 if (little_endian()) {
267 swap_2_bytes(&unamb_rng_sh);
268 swap_2_bytes(&nyq_vel_sh);
270 *unamb_rng = unamb_rng_sh / 10.;
271 *nyq_vel = nyq_vel_sh / 100.;
279 int read_wsr88d_ray_m31(Wsr88d_file *wf, int msg_size,
280 Wsr88d_ray_m31 *wsr88d_ray)
283 float nyq_vel, unamb_rng;
285 /* Read wsr88d ray. */
287 n = fread(wsr88d_ray->data, msg_size, 1, wf->fptr);
289 fprintf(stderr,"read_wsr88d_ray_m31: Read failed.\n");
293 /* Copy data header block to ray header structure. */
294 memcpy(&wsr88d_ray->ray_hdr, &wsr88d_ray->data, sizeof(Ray_header_m31));
296 if (little_endian()) wsr88d_swap_m31_ray_hdr(&wsr88d_ray->ray_hdr);
298 /* Retrieve unambiguous range and Nyquist velocity here so that we don't
299 * have to do it for each data moment later.
301 get_wsr88d_unamb_and_nyq_vel(wsr88d_ray, &unamb_rng, &nyq_vel);
302 wsr88d_ray->unamb_rng = unamb_rng;
303 wsr88d_ray->nyq_vel = nyq_vel;
309 void wsr88d_load_ray_hdr(Wsr88d_ray_m31 *wsr88d_ray, Ray *ray)
311 int month, day, year, hour, minute, sec;
314 Ray_header_m31 ray_hdr;
316 ray_hdr = wsr88d_ray->ray_hdr;
317 m1_ray.ray_date = ray_hdr.ray_date;
318 m1_ray.ray_time = ray_hdr.ray_time;
320 wsr88d_get_date(&m1_ray, &month, &day, &year);
321 wsr88d_get_time(&m1_ray, &hour, &minute, &sec, &fsec);
322 ray->h.year = year + 1900;
323 ray->h.month = month;
326 ray->h.minute = minute;
327 ray->h.sec = sec + fsec;
328 ray->h.azimuth = ray_hdr.azm;
329 ray->h.ray_num = ray_hdr.azm_num;
330 ray->h.elev = ray_hdr.elev;
331 ray->h.elev_num = ray_hdr.elev_num;
332 ray->h.unam_rng = wsr88d_ray->unamb_rng;
333 ray->h.nyq_vel = wsr88d_ray->nyq_vel;
335 elev_index = ray_hdr.elev_num - 1;
336 ray->h.azim_rate = vcp_data.azim_rate[elev_index];
337 ray->h.fix_angle = vcp_data.fixed_angle[elev_index];
338 ray->h.vel_res = vcp_data.vel_res;
339 if (ray_hdr.azm_res != 1)
340 ray->h.beam_width = 1.0;
341 else ray->h.beam_width = 0.5;
343 /* For convenience, use message type 1 routines to get some values.
344 * First load VCP and elevation numbers into a msg 1 ray.
346 m1_ray.vol_cpat = vcp_data.vcp;
347 m1_ray.elev_num = ray_hdr.elev_num;
348 m1_ray.unam_rng = (short) (wsr88d_ray->unamb_rng * 10.);
349 /* Get values from message type 1 routines. */
350 ray->h.frequency = wsr88d_get_frequency(&m1_ray);
351 ray->h.pulse_width = wsr88d_get_pulse_width(&m1_ray);
352 ray->h.pulse_count = wsr88d_get_pulse_count(&m1_ray);
353 ray->h.prf = (int) wsr88d_get_prf(&m1_ray);
354 ray->h.wavelength = 0.1071;
358 int wsr88d_get_vol_index(char* dataname)
360 if (strncmp(dataname, "DREF", 4) == 0) return DZ_INDEX;
361 if (strncmp(dataname, "DVEL", 4) == 0) return VR_INDEX;
362 if (strncmp(dataname, "DSW", 3) == 0) return SW_INDEX;
363 if (strncmp(dataname, "DZDR", 4) == 0) return DR_INDEX;
364 if (strncmp(dataname, "DPHI", 4) == 0) return PH_INDEX;
365 if (strncmp(dataname, "DRHO", 4) == 0) return RH_INDEX;
371 #define MAXRAYS_M31 800
374 void wsr88d_load_ray_into_radar(Wsr88d_ray_m31 *wsr88d_ray, int isweep,
377 /* Load data into ray structure for each data field. */
383 const nconstblocks = 3;
385 Data_moment_hdr data_hdr;
389 float value, scale, offset;
391 Range (*invf)(float x);
394 int vol_index, waveform;
397 int keep_hi_prf_dz = 0; /* TODO: make this an argument. */
399 enum waveforms {surveillance=1, doppler_w_amb_res, doppler_no_amb_res,
402 nfields = wsr88d_ray->ray_hdr.data_block_count - nconstblocks;
403 field_offset = (int *) &wsr88d_ray->ray_hdr.radial_const;
404 do_swap = little_endian();
405 iray = wsr88d_ray->ray_hdr.azm_num - 1;
407 for (ifield=0; ifield < nfields; ifield++) {
409 data_index = *field_offset;
410 /* Get data moment header. */
411 hdr_size = sizeof(data_hdr);
412 memcpy(&data_hdr, &wsr88d_ray->data[data_index], hdr_size);
413 if (do_swap) wsr88d_swap_data_hdr(&data_hdr);
414 data_index += hdr_size;
416 vol_index = wsr88d_get_vol_index(data_hdr.dataname);
418 fprintf(stderr,"wsr88d_load_ray_into_radar: Unknown dataname %s. "
419 "isweep = %d, iray = %d.\n", data_hdr.dataname, isweep,
424 case DZ_INDEX: f = DZ_F; invf = DZ_INVF;
425 type_str = "Reflectivity"; break;
426 case VR_INDEX: f = VR_F; invf = VR_INVF;
427 type_str = "Velocity"; break;
428 case SW_INDEX: f = SW_F; invf = SW_INVF;
429 type_str = "Spectrum width"; break;
430 case DR_INDEX: f = DR_F; invf = DR_INVF;
431 type_str = "Diff. Reflectivity"; break;
432 case PH_INDEX: f = PH_F; invf = PH_INVF;
433 type_str = "Diff. Phase"; break;
434 case RH_INDEX: f = RH_F; invf = RH_INVF;
435 type_str = "Correlation Coef (Rho)"; break;
438 waveform = vcp_data.waveform[isweep];
440 /* Ignore short-range reflectivity from velocity split cuts unless
441 * merging of split cuts is suppressed. The indicators for this type of
442 * reflectivity are surveillance mode is 0 and elevation angle is
445 if (vol_index == DZ_INDEX && (vcp_data.surveil_prf_num[isweep] == 0 &&
446 vcp_data.fixed_angle[isweep] < 6.0 && !keep_hi_prf_dz))
449 /* Load the data for this field. */
450 if (radar->v[vol_index] == NULL) {
451 radar->v[vol_index] = RSL_new_volume(MAXSWEEPS);
452 radar->v[vol_index]->h.f = f;
453 radar->v[vol_index]->h.invf = invf;
454 radar->v[vol_index]->h.type_str = type_str;
456 if (radar->v[vol_index]->sweep[isweep] == NULL) {
457 radar->v[vol_index]->sweep[isweep] = RSL_new_sweep(MAXRAYS_M31);
458 radar->v[vol_index]->sweep[isweep]->h.f = f;
459 radar->v[vol_index]->sweep[isweep]->h.invf = invf;
461 ngates = data_hdr.ngates;
462 ray = RSL_new_ray(ngates);
464 /* Convert data to float, then use range function to store in ray.
465 * Note: data range is 2-255. 0 means signal is below threshold, and 1
466 * means range folded.
469 offset = data_hdr.offset;
470 scale = data_hdr.scale;
471 if (data_hdr.scale == 0) scale = 1.0;
472 data = &wsr88d_ray->data[data_index];
473 for (i = 0; i < ngates; i++) {
474 if (data_hdr.datasize_bits != 16) {
478 item = *(unsigned short *)data;
479 if (do_swap) swap_2_bytes(&item);
483 value = (item - offset) / scale;
484 else value = (item == 0) ? BADVAL : RFVAL;
485 ray->range[i] = invf(value);
489 wsr88d_load_ray_hdr(wsr88d_ray, ray);
490 ray->h.range_bin1 = data_hdr.range_first_gate;
491 ray->h.gate_size = data_hdr.range_samp_interval;
492 ray->h.nbins = ngates;
493 radar->v[vol_index]->sweep[isweep]->ray[iray] = ray;
494 radar->v[vol_index]->sweep[isweep]->h.nrays = iray+1;
495 } /* for each data field */
499 void wsr88d_load_sweep_header(Radar *radar, int isweep)
505 for (ivolume=0; ivolume < MAX_RADAR_VOLUMES; ivolume++) {
506 if (radar->v[ivolume] != NULL &&
507 radar->v[ivolume]->sweep[isweep] != NULL) {
508 sweep = radar->v[ivolume]->sweep[isweep];
509 nrays = sweep->h.nrays;
510 if (nrays == 0) continue;
511 last_ray = sweep->ray[nrays-1];
512 sweep->h.sweep_num = last_ray->h.elev_num;
513 sweep->h.elev = vcp_data.fixed_angle[isweep];
514 sweep->h.beam_width = last_ray->h.beam_width;
515 sweep->h.vert_half_bw = sweep->h.beam_width / 2.;
516 sweep->h.horz_half_bw = sweep->h.beam_width / 2.;
522 Radar *wsr88d_load_m31_into_radar(Wsr88d_file *wf)
524 Wsr88d_msg_hdr msghdr;
525 Wsr88d_ray_m31 wsr88d_ray;
526 short non31_seg_remainder[1202]; /* Remainder after message header */
527 int end_of_vos = 0, isweep = 0;
528 int msg_hdr_size, msg_size, n;
529 int sweep_hdrs_written = 0, prev_elev_num = 1, prev_raynum = 0, raynum = 0;
532 /* Message type 31 is a variable length message. All other types are made
533 * up of 1 or more segments, where each segment is 2432 bytes in length.
534 * To handle these differences, read the message header and check its type.
535 * If it is 31, use the size given in the message header to determine the
536 * number of bytes to read. If not, simply read the remainder of the
540 n = fread(&msghdr, sizeof(Wsr88d_msg_hdr), 1, wf->fptr);
542 /* printf("msgtype = %d\n", msghdr.msg_type); */
543 msg_hdr_size = sizeof(Wsr88d_msg_hdr) - sizeof(msghdr.rpg);
545 radar = RSL_new_radar(MAX_RADAR_VOLUMES);
547 while (! end_of_vos) {
548 if (msghdr.msg_type == 31) {
549 if (little_endian()) wsr88d_swap_m31_hdr(&msghdr);
551 /* Get size of the remainder of message. The given size is in
552 * halfwords, but we want it in bytes, so double it.
554 msg_size = (int) msghdr.msg_size * 2 - msg_hdr_size;
556 n = read_wsr88d_ray_m31(wf, msg_size, &wsr88d_ray);
557 /* Assume error message was issued from read routine */
558 if (n <= 0) return NULL;
559 raynum = wsr88d_ray.ray_hdr.azm_num;
560 if (raynum > MAXRAYS_M31) {
561 fprintf(stderr,"Error: raynum = %d, exceeds MAXRAYS_M31"
562 " (%d)\n", raynum, MAXRAYS_M31);
563 fprintf(stderr,"isweep = %d\n", isweep);
564 RSL_free_radar(radar);
568 /* Check for an unexpected start of new elevation, and issue a
569 * warning if this has occurred. This usually means less rays than
570 * expected. It happens, but rarely.
572 if (wsr88d_ray.ray_hdr.radial_status == START_OF_ELEV &&
573 sweep_hdrs_written != prev_elev_num) {
574 fprintf(stderr,"Warning: Radial status is Start-of-Elevation, "
575 "but End-of-Elevation was not\n"
576 "issued for elevation number %d. Number of rays = %d"
577 "\n", prev_elev_num, prev_raynum);
578 wsr88d_load_sweep_header(radar, isweep);
580 sweep_hdrs_written++;
581 prev_elev_num = wsr88d_ray.ray_hdr.elev_num - 1;
584 /* Load ray into radar structure. */
585 wsr88d_load_ray_into_radar(&wsr88d_ray, isweep, radar);
586 prev_raynum = raynum;
588 else { /* msg_type not 31 */
589 n = fread(&non31_seg_remainder, sizeof(non31_seg_remainder), 1,
592 fprintf(stderr,"Warning: load_wsr88d_m31_into_radar: ");
593 if (feof(wf->fptr) != 0)
594 fprintf(stderr, "Unexpected end of file.\n");
596 fprintf(stderr,"Read failed.\n");
597 fprintf(stderr,"Current sweep index: %d\n"
598 "Last ray read: %d\n", isweep, prev_raynum);
599 wsr88d_load_sweep_header(radar, isweep);
602 if (msghdr.msg_type == 5) {
603 wsr88d_get_vcp_data(non31_seg_remainder);
604 radar->h.vcp = vcp_data.vcp;
608 /* Check for end of sweep */
609 if (wsr88d_ray.ray_hdr.radial_status == END_OF_ELEV) {
610 wsr88d_load_sweep_header(radar, isweep);
612 sweep_hdrs_written++;
613 prev_elev_num = wsr88d_ray.ray_hdr.elev_num;
616 /* If not at end of volume scan, read next message header. */
617 if (wsr88d_ray.ray_hdr.radial_status != END_VOS) {
618 n = fread(&msghdr, sizeof(Wsr88d_msg_hdr), 1, wf->fptr);
620 fprintf(stderr,"Warning: load_wsr88d_m31_into_radar: ");
621 if (feof(wf->fptr) != 0) fprintf(stderr,
622 "Unexpected end of file.\n");
623 else fprintf(stderr,"Failed reading msghdr.\n");
624 fprintf(stderr,"Current sweep index: %d\n"
625 "Last ray read: %d\n", isweep, prev_raynum);
626 wsr88d_load_sweep_header(radar, isweep);
632 wsr88d_load_sweep_header(radar, isweep);
634 if (feof(wf->fptr) != 0) end_of_vos = 1;
635 } /* while not end of vos */