|
| 1 | +#! /bin/bash |
| 2 | +# This script is meant to evoke the algorithm without requiring any input arguments |
| 3 | +# |
| 4 | + |
| 5 | +# Define directory names |
| 6 | +FLYWHEEL_BASE=/flywheel/v0 |
| 7 | +MANIFEST_FILE=$FLYWHEEL_BASE/manifest.json |
| 8 | +CONFIG_FILE=$FLYWHEEL_BASE/config.json |
| 9 | +# Base input directory |
| 10 | +INPUT_DIR=$FLYWHEEL_BASE/input |
| 11 | +# Siemens Dat directory |
| 12 | +DAT_DIR=$INPUT_DIR/dat |
| 13 | +# Optional XML and XSL directories |
| 14 | +USER_XML=$INPUT_DIR/user_map |
| 15 | +USER_XSL=$INPUT_DIR/user_stylesheet |
| 16 | +# Output |
| 17 | +OUTPUT_DIR=$FLYWHEEL_BASE/output |
| 18 | + |
| 19 | +CONTAINER='[flywheel/siemens-to-ismrmrd]' |
| 20 | + |
| 21 | +# Configure the ENV according to the original Docker image |
| 22 | +source ${FLYWHEEL_BASE}/docker-env.sh |
| 23 | + |
| 24 | +# Check if the input directory is not empty |
| 25 | +if [[ "$(ls -A $INPUT_DIR)" ]] ; then |
| 26 | + echo "$CONTAINER Starting..." |
| 27 | +else |
| 28 | + echo "Input directory is empty: $INPUT_DIR" |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +# Check if XML directory is present |
| 33 | +if [[ -e $USER_XML ]] ; then |
| 34 | + xml_file=`find $USER_XML -iname '*.xml'` |
| 35 | + if [[ ! -e "$xml_file" ]]; then |
| 36 | + echo "No parameter map (.xml) was found within input directory $USER_XML" |
| 37 | + exit 1 |
| 38 | + fi |
| 39 | + # Replace spaces with _ |
| 40 | + xml_file_nospaces="${xml_file// /_}" |
| 41 | + mv "$xml_file" "$xml_file_nospaces" |
| 42 | +else |
| 43 | + xml_file_nospaces="" |
| 44 | +fi |
| 45 | +# Check if XSL directory is present |
| 46 | +if [[ -e $USER_XSL ]] ; then |
| 47 | + xsl_file=`find $USER_XSL -iname '*.xsl'` |
| 48 | + if [[ ! -e "$xsl_file" ]]; then |
| 49 | + echo "No parameter stylesheet (.xsl) was found within input directory $USER_XSL" |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | + # Replace spaces with _ |
| 53 | + xsl_file_nospaces="${xsl_file// /_}" |
| 54 | + mv "$xsl_file" "$xsl_file_nospaces" |
| 55 | +else |
| 56 | + xsl_file_nospaces="" |
| 57 | +fi |
| 58 | + |
| 59 | +# Find input file in input directory with the extension .dat |
| 60 | +input_file=`find $DAT_DIR -iname '*.dat'` |
| 61 | +bni=`basename "$input_file"` |
| 62 | +filename="${bni%%.*}" |
| 63 | + |
| 64 | +# If input file found |
| 65 | +if [[ ! -e "$input_file" ]] ; then |
| 66 | + echo "No SIEMENS Raw file (.dat) was found within input directory $DAT_DIR" |
| 67 | + exit 1 |
| 68 | +fi |
| 69 | +# Replace spaces with _ |
| 70 | +filename_nospaces=${filename// /_} |
| 71 | +mv "$input_file" $DAT_DIR/$filename_nospaces.dat |
| 72 | + |
| 73 | +# Get output file extension from config file, if it exists |
| 74 | +if [[ -e $CONFIG_FILE ]] ; then |
| 75 | + echo "Config file is present" |
| 76 | + debug=`cat $CONFIG_FILE | jq -r '.config.debug'` |
| 77 | + measNum=`cat $CONFIG_FILE | jq -r '.config.measNum'` |
| 78 | +else |
| 79 | + echo "Config file is not present, using default values" |
| 80 | + debug=`cat $MANIFEST_FILE | jq -r '.config.debug.default'` |
| 81 | + measNum=`cat $MANIFEST_FILE | jq -r '.config.measNum.default'` |
| 82 | +fi |
| 83 | + |
| 84 | +### Sort through different configurations to add to command |
| 85 | +# Define debug command |
| 86 | +if [[ $debug = 'true' ]] ; then |
| 87 | + debug_cmd="--debug" |
| 88 | +else |
| 89 | + debug_cmd="" |
| 90 | +fi |
| 91 | +# Define measNum command |
| 92 | +if [[ $measNum = '1' ]] ; then |
| 93 | + # If measNum is 1, no need to add a command, that is the default |
| 94 | + # or the input file is VB and measNum does not apply |
| 95 | + measNum_cmd="" |
| 96 | +else |
| 97 | + measNum_cmd="--measNum $measNum" |
| 98 | +fi |
| 99 | +# Determine if XML file is given or pMap is defined |
| 100 | +if [[ $xml_file_nospaces ]] ; then |
| 101 | + pMap_cmd="--user-map $xml_file_nospaces" |
| 102 | +else |
| 103 | + pMap_cmd="" |
| 104 | +fi |
| 105 | +# Determine if XSL file is given or pMapStyle is defined |
| 106 | +if [[ $xsl_file_nospaces ]] ; then |
| 107 | + pMapStyle_cmd="--user-stylesheet $xsl_file_nospaces" |
| 108 | +else |
| 109 | + pMapStyle_cmd="" |
| 110 | +fi |
| 111 | + |
| 112 | +## Call siemens_to_ismrmrd |
| 113 | +# cd into output directory incase of debug files being generated |
| 114 | +cd $OUTPUT_DIR && siemens_to_ismrmrd -f $DAT_DIR/$filename_nospaces.dat -o $OUTPUT_DIR/$filename_nospaces.h5 $debug_cmd $measNum_cmd $pMap_cmd $pMapStyle_cmd |
| 115 | + |
| 116 | +# Rename the debug files, if present |
| 117 | +for debugfile in 'config_buffer.xprot' 'processed.xml' 'xml_raw.xml' |
| 118 | +do |
| 119 | + debugfile_full=$OUTPUT_DIR/$debugfile |
| 120 | + if [[ -e $debugfile_full ]] ; then |
| 121 | + mv $debugfile_full $OUTPUT_DIR/"$filename_nospaces"_"$debugfile" |
| 122 | + fi |
| 123 | +done |
| 124 | + |
| 125 | +# Get a list of the files in the output directory |
| 126 | +outputs=`find $OUTPUT_DIR -type f -name "*"` |
| 127 | +# If outputs exist, then go on... |
| 128 | +if [[ -z $outputs ]] ; then |
| 129 | + echo "No results found in output directory... Exiting" |
| 130 | + exit 1 |
| 131 | +else |
| 132 | + chmod -R 777 $OUTPUT_DIR |
| 133 | + echo -e "Wrote: `ls $OUTPUT_DIR`" |
| 134 | +fi |
| 135 | + |
| 136 | +exit 0 |
0 commit comments