#!/bin/csh -f
# addinventory-level0

# Adds new entry to level 0 inventory file.

# Nov. 13, 2006;  Makes this work for Northeast Pacific data.  mda
# Oct. 15, 2001.  Makes this work for Southern Ocean Data.  mda
# February 23, 1999.  Change update message to reflect master file.  rcg
# R. Groman February 20, 1997
cd /data/Inventory/scripts_nep

set backup=/data/Inventory/nep/backup

set datadir=/data/Inventory/nep
#set datadir=/data/rgroman/Inventory/debug

#Make backup of existing data file just in case
setenv TMP `date '+%Y.%b.%d.%H%M'`
cp $datadir/toplevel_nep.dat $backup/toplevel_nep.dat-$TMP

if ( -e /tmp/toplevel_nep.dat ) \rm -f /tmp/toplevel_nep.dat
cp $datadir/toplevel_nep.dat /tmp/toplevel_nep.dat

#Save comments
if ( -e /tmp/comments ) \rm -f /tmp/comments
head -3 /tmp/toplevel_nep.dat > /tmp/comments

#Add current date to comments
echo "# Master list last updated: $TMP" >> /tmp/comments

#Save existing data with and without field names
if ( -e /tmp/data ) \rm -f /tmp/data
tail +5 /tmp/toplevel_nep.dat > /tmp/data
if ( -e /tmp/data1 ) \rm -f /tmp/data1
tail +4 /tmp/data > /tmp/data1

#Save field name lines
if (-e /tmp/fieldnames ) \rm -f /tmp/fieldnames
head -3 /tmp/data > /tmp/fieldnames



#Add new record (must replace spaces with tabs first) and sort
cat inventoryopt  | tr '\040' '\011' >>/tmp/data1

\rm -f /tmp/data
sort /tmp/data1 > /tmp/data

#Recombine sorted file with comments
cat /tmp/comments /tmp/fieldnames /tmp/data >$datadir/toplevel_nep.dat

