#!/usr/bin/perl -w

use File::Copy;

$dir = "/home/panisse/www/htdocs/followup/charts/fall_2002/";

$filename = shift;
$filename = $dir.$filename;
$new_filename = $filename.'_keck';
$backup_filename = $filename.'_oldformat';

unless ( -e $filename) {  die "File $filename not found.\n";  }

# First we check to see if this file needs to be changed:
open OLDFILE, $filename;
while($line = <OLDFILE>) {
  if (grep(/Epoch/,$line)) {  
    close OLDFILE;
    print "File $filename already in new format.\nLeaving it untouched.\n";
    exit;
  }
}
close OLDFILE;

## Okay, if we've gotten here then it's an old-style file
# Next we need to read in the file
open OLDFILE, $filename;
open NEWFILE, '>'.$new_filename;
while ($line = <OLDFILE>)  {  
  # Read through until the first line we want;
  if (grep(/Object/, $line)) { last; }

  # Copy the line into the new file.
  printf NEWFILE $line;

}
# We want to output the following two lines next:
#sprintf( );
print NEWFILE sprintf( "%-20s%-14s%-14s%-9s%-7s%-25s%-15s\n",
                       ("Object","RA","Dec","Epoch","Mag",
                        "Offset to Cand (E, N)","Angle(E, N)"));
printf NEWFILE "----------------------------------------------------------------------------------------------------\n";
# Now we read in one more line from OLDFILE and do nothing with it
# It's important that we do assign it to something, like '$line',
# otherwise it won't advance in the file (I don't quite understand why here).
$line = <OLDFILE>;

# The first line doesn't have a magnitude
$line = <OLDFILE>;
chomp $line;
@array = split(/:\s*|\s+/,$line);
$name = $array[0];
  @ra  = @array[1..3];
# Get the sign for the dec
if (substr($array[4],0,1) eq '-')
  {  $decsign = '-';  }  else {  $decsign = '+';  }
# Now get the rest of everything with respect to the element from the end
#  since we're uncertain as to what happened above w.r.t the dec sign
@dec = @array[-3..-1];
# Now that we have the dec sign, we want to make sure that 
# all of the dec values are set to their absolute values
@dec = map{ abs $_ } @dec;

$epoch = 1950.0;
print NEWFILE 
  sprintf( "%-20s%02.2d %02.2d %05.2f   %1s%02.2d %02.2d %04.1f  %7.1f \n",
           ($name, @ra, $decsign, @dec, $epoch) );


# Now read in the name, magnitude, ra, dec and offsets
while ($line = <OLDFILE>) {
  unless (grep(/[a-zA-Z0-9]/,$line) or grep(/Offset/,$line)) {  last;  }
  chomp $line;
  @array = split(/:\s*|\s+/,$line);
  $name = $array[0];
  $mag = $array[1];
  @ra  = @array[2..4];
  # Get the sign for the dec
  if (substr($array[5],0,1) eq '-')
    {  $decsign = '-';  }  else {  $decsign = '+';  }
  # Now get the rest of everything with respect to the element from the end
  #  since we're uncertain as to what happened above w.r.t the dec sign
  @dec = @array[-6..-4];
  $offsetE = $array[-3];
  $offsetN = $array[-2];
  $angle = $array[-1];

  # Now that we have the dec sign, we want to make sure that 
  # all of the dec values are set to their absolute values
  @dec = map{ abs $_ } @dec;

  
  

# Then once we have it we need to dump it back out in the appropriate format
# Epoch is to be set at 1950.0
$epoch = 1950.0;
print NEWFILE 
  sprintf( "%-20s%02.2d %02.2d %05.2f   %1s%02.2d %02.2d %04.1f  %7.1f  %6.2f".
# For now the $offsets were grabbed with the comma at the end so they
#  must be treated as strings and not numbers.
#           "  %8.2f,%8.2f, %14.2f\n",
           "  %9s%9s %14.2f\n",
           ($name, @ra, $decsign, @dec, $epoch, $mag,
            $offsetE, $offsetN, $angle)  );

}

# Now finish off the rest of the file
## First get the last reject line into the new file
print NEWFILE $line;

while(<OLDFILE>) {  print NEWFILE $_;  }
close OLDFILE;
close NEWFILE;


# Now make a backup copy of the file
copy $filename, $backup_filename;
# and then move the new one into its place
move $new_filename, $filename;
