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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
|
/*
NASA/TRMM, Code 910.1.
This is the TRMM Office Radar Software Library.
Copyright (C) 1996 Dennis F. Flanigan Jr. of Applied Research Corporation,
Landover, Maryland, a NASA/GSFC on-site contractor.
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.
*/
/*
* Interpolation functions
*
* Dennis Flanigan, Jr.
*
* 7/7/95
* Finished testing interpolation routine.
*
* 7/6/95
* Finished up bilinear interpolation routine.
* Routine calculates values using four surrounding
* values with the same slant range.
*
* 6/29/95
* Added bilinear interpolation value routine. Rewrote
* get_surrounding sweeps so that it returns Sweeps instead of
* Sweep indexes.
*
* 6/28/95
* Added internal ray searching routine. Replaces
* RSL_get_next_closest_ray.
*
* 6/25/95
* Added internal sweep searching routine designed for use
* by interpolation routines. Replaced RSL_get_next_closest_sweep.
*
* 6/23/95
* This file was created. Started adding internal routines
* needed for calculating distences between two points
* in space.
*/
#include <stdio.h>
#include <math.h>
#include "rsl.h"
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
extern double hypot(double, double);
/***************************************************/
/* */
/* get_xyz_coord */
/* */
/* */
/***************************************************/
void get_xyz_coord(double *x,double *y,double *z,
double range,double azim,double elev)
{
/* Return x,y,z coordinates given range, azimuth angle and
* elevation angle. Memory allocation for x,y and z are
* not provided by this routine!
*/
double azim_rad,elev_rad;
# define M_PI 3.14159265358979323846
azim_rad = azim * M_PI / 180.0;
elev_rad = elev * M_PI / 180.0;
*x = cos(elev_rad) * cos(azim_rad) * range;
*y = cos(elev_rad) * sin(azim_rad) * range;
*z = sin(elev_rad) * range;
}
/***************************************************/
/* */
/* get_dist */
/* */
/* */
/***************************************************/
float get_dist(float r1,float a1,float e1,
float r2,float a2,float e2)
{
/* Give two points described by range, azimuth angle
* and elevation angle, return the distence between
* the two.
*/
double x1,y1,z1,x2,y2,z2;
get_xyz_coord(&x1,&y1,&z1,(double)r1,(double)a1,(double)e1);
get_xyz_coord(&x2,&y2,&z2,(double)r2,(double)a2,(double)e2);
return (float) sqrt(pow(x1 - x2,2) + pow(y1 - y2,2) + pow(z1 - z2,2));
}
/***************************************************/
/* */
/* get_surrounding_sweeps */
/* */
/* */
/***************************************************/
void get_surrounding_sweep(Sweep **below,Sweep **above, Volume *v,
float elev)
{
/* Return the pointers of the sweeps that are above and
* and below the elevation angle in the parameter list.
*
* Assume at least one non-NULL sweep exist in Volume.
*
* A value of NULL is set to above or below in cases
* where there is no sweep above or below.
*/
int a;
/* look for above index first */
a = 0;
while(a < v->h.nsweeps)
{
if(v->sweep[a] != NULL)
{
if(v->sweep[a]->h.elev >= elev)
{
*above = v->sweep[a];
break;
}
}
a++;
}
/* Was above ever set ?
* If not, set above to counter.
*/
if(a == v->h.nsweeps)
{
*above = NULL;
}
/* Look for index just below elev */
a--;
while(a >= 0)
{
if(v->sweep[a] != NULL)
{
if(v->sweep[a]->h.elev <= elev)
{
*below = v->sweep[a];
break;
}
}
a--;
}
/* Was a below found ? */
if (a == -1)
{
*below = NULL;
}
/* Bye */
}
/******************************************
* *
* dir_angle_diff *
* *
* Dennis Flanigan,Jr. 4/29/95 *
******************************************/
double dir_angle_diff(float x,float y)
{
/* returns difference between angles x and y. Returns
* positive value if y > x, negitive value if
* y < x.
*
*/
double d;
d = (double)(y - x);
while (d >= 180) d = -1 * (360 - d);
while (d < -180) d = (360 + d);
return d;
}
/***************************************************/
/* */
/* get_surrounding_rays */
/* */
/* */
/***************************************************/
void get_surrounding_ray(Ray **ccwise,Ray **cwise,Sweep *s,
float ray_angle)
{
/* Return the pointers to the rays that are counterclockwise
* and clockwise to the ray_angle in the parameter list.
* Memory space for the variables ccwise and cwise
* is not allocated by this routine.
*
* Assume at least two rays exist and that a hash
* table has been created.
*
* Will need to add test for first ray and last
* ray if this routine is going to be used for
* RHI scans.
*/
int hindex;
double close_diff;
Hash_table *hash_table;
Azimuth_hash *closest;
/* Find hash index close to hindex we want. This will
* used as a starting point for a search to the closest
* ray.
*/
hash_table = hash_table_for_sweep(s);
if (hash_table == NULL) return; /* Nada. */
hindex = hash_bin(hash_table,ray_angle);
/* Find hash entry with closest Ray */
closest = the_closest_hash(hash_table->indexes[hindex],ray_angle);
close_diff = dir_angle_diff(ray_angle,closest->ray->h.azimuth);
if(close_diff < 0)
{
/* Closest ray is counterclockwise to ray_angle */
*ccwise = closest->ray;
*cwise = closest->ray_high->ray;
}
else
{
/* Closest ray is clockwise to ray_angle. */
*cwise = closest->ray;
*ccwise = closest->ray_low->ray;
}
}
/***************************************************/
/* */
/* from_dB, to_dB */
/* */
/***************************************************/
double from_dB(double db)
{
return pow(10,db/10.0);
}
double to_dB(double value)
{
return 10.0 * log10(value);
}
/***************************************************/
/* */
/* get_linear_value_from_sweep */
/* */
/***************************************************/
double get_linear_value_from_sweep(Sweep *sweep,float srange,float azim,
float limit)
{
float ccw_db_value,cw_db_value;
double ccw_value,cw_value,value;
double delta_angle;
Ray *ccw_ray,*cw_ray;
get_surrounding_ray(&ccw_ray,&cw_ray,sweep,azim);
/* Assume that ccw_ray and cw_ray will be non_NULL */
if((azim - ccw_ray->h.azimuth) > limit)
{
ccw_value = -1;
}
else
{
ccw_db_value = RSL_get_value_from_ray(ccw_ray,srange);
/* Check to make sure this is a valid value */
if (ccw_db_value == BADVAL)
{
ccw_value = 0;
}
else
{
ccw_value = from_dB(ccw_db_value);
}
}
if((cw_ray->h.azimuth - azim) > limit)
{
cw_value = -1;
}
else
{
cw_db_value = RSL_get_value_from_ray(cw_ray,srange);
/* Check to make sure this is a valid value */
if (cw_db_value == BADVAL)
{
cw_value = 0;
}
else
{
cw_value = from_dB(cw_db_value);
}
}
if((cw_value != -1) && (ccw_value != -1))
{
/* Both the clockwise ray and the counterclockwise
* ray is valid.
*/
delta_angle = angle_diff(ccw_ray->h.azimuth,cw_ray->h.azimuth);
value=((angle_diff(azim,cw_ray->h.azimuth)/delta_angle)*ccw_value)
+ ((angle_diff(azim,ccw_ray->h.azimuth)/delta_angle) * cw_value);
}
else if((cw_value == -1) && (ccw_value == -1))
{
/* Neither ray is valid. */
value = -1;
}
else if(cw_value != -1)
{
/* cw_ray is only ray that is within limit. */
value = cw_value;
}
else
{
/* ccw_ray is only ray that is within limit. */
value = ccw_value;
}
return value;
}
/***************************************************/
/* */
/* RSL_get_linear_value */
/* */
/* */
/***************************************************/
float RSL_get_linear_value(Volume *v,float srange,float azim,
float elev,float limit)
{
/* Compute bilinear value from four surrounding values
* in Volume v. The four surrounding values
* are at a constant range.
*
* Limit is an angle used only to reject values
* in the azimuth plane.
*/
float db_value;
double value = 0;
double up_value, down_value;
double delta_angle;
Sweep *up_sweep,*down_sweep;
get_surrounding_sweep(&down_sweep,&up_sweep,v,elev);
/* Calculate interpolated value in sweep above
* requested point.
*/
if(up_sweep == NULL)
{
up_value = -1;
}
else
{
up_value = get_linear_value_from_sweep(up_sweep,srange,azim,limit);
}
/* Calculate interpolated value in sweep below requested point.
*/
if(down_sweep == NULL)
{
down_value = -1;
}
else
{
down_value = get_linear_value_from_sweep(down_sweep,srange,azim,limit);
}
/* Using the interpolated values calculated at the elevation
* angles in the above and below sweeps, interpolate a value
* for the elvation angle at the requested point.
*/
if((up_value != -1) && (down_value != -1))
{
delta_angle = angle_diff(up_sweep->h.elev,down_sweep->h.elev);
value =((angle_diff(elev,up_sweep->h.elev)/delta_angle) * down_value) +
((angle_diff(elev,down_sweep->h.elev)/delta_angle) * up_value);
}
else if((up_value == -1) && (down_value == -1))
{
value = -1;
}
else if(up_value != -1)
{
value = up_value;
}
else
{
value = down_value;
}
/* Convert back to dB value and return. */
if(value > 0)
{
db_value = (float)to_dB(value);
return db_value;
}
else
{
return BADVAL;
}
}
|