// file: $USFS/util/usfs_eval/v1.0/usfs_eval.cc
//

// system include files
//
#include <stdio.h>

// isip include files
//
#include <image_eval.h>
#include "usfs_eval.h"

#include <integral_constants.h>
#include "image_eval_constants.h"
#include "usfs_eval_constants.h"

// main program
//
int main(int argc, char** argv) {

  // declare command line arguments
  //
  char_1 tst_file[ISIP_MAX_FNAME_SIZE];
  char_1 ref_file[ISIP_MAX_FNAME_SIZE];

  // declare flags for computation modes
  //
  logical_1 flag_histogram = ISIP_FALSE;
  logical_1 flag_stats = ISIP_FALSE;
  logical_1 flag_corr = ISIP_FALSE;
  logical_1 flag_summary = ISIP_FALSE;
    
  // declare a debug parameter
  //
  int_4 debug_level = ISIP_DEBUG_NONE;

  // declare objects with non fatal error mode
  //
  Image_eval usev(ISIP_FALSE);

  // parse the command line
  //
  if (get_parameters_cc(flag_histogram, flag_stats, flag_corr, flag_summary,
			debug_level, ref_file, tst_file,
			argc, argv) == ISIP_FALSE) {
    fprintf(stdout, "<%s: error in command line arguments>\n", argv[0]);
    return ISIP_ERROR;
  }

  // set the debug mode
  //
  if (usev.set_value_cc(debug_level) == ISIP_FALSE) {
      fprintf(stdout, "<%s: invalid debug level>\n", argv[0]);
      return ISIP_ERROR;
  }    

  // echo command line options
  //
  fprintf(stdout, "reference file: %s\n", ref_file);
  fprintf(stdout, "test file: %s\n", tst_file);

  if (flag_histogram == ISIP_TRUE) {
    fprintf(stdout, "mode = histogram\n");
  }
  if (flag_stats == ISIP_TRUE) {
    fprintf(stdout, "mode = statistics\n");
  }
  if (flag_corr == ISIP_TRUE) {
    fprintf(stdout, "mode = correlation\n");
  }
  if (flag_summary == ISIP_TRUE) {
    fprintf(stdout, "mode = summary\n");
  }
  fprintf(stdout, "\n");
  
  // declare buffers for reference and test data
  //
  int_4 ref_array_size;
  char_1** ref_fnames;
  float_4* ref_values;
  int_4 ref_num_items;
  
  int_4 tst_array_size;
  char_1** tst_fnames;
  float_4* tst_values;
  int_4 tst_num_items;
  
  // load data
  //
  if (debug_level > ISIP_DEBUG_NONE) {
    fprintf(stdout, "\nloading ref data:\n");
  }
  if (usev.load_cc(ref_fnames, ref_values, ref_num_items, ref_array_size,
		   ref_file) == ISIP_FALSE) {
    fprintf(stdout, "%s: error loading ref data\n", argv[0]);
    return ISIP_ERROR;
  }

  if (debug_level > ISIP_DEBUG_NONE) {
    fprintf(stdout, "\nloading test data:\n");
  }
  if (usev.load_cc(tst_fnames, tst_values, tst_num_items, tst_array_size,
		   tst_file) == ISIP_FALSE) {
    fprintf(stdout, "%s: error loading test data\n", argv[0]);
    return ISIP_ERROR;
  }

  //---------------------------------------------------------------------------
  //
  // data analysis methods
  //
  //---------------------------------------------------------------------------
  //
  // do a histogram
  //
  int_4 num_bins = USEV_NUM_BINS;
  float_4* bins_midpts = (float_4*)NULL;
  int_4* bins_freqs = (int_4*)NULL;
    
  if (flag_histogram == ISIP_TRUE) {

    // allocate space
    //
    bins_midpts = new float_4[num_bins];
    bins_freqs = new int_4[num_bins];

    // initialize variables
    //
    for (int_4 i=0; i<num_bins; i++) {
      bins_midpts[i]=(float_4)0.0;
      bins_freqs[i]=(int_4)0;
    }
    
    if (usev.histogram_cc(bins_midpts, bins_freqs,
			  USEV_MIN_SBE, USEV_MAX_SBE, num_bins,
			  ref_fnames, ref_values, ref_num_items,
			  tst_fnames, tst_values, tst_num_items)
	== ISIP_FALSE) {
      fprintf(stdout, "<unable to calculate a histogram>\n");
      return ISIP_ERROR;
    }
  }
  
  // do a correlation
  //
  float_4 corr_value = (float_4)0;

  if (flag_corr == ISIP_TRUE) {
    corr_value = usev.correlation_cc(ref_fnames, ref_values, ref_num_items,
				     tst_fnames, tst_values, tst_num_items);
    if (corr_value < USEV_ERROR) {
      fprintf(stdout, "<bad correlation value>\n");
      return ISIP_ERROR;
    }
  }
  
  // always do a statistical analysis because this information is
  // needed for the summary
  //
  float_4 min, max, avg, median, rms;
  char_1 min_file[ISIP_MAX_STRING_LENGTH];
  char_1 max_file[ISIP_MAX_STRING_LENGTH];
  char_1 median_file[ISIP_MAX_STRING_LENGTH];
  int_4 proc_count=0;
  int_4 total_count=0;
  
  if (usev.stats_cc(min, median, max, avg, rms, min_file, median_file,
		    max_file, proc_count, total_count, ref_fnames,
		    ref_values, ref_num_items, tst_fnames, tst_values,
		    tst_num_items) == ISIP_FALSE) {
    fprintf(stdout, "<unable to calculate statistical analysis>\n");
    return ISIP_ERROR;
  }

  //---------------------------------------------------------------------------
  //
  // display results
  //
  //---------------------------------------------------------------------------
  //
  // display a histogram
  //
  if (flag_histogram == ISIP_TRUE) {
    usev.print_histogram_cc (num_bins, bins_midpts, bins_freqs);
  }
  
  // display the correlation
  //
  if (flag_corr == ISIP_TRUE) {
    usev.print_correlation_cc(corr_value);
  }

  // always print a summary
  //
  usev.print_summary_cc(min, median, max, avg, rms, min_file, median_file,
			max_file);
  
  // clean up
  //
  usev.cleanup_cc(ref_fnames, ref_num_items);
  usev.cleanup_cc(tst_fnames, tst_num_items);
  delete [] ref_values;
  delete [] tst_values;
  
  if (bins_midpts != (float_4*)NULL) {
    delete [] bins_midpts;
  }
  if (bins_freqs != (int_4*)NULL) {
    delete [] bins_freqs;
  }
    
  // exit gracefully
  //
  fprintf(stdout, "\n<%s: %ld of %ld files processed successfully>\n",
	  argv[0], proc_count, total_count);
  
  return ISIP_NO_ERROR;
}



