#!/bin/perl -w
#+
# keck2irsa -- generate IRSA finder charts from Keck Starlist
#
# Purpose: 
#       Given a Keck format starlist
#
# Usage:
#       keck2irsa keckstarlist
# 
# Arguments:
#       keckstarlist       = Keck format starlist
# 
# Output:
#       to STDOUT
# 
# Restrictions
#       Unsure, original version was limited to 50 rows
# 
# Exit values:
#        0 = normal completion
#        1 = wrong number of arguments
#
# Example:
#       1) Generate a CSV list from your Keck Starlist
#               keck2irsa keckstarlist
# 
#       2) Create a file for upload at:
#      https://irsa.ipac.caltech.edu/applications/finderchart/
#               keck2irsa keckstarlist > irsa.list
##
# Old URL: http://irsa.ipac.caltech.edu/applications/FinderChart/
#
# 
#-
# Modification history:
#       2009-Jun-02     jlyke   Original W.M. Keck Observatory
#       2019-Sep-06     jlyke   IRSA changed format, requires decimal coords
#-----------------------------------------------------------------------

use File::Basename;

#########################################################################
# notes from IRSA
# http://irsa.ipac.caltech.edu/applications/FinderChart/docs/upload.html
#########################################################################
# Input Paramters
#
#    * "objname"
#
#      The objname can be any name resolvable by NED or SIMBAD. This 
#    field is required but will be overridden if "ra" and "dec" fields 
#    are also present.
#    
#    * "ra", "dec", "csys", "equinox"
#
#      These four fields make up the object string to be parsed by 
#    IRSAs lookup service. "ra" and "dec" can be specified either in 
#    degrees or in sexigesimal; if "csys" and "equinox" are not specified, 
#    they default to "equ" and "j2000". The acceptable "csys" values are 
#    "equ", "ecl", "gal", and "sgal". The acceptable "equinox" values are 
#    "j2000", "b1950". "ra" and "dec" are required only if objname is not 
#    present.
#
#    * "size"
#
#      This is a double precision number specifying the size of the 
#    image cutout in arcmin. This field is optional; it defaults to the 
#    FinderChart main page value which is set to 5.0 arcmin initially.
#
#    * "flip"
#
#      This optional field is either "true" or "false" specifying 
#    whether the image be displayed with East to the right ("true") or 
#    East to the left ("false"). North is always up. The initial default 
#    value is "false".
#
#    * "reproject"
#
#      This optional field takes "true"/"false" value specifying whether 
#    to reproject the image pixels to a uniform scale and orientation. 
#    The initial default value is "false".
#
#    * "grid"
#
#      An optional integer specifying whether to draw grid on the postage 
#    stamp image on the result page. The default is "0" (no grid).
#
#    * "marker"
#
#      An optional integer specifying whether to put a marker at the input 
#    coordinate on the postage stamp image on the result page. The default 
#    is "1" (show marker).
#
#    * "survey"
#
#      This is a string specifying the datasets to be processed. 
#    Datasets names are enclosed in brackets '[' and ']' and separated by 
#    blank space. If this column does not exist, your selections on the 
#    main page are used.
#       
#      Example:  [DSS] [2MASS]
#########################################################################


# declarations...
$| = 1;
my( $obj, $ra, $dec, $eq, $equ);
my( $csys) = 'equ';
my( $imgsz) = '2.0';
my( $flip) = 'false';
my( $proj) = 'false';
my( $grid) = '0';
my( $marker) = '1';
my( $survey) = '[SDSS] [DSS] [2MASS]';

# check args...
die "Usage: $0 keckstarlist\n" if ( @ARGV < 1 );

# Print Header
#printf "objname,ra,dec,csys,equinox,size,flip,reproject,grid,marker,survey\n";
printf "objname,ra,dec,csys,equinox\n";

# Process and print the lines
while(<>){
#    if ( m/^(.{16})\s+(\d+\s+\d+\s+\d+\.\d*)\s+\+?(-?\d+\s+\d+\s+\d+\.?\d*)\s+(\d+)/ ) {
    if ( m/^(.{16})\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) {
	$obj = $1;
	$ra = 15.*($2+($3/60)+($4/3600));
        if ( $5 < 0) {
	  $dec = $5-($6/60)-($7/3600);
        } else {
	  $dec = $5+($6/60)+($7/3600);
        }
	$eq = $8;
	if ( $eq =~ m/1950/){
	    $equ = b1950;
	} else {
	    $equ = j2000;
    }
#    printf "$obj,$ra,$dec,$csys,$equ,$imgsz,$flip,$proj,$grid,$marker,$survey\n";
    printf "$obj,$ra,$dec,$csys,$equ\n";
    } 
}
