105 {
106 SCANNED_ARG *s_arg;
107 SDDS_DATASET modDataSet, valDataSet, dataDataSet, outDataSet;
108 unsigned long modFlags, valFlags, majorOrderFlag;
109 char *modAbscissaName, *modOrdinateName;
110 char *valAbscissaName;
111 char *dataAbscissaName, *dataOrdinateName;
112 char *outAbscissaName, *outOrdinateName;
113 char *modFile, *dataFile, *valFile, *outFile;
114 long dataOrder;
115 int32_t modOrder;
116 double *modAbscissa, *modOrdinate;
117 double *valAbscissa;
118 double *dataAbscissa, *dataOrdinate, *dataScale, *dataOrdinateInterp;
119 double *outScale, *outAbscissa, *outOrdinate;
120 int64_t i, modRows, valRows, dataRows, outRows;
121
122 long i_arg;
123
124 long warning;
125 long ascii;
126 long returnCode;
127 unsigned long pipeFlags;
128 long tmpfile_used, noWarnings;
129 short columnMajorOrder = -1;
130
132 argc =
scanargs(&s_arg, argc, argv);
133 if (argc == 1)
135
136
137
138 dataFile = dataAbscissaName = dataOrdinateName = modFile = valFile = NULL;
139 modOrdinate = dataOrdinate = NULL;
140 outFile = NULL;
141 warning = 0;
142 modOrder = DEFAULT_ORDER;
143 dataOrder = DEFAULT_ORDER;
144 ascii = 0;
145 modFlags = 0x0UL;
146 valFlags = 0x0UL;
147 pipeFlags = 0;
148 tmpfile_used = 0;
149 noWarnings = 0;
150
151 for (i_arg = 1; i_arg < argc; i_arg++) {
152 if (s_arg[i_arg].arg_type == OPTION) {
153 switch (
match_string(s_arg[i_arg].list[0], commandline_option, N_OPTIONS, UNIQUE_MATCH)) {
154 case CLO_MAJOR_ORDER:
155 majorOrderFlag = 0;
156 s_arg[i_arg].n_items--;
157 if (s_arg[i_arg].n_items > 0 &&
158 !
scanItemList(&majorOrderFlag, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
159 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
160 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER,
161 NULL))
162 SDDS_Bomb(
"Invalid -majorOrder syntax or values");
163 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
164 columnMajorOrder = 1;
165 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
166 columnMajorOrder = 0;
167 break;
168 case CLO_VERBOSE:
169
170 break;
171 case CLO_ASCII:
172 ascii = 1;
173 break;
174 case CLO_PIPE:
175 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
177 break;
178 case CLO_MODEL:
179 if ((s_arg[i_arg].n_items -= 2) < 0 ||
180 !
scanItemList(&modFlags, s_arg[i_arg].list + 2, &s_arg[i_arg].n_items, 0,
181 "abscissa",
SDDS_STRING, &modAbscissaName, 1, MOD_ABSCISSA,
182 "ordinate",
SDDS_STRING, &modOrdinateName, 1, MOD_ORDINATE,
183 "interp",
SDDS_LONG, &modOrder, 1, MOD_ORDER,
184 NULL) ||
185 !(modFlags & MOD_ABSCISSA) || !(modFlags & MOD_ORDINATE))
186 bomb(
"Invalid -model syntax", modUsage);
187 if (!strlen(modFile = s_arg[i_arg].list[1]))
188 bomb(
"Invalid -model syntax", modUsage);
189 break;
190 case CLO_VALUES:
191 if ((s_arg[i_arg].n_items -= 2) < 0 ||
192 !
scanItemList(&valFlags, s_arg[i_arg].list + 2, &s_arg[i_arg].n_items, 0,
193 "abscissa",
SDDS_STRING, &valAbscissaName, 1, VAL_ABSCISSA,
194 NULL) ||
195 !(valFlags & VAL_ABSCISSA))
196 bomb(
"Invalid -fileValues syntax", valUsage);
197 if (!strlen(valFile = s_arg[i_arg].list[1]))
198 bomb(
"Invalid -fileValues syntax", valUsage);
199 break;
200 case CLO_ORDER:
201 if (!
get_long(&dataOrder, s_arg[i_arg].list[1]))
202 bomb(
"No value provided for option -order", USAGE);
203 break;
204 case CLO_COLUMNS:
205
206
207
208
209 if (s_arg[i_arg].n_items != 3 ||
210 !strlen(dataAbscissaName = s_arg[i_arg].list[1]) ||
211 !strlen(dataOrdinateName = s_arg[i_arg].list[2]))
213 break;
214 default:
215 SDDS_Bomb(
"Unrecognized option provided");
216 }
217 } else {
218 if (!dataFile)
219 dataFile = s_arg[i_arg].list[0];
220 else if (!outFile)
221 outFile = s_arg[i_arg].list[0];
222 else
223 SDDS_Bomb(
"Too many filenames provided");
224 }
225 }
226
227 processFilenames(
"sddsminterp", &dataFile, &outFile, pipeFlags, noWarnings, &tmpfile_used);
228
229 if (valFlags) {
230 fprintf(stderr, "Warning: Option -fileValues is not operational yet. Using model abscissa values.\n");
231 }
232
237
238 if (valFlags) {
243 }
244
245
247 case SDDS_CHECK_NONEXISTENT:
248 fprintf(stderr, "Error: Column '%s' does not exist in file '%s'.\n", modAbscissaName, modFile);
249 exit(EXIT_FAILURE);
250 }
252 case SDDS_CHECK_NONEXISTENT:
253 fprintf(stderr, "Error: Column '%s' does not exist in file '%s'.\n", modOrdinateName, modFile);
254 exit(EXIT_FAILURE);
255 }
256
257 if (valFlags) {
259 case SDDS_CHECK_NONEXISTENT:
260 fprintf(stderr, "Error: Column '%s' does not exist in file '%s'.\n", valAbscissaName, valFile);
261 exit(EXIT_FAILURE);
262 }
263 }
264
268 if (!modAbscissa || !modOrdinate)
270
271 if (valFlags) {
274 if (!valAbscissa)
276 outAbscissa = valAbscissa;
277 outRows = valRows;
278 } else {
279 outAbscissa = modAbscissa;
280 outRows = modRows;
281 }
282
285
287 case SDDS_CHECK_NONEXISTENT:
288 fprintf(stderr, "Error: Column '%s' does not exist in file '%s'.\n", dataAbscissaName, dataFile);
289 exit(EXIT_FAILURE);
290 }
292 case SDDS_CHECK_NONEXISTENT:
293 fprintf(stderr, "Error: Column '%s' does not exist in file '%s'.\n", dataOrdinateName, dataFile);
294 exit(EXIT_FAILURE);
295 }
296
298 "Interpolation data from model file", "Interpolated data", outFile) ||
301
302 if (columnMajorOrder != -1)
303 outDataSet.layout.data_mode.column_major = columnMajorOrder;
304 else
305 outDataSet.layout.data_mode.column_major = dataDataSet.layout.data_mode.column_major;
306
309
310 if (valFlags) {
312 } else {
314 }
316
320 if (!dataAbscissa || !dataOrdinate)
322
324 dataOrdinateInterp = (double *)malloc(sizeof(double) * dataRows);
325 dataScale = (double *)malloc(sizeof(double) * dataRows);
326
327 outScale = (double *)malloc(sizeof(double) * outRows);
328 outOrdinate = (double *)malloc(sizeof(double) * outRows);
329
330
331 for (i = 0; i < dataRows; i++) {
332 dataOrdinateInterp[i] =
interp(modOrdinate, modAbscissa, modRows, dataAbscissa[i], warning, modOrder, &returnCode);
333 dataScale[i] = dataOrdinate[i] / dataOrdinateInterp[i];
334 }
335
336 for (i = 0; i < outRows; i++) {
337 outScale[i] =
interp(dataScale, dataAbscissa, dataRows, outAbscissa[i], warning, dataOrder, &returnCode);
338 outOrdinate[i] = modOrdinate[i] * outScale[i];
339 }
340
343
346 !
SDDS_SetColumn(&outDataSet, SDDS_SET_BY_NAME | SDDS_PASS_BY_REFERENCE, modAbscissa, outRows, outAbscissaName) ||
347 !
SDDS_SetColumn(&outDataSet, SDDS_SET_BY_NAME | SDDS_PASS_BY_REFERENCE, outOrdinate, outRows, outOrdinateName) ||
350
351 free(dataAbscissa);
352 free(dataOrdinate);
353 free(dataOrdinateInterp);
354 free(dataScale);
355 free(outScale);
356 free(outOrdinate);
357 }
358
363
365 exit(EXIT_FAILURE);
366
367 return EXIT_SUCCESS;
368}
int32_t SDDS_CopyParameters(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_InitializeCopy(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, char *filename, char *filemode)
int32_t SDDS_CopyArrays(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
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_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_CheckColumn(SDDS_DATASET *SDDS_dataset, char *name, char *units, int32_t type, FILE *fp_message)
Checks if a column exists in the SDDS dataset with the specified name, units, and type.
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.
int32_t SDDS_CopyString(char **target, const char *source)
Copies a source string to a target string with memory allocation.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
#define SDDS_DOUBLE
Identifier for the double data type.
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.
double interp(double *f, double *x, long n, double xo, long warnings, long order, long *returnCode)
Performs simple linear interpolation of data.
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.
long replaceFileAndBackUp(char *file, char *replacement)
Replaces a file with a replacement file and creates a backup of the original.
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.