#!/usr/bin/perl
###############################################################################
# A perl script to get the fits files corresponding to entries in a
# 2QZ catalogue extract from the CD-ROM
#
# written by Scott Croom 08/03/01
#
# SMC 06/06/03 (v1.1) : update to include new catalogue reading routines
#
################################################################################
#                                                                              #
#          LICENCE                                                             #
#                                                                              #
#          This program is free software; you can redistribute it and/or       #
#          modify it under the terms of the GNU General Public License         #
#          as published by the Free Software Foundation; either version 2      #
#          of the License, or (at your option) any later version.              #
#                                                                              #
#          This program is distributed in the hope that it will be useful,     #
#          but WITHOUT ANY WARRANTY; without even the implied warranty of      #
#          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       #
#          GNU General Public License for more details.                        #
#                                                                              #
#          You should have received a copy of the GNU General Public License   #
#          along with this program; if not, write to the Free Software         #
#          Foundation, Inc., 59 Temple Place - Suite 330,                      #
#          Boston, MA  02111-1307, USA.                                        #
#                                                                              #
################################################################################
#
# Pragmas:
#use diagnostics;
#
# Modules:
use Shell qw(cp);
#
# version control:
$version=1.1;
printf ("get_fits.pl version %3.1f\n\n",$version);
# get command line input:
if ($ARGV[0] eq "-h") {
    help();
    exit;
}
if ( scalar(@ARGV) != 3 ) {
    print "Usage: get_fits.pl <catalogue_file> <CD_path> <output_path>\n";
    print "or:    get_fits.pl -h for more help\n";
    exit;
}
else {
    $catfile=$ARGV[0];
    $cdpath=$ARGV[1];
    $outpath=$ARGV[2];
}
# open the catalogue file:
open ( CAT , $catfile );
$nlines=0;
while (  $line = <CAT> ) {
    chomp($line);
    rline($line);
    $nlines++;
    $namea=$iauname."a.fits.gz";
    $dir=sprintf("ra%02d_%02d/",substr($iauname,1,2),substr($iauname,1,2)+1); # sub-directory for data
    $inname=$cdpath."/fits/".$dir.$namea;
    $outname=$outpath."/".$namea;
    if (-e $outname) { # check if the file already exists
	print "$outname already exists\n";
    }
    elsif (not -e $inname) {
	print "$inname doesn\'t exist\n";
    }
    else {
	cp($inname,$outname);
    }
    if ($nobs > 1) { # if there is a second observation get that too
	$nameb=$iauname."b.fits.gz";
	$inname=$cdpath."/fits/".$dir.$nameb;
	$outname=$outpath."/".$nameb;
	if (-e $outname) { # check if the file already exists
	    print "$outname already exists\n";
	}
	elsif (not -e $inname) {
	    print "$inname doesn\'t exist\n";
	}
	else {
	    cp($inname,$outname);
	}
    }
}
close(CAT);

exit;

###############################################################################
# subroutine to print out help information:
sub help {
    print "GET_FITS.PL HELP\n";
    print "------------------\n";
    print "This script allows the user to copy FITS spectrum files from the\n";
    print "2QZ CD-ROM.  The spectra to be copied are specified by inputting\n";
    print "a 2QZ catalogue file which contains entries for all the objects you\n";
    print "require.  This catalogue file will typically be produced using the\n";
    print "2QZ catalogue search tool run via a web browser or the command line\n";
    print "version: cat_search.pl.\n\n";
    print "usage:\n";
    print "get_fits.pl <catalogue_file> <CD_path> <output_path>\n\n";
    print "<catalogue_file> - an extract from the 2QZ catalogue\n";
    print "<CD_path>        - the path to the 2QZ CD, e.g.: /cdrom/cdrom0/\n";
    print "<output_path>    - where to put the FITS files, e.g.: /data/me/fits/\n\n";
    print "example:\n";
    print "get_fits.pl 2QZ.cat /cdrom/cdrom0/ /data/scroom/great_spectra/\n\n";

    return;
}
###############################################################################
# subroutine to read the catalogue entries in a standard format 2QZ catalogue 
# file
sub rline {
    my @tmp=split(/\s+/," ".$_[0]);
    $iauname=$tmp[1];
    $ra1j=$tmp[2];
    $ra2j=$tmp[3];
    $ra3j=$tmp[4];
    $dec1j=$tmp[5];
    $dec2j=$tmp[6];
    $dec3j=$tmp[7];
    $catno=$tmp[8];
    $catname=$tmp[9];
    $sector=$tmp[10];
    $ra1=$tmp[11];
    $ra2=$tmp[12];
    $ra3=$tmp[13];
    $dec1=$tmp[14];
    $dec2=$tmp[15];
    $dec3=$tmp[16];
    $ukstfld=$tmp[17];
    $apmx=$tmp[18];
    $apmy=$tmp[19];
    $rarad=$tmp[20];
    $decrad=$tmp[21];
    $b=$tmp[22];
    $ub=$tmp[23];
    $br=$tmp[24];
    $nobs=$tmp[25];
    $z1=$tmp[26];
    $zq1=$tmp[27];
    $id1=$tmp[28];
    $date1=$tmp[29];
    $fobs1=$tmp[30];
    $fibre1=$tmp[31];
    $sn1=$tmp[32];
    $z2=$tmp[33];
    $zq2=$tmp[34];
    $id2=$tmp[35];
    $date2=$tmp[36];
    $fobs2=$tmp[37];
    $fibre2=$tmp[38];
    $sn2=$tmp[39];
    $zprev=$tmp[40];
    $radio=$tmp[41];
    $xray=$tmp[42];
    $dust=$tmp[43];
    $comment1=$tmp[44];
    $comment2=$tmp[45];
# set up checks for declination sign:
    $dsign=1.0;
    $dsignchar="+";
    if ($dec1 =~ /-/) {
	$dsign=-1;
	$dsignchar="-";
    }
    $dsignj=1.0;
    $dsigncharj="+";
    if ($dec1j =~ /-/) {
	$dsignj=-1;
	$dsigncharj="-";
    }
# remove signs (-/+) from declination:
    $dec1=~s/-//;
    $dec1=~s/\+//;
    $dec1j=~s/-//;
    $dec1j=~s/\+//;
    return;
}

###############################################################################

