#!/usr/bin/perl -w
#
# $Name:  $
#
# $Log: dbLoadTycho2.pl,v $
# Revision 1.3  2003/06/07 23:10:47  robert
# Add user/password to variables used.
#
# Revision 1.2  2002/02/15 04:18:53  robc
# Add support for AppConfig.
# Add timming of entire operation.
# Perltidy
#
# Revision 1.1  2001/11/23 16:11:29  robc
# First version
#

use DBI;
use IO::Socket;
use AppConfig qw( :argcount :expand );
use Time::HiRes qw( tv_interval gettimeofday );

sub setup_args( $$ );

autoflush STDOUT;

use constant ID => q$Id: dbLoadTycho2.pl,v 1.3 2003/06/07 23:10:47 robert Exp $;

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

die "Where's Tycho2?" unless my $tycho2 = shift;

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;

my ( $ra, $dec );
my ( $copy_sql, $insert_sql, $sth_insert );
my $count       = 1;
my $total_count = 0;
my $short_line  = 0;
my $no_mean     = 0;
my $photo_mean  = 0;
my $sep_double  = 0;
my $comb_double = 0;
my $no_mag      = 0;
my $t0          = [ gettimeofday ];

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

$ctass_dbh->do( "SELECT setval('star_id_seq', 10000000 )" );

$copy_sql = <<__END_COPY__;
COPY tycho2
FROM stdin
__END_COPY__

$ctass_dbh->do( $copy_sql );

open( FILE, "< $tycho2" );

while ( <FILE> )
   {
   ++$total_count;
   my @line = split ( '\|', $_ );

   # Remove entries which are not 'full'
   if ( scalar( @line ) < 30 ) { ++$short_line; next; }

   # Remove entries with no mean position (X) or were the photo-centre of two
   # Tycho-2 entries (P)
   if ( $line[ 1 ] =~ /X/ ) { ++$no_mean;    next; }
   if ( $line[ 1 ] =~ /P/ ) { ++$photo_mean; next; }

   # Remove entries which had double stars
   if ( $line[ 30 ] =~ /D/ ) { ++$sep_double;   next; }
   if ( $line[ 30 ] =~ /P/ ) { ++$$comb_double; next; }

   # Remove entries with no Bt or Vt
   if ( !( ( $line[ 17 ] =~ /[\d.-]+/o ) && ( $line[ 19 ] =~ /[\d.-]+/o ) ) )
      {
      ++$no_mag;
      next;
      }

   $ra  = $line[ 2 ];
   $dec = $line[ 3 ];

   $line[ 0 ] =~ s/ /-/g;

   $ctass_dbh->func(
                     join (
                            "\t",        $count,
                            $line[ 0 ],  $ra,
                            $dec,        $line[ 17 ],
                            $line[ 18 ], $line[ 19 ],
                            $line[ 20 ], $line[ 21 ]
                     ) . "\n",
                     'putline'
                     );

   print "." if !( $count % 1000 );
   printf( "%7d\n", $count ) if !( $count % 70000 );
   ++$count;
   }
close( FILE );

$ctass_dbh->func( "\\.\n", 'putline' );
$ctass_dbh->func( 'endcopy' );
$ctass_dbh->commit;

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

printf "\n%.0f records/second in %d seconds\n",
       $total_count / $elapsed, $elapsed;
print
   "\n$count out of $total_count stars used.  Following rejection counts...\n";
printf "             Short lines: %8d\n", $short_line;
printf "        No position mean: %8d\n", $no_mean;
printf "       Photo-centre mean: %8d\n", $photo_mean;
printf "  Seperated double stars: %8d\n", $sep_double;
printf "Unseperated double stars: %8d\n", $comb_double;
printf "            No magnitude: %8d\n", $no_mag;

$ctass_dbh->disconnect;

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

   return $c;
   }
