-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcarpi.c
302 lines (271 loc) · 9.61 KB
/
carpi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
NASA/TRMM, Code 910.1.
This is the TRMM Office Radar Software Library.
Copyright (C) 1996, 1997
Mike Kolander
Space Applications Corporation
Vienna, Virginia
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* Mike Kolander
* Space Applications Corporation
* NASA/Goddard 910.1
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "rsl.h"
#define RAD2DEG 57.29578 /* radian to degree conversion */
#define MAXRAYS 512 /* loop safety valve when traversing a sweep */
extern int radar_verbose_flag;
/*************************************************************/
/* */
/* RSL_free_carpi */
/* */
/*************************************************************/
void RSL_free_carpi(Carpi *carpi)
{
/* Frees memory allocated to a carpi structure, and associated
pointer and data arrays.
*/
if (carpi != NULL)
{
if (carpi->data != NULL)
{
if (carpi->data[0] != NULL)
free(carpi->data[0]); /* Free the 2D data array. */
free(carpi->data); /* Free the vector of pointers. */
}
free(carpi); /* Free the carpi structure. */
}
}
/*************************************************************/
/* */
/* RSL_new_carpi */
/* */
/*************************************************************/
Carpi *RSL_new_carpi(int nrows, int ncols)
{
/* Allocates memory for a carpi structure, and associated pointer
and data arrays.
*/
Carpi *c;
Carpi_value *data;
int row;
/* Allocate a carpi. */
c = (Carpi *)calloc(1, sizeof(Carpi));
if (c == NULL) perror("RSL_new_carpi");
/* Allocate a vector of 'nrows' pointers. */
c->data = (Carpi_value **)calloc(nrows, sizeof(Carpi_value *));
if (c->data == NULL) perror("RSL_new_carpi");
/* Allocate a 2 dimensional array for the actual data values. */
data = (Carpi_value *)calloc(nrows*ncols, sizeof(Carpi_value));
if (data == NULL) perror("RSL_new_carpi");
/* Fill all the 'nrows' pointer slots created above. */
for (row=0; row<nrows; row++)
c->data[row] = data + row*ncols;
return(c);
}
/*************************************************************/
/* */
/* RSL_volume_to_carpi */
/* */
/*************************************************************/
Carpi *RSL_volume_to_carpi(Volume *v, float h, float grnd_r,
float dx, float dy, int nx, int ny,
int radar_x, int radar_y, float lat, float lon)
/*
* Creates a carpi from a volume structure.
*
* +--------------------+ ^
* | | |
* | | |
* | cell size dx,dy | ny
* | | |
* | | |
* +--------------------+ V
* lat,lon
* <-------- nx -------->
*
* Radar centered at radar_x, radar_y.
*/
{
Cappi *cappi;
Carpi *carpi;
if (v == NULL) return NULL;
cappi = RSL_cappi_at_h(v, h, grnd_r);
cappi->lat = lat;
cappi->lon = lon;
cappi->interp_method = 0;
carpi = RSL_cappi_to_carpi(cappi, dx, dy, lat, lon, nx, ny,
radar_x, radar_y);
if (carpi == NULL) return NULL;
RSL_free_cappi(cappi);
return carpi;
}
/*************************************************************/
/* */
/* RSL_find_rng_azm */
/* */
/*************************************************************/
void RSL_find_rng_azm(float *r, float *ang, float x, float y)
{
/* Convert Cartesian coords (x,y) to polar (rng,angle); 0<angle<360 */
*r = sqrt((double)x*x + (double)y*y);
if (x != 0.0)
{
*ang = RAD2DEG*atan((double)y/x);
if (x > 0.0)
*ang = 90.0 - *ang;
else
*ang = 270.0 - *ang;
}
else /* x is 0.0 */
{
if (y >= 0.0) *ang = 0.0;
else *ang = 180.0;
}
}
/*************************************************************/
/* */
/* RSL_cappi_to_carpi */
/* */
/*************************************************************/
Carpi *RSL_cappi_to_carpi(Cappi *cappi, float dx, float dy, float lat,
float lon, int nx, int ny, int radar_x, int radar_y)
/****** Simple and straightforward algorithm:
Divide each of the nx*ny carpi cells into scx*scy subcells.
Find the data value for each subcell from the cappi rays.
Average the subcell data values over a cell to obtain the cell value.
Store the cell value into the 2_D carpi array.
********/
{
Carpi *carpi;
Ray *first_ray;
int row, col, j, k, m, n, scx, scy, valid_subcells;
float x, y, rng, azm, cell, cell_diag, gate_size;
float carpi_max_rng, radar_max_rng;
float subcell[3][3]; /* Maximum of 9 subcells per carpi cell*/
if (cappi == NULL) return NULL;
if (cappi->sweep == NULL) return NULL;
first_ray = RSL_get_first_ray_of_sweep(cappi->sweep);
if (first_ray == NULL) return NULL; /* No data. */
if (radar_verbose_flag) fprintf(stderr,"\nCreating carpi...\n");
/* Allcate space for a carpi, and fill in its values. */
carpi = RSL_new_carpi(ny, nx);
carpi->month = cappi->month;
carpi->day = cappi->day;
carpi->year = cappi->year;
carpi->hour = cappi->hour;
carpi->minute = cappi->minute;
carpi->sec = cappi->sec;
carpi->dx = dx;
carpi->dy = dy;
carpi->nx = nx;
carpi->ny = ny;
carpi->radar_x = radar_x;
carpi->radar_y = radar_y;
carpi->height = cappi->height;
carpi->lat = lat;
carpi->lon = lon;
strncpy(carpi->radar_type, cappi->radar_type, sizeof(cappi->radar_type));
carpi->field_type = cappi->field_type;
carpi->interp_method = cappi->interp_method;
carpi->f = first_ray->h.f;
carpi->invf = first_ray->h.invf;
gate_size = first_ray->h.gate_size / 1000.0; /* km */
cell_diag = sqrt(dx*dx + dy*dy);
/* How many subcells per carpi cell? The larger the carpi cell relative
to gate_size, the more subcells we want. Will have scx*scy subcells
per carpi cell. Note below that the max no. of subcells is 9 . */
if ((dy - gate_size) < 0.0)
scy = 1;
else if ((dy - gate_size) <= 1.0)
scy = 2;
else
scy = 3;
if ((dx - gate_size) < 0.0)
scx = 1;
else if ((dx - gate_size) <= 1.0)
scx = 2;
else
scx = 3;
/* Max range of the radar data */
radar_max_rng = first_ray->h.nbins * gate_size; /* km */
/* Max range of desired carpi is max of x and y directions. */
carpi_max_rng = nx / 2.0 * dx; /* km */
if ( (ny / 2.0 * dy) > carpi_max_rng )
carpi_max_rng = ny / 2.0 * dy;
/* carpi_max_rng is smaller of (radar_max_rng, carpi_max_rng) */
if (radar_max_rng < carpi_max_rng)
carpi_max_rng = radar_max_rng;
if (radar_verbose_flag)
fprintf(stderr,"carpi_max_rng:%.1f(km) beam_width:%.1f gate_size:%d(m)\n",
carpi_max_rng, cappi->sweep->h.beam_width, first_ray->h.gate_size);
/* For each cell (row,col) comprising the carpi...*/
for (row=0; row<ny; row++)
{
for (col=0; col<nx; col++)
{
/* For each subcell (there are scx*scy subcells per cell)
of carpi cell (row,col)...*/
for (n=0; n<scy; n++)
{
/* Convert carpi coords (row,col) to km coords (x,y) relative to
radar. */
y = (row + n/(float)scy - radar_y)*dy;
for (m=0; m<scx; m++)
{
x = (col + m/(float)scx - radar_x)*dx;
/* Find range and azimuth of subcell (relative to radar) */
RSL_find_rng_azm(&rng, &azm, x, y);
/* Check if carpi cell outside of data range by noting
location of lower left corner of cell */
if (m == 0)
{
if (n == 0)
if ((rng - cell_diag) > carpi_max_rng)
{
/* Totality of carpi cell lies outside data range. */
cell = (float) BADVAL;
goto escape;
}
} /* end if (m == 0) */
/* cell lies in data range. Get data value from cappi. */
subcell[n][m] = RSL_get_value_from_cappi(cappi,rng,azm);
} /* end for (m=... */
} /* end for (n=... */
/* All subcell values have now been determined. Average them
to determine the cell value. */
valid_subcells = 0; /* number subcells having valid data values.*/
cell = 0.0;
for (j=0; j<scy; j++)
{
for (k=0; k<scx; k++)
if ((subcell[j][k] <= BADVAL) && (subcell[j][k] >= NOECHO))
continue;
else /* valid data value. */
{
cell = cell + subcell[j][k];
valid_subcells++;
}
} /* end for (j=0; j<scy; j++) */
if (valid_subcells != 0) cell = cell/valid_subcells;
else cell = (float) BADVAL;
escape:
carpi->data[row][col] = (Carpi_value) carpi->invf(cell);
} /* end for (col=... */
} /* end for (row=... */
return(carpi);
}