SDDSlib
Loading...
Searching...
No Matches
col2sdds.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#include "mdb.h"
11#include "SDDS.h"
12#include "scan.h"
13#include "column.h"
14
15#define SET_FIXMPLNAMES 0
16#define N_OPTIONS 1
17
18char *option[N_OPTIONS] = {
19 "fixMplNames"};
20
21char *USAGE = "col2sdds <inputfile> <outputfile> [-fixMplNames]\n\
22Link date: " __DATE__ " " __TIME__ ", SVN revision: " SVN_VERSION "\n";
23
24int main(int argc, char **argv) {
25 SCANNED_ARG *scanned;
26 char *input = NULL, *output = NULL;
27 int fixMplNames = 0;
28 long i_arg, i, j, k, len;
29 MC_TABLE mcTable;
30 SDDS_TABLE SDDS_table;
31 char buffer[100];
32 char **aux_symbol = NULL;
33 char **symbol = NULL;
34
36 argc = scanargs(&scanned, argc, argv);
37 if (argc < 3)
38 bomb(NULL, USAGE);
39 for (i_arg = 1; i_arg < argc; i_arg++) {
40 if (scanned[i_arg].arg_type == OPTION) {
41 switch (match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) {
42 case SET_FIXMPLNAMES:
43 fixMplNames = 1;
44 break;
45 default:
46 fprintf(stderr, "Invalid option seen\n%s", USAGE);
47 return (1);
48 }
49 } else {
50 if (!input)
51 input = scanned[i_arg].list[0];
52 else if (!output)
53 output = scanned[i_arg].list[0];
54 else {
55 fprintf(stderr, "Too many file names\n%s", USAGE);
56 return (1);
57 }
58 }
59 }
60 if (!input) {
61 fprintf(stderr, "Input file not seen\n%s", USAGE);
62 return (1);
63 }
64 if (!output) {
65 fprintf(stderr, "Output file not seen\n%s", USAGE);
66 return (1);
67 }
68
69 if (get_mc_table(&mcTable, input, GMCT_WARNINGS) == 0) {
70 fprintf(stderr, "Unable to open %s\n", input);
71 return (1);
72 }
73
74 if (fixMplNames) {
75 aux_symbol = malloc(sizeof(char *) * mcTable.n_auxiliaries);
76 for (i = 0; i < mcTable.n_auxiliaries; i++) {
77 len = strlen(mcTable.aux_name[i]);
78 aux_symbol[i] = malloc(sizeof(char) * (len + 1));
79 k = 0;
80 j = 0;
81 while (j < len) {
82 if (mcTable.aux_name[i][j] == '$') {
83 j++;
84 } else {
85 buffer[k] = mcTable.aux_name[i][j];
86 k++;
87 }
88 j++;
89 }
90 buffer[k] = 0;
91 sprintf(aux_symbol[i], "%s", buffer);
92 }
93
94 symbol = malloc(sizeof(char *) * mcTable.n_cols);
95 for (i = 0; i < mcTable.n_cols; i++) {
96 len = strlen(mcTable.name[i]);
97 symbol[i] = malloc(sizeof(char) * (len + 1));
98 k = 0;
99 j = 0;
100 while (j < len) {
101 if (mcTable.name[i][j] == '$') {
102 j++;
103 } else {
104 buffer[k] = mcTable.name[i][j];
105 k++;
106 }
107 j++;
108 }
109 buffer[k] = 0;
110 sprintf(symbol[i], "%s", buffer);
111 }
112 }
113
114 if (SDDS_InitializeOutput(&SDDS_table, SDDS_BINARY, 1, mcTable.title, mcTable.label, output) != 1) {
115 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
116 return (1);
117 }
118 for (i = 0; i < mcTable.n_auxiliaries; i++) {
119 if (fixMplNames) {
120 if (SDDS_DefineParameter1(&SDDS_table, aux_symbol[i], mcTable.aux_name[i], mcTable.aux_unit[i], mcTable.aux_description[i], NULL, SDDS_DOUBLE, &(mcTable.aux_value[i])) == -1) {
121 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
122 return (1);
123 }
124 } else {
125 if (SDDS_DefineParameter1(&SDDS_table, mcTable.aux_name[i], NULL, mcTable.aux_unit[i], mcTable.aux_description[i], NULL, SDDS_DOUBLE, &(mcTable.aux_value[i])) == -1) {
126 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
127 fprintf(stderr, "Try rerunning with the -fixMplNames option\n");
128 return (1);
129 }
130 }
131 }
132 for (i = 0; i < mcTable.n_cols; i++) {
133 if (fixMplNames) {
134 if (SDDS_DefineColumn(&SDDS_table, symbol[i], mcTable.name[i], mcTable.unit[i], mcTable.description[i], mcTable.format[i], SDDS_DOUBLE, 0) == -1) {
135 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
136 return (1);
137 }
138 } else {
139 if (SDDS_DefineColumn(&SDDS_table, mcTable.name[i], NULL, mcTable.unit[i], mcTable.description[i], mcTable.format[i], SDDS_DOUBLE, 0) == -1) {
140 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
141 fprintf(stderr, "Try rerunning with the -fixMplNames option\n");
142 return (1);
143 }
144 }
145 }
146
147 if (SDDS_SaveLayout(&SDDS_table) != 1) {
148 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
149 return (1);
150 }
151 if (SDDS_WriteLayout(&SDDS_table) != 1) {
152 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
153 return (1);
154 }
155
156 if (SDDS_StartTable(&SDDS_table, mcTable.n_rows) != 1) {
157 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
158 return (1);
159 }
160 for (i = 0; i < mcTable.n_cols; i++) {
161 if (SDDS_SetColumnFromDoubles(&SDDS_table, SDDS_SET_BY_INDEX, mcTable.value[i], mcTable.n_rows, i) != 1) {
162 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
163 return (1);
164 }
165 }
166 if (SDDS_WriteTable(&SDDS_table) != 1) {
167 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
168 return (1);
169 }
170 if (SDDS_Terminate(&SDDS_table) != 1) {
171 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
172 return (1);
173 }
174
175 return (0);
176}
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_SaveLayout(SDDS_DATASET *SDDS_dataset)
Definition SDDS_copy.c:615
int32_t SDDS_SetColumnFromDoubles(SDDS_DATASET *SDDS_dataset, int32_t mode, double *data, int64_t rows,...)
Sets the values for a single data column using double-precision floating-point numbers.
int32_t SDDS_Terminate(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_DefineParameter1(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, void *fixed_value)
Defines a data parameter with a fixed numerical value.
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_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.
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
#define SDDS_DOUBLE
Identifier for the double data type.
Definition SDDStypes.h:37
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26
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.
long get_mc_table(MC_TABLE *table, char *file, long flags)
Reads a multi-column table from a file.
Definition mcTable.c:42
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
Definition scanargs.c:36