#!/bin/bash
if [ $# -lt 3 ]; then
	echo "argv1: synthetic model (e.g., flat.mod)"
	echo "argv2: alpha (e.g., 1, 2 or 5)"
	echo "argv3: noise level (e.g., 0.1 (=10%))"
	exit
fi
fmodel=$1
alpha=$2
noise=$3

str=n${noise}${fmodel%.mod}.g${alpha}

bazmimx=(0 360 5)
BazSumBin=5
rayp=0.0576
vpc=`awk '($1!~/^#/)' $fmodel | awk '(NR==1){print $3*0.001}'`
vpm=`awk '($1!~/^#/)' $fmodel | awk 'END{print $3*0.001}'`

cat <<eof >raysum-params_bkup
# Multiples: 0 for none, 1 for Moho, 2 for all first-order
2
# Number of samples per trace
2000
# Sample rate (seconds)
0.05
# Gaussian pulse width (seconds)
1
# Alignment: 0 is none, 1 aligns on primary phase (P or S)
1
# Shift of traces -- t=0 at this time (sec)
5
# Rotation to output: 0 is NS/EW/Z, 1 is R/T/Z, 2 is P/SV/SH
1
eof

############### create synthetic RFs by RAYSUM and add random noise ############
awk '{if($1 !~ /^#/ && NR==8) $1=1/('$alpha'*0.5); print $0}' raysum-params_bkup >raysum-params
read dt npts tA tF <<< `awk '($1 !~ /^#/)' raysum-params | awk '{if(NR==2) npts=$1; else if (NR==3) dt=$1; else if (NR==6) t0=$1}END{print dt, npts, -t0, -t0+dt*npts}'`

rm -f decon.rt.*.${str}
n=0
for ((baz=${bazmimx[0]}; baz<${bazmimx[1]}; baz+=${bazmimx[2]})); do
	n=$[n+1]
	file=${n}.tr
	########### create synthetic RTZ waveform by RAYSUM
	echo $baz $rayp | awk '{print $1, $2*0.001, 0, 0}' > tmp.geom
	seis-spread $fmodel tmp.geom sample.ph sample.arr sample.tr P >/dev/null
	awk '($1 !~ /^#/){
		if(NF==5) {npts=$2; dt=$3; align=$4; t0=-$5; ipt=0}
		else if (NF==3) {time=t0+ipt*dt; ipt++; print time, $1, $2, $3, '$baz'}
	}' sample.tr > $file
	rm -f tmp.geom sample.ph sample.arr sample.tr
	########### add random noise
	Rmax=`cat $file | awk -v rmax=-100000 '($1>=0 && $1<9){rmax=($2>rmax)?$2:rmax} END {print rmax}'`
	read tAi tFi <<<`echo $n $tA $tF | awk '{srand($1); dt=rand(); print $2+$1*dt+5, $3+$1*dt+5}'`
	sac <<EOF >/dev/null
cut $tAi $tFi
r /ms_work/Pms/SAmerica/Noise_data/*[ENZ].sac
#r /home/mei/mywork/Pms/SAmerica/Noise_data/*[ENZ].sac
decimate 2
rtr; rmean; bp co 0.5 2 n 2 p 2
w r.sac t.sac z.sac
quit
EOF

	sac2xy r.sac > r.xy
	sac2xy t.sac > t.xy
	sac2xy z.sac | paste r.xy t.xy - |\
	awk -v rmax=-1000000 -v tmax=-1000000 -v zmax=-1000000 -v rmin=1000000 -v tmin=1000000 -v zmin=1000000  '{
		n++; r[n]=$2; t[n]=$4; z[n]=$6
		rmax=($2>rmax)?$2:rmax
		rmin=($2<rmin)?$2:rmin
		tmax=($4>tmax)?$4:tmax
		tmin=($4<tmin)?$4:tmin
		zmax=($6>tmax)?$6:tmax
		zmin=($6<tmin)?$6:tmin
	}END{for(i=1; i<=n; i++){print (r[i]-rmin)*2/(rmax-rmin)-1, (t[i]-tmin)*2/(tmax-tmin)-1, (z[i]-zmin)*2/(zmax-zmin)-1}}' > rtz.xy
	awk -v amp=$noise '{
		if(FNR==NR)
		{ n++; rnoise[n]=$1; tnoise[n]=$2; znoise[n]=$3 }
		else
		{ m++; time[m]=$1; r[m]=$2; t[m]=$3; z[m]=$4; baz=$5}
	}END{
		mn=(n>m)?m:n
		for(i=1;i<=mn;i++)
		{
			rnew = r[i] + rnoise[i] * '$Rmax'* amp
			tnew = t[i] + tnoise[i] * '$Rmax'* amp * 0.5
			znew = z[i] + znoise[i] * '$Rmax'* amp * 0.5
			print time[i], rnew, tnew, znew, baz
		}

	}' rtz.xy $file >${file}n${noise}${fmodel%.mod}
	awk '{print $1, $2}' ${file}n${noise}${fmodel%.mod} > ${file}.r ; xy2sac ${file}.r -delta=$dt -npts=$npts -baz=$baz -kstnm=n${noise}${fmodel%.mod} -o=${file}.r.sac
	awk '{print $1, $3}' ${file}n${noise}${fmodel%.mod} > ${file}.t ; xy2sac ${file}.t -delta=$dt -npts=$npts -baz=$baz -kstnm=n${noise}${fmodel%.mod} -o=${file}.t.sac
	awk '{print $1, $4}' ${file}n${noise}${fmodel%.mod} > ${file}.z ; xy2sac ${file}.z -delta=$dt -npts=$npts -baz=$baz -kstnm=n${noise}${fmodel%.mod} -o=${file}.z.sac
	rm -f [rtz].sac [rtz].xy ${file}.[rtz] rtz.xy
	saciterd -FN ${file}.r.sac -FD ${file}.z.sac -N 500 -D 5.0 -E 0.0001 -ALP $alpha -RAYP $rayp -S >/dev/null
	\mv decon.out decon.r.${baz}.${str}
	saciterd -FN ${file}.t.sac -FD ${file}.z.sac -N 500 -D 5.0 -E 0.0001 -ALP $alpha -RAYP $rayp -S >/dev/null
	\mv decon.out decon.t.${baz}.${str}
	rm -f decon.out observed numerator denominator predicted ${file}.[rtz].sac ${file}n${noise}${fmodel%.mod} ${file} 	
done

rm -f raysum-params raysum-params_bkup
