86 {
88 SCANNED_ARG *scanned;
89 unsigned long majorOrderFlag;
90 long i_arg;
91 char *input, *output, *definition;
92 long hsize, vsize;
93 char *data, *data_name;
94 char ts1[100], ts2[100];
95 FILE *fpi;
96 short columnMajorOrder = 0;
97
98 argc =
scanargs(&scanned, argc, argv);
99 if (argc < 3) {
100 fprintf(stderr, "%s", USAGE);
101 return EXIT_FAILURE;
102 }
103
104 input = output = data_name = NULL;
105 hsize = DEFAULT_HSIZE;
106 vsize = DEFAULT_VSIZE;
107 definition = NULL;
108
109 for (i_arg = 1; i_arg < argc; i_arg++) {
110 if (scanned[i_arg].arg_type == OPTION) {
111
112 switch (
match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) {
113 case SET_MAJOR_ORDER:
114 majorOrderFlag = 0;
115 scanned[i_arg].n_items--;
116 if (scanned[i_arg].n_items > 0 &&
117 (!
scanItemList(&majorOrderFlag, scanned[i_arg].list + 1, &scanned[i_arg].n_items,
118 0, "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
119 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL))) {
120 fprintf(stderr, "Error: Invalid -majorOrder syntax or values.\n");
121 return EXIT_FAILURE;
122 }
123 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
124 columnMajorOrder = 1;
125 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
126 columnMajorOrder = 0;
127 break;
128
129 case SET_DEFINITION:
130 if (scanned[i_arg].n_items < 2) {
131 fprintf(stderr, "Error: -definition requires at least a name and one definition entry.\n");
132 fprintf(stderr, "%s", USAGE);
133 return EXIT_FAILURE;
134 }
135 data_name = scanned[i_arg].list[1];
136 definition = process_column_definition(scanned[i_arg].list + 1, scanned[i_arg].n_items - 1);
137 if (!definition) {
138 fprintf(stderr, "Error: Invalid column definition.\n");
139 return EXIT_FAILURE;
140 }
141 if (!strstr(definition, "type=character")) {
142 fprintf(stderr, "Error: Data type must be 'character' for now.\n");
143 return EXIT_FAILURE;
144 }
145 break;
146
147 case SET_SIZE:
148 if (scanned[i_arg].n_items != 3 ||
149 sscanf(scanned[i_arg].list[1], "%ld", &hsize) != 1 || hsize <= 0 ||
150 sscanf(scanned[i_arg].list[2], "%ld", &vsize) != 1 || vsize <= 0) {
151 fprintf(stderr, "Error: Invalid -size syntax.\n");
152 fprintf(stderr, "%s", USAGE);
153 return EXIT_FAILURE;
154 }
155 break;
156
157 default:
158 fprintf(stderr, "Error: Invalid option '%s'.\n", scanned[i_arg].list[0]);
159 fprintf(stderr, "%s", USAGE);
160 return EXIT_FAILURE;
161 }
162 } else {
163 if (!input)
164 input = scanned[i_arg].list[0];
165 else if (!output)
166 output = scanned[i_arg].list[0];
167 else {
168 fprintf(stderr, "Error: Too many filenames provided.\n");
169 fprintf(stderr, "%s", USAGE);
170 return EXIT_FAILURE;
171 }
172 }
173 }
174
175 if (!input) {
176 fprintf(stderr, "Error: Input file not specified.\n");
177 fprintf(stderr, "%s", USAGE);
178 return EXIT_FAILURE;
179 }
180
181 if (!output) {
182 fprintf(stderr, "Error: Output file not specified.\n");
183 fprintf(stderr, "%s", USAGE);
184 return EXIT_FAILURE;
185 }
186
187 if (!definition) {
188 fprintf(stderr, "Error: Column definition not specified.\n");
189 fprintf(stderr, "%s", USAGE);
190 return EXIT_FAILURE;
191 }
192
193 snprintf(ts1, sizeof(ts1), "%ld", hsize);
194 snprintf(ts2, sizeof(ts2), "%ld", vsize);
195
197 "Screen image from raw file", "screen image",
198 output) ||
203 return EXIT_FAILURE;
204 }
205
206 SDDS_table.layout.data_mode.column_major = columnMajorOrder;
207
208 if (!
SDDS_WriteLayout(&SDDS_table) || !SDDS_StartTable(&SDDS_table, hsize * vsize)) {
210 return EXIT_FAILURE;
211 }
212
213 data =
tmalloc(
sizeof(*data) * hsize * vsize);
215 if (fread(data, sizeof(*data), hsize * vsize, fpi) != (size_t)(hsize * vsize)) {
216 fclose(fpi);
217 fprintf(stderr, "Error: Unable to read all data from input file '%s'.\n", input);
218 return EXIT_FAILURE;
219 }
220 fclose(fpi);
221
222 if (!
SDDS_SetColumn(&SDDS_table, SDDS_SET_BY_NAME, data, hsize * vsize, data_name) ||
223 !SDDS_WriteTable(&SDDS_table) ||
226 return EXIT_FAILURE;
227 }
228
229 free(data);
230 return EXIT_SUCCESS;
231}
int32_t SDDS_SetColumn(SDDS_DATASET *SDDS_dataset, int32_t mode, void *data, int64_t rows,...)
Sets the values for one data column in the current data table of an 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_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
int32_t SDDS_DefineParameter(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, char *fixed_value)
Defines a data parameter with a fixed string value.
int32_t SDDS_ProcessColumnString(SDDS_DATASET *SDDS_dataset, char *string, int32_t mode)
Process a column definition string.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
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)
long scanItemList(unsigned long *flags, char **item, long *items, unsigned long mode,...)
Scans a list of items and assigns values based on provided keywords and types.