######################################
proc DataFile {fname dt criterium} {
  set fd [open $fname a]
  puts $fd "# Time Temp Curr Volt"
  puts $fd "# sec    K   pA   V  "

  set t0 [clock clicks -milliseconds]
  set time [expr ([clock clicks -milliseconds] - $t0) * 0.001] 
  set temp  [result tt ts]
  set curr  [result kth] 
  set volt  [result kth volt]
  set nr 0
  hsetprop tt updateperiod $dt
  while {[expr $criterium]} {
 
    set time [expr ([clock clicks -milliseconds] - $t0) * 0.001] 
    set temp  [result tt ts]
    set curr  [result kth] 
    set volt  [result kth volt]
    set nr [expr $nr + 1]

    puts $fd "$time $temp $curr $volt"
    
    if {$nr > 10 } {
     close $fd
     set nr 0
     set fd [open $fname a]
    }   

    wait $dt
    

  }
  clientput "...done!"
  close $fd
  hsetprop tt updateperiod 5
  
}

####################################
##example 
#set fname "testfile.dat"
#set dt 1
#set criterium {$time < 8.0}

#DataFile $fname $dt $criterium
###################################
