3 This is the TRMM Office Radar Software Library.
4 Copyright (C) 1996, 1997
6 Space Applications Corporation
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with this library; if not, write to the Free
21 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *---------------------------------------------------------------------
26 * Image generation functions:
28 * void RSL_load_color_table(char *infile, char buffer[256], int *ncolors);
29 * void RSL_load_red_table(char *infile);
30 * void RSL_load_green_table(char *infile);
31 * void RSL_load_blue_table(char *infile);
32 * void RSL_load_refl_color_table();
33 * void RSL_load_vel_color_table();
34 * void RSL_load_sw_color_table();
35 * void RSL_load_height_color_table();
36 * void RSL_load_rainfall_color_table();
37 * void RSL_bscan_ray(FILE *fp, Ray *r);
38 * void RSL_bscan_sweep(Sweep *s, char *outfile);
39 * void RSL_bscan_volume(Volume *v, char *basename);
40 * unsigned char *RSL_sweep_to_cart(Sweep *s, int xdim, int ydim, float range);
41 * unsigned char *RSL_rhi_sweep_to_cart(Sweep *s, int xdim, int ydim, float range, int vert_scale);
42 * void RSL_write_gif(char *outfile, unsigned char *image, int xdim, int ydim, char c_table[256][3]);
43 * void RSL_write_pict(char *outfile, unsigned char *image, int xdim, int ydim, char c_table[256][3]);
44 * void RSL_write_ppm(char *outfile, unsigned char *image, int xdim, int ydim, char c_table[256][3]);
45 * void RSL_write_pgm(char *outfile, unsigned char *image, int xdim, int ydim);
46 * void RSL_sweep_to_gif(Sweep *s, char *outfile, int xdim, int ydim, float range);
47 * void RSL_rhi_sweep_to_gif(Sweep *s, char *outfile, int xdim, int ydim, float range, int vert_scale);
48 * void RSL_sweep_to_pict(Sweep *s, char *outfile, int xdim, int ydim, float range);
49 * void RSL_sweep_to_ppm(Sweep *s, char *outfile, int xdim, int ydim, float range);
50 * void RSL_sweep_to_pgm(Sweep *s, char *outfile, int xdim, int ydim, float range);
51 * void RSL_volume_to_gif(Volume *v, char *basename, int xdim, int ydim, float range);
52 * void RSL_volume_to_pict(Volume *v, char *basename, int xdim, int ydim, float range);
53 * void RSL_volume_to_ppm(Volume *v, char *basename, int xdim, int ydim, float range);
54 * void RSL_volume_to_pgm(Volume *v, char *basename, int xdim, int ydim, float range);
55 * void RSL_rebin_velocity_ray(Ray *r);
56 * void RSL_rebin_velocity_sweep(Sweep *s);
57 * void RSL_rebin_velocity_volume(Volume *v);
58 * void RSL_rebin_zdr_ray(Ray *r);
59 * void RSL_rebin_zdr_sweep(Sweep *s);
60 * void RSL_rebin_zdr_volume(Volume *v);
65 extern FILE *popen(const char *, const char *);
66 extern int pclose(FILE *stream);
67 extern int radar_verbose_flag;
69 static char color_table[256][3];
70 static int ncolors = 0;
73 /**********************************************************************/
75 /* RSL_load_color_table */
77 /* By: John Merritt */
78 /* Space Applications Corporation */
80 /**********************************************************************/
81 void RSL_load_color_table(char *infile, char buffer[256], int *num_colors)
85 fp = fopen(infile, "r");
90 *num_colors = fread(buffer, sizeof(char), 256, fp);
95 /**********************************************************************/
97 /* RSL_set_color_table */
99 /* By: John Merritt */
100 /* SM&A Corporation */
102 /**********************************************************************/
103 void RSL_set_color_table(int icolor, char buffer[256], int ncolors)
106 /* In RSL: Red table=RSL_RED_TABLE,
107 * green table=RSL_GREEN_TABLE,
108 * blue table=RSL_BLUE_TABLE.
110 for (i=0; i<ncolors; i++) color_table[i][icolor] = buffer[i];
113 /**********************************************************************/
115 /* RSL_get_color_table */
117 /* By: John Merritt */
118 /* SM&A Corporation */
120 /**********************************************************************/
121 void RSL_get_color_table(int icolor, char buffer[256], int *ncolors)
124 /* In RSL: Red table=RSL_RED_TABLE,
125 * green table=RSL_GREEN_TABLE,
126 * blue table=RSL_BLUE_TABLE.
129 for (i=0; i<*ncolors; i++) buffer[i] = color_table[i][icolor];
132 /**********************************************************************/
134 /* RSL_load_red_table */
135 /* RSL_load_green_table */
136 /* RSL_load_blue_table */
138 /* By: John Merritt */
139 /* Space Applications Corporation */
141 /**********************************************************************/
142 void RSL_load_red_table(char *infile)
146 RSL_load_color_table(infile, buffer, &ncolors);
147 RSL_set_color_table(RSL_RED_TABLE, buffer, ncolors);
149 void RSL_load_green_table(char *infile)
153 RSL_load_color_table(infile, buffer, &ncolors);
154 RSL_set_color_table(RSL_GREEN_TABLE, buffer, ncolors);
156 void RSL_load_blue_table(char *infile)
160 RSL_load_color_table(infile, buffer, &ncolors);
161 RSL_set_color_table(RSL_BLUE_TABLE, buffer, ncolors);
165 /**********************************************************************/
167 /* RSL_load_refl_color_table */
168 /* RSL_load_vel_color_table */
169 /* RSL_load_sw_color_table */
170 /* RSL_load_zdr_color_table */
171 /* RSL_load_rainfall_color_table */
173 /* By: John Merritt */
174 /* Space Applications Corporation */
176 /**********************************************************************/
177 void RSL_load_refl_color_table()
180 char *fnames[3] = { REFL_RED_FILE, REFL_GREEN_FILE, REFL_BLUE_FILE };
184 for (igun=0; igun<3; igun++) {
185 RSL_load_color_table(fnames[igun], buffer, &ncolors);
186 for (i=0; i<ncolors; i++) color_table[i][igun] = buffer[i];
189 void RSL_load_vel_color_table()
192 char *fnames[] = { VEL_RED_FILE, VEL_GREEN_FILE, VEL_BLUE_FILE };
196 for (igun=0; igun<3; igun++) {
197 RSL_load_color_table(fnames[igun], buffer, &ncolors);
198 for (i=0; i<ncolors; i++) color_table[i][igun] = buffer[i];
201 void RSL_load_sw_color_table()
204 char *fnames[] = { SW_RED_FILE, SW_GREEN_FILE, SW_BLUE_FILE };
208 for (igun=0; igun<3; igun++) {
209 RSL_load_color_table(fnames[igun], buffer, &ncolors);
210 for (i=0; i<ncolors; i++) color_table[i][igun] = buffer[i];
214 void RSL_load_height_color_table()
217 char *fnames[] = { HEIGHT_RED_FILE, HEIGHT_GREEN_FILE, HEIGHT_BLUE_FILE };
221 for (igun=0; igun<3; igun++) {
222 RSL_load_color_table(fnames[igun], buffer, &ncolors);
223 for (i=0; i<ncolors; i++) color_table[i][igun] = buffer[i];
227 void RSL_load_zdr_color_table()
230 char *fnames[] = { ZDR_RED_FILE, ZDR_GREEN_FILE, ZDR_BLUE_FILE };
234 for (igun=0; igun<3; igun++) {
235 RSL_load_color_table(fnames[igun], buffer, &ncolors);
236 for (i=0; i<ncolors; i++) color_table[i][igun] = buffer[i];
240 void RSL_load_rainfall_color_table()
243 char *fnames[] = { RAINFALL_RED_FILE, RAINFALL_GREEN_FILE, RAINFALL_BLUE_FILE };
247 for (igun=0; igun<3; igun++) {
248 RSL_load_color_table(fnames[igun], buffer, &ncolors);
249 for (i=0; i<ncolors; i++) color_table[i][igun] = buffer[i];
252 /**********************************************************************/
254 /* Volume to BSCAN GIF */
255 /* (simple dump of rays) */
257 /* RSL_bscan_sweep */
258 /* RSL_bscan_volume */
260 /* By: John Merritt */
261 /* Space Applications Corporation */
263 /**********************************************************************/
264 static unsigned char *outvect;
266 void RSL_bscan_ray(Ray *r, FILE *fp)
271 if (r == NULL) return;
272 memset(outvect, 0, r->h.nbins);
274 for (i=0; i<r->h.nbins; i++)
275 if (f(r->range[i]) != BADVAL) {
276 if (f(r->range[i]) >= 0) outvect[i] = (unsigned char) f(r->range[i]);
279 outvect[i] = (unsigned char) (255 + f(r->range[i]));
281 for(i=0; i<r->h.nbins; i++)
282 if (fwrite(color_table[outvect[i]], sizeof(char), 3, fp) != 3)
283 fprintf(stderr, "RSL_bscan_ray: short write\n");
286 void RSL_bscan_sweep(Sweep *s, char *outfile)
288 int i, j, nrecs, nbins;
291 if (s == NULL) return;
293 fp = fopen(outfile,"w");
298 first_ray = RSL_get_first_ray_of_sweep(s);
300 nbins = first_ray->h.nbins;
301 fprintf(fp, "P6\n# %s\n%d %d\n255\n",outfile, nbins, nrecs);
303 outvect = (unsigned char *) calloc(nbins, sizeof(unsigned char));
304 if (outvect == NULL) {
305 perror((char *)outvect);
309 /* Now, write the bscan. */
310 for (j=0; j<s->h.nrays; j++) {
311 if (s->ray[j]) RSL_bscan_ray(s->ray[j], fp);
313 memset(outvect, 0, nbins);
314 for(i=0; i<nbins; i++)
315 if (fwrite(color_table[outvect[i]], sizeof(char), 3, fp) != 3)
316 fprintf(stderr, "RSL_bscan_sweep: short write\n");
323 void RSL_bscan_volume(Volume *v, char *basename)
328 RSL_load_refl_color_table();
329 outfile = (char *)calloc(strlen(basename)+7, sizeof(char));
330 for (i=0; i<v->h.nsweeps; i++) {
331 (void)sprintf(outfile,"bscan.%2.2d.ppm", i);
333 RSL_bscan_sweep(v->sweep[i], outfile);
334 if (radar_verbose_flag)
335 fprintf(stderr,"Output: %s\n", outfile);
341 /**********************************************************************/
342 /**********************************************************************/
344 /* Volume to cartesean GIF */
345 /* (polar to cartesean) */
347 /* By: John Merritt */
348 /* Space Applications Corporation */
350 /**********************************************************************/
351 /**********************************************************************/
355 /**********************************************************************/
357 /* RSL_sweep_to_cart */
359 /* By: John Merritt */
360 /* Space Applications Corporation */
362 /**********************************************************************/
363 unsigned char *RSL_sweep_to_cart(Sweep *s, int xdim, int ydim, float range)
365 /* Range specifies the maximum range to load that points into the image. */
375 static unsigned char *cart_image = NULL;
377 if (s == NULL) return NULL;
378 if (xdim != ydim || ydim < 0 || xdim < 0) {
379 fprintf(stderr, "(xdim=%d) != (ydim=%d) or either negative.\n", xdim, ydim);
382 cart_image = (unsigned char *) calloc(xdim*ydim, sizeof(unsigned char));
384 beam_width = s->h.beam_width/2.0 * 1.2;
385 if (beam_width == 0) beam_width = 1.2; /* Sane image generation. */
387 for (y=-ydim/2; y<ydim/2; y++)
388 for (x=-xdim/2; x<xdim/2; x++){/* Find azimuth and range, then search Volume. */
390 azim = (float)atan((double)y/(double)x)*180.0/3.14159;
392 if (y < 0) azim = -90.0;
394 if (y<0 && x<0) /* Quadrant 3 (math notation). */
396 else if (y>=0 && x<0) /* Quad: 2 */
399 /* Radar is clockwise increasing. */
403 if (azim < 0) azim += 360.0;
405 r = (float)sqrt((double)x*x + (double)y*y);
406 if (ydim < xdim) r *= range/(.5*ydim);
407 else r *= range/(.5*xdim);
408 if (r > range) val = BADVAL;
410 ray = RSL_get_closest_ray_from_sweep(s, azim, beam_width);
411 val = RSL_get_value_from_ray(ray, r);
413 the_index = (y+ydim/2)*ydim + (xdim-1)-(x+xdim/2);
414 if (val == BADVAL || val == NOTFOUND_V || val == NOTFOUND_H)
415 cart_image[the_index] = (unsigned char) 0;
417 cart_image[the_index] = (unsigned char) val;
419 cart_image[the_index] = (unsigned char) (256+val);
425 /**********************************************************************/
427 /* RSL_rhi_sweep_to_cart */
429 /* A modified version of RSL_sweep_to_cart() */
430 /* -> Handles rhi (vertical) sweeps */
432 /* Space Applications Corporation */
434 /**********************************************************************/
435 unsigned char *RSL_rhi_sweep_to_cart(Sweep *s, int xdim, int ydim,
436 float range, int vert_scale)
438 int x, y, xx, yy, i, j;
443 static unsigned char *rhi_cart_image = NULL;
444 static unsigned char *buffer = NULL;
446 if (s == NULL) return NULL;
448 buffer = (unsigned char *)calloc(xdim*ydim, sizeof(unsigned char));
449 if (rhi_cart_image == NULL)
450 rhi_cart_image = (unsigned char *)calloc(xdim*ydim, sizeof(unsigned char));
451 memset(buffer, (unsigned char)0, xdim*ydim);
452 memset(rhi_cart_image, (unsigned char)0, xdim*ydim);
454 for (y=0; y<xdim; y++)
455 for (x=0; x<ydim; x++){
457 angle = (float)atan((double)y/(double)x)*180.0/3.14159;
461 angle = 90.0 - angle;
462 r = (float)sqrt((double)x*x + (double)y*y);
466 val = RSL_get_value_from_sweep(s, angle, r);
468 /* A vertical sweep extends from 0 deg to perhaps 25 deg in
469 elevation. For proper orientation of a displayed rhi
471 1. reflect image about the line y=x .
472 2. translate image downward so that radar site is located
473 at the bottom left corner of the image. The sweep will
474 then extend upward from the bottom of the image.
476 xx = y; /* reflection about line y=x */
477 yy = x; /* reflection about line y=x */
478 the_index = (ydim - 1 - yy)*xdim + xx;
479 if (val == BADVAL || val == NOTFOUND_V || val == NOTFOUND_H)
480 buffer[the_index] = (unsigned char) 0;
482 buffer[the_index] = (unsigned char) val;
484 buffer[the_index] = (unsigned char) (256+val);
487 /* To see details in the sweep, it is customary to scale the image
488 so that the scale of the vertical axis is 5 or 10 times the scale
489 of the horizontal axis.
490 Copy each row of pixel values from buffer[] a constant multiple
491 number of times into rhi_cart_image[], in order to vertically
494 for (j=0; j<ydim/vert_scale; j++)
495 for (i=1; i<=vert_scale; i++)
496 memcpy(&rhi_cart_image[(ydim - i - vert_scale*j)*xdim],
497 &buffer[(ydim - 1 - j)*xdim], xdim);
499 return rhi_cart_image;
503 /**********************************************************************/
507 /* By: John Merritt */
508 /* Space Applications Corporation */
510 /**********************************************************************/
511 void RSL_write_gif(char *outfile, unsigned char *image, int xdim, int ydim, char c_table[256][3])
519 fprintf(stderr, "No image for file %s\n", outfile);
524 (void)sprintf(pipecmd, "ppmtogif > %s 2>/dev/null", outfile);
525 fpipe = popen(pipecmd, "w"); /* Global FILE * */
527 perror("RSL_write_gif1");
530 if (fprintf(fpipe, "P6\n# %s\n%d %d\n255\n",outfile, xdim, ydim) < 0) {
531 perror("RSL_write_gif2");
536 for (i=0; i<nbytes; i++)
537 if (fwrite(c_table[image[i]], sizeof(char), 3, fpipe) != 3) {
538 perror("RSL_write_gif3");
543 if(pclose(fpipe) != 0) perror("RSL_write_gif4");
545 /**********************************************************************/
549 /* By: John Merritt */
550 /* Space Applications Corporation */
552 /**********************************************************************/
553 void RSL_write_pict(char *outfile, unsigned char *image, int xdim, int ydim, char c_table[256][3])
561 fprintf(stderr, "No image for file %s\n", outfile);
565 (void)sprintf(pipecmd, "ppmtopict > %s 2>/dev/null", outfile);
566 fpipe = popen(pipecmd, "w"); /* Global FILE * */
567 fprintf(fpipe, "P6\n# %s\n%d %d\n255\n",outfile, xdim, ydim);
568 for (i=0; i<nbytes; i++)
569 if (fwrite(c_table[image[i]], sizeof(char), 3, fpipe) != 3)
570 fprintf(stderr, "RSL_write_pict: short write\n");
573 /**********************************************************************/
577 /* By: John Merritt */
578 /* Space Applications Corporation */
580 /**********************************************************************/
581 void RSL_write_ppm(char *outfile, unsigned char *image, int xdim, int ydim, char c_table[256][3])
588 fprintf(stderr, "No image for file %s\n", outfile);
592 fpipe = fopen(outfile, "w"); /* Global FILE * */
593 fprintf(fpipe, "P6\n# %s\n%d %d\n255\n",outfile, xdim, ydim);
594 for (i=0; i<nbytes; i++)
595 if (fwrite(c_table[image[i]], sizeof(char), 3, fpipe) != 3)
596 fprintf(stderr, "RSL_write_ppm: short write\n");
599 /**********************************************************************/
603 /* By: John Merritt */
604 /* Space Applications Corporation */
606 /**********************************************************************/
607 void RSL_write_pgm(char *outfile, unsigned char *image, int xdim, int ydim)
614 fprintf(stderr, "No image for file %s\n", outfile);
618 (void)sprintf(pipecmd, "gzip > %s.gz 2>/dev/null", outfile);
619 fpipe = popen(pipecmd, "w"); /* Global FILE * */
620 fprintf(fpipe, "P5\n# %s\n%d %d\n255\n",outfile, xdim, ydim);
621 if (fwrite(image, sizeof(char), nbytes, fpipe) != nbytes)
622 fprintf(stderr, "RSL_write_pgm: short write\n");
625 /* The following is commented and is for non compressed. */
628 fpipe = fopen(outfile, "w"); /* Global FILE * */
629 fprintf(fpipe, "P5\n# %s\n%d %d\n255\n",outfile, xdim, ydim);
630 (void)fwrite(image, sizeof(char), nbytes, fpipe);
635 /**********************************************************************/
637 /* RSL_sweep_to_gif */
639 /* By: John Merritt */
640 /* Space Applications Corporation */
642 /**********************************************************************/
643 void RSL_sweep_to_gif(Sweep *s, char *outfile, int xdim, int ydim, float range)
645 /* Currently range is not used. */
646 unsigned char *cart_image;
648 /* Assumes the color table is already loaded. */
649 if (s == NULL) return;
651 fprintf(stderr, "No colors in color table. Load color table first.\n");
654 cart_image = RSL_sweep_to_cart(s, xdim, ydim, range);
655 RSL_write_gif(outfile, cart_image, xdim, ydim, color_table);
659 /**********************************************************************/
661 /* RSL_rhi_sweep_to_gif */
663 /* Slightly modified version of RSL_sweep_to_gif() */
666 /* Space Applications Corporation */
668 /**********************************************************************/
669 void RSL_rhi_sweep_to_gif(Sweep *s, char *outfile, int xdim, int ydim,
670 float range, int vert_scale)
672 unsigned char *cart_image;
674 /* Assumes the color table is already loaded. */
675 if (s == NULL) return;
677 fprintf(stderr, "No colors in color table. Load color table first.\n");
680 cart_image = RSL_rhi_sweep_to_cart(s, xdim, ydim, range, vert_scale);
681 RSL_write_gif(outfile, cart_image, xdim, ydim, color_table);
685 /**********************************************************************/
687 /* RSL_sweep_to_pict */
689 /* By: John Merritt */
690 /* Space Applications Corporation */
692 /**********************************************************************/
693 void RSL_sweep_to_pict(Sweep *s, char *outfile, int xdim, int ydim, float range)
695 /* Currently range is not used. */
696 unsigned char *cart_image;
698 /* Assumes the color table is already loaded. */
699 if (s == NULL) return;
701 fprintf(stderr, "No colors in color table. Load color table first.\n");
704 cart_image = RSL_sweep_to_cart(s, xdim, ydim, range);
705 RSL_write_pict(outfile, cart_image, xdim, ydim, color_table);
708 /**********************************************************************/
710 /* RSL_sweep_to_ppm */
712 /* By: John Merritt */
713 /* Space Applications Corporation */
715 /**********************************************************************/
716 void RSL_sweep_to_ppm(Sweep *s, char *outfile, int xdim, int ydim, float range)
718 /* Currently range is not used. */
719 unsigned char *cart_image;
721 /* Assumes the color table is already loaded. */
722 if (s == NULL) return;
724 fprintf(stderr, "No colors in color table. Load color table first.\n");
727 cart_image = RSL_sweep_to_cart(s, xdim, ydim, range);
728 RSL_write_ppm(outfile, cart_image, xdim, ydim, color_table);
731 /**********************************************************************/
733 /* RSL_sweep_to_pgm */
735 /* By: John Merritt */
736 /* Space Applications Corporation */
738 /**********************************************************************/
739 void RSL_sweep_to_pgm(Sweep *s, char *outfile, int xdim, int ydim, float range)
741 /* Currently range is not used. */
742 unsigned char *cart_image;
744 if (s == NULL) return;
745 cart_image = RSL_sweep_to_cart(s, xdim, ydim, range);
746 RSL_write_pgm(outfile, cart_image, xdim, ydim);
750 /**********************************************************************/
752 /* RSL_volume_to_gif */
754 /* By: John Merritt */
755 /* Space Applications Corporation */
757 /**********************************************************************/
758 void RSL_volume_to_gif(Volume *v, char *basename, int xdim, int ydim, float range)
761 * Make a xdim by ydim cartesean image. Center of radar is xdim/2,ydim/2.
765 unsigned char *cart_image;
767 if (v == NULL) return;
769 fprintf(stderr, "No colors in color table. Load color table first.\n");
773 * Sweep 0 has Reflectivity only.
774 * Sweep 1 has Velocity and Spectrum width, No Reflectivity.
778 for (i=0; i<v->h.nsweeps; i++) {
779 (void)sprintf(outfile,"%s.%2.2d.gif", basename, i); /* File name: sweep.[0-10] */
780 if (v->sweep[i] == NULL) continue;
781 cart_image = RSL_sweep_to_cart(v->sweep[i], xdim, ydim, range);
782 if (radar_verbose_flag)
783 fprintf(stderr,"==> Sweep %d of %d\n",i, v->h.nsweeps);
784 if (cart_image != NULL) {
785 RSL_write_gif(outfile, cart_image, xdim, ydim, color_table);
786 printf("%s\n", outfile);
789 if (radar_verbose_flag)
790 fprintf(stderr,"No image. cart_image for sweep %d is NULL.\n", i);
795 /**********************************************************************/
797 /* RSL_volume_to_pict */
799 /* By: John Merritt */
800 /* Space Applications Corporation */
802 /**********************************************************************/
803 void RSL_volume_to_pict(Volume *v, char *basename, int xdim, int ydim, float range)
806 * Make a xdim by ydim cartesean image. Center of radar is xdim/2,ydim/2.
810 unsigned char *cart_image;
812 if (v == NULL) return;
814 fprintf(stderr, "No colors in color table. Load color table first.\n");
818 * Sweep 0 has Reflectivity only.
819 * Sweep 1 has Velocity and Spectrum width, No Reflectivity.
823 for (i=0; i<v->h.nsweeps; i++) {
824 cart_image = RSL_sweep_to_cart(v->sweep[i], xdim, ydim, range);
825 if (radar_verbose_flag)
826 fprintf(stderr,"==> Sweep %d of %d\n",i, v->h.nsweeps);
827 (void)sprintf(outfile,"%s.%2.2d.pict", basename, i); /* File name: sweep.[0-10] */
828 if (cart_image != NULL) {
829 RSL_write_pict(outfile, cart_image, xdim, ydim, color_table);
830 if (radar_verbose_flag)
831 fprintf(stderr,"Wrote: %s\n", outfile);
834 if (radar_verbose_flag)
835 fprintf(stderr,"No image. cart_image for sweep %d is NULL.\n", i);
840 /**********************************************************************/
842 /* RSL_volume_to_ppm */
844 /* By: John Merritt */
845 /* Space Applications Corporation */
847 /**********************************************************************/
848 void RSL_volume_to_ppm(Volume *v, char *basename, int xdim, int ydim, float range)
851 * Make a xdim by ydim cartesean image. Center of radar is xdim/2,ydim/2.
855 unsigned char *cart_image;
857 if (v == NULL) return;
859 fprintf(stderr, "No colors in color table. Load color table first.\n");
863 * Sweep 0 has Reflectivity only.
864 * Sweep 1 has Velocity and Spectrum width, No Reflectivity.
868 for (i=0; i<v->h.nsweeps; i++) {
869 cart_image = RSL_sweep_to_cart(v->sweep[i], xdim, ydim, range);
870 if (radar_verbose_flag)
871 fprintf(stderr,"==> Sweep %d of %d\n",i, v->h.nsweeps);
872 (void)sprintf(outfile,"%s.%2.2d.ppm", basename, i); /* File name: sweep.[0-10] */
873 if (cart_image != NULL) {
874 RSL_write_ppm(outfile, cart_image, xdim, ydim, color_table);
875 if (radar_verbose_flag)
876 fprintf(stderr,"Wrote: %s\n", outfile);
879 if (radar_verbose_flag)
880 fprintf(stderr,"No image. cart_image for sweep %d is NULL.\n", i);
884 /**********************************************************************/
886 /* RSL_volume_to_pgm */
888 /* By: John Merritt */
889 /* Space Applications Corporation */
891 /**********************************************************************/
892 void RSL_volume_to_pgm(Volume *v, char *basename, int xdim, int ydim, float range)
895 * Make a xdim by ydim cartesean image. Center of radar is xdim/2,ydim/2.
899 unsigned char *cart_image;
902 * Sweep 0 has Reflectivity only.
903 * Sweep 1 has Velocity and Spectrum width, No Reflectivity.
907 if (v == NULL) return;
908 for (i=0; i<v->h.nsweeps; i++) {
909 cart_image = RSL_sweep_to_cart(v->sweep[i], xdim, ydim, range);
910 if (radar_verbose_flag)
911 fprintf(stderr,"==> Sweep %d of %d\n",i, v->h.nsweeps);
912 (void)sprintf(outfile,"%s.%2.2d.pgm", basename, i); /* File name: sweep.[0-10] */
913 if (cart_image != NULL) {
914 RSL_write_pgm(outfile, cart_image, xdim, ydim);
915 if (radar_verbose_flag)
916 fprintf(stderr,"Wrote: %s\n", outfile);
919 if (radar_verbose_flag)
920 fprintf(stderr,"No image. cart_image for sweep %d is NULL.\n", i);
926 /***********************************************************************/
928 /* RSL_rebin_velocity_ray */
929 /* RSL_rebin_velocity_sweep */
930 /* RSL_rebin_velocity_volume */
932 /* By: John Merritt */
933 /* Space Applications Corporation */
936 /***********************************************************************/
937 void RSL_rebin_velocity_ray(Ray *r)
939 /* Rebin the velocity data to the range -nyquist, +nyquist.
940 * 14 bins are created centered at 0. It sets the proper color look up
941 * indexes. This function modifies Ray r.
945 float val /* val1 */;
947 int ncbins = 15; /* Number of color bins */
949 Range (*invf)(float x);
951 if (r == NULL) return;
953 nyquist = r->h.nyq_vel;
954 if (nyquist == 0.0) {
955 fprintf(stderr, "RSL_rebin_velocity_ray: nyquist == 0.0\n");
956 fprintf(stderr, "RSL_rebin_velocity_ray: Unable to rebin.\n");
961 for (i=0; i<r->h.nbins; i++) {
962 val = f(r->range[i]);
965 } else if (val != BADVAL) {
967 Okay, we want to shift the data to positive values
968 then we re-scale them by the number of color bins/nyquist
970 val = (int)(val/nyquist*(ncbins/2) + 1.0 + ncbins/2);
976 r->range[i] = invf(val);
981 void RSL_rebin_velocity_sweep(Sweep *s)
983 /* Rebin the velocity data to the range -nyquist, +nyquist.
984 * 14 bins are created centered at 0. It sets the proper color look up
985 * indexes. This function modifies Sweep s. Use this function prior
986 * RSL_sweep_to_cart. The binning is done in RSL_rebin_velocity_ray.
991 if (s == NULL) return;
993 for (i=0; i<s->h.nrays; i++)
994 RSL_rebin_velocity_ray(s->ray[i]);
998 void RSL_rebin_velocity_volume(Volume *v)
1000 /* Rebin the velocity data to the range -nyquist, +nyquist.
1001 * 14 bins are created centered at 0. It sets the proper color look up
1002 * indexes. This function modifies Volume v. Use this function prior
1003 * RSL_sweep_to_cart. The binning is done in RSL_rebin_velocity_ray.
1008 if (v == NULL) return;
1010 for (i=0; i<v->h.nsweeps; i++)
1011 RSL_rebin_velocity_sweep(v->sweep[i]);
1016 /**********************************************************************/
1018 /* RSL_carpi_to_cart */
1020 /* Space Applications Corporation */
1022 /**********************************************************************/
1023 unsigned char *RSL_carpi_to_cart(Carpi *carpi, int xdim, int ydim, float range)
1025 /* Converts carpi data from the native RSL 1_ or 2_byte format to
1026 unsigned char format for image generation. Also reverses the
1027 row order of the data so that displayed images come out right side up.
1029 int i, j, cart_index;
1030 unsigned char *cart_image = NULL;
1033 if (carpi == NULL) return NULL;
1034 cart_image = (unsigned char *) calloc(xdim*ydim, sizeof(unsigned char));
1036 for (j=0; j<ydim; j++)
1037 for (i=0; i<xdim; i++)
1039 cart_index = (ydim-1-j)*xdim + i; /* Reverse the row order. */
1040 val = carpi->f(carpi->data[j][i]);
1041 if (val == BADVAL || val == NOTFOUND_V || val == NOTFOUND_H)
1042 cart_image[cart_index] = (unsigned char) 0;
1044 cart_image[cart_index] = (unsigned char) val;
1046 cart_image[cart_index] = (unsigned char) (256+val); /*2's complement.*/
1051 /**********************************************************************/
1053 /* RSL_carpi_to_gif */
1055 /* Space Applications Corporation */
1057 /**********************************************************************/
1058 void RSL_carpi_to_gif(Carpi *carpi, char *outfile, int xdim, int ydim, float range)
1060 /* Currently range is not used. */
1061 unsigned char *cart_image;
1063 /* Assumes the color table is already loaded. */
1064 if (carpi == NULL) return;
1066 fprintf(stderr, "No colors in color table. Load color table first.\n");
1069 cart_image = RSL_carpi_to_cart(carpi, xdim, ydim, range);
1070 RSL_write_gif(outfile, cart_image, xdim, ydim, color_table);
1074 /**********************************************************************/
1076 /* RSL_carpi_to_pict */
1078 /* Space Applications Corporation */
1080 /**********************************************************************/
1081 void RSL_carpi_to_pict(Carpi *carpi, char *outfile, int xdim, int ydim, float range)
1083 /* Currently range is not used. */
1084 unsigned char *cart_image;
1086 /* Assumes the color table is already loaded. */
1087 if (carpi == NULL) return;
1089 fprintf(stderr, "No colors in color table. Load color table first.\n");
1092 cart_image = RSL_carpi_to_cart(carpi, xdim, ydim, range);
1093 RSL_write_pict(outfile, cart_image, xdim, ydim, color_table);
1097 /**********************************************************************/
1099 /* RSL_carpi_to_ppm */
1101 /* Space Applications Corporation */
1103 /**********************************************************************/
1104 void RSL_carpi_to_ppm(Carpi *carpi, char *outfile, int xdim, int ydim, float range)
1106 /* Currently range is not used. */
1107 unsigned char *cart_image;
1109 /* Assumes the color table is already loaded. */
1110 if (carpi == NULL) return;
1112 fprintf(stderr, "No colors in color table. Load color table first.\n");
1115 cart_image = RSL_carpi_to_cart(carpi, xdim, ydim, range);
1116 RSL_write_ppm(outfile, cart_image, xdim, ydim, color_table);
1120 /**********************************************************************/
1122 /* RSL_carpi_to_pgm */
1124 /* Space Applications Corporation */
1126 /**********************************************************************/
1127 void RSL_carpi_to_pgm(Carpi *carpi, char *outfile, int xdim, int ydim, float range)
1129 /* Currently range is not used. */
1130 unsigned char *cart_image;
1132 /* Assumes the color table is already loaded. */
1133 if (carpi == NULL) return;
1135 fprintf(stderr, "No colors in color table. Load color table first.\n");
1138 cart_image = RSL_carpi_to_cart(carpi, xdim, ydim, range);
1139 RSL_write_pgm(outfile, cart_image, xdim, ydim);
1143 /**********************************************************************/
1144 /* RSL_rebin_volume */
1145 /* More generic version of RSL_rebin_velocity. Allows for centering */
1146 /* +/- field like zdr or vel around zero. Width is the 1/2 width of */
1147 /* what is to be centered; e.g., +/- 5, width=5. */
1148 /* Space Applications Corporation */
1150 /**********************************************************************/
1151 void RSL_rebin_ray(Ray *r, int width)
1155 int ncbins = 15; /* Number of color bins */
1156 float (*f)(Range x);
1157 Range (*invf)(float x);
1159 if (r == NULL) return;
1162 if (nyquist == 0.0) {
1163 fprintf(stderr, "RSL_rebin_ray: nyquist == 0.0\n");
1164 fprintf(stderr, "RSL_rebin_ray: Unable to rebin.\n");
1169 for (i=0; i<r->h.nbins; i++) {
1170 val = f(r->range[i]);
1171 if (val == width+1) {
1173 } else if (val != BADVAL) {
1175 Okay, we want to shift the data to positive values
1176 then we re-scale them by the number of color bins/nyquist
1178 val = (int)(val/nyquist*(ncbins/2) + 1.0 + ncbins/2);
1183 r->range[i] = invf(val);
1187 void RSL_rebin_sweep(Sweep *s, int width)
1190 if (s == NULL) return;
1191 for (i=0; i<s->h.nrays; i++)
1192 RSL_rebin_ray(s->ray[i], width);
1195 void RSL_rebin_volume(Volume *v, int width)
1198 if (v == NULL) return;
1199 for (i=0; i<v->h.nsweeps; i++)
1200 RSL_rebin_sweep(v->sweep[i], width);
1203 /**********************************************************************/
1205 /* RSL_rebin_zdr_volume, _sweep, _ray */
1207 /* Space Applications Corporation */
1208 /**********************************************************************/
1210 Maps valid zdr values from the real set [-6.0, 10.0] onto the set of
1211 integers {0, 1, 2, ..., 35}, which forms the set of indices into
1212 the zdr color table. Used in conjunction with gif image generation
1217 -6.0 <= zdr < -4.0 --> black
1218 -4.0 <= zdr < -2.0 --> gray
1219 -2.0 <= zdr < 0.0 --> blue
1220 0.0 <= zdr < 2.0 --> green
1221 2.0 <= zdr < 4.0 --> orange
1222 4.0 <= zdr < 6.0 --> red
1223 6.0 <= zdr < 8.0 --> pink
1224 8.0 <= zdr < 10.0 --> white
1226 Space Applications Corporation
1230 void RSL_rebin_zdr_ray(Ray *r)
1234 float (*f)(Range x);
1235 Range (*invf)(float x);
1237 if (r == NULL) return;
1240 for (i=0; i<r->h.nbins; i++)
1242 val = f(r->range[i]);
1243 if ((val >= -6.0) && (val < 8.4)) val = (floor) ((val + 6.0) * 2.5);
1244 else if (val < 10.0) val = 35.0; /* Make all these white. */
1245 else val = 0; /* invalid zdr value */
1246 r->range[i] = invf(val);
1250 void RSL_rebin_zdr_sweep(Sweep *s)
1253 if (s == NULL) return;
1254 for (i=0; i<s->h.nrays; i++) RSL_rebin_zdr_ray(s->ray[i]);
1257 void RSL_rebin_zdr_volume(Volume *v)
1260 if (v == NULL) return;
1261 for (i=0; i<v->h.nsweeps; i++) RSL_rebin_zdr_sweep(v->sweep[i]);