#!/apps/bin/perl -w
use strict;
####################################################
#
# This file gets the gas density of the reference cell at 135psig.
# 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_hpchan7, HacAi_hpchan8, HacAi_hpchan9 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 $ch7_avg;
	  my $ch8_avg;
	  my $ch9_avg;
	    my $i = 1;
	  my $ch7sum = 0;
	  my $ch8sum = 0;
	  my $ch9sum = 0;
          while (@data2 = $sth->fetchrow_array()) {
          #  my $run_start = $data2[0];
            my $temp_ch7 = $data2[1];
            my $temp_ch8 = $data2[2];
            my $temp_ch9 = $data2[3];
#            print "\t$run_number: $temp_ch0\n";
	    $ch7sum += $temp_ch7;
	    $ch8sum += $temp_ch8;
	    $ch9sum += $temp_ch9;
	    $ch7_avg = $ch7sum / $i;
	    $ch8_avg = $ch8sum / $i;
	    $ch9_avg = $ch9sum / $i;
	    $i += 1;
          }

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

	my $Ppsig = 135;
	my $TK = 273.15;
	my $TT = ($ch7_avg+$ch8_avg+$ch9_avg)/3.00 + $TK;
	my $Patm = ($Ppsig + 14.7)*0.068045;
	my $rhoT = $Patm*273.2746/$TT;

          $sth->finish;
	

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

          print "\nThe gas density (molecular) in the target chamber is $rhoT amg.\n";
          print "\nMultiply by k for a k-atomic molecule for atomic density ...\n";
          print "\n... and then by 2.6868e19 to get number density in atoms/cm^3.\n\n";
          print "Enter run number (Ctrl+c to exit) > ";
        }

   $dbh->disconnect;
