SDDSlib
Loading...
Searching...
No Matches
mpl2sdds.c
1/*************************************************************************\
2 * Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * This file is distributed subject to a Software License Agreement found
7 * in the file LICENSE that is included with this distribution.
8\*************************************************************************/
9
10/* program: mpl2sdds
11 * purpose: add mpl data sets to a SDDS data set
12 *
13 * Michael Borland, 1993
14 $Log: not supported by cvs2svn $
15 Revision 1.6 2002/08/14 17:12:38 soliday
16 Added Open License
17
18 Revision 1.5 2001/01/23 19:14:56 soliday
19 Standardized usage message.
20
21 Revision 1.4 1999/05/25 19:04:02 soliday
22 Removed compiler warning on linux.
23
24 Revision 1.3 1995/09/06 14:55:55 saunders
25 First test release of SDDS1.5
26
27*/
28#include "mdb.h"
29#include "table.h"
30#include "SDDS.h"
31#include "scan.h"
32#include "match_string.h"
33
34#define SET_ERASE 0
35#define SET_OUTPUT 1
36#define SET_BINARY 2
37#define N_OPTIONS 3
38
39char *option[N_OPTIONS] = {
40 "erase", "output", "binary"};
41
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 ").";
45
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);
49
50int main(int argc, char **argv) {
51 SDDS_DATASET SDDS_dataset;
52 TABLE *mpl_data;
53 SCANNED_ARG *scanned;
54 char **input;
55 long i, j, i_arg, inputs;
56 char *output;
57 long erase, rows, new_columns, SDDS_rows;
58 double **data;
59 long *index;
60 long binary;
61
63 argc = scanargs(&scanned, argc, argv);
64 if (argc < 3)
65 bomb(NULL, USAGE);
66
67 input = NULL;
68 output = NULL;
69 inputs = erase = binary = 0;
70
71 for (i_arg = 1; i_arg < argc; i_arg++) {
72 if (scanned[i_arg].arg_type == OPTION) {
73 /* process options here */
74 switch (match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) {
75 case SET_ERASE:
76 erase = 1;
77 break;
78 case SET_OUTPUT:
79 if (scanned[i_arg].n_items != 2)
80 bomb("invalid -output syntax", USAGE);
81 output = scanned[i_arg].list[1];
82 break;
83 case SET_BINARY:
84 if (scanned[i_arg].n_items != 1)
85 bomb("invalid -binary syntax", USAGE);
86 binary = 1;
87 break;
88 default:
89 bomb("unknown option given", USAGE);
90 break;
91 }
92 } else {
93 input = trealloc(input, (inputs + 1) * sizeof(*input));
94 input[inputs++] = scanned[i_arg].list[0];
95 }
96 }
97
98 if (!output)
99 bomb("-output option must be given", USAGE);
100 if (!input)
101 bomb("no input files listed", USAGE);
102
103 if (!erase && fexists(output)) {
104 /* load data from existing file */
105 if (!SDDS_InitializeInput(&SDDS_dataset, output)) {
106 fprintf(stderr, "error: couldn't read SDDS layout\n");
107 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
108 exit(1);
109 }
110 if (!SDDS_ReadPage(&SDDS_dataset)) {
111 fprintf(stderr, "error: couldn't read data table\n");
112 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
113 exit(1);
114 }
115 SDDS_rows = SDDS_CountRowsOfInterest(&SDDS_dataset);
116 fclose(SDDS_dataset.layout.fp);
117 SDDS_dataset.layout.fp = fopen_e(output, "w", 0);
118 } else {
119 /* start a new file */
120 if (!SDDS_InitializeOutput(&SDDS_dataset, SDDS_BINARY, 1, NULL, NULL, output)) {
121 fprintf(stderr, "error: unable to initialize output SDDS structure\n");
122 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
123 exit(1);
124 }
125 SDDS_rows = -1;
126 }
127
128 rows = 0;
129 mpl_data = tmalloc(sizeof(*mpl_data) * inputs);
130 data = tmalloc(sizeof(*data) * (2 * inputs));
131 index = tmalloc(sizeof(*index) * (2 * inputs));
132 new_columns = 0;
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]);
136 continue;
137 }
138
139 if (!rows) {
140 if (!(rows = mpl_data[i].n_data)) {
141 fprintf(stderr, "warning: no data in file %s--continuing\n", input[i]);
142 continue;
143 }
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");
148
149 if ((index[new_columns] = add_definition(&SDDS_dataset, mpl_data[i].xlab, input[i])) < 0)
150 free(mpl_data[i].c1);
151 else
152 data[new_columns++] = mpl_data[i].c1;
153
154 if ((index[new_columns] = add_definition(&SDDS_dataset, mpl_data[i].ylab, input[i])) < 0)
155 free(mpl_data[i].c2);
156 else
157 data[new_columns++] = mpl_data[i].c2;
158 }
159
160 if (!rows || !new_columns)
161 SDDS_Bomb("all files are empty or invalid");
162
163 if (!SDDS_WriteLayout(&SDDS_dataset)) {
164 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
165 exit(1);
166 }
167 if (!SDDS_StartPage(&SDDS_dataset, rows)) {
168 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
169 exit(1);
170 }
171
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);
175
176 if (!SDDS_WritePage(&SDDS_dataset)) {
177 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
178 exit(1);
179 }
180 return (0);
181}
182
183long add_definition(SDDS_DATASET *SDDS_dataset, char *label, char *filename) {
184 char *symbol, *name, *unit;
185 long index;
186
187 extract_name_and_unit(&symbol, &unit, label);
188 SDDS_CopyString(&name, symbol);
189 fix_mpl_name(name);
190 if (SDDS_GetColumnIndex(SDDS_dataset, name) >= 0) {
191 fprintf(stderr, "warning: column name %s from file %s already exists--ignored\n", name, filename);
192 return (-1);
193 }
194 if ((index = SDDS_DefineColumn(SDDS_dataset, name, symbol, unit, NULL, NULL, SDDS_DOUBLE, 0)) < 0) {
195 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
196 exit(1);
197 }
198 return (index);
199}
200
201void extract_name_and_unit(char **name, char **unit, char *label) {
202 char *ptr, *uptr;
203
204 if ((uptr = strchr(label, '('))) {
205 *uptr++ = 0;
206 if ((ptr = strchr(uptr, ')')))
207 *ptr = 0;
208 SDDS_CopyString(unit, uptr);
209 } else
210 *unit = NULL;
211 ptr = label + strlen(label) - 1;
212 while (ptr != label && *ptr == ' ')
213 *ptr-- = 0;
214 SDDS_CopyString(name, label);
215}
216
217void fix_mpl_name(char *name) {
218 char *ptr, *ptr1;
219 ptr = name;
220 while ((ptr = strchr(ptr, '$'))) {
221 switch (*(ptr + 1)) {
222 case 'a':
223 case 'b':
224 case 'n':
225 case 'g':
226 case 'r':
227 case 's':
228 case 'e':
229 strcpy_ss(ptr, ptr + 2);
230 break;
231 default:
232 ptr += 1;
233 break;
234 }
235 }
236 ptr = name;
237 while ((ptr = strchr(ptr, ' '))) {
238 ptr1 = ptr;
239 while (*ptr1 == ' ')
240 ptr1++;
241 strcpy_ss(ptr, ptr1);
242 }
243}
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)
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_InitializeInput(SDDS_DATASET *SDDS_dataset, char *filename)
Definition SDDS_input.c:49
int32_t SDDS_ReadPage(SDDS_DATASET *SDDS_dataset)
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.
Definition SDDS_utils.c:432
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
Definition SDDS_utils.c:288
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
#define SDDS_DOUBLE
Identifier for the double data type.
Definition SDDStypes.h:37
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
Definition array.c:181
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:59
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26
long fexists(const char *filename)
Checks if a file exists.
Definition fexists.c:27
FILE * fopen_e(char *file, char *open_mode, long mode)
Opens a file with error checking, messages, and aborts.
Definition fopen_e.c:30
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
char * strcpy_ss(char *dest, const char *src)
Safely copies a string, handling memory overlap.
Definition str_copy.c:34
long get_table(TABLE *tab, char *file, int64_t sample_interval, long flags)
Gets a table from a file in DPL format.
Definition table.c:45