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

# Adds new entry to level 1 inventory file.

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

# Passed parameters are:
#	1 == project
#	2 - end == instrument name (or data type) and si name, in pairs


#echo " <p>**DEBUG in routine addinventory-level1"
cd /data/Inventory/scripts

set backup=/data/Inventory/so/backup

#set datadir=/data/rgroman/Inventory
set datadir=/data/Inventory/so

#echo " <p>**DEBUG datadir=$datadir"

set inventorytop=$datadir/toplevel_so.dat

#echo " <p>**DEBUG passed parameter 1 = $1"
#echo " <p>**DEBUG passed parameter 2 = $2"

set numbarg=$#argv
#echo " <p>**DEBUG argv = $argv"
#echo " <p>**DEBUG numbarg= $numbarg"

set current_project=$argv[1]
#echo " <p>**DEBUG current_project = $current_project"

#Get name of data file from inventory file
set existing=(`grep $current_project ${inventorytop}`)
#echo " <p>**DEBUG existing = $existing"
#echo " <p>**DEBUG existing[last] = $existing[${#existing}]"
set level1file=$existing[${#existing}]
#echo " <p>**DEBUG level1file = $level1file"
if ( $level1file == "level2-stub.dat") then
	echo " <p>Error found in addinventory-level1 script.  The level 1"
	echo " file name is $level1file and it should not be.  "
	echo " This is for project $current_project.  Contact"
	echo " the Data Management Office"
	exit 1
endif

#Create level1file (so called level 2) if not exist
if( -e ${datadir}/$level1file ) then
	:
#	echo "<p> The ${datadir}/$level1file level 1 data file "
#	echo "already exists. "
#	echo "<br>The existing file will be used."
else
	cp ${datadir}/level2-stub.dat ${datadir}/$level1file
#	echo "<p> Successfully created the ${datadir}/$level1file "
#	echo "level 1 data file stub."
endif

	
#Make backup of existing data file just in case
setenv TMP `date '+%Y.%b.%d.%H%M'`
cp ${datadir}/$level1file $backup/${level1file}-$TMP
#echo " <p>**DEBUG made backup of level one file"

if ( -e /tmp/$level1file ) \rm -f /tmp/$level1file
cp ${datadir}/$level1file /tmp/$level1file
#echo " <p>**DEBUG made temp version for manipulation purposes"

#Save comments
if ( -e /tmp/comments ) \rm -f /tmp/comments
grep "#" /tmp/$level1file > /tmp/comments
#echo " <p>**DEBUG made temp comments file"

#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
grep -v "#" /tmp/$level1file > /tmp/data
\rm /tmp/$level1file
#echo " <p>**DEBUG extract out data with field names"

#Save any data lines
if ( -e /tmp/data1 ) \rm -f /tmp/data1
tail +3 /tmp/data > /tmp/data1
#echo " <p>**DEBUG extract out data only"

#Save field name lines
if (-e /tmp/fieldnames ) \rm -f /tmp/fieldnames
head -2 /tmp/data > /tmp/fieldnames
#echo " <p>**DEBUG extract out field names"

if ( $numbarg == 1 ) exit
@ counter = 1
foreach datatype ($argv[2-$numbarg])
#	echo " <p>**DEBUG datatype = $datatype"
	@ counter = $counter + 1
	@ skip=$counter % 2
#	echo " <p>**DEBUG counter = $counter and skip = $skip"
	if ( $skip == 1 ) continue
	@ pos = $counter + 1
#	echo " <p>**DEBUG pos = $pos"
	set siname=$argv[$pos]
#	echo " <p>**DEBUG siname = $siname"
	#Create stub datafile (called level3_stub), but really level 2
	if ( ! -e ${datadir}/${current_project}-${datatype}.dat ) then
		cp ${datadir}/level3-stub.dat ${datadir}/${current_project}-${datatype}.dat
#		echo " <br>Created file ${datadir}/${current_project}-${datatype}.dat"
	else
		:
#		echo " <br>File ${datadir}/${current_project}-${datatype}.dat exists."
#		echo " Will use existing version."
	endif
	
	#Add new record to level 1 (must replace spaces with tabs first)
	set record="${datatype} nd nd nd nd nd nd ${siname} ${current_project}-${datatype}.dat"
#	echo " <p>**DEBUG record = $record"
	echo $record  | tr '\040' '\011' >>/tmp/data1


end

# Sort data records before recombining.  Remove duplicates.
\rm -f /tmp/data
sort -u -k 1,1 /tmp/data1 > /tmp/data
#echo " <p>**DEBUG after sort in addinventory-level1"
\rm /tmp/data1

#Recombine sorted file with comments
cat /tmp/comments /tmp/fieldnames /tmp/data >$datadir/$level1file
\rm /tmp/data
\rm /tmp/comments
\rm /tmp/fieldnames
#echo " <p>**DEBUG at end of addinventory-lever1"

