-
Notifications
You must be signed in to change notification settings - Fork 3
/
fix-dicoms.sh
executable file
·156 lines (130 loc) · 6.96 KB
/
fix-dicoms.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env bash
# fix-dicoms corrects issues in converting 2D dicoms to
# multi-dimensional dicoms in DCMULTI
#
# - Michael Eager (michael.eager@monash.edu)
# - Monash Biomedical Imaging
#
#
# Copyright (C) 2014 Michael Eager (michael.eager@monash.edu)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
############################################
RM="/bin/rm -f"
# Check DCMTK on MASSIVE or Agilent console
if test "${MASSIVEUSERNAME+defined}"; then
if [ ! -x "$(which dcmodify)" ];then
module load dcmtk
fi
else
DCMTK="/home/vnmr1/src/dcmtk-3.6.0/bin"
export PATH="${PATH}":"${DCMTK}"
fi
if [ ! -x "$(which dcmodify)" ];then
echo "fix-dicoms.sh: dcmodify not found.";
exit 1
fi
if [ ! -x "$(which dcdump)" ];then
echo "fix-dicoms.sh: dcdump not found.";
exit 1
fi
if [ ! -x "$(which dciodvfy)" ];then
echo "fix-dicoms.sh: dciodvfy not found.";
exit 1
fi
output_dir=$1
MODIFY=1
##COMMON FIXES to enhanced DICOMs
DCMODIFY="dcmodify --no-backup " # --ignore-errors"
# Find dcmulti converted DICOMs - do not descending into tmp directory
files=$(find "${output_dir}" -maxdepth 1 -type f -name "*.dcm")
# "${DCMODIFY}" -m "(0020,0060)=" "${files}" # Laterality # fixed in agilent2dicom
# In-plane phase encoding direction
${DCMODIFY} -i "(5200,9229)[0].(0018,9125)[0].(0018,1312)=ROW" "${files}"
# Transmit Coil Type
# > (0x0018,0x9051) CS Transmit Coil Type VR=<CS> VL=<0x0008> <UNKNOWN >
transcoiltype=$(dcdump "${output_dir}"/tmp/slice001image001echo001.dcm 2>&1 >/dev/null | grep 'Transmit Coil Type' | awk '{print $9}' | tr -d '<>' )
echo "Fixing Receive Coil Type : ${transcoiltype}"
${DCMODIFY} -m "(5200,9229)[0].(0018,9049)[0].(0018,9051)=${transcoiltype}" "${files}"
# Tranmitter Frequency
# > (0x0018,0x9098) FD Transmitter Frequency VR=<FD> VL=<0x0008> {0}
transfrq=$(dcdump "${output_dir}"/tmp/slice001image001echo001.dcm 2>&1 >/dev/null | grep 'Transmitter Frequency' | sed 's/^.*{\(.*\)} *$/\1/' )
echo "Fixing Transmitter Frequency : ${transfrq} "
${DCMODIFY} -m "(5200,9229)[0].(0018,9006)[0].(0018,9098)=${transfrq}" "${files}"
# > (0x0018,0x9042) SQ MR Receive Coil Sequence VR=<SQ> VL=<0xffffffff>
# > (0x0018,0x9043) CS Receive Coil Type VR=<CS> VL=<0x0008> <UNKNOWN >
reccoiltype=$(dcdump "${output_dir}"/tmp/slice001image001echo001.dcm 2>&1 >/dev/null | grep 'Receive Coil Type' | awk '{print $9}' | tr -d '<>' )
echo "Fixing Receive Coil Type : ${reccoiltype} "
${DCMODIFY} -m "(5200,9229)[0].(0018,9042)[0].(0018,9043)=${reccoiltype}" "${files}"
## TODO use ImageType definition rather than filename cine
# if [[ ${output_dir} = *cine* ]] ## pattern match cine in output directory string
# then
# echo "Disabling Frame anatomy modification in CINE";
# MODIFY=0
# ${DCMODIFY} -i "(5200,9229)[0].(0020,9071)[0].(0008,2218)[0].(0008,0102)=SRT" ${files}
# ${DCMODIFY} -i "(5200,9229)[0].(0020,9071)[0].(0008,2228)[0].(0008,0102)=SRT" ${files}
# fi
firsttmpdcm=$(find "${output_dir}"/tmp/ -name "*.dcm" -print0 | head -1 )
echo "${firsttmpdcm}"
# multiple spin echo (0018,9011) - not in diffusion or asl
multspinecho=$(dcdump "${firsttmpdcm}" 2>&1 | grep 'Multiple Spin Echo' | awk '{print $8}' | tr -d '<>')
echo "Fixing Multiple Spin Echo : ${multspinecho} "
${DCMODIFY} -i "(0018,9011)=${multspinecho}" "${files}"
if (( MODIFY == 1 )); then
#"$(dirname $0)/dmodify"
dcdump "${firsttmpdcm}" 2>&1 >/dev/null | grep '(0x0008,0x010' | awk -F'>' '/</ {print $4}'| tr -d '<' > "${output_dir}/anatomy.tmp"
if [ -f "${output_dir}/anatomy.tmp" ]; then
declare -a FrameAnatomySequence
let i=0;while IFS=$'\r\n' read -r line_data; do
FrameAnatomySequence[i]="${line_data}"; ((++i));
done < "${output_dir}/anatomy.tmp"
${RM} "${output_dir}/anatomy.tmp"
else
echo "Cannot find anatomy.tmp in output dir"
fi
echo "Frame Anatomy Seq: ${firsttmpdcm} size: ${#FrameAnatomySequence[*]}"
if [ ${#FrameAnatomySequence[*]} -ne 8 ];then
echo "DCM modification error. Not enough Frame Anatomy Sequence parameters."
echo " Ignoring Anatomy modifications."
else
echo "FrameAnt 7: ${FrameAnatomySequence[7]}"
${DCMODIFY} -i "(5200,9229)[0].(0020,9071)[0].(0008,2218)[0].(0008,0100)=${FrameAnatomySequence[0]}" "${files}"
${DCMODIFY} -i "(5200,9229)[0].(0020,9071)[0].(0008,2218)[0].(0008,0104)=${FrameAnatomySequence[1]}" "${files}" #CodeMeaning=
${DCMODIFY} -i "(5200,9229)[0].(0020,9071)[0].(0008,2218)[0].(0008,0102)=SRT" "${files}"
# ${DCMODIFY} -i "(5200,9229)[0].(0020,9071)[0].(0008,2220)[0].(0008,0100)=${FrameAnatomySequence[2]}" "${files}" #CodeValue=
# ${DCMODIFY} -i "(5200,9229)[0].(0020,9071)[0].(0008,2220)[0].(0008,0104)=${FrameAnatomySequence[3]}" "${files}"
# ${DCMODIFY} -i "(5200,9229)[0].(0020,9071)[0].(0008,2220)[0].(0008,0102)=SRT" "${files}"
${DCMODIFY} -i "(5200,9229)[0].(0020,9071)[0].(0008,2228)[0].(0008,0100)=${FrameAnatomySequence[4]}" "${files}"
${DCMODIFY} -i "(5200,9229)[0].(0020,9071)[0].(0008,2228)[0].(0008,0104)=${FrameAnatomySequence[5]}" "${files}"
${DCMODIFY} -i "(5200,9229)[0].(0020,9071)[0].(0008,2228)[0].(0008,0102)=SRT" "${files}"
# ${DCMODIFY} -i "(5200,9229)[0].(0020,9071)[0].(0008,2230)[0].(0008,0100)=${FrameAnatomySequence[6]}" "${files}"
# ${DCMODIFY} -i "(5200,9229)[0].(0020,9071)[0].(0008,2230)[0].(0008,0104)=${FrameAnatomySequence[7]}" "${files}"
# ${DCMODIFY} -i "(5200,9229)[0].(0020,9071)[0].(0008,2230)[0].(0008,0102)=SRT" "${files}"
${DCMODIFY} -i "(5200,9229)[0].(0020,9071)[0].(0020,9072)=R" "${files}"
#${DCMODIFY} -i "(0018,9125)[0].(0018,1312)=ROW" "${files}"
fi # array check
echo "DCModify done"
fi #debugging modify
echo "Removing Per-frame Anatomy sequences"
index=0
total_anatseq=$(dciodvfy "${output_dir}/0001.dcm" 2>&1 >/dev/null | grep -c -e '^Error - Functional Group Sequence already used in Shared Functional Groups Sequence - (0x0020,0x9071) Frame Anatomy Sequence - in Per-frame Functional Groups Sequence')
echo "Total Frame Anatomy Errors ", "${total_anatseq}"
# current_anatseq=${total_anatseq}
# while (( current_anatseq > 0 )); do
for ((i=0;index<total_anatseq;++index)); do
echo "# ${index} of ${total_anatseq}"
${DCMODIFY} -ea "(5200,9230)[${index}].(0020,9071)" "${files}"
done
# current_anatseq=$(dciodvfy "${output_dir}"/0001.dcm 2>&1 >/dev/null | grep -e '^Error - Functional Group Sequence already used in Shared Functional Groups Sequence - (0x0020,0x9071) Frame Anatomy Sequence - in Per-frame Functional Groups Sequence' | wc -l)
#done