#!/usr/bin/perl

# SMF2obscat constructs Magellan observing list files from SMF files

$rotoff= -46.15;
print "\nEnter observing catalog name:       ";
$_=<STDIN>;
chop;
open(OUT,">$_");

##
#$_=<STDIN>;
#if(/[0-9]/){
#    $rotoff=$_;}


$n=0;
print "Enter names of SMF files; CR=>done:\n\n";
while(<STDIN>){
    chop;
    last if($_ eq "");
    s/\.SMF//;
    $_=$_.".SMF";
    if(!(-e $_)){
	print "file $_ cannot be found!\n";
	next;}
    open(IN,$_);
    $n++;
    $_=<IN>;
    chop;
    ($a,$name)=split;
    for($i=0;$i<4;$i++){<IN>;}
    $_=<IN>;
    ($a,$instrument)=split;
    if($instrument=~/LDSS3/){
      $offangle=29.7;
      $mode = "EQU";}
    else{
      $offangle=-136.15;
      $mode="OFF";}
    $_=<IN>;
    $_=<IN>;
    ($a,$b,$ra,$dec,$epoch,$angle)=split;
    if($dec>=0){$dec="+".$dec;}
    $dec=~s/(\.\d)\d/\1/;
    $ra=~s/(\.\d\d)\d/\1/;
    $angle=$angle+$offangle;
    if($angle>180.) {$angle-=180;}
if($angle<-180.) {$angle+=180.;}
printf OUT "%3d %10s %s %s %7.2f 0.00 0.00 %7.2f %3s 00:00:00.0 +00:00:00 0000.0 00:00:00.0 +00:00:00 0000.0\n",$n,$name,$ra,$dec,$epoch,$angle,$mode;}

