// file: ih_print_0.cc
//

// isip include files
//
#include "init_hmm.h"
#include "init_hmm_constants.h"

// method: print_state_cc
//
// arguments:
//  FILE* fout : (output) the output file
//  FILE* fvar : (output) the variance floor file
//  float_4* means : (input) the means to print out
//  float_4** covar : (input) the covariances to print out
//  int_4 num_feat : (input) the number of features in one feature vector
//  int_4 num_states : (input) the number of states to be output
//  float_4 var_floor : (input) variance floor
//
// return: a logical_1 flag indicating success
//
// this method dumps the means and covariance metrix to the output file
//
logical_1 print_state_cc(FILE* fout_a, FILE* fvar_a, float_4* means_a,
			 float_4** covar_a, int_4 num_feat_a,
			 int_4 num_states_a, float_4 var_floor_a) {
  
  // some temp variables
  //
  int_4 num_mixes = 1;
  float_4 temp_weight = (float_4)1/(float_4)num_mixes;
  float_8 temp_scale = 1.0;

  //  (include the dummy zero'th state)
  //
  int_4 num_states = num_states_a + 1;

  // output the comments
  //
  fprintf(fout_a, "%s\n", "# file : state.text");
  fprintf(fout_a, "%s\n", "#");
  fprintf(fout_a, "%s\n", "# data for acoustic models --- list of all the states that are tied");
  fprintf(fout_a, "%s\n\n", "#");

  // output the size and num_states
  //
  fprintf(fout_a, "%s = %ld\n", "feature_size", num_feat_a);
  fprintf(fout_a, "%s = %ld\n\n", "num_states", num_states);

  // output the first state
  //
  fprintf(fout_a, "State: 0\n");
  fprintf(fout_a, "nummixes: 0\n\n");

  // output all the states with the same mean and covariance matrix
  //
  for (int_4 i = 1; i < num_states; i++) {

    fprintf(fout_a, "%s: %ld\n", "State", i);
    fprintf(fout_a, "%s: %ld\n\n", "nummixes", num_mixes);

    for(int_4 j = 1; j <= num_mixes; j++) {
      fprintf(fout_a, "%s: %ld\n", "mixture", j);
      fprintf(fout_a, "%s: %e\n", "mix_weight", temp_weight);
      
      fprintf(fout_a, "%s:   ", "mean");
      for (int_4 l = 0; l < num_feat_a; l++) {
	fprintf(fout_a, "%e ", means_a[l]);
      }
      fprintf(fout_a, "\n");

      fprintf(fout_a, "%s:  ", "covar");
      for (int_4 k = 0; k < num_feat_a; k++) {
	temp_scale *= ISIP_TWOPI * covar_a[k][k];
	fprintf(fout_a, "%e ", covar_a[k][k]);
      }
      fprintf(fout_a, "\n");

      temp_scale = -log((float_8)1.0/temp_scale);
      fprintf(fout_a, "%s:  %e\n\n", "scale", temp_scale);
      temp_scale = 1.0;
    }
  }

  // print the variance floor
  //
  for (int_4 k = 0; k < num_feat_a; k++) {
    fprintf(fvar_a, "%e ", var_floor_a*covar_a[k][k]);
  }
  fprintf(fvar_a, "\n");

  // exit gracefully
  //
  return ISIP_TRUE;
}

