#----------------------------------------------------------
#  This script creates the file BLOCK_SIZE.CSV, which contains
#  all kriging debug parameters for a series of block sizes,
#  each centered at a given coordinate.  The output can be
#  used to determine optimal block size based on any of the
#  values of various parameters, such as Kriging efficiency.
#----------------------------------------------------------
#  Current limitations are:
#  - all parameter values must be entered directly into this script
#  - all block dimensions are the same (X = Y = Z)
#  - only a single centroid can be estimated
#----------------------------------------------------------

file delete -force block_size.csv

#----------------------------------------------------------
# Open the output file and write header info
#----------------------------------------------------------
set outputFileName "block_size.csv"
set outputFileHandle [open $outputFileName "w"]
set csvData "Y,X,Z,Kriging variance,Twice std dev,Block variance,Kriging efficiency,Slope,Lagrange multiplier,Conditional bias slope"
puts $outputFileHandle $csvData

set minblksize      5
set maxblksize    100
set sizeincrement   5

set ycen 7340
set xcen 1660
set zcen 110

set ycen [expr 1.0*$ycen]
set xcen [expr 1.0*$xcen]
set zcen [expr 1.0*$zcen]

for {set size $minblksize} {$size <= $maxblksize} {set size [expr $size + $sizeincrement]} {

  file delete -force block_size.mdl
  file delete -force block_size.not

  set size [expr 1.0*$size]
  set halfsize [expr $size/2]

  set ymin [expr $ycen - $halfsize]
  set ymax [expr $ycen + $halfsize + $size]

  set xmin [expr $xcen - $halfsize]
  set xmax [expr $xcen + $halfsize]

  set zmin [expr $zcen - $halfsize]
  set zmax [expr $zcen + $halfsize]

  #puts "size = $size"
  #puts "halfsize = $halfsize"
  #puts "ymin = $ymin  ymax = $ymax"
  #puts "xmin = $xmin  xmax = $xmax"
  #puts "zmin = $zmin  zmax = $zmax"
  #SclPause

  set status [ SclFunction "BM EXIT" {
    frm20103={
      {
        exit_mode="S"
      }
    }
  }]

  set status [ SclFunction "BLOCK MODEL OPEN" {
    frm00906={
      {
        modname="block_size"
        use_const="N"
      }
    }
    frm20020={
      {
        _action="apply"
      }
    }
    frm20025={
      {
        description=""
        invoke_mode=""
        method="minMax"
        extentsType="N"
        yorg="$ymin"
        xorg="$xmin"
        zorg="$zmin"
        yext="$ymax"
        xext="$xmax"
        zext="$zmax"
        yblk="$size"
        xblk="$size"
        zblk="$size"
        bearing="0"
        dip="0"
        plunge="0"
        xrot=""
        yrot=""
        zrot=""
        subBlockingType="Standard"
        maxres=""
        save_audit="Y"
      }
    }
    frm20026={
      {
        moved="No"
        res=""
      }
    }
  }]

  set status [ SclFunction "BM ADD ATTRIBUTE" {
    frm20150={
      {
        attributes={
          {
            name="estimated_value"
            type="float"
            decimals="2"
            bgv="0"
            description=""
          }
        }
      }
    }
  }]


  set status [ SclFunction "BM FILL OK" {
    frm20030={
      {
        attname="estimated_value"
      }
    }
    frm20601={
      {
        dist_att=""
        avgdist_att=""
        numsam_att=""
        kgvar_att=""
      }
    }
    frm20040={
      {
        datasrc="STRING FILE"
        location="gold_cut"
        id="17"
        range="1"
        field="1"
        constrain="N"
      }
    }
    frm20080={
      {
        search="ELLIPSOID"
        minsamples="1"
        maxsamples="5"
        majordist="9999"
        vertdist="9999"
        const_by_id="N"
        majorbrg="0"
        dip="0"
        tilt="0"
        semimajfact="1"
        minorfact="1"
        string_file_source="true"
        rotationTypes="Surpac ZXY LRL"
        first_axis_rotation="L"
        sec_axis_rotation="R"
        third_axis_rotation="L"
        sec_axis="X"
        third_axis="Y"
      }
    }
    frm20101={
      {
        vg_model_file_name=""
        model="SPHERICAL"
        co="0.5"
        c1="0.5"
        a1="100"
        azimuth1="0"
        plunge1="0"
        dip1="0"
        semi_ratio1="1"
        minor_ratio1="1"
        dpt0="3"
        dpt1="3"
        dpt2="3"
        outfile="block_size"
        report_format=".not - Surpac Note File"
        debug="Y"
        constrain="Y"
      }
    }
    frm20111={
      {
        constraints=table { not op contype location id rnga rngb lbound ubound ext } {
          { "NOT" "ABOVE" "Y PLANE" "99999999" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
          { "" "" "" "" "" "" "" "" "" "" }
        }
        conexp=""
        confile=""
        keep_blocks="N"
      }
    }
  }]

  set status [ SclFunction "BM EXIT" {
    frm20103={
      {
        exit_mode="S"
      }
    }
  }]

  #----------------------------------------------------------
  # Open the input file.
  #----------------------------------------------------------
  set fileName "block_size.not"
  set fileHandle [open $fileName "r"]

  #----------------------------------------------------------
  # read the first line of the input file
  #----------------------------------------------------------
  set data [gets $fileHandle]

  #----------------------------------------------------------
  # read each line of the file until the end of the file
  #----------------------------------------------------------
  while {![eof $fileHandle]} {

    # Keep reading in lines from the file until we encounter
    # the phrase Estimated grade
    while {![string match "*Estimated grade*" $data]} {

      # If we have reached the end of the file, then bail out
      if {[eof $fileHandle]} {
        break
      }

      set data [gets $fileHandle]

    }

    set data [gets $fileHandle]
    set data_list [split $data : ]
    set kriging_variance [string trim [lindex $data_list 1]]
    #puts "$kriging_variance"

    set csvData "$size,$size,$size,"
    append csvData "$kriging_variance,"

    set data [gets $fileHandle]
    set data_list [split $data : ]
    set twice_std_dev [string trim [lindex $data_list 1]]
    append csvData "$twice_std_dev,"

    set data [gets $fileHandle]
    set data_list [split $data : ]
    set block_variance [string trim [lindex $data_list 1]]
    append csvData "$block_variance,"

    set data [gets $fileHandle]
    set data_list [split $data : ]
    set kriging_efficiency [string trim [lindex $data_list 1]]
    append csvData "$kriging_efficiency,"

    set data [gets $fileHandle]
    set data_list [split $data : ]
    set slope [string trim [lindex $data_list 1]]
    append csvData "$slope,"

    set data [gets $fileHandle]
    set data_list [split $data : ]
    set lagrange_multiplier [string trim [lindex $data_list 1]]
    append csvData "$lagrange_multiplier,"

    set data [gets $fileHandle]
    set data_list [split $data : ]
    set conditional_bias_slope [string trim [lindex $data_list 1]]
    append csvData "$conditional_bias_slope,"

    puts $outputFileHandle $csvData

  break
  }

  close $fileHandle

}

close $outputFileHandle

puts ""
puts ""
puts "----------------------------"
puts "Results in BLOCK_SIZE.CSV"
puts "----------------------------"

