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

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

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

// method: pca_write_cc
//
// arguments: none
//  
// return: a logical_1 value indicating status
//
// this method writes the feature vector into a model file
//
logical_1 Image_analysis::pca_write_cc() {
  
  // declare local variables
  //
  int_4 counter = 0;
  
  // open the file for writing the model file
  //
  FILE* fp = open_cc(model_fname_d, ISIP_WRITE_ONLY);
  
  if (fp == (FILE*)NULL) {
    error_handler_cc((char_1*)"pca_write_cc",
		     (char_1*)"error opening the model file");
    return ISIP_FALSE;
  }
  
  for (int_4 i=0; i < IMAGA_HE_MODEL_LINES; i++) {
    
    if (counter == 0) {
      
      // write the version into the file
      //
      fprintf(fp, "%s\n\n", (char*)IMAGA_MODEL_VERSION);
    }
    
    else if (counter == 1) {
      
      // write the type of the algorithm
      //
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_HIST_ALG_COMMENT, ISIP_POUND);
      fprintf(fp, "algorithm = %s\n\n", (char*)IMAGA_ALG_FACTOR_NAME);
    }
    
    else if (counter == 2) {

      // write the paramters of the histogram
      //
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_HIST_PARAM_COMMENT, ISIP_POUND);
      fprintf(fp, "min_value = %ld\nmax_value = %ld\nhist_num_bins = %ld\n\n",
	      (int_4)IMAGA_HIST_MIN, (int_4)IMAGA_HIST_MAX, IMAGA_HIST_NBINS);
    }

    else if (counter == 3) {

      // write the pointers of each of the colors into the file
      //
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_HIST_COLOR_COMMENT, ISIP_POUND);
      fprintf(fp, "red_ptr = %ld\ngreen_ptr = %ld\nblue_ptr = %ld\n\n",
	      IMAGA_HIST_RPTR, IMAGA_HIST_GPTR, IMAGA_HIST_BPTR);
    }

    else if (counter == 4) {

      // write the color weights
      //
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_HIST_CWTS_COMMENT, ISIP_POUND);
      fprintf(fp, "weight_red = %0.2f\nweight_green = %0.2f\nweight_blue = %0.2f\n\n",
	      IMAGA_HIST_WRED, IMAGA_HIST_WGRN, IMAGA_HIST_WBLU);
    }

    else if (counter == 5) {

      // write the scaling values
      //
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_HIST_SCAL_COMMENT, ISIP_POUND);
      fprintf(fp, "scale_a = %ld\nscale_b = %ld\n\n", IMAGA_HIST_ASCL,
	      IMAGA_HIST_BSCL);
    }
      
    else if (counter == 6) {
      
      // write the parameters of the edge detection
      //
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_EDGE_PARAM_COMMENT, ISIP_POUND);
      fprintf(fp, "edge_num_bins = %ld\nedge_xmean = %f\nedge_ymean = %f\n",
	      IMAGA_EDGE_NBINS, IMAGA_EDGE_XMEAN, IMAGA_EDGE_YMEAN);
      fprintf(fp, "edge_stdv = %f\nedge_mask_size = %ld\nedge_threshold = %f\n\n",
	      IMAGA_EDGE_SIGMA, IMAGA_EDGE_MSIZE, IMAGA_EDGE_FACTOR);
    }
    
    else if (counter == 7) {
      
      // write the number of pixels and images/blocks processed
      //
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_HIST_DATA_COMMENT, ISIP_POUND);
      fprintf(fp, "num_pixels = %ld\n", pca_t_npxls_d);
      if (frm_size_d > 0) {
	fprintf(fp, "num_blocks_processed = %ld\n\n", pca_t_nblks_d);
      }
      else if (frm_size_d == 0) {
	fprintf(fp, "num_images_processed = %ld\n\n", pca_t_nimages_d);
      }
    }
    
    else if (counter == 8) {

      // print the number of vectors
      //
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_HIST_DATA_COMMENT, ISIP_POUND);
      fprintf(fp, "max_num_features = %d\nnum_features = %ld\n\n",
	      IMAGA_HE_VECTORS_VALUE, pca_t_values_read_d);
    }

    else if (counter == 9) {

      // print the selection vector
      //
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_SEL_VECTOR_COMMENT, ISIP_POUND);
      fprintf(fp, "sel_vec = %s\n", model_name_d);

      for (int_4 m=0; m < pca_t_num_vectors_d; m++) {
	fprintf(fp, "%ld ", sel_vec_d[m]);
      }

      fprintf(fp, "\n\n");
    }
    
    else if (counter == 10) {
      
      // write the mean vector into the file
      //
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_HE_MEAN_COMMENT, ISIP_POUND);
      
      // print the mean values
      //
      for (int_4 m=0; m < pca_t_values_read_d; m++) {
	fprintf(fp, "%ld\t%f\n", m, mean_t_data_d[m]);
      }

      fprintf(fp, "\n");
    }
    
    else if (counter == 11) {
      
      // write the covariance matrix into the file
      //
      fprintf(fp,(char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_HE_COVAR_COMMENT, ISIP_POUND);
      
      // printing the covariance data
      //
      for (int_4 m=0; m < pca_t_values_read_d; m++) {
	for (int_4 n=0; n < pca_t_values_read_d; n++) {
	  fprintf(fp, "%ld %ld %f\n", m, n, 
		  covar_t_data_d[m*pca_t_values_read_d+n]);
	}
      }
    }

    counter++;
  }
  
  // close the file
  //
  if (close_cc(fp) == ISIP_FALSE) {
    error_handler_cc((char_1*)"pca_write_cc",
		     (char_1*)"error closing the file");
    return ISIP_FALSE;
  }

  // exit gracefully
  //
  return ISIP_TRUE;
}
		     




