Skip to content

Commit

Permalink
Added error processing for appropriate default case conditions.
Browse files Browse the repository at this point in the history
Prior to this, these default conditions would keep on processing, but these are
fatal conditions and should be trapped.
  • Loading branch information
M. J. Brodzik committed Aug 16, 2024
1 parent dfc3dc7 commit 2ebdddf
Showing 1 changed file with 66 additions and 49 deletions.
115 changes: 66 additions & 49 deletions src/prod/meas_meta_setup/meas_meta_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ static int day_offset_from( int year, int month, int day,
double *offsetEpochTime );
static int check_for_consistent_regions( region_save *save_area,
setup_ltod_flag *ltdflag );
static void pixtolatlon(float x, float y, float *alon, float *alat,
int iopt, float ascale, float bscale, float a0, float b0);
static void latlon2pix(float alon, float alat, float *x, float *y,
static int pixtolatlon(float x, float y, float *alon, float *alat,
int iopt, float ascale, float bscale, float a0, float b0);
static void ease2grid(int iopt, float alon, float alat,
float *thelon, float *thelat, float ascale, float bscale);
static void iease2grid(int iopt, float *alon, float *alat,
float thelon, float thelat, float ascale, float bscale);
static int latlon2pix(float alon, float alat, float *x, float *y,
int iopt, float ascale, float bscale, float a0, float b0);
static int ease2grid(int iopt, float alon, float alat,
float *thelon, float *thelat, float ascale, float bscale);
static int iease2grid(int iopt, float *alon, float *alat,
float thelon, float thelat, float ascale, float bscale);
static double easeconv_normalize_degrees(double b);
static void f2ipix(float x, float y, int *ix, int *iy, int nsx, int nsy);
static int nint(float r);
Expand Down Expand Up @@ -1038,7 +1038,11 @@ int main(int argc,char *argv[])
dscale=save_area.sav_km[iregion];

/* transform center lat/lon location of measurement to image pixel location */
latlon2pix(cx, cy, &x, &y, projt, ascale, bscale, a0, b0);
if ( 0 != latlon2pix(cx, cy, &x, &y, projt, ascale, bscale, a0, b0) ) {
fprintf( stderr, "%s: fatal error in latlon2pix\n", __FILE__ );
exit ( -1 );
}

/* quantize pixel location to 1-based integer pixel indices */
f2ipix(x, y, &ix2, &iy2, nsx, nsy); /* note: ix2,iy2 are 1-based pixel address of center */

Expand All @@ -1055,12 +1059,14 @@ int main(int argc,char *argv[])
centers to the center of the output pixel */
x=(ix2+0.5f);
y=(iy2+0.5f);
pixtolatlon(x, y, &clon, &clat, projt, ascale, bscale, a0, b0);
if ( 0 != pixtolatlon(x, y, &clon, &clat, projt, ascale, bscale, a0, b0) ) {
fprintf( stderr, "%s: fatal error in pixtolatlon\n", __FILE__ );
exit ( -1 );
}

/* define size of box centered at(ix2,iy2) in which the gain response
is computed for each pixel in the box and tested to see if
the response exceeds a threshold. if so, it is used */

status = box_size_by_channel( ibeam, gsx->short_sensor,
base_resolution, &box_size );
if ( status != 0 ) {
Expand Down Expand Up @@ -1959,7 +1965,7 @@ static FILE * get_meta(char *mname, char *outpath,
* noffset : int **, ptr array to each region's section
* latlon_store : short int **, lat/lon locations of each pixel
*
* result : n/a
* result : 0 on success, 1 on failure with error written to stderr
*
*/
static void compute_locations(region_save *a, int *nregions, int **noffset,
Expand Down Expand Up @@ -2035,9 +2041,13 @@ static void compute_locations(region_save *a, int *nregions, int **noffset,
y=(iy+1.5f); /* center of pixel, 1-based */
for (ix=0; ix<a->sav_nsx[iregion]; ix++) {
x=(ix+1.5f); /* center of pixel, 1-based */
pixtolatlon(x, y, &clon, &clat, a->sav_projt[iregion],
a->sav_ascale[iregion], a->sav_bscale[iregion],
a->sav_a0[iregion], a->sav_b0[iregion]);
if ( 0 != pixtolatlon(x, y, &clon, &clat, a->sav_projt[iregion],
a->sav_ascale[iregion], a->sav_bscale[iregion],
a->sav_a0[iregion], a->sav_b0[iregion]) ) {
fprintf( stderr, "%s: fatal error in pixtolatlon\n", __FILE__ );
return 1;
}

iadd=a->sav_nsx[iregion]*iy+ix; /* zero-based lexicographic pixel address */
if (iadd<0) iadd=0;
if (iadd>=nsize) iadd=0;
Expand All @@ -2049,6 +2059,9 @@ static void compute_locations(region_save *a, int *nregions, int **noffset,
}
}
}

return 0;

}

/*
Expand Down Expand Up @@ -2165,6 +2178,7 @@ static void rel_latlon(float *x_rel, float *y_rel, float alon, float alat, float
* a failure will terminate the application upon returning to the main{}
*
* result : r, nominal km/pixel
*
*/
static float km2pix(float *x, float *y, int iopt, float ascale, float bscale, int *stat) {

Expand Down Expand Up @@ -2197,9 +2211,11 @@ static float km2pix(float *x, float *y, int iopt, float ascale, float bscale, in
*x=0.0;
*y=0.0;
*stat = 0;
fprintf( stderr, "%s: Unknown transformation type - %d region id\n", __FUNCTION__, iopt );
fprintf( stderr, "%s: Invalid projection iopt=%d\n", __FUNCTION__, iopt );
}

return(r);

}

/*
Expand Down Expand Up @@ -3345,12 +3361,10 @@ static int check_for_consistent_regions( region_save *save_area,
* alon, alat : float, (longitude, latitude) of lower-left corner of
* pixel (x, y)
*
* result : n/a
* result : 0 on success or 1 on failure, with error message to stderr
*
* FIXME: default action should exit with error message, it should not returen
* bogus coordinates
*/
static void pixtolatlon(float x, float y, float *alon, float *alat,
static int pixtolatlon(float x, float y, float *alon, float *alat,
int iopt, float ascale, float bscale, float a0, float b0) {

float thelon, thelat;
Expand All @@ -3361,13 +3375,18 @@ static void pixtolatlon(float x, float y, float *alon, float *alat,
case 10:
thelon = x - (float)1.0 + a0;
thelat = y - (float)1.0 + b0;
iease2grid(iopt, alon, alat, thelon, thelat, ascale, bscale);
if ( 0 != iease2grid(iopt, alon, alat, thelon, thelat, ascale, bscale) ) {
fprintf( stderr, "%s: Error calling iease2grid\n", __FUNCTION__);
return 1;
}
break;
default:
*alon=0.0;
*alat=0.0;
fprintf( stderr, "%s: Invalid projection iopt=%d\n", __FUNCTION__, iopt );
return 1;
}
return;

return 0;

}

/*
Expand All @@ -3385,27 +3404,32 @@ static void pixtolatlon(float x, float y, float *alon, float *alat,
* output :
* x, y : float, (col, row) pixel coordinates corresponding to (lat,lon)
*
* result : n/a
* result : 0 on success or 1 on failure with error message written to stderr
*
*/
static void latlon2pix(float alon, float alat, float *x, float *y,
int iopt, float ascale, float bscale, float a0, float b0) {
static int latlon2pix(float alon, float alat, float *x, float *y,
int iopt, float ascale, float bscale, float a0, float b0) {

static float thelon, thelat;

switch(iopt) {
case 8:
case 9:
case 10:
ease2grid(iopt, alon, alat, &thelon, &thelat, ascale, bscale);
if ( 0 != ease2grid(iopt, alon, alat, &thelon, &thelat, ascale, bscale) ) {
fprintf( stderr, "%s: Error calling ease2grid\n", __FUNCTION__);
return 1;
}
*x = thelon + (float)1.0 - a0;
*y = thelat + (float)1.0 - b0;
break;
default:
*x=0.0;
*y=0.0;
fprintf( stderr, "%s: Invalid projection iopt=%d\n", __FUNCTION__, iopt );
return 1;
}
return;

return 0;

}

/*
Expand All @@ -3425,16 +3449,14 @@ static void latlon2pix(float alon, float alat, float *x, float *y,
* thelon, thelat : float *, (col, row) pixel coordinates (can be outside of
* image)
*
* result : n/a
* result : 0 on success or 1 on failure with error written to stderr
*
* Implementation derived from NSIDC (MJ Brodzik) reference implementation
* "easeconv" written in IDL
*
* FIXME: default action should exit with error message, it should not return
* bogus coordinates
*/
static void ease2grid(int iopt, float alon, float alat,
float *thelon, float *thelat, float ascale, float bscale) {
static int ease2grid(int iopt, float alon, float alat,
float *thelon, float *thelat, float ascale, float bscale) {

double map_equatorial_radius_m,map_eccentricity, e2,
map_reference_latitude, map_reference_longitude,
Expand Down Expand Up @@ -3494,14 +3516,14 @@ static void ease2grid(int iopt, float alon, float alat,
y = ( map_equatorial_radius_m * q ) / ( 2.0 * kz );
break;
default:
fprintf(stderr,"*** invalid EASE2 projection specificaion %d in ease2grid\n",iopt);
break;
fprintf(stderr,"%s: invalid projection iopt=%d\n", __FUNCTION__, iopt);
return 1;
}

*thelon = (float) (r0 + ( x / map_scale ) + 0.5);
*thelat = (float) (s0 + ( y / map_scale ) + 0.5); /* 0 at bottom (BYU SIR files) */

return;
return 0;
}

/*
Expand All @@ -3521,17 +3543,14 @@ static void ease2grid(int iopt, float alon, float alat,
* alon, alat: float *, converted (lon, lat) (degrees) coordinates
* (can be outside of image)
*
* result : n/a
* result : 0 on success or 1 on failure, with error to stderr
*
* Implementation derived from NSIDC (MJ Brodzik) reference implementation
* "easeconv" written in IDL
*
* FIXME: default action should exit with error message, it should not return
* bogus coordinates
*
*/
static void iease2grid(int iopt, float *alon, float *alat,
float thelon, float thelat, float ascale, float bscale) {
static int iease2grid(int iopt, float *alon, float *alat,
float thelon, float thelat, float ascale, float bscale) {

double map_equatorial_radius_m,map_eccentricity, e2,
map_reference_latitude, map_reference_longitude,
Expand Down Expand Up @@ -3587,8 +3606,8 @@ static void iease2grid(int iopt, float *alon, float *alat,
lam = x / ( map_equatorial_radius_m * kz );
break;
default:
fprintf(stderr,"*** invalid EASE2 projection specification %d in iease2grid\n",iopt);
break;
fprintf(stderr,"%s: invalid projection iopt=%d\n", __FUNCTION__, iopt);
return 1;
}

phi = beta
Expand All @@ -3601,7 +3620,7 @@ static void iease2grid(int iopt, float *alon, float *alat,
*alat = (float) (UTILS_RTD * phi);
*alon = (float) (easeconv_normalize_degrees( map_reference_longitude + ( UTILS_RTD*lam ) ) );

return;
return 0;
}

/*
Expand Down Expand Up @@ -3664,8 +3683,6 @@ static void f2ipix(float x, float y, int *ix, int *iy, int nsx, int nsy) {
*
* result : nearest integer to r
*
* FIXME: this function is identical to nint, one of these should be replaced
* with the other
*/
static int nint(float r) {
int ret_val = r;
Expand Down

0 comments on commit 2ebdddf

Please sign in to comment.