// file: $isip/class/stat/GaussianModel/gaus_03.cc
// version: $Id: gaus_03.cc 9856 2004-10-20 23:24:41Z raghavan $
//

// isip include files
//
#include "GaussianModel.h"

// method: readAccumulator
//
// arguments:
//  Sof& sof: (input) sof file object
//  long tag: (input) sof object instance tag
//  const String& name: (input) sof object instance name
//
// return: a boolean value indicating status
//
// this method has the object read accumulators from an Sof file
//
boolean GaussianModel::readAccumulator(Sof& sof_a, long tag_a, const String& name_a) {

  // read the instance of the object from the Sof file
  //
  if (!sof_a.find(name_a, tag_a)) {
    return false;
  }
  
  // read the actual data from the sof file
  //
  if (!readAccumulatorData(sof_a)) {
    return false;
  }
  
  // exit gracefully
  //
  return true;
}

// method: readAccumulatorData
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//  long size: (input) size in bytes of object (or full_size)
//  boolean param: (input) is the parameter name in the file?
//  boolean nested: (input) are we nested?
//
// return: a boolean value indicating status
//
// this method has the object read itself from an Sof file. it assumes
// that the Sof file is already positioned correctly.
//
boolean GaussianModel::readAccumulatorData(Sof& sof_a, const String& pname_a,
				     long size_a, boolean param_a,
				     boolean nested_a) {

  Integral::DEBUG debug_level = Integral::NONE;

  SofParser parser;

  parser.setDebug(debug_level);
    
  // declare local variables
  //
  Double occ_accum;
  Long access_accum;  
  VectorDouble mean_accum;  
  MatrixDouble covar_accum;
  
  // are we nested?
  //
  if (nested_a) {
    parser.setNest();
  }
  
  // load the parse
  //
  if (!parser.load(sof_a, size_a)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }	    

  // get the access count accumulator
  //
  if (!access_accum.readData(sof_a, PARAM_ACCESS_ACCUM,
			     parser.getEntry(sof_a, PARAM_ACCESS_ACCUM))) {
    return Error::handle(name(), L"readAccumData", Error::ARG, __FILE__, __LINE__);
  }	      
  access_accum_d.add(access_accum);
  
  // get the occupancy accumulator
  //
  if (!occ_accum.readData(sof_a, PARAM_OCCUPANCY_ACCUM,
			  parser.getEntry(sof_a, PARAM_OCCUPANCY_ACCUM))) {
    return Error::handle(name(), L"readAccumData", Error::ARG, __FILE__, __LINE__);
  }
  occ_accum_d.add(occ_accum);
  
  // get the mean accumulator
  //
  if (!mean_accum.readData(sof_a, PARAM_MEAN_ACCUM,
			   parser.getEntry(sof_a, PARAM_MEAN_ACCUM),
			   false, false)) {
    return Error::handle(name(), L"readAccumData", Error::ARG, __FILE__, __LINE__);
  }

  // if the length of the vectors are same, then add
  //
  if (mean_accum_d.length() == mean_accum.length()) {
    mean_accum_d.add(mean_accum);
  }
  
  // set the length of the vector mean_accum_d once in the beginning
  // when its length is zero. after this point, during the next
  // function calls, if the length of the mean_accum is zero, it is
  // not added to mean_accum_d
  //
  else if (mean_accum_d.length() == (long)0) {
    mean_accum_d.setLength(mean_accum.length());
    mean_accum_d.add(mean_accum);
  }
  
  // get the covariance accumulator
  //
  if (!covar_accum.readData(sof_a, PARAM_COVARIANCE_ACCUM,
			    parser.getEntry(sof_a, PARAM_COVARIANCE_ACCUM),
			    true, true)) {
    return Error::handle(name(), L"readAccumData", Error::ARG, __FILE__, __LINE__);
  }

  // if the dimensions of the matrices are same, then add
  //
  if ((covar_accum_d.getNumRows() == covar_accum.getNumRows()) &&
      (covar_accum_d.getNumColumns() == covar_accum.getNumColumns())) {
    covar_accum_d.add(covar_accum);
  }
  
  // set the dimensions of the matrix covar_accum_d once in the
  // beginning when its dimensions are zero. after this point, during
  // the next function calls, if the dimensions of the covar_accum are
  // zero, it is not added to covar_accum_d
  //
  else if((covar_accum_d.getNumRows() == (long)0) &&
	  (covar_accum_d.getNumColumns() == (long)0)) {
    covar_accum_d.setDimensions(covar_accum.getNumRows(),
				covar_accum.getNumColumns());
    covar_accum_d.add(covar_accum);    
  }

  // exit gracefully
  //
  return true;    
}

