# --------------------------------------------------------------------------
# Initialization script for a simulated TOPSI instrument
#
#
# Dr. Mark Koennecke February, 1996
#---------------------------------------------------------------------------
#                        O P T I O N S
# --------------- Initialize Tcl internals --------------------------------

rename scan stscan

#-------- a home for this, everything else is in relation to this
set shome /data/koenneck/src



# first all the server options are set

#ServerOption RedirectFile  $shome/sics/stdout

ServerOption ReadTimeOut  100
# timeout when checking for commands. In the main loop SICS checks for
# pending commands on each connection with the above timeout, has 
# PERFORMANCE impact!

ServerOption AcceptTimeOut  100
# timeout when checking for connection req.
# Similar to above, but for connections

ServerOption ReadUserPasswdTimeout  10
# time to wiat for a user/passwd to be sent from a client. Increase this
# if there is a problem connecting to a server due to network overload\

ServerOption LogFileDir $shome/log
#LogFileDir is the directory where the command log is going

#ServerOption logstartfile $shome/sics/start.tcl

ServerOption LogFileBaseName  $shome/sics/tmp/server
# the path and base name of the internal server logfile to which all
# activity will be logged.

ServerOption TecsStartCmd "tecs/bin/TecsServer -h lnsp26:4000/0"
# -h host:port/channel is for serial server
ServerOption TecsBinDir tecs/bin/
ServerOption TecsLogDir /data/koenneck/tmp/
ServerOption TecsPort 9753

ServerOption statusfile sicsstatus.tcl

ServerOption ServerPort 3006
# the port number the server is going to listen at. The client MUST know
# this number in order to connect. It is in client.ini

ServerOption InterruptPort 2914
# The UDP port where the server will wait for Interrupts from clients.
# Obviously, clients wishing to interrupt need to know this number.

# Telnet options
ServerOption TelnetPort 1301
ServerOption TelWord    sicslogin

ServerOption DefaultTclDirectory $shome/sics/tcl
ServerOption DefaultCommandFile  ""

#------ a port for broadcasting UDP messages
#ServerOption QuieckPort 2108

TokenInit connan

#---------------------------------------------------------------------------
#                        U S E R S

# than the SICS users are specified
# Syntax: SicsUser name password userRightsCode
SicsUser Mugger Diethelm  1
SicsUser User   Rosy      2
SicsUser Spy    007       1
SicsUser me     me        1
SicsUser testuser 01lns1  3

#--------------------------------------------------------------------------
#               S I M P L E     V A R I A B L E S

# now a few general variables are created
# Syntax: VarMake name type access
# type can be one of: Text, Int, Float
#access can be one of: Internal, Mugger, user, Spy

VarMake Instrument Text Internal
Instrument  "TOPSI"
Instrument lock

VarMake starttime Text User

VarMake Title Text User
VarMake sample Text User
sample "DanielOxid"
Title "TopsiTupsiTapsi"
VarMake User  Text User
User  "Daniel_the_Clementine"

VarMake detectordist Float Mugger
detectordist 2500
detectordist lock
VarMake sampledist Float Mugger
sampledist 496.
sampledist lock

VarMake batchroot Text User

#--------------------------------------------------------------------------
#                D E V I C E S :  M O T O R S

# Motor a4 EL734 LNSP22 4000 5 6
# EL734 motor with parameters: hostname PortNumber Channel MotorID
#Motor D1V EL734 lnsp22.psi.ch 4000 3 3
Motor A1 SIM 15.0 120. .1 2.   # Monochromator Theta
Motor A2 SIM -73. 137.  .1 1.   # Monochromator 2Theta
Motor A3 SIM -360. 360.   .1 3.   # Sample Omega
#Motor A3 el734 localhost 4000 2 3
Motor A4 SIM -130. 130. .1 1.  # Sample 2Theta
Motor A5 SIM -30. 30.   .1 3.  # ? horiz. Translation
Motor A6 SIM -30. 30.   .1 3.  # ? vert Translation
Motor MTL SIM -30. 30.  .1 3.   # mono lower translation
Motor MTU SIM -30. 30.  .1 3.   # mono upper translation
Motor STL SIM -30. 30.  .1 3.   # sample lower translation
Motor STU SIM -30. 30.  .1 3.   # sample upper translation
Motor MGU SIM -50. 50.  .1 3.   # mono upper goniometer
Motor SGL SIM -20. 20.  .1 3.   # sample lower goniometer
Motor SGU SIM -20. 20.  .1 3.   # sample  upper goniometer
Motor SDM SIM -5 50.     .1 3.   # weird Motor
SicsAlias A4 Tasse
SicsAlias A5 MonoX
SicsAlias A5 MonoY
SicsAlias A5 MonoPhi
SicsAlias A5 MonoChi

Motor D1R SIM -20. 20. .1 3. # Diaphragm 1 right 
Motor D1L SIM -20. 20. .1 3. # Diaphragm 1 left 
Motor D1T SIM -20. 20. .1 3. # Diaphragm 1 top & Bottom 

Motor D2R SIM -20. 20. .1 3. # Diaphragm 2 right 
Motor D2L SIM -20. 20. .1 3. # Diaphragm 2 left 
Motor D2T SIM -20. 20. .1 3. # Diaphragm 2 top & Bottom 

Motor BeamStopX SIM -20. 20. .1 3. # Diaphragm 2 right 
Motor BeamStopY SIM -20. 20. .1 3. # Diaphragm 2 left 

Motor DetectorX SIM -20. 20. .1 3. # Diaphragm 2 right 
Motor DetectorY SIM -20. 20. .1 3. # Diaphragm 2 left 
Motor DetectorRotation SIM -20. 20. .1 3. # Diaphragm 2 top & Bottom 

ClientPut "Motors done"

MakeMulti sampletable
sampletable alias D1r x
sampletable alias D1L y
sampletable alias D1T z
sampletable pos   henry D1r  5. D1L  -5. D1T  0.
sampletable endconfig
SicsAlias sampletable st
#------------------------------------------------------------------------
#               Velocity selector 
#Motor tilt EL734 lnsp25.psi.ch 4000 2 2 
Motor tilt SIM -15 15 .1 3.
set dornen(Host) lnsp25.psi.ch
set dornen(Port) 4000
set dornen(Channel) 6
set dornen(Timeout) 5000
#VelocitySelector nvs tilt DORNIER dornen
VelocitySelector nvs tilt SIM
nvs add -20 28800
nvs add 3800 4500
nvs add 5900 6700
nvs add 8100 9600
unset dornen
emon unregister nvswatch
MakeSANSWave lumbda nvs
ClientPut "Velocity Selector done"
SicsAlias MTL sax
SicsAlias A3 som
#--------------------------------------------------------------------------
#                      C O U N T E R S
MakeCounter counter SIM  .05
#MakeCounter counter EL737 lnsp19.psi.ch 4000 4

#--------------------------------------------------------------------------
#         M U L T I D E V I C E  V A R I A B L E S 
MakeMono Mono "Ge-111" A1 A2 SDM
Mono dd 3.3537
Mono vk1 -0.025942
Mono vk2 5.35166
MakeWaveLength lambda Mono
MakeO2T  O2T A3 A4
#--------------------------------------------------------------------------
#             P R O C E D U R E S

source tcl/log.tcl

MakeDrive
SicsAlias drive dr
Publish LogBook Spy
MakeRuenBuffer
#---------------- TestVariables for Storage 
VarMake SicsDataPath Text Mugger
SicsDataPath "$shome/sics/"
SicsDataPath lock
VarMake SicsDataPrefix Text Mugger
SicsDataPrefix test
SicsDataPrefix lock
VarMake SicsDataPostFix Text Mugger
SicsDataPostFix ".hdf"
SicsDataPostFix lock

VarMake Adress Text User
VarMake phone  Text User
VarMake fax    Text User
VarMake email  Text User
VarMake sample_mur Float User

MakeDataNumber SicsDataNumber "$shome/sics/danu.dat"
#InitSANS  $shome/sics/sansdict.dic
InitDMC

MakeScanCommand  xxxscan counter topsi.hdd recover.bin
MakePeakCenter xxxscan


MakeHM banana SIM 
#MakeHM banana SINQHM
#banana configure HistMode HRPT
banana configure HistMode Normal
#banana configure OverFlowMode    reflect
banana configure OverFlowMode    Ceil
banana configure Rank     1
#banana configure Length   400
banana configure Length   16384
banana configure dim0 128
banana configure dim1 128
banana configure BinWidth 4
banana preset 100.
banana CountMode Timer
banana configure HMComputer lnse02.psi.ch
banana configure HMPort 2400
banana configure Counter counter
banana init

ClientPut "HM initialized"
source $shome/sics/tcl/scancom.tcl
#source $shome/sics/countf.tcl
#source $shome/sics/tcl/count.tcl
#Publish count User
#Publish repeat user
source $shome/sics/tcl/fit.tcl
Publish fit Spy
SerialInit
Publish serialport User
Publish p1 User
Publish HakleGet User
#-------------------- Testing Haake thermostat
source hakle.tcl
Publish IniHaakeArray  Mugger
ClientPut "Installed ev test stuff"


#------------------ 4 circle stuff
Motor twotheta  SIM -120.0 120. .1 2.   # 2 theta arm
Motor omega SIM -73. 134. .1 2.       # omega
Motor chi SIM  0. 360. .1 2.           # chi circle 
Motor phi SIM  -360. 360. .1 2.          # phi circle
Motor muca SIM  30. 36. .1 2.          # phi circle
Motor dg1 SIM  -10. 40. .1 2.          # phi circle
Motor dg2 SIM  -10. 40. .1 2.          # phi circle
Motor dg3 SIM  -10. 40. .1 2.          # phi circle
SicsAlias phi ph
SicsAlias chi ch
SicsAlias twotheta stt
SicsAlias omega om
VarMake monodescription Text Mugger
monodescription  "unknownit crystal"
VarMake mono2theta Text Mugger
mono2theta  36.77
VarMake  det1zerox Float Mugger
det1zerox  128.
VarMake det1zeroy Float Mugger
det1zeroy   128.
VarMake det1dist Float Mugger
det1dist   300.
VarMake  det2zerox Float Mugger
det2zerox  128.
VarMake det2zeroy Float Mugger
det2zeroy   128.
VarMake det2dist Float Mugger
det2dist   300.
VarMake  det3zerox Float Mugger
det3zerox  128.
VarMake det3zeroy Float Mugger
det3zeroy   128.
VarMake det3dist Float Mugger
det3dist   300.
VarMake detstepwidth Float Mugger
detstepwidth 0.1
detstepwidth lock

MakeHKL twotheta omega chi phi chi
HKL lambda 0.70379
HKL setub -0.1247023 0.0016176 -0.0413566  \
          -0.1044479 -0.0013264 0.0493878 \
          0.0007513  0.0840941 0.0015745
MakeOptimise opti counter
MakeMesure mess HKL xxxscan omega o2t $shome/sics/tmp \
    SicsDataNumber
MakeHklscan xxxscan HKL

#------test rliste
SicsAlias twotheta th
SicsAlias omega om
SicsAlias chi ch
SicsAlias phi ph

source tcl/reflist.tcl
Publish rliste User


source fcircle.tcl
fcircleinit

MakeDifrac twotheta omega chi phi counter

#----------- histogram memory

#MakeHM hm1 SinqHM
MakeHM hm1 SIM
hm1 configure HistMode PSD
hm1 configure Rank     1
hm1 configure Length   65536
hm1 configure dim0 256
hm1 configure dim1 256
hm1 configure BinWidth 4
hm1 preset 100.
hm1 CountMode Timer
hm1 configure xoff 100
hm1 configure xfac 10
hm1 configure yoff 100
hm1 configure yfac 10
#hm1 configure HMComputer psds03.psi.ch
#hm1 configure HMPort 2400
hm1 configure Counter counter
hm1 genbin 0 10000 2
hm1 configure init 0
hm1 init

MakeHM hm2 SIM 
hm2 configure HistMode PSD
hm2 configure Rank     1
hm2 configure Length   65536
hm2 configure dim0 256
hm2 configure dim1 256
hm2 configure BinWidth 4
#hm2 configure HMComputer lnse02.psi.ch
#hm2 configure HMPort 2400
hm2 init

#MakeHM hm3 SIM 
#hm3 configure HistMode PSD
#hm3 configure Rank     1
#hm3 configure Length   65536
#hm3 configure dim0 256
#hm3 configure dim1 256
#hm3 configure BinWidth 4
#hm3 configure HMComputer lnse02.psi.ch
#hm3 configure HMPort 2400
#hm3 configure Counter counter
#hm3 init


ClientPut "Installed 4-circle stuff"


#source transact.tcl
#Publish transact Spy
#MakeSPS sps lnsa12.psi.ch 4000 4

#source beamdt.tcl
#------------- C804-DC motors
set nob(Computer) lnsp22.psi.ch
set nob(port)     4000
set nob(channel)  7
set nob(motor)    1
set nob(upperlimit) 250000
set nob(lowerlimit) -250000
#MakePIMotor nobi c804 nob
#set nob(channel) 6
#Motor pilz pipiezo nob

VarMake datafile Text Spy
datafile focus-1001848.hdf
#--------- create a time array for histogramming
#MakeHM chiquita SINQHM
MakeHM  hm SIM
#chiquita configure HMComputer lnse03.psi.ch
#chiquita configure HMport     2400
hm configure HistMode TOF
hm configure OverFlowMode    Ceil
hm configure Rank     1
hm configure dim0 383
hm configure Length   383
hm configure BinWidth 4
hm preset 100.
hm CountMode Timer
hm configure Counter counter
hm configure init 0
hm genbin 120. 35. 512
hm init

VarMake delay Float Mugger
delay 158.8
VarMake flightpath Float Mugger
delay 2000
VarMake flightpathlength Float Mugger
delay 2500

MakeFocusAverager average hm

FocusInstall hm focus.dic $shome/sics/focusmerge.dat 
storefocus upper 1
storefocus lower 1


#MakeChopper choco docho lnsp20 4000 8
MakeChopper choco sim
#ChopperAdapter fermispeed choco chopper1.nspee 0 20000
#ChopperAdapter diskspeed  choco chopper2.nspee 0 20000
#ChopperAdapter phase      choco chopper2.nphas 0 90.
#ChopperAdapter ratio      choco chopper2.ratio 0 6.
ChopperAdapter diskspeed  choco speed 0 20000
ChopperAdapter phase      choco phase  0 90.
ChopperAdapter ratio      choco ratio 0 6.
 
#-------- SANS Cooker
#MakeChopper cooker sanscook lnsa10 4000 11
#ChopperAdapter cookp   cooker mp 0 400
#ChopperAdapter cookz   cooker mz 0 400


source chosta.tcl
Publish chosta Spy
 
#MakeSPS sps1 lnsp25.psi.ch 4000 10


Publish testscan User
proc testscan {} {
  return " 1 2 3 4 5"
}

MakeXYTable ixi

source cotop.tcl
Publish co User

#source helium.tcl

MakeTRICSNEXUS sicsdatanumber $shome/sics/tmp trics.dic
source $shome/sics/tcl/tricscount.tcl

source autofile.tcl
autofilepath $shome/tmp/auto

MakeXYTable omth

Publish info user
MakeLin2Ang a5l a5

#source tmp/beam.tcl
source tcl/wwwpar.tcl
source bef.tcl

source batch.tcl
Publish batchrun User


#------- test of RS232Controller
#MakeRS232Controller hugo psts213 3004

#-------------------------- batch run issue
VarMake BatchRoot Text User
BatchRoot /data/koenneck/src/sics
Publish batchrun User

proc SplitReply { text } {
     set l [split $text =]
     return [lindex $l 1]
}

proc batchrun file {
   fileeval [string trim [SplitReply [BatchRoot]]/$file]
}
