#!/usr/bin/perl
### ers2roi.pl

$] >= 5.004 or die "Perl version must be >= 5.004 (Currently $]).\n";

use Env qw(INT_SCR INT_BIN);
use lib "$INT_SCR";  #### Location of Generic.pm
use Generic;

###Usage info/check
sub Usage{
print STDERR "\n";
print STDERR "Usage:\n";
print STDERR "\n";
print STDERR "      ers2roi.pl [ORB] [DIR] \n";
print STDERR "\n";
print STDERR "ers2roi.pl converts ERS Winsar archive files \n";
print STDERR "into raw data in a ROI_PAC format; \n";
print STDERR "ORB is an orbit type, e.g., PRC (default), or ODR \n";
print STDERR "DIR is the track directory (default is .) \n";
exit 1;
}

#@ARGV >= 0  or Usage();
@args = @ARGV;

$ORB    = shift or $ORB = "PRC";
$dir0   = shift or chomp($dir0=`pwd`);

if($ORB eq "\?"){Usage();}

chdir("$dir0") || die "cannot cd to $dir0";
#chomp($dir0=`pwd`);

#@imagery=split /\s+/, `ls *.raw.gz`;
@imagery=split /\s+/, `ls *.zip`;
if ($#imagery < 0)
{
print STDERR "There are no *.zip files in this directory \n";
exit 1;
}
#@vdfs =split /\s+/, `ls *.vdf`;

$no_vdf = 1; 
#if ( $#imagery == $#vdfs ) {  # see if there are any missing VDF files
# foreach $vdfs (@vdfs){    # cycle through all scenes    
#  @fields=split /\s+/, `ls -l $vdfs`;
#  $vdf_size=@fields[4]; 
#  if ($vdf_size < 1 ){$no_vdf = 1}
# }
#}
#else { $no_vdf = 1 }

###########################
print STDERR "Begin conversion\n";
###########################

$fr_cnt = 0;
while($#imagery >= 0){       # cycle through all the *.ldr files
 print STDERR "working on file $imagery[0] \n";
 @tags=split /\_/, $imagery[0];
 $frame_num=@tags[2];
# $frame_num=substr($imagery[0],-11); # save last 8 characters
# $frame_num=substr($frame_num,0,-7); # delete last 4 characters
 @frame=split /\s+/, `ls *$frame_num*.zip`;
 $fr_num=0;
 foreach $frame (@frame){    # cycle through all frames for a given track    
  $fname=substr($frame,0,-4);
  @fields=split /\s+/, `ls -l $fname.zip`;
  $raw_size=@fields[4];
  if ($raw_size < 1 ){die "Raw data file $fname.zip does not exist \n"};
  `unzip $fname.zip`;
  `chmod 644 $fname.zip`;
  if ($no_vdf == 0 ) { # use load_cd script
   if ($OS eq "Irix"){`load_cd_sgi.pl $dir0`; }
   else{`load_cd.pl $dir0`; }
   chomp($LEADER=`ls SARLEA*`);
#  $fulldate=substr($LEADER,9,8);
   $date=substr($LEADER,11,6);
  }
  else {  # use sequential name convention
   $fr1 = 1 + $fr_cnt;    
   $raw = "IMAGERY"."$fr1";
   $lea = "SARLEADER"."$fr1";
   `chmod 644 *.001`;
   `mv DAT_01.001 $raw`;
   `mv LEA_01.001 $lea`;
   chomp($LEADER=`ls SARLEA*`);
   $g_facility = ByteRead ($LEADER, 1766, 15);
   $facility or $facility = $g_facility;
   $version    = ByteRead ($LEADER, 1782, 8);
   $sat_num    = ByteRead ($LEADER, 1119, 2);
   $orbit_num  = ByteRead ($LEADER, 1164, 8); 
   $sat = sqrt($sat_num**2);
   $sat = "ERS$sat";
   $facility =~ s/\s//g;
#   print STDERR "$facility \n";
   $facility = "D-PAF";
   if    ($facility eq "CRDC_SARDPF"
            or $facility eq "GTS")              { $facility = "CRDC-SARDPF";}
   elsif ($facility eq "UK-PAF")                   { $facility = "UK-PAF";}
   elsif ($facility eq "D-PAF"){
     if ($version =~ /VMP/)                        { $facility = "D-PAF";}
     elsif ($version =~ /MSAR/)                    { $facility = "NEW-D-PAF";}
}
   elsif ($facility =~ /^(NEW-D-PAF|ESRIN|ES|IP|IPAF|I-PAF)$/){ $facility = "D-PAF";}
   elsif ($facility =~ /^(SCRIPPS)$/) { $facility = "SCRIPPS";}
   else {die "Unknown facility: $facility\n";}
   $call_leader="$INT_BIN/leader2rsc $LEADER $INT_SCR/format_leaderfile_$facility tmp_IMAGERY.raw.rsc"; 
#   Message "$call_leader";
   `$call_leader`;
   $day   = Use_rsc "tmp_IMAGERY.raw read FIRST_LINE_DAY_OF_MONTH"; 
   $month = Use_rsc "tmp_IMAGERY.raw read FIRST_LINE_MONTH_OF_YEAR"; 
   $year  = Use_rsc "tmp_IMAGERY.raw read FIRST_LINE_YEAR";
   $date=substr($year,-2).$month.$day;
   `/bin/rm tmp*`;
  }
  open ORBIN, ">>date.orb" or die "Can't write to date.orb\n";
  print ORBIN "$fname $date \n";
  close(ORBIN);
  if (-e "$dir0/$date"){}
  else{`mkdir $dir0/$date`;}
  @dirs[$fr_num]=$date;      # keep track of acquisition dates
#  `mv SA*$date* $dir0/$date`;
  `mv SARLEA* $dir0/$date`;
  `mv IMA* $dir0/$date`;
  `/bin/rm VDF_* NUL_* QUIC* SAR_IM* $fname.zip`;
  if ($no_vdf == 0 ) {`/bin/rm VD*`};
#  `mv VD*$date* $dir0/$date`;
  $fr_num++;
 }
 $fr_cnt++;
 @imagery=split /\s+/, `ls *.zip`;
}

 foreach $dirs (@dirs){
  print STDERR "$dir0/$dirs \n";
  chdir("$dir0/$dirs") || die "cannot cd to $dir0/$dirs";
  chomp(@LEADER=`ls SARLEA*`);
  `make_raw_all.pl $ORB $dirs `;
#  `make_raw.pl $ORB @LEADER[0] $dirs `;
#  `/usr/bin/rm SA* IM*`;
#  print STDERR "working on file $fname \n";
  }
#  update data list:
  chdir("$dir0") || die "cannot cd to $dir0";
  `sort date.orb | uniq > junk.orb`;
  `/bin/mv junk.orb date.orb`;
  
  print STDERR "\n";


#########################
#print STDERR "Finished processing\n";
#print STDERR "total of: $count files\n";
#########################

exit 0;


#    Perl  Script : Yuri Fialko 09/17/2001

