/* * Khoros: $Id$ */ #if !defined(__lint) && !defined(__CODECENTER__) static char rcsid[] = "Khoros: $Id$"; #endif /* * Copyright (C) 1997, INPE. Sergio Donizete Faria, Gerald Jean Francis Banon * All rights reserved. See $K_PROG/repos/license/License. */ /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<<<<<<<< >>>> >>>> Main program for klgmaxrel >>>> >>>> Private: >>>> main >>>> >>>> Static: >>>> Public: >>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<<<<<<<< */ #include "klgmaxrel.h" clui_info_struct *clui_info = NULL; /*----------------------------------------------------------- | | Routine Name: main() - Search for global maximum and generate a report | | Purpose: main program for klgmaxrel | | Input: | char *clui_info->i_img_file; {Input data object} | int clui_info->i_img_flag; {TRUE if -i_img specified} | | char *clui_info->f_file; {Formatted ASCII output file} | int clui_info->f_flag; {TRUE if -f specified} | | char *clui_info->o_img_file; {Resulting output data object} | int clui_info->o_img_flag; {TRUE if -o_img specified} | | double clui_info->lat_double; {Specify expected point latitude} | int clui_info->lat_flag; {TRUE if -lat specified} | | double clui_info->lon_double; {Specify expected point latitude} | int clui_info->lon_flag; {TRUE if -lon specified} | | double clui_info->alt_double; {Specify the altitude of expected point} | int clui_info->alt_flag; {TRUE if -alt specified} | | int clui_info->wsize_int; {Specifies the region width} | int clui_info->wsize_flag; {TRUE if -wsize specified} | | int clui_info->hsize_int; {Specifies the region height} | int clui_info->hsize_flag; {TRUE if -hsize specified} | | Output: | Returns: | | Written By: Sergio Donizete Faria | Date: February 18, 1998 | Modifications: | ------------------------------------------------------------*/ int main( int argc, char **argv) { /* -main_variable_list */ kobject src; /* source object - input search image */ kobject dest; /* destination object - output image */ kfile *ascii_file=NULL; char *filename=NULL; int output_ascii; int src_width; /* input image width (num cols) */ int src_height; /* input image height (num rows) */ int src_depth; /* depth and time give number of input images */ int src_time; int src_bands; /* number of bands per input image */ int num_regions, num_of_max, dim_src; int type1; int h, i, i0, i1, j, k, k0, k1; double lat_expected, lon_expected, alt_expected; double vmax; int region_width, region_height; int shift_region_width, shift_region_height, shift_i0, shift_j; double *src_data=NULL; double *dest_data=NULL; /* -main_variable_list_end */ khoros_init(argc, argv, "MATCHING", PRODUCT_RELEASE_DATE, PRODUCT_RELEASE_NAME, PRODUCT_RELEASE_VERSION, PRODUCT_RELEASE_MAJOR, PRODUCT_RELEASE_MINOR, "$MATCHING/objects/kroutine/klgmaxrel"); kexit_handler(klgmaxrel_free_args, NULL); /* -main_get_args_call */ kclui_init("MATCHING", "klgmaxrel", KGEN_KROUTINE, &clui_uis_spec, klgmaxrel_usage_additions, klgmaxrel_get_args, klgmaxrel_free_args); /* -main_get_args_call_end */ /* -main_before_lib_call */ /* * Open the source (src) and destination (dest) data objects using * kpds_open_input_object and kpds_open_output_object calls. These calls * will open the source and destination data object with the flags set * correctly. If an open fails, report the error using kerror, close * opened objects if necessary, and exit the program using kexit call. */ if ((src = kpds_open_input_object (clui_info->i_img_file)) == KOBJECT_INVALID) { kerror(NULL, "klgmaxrel", "Cannot open source object src."); kexit(KEXIT_FAILURE); } if ((dest = kpds_open_output_object (clui_info->o_img_file)) == KOBJECT_INVALID) { kerror(NULL, "klgmaxrel", "Cannot open destination object."); kpds_close_object(src); kexit(KEXIT_FAILURE); } /* * Copy the source object src to destination object. If the copy * function fails, report the error and exit. The object will be closed * by kexit. Since, the exit status is KEXIT FAILURE the output object * will not be written to a permanent transport. */ if (! kpds_copy_object (src, dest)) { kerror(NULL, "klgmaxrel", "kpds_copy_object failed"); kexit(KEXIT_FAILURE); } /* * Open the ASCII output file. */ output_ascii = TRUE; if (clui_info->f_flag) ascii_file = kfopen(clui_info->f_file, "w"); else output_ascii = FALSE; if (output_ascii) { filename = kbasename(clui_info->i_img_file, NULL); } /* * Check the datatype, and return if the data is complex. Set the * presentation type to double. */ if (! kpds_get_attribute(src, KPDS_VALUE_DATA_TYPE, &type1)) { kerror("klgmaxrel", "main", "kpds_get_attribute failed for KPDS_VALUE_DATA_TYPE"); kexit(KEXIT_FAILURE); } if ((type1 == KCOMPLEX) || (type1 == KDCOMPLEX)) { kerror("klgmaxrel", "main", "Cannot compute complex statistics at this time.\n"); kexit(KEXIT_FAILURE); } type1 = KDOUBLE; if (! kpds_set_attribute(src, KPDS_VALUE_DATA_TYPE, type1)) { kerror("klgmaxrel", "main", "kpds_set_attribute failed for KPDS_VALUE_DATA_TYPE"); kexit(KEXIT_FAILURE); } /* * Get the KPDS_VALUE_SIZE attribute of the source object (src) which * provides the information about number of data elements in each * direction of source object. */ if (! kpds_get_attribute (src, KPDS_VALUE_SIZE, &src_width, &src_height, &src_depth, &src_time, &src_bands)) { kerror(NULL,"klgmaxrel", "kpds_get_attribute failed for KPDS_VALUE_SIZE"); kexit(KEXIT_FAILURE); } /* * Specifying the size of the region to process by selection a set of * rows from the source image as the region process. * Set this region size to: * - src_width = width of region (width source image) * - 1 = height of region * - 1 = depth size of region * - 1 = time size of region * - 1 = number of bands in the region */ if (! kpds_set_attribute (src, KPDS_VALUE_REGION_SIZE, src_width, 1, 1, 1, 1)) { kerror("NULL", "klgmaxrel", "kpds_set_attribute failed for KPDS_VALUE_REGION_SIZE"); kexit(KEXIT_FAILURE); } if (! kpds_set_attribute (dest, KPDS_VALUE_REGION_SIZE, src_width, 1, 1, 1, 1)) { kerror("NULL", "klgmaxrel", "kpds_set_attribute failed for KPDS_VALUE_REGION_SIZE (dest)"); kexit(KEXIT_FAILURE); } /* * If ascii output, print the object size now */ if (output_ascii) { kfprintf(ascii_file,"\n"); } /* *********************************************************************** * Loop for search maximum *********************************************************************** */ /* * Outer loops: variable h and i are the number of images in input * object. */ dim_src = src_width * src_height; for (h = 0; h < src_time; h++) { for (i = 0; i < src_depth; i++) { /* * 1st Middle loop: variable is the number of bands (elements) * to process. * - src_bands = number of bands (elements) per image in source * object */ for ( k = 0; k < src_bands; k++) { /* * 2nd Middle loop: variable is the number of regions to * process * - num_regions = number of rows (height) of source object */ num_regions = src_height; j = 0; vmax = (- 1) * dim_src; for (i1 = 0; i1 < num_regions; i1++) { /* * Get a region of data and apply the operator across that * region. */ kpds_set_attribute(src, KPDS_VALUE_POSITION, 0, j, i, h, k); src_data = (double *)(kpds_get_data(src, KPDS_VALUE_REGION, src_data)); if (src_data == NULL) { kerror("NULL", "klgmaxrel", "kpds_get_data failed for KPDS_VALUE_REGION src."); kexit(KEXIT_FAILURE); } /* * Inner loop: variable is the number of columns of region * to be processed. * - src_width = width of source object (input image) */ /* * Global Maximum */ for (i0 = 0; i0 < src_width; i0++) { if (src_data[i0] > vmax) vmax = src_data[i0]; } j++; } } } }/* end of loop */ /* *********************************************************************** * Loop for generate image with maximum and some assessment measure *********************************************************************** */ /* * Outer loops: variable h and i are the number of images in input * object. */ lat_expected = clui_info->lat_double; lon_expected = clui_info->lon_double; alt_expected = clui_info->alt_double; num_of_max = 0; region_width = clui_info->wsize_int; region_height = clui_info->hsize_int; shift_region_width = region_width/2; shift_region_height = region_height/2; for (h = 0; h < src_time; h++) { for (i = 0; i < src_depth; i++) { /* * 1st Middle loop: variable is the number of bands (elements) * to process. * - src_bands = number of bands (elements) per image in source * object */ for ( k = 0; k < src_bands; k++) { /* * 2nd Middle loop: variable is the number of regions to * process * - num_regions = number of rows (height) of source object */ j = 0; for (i1 = 0; i1 < num_regions; i1++) { /* * Get a region of data and apply the operator across that * region. */ kpds_set_attribute(src, KPDS_VALUE_POSITION, 0, j, i, h, k); src_data = (double *)(kpds_get_data(src, KPDS_VALUE_REGION, src_data)); if (src_data == NULL) { kerror("NULL", "klgmaxrel", "kpds_get_data failed for KPDS_VALUE_REGION src."); kexit(KEXIT_FAILURE); } /* * Get a region of data in which temporarily store the * results of the operator. */ kpds_set_attribute(dest, KPDS_VALUE_POSITION, 0, j, i, h, k); dest_data = (double *)(kpds_get_data(dest, KPDS_VALUE_REGION, dest_data)); if (dest_data == NULL) { kerror("NULL", "klgmaxrel", "kpds_get_data failed for KPDS_VALUE_REGION dest."); kexit(KEXIT_FAILURE); } /* * Inner loop: variable is the number of columns of region * to be processed. * - src_width = width of source object (input image) */ /* * Generate binary image indicating where is the global * maximum and some assessment measure */ for (i0 = 0; i0 < src_width; i0++) { if (src_data[i0] == vmax) { dest_data[i0] = vmax; num_of_max ++; shift_i0 = i0 + shift_region_width; shift_j = j + shift_region_height; if (num_of_max <= 10) { if (output_ascii) kfprintf(ascii_file,"%d", shift_i0,","); kfprintf(ascii_file,","); kfprintf(ascii_file,"\n"); if (output_ascii) kfprintf(ascii_file,"%d", shift_j,","); kfprintf(ascii_file,","); kfprintf(ascii_file,"\n"); if (output_ascii) kfprintf(ascii_file,"%11.8f", lat_expected); kfprintf(ascii_file,","); kfprintf(ascii_file,"\n"); if (output_ascii) kfprintf(ascii_file,"%12.8f", lon_expected); kfprintf(ascii_file,","); kfprintf(ascii_file,"\n"); if (output_ascii) kfprintf(ascii_file,"%f", alt_expected); kfprintf(ascii_file,"\n"); kfprintf(ascii_file,"\n"); } if (num_of_max == 11) { if (output_ascii) kfprintf(ascii_file," ... "); kfprintf(ascii_file,"\n"); } } else { if (vmax == 0.0) { dest_data[i0] = 1.0; } else { dest_data[i0] = 0.0; } } } if (! kpds_put_data(dest, KPDS_VALUE_REGION, dest_data)) { kerror("NULL", "klgmaxrel", "kpds_put_data failed for destination object."); kexit(KEXIT_FAILURE); } j++; } } } }/* end of loop */ /* -main_before_lib_call_end */ /* -main_library_call */ /* -main_library_call_end */ /* -main_after_lib_call */ /* add history to the output object */ if (!kpds_set_attribute(dest, KPDS_HISTORY, kpds_history_string())) { kerror("klgmaxrel", "main", "Unable to set history on the destination object"); kexit(KEXIT_FAILURE); } /* * If the library call succeeded, close the source, kernel and * destination data objects using kpds_close_object and exit * using kexit. */ kpds_close_object(src); kpds_close_object(dest); /* -main_after_lib_call_end */ kexit(KEXIT_SUCCESS); return KEXIT_SUCCESS; } /*----------------------------------------------------------- | | Routine Name: klgmaxrel_usage_additions | | Purpose: Prints usage additions in klgmaxrel_usage routine | | Input: None | | Output: None | Written By: ghostwriter -oname klgmaxrel | Date: July 1, 1997 | Modifications: | ------------------------------------------------------------*/ void klgmaxrel_usage_additions(void) { kfprintf(kstderr, "\tSearch for global maximum and generate a report\n"); /* -usage_additions */ /* -usage_additions_end */ } /*----------------------------------------------------------- | | Routine Name: klgmaxrel_free_args | | Purpose: Frees CLUI struct allocated in klgmaxrel_get_args() | | Input: None | | Output: None | Written By: ghostwriter -oname klgmaxrel | Date: July 1, 1997 | Modifications: | ------------------------------------------------------------*/ /* ARGSUSED */ void klgmaxrel_free_args( kexit_status status, kaddr client_data) { /* do the wild and free thing */ if (clui_info != NULL) { kfree_and_NULL(clui_info->i_img_file); kfree_and_NULL(clui_info->f_file); kfree_and_NULL(clui_info->o_img_file); kfree_and_NULL(clui_info); } /* -free_handler_additions */ /* -free_handler_additions_end */ }