#
#
# dataio.tcl - procedures for tranferring information between processes and 
#		scientific datasets
#

#
# get_dims	extract the dimensions from a scientific dataset
#		return the dimensions as a list containing {nrows, ncolumns}
#               Return -1 if the file contains no data.
#
proc get_dims {infile} {
	exec awk {BEGIN{d1 = 0}
	    !/^[a-zA-Z#%;@!]/ && NF>0 {d1++ ; d2 = NF}
	    END{
		if (d1 > 0)
		  print d1,d2
		else
		  print -1
	    }
	} $infile
}

#proc get_dims {infile} {
#    set EOF -1
#    set fp [open $infile r]
#    set nl 0
#    while {[gets $fp s] != $EOF} {
#	if {![IsCommentString $s] && ![IsEmptyString $s]} {
#	    incr nl
#	    set nc [llength $s]
#	    break
#	}
#    }
#    while {[gets $fp s] != $EOF} {
#	if {![IsCommentString $s] && ![IsEmptyString $s]} {
#	    incr nl
#	}
#    }
#    close $fp
#    if [info exists nc] {return "$nl $nc"} else {return -1}
#}

proc IsCommentString {s} {
    return [regexp {^[a-zA-Z#%;@!]} $s]
}

proc IsEmptyString {s} {
    set exp "^\[ \t\]*\$"
    return [regexp $exp $s]
}


#
# save		Save data to a file and report outcome to a window
#
# 	arguments:
#	      in	name of temporary file
#	     out	name of destination file
#	       w	window to write messages to
#
proc save {in out} {
    exec cp $in $out
}

