#!/usr/bin/perl

# This program makes the PDS-required, short index.tab file from the
# index.tab files originally delivered with the DI NAV Reduced Encounter
# data set (dihvnv_enc).
#
# Inputs:
#
#   index.tab_orig = Original index file.  May need to taylor which colums
#                    are extracted based on the specific index file.
#                    MUST FIRST CONVERT ", " to ";" before running.
#
# Outputs:
#
#   index.tab       = PDS-required, short index table.
#
#
# To run (from index/):
#
#    >cd /n/diraid02/data/lien/dihvnv_enc/index
#    >../CODE/extract_required_index.pl
#
# History:
#
#    2007-05-27  sam  Created.
#
#---------------------------------------------------------------------------

open (INTAB, "<index.tab_orig") || die "Unable to open index/index.tab_orig";
open (OUTTAB,">index.tab")       || die "Unable to open index/index.tab";

for ($i=0; $i<2; $i++)
{ $line=<INTAB>; }              # Read past the two header records

$instinfo = '"DEEP IMPACT FLYBY SPACECRAFT","DEEP IMPACT HIGH RESOLUTION INSTRUMENT - VISIBLE CCD"';

while ($line=<INTAB>)
{ chomp($line);                 # Remove newline \n
  chop($line);                  # Remove carriage return \r
  $line =~ s/, /;/g;            # Replace comma+space column delimiter with a colon
  @col = split(";",$line);      # Split into columns and store in an array
  $col[92] =~ s/ //g;           # volid: Remove trailing blanks in several alpha columns
  $col[0]  =~ s/ //g;           # path name
  $col[1]  =~ s/ //g;           # label name
  #$col[4]  =~ s/ //g;           # target name
  $col[15] =~ s/ //g;           # filter
  #$col[28] =~ s/ //;            # pixel scale
  #$col[30] =~ s/ //;            # boresight RA
  #$col[31] =~ s/ //;            # boressight DEC
  #$col[32] =~ s/ //;            # CelN clock angle
  #$col[33] =~ s/ //;            # EclN clock angle
  #$col[47] =~ s/ //;            # target-S/C distance
  #$col[58] =~ s/ //;            # target-Sun distance
  #$col[69] =~ s/ //;            # target-Earth distance
  #$col[70] =~ s/ //;            # phase angle
  $col[87] =~ s/ //g;           # data set id
  $col[85] =~ s/ //g;           # product id
  $col[84] =~ s/RAW   /RADREV/; # reduction level
  print OUTTAB "$col[92],$col[0],$col[1],$col[87],$col[85],$col[86],$col[84],$col[6],$col[19],$col[4],$col[15],$instinfo\r\n";
  #print OUTTAB "$col[92],$col[0],$col[1],$col[87],$col[85],$col[86],$col[84],$col[6],$col[19],$col[4],$col[15], $col[30],$col[31], $col[32], $col[33],$col[70],$col[28],$col[47],$col[58],$col[69],$instinfo\r\n";
}

close INTAB;
close OUTTAB;

