#!/usr/bin/perl

# This program makes a new, shorter sciindex.tab file for the peer-reviewed
# version 2.0 of the DI ITS Encounter (science) data set.  This program will 
# only read RADREV entries of the input table because the PI wants only one 
# entry per FITS file in the new indices.
#
# The intent is to include these revised tables in the DI Documentation
# set instead in the actual data sets (index/), but may want to consider
# putting them there, too. 
#
# Inputs:
#
#   sciindex.tab_orig   = Original, wide science/image index table. 
#
# Outputs:
#
#   <output_table_name> = New, short science/image index table.
#
# To run (from index/):
#
#    >cd /n/diraid02/data/lien/diiv9p_enc_v2/CODE
#    >mk_new_sciindex_reduced.pl its_reduced_enc_index.tab
#
# History:
#
#    2007-07-19  sam  Created.
#
#---------------------------------------------------------------------------

# Logic:
# Grep all RADREV recs from original, wide sciindex.tab.
# Replace the extra space between each column.
# Replace each missing constant value 1.E32 with blanks in preparation for 
#   running the lcltool called nullval.
# Write selected columns using a new order to an intermediate file
# Run nullval on the intermediate file to produce the final version of a
#   short science index table.

if (@ARGV != 1)
  { die "Usage: mk_new_sciindex_reduced.pl <output_table_name> \n"; }
else
  { $outfile = $ARGV[0]; }

# First, extract all RADREV records from the original science index table
open (INTAB, "grep RADREV sciindex.tab_orig|") || die "Unable to open sciindex.tab_orig";
open (OUTTAB,">$outfile") || die "Unable to open $outfile";

while ($line = <INTAB>)
  { chomp($line);                 # Remove newline \n
    chop($line);                  # Remove carriage return \r
    print "$line";
    $line =~ s/, /;/g;            # Replace comma+space column delimiter with a colon
    $line =~ s/ 1.E32/      /g;   # Replace each missing constant values with blanks; there are no not-applicable constants (-1.E32)
    @col  = split(";",$line);     # Split into columns and store in an array
    @path = split("/",$col[0]);   # Split path to extract DOY
    print OUTTAB "$col[18],$col[8],$col[9],$col[10],$col[55],$col[73],$col[26],$col[96],$col[11],$col[17],$col[15],$col[16],$col[84],$col[95],$col[78],$col[89],\"$path[3]\",$col[5],$col[56],$col[57],$col[58],$col[59],$col[102],$col[103],$col[104],$col[105],$col[106],$col[107],$col[108],$col[109],$col[6],$col[19],$col[22],$col[24],$col[23],$col[25],$col[67],$col[68],$col[69],$col[70],$col[71],$col[72],$col[78],$col[79],$col[80],$col[81],$col[82],$col[83],$col[89],$col[90],$col[91],$col[92],$col[93],$col[94],$col[74],$col[75],$col[76],$col[85],$col[86],$col[87],$col[64],$col[65],$col[66],$col[3]\r\n";
  }

close INTAB;
close OUTTAB;

# Now run nullval to replace the blank values in specific columns to an appropriate formatted value (e.g., 9999.999) for a missing constant.
# system ("nullval $outfile_temp .... > $outfile")
