// file: ht_dump_stats_0.cc
//
// isip include files
//
#include "hmm_train.h"
#include "hmm_train_constants.h"

// method: dump_stats_cc
//
// arguments:
//  FILE* fp_out : (input) output file pointer
//  float_4*** mean : (input) accumulated mean each state
//  float_4*** covar : (input) accumulated covariance in each state
//  int_4** st_count : (input) the count of the all states
//  int_*** trans_count : (input) the appearing time of the arcs in one
//                                transition matrix
//  int_4* trans_size : (input) size of each transition matrix
//  int_4 num_st : (input) the number of states
//  int_4 num_mix : (input) the number of mixtures
//  int_4 num_feat : (input) the number of features
//  int_4 num_trans : (input) the number of transition
//
// return: a logical flag to indicate success
//
// this method dumps the counts related to states and transitions used for
// viterbi training in a batch mode. these counts can be later combined
// to update the models
//
logical_1 dump_stats_cc(FILE* fp_out_a, float_8*** mean_a, 
			float_8***covar_a, int_4** st_count_a,
			int_4*** trans_count_a, int_4* trans_size_a,
			int_4 num_st_a, int_4 num_mix_a,
			int_4 num_feat_a, int_4 num_trans_a) {

  // loop over all states
  //
  for (int_4 i = 1; i < num_st_a; i++) {
    for (int_4 j = 0; j < num_mix_a; j++) {

      // dump the number of times each mixture has been accessed during traning
      //
      fwrite((void_p)&st_count_a[i][j], sizeof(int_4), (int_4)1, fp_out_a);    
    }
  }
  
  // dump the number of times each transition occured 
  //
  for (int_4 i = 1; i < num_trans_a; i++) {
    for (int_4 j = 0; j < trans_size_a[i]; j++) {
      for (int_4 k = 0; k < trans_size_a[i]; k++) { 
	fwrite((void_p)&trans_count_a[i][j][k], sizeof(int_4), (int_4)1,
	       fp_out_a);
      }
    }
  }
    
  // dump the accumulated mean and covariances
  //
  for (int_4 i = 1; i < num_st_a; i++) {
    for (int_4 j = 0; j < num_mix_a; j++) {
      for (int_4 k = 0; k < num_feat_a; k++) {
	fwrite(&mean_a[i][j][k], sizeof(float_8), (int_4)1, fp_out_a);
      }
    }
  }

  for (int_4 i = 1; i < num_st_a; i++) {
    for (int_4 j = 0; j < num_mix_a; j++) {
      for (int_4 k = 0; k < num_feat_a; k++) {
	fwrite(&covar_a[i][j][k], sizeof(float_8), (int_4)1, fp_out_a);
      }
    }
  }

  // exit gracefully
  //
  return ISIP_TRUE;
}



