// file: $isip/class/algo/FilterBank/fb_03.cc
// version: $Id: fb_03.cc 8389 2002-07-16 02:02:14Z gao $
//

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

//------------------------------------------------------------------
// these methods have to be in the same file so they can use the same
// static parser pointer
//------------------------------------------------------------------

// declare a static sof parser pointer
//
static SofParser* parser_d = (SofParser*)NULL;

// method: setParser
//
// arguments:
//  SofParser* parser: (input) sof file object
//
// return: a boolean value indicating status
//
// this method sets the parser from the parent object to be used in
// the next readData method.
//
boolean FilterBank::setParser(SofParser* parser_a) {

  // set the parser
  //
  parser_d = parser_a;

  // 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 according
// to the specified name and tag
//
boolean FilterBank::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
  //
  return readData(sof_a);
}

// 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_OBJECT)
//  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 FilterBank::readData(Sof& sof_a, const String& pname_a, long size_a,
			     boolean param_a, boolean nested_a) {
  
  // declare local variables
  //
  boolean status = true;
  
  // we need a parser
  // 
  if (parser_d == (SofParser*)NULL) {

    // initialize the parser
    //
    parser_d = new SofParser;

    // check if the object is nested
    //
    if (nested_a) {
      parser_d->setNest();
    }
    
    // load the parser
    //
    if (!parser_d->load(sof_a, size_a)) {

      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
                           __FILE__, __LINE__, Error::WARNING);
    }           
  }

  // set the debug level of the parser
  //
  parser_d->setDebug(debug_level_d.getIndex());

  // get algorithm type
  //
  if (parser_d->isPresent(sof_a, PARAM_ALGORITHM)) {
    if (!ALGO_MAP.readElementData((long&)algorithm_d, sof_a, PARAM_ALGORITHM,
				  parser_d->getEntry(sof_a,
						     PARAM_ALGORITHM))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }      
  }
  else {
    algorithm_d = DEF_ALGORITHM;
  }
  
  // get the implementation
  //
  if (parser_d->isPresent(sof_a, PARAM_IMPLEMENTATION)) {
    if (!IMPL_MAP.readElementData((long&)implementation_d, sof_a,
				  PARAM_IMPLEMENTATION,
				  parser_d->getEntry(sof_a,
						     PARAM_IMPLEMENTATION))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }      
  }
  else {
    implementation_d = DEF_IMPLEMENTATION;
  }

  // check known algorithms: TIME
  //
  if (algorithm_d == TIME) {
    status = readDataTime(sof_a, pname_a, size_a, param_a, nested_a);
  }

  // check known algorithms: FREQUENCY
  //
  else if (algorithm_d == FREQUENCY) {
    status = readDataFrequency(sof_a, pname_a, size_a, param_a, nested_a);
  }
  
  // check unsupported algorithms
  //
  else {
    return Error::handle(name(), L"readData",
			 ERR_UNKALG, __FILE__, __LINE__);
  }

  // get the debug level
  //
  if (parser_d->isPresent(sof_a, PARAM_DBGL)) {
    if (!debug_level_d.readData(sof_a, PARAM_DBGL,
				parser_d->getEntry(sof_a, PARAM_DBGL))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::IO,
			   __FILE__, __LINE__, Error::WARNING);
    }
  }
  else {
    debug_level_d = Integral::DEF_DEBUG;
  }
  
  // check that all parameters are accounted for
  //
  if (!parser_d->checkParams(sof_a)) {

    // delete the parser
    //
    delete parser_d;
    parser_d = (SofParser*)NULL;
    
    // return a warning message
    //
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }
  
  // delete the parser
  //
  delete parser_d;
  parser_d = (SofParser*)NULL;
  
  // exit gracefully
  //
  return status;
}


