#----------------------------------------------------------
#  This script creates the file INFORMING_SAMPLES.STR, 
#  which contains all points used to estimate a single block:
#  D1=grade,D2=anisotropic_distance,D3=actual_distance,D4=weight.
#  The entire original string file is displayed as markers,
#  and INFORMING_SAMPLES.STR is displayed with D1 on the left
#  and D4 on the right of the point.
#----------------------------------------------------------
#  Current limitations are:
#  - all parameter values must be entered directly into this script
#----------------------------------------------------------

file delete -force informing_samples.str

set status [ SclFunction "EXIT GRAPHICS" {
  frm00108={
    {
      exit_mode="E"
    }
  }
}]

set status [ SclFunction "RECALL ANY FILE" {
  file="gold_cut17.str"
  mode="openInNewLayer"
}]

set status [ SclFunction "ERASE STRINGS" {
  frm00089={
    {
      range1=""
      range2=""
      range3=""
      ifld_num="d1"
      textalignment="<"
      legend_location="no legend"
      layer_name="gold_cut17.str"
    }
  }
}]

set status [ SclFunction "DRAW MARKERS" {
  frm00089={
    {
      range1=""
      range2=""
      range3=""
      ifld_num="d1"
      textalignment="<"
      position="All points"
      legend_location="no legend"
      layer_name="gold_cut17.str"
    }
  }
}]

set size 25
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]

file delete -force informing_samples.mdl
file delete -force informing_samples.not

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

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

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="informing_samples"
      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 GRAPHICS CONSTRAIN ADD" {
  frm20111={
    {
      constraints=table { not op contype location id rnga rngb lbound ubound ext } {
        { "NOT" "ABOVE" "Y PLANE" "$ycon" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
      }
      conexp=""
      confile=""
      keep_blocks="N"
    }
  }
}]

set status [ SclFunction "BM GRAPHICS DRAW" {
  frm20503={
    {
      display="normal"
      constraint="N"
      slice="N"
      rescale="Y"
      all_blocks="N"
      cloud_marker="."
      cloud_size="4"
    }
  }
}]

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="20"
      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="informing_samples"
      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" "$ycon" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
        { "" "" "" "" "" "" "" "" "" "" }
      }
      conexp=""
      confile=""
      keep_blocks="N"
    }
  }
}]

#----------------------------------------------------------
# Open the output file and write header info
#----------------------------------------------------------
set outputFileName "informing_samples.str"
set outputFileHandle [open $outputFileName "w"]

set strData "informing_samples,1-Jan-01,,SSI_STYLES:styles.ssi"
puts $outputFileHandle $strData
set strData "0,           0.000,           0.000,           0.000,           0.000,           0.000,           0.000"
puts $outputFileHandle $strData

#----------------------------------------------------------
# Open the input file.
#----------------------------------------------------------
set fileName "informing_samples.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 INFORMING SAMPLES
  while {![string match "*INFORMING SAMPLES*" $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]
  puts "$data"

  while {$data != ""} {
    set data [gets $fileHandle]
    puts "$data"

    scan $data "%f %f %f %f %f %f %f" y x z grade anisotropic_dist actual_dist weight

    set strData "1,$y,$x,$z,$grade,$anisotropic_dist,$actual_dist,$weight"
    puts $outputFileHandle $strData

  }

break
}

close $fileHandle

set strData "0, 0.000, 0.000, 0.000,"
puts $outputFileHandle $strData
set strData "0, 0.000, 0.000, 0.000, END"
puts $outputFileHandle $strData

close $outputFileHandle

set status [ SclFunction "RECALL ANY FILE" {
  file="informing_samples.str"
  mode="openInNewLayer"
}]

set status [ SclFunction "ERASE STRINGS" {
  frm00089={
    {
      range1=""
      range2=""
      range3=""
      ifld_num="d1"
      textalignment="<"
      legend_location="no legend"
      layer_name="informing_samples.str"
    }
  }
}]

set status [ SclFunction "DRAW MARKERS" {
  frm00089={
    {
      range1=""
      range2=""
      range3=""
      ifld_num="d1"
      textalignment="<"
      position="All points"
      legend_location="no legend"
      layer_name="informing_samples.str"
    }
  }
}]

set status [ SclFunction "DRAW DESC" {
  frm00089={
    {
      range1=""
      range2=""
      range3=""
      ifld_num="d1"
      textalignment=">"
      position="All points"
      legend_location="no legend"
      layer_name="informing_samples.str"
    }
  }
}]

set status [ SclFunction "DRAW DESC" {
  frm00089={
    {
      range1=""
      range2=""
      range3=""
      ifld_num="d4"
      textalignment="<"
      position="All points"
      legend_location="no legend"
      layer_name="informing_samples.str"
    }
  }
}]

set status [ SclFunction "ZOOM OUT" {}]
puts "---------------------------------"
puts "Results in informing_samples.str  (D1=grade,D2=anisotropic_distance,D3=actual_distance,D4=weight)"
puts "---------------------------------"
