#!/apps/bin/perl -w
use strict;
####################################################
#
# This file gets the nitrogen density of the target chamber of the cell.
# All data from EPICS/general tables of MySQL database e02013.
#
# - Ameya Kolarkar (12-Feb-2007)
#
####################################################

print STDOUT "Script still under development ... \n";

use DBI;
my $dbh = DBI->connect('DBI:mysql:e02013:yerphi')
                or die "Couldn't connect to database: " . DBI->errstr;
#        my $sth = $dbh->prepare('SELECT * FROM general WHERE run_number = ?')
#                or die "Couldn't prepare statement: " . $dbh->errstr;

        my $run_start;
        my $run_end;
        print "Enter run number> ";
        while (my $run_number = <>) {               # Read input from the user

        my $sth = $dbh->prepare('SELECT * FROM general WHERE run_number = ?')
                or die "Couldn't prepare statement: " . $dbh->errstr;

          my @data;
          chomp $run_number;
          $sth->execute($run_number)             # Execute the query
            or die "Couldn't execute statement: " . $sth->errstr;

          # Read the matching records and print them out          
          while (@data = $sth->fetchrow_array()) {
            $run_start = $data[0];
            $run_end = $data[1];
            print "\t$run_number: $run_start $run_end\n";
          }

          if ($sth->rows == 0) {
            print "No run number matched `$run_number'.\n\n";
          }

          $sth->finish;


######### use run start and end times above to get the temperatures of RTDs

        $sth = $dbh->prepare('SELECT time, HacAi_hpchan0, HacAi_hpchan1, HacAi_hpchan2, HacAi_hpchan4, HacAi_hpchan6 FROM EPICS WHERE (time >= ? AND time <= ?)')
                or die "Couldn't prepare statement: " . $dbh->errstr;

          my @data2;
          #chomp $run_number;
          $sth->execute($run_start,$run_end)             # Execute the query
            or die "Couldn't execute statement: " . $sth->errstr;

          # Read the matching records and print them out          
	  my $ch0_avg;
	  my $ch1_avg;
	  my $ch2_avg;
	  my $ch4_avg;
	  my $ch6_avg;
	    my $i = 1;
	  my $ch0sum = 0;
	  my $ch1sum = 0;
	  my $ch2sum = 0;
	  my $ch4sum = 0;
	  my $ch6sum = 0;
          while (@data2 = $sth->fetchrow_array()) {
          #  my $run_start = $data2[0];
            my $temp_ch0 = $data2[1];
            my $temp_ch1 = $data2[2];
            my $temp_ch2 = $data2[3];
            my $temp_ch4 = $data2[4];
            my $temp_ch6 = $data2[5];
#            print "\t$run_number: $temp_ch0\n";
	    $ch0sum += $temp_ch0;
	    $ch1sum += $temp_ch1;
	    $ch2sum += $temp_ch2;
	    $ch4sum += $temp_ch4;
	    $ch6sum += $temp_ch6;
	    $ch0_avg = $ch0sum / $i;
	    $ch1_avg = $ch1sum / $i;
	    $ch2_avg = $ch2sum / $i;
	    $ch4_avg = $ch4sum / $i;
	    $ch6_avg = $ch6sum / $i;
	    $i += 1;
          }

          if ($sth->rows == 0) {
          }

	my $TK = 273.15;
	my $TT = ($ch0_avg+$ch1_avg+$ch2_avg+$ch4_avg)/4.00 + $TK;
	my $TP = ($ch6_avg) + $TK;
	my $rho = 0.112;
	my $herho = 7.4;
	my $rhoT = (1.2837*$rho)/(($TT/$TP)+0.2837);  # for Edna
	my $herhoT = (1.2837*$herho)/(($TT/$TP)+0.2837);  # for Edna
	my $rat = $rhoT/$herhoT;

          $sth->finish;
	

######### end RTD temp. extraction part

          print "\nThe nitrogen density in the target chamber is $rhoT amg.\n";
          print "\nThe helium-3 density in the target chamber is $herhoT amg.\n";
          print "\nThe ratio of nitrogen to helium-3 in the target chamber is $rat.\n\n";
          print "Enter run number (Ctrl+c to exit) > ";
        }

   $dbh->disconnect;
