73static char *option[N_OPTIONS] = {
84 "sddscliptails [<input>] [<output>] [-pipe=[input][,output]]\n"
85 " [-columns=<listOfNames>] [-fractional=<value>] [-absolute=<value>] [-fwhm=<multiplier>]\n"
86 " [-afterzero[=<bufferWidth>]] [-majorOrder=row|column]\n\n"
87 "-columns List of columns to process.\n"
88 "-fractional Clip a tail if it falls below this fraction of the peak.\n"
89 "-absolute Clip a tail if it falls below this absolute value.\n"
90 "-fwhm Clip a tail if it is beyond this many FWHM from the peak.\n"
91 "-afterzero Clip a tail if it is separated from the peak by values equal to zero.\n"
92 " If <bufferWidth> is specified, then a region <bufferWidth> wide is kept\n"
93 " on either side of the peak, if possible.\n"
94 "-majorOrder Writes output file in row or column major order.\n\n"
95 "Program by Michael Borland. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
97static long resolve_column_names(
SDDS_DATASET *sdds_in,
char ***column, int32_t *columns);
98static void clip_tail(
double *data, int64_t rows,
double abs_limit,
double frac_limit,
short *in_tail);
99static void clip_fwhm(
double *data, int64_t n_data,
double fwhm_limit,
double *indep_data,
short *in_tail);
100static void clip_after_zero(
double *data, int64_t rows,
long buffer_width,
short *in_tail);
102int main(
int argc,
char **argv) {
105 char *input, *output;
106 long read_code, after_zero, after_zero_buffer_width;
109 unsigned long pipe_flags, major_order_flag;
110 SCANNED_ARG *scanned;
112 double *data, *indep_data;
113 double fractional_limit, absolute_limit, fwhm_limit;
115 short column_major_order = -1;
118 argc =
scanargs(&scanned, argc, argv);
122 output = input = NULL;
126 fractional_limit = fwhm_limit = 0;
130 for (i_arg = 1; i_arg < argc; i_arg++) {
131 if (scanned[i_arg].arg_type == OPTION) {
133 switch (
match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) {
134 case CLO_MAJOR_ORDER:
135 major_order_flag = 0;
136 scanned[i_arg].n_items--;
137 if (scanned[i_arg].n_items > 0 &&
138 (!
scanItemList(&major_order_flag, scanned[i_arg].list + 1, &scanned[i_arg].n_items, 0,
139 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
140 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)))
141 SDDS_Bomb(
"invalid -majorOrder syntax/values");
142 if (major_order_flag & SDDS_COLUMN_MAJOR_ORDER)
143 column_major_order = 1;
144 else if (major_order_flag & SDDS_ROW_MAJOR_ORDER)
145 column_major_order = 0;
148 if (scanned[i_arg].n_items < 2)
150 input_column =
tmalloc(
sizeof(*input_column) * (columns = scanned[i_arg].n_items - 1));
151 for (i = 0; i < columns; i++)
152 input_column[i] = scanned[i_arg].list[i + 1];
155 if (!
processPipeOption(scanned[i_arg].list + 1, scanned[i_arg].n_items - 1, &pipe_flags))
159 if (scanned[i_arg].n_items < 2 || sscanf(scanned[i_arg].list[1],
"%lf", &fractional_limit) != 1 || fractional_limit < 0)
163 if (scanned[i_arg].n_items < 2 || sscanf(scanned[i_arg].list[1],
"%lf", &absolute_limit) != 1 || absolute_limit < 0)
167 if (scanned[i_arg].n_items < 2 || sscanf(scanned[i_arg].list[1],
"%lf", &fwhm_limit) != 1 || fwhm_limit < 0)
172 if (scanned[i_arg].n_items > 2 ||
173 (scanned[i_arg].n_items == 2 &&
174 (sscanf(scanned[i_arg].list[1],
"%ld", &after_zero_buffer_width) != 1 ||
175 after_zero_buffer_width <= 0)))
179 fprintf(stderr,
"error: unknown/ambiguous option: %s\n", scanned[i_arg].list[0]);
185 input = scanned[i_arg].list[0];
187 output = scanned[i_arg].list[0];
196 SDDS_Bomb(
"supply the names of columns to process with the -columns option");
201 if (!resolve_column_names(&sdds_in, &input_column, &columns))
204 SDDS_Bomb(
"no columns selected for processing");
208 if (column_major_order != -1)
209 sdds_out.layout.data_mode.column_major = column_major_order;
211 sdds_out.layout.data_mode.column_major = sdds_in.layout.data_mode.column_major;
225 if (fwhm_limit > 0) {
226 indep_data =
SDDS_Realloc(indep_data,
sizeof(*indep_data) * rows);
229 for (i = 0; i < rows; i++)
232 in_tail =
SDDS_Realloc(in_tail,
sizeof(*in_tail) * rows);
235 for (i = 0; i < rows; i++)
237 for (i = 0; i < columns; i++) {
241 clip_tail(data, rows, absolute_limit, fractional_limit, in_tail);
243 clip_fwhm(data, rows, fwhm_limit, indep_data, in_tail);
245 clip_after_zero(data, rows, after_zero_buffer_width, in_tail);
251 if (!
SDDS_SetColumn(&sdds_out, SDDS_SET_BY_NAME, in_tail, rows,
"InTail"))
271static long resolve_column_names(
SDDS_DATASET *sdds_in,
char ***column, int32_t *columns) {
275 for (i = 0; i < *columns; i++) {
281 if (!*column || *columns == 0) {
288static void clip_tail(
double *data, int64_t rows,
double abs_limit,
double frac_limit,
short *in_tail) {
290 int64_t i, imin, imax;
293 if (abs_limit < 0 && frac_limit <= 0)
302 abs_limit2 = frac_limit * data[imax];
303 if (abs_limit < 0 || (frac_limit && abs_limit2 < abs_limit))
304 abs_limit = abs_limit2;
309 for (i = imax - 1, clip = 0; i >= 0; i--) {
310 if (!clip && data[i] < abs_limit)
318 for (i = imax + 1, clip = 0; i < rows; i++) {
319 if (!clip && data[i] < abs_limit)
328static void clip_fwhm(
double *data, int64_t n_data,
double fwhm_limit,
double *indep_data,
short *in_tail) {
329 double top, base, fwhm;
330 int64_t i1, i2, i2_save, i, imin, imax;
331 double point1, point2;
333 if (n_data < 3 || fwhm_limit <= 0)
355 fwhm = point2 - point1;
357 for (i = imax + fwhm * fwhm_limit; i < n_data; i++) {
361 for (i = imax - fwhm * fwhm_limit; i >= 0; i--) {
367static void clip_after_zero(
double *data, int64_t rows,
long buffer_width,
short *in_tail) {
368 int64_t imin, imax, i, j;
372 for (i = imax + 1; i < rows; i++) {
374 for (j = i + buffer_width; j < rows; j++) {
382 for (i = imax - 1; i >= 0; i--) {
384 for (j = i - buffer_width; j >= 0; j--) {
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_SetColumnFromDoubles(SDDS_DATASET *SDDS_dataset, int32_t mode, double *data, int64_t rows,...)
Sets the values for a single data column using double-precision floating-point numbers.
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.
void SDDS_SetError(char *error_text)
Records an error message in the SDDS error stack.
char ** SDDS_GetColumnNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all columns in the SDDS dataset.
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_SHORT
Identifier for the signed short integer data type.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
int index_min_max(int64_t *imin, int64_t *imax, double *list, int64_t n)
Finds the indices of the minimum and maximum values in a list of doubles.
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 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.
long findTopBaseLevels(double *top, double *base, double *data, int64_t points, long bins, double sigmasRequired)
Finds the top-level and base-level of a dataset.
int64_t findCrossingPoint(int64_t start, double *data, int64_t points, double level, long direction, double *indepData, double *location)
Finds the crossing point in the data where the data crosses a specified level.