// file: $USFS/class/image_analysis/v6.1/imaga_he_00.cc
//

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

// isip include files
//
#include "image_analysis.h"

#include "image_analysis_constants.h"
#include <integral_constants.h>

// method: pca_load_cc
//
// arguments: none
//
// return: a logical_1 value indicating status
//
// this method loads a model file
//
logical_1 Image_analysis::pca_load_cc() {

  // declare a scratch buffer
  //
  char_1 buf[ISIP_MAX_STRING_LENGTH];
  int_4 i, j;
  float_8 val;
  
  // open the model file for the histogram and edge_detection combined
  //
  FILE* fp = open_cc(model_fname_d, ISIP_READ_ONLY);
  if (fp == (FILE*)NULL) {
    error_handler_cc((char_1*)"pca_load_cc",
		     (char_1*)"error opening the model file");
    return ISIP_FALSE;
  }

  // read the version used
  //
  if (fscanf(fp, "%s", (char*)buf) == (int)0) {
    error_handler_cc((char_1*)"pca_load_cc",
		     (char_1*)"incorrect version");
    return ISIP_FALSE;
  }
  
  // check the version
  //
  if (strcmp((char*)buf, (char*)IMAGA_MODEL_VERSION) != (int)0) {
    error_handler_cc((char_1*)"pca_load_cc",
		     (char_1*)"incorrect model version");
    return ISIP_FALSE;
  }
  
  // save the model version
  //
  if (model_version_d != (char_1*)NULL) {
    delete [] model_version_d;
  }
  
  model_version_d = new char_1[strlen((char*)buf) + (int_4)1];
  strcpy((char*)model_version_d, (char*)buf); 
  
  // loop over all lines in the file and collect the model
  //
  char_1 str_key[ISIP_MAX_STRING_LENGTH];
  char_1 str_val[ISIP_MAX_STRING_LENGTH];

  int_4 counter = 0;
  
  while(fgets((char*)buf, ISIP_MAX_STRING_LENGTH, fp) != (char*)NULL) {
    
    // checking for a comment line and/or a blank line
    //
    char_1 nonwhitespace[2];
    
    if ((buf[0] != ISIP_POUND) &&
	(sscanf((char*)buf, "%1s", nonwhitespace) > (int)0)) {
      
      // parse the line
      //
      if (counter <= IMAGA_HE_NPARAMS) {
	sscanf((char*)buf, (char*)IMAGA_FORMAT_0 , str_key, str_val);	
      }
      
      if (counter == 0) {
	if ((strcmp((char*)str_key, (char*)IMAGA_ALG) != (int)0) ||
	    (strcmp((char*)str_val,
		    (char*)IMAGA_ALG_FACTOR_NAME)!= (int)0)) {
	  error_handler_cc((char_1*)"pca_load_cc",
			   (char_1*)"incorrect type of model file");
	  return ISIP_FALSE;
	}
      }

      else if (counter == 1) {
	hist_r_min_d = atof((char*)str_val);
      } 
      
      else if (counter == 2) {
	hist_r_max_d = atof((char*)str_val);
      }
      
      else if (counter == 3) {
	hist_r_nbins_d = atoi((char*)str_val);

	// delete memory if necessary
	//
	if (hist_r_rpdf_d != (float_8*)NULL) {
	  delete [] hist_r_rpdf_d;
	  hist_r_rpdf_d = (float_8*)NULL;
	}
	if (hist_r_gpdf_d != (float_8*)NULL) {
	  delete [] hist_r_gpdf_d;
	  hist_r_gpdf_d = (float_8*)NULL;
	}
	if (hist_r_bpdf_d != (float_8*)NULL) {
	  delete [] hist_r_bpdf_d;
	  hist_r_bpdf_d = (float_8*)NULL;
	}
	if (hist_r_ylwpdf_d != (float_8*)NULL) {
	  delete [] hist_r_ylwpdf_d;
	  hist_r_ylwpdf_d = (float_8*)NULL;
	}
	if (hist_r_brnpdf_d != (float_8*)NULL) {
	  delete [] hist_r_brnpdf_d;
	  hist_r_brnpdf_d = (float_8*)NULL;
	}
	if (hist_r_rcval_d != (float_8*)NULL) {
	  delete [] hist_r_rcval_d;
	  hist_r_rcval_d = (float_8*)NULL;
	}
	if (hist_r_gcval_d != (float_8*)NULL) {
	  delete [] hist_r_gcval_d;
	  hist_r_gcval_d = (float_8*)NULL;
	}
	if (hist_r_bcval_d != (float_8*)NULL) {
	  delete [] hist_r_bcval_d;
	  hist_r_bcval_d = (float_8*)NULL;
	}
	if (hist_r_ylwcval_d != (float_8*)NULL) {
	  delete [] hist_r_ylwcval_d;
	  hist_r_ylwcval_d = (float_8*)NULL;
	}
	if (hist_r_brncval_d != (float_8*)NULL) {
	  delete [] hist_r_brncval_d;
	  hist_r_brncval_d = (float_8*)NULL;
	}
	
	// initialize space for pdfs
	//
	hist_r_rpdf_d = new float_8[hist_r_nbins_d];
	hist_r_gpdf_d = new float_8[hist_r_nbins_d];
	hist_r_bpdf_d = new float_8[hist_r_nbins_d];
	hist_r_ylwpdf_d = new float_8[hist_r_nbins_d];
	hist_r_brnpdf_d = new float_8[hist_r_nbins_d];
	hist_r_rcval_d = new float_8[hist_r_nbins_d];
	hist_r_gcval_d = new float_8[hist_r_nbins_d];
	hist_r_bcval_d = new float_8[hist_r_nbins_d];
	hist_r_ylwcval_d = new float_8[hist_r_nbins_d];
	hist_r_brncval_d = new float_8[hist_r_nbins_d];
      }
      
      else if (counter == 4) {
	hist_r_redp_d = atoi((char*)str_val);
      }
      
      else if (counter == 5) {
	hist_r_grnp_d = atoi((char*)str_val);
      }
      
      else if (counter == 6) {
	hist_r_blup_d = atoi((char*)str_val);
      }
      
      else if (counter == 7) {
	hist_rwgt_d = atof((char*)str_val);
      }
    
      else if (counter == 8) {
	hist_gwgt_d = atof((char*)str_val);
      }
      
      else if (counter == 9) {
	hist_bwgt_d = atof((char*)str_val);
      }
      
      else if (counter == 10) {
	hist_ascale_d = atof((char*)str_val);
      }
      
      else if (counter == 11) {
	hist_bscale_d = atof((char*)str_val);
      }
      
      // read the number of bins
      //
      else if (counter == 12) {
	
	edge_r_nbins_d = atoi((char*)str_val);

	// delete memory if necessary
	//
	if (edge_r_lpdf_d != (float_8*)NULL) {
	  delete [] edge_r_lpdf_d;
	  edge_r_lpdf_d = (float_8*)NULL;
	}
	if (edge_r_lcval_d != (float_8*)NULL) {
	  delete [] edge_r_lcval_d;
	  edge_r_lcval_d = (float_8*)NULL;
	}

	// initialize space for pdfs
	//
	edge_r_lpdf_d = new float_8[edge_r_nbins_d];
	edge_r_lcval_d = new float_8[edge_r_nbins_d];
      }
    
      // read the xmean for the gaussian mask
      //
      else if (counter == 13) {
	edge_r_xmean_d = atof((char*)str_val);
      }
      
      else if (counter == 14) {
	edge_r_ymean_d = atof((char*)str_val);
      }

      else if (counter == 15) {
	edge_r_sigma_d = atof((char*)str_val);
      }
    
      // read the gaussian mask size
      //
      else if (counter == 16) {
	edge_r_msize_d = atoi((char*)str_val);
      }
      
      // read the ratio factor
      //
      else if (counter == 17) {
	edge_r_rfact_d = atof((char*)str_val);
      }
      
      else if (counter == 18) {
	pca_t_npxls_d = atoi((char*)str_val);
      }
      
      else if (counter == 19) {
	pca_t_nblks_d = atoi((char*)str_val);
      }
      
      else if (counter == 20) {
	pca_t_num_vectors_d = atoi((char*)str_val);
      }
      
      else if (counter == 21) {
	pca_t_values_read_d = atoi((char*)str_val);

	// delete memory if necessary
	//
	if (mean_t_data_d != (float_4*)NULL) {
	  delete [] mean_t_data_d;
	  mean_t_data_d = (float_4*)NULL;
	}
	if (covar_t_data_d != (float_4*)NULL) {
	  delete [] covar_t_data_d;
	  covar_t_data_d = (float_4*)NULL;
	}
	
	// define memory for the mean and the covariance vector
	//
	mean_t_data_d = new float_4[pca_t_values_read_d];
	covar_t_data_d = new float_4[pca_t_values_read_d * pca_t_values_read_d];

	memset ((void*)mean_t_data_d, (int)0, pca_t_values_read_d);
	memset ((void*)covar_t_data_d, (int)0,
		pca_t_values_read_d * pca_t_values_read_d);
      }

      else if (counter == 22) {
	
	strcpy((char*)model_name_d, (char*)str_val);
	
	fgets ((char*)buf, ISIP_MAX_STRING_LENGTH, fp);

	char_1* set_tmp = (char_1*)buf;
	
	// read the selection vector
	//
	for (int_4 m=0; m < IMAGA_HE_VECTORS_VALUE; m++) {
	  
	  // read the vector into an array
	  //
	  sel_vec_d[m] = strtol((char*)set_tmp, (char**) &set_tmp, 10);
	}
      }
      
      // load the mean and the covariance matrix
      //
      else if ((counter >= IMAGA_HE_NPARAMS) &&
	       (counter < (IMAGA_HE_NPARAMS + pca_t_values_read_d))) {
	
	sscanf((char*)buf, "%ld%lf", &i, &val);
	mean_t_data_d[i] = val;
      }
      
      else if (counter >= (IMAGA_HE_NPARAMS + pca_t_values_read_d)) {
	
	sscanf((char*)buf, "%ld%ld%lf", &i, &j, &val);
	covar_t_data_d[i*pca_t_values_read_d + j] = val;
      }
      counter++;
    }
  }
  
  // close the file
  //
  if (close_cc(fp) == ISIP_FALSE) {
    error_handler_cc((char_1*)"pca_load_cc",
		     (char_1*)"error closing the file");
    return ISIP_FALSE;
  }
  
  // exit gracefully
  //
  return ISIP_TRUE;
}






