#!/usr/bin/perl -W

# This program corrects the DOY in the path column of the reviewed index table
# then used the revised long table to make the PDS-required, short index.tab 
# for the DI NAV Raw Encounter data set (dihvnv_rawenc).
#
# 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_reviewed  = PDS-required, short index table.
#
#
# To run (from index/):
#
#    >cd /n/diraid02/data/lien/dihvnv_rawenc/index
#    >../CODE/correct_doy_in_index.pl
#
# History:
#
#    2007-06-14  sam  Created.
#
#---------------------------------------------------------------------------

system "sort -k 1.2,1.14 -k 1.30,1.56 -o index.tab_reviewed_sorted index.tab_reviewed";
system "sort -k 1.4,1.16 -k 1.18,1.44 -o move_mri_rawenc_rest.txt_sorted move_mri_rawenc_rest.txt";

open (INTAB, "<index.tab_reviewed_sorted") || die "Unable to open index.tab_reviewed_sorted";
open (INMV,"<move_mri_rawenc_rest.txt_sorted") || die "Unable to open move_mri_rawenc_rest.txt_sorted";
open (OUTTAB,">index_doy_corrected.tab_short") || die "Unable to open index_doy_corrected.tab_short";
open (OUTCHK,">check_results.txt") || die "Unable to open check_results.txt";

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

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

# INTAB sample record:
#"DATA/2005/121/          ", "DOM5000100_2005121211802000.LBL",
# INMV sample record:
#mv data/2005/138/dom5001736_2005138184311000.* data/2005/137/


$cnt = 1;
$mvline=<INMV>;
$mvline =~ tr/a-z/A-Z/;
while ($tabline=<INTAB>)
{ #if ($cnt < 100) {print "MV :",$mvline; print "IDX:",$tabline; }
  if ((substr($mvline,3,14) eq substr($tabline,1,14)) && (substr($mvline,17,27) eq substr($tabline,29,27)))
    { $taborig = $tabline;
      substr($tabline,11,3,substr($mvline,57,3));
      print "----\n";
      print "MV :",substr($mvline,3,14),"\n";
      print "MV :",substr($mvline,17,27),"\n";
      print "MV :",substr($mvline,57,3),"\n";
      print "IDX:",substr($tabline,1,14),"\n";
      print "IDX:",substr($tabline,29,27),"\n";
      print "STR:",substr($tabline,11,3,substr($mvline,57,3)),"\n";
      print "CNT:",$cnt,"\n";
      $tabnew = $tabline;
      $taborig =~ tr/A-Z/a-z/;
      $tabnew  =~ tr/A-Z/a-z/;
      print OUTCHK "mv ",substr($taborig,1,14),substr($tabnew,29,28),"* data/2005/",substr($tabnew,11,4),"\r\n";
      $cnt++;  
      if ($cnt <= 412 ) 
        { $mvline=<INMV>;
          $mvline =~ tr/a-z/A-Z/;
        }
      else
        { $mvline = "                                                             "; 
        }
    }
  chomp($tabline);              # Remove newline \n
  chop($tabline);               # Remove carriage return \r    
  $tabline =~ s/, /;/g;         # Replace comma+space column delimiter with a colon
  @col = split(";",$tabline);   # Split into columns and store in an array
  $col[80] =~ s/ //g;           # Remove trailing blanks in several alpha columns
  $col[0]  =~ s/ //g;
  $col[75] =~ s/ //g;
  $col[73] =~ s/ //g;
  $col[72] =~ s/ //g;
  print OUTTAB "$col[80],$col[0],$col[1],$col[75],$col[73],$col[74],$col[72],$col[7],$col[20],$col[4],$instinfo\r\n";
}
print "$cnt\n";

#while ($mvline=<INMV>)
#{ $mvline =~ tr/a-z/A-Z/;
#  #$mvline =~ s/\*/LBL/;
#  #@mvcol = split(" ",$mvline);
#  read_another:
#  $tabline=<INTAB>;
#  if ((substr($mvline,3,14) = substr($tabline,1,14)) && (substr($mvline,3,17) = substr($tabline,29,27)))
#    { substr($tabline,11,3,substr($mvline,57,3));
#      $readmore = 1;
#    }
#  else  
#    { }
#  chomp($tabline);             # Remove newline \n
#  chop($tabline);              # Remove carriage return \r
#  $tabline =~ s/, /;/g;        # Replace comma+space column delimiter with a colon
#  @col = split(";",$tabline);  # Split into columns and store in an array
#  $col[80] =~ s/ //g;          # Remove trailing blanks in several alpha columns
#  $col[0]  =~ s/ //g;
#  $col[75] =~ s/ //g;
#  $col[73] =~ s/ //g;
#  $col[72] =~ s/ //g;
#  print OUTTAB "$col[80],$col[0],$col[1],$col[75],$col[73],$col[74],$col[72],$col[7],$col[20],$col[4
#],$instinfo\r\n";
#}

close INTAB;
close INMV;
close OUTTAB;
close OUTCHK;

