58char *option[N_OPTIONS] =
69 "sddszerofind [<inputfile>] [<outputfile>] [-pipe=[input][,output]]\n"
70 "-zeroesOf=<columnName> [-columns=<columnNames>] [-offset=<value>] "
71 "[-slopeOutput] [-majorOrder=row|column]\n\n"
72 "Finds values of columns of data at interpolated zero positions in another\n"
74 "-zeroesOf Specifies the column for which to find zeroes.\n"
75 "-offset Specifies a value to add to the values of the -zeroesOf column\n"
76 " prior to finding the zeroes. -offset=1 will find places where\n"
77 " the original values are -1.\n"
78 "-columns Specifies the columns to interpolate at the zero positions.\n"
79 " Default is all numerical columns in the file.\n"
80 "-majorOrder Specify output file in row or column order.\n"
81 "-slopeOutput Provide output of the slope of each -column column at the zero\n"
83 "Program by Michael Borland. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
85#define FL_SLOPEOUTPUT 0x00001UL
87long resolve_column_names(
SDDS_DATASET *SDDSin,
char *depen_quantity,
char ***indep_quantity, int32_t *indep_quantities);
89int main(
int argc,
char **argv) {
92 char *input = NULL, *output = NULL, *zero_name = NULL, **column_name = NULL;
93 long i_arg, page_returned;
95 int32_t column_names = 0;
96 double **indep_data, *depen_data, **slope_data, slope, offset = 0;
97 unsigned long pipe_flags = 0, flags = 0, major_order_flag;
99 short column_major_order = -1;
102 argc =
scanargs(&s_arg, argc, argv);
103 if (argc < 2 || argc > (2 + N_OPTIONS)) {
107 for (i_arg = 1; i_arg < argc; i_arg++) {
108 if (s_arg[i_arg].arg_type == OPTION) {
109 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
110 case CLO_MAJOR_ORDER:
111 major_order_flag = 0;
112 s_arg[i_arg].n_items--;
113 if (s_arg[i_arg].n_items > 0 &&
114 (!
scanItemList(&major_order_flag, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
115 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
116 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL))) {
117 SDDS_Bomb(
"invalid -majorOrder syntax/values");
119 column_major_order = (major_order_flag & SDDS_COLUMN_MAJOR_ORDER) ? 1 : 0;
122 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipe_flags)) {
127 if (s_arg[i_arg].n_items != 2) {
130 zero_name = s_arg[i_arg].list[1];
133 if (s_arg[i_arg].n_items < 2) {
136 column_name =
tmalloc(
sizeof(*column_name) * (column_names = s_arg[i_arg].n_items - 1));
137 for (
int i = 0; i < column_names; i++) {
138 column_name[i] = s_arg[i_arg].list[i + 1];
141 case CLO_SLOPEOUTPUT:
142 flags |= FL_SLOPEOUTPUT;
145 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1],
"%le", &offset) != 1) {
150 fprintf(stderr,
"Error (%s): unknown/ambiguous option: %s\n", argv[0], s_arg[i_arg].list[0]);
155 input = s_arg[i_arg].list[0];
156 }
else if (output == NULL) {
157 output = s_arg[i_arg].list[0];
167 SDDS_Bomb(
"-zeroesOf option must be given");
174 if (!resolve_column_names(&in_set, zero_name, &column_name, &column_names) ||
180 out_set.layout.data_mode.column_major = (column_major_order != -1) ? column_major_order : in_set.layout.data_mode.column_major;
182 for (
int i = 0; i < column_names; i++) {
183 snprintf(s, SDDS_MAXLINE,
"%sSlope", column_name[i]);
194 indep_data =
tmalloc(
sizeof(*indep_data) * column_names);
195 slope_data =
tmalloc(
sizeof(*slope_data) * column_names);
208 for (
int i = 0; i < column_names; i++) {
210 if (!indep_data[i]) {
214 if (flags & FL_SLOPEOUTPUT) {
215 slope_data[i] =
tmalloc(
sizeof(**slope_data) * rows);
220 for (
int row = 0; row < rows; row++) {
221 depen_data[row] += offset;
226 for (
int row = 0; row < rows - 1; row++) {
227 if ((depen_data[row] <= 0 && depen_data[row + 1] >= 0) ||
228 (depen_data[row] >= 0 && depen_data[row + 1] <= 0)) {
229 for (
int i = 0; i < column_names; i++) {
230 if (indep_data[i][row] == indep_data[i][row + 1]) {
231 if (flags & FL_SLOPEOUTPUT) {
232 slope_data[i][zrow] = DBL_MAX;
234 indep_data[i][zrow] = indep_data[i][row];
236 slope = (depen_data[row + 1] - depen_data[row]) / (indep_data[i][row + 1] - indep_data[i][row]);
237 if (flags & FL_SLOPEOUTPUT) {
238 slope_data[i][zrow] = slope;
240 indep_data[i][zrow] = (slope) ? (indep_data[i][row] - depen_data[row] / slope) : ((indep_data[i][row] + indep_data[i][row + 1]) / 2);
243 depen_data[zrow] = -offset;
254 for (
int i = 0; i < column_names; i++) {
255 snprintf(s, SDDS_MAXLINE,
"%sSlope", column_name[i]);
264 for (
int i = 0; i < column_names; i++) {
267 if (flags & FL_SLOPEOUTPUT) {
268 for (
int i = 0; i < column_names; i++) {
287long resolve_column_names(
SDDS_DATASET *SDDSin,
char *depen_quantity,
char ***indep_quantity, int32_t *indep_quantities) {
289 char s[SDDS_MAXLINE];
293 snprintf(s, SDDS_MAXLINE,
"column %s is non-existent or non-numeric", depen_quantity);
298 if (*indep_quantities) {
300 for (
long i = 0; i < *indep_quantities; i++) {
311 if (!(*indep_quantity) || *indep_quantities == 0) {
315 for (
long i = 0; i < *indep_quantities; i++) {
324 free(*indep_quantity);
326 if (!(*indep_quantity) || *indep_quantities == 0) {
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_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.
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.
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
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.
#define SDDS_NUMERIC_TYPE(type)
Checks if the given type identifier corresponds to any numeric type.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
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 processPipeOption(char **item, long items, unsigned long *flags)
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
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.