#!/usr/bin/perl
#
# $Name:  $
#
# $Log: dbSeedubvri.pl,v $
# Revision 1.3  2003/06/07 23:24:45  robert
# Add user and password to DBI usage.
#
# Revision 1.2  2002/02/19 14:52:00  robc
# Add support for AppConfig.
# Add timing of operation.
#
# Revision 1.1  2001/11/23 16:11:30  robc
# First version
#

use warnings;
use strict;
use DBI;
use AppConfig qw( :argcount :expand );
use Time::HiRes qw( tv_interval gettimeofday );

sub setup_args( $$ );

use constant ID => q$Id: dbSeedubvri.pl,v 1.3 2003/06/07 23:24:45 robert Exp $;

my $version = join ( ' ', ( split ( ' ', ID ) )[ 1 .. 3 ] );
$version =~ s/,v\b//;
$version =~ s/(\S+)$/($1)/;

my $CONFIG_FILE = $ENV{ TASSIV_CFG }
   || '/tass/src/tassiv_reduce/tassiv_reduce.cfg';
my $c = AppConfig->new(
   {
      CREATE => 1,
      GLOBAL => {
                  EXPAND   => EXPAND_VAR,
                  ARGCOUNT => ARGCOUNT_ONE
      }
   }
   );
$c = setup_args( $c, $CONFIG_FILE );

my $DB_NAME = $c->name;
my $DB_HOST = $c->host;

print "Connecting to $DB_NAME at $DB_HOST\n";
my $dbh =
   DBI->connect( "DBI:Pg:dbname=$DB_NAME;host=$DB_HOST", $c->user, $c->passwd,
                 { RaiseError => 1, AutoCommit => 1 } );

my $U = "Bt - 0.325 + 0.8034*(Bt-Vt)";
my $B = "Bt + 0.018 - 0.2580*(Bt-Vt)";
my $V = "Vt + 0.008 - 0.0988*(Bt-Vt)";
my $R = "Vt - 0.014 - 0.5405*(Bt-Vt)";
my $I = "Vt - 0.039 - 0.9376*(Bt-Vt)";

my $insert_sql = <<__END_INSERT__;
INSERT INTO reference_ubvri(
   star_id,
   ra,
   dec,
   U, B, V, R, I
   )
SELECT star_id, ra, dec, $U, $B, $V, $R, $I
FROM tycho2
WHERE   Bt > 1.0
  AND   Vt > 1.0
  AND e_Bt < 0.075
  AND e_Vt < 0.075
  AND (Bt - Vt) BETWEEN -0.2 AND 1.8
  AND prox > 500
__END_INSERT__

{
   print "Deleting index.  Dont' care if an error shows up here...\n";
   local $dbh->{ RaiseError };
   $dbh->do( "DROP INDEX reference_ubvri_loc_index" );
}

print "Deleting from reference_ubvri...\n";
$dbh->do( "DELETE FROM reference_ubvri" );

my $t0          = [ gettimeofday ];
print "Creating new UBVRI data from Vt, Bt.  This may take a while...\n";
$dbh->do( $insert_sql );

my $elapsed = tv_interval( $t0, [ gettimeofday ] );

print "Counting stars inserted: ";
my $count = $dbh->selectrow_array( "SELECT count(*) FROM reference_ubvri" );
printf "%d at %.0f per/second\n", $count, $count / $elapsed;


print "Creating index...\n";
$dbh->do(
"CREATE INDEX reference_ubvri_loc_index ON reference_ubvri USING btree( ra, dec)"
   );

print "Vacuuming db...\n";
$dbh->do( "VACUUM FULL ANALYZE" );

$dbh->disconnect;

sub setup_args( $$ )
   {
   my ( $c, $file ) = @_;
   $c->define(
               DB_name => {
                            DEFAULT => 'tassiv',
                            ALIAS   => 'name'
               },
               DB_user => {
                            DEFAULT => 'robert',
                            ALIAS   => 'user'
               },
               DB_passwd => {
                            DEFAULT => '',
                            ALIAS   => 'passwd'
               },
               DB_host => {
                            DEFAULT => 'localhost',
                            ALIAS   => 'host'
               }
               );
   $c->file( $file );

   return $c;
   }

