]> Pileus Git - ~andy/rsl/blob - dorade_to_radar.c
Initial import
[~andy/rsl] / dorade_to_radar.c
1 /*
2     NASA/TRMM, Code 910.1.
3     This is the TRMM Office Radar Software Library.
4     Copyright (C) 1996-1999
5             John H. Merritt
6             Space Applications Corporation
7             Vienna, 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
21     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <strings.h>
28 #define USE_RSL_VARS
29 #include "rsl.h"
30 #include "dorade.h"
31
32 extern int radar_verbose_flag;
33
34 /********************************************************************/
35 /*                                                                  */
36 /*                    find_rsl_field_index                          */
37 /*                                                                  */
38 /********************************************************************/
39 int find_rsl_field_index(char *dorade_field_name)
40 {
41   /*  
42    * Dorade: VE, DM, SW, DZ, ZDR, PHI, RHO, LDR,  DX,  CH,  AH,  CV,  AV
43    *    RSL: VR, DM, SW, DZ,  ZD,  PH,  RH,  LR, *DX, *CH, *AH, *CV, *AV.
44    */
45   if (strncasecmp(dorade_field_name, "ve", 2) == 0) return VR_INDEX;
46   if (strncasecmp(dorade_field_name, "dm", 2) == 0)     return DM_INDEX;
47   if (strncasecmp(dorade_field_name, "sw", 2) == 0)     return SW_INDEX;
48   if (strncasecmp(dorade_field_name, "dz", 2) == 0)     return DZ_INDEX;
49   if (strncasecmp(dorade_field_name, "zdr", 3) == 0) return ZD_INDEX;
50   if (strncasecmp(dorade_field_name, "phi", 3) == 0) return PH_INDEX;
51   if (strncasecmp(dorade_field_name, "rho", 3) == 0) return RH_INDEX;
52   if (strncasecmp(dorade_field_name, "ldr", 3) == 0) return LR_INDEX;
53   if (strncasecmp(dorade_field_name, "dx", 2) == 0)     return DX_INDEX;
54   if (strncasecmp(dorade_field_name, "ch", 2) == 0)     return CH_INDEX;
55   if (strncasecmp(dorade_field_name, "ah", 2) == 0)     return AH_INDEX;
56   if (strncasecmp(dorade_field_name, "cv", 2) == 0)     return CV_INDEX;
57   if (strncasecmp(dorade_field_name, "av", 2) == 0)     return AV_INDEX;
58
59   fprintf(stderr, "Unknown DORADE type <%s>\n", dorade_field_name);
60   return -1;
61 }
62
63 /* Secretly defined in uf_to_radar.c */
64 Volume *copy_sweeps_into_volume(Volume *new_volume, Volume *old_volume);
65
66 Radar *RSL_dorade_to_radar(char *infile)
67 {
68   Radar  *radar;
69   Volume *new_volume;
70   Sweep  *sweep;
71   Ray    *ray;
72   int iv, iray, iparam;
73
74   FILE  *fp;
75   Volume_desc     *vd;
76   Sensor_desc    **sd;
77   Sweep_record    *sr;
78   Radar_desc      *rd;
79   Data_ray        *dray;
80   Parameter_data  *pd;
81
82   int nsweep;
83   int i;
84   char buf[1024];
85
86   int degree, minute;
87   float second;
88
89   radar = NULL;
90   if (infile == NULL) {
91         int save_fd;
92         save_fd = dup(0);
93         fp = fdopen(save_fd, "r");
94   }  else
95     if((fp=fopen(infile, "r"))==(FILE *)NULL) {
96           perror(infile);
97           return radar;
98     }
99
100   fp = uncompress_pipe(fp); /* Transparently, use gunzip. */
101
102   /**********************************************************************/
103
104   vd = dorade_read_volume_desc(fp);   /* R E A D */
105   if (radar_verbose_flag)       dorade_print_volume_desc(vd);  /* P R I N T */
106
107   /* R E A D */
108   sd = (Sensor_desc **) calloc(vd->nsensors, sizeof(Sensor_desc *));
109   for (i=0; i<vd->nsensors; i++) {
110         sd[i] = dorade_read_sensor(fp);
111   }
112
113   /* P R I N T */
114   if (radar_verbose_flag) {
115         for (i=0; i<vd->nsensors; i++) {
116           fprintf(stderr, "============ S E N S O R   # %d =====================\n", i);
117           dorade_print_sensor(sd[i]);
118         }
119   }
120   /* R E A D   sweeps. */
121   if (vd->nsensors > 1) {
122         fprintf(stderr, "RSL_dorade_to_radar: Unable to process for more than 1 sensor.\n");
123         fprintf(stderr, "RSL_dorade_to_radar: Number of sensors is %d\n", vd->nsensors);
124         return NULL;
125   }
126
127   /* Use sensor 0 for vitals. */
128   rd = sd[0]->radar_desc;
129
130   radar = RSL_new_radar(MAX_RADAR_VOLUMES);
131   radar->h.month = vd->month;
132   radar->h.day   = vd->day;
133   radar->h.year  = vd->year;
134   radar->h.hour  = vd->hour;
135   radar->h.minute = vd->minute;
136   radar->h.sec    = vd->second;
137   sprintf(radar->h.radar_type, "dorade");
138   radar->h.number = 0;
139   strncpy(radar->h.name, vd->flight_num, sizeof(radar->h.name));
140   strncpy(radar->h.radar_name, rd->radar_name, sizeof(radar->h.radar_name));
141   strncpy(radar->h.project, vd->project_name, sizeof(radar->h.project));
142   sprintf(radar->h.city, "Unknown");
143   strncpy(radar->h.state, "UKN", 3);
144   sprintf(radar->h.country, "Unknown");
145   /* Convert lat to d:m:s */
146   degree = (int)rd->latitude;
147   minute = (int)((rd->latitude - degree) * 60);
148   second = (rd->latitude - degree - minute/60.0) * 3600.0;
149   radar->h.latd = degree;
150   radar->h.latm = minute;
151   radar->h.lats = second;
152   /* Convert lat to d:m:s */
153   degree = (int)rd->longitude;
154   minute = (int)((rd->longitude - degree) * 60);
155   second = (rd->longitude - degree - minute/60.0) * 3600.0;
156   radar->h.lond = degree;
157   radar->h.lonm = minute;
158   radar->h.lons = second;
159   radar->h.height = rd->altitude * 1000.0;
160   radar->h.spulse = 0; /* FIXME */
161   radar->h.lpulse = 0; /* FIXME */
162   
163   /* Begin volume code. */
164   /* We don't know how many sweeps per volume exist, until we read
165    * the file.  So allocate a large number of pointers, hope we don't
166    * exceed it, and adjust the pointer array at the end.  This is 
167    * efficient because we'll be manipulating pointers to the sweeps and
168    * not the sweeps themselves.
169    */
170
171   if (radar_verbose_flag)
172         fprintf(stderr, "Number of parameters: %d\n", rd->nparam_desc);
173
174   /* All the parameters are together, however, their order within
175    * the ray is not guarenteed.  For instance, VE could appear after
176    * DM.  For this we'll keep a list of parameter names and perform
177    * a (linear) search.  The result will be an index into the RSL
178    * volume array (radar->v[i]).  It is likely that the order will
179    * consistant within a file, therefore, we'll keep track of the index of
180    * our previous parameter type and begin the search from there; the next
181    * index should be a match.
182    *
183    * The dorade parameter names and the rsl mapping is:
184    *
185    * Dorade: VE, DM, SW, DZ, ZDR, PHI, RHO, LDR,  DX,  CH,  AH,  CV,  AV
186    *    RSL: VR, DM, SW, DZ,  ZD,  PH,  RH,  LR, *DX, *CH, *AH, *CV, *AV.
187    * 
188    *    * means this is a new RSL name.
189    */
190
191 #define DORADE_MAX_SWEEP 20
192   nsweep = 0;
193   while((sr = dorade_read_sweep(fp, sd))) {
194         for(iray = 0; iray < sr->nrays; iray++) {
195           dray = sr->data_ray[iray];
196
197           /* Now, loop through the parameters and fill the rsl structures. */
198           for (iparam = 0; iparam < dray->nparam; iparam++) {
199                 pd = dray->parameter_data[iparam];
200                 iv = find_rsl_field_index(pd->name);
201                 if (radar->v[iv] == NULL) {
202                   radar->v[iv] = RSL_new_volume(DORADE_MAX_SWEEP); /* Expandable */
203                 } else if (nsweep >= radar->v[iv]->h.nsweeps) {
204                   /* Must expand the number of sweeps. */
205                   /* Expand by another DORADE_MAX_SWEEP. */
206                   if (radar_verbose_flag) {
207                         fprintf(stderr, "nsweeps (%d) exceeds radar->v[%d]->h.nsweeps (%d).\n", nsweep, iv, radar->v[iv]->h.nsweeps);
208                         fprintf(stderr, "Increading it to %d sweeps\n", radar->v[iv]->h.nsweeps+DORADE_MAX_SWEEP);
209                   }
210                   new_volume = RSL_new_volume(radar->v[iv]->h.nsweeps+DORADE_MAX_SWEEP);
211                   /* Look in uf_to_radar.c for 'copy_sweeps_into_volume' */
212                   new_volume = copy_sweeps_into_volume(new_volume, radar->v[iv]);
213                   radar->v[iv] = new_volume;
214                 }
215
216                 /* Allocate the ray and load the parameter data. */
217                 if ((sweep = radar->v[iv]->sweep[nsweep]) == NULL) {
218                   sweep = radar->v[iv]->sweep[nsweep] = RSL_new_sweep(sr->s_info->nrays);
219               sweep->h.sweep_num    = sr->s_info->sweep_num;
220                   sweep->h.elev         = sr->s_info->fixed_angle;
221           sweep->h.beam_width   = rd->horizontal_beam_width;
222                   sweep->h.vert_half_bw = radar->v[iv]->sweep[nsweep]->h.beam_width / 2.0;
223                   sweep->h.horz_half_bw = rd->horizontal_beam_width / 2.0;
224                   sweep->h.f = RSL_f_list[iv];
225                   sweep->h.invf = RSL_invf_list[iv];
226                 }
227                 
228
229                 if ((ray = sweep->ray[iray]) == NULL) {
230                   if (radar_verbose_flag)
231                         fprintf(stderr, "Allocating %d bins for ray %d\n", dray->data_len[iparam], iray);
232                   ray = sweep->ray[iray] = RSL_new_ray(dray->data_len[iparam]);
233                 }
234
235                 /* Copy the ray data into the RSL ray. */
236
237         /* .... fill here .... */
238           }
239         }
240         nsweep++;
241         if (radar_verbose_flag) fprintf(stderr, "______NEW SWEEP__<%d>____\n", nsweep);
242         /* Save for loading into volume structure. */
243         dorade_free_sweep(sr);
244   }
245
246   /* The following avoids a broken pipe message, since a VOLD at the end
247    * is not read yet.
248    */
249   while(fread(buf, sizeof(buf), 1, fp)) continue;  /* Read til EOF */
250
251   rsl_pclose(fp);
252
253   return radar;
254 }
255