#!/bin/tcsh -f
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# copyright: 2016 to the present, california institute of technology.
# all rights reserved. united states government sponsorship acknowledged.
#
# THESE SCRIPTS ARE PROVIDED TO YOU "AS IS" WITH NO WARRANTIES OF CORRECTNESS. USE AT YOUR OWN RISK.
#
# Author: David Bekaert
# Organization: Jet Propulsion Laboratory, California Institute of Technology
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# generate the amplitude products
set amplitude_flag=n

set PRG = `basename "$0"`
set AUT = "David Bekaert, January 2016"
echo "$PRG $AUT"
echo " "

# working directory
set WORKDIR=$cwd

# checking the list for which the SB needs to be made
if ($#argv == 1) then
        set list = $argv[1]
        echo List given as input
        echo $list
else
        set list = small_baselines.list
endif

if (! -e $list ) then
    echo $list file does not exist, you need to generate this first. 
    echo Do this manually where you provide an interferogram on each line as: reference_YYYYMMDD secondary_YYYYMMDD  
    echo Or use some scripts e.g. run:
    echo \> mt_extract_info_isce \(in case you did not run PS before\)
    echo \> matlab
    echo \>\> ps_load_info
    echo \>\> sb_find or sb_find_delaunay
    echo
    exit 1
endif

# check if the SMALL_BASELINE dir already exists
if (! -e SMALL_BASELINES) then
    mkdir SMALL_BASELINES
endif
set SB_DIR = $WORKDIR/SMALL_BASELINES

# check if there is a fixed suffix for the slc names
set slc_suffix = `grep slc_suffix  input_file | awk '{print $2}'`
if ($slc_suffix != "") then
    echo SLC has a suffix as: $slc_suffix
endif


# quick check to see if the SM set-up was run before
if (! -e reference) then
    echo Looks like you did not run the SM processing or SM set-up before.
    echo 
    exit 1
endif


# getting the tpye of data, in case the data is already pre-processed as a stack
set datasource = `grep source_data input_file | awk '{print $2}'`
if ($datasource == "slc_stack") then
    echo Looks like an SLC stack is specified
    # check if there is an slc stack path specified
    set slc_stack_path = `grep slc_stack_path input_file | awk '{print $2}'`
    # check the reference date 
    set referencedate = `grep slc_stack_reference input_file | awk '{print $2}'`
    # give message in case not specified
    if (( "$slc_stack_path" == "" ) || ( "$referencedate" == "" ))  then
        echo Need to define:
        echo \- slc_stack_path as the path to the slc\'s
        echo \- slc_stack_reference as the reference of the \"NEW\" SM network as YYYYMMDD. This does not need to be the reference used to generate the stack
        echo
        exit 1
    endif

    # make sure the slash is added in case the variable is not empty
    if ($slc_stack_path != "") then    
        set slc_stack_path = `echo ${slc_stack_path}/`
        echo Stack path is: $slc_stack_path
    endif

else
    # Getting the reference date information 
    basename `ls reference/[0-9]*[0-9].slc$slc_suffix` | gawk '{print substr($0,1,8)}' > tmp.reference
    set referencedate = `cat tmp.reference`
    rm tmp.reference
endif

# output the reference date
echo
echo SM: Reference Date = $referencedate 
echo 

# loop over the set of SB interferogram to be made
echo Looping over the interferogram list now: 
set interferogram_number = 1
while ($interferogram_number  <= `cat $WORKDIR/$list | wc -l`)
   set interferogram = `cat $WORKDIR/$list | head -$interferogram_number | tail -1`
   echo $interferogram

   # splitting into reference and secondary
   set reference = `echo $interferogram | awk '{split($0,a," "); print a[1]'}`
   set secondary =  `echo $interferogram | awk '{split($0,a," "); print a[2]'}`

   # check if the interferogram directoy needs to be made
   set IFG_DIR = $SB_DIR/$reference"_"$secondary
   if (! -e $IFG_DIR) then
       mkdir $IFG_DIR
   endif
    
   # retrieving the reference and secondary SLC name
   if ($datasource == "slc_stack") then
        # for and pre-processed SLC stack, the structure is YYYYMMDD/YYYYMMDD.slc, with the SM reference assumed to be in reference/yyyymmdd.slc
        set reference_slc = ${slc_stack_path}$reference/$reference.slc$slc_suffix
        set secondary_slc = ${slc_stack_path}$secondary/$secondary.slc$slc_suffix
    else if ($datasource == "slc_stack_burst") then
        echo TO BE IMPLEMENTED
        echo
        exit 1
    else
        # the format when processing the stack using isce2stamps
        # check if the reference of the SB interferogram reference is the SM reference as these have a different directory  
        if ($reference == $referencedate) then
            set reference_slc = reference/$referencedate.slc$slc_suffix
        else
            set reference_slc = $reference/coreg_fine/coreg.slc
        endif
        if ($secondary == $referencedate) then
            set secondary_slc = reference/$referencedate.slc$slc_suffix
        else  
            set secondary_slc = $secondary/coreg_fine/coreg.slc
        endif


   endif
   set save_ifg = $IFG_DIR/isce_minrefdem.int

   # store the information on how isce will be called
   echo "imageMath.py -e='a*conj(b)' --a=$reference_slc --b=$secondary_slc -o $save_ifg -t CFLOAT -s BIP" >> processing_SB.log
   echo "imageMath.py -e='a*conj(b)' --a=$reference_slc --b=$secondary_slc -o $save_ifg -t CFLOAT -s BIP"
   imageMath.py -e='a*conj(b)' --a=$reference_slc --b=$secondary_slc -o $save_ifg -t CFLOAT -s BIP


   # making the amplitude file
   if ( "$amplitude_flag" == "y") then
       set save_ampl = $IFG_DIR/isce_minrefdem.amp
       # making the amplitude for the ifgs, for ISCE the amp is a BIP file typically
       echo "imageMath.py -e='abs(a);abs(b)' --a=$reference_slc --b=$secondary_slc -o $save_ampl -t FLOAT -s BIP" >> processing_SB.log
       echo "imageMath.py -e='abs(a);abs(b)' --a=$reference_slc --b=$secondary_slc -o $save_ampl -t FLOAT -s BIP"
       imageMath.py -e='abs(a);abs(b)' --a=$reference_slc --b=$secondary_slc -o $save_ampl -t FLOAT -s BIP
   endif


   # include a symbolic link for the secondary and reference slc.
   # This will be used latter on to get the amplitude information from
   cd $IFG_DIR
   if ($datasource == "slc_stack") then
       ln -s -f $secondary_slc secondary.slc
       ln -s -f  $reference_slc reference.slc
   else if ($datasource == "slc_stack_burst") then
       echo TO BE IMPLEMENTED
       echo
       exit 1
   else
       ln -s -f ../../$secondary_slc secondary.slc
       ln -s -f  ../../$reference_slc reference.slc
   endif
   cd $WORKDIR


   @ interferogram_number = $interferogram_number + 1
   echo 
   echo
end
cd $WORKDIR

