Skip to content

Instantly share code, notes, and snippets.

@meereeum
Last active February 11, 2020 22:57
Show Gist options
  • Save meereeum/87e267dc80421aea50cbb1ce63be5612 to your computer and use it in GitHub Desktop.
Save meereeum/87e267dc80421aea50cbb1ce63be5612 to your computer and use it in GitHub Desktop.
tree + h5ls
#!/bin/bash
usage () {
cat <<HELP_USAGE
usage: h5tree [OPTIONS] file[/OBJECT]
OPTIONS
-h, -?, --help Print a usage message and exit
-L level, -P pattern, etc Any flag accepted by \`tree\`
file/OBJECT
Each object consists of an HDF5 file name optionally followed by a
slash and an object name within the file (if no object is specified
within the file then the contents of the root group are displayed).
The file name may include a printf(3C) integer format such as
"%05d" to open a file family.
HELP_USAGE
}
# 0. print usage IFF no args or --help flag
(( ! $# )) || [[ " $@ " =~ " --help "|" -h "|" -? " ]] && { usage; exit 0; }
# 1. parse arg/s
_H5="${@:$#}" # last arg
FLAGS="${*%${!#}}" # rest of args (`tree` flags)
# H5="$( echo $_H5 | sed -E 's/(\.h5[^/]*).*$/\1/' )" # .h5* file (e.g. includes .h5ad)
# H5="$( echo $_H5 | sed -E 's/(\.[^/]*).*$/\1/' )" # <-- allow file with any .suffix
H5="${_H5/.h5\/*/.h5}" # .h5 file
H5PATH="${_H5/$H5/}" # (optional) path/within/h5
[[ -f "$H5" ]] && {
_HASHED=$( sha1sum "$H5" | awk '{print $1}' )
DIR="/tmp/$_HASHED"
}
# 2. walk $H5 & mirror structure
# (skip if already processed <--> hashed $DIR exists)
[[ ! -d "$DIR" ]] && h5ls -r "$H5" 2>/dev/null | sed -n 's/Dataset //p' | sed 's/\/Inf/+/g' |
while read -r line; do
mkdir -p "$DIR/$line"
done
# 3. tree the shadow directory
# (after asserting $H5 is valid hdf5 & $H5PATH is valid path/to/object)
MAYBE_ERROR=$( h5ls "$H5$H5PATH" 2>&1 | grep -e "\*\*NOT FOUND\*\*" -e "unable to open file" )
[[ $MAYBE_ERROR ]] && {
echo $MAYBE_ERROR
exit 1
} || {
# replace 1st line (/tmp/dir) with actual filename.h5, & skip junk at end
echo "$( basename $H5 )$H5PATH"
tree $FLAGS "$DIR$H5PATH" | tail -n +2 | head -n -2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment