#!/usr/local/bin/perl -w

$ENV{'PATH'}='/bin:/usr/bin:/usr/local/bin:.:/data/cass50a/rgm/bin';

##########################################################################
# apmcat_tng.pl is a Perl5 script that will access
# the apm catalogues at IOA/RGO Cambridge using 
# the asu protocols.
#
#
#
#
# Based on the skyview software:
#
# Calling sequence and parameter information
# can be found at the apmcat_tng web site:
# 
#
# script based on skvbatch.pl from
#    http://skyview.gsfc.nasa.gov/skyview.html
#
#
# $Id: getfirst.pl,v 1.2 1997/01/31 18:36:01 rgm Exp rgm $
# $Log: getfirst.pl,v $
# Revision 1.2  1997/01/31  18:36:01  rgm
# added gif support
#
# Revision 1.1  1997/01/31  18:35:24  rgm
# Initial revision
#
#
#
###########################################################################

print "\n"," running ",$0," from ",`pwd`, "\n";
system ("date 2>&1");

# Loop over the argument list and look to see
# if file has been defined 
$i=0;
foreach (@ARGV) {
   last if (/^file=/i);
   $i++;
}

#print "filename at argv = ",$i," of ",$#ARGV+1,"\n";

# if a filename has been specified, make it the filename
# and splice it out of the argument list
$filename='';
if( $i > 1) {$filename = splice(@ARGV,$i,1);}

# Read in the argument list and put the
# values in quotes

#changed by rgm to point to pskcall_test to get the NVSS
#$new = 'webquery.pl url="/cgi-bin/pskcall" host="skys.gsfc.nasa.gov" ' . '\'' . join('\' \'',@ARGV) . '\'';

#$new = 'webquery.pl url="/nvss-cgi/postage.pl/NVSS.fits" host="www.cv.nrao.edu" ' . '\'' . join('\' \'',@ARGV) . '\'';

#http://third.llnl.gov/cgi-bin/firstcutout

#$new = 'webquery.pl url="/cgi-bin/firstcutout" host="third.llnl.gov" ' . '\'' . join('\' \'',@ARGV)  . '\'' . ".submit=+Extract+the+Cutout+" . '\'';

$new = 'webquery.pl url="/cgi-bin/firstimage" host="third.llnl.gov" ' . '\'' . join('\' \'',@ARGV) . '\'';

print " executing: ",$new,"\n";
print " entering  webquery","\n";

# Exexcute webquery.pl
@arr = `$new`;

print " exited  webquery","\n";

#print @arr,"\n";

# Put all the blocks togehter
$data = join('',@arr);



# Find where the SIMPLE/GIF command begins
if(index($data, "SIMPLE  =")>0) {$locat=index($data, "SIMPLE  =")};
if(index($data, "GIF")>0) {$locat=index($data, "GIF")};

# Either write to a file or standard output
#print " i = ",$i,"\n";
#print "filename = ",$filename,"\n";
#print "length(filename) = ",length($filename),"\n";


if($i > 1) {
if (length($filename)) {
   # get the filename from the argument
   $key='';
   ($key,$truename) = split('=',$filename);

   # Open the file
   open(WWWOUT,">$truename");

   # Print out only from SIMPLE on
   print(WWWOUT substr($data, $locat));
} else {
   # Print to standard outuput
   print substr($data, $locat);
}
}

#print, $data;
#$checksum=unpack("%16C*",substr($data, $locat)) % 65536;
#print," checkum is ",$checksum,"\n";

# And that's all there is






