82 {
84 int32_t i, j, k, i_arg, rows, xfilter_provided, yfilter_provided, zfilter_provided, row;
85 long power = 1;
86 SCANNED_ARG *s_arg;
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;
94
95 x_min = x_max = y_min = y_max = z_min = z_max = 0;
96 xfilter_provided = yfilter_provided = zfilter_provided = 0;
97
98 inputFile = outputRoot = NULL;
100 argc =
scanargs(&s_arg, argc, argv);
101 if (argc < 2) {
102 fprintf(stderr, "Error: Insufficient arguments.\n\n%s", USAGE);
103 exit(EXIT_FAILURE);
104 }
105
106 columnNames = outputColumns = 0;
107 columnName = outputColumn = NULL;
108 Rho = Jz = NULL;
109
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)) {
114 case SET_POWER:
115 if (s_arg[i_arg].n_items != 2) {
117 }
118 if (!
get_long(&power, s_arg[i_arg].list[1])) {
119 SDDS_Bomb(
"Invalid -power value provided.");
120 }
121 break;
122
123 case SET_XFILTER:
124 if (s_arg[i_arg].n_items < 2) {
126 }
127 s_arg[i_arg].n_items--;
128 if (!
scanItemList(&dummyFlags, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
131 NULL)) {
133 }
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");
137 exit(EXIT_FAILURE);
138 }
139 xfilter_provided = 1;
140 break;
141
142 case SET_YFILTER:
143 if (s_arg[i_arg].n_items < 2) {
145 }
146 s_arg[i_arg].n_items--;
147 if (!
scanItemList(&dummyFlags, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
150 NULL)) {
152 }
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");
156 exit(EXIT_FAILURE);
157 }
158 yfilter_provided = 1;
159 break;
160
161 case SET_ZFILTER:
162 if (s_arg[i_arg].n_items < 2) {
164 }
165 s_arg[i_arg].n_items--;
166 if (!
scanItemList(&dummyFlags, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
169 NULL)) {
171 }
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");
175 exit(EXIT_FAILURE);
176 }
177 zfilter_provided = 1;
178 break;
179
180 default:
181 fprintf(stderr, "Error: Unknown option -%s provided.\n", s_arg[i_arg].list[0]);
182 exit(EXIT_FAILURE);
183 }
184 } else {
185 if (!inputFile) {
186 inputFile = s_arg[i_arg].list[0];
187 } else if (!outputRoot) {
188 outputRoot = s_arg[i_arg].list[0];
189 } else {
190 SDDS_Bomb(
"Error: Too many file names provided.");
191 }
192 }
193 }
194
195 if (!outputRoot) {
196 outputRoot = inputFile;
197 }
198
199 snprintf(output, sizeof(output), "%s.ave", outputRoot);
200
203 exit(EXIT_FAILURE);
204 }
205
206 xdim = ydim = zdim = 1;
207 zmin = zmax = zinterval = 0;
209 if (page == 0) {
219 exit(EXIT_FAILURE);
220 }
221
228 exit(EXIT_FAILURE);
229 }
230 }
231
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);
237 exit(EXIT_FAILURE);
238 }
239 } else {
240 x_min = xmin;
241 x_max = xmax;
242 }
243
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);
249 exit(EXIT_FAILURE);
250 }
251 } else {
252 y_min = ymin;
253 y_max = ymax;
254 }
255
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);
261 exit(EXIT_FAILURE);
262 }
263 } else {
264 z_min = zmin;
265 z_max = zmax;
266 }
267
268 Rho = malloc(sizeof(*Rho) * zdim);
269 if (!Rho) {
270 fprintf(stderr, "Error: Memory allocation failed for Rho.\n");
271 exit(EXIT_FAILURE);
272 }
273
274 Jz = malloc(sizeof(*Jz) * zdim);
275 if (!Jz) {
276 free_data_memory(Rho, zdim, ydim);
277 fprintf(stderr, "Error: Memory allocation failed for Jz.\n");
278 exit(EXIT_FAILURE);
279 }
280
281 for (i = 0; i < zdim; i++) {
282 Rho[i] = malloc(sizeof(**Rho) * ydim);
283 if (!Rho[i]) {
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);
287 exit(EXIT_FAILURE);
288 }
289
290 Jz[i] = malloc(sizeof(**Jz) * ydim);
291 if (!Jz[i]) {
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);
295 exit(EXIT_FAILURE);
296 }
297 }
298
299 SetupOutputFile(&SDDS_out, output, zdim);
300 }
301
303 if (rows != xdim) {
304 fprintf(stderr, "Error: Row number does not equal xdim size.\n");
305 exit(EXIT_FAILURE);
306 }
307
308 for (j = 1; j <= ydim; j++) {
309 snprintf(tmpcol, sizeof(tmpcol), "Rho_%d", j);
310 Rho[page][j - 1] = NULL;
313 }
314
315 snprintf(tmpcol, sizeof(tmpcol), "Jz_%d", j);
316 Jz[page][j - 1] = NULL;
319 }
320 }
321
322 page++;
323 }
324
327 exit(EXIT_FAILURE);
328 }
329
330 if (page != zdim) {
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");
334 exit(EXIT_FAILURE);
335 }
336
339 "origin1", xmin,
340 "origin2", ymin,
341 "origin3", zmin,
342 "max_ext1", xmax,
343 "max_ext2", ymax,
344 "delta1", xinterval,
345 "delta2", yinterval,
346 "numPhysCells1", xdim,
347 "numPhysCells2", ydim,
348 "xstart", x_min,
349 "xend", x_max,
350 "ystart", y_min,
351 "yend", y_max,
352 NULL)) {
354 }
355
356 if (zdim > 1) {
358 "origin3", zmin,
359 "max_ext3", zmax,
360 "delta3", zinterval,
361 "numPhysCells3", zdim,
362 "zstart", z_min,
363 "zend", z_max,
364 NULL)) {
366 }
367 }
368
369 row = 0;
370 for (i = 0; i < xdim; i++) {
371 rhoSum = rhoSum1 = 0;
372 yRho = zRho = jzRho = 0;
373 x = i * xinterval + xmin;
374
375 if (xfilter_provided && (x < x_min || x > x_max)) {
376 continue;
377 }
378
379 for (j = 0; j < ydim; j++) {
380 y = j * yinterval + ymin;
381
382 if (yfilter_provided && (y < y_min || y > y_max)) {
383 continue;
384 }
385
386 for (k = 0; k < zdim; k++) {
387 z = k * zinterval + zmin;
388
389 if (zfilter_provided && zdim > 1 && (z < z_min || z > z_max)) {
390 continue;
391 }
392
393 if (power == 1) {
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];
399 } else {
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);
405 }
406 }
407 }
408
409
411 "x", x,
412 "YAve", yRho / (rhoSum + 1.0e-20),
413 "JzAve", jzRho / (rhoSum1 + 1.0e-20),
414 NULL)) {
416 }
417
418 if (zdim > 1) {
420 "ZAve", zRho / (rhoSum + 1.0e-20),
421 NULL)) {
423 }
424 }
425
426 row++;
427 }
428
431 }
432
433 free_data_memory(Rho, zdim, ydim);
434 free_data_memory(Jz, zdim, ydim);
435
436 return EXIT_SUCCESS;
437}
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_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table 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_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.