// file: $isip_ifc/class/stat/Histogram/hist_03.cc
// version: $Id: hist_03.cc 10393 2006-01-25 05:20:55Z srinivas $
//

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

// method: sofSize
//
// arguments: none
//
// return: size of object
//
// this method returns the size of the object as written to an Sof file
//
long Histogram::sofSize() const {

  long bytes = dim_d.sofSize();
  bytes += bins_d.sofSize();
  bytes += counts_d.sofSize();
  bytes += min_d.sofSize();
  bytes += max_d.sofSize();
  bytes += num_bins_d.sofSize();
  bytes += SCALE_MAP.elementSofSize();
  bytes += MODE_MAP.elementSofSize();
  return bytes;
}


// 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 Histogram::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 Histogram::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 scale
  //
  if (!SCALE_MAP.readElementData((long&)scale_d, sof_a, PARAM_SCALE,
				parser.getEntry(sof_a, PARAM_SCALE))) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // get the mode
  //
  if (!MODE_MAP.readElementData((long&)mode_d, sof_a, PARAM_MODE,
				parser.getEntry(sof_a, PARAM_MODE))) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }
  
  // get the dimension
  //
  if (!dim_d.readData(sof_a, PARAM_DIM,
		       parser.getEntry(sof_a, PARAM_DIM),
		       false, false)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }
  
  // get the bins
  //
  if (!bins_d.readData(sof_a, PARAM_BINS,
		       parser.getEntry(sof_a, PARAM_BINS),
		       false, false)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }	    

  // get the minimum values
  //
  if (!min_d.readData(sof_a, PARAM_MIN,
			 parser.getEntry(sof_a, PARAM_MIN),
			 false, false)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }
  
  // get the maximum values
  //
  if (!max_d.readData(sof_a, PARAM_MAX,
			 parser.getEntry(sof_a, PARAM_MAX),
			 false, false)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // get the number of bins for all dimensions
  //
  if (!num_bins_d.readData(sof_a, PARAM_NUM_BINS,
			 parser.getEntry(sof_a, PARAM_NUM_BINS),
			 false, false)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }
  
  // get the counts
  //
  if (!counts_d.readData(sof_a, PARAM_COUNTS,
			 parser.getEntry(sof_a, PARAM_COUNTS),
			 false, false)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // exit gracefully
  //
  return true;
}
