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.
24 * Space Applications Corporation
31 extern int radar_verbose_flag;
33 /*************************************************************/
37 /*************************************************************/
38 void RSL_free_slice(Slice *slice)
40 /* Frees memory allocated to a slice structure, and associated
41 pointer and data arrays.
46 if (slice->data != NULL)
48 if (slice->data[0] != NULL)
49 free(slice->data[0]); /* Free the 2D data array. */
50 free(slice->data); /* Free the vector of pointers. */
52 free(slice); /* Free the slice structure. */
56 /*************************************************************/
60 /*************************************************************/
61 Slice *RSL_new_slice(int nrows, int ncols)
63 /* Allocates memory for a slice structure, and associated pointer
70 /* Allocate a slice. */
71 s = (Slice *)calloc(1, sizeof(Slice));
72 if (s == NULL) perror("RSL_new_slice");
73 /* Allocate a vector of 'nrows' pointers. */
74 s->data = (Slice_value **)calloc(nrows, sizeof(Slice_value *));
75 if (s->data == NULL) perror("RSL_new_slice");
76 /* Allocate a 2 dimensional array for the actual data values. */
77 data = (Slice_value *)calloc(nrows*ncols, sizeof(Slice_value));
78 if (data == NULL) perror("RSL_new_slice");
79 /* Fill all the 'nrows' pointer slots created above. */
80 for (row=0; row<nrows; row++)
81 s->data[row] = data + row*ncols;
86 /*************************************************************/
90 /*************************************************************/
91 Cube *RSL_new_cube(int ncarpi)
93 /* Allocate memory for a cube structure, and an associated array
98 cube = (Cube *) calloc(1, sizeof(Cube));
99 if (cube == NULL) return(NULL);
100 cube->carpi = (Carpi **) calloc(ncarpi, sizeof(Carpi *));
101 if (cube->carpi == NULL) return(NULL);
105 /*************************************************************/
109 /*************************************************************/
110 void RSL_free_cube(Cube *cube)
112 /* Frees memory allocated to a cube structure and associated
119 if (cube->carpi != NULL)
121 for (j=0; j<cube->nz; j++)
122 if (cube->carpi[j] != NULL)
123 RSL_free_carpi(cube->carpi[j]);
130 /*************************************************************/
132 /* RSL_volume_to_cube */
134 /*************************************************************/
135 Cube *RSL_volume_to_cube(Volume *v, float dx, float dy, float dz,
136 int nx, int ny, int nz, float grnd_r,
137 int radar_x, int radar_y, int radar_z)
138 /* radar_z = 0 is the only thing that makes sense. Why pass it? */
145 if (v == NULL) return NULL;
146 /* check validity of radar site coordinates in cube. */
147 if (radar_z != 0) return NULL;
148 if ((radar_x < 0) || (radar_x > nx)) return NULL;
149 if ((radar_y < 0) || (radar_y > ny)) return NULL;
151 cube = (Cube *)RSL_new_cube(nz);
152 if (cube == NULL) return NULL;
160 if (v->h.type_str != (char *) NULL)
161 cube->data_type = (char *)strdup(v->h.type_str);
165 /* Create nz carpis */
167 cube->carpi[i] = (Carpi *)RSL_volume_to_carpi(v, (i+1)*dz, grnd_r,
174 /*************************************************************/
176 /* RSL_get_slice_from_cube */
178 /*************************************************************/
179 Slice *RSL_get_slice_from_cube(Cube *cube, int x, int y, int z)
181 Check validity of parameters x,y,z , which define the plane of the
182 required slice. Two of the three parameters must equal -1 and the
183 third must be nonnegative; eg, the vertical plane y=100 is
184 specified by the parameters x=-1, y=100, z=-1
186 Assumes valid ranges for x, y, z are:
187 0 <= x <= nx-1 , 0 <= y <= ny-1 , 1 <= z <= nz
188 The range of z starts at 1 , since a cappi (or carpi) at
189 height z=0 makes no sense.
195 if (cube == NULL) return(NULL);
197 /* Slice defined by the plane y = const */
198 if ((x == -1) && (z == -1) && (y > -1) && (y < cube->ny))
200 slice = (Slice *) RSL_new_slice(cube->nz, cube->nx);
201 slice->data_type = (char *)strdup(cube->data_type);
202 slice->dx = cube->dx;
203 slice->dy = cube->dz;
204 slice->nx = cube->nx;
205 slice->ny = cube->nz;
206 slice->f = cube->carpi[0]->f;
207 slice->invf = cube->carpi[0]->invf;
208 /* Retrieve the required data values from the cube and place into
209 the slice structure. */
210 for (j=0; j<cube->nz; j++)
211 for (i=0; i<cube->nx; i++)
212 slice->data[j][i] = (Slice_value) cube->carpi[j]->data[y][i];
216 /* Slice defined by the plane x = const */
217 else if ((y == -1) && (z == -1) && (x > -1) && (x < cube->nx))
219 slice = (Slice *) RSL_new_slice(cube->nz, cube->ny);
220 slice->data_type = (char *)strdup(cube->data_type);
221 slice->dx = cube->dy;
222 slice->dy = cube->dz;
223 slice->nx = cube->ny;
224 slice->ny = cube->nz;
225 slice->f = cube->carpi[0]->f;
226 slice->invf = cube->carpi[0]->invf;
227 /* Retrieve the required data values from the cube and place into
228 the slice structure. */
229 for (j=0; j<cube->nz; j++)
230 for (i=0; i<cube->ny; i++)
231 slice->data[j][i] = (Slice_value) cube->carpi[j]->data[i][x];
235 /* Want slice defined by the plane z = const ; ie, a carpi */
236 else if ((x == -1) && (y == -1) && (z > 0) && (z <= cube->nz))
238 slice = (Slice *) RSL_new_slice(cube->ny, cube->nx);
239 slice->data_type = (char *)strdup(cube->data_type);
240 slice->dx = cube->dx;
241 slice->dy = cube->dy;
242 slice->nx = cube->nx;
243 slice->ny = cube->ny;
244 slice->f = cube->carpi[z-1]->f;
245 slice->invf = cube->carpi[z-1]->invf;
246 /* Just copy carpi data values into slice structure. */
247 for (j=0; j<cube->ny; j++)
248 for (i=0; i<cube->nx; i++)
249 slice->data[j][i] = (Slice_value) cube->carpi[z-1]->data[j][i];
253 else /* Invalid parameters. */
255 if (radar_verbose_flag)
257 fprintf(stderr,"\nRSL_get_slice_from_cube(): passed invalid parameters\n");
258 fprintf(stderr,"nx:%d ny:%d nz:%d x:%d y:%d z:%d\n",cube->nx,
259 cube->ny,cube->nz,x,y,z);