#! /bin/tcsh -fe
###########################################################################
###     COV_MATRIX: script to run LAT program sequence to create        ###
###                  one element of the covariance matrix               ###                                
###########################################################################
### version 1.1 - ms  2010114                                           ###
###                                                                     ###
###########################################################################


echo " "
echo "COV_MATRIX: covariance matrix derivation from elements of scattering matrix"

if ($#argv < 6)then
  echo " "
  echo "usage: COV_MATRIX <SLC_HH> <SLC_HV> <SLC_VV> <nlines> <COV> <elem>"
  echo "  "
  echo "       SLC_HH      (input) SLC HH-polarization (-: if not required)"
  echo "       SLC_HV      (input) SLC HV-polarization (-: if not required)"
  echo "       SLC_VV      (input) SLC VV-polarization (-: if not required)"
  echo "       nlines      (input) width of input SLC"
  echo "       COV         (output) Covariance matrix file name, e.g. date or orbit number"
  echo "       elem        (input) element of covariance matrix"
  echo "				1 : C11"
  echo "				2 : C12"
  echo "				3 : C13"
  echo "				4 : C21"
  echo "				5 : C22"
  echo "				6 : C23"
  echo "				7 : C31"
  echo "				8 : C32"
  echo "				9 : C33"
  echo " "
  exit
endif


set slc_hh = $1
set slc_hv = $2
set slc_vv = $3
set nlines = $4
set cov = $5
set elem = $6


echo " "
echo " "


switch ($elem)
	case 1:
		# covariance matrix C11 
		if ("$slc_hh" == "-") then
			echo "The dataset for the computation of C11 has not been defined correctly"
			echo "The HH component must be specified"
			echo " "
			exit
		else

			echo "Computing C11 - FLOAT format"
			echo "cpx_to_real $slc_hh $cov.c11 $nlines 2"
			nice cpx_to_real $slc_hh $cov.c11 $nlines 2
			echo " "
			echo "The file $cov.c11 has been generated - FLOAT format"
			echo " "

		endif
		breaksw
	case 2:
		# covariance matrix T12 
		if (("$slc_hh" == "-") || ("$slc_hv" == "-")) then
			echo "The dataset for the computation of C12 has not been defined correctly"
			echo "The HH and HV components must be specified"
			echo " "
			exit
		else
			echo "Computing C12 - FCOMPLEX format"
			echo "product_cpx $slc_hh $slc_hv tmp1 $nlines - - 1"
			nice product_cpx $slc_hh $slc_hv tmp1 $nlines - - 1
			echo "lin_comb_cpx 1 tmp1 0 0 1.414 0 $cov.c12 $nlines - - 1 1 1 " 
			nice lin_comb_cpx 1 tmp1 0 0 1.414 0 $cov.c12 $nlines - - 1 1 1 
			echo " "
			echo "The file $cov.c12 has been generated - FCOMPLEX format"
			echo " "

			/bin/rm	tmp*	
		endif
		breaksw
	case 3:
		# covariance matrix C13 
		if (("$slc_hh" == "-") || ("$slc_vv" == "-")) then
			echo "The dataset for the computation of T13 has not been defined covectly"
			echo "The HH and VV components must be specified"
			echo " "
			exit
		else
			echo "Computing C13 - FCOMPLEX format"
			echo "product_cpx $slc_hh $slc_vv $cov.c13 $nlines - - 1"
			nice product_cpx $slc_hh $slc_vv $cov.c13 $nlines - - 1
			echo " "
			echo "The file $cov.c13 has been generated - FCOMPLEX format"
			echo " "
		endif
		breaksw
	case 4:
		# covariance matrix C21 
		if (("$slc_hh" == "-") || ("$slc_hv" == "-")) then
			echo "The dataset for the computation of C21 has not been defined correctly"
			echo "The HH and HV components must be specified"
			echo " "
			exit
		else
			echo "Computing C21 - FCOMPLEX format"
			echo "product_cpx tmp2 tmp1 tmp3 $nlines - - 1"
			nice product_cpx $slc_hv $slc_hh tmp1 $nlines - - 1
			echo "lin_comb_cpx 1 tmp3 0 0 0.5 0 $cov.c21 $nlines - - 1 1 1" 
			nice lin_comb_cpx 1 tmp1 0 0 1.414 0 $cov.c21 $nlines - - 1 1 1  
			echo " "
			echo "The file $cov.c21 has been generated - FCOMPLEX format"
			echo " "

			/bin/rm	tmp*	
		endif
		breaksw
	case 5:
		# covariance matrix C22 
		if ("$slc_hv" == "-") then
			echo "The dataset for the computation of C22 has not been defined correctly"
			echo "The HV component must be specified"
			echo " "
			exit
		else
			echo "Computing C22 - FLOAT format"
			echo "cpx_to_real $slc_hv tmp1 $nlines 2"
			nice cpx_to_real $slc_hv tmp1 $nlines 2
			echo "lin_comb 1 tmp1 0 2 $cov.c22 $nlines - - 1 1 1"
			nice lin_comb 1 tmp1 0 2 $cov.c22 $nlines - - 1 1 1
			echo " "
			echo "The file $cov.c22 has been generated - FLOAT format"
			echo " "

			/bin/rm	tmp*	
		endif
		breaksw
	case 6:
		# covariance matrix C23 
		if (("$slc_hv" == "-") || ("$slc_vv" == "-")) then
			echo "The dataset for the computation of C23 has not been defined correctly"
			echo "The HV and VV components must be specified"
			echo " "
			exit
		else
			echo "Computing C23 - FCOMPLEX format"
			echo "product_cpx $slc_hv $slc_vv tmp1 $nlines - - 1"
			nice product_cpx $slc_hv $slc_vv tmp1 $nlines - - 1
			echo "lin_comb_cpx 1 tmp1 0 0 1.414 0 $cov.c23 $nlines - - 1 1 1 "
			nice lin_comb_cpx 1 tmp1 0 0 1.414 0 $cov.c23 $nlines - - 1 1 1 
			echo " "
			echo "The file $cov.c23 has been generated - FCOMPLEX format"
			echo " "

			/bin/rm	tmp*	
		endif
		breaksw
	case 7:
		# covariance matrix C31 
		if (("$slc_hh" == "-") || ("$slc_vv" == "-")) then
			echo "The dataset for the computation of T31 has not been defined correctly"
			echo "The HH and VV components must be defined"
			echo " "
			exit
		else		
			echo "Computing C31 - FCOMPLEX format"
			echo "product_cpx $slc_vv $slc_hh $cov.c31 $nlines - - 1"
			nice product_cpx $slc_vv $slc_hh $cov.c31 $nlines - - 1
			echo " "
			echo "The file $cov.c31 has been generated - FCOMPLEX format"
			echo " "

		endif
		breaksw
	case 8:
		# covariance matrix C32 
		if (("$slc_vv" == "-") || ("$slc_hv" == "-")) then
			echo "The dataset for the computation of C32 has not been defined correctly"
			echo "The VV and HV components must be specified"
			echo " "
			exit
		else
			echo "Computing T32 - FCOMPLEX format"
			echo "product_cpx $slc_vv $slc_hv tmp1 $nlines - - 1"
			nice product_cpx $slc_vv $slc_hv tmp1 $nlines - - 1
			echo "lin_comb_cpx 1 tmp1 0 0 1.414 0 $cov.c32 $nlines - - 1 1 1 "
			nice lin_comb_cpx 1 tmp1 0 0 1.414 0 $cov.c32 $nlines - - 1 1 1 	
			echo " "
			echo "The file $cov.c32 has been generated - FCOMPLEX format"
			echo " "

			/bin/rm	tmp*	
		endif
		breaksw
	case 9:
		# covariance matrix C33 
		if ("$slc_vv" == "-") then
			echo "The dataset for the computation of C33 has not been defined correctly"
			echo "The VV component must be specified"
			echo " "
			exit
		else
			echo "Computing C33 - FLOAT format"
			echo "cpx_to_real $slc_vv $cov.c33 $nlines 2"
			nice cpx_to_real $slc_vv $cov.c33 $nlines 2
			echo " "
			echo "The file $cov.c33 has been generated - FLOAT format"
			echo " "

		endif
		breaksw
endsw

echo End of cov_MATRIX



