]> Pileus Git - ~andy/rsl/blob - src/rainbow.c
Build fixes for win32
[~andy/rsl] / src / rainbow.c
1 /*
2     NASA/TRMM, Code 912
3     This is the TRMM Office Radar Software Library.
4     Copyright (C) 2004
5             Bart Kelley
6             George Mason University
7             Fairfax, Virginia
8
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.
13
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.
18
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 Software
21     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "rsl.h"
28 #include "rainbow.h"
29
30 #include "win32compat.h"
31
32 static int get_param_int(char *buf)
33 {
34     /* Returns an integer parameter from a header line. */
35
36     int value;
37     char *substr;
38
39     substr = index(buf, ':');
40     sscanf(substr, ": %d", &value);
41     return value;
42 }
43
44 static float get_param_float(char *buf)
45 {
46     /* Returns a floating point parameter from a header line. */
47
48     float value;
49     char *substr;
50
51     substr = index(buf, ':');
52     sscanf(substr, ": %f", &value);
53     return value;
54 }
55
56
57 static char *get_param_string(char *buf)
58 {
59     /* Returns a string parameter from a header line. */
60
61     static char string[20];
62     char *substr;
63
64     substr = index(buf, ':');
65     sscanf(substr, ": %s", string);
66     return string;
67 }
68
69 void A_label(Rainbow_hdr *rainbow_header, char* buf)
70 {
71     int labelnum;
72     char label;
73
74     sscanf(buf, "%c%d", &label, &labelnum);
75
76     if (labelnum == 3) 
77         rainbow_header->az_step = get_param_float(buf);
78      else if (labelnum == 9)
79         rainbow_header->nsweeps = get_param_int(buf);
80 }
81
82 void F_label(Rainbow_hdr *rainbow_header, char* buf)
83 {
84     int labelnum, day, month, year, sec, minute, hour;
85     float lat, lon;
86
87     sscanf(buf, "%*c%d", &labelnum);
88
89     switch (labelnum) {
90         case 3:
91             rainbow_header->compressed = get_param_int(buf);
92             break;
93         case 4:
94             sscanf(buf, "%*c%*d : %f %f", &lon, &lat);
95             rainbow_header->lon = lon;
96             rainbow_header->lat = lat;
97             break;
98         case 5:
99             sscanf(buf, "%*c%*d : %d %d %d", &day, &month, &year);
100             rainbow_header->month = month;
101             rainbow_header->day = day;
102             rainbow_header->year = year;
103             break;
104         case 6:
105             sscanf(buf, "%*c%*d : %d %d %d", &sec, &minute, &hour);
106             rainbow_header->hour = hour;
107             rainbow_header->minute = minute;
108             rainbow_header->sec = sec;
109             break;
110         case 9:
111             rainbow_header->datatype = get_param_int(buf);
112             break;
113     }
114 }
115
116 void H_label(Rainbow_hdr *rainbow_header, char* buf)
117 {
118     int labelnum;
119     char label;
120
121     sscanf(buf, "%c%d", &label, &labelnum);
122
123     if (labelnum == 3)
124       rainbow_header->filetype = get_param_int(buf);
125     else if (labelnum == 8)
126       strcpy(rainbow_header->radarname, get_param_string(buf));
127 }
128
129 void P_label(Rainbow_hdr *rainbow_header, char* buf)
130 {
131     int labelnum;
132     char label;
133
134     sscanf(buf, "%c%d", &label, &labelnum);
135
136     if (labelnum == 3)
137         rainbow_header->range_start= get_param_float(buf);
138     else if (labelnum == 4)
139         rainbow_header->range_stop = get_param_float(buf);
140     else if (labelnum == 5)
141         rainbow_header->range_step = get_param_float(buf);
142 }
143
144 void R_label(Rainbow_hdr *rainbow_header, char* buf)
145 {
146     int labelnum;
147
148     sscanf(buf, "%*c%d", &labelnum);
149
150     if (labelnum == 1)
151       rainbow_header->nbins = get_param_int(buf);
152     else if (labelnum == 2)
153       rainbow_header->bin_resolution = get_param_float(buf);
154     else if (labelnum == 8)
155       rainbow_header->nvalues = get_param_int(buf);
156 }
157
158 void W_label(Rainbow_hdr *rainbow_hdr, char* buf)
159 {
160     int labelnum, az_start, az_stop, pw_code, prf_high, prf_low, zdata;
161     int vdata, wdata, unfolding, cdata, ddata, uzdata;
162     float elev, az_step, az_rate, range_stop;
163
164     sscanf(buf, "%*c%d : %f %d %d %f %f %d %d %d %d %d %d %d %f %d %d %d",
165             &labelnum, &elev, &az_start, &az_stop, &az_step,
166             &az_rate, &pw_code, &prf_high, &prf_low, &zdata, &vdata,
167             &wdata, &unfolding, &range_stop, &cdata, &ddata, &uzdata);
168
169     /* Note: Only need to collect parameters 1, 5, 7, 8, and 13 for each
170      * elevation.  Parameters 2, 3, and 4 are fixed at 0, 359, and 1 for volume
171      * scan.  The remaining parameters can be taken once from label number 1
172      * (first elevation).  Also, don't need az_step; got it from A3. 
173      */
174
175     if (labelnum == 1) {
176         rainbow_hdr->az_start = az_start;
177         rainbow_hdr->az_stop =  az_stop;
178         rainbow_hdr->pulse_width_code = pw_code;
179         rainbow_hdr->zdata = zdata;
180         rainbow_hdr->vdata = vdata;
181         rainbow_hdr->wdata = wdata;
182         rainbow_hdr->unfolding = unfolding;
183         rainbow_hdr->cdata = cdata;
184         rainbow_hdr->ddata = ddata;
185         rainbow_hdr->uzdata = uzdata;
186     }
187     rainbow_hdr->elev_params[labelnum-1] =
188         (struct elev_params *) malloc(sizeof(struct elev_params));
189     rainbow_hdr->elev_params[labelnum-1]->elev_angle = elev;
190     rainbow_hdr->elev_params[labelnum-1]->az_rate = az_rate;
191     rainbow_hdr->elev_params[labelnum-1]->prf_high = prf_high;
192     rainbow_hdr->elev_params[labelnum-1]->prf_low = prf_low;
193     rainbow_hdr->elev_params[labelnum-1]->maxrange = range_stop;
194 }
195
196 /**********************************************************/
197 /*                                                        */
198 /*                    read_hdr_line                       */
199 /*                                                        */
200 /**********************************************************/
201
202 static int read_hdr_line(char *buf, int maxchars, FILE *fp)
203 {
204     /* Read a line from the Rainbow file header into character buffer.
205      * Function returns the first character in buffer (the "label") if
206      * the line was successfully read, -1 otherwise.
207      *
208      * Note: the following control characters (defined in rainbow.h) are used
209      * in the Rainbow header:
210      *   CR  - Carriage Return: end of a line.
211      *   ETB - End of Transmission Block: divides header into sections (we can
212      *         ignore this one.)
213      *   ETX - End of Text: end of header.
214      */
215
216     int c = 0;
217     int badline = 0, i;
218
219     i = 0;
220     while ((c = getc(fp)) != CR && c != ETX) {
221         if (c == ETB) {               
222             c = getc(fp);                /* Read past both <ETB> and the */
223             if (c == CR) c = getc(fp);   /* combination <ETB><CR>.       */
224         }
225
226         buf[i++] = c;
227
228         if (i == maxchars) {
229             badline = 1;
230             break;
231         }
232     }
233
234     if (badline) {
235         fprintf(stderr,"A header line exceeded buffer size %d.\n",maxchars);
236         fprintf(stderr,"Did not find end-of-line character 0x%02x.\n",CR);
237         buf[maxchars - 1] = '\0';  /* Make it a legal string anyway. */
238         return -1;
239     }
240
241     buf[i] = '\0';
242
243     if (c != ETX) c = (int) buf[0];
244     return c;
245 }
246
247 /**********************************************************/
248 /*                                                        */
249 /*                  read_rainbow_header                   */
250 /*                                                        */
251 /**********************************************************/
252
253 #define BUFSIZE 128
254
255 void read_rainbow_header(Rainbow_hdr *rainbow_header, FILE *fp)
256 {
257     /* Reads parameters from Rainbow file header into a rainbow header
258        structure. */
259
260     char buf[BUFSIZE];
261     int label;
262
263     /* Read each line of the header and extract parameters according to the
264      * label.  The label is a single alphabetic character at the beginning
265      * of the line which indicates a category of parameters.
266      */
267
268     while ((label = read_hdr_line(buf, BUFSIZE, fp)) != ETX && label > 0) {
269         switch (label) {
270             case 'H': H_label(rainbow_header, buf);
271                       break;
272             case 'P': P_label(rainbow_header, buf);
273                       break;
274             case 'W': W_label(rainbow_header, buf);
275                       break;
276             case 'F': F_label(rainbow_header, buf);
277                       break;
278             case 'R': R_label(rainbow_header, buf);
279                       break;
280             case 'A': A_label(rainbow_header, buf);
281                       break;
282             case 'N': rainbow_header->product = get_param_int(buf);
283                       break;
284             case 'X': /* X_label(rainbow_header, buf); */
285                       break;
286         }
287     }
288 }