// method: readDataTime
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//  long size: (input) size in bytes of object (or FULL_OBJECT)
//  boolean param: (input) is the parameter name in the file?
//  boolean nested: (input) are we nested?
//
// return: a boolean value indicating status
//
// this method assumes the algorithm has been read, and proceeds to
// read the filename that contains the filters and their corresponding
// coefficients for the TIME algorithm. the filters coefficients are
// read in the init method
//
boolean FilterBank::readDataTime(Sof& sof_a, const String& pname_a,
				 long size_a, boolean param_a,
				 boolean nested_a) {
  
  
  // get the computational mode
  //
  if (parser_d->isPresent(sof_a, PARAM_CMODE)) {
    if (!CMODE_MAP.readElementData((long&)cmode_d, sof_a, PARAM_CMODE,
                                   parser_d->getEntry(sof_a, PARAM_CMODE))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
                           __FILE__, __LINE__, Error::WARNING);
    }
  }
  else {
    cmode_d = DEF_CMODE;
  }
  
  // get the filter parameter file
  //
  if (parser_d->isPresent(sof_a, PARAM_FILTERS_PFILE)) {

    if (!parser_d->read(filters_pfile_d, sof_a,
			parser_d->getEntry(sof_a, PARAM_FILTERS_PFILE))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readDataTime", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }      
  }
  
  
  else {
    filters_pfile_d = DEF_FILTERS_PFILE;
  }
  
  // initalize the filters since the getTrailingPad and getLeadingPad
  // need the maximum past and future lag before the frontend calls
  // the apply method
  //
  if (!is_valid_d) {
    init();
  }

  // exit gracefully
  //
  return true;
}

// method: readDataFrequency
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//  long size: (input) size in bytes of object (or FULL_OBJECT)
//  boolean param: (input) is the parameter name in the file?
//  boolean nested: (input) are we nested?
//
// return: a boolean value indicating status
//
// this method assumes the algorithm has been read, and proceeds to read
// the rest of the object for the FREQUENCY algorithm.
//
boolean FilterBank::readDataFrequency(Sof& sof_a, const String& pname_a,
				      long size_a, boolean param_a,
				      boolean nested_a) {
  
  
  // get the input_mode
  //
  if (parser_d->isPresent(sof_a, PARAM_INPUT_MODE)) {
    if (!INPUT_MODE_MAP.readElementData((long&)input_mode_d, sof_a,
					PARAM_INPUT_MODE,
					parser_d->getEntry(sof_a, PARAM_INPUT_MODE))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }      
  }
  else {
    input_mode_d = DEF_INPUT_MODE;
  }

  // get the data mode
  //
  if (parser_d->isPresent(sof_a, PARAM_DMODE)) {
    if (!DMODE_MAP.readElementData((long&)dmode_d, sof_a, PARAM_DMODE,
                                   parser_d->getEntry(sof_a, PARAM_DMODE))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
                           __FILE__, __LINE__, Error::WARNING);
    }
  }
  else {
    dmode_d = DEF_DMODE;
  }

  // get the scale
  //
  if (parser_d->isPresent(sof_a, PARAM_SCALE)) {
    if (!SCALE_MAP.readElementData((long&)scale_d, sof_a, PARAM_SCALE,
				   parser_d->getEntry(sof_a, PARAM_SCALE))) {

      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;

      // return a warning message
      //
      return Error::handle(name(), L"readDataFrequency", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }      
  }
  else {
    scale_d = DEF_SCALE;
  }
  
  // get the frequency sampling method
  //
  if (parser_d->isPresent(sof_a, PARAM_FREQUENCY_SAMPLING)) {
    if (!FREQUENCY_SAMPLING_MAP.
	readElementData((long&)fsmp_d, sof_a, PARAM_FREQUENCY_SAMPLING,
			parser_d->getEntry(sof_a, PARAM_FREQUENCY_SAMPLING))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readDataFrequency", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }      
  }
  else {
    fsmp_d = DEF_FREQUENCY_SAMPLING;
  }
  
  // get the order
  //
  if (parser_d->isPresent(sof_a, PARAM_ORDER)) {
    if (!order_d.readData(sof_a, PARAM_ORDER,
			  parser_d->getEntry(sof_a, PARAM_ORDER))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readDataFrequency", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }
  }
  else {
    order_d.assign(DEF_ORDER);
  }
  
  // get the filter spacing width
  //
  if (parser_d->isPresent(sof_a, PARAM_BANDWIDTH)) {
    if (!bandwidth_d.readData(sof_a, PARAM_BANDWIDTH,
			      parser_d->getEntry(sof_a, PARAM_BANDWIDTH))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;

      // return a warning message
      //
      return Error::handle(name(), L"readDataFrequency", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }
  }
  else {
    bandwidth_d.assign(DEF_BANDWIDTH);
  }
  
  // exit gracefully
  //
  return true;
}
