proc writeline line {
  global filename
  set fil [open $filename a]
  puts $fil [concat $line]
  close $fil
}

if {[string equal notfound [sicsdescriptor filenumber]]} {
  makeobject filenumber string
  filenumber = 0
}

proc newfile {} {
  global filename
  set n [result filenumber]
  incr n
  filenumber = $n
  set num [format %4.4d $n]
  set filename "[lindex [split [result exe batchpath] :] 0]/$num.txt"
  writeline "#T        X        Y"
}

proc tscan {args} {
  global filename
  writeline "#T        X        Y       (freq=[result la])"
#  tt controlmode 1
  foreach t $args {
    clientput "drive to $t"
    drive tt $t
    set res "[result tt] [result la x] [result la y]"
    clientput $res
    writeline $res
  }
}

proc tramp {start step end} {
  global filename
  writeline "#T        X        Y        (freq=[result la])"
#  tt controlmode 1
  if {$step == 0} {
    return
  }
  set n [expr round(double(($end - $start) / $step))]
  if {$n < 0} {
    set n [expr -$n]
    set step [expr -$step]
  }
  for {set i 0} {$i <= $n} {incr i} {
    set t [expr $start + $i * $step]
    clientput "drive to $t"
    drive tt $t
    set res "[result tt] [result la x] [result la y]"
    clientput $res
    writeline $res
  }
}

proc recondense {mode} {
  switch $mode {
    start {
      tt controlmode 0
      tt set 40
      clientput "after 15 min. enter: recondense stop"
    }
    stop {
      tt set 0
      tt controlmode 1
    }
    default {
       clientput "Usage: recondense start"
       clientput "       recondense stop"
    }
  }
}
publishLazy newfile
publishLazy tscan
publishLazy tramp
publishLazy recondense

