# Smoothing the optimum initial parameters of GBs
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Karel Zacek (zacek@karel.troja.mff.cuni.cz)
# see: Optimization of the Shape of Gaussian Beams,
#      In: Seismic Waves in Complex 3-D Structures, Report 11
#      (Department of Geophysics, Charles University, Prague 2001)
#      pp. 181-201

# Input files required
 #chk.pl: "data/mar/" "gb-crt.dat"
 #chk.pl: "data/mar/" "gb-m0.dat"
 #chk.pl: "data/mar/" "mar-crt.dat"
 #chk.pl: "data/mar/" "mar-mod.dat"
 #chk.pl: "data/mar/" "mar-srp.dat"
 #chk.pl: "data/mar/" "gb-opt.h"
 #chk.pl: "data/mar/" "gb-test.h"
 #chk.pl: "data/mar/" "gb-yd.cal"
 #chk.pl: "crt/"      "codep.dat"
 #chk.pl: "crt/"      "writall.dat"
 #chk.pl: "model/"    "sob11.dat"
 #chk.pl: "forms/"    "echo.pl"
 #chk.pl: "forms/"    "append.pl"
 #chk.pl: "forms/"    "addsob.cal"
 #chk.pl: "forms/"    "div.cal"
 #chk.pl: "forms/"    "inv.cal"
 #chk.pl: "forms/"    "mul.cal"
 #chk.pl: "forms/"    "sqrt.cal"


# Minimizing the mean squared width of Gaussian beams
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Creating blank files to accumulate gridded values for all sources
  echo.pl: "" ">y0a.out"                                                      #
  echo.pl: "" ">r0a.out"                                                      #
  echo.pl: "" ">sigmaa.out"                                                   #

# Ray tracing for individual source positions
# SRP='mar-srp.dat'  CPAR1=1
# srp:
# go.pl: "gb-opt.h"
# append.pl: "y0a.out"    "y0.out"
# append.pl: "r0a.out"    "r0.out"
# append.pl: "sigmaa.out" "sigma.out"
# .
# .
# .
  SRP='mar-srp.dat'  CPAR1=220
  srp:                                                                        #
  go.pl: "gb-opt.h"                                                           #
  append.pl: "y0a.out"    "y0.out"                                            #
  append.pl: "r0a.out"    "r0.out"                                            #
  append.pl: "sigmaa.out" "sigma.out"                                         #
# .
# .
# .
# SRP='mar-srp.dat'  CPAR1=335
# srp:
# go.pl: "gb-opt.h"
# append.pl: "y0a.out"    "y0.out"
# append.pl: "r0a.out"    "r0.out"
# append.pl: "sigmaa.out" "sigma.out"

# Smoothing the parameter R - 1st iteration
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Initial and updated models
  MODEL='gb-m0.dat'  MODOUT='r0m.out'  NEGPAR=1

# Calculating matrices for inversion
  N1=26  N2=181  N3=1  D1=1  D2=1  D3=1  O1=1  O2=1  O3=1
  M1='m1.out'  M2='m2.out'  MODL2=' '  MODSOB='modsob.out'
  SOBOLEV='sob11.dat'  SOBW01=1  # minimizing first derivatives
  invsoft:                                                                    #
# Unit given standard deviation: ERRMUL=SQRT(N1*N2*N3)
  GRD='r0a.out'  INDFUN=1  MPAR=1  POWERM=1  ERRMUL=68.6
  GM1='gm1.out'  GM2='gm2.out'  GM3=' '  DM1='dm1.out'
  GRDERR='sigmaa.out'
  invpts:                                                                     #
  GRD=' '

# Matrix operations
  N1=0  N2=1  N3=1  M1='m2.out'
  CAL='inv.cal' GRD1='dm1.out' GRD2='dm2.out'
  grdcal:                                                                     #
  M1='m1.out'  M2='m2.out'  GM1='gm1.out'  DM1='dm2.out'  SM1='sm1.out'
  gmdmgmt:                                                                    #
  N1=0  N2=0  N3=1  M1='m1.out'  M2='m1.out'  SOBMUL=1E11 # parameter s_R
  CAL='addsob.cal' GRD1='sm1.out' GRD2='modsob.out' GRD3='sm2.out'
  grdcal:                                                                     #
  M1='m1.out'               SM1='sm2.out'  SM2='sm3.out'
  sminv:                                                                      #
  M1='m2.out'  M2=' '       DM1='dm2.out'  GM1='gm2.out'  GM2='gm3.out'
  dmgm:                                                                       #
  M1='m1.out'  M2='m2.out'  GM1='gm1.out'  GM2='gm3.out'  GM3='gm4.out'
  gmgm:                                                                       #
  M1='m1.out'  M2=' '       SM1='sm3.out'  GM1='gm4.out'  GM2='gm5.out'
  smgm:                                                                       #

# Updating the model
  M1='m1.out'  MODNEW='gm5.out'
  modmod:                                                                     #
#
# Calculating gridded values of optimized GB parameter R
  N1=26  N2=181  N3=1  D1=1  D2=1  D3=1  O1=1  O2=1  O3=1
  MODEL='r0m.out'
  MPAR=1  ICB=' '  VEL='fgbr22.out'
  grid:                                                                       #

# Smoothing the parameter Y - 1st iteration
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Data to be fit
  CAL='gb-yd.cal' GRD1='y0a.out' GRD2='fgbr22.out' GRD3='r0a.out'
  GRD4='yd.out'
  grdcal:                                                                     #

# Initial and updated models
  MODEL='gb-m0.dat'  MODOUT='y0m.out'  NEGPAR=1

# Calculating matrices for inversion
  N1=26  N2=181  N3=1  D1=1  D2=1  D3=1  O1=1  O2=1  O3=1
  M1='m1.out'  M2='m2.out'  MODL2=' '  MODSOB='modsob.out'
  SOBOLEV='sob11.dat'  SOBW01=1  # minimizing first derivatives
  invsoft:                                                                    #
# Unit given standard deviation: ERRMUL=SQRT(N1*N2*N3)
  GRD='yd.out'  INDFUN=1  MPAR=1  POWERM=1  ERRMUL=68.6
  GM1='gm1.out'  GM2='gm2.out'  GM3=' '  DM1='dm1.out'
  GRDERR='sigmaa.out'
  invpts:                                                                     #
  GRD=' '

# Matrix operations
  N1=0  N2=1  N3=1  M1='m2.out'
  CAL='inv.cal' GRD1='dm1.out' GRD2='dm2.out'
  grdcal:                                                                     #
  M1='m1.out'  M2='m2.out'  GM1='gm1.out'  DM1='dm2.out'  SM1='sm1.out'
  gmdmgmt:                                                                    #
  N1=0  N2=0  N3=1  M1='m1.out'  M2='m1.out'  SOBMUL=1E11 # parameter s_Y
  CAL='addsob.cal' GRD1='sm1.out' GRD2='modsob.out' GRD3='sm2.out'
  grdcal:                                                                     #
  M1='m1.out'               SM1='sm2.out'  SM2='sm3.out'
  sminv:                                                                      #
  M1='m2.out'  M2=' '       DM1='dm2.out'  GM1='gm2.out'  GM2='gm3.out'
  dmgm:                                                                       #
  M1='m1.out'  M2='m2.out'  GM1='gm1.out'  GM2='gm3.out'  GM3='gm4.out'
  gmgm:                                                                       #
  M1='m1.out'  M2=' '       SM1='sm3.out'  GM1='gm4.out'  GM2='gm5.out'
  smgm:                                                                       #

# Updating the model
  M1='m1.out'  MODNEW='gm5.out'
  modmod:                                                                     #

# Calculating gridded values of optimized GB parameter Y
  N1=26  N2=181  N3=1  D1=1  D2=1  D3=1  O1=1  O2=1  O3=1
  MODEL='y0m.out'
  MPAR=1  ICB=' '  VEL='fgby22.out'
  grid:                                                                       #

# Smoothing the parameter R - 2nd iteration
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Standard deviation for data fitting
  CAL='mul.cal' GRD1='sigmaa.out' GRD2='sigmaa.out' GRD3='c22.out'
  grdcal:                                                                     #
  CAL='div.cal' GRD1='y0a.out' GRD2='c22.out' GRD3='c22.out'
  grdcal:                                                                     #
  CAL='div.cal' GRD1='fgby22.out' GRD2='c22.out' GRD3='sigmab.out'
  grdcal:                                                                     #
  CAL='sqrt.cal' GRD1='sigmab.out' GRD2='sigmab.out'
  grdcal:                                                                     #

# Initial and updated models
  MODEL='gb-m0.dat'  MODOUT='r0m.out'  NEGPAR=1

# Calculating matrices for inversion
  N1=26  N2=181  N3=1  D1=1  D2=1  D3=1  O1=1  O2=1  O3=1
  M1='m1.out'  M2='m2.out'  MODL2=' '  MODSOB='modsob.out'
  SOBOLEV='sob11.dat'  SOBW01=1  # minimizing first derivatives
  invsoft:                                                                    #
# Unit given standard deviation: ERRMUL=SQRT(N1*N2*N3)
  GRD='r0a.out'  INDFUN=1  MPAR=1  POWERM=1  ERRMUL=68.6
  GM1='gm1.out'  GM2='gm2.out'  GM3=' '  DM1='dm1.out'
  GRDERR='sigmab.out'
  invpts:                                                                     #
  GRD=' '

