91 {
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)) {
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)) {
126 }
127 break;
128 case CLO_ZEROESOF:
129 if (s_arg[i_arg].n_items != 2) {
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) {
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) {
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 {
162 }
163 }
164 }
165
167
168 if (!zero_name) {
169 SDDS_Bomb(
"-zeroesOf option must be given");
170 }
171
174 }
175
176 if (!resolve_column_names(&in_set, zero_name, &column_name, &column_names) ||
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]);
189 }
190 }
191
194 }
195
196 indep_data =
tmalloc(
sizeof(*indep_data) * column_names);
197 slope_data =
tmalloc(
sizeof(*slope_data) * column_names);
198
202 }
203
206 if (!depen_data) {
208 }
209
210 for (int i = 0; i < column_names; i++) {
212 if (!indep_data[i]) {
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) {
254 }
255
256 for (int i = 0; i < column_names; i++) {
257 snprintf(s, SDDS_MAXLINE, "%sSlope", column_name[i]);
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
278 }
279 }
280
283 exit(1);
284 }
285
286 return 0;
287}
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.