// file: $isip/util/speech/isip_feature_select/isip_feature_select.cc
// version: $Id: isip_feature_select.cc 10613 2006-08-24 16:01:24Z dom5 $
//
// isip include files
//
#include "isip_feature_select.h"

// isip_feature_select: utility that selects the out-of-class features
// for a given symbol.
//
int main(int argc, const char** argv) {
  
  // declare primary objects
  //
  Sdb sdb;
  CommandLine cmdl(sdb);
  
  // declare local variables used to handle command line options
  //    
  String symbol;
  Filename audiodb_file;
  Long dim(0);
  Filename symbol_list_file;  
  String ratio_class;
  String ratio_similar;
  String input_file_format;
  String input_file_type;
  String output_file_type;
  String output_file_format;
  Filename log_file;
  DebugLevel debug_level;
  DebugLevel verbosity;
    
  //---------------------------------------------------------------------------
  //
  // configure and parse the command line:
  //  add only the options that are not included by default.
  // 
  //---------------------------------------------------------------------------

  // set up messages
  //
  cmdl.setUsage(
#include "usage_message.text"
                );
  cmdl.setHelp(
#include "help_message.text"
               );

  cmdl.setIdent("$Revision: 10613 $",
		"$Name: $",
		"$Date: 2006-08-24 11:01:24 -0500 (Thu, 24 Aug 2006) $");

  // set command line options
  //  
  cmdl.addOptionParam(symbol, OPTION_SYMBOL, (String)CommandLine::DEF_EMPTY);
  cmdl.addOptionParam(audiodb_file, OPTION_AUDIO_DB, CommandLine::DEF_EMPTY);
  cmdl.addOptionParam(dim, OPTION_DIM, DEF_DIM);
  cmdl.addOptionParam(symbol_list_file, OPTION_SYMBOL_LIST,
		      CommandLine::DEF_EMPTY);
  cmdl.addOptionParam(ratio_class, OPTION_RATIO_CLASS,
		      DEF_RATIO_CLASS);
  cmdl.addOptionParam(ratio_similar, OPTION_RATIO_SIMILAR,
		      DEF_RATIO_SIMILAR);
  cmdl.addOptionParam(input_file_format, OPTION_INPUT_FILE_FORMAT,
		      DEF_FILE_FORMAT);
  cmdl.addOptionParam(input_file_type, OPTION_INPUT_FILE_TYPE, DEF_FILE_TYPE);
  cmdl.addOptionParam(output_file_type, OPTION_OUTPUT_FILE_TYPE,
		      (String)CommandLine::DEF_EMPTY);
  cmdl.addOptionParam(output_file_format, OPTION_OUTPUT_FILE_FORMAT,
		      DEF_FILE_FORMAT);
  cmdl.addOptionParam(debug_level, CommandLine::DEF_PARAM_DEBUG);
  cmdl.addOptionParam(verbosity, CommandLine::DEF_PARAM_VERBOSITY);
  cmdl.addOptionParam(log_file, CommandLine::DEF_PARAM_LFILE,
		      CommandLine::DEF_LFILE_NAME);
  
  // parse the command line
  //
  if (!cmdl.parse(argc, argv)) {
    cmdl.printUsage();
  }
  
  // open the log file
  //
  if (log_file.ne(CommandLine::DEF_LFILE_NAME)) {
    Console::open(log_file);
    Console::setLineWrap(File::NO_WRAP);
  }

  // echo the command line
  //
  if (verbosity > Integral::BRIEF) {
    cmdl.printCommandLine();
    cmdl.printVersion();
  }

  //-------------------------------------------------------------------------
  //
  // verification of the input parameters
  //
  //-------------------------------------------------------------------------
  
  if (symbol.eq((String)CommandLine::DEF_EMPTY)) {
    cmdl.printUsage();
  };  
  if (audiodb_file.eq(CommandLine::DEF_EMPTY)) {
    cmdl.printUsage();
  }
  if (input_file_format.eq(FILE_FORMAT_RAW) && ((long)dim <= (long)0)) {
    cmdl.printUsage();
  };
  if (!sdb.gotoFirst()) {
    cmdl.printUsage();
  };
  if (symbol_list_file.eq(CommandLine::DEF_EMPTY)) {
    cmdl.printUsage();
  };
  
  //-------------------------------------------------------------------------
  //
  // set the various options
  //
  //-------------------------------------------------------------------------
  
  // set the file 
  //
  FeatureFile::FILE_FORMAT in_f_format = FeatureFile::SOF;
  if (input_file_format.eq(FILE_FORMAT_RAW)) {
    in_f_format = FeatureFile::RAW;
  }
  FeatureFile::FILE_FORMAT o_f_format = FeatureFile::SOF;
  if (output_file_format.eq(FILE_FORMAT_RAW)) {
    o_f_format = FeatureFile::RAW;
  }
  
  // set the file type
  //
  FeatureFile::FILE_TYPE in_f_type = FeatureFile::TEXT;
  if (input_file_type.eq(FILE_TYPE_BINARY)) {
    in_f_type = FeatureFile::BINARY;
  }
  FeatureFile::FILE_TYPE o_f_type = FeatureFile::TEXT;
  if (output_file_type.eq(FILE_TYPE_BINARY)) {
    o_f_type = FeatureFile::BINARY;
  }
  
  // local variables
  //
  Sdb symbol_list_sdb;
  Sof symbol_list_sof;    
  FeatureSelect fs;
  
  // set the parameters of the sc object
  //
  fs.setDebug(debug_level);
  fs.setVerbosity(verbosity);
  fs.setDimension(dim);
  fs.setAlgorithm(FeatureSelect::CLASS_BASED);

  fs.setInputType(in_f_type);
  fs.setInputFormat(in_f_format);
  fs.setOutputType(o_f_type);
  fs.setOutputFormat(o_f_format);
  fs.setAudioDatabaseFile(audiodb_file);
  fs.setRatioClass(ratio_class);
  fs.setRatioSimilar(ratio_similar);
  
  // read the symbol list as sdb
  //    
  symbol_list_sof.open(symbol_list_file, File::READ_ONLY);
  symbol_list_sdb.read(symbol_list_sof, (long)0);
  symbol_list_sof.close();
  
  //-------------------------------------------------------------------------
  //
  // get the value of the options that are defined as command line
  // class options.
  //
  //-------------------------------------------------------------------------
  
  String cmdl_value;
  
  if (cmdl.getValue(cmdl_value, CommandLine::DEF_PARAM_DIRECTORY)) {
    fs.setOutputDirectory(cmdl_value);
  }    
  if (cmdl.getValue(cmdl_value, CommandLine::DEF_PARAM_EXTENSION)) {
    fs.setOutputExtension(cmdl_value);
  }    
  if (cmdl.getValue(cmdl_value, CommandLine::DEF_PARAM_PRESERVE)) {
    long preserve;
    cmdl_value.get(preserve);
    fs.setOutputPreserve(preserve);
  }  
  if (cmdl.getValue(cmdl_value, CommandLine::DEF_PARAM_SUFFIX)) {
    fs.setOutputSuffix(cmdl_value);
  }
  
  //-------------------------------------------------------------------------
  //
  // process the data
  //
  //-------------------------------------------------------------------------
  
  fs.compute(symbol, sdb, symbol_list_sdb);    
  
  // close the log file
  //
  if (log_file.ne(CommandLine::DEF_LFILE_NAME)) {
    Console::close();
  }
  
  // exit gracefully
  //
  return Integral::exit();
}
