#!/bin/bash
stn=$1
alpha=$2 #3
noise=$3

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

## moveout
moveout.sh $stn $alpha

### check the R & T pair
for file in decon.r.*.${str}.m0; do [[ ! -f decon.t${file#decon.r} ]] && \mv ${file}* trash_rf ; done
for file in decon.t.*.${str}.m0; do [[ ! -f decon.r${file#decon.t} ]] && \mv ${file}* trash_rf ; done

### binned by given baz interval
rf_preprocess -o=rRFbin.${str} -dBaz=5 -dT=0.05 decon.r.*.${str}.m0
rf_preprocess -o=tRFbin.${str} -dBaz=5 -dT=0.05 decon.t.*.${str}.m0
rf_preprocess -o=rRFbinM1.${str} -dBaz=5 -dT=0.05 decon.r.*.${str}.m1
rf_preprocess -o=rRFbinM2.${str} -dBaz=5 -dT=0.05 decon.r.*.${str}.m2

### Step 1: do the classical H-K analysis, set lamda=gama=fai=tau=0 
HKr-grdsearch -o=HK_${str}.grd -Hmimx=30,65 -Kmimx=1.60,1.90 -dH=0.1 -dK=0.01 -vpc=6.4 -vpm=$vpm -Lsol=0 -Gsol=0 -Fsol=0 -Tsol=0 -dBaz=5 -Ws="0.6 0.3 -0.1" decon.r.*.${str}
plot_HK_grid.sh $stn $alpha $noise

### Optional step: run JOF analysis, only get anisotropy
read vpc vsc moho kappa <<<`awk '(NR==1 && $1~ ">"){print $(NF-2), $(NF-2)/$NF, $(NF-1), $NF}' HK_${str}.grd`
FT-grdsearch -input=RFbin.${str} -moho=$moho -kappa=$kappa -vpm=$vpm -vpc=$vpc -dF=5 -dT=0.05 -Wd="-1.5 1.5" -Ws="0.7 0.2 -0.1" -o=FT_${str}.grd
plot_FT_grid.sh $stn $alpha $noise

### prepare the polarization angle epsilon, it is the same for all stations and only needs to run once, then put the output files in a common directory, say, ~/common .
get_epsilon_by_raysum.sh
\mv epsilon_by_raysum.lst ~/common/epsilon_by_raysum.lst
awk '{epsi[$1,$2]=$3}END{ for(lam=0; lam<360; lam+=10) { for(i in epsi) {split(i, ii, SUBSEP); baz=ii[2]+lam; if(baz<0) baz+=360; if(baz>=360) baz-=360; print lam, ii[1], baz, epsi[i]}}}' ~/common/epsilon_by_raysum.lst | sort -n -k1 -k2 -k3 > ~/common/epsilon_all_lams.lst

### Step 2: do the JTPCM analysis to get Moho dip and anisotropy
## keep epsilon for those BAZs with RF
for baz in `grep ">" rRFbin.${str} | awk '{print $(NF-1)}'`; do awk '($3=="'$baz'")' ~/common/epsilon_all_lams.lst; done | sort -n -k1 -k2 -k3 > epsilon_all_dips.lst
read vpc vsc moho kappa <<<`awk '(NR==1 && $1~ ">"){print $(NF-2), $(NF-2)/$NF, $(NF-1), $NF}' HK_${str}.grd`
LGFT-grdsearch -input=RFbin.${str} -fepsi=epsilon_all_dips.lst -moho=$moho -kappa=$kappa -vpm=$vpm -vpc=$vpc -Gmx=35 -dL=10 -dG=1 -dF=5 -dT=0.05 -Wd="-1.5 1.5" -Ws="0.7 0.2 -0.1" -o=LGFT_${str}.grd

### remove Moho dip and anisotropy from RFs
read Lsol Gsol Fsol Tsol <<<`awk '(NR==1 && $1~ ">"){print $(NF-3), $(NF-2), $(NF-1), $NF}' LGFT_${str}.grd`
awk '($1=='$Lsol' && $2=='$Gsol'){print $3, $4}'  epsilon_all_dips.lst | sort -n -k1 > epsilon.lst
LGFT-correctRF -input=RFbin.${str} -fepsi=epsilon.lst -moho=$moho -kappa=$kappa -vpm=$vpm -vpc=$vpc -L=$Lsol -G=$Gsol -F=$Fsol -T=$Tsol

### Step 3: revise the H and K with the solution of Moho dip and anisotropy from the 2nd step
HKr-grdsearch -o=HKr_${str}.grd -Hmimx=30,65 -Kmimx=1.60,1.90 -dH=0.1 -dK=0.01 -vpc=$vpc -vpm=$vpm -Lsol=$Lsol -Gsol=$Gsol -Fsol=$Fsol -Tsol=$Tsol -dBaz=5 -Ws="0.6 0.3 -0.1" decon.r.*.${str}
plot_LGFT_grid_v2.sh $stn $alpha $noise

rm -f epsilon.lst


