1 |
|
|
/* |
2 |
|
|
Teem: Tools to process and visualize scientific data and images . |
3 |
|
|
Copyright (C) 2013, 2012, 2011, 2010, 2009 University of Chicago |
4 |
|
|
Copyright (C) 2008, 2007, 2006, 2005 Gordon Kindlmann |
5 |
|
|
Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998 University of Utah |
6 |
|
|
|
7 |
|
|
This library is free software; you can redistribute it and/or |
8 |
|
|
modify it under the terms of the GNU Lesser General Public License |
9 |
|
|
(LGPL) as published by the Free Software Foundation; either |
10 |
|
|
version 2.1 of the License, or (at your option) any later version. |
11 |
|
|
The terms of redistributing and/or modifying this software also |
12 |
|
|
include exceptions to the LGPL that facilitate static linking. |
13 |
|
|
|
14 |
|
|
This library is distributed in the hope that it will be useful, |
15 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 |
|
|
Lesser General Public License for more details. |
18 |
|
|
|
19 |
|
|
You should have received a copy of the GNU Lesser General Public License |
20 |
|
|
along with this library; if not, write to Free Software Foundation, Inc., |
21 |
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 |
|
|
*/ |
23 |
|
|
|
24 |
|
|
#include "unrrdu.h" |
25 |
|
|
#include "privateUnrrdu.h" |
26 |
|
|
|
27 |
|
|
/* learned: some header file must declare private functions |
28 |
|
|
** within extern "C" so that others can link with them |
29 |
|
|
*/ |
30 |
|
|
|
31 |
|
|
#define NO_STRING "." |
32 |
|
|
|
33 |
|
|
#define INFO "Create a nrrd (or nrrd header) from scratch" |
34 |
|
|
static const char *_unrrdu_makeInfoL = |
35 |
|
|
(INFO |
36 |
|
|
". The data can be in one or more files, or coming from stdin. " |
37 |
|
|
"This provides an easy way of specifying the information about some " |
38 |
|
|
"data as to wrap it in a NRRD file, either to pass on for further " |
39 |
|
|
"unu processing, or to save to disk. Note that with \"-h\", this creates " |
40 |
|
|
"a detached nrrd header file, without ever reading or writing data files. " |
41 |
|
|
"\n \n " |
42 |
|
|
"When using multiple datafiles, the data from each is simply " |
43 |
|
|
"concatenated in memory (as opposed to interleaving along a faster axis). " |
44 |
|
|
"Keep in mind that all the options below refer to the finished data segment " |
45 |
|
|
"resulting from joining all the data pieces together, " |
46 |
|
|
"except for \"-ls\", \"-bs\", and \"-e\", which apply (uniformly) to the " |
47 |
|
|
"individual data files. Use the \"-fd\" option when the things being joined " |
48 |
|
|
"together are not slices of the final result, but slabs or scanlines. " |
49 |
|
|
"It may be easier to put multiple filenames in a response file; " |
50 |
|
|
"there can be one or more filenames per line of the response file. " |
51 |
|
|
"You can also use a sprintf-style format to identify a numbered " |
52 |
|
|
"range of files, so for example \"-i I.%03d 1 90 1\" " |
53 |
|
|
"refers to I.001, I.002, ... I.090, using the inclusive range from the first " |
54 |
|
|
"to the second integer (following the sprintf-style format), in steps of " |
55 |
|
|
"the third. Can optionally give a fourth integer to serve same role as " |
56 |
|
|
"\"-fd\"." |
57 |
|
|
"\n \n " |
58 |
|
|
"NOTE: for the \"-l\" (labels), \"-u\" (units), and \"-spu\" (space units) " |
59 |
|
|
"options below, you can use a single unquoted period (.) to signify " |
60 |
|
|
"an empty string. This creates a convenient way to convey something that " |
61 |
|
|
"the shell doesn't make it easy to convey. Shell expansion weirdness " |
62 |
|
|
"also requires the use of quotes around the arguments to \"-orig\" (space " |
63 |
|
|
"origin), \"-dirs\" (space directions), and \"-mf\" (measurement frame).\n " |
64 |
|
|
"\n " |
65 |
|
|
"* Uses various components of file and data IO, but currently there is no " |
66 |
|
|
"library function that encapsulates the functionality here."); |
67 |
|
|
|
68 |
|
|
int |
69 |
|
|
unrrdu_makeMain(int argc, const char **argv, const char *me, |
70 |
|
|
hestParm *hparm) { |
71 |
|
2 |
hestOpt *opt = NULL; |
72 |
|
1 |
char *out, *outData, *err, |
73 |
|
|
**dataFileNames, **kvp, *content, encInfo[AIR_STRLEN_LARGE]; |
74 |
|
|
Nrrd *nrrd; |
75 |
|
1 |
size_t *size, bufLen; |
76 |
|
1 |
int headerOnly, pret, lineSkip, endian, type, |
77 |
|
|
encodingType, gotSpacing, gotThickness, space, |
78 |
|
|
spaceSet; |
79 |
|
1 |
long int byteSkip; |
80 |
|
1 |
unsigned int ii, kindsLen, thicknessLen, spacingLen, sizeLen, nameLen, |
81 |
|
|
centeringsLen, unitsLen, labelLen, kvpLen, spunitsLen, dataFileDim, |
82 |
|
|
spaceDim; |
83 |
|
1 |
double *spacing, *thickness; |
84 |
|
|
airArray *mop; |
85 |
|
|
NrrdIoState *nio; |
86 |
|
|
FILE *fileOut; |
87 |
|
1 |
char **label, **units, **spunits, **kinds, **centerings, *parseBuf, |
88 |
|
|
*spcStr, *_origStr, *origStr, *_dirStr, *dirStr, *_mframeStr, *mframeStr; |
89 |
|
|
const NrrdEncoding *encoding; |
90 |
|
|
|
91 |
|
|
/* so that long lists of filenames can be read from file */ |
92 |
|
1 |
hparm->respFileEnable = AIR_TRUE; |
93 |
|
1 |
hparm->greedySingleString = AIR_TRUE; |
94 |
|
|
|
95 |
|
1 |
mop = airMopNew(); |
96 |
|
|
|
97 |
|
1 |
hestOptAdd(&opt, "h", NULL, airTypeBool, 0, 0, &headerOnly, NULL, |
98 |
|
|
"Generate header ONLY: don't write out the whole nrrd, " |
99 |
|
|
"don't even bother reading the input data, just output the " |
100 |
|
|
"detached nrrd header file (usually with a \".nhdr\" " |
101 |
|
|
"extension) determined by the options below. The single " |
102 |
|
|
"constraint is that detached headers are incompatible with " |
103 |
|
|
"using stdin as the data source."); |
104 |
|
1 |
hestOptAdd(&opt, "i,input", "file", airTypeString, 1, -1, |
105 |
|
|
&dataFileNames, "-", |
106 |
|
|
"Filename(s) of data file(s); use \"-\" for stdin. *OR*, can " |
107 |
|
|
"use sprintf-style format for identifying a range of numbered " |
108 |
|
|
"files, see above for details.", &nameLen); |
109 |
|
1 |
hestOptAdd(&opt, "t,type", "type", airTypeEnum, 1, 1, &type, NULL, |
110 |
|
|
"type of data (e.g. \"uchar\", \"int\", \"float\", " |
111 |
|
|
"\"double\", etc.)", |
112 |
|
1 |
NULL, nrrdType); |
113 |
|
1 |
hestOptAdd(&opt, "s,size", "sz0 sz1", airTypeSize_t, 1, -1, &size, NULL, |
114 |
|
|
"number of samples along each axis (and implicit indicator " |
115 |
|
|
"of dimension of nrrd)", &sizeLen); |
116 |
|
1 |
hestOptAdd(&opt, "fd,filedim", "dim", airTypeUInt, 1, 1, |
117 |
|
|
&dataFileDim, "0", |
118 |
|
|
"When using *multiple* input data files (to \"-i\"), what is " |
119 |
|
|
"the dimension of the array data in each individual file. By " |
120 |
|
|
"default (not using this option), this dimension is assumed " |
121 |
|
|
"to be one less than the whole data dimension. "); |
122 |
|
1 |
hestOptAdd(&opt, "sp,spacing", "sp0 sp1", airTypeDouble, 1, -1, |
123 |
|
|
&spacing, "nan", |
124 |
|
|
"spacing between samples on each axis. Use \"nan\" for " |
125 |
|
|
"any non-spatial axes (e.g. spacing between red, green, and blue " |
126 |
|
|
"along axis 0 of interleaved RGB image data)", &spacingLen); |
127 |
|
1 |
hestOptAdd(&opt, "th,thickness", "th0 th1", airTypeDouble, 1, -1, |
128 |
|
|
&thickness, "nan", |
129 |
|
|
"thickness of region represented by one sample along each axis. " |
130 |
|
|
" As with spacing, use \"nan\" for " |
131 |
|
|
"any non-spatial axes.", &thicknessLen); |
132 |
|
1 |
hestOptAdd(&opt, "k,kind", "k0 k1", airTypeString, 1, -1, &kinds, "", |
133 |
|
|
"what \"kind\" is each axis, from the nrrdKind airEnum " |
134 |
|
|
"(e.g. space, time, 3-vector, 3D-masked-symmetric-matrix, " |
135 |
|
|
"or \"none\" to signify no kind)", &kindsLen); |
136 |
|
1 |
hestOptAdd(&opt, "cn,centering", "c0 c1", airTypeString, 1, -1, |
137 |
|
|
¢erings, "", |
138 |
|
|
"kind of centering (node or cell) for each axis, or " |
139 |
|
|
"\"none\" to signify no centering", ¢eringsLen); |
140 |
|
1 |
hestOptAdd(&opt, "l,label", "lb0 lb1", airTypeString, 1, -1, &label, "", |
141 |
|
|
"short string labels for each of the axes", &labelLen); |
142 |
|
1 |
hestOptAdd(&opt, "u,unit", "un0 un1", airTypeString, 1, -1, &units, "", |
143 |
|
|
"short strings giving units for each of the axes", &unitsLen); |
144 |
|
1 |
hestOptAdd(&opt, "c,content", "content", airTypeString, 1, 1, &content, "", |
145 |
|
|
"Specifies the content string of the nrrd, which is built upon " |
146 |
|
|
"by many nrrd function to record a history of operations"); |
147 |
|
1 |
hestOptAdd(&opt, "ls,lineskip", "num", airTypeInt, 1, 1, &lineSkip, "0", |
148 |
|
|
"number of ascii lines to skip before reading data"); |
149 |
|
1 |
hestOptAdd(&opt, "bs,byteskip", "num", airTypeLongInt, 1, 1, &byteSkip, "0", |
150 |
|
|
"number of bytes to skip (after skipping ascii lines, if any) " |
151 |
|
|
"before reading data. Can use \"-bs -1\" to skip a binary " |
152 |
|
|
"header of unknown length in raw-encoded data"); |
153 |
|
1 |
strcpy(encInfo, |
154 |
|
|
"encoding of input data. Possibilities include:" |
155 |
|
|
"\n \b\bo \"raw\": raw encoding" |
156 |
|
|
"\n \b\bo \"ascii\": ascii values, one scanline per line of text, " |
157 |
|
|
"values within line are delimited by space, tab, or comma" |
158 |
|
|
"\n \b\bo \"hex\": two hex digits per byte"); |
159 |
✓✗ |
1 |
if (nrrdEncodingGzip->available()) { |
160 |
|
1 |
strcat(encInfo, |
161 |
|
|
"\n \b\bo \"gzip\", \"gz\": gzip compressed raw data"); |
162 |
|
1 |
} |
163 |
✓✗ |
1 |
if (nrrdEncodingBzip2->available()) { |
164 |
|
1 |
strcat(encInfo, |
165 |
|
|
"\n \b\bo \"bzip2\", \"bz2\": bzip2 compressed raw data"); |
166 |
|
1 |
} |
167 |
|
1 |
hestOptAdd(&opt, "e,encoding", "enc", airTypeEnum, 1, 1, |
168 |
|
|
&encodingType, "raw", |
169 |
|
1 |
encInfo, NULL, nrrdEncodingType); |
170 |
|
1 |
hestOptAdd(&opt, "en,endian", "end", airTypeEnum, 1, 1, &endian, |
171 |
|
1 |
airEnumStr(airEndian, airMyEndian()), |
172 |
|
|
"Endianness of data; relevent for any data with value " |
173 |
|
|
"representation bigger than 8 bits, with a non-ascii encoding: " |
174 |
|
|
"\"little\" for Intel and friends " |
175 |
|
|
"(least significant byte first, at lower address); " |
176 |
|
|
"\"big\" for everyone else (most significant byte first). " |
177 |
|
|
"Defaults to endianness of this machine", |
178 |
|
1 |
NULL, airEndian); |
179 |
|
1 |
hestOptAdd(&opt, "kv,keyvalue", "key/val", airTypeString, 1, -1, &kvp, "", |
180 |
|
|
"key/value string pairs to be stored in nrrd. Each key/value " |
181 |
|
|
"pair must be a single string (put it in \"\"s " |
182 |
|
|
"if the key or the value contain spaces). The format of each " |
183 |
|
|
"pair is \"<key>:=<value>\", with no spaces before or after " |
184 |
|
|
"\":=\".", &kvpLen); |
185 |
|
1 |
hestOptAdd(&opt, "spc,space", "space", airTypeString, 1, 1, &spcStr, "", |
186 |
|
|
"identify the space (e.g. \"RAS\", \"LPS\") in which the array " |
187 |
|
|
"conceptually lives, from the nrrdSpace airEnum, which in turn " |
188 |
|
|
"determines the dimension of the space. Or, use an integer>0 to" |
189 |
|
|
"give the dimension of a space that nrrdSpace doesn't know about. " |
190 |
|
|
"By default (not using this option), the enclosing space is " |
191 |
|
|
"set as unknown."); |
192 |
|
1 |
hestOptAdd(&opt, "orig,origin", "origin", airTypeString, 1, 1, &_origStr, "", |
193 |
|
|
"(NOTE: must quote vector) the origin in space of the array: " |
194 |
|
|
"the location of the center " |
195 |
|
|
"of the first sample, of the form \"(x,y,z)\" (or however " |
196 |
|
|
"many coefficients are needed for the chosen space). Quoting the " |
197 |
|
|
"vector is needed to stop interpretation from the shell"); |
198 |
|
1 |
hestOptAdd(&opt, "dirs,directions", "v0 v1 ...", airTypeString, 1, 1, |
199 |
|
|
&_dirStr, "", |
200 |
|
|
"(NOTE: must quote whole vector list) The \"space directions\": " |
201 |
|
|
"the vectors in space spanned by incrementing (by one) each " |
202 |
|
|
"axis index (the column vectors of the index-to-world " |
203 |
|
|
"matrix transform), OR, \"none\" for non-spatial axes. Give " |
204 |
|
|
"one vector per axis. (Quoting around whole vector list, not " |
205 |
|
|
"individually, is needed because of limitations in the parser)"); |
206 |
|
1 |
hestOptAdd(&opt, "mf,measurementframe", "v0 v1 ...", airTypeString, 1, 1, |
207 |
|
|
&_mframeStr, "", |
208 |
|
|
"(NOTE: must quote whole vector list). Each vector is a *column* " |
209 |
|
|
"vector of the matrix which transforms from coordinates in " |
210 |
|
|
"measurement frame (in which the coefficients of vectors and " |
211 |
|
|
"tensors are given) to coordinates of world space (given with " |
212 |
|
|
"\"-spc\"). This is not a per-axis field: the column vectors " |
213 |
|
|
"comprise a D-by-D square matrix, where D is the dimension of " |
214 |
|
|
"world space."); |
215 |
|
1 |
hestOptAdd(&opt, "spu,spaceunit", "su0 su1", airTypeString, 1, -1, |
216 |
|
|
&spunits, "", |
217 |
|
|
"short strings giving units with which the coefficients of the " |
218 |
|
|
"space origin and direction vectors are measured.", &spunitsLen); |
219 |
|
1 |
hestOptAdd(&opt, "o,output", "nout", airTypeString, 1, 1, &out, "-", |
220 |
|
|
"output filename. If \"-h\" has been used, the output file is " |
221 |
|
|
"always a detached header. Otherwise, use extension " |
222 |
|
|
"\".nrrd\" to signal creation of self-contained nrrd, and " |
223 |
|
|
"\".nhdr\" to signal creating of a detached header with " |
224 |
|
|
"(single) data file."); |
225 |
|
1 |
hestOptAdd(&opt, "od,outputdata", "name", airTypeString, 1, 1, &outData, "", |
226 |
|
|
"when *not* using \"-h\" and saving to a \".nhdr\" file, using " |
227 |
|
|
"this option allows you to explicitly name the data file, " |
228 |
|
|
"instead of (by default, not using this option) having it be " |
229 |
|
|
"the same filename base as the header file."); |
230 |
|
1 |
airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways); |
231 |
|
|
|
232 |
✓✗ |
2 |
USAGE(_unrrdu_makeInfoL); |
233 |
|
|
airStrtokQuoting = AIR_TRUE; |
234 |
|
|
PARSE(); |
235 |
|
|
airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways); |
236 |
|
|
encoding = nrrdEncodingArray[encodingType]; |
237 |
|
|
|
238 |
|
|
/* ---------------- BEGIN ERROR CHECKING --------------- */ |
239 |
|
|
|
240 |
|
|
if (headerOnly) { |
241 |
|
|
for (ii=0; ii<nameLen; ii++) { |
242 |
|
|
if (!strcmp("-", dataFileNames[ii])) { |
243 |
|
|
fprintf(stderr, "%s: can't use detached headers (\"-h\") " |
244 |
|
|
"with stdin (\"-\") as data source " |
245 |
|
|
"(filename %d of %d)\n", me, ii+1, nameLen); |
246 |
|
|
airMopError(mop); return 1; |
247 |
|
|
} |
248 |
|
|
} |
249 |
|
|
} |
250 |
|
|
/* given the information we have, we set the fields in the nrrdIoState |
251 |
|
|
so as to simulate having read the information from a header */ |
252 |
|
|
if (!( AIR_IN_CL(1, sizeLen, NRRD_DIM_MAX) )) { |
253 |
|
|
fprintf(stderr, "%s: # axis sizes (%d) not in valid nrrd dimension " |
254 |
|
|
"range [1,NRRD_DIM_MAX] = [1,%d]\n", me, sizeLen, NRRD_DIM_MAX); |
255 |
|
|
airMopError(mop); |
256 |
|
|
return 1; |
257 |
|
|
} |
258 |
|
|
gotSpacing = (spacingLen > 1 || |
259 |
|
|
(sizeLen == 1 && AIR_EXISTS(spacing[0]))); |
260 |
|
|
if (gotSpacing && spacingLen != sizeLen) { |
261 |
|
|
fprintf(stderr, |
262 |
|
|
"%s: number of spacings (%d) not same as dimension (%d)\n", |
263 |
|
|
me, spacingLen, sizeLen); |
264 |
|
|
airMopError(mop); |
265 |
|
|
return 1; |
266 |
|
|
} |
267 |
|
|
gotThickness = (thicknessLen > 1 || |
268 |
|
|
(sizeLen == 1 && AIR_EXISTS(thickness[0]))); |
269 |
|
|
if (gotThickness && thicknessLen != sizeLen) { |
270 |
|
|
fprintf(stderr, |
271 |
|
|
"%s: number of thicknesses (%d) not same as dimension (%d)\n", |
272 |
|
|
me, thicknessLen, sizeLen); |
273 |
|
|
airMopError(mop); |
274 |
|
|
return 1; |
275 |
|
|
} |
276 |
|
|
if (airStrlen(label[0]) && sizeLen != labelLen) { |
277 |
|
|
fprintf(stderr, |
278 |
|
|
"%s: number of labels (%d) not same as dimension (%d)\n", |
279 |
|
|
me, labelLen, sizeLen); |
280 |
|
|
airMopError(mop); |
281 |
|
|
return 1; |
282 |
|
|
} |
283 |
|
|
if (airStrlen(units[0]) && sizeLen != unitsLen) { |
284 |
|
|
fprintf(stderr, |
285 |
|
|
"%s: number of units (%d) not same as dimension (%d)\n", |
286 |
|
|
me, unitsLen, sizeLen); |
287 |
|
|
airMopError(mop); |
288 |
|
|
return 1; |
289 |
|
|
} |
290 |
|
|
if (airStrlen(kinds[0]) && sizeLen != kindsLen) { |
291 |
|
|
fprintf(stderr, |
292 |
|
|
"%s: number of kinds (%d) not same as dimension (%d)\n", |
293 |
|
|
me, kindsLen, sizeLen); |
294 |
|
|
airMopError(mop); |
295 |
|
|
return 1; |
296 |
|
|
} |
297 |
|
|
if (airStrlen(centerings[0]) && sizeLen != centeringsLen) { |
298 |
|
|
fprintf(stderr, |
299 |
|
|
"%s: number of centerings (%d) not same as dimension (%d)\n", |
300 |
|
|
me, centeringsLen, sizeLen); |
301 |
|
|
airMopError(mop); |
302 |
|
|
return 1; |
303 |
|
|
} |
304 |
|
|
|
305 |
|
|
/* ----------------- END ERROR CHECKING ---------------- */ |
306 |
|
|
/* ----------------- BEGIN SETTING INFO ---------------- */ |
307 |
|
|
|
308 |
|
|
nio = nrrdIoStateNew(); |
309 |
|
|
airMopAdd(mop, nio, (airMopper)nrrdIoStateNix, airMopAlways); |
310 |
|
|
nrrd = nrrdNew(); |
311 |
|
|
airMopAdd(mop, nrrd, (airMopper)nrrdNuke, airMopAlways); |
312 |
|
|
|
313 |
|
|
nrrd->type = type; |
314 |
|
|
nrrd->dim = sizeLen; |
315 |
|
|
nrrdAxisInfoSet_nva(nrrd, nrrdAxisInfoSize, size); |
316 |
|
|
/* have to simulate having parsed this line for error checking in |
317 |
|
|
_nrrdDataFNCheck() to not cause problems */ |
318 |
|
|
nio->seen[nrrdField_sizes] = AIR_TRUE; |
319 |
|
|
if (_nrrdContainsPercentThisAndMore(dataFileNames[0], 'd')) { |
320 |
|
|
/* trying to do a formatted filename list */ |
321 |
|
|
if (nameLen < 4 || nameLen > 5) { |
322 |
|
|
fprintf(stderr, "%s: formatted list of filenames needs between " |
323 |
|
|
"3 and 4 ints after the format (not %d)\n", me, nameLen-1); |
324 |
|
|
airMopError(mop); |
325 |
|
|
return 1; |
326 |
|
|
} |
327 |
|
|
bufLen = 0; |
328 |
|
|
for (ii=0; ii<nameLen; ii++) { |
329 |
|
|
bufLen += strlen(dataFileNames[ii]) + 1; |
330 |
|
|
} |
331 |
|
|
parseBuf = AIR_CALLOC(bufLen+1, char); |
332 |
|
|
airMopAdd(mop, parseBuf, airFree, airMopAlways); |
333 |
|
|
strcpy(parseBuf, ""); |
334 |
|
|
for (ii=0; ii<nameLen; ii++) { |
335 |
|
|
if (ii) { |
336 |
|
|
strcat(parseBuf, " "); |
337 |
|
|
} |
338 |
|
|
strcat(parseBuf, dataFileNames[ii]); |
339 |
|
|
} |
340 |
|
|
nio->line = parseBuf; |
341 |
|
|
nio->pos = 0; |
342 |
|
|
if (nrrdFieldInfoParse[nrrdField_data_file](NULL, nrrd, |
343 |
|
|
nio, AIR_TRUE)) { |
344 |
|
|
airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); |
345 |
|
|
fprintf(stderr, "%s: trouble with formatted filename list \"%s\":\n%s", |
346 |
|
|
me, parseBuf, err); |
347 |
|
|
nio->line = NULL; airMopError(mop); return 1; |
348 |
|
|
} |
349 |
|
|
nio->line = NULL; |
350 |
|
|
} else { |
351 |
|
|
/* single or regular LIST of files */ |
352 |
|
|
if (nameLen > 1) { |
353 |
|
|
nio->dataFileDim = dataFileDim ? dataFileDim : nrrd->dim-1; |
354 |
|
|
} else { |
355 |
|
|
nio->dataFileDim = nrrd->dim; |
356 |
|
|
} |
357 |
|
|
airArrayLenSet(nio->dataFNArr, nameLen); |
358 |
|
|
for (ii=0; ii<nameLen; ii++) { |
359 |
|
|
nio->dataFN[ii] = airStrdup(dataFileNames[ii]); |
360 |
|
|
} |
361 |
|
|
} |
362 |
|
|
if (_nrrdDataFNCheck(nio, nrrd, AIR_TRUE)) { |
363 |
|
|
airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); |
364 |
|
|
fprintf(stderr, "%s: trouble with input datafiles:\n%s", me, err); |
365 |
|
|
airMopError(mop); return 1; |
366 |
|
|
} |
367 |
|
|
|
368 |
|
|
if (gotSpacing) { |
369 |
|
|
nrrdAxisInfoSet_nva(nrrd, nrrdAxisInfoSpacing, spacing); |
370 |
|
|
} |
371 |
|
|
if (gotThickness) { |
372 |
|
|
nrrdAxisInfoSet_nva(nrrd, nrrdAxisInfoThickness, thickness); |
373 |
|
|
} |
374 |
|
|
if (airStrlen(label[0])) { |
375 |
|
|
for (ii=0; ii<nrrd->dim; ii++) { |
376 |
|
|
if (!strcmp(NO_STRING, label[ii])) { |
377 |
|
|
strcpy(label[ii], ""); |
378 |
|
|
} |
379 |
|
|
} |
380 |
|
|
nrrdAxisInfoSet_nva(nrrd, nrrdAxisInfoLabel, label); |
381 |
|
|
} |
382 |
|
|
if (airStrlen(units[0])) { |
383 |
|
|
for (ii=0; ii<nrrd->dim; ii++) { |
384 |
|
|
if (!strcmp(NO_STRING, units[ii])) { |
385 |
|
|
strcpy(units[ii], ""); |
386 |
|
|
} |
387 |
|
|
} |
388 |
|
|
nrrdAxisInfoSet_nva(nrrd, nrrdAxisInfoUnits, units); |
389 |
|
|
} |
390 |
|
|
if (airStrlen(content)) { |
391 |
|
|
nrrd->content = airStrdup(content); |
392 |
|
|
} |
393 |
|
|
if (kvpLen) { |
394 |
|
|
for (ii=0; ii<kvpLen; ii++) { |
395 |
|
|
/* a hack: have to use NrrdIoState->line as the channel to communicate |
396 |
|
|
the key/value pair, since we have to emulate it having been |
397 |
|
|
read from a NRRD header. But because nio doesn't own the |
398 |
|
|
memory, we must be careful to unset the pointer prior to |
399 |
|
|
NrrdIoStateNix being called by the mop. */ |
400 |
|
|
nio->line = kvp[ii]; |
401 |
|
|
nio->pos = 0; |
402 |
|
|
if (nrrdFieldInfoParse[nrrdField_keyvalue](NULL, nrrd, |
403 |
|
|
nio, AIR_TRUE)) { |
404 |
|
|
airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); |
405 |
|
|
fprintf(stderr, "%s: trouble with key/value %d \"%s\":\n%s", |
406 |
|
|
me, ii, kvp[ii], err); |
407 |
|
|
nio->line = NULL; airMopError(mop); return 1; |
408 |
|
|
} |
409 |
|
|
nio->line = NULL; |
410 |
|
|
} |
411 |
|
|
} |
412 |
|
|
if (airStrlen(kinds[0])) { |
413 |
|
|
/* have to allocate line then pass it to parsing */ |
414 |
|
|
bufLen = 0; |
415 |
|
|
for (ii=0; ii<sizeLen; ii++) { |
416 |
|
|
bufLen += airStrlen(" ") + airStrlen(kinds[ii]); |
417 |
|
|
} |
418 |
|
|
parseBuf = AIR_CALLOC(bufLen+1, char); |
419 |
|
|
airMopAdd(mop, parseBuf, airFree, airMopAlways); |
420 |
|
|
strcpy(parseBuf, ""); |
421 |
|
|
for (ii=0; ii<sizeLen; ii++) { |
422 |
|
|
if (ii) { |
423 |
|
|
strcat(parseBuf, " "); |
424 |
|
|
} |
425 |
|
|
strcat(parseBuf, kinds[ii]); |
426 |
|
|
} |
427 |
|
|
nio->line = parseBuf; |
428 |
|
|
nio->pos = 0; |
429 |
|
|
if (nrrdFieldInfoParse[nrrdField_kinds](NULL, nrrd, |
430 |
|
|
nio, AIR_TRUE)) { |
431 |
|
|
airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); |
432 |
|
|
fprintf(stderr, "%s: trouble with kinds \"%s\":\n%s", |
433 |
|
|
me, parseBuf, err); |
434 |
|
|
nio->line = NULL; airMopError(mop); return 1; |
435 |
|
|
} |
436 |
|
|
nio->line = NULL; |
437 |
|
|
} |
438 |
|
|
if (airStrlen(centerings[0])) { |
439 |
|
|
/* have to allocate line then pass it to parsing */ |
440 |
|
|
bufLen = 0; |
441 |
|
|
for (ii=0; ii<sizeLen; ii++) { |
442 |
|
|
bufLen += airStrlen(" ") + airStrlen(centerings[ii]); |
443 |
|
|
} |
444 |
|
|
parseBuf = AIR_CALLOC(bufLen+1, char); |
445 |
|
|
airMopAdd(mop, parseBuf, airFree, airMopAlways); |
446 |
|
|
strcpy(parseBuf, ""); |
447 |
|
|
for (ii=0; ii<sizeLen; ii++) { |
448 |
|
|
if (ii) { |
449 |
|
|
strcat(parseBuf, " "); |
450 |
|
|
} |
451 |
|
|
strcat(parseBuf, centerings[ii]); |
452 |
|
|
} |
453 |
|
|
nio->line = parseBuf; |
454 |
|
|
nio->pos = 0; |
455 |
|
|
if (nrrdFieldInfoParse[nrrdField_centers](NULL, nrrd, |
456 |
|
|
nio, AIR_TRUE)) { |
457 |
|
|
airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); |
458 |
|
|
fprintf(stderr, "%s: trouble with centerings \"%s\":\n%s", |
459 |
|
|
me, parseBuf, err); |
460 |
|
|
nio->line = NULL; airMopError(mop); return 1; |
461 |
|
|
} |
462 |
|
|
nio->line = NULL; |
463 |
|
|
} |
464 |
|
|
if (airStrlen(spcStr)) { |
465 |
|
|
space = airEnumVal(nrrdSpace, spcStr); |
466 |
|
|
if (!space) { |
467 |
|
|
/* couldn't parse it as space, perhaps its a uint */ |
468 |
|
|
if (1 != sscanf(spcStr, "%u", &spaceDim)) { |
469 |
|
|
fprintf(stderr, "%s: couldn't parse \"%s\" as a nrrdSpace " |
470 |
|
|
"or as a uint", me, spcStr); |
471 |
|
|
airMopError(mop); return 1; |
472 |
|
|
} |
473 |
|
|
/* else we did parse it as a uint */ |
474 |
|
|
nrrd->space = nrrdSpaceUnknown; |
475 |
|
|
nrrd->spaceDim = spaceDim; |
476 |
|
|
} else { |
477 |
|
|
/* we did parse a known space */ |
478 |
|
|
nrrdSpaceSet(nrrd, space); |
479 |
|
|
} |
480 |
|
|
spaceSet = AIR_TRUE; |
481 |
|
|
} else { |
482 |
|
|
/* we got no space information at all */ |
483 |
|
|
nrrdSpaceSet(nrrd, nrrdSpaceUnknown); |
484 |
|
|
spaceSet = AIR_FALSE; |
485 |
|
|
} |
486 |
|
|
if (airStrlen(_origStr)) { |
487 |
|
|
/* why this is necessary is a bit confusing to me, both the check for |
488 |
|
|
enclosing quotes, and the need to use to a separate variable (isn't |
489 |
|
|
hest doing memory management of addresses, not variables?) */ |
490 |
|
|
if ('\"' == _origStr[0] && '\"' == _origStr[strlen(_origStr)-1]) { |
491 |
|
|
_origStr[strlen(_origStr)-1] = 0; |
492 |
|
|
origStr = _origStr + 1; |
493 |
|
|
} else { |
494 |
|
|
origStr = _origStr; |
495 |
|
|
} |
496 |
|
|
/* same hack about using NrrdIoState->line as basis for parsing */ |
497 |
|
|
nio->line = origStr; |
498 |
|
|
nio->pos = 0; |
499 |
|
|
if (nrrdFieldInfoParse[nrrdField_space_origin](NULL, nrrd, |
500 |
|
|
nio, AIR_TRUE)) { |
501 |
|
|
airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); |
502 |
|
|
fprintf(stderr, "%s: trouble with origin \"%s\":\n%s", |
503 |
|
|
me, origStr, err); |
504 |
|
|
nio->line = NULL; airMopError(mop); return 1; |
505 |
|
|
} |
506 |
|
|
nio->line = NULL; |
507 |
|
|
} |
508 |
|
|
if (airStrlen(_dirStr)) { |
509 |
|
|
/* same confusion as above */ |
510 |
|
|
if ('\"' == _dirStr[0] && '\"' == _dirStr[strlen(_dirStr)-1]) { |
511 |
|
|
_dirStr[strlen(_dirStr)-1] = 0; |
512 |
|
|
dirStr = _dirStr + 1; |
513 |
|
|
} else { |
514 |
|
|
dirStr = _dirStr; |
515 |
|
|
} |
516 |
|
|
/* same hack about using NrrdIoState->line as basis for parsing */ |
517 |
|
|
nio->line = dirStr; |
518 |
|
|
nio->pos = 0; |
519 |
|
|
if (nrrdFieldInfoParse[nrrdField_space_directions](NULL, nrrd, |
520 |
|
|
nio, AIR_TRUE)) { |
521 |
|
|
airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); |
522 |
|
|
fprintf(stderr, "%s: trouble with space directions \"%s\":\n%s", |
523 |
|
|
me, dirStr, err); |
524 |
|
|
nio->line = NULL; airMopError(mop); return 1; |
525 |
|
|
} |
526 |
|
|
nio->line = NULL; |
527 |
|
|
} |
528 |
|
|
if (airStrlen(_mframeStr)) { |
529 |
|
|
/* same confusion as above */ |
530 |
|
|
if ('\"' == _mframeStr[0] && '\"' == _mframeStr[strlen(_mframeStr)-1]) { |
531 |
|
|
_mframeStr[strlen(_mframeStr)-1] = 0; |
532 |
|
|
mframeStr = _mframeStr + 1; |
533 |
|
|
} else { |
534 |
|
|
mframeStr = _mframeStr; |
535 |
|
|
} |
536 |
|
|
/* same hack about using NrrdIoState->line as basis for parsing */ |
537 |
|
|
nio->line = mframeStr; |
538 |
|
|
nio->pos = 0; |
539 |
|
|
if (nrrdFieldInfoParse[nrrdField_measurement_frame](NULL, nrrd, |
540 |
|
|
nio, AIR_TRUE)) { |
541 |
|
|
airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); |
542 |
|
|
fprintf(stderr, "%s: trouble with measurement frame \"%s\":\n%s", |
543 |
|
|
me, mframeStr, err); |
544 |
|
|
nio->line = NULL; airMopError(mop); return 1; |
545 |
|
|
} |
546 |
|
|
nio->line = NULL; |
547 |
|
|
} |
548 |
|
|
if (airStrlen(spunits[0])) { |
549 |
|
|
if (!spaceSet) { |
550 |
|
|
fprintf(stderr, "%s: can't have space units with no space set\n", me); |
551 |
|
|
airMopError(mop); return 1; |
552 |
|
|
} |
553 |
|
|
if (nrrd->spaceDim != spunitsLen) { |
554 |
|
|
fprintf(stderr, |
555 |
|
|
"%s: number of space units (%d) " |
556 |
|
|
"not same as space dimension (%d)\n", |
557 |
|
|
me, spunitsLen, nrrd->spaceDim); |
558 |
|
|
airMopError(mop); |
559 |
|
|
return 1; |
560 |
|
|
} |
561 |
|
|
for (ii=0; ii<nrrd->spaceDim; ii++) { |
562 |
|
|
if (!strcmp(NO_STRING, spunits[ii])) { |
563 |
|
|
strcpy(spunits[ii], ""); |
564 |
|
|
} |
565 |
|
|
} |
566 |
|
|
/* have to allocate line then pass it to parsing */ |
567 |
|
|
bufLen = 0; |
568 |
|
|
for (ii=0; ii<nrrd->spaceDim; ii++) { |
569 |
|
|
bufLen += airStrlen(" ") + airStrlen("\"\"") + airStrlen(spunits[ii]); |
570 |
|
|
} |
571 |
|
|
parseBuf = AIR_CALLOC(bufLen+1, char); |
572 |
|
|
airMopAdd(mop, parseBuf, airFree, airMopAlways); |
573 |
|
|
strcpy(parseBuf, ""); |
574 |
|
|
for (ii=0; ii<nrrd->spaceDim; ii++) { |
575 |
|
|
if (ii) { |
576 |
|
|
strcat(parseBuf, " "); |
577 |
|
|
} |
578 |
|
|
strcat(parseBuf, "\""); |
579 |
|
|
strcat(parseBuf, spunits[ii]); |
580 |
|
|
strcat(parseBuf, "\""); |
581 |
|
|
} |
582 |
|
|
nio->line = parseBuf; |
583 |
|
|
nio->pos = 0; |
584 |
|
|
if (nrrdFieldInfoParse[nrrdField_space_units](NULL, nrrd, |
585 |
|
|
nio, AIR_TRUE)) { |
586 |
|
|
airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); |
587 |
|
|
fprintf(stderr, "%s: trouble with space units \"%s\":\n%s", |
588 |
|
|
me, parseBuf, err); |
589 |
|
|
nio->line = NULL; airMopError(mop); return 1; |
590 |
|
|
} |
591 |
|
|
nio->line = NULL; |
592 |
|
|
} |
593 |
|
|
if (_nrrdCheck(nrrd, AIR_FALSE, AIR_TRUE)) { |
594 |
|
|
airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); |
595 |
|
|
fprintf(stderr, "%s: problems with nrrd as set up:\n%s", me, err); |
596 |
|
|
airMopError(mop); return 1; |
597 |
|
|
} |
598 |
|
|
|
599 |
|
|
/* ----------------- END SETTING INFO ---------------- */ |
600 |
|
|
/* -------------------- BEGIN I/O -------------------- */ |
601 |
|
|
|
602 |
|
|
nio->lineSkip = lineSkip; |
603 |
|
|
nio->byteSkip = byteSkip; |
604 |
|
|
nio->encoding = encoding; |
605 |
|
|
nio->endian = endian; |
606 |
|
|
/* for the sake of reading in data files, this is as good a guess |
607 |
|
|
as any as to what the header-relative path to them is. This |
608 |
|
|
assuages concerns that come up even with headerOnly */ |
609 |
|
|
nio->path = airStrdup("."); |
610 |
|
|
if (headerOnly) { |
611 |
|
|
/* we open and hand off the output FILE* to the nrrd writer, which |
612 |
|
|
will not write any data, because of nio->skipData = AIR_TRUE */ |
613 |
|
|
if (!( fileOut = airFopen(out, stdout, "wb") )) { |
614 |
|
|
fprintf(stderr, "%s: couldn't fopen(\"%s\",\"wb\"): %s\n", |
615 |
|
|
me, out, strerror(errno)); |
616 |
|
|
airMopError(mop); return 1; |
617 |
|
|
} |
618 |
|
|
airMopAdd(mop, fileOut, (airMopper)airFclose, airMopAlways); |
619 |
|
|
/* whatever line and byte skipping is required will be simply |
620 |
|
|
recorded in the header, and done by the next reader */ |
621 |
|
|
nio->detachedHeader = AIR_TRUE; |
622 |
|
|
nio->skipData = AIR_TRUE; |
623 |
|
|
if (nrrdFormatNRRD->write(fileOut, nrrd, nio)) { |
624 |
|
|
airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); |
625 |
|
|
fprintf(stderr, "%s: trouble writing header:\n%s", me, err); |
626 |
|
|
airMopError(mop); return 1; |
627 |
|
|
} |
628 |
|
|
} else { |
629 |
|
|
/* all this does is read the data from the files. We up the verbosity |
630 |
|
|
because of all places this is probably where we really want it */ |
631 |
|
|
nrrdStateVerboseIO++; |
632 |
|
|
if (nrrdFormatNRRD->read(NULL, nrrd, nio)) { |
633 |
|
|
airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); |
634 |
|
|
fprintf(stderr, "%s: trouble reading data files:\n%s", me, err); |
635 |
|
|
airMopError(mop); return 1; |
636 |
|
|
} |
637 |
|
|
nrrdStateVerboseIO--; |
638 |
|
|
/* then save normally */ |
639 |
|
|
nrrdIoStateInit(nio); |
640 |
|
|
if (strlen(outData)) { |
641 |
|
|
airArrayLenSet(nio->dataFNArr, 1); |
642 |
|
|
nio->dataFN[0] = airStrdup(outData); |
643 |
|
|
} |
644 |
|
|
SAVE(out, nrrd, nio); |
645 |
|
|
} |
646 |
|
|
|
647 |
|
|
airMopOkay(mop); |
648 |
|
|
return 0; |
649 |
|
1 |
} |
650 |
|
|
|
651 |
|
|
UNRRDU_CMD(make, INFO); |