SDDS ToolKit Programs and Libraries for C and Python
All Classes Files Functions Variables Macros Pages
sddszerofind.c
Go to the documentation of this file.
1/**
2 * @file sddszerofind.c
3 * @brief A program to identify zero crossings in a specified column of an SDDS file.
4 *
5 * @details
6 * This program processes SDDS files to find zero-crossing points in one column as a function of another column.
7 * It performs interpolation to compute zero positions and optionally calculates the slope at each zero.
8 * The output can be configured to include additional slope information and different ordering formats.
9 *
10 * @section Usage
11 * ```
12 * sddszerofind [<inputfile>] [<outputfile>]
13 * [-pipe=[input][,output]]
14 * -zeroesOf=<columnName>
15 * [-columns=<columnNames>]
16 * [-offset=<value>]
17 * [-slopeOutput]
18 * [-majorOrder=row|column]
19 * ```
20 *
21 * @section Options
22 * | Required | Description |
23 * |---------------------------------------|---------------------------------------------------------------------------------------|
24 * | `-zeroesOf` | Specifies the column to find zero crossings. |
25 *
26 * | Optional | Description |
27 * |---------------------------------------|---------------------------------------------------------------------------------------|
28 * | `-pipe` | Enables input and/or output through a pipe. |
29 * | `-columns` | Specifies columns for interpolation (default: all numerical columns). |
30 * | `-offset` | Adjusts the zero-finding threshold by adding an offset. |
31 * | `-slopeOutput` | Includes the slope at zero-crossing points in the output. |
32 * | `-majorOrder` | Configures the output ordering in row or column-major format. |
33 *
34 * @copyright
35 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
36 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
37 *
38 * @license
39 * This file is distributed under the terms of the Software License Agreement
40 * found in the file LICENSE included with this distribution.
41 *
42 * @author
43 * M. Borland, C. Saunders, R. Soliday, H. Shang
44 */
45
46#include "mdb.h"
47#include "SDDS.h"
48#include "scan.h"
49
50typedef enum {
51 CLO_PIPE,
52 CLO_COLUMNS,
53 CLO_SLOPEOUTPUT,
54 CLO_ZEROESOF,
55 CLO_OFFSET,
56 CLO_MAJOR_ORDER,
57 N_OPTIONS
58} option_type;
59
60char *option[N_OPTIONS] =
61 {
62 "pipe",
63 "columns",
64 "slopeoutput",
65 "zeroesof",
66 "offset",
67 "majorOrder",
68 };
69
70static char *USAGE =
71 "sddszerofind [<inputfile>] [<outputfile>] [-pipe=[input][,output]]\n"
72 "-zeroesOf=<columnName> [-columns=<columnNames>] [-offset=<value>] "
73 "[-slopeOutput] [-majorOrder=row|column]\n\n"
74 "Finds values of columns of data at interpolated zero positions in another\n"
75 "column.\n\n"
76 "-zeroesOf Specifies the column for which to find zeroes.\n"
77 "-offset Specifies a value to add to the values of the -zeroesOf column\n"
78 " prior to finding the zeroes. -offset=1 will find places where\n"
79 " the original values are -1.\n"
80 "-columns Specifies the columns to interpolate at the zero positions.\n"
81 " Default is all numerical columns in the file.\n"
82 "-majorOrder Specify output file in row or column order.\n"
83 "-slopeOutput Provide output of the slope of each -column column at the zero\n"
84 " position.\n\n"
85 "Program by Michael Borland. (" __DATE__ " " __TIME__ ", SVN revision: " SVN_VERSION ")\n";
86
87#define FL_SLOPEOUTPUT 0x00001UL
88
89long resolve_column_names(SDDS_DATASET *SDDSin, char *depen_quantity, char ***indep_quantity, int32_t *indep_quantities);
90
91int main(int argc, char **argv) {
92 SDDS_DATASET in_set, out_set;
93 SCANNED_ARG *s_arg;
94 char *input = NULL, *output = NULL, *zero_name = NULL, **column_name = NULL;
95 long i_arg, page_returned;
96 int64_t rows, zrow;
97 int32_t column_names = 0;
98 double **indep_data, *depen_data, **slope_data, slope, offset = 0;
99 unsigned long pipe_flags = 0, flags = 0, major_order_flag;
100 char s[SDDS_MAXLINE];
101 short column_major_order = -1;
102
104 argc = scanargs(&s_arg, argc, argv);
105 if (argc < 2 || argc > (2 + N_OPTIONS)) {
106 bomb(NULL, USAGE);
107 }
108
109 for (i_arg = 1; i_arg < argc; i_arg++) {
110 if (s_arg[i_arg].arg_type == OPTION) {
111 switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
112 case CLO_MAJOR_ORDER:
113 major_order_flag = 0;
114 s_arg[i_arg].n_items--;
115 if (s_arg[i_arg].n_items > 0 &&
116 (!scanItemList(&major_order_flag, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
117 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
118 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL))) {
119 SDDS_Bomb("invalid -majorOrder syntax/values");
120 }
121 column_major_order = (major_order_flag & SDDS_COLUMN_MAJOR_ORDER) ? 1 : 0;
122 break;
123 case CLO_PIPE:
124 if (!processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipe_flags)) {
125 SDDS_Bomb("invalid -pipe syntax");
126 }
127 break;
128 case CLO_ZEROESOF:
129 if (s_arg[i_arg].n_items != 2) {
130 SDDS_Bomb("invalid -zeroesOf syntax");
131 }
132 zero_name = s_arg[i_arg].list[1];
133 break;
134 case CLO_COLUMNS:
135 if (s_arg[i_arg].n_items < 2) {
136 SDDS_Bomb("invalid -columns syntax");
137 }
138 column_name = tmalloc(sizeof(*column_name) * (column_names = s_arg[i_arg].n_items - 1));
139 for (int i = 0; i < column_names; i++) {
140 column_name[i] = s_arg[i_arg].list[i + 1];
141 }
142 break;
143 case CLO_SLOPEOUTPUT:
144 flags |= FL_SLOPEOUTPUT;
145 break;
146 case CLO_OFFSET:
147 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1], "%le", &offset) != 1) {
148 SDDS_Bomb("invalid -offset syntax");
149 }
150 break;
151 default:
152 fprintf(stderr, "Error (%s): unknown/ambiguous option: %s\n", argv[0], s_arg[i_arg].list[0]);
153 exit(1);
154 }
155 } else {
156 if (input == NULL) {
157 input = s_arg[i_arg].list[0];
158 } else if (output == NULL) {
159 output = s_arg[i_arg].list[0];
160 } else {
161 SDDS_Bomb("too many filenames");
162 }
163 }
164 }
165
166 processFilenames("sddszerofind", &input, &output, pipe_flags, 0, NULL);
167
168 if (!zero_name) {
169 SDDS_Bomb("-zeroesOf option must be given");
170 }
171
172 if (!SDDS_InitializeInput(&in_set, input)) {
173 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
174 }
175
176 if (!resolve_column_names(&in_set, zero_name, &column_name, &column_names) ||
177 !SDDS_InitializeOutput(&out_set, SDDS_BINARY, 0, NULL, "sddszerofind output", output) ||
178 !SDDS_TransferColumnDefinition(&out_set, &in_set, zero_name, NULL)) {
179 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
180 }
181
182 out_set.layout.data_mode.column_major = (column_major_order != -1) ? column_major_order : in_set.layout.data_mode.column_major;
183
184 for (int i = 0; i < column_names; i++) {
185 snprintf(s, SDDS_MAXLINE, "%sSlope", column_name[i]);
186 if (!SDDS_TransferColumnDefinition(&out_set, &in_set, column_name[i], NULL) ||
187 (flags & FL_SLOPEOUTPUT && !SDDS_TransferColumnDefinition(&out_set, &in_set, column_name[i], s))) {
188 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
189 }
190 }
191
192 if (!SDDS_WriteLayout(&out_set)) {
193 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
194 }
195
196 indep_data = tmalloc(sizeof(*indep_data) * column_names);
197 slope_data = tmalloc(sizeof(*slope_data) * column_names);
198
199 while ((page_returned = SDDS_ReadPage(&in_set)) > 0) {
200 if (!SDDS_StartPage(&out_set, 0)) {
201 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
202 }
203
204 if ((rows = SDDS_CountRowsOfInterest(&in_set)) > 1) {
205 depen_data = SDDS_GetColumnInDoubles(&in_set, zero_name);
206 if (!depen_data) {
207 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
208 }
209
210 for (int i = 0; i < column_names; i++) {
211 indep_data[i] = SDDS_GetColumnInDoubles(&in_set, column_name[i]);
212 if (!indep_data[i]) {
213 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
214 }
215
216 if (flags & FL_SLOPEOUTPUT) {
217 slope_data[i] = tmalloc(sizeof(**slope_data) * rows);
218 }
219 }
220
221 if (offset) {
222 for (int row = 0; row < rows; row++) {
223 depen_data[row] += offset;
224 }
225 }
226
227 zrow = 0;
228 for (int row = 0; row < rows - 1; row++) {
229 if ((depen_data[row] <= 0 && depen_data[row + 1] >= 0) ||
230 (depen_data[row] >= 0 && depen_data[row + 1] <= 0)) {
231 for (int i = 0; i < column_names; i++) {
232 if (indep_data[i][row] == indep_data[i][row + 1]) {
233 if (flags & FL_SLOPEOUTPUT) {
234 slope_data[i][zrow] = DBL_MAX;
235 }
236 indep_data[i][zrow] = indep_data[i][row];
237 } else {
238 slope = (depen_data[row + 1] - depen_data[row]) / (indep_data[i][row + 1] - indep_data[i][row]);
239 if (flags & FL_SLOPEOUTPUT) {
240 slope_data[i][zrow] = slope;
241 }
242 indep_data[i][zrow] = (slope) ? (indep_data[i][row] - depen_data[row] / slope) : ((indep_data[i][row] + indep_data[i][row + 1]) / 2);
243 }
244 }
245 depen_data[zrow] = -offset;
246 zrow++;
247 }
248 }
249
250 if (zrow) {
251 if (!SDDS_LengthenTable(&out_set, zrow) ||
252 !SDDS_SetColumnFromDoubles(&out_set, SDDS_SET_BY_NAME, depen_data, zrow, zero_name)) {
253 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
254 }
255
256 for (int i = 0; i < column_names; i++) {
257 snprintf(s, SDDS_MAXLINE, "%sSlope", column_name[i]);
258 if (!SDDS_SetColumnFromDoubles(&out_set, SDDS_SET_BY_NAME, indep_data[i], zrow, column_name[i]) ||
259 (flags & FL_SLOPEOUTPUT && !SDDS_SetColumnFromDoubles(&out_set, SDDS_SET_BY_NAME, slope_data[i], zrow, s))) {
260 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
261 }
262 }
263 }
264
265 free(depen_data);
266 for (int i = 0; i < column_names; i++) {
267 free(indep_data[i]);
268 }
269 if (flags & FL_SLOPEOUTPUT) {
270 for (int i = 0; i < column_names; i++) {
271 free(slope_data[i]);
272 }
273 }
274 }
275
276 if (!SDDS_WritePage(&out_set)) {
277 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
278 }
279 }
280
281 if (!SDDS_Terminate(&in_set) || !SDDS_Terminate(&out_set)) {
282 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
283 exit(1);
284 }
285
286 return 0;
287}
288
289long resolve_column_names(SDDS_DATASET *SDDSin, char *depen_quantity, char ***indep_quantity, int32_t *indep_quantities) {
290 long index;
291 char s[SDDS_MAXLINE];
292
293 index = SDDS_GetColumnIndex(SDDSin, depen_quantity);
294 if (index < 0 || !SDDS_NUMERIC_TYPE(SDDS_GetColumnType(SDDSin, index))) {
295 snprintf(s, SDDS_MAXLINE, "column %s is non-existent or non-numeric", depen_quantity);
296 SDDS_SetError(s);
297 return 0;
298 }
299
300 if (*indep_quantities) {
301 SDDS_SetColumnFlags(SDDSin, 0);
302 for (long i = 0; i < *indep_quantities; i++) {
303 if (!SDDS_SetColumnsOfInterest(SDDSin, SDDS_MATCH_STRING, (*indep_quantity)[i], SDDS_OR)) {
304 return 0;
305 }
306 }
307 } else {
308 SDDS_SetColumnFlags(SDDSin, 1);
309 if (!SDDS_SetColumnsOfInterest(SDDSin, SDDS_MATCH_STRING, depen_quantity, SDDS_NEGATE_MATCH | SDDS_AND)) {
310 return 0;
311 }
312 *indep_quantity = SDDS_GetColumnNames(SDDSin, indep_quantities);
313 if (!(*indep_quantity) || *indep_quantities == 0) {
314 SDDS_SetError("no independent quantities found");
315 return 0;
316 }
317 for (long i = 0; i < *indep_quantities; i++) {
318 index = SDDS_GetColumnIndex(SDDSin, (*indep_quantity)[i]);
319 if (!SDDS_NUMERIC_TYPE(SDDS_GetColumnType(SDDSin, index)) &&
320 !SDDS_AssertColumnFlags(SDDSin, SDDS_INDEX_LIMITS, index, index, 0)) {
321 return 0;
322 }
323 }
324 }
325
326 free(*indep_quantity);
327 *indep_quantity = SDDS_GetColumnNames(SDDSin, indep_quantities);
328 if (!(*indep_quantity) || *indep_quantities == 0) {
329 SDDS_SetError("no independent quantities found");
330 return 0;
331 }
332 return 1;
333}
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_LengthenTable(SDDS_DATASET *SDDS_dataset, int64_t n_additional_rows)
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
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_AssertColumnFlags(SDDS_DATASET *SDDS_dataset, uint32_t mode,...)
Sets acceptance flags for columns based on specified criteria.
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_SetColumnsOfInterest(SDDS_DATASET *SDDS_dataset, int32_t mode,...)
Sets the acceptance flags for columns based on specified naming criteria.
int32_t SDDS_SetColumnFlags(SDDS_DATASET *SDDS_dataset, int32_t column_flag_value)
Sets the acceptance flags for all columns in the current data table of a data set.
double * SDDS_GetColumnInDoubles(SDDS_DATASET *SDDS_dataset, char *column_name)
Retrieves the data of a specified numerical column as an array of doubles, considering only rows mark...
int32_t SDDS_InitializeInput(SDDS_DATASET *SDDS_dataset, char *filename)
Definition SDDS_input.c:49
int32_t SDDS_Terminate(SDDS_DATASET *SDDS_dataset)
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_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
int32_t SDDS_TransferColumnDefinition(SDDS_DATASET *target, SDDS_DATASET *source, char *name, char *newName)
Transfers a column definition from a source dataset to a target dataset.
void SDDS_SetError(char *error_text)
Records an error message in the SDDS error stack.
Definition SDDS_utils.c:379
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column in the SDDS dataset.
char ** SDDS_GetColumnNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all columns 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
int32_t SDDS_GetColumnType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of a column in the SDDS dataset by its index.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
Definition SDDS_utils.c:342
#define SDDS_NUMERIC_TYPE(type)
Checks if the given type identifier corresponds to any numeric type.
Definition SDDStypes.h:138
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 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
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
Definition scanargs.c:390
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.