73static char *option[N_OPTIONS] = {
83 " sdds2math [<SDDSfilename>] [<outputname>]\n"
84 " [-pipe[=in][,out]]\n"
87 " [-format=<format-string>]\n"
89 " -pipe[=in][,out] Standard SDDS Toolkit pipe option.\n"
90 " -comments Include helpful Mathematica comments in the output file.\n"
91 " -format=<format-string> Specify the format for double precision numbers (Default: " FORMAT
").\n"
92 " -verbose Display header information to the terminal.\n"
95 " sdds2math converts an SDDS file into a Mathematica-readable format.\n"
96 " The output is a single Mathematica variable with the structure:\n"
97 " sdds = {description, coldef, pardef, arraydef, associates, tables}\n"
98 " where each component contains detailed information about the SDDS data.\n"
101 " Kenneth Evans (Original version: 1994)\n"
102 " Updated: " __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
"\n";
104int main(
int argc,
char **argv) {
112 char *input, *output;
113 long i, i_arg, ntable;
116 char *text, *contents, *ss, *ptr, *iformat = FORMAT, *format, *rformat;
117 long verbose = 0, comments = 0, addquotes = 1;
122 unsigned long pipeFlags;
126 input = output = NULL;
129 argc =
scanargs(&s_arg, argc, argv);
131 fprintf(stderr,
"%s", USAGE);
135 for (i_arg = 1; i_arg < argc; i_arg++) {
136 if (s_arg[i_arg].arg_type == OPTION) {
137 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
142 if (s_arg[i_arg].n_items < 2)
144 iformat = s_arg[i_arg].list[1];
150 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
154 fprintf(stderr,
"Unknown option: %s\n", s_arg[i_arg].list[0]);
155 fprintf(stderr,
"%s", USAGE);
160 input = s_arg[i_arg].list[0];
161 else if (output == NULL)
162 output = s_arg[i_arg].list[0];
164 SDDS_Bomb(
"Too many filenames provided.");
171 outfile = fopen(output,
"w");
173 fprintf(stderr,
"Error: Cannot open output file '%s'\n", output);
181 format = (
char *)calloc(256,
sizeof(
char));
183 fprintf(stderr,
"Memory allocation error for format.\n");
187 rformat = (
char *)calloc(256,
sizeof(
char));
189 fprintf(stderr,
"Memory allocation error for rformat.\n");
194 strcpy(format, iformat);
195 if ((ptr = strchr(format,
'E')))
197 if ((ptr = strchr(format,
'G')))
199 strcpy(rformat, format);
200 if ((ptr = strpbrk(rformat,
"eg")))
209 layout = &SDDS_table.layout;
212 fprintf(outfile,
"{");
215 fprintf(outfile,
"{");
217 printf(
"\nFile '%s' is in SDDS protocol version %" PRId32
"\n", input, layout->version);
224 printf(
"Description: %s\n", text);
225 fprintf(outfile,
"\"%s\",", text);
230 printf(
"Contents: %s\n", contents);
231 fprintf(outfile,
"\"%s\"", contents);
233 fprintf(outfile,
"\"No contents\"");
236 if (layout->data_mode.mode == SDDS_ASCII) {
238 printf(
"\nData is ASCII with %" PRId32
" lines per row and %" PRId32
" additional header lines expected.\n",
239 layout->data_mode.lines_per_row, layout->data_mode.additional_header_lines);
241 printf(
"Row counts: %s\n", layout->data_mode.no_row_counts ?
"No" :
"Yes");
242 }
else if (verbose) {
243 printf(
"\nData is binary\n");
246 fprintf(outfile,
"},\n");
249 fprintf(outfile,
" {");
250 if (layout->n_columns) {
252 printf(
"\n%" PRId32
" columns of data:\n", layout->n_columns);
254 printf(
"NAME UNITS SYMBOL FORMAT TYPE FIELD DESCRIPTION\n");
257 for (i = 0; i < layout->n_columns; i++) {
259 fprintf(outfile,
",\n ");
260 coldef = layout->column_definition + i;
262 printf(
"%-15s %-15s %-15s %-15s %-7s %-7" PRId32
" %s\n",
263 coldef->name ? coldef->name :
"No name",
264 coldef->units ? coldef->units :
"",
265 coldef->symbol ? coldef->symbol :
"",
266 coldef->format_string ? coldef->format_string :
"",
268 coldef->field_length,
269 coldef->description ? coldef->description :
"No description");
270 fprintf(outfile,
"{\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",%" PRId32
",\"%s\"}",
271 coldef->name ? coldef->name :
"No name",
272 coldef->units ? coldef->units :
"",
273 coldef->symbol ? coldef->symbol :
"",
274 coldef->format_string ? coldef->format_string :
"",
276 coldef->field_length,
277 coldef->description ? coldef->description :
"No description");
280 fprintf(outfile,
"},\n");
283 fprintf(outfile,
" {");
284 if (layout->n_parameters) {
286 printf(
"\n%" PRId32
" parameters:\n", layout->n_parameters);
288 printf(
"NAME UNITS SYMBOL TYPE DESCRIPTION\n");
289 for (i = 0; i < layout->n_parameters; i++) {
291 fprintf(outfile,
",\n ");
292 pardef = layout->parameter_definition + i;
294 printf(
"%-19s %-19s %-19s %-19s %s\n",
295 pardef->name ? pardef->name :
"No name",
296 pardef->units ? pardef->units :
"",
297 pardef->symbol ? pardef->symbol :
"",
299 pardef->description ? pardef->description :
"No description");
300 fprintf(outfile,
"{\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"}",
301 pardef->name ? pardef->name :
"No name",
302 pardef->fixed_value ? pardef->fixed_value :
"",
303 pardef->units ? pardef->units :
"",
304 pardef->symbol ? pardef->symbol :
"",
306 pardef->description ? pardef->description :
"No description");
309 fprintf(outfile,
"},\n");
312 fprintf(outfile,
" {");
313 if (layout->n_arrays) {
315 printf(
"\n%" PRId32
" arrays of data:\n", layout->n_arrays);
317 printf(
"NAME UNITS SYMBOL FORMAT TYPE FIELD GROUP DESCRIPTION\n");
319 printf(
" LENGTH NAME\n");
320 for (i = 0; i < layout->n_arrays; i++) {
322 fprintf(outfile,
",\n ");
323 arraydef = layout->array_definition + i;
325 printf(
"%-15s %-15s %-15s %-7s %-8s*^%-5" PRId32
" %-7" PRId32
" %-15s %s\n",
326 arraydef->name ? arraydef->name :
"No name",
327 arraydef->units ? arraydef->units :
"",
328 arraydef->symbol ? arraydef->symbol :
"",
329 arraydef->format_string ? arraydef->format_string :
"",
331 arraydef->dimensions,
332 arraydef->field_length,
333 arraydef->group_name ? arraydef->group_name :
"",
334 arraydef->description ? arraydef->description :
"No description");
335 fprintf(outfile,
"{\"%s\",\"%s\",\"%s\",\"%s\",\"%s*^%" PRId32
"\",%" PRId32
",\"%s\",\"%s\"}",
336 arraydef->name ? arraydef->name :
"No name",
337 arraydef->units ? arraydef->units :
"",
338 arraydef->symbol ? arraydef->symbol :
"",
339 arraydef->format_string ? arraydef->format_string :
"",
341 arraydef->dimensions,
342 arraydef->field_length,
343 arraydef->group_name ? arraydef->group_name :
"",
344 arraydef->description ? arraydef->description :
"No description");
347 fprintf(outfile,
"},\n");
350 fprintf(outfile,
" {");
351 if (layout->n_associates) {
353 printf(
"\n%" PRId32
" associates:\n", layout->n_associates);
355 printf(
"SDDS FILENAME PATH CONTENTS DESCRIPTION\n");
356 for (i = 0; i < layout->n_associates; i++) {
358 fprintf(outfile,
",\n ");
360 printf(
"%-5s %-19s %-29s %-19s %s\n",
361 layout->associate_definition[i].sdds ?
"True" :
"False",
362 layout->associate_definition[i].filename ? layout->associate_definition[i].filename :
"",
363 layout->associate_definition[i].path ? layout->associate_definition[i].path :
"",
364 layout->associate_definition[i].contents ? layout->associate_definition[i].contents :
"",
365 layout->associate_definition[i].description ? layout->associate_definition[i].description :
"No description");
366 fprintf(outfile,
"{\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"}",
367 layout->associate_definition[i].sdds ?
"True" :
"False",
368 layout->associate_definition[i].filename ? layout->associate_definition[i].filename :
"",
369 layout->associate_definition[i].path ? layout->associate_definition[i].path :
"",
370 layout->associate_definition[i].contents ? layout->associate_definition[i].contents :
"",
371 layout->associate_definition[i].description ? layout->associate_definition[i].description :
"No description");
374 fprintf(outfile,
"},\n");
377 fprintf(outfile,
" {");
378 while ((ntable = SDDS_ReadTable(&SDDS_table)) > 0) {
380 fprintf(outfile,
",\n ");
382 fprintf(outfile,
"(*Table %ld*)", ntable);
383 fprintf(outfile,
"{\n");
386 fprintf(outfile,
" {");
387 for (i = 0; i < layout->n_parameters; i++) {
389 fprintf(outfile,
",\n ");
390 pardef = layout->parameter_definition + i;
398 fprintf(outfile,
"(* %s *)", pardef->name);
401 switch (pardef->type) {
403 dd = *(
double *)data;
404 sprintf(s, format, dd);
405 if ((ptr = strchr(s,
'e'))) {
407 sscanf(s,
"%lf %hd", &ddred, &nexp);
408 fprintf(outfile, rformat, ddred);
409 fprintf(outfile,
"*10^%d", nexp);
411 fprintf(outfile,
"%s", s);
416 sprintf(s, format, ff);
417 if ((ptr = strchr(s,
'e'))) {
419 sscanf(s,
"%f %hd", &ffred, &nexp);
420 fprintf(outfile, rformat, ffred);
421 fprintf(outfile,
"*10^%d", nexp);
423 fprintf(outfile,
"%s", s);
434 fprintf(outfile,
"\"");
437 fprintf(outfile,
"\"");
444 fprintf(outfile,
"},\n");
447 fprintf(outfile,
" {");
448 if (layout->n_columns) {
457 for (j = 0; j < nrows; j++) {
459 fprintf(outfile,
",\n ");
460 fprintf(outfile,
"{");
461 for (i = 0; i < layout->n_columns; i++) {
463 fprintf(outfile,
",");
464 coldef = layout->column_definition + i;
465 if (!(data =
SDDS_GetValue(&SDDS_table, coldef->name, j, NULL))) {
471 switch (coldef->type) {
473 dd = *(
double *)data;
474 sprintf(s, format, dd);
475 if ((ptr = strchr(s,
'e'))) {
477 sscanf(s,
"%lf %hd", &ddred, &nexp);
478 fprintf(outfile, rformat, ddred);
479 fprintf(outfile,
"*10^%d", nexp);
481 fprintf(outfile,
"%s", s);
486 sprintf(s, format, ff);
487 if ((ptr = strchr(s,
'e'))) {
489 sscanf(s,
"%f %hd", &ffred, &nexp);
490 fprintf(outfile, rformat, ffred);
491 fprintf(outfile,
"*10^%d", nexp);
493 fprintf(outfile,
"%s", s);
504 fprintf(outfile,
"\"");
507 fprintf(outfile,
"\"");
514 fprintf(outfile,
"}");
518 fprintf(outfile,
"}");
519 fprintf(outfile,
"}");
521 fprintf(outfile,
"\n }\n");
524 fprintf(outfile,
"}\n");
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
char * SDDS_type_name[SDDS_NUM_TYPES]
Array of supported data type names.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
int32_t SDDS_StringIsBlank(char *s)
Checks if a string is blank (contains only whitespace characters).
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
int32_t SDDS_PrintTypedValue(void *data, int64_t index, int32_t type, char *format, FILE *fp, uint32_t mode)
Prints a data value of a specified type using an optional printf format string.
int32_t SDDS_HasWhitespace(char *string)
Checks if a string contains any whitespace characters.
#define SDDS_FLOAT
Identifier for the float data type.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_CHARACTER
Identifier for the character data type.
#define SDDS_DOUBLE
Identifier for the double data type.
long match_string(char *string, char **option, long n_options, long mode)
Matches a given string against an array of option strings based on specified modes.
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
long processPipeOption(char **item, long items, unsigned long *flags)
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)