// file: itri_print_0.cc
//
// isip include files
//
#include "init_triphones.h"
#include "init_triphones_constants.h"

// method: print_state_cc
//
// arguments:
//  char_1* fp_out: (input) the new states file
//  State* states : (input) the new state to be output
//
// return: a logical flag to indicate success
//
logical_1 print_state_cc(FILE* fp_out_a, Train_State* states_a) {

  // local variables
  //
  int_4 num_mixture = (int_4)0;
  int_4 num_features = (int_4)0;
  float_4 temp_value = (float_4)0;
  float_4* tmp_weight = (float_4*)NULL;
  float_4* tmp_scale = (float_4*)NULL;
  float_4** tmp_mean = (float_4**)NULL;
  float_4** tmp_covar = (float_4**)NULL;
  
  // get the parameters
  //
  num_features = states_a->get_num_features_cc();
  num_mixture = states_a->get_num_mixtures_cc();
  tmp_weight = states_a->get_weights_cc();
  tmp_scale = states_a->get_scale_cc();
  tmp_mean = states_a->get_mean_cc();
  tmp_covar = states_a->get_covar_cc();

  //  output the number of mixtures
  //
  fwrite((void*)&num_mixture, sizeof(int_4), 1, fp_out_a);
  
  for (int_4 j = 0; j < num_mixture; j++) {

    // output the mixture weight
    //
    temp_value = exp(tmp_weight[j]);
    fwrite((void*)&temp_value, sizeof(float_4), 1, fp_out_a);

    // output the mean vector
    //
    for (int_4 k = 0; k < num_features; k++) {
      fwrite((void*)&tmp_mean[j][k], sizeof(float_4), 1, fp_out_a);
    }
    
    // output the covariance vector
    //
    for (int_4 l = 0; l < num_features; l++) {
      temp_value = 1.0/tmp_covar[j][l];
      fwrite((void*)&temp_value, sizeof(float_4), 1, fp_out_a);
    }

    // output the constant scale factor
    //
    fwrite((void*)&tmp_scale[j], sizeof(float_4), 1, fp_out_a);
  }

  // free memory
  //
  tmp_weight = (float_4*)NULL;
  tmp_scale = (float_4*)NULL;
  tmp_mean = (float_4**)NULL;
  tmp_covar = (float_4**)NULL;

  // exit gracefully
  //
  return ISIP_TRUE;
}
