#!/bin/csh -f

# Version 1.01
# Modified to handle spaces in attribute names
# J.M. Gales
# 07/10/08

if ( $#argv == 0) then
    echo h5_dumpattr.csh filename attribute_name/attribute_inputfile
    exit
endif

set file=${1}
set attr="${2}"

if (-e "$attr") then
    set attrlist=`cat $attr`
else
    set attrlist="$attr"
endif


@ iattr = 1
while ($iattr <= $#attrlist)
    set attr0="$attrlist[$iattr]"
    set nme=`h5dump -a "$attr0" $file | grep ATTRIBUTE | cut -d '"' -f 2`
    set n=`h5dump -a "$attr0" $file | grep -A 100000 "DATA " | nl | grep "}" | cut -d"}" -f1 | head -1`

    @ n1 = $n + 1
    @ n2 = $n - 2

#echo $n1 $n2

    if ($n2 == 1) then
	set a=`h5dump -a "$attr0" $file | grep -A 1 "DATA " | tail -1`
	set a=`echo $a | sed -e 's/"//g'`

	if ($status == 0) then
	    echo $nme `echo $a | cut -d ":" -f1`=`echo $a | cut -d ":" -f2`
	else
	    echo $nme=$a
	endif
    else
	# Array attribute
	set a=`h5dump -a "$attr0" $file | grep -A 100000 "DATA " | tail -$n1 | head -$n2`
	set a=`echo $a | sed -e 's/"//g'`
	@ i = 0
	while ($i < $n2)
	    @ j1 = $i * 2 + 1
	    @ j2 = $i * 2 + 2
	    echo $nme `echo $a[$j1] | cut -d ":" -f 1`=`echo $a[$j2] | cut -d "," -f 1`
	    @ i++
	end
    endif

    @ iattr++
end

exit
