#!/usr/bin/env perl

use Pezca; 
use lib ("$ENV{APP}/fcup_gain/scripts/");
use LSF;

$| = 1;

$n = 0;
$date_time = localtime;
print "----------------  $date_time  ----------------------------------\n";
($error,$tacx) = Pezca::GetDouble('tac_x.RBV');
($error,$tac) = Pezca::GetDouble('frwd_scalerS13b.VAL');
($error,$slm) = Pezca::GetDouble('scaler_calc2');
($error, $na_2c21) = Pezca::GetDouble('IPM2C21A.IENG');
($error, $na_2c24) = Pezca::GetDouble('IPM2C24A.IENG');
$slm_s = &round_off($slm);
$c21_s = &round_off($na_2c21);
$c24_s = &round_off($na_2c24);
$tac_s = &round_off($tac);
$tacx_s = &round_off($tacx);
while ($tacx<$tacx_s+5.) {
  $tacx_p=$tacx+0.02;
#  while($n<5){
#    if ($na_2c21 < 5. || abs($c21_s - $na_2c21)>0.1*$c21_s ) {
## beam current does not match
#    }
#    else {
#      $n++;
#      sleep 2;
#   }
#    ($error,$slm) = Pezca::GetDouble('scaler_calc2');
#    ($error, $na_2c21) = Pezca::GetDouble('IPM2C21A.IENG');
#    ($error, $na_2c24) = Pezca::GetDouble('IPM2C24A.IENG');
# }
  sleep 20;
  $n=0;
  $error = Pezca::PutDouble("tac_x",$tacx_p);
  ($error,$tacx) = Pezca::GetDouble('tac_x.RBV');
  $tacx_a = &round_off($tacx);
  ($error,$tac) = Pezca::GetDouble('frwd_scalerS13b.VAL');
  $tac_a = &round_off($tac);
  $date_time = localtime;
  print "$date_time $tacx_a  $tac_a \n";
}


sub round_off {
    my ($x) = @_;

    $x = int(1000.*$x)/1000.;
}









