#!/usr/bin/perl

# This program makes the PDS-required, short index.tab file from the
# index.tab files originally delivered with the DI ITS NAV Reduced Encounter
# data set (diivnv_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/diivnv_enc/index
#    >../CODE/extract_required_index.pl
#
# History:
#
#    2007-05-27  sam  Created.
#
#---------------------------------------------------------------------------

open (INTAB, "<../index/index.tab_orig") || die "Unable to open index/index.tab_orig";
open (OUTTAB,">../index/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 IMPACTOR SPACECRAFT","DEEP IMPACT IMPACTOR TARGETING SENSOR - 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[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],$instinfo\r\n";
}

close INTAB;
close OUTTAB;

