#!/bin/tcsh

# Get object fits files

set progname = $0

# Check for usage
if ($#argv < 2 || $1 == "--help") then
    echo " "
    echo Usage: ${progname:t} cat dir
    echo " "
    echo Retrieve object fits files from the MGC database 
    echo for all objects listed in the ASCII catalogue cat
    echo and put them in the directory dir.
    echo " "
    echo Options include:
    echo "\t--help\t\tprint this message"
    echo " "
    exit 1
endif

set dir = $2
set cat = `pwd`/$1

# Test if the target directory already exists
if ( ! { test -d $dir } ) then
    mkdir $dir
endif

# Go to the target directory
cd $dir

# Make a temporary file name and delete it if it already exists
set tmp = ${progname:t}.tmp
rm -f $tmp

# Determine the column of the MGC ID in cat
if ( { test -r ${cat}.par } ) then
    set idcol = `awk '$1 == "ID" {print NR}' ${cat}.par`
else
    set idcol = 1
endif

# Make a file with the URLs
awk -v id=$idcol '{printf "http://www.hs.uni-hamburg.de/jliske/mgc/data/pstamps/MGC%05dB.fit.gz\n", $id}' $cat > $tmp

# Retrieve the object fits files
wget --input-file=$tmp --non-verbose

# Clean up
rm -f $tmp

exit 0
