SDDS ToolKit Programs and Libraries for C and Python
All Classes Files Functions Variables Macros Pages
sdds2mpl.c File Reference

Detailed Description

Converts SDDS data files into Borland's older 'mpl' (Multi-Purpose Library) format.

This program reads data from SDDS (Self Describing Data Set) files and generates output files in 'mpl' format. It supports various options to configure input files, output formats, and page separation.

Usage

sdds2mpl [<SDDSfilename>]
[-pipe[=input]]
[-rootname=<string>]
-output={column|parameter},<x-name>,<y-name>[,{<sy-name>|<sx-name>,<sy-name>}]...
[-labelParameters=<name>[=<format>]...]
[-separatePages]
[-announceOpenings]

Options

Required Description
-output Defines the output format and data.
Option Description
-pipe Reads input directly from a pipeline.
-rootname Specifies a root name for output files.
-labelParameters Adds labeled parameters to outputs.
-separatePages Separates data into multiple pages in output.
-announceOpenings Prints a message when files are opened.

Specific Requirements

  • For -rootname:
    • If not provided, the input filename (minus extension) is used as the root name.
License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday

Definition in file sdds2mpl.c.

#include "mdb.h"
#include "table.h"
#include "scan.h"
#include "SDDS.h"
#include "SDDSaps.h"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 86 of file sdds2mpl.c.

