81static char *option[N_OPTIONS] = {
91 " sdds2math [<SDDSfilename>] [<outputname>] [OPTIONS]\n"
94 " -pipe[=in][,out] Standard SDDS Toolkit pipe option.\n"
95 " -comments Include helpful Mathematica comments in the output file.\n"
96 " -format=<format-string> Specify the format for double precision numbers (Default: " FORMAT
").\n"
97 " -verbose Display header information to the terminal.\n"
100 " sdds2math converts an SDDS file into a Mathematica-readable format.\n"
101 " The output is a single Mathematica variable with the structure:\n"
102 " sdds = {description, coldef, pardef, arraydef, associates, tables}\n"
103 " where each component contains detailed information about the SDDS data.\n"
106 " Kenneth Evans (Original version: 1994)\n"
107 " Updated: " __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
"\n";
109int main(
int argc,
char **argv) {
117 char *input, *output;
118 long i, i_arg, ntable;
121 char *text, *contents, *ss, *ptr, *iformat = FORMAT, *format, *rformat;
122 long verbose = 0, comments = 0, addquotes = 1;
127 unsigned long pipeFlags;
131 input = output = NULL;
134 argc =
scanargs(&s_arg, argc, argv);
136 fprintf(stderr,
"%s", USAGE);
140 for (i_arg = 1; i_arg < argc; i_arg++) {
141 if (s_arg[i_arg].arg_type == OPTION) {
142 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
147 if (s_arg[i_arg].n_items < 2)
149 iformat = s_arg[i_arg].list[1];
155 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
159 fprintf(stderr,
"Unknown option: %s\n", s_arg[i_arg].list[0]);
160 fprintf(stderr,
"%s", USAGE);
165 input = s_arg[i_arg].list[0];
166 else if (output == NULL)
167 output = s_arg[i_arg].list[0];
169 SDDS_Bomb(
"Too many filenames provided.");
176 outfile = fopen(output,
"w");
178 fprintf(stderr,
"Error: Cannot open output file '%s'\n", output);
186 format = (
char *)calloc(256,
sizeof(
char));
188 fprintf(stderr,
"Memory allocation error for format.\n");
192 rformat = (
char *)calloc(256,
sizeof(
char));
194 fprintf(stderr,
"Memory allocation error for rformat.\n");
199 strcpy(format, iformat);
200 if ((ptr = strchr(format,
'E')))
202 if ((ptr = strchr(format,
'G')))
204 strcpy(rformat, format);
205 if ((ptr = strpbrk(rformat,
"eg")))
214 layout = &SDDS_table.layout;
217 fprintf(outfile,
"{");
220 fprintf(outfile,
"{");
222 printf(
"\nFile '%s' is in SDDS protocol version %" PRId32
"\n", input, layout->version);
229 printf(
"Description: %s\n", text);
230 fprintf(outfile,
"\"%s\",", text);
235 printf(
"Contents: %s\n", contents);
236 fprintf(outfile,
"\"%s\"", contents);
238 fprintf(outfile,
"\"No contents\"");
241 if (layout->data_mode.mode == SDDS_ASCII) {
243 printf(
"\nData is ASCII with %" PRId32
" lines per row and %" PRId32
" additional header lines expected.\n",
244 layout->data_mode.lines_per_row, layout->data_mode.additional_header_lines);
246 printf(
"Row counts: %s\n", layout->data_mode.no_row_counts ?
"No" :
"Yes");
247 }
else if (verbose) {
248 printf(
"\nData is binary\n");
251 fprintf(outfile,
"},\n");
254 fprintf(outfile,
" {");
255 if (layout->n_columns) {
257 printf(
"\n%" PRId32
" columns of data:\n", layout->n_columns);
259 printf(
"NAME UNITS SYMBOL FORMAT TYPE FIELD DESCRIPTION\n");
262 for (i = 0; i < layout->n_columns; i++) {
264 fprintf(outfile,
",\n ");
265 coldef = layout->column_definition + i;
267 printf(
"%-15s %-15s %-15s %-15s %-7s %-7" PRId32
" %s\n",
268 coldef->name ? coldef->name :
"No name",
269 coldef->units ? coldef->units :
"",
270 coldef->symbol ? coldef->symbol :
"",
271 coldef->format_string ? coldef->format_string :
"",
273 coldef->field_length,
274 coldef->description ? coldef->description :
"No description");
275 fprintf(outfile,
"{\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",%" PRId32
",\"%s\"}",
276 coldef->name ? coldef->name :
"No name",
277 coldef->units ? coldef->units :
"",
278 coldef->symbol ? coldef->symbol :
"",
279 coldef->format_string ? coldef->format_string :
"",
281 coldef->field_length,
282 coldef->description ? coldef->description :
"No description");
285 fprintf(outfile,
"},\n");
288 fprintf(outfile,
" {");
289 if (layout->n_parameters) {
291 printf(
"\n%" PRId32
" parameters:\n", layout->n_parameters);
293 printf(
"NAME UNITS SYMBOL TYPE DESCRIPTION\n");
294 for (i = 0; i < layout->n_parameters; i++) {
296 fprintf(outfile,
",\n ");
297 pardef = layout->parameter_definition + i;
299 printf(
"%-19s %-19s %-19s %-19s %s\n",
300 pardef->name ? pardef->name :
"No name",
301 pardef->units ? pardef->units :
"",
302 pardef->symbol ? pardef->symbol :
"",
304 pardef->description ? pardef->description :
"No description");
305 fprintf(outfile,
"{\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"}",
306 pardef->name ? pardef->name :
"No name",
307 pardef->fixed_value ? pardef->fixed_value :
"",
308 pardef->units ? pardef->units :
"",
309 pardef->symbol ? pardef->symbol :
"",
311 pardef->description ? pardef->description :
"No description");
314 fprintf(outfile,
"},\n");
317 fprintf(outfile,
" {");
318 if (layout->n_arrays) {
320 printf(
"\n%" PRId32
" arrays of data:\n", layout->n_arrays);
322 printf(
"NAME UNITS SYMBOL FORMAT TYPE FIELD GROUP DESCRIPTION\n");
324 printf(
" LENGTH NAME\n");
325 for (i = 0; i < layout->n_arrays; i++) {
327 fprintf(outfile,
",\n ");
328 arraydef = layout->array_definition + i;
330 printf(
"%-15s %-15s %-15s %-7s %-8s*^%-5" PRId32
" %-7" PRId32
" %-15s %s\n",
331 arraydef->name ? arraydef->name :
"No name",
332 arraydef->units ? arraydef->units :
"",
333 arraydef->symbol ? arraydef->symbol :
"",
334 arraydef->format_string ? arraydef->format_string :
"",
336 arraydef->dimensions,
337 arraydef->field_length,
338 arraydef->group_name ? arraydef->group_name :
"",
339 arraydef->description ? arraydef->description :
"No description");
340 fprintf(outfile,
"{\"%s\",\"%s\",\"%s\",\"%s\",\"%s*^%" PRId32
"\",%" PRId32
",\"%s\",\"%s\"}",
341 arraydef->name ? arraydef->name :
"No name",
342 arraydef->units ? arraydef->units :
"",
343 arraydef->symbol ? arraydef->symbol :
"",
344 arraydef->format_string ? arraydef->format_string :
"",
346 arraydef->dimensions,
347 arraydef->field_length,
348 arraydef->group_name ? arraydef->group_name :
"",
349 arraydef->description ? arraydef->description :
"No description");
352 fprintf(outfile,
"},\n");
355 fprintf(outfile,
" {");
356 if (layout->n_associates) {
358 printf(
"\n%" PRId32
" associates:\n", layout->n_associates);
360 printf(
"SDDS FILENAME PATH CONTENTS DESCRIPTION\n");
361 for (i = 0; i < layout->n_associates; i++) {
363 fprintf(outfile,
",\n ");
365 printf(
"%-5s %-19s %-29s %-19s %s\n",
366 layout->associate_definition[i].sdds ?
"True" :
"False",
367 layout->associate_definition[i].filename ? layout->associate_definition[i].filename :
"",
368 layout->associate_definition[i].path ? layout->associate_definition[i].path :
"",
369 layout->associate_definition[i].contents ? layout->associate_definition[i].contents :
"",
370 layout->associate_definition[i].description ? layout->associate_definition[i].description :
"No description");
371 fprintf(outfile,
"{\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"}",
372 layout->associate_definition[i].sdds ?
"True" :
"False",
373 layout->associate_definition[i].filename ? layout->associate_definition[i].filename :
"",
374 layout->associate_definition[i].path ? layout->associate_definition[i].path :
"",
375 layout->associate_definition[i].contents ? layout->associate_definition[i].contents :
"",
376 layout->associate_definition[i].description ? layout->associate_definition[i].description :
"No description");
379 fprintf(outfile,
"},\n");
382 fprintf(outfile,
" {");
383 while ((ntable = SDDS_ReadTable(&SDDS_table)) > 0) {
385 fprintf(outfile,
",\n ");
387 fprintf(outfile,
"(*Table %ld*)", ntable);
388 fprintf(outfile,
"{\n");
391 fprintf(outfile,
" {");
392 for (i = 0; i < layout->n_parameters; i++) {
394 fprintf(outfile,
",\n ");
395 pardef = layout->parameter_definition + i;
403 fprintf(outfile,
"(* %s *)", pardef->name);
406 switch (pardef->type) {
408 dd = *(
double *)data;
409 sprintf(s, format, dd);
410 if ((ptr = strchr(s,
'e'))) {
412 sscanf(s,
"%lf %hd", &ddred, &nexp);
413 fprintf(outfile, rformat, ddred);
414 fprintf(outfile,
"*10^%d", nexp);
416 fprintf(outfile,
"%s", s);
421 sprintf(s, format, ff);
422 if ((ptr = strchr(s,
'e'))) {
424 sscanf(s,
"%f %hd", &ffred, &nexp);
425 fprintf(outfile, rformat, ffred);
426 fprintf(outfile,
"*10^%d", nexp);
428 fprintf(outfile,
"%s", s);
439 fprintf(outfile,
"\"");
442 fprintf(outfile,
"\"");
449 fprintf(outfile,
"},\n");
452 fprintf(outfile,
" {");
453 if (layout->n_columns) {
462 for (j = 0; j < nrows; j++) {
464 fprintf(outfile,
",\n ");
465 fprintf(outfile,
"{");
466 for (i = 0; i < layout->n_columns; i++) {
468 fprintf(outfile,
",");
469 coldef = layout->column_definition + i;
470 if (!(data =
SDDS_GetValue(&SDDS_table, coldef->name, j, NULL))) {
476 switch (coldef->type) {
478 dd = *(
double *)data;
479 sprintf(s, format, dd);
480 if ((ptr = strchr(s,
'e'))) {
482 sscanf(s,
"%lf %hd", &ddred, &nexp);
483 fprintf(outfile, rformat, ddred);
484 fprintf(outfile,
"*10^%d", nexp);
486 fprintf(outfile,
"%s", s);
491 sprintf(s, format, ff);
492 if ((ptr = strchr(s,
'e'))) {
494 sscanf(s,
"%f %hd", &ffred, &nexp);
495 fprintf(outfile, rformat, ffred);
496 fprintf(outfile,
"*10^%d", nexp);
498 fprintf(outfile,
"%s", s);
509 fprintf(outfile,
"\"");
512 fprintf(outfile,
"\"");
519 fprintf(outfile,
"}");
523 fprintf(outfile,
"}");
524 fprintf(outfile,
"}");
526 fprintf(outfile,
"\n }\n");
529 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)