// file: st_read_0.cc
//

// isip include files
//
#include "state.h"
#include "state_constants.h"
 
// method: read_data_cc
//
// arguments:
//  FILE* fp : (input) the file pointer to read data from
//
// return: a logical_1 indicating success
//
// this method reads all the parameters for this state from file
//
logical_1 State::read_data_cc(FILE* fp_a) {

  // make sure the file is valid
  //
  if (fp_a == (FILE*)NULL) {
    error_handler_cc((char_1*)"read_data_cc", (char_1*)"File pointer is NULL");
  }
  
  // read the number of mixtures in the state
  //
  fread((void_p)&num_mixtures_d, sizeof(int_4), 1, fp_a);
  
  // declare memory for the mean, covar, scale and weights for this state
  //
  if (num_mixtures_d > (int_4)0) {
    
    weights_d = new float_4[num_mixtures_d];
    scale_d = new float_4[num_mixtures_d];
    mean_d = new float_4*[num_mixtures_d];
    covar_d = new float_4*[num_mixtures_d];
      
    // loop over all mixtures and read data
    //
    for (int_4 j = 0; j < num_mixtures_d; j++) {
      
      // read the mixture weight
      //
      fread(&weights_d[j], sizeof(float_4), 1, fp_a);
      weights_d[j] = (float_4)log(weights_d[j]);
      
      // read the mean
      //
      mean_d[j] = new float_4[num_features_d];
      for (int_4 k = 0; k < num_features_d; k++) {
	fread((void_p)&mean_d[j][k], sizeof(float_4), 1, fp_a);
      }
      
      // read the covar and save as inverse
      //
      covar_d[j] = new float_4[num_features_d];
      for (int_4 k = 0; k < num_features_d; k++) {
	fread((void_p)&covar_d[j][k], sizeof(float_4), 1, fp_a);
	covar_d[j][k] = (float_4)1 / (float_4)covar_d[j][k];
      }
      
      // read in the scale factor
      //
      fread((void_p)&scale_d[j], sizeof(float_4), 1, fp_a);
    }
  } // end if num_mixtures_d > 0

  // exit gracefully
  //
  return ISIP_TRUE;
}
