47#include "match_string.h"
63char *commandline_option[N_OPTIONS] = {
75 "Usage: sddsminterp <input-file> <output-file> [OPTIONS]\n"
77 " -pipe=<input>,<output> Use pipes for input and output.\n"
78 " -columns=<independent-quantity>,<name> Specify the columns to interpolate.\n"
79 " -interpOrder=<order> Set the interpolation order (default: 1).\n"
80 " -model=<modelFile>,abscissa=<column>,\n"
81 " ordinate=<column>[,interp=<order>] Define the model data set.\n"
82 " -fileValues=<valuesFile>[,abscissa=<column>]\n"
83 " Specify abscissa values for interpolation.\n"
84 " -majorOrder=row|column Set output data order.\n"
85 " -verbose Enable verbose mode.\n"
86 " -ascii Output in ASCII format.\n"
88 " Multiplicative renormalized model interpolation of a data set using another\n"
89 " data set as a model function.\n"
91 "Program by Louis Emery, ANL (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
93#define MOD_ABSCISSA 0x0001U
94#define MOD_ORDINATE 0x0002U
95#define MOD_ORDER 0x0004U
96static char *modUsage =
97 "-model=<file>,abscissa=<column>,ordinate=<column>[,interp=<order>]\n";
99#define VAL_ABSCISSA 0x0001U
100static char *valUsage =
101 "-fileValues=<file>,abscissa=<column>\n";
103#define DEFAULT_ORDER 1
105int main(
int argc,
char **argv) {
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;
116 double *modAbscissa, *modOrdinate;
118 double *dataAbscissa, *dataOrdinate, *dataScale, *dataOrdinateInterp;
119 double *outScale, *outAbscissa, *outOrdinate;
120 int64_t i, modRows, valRows, dataRows, outRows;
127 unsigned long pipeFlags;
128 long tmpfile_used, noWarnings;
129 short columnMajorOrder = -1;
132 argc =
scanargs(&s_arg, argc, argv);
138 dataFile = dataAbscissaName = dataOrdinateName = modFile = valFile = NULL;
139 modOrdinate = dataOrdinate = NULL;
142 modOrder = DEFAULT_ORDER;
143 dataOrder = DEFAULT_ORDER;
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:
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,
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;
175 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
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,
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);
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,
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);
201 if (!
get_long(&dataOrder, s_arg[i_arg].list[1]))
202 bomb(
"No value provided for option -order", USAGE);
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]))
215 SDDS_Bomb(
"Unrecognized option provided");
219 dataFile = s_arg[i_arg].list[0];
221 outFile = s_arg[i_arg].list[0];
223 SDDS_Bomb(
"Too many filenames provided");
227 processFilenames(
"sddsminterp", &dataFile, &outFile, pipeFlags, noWarnings, &tmpfile_used);
230 fprintf(stderr,
"Warning: Option -fileValues is not operational yet. Using model abscissa values.\n");
247 case SDDS_CHECK_NONEXISTENT:
248 fprintf(stderr,
"Error: Column '%s' does not exist in file '%s'.\n", modAbscissaName, modFile);
252 case SDDS_CHECK_NONEXISTENT:
253 fprintf(stderr,
"Error: Column '%s' does not exist in file '%s'.\n", modOrdinateName, modFile);
259 case SDDS_CHECK_NONEXISTENT:
260 fprintf(stderr,
"Error: Column '%s' does not exist in file '%s'.\n", valAbscissaName, valFile);
268 if (!modAbscissa || !modOrdinate)
276 outAbscissa = valAbscissa;
279 outAbscissa = modAbscissa;
287 case SDDS_CHECK_NONEXISTENT:
288 fprintf(stderr,
"Error: Column '%s' does not exist in file '%s'.\n", dataAbscissaName, dataFile);
292 case SDDS_CHECK_NONEXISTENT:
293 fprintf(stderr,
"Error: Column '%s' does not exist in file '%s'.\n", dataOrdinateName, dataFile);
298 "Interpolation data from model file",
"Interpolated data", outFile) ||
302 if (columnMajorOrder != -1)
303 outDataSet.layout.data_mode.column_major = columnMajorOrder;
305 outDataSet.layout.data_mode.column_major = dataDataSet.layout.data_mode.column_major;
320 if (!dataAbscissa || !dataOrdinate)
324 dataOrdinateInterp = (
double *)malloc(
sizeof(
double) * dataRows);
325 dataScale = (
double *)malloc(
sizeof(
double) * dataRows);
327 outScale = (
double *)malloc(
sizeof(
double) * outRows);
328 outOrdinate = (
double *)malloc(
sizeof(
double) * outRows);
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];
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];
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) ||
353 free(dataOrdinateInterp);
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
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.