#!/bin/sh
#
# Convert Prelude file to S files
#
# Ideally, the first column in the Prelude file should be a labelling column,
# e.g. year.

FILE=$1
cat $FILE > /tmp/tmp$$
tail +3 < /tmp/tmp$$ | grep -v '\-999	' > $FILE.s
    ( echo '-f	' ; awk 'NR==1{for(i=1;i<=NF;i++)print $i,"REAL",i}' < /tmp/tmp$$) > $FILE.s.des
    Splus extract $FILE.s

sed -n  '3,$s/^\([^	]*\).*$/"\1"/p' < /tmp/tmp$$ > rownames.txt
head -1 /tmp/tmp$$ |
	tr '	' '\012' | sed 's/\(.*\)/"\1"/' > varnames.txt
NCOL=`wc -l < varnames.txt`
rm /tmp/tmp$$
echo 'Now use restore("'$FILE'.s.ext") within Splus, to read the data 
	or use x<-matrix(read("'$FILE'.s"),ncol='$NCOL',byrow=TRUE)
	to read the data as a matrix
	and colnames<-read("varnames.txt") to get the column labels
	for the matrix.
	If the first column contained labels, you can use
	rownames<-read("rownames.txt") to get the row labels.'
