#!/bin/csh -f
#
# Erith Alexander Munoz 10 Octubre 2018
#
# This script set up the necesaries files and steps required to
# execute the p2p_S1A_SLC.csh script from GMT.
#
#-----------------------------------------------------------------------------------------------
#
#                                   p2p_S1_SLC_Launcher.csh
#
#-----------------------------------------------------------------------------------------------

if ($#argv != 1) then 
	echo "Usage:  "
	echo ""
	echo "p2p_S1_SLC_Launcher.csh burstNumber"
	echo ""
endif
#
# start
set burts=$1
	echo $burts
if (! -d orig) then
	echo "You have to make a directory called orig"
	echo "and sabe the two uncompressed sentinel images"
	echo "to conform the interferometric pair"
else
set tag1=001
set tag2=002
set tag3=003
set tag4=1SSV
set tag5=1SDV
if (! -d raw) then
mkdir -p raw
endif
if (! -d intf) then
mkdir -p intf
endif
if (! -d SLC) then
mkdir -p SLC
endif

cd orig
set lista = `ls -d *.SAFE`
echo $lista[1]
echo $lista[2]
if ($#lista == 2) then
	echo "Both Sentinel images have been found succesfully in the orig directory"
	echo "Now it will start the preparation process:                            "
		foreach image ($lista)
		echo "The image read in this step is: "
                echo $image
                cd $image'/annotation'
                ln -s ../measurement/*.tiff .
                set word1=`echo $image | awk '{print substr($0,1,3)}'`
                echo $word1
                set word2=`echo $image | awk '{print substr($0,18,8)}'`
                echo $word2
                set nameGMT=$word1$word2
                echo $nameGMT
                set Polarization=`echo $image | awk '{print substr($0,13,4)}'`
                echo $Polarization
		if($Polarization == $tag4) then
		echo 'Using the 1SSV format'
			if($burts == $tag1) then
			set xmls = `ls -1 *001.xml`
			foreach xmlfile($xmls)
                        echo $xmlfile
                        set tiffs = `ls -1 *001.tiff`
                        foreach tifffile($tiffs)
                        echo $tifffile
                        make_slc_s1a $xmlfile $tifffile $nameGMT
                        mv S* ../../../raw
                        cd ../..
                        end
                        end
                        else if($burts == $tag2) then
                        cd ../../..
        		if(! -d raw001) then
        		mv raw raw001
        		mkdir raw
        		mv SLC SLC001
        		mkdir SLC
        		mv intf intf001
        		mkdir intf
        		endif
    			cd 'orig/'$image'/annotation'
    			set xmls = `ls -1 *002.xml`
    			foreach xmlfile($xmls)
    			echo $xmlfile
    			set tiffs = `ls -1 *002.tiff`
    			foreach tifffile($tiffs)
    			echo $tifffile
    			make_slc_s1a $xmlfile $tifffile $nameGMT
    			mv S* ../../../raw
    			cd ../..
    			end
    			end
    			else
			cd ../../..
       			if(! -d raw002) then
        		mv raw raw002
        		mkdir raw
        		mv SLC SLC002
        		mkdir SLC
        		mv intf intf002
    			mkdir intf
    			endif
    			cd 'orig/'$image'/annotation'
    			set xmls = `ls -1 *003.xml`
    			foreach xmlfile($xmls)
    			echo $xmlfile
    			set tiffs = `ls -1 *003.tiff`
    			foreach tifffile($tiffs)
    			echo $tifffile
    			make_slc_s1a $xmlfile $tifffile $nameGMT
    			mv S* ../../../raw
    			cd ../..
    			end
    			end
    			endif
		else if($Polarization == $tag5) then
		echo 'Using the 1SDV format'
			if($burts == $tag1) then
			set xmls = `ls -1 *004.xml`
			foreach xmlfile($xmls)
			echo $xmlfile
			set tiffs = `ls -1 *004.tiff`
			foreach tifffile($tiffs)
			echo $tifffile
			make_slc_s1a $xmlfile $tifffile $nameGMT
			mv S* ../../../raw
                        cd ../..
                        end
                        end
    			else if($burts == $tag2) then
    			cd ../../..
    			if(! -d raw001) then
    			mv raw raw001
    			mkdir raw
    			mv SLC SLC001
    			mkdir SLC
    			mv intf intf001
    			mkdir intf
    			endif
    			cd 'orig/'$image'/annotation'
    			set xmls = `ls -1 *005.xml`
    			foreach xmlfile($xmls)
    			echo $xmlfile
    			set tiffs = `ls -1 *005.tiff`
    			foreach tifffile($tiffs)
    			echo $tifffile
    			make_slc_s1a $xmlfile $tifffile $nameGMT
    			mv S* ../../../raw
    			cd ../..
    			end
    			end
    			else
    			cd ../../..
    			if(! -d raw002) then
    			mv raw raw002
    			mkdir raw
    			mv SLC SLC002
    			mkdir SLC
    			mv intf intf002
    			mkdir intf
    			endif
    			cd 'orig/'$image'/annotation'
    			set xmls = `ls -1 *006.xml`
    			foreach xmlfile($xmls)
    			echo $xmlfile
    			set tiffs = `ls -1 *006.tiff`
    			foreach tifffile($tiffs)
    			echo $tifffile
    			make_slc_s1a $xmlfile $tifffile $nameGMT
    			mv S* ../../../raw
    			cd ../..
    			end
    			end
    			endif
                    endif
        end
    else
    echo "The sentine 1 A/B data are missing, please put them inside the orig file"
    endif

    cd ..

	cd raw
	set lista2 = `ls -1 *.LED`
		foreach leader($lista2)
		extend_orbit $leader tmp 2. 
		mv tmp $leader
		end
	set wordi1=`echo $lista2[1] | awk '{print substr($0,1,11)}'`
	set wordi2=`echo $lista2[2] | awk '{print substr($0,1,11)}'`
	cd ..
	p2p_S1_SLC.csh $wordi1 $wordi2 'config.s1a.txt'


endif

exit
