Skip to content

Instantly share code, notes, and snippets.

@xtman
Created August 21, 2012 01:31
Show Gist options
  • Save xtman/3410389 to your computer and use it in GitHub Desktop.
Save xtman/3410389 to your computer and use it in GitHub Desktop.
Calculate the duration of mr studies in Bruker's nmr directory.
#!/bin/bash
function adjust() {
local cwd=$(pwd)
local sd=$1
local total=$2
local mtime1=0
local mtime2=0
local gap=0
cd ${sd}
for ssd in $(ls -t -r -d */)
do
if [[ ! ${ssd} =~ ^[0-9]+ ]]; then
continue
fi
if [[ $mtime1 -eq 0 ]]; then
mtime1=$(stat -c "%Y" ${ssd})
fi
mtime2=$(stat -c "%Y" ${ssd})
gap=$((mtime2-mtime1))
if [[ ${gap} -gt 86400 ]]; then
total=$((total-gap))
fi
mtime1=${mtime2}
done
cd ${cwd}
echo ${total}
}
CWD=$(pwd)
if [[ $(basename ${CWD}) != "nmr" ]]; then
echo "${CWD} is not a valid parent directory for nmr studies." 1>&2
exit 1
fi
DAYS=$1
if [[ -n "$DAYS" ]]; then
NOW=$(date +%s)
START=$((NOW-86400*DAYS))
else
START=0
fi
echo "\"Study Directory\", \"Duration(seconds)\", \"Duration(minutes)\", \"Duration(hours)\", \"Adjusted\","
TOTAL_SECONDS=0
for SDIR in $(ls -d */)
do
cd ${CWD}/${SDIR}
SDIR=$(pwd)
ADJUSTED=false
MIN_MTIME=$(for f in $(ls); do stat -c "%Y" $f; done | sort -n | head -n 1)
MAX_MTIME=$(for f in $(ls); do stat -c "%Y" $f; done | sort -n | tail -n 1)
if [[ ${MIN_MTIME} -lt ${START} ]]; then
continue
fi
SECONDS=$((MAX_MTIME-MIN_MTIME))
if [[ $SECONDS -gt 86400 ]]; then
SECONDS=$(adjust $SDIR $SECONDS)
ADJUSTED=true
fi
MINUTES=$(printf "%5.2f" $(echo "${SECONDS}/60" | bc -l))
HOURS=$(printf "%3.2f" $(echo "${SECONDS}/3600" | bc -l))
TOTAL_SECONDS=$((TOTAL_SECONDS+SECONDS))
echo "\"$(basename ${SDIR})\", ${SECONDS}, ${MINUTES}, ${HOURS}, ${ADJUSTED}"
cd ${CWD}
done
TOTAL_MINUTES=$(printf "%5.2f" $(echo "${TOTAL_SECONDS}/60" | bc -l))
TOTAL_HOURS=$(printf "%3.2f" $(echo "${TOTAL_SECONDS}/3600" | bc -l))
echo "Total, ${TOTAL_SECONDS}, ${TOTAL_MINUTES}, ${TOTAL_HOURS}, ,"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment