#!/bin/bash
if [ $# -lt 2 ]; then
	echo "argv1: stn"
	echo "argv2: alpha"
	exit
fi
stn=$1
alpha=$2

str=${stn}.g${alpha}
rayp=0.0576
vpm=8.1

fgrid=LGFT_${str}.grd
output=${fgrid%.grd}.ps

font="14p,31,black"
font_title="14p,29,black"
gmt set PS_MEDIA A4 PROJ_LENGTH_UNIT cm \
	FORMAT_GEO_MAP ddd \
        MEASURE_UNIT inch \
	MAP_FRAME_TYPE plain MAP_FRAME_PEN 0.8p,0 \
	MAP_TICK_LENGTH 0.1c MAP_TICK_PEN 0.8p \
	FONT_ANNOT $font MAP_ANNOT_OFFSET 0.06c \
	FONT_LABEL $font MAP_LABEL_OFFSET 0.12c \
	FONT_TITLE $font_title MAP_TITLE_OFFSET 0.1c \
	PS_CHAR_ENCODING Standard+

########################### get tPs
if [ -f HK${fgrid#LGFT} ]; then
	read vpc vsc Hsol Ksol <<< `awk '(NR==1){print $(NF-2), $(NF-2)/$NF, $(NF-1), $NF}' HK${fgrid#LGFT}`
	read tPs tPpPs tPpSs <<< `echo $rayp $Hsol $vsc $Ksol | awk '{p1=sqrt(1/($3*$3)-($1*$1)); p2=sqrt(1/(($4*$3)*($4*$3))-($1*$1)); printf("%.2f\t%.2f\t%.2f\n", $2*(p1-p2), $2*(p1+p2), 2*$2*p1)}'`
else
	echo "no HK grid available"
	exit
fi
########################### get corrected tPs
if [ -f HKr${fgrid#LGFT} ]; then
	read vpc vsc Hrsol Krsol <<< `awk '(NR==1){print $(NF-2), $(NF-2)/$NF, $(NF-1), $NF}' HKr${fgrid#LGFT}`
	read tPsr tPpPsr tPpSsr <<< `echo $rayp $Hrsol $vsc $Krsol | awk '{p1=sqrt(1/($3*$3)-($1*$1)); p2=sqrt(1/(($4*$3)*($4*$3))-($1*$1)); printf("%.2f\t%.2f\t%.2f\n", $2*(p1-p2), $2*(p1+p2), 2*$2*p1)}'`
else
	echo "no HK grid available"
	exit
fi
echo $tPs $tPpPs $tPpSs $tPsr $tPpPsr $tPpSsr

if [ -f $fgrid  ]; then
	read Gmx dL dG dF dT Lsol Gsol Fsol Tsol <<< `awk '(NR==1){print $3, $4, $5, $6, $7, $8, $9, $10, $11}' $fgrid`
	awk -v pai=3.141592653 -v rayp=$rayp -v vpc=$vpc -v vsc=$vsc -v vpm=$vpm -v lamda=$Lsol -v gama=$Gsol -v az=$Fsol -v tau=$Tsol 'BEGIN{
		lam=lamda*pai/180
		stk = lam - 0.5*pai
		gam=gama*pai/180
		for(baz=0;baz<=360; baz+=5) 
		{
			bazi=baz*pai/180
			fai = (az - baz)*pai/180;

			# calculate tPs for dipping interface, according to TanPing, ChenYun et al., 2018. CJG
			PI1=-rayp*cos(bazi);
			PI2=-rayp*sin(bazi);
			PI3=-sqrt(1/(vpm^2)-(rayp^2));          
			P1I1=sin(lam)*PI1-cos(lam)*PI2;
			P1I2=cos(gam)*cos(lam)*PI1+cos(gam)*sin(lam)*PI2+sin(gam)*PI3;
			# P1I3=-sin(gam)*cos(lam)*PI1-sin(gam)*sin(lam)*PI2+cos(gam)*PI3;           
			P2I1=P1I1;
			P2I2=P1I2;
			P2I3=-sqrt(1/(vpc^2)-P1I1^2-P1I2^2);              
			n1=sin(gam)*cos(lam);
			n2=sin(gam)*sin(lam);
			n3=-cos(gam);
			nPI=n1*PI1+n2*PI2+n3*PI3;
			P3I1=sin(lam)*P2I1+cos(gam)*cos(lam)*P2I2-sin(gam)*cos(lam)*P2I3;
			P3I2=-cos(lam)*P2I1+cos(gam)*sin(lam)*P2I2-sin(gam)*sin(lam)*P2I3;
			# P3I3=sin(gam)*P2I2+cos(gam)*P2I3;
			PR1=P3I1;
			PR2=P3I2;
			PR3=sqrt(1/(vpc^2)-P3I1^2-P3I2^2);
			nPR=n1*PR1+n2*PR2+n3*PR3;
			SR1=P3I1;
			SR2=P3I2;
			SR3=sqrt(1/(vsc^2)-P3I1^2-P3I2^2);
			nSR=n1*SR1+n2*SR2+n3*SR3;
			tPsDip = '$Hrsol' * cos(gam) * (sqrt((1/vsc)^2-1/(vpm^2)+nPI*nPI)-sqrt(1/(vpc^2)-1/(vpm^2)+nPI*nPI));
			tPpPsDip = '$Hrsol' * cos(gam) * (sqrt(nPR*nPR) + sqrt(1/(vsc^2)-1/(vpc^2)+nPR*nPR));
			tPpSsDip = 2 * '$Hrsol' * cos(gam) * sqrt(nSR*nSR);
			print baz, tPsDip - 0.5*tau*cos(gam)*cos(2*fai), tPpPsDip - 0.5*tau*cos(gam)*cos(2*fai), tPpSsDip - tau*cos(gam)*cos(2*fai);
		}
	}' > baz_tPsDip_tPpPsDip_tPpSsDip.lst
else
	echo "no LGFT grid available"
	exit
fi

########################### 
read stn stlo stla stel <<< `saclst kstnm stlo stla stel f decon.r.*.${str} | awk '(NR==1){print $2, $3, $4, $5}'`
gmt makecpt -C255,230,220,200,180,160,140,120,100 -T0,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1 -D  > plot.cpt
################ Ps fitting results
##### R rf
y0=0
y1=360
read x0 x1 <<< `echo $tPsr | awk '{print int($1-2), int($1+2)}'`
depmax=`awk '($1>'$x0' && $1<'$x1'){printf("%e\n", $2)}' rRFbin.${str} | gmt info -C | awk '{printf("%e", $2*7)}'`
gmt psbasemap -R$x0/$x1/${y0}/${y1} -X0.9 -Y6.5 -JX1/3 -BSWen+t"obs. R-Ps" -Bxa2f1+l"Time (s)" -Bya60f30+l"BAZ (@~\260@~)" -P -K >$output
awk '{
	if($1==">") {print $0; baz=$(NF-1)}
	else
		print $1, baz, $2/'$depmax'
}' rRFbin.${str} | gmt pswiggle -R -J -Gtomato1+p -G150+n -Z1 -W0.2p,125 -O -K >>$output
echo $tPs | awk '{print $1, 0 "\n" $1, 360}' | gmt psxy -R -J -W0.7p,0,- -O -K >> $output
awk '{print $2, $1}' baz_tPsDip_tPpPsDip_tPpSsDip.lst | gmt psxy -R -J -W0.7p,blue -O -K >> $output
# echo $x1 $y0 | awk '{print $1-0.1, $2+5, "10,1,black RB", "(a)"}' | gmt pstext -F+f+j -D0c/0c -N -R -J -O -K >> $output

##### T rf
# depmax1=`awk '($1>'$x0' && $1<'$x1'){printf("%e\n", $2)}' tRFbin.${str} | gmt info -C | awk '{printf("%e", $2*5)}'`
gmt psbasemap -R$x0/$x1/${y0}/${y1} -X1.15 -JX1/3 -BSwen+t"obs. T-Ps" -Bxa2f1+l"Time (s)" -Bya60f30+l"BAZ (@~\260@~)" -O -K >>$output
awk '{
	if($1==">") {print $0; baz=$(NF-1)}
	else
		print $1, baz, $2/'$depmax'
}' tRFbin.${str} | gmt pswiggle -R -J -Gtomato1+p -G150+n -Z1 -W0.2p,125 -O -K >>$output
echo $tPs | awk '{print $1, 0 "\n" $1, 360}' | gmt psxy -R -J -W0.7p,0,- -O -K >> $output
# echo $x1 $y0 | awk '{print $1-0.1, $2+5, "10,1,black RB", "(b)"}' | gmt pstext -F+f+j -D0c/0c -N -R -J -O -K >> $output

################  R rf corrected
gmt psbasemap -R$x0/$x1/${y0}/${y1} -X1.15 -JX1/3 -BSwen+t"corr. R-Ps" -Bxa2f1+l"Time (s)" -Bya30+l"BAZ (@~\260@~)" -O -K >>$output
awk '{
	if($1==">") {print $0; baz=$(NF-1)}
	else
	{print $1, baz, $2/'$depmax'}
}' ADCorrectedRFbin.${str}  | gmt pswiggle -R -J -Gtomato1+p -G150+n -Z1 -W0.2p,125 -O -K >>$output
echo $tPs | awk '{print $1, 0 "\n" $1, 360}' | gmt psxy -R -J -W0.7p,0,- -O -K >> $output
# echo $tPsr | awk '{print $1, 0 "\n" $1, 360}' | gmt psxy -R -J -W0.7p,blue -O -K >> $output
# echo $x1 $y0 | awk '{print $1-0.1, $2+5, "10,1,black RB", "(c)"}' | gmt pstext -F+f+j -D0c/0c -N -R -J -O -K >> $output

################ T rf corrected
# depmax=`awk '($1>'$x0' && $1<'$x1'){printf("%e\n", $2)}' $fin  | gmt info -C | awk '{printf("%e", $2*4)}'`
gmt psbasemap -R$x0/$x1/${y0}/${y1} -X1.15 -JX1/3 -BSwen+t"corr. T-Ps" -Bxa2f1+l"Time (s)" -Bya30+l"BAZ (@~\260@~)"  -O -K >>$output
awk '{
	if($1==">") {print $0; baz=$(NF-1)}
	else
	{print $1, baz, $3/'$depmax'}
}' ADCorrectedRFbin.${str} | gmt pswiggle -R -J -Gtomato1+p -G150+n -Z1 -W0.2p,125 -O -K >>$output
echo $tPs | awk '{print $1, 0 "\n" $1, 360}' | gmt psxy -R -J -W0.7p,0,- -O -K >> $output
# echo $x1 $y0 | awk '{print $1-0.1, $2+5, "10,1,black RB", "(d)"}' | gmt pstext -F+f+j -D0c/0c -N -R -J -O -K >> $output


##### R M1 
read x0 x1 <<< `echo $tPpPsr | awk '{print int($1-2), int($1+2)}'`
# depmax=`awk '($1>'$x0' && $1<'$x1'){printf("%e\n", $2)}' rRFbin.${str} | gmt info -C | awk '{printf("%e", $2*7)}'`
gmt psbasemap -R$x0/$x1/${y0}/${y1} -X1.15 -JX1/3 -BSwen+t"obs. R-M1" -Bxa2f1+l"Time (s)" -Bya60f30+l"BAZ (@~\260@~)" -O -K >>$output
awk '{
	if($1==">") {print $0; baz=$(NF-1)}
	else
		print $1, baz, $2/'$depmax'
}' rRFbinM1.${str} | gmt pswiggle -R -J -Gtomato1+p -G150+n -Z1 -W0.2p,125 -O -K >>$output
echo $tPpPs | awk '{print $1, 0 "\n" $1, 360}' | gmt psxy -R -J -W0.7p,0,- -O -K >> $output
# echo $tPpPsr | awk '{print $1, 0 "\n" $1, 360}' | gmt psxy -R -J -W0.7p,blue -O -K >> $output
awk '{print $3, $1}' baz_tPsDip_tPpPsDip_tPpSsDip.lst | gmt psxy -R -J -W0.7p,blue -O -K >> $output
# echo $x1 $y0 | awk '{print $1-0.1, $2+5, "10,1,black RB", "(e)"}' | gmt pstext -F+f+j -D0c/0c -N -R -J -O -K >> $output

##### R M2
read x0 x1 <<< `echo $tPpSsr | awk '{print int($1-2), int($1+2)}'`
gmt psbasemap -R$x0/$x1/${y0}/${y1} -X1.2 -JX1/3 -BSwen+t"obs. R-M2" -Bxa2f1+l"Time (s)" -Bya60f30+l"BAZ (@~\260@~)" -O -K >>$output
awk '{
	if($1==">") {print $0; baz=$(NF-1)}
	else
		print $1, baz, $2/'$depmax'
}' rRFbinM2.${str} | gmt pswiggle -R -J -Gtomato1+p -G150+n -Z1 -W0.2p,125 -O -K >>$output
echo $tPpSs | awk '{print $1, 0 "\n" $1, 360}' | gmt psxy -R -J -W0.7p,0,- -O -K >> $output
# echo $tPpSsr | awk '{print $1, 0 "\n" $1, 360}' | gmt psxy -R -J -W0.7p,blue -O -K >> $output
awk '{print $4, $1}' baz_tPsDip_tPpPsDip_tPpSsDip.lst | gmt psxy -R -J -W0.7p,blue -O -K >> $output
# echo $x1 $y0 | awk '{print $1-0.1, $2+5, "10,1,black RB", "(f)"}' | gmt pstext -F+f+j -D0c/0c -N -R -J -O -K >> $output



##### H-K grid
read xmin xmax dx ymin ymax dy moho kappa dxi dyi <<< `awk '(NR==1){print $3, $4, $5, $6, $7, $8, $10, $11, $5*0.1, $8*0.1}' HK_${str}.grd`
read sigmaH sigmaK <<< `HK-sigma.sh -grid=HK_${str}.grd`
awk '(NR>1){print $1, $2, $NF}' HK_${str}.grd | gmt xyz2grd -R$xmin/$xmax/$ymin/$ymax -I$dx/$dy -Gplot.grd
gmt grdsample plot.grd -I$dxi/$dyi -Gplot_new.grd; mv -f plot_new.grd plot.grd
gmt grdimage  -R$xmin/$xmax/$ymin/$ymax -JX1.4/1.4 -X-5.8 -Y-3.3 -BWSne+t"H-@~\153@~" -Bx10+l"H (km)" -Bya0.1+l"@~\153@~" -Cplot.cpt plot.grd -O -K >> ${output}
echo $moho $kappa $sigmaH $sigmaK | gmt psxy -Exy+p1p,white -Sc0.05 -W1p,white -Gwhite -R -J -O -K >> $output
# echo $xmax $ymin | awk '{print $1-1, $2+0.01, "10,1,black RB", "(g)" }' | gmt pstext -F+f+j -D0c/0c -N -R -J -O -K  >> $output

##### L/G grid
read Lmean sigmaL Gmean sigmaG Fmean sigmaF Tmean sigmaT <<< `LGFT-sigma.sh $stn $alpha`
read sigmaL sigmaG sigmaF sigmaT <<< `awk '{print $3, $6, $9, $12}' LGFT_${str}.sigma`
read xmin xmax dx ymin ymax dy lamda gama dxi dyi <<< `awk '(NR==1){print 0, 360, $4*2, 0, $3, $5*2, $(NF-3), $(NF-2), $4*0.1, $5*0.1}' LGFT_${str}.grd`
read mi mx <<< `awk '(NR>1 && $3=='$Fsol' && $4=='$Tsol'){print $NF}' LGFT_${str}.grd | gmt info -C`
awk '{if(NR==1) print "> Lmi,Lmx,dL,Gmi,Gmx,dG,Vp,Lsol,Gsol= '$xmin' '$xmax' '$dx' '$ymin' '$ymax' '$dy' 6.4 '$Lsol' '$Gsol'"; else if ($3=='$Fsol' && $4=='$Tsol') {print $1, $2, ($NF-('$mi'))/('$mx'-('$mi')); if($1==0) print 360, $2, ($NF-('$mi'))/('$mx'-('$mi'))}}' LGFT_${str}.grd >LGgrid.tmp
awk '(NR>1){print $1, $2, $NF}' LGgrid.tmp | gmt xyz2grd -R$xmin/$xmax/$ymin/$ymax -I$dx/$dy -Gplot.grd
gmt grdsample plot.grd -I$dxi/$dyi -Gplot_new.grd; mv -f plot_new.grd plot.grd
gmt grdimage  -R$xmin/$xmax/$ymin/$ymax -JPa3.2c -BwsNe+t"Moho dip" -Bxa45g45+l"@~\152@~ (@~\260@~)" -Bya10f10g10+l"@~\147@~ (@~\260@~" -Cplot.cpt plot.grd -X1.7 -O -K >> ${output}
echo 0 10 | awk '{print $1, $2, "9,1,black TC", $2"@~\260@~"}' | gmt pstext -F+f+j -D0c/-0.02 -N -R -J -O -K  >> $output
echo 0 20 | awk '{print $1, $2, "9,1,black TC", $2"@~\260@~"}' | gmt pstext -F+f+j -D0c/-0.02 -N -R -J -O -K  >> $output
echo 0 30 | awk '{print $1, $2, "9,1,black TC", $2"@~\260@~"}' | gmt pstext -F+f+j -D0c/-0.02 -N -R -J -O -K  >> $output
# gmt grdimage  -R$xmin/$xmax/$ymin/$ymax -JX1.5/1.5 -X2 -BWSne -Bxa90f45+l"@~\154@~ (@~\260@~)" -Bya10f5+l"@~\147@~ (@~\260@~)" -Cplot.cpt plot.grd -O -K >> ${output}
# echo $Lsol $Gsol $sigmaL $sigmaG | gmt psxy -Exy+p1p,white -Sc0.05 -W1p,white -Gwhite -R -J -O -K >> $output
echo $Lsol $Gsol | gmt psxy -R -J -S+0.1 -W1.0p,white -O -K >> ${output}
# echo 165 $ymax | awk '{print $1, $2-1, "10,1,black RB", "(h)" }' | gmt pstext -F+f+j -D0c/0c -N -R -J -O -K  >> $output

##### F/T grid
read xmin xmax dx ymin ymax dy fai tau dxi dyi <<< `awk '(NR==1){print 0, 360, $6*2, 0, 1, $7*2, $(NF-1), $NF, $6*0.1, $7*0.1}' LGFT_${str}.grd`
read mi mx <<< `awk '(NR>1 && $1=='$Lsol' && $2=='$Gsol'){print $NF}' LGFT_${str}.grd | gmt info -C`
awk '{if(NR==1) print "> Fmi,Fmx,dF,Tmi,Tmx,dT,Vp,Fsol,Tsol= '$xmin' '$xmax' '$dx' '$ymin' '$ymax' '$dy' 6.4 '$Fsol' '$Tsol'"; else if ($1=='$Lsol' && $2=='$Gsol') {print $3, $4, ($NF-('$mi'))/('$mx'-('$mi')); if($3==0) print 180, $4, ($NF-('$mi'))/('$mx'-('$mi'))}}' LGFT_${str}.grd >FTgrid.tmp
awk '(NR>1){print $1, $2, $NF; print $1+180, $2, $NF}' FTgrid.tmp | gmt xyz2grd -R$xmin/$xmax/$ymin/$ymax -I$dx/$dy -rg -Gplot.grd 
gmt grdsample plot.grd -I$dxi/$dyi -Gplot_new.grd; mv -f plot_new.grd plot.grd
gmt grdimage  -R$xmin/$xmax/$ymin/$ymax -JPa3.2c -BwsNe+t"Anisotropy" -Bxa45g45+l"@~\152@~ (@~\260@~)" -Bya0.5f0.5g0.5+l"@~\164@~ (s)" -Cplot.cpt plot.grd -X1.7 -O -K >> ${output}
echo 0 0.5 | awk '{print $1, $2, "9,1,black TC", $2"s"}' | gmt pstext -F+f+j -D0c/-0.02 -N -R -J -O -K  >> $output
echo 0 1.0 | awk '{print $1, $2, "9,1,black TC", $2"s"}' | gmt pstext -F+f+j -D0c/-0.02 -N -R -J -O -K  >> $output

# echo $Fsol $Tsol $sigmaF $sigmaT | gmt psxy -Exy+p1p,white -Sc0.05 -W1p,white -Gwhite -R -J -O -K >> $output
echo $Fsol $Tsol | gmt psxy -R -J -S+0.1 -W1.0p,white -O -K >> ${output}
# echo 165 $ymax | awk '{print $1, $2-0.05, "10,1,black RB", "(i)" }' | gmt pstext -F+f+j -D0c/0c -N -R -J -O -K  >> $output

##### H-K-r grid
read xmin xmax dx ymin ymax dy mohor kappar dxi dyi <<< `awk '(NR==1){print $3, $4, $5, $6, $7, $8, $10, $11, $5*0.1, $8*0.1}' HKr_${str}.grd`
read sigmaHr sigmaKr <<< `HK-sigma.sh -grid=HKr_${str}.grd`
awk '(NR>1){print $1, $2, $NF}' HKr_${str}.grd | gmt xyz2grd -R$xmin/$xmax/$ymin/$ymax -I$dx/$dy -Gplot.grd
gmt grdsample plot.grd -I$dxi/$dyi -Gplot_new.grd; mv -f plot_new.grd plot.grd
gmt grdimage  -R$xmin/$xmax/$ymin/$ymax -JX1.4/1.4 -X2 -BWSne+t"revised H-@~\153@~" -Bx10+l"H@-R@- (km)" -Bya0.1+l"@~\153@~@-R@-" -Cplot.cpt plot.grd -O -K >> ${output}
echo $mohor $kappar $sigmaHr $sigmaKr | gmt psxy -Exy+p1p,white -Sc0.05 -W1p,white -Gwhite -R -J -O -K >> $output
# echo $xmax $ymin | awk '{print $1-1, $2+0.01, "10,1,black RB", "(j)" }' | gmt pstext -F+f+j -D0c/0c -N -R -J -O -K  >> $output

gmt psscale -Cplot.cpt -Bxa0.5f0.25 -O -K -Dx-2.1/2.65/2/.15h >> ${output}

cat<<eof>labels.tmp
0.3 0.345 12,1,black TC H = $moho @~\261@~ $sigmaH
0.3 0.32 12,1,black TC @~\153@~ = $kappa @~\261@~ $sigmaK
0.53 0.345 12,1,black TC @~\154@~ = $lamda @~\261@~ $sigmaL
0.53 0.32 12,1,black TC @~\147@~ = $gama @~\261@~ $sigmaG
0.75 0.345 12,1,black TC @~\152@~ = $fai @~\261@~ $sigmaF
0.75 0.32 12,1,black TC @~\164@~ = $tau @~\261@~ $sigmaT
1.02 0.345 12,1,black TC H@-R@- = $mohor @~\261@~ $sigmaHr
1.02 0.32 12,1,black TC @~\153@~@-R@- = $kappar @~\261@~ $sigmaKr
0.86 0.415 12,1,black TC Obj. Norm
0.35 0.415 12,1,black TC STN: ${stn}
eof

gmt pstext labels.tmp -F+f+j -D0c/0 -N -R0/1/0/1 -JX19c/21c -X-7 -Y-0.8 -O -K  >> $output

echo "# H dH K dK Lam dL Gam dG Fai dF Tau dT Hr dHr Kr dKr stn stlo stla stel" > ${str}.sol
echo $Hsol $sigmaH $Ksol $sigmaK $Lsol $sigmaL $Gsol $sigmaG $Fsol $sigmaF $Tsol $sigmaT $mohor $sigmaHr $kappar $sigmaKr $stn $stlo $stla $stel >> ${str}.sol


ps2pdf $output
rm -f tmp.xyz $output  gmt.* LGgrid.tmp FTgrid.tmp plot.cpt plot.grd baz_tPsDip.lst labels.tmp

spdf.sh ${output%.ps}.pdf

