]> Pileus Git - ~andy/rsl/blob - examples/any_to_gif.c
f5980334d687f8d9f43ea30a13df3c8204dbb8cc
[~andy/rsl] / examples / any_to_gif.c
1 /*
2  * Ingest NEXRAD (wsr88d) data and output images representing
3  * all field types found.
4  *
5  * This example is the most minimum of coding that you need to do
6  * to achieve good results from using the RSL code.
7  *
8  * This is short and sweet to demonstrate the simplicity of use for
9  * the RSL.
10  *
11  */
12
13 #define USE_RSL_VARS
14 #include "rsl.h"
15
16 /* Example use for getopt; argument processing */
17 /* ------
18  * Remember that to pass back strings you must pass the
19  *    address of the pointer to the string; argument is char **.
20  *    (Note: arrays of characters; the name is char **)
21  *    Ex.
22  *    char *file
23  *    process_args(... &file);
24  *    DECLARE as: process_args(... char **file)
25  *       *file = strdup(argv[optind]);
26  *
27  *    char infile[80];
28  *    process_args(... infile ...);
29  *    DECLARE as: process_args(... char *infile ...)
30  *       strcpy(infile, argv[optind]);
31  */
32 #include <stdio.h>
33 #include <getopt.h>
34 #include <stdlib.h>
35 #include <string.h>
36 int usage(char **argv)
37 {
38   fprintf(stderr, "Usage: %s (v1.20) [-v] [-V] [-x n] [-y n] [-r x] infile [callid_or_firstfile]\n\n", argv[0]);
39   fprintf(stderr, "Where: -v  = verbose print.  Default = no printing.\n");
40   fprintf(stderr, "       -V  = Output entire volume.  Default = output first sweep only.\n");
41   fprintf(stderr, "       -x n = Image dimension, image is n x n pixels. Default = 400x400 pixels.\n");
42   fprintf(stderr, "       -r x = Range of radar data. Default = 400.0 km.\n");
43   fprintf(stderr, "       -b x = Make black for dbz below x. Default = 0 (no action).\n");
44   exit(-1);
45 }
46 process_args(int argc, char **argv,
47                          char **in_file, char **callid, int *verbose, int *wholevolume,
48                          int *xdim, float *range, int *dbz_black)
49 {
50   int c;
51   
52   while ((c = getopt(argc, argv, "x:r:b:vV")) != -1)
53         switch (c) {
54         case 'v': *verbose = 1;  break;
55         case 'V': *wholevolume = 1;  break;
56         case 'x': *xdim = atoi(optarg);  break;
57         case 'r': *range = atof(optarg);  break;
58         case 'b': *dbz_black = atoi(optarg);  break;
59         case '?': usage(argv); break;
60         default:  break;
61         }
62
63 /* must have 2 files listed */
64   if (argc - optind != 1 && argc - optind != 2) usage(argv);
65
66 /* Can use strdup, if desired */
67 /* strdup allocates memory */
68 /* in_file = strdup(argv[optind]); */
69   *in_file = strdup(argv[optind]);
70   if (argc - optind == 2) *callid = strdup(argv[optind+1]);
71   else *callid = NULL;
72
73 }
74
75
76 void main(int argc, char **argv)
77 {
78   Radar *radar;
79   Sweep *sweep;
80   Ray   *ray;
81   int   i, j;
82   char fname[1000];
83   char *infile, *callid;
84   char time_string[100];
85   int verbose;
86   int wholevolume;
87   int xdim;
88   float range;
89   int dbz_black;
90
91   verbose = 0;
92   wholevolume = 0;
93   xdim  = 400;
94   range = 400.0;
95   dbz_black = 0;
96   process_args(argc, argv, &infile, &callid, &verbose, &wholevolume,
97                            &xdim, &range, &dbz_black);
98
99   if (verbose)
100         RSL_radar_verbose_on(); /* Not needed; it bides the time. */
101   RSL_select_fields("all", NULL);
102   RSL_read_these_sweeps("all", NULL);
103   radar = RSL_anyformat_to_radar(infile, callid);
104
105   if (radar == NULL) exit(-1);
106
107   if (0) {
108         RSL_write_radar(radar, "rsl.rsl");
109         exit(0);
110   }
111
112   sprintf(time_string,"%2.2d%2.2d%2.2d_%2.2d%2.2d", 
113                   radar->h.month, radar->h.day, radar->h.year-1900, 
114                   radar->h.hour, radar->h.minute);
115
116   for (i=0; i<MAX_RADAR_VOLUMES; i++) {
117         sweep = RSL_get_first_sweep_of_volume(radar->v[i]);
118         ray   = RSL_get_first_ray_of_volume(radar->v[i]);
119
120         if (sweep) {
121           if (i == SW_INDEX)
122                 RSL_load_sw_color_table();
123           else if (i == VR_INDEX || i == VE_INDEX) {
124                 RSL_load_vel_color_table();
125                 RSL_rebin_velocity_volume(radar->v[i]);
126           } else
127                 RSL_load_refl_color_table();
128
129           {
130                 char r[256], g[256], b[256];
131                 int nc;
132                 int i;
133                 RSL_get_color_table(RSL_RED_TABLE, r, &nc);
134                 RSL_get_color_table(RSL_GREEN_TABLE, g, &nc);
135                 RSL_get_color_table(RSL_BLUE_TABLE, b, &nc);
136                 for (i=0; i<dbz_black; i++) {
137                   r[i]=(char)0;
138                   g[i]=(char)0;
139                   b[i]=(char)0;
140                 }
141                 RSL_set_color_table(RSL_RED_TABLE, r, nc);
142                 RSL_set_color_table(RSL_GREEN_TABLE, g, nc);
143                 RSL_set_color_table(RSL_BLUE_TABLE, b, nc);
144           }
145           memcpy(fname,radar->h.name, sizeof(radar->h.name));
146           for (j=0; j<sizeof(radar->h.name); j++) 
147                 if (fname[j] == '\0' || fname[j] == ' ') break;
148           if (j==sizeof(radar->h.name)) j--;
149
150           if (! wholevolume) {
151                 sprintf(&fname[j], "_%s_%s.gif", RSL_ftype[i],  time_string);
152                 /*        printf("FNAME = <%s>\n", fname); */
153                 RSL_sweep_to_gif(sweep, fname, xdim, xdim, range);
154                 printf("%s\n", fname);
155           } else {
156                 sprintf(&fname[j], "_%s_%s", RSL_ftype[i],  time_string);
157                 /* RSL_volume_to_gif outputs to stdout the filenames produced. */
158                 RSL_volume_to_gif(radar->v[i], fname, xdim, xdim, range);
159           }
160         }
161   }
162   exit(0);
163
164 }
165
166
167