#!/usr/bin/perl -w
#
# preliminary way to set the monochromator 2 theta angle 'mth' and
# based thereon the sample 2 theta angle 's2t'. 
#
use Math::Trig ;
#
##################################################################
#
if ($ARGV[0]) {
  $m2t = $ARGV[0] ;
} else {
  die " *** usage: m2t_generator <m2t>\n" ;
} ;
#----------------------------------------------
# list of off-sets:
 $M   =  100.0 ; # monitor / polariser
 $DS  =  -50.0 ; # shielding slit
 $D2  =  -52.5 ; # 2nd diaphragm
 $D3  =  -53.5 ; # 3rd diaphragm
 $S   =  280.8 ; # sample table
 #$D4  =    0.0 ;  4th diaphragm
 #$D5  =    0.0 ; # 5th diaphragm
 $D   = -162.0 ; # single detector
 #$D   =    0.0 ; # area detector
#----------------------------------------------
# list of fix or default values:
 $DST =   15.0 ; # opening shielding slit
 $D2T =    1.0 ; # opening 2nd diaphragm
 $D3T =    1.0 ; # opening 2rd diaphragm
 if ( $ARGV[1] ) {
  $s2t = $ARGV[1] ;
 } else {
  $s2t =    0.0 ; # sample 2 theta
 } ;
#----------------------------------------------
# list of positions due to the ruler:
 $M  += 7440.0 ; # monitor / polariser
 $DS += 6980.0 ; # shielding slit
 $D2 += 6653.0 ; # 2nd diaphragm
 $D3 += 5956.0 ; # 3rd diaphragm
 $S  += 5047.8 ; # sample table
 #$D4 +=    0.0 ; # 4th diaphragm
 #$D5 +=    0.0 ; # 5th diaphragm
 $D  += 2600.0 ; # detector stage
#----------------------------------------------
#----------------------------------------------
# calculus 
 #  from polariser / monochromator to sample
 $DSB =  abs($M-$DS) * tan(deg2rad($m2t)) - 0.5 * $DST ;
 $D2B =  abs($M-$D2) * tan(deg2rad($m2t)) - 0.5 * $D2T ;
 $D3B =  abs($M-$D3) * tan(deg2rad($m2t)) - 0.5 * $D3T ;
 $SOZ =  abs($M-$S)  * tan(deg2rad($m2t)) ;
 #  from sample to detector 
 $com =  $s2t + $m2t ;
 $COX =  abs($S-$D)  * ( cos(deg2rad(-$com)) - 1 ) ; 
 $COZ =  abs($S-$D)  * sin(deg2rad($com)) + $SOZ ;
#
 printf "clientput    MS = %5.1f mm\n", abs($M-$S) ;
 printf "clientput    SD = %5.1f mm\n", abs($S-$D) ;
 printf "clientput    MD = %5.1f mm\n", abs($M-$D) ;
 printf "clientput    D2M = %5.1f mm\n", abs($M-$D2) ;
 printf "clientput    D3M = %5.1f mm\n", abs($M-$D3) ;
 printf "clientput    DBM = %5.1f mm\n", abs($M-$DS) ;
#
 printf "clientput  run dbs %5.1f \n", $DSB ;
 printf "clientput [run dbs %5.1f]\n", $DSB ;
 printf "clientput  run d2b %5.1f \n", $D2B ;
 printf "clientput [run d2b %5.1f]\n", $D2B ;
 printf "clientput  run d2t %5.1f \n", $D2T ;
 printf "clientput [run d2t %5.1f]\n", $D2T ;
 printf "clientput  run d3b %5.1f \n", $D3B ;
 printf "clientput [run d3b %5.1f]\n", $D3B ;
 printf "clientput  run d3t %5.1f \n", $D3T ;
 printf "clientput [run d3t %5.1f]\n", $D3T ;
 printf "clientput  run soz %5.1f \n", $SOZ ;
 printf "clientput [run soz %5.1f]\n", $SOZ ;
 printf "clientput  run com %5.1f \n", $com ;
 printf "clientput [run com %5.1f]\n", $com ;
 printf "clientput  run cox %5.1f \n", $COX ;
 printf "clientput [run cox %5.1f]\n", $COX ;
 printf "clientput  run coz %5.1f \n", $COZ ;
 printf "clientput [run coz %5.1f]\n", $COZ ;
#
#  The End *
