104 {
105 SCANNED_ARG *s_arg;
106 double *output_col;
107 char *input_col_name, *match_col_name, *inputfile, *outputfile;
108 long i, i_arg, delay, tmp_file_used;
109 unsigned long pipe_flags, major_order_flag;
110 char actual_name[256], actual_desc[256], delay_name[256];
111 double sim_delay, lower, upper, final_mse;
113 short column_major_order = -1;
114 char **input_col_names = NULL;
115 int32_t n_input_col_names = 0;
116
118 argc =
scanargs(&s_arg, argc, argv);
119 if (argc < 2) {
121 }
122
123
124 tmp_file_used = 0;
125 pipe_flags = 0;
126 inputfile = outputfile = input_col_name = match_col_name = NULL;
127 delay = 0;
128 sim_delay = 0.0;
129 zero = circular = 0;
130 input_col = match_col = output_col = working = NULL;
131
132
133 for (i_arg = 1; i_arg < argc; i_arg++) {
134 if (s_arg[i_arg].arg_type == OPTION) {
136 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
137 case CLO_MAJOR_ORDER:
138 major_order_flag = 0;
139 s_arg[i_arg].n_items--;
140 if (s_arg[i_arg].n_items > 0 &&
141 (!
scanItemList(&major_order_flag, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
142 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
143 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL))) {
144 SDDS_Bomb(
"Invalid -majorOrder syntax/values");
145 }
146 if (major_order_flag & SDDS_COLUMN_MAJOR_ORDER) {
147 column_major_order = 1;
148 } else if (major_order_flag & SDDS_ROW_MAJOR_ORDER) {
149 column_major_order = 0;
150 }
151 break;
152 case CLO_PIPE:
153 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipe_flags)) {
155 }
156 break;
157 case CLO_COLUMNS:
158 if (s_arg[i_arg].n_items < 2) {
160 }
161 if (n_input_col_names) {
162 SDDS_Bomb(
"Invalid syntax: specify -columns once only");
163 }
164 input_col_names =
tmalloc(
sizeof(*input_col_names) * (s_arg[i_arg].n_items - 1));
165 for (i = 1; i < s_arg[i_arg].n_items; i++) {
166 input_col_names[i - 1] = s_arg[i_arg].list[i];
167 }
168 n_input_col_names = s_arg[i_arg].n_items - 1;
169 break;
170 case CLO_DELAY:
171 if (s_arg[i_arg].n_items != 2) {
173 }
174 if (!
get_long(&delay, s_arg[i_arg].list[1])) {
175 SDDS_Bomb(
"Invalid delay value provided.");
176 }
177 break;
178 case CLO_MATCH:
179 if (s_arg[i_arg].n_items != 2) {
181 }
182 match_col_name = s_arg[i_arg].list[1];
183 break;
184 case CLO_ZERO:
185 zero = 1;
186 break;
187 case CLO_CIRCULAR:
188 circular = 1;
189 break;
190 default:
191 fprintf(stderr, "Error (%s): unknown switch: %s\n", argv[0], s_arg[i_arg].list[0]);
192 exit(EXIT_FAILURE);
193 }
194 } else {
195 if (!inputfile) {
196 inputfile = s_arg[i_arg].list[0];
197 } else if (!outputfile) {
198 outputfile = s_arg[i_arg].list[0];
199 } else {
201 }
202 }
203 }
204
205 processFilenames(
"sddsshift", &inputfile, &outputfile, pipe_flags, 1, &tmp_file_used);
206
207 if (zero && circular) {
208 SDDS_Bomb(
"The -zero and -circular options are mutually exclusive.");
209 }
210
211 if (n_input_col_names == 0) {
212 SDDS_Bomb(
"A shift column is not given!");
213 }
214 if (!match_col_name && !delay) {
215 SDDS_Bomb(
"Either match column or shift should be provided.");
216 }
217 if (match_col_name && delay) {
218 SDDS_Bomb(
"-match column option and -shift option are incompatible.");
219 }
220
221
224 }
225
226 if (!resolve_column_names(&SDDS_input, &input_col_names, &n_input_col_names)) {
228 }
229
232 exit(EXIT_FAILURE);
233 }
234
235 SDDS_output.layout.data_mode.column_major = column_major_order != -1 ? column_major_order : SDDS_input.layout.data_mode.column_major;
236
237 for (i = 0; i < n_input_col_names; i++) {
238 snprintf(actual_name, sizeof(actual_name), "Shifted%s", input_col_names[i]);
239 snprintf(actual_desc, sizeof(actual_desc), "Shifted %s", input_col_names[i]);
240 snprintf(delay_name, sizeof(delay_name), "%sShift", input_col_names[i]);
244 exit(EXIT_FAILURE);
245 }
246 }
247
250 exit(EXIT_FAILURE);
251 }
252
256 }
257
259 if (npoints < 0) {
261 }
262
263 for (i = 0; i < n_input_col_names; i++) {
264 snprintf(actual_name, sizeof(actual_name), "Shifted%s", input_col_names[i]);
265 snprintf(delay_name, sizeof(delay_name), "%sShift", input_col_names[i]);
267 if (!input_col) {
269 }
270
271 output_col = calloc(npoints, sizeof(*output_col));
272 if (!output_col) {
274 }
275
276 if (match_col_name) {
278 if (!match_col) {
280 }
281 working = calloc(npoints, sizeof(*working));
282 if (!working) {
284 }
285 lower = -((double)npoints - 1);
286 upper = (double)npoints - 1;
287 simplexMin(&final_mse, &sim_delay, NULL, &lower, &upper, NULL, 1, 1e-6, 1e-12, simplex_driver, NULL, 2 * npoints, 6, 12, 3, 1.0, 0);
288 delay = (long)sim_delay;
289 free(working);
290 free(match_col);
291 match_col = working = NULL;
292 }
293
294 shift(input_col, output_col, npoints, delay, zero, circular);
295 if (!
SDDS_SetParameters(&SDDS_output, SDDS_SET_BY_NAME | SDDS_PASS_BY_VALUE, delay_name, delay, NULL) ||
296 !
SDDS_SetColumn(&SDDS_output, SDDS_SET_BY_NAME, output_col, npoints, actual_name)) {
298 }
299
300 free(output_col);
301 free(input_col);
302 output_col = input_col = NULL;
303 }
304
307 }
308 }
309
312 }
313
315 return EXIT_SUCCESS;
316}
int32_t SDDS_InitializeCopy(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, char *filename, char *filemode)
int32_t SDDS_CopyPage(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_SetParameters(SDDS_DATASET *SDDS_dataset, int32_t mode,...)
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_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
int32_t SDDS_DefineColumn(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, int32_t field_length)
Defines a data column within the SDDS 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.
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.
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
#define SDDS_DOUBLE
Identifier for the double data 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.
int get_long(long *iptr, char *s)
Parses a long integer value from the given string.
char * delete_chars(char *s, char *t)
Removes all occurrences of characters found in string t from string s.
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)
void free_scanargs(SCANNED_ARG **scanned, int argc)
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.
long simplexMin(double *yReturn, double *xGuess, double *dxGuess, double *xLowerLimit, double *xUpperLimit, short *disable, long dimensions, double target, double tolerance, double(*func)(double *x, long *invalid), void(*report)(double ymin, double *xmin, long pass, long evals, long dims), long maxEvaluations, long maxPasses, long maxDivisions, double divisorFactor, double passRangeFactor, unsigned long flags)
Top-level convenience function for simplex-based minimization.