#! /bin/sh
#++++++++++++++++
#.IDENTIFICATION tofits.sh
#.LANGUAGE       Bourne Shell
#.AUTHOR         CDS Catalogues Service (F. Ochsenbein)
#.ENVIRONMENT    Unix
#.KEYWORDS       FITS
#.VERSION  1.0   18-Apr-1994
#.PURPOSE        Generate the FITS file "models.fits"
#.COMMENTS       This script transforms the set of input files + headers
#			into standard FITS TABLE EXTENSION files
#----------------
#
PATH="/bin:/usr/bin:/usr/ucb"
pgm=`basename $0`
usage="Usage: $0 [> models.fits] "
#
#  Generate the standard SIMPLE header for ascii table
#
if [ -f SIMPLE.fih ]; then
    awk '{print} END { while (NR%36 != 0) { print ""; NR++}}' SIMPLE.fih \
    | dd conv=block cbs=80 bs=2880
else
  today__=`date +%d/%m/%y`
  ( echo "SIMPLE  =                    T / Standard FITS format"; \
    echo "BITPIX  =                    8 / Character information"; \
    echo "NAXIS   =                    0 / No image data array present"; \
    echo "EXTEND  =                    T / There may be standard extensions"; \
    echo "BLOCKED =                    T / The file may be blocked";\
    echo "ORIGIN  = 'CDS     '           / Written at CDS, Strasbourg/France";\
    echo "DATE    = '$today__'           / Date FITS table written (dd/mm/yy)";\
    awk '{print "COMMENT", substr($0,1,72)}' Intro ;\
    echo "END";\
  ) | awk '{print} END { while (NR%36 != 0) { print ""; NR++}}' \
  | dd conv=block cbs=80 bs=2880
fi
#
#   Append the tables. One table is made of  file.fih  and  file
#
awk '{print} END { while (NR%36 != 0) { print ""; NR++}}' models.fih \
| dd conv=block cbs=80 obs=2880		# First, the FITS Header...
lr=`awk '/^NAXIS1 /{print $3}' models.fih`
nr=`awk '/^NAXIS2 /{print $3}' models.fih`
echo "... File models = $nr x $lr bytes "  1>&2
nb=`expr $lr '*' $nr '%' 2880`		# Number of bytes in last block
#
f=z22.dat			# Just an example
if [ -f $f.Z ]; then		# Compressed file exists
    pcat=zcat
else
    if [ -f $f ]; then
	pcat=cat
    else
	echo "****Missing file: $f or $f.Z"  1>&2
	continue
    fi
fi
( $pcat g14.dat e64.dat j63.dat z22.dat z42.dat z62.dat yz42.dat yz62.dat ) \
| awk '/^Track/{Y=$15;Fe=$9;O=$12;M=$6;next}\
	  /^ *Age/{next} \
	{printf("%6s %5s %5s%6s",Y,Fe,O,M);print;}' \
| dd conv=block cbs=$lr obs=2880
# echo "nb=$nb" 1>&2
if [ $nb -ne 0 ]; then
    nb=`expr 2880 - $nb`
    echo " " | dd conv=block cbs=$nb obs=2880
fi
exit 0
