Skip to content

Commit

Permalink
Merge pull request #16 from magnostherobot/trh/utility-scripts
Browse files Browse the repository at this point in the history
Implement scripts for mass-processing
  • Loading branch information
sysvinit authored Aug 2, 2018
2 parents cee90c9 + 8b28a7f commit 36ca87d
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 63 deletions.
63 changes: 0 additions & 63 deletions bin/batch.sh

This file was deleted.

126 changes: 126 additions & 0 deletions bin/mass-czinspect.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/bin/bash

set -e

CZINSPECT="czinspect"
JXRCONVERT="mass-jxr-convert.sh -P1"

usage () {
cat<<EOF
Usage: mass-czinspect.sh [operation] <in_folder>
This a wrapper script around a czi manipulation tool, and will pass most options
directly to said tool.
mass-czinspect's options:
-d <dir> Output directory for output(s) of czinspect.
-P Number of maximum concurrent czinspect processes; passed
directly to xargs if used
-v Verbosity of output (default is 0 - silent)
-x Remove .czi files after extraction (caution!)
-t <suffix> Specify output image type (default is '.jxr')
-v Verbosity of output (default is 0 - silent)
czi manipulation tool in use: '$CZINSPECT'
jxr conversion tool in use: '$JXRCONVERT'
czi extraction tool's help text:
---
$($CZINSPECT $opts -h $*)
---
If -t is specified to be anything other than '.jxr', an image conversion tool is
used, limited to one process to allow the concurrency of many mass-czinspect
jobs. In this case, -v and -x are passed to it. Make sure the conversion tool
can convert to the image type you're specifying!
jxr conversion tool's help text:
---
$($JXRCONVERT -h $*)
---
EOF
}

error () {
echo "$@" >&2
exit 1
}

outdir=""
opts=""
xargargs=""
destroy=""
suffix=".jxr"
verbosity=0

while getopts "xv:P:EShd:f:gaes:t:" opt; do
case $opt in
d)
outdir="$OPTARG"
;;
h)
usage
exit 0
;;
E|S|a|e|g)
opts="$opts -$opt"
;;
s|f)
opts="$opts -$opt $OPTARG"
;;
v)
verbosity="$OPTARG"
;;
P)
xargargs="$xargargs -$opt $OPTARG"
;;
x)
destroy="1"
;;
t)
suffix="$OPTARG"
;;
esac
done

shift $((OPTIND - 1))

if (( $# == 0 )); then
error "Missing input directory"
fi

if [[ -z "$outdir" ]]; then
error "Missing output directory"
fi

INDIR="$1"
indirsafe="$(echo $INDIR | sed 's/[[\.*^$,]/\\&/g')"

EXEC="out=\"\$(echo '{}' | sed 's,$indirsafe/*\(.*\).czi\$,$outdir/\1,g')\""

if [[ -n "$verbosity" ]] ; then
EXEC="$EXEC && echo extracting '{}'"
fi

EXEC="$EXEC && mkdir -p \"\$out\""
EXEC="$EXEC && '$CZINSPECT' $opts -d\"\$out\" '{}'"

if [[ -n "$destroy" ]] ; then
EXEC="$EXEC && rm -f '{}'"
fi

if [[ "$suffix" != ".jxr" ]] ; then
jxrcflags="-v \"$verbosity\" -t \"$suffix\""

if [[ -n "$destroy" ]] ; then
jxrcflags="$jxrcflags -x"
fi

if [[ -n "$verbose" ]] ; then
EXEC="$EXEC && echo converting tiles for '{}'"
fi

EXEC="$EXEC && $JXRCONVERT $jxrcflags \"\$out\""
fi

find $INDIR -maxdepth 1 -name '*.czi' | xargs $xargargs -I'{}' sh -c "$EXEC"
114 changes: 114 additions & 0 deletions bin/mass-jxr-convert.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/bin/bash

set -e

JXRDECAPP="JxrDecApp"
CONVERT="vips --vips-concurrency=1 copy"

JXRFMT="-i %s -o %s"

usage () {
cat <<EOF
Usage: deczi.sh [options] <input_dir>
mass-jxr-convert's options:
-d <dir> Output directory (defaults to input directory)
-x Destructive: remove old images after making new ones
-t <suffix> Specify output image type (default is '.tif')
-P <procs> Number of processes to run simultaneously;
passed to xargs if used
-v Verbosity of output (default is 0 - silent)
-h Print this help message
jxr conversion tool in use: '$JXRDECAPP'
general image converter in use: '$CONVERT'
jxr conversion tool's help text:
===
$($JXRDECAPP -h $*)
===
If -t is specified to be anything other than '.tif', a general image conversion
tool is used, limited to one process. MAke sure this converter can convert to
the image type you're specifying!
general image converter's help text:
===
$($CONVERT --help)
===
EOF
}

error () {
echo "$@" >& 2
exit 1
}

OUTDIR=""
DESTROY=""
SUFFIX=".tif"
XARGSARGS=""
VERBOSITY=0

while getopts "d:hxt:P:v:" opt; do
case $opt in
d)
OUTDIR="$OPTARG"
;;
h)
usage
exit 0
;;
x)
DESTROY="1"
;;
t)
SUFFIX="$OPTARG"
;;
P)
XARGSARGS="$XARGSARGS -$opt$OPTARG"
;;
v)
VERBOSITY="$OPTARG"
;;
esac
done

shift $((OPTIND - 1))

if (( $# == 0 )); then
error "Missing input folder"
fi

INDIR=$1
INDIRSAFE="$(echo $INDIR | sed 's/[[\.*^$,]/\\&/g')"

if [[ -z "$OUTDIR" ]]; then
OUTDIR="$INDIR"
fi

EXEC="mid=\"\$(echo '{}' | sed 's,$INDIRSAFE/*\(.*\)\.jxr\$,$OUTDIR/\1,g')\""

if [[ -n "$VERBOSITY" ]]; then
EXEC="$EXEC && echo \"\$mid\"\"{.jxr->.tif}\""
fi

EXEC="$EXEC && $JXRDECAPP \$(printf -- '$JXRFMT' '{}' \"\$mid\"\".tif\")"

if [[ -n "$DESTROY" ]] ; then
EXEC="$EXEC && rm -f '{}'"
fi

if [ "$SUFFIX" != ".tif" ] ; then
if [[ -n "$VERBOSITY" ]] ; then
EXEC="$EXEC && echo \"\$mid\"\"{.tif->$SUFFIX}\""
fi

EXEC="$EXEC && $CONVERT \"\$mid\".tif \"\$mid\"\"$SUFFIX\""

if [[ -n "$DESTROY" ]] ; then
EXEC="$EXEC && rm -f \"\$mid\".tif"
fi
fi

find $INDIR -maxdepth 1 -name '*.jxr' | xargs $XARGSARGS -I'{}' sh -c "$EXEC"

0 comments on commit 36ca87d

Please sign in to comment.