#!/bin/sh
# script merges all the selected cat files found in current directory 
# and sorts them according to frequency.
#
# All available jpl tag numbers were extracted from the arts output
# (arts -r33 with a jpl cat file does the trick) and copies into this 
# file (rather complicated, but I couldn't think of a better way. 
#
# The list tag gives all possible tag numbers, to mark the end of 
# the list, the last entry is 0 !!!!
# Compile a new catalogue by adding or removing jpl tags to/from the 
# tag list.
#
# AvE and OL 01.11.00

# handle ctrl c events:
trap 'echo "Cleaning up."; rm -f $$.tmp; exit' 2


# generate this file
JPLCAT='jpl_catalogue.cat'


# these are all the jpl tag numbers currently allowed in arts, you can get 
# a listing by running arts with a jpl cat file and the option -r 33
# to identify the end of the list, the last entry is 0 !!!!!!!!!!!!
   tag[0]=18003 
   tag[1]=18005 
   tag[2]=20003 
   tag[3]=19003 
   tag[4]=19002 
   tag[5]=21001 
   tag[6]=20001 
   tag[7]=46013 
   tag[8]=45012 
   tag[9]=48004 
  tag[10]=48005 
  tag[11]=48006 
  tag[12]=48007 
  tag[13]=48008 
  tag[14]=50004 
  tag[15]=50006 
  tag[16]=50003 
  tag[17]=50005 
  tag[18]=49002 
  tag[19]=49001 
  tag[20]=44004 
  tag[21]=44009 
  tag[22]=44012 
  tag[23]=45007 
  tag[24]=45008 
  tag[25]=46007 
  tag[26]=28001 
  tag[27]=29001 
  tag[28]=30001 
  tag[29]=29006 
  tag[30]=17003 
  tag[31]=32001 
  tag[32]=32002 
  tag[33]=34001  
  tag[34]=33002 
  tag[35]=30008 
  tag[36]=64002 
  tag[37]=64005 
  tag[38]=66002 
  tag[39]=65001 
  tag[40]=66004 
  tag[41]=46006 
  tag[42]=17002 
  tag[43]=17004 
  tag[44]=18002 
  tag[45]=18004 
  tag[46]=63001 
  tag[47]=63002 
  tag[48]=63003 
  tag[49]=63004 
  tag[50]=63005 
  tag[51]=63006 
  tag[52]=17001 
  tag[53]=19001 
  tag[54]=18001 
  tag[55]=20002 
  tag[56]=21002 
  tag[57]=36001 
  tag[58]=38001 
  tag[59]=37001 
  tag[60]=39004 
  tag[61]=80001 
  tag[62]=82001 
  tag[63]=51002 
  tag[64]=51003 
  tag[65]=53002 
  tag[66]=53006 
  tag[67]=60001 
  tag[68]=62001 
  tag[69]=61001 
  tag[70]=62002 
  tag[71]=30004 
  tag[72]=31002 
  tag[73]=32004 
  tag[74]=31003 
  tag[75]=32006 
  tag[76]=52006 
  tag[77]=54005 
  tag[78]=27001 
  tag[79]=27003 
  tag[80]=28002 
  tag[81]=28003 
  tag[82]=28004 
  tag[83]=50007 
  tag[84]=52009 
  tag[85]=34004 
  tag[86]=34003 
  tag[87]=66001 
  tag[88]=34002 
  tag[89]=35001 
  tag[90]=46005 
  tag[91]=47002 
  tag[92]=47003 
  tag[93]=47004 
  tag[94]=33001 
  tag[95]=16001 
  tag[96]=97002 
  tag[97]=99001 
  tag[98]=30011 
  tag[99]=67001 
 tag[100]=69001 
 tag[101]=95001 
 tag[102]=97001 
 tag[103]=98001 
 tag[104]=102001
 tag[105]=104001
 tag[106]=41001
 tag[107]=42006
 tag[108]=42007
 tag[109]=42001
 tag[110]=42008
 tag[111]=27002
 tag[112]=28005
 tag[113]=28006
 tag[114]=28007
 tag[115]=0


echo 
echo 'Generating merged and sorted catalogue file ': $JPLCAT

# make sure file is not present
if [ -s $JPLCAT ] 
  then
    echo 'Error: File '$JPLCAT' exists!'
    echo 'Please rename or remove before running script.'
    echo 
    exit 1
fi
 
# merge the cat files defined in tag list:
typeset -i i=0
while [ ${tag[$i]} -ne 0 ] 
  do
    # add an additional 0
    if [ ${tag[$i]} -lt 99999 ]
      then
        file='c0'${tag[$i]}'.cat'
    else 
        file='c'${tag[$i]}'.cat'
    fi
    # now merge the files
    cat ${file} >> $$.tmp
    i=i+1
done


# now sort the files according to frequency

# first add a 0 to frequency, if only . is present:
# .001 -> 0.001
# otherwise the sort does not work
sed "s/\(^ *\)\ \./\10\./" $$.tmp | sort +0 -13 -g > $JPLCAT

# remove tmp file
rm -f $$.tmp

echo 'Done'
echo 
