#! /bin/csh -f
#  D. Sandwell and R. Mellors 09/09/97
#
#  Modified by Anders Hogrelius 04/18/2017
#
#  Script to preprocess Envisat L.1 ASAR data 
#  produces .SLC .PRM and .LED files from Envisat formatted archive files ( files with extension .E1 .E2 or .N1)
#
#
 if ($#argv < 2) then
  echo ""
  echo " Usage: ENVI_SLC_pre_process  <name_stem>  <earth_radius> "
  echo ""
  echo " name_stem     -    filename of the data file"
  echo " earth_radius  -    local earth radius (in meters) (put 0 if use default value)"
  echo ""
  echo " Example: "
  echo " command line: ENVI_SLC_pre_process ASA_IMS_1PNESA20060130_054255_000000182044_00392_20486_0000 6378000 "
  echo " will preprocess Envisat formatted L.1 ASAR data 'ASA_IMS_1PNESA20060130_054255_000000182044_00392_20486_0000.N1' "
  echo " then output parameter file 'ASA_IMS_1PNESA20060130_054255_000000182044_00392_20486_0000.PRM', SLC data file 'ASA_IMS_1PNESA20060130_054255_000000182044_00392_20486_0000.SLC' and LED file 'ASA_IMS_1PNESA20060130_054255_000000182044_00392_20486_0000.LED' "
  echo " with earth radius 6378000 meters"  
  echo ""
  echo " ENVI_SLC_pre_process will digest Envisat formatted L.1 data from the ERS-1 ERS-2 and ENVISAT platforms (files with extension .E1 .E2 or .N1)"
  echo ""
  echo " It will not (yet) read the Wide Scan mode format (file names starting with ASA_WSS_1P)"
  echo ""
  exit 1
 endif
#
 set ORBITS = "/usr/local/orbits"
 echo " "
 echo "========================="
 echo "Start pre-process $1"

#
#  make sure the file exists
#
set FEXTENSION = ""
set ORBDIR = ""
set SCID = 0

if ( -f $1.N1) then
set FEXTENSION = "N1"
set ORBDIR = "ENVI/Doris"
set SCID = 4
# ENVISAT
else if ( -f $1.E1) then
set FEXTENSION = "E1"
set ORBDIR = "ERS/ers1"
set SCID = 1
# ERS-1
else if ( -f $1.E2) then
set FEXTENSION = "E2"
set ORBDIR = "ERS/ers2"
set SCID = 2
#ERS-2
else
  echo ""
  echo "error - raw file does not have correct .N1 .E1 or .E2 suffix"
  echo ""
  exit 1
endif

#
# 1) extract binary data and initial PRM and LED files from archive 
#
echo envi_slc_decode $1.$FEXTENSION
envi_slc_decode $1.$FEXTENSION


#
#  2) make orbit file 
#
set START = `grep SC_clock_start $1.PRM | awk '{print $3}'`
set END = `grep SC_clock_stop $1.PRM | awk '{print $3}'`

if ($SCID == 1) then
    echo "Reading ERS1 orbits"
    echo dump_orbit_ers.pl $1 $ORBITS/ERS/ers1
    dump_orbit_ers.pl $1 $ORBITS/ERS/ers1
else if ($SCID == 2) then
    echo "REading ERS2 orbits"
    echo dump_orbit_ers.pl $1 $ORBITS/ERS/ers2
    dump_orbit_ers.pl $1 $ORBITS/ERS/ers2
else if ($SCID == 4) then
    echo "Reading ENVISAT orbits"
    echo dump_orbit_envi.pl $START $END $1.LED $ORBITS/$ORBDIR
    dump_orbit_envi.pl $START $END $1.LED $ORBITS/$ORBDIR
else
   echo "ERROR: Unknown satellite platform"
   exit 1
endif
#
#  4) add the orbital information but keep the FD1
#
set FD1 = `grep fd1 $1.PRM | awk '{print $3}'`
calc_dop_orb $1.PRM $1.log $2 $FD1
cat $1.log >> $1.PRM
update_PRM.csh $1.PRM fd1 $FD1
#
# clean unwanted files
#
 rm *$1.log*
#
 echo "End pre-process $1"
#
