// file: $isip_ifc/class/algo/KalmanFilter/kf_04.cc
// version: $Id: kf_04.cc 10602 2006-08-17 17:43:12Z ss754 $
//

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

// method: sofSize
//
// arguments: none 
//
// return: size of object
//
// this method returns the size of the object in the Sof file and is
// used for binary write
//
long KalmanFilter::sofSize() const {

  // start with the space required for the algorithm name
  //
  long bytes = ALGO_MAP.elementSofSize();

  // add the space required for the implementation name
  //
  bytes += IMPL_MAP.elementSofSize();

  // add the space required for the debug level
  //
  bytes += debug_level_d.sofSize();

  // add the space required for order
  //
  bytes += order_d.sofSize();

  // add the space required for the initial state
  //
  bytes += state_mem_d.sofSize();

  // add the space required for the transition model
  //
  bytes += trn_d.sofSize();  

  // add the space required for the observation model
  //
  bytes += obs_d.sofSize();

  // add the space required for the process noise covariance
  //
  bytes += covar_nse_pro_d.sofSize();

  // add the space required for the observation noise covariance
  //
  bytes += covar_nse_obs_d.sofSize();

  // add the space required for the estimate error covariance
  //
  bytes += covar_err_est_d.sofSize();

  // return the size
  //
  return bytes;
}

// method: write
//
// 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 write itself to an Sof file
//
boolean KalmanFilter::write(Sof& sof_a, long tag_a,
			    const String& name_a) const {

  // declare local variable
  //
  long obj_size;
  
  // write the instance of the object into the Sof file
  //
  if (sof_a.isText()) {

    // set the size to be dynamic
    //
    obj_size = Sof::ANY_SIZE;
  }
  else {

    // set the size to be the size of the object written to the Sof file
    //
    obj_size = sofSize();
  }

  // write the object into the sof file's index
  //
  if (!sof_a.put(name_a, tag_a, obj_size)) {
    return false;
  }

  // write data and exit gracefully
  //
  return writeData(sof_a);
}

// method: writeData
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//
// return: a boolean value indicating status
//
// this method branches on the algorithm name
//
boolean KalmanFilter::writeData(Sof& sof_a, const String& pname_a) const {
  
  // declare local variables
  //
  boolean status = false;
  
  // write a start string if necessary
  //
  sof_a.writeLabelPrefix(pname_a);

  // write the algorithm name
  //
  ALGO_MAP.writeElementData(sof_a, PARAM_ALGORITHM, (long)algorithm_d);
    
  // write the implementation type
  //
  IMPL_MAP.writeElementData(sof_a, PARAM_IMPLEMENTATION,
			    (long)implementation_d);
  
  // write the debug level (from the base class)
  //
  debug_level_d.writeData(sof_a, PARAM_DBGL);

  // check known algorithms: CONVENTIONAL
  //
  if ((algorithm_d == CONVENTIONAL) && (implementation_d == AR_SCALAR)) {
    status = writeDataKalmanFilter(sof_a, pname_a);
  }

  // check unsupported algorithms
  //
  else {
    return Error::handle(name(), L"writeData",
			 ERR_UNKALG, __FILE__, __LINE__);
  }

  // put an end string if necessary
  //
  sof_a.writeLabelSuffix(pname_a);
  
  // exit gracefully
  //
  return status;
}

// method: writeDataKalmanFilter
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//
// return: a boolean value indicating status
//
// this method writes data to a file for the CONVENTIONAL algorithm
//
boolean KalmanFilter::writeDataKalmanFilter(Sof& sof_a,
				       const String& pname_a) const {

  order_d.writeData(sof_a, PARAM_ORDER);

  state_mem_d.writeData(sof_a, PARAM_STATE_MEM);
  trn_d.writeData(sof_a, PARAM_TRN);
  obs_d.writeData(sof_a, PARAM_OBS);

  covar_nse_pro_d.writeData(sof_a, PARAM_NOISE_PRC);
  covar_nse_obs_d.writeData(sof_a, PARAM_NOISE_OBS);
  covar_err_est_d.writeData(sof_a, PARAM_NOISE_ERR);
  
  // exit gracefully
  //
  return true;
}