// method: read
//
// arguments:
//  Sof& sof: (input) sof file object
//  long tag: (input) sof object instance tag
//  const String& name: (input) sof object instance name
//
// return: a boolean value indicating status
//
// this method has the object read itself from an Sof file
//
boolean GaussianModel::read(Sof& sof_a, long tag_a, const String& name_a) {

  // read the instance of the object from the Sof file
  //
  if (!sof_a.find(name_a, tag_a)) {
    return false;
  }
  
  // read the actual data from the sof file
  //
  if (!readData(sof_a)) {
    return false;
  }
  
  // exit gracefully
  //
  return true;  
}

// method: readData
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//  long size: (input) size in bytes of object (or full_size)
//  boolean param: (input) is the parameter name in the file?
//  boolean nested: (input) are we nested?
//
// return: a boolean value indicating status
//
// this method has the object read itself from an Sof file. it assumes
// that the Sof file is already positioned correctly.
//
boolean GaussianModel::readData(Sof& sof_a, const String& pname_a,
				long size_a, boolean param_a,
				boolean nested_a) {

  Integral::DEBUG debug_level = Integral::NONE;

  SofParser parser;

  parser.setDebug(debug_level);
    
  // ignore implicit parameter setting
  //
  
  // are we nested?
  //
  if (nested_a) {
    parser.setNest();
  }
  
  // load the parse
  //
  if (!parser.load(sof_a, size_a)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }	    

  // get the vector of means
  //
  if (!mean_d.readData(sof_a, PARAM_MEAN,
		       parser.getEntry(sof_a, PARAM_MEAN),
		       false, false)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }	    

  // get the covariance matrix
  //
  if (!covariance_d.readData(sof_a, PARAM_COVARIANCE,
			     parser.getEntry(sof_a, PARAM_COVARIANCE),
			     true, true)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // save a copy of the value just read
  //
  orig_covar_d.assign(covariance_d);
  
  // set the initialization flag
  //
  is_valid_d = false;

  // call initialization method and exit gracefully
  //
  return init();
}

// method: readOccupancies
//
// arguments:
//  Sof& sof: (input) sof file object
//  long tag: (input) sof object instance tag
//  const String& name: (input) sof object instance name
//
// return: a boolean value indicating status
//
// this method has the object read occupancies from an Sof accumulator file
//
boolean GaussianModel::readOccupancies(Sof& sof_a, long tag_a, const String& name_a) {

  // read the instance of the object from the Sof file
  //
  if (!sof_a.find(name_a, tag_a)) {
    return false;
  }
  
  // read the actual data from the sof file
  //
  if (!readOccupanciesData(sof_a)) {
    return false;
  }
  
  // exit gracefully
  //
  return true;
}

// method: readOccupanciesData
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//  long size: (input) size in bytes of object (or full_size)
//  boolean param: (input) is the parameter name in the file?
//  boolean nested: (input) are we nested?
//
// return: a boolean value indicating status
//
// this method has the object read itself from an Sof file. it assumes
// that the Sof file is already positioned correctly.
//
boolean GaussianModel::readOccupanciesData(Sof& sof_a, const String& pname_a,
					   long size_a, boolean param_a,
					   boolean nested_a) {

  Integral::DEBUG debug_level = Integral::NONE;

  SofParser parser;

  parser.setDebug(debug_level);
    
  // declare local variables
  //
  Double occ_accum;
  
  // are we nested?
  //
  if (nested_a) {
    parser.setNest();
  }
  
  // load the parse
  //
  if (!parser.load(sof_a, size_a)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }	    

  // get the occupancy accumulator
  //
  if (!occ_accum.readData(sof_a, PARAM_OCCUPANCY_ACCUM,
			  parser.getEntry(sof_a, PARAM_OCCUPANCY_ACCUM))) {
    return Error::handle(name(), L"readAccumData", Error::ARG, __FILE__, __LINE__);
  }
  occ_accum_d.add(occ_accum);
  
  // exit gracefully
  //
  return true;    
}