86 {
87 SDDS_DATASET sdds_dataset;
88 LABEL_PARAMETER *label_parameter = NULL;
89 long label_parameters = 0;
90 COLUMN_DEFINITION **coldef;
91 PARAMETER_DEFINITION **pardef;
92 char *inputfile = NULL;
93 char *rootname = NULL;
94 char filename[200];
95 long outputs = 0;
96 OUTPUT_REQUEST **output = NULL;
97 long i, i_arg;
98 int64_t n_rows;
99 long page_number, separate_pages = 0;
100 long announce_openings = 0;
101 SCANNED_ARG *s_arg;
102 char *ptr;
103 void *data[4];
104 double param[4];
105 long data_present = 0;
106 unsigned long pipe_flags = 0;
107
108 argc = scanargs(&s_arg, argc, argv);
109 if (argc < 3) {
110 fputs(usage, stderr);
111 exit(EXIT_FAILURE);
112 }
113
114 for (i_arg = 1; i_arg < argc; i_arg++) {
115 if (s_arg[i_arg].arg_type == OPTION) {
116 delete_chars(s_arg[i_arg].list[0], "_");
117 switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
118 case SET_ROOTNAME:
119 if (s_arg[i_arg].n_items != 2)
120 SDDS_Bomb("Invalid -rootname syntax");
121 rootname = s_arg[i_arg].list[1];
122 break;
123 case SET_OUTPUT:
124 if (s_arg[i_arg].n_items < 4 || s_arg[i_arg].n_items > 6)
125 SDDS_Bomb("Invalid -output syntax");
126 output = trealloc(output, sizeof(*output) * (outputs + 1));
127 if (!(output[outputs] = process_output_request(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, outputs ? output[outputs - 1] : NULL)))
128 SDDS_Bomb("Invalid -output syntax");
129 outputs++;
130 break;
131 case SET_SEPARATE_PAGES:
132 separate_pages = 1;
133 break;
134 case SET_LABEL_PARAMETERS:
135 if (s_arg[i_arg].n_items < 2)
136 SDDS_Bomb("Invalid -labelparameters syntax");
137 label_parameter = trealloc(label_parameter, sizeof(*label_parameter) * (label_parameters + s_arg[i_arg].n_items));
138 for (i = 1; i < s_arg[i_arg].n_items; i++)
139 scan_label_parameter(label_parameter + i - 1, s_arg[i_arg].list[i]);
140 label_parameters += s_arg[i_arg].n_items - 1;
141 break;
142 case SET_ANNOUNCE_OPENINGS:
143 announce_openings = 1;
144 break;
145 case SET_PIPE:
146 if (!processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipe_flags))
147 SDDS_Bomb("Invalid -pipe syntax");
148 break;
149 default:
150 SDDS_Bomb("Unknown switch");
151 break;
152 }
153 } else {
154 if (!inputfile)
155 inputfile = s_arg[i_arg].list[0];
156 else
157 SDDS_Bomb("Too many filenames");
158 }
159 }
160
161 if (!inputfile && !(pipe_flags & USE_STDIN))
162 SDDS_Bomb("No input source given");
163
164 if (!rootname) {
165 if (!inputfile)
166 SDDS_Bomb("You must give a rootname if you don't give an input filename");
167 SDDS_CopyString(&rootname, inputfile);
168 if ((ptr = strrchr(rootname, '.')))
169 *ptr = '\0';
170 }
171
172 if (outputs <= 0)
173 SDDS_Bomb("No output specifications given");
174
175 if (!SDDS_InitializeInput(&sdds_dataset, inputfile)) {
176 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
177 exit(EXIT_FAILURE);
178 }
179
180 for (i = 0; i < outputs; i++) {
181 if (!output[i]->parameter_output) {
182 if (SDDS_GetColumnIndex(&sdds_dataset, output[i]->item[0]) < 0 ||
183 SDDS_GetColumnIndex(&sdds_dataset, output[i]->item[1]) < 0 ||
184 (output[i]->item[2] && SDDS_GetColumnIndex(&sdds_dataset, output[i]->item[2]) < 0) ||
185 (output[i]->item[3] && SDDS_GetColumnIndex(&sdds_dataset, output[i]->item[3]) < 0)) {
186 fprintf(stderr, "Error: unrecognized column name given\n");
187 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
188 exit(EXIT_FAILURE);
189 }
190 } else {
191 if (SDDS_GetParameterIndex(&sdds_dataset, output[i]->item[0]) < 0 ||
192 SDDS_GetParameterIndex(&sdds_dataset, output[i]->item[1]) < 0 ||
193 (output[i]->item[2] && SDDS_GetParameterIndex(&sdds_dataset, output[i]->item[2]) < 0) ||
194 (output[i]->item[3] && SDDS_GetParameterIndex(&sdds_dataset, output[i]->item[3]) < 0)) {
195 fprintf(stderr, "Error: unrecognized parameter name given\n");
196 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
197 exit(EXIT_FAILURE);
198 }
199 }
200 }
201
202 while ((page_number = SDDS_ReadPage(&sdds_dataset)) > 0) {
203 data_present = 1;
204 if (!SDDS_SetRowFlags(&sdds_dataset, 1)) {
205 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
206 exit(EXIT_FAILURE);
207 }
208 if ((n_rows = SDDS_CountRowsOfInterest(&sdds_dataset)) <= 0) {
209 fprintf(stderr, "Warning: no rows selected for page %" PRId32 "\n", sdds_dataset.page_number);
211 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
212 }
213 for (i = 0; i < outputs; i++) {
214 if (!output[i]->fp) {
215 if (separate_pages && !output[i]->parameter_output)
216 snprintf(filename, sizeof(filename), "%s_%03ld_%s_%s.out", rootname, output[i]->counter++, output[i]->item[0], output[i]->item[1]);
217 else
218 snprintf(filename, sizeof(filename), "%s_%s_%s.out", rootname, output[i]->item[0], output[i]->item[1]);
219 set_up_output(filename, output[i], label_parameter, label_parameters, separate_pages, announce_openings, &sdds_dataset);
220 }
221 if (!output[i]->parameter_output) {
222 coldef = (COLUMN_DEFINITION **)output[i]->definitions;
223 data[2] = data[3] = NULL;
224 if (!(data[0] = SDDS_GetColumn(&sdds_dataset, output[i]->item[0])) ||
225 !(data[1] = SDDS_GetColumn(&sdds_dataset, output[i]->item[1])) ||
226 (output[i]->columns > 2 && !(data[2] = SDDS_GetColumn(&sdds_dataset, output[i]->item[2]))) ||
227 (output[i]->columns > 3 && !(data[3] = SDDS_GetColumn(&sdds_dataset, output[i]->item[3])))) {
228 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
229 exit(EXIT_FAILURE);
230 }
231 for (int64_t j = 0; j < n_rows; j++) {
232 for (long k = 0; k < output[i]->columns; k++) {
233 SDDS_PrintTypedValue((char *)data[k], j, coldef[k]->type, coldef[k]->format_string, output[i]->fp, 0);
234 if (k < output[i]->columns - 1)
235 fputc(' ', output[i]->fp);
236 }
237 fputc('\n', output[i]->fp);
238 }
239 output[i]->points += n_rows;
240 for (long k = 0; k < output[i]->columns; k++) {
241 free(data[k]);
242 }
243 } else {
244 pardef = (PARAMETER_DEFINITION **)output[i]->definitions;
245 for (long k = 0; k < output[i]->columns; k++) {
246 if (!SDDS_GetParameter(&sdds_dataset, output[i]->item[k], &param[k])) {
247 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
248 exit(EXIT_FAILURE);
249 }
250 }
251 output[i]->points += 1;
252 for (long k = 0; k < output[i]->columns; k++) {
253 SDDS_PrintTypedValue((char *)&param[k], 0, pardef[k]->type, pardef[k]->format_string, output[i]->fp, 0);
254 if (k < output[i]->columns - 1)
255 fputc(' ', output[i]->fp);
256 }
257 fputc('\n', output[i]->fp);
258 }
259 if (separate_pages && !output[i]->parameter_output) {
260 fclose(output[i]->fp);
261 output[i]->fp = NULL;
262 output[i]->points = 0;
263 }
264 }
265 }
266
267 if (page_number == 0) {
268 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
269 exit(EXIT_FAILURE);
270 }
271 if (page_number == -1 && !data_present) {
272 if (SDDS_NumberOfErrors()) {
273 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
274 exit(EXIT_FAILURE);
275 }
276 fprintf(stderr, "Error: input data file is empty\n");
277 exit(EXIT_FAILURE);
278 }
279
280 for (i = 0; i < outputs; i++) {
281 if (output[i]->fp)
282 fclose(output[i]->fp);
283 }
284
285 for (i = 0; i < outputs; i++) {
286 if (!separate_pages || output[i]->parameter_output) {
287 snprintf(filename, sizeof(filename), "%s_%s_%s.out", rootname, output[i]->item[0], output[i]->item[1]);
288 fixcount(filename, output[i]->points);
289 }
290 }
291 return 0;
292}
void * SDDS_GetColumn(SDDS_DATASET *SDDS_dataset, char *column_name)
Retrieves a copy of the data for a specified column, including only rows marked as "of interest".
int64_t SDDS_CountRowsOfInterest(SDDS_DATASET *SDDS_dataset)
Counts the number of rows marked as "of interest" in the current data table.
int32_t SDDS_SetRowFlags(SDDS_DATASET *SDDS_dataset, int32_t row_flag_value)
Sets the acceptance flags for all rows in the current data table of a data set.
void * SDDS_GetParameter(SDDS_DATASET *SDDS_dataset, char *parameter_name, void *memory)
Retrieves the value of a specified parameter from the current data table of a data set.
int32_t SDDS_InitializeInput(SDDS_DATASET *SDDS_dataset, char *filename)
Definition SDDS_input.c:49
int32_t SDDS_ReadPage(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_GetParameterIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named parameter in the SDDS dataset.
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.
Definition SDDS_utils.c:432
int32_t SDDS_NumberOfErrors()
Retrieves the number of errors recorded by SDDS library routines.
Definition SDDS_utils.c:304
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
Definition SDDS_utils.c:342
int32_t SDDS_CopyString(char **target, const char *source)
Copies a source string to a target string with memory allocation.
Definition SDDS_utils.c:856
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.
Definition SDDS_utils.c:59
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
Definition array.c:181
char * delete_chars(char *s, char *t)
Removes all occurrences of characters found in string t from string s.
int fixcount(char *filename, long n_points)
Updates the data point count in a specified file.
Definition fixcounts.c:33
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)
Definition scanargs.c:36
long processPipeOption(char **item, long items, unsigned long *flags)
Definition scanargs.c:356