#!/usr/bin/perl
#-------------------------------------------------------------------------------
#Purpose     : creates {arc,bias,flat,object}#####.in from frame information
#Author      : Robert Greimel (greimel@uvastro.phys.uvic.ca,greimel@ing.iac.es)
#Last Change : 21-Feb-2000
#-------------------------------------------------------------------------------
# recognizes : flat, bias, arc, object
########################################################################
###### to search local directory for modules
use FindBin 1.51 qw( $RealBin );
use lib $RealBin;
######


require Astrometry;
use Scripts;
use POSIX;

die("usage: $0 <dir>\n") if ($#ARGV != 0);

$deg2rad = atan2(1,1)*4/180;
$rad2deg = 1/$deg2rad;

# difference in flatfield or arc position in degree
$d_eps = 5;

##
# make sure directory ends with a "/"
##
$dir = $ARGV[0];
if ($dir !~ m|/$|) {$dir = $dir."/"};

##########
#read generation options
##########
%filenames=();
print "$0: reading filename configuration ... ";
&Scripts::read_filenames(\%filenames,"config.names");
print "done\n";

%options=();
print "$0: reading default options ... ";
( &Scripts::read_options(\%options,$filenames{"default_options"}) ) ? print "successful\n" : print "failed\n" ;

print "$0: reading directory specific options ... ";
( &Scripts::read_options(\%options,$dir.$filenames{"custom_options"}) ) ? print "successful\n" : print "failed\n" ;

if ($options{"directory"} eq "yes")
  {
  print "directory option is on\n";
  $dir_inout=$filenames{"dir_inout"}."/";
  if ( ! -e $dir.$dir_inout ) {mkdir $dir.$dir_inout,0755;}
  }
else
  {
  print "directory option is off\n";
  $dir_inout="";
  }

##########
# get RA/DEC information for every frame
##########
print "reading HEADER information ... ";
open HEADER,$dir.$filenames{"header_info"};
while (<HEADER>)
  {
# END
  if ( /^END$/ )
    {
    next;
    }
# filename
  if ( ! /=/)
    {
    chomp;
    $file=$_;
    ($telescope,$year)= ($_ =~ /.*c([0-9]+)_([0-9]+).[0-9]+.*/);
    }
# RA
  if (/^RA      =/)
    {
    $tmp = (split "=",$_)[1];
    $tmp = (split "/",$tmp)[0];
    $tmp =~ s/ *//;
    $tmp =~ s/ *$//;
    $ra{$file}=&Astrometry::RA2Deg( $tmp );
    }
# DEC
  if (/^DEC     =/)
    {
    $tmp = (split "=",$_)[1];
    $tmp = (split "/",$tmp)[0];
    $tmp =~ s/ *//;
    $tmp =~ s/ *$//;
    $dec{$file} = &Astrometry::Dec2Deg ( $tmp );
    }
# UT
  if (/^DATE-OBS=/)
    {
    $tmp = (split "=",$_)[1];
    $tmp = (split "/",$tmp)[0];
    $tmp = (split "T",$tmp)[1];
    $tmp =~ s/ *//;
    $tmp =~ s/ *$//;
    $tmp =~ m/([0-9]{2}).([0-9]{2}).([0-9]{2})/;
    $ut{$file}=$1*3600 + $2*60 + $3;
    }
  }
close HEADER;
print "done\n";

##########
# split according to data type and RA/DEC for flat and arc (due to 1.8m)
##########
$otype="";
open TYPES,$dir.$filenames{"type_file"};
while (<TYPES>)
  {
  $file=(split)[0];
  $type=(split)[2];
  $name=(split)[-1];
  $done=0;
  if ( $type ne $otype ) {$done=1;goto ALLDONE;}
  if ( $type eq "flat" || ($type eq "arc" && $telescope == 182 ) )
    {
    $ca1 = POSIX::cos( $ra{$file}*$deg2rad );
    $sa1 = POSIX::sin( $ra{$file}*$deg2rad );
    $cd1 = POSIX::cos( $dec{$file}*$deg2rad );
    $sd1 = POSIX::sin( $dec{$file}*$deg2rad );
    $ca2 = POSIX::cos( $ra{$ofile}*$deg2rad );
    $sa2 = POSIX::sin( $ra{$ofile}*$deg2rad );
    $cd2 = POSIX::cos( $dec{$ofile}*$deg2rad );
    $sd2 = POSIX::sin( $dec{$ofile}*$deg2rad );

    $d_deg=POSIX::acos( $ca1*$cd1*$ca2*$cd2 + $sa1*$cd1*$sa2*$cd2 + $sd1*$sd2) * $rad2deg;
    if ( $d_deg > $d_eps ) {$done=1;goto ALLDONE;}
    }
  if ( $type eq "flat" || $type eq "arc" || $type eq "bias" )
    {
    $dt = $ut{$file} - $ut{$ofile};
    if ( $dt < 0 ) {$dt += 24*3600;}
    if ( $type eq "arc"  && $dt > $options{"arc-dt"} )  {$done=1;goto ALLDONE;}
    if ( $type eq "bias" && $dt > $options{"bias-dt"} ) {$done=1;goto ALLDONE;}
    if ( $type eq "flat" && $dt > $options{"flat-dt"} ) {$done=1;goto ALLDONE;}
    }
  if ( $otype eq "arc" && $oname ne $name ) {$done=1;goto ALLDONE;}
  ALLDONE:
  if ( $done )
    {
    close OUT;
    $ofile=$dir.$dir_inout.$type.(split /\./,$file)[1].".in";
    if ( $type eq "object" ) {$ofile=$dir.$dir_inout."object.in";}
    print "opening $ofile:\n";
    open OUT,">>$ofile";
    }
  print OUT $file,"\n";
  $otype = $type;
  $ofile = $file;
  $oname = $name;
  }
close OUT;
close TYPES;