# Matrix operations
  N1=0  N2=1  N3=1  M1='m2.out'
  CAL='inv.cal' GRD1='dm1.out' GRD2='dm2.out'
  grdcal:                                                                     #
  M1='m1.out'  M2='m2.out'  GM1='gm1.out'  DM1='dm2.out'  SM1='sm1.out'
  gmdmgmt:                                                                    #
  N1=0  N2=0  N3=1  M1='m1.out'  M2='m1.out'  SOBMUL=1E11 # parameter s_R
  CAL='addsob.cal' GRD1='sm1.out' GRD2='modsob.out' GRD3='sm2.out'
  grdcal:                                                                     #
  M1='m1.out'               SM1='sm2.out'  SM2='sm3.out'
  sminv:                                                                      #
  M1='m2.out'  M2=' '       DM1='dm2.out'  GM1='gm2.out'  GM2='gm3.out'
  dmgm:                                                                       #
  M1='m1.out'  M2='m2.out'  GM1='gm1.out'  GM2='gm3.out'  GM3='gm4.out'
  gmgm:                                                                       #
  M1='m1.out'  M2=' '       SM1='sm3.out'  GM1='gm4.out'  GM2='gm5.out'
  smgm:                                                                       #

# Updating the model
  M1='m1.out'  MODNEW='gm5.out'
  modmod:                                                                     #

# Calculating gridded values of optimized GB parameter R
  N1=26  N2=181  N3=1  D1=1  D2=1  D3=1  O1=1  O2=1  O3=1
  MODEL='r0m.out'
  MPAR=1  ICB=' '  VEL='fgbr22.out'
  grid:                                                                       #

# Smoothing the parameter Y - 2nd iteration
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Data to be fit
  CAL='gb-yd.cal'
  GRD1='y0a.out' GRD2='fgbr22.out' GRD3='r0a.out'  GRD4='yd.out'
  grdcal:                                                                     #

# Initial and updated models
  MODEL='gb-m0.dat'  MODOUT='y0m.out'  NEGPAR=1

# Calculating matrices for inversion
  N1=26  N2=181  N3=1  D1=1  D2=1  D3=1  O1=1  O2=1  O3=1
  M1='m1.out'  M2='m2.out'  MODL2=' '  MODSOB='modsob.out'
  SOBOLEV='sob11.dat'  SOBW01=1  # minimizing first derivatives
  invsoft:                                                                    #
# Unit given standard deviation: ERRMUL=SQRT(N1*N2*N3)
  GRD='yd.out'  INDFUN=1  MPAR=1  POWERM=1  ERRMUL=68.6
  GM1='gm1.out'  GM2='gm2.out'  GM3=' '  DM1='dm1.out'
  GRDERR='sigmab.out'
  invpts:                                                                     #
  GRD=' '

# Matrix operations
  N1=0  N2=1  N3=1  M1='m2.out'
  CAL='inv.cal' GRD1='dm1.out' GRD2='dm2.out'
  grdcal:                                                                     #
  M1='m1.out'  M2='m2.out'  GM1='gm1.out'  DM1='dm2.out'  SM1='sm1.out'
  gmdmgmt:                                                                    #
  N1=0  N2=0  N3=1  M1='m1.out'  M2='m1.out'  SOBMUL=1E11 # parameter s_Y
  CAL='addsob.cal' GRD1='sm1.out' GRD2='modsob.out' GRD3='sm2.out'
  grdcal:                                                                     #
  M1='m1.out'               SM1='sm2.out'  SM2='sm3.out'
  sminv:                                                                      #
  M1='m2.out'  M2=' '       DM1='dm2.out'  GM1='gm2.out'  GM2='gm3.out'
  dmgm:                                                                       #
  M1='m1.out'  M2='m2.out'  GM1='gm1.out'  GM2='gm3.out'  GM3='gm4.out'
  gmgm:                                                                       #
  M1='m1.out'  M2=' '       SM1='sm3.out'  GM1='gm4.out'  GM2='gm5.out'
  smgm:                                                                       #

# Updating the model
  M1='m1.out'  MODNEW='gm5.out'
  modmod:                                                                     #

# Calculating gridded values of optimized GB parameter Y
  N1=26  N2=181  N3=1  D1=1  D2=1  D3=1  O1=1  O2=1  O3=1
  MODEL='y0m.out'
  MPAR=1  ICB=' '  VEL='fgby22.out'
  grid:                                                                       #

# Plotting optimized parameters of Gaussian beams
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Before smoothing and interpolation
  GRD='y0a.out'     PS='y0a.ps'
# grdps:
  GRD='r0a.out'     PS='r0a.ps'
# grdps:

# After smoothing and interpolation
  GRD='fgby22.out'  PS='fgby22.ps'
# grdps:
  GRD='fgbr22.out'  PS='fgbr22.ps'
# grdps:

# Plotting widths of Gaussian beams for given receiver positions
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  SRP='mar-srp.dat'  CPAR1=220
  srp:                                                                        #
  go.pl: "gb-test.h"                                                          #
# copy.pl: "mar-gb.ps" "mar-220.ps"

# Important output files
# ~~~~~~~~~~~~~~~~~~~~~~
# 'fgby22.out'...   Smoothed optimum initial parameters Y
#                   (imaginary part of M)
# 'fgbr22.out'...   Smoothed optimum initial parameters R
#                   (real part of M)
# 'mar-gb.ps' ...   Displayed halfwidths of GBs
