89 {
91 SCANNED_ARG *s_arg;
92 char *input = NULL, *output = NULL, *zero_name = NULL, **column_name = NULL;
93 long i_arg, page_returned;
94 int64_t rows, zrow;
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;
98 char s[SDDS_MAXLINE];
99 short column_major_order = -1;
100
102 argc =
scanargs(&s_arg, argc, argv);
103 if (argc < 2 || argc > (2 + N_OPTIONS)) {
105 }
106
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");
118 }
119 column_major_order = (major_order_flag & SDDS_COLUMN_MAJOR_ORDER) ? 1 : 0;
120 break;
121 case CLO_PIPE:
122 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipe_flags)) {
124 }
125 break;
126 case CLO_ZEROESOF:
127 if (s_arg[i_arg].n_items != 2) {
129 }
130 zero_name = s_arg[i_arg].list[1];
131 break;
132 case CLO_COLUMNS:
133 if (s_arg[i_arg].n_items < 2) {
135 }
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];
139 }
140 break;
141 case CLO_SLOPEOUTPUT:
142 flags |= FL_SLOPEOUTPUT;
143 break;
144 case CLO_OFFSET:
145 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1], "%le", &offset) != 1) {
147 }
148 break;
149 default:
150 fprintf(stderr, "Error (%s): unknown/ambiguous option: %s\n", argv[0], s_arg[i_arg].list[0]);
151 exit(1);
152 }
153 } else {
154 if (input == NULL) {
155 input = s_arg[i_arg].list[0];
156 } else if (output == NULL) {
157 output = s_arg[i_arg].list[0];
158 } else {
160 }
161 }
162 }
163
165
166 if (!zero_name) {
167 SDDS_Bomb(
"-zeroesOf option must be given");
168 }
169
172 }
173
174 if (!resolve_column_names(&in_set, zero_name, &column_name, &column_names) ||
178 }
179
180 out_set.layout.data_mode.column_major = (column_major_order != -1) ? column_major_order : in_set.layout.data_mode.column_major;
181
182 for (int i = 0; i < column_names; i++) {
183 snprintf(s, SDDS_MAXLINE, "%sSlope", column_name[i]);
187 }
188 }
189
192 }
193
194 indep_data =
tmalloc(
sizeof(*indep_data) * column_names);
195 slope_data =
tmalloc(
sizeof(*slope_data) * column_names);
196
200 }
201
204 if (!depen_data) {
206 }
207
208 for (int i = 0; i < column_names; i++) {
210 if (!indep_data[i]) {
212 }
213
214 if (flags & FL_SLOPEOUTPUT) {
215 slope_data[i] =
tmalloc(
sizeof(**slope_data) * rows);
216 }
217 }
218
219 if (offset) {
220 for (int row = 0; row < rows; row++) {
221 depen_data[row] += offset;
222 }
223 }
224
225 zrow = 0;
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;
233 }
234 indep_data[i][zrow] = indep_data[i][row];
235 } else {
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;
239 }
240 indep_data[i][zrow] = (slope) ? (indep_data[i][row] - depen_data[row] / slope) : ((indep_data[i][row] + indep_data[i][row + 1]) / 2);
241 }
242 }
243 depen_data[zrow] = -offset;
244 zrow++;
245 }
246 }
247
248 if (zrow) {
252 }
253
254 for (int i = 0; i < column_names; i++) {
255 snprintf(s, SDDS_MAXLINE, "%sSlope", column_name[i]);
259 }
260 }
261 }
262
263 free(depen_data);
264 for (int i = 0; i < column_names; i++) {
265 free(indep_data[i]);
266 }
267 if (flags & FL_SLOPEOUTPUT) {
268 for (int i = 0; i < column_names; i++) {
269 free(slope_data[i]);
270 }
271 }
272 }
273
276 }
277 }
278
281 exit(1);
282 }
283
284 return 0;
285}
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_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.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
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.