52#if defined(linux) || (defined(_WIN32) && !defined(_MINGW))
55void omp_set_num_threads(
int a) {}
68char *option[N_OPTIONS] = {
77static char *USAGE =
"\n" \
78 " sddsinsideboundaries [<inputfile>] [<outputfile>]\n" \
79 " [-pipe=[input][,output]]\n" \
80 " -columns=<x-name>,<y-name>\n" \
81 " -boundary=<filename>,<x-name>,<y-name>\n" \
82 " [-insideColumn=<column_name>]\n" \
83 " [-keep={inside|outside}]\n" \
84 " [-threads=<number>]\n" \
86 " -columns=<x-name>,<y-name>\n" \
87 " Specify the names of the (x, y) columns in the input file.\n" \
88 " -boundary=<filename>,<x-name>,<y-name>\n" \
89 " Provide a file with boundary data, including x and y columns.\n" \
90 " The file can have multiple pages.\n" \
91 " -insideColumn=<column_name>\n" \
92 " Specify the name of the output column for the count of boundaries\n" \
93 " containing each point (default: InsideSum).\n" \
94 " -keep={inside|outside}\n" \
96 " inside - Keep only points inside any boundary.\n" \
97 " outside - Keep only points outside all boundaries.\n" \
98 " By default, all points are kept.\n" \
99 " -threads=<number>\n" \
100 " Set the number of threads for computation (default: 1).\n\n" \
101 "Program by Michael Borland. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
110char *keepOption[N_KEEP_OPTIONS] = {
116long compute_inside_sum(
double x,
double y,
double **xBoundary,
double **yBoundary, int64_t *nValues,
long nBoundaries);
117long read_boundary_data(
char *boundaryInput,
char *bxColumn,
char *byColumn,
double ***xBoundary,
double ***yBoundary, int64_t **nValues);
119double *xData, *yData;
120double **xBoundary = NULL, **yBoundary = NULL;
121int64_t *nValues = NULL, rows;
124int32_t *insideSumData;
126int main(
int argc,
char **argv) {
128 SCANNED_ARG *scanned = NULL;
129 char *input = NULL, *output = NULL, *boundaryInput = NULL;
130 char *xColumn = NULL, *yColumn = NULL, *bxColumn = NULL, *byColumn = NULL;
131 char *insideColumn =
"InsideSum";
134 long keepCode = KEEP_ALL;
135 long readCode, keepSeen = 0;
137 unsigned long pipeFlags = 0;
140 argc =
scanargs(&scanned, argc, argv);
144 for (iArg = 1; iArg < argc; iArg++) {
145 if (scanned[iArg].arg_type == OPTION) {
147 switch (
match_string(scanned[iArg].list[0], option, N_OPTIONS, 0)) {
149 if (xColumn || yColumn)
150 SDDS_Bomb(
"only one -columns option may be given");
151 if (scanned[iArg].n_items != 3)
153 xColumn = scanned[iArg].list[1];
154 yColumn = scanned[iArg].list[2];
158 SDDS_Bomb(
"only one -boundary option may be given");
159 if (scanned[iArg].n_items != 4)
161 boundaryInput = scanned[iArg].list[1];
162 bxColumn = scanned[iArg].list[2];
163 byColumn = scanned[iArg].list[3];
165 case SET_INSIDE_COLUMN:
166 if (scanned[iArg].n_items != 2)
167 SDDS_Bomb(
"invalid -insideColumn syntax");
168 insideColumn = scanned[iArg].list[1];
172 SDDS_Bomb(
"only one -keep option may be given");
173 if (scanned[iArg].n_items != 2)
175 if ((keepCode =
match_string(scanned[iArg].list[1], keepOption, N_KEEP_OPTIONS, 0)) < 0)
176 SDDS_Bomb(
"invalid -keep value. Supply 'all', 'inside', or 'outside' or a unique abbreviation");
179 if (!
processPipeOption(scanned[iArg].list + 1, scanned[iArg].n_items - 1, &pipeFlags))
183 if (scanned[iArg].n_items != 2 ||
184 sscanf(scanned[iArg].list[1],
"%d", &threads) != 1 || threads < 1)
188 fprintf(stderr,
"error: unknown/ambiguous option: %s\n", scanned[iArg].list[0]);
194 input = scanned[iArg].list[0];
196 output = scanned[iArg].list[0];
202 processFilenames(
"sddsinsideboundaries", &input, &output, pipeFlags, 0, NULL);
203 if (!pipeFlags && strcmp(input, output) == 0)
204 SDDS_Bomb(
"can't use same file for input and output");
206 if (!boundaryInput || !bxColumn || !byColumn)
207 SDDS_Bomb(
"-boundaries option must be given");
208 if ((nBoundaries = read_boundary_data(boundaryInput, bxColumn, byColumn, &xBoundary, &yBoundary, &nValues)) <= 0)
209 SDDS_Bomb(
"No valid data in boundary data file");
216 SDDS_Bomb(
"-xColumn is not present or not numeric");
218 SDDS_Bomb(
"-yColumn is not present or not numeric");
226 insideSumData = NULL;
228 xData = yData = NULL;
229 omp_set_num_threads(threads);
240 insideSumData = calloc(rows,
sizeof(*insideSumData));
241 keep = calloc(rows,
sizeof(*keep));
243#pragma omp parallel for
244 for (i = 0; i < rows; i++) {
245 insideSumData[i] = compute_inside_sum(xData[i], yData[i], xBoundary, yBoundary, nValues, nBoundaries);
248 keep[i] = insideSumData[i];
251 keep[i] = insideSumData[i] == 0;
258 if (!
SDDS_SetColumn(&SDDSout, SDDS_SET_BY_NAME, insideSumData, rows, insideColumn) ||
272long read_boundary_data(
char *boundaryInput,
char *bxColumn,
char *byColumn,
double ***xBoundary,
double ***yBoundary, int64_t **nValues) {
282 if (SDDS_RowCount(&SDDSin) == 0)
284 *xBoundary =
SDDS_Realloc(*xBoundary,
sizeof(**xBoundary) * (nSets + 1));
285 *yBoundary =
SDDS_Realloc(*yBoundary,
sizeof(**yBoundary) * (nSets + 1));
286 *nValues =
SDDS_Realloc(*nValues,
sizeof(**nValues) * (nSets + 1));
287 (*nValues)[nSets] = SDDS_RowCount(&SDDSin);
296long compute_inside_sum(
double x,
double y,
double **xBoundary,
double **yBoundary, int64_t *nValues,
long nBoundaries) {
299 for (ib = 0; ib < nBoundaries; ib++)
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
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_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_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.
void * SDDS_Realloc(void *old_ptr, size_t new_size)
Reallocates memory to a new size.
#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.
Utility functions for SDDS dataset manipulation and string array operations.
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
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 pointIsInsideContour(double x0, double y0, double *x, double *y, int64_t n, double *center, double theta)
Determine if a given point (x0, y0) is inside a specified polygonal contour.
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)