#!/usr/bin/env perl
#=======================================================================
#
#  NAME
#
#  get_keyword_info.pl
#
#  DESCRIPTION
#
#  Subroutines to get information from the specific tables in the 
#  keyword database.
#
#  OPTIONS
#
#   None
#
#  HISTORY
#
#   02/15/2004 49680 L Gardner Initial Release
#   09/19/06   56505 MSwam     Use fieldname prefixes on KWDB tables
#   06/13/2008 60161 L Gardner Add new extensions for COS
#   10/28/2008 61112 L Gardner Changed GetExtensionList to GetSuffixList
#                              Added GetImsetExtList
#
#=======================================================================

#-----------------------------------------------------------------------
#
# Subroutine GetTableType:
#   Load in the ingest_tables table from the keyword database.
#   One row is returned for each table and we get the selector value.
#   If the selector is MEMBEXP it means that only exposure members
#   of an association should populate the table.
#
#-----------------------------------------------------------------------
sub GetTableType
{
   my ( $dbh ) = @_;

   my $query = "select igt_mission, igt_instrument, igt_tablename, igt_selector " .
               "from ingest_tables ";

   my @dat = GetDbValues( $dbh, $query );
   foreach my $row ( @dat ) {
      my ( $mission, $instr, $tableName, $selector ) = @$row;
      $selector = 'PRODEXP' if $selector =~ /^t_/ and $instr ne 'WFPC2';
      $selector = 'PRODEXP' if $instr eq 'OMS' and $tableName eq 'oms_summary';
      $selector = 'MEMBEXP' if $instr eq 'OMS' and $tableName eq 'oms_data';
      $selector = 'MEMBEXP' if $instr eq 'FUSE' and 
                               $tableName eq 'fuse_exposures';
      $tableType{$mission}{$instr}{$tableName} = $selector;
   }

   return %tableType;
}

#-----------------------------------------------------------------------
#
# Subroutine GetKeyFields:
#   Load in the ingest_keyfields table from the keyword database.
#   Get the key fields that will be used in constructing the where clause.
#   Store them in a hash to be used later.
#
#-----------------------------------------------------------------------
sub GetKeyFields
{
   my ( $dbh, $acWhere ) = @_;

   my %keyFields = ();

   my $query = "select igk_mission, igk_instrument, igk_tablename, igk_fieldname, " . 
               "igk_field_order from ingest_keyfields where igk_archive_class $acWhere";
   
   my @dat = GetDbValues( $dbh, $query );
   foreach my $row ( @dat ) {
      my ( $mission, $instr, $tableName, $fieldName, $order ) = @$row;
      $keyFields{$mission}{$instr}{$tableName}{$fieldName} = $order;
   }
   return %keyFields;
}
#-----------------------------------------------------------------------
#
# Subroutine GetSuffixList:
#   Load in the ingest_files_mapping table from the keyword database.
#   Store the suffixes and the open order in a hash so will know which
#   files to open and in which order.
#
#-----------------------------------------------------------------------
sub GetSuffixList
{
   my ( $dbh ) = @_;

   my %suffixList = ();

   my $query = "SELECT ifm_mission, ifm_instrument, ifm_assoc, ifm_suffix, ".
                       "ifm_open_order " .
               "FROM ingest_files_mapping " .
               "WHERE ifm_mission in ('HST')";
               "ORDER BY ifm_mission, ifm_instrument, ifm_assoc, ifm_suffix, ifm_open_order";
   
   my @dat = GetDbValues( $dbh, $query );
   foreach my $row ( @dat ) {
      my ( $mission, $instr, $assoc, $suffix, $order ) = @$row;
      $suffixList{$mission}{$instr}{$assoc}{$suffix} = $order 
         unless $suffixList{$mission}{$instr}{$assoc}{$suffix};
   }
   return %suffixList;
}
#-----------------------------------------------------------------------
#
# Subroutine GetImsetExtList:
#   Load in the ingest_files_mapping table from the keyword database.
#   Store the the imset extensions to know which ones to process.
#
#-----------------------------------------------------------------------
sub GetImsetExtList
{
   my ( $dbh ) = @_;

   my %imsetExtList = ();

   my $query = "SELECT ifm_instrument, ifm_suffix, ifm_extension " .
               "FROM ingest_files_mapping " .
               "WHERE ifm_mission in ('HST')";
   
   my @dat = GetDbValues( $dbh, $query );
   foreach my $row ( @dat ) {
      my ( $instr, $suffix, $ext ) = @$row;
      $imsetExtList{$instr}{$suffix}{$ext} = 1;
   }
   return %imsetExtList;
}
#-----------------------------------------------------------------------
#
# GetNonKywdFlds
#   Get list of fields that are in tables with keyword populated fields
#   but which are not populated by keywords themselves.
#   Note: This is wrong.  It returns all fields other than what is in
#         dads_keywords for this one instrument.
#
#-----------------------------------------------------------------------
sub GetNonKywdFlds
{
   my ( $dbh ) = @_;

   my %skipFld = ();

   my $query = <<"   EOQ";
   SELECT hda_fieldname FROM hda_fields WHERE hda_fieldname NOT IN
   ( SELECT dkw_fieldname FROM dads_keywords )
   AND SUBSTRING(hda_fieldname,1,3) IN 
   ( SELECT SUBSTRING(dkw_fieldname,1,3) FROM dads_keywords )
   EOQ

   my @dat = GetDbValues( $dbh, $query );
   foreach my $row ( @dat ) {
      my ( $fldName ) = @$row;
      $skipFld{fldName} = 1;
   }
   return %skipFld;
}

#-----------------------------------------------------------------------
#
# GetHeaderName
#   We are mapping instr/kywd to section names.  These will be used
#   in processing left over keywords to see whether the keywords
#   should be in the file.
#
#-----------------------------------------------------------------------
sub GetHeaderName
{
   my ( $dbh ) = @_;

   my %headerName = ();

   my $query = <<"   EOQ";
   SELECT kwm_keyword, hdf_section_name FROM keyword_main a, header_file b
   WHERE  kwm_instrument = hdf_instrument AND kwm_groupname = hdf_groupname
   EOQ

   my @dat = GetDbValues( $dbh, $query );
   foreach my $row ( @dat ) {
      my ( $kywd, $secName ) = @$row;
      next if $secName !~ /_/;
      my $instr = $secName;
      $instr =~ s/^([^_]+)_.*/$1/;
      $instr = 'NICMOS' if $instr eq 'NIC';
      $instr = 'WFPC2' if $instr eq 'WF2';
      $headerName{HST}{$instr}{$kywd}{$secName} = 1;
   }
   return %headerName;
}
1;
