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

# Update entry in level 0 inventory file.

# Makes this work for Southern Ocean Globec data
#  Oct. 15, 2001  mda
#
# R. Groman February 20, 1997

cd /data/Inventory/scripts

set backup=/data/Inventory/so/backup

set datadir=/data/Inventory/so
#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_so.dat $backup/toplevel_so.dat-$TMP

#Remove existing record
if ( -e /tmp/toplevel_so.dat ) \rm -f /tmp/toplevel_so.dat
set curopt=(`cat inventoryopt`)
#echo "<p>**DEBUG curopt[1]=$curopt[1]"
grep -v $curopt[1] $datadir/toplevel_so.dat > /tmp/toplevel_so.dat

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

#Add current date to comments
echo "# 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_so.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_so.dat

