#!/usr/bin/tclsh
#-------------------------------------------------------------------------
# This is the regression test for SICS on the AMOR instrument
#
# Mark Koennecke, November 2006
#------------------------------------------------------------------------
source sicstcldebug.tcl
source insttestutil.tcl
source amorinventory.tcl

#------ If there is another argument, test at localhost: for debugging...
if {$argc > 0} {
	set instrument local
} else {
	set instrument amor
} 

initSicsDebug $instrument
config rights lnsmanager lnsSICSlns

#--------- remember soz position
set sozpos [SICSValue soz]

standardPrelude $instrument count

set notList [list chopperspeed chopper1phase chopper2phase ch1ph ch2ph chsp]
lappend notList aby pby

lappend commandList "counter setthreshold 1 0"
lappend commandList "count timer 20"
lappend commandList "cscan soz 0 .1 2 2"
lappend commandList "sscan soz 0. .1 3 2"
lappend commandList "repeat 2"
lappend commandList "wwwsics"
lappend commandList "amorpar"
lappend commandList "amorstatus collapse"
lappend commandList "amorstatus singletof"
lappend commandList "amorstatus projectytof"


lappend varList [list "amorset chopper offset" 137.5 10]
lappend varList [list "amorset chopper scaleoffset" 0 1]
# TODO: slit1  when configured
lappend varList [list "amorset mono offset" 200.5 10]
lappend varList [list "amorset mono scaleoffset" 0 1]
lappend varList [list "amorset ds offset" -40 5]
lappend varList [list "amorset ds scaleoffset" 0 1]
lappend varList [list "amorset slit2 offset" 267. 5]
lappend varList [list "amorset ds scaleoffset" 0 1]
lappend varList [list "amorset slit3 offset" -53 5]
lappend varList [list "amorset slit3 scaleoffset" 0 1]
lappend varList [list "amorset slit4 offset" 270 5]
lappend varList [list "amorset slit4 scaleoffset" 0 1]
lappend varList [list "amorset sample offset" 308 5]
lappend varList [list "amorset sample scaleoffset" 0 1]
lappend varList [list "amorset slit5 offset" -54. 5]
lappend varList [list "amorset slit5 scaleoffset" 0 1]
lappend varList [list "amorset detector offset" 297 5]
lappend varList [list "amorset detector scaleoffset" 0 1]
lappend varList [list "amorset ana offset" 200 5]
lappend varList [list "amorset ana scaleoffset" 0 1]
lappend varList [list "d1b sign" -1 .1]
lappend varList [list "dbs sign" -1 .1]
lappend varList [list "d2b sign" -1 .1]
lappend varList [list "d3b sign" -1 .1]
lappend varList [list "d4b sign" -1 .1]
lappend varList [list "aom sign" -1 .1]
lappend varList [list "atz sign" -1 .1]
lappend varList [list "d5b sign" -1 .1]
lappend varList [list "com sign" -1 .1]
lappend varList [list "cox sign" -1 .1]


lappend interruptList "drive soz 90"
lappend interruptList "count timer 120"
lappend interruptList "exe insttestbatch.tcl"

set par(inventory) $inventory
set par(driveCommand) drive
set par(motorList) $motors
set par(notList) $notList
set par(commandList) $commandList
set par(interruptList) $interruptList
set par(instrument) $instrument
set par(user) lnsmanager
set par(password) lnsSICSlns

#================= actually test =====================================
set counters [runStandardTests par]

#--------- Test detection system
puts stdout "Testing detector"
set det 1
hm genbin 0 200000 2
hm init
count timer 600
set counts [SICSValue "hm sum 0 128 0 256"]
if {$counts <= 0}  {
   set det 0
}
if {$det == 1} {
	puts stdout "Detector passed"
} else {
   puts stdout "Check detector, found no noise after 10 minutes counting"
}
#--------- put detector back where it was
drive soz $sozpos
standardRestore 

#------------ print a summary
set inst [string toupper $instrument]
puts stdout "Test Summary for $inst"
printStandardSummary $counters
puts stdout "Chopper NOT tested!!"

exit
