Debian Codegraph @

conversion/sdt2mnc/sdt2mnc

raw
1 #! /usr/bin/env perl
2 #
3 # Andrew Janke - [email protected]
4 # Center for Magnetic Resonance
5 # The University of Queensland
6 #
7 # Copyright Andrew Janke, The University of Queensland.
8 # Permission to use, copy, modify, and distribute this software and its
9 # documentation for any purpose and without fee is hereby granted,
10 # provided that the above copyright notice appear in all copies. The
11 # author and the University of Queensland make no representations about the
12 # suitability of this software for any purpose. It is provided "as is"
13 # without express or implied warranty.
14 #
15 # Converts a .sdt file to minc
16 #
17 # Mon Oct 20 16:08:45 EDT 2003 - initial versions
18
19 use strict;
20 use warnings "all";
21 use Getopt::Tabular;
22 use File::Basename;
23
24 my($Help, $Usage, $me, @opt_table, $tmpdir, %opt, $history);
25 my(@args, $infile, $inbase, $outfile);
26
27 $me = &basename($0);
28 %opt = (
29 'verbose' => 0,
30 'clobber' => 0,
31 # 'byte_swap' => 0,
32 );
33
34 $Help = <<HELP;
35 | $me converts a (.sdt|.spr) pair of file
36 | (stimulate of CMRR) format images to MINC
37 |
38 | Problems or comments should be sent to: rotor\@cmr.uq.edu.au
39 HELP
40
41 $Usage = "Usage: $me [options] <infile.sdt> <outfile.mnc>\n".
42 " $me -help to list options\n\n";
43
44 @opt_table = (
45 ["-verbose", "boolean", 0, \$opt{'verbose'},
46 "be verbose"],
47 ["-clobber", "boolean", 0, \$opt{'clobber'},
48 "clobber existing files"],
49 # ["-byte_swap", "boolean", 0, \$opt{'byte_swap'},
50 # "input data requires byte swapping"],
51 );
52
53 # check arguments
54 &Getopt::Tabular::SetHelp ($Help, $Usage);
55 &GetOptions (\@opt_table, \@ARGV) || exit 1;
56 die $Usage if ($#ARGV != 1);
57 $infile = $ARGV[0];
58 $outfile = $ARGV[1];
59
60 # check for output file
61 die "$me: $outfile exists, -clobber to overwrite\n\n" if (-e $outfile && !$opt{clobber});
62
63 # get the history string
64 chomp($history = `date`);
65 $history .= '>>>> ' . join(' ', $me, @ARGV);
66
67 # get the input basename
68 $inbase = $infile;
69 $inbase =~ s/\.(sdt|spr)$//;
70
71 # check for input files
72 die "$me: Couldn't find $inbase.spr\n\n" if (!-e "$inbase.spr");
73 die "$me: Couldn't find $inbase.sdt\n\n" if (!-e "$inbase.sdt");
74
75 # Read in the header file
76 my($gen_hdr) = read_sdt_header("$inbase.spr");
77 print STDOUT dump_general_header($gen_hdr) if $opt{verbose};
78
79 # write out the result
80 write_minc($gen_hdr, $outfile, "$inbase.sdt");
81
82
83 ## subroutines ##
84
85 # read in a sdt header (.spr) and return a general header
86 sub read_sdt_header{
87 my($infile) = @_;
88
89 my($gen_hdr) = {};
90 my(%hdr, $ndim, $key, $val);
91
92 my(%sdt_dtypes) = (
93 BYTE => ['-byte', '-signed'],
94 WORD => ['-short', '-signed'],
95 LWORD => ['-int', '-signed'],
96 REAL => ['-float'],
97 COMPLEX => ['-float', '-vector', 2]
98 );
99
100 my(%sdt_orients) = (
101 AX => '-transverse',
102 );
103
104 # first convert the header to a hash
105 foreach (split(/\n/, `cat $infile`)){
106 ($key, $val) = split(/\:/, $_, 2);
107
108 # clean it up a bit
109 $val =~ s/^(\ )*//;
110 $val =~ s/\ \ /\ /g;
111 $val =~ s/(\ )*$//;
112
113 $hdr{$key} = $val;
114 }
115
116 # datatype
117 if(!defined($sdt_dtypes{$hdr{dataType}})) {
118 die "$me: unknown data type: $hdr{dataType}\n\n";
119 }
120 $gen_hdr->{datatype} = $sdt_dtypes{$hdr{dataType}};
121
122 # $gen_hdr->{voxel_min} = $ana_hdr->{glmin};
123 # $gen_hdr->{voxel_max} = $ana_hdr->{glmax};
124 # $gen_hdr->{real_min} = $ana_hdr->{cal_min};
125 # $gen_hdr->{real_max} = $ana_hdr->{cal_max};
126
127 # dimension sizes, steps and starts
128 (@{$gen_hdr->{sizes}}) = split(/\ /, $hdr{dim});
129 (@{$gen_hdr->{starts}}) = split(/\ /, $hdr{origin});
130 (@{$gen_hdr->{steps}}) = split(/\ /, $hdr{interval});
131
132 # orientation
133 if(!defined($sdt_orients{$hdr{sdtOrient}})) {
134 warn "$me: unknown orientation $hdr{sdtOrient}, assuming transverse\n\n";
135 $gen_hdr->{orientation} = '-transverse';
136 }
137 else{
138 $gen_hdr->{orientation} = $sdt_orients{$hdr{sdtOrient}};
139 }
140
141 if(defined($opt{orientation})){
142 warn "$me: overriding file orientation with $opt{orientation}\n";
143 $gen_hdr->{orientation} = $opt{orientation};
144 }
145
146 return $gen_hdr;
147 }
148
149 # return an ASCII dump of a general header
150 sub dump_general_header{
151 my($h) = shift;
152 my($tmp);
153
154 $tmp = "General Header\n";
155 foreach (sort(keys(%{$h}))){
156 if($h->{$_} =~ /ARRAY/){
157 $tmp .= " $_\t<". join(' ' , @{$h->{$_}}) . ">\n";
158 }
159 else{
160 $tmp .= " $_\t<$h->{$_}>\n";
161 }
162 }
163 $tmp .= "\n";
164
165 return $tmp;
166 }
167
168 # write a MINC file from a general header
169 sub write_minc{
170 my($gen_hdr, $outfile, $infile) = @_;
171
172 # Set up rawtominc command
173 my(@args) = ('rawtominc', '-clobber');
174
175 # datatype and ranges
176 push(@args, @{$gen_hdr->{datatype}});
177 # push(@args, '-range', $gen_hdr->{voxel_min}, $gen_hdr->{voxel_max});
178 if($gen_hdr->{real_min} < $gen_hdr->{real_max}){
179 push(@args, '-real_range', $gen_hdr->{real_min}, $gen_hdr->{real_max});
180 }
181 else{
182 push(@args, '-scan_range');
183 }
184
185 # orientation and step information
186 push(@args, $gen_hdr->{orientation},
187 '-xstep', $gen_hdr->{steps}[0],
188 '-ystep', $gen_hdr->{steps}[1],
189 '-zstep', $gen_hdr->{steps}[2],
190 '-xstart', $gen_hdr->{starts}[0],
191 '-ystart', $gen_hdr->{starts}[1],
192 '-zstart', $gen_hdr->{starts}[2]);
193
194 # files and sizes
195 push(@args, '-input', $infile,
196 $outfile,
197 reverse(@{$gen_hdr->{sizes}})
198 );
199
200 # do the conversion
201 &do_cmd(@args);
202
203 # add history string
204 &do_cmd('minc_modify_header',
205 '-sinsert', ":history=$history",
206 $outfile);
207 }
208
209
210 sub do_cmd {
211 print STDERR "@_\n" if $opt{'verbose'};
212 system(@_) == 0 or die;
213 }