// file: normalize_features/nf_write_0.cc
//

// local include files
//
#include "normalize_features.h"
#include "normalize_features_constants.h"

// function: write_cc
// 
// arguments:
//  float_8* out_vector (input): the vector to write
//  int_4 num_vectors (input): number of feature vectors in this file
//  int_4 num_coeffs (input): number of coefficients in each feature vector
//  char_1* output_file (input): the output file name
//  int_4 output_format (input): the output format (ascii or binary)
//
//  return value: a logical_1 indicating status
//
// this function writes the vector to the specified file
//
logical_1 write_cc(float_8* output_vector_a, int_4 num_vectors_a,
		   int_4 num_coeffs_a, char_1*  output_file_a,
		   int_4 output_format_a) {
  
  // open output file
  //
  FILE* fout;

  // declare the total number of elements in the output file
  //
  int_4 vector_size = 0;
  
  if (output_format_a == NF_IO_MODE_ASCII) {
    fout = fopen((char*)output_file_a, "w");
  }
  else {
    fout = fopen((char*)output_file_a, "wb");
  }
  
  if (fout == (FILE*)NULL) {
    fprintf(stdout, "Error: cannot open output file %s \n",
	    output_file_a);
    return ISIP_FALSE;
  }
  
  // make sure memory is allocated
  //
  if (output_vector_a == (float_8*)NULL) {
    fprintf(stdout, "Error output feature is NULL\n");
    fclose(fout);
    return ISIP_FALSE;
  }

  vector_size = num_vectors_a * num_coeffs_a;
  
  // ascii mode
  //
  if (output_format_a == NF_IO_MODE_ASCII) {
    
    for (int_4 i = 0; i < num_vectors_a; i++) {
      for (int_4 j = 0; j < num_coeffs_a; j++) {
	fprintf(fout, "%5.3f  ", output_vector_a[i*num_coeffs_a+j]);
      }
      fprintf(fout, "\n");
    }
  }

  // binary mode
  //
  else {
    if (!fwrite(output_vector_a, sizeof(float_8), vector_size, fout)) {
      fclose(fout);
      return ISIP_FALSE;
    }
  }

  // close file
  //
  fclose(fout);
  
  return ISIP_TRUE;

}
