#!/usr/bin/perl
use FileHandle;
use File::Basename;

if (($#ARGV + 1) < 3){die <<EOS;}
*** Preprocessing of TerraSAR-X TDX1 and TSX1 SLC products using par_TX_SLC ***
*** Copyright 2013, Gamma Remote Sensing, v1.2 22-Oct-2013 clw ***

usage: $0 <TSX_list> <SLC_dir> <log>
  TX_list  (input) single column text file with directories (including path) 
           containing path to directory containing product XML for IMAGEDATA/*.cos files
  SLC_dir  directory for output SLC data files and SLC parameter files
  log      (output) processing log file
EOS

$out_dir = $ARGV[1];
$log = "$out_dir/$ARGV[2]";

unless (-d $out_dir) {
  print "creating output directory for TSX products: $out_dir\n";
  $exit = system("mkdir $out_dir");
  $exit == 0 or die "ERROR $0: non-zero exit status: mkdir $out_dir\n"
}

open(LOG,">$log") or die "ERROR $0: cannot open log file: $log\n";
LOG->autoflush;			#set filehandle special variable to flush output immediately
print LOG "$0 @ARGV\n\n";	#print command line that was used
print "log file: $log\n";

$time = localtime;
print "PROCESSING START: $time\n\n";
print LOG "PROCESSING START: $time\n\n";
open(SLIST, "<$ARGV[0]") or die "ERROR $0: TerraSAR-X list of product directories does not exist: $ARGV[0]\n";
print "TSX product list: $ARGV[0]\n";
print "SLC output directory: $ARGV[1]\n";

LINE: while (<SLIST>) {		#read list of directories with unreformated SLC data
  chomp $_;
  next LINE if /^$/; 		#skip blank lines in input list
  next LINE if /^#/; 		#skip comments in the input list
  @fields = split;		#extract the identifier
  $path = $fields[0];

#  @files = glob "$path/TSX-1.SAR.L1B/TDX1*/TDX1*.xml";		# TDX data
  @files = glob "$path/TSX1_SAR*.xml $path/TDX1_SAR*.xml";
  $ann_xml = $files[0];
  print "\n*** TerraSAR-X Product Directory: $path\n";
  print "XML annotation file: $ann_xml\n";

  my ($base, $dir1, $ext1) = fileparse($ann_xml, qr/\.[^.]*/); #extension is the last bit after the last period
  $satname = substr($base,0,4);
  print "satellite: $satname\n";
  $tstart = substr($base, -31, 15);
  print "SLC start time (YYYYMMDDTHHMMSS): $tstart\n";

#  @files = glob "$path/TSX-1.SAR.L1B/TDX1*/IMAGEDATA/*.cos";  #TDX data
  @files = glob "$path/IMAGEDATA/*.cos";
  
  for ($i= 0; $i < scalar @files; $i = $i+1){
    $cos = $files[$i];
    my ($base, $dir1, $ext1) = fileparse($cos, qr/\.[^.]*/); #extension is the last bit after the last period
    printf "\nTX image: $base$ext1\n";
    $pol = substr($base,5,3);
    $id =$tstart."_".$satname.$pol;
    
    $slc_par = "$out_dir"."\/".$id.".slc.par";
    $slc = "$out_dir"."\/".$id.".slc";
    execute("par_TX_SLC $ann_xml $cos $slc_par $slc", $log);
  }
}

$time = localtime;
print "\nPROCESSING END: $time\n";
print LOG "\nPROCESSING END: $time\n";
exit 0;

sub extract_param{
  my ($infile,$keyword) = @_;
  open(PAR_IN,$infile) || die "ERROR $0: cannot open parameter file: $infile\n";

  while(<PAR_IN>){
    chomp;
    @tokens = split;
    if($tokens[0] eq $keyword){close PAR_IN; return @tokens;}
  }
  close PAR_IN;
  die "ERROR $0: keyword $keyword not found in file: $infile\n";
}

sub execute{
  my ($command, $log) = @_;
  print "$command\n";
  print LOG ("\n${command}\n");
  close LOG;
  $exit = system("$command 1>> $log 2>&1");
  $exit == 0 or die "ERROR $0: non-zero exit status: $command\n";
  if (-e $log){
    open(LOG,">>$log") or die "ERROR $0: cannot open log file: $log\n";
  }
  else {
    open(LOG,">$log") or die "ERROR : cannot open log file: $log\n";
  }
  LOG->autoflush;
}
