58char *option[N_OPTIONS] = {
66 "Usage: sdds3daverage <inputFile> [<outputRoot>] [OPTIONS]\n"
69 " -power=<integer> Set the power for averaging.\n"
70 " -xfilter=minimum=<value>,maximum=<value> Filter x values within the specified range.\n"
71 " -yfilter=minimum=<value>,maximum=<value> Filter y values within the specified range.\n"
72 " -zfilter=minimum=<value>,maximum=<value> Filter z values within the specified range.\n"
75 " sdds3daverage data.sdds outputRoot -power=2 -xfilter=minimum=0.1,maximum=1.0\n"
77 "Link date: " __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
"\n";
79void SetupOutputFile(
SDDS_DATASET *SDDS_out,
char *output,
long zdim);
80void free_data_memory(
double ***data,
long zdim,
long ydim);
82int main(
int argc,
char **argv) {
84 int32_t i, j, k, i_arg, rows, xfilter_provided, yfilter_provided, zfilter_provided, row;
87 char *inputFile, *outputRoot, output[1024], tmpcol[256];
88 double xmin, xmax, ymin, ymax, zmin, zmax, xinterval, yinterval, zinterval;
89 int32_t xdim, ydim, zdim, columnNames, outputColumns, page = 0;
90 char **columnName, **outputColumn;
91 double ***Rho, ***Jz, rhoSum, rhoSum1, yRho, zRho, jzRho;
92 double x_min, x_max, y_min, y_max, z_min, z_max, x, y, z;
93 unsigned long dummyFlags = 0;
95 x_min = x_max = y_min = y_max = z_min = z_max = 0;
96 xfilter_provided = yfilter_provided = zfilter_provided = 0;
98 inputFile = outputRoot = NULL;
100 argc =
scanargs(&s_arg, argc, argv);
102 fprintf(stderr,
"Error: Insufficient arguments.\n\n%s", USAGE);
106 columnNames = outputColumns = 0;
107 columnName = outputColumn = NULL;
110 for (i_arg = 1; i_arg < argc; i_arg++) {
111 if (s_arg[i_arg].arg_type == OPTION) {
113 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
115 if (s_arg[i_arg].n_items != 2) {
118 if (!
get_long(&power, s_arg[i_arg].list[1])) {
119 SDDS_Bomb(
"Invalid -power value provided.");
124 if (s_arg[i_arg].n_items < 2) {
127 s_arg[i_arg].n_items--;
128 if (!
scanItemList(&dummyFlags, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
134 s_arg[i_arg].n_items++;
135 if (x_max <= x_min) {
136 fprintf(stderr,
"Error: Invalid -xfilter provided, x_max <= x_min.\n");
139 xfilter_provided = 1;
143 if (s_arg[i_arg].n_items < 2) {
146 s_arg[i_arg].n_items--;
147 if (!
scanItemList(&dummyFlags, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
153 s_arg[i_arg].n_items++;
154 if (y_max <= y_min) {
155 fprintf(stderr,
"Error: Invalid -yfilter provided, y_max <= y_min.\n");
158 yfilter_provided = 1;
162 if (s_arg[i_arg].n_items < 2) {
165 s_arg[i_arg].n_items--;
166 if (!
scanItemList(&dummyFlags, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
172 s_arg[i_arg].n_items++;
173 if (z_max <= z_min) {
174 fprintf(stderr,
"Error: Invalid -zfilter provided, z_max <= z_min.\n");
177 zfilter_provided = 1;
181 fprintf(stderr,
"Error: Unknown option -%s provided.\n", s_arg[i_arg].list[0]);
186 inputFile = s_arg[i_arg].list[0];
187 }
else if (!outputRoot) {
188 outputRoot = s_arg[i_arg].list[0];
190 SDDS_Bomb(
"Error: Too many file names provided.");
196 outputRoot = inputFile;
199 snprintf(output,
sizeof(output),
"%s.ave", outputRoot);
206 xdim = ydim = zdim = 1;
207 zmin = zmax = zinterval = 0;
232 if (xfilter_provided) {
233 if (x_min > xmax || x_max < xmin) {
234 fprintf(stderr,
"Error: Invalid xfilter provided, it should be between %le and %le.\n", xmin, xmax);
244 if (yfilter_provided) {
245 if (y_min > ymax || y_max < ymin) {
246 fprintf(stderr,
"Error: Invalid yfilter provided, it should be between %le and %le.\n", ymin, ymax);
256 if (zfilter_provided && zdim > 1) {
257 if (z_min > zmax || z_max < zmin) {
258 fprintf(stderr,
"Error: Invalid zfilter provided, it should be between %le and %le.\n", zmin, zmax);
268 Rho = malloc(
sizeof(*Rho) * zdim);
270 fprintf(stderr,
"Error: Memory allocation failed for Rho.\n");
274 Jz = malloc(
sizeof(*Jz) * zdim);
276 free_data_memory(Rho, zdim, ydim);
277 fprintf(stderr,
"Error: Memory allocation failed for Jz.\n");
281 for (i = 0; i < zdim; i++) {
282 Rho[i] = malloc(
sizeof(**Rho) * ydim);
284 free_data_memory(Rho, zdim, ydim);
285 free_data_memory(Jz, zdim, ydim);
286 fprintf(stderr,
"Error: Memory allocation failed for Rho[%d].\n", i);
290 Jz[i] = malloc(
sizeof(**Jz) * ydim);
292 free_data_memory(Rho, zdim, ydim);
293 free_data_memory(Jz, zdim, ydim);
294 fprintf(stderr,
"Error: Memory allocation failed for Jz[%d].\n", i);
299 SetupOutputFile(&SDDS_out, output, zdim);
304 fprintf(stderr,
"Error: Row number does not equal xdim size.\n");
308 for (j = 1; j <= ydim; j++) {
309 snprintf(tmpcol,
sizeof(tmpcol),
"Rho_%d", j);
310 Rho[page][j - 1] = NULL;
315 snprintf(tmpcol,
sizeof(tmpcol),
"Jz_%d", j);
316 Jz[page][j - 1] = NULL;
331 free_data_memory(Rho, zdim, ydim);
332 free_data_memory(Jz, zdim, ydim);
333 fprintf(stderr,
"Error: The page number does not equal the zdim size.\n");
346 "numPhysCells1", xdim,
347 "numPhysCells2", ydim,
361 "numPhysCells3", zdim,
370 for (i = 0; i < xdim; i++) {
371 rhoSum = rhoSum1 = 0;
372 yRho = zRho = jzRho = 0;
373 x = i * xinterval + xmin;
375 if (xfilter_provided && (x < x_min || x > x_max)) {
379 for (j = 0; j < ydim; j++) {
380 y = j * yinterval + ymin;
382 if (yfilter_provided && (y < y_min || y > y_max)) {
386 for (k = 0; k < zdim; k++) {
387 z = k * zinterval + zmin;
389 if (zfilter_provided && zdim > 1 && (z < z_min || z > z_max)) {
394 yRho += fabs(Rho[k][j][i]) * y;
395 zRho += fabs(Rho[k][j][i]) * z;
396 jzRho += Rho[k][j][i] * Jz[k][j][i];
397 rhoSum += fabs(Rho[k][j][i]);
398 rhoSum1 += Rho[k][j][i];
400 yRho += pow(fabs(Rho[k][j][i]), power) * y;
401 zRho += pow(fabs(Rho[k][j][i]), power) * z;
402 jzRho += pow(Rho[k][j][i] * Jz[k][j][i], power);
403 rhoSum += pow(fabs(Rho[k][j][i]), power);
404 rhoSum1 += pow(Rho[k][j][i], power);
412 "YAve", yRho / (rhoSum + 1.0e-20),
413 "JzAve", jzRho / (rhoSum1 + 1.0e-20),
420 "ZAve", zRho / (rhoSum + 1.0e-20),
433 free_data_memory(Rho, zdim, ydim);
434 free_data_memory(Jz, zdim, ydim);
439void free_data_memory(
double ***data,
long zdim,
long ydim) {
441 for (i = 0; i < zdim; i++) {
442 for (j = 0; j < ydim; j++) {
450void SetupOutputFile(
SDDS_DATASET *SDDS_out,
char *output,
long zdim) {
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_SetRowValues(SDDS_DATASET *SDDS_dataset, int32_t mode, int64_t row,...)
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
int32_t SDDS_SetParameters(SDDS_DATASET *SDDS_dataset, int32_t mode,...)
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_DefineSimpleColumn(SDDS_DATASET *SDDS_dataset, const char *name, const char *unit, int32_t type)
Defines a simple data column within the SDDS dataset.
int32_t SDDS_DefineSimpleParameter(SDDS_DATASET *SDDS_dataset, const char *name, const char *unit, int32_t type)
Defines a simple data parameter within the SDDS 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.
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_CheckParameter(SDDS_DATASET *SDDS_dataset, char *name, char *units, int32_t type, FILE *fp_message)
Checks if a parameter exists in the SDDS dataset with the specified name, units, and type.
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
#define SDDS_ANY_NUMERIC_TYPE
Special identifier used by SDDS_Check*() routines to accept any numeric type.
#define SDDS_DOUBLE
Identifier for the double data type.
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 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.