9 int const sample_number,
10 int const num_detectors,
15 double terrain_sample_position[][
MAX_PADDED][3]
280 #define MIN_COS_VIEW 0.087155743
287 double search_grid = 500.0;
288 double step_factor = 2.0;
292 PGSt_double unit_u[3];
304 double previous_height;
306 double previous_h_prime = 0.0;
309 double interpolation_weight = 0.0;
310 double minimum_height = 1.0;
311 static int hgtmin = 0;
312 static int hgtmax = 0;
315 static char correct_terrain[PGSd_PC_LINE_LENGTH_MAX]=
"";
316 char msgbuf[PGS_SMF_MAX_MSGBUF_SIZE];
317 char filefunc[] = __FILE__
", GEO_terrain_correct";
325 if( sample_number<0 || sample_number>=
MAX_PADDED ||
327 sample_view_vec ==
NULL || ecr_sample_position ==
NULL ||
328 ellip_sample_position ==
NULL || sample_flags ==
NULL ||
329 terrain_sample_position ==
NULL
332 sprintf(msgbuf,
"\nsample_number: %d, num_detectors: %d"
333 "sample_view_vec: %p, ecr_sample_position: %p"
334 "ellip_sample_position: %p, sample_flags: %p"
335 "terrain_sample_position: %p", sample_number,num_detectors,
336 (
void *)sample_view_vec, (
void *)ecr_sample_position,
337 (
void *)ellip_sample_position, (
void *)sample_flags,
338 (
void *)terrain_sample_position );
344 for (det = 0; det < num_detectors; det++) {
345 for (
i = 0;
i < 3;
i++)
346 terrain_sample_position[det][sample_number][
i] =
347 ellip_sample_position[det][sample_number][
i];
351 if ((correct_terrain[0] ==
'\0')&&(PGS_PC_GetConfigData(
359 if(strncmp(correct_terrain,
TERRAIN_CORRECT,
sizeof(correct_terrain))!=0)
361 for (det = 0; det < num_detectors; det++)
363 return PGS_S_SUCCESS;
368 for (det = 0; det < num_detectors; det++)
373 ellip_sample_position[det][sample_number][
LAT_IDX],
374 ellip_sample_position[det][sample_number][
LON_IDX],
377 sprintf(msgbuf,
"GEO_read_DEM(%f, %f) for detector %d",
378 ellip_sample_position[det][sample_number][
LAT_IDX],
379 ellip_sample_position[det][sample_number][
LON_IDX], det);
388 cos(ellip_sample_position[det][sample_number][
LAT_IDX])*
389 cos(ellip_sample_position[det][sample_number][
LON_IDX]);
391 cos(ellip_sample_position[det][sample_number][
LAT_IDX])*
392 sin(ellip_sample_position[det][sample_number][
LON_IDX]);
394 sin(ellip_sample_position[det][sample_number][
LAT_IDX]);
398 for (
i = 0;
i < 3; ++
i)
399 u[
i] = - sample_view_vec[det][sample_number][
i];
403 sprintf(msgbuf,
"u for detector %d", det);
411 for (
i = 0;
i < 3; ++
i)
414 cos_view = PGS_CSC_dotProduct(unit_n, unit_u, 3);
420 if(cos_view > 1.0) cos_view =1.0;
425 D_max = (
double)hgtmax / cos_view;
426 D_min = (
double)hgtmin / cos_view;
429 sin_view = sqrt(1.0 - cos_view*cos_view);
431 if( (
double)(hgtmax - hgtmin) < minimum_height)
434 D = 0.5*(D_max + D_min);
438 if(sin_view*(D_max-D_min) < search_grid/step_factor)
440 delta_D = (D_max-D_min)*step_factor;
442 delta_D = search_grid / sin_view;
447 num_steps = 1+(
int)ceil( (D_max-D_min)/delta_D + 0.1);
454 D0 = 0.5*( D_max + D_min + (
double)(num_steps-1)*delta_D );
463 for(step = 0; (step < num_steps) && (height<h_prime) &&
464 !(sample_flags[det][sample_number] &
BAD_TERRAIN); step++)
466 previous_height = height;
467 previous_h_prime = h_prime;
471 D = D0 - (
double)step*delta_D;
475 for (
i = 0;
i < 3;
i++)
476 x[
i] = ecr_sample_position[det][sample_number][
i]
484 sprintf(msgbuf,
"PGS_CSC_ECRtoGEO() on detector %d", det);
493 sprintf(msgbuf,
"GEO_latlon2height(%f, %f) on detector %d",
512 interpolation_weight = (height-h_prime) /
513 (height - previous_height + previous_h_prime - h_prime);
516 D = D0 + ( interpolation_weight-(
double)(step-1) ) * delta_D;
521 if (
fabs(D) > minimum_height)
523 for (
i = 0;
i < 3; ++
i)
524 ecr_sample_position[det][sample_number][
i]
528 if(PGS_CSC_ECRtoGEO(ecr_sample_position[det][sample_number],
532 sprintf(msgbuf,
"PGS_CSC_ECRtoGEO() on detector %d", det);
539 terrain_sample_position[det][sample_number][
LAT_IDX] =
541 terrain_sample_position[det][sample_number][
LON_IDX] =
543 terrain_sample_position[det][sample_number][
HT_IDX] =