#!/usr/bin/perl

# This program makes move a move statement for each product (.fit & .lbl)
# that needs to be moved from a "ground-received-time" directory to a
# "observation date" directory.
#
# Inputs:
#
#   index.tab_orig = Index files **after** moves done for known raw2reduced
#                    products 
#
# Outputs:
#
#   move_its_rawenc_rest.txt
#
#
# To run (from index/):
#
#    >cd /n/diraid02/data/lien/dihvnv_rawenc/CODE
#    >../make_data_move_list.pl
#
# History:
#
#    2007-06-07  sam  Created.
#
#---------------------------------------------------------------------------

open (INTAB,  "<../index/index.tab_orig")         || die "Unable to open index/index.tab_orig";
#open (OUTTAB, ">index.tab_revised")              || die "Unable to open index.tab_revised";
open (OUTLIST1,">move_its_rawenc_rest.txt")       || die "Unable to open move_its_rawenc_rest.txt";
open (OUTLIST2,">move_its_rawenc_rest_check.txt") || die "Unable to open move_its_rawenc_rest_check.txt";

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

#  Col  Name
#    0  path
#    1  label name
#    4  target
#    7  start time
#   19  image mid time
#   20  stop time
#   72  reduction level
#   73  product id
#   74  product creation time
#   75  data set id
#   78  instrument host id
#   79  instrument id
#   80  volume id

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[0]  =~ s/ //g;           # Remove trailing blanks in several alpha columns
  $col[1]  =~ s/ //g;
  $col[4]  =~ s/ //g;
  $col[7]  =~ s/ //g;
  $col[19] =~ s/ //g;
  $col[20] =~ s/ //g;
  $col[72] =~ s/ //g;
  $col[73] =~ s/ //g;
  $col[74] =~ s/ //g;
  $col[75] =~ s/ //g;
  $col[78] =~ s/ //g;
  $col[79] =~ s/ //g;
  $col[80] =~ s/ //g; 
  $col[0]  =~ s/"//g;           
  $col[1]  =~ s/"//g;
  $col[4]  =~ s/"//g;
  $col[7]  =~ s/"//g;
  $col[19] =~ s/"//g;
  $col[20] =~ s/"//g;
  $col[72] =~ s/"//g;
  $col[73] =~ s/"//g;
  $col[74] =~ s/"//g;
  $col[75] =~ s/"//g;
  $col[78] =~ s/"//g;
  $col[79] =~ s/"//g;
  $col[80] =~ s/"//g;                   
  @path    = split("/",$col[0]);    # For raw data, DOY is the third element, $path[2]
  @middt   = split("T",$col[19]);   # Extract the date from image mid time, $middt[0]
  $middoy  = date2doy($middt[0]);   # Convert image mid time to DOY
  $middt_chk = doy2date($middoy);   # Check date2doy conversion 
  if ($middoy ne $path[2])
  { #$obsdoy = sprintf("data/2005/%s/",$middt[0]);   
    $col[0] =~ tr/[A-Z]/[a-z]/;     # convert path to lowercase
    $col[1] =~ tr/[A-Z]/[a-z]/;     # convert label name to lowercase
    $col[1] =~ s/lbl/\*/;           # convert label name extention to *
    if ($middt[0] ne $middt_chk)
    { $note = "obs DOY ne image mid time";
    }
    print OUTLIST1 "mv $col[0]$col[1] data/2005/$middoy/\r\n"; 
    print OUTLIST2 "mv $col[0]$col[1] data/2005/$middoy/,$col[7],$col[19],$col[20],$middt_chk,$note\r\n"; 
  }  
}

close INTAB;
#close OUTTAB;
close OUTLIST1;
close OUTLIST2;


# Examples:
#
#($hdrfile = $file) =~ s/dat/h99/;
#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";
#printf "%s \n",&doy2date($ARGV[0]);  #<- can just use this in printf statement!


#===============================================================================
# Converts a 3-digit DOY (ddd) for 2005 to a date (yyyy-mm-dd ).
#===============================================================================

sub doy2date

{ local ($indoy)     = $_[0];          # input fractional doy
  my ($outmonth,$outday);              # output variables
  my $i;                               # index
  my ($doy);                           # work variables
  my @months = (0,31,59,90,120,151,181,212,243,273,304,334,365);

  # Convert DOY to a month and day; Year 2005 is assumed!
  $doy     = int($indoy);
  #print "1) $indoy,$doy \n";
  for($i=1;$i<=12;$i++) 
  { if ($doy <= $months[$i]) 
    { #print "2) $doy,$i,$months[$i]\n"; 
      goto gotmonth; 
    } 
  }
  gotmonth:
  $outmonth = $i;
  $outday   = $doy - $months[$i-1];

  #print "3) $outmonth,$outday,$outhour,$outmin,$outsec \n";
  #print "4) $doy,$hours,$minutes,$seconds \n";

  #$outdate = sprintf("2005-%02d-%02dT%02d:%02d:%02d",$outmonth,$outday,$outhour,$outmin,$outsec);

  return sprintf("2005-%02d-%02d",$outmonth,$outday);
}


#===============================================================================
# Convert a date (yyyy-mm-dd) to a 3-digit DOY (ddd).
#===============================================================================

sub date2doy

{ local ($indate)     = $_[0];   # input date in yyyy-mm-dd format
  my ($year,$month,$day);        # split input date to these scalars
  my $doy;                       # day of year to return

  ($year,$month,$day) = split(/-/,$indate);

  if (($year =~ /1996/) || ($year =~ /2000/))
    { if ($month =~ /01/) {$doy = $day;}
      elsif ($month =~ /02/) {$doy = $day+31;}
      elsif ($month =~ /03/) {$doy = $day+60;}
      elsif ($month =~ /04/) {$doy = $day+91;}
      elsif ($month =~ /05/) {$doy = $day+121;}
      elsif ($month =~ /06/) {$doy = $day+152;}
      elsif ($month =~ /07/) {$doy = $day+182;}
      elsif ($month =~ /08/) {$doy = $day+213;}
      elsif ($month =~ /09/) {$doy = $day+244;}
      elsif ($month =~ /10/) {$doy = $day+274;}
      elsif ($month =~ /11/) {$doy = $day+305;}
      elsif ($month =~ /12/) {$doy = $day+335;}
    }
  else
    { if ($month =~ /01/) {$doy = $day;}
      elsif ($month =~ /02/) {$doy = $day+31;}
      elsif ($month =~ /03/) {$doy = $day+59;}
      elsif ($month =~ /04/) {$doy = $day+90;}
      elsif ($month =~ /05/) {$doy = $day+120;}
      elsif ($month =~ /06/) {$doy = $day+151;}
      elsif ($month =~ /07/) {$doy = $day+181;}
      elsif ($month =~ /08/) {$doy = $day+212;}
      elsif ($month =~ /09/) {$doy = $day+243;}
      elsif ($month =~ /10/) {$doy = $day+273;}
      elsif ($month =~ /11/) {$doy = $day+304;}
      elsif ($month =~ /12/) {$doy = $day+334;}
    }

  return sprintf("%03d",$doy);
}


