32#include "match_string.h"
39char *option[N_OPTIONS] = {
40 "erase",
"output",
"binary"};
42char *USAGE =
"mpl2sdds <mpl-filename> [<mpl-filename>...] \n\
43-output=<SDDS-filename> [-erase] [-binary]\n\n\
44Program by Michael Borland. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
").";
46void extract_name_and_unit(
char **name,
char **unit,
char *label);
47long add_definition(
SDDS_DATASET *SDDS_dataset,
char *label,
char *filename);
48void fix_mpl_name(
char *name);
50int main(
int argc,
char **argv) {
55 long i, j, i_arg, inputs;
57 long erase, rows, new_columns, SDDS_rows;
63 argc =
scanargs(&scanned, argc, argv);
69 inputs = erase = binary = 0;
71 for (i_arg = 1; i_arg < argc; i_arg++) {
72 if (scanned[i_arg].arg_type == OPTION) {
74 switch (
match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) {
79 if (scanned[i_arg].n_items != 2)
80 bomb(
"invalid -output syntax", USAGE);
81 output = scanned[i_arg].list[1];
84 if (scanned[i_arg].n_items != 1)
85 bomb(
"invalid -binary syntax", USAGE);
89 bomb(
"unknown option given", USAGE);
93 input =
trealloc(input, (inputs + 1) *
sizeof(*input));
94 input[inputs++] = scanned[i_arg].list[0];
99 bomb(
"-output option must be given", USAGE);
101 bomb(
"no input files listed", USAGE);
103 if (!erase &&
fexists(output)) {
106 fprintf(stderr,
"error: couldn't read SDDS layout\n");
111 fprintf(stderr,
"error: couldn't read data table\n");
116 fclose(SDDS_dataset.layout.fp);
117 SDDS_dataset.layout.fp =
fopen_e(output,
"w", 0);
121 fprintf(stderr,
"error: unable to initialize output SDDS structure\n");
129 mpl_data =
tmalloc(
sizeof(*mpl_data) * inputs);
130 data =
tmalloc(
sizeof(*data) * (2 * inputs));
131 index =
tmalloc(
sizeof(*index) * (2 * inputs));
133 for (i = 0; i < inputs; i++) {
134 if (!
get_table(mpl_data + i, input[i], 1, 0)) {
135 fprintf(stderr,
"warning: unable to read data from %s--continuing\n", input[i]);
140 if (!(rows = mpl_data[i].n_data)) {
141 fprintf(stderr,
"warning: no data in file %s--continuing\n", input[i]);
144 }
else if (rows != mpl_data[i].n_data)
145 SDDS_Bomb(
"mpl files do not have the same number of data points");
146 else if (SDDS_rows != -1 && rows != SDDS_rows)
147 SDDS_Bomb(
"mpl files must have same number of data points as SDDS file has rows");
149 if ((index[new_columns] = add_definition(&SDDS_dataset, mpl_data[i].xlab, input[i])) < 0)
150 free(mpl_data[i].c1);
152 data[new_columns++] = mpl_data[i].c1;
154 if ((index[new_columns] = add_definition(&SDDS_dataset, mpl_data[i].ylab, input[i])) < 0)
155 free(mpl_data[i].c2);
157 data[new_columns++] = mpl_data[i].c2;
160 if (!rows || !new_columns)
161 SDDS_Bomb(
"all files are empty or invalid");
172 for (i = 0; i < new_columns; i++)
173 for (j = 0; j < rows; j++)
174 SDDS_SetRowValues(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, index[i], data[i][j], -1);
183long add_definition(
SDDS_DATASET *SDDS_dataset,
char *label,
char *filename) {
184 char *symbol, *name, *unit;
187 extract_name_and_unit(&symbol, &unit, label);
191 fprintf(stderr,
"warning: column name %s from file %s already exists--ignored\n", name, filename);
201void extract_name_and_unit(
char **name,
char **unit,
char *label) {
204 if ((uptr = strchr(label,
'('))) {
206 if ((ptr = strchr(uptr,
')')))
211 ptr = label + strlen(label) - 1;
212 while (ptr != label && *ptr ==
' ')
217void fix_mpl_name(
char *name) {
220 while ((ptr = strchr(ptr,
'$'))) {
221 switch (*(ptr + 1)) {
237 while ((ptr = strchr(ptr,
' '))) {
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_SetRowValues(SDDS_DATASET *SDDS_dataset, int32_t mode, int64_t row,...)
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
int32_t SDDS_InitializeOutput(SDDS_DATASET *SDDS_dataset, int32_t data_mode, int32_t lines_per_row, const char *description, const char *contents, const char *filename)
Initializes the SDDS output dataset.
int32_t SDDS_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
int32_t SDDS_DefineColumn(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, int32_t field_length)
Defines a data column within the SDDS dataset.
int32_t SDDS_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column in the SDDS dataset.
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.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
int32_t SDDS_CopyString(char **target, const char *source)
Copies a source string to a target string with memory allocation.
#define SDDS_DOUBLE
Identifier for the double data type.
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
long fexists(const char *filename)
Checks if a file exists.
FILE * fopen_e(char *file, char *open_mode, long mode)
Opens a file with error checking, messages, and aborts.
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)
char * strcpy_ss(char *dest, const char *src)
Safely copies a string, handling memory overlap.
long get_table(TABLE *tab, char *file, int64_t sample_interval, long flags)
Gets a table from a file in DPL format.