62#if !defined(MAXDOUBLE)
63# define MAXDOUBLE 1.79769313486231570e+308
66char *option[N_OPTIONS] = {
80 "Usage: sddsconvertalarmlog [<input-file>] [<output-file>]\n"
82 " -pipe=[input][,output] Use pipe for input and/or output.\n"
83 " -binary Output in binary format.\n"
84 " -ascii Output in ASCII format.\n"
85 " -double Use double precision for output.\n"
86 " -float Use float precision for output.\n"
87 " -minimumInterval=<seconds> Set minimum interval between data points.\n"
88 " -snapshot=<epochtime> Take a snapshot at the specified epoch time.\n"
89 " -time=[start=<epochtime>][,end=<epochtime>]\n"
90 " Filter data by time range.\n"
91 " -delete={<matching-string>[,...]} Delete columns matching the pattern.\n"
92 " -retain={<matching-string>[,...]} Retain only columns matching the pattern.\n"
93 "\nProgram by Robert Soliday. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
95long SDDS_SetRowValuesMod(
SDDS_DATASET *SDDS_dataset,
long mode, int64_t row, ...);
96char **process_name_options(
char **orig_name,
long orig_names,
char **
delete,
long deletes,
char **retain,
long retains,
long *num_matched,
long **origToNewIndex);
98int main(
int argc,
char **argv) {
102 long tmpfile_used, noWarnings;
103 char *input, *output;
104 long ascii_output, binary_output;
105 unsigned long pipeFlags;
110 long readbackNameIndex = -1, controlNameIndex = -1;
111 long timeIndex = -1, valueIndex = -1, controlNameIndexIndex = -1, previousRowIndex = -1;
112 double *timeData = NULL, *valueData = NULL, *previousRowData = NULL;
113 int32_t *controlNameIndexData = NULL;
117 int64_t row, rows, snapshotrow = 0, initRow = 0, outrow;
118 double *rowdata = NULL;
119 long *origToNewIndex = NULL;
120 double minimumInterval = -1;
121 double timeInterval, previousTime = 0;
122 int64_t totalRows = 0, currentRows = 0;
123 short filterTime = 0;
124 double startTime = 0, endTime = MAXDOUBLE;
125 int64_t startTimeRow = 0, endTimeRow = 0;
127 char **ColumnName = NULL;
129 char **retain_name = NULL;
130 long retain_names = 0;
131 char **delete_name = NULL;
132 long delete_names = 0;
135 argc =
scanargs(&s_arg, argc, argv);
137 fprintf(stderr,
"%s", USAGE);
140 input = output = NULL;
141 ascii_output = binary_output = noWarnings = 0;
146 for (i_arg = 1; i_arg < argc; i_arg++) {
147 if (s_arg[i_arg].arg_type == OPTION) {
149 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
165 if (s_arg[i_arg].n_items < 2) {
166 fprintf(stderr,
"invalid -snapshot syntax\n");
170 if (sscanf(s_arg[i_arg].list[1],
"%lf", &epochtime) != 1) {
171 fprintf(stderr,
"invalid -snapshot syntax or value\n");
176 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags)) {
177 fprintf(stderr,
"invalid -pipe syntax\n");
181 case SET_MININTERVAL:
182 if (s_arg[i_arg].n_items < 2) {
183 fprintf(stderr,
"invalid -minimumInterval syntax\n");
186 if (sscanf(s_arg[i_arg].list[1],
"%lf", &minimumInterval) != 1) {
187 fprintf(stderr,
"invalid -minimumInterval syntax or value\n");
192 s_arg[i_arg].n_items -= 1;
194 if (!
scanItemList(&flags, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
197 fprintf(stderr,
"invalid -time syntax\n");
200 s_arg[i_arg].n_items += 1;
203 retain_name =
trealloc(retain_name,
sizeof(*retain_name) * (retain_names + s_arg[i_arg].n_items - 1));
204 for (i = 1; i < s_arg[i_arg].n_items; i++)
205 retain_name[i - 1 + retain_names] = s_arg[i_arg].list[i];
206 retain_names += s_arg[i_arg].n_items - 1;
209 delete_name =
trealloc(delete_name,
sizeof(*delete_name) * (delete_names + s_arg[i_arg].n_items - 1));
210 for (i = 1; i < s_arg[i_arg].n_items; i++)
211 delete_name[i - 1 + delete_names] = s_arg[i_arg].list[i];
212 delete_names += s_arg[i_arg].n_items - 1;
215 fprintf(stderr,
"Error (%s): unknown switch: %s\n", argv[0], s_arg[i_arg].list[0]);
221 input = s_arg[i_arg].list[0];
222 else if (output == NULL)
223 output = s_arg[i_arg].list[0];
225 fprintf(stderr,
"too many filenames\n");
231 if ((snapshot == 1) && (minimumInterval >= 0)) {
232 fprintf(stderr,
"-snapshot and -minimumInterval options cannot be used together\n");
235 if ((snapshot == 1) && (filterTime == 1)) {
236 fprintf(stderr,
"-snapshot and -time options cannot be used together\n");
240 processFilenames(
"sddsconvertalarmlog", &input, &output, pipeFlags, noWarnings, &tmpfile_used);
253 if ((readbackNameIndex == -1) && (controlNameIndex == -1)) {
254 fprintf(stderr,
"Error: ReadbackName and ControlName arrays are both missing from the input file.\n");
257 if (timeIndex == -1) {
258 fprintf(stderr,
"Error: Time column is missing\n");
261 if (valueIndex == -1) {
262 fprintf(stderr,
"Error: Value column is missing\n");
265 if (controlNameIndexIndex == -1) {
266 fprintf(stderr,
"Error: ControlNameIndex column is missing\n");
270 if (!
SDDS_InitializeOutput(&SDDS_output, ascii_output ? SDDS_ASCII : (binary_output ? SDDS_BINARY : SDDS_input.layout.data_mode.mode), 1, NULL, NULL, output)) {
276 while (SDDS_ReadTable(&SDDS_input) > 0) {
278 rows = SDDS_RowCount(&SDDS_input);
280 if (readbackNameIndex != -1) {
281 readbackNameArray =
SDDS_GetArray(&SDDS_input,
"ReadbackName", NULL);
283 readbackNameArray =
SDDS_GetArray(&SDDS_input,
"ControlName", NULL);
285 if (readbackNameArray == NULL) {
290 origToNewIndex = malloc(
sizeof(
long) * readbackNameArray->elements);
291 for (i = 0; i < readbackNameArray->elements; i++) {
292 origToNewIndex[i] = -1;
295 ColumnName = process_name_options(((
char **)readbackNameArray->data), readbackNameArray->elements, delete_name, delete_names, retain_name, retain_names, &ColumnNames, &origToNewIndex);
296 rowdata = malloc(
sizeof(
double) * ColumnNames);
297 for (i = 0; i < ColumnNames; i++) {
314 if (SDDS_StartTable(&SDDS_output, 1) == 0) {
319 if (SDDS_StartTable(&SDDS_output, 100) == 0) {
326 if (readbackNameIndex != -1) {
327 readbackNameArray2 =
SDDS_GetArray(&SDDS_input,
"ReadbackName", NULL);
329 readbackNameArray2 =
SDDS_GetArray(&SDDS_input,
"ControlName", NULL);
331 if (readbackNameArray2 == NULL) {
335 if (readbackNameArray->elements != readbackNameArray2->elements) {
336 fprintf(stderr,
"Error: Unable to process multiple pages with different ReadbackName and/or ControlName columns\n");
339 for (i = 0; i < readbackNameArray->elements; i++) {
340 if (strcmp((
char *)((
char **)readbackNameArray->data)[i], (
char *)((
char **)readbackNameArray2->data)[i]) != 0) {
341 fprintf(stderr,
"Error: Unable to process multiple pages with different ReadbackName and/or ControlName columns\n");
361 for (row = 0; row < rows; row++) {
362 if (timeData[row] <= epochtime) {
370 for (row = 0; row < rows; row++) {
371 if (timeData[row] <= startTime) {
377 for (row = 0; row < rows; row++) {
378 if (timeData[row] <= endTime) {
385 if (previousRowIndex != -1) {
390 for (row = rows - 1; row >= 0; row--) {
391 if (previousRowData[row] == -2) {
392 if (origToNewIndex[controlNameIndexData[row]] == -1) {
399 free(previousRowData);
402 if (minimumInterval > 0) {
403 previousTime = timeData[0] - minimumInterval - 1;
405 for (row = 0; row < rows; row++) {
406 if ((readbackNameArray->elements < controlNameIndexData[row]) ||
407 (controlNameIndexData[row] < 0)) {
411 if (origToNewIndex[controlNameIndexData[row]] == -1) {
414 rowdata[origToNewIndex[controlNameIndexData[row]]] = valueData[row];
415 if (previousRowIndex != -1) {
420 if (((snapshot == 0) && (filterTime == 0)) ||
421 ((snapshot == 1) && (row == snapshotrow)) ||
422 ((filterTime == 1) && (row >= startTimeRow) && (row <= endTimeRow))) {
423 if (minimumInterval > 0) {
424 timeInterval = timeData[row] - previousTime;
425 if (timeInterval <= minimumInterval) {
428 previousTime = timeData[row];
431 if ((snapshot == 0) && (totalRows == currentRows)) {
439 if (SDDS_SetRowValuesMod(&SDDS_output, SDDS_SET_BY_INDEX | SDDS_PASS_BY_REFERENCE, outrow, 0, &timeData[row], -1) == 0) {
443 for (i = 0; i < ColumnNames; i++) {
444 if (SDDS_SetRowValuesMod(&SDDS_output, SDDS_SET_BY_INDEX | SDDS_PASS_BY_REFERENCE, outrow, i + 1, &rowdata[i], -1) == 0) {
458 if (controlNameIndexData)
459 free(controlNameIndexData);
464 free(origToNewIndex);
467 for (i = 0; i < ColumnNames; i++) {
475 if (SDDS_WriteTable(&SDDS_output) == 0) {
492long SDDS_SetRowValuesMod(
SDDS_DATASET *SDDS_dataset,
long mode, int64_t row, ...) {
502 if (!(mode & SDDS_SET_BY_INDEX || mode & SDDS_SET_BY_NAME) ||
503 !(mode & SDDS_PASS_BY_VALUE || mode & SDDS_PASS_BY_REFERENCE)) {
504 SDDS_SetError(
"Unable to set column values--unknown mode (SDDS_SetRowValues)");
511 row -= SDDS_dataset->first_row_in_mem;
512 if (row >= SDDS_dataset->n_rows_allocated) {
513 sprintf(buffer,
"Unable to set column values--row number (%" PRId64
") exceeds exceeds allocated memory (%" PRId64
") (SDDS_SetRowValues)", row, SDDS_dataset->n_rows_allocated);
517 if (row > SDDS_dataset->n_rows - 1)
518 SDDS_dataset->n_rows = row + 1;
520 va_start(argptr, row);
521 layout = &SDDS_dataset->layout;
526 fprintf(stderr,
"setting row %" PRId64
" (mem slot %" PRId64
")\n", row + SDDS_dataset->first_row_in_mem, row);
529 if (mode & SDDS_SET_BY_INDEX) {
530 if ((index = va_arg(argptr, int32_t)) == -1) {
534 if (index < 0 || index >= layout->n_columns) {
535 SDDS_SetError(
"Unable to set column values--index out of range (SDDS_SetRowValues)");
540 fprintf(stderr,
"Setting values for column #%ld\n", index);
543 if ((name = va_arg(argptr,
char *)) == NULL) {
548 fprintf(stderr,
"Setting values for column %s\n", name);
551 SDDS_SetError(
"Unable to set column values--name not recognized (SDDS_SetRowValues)");
556 switch (layout->column_definition[index].type) {
558 if (mode & SDDS_PASS_BY_VALUE)
559 *(((
short *)SDDS_dataset->data[index]) + row) = (short)va_arg(argptr,
int);
561 *(((
short *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr,
short *));
564 if (mode & SDDS_PASS_BY_VALUE)
565 *(((
unsigned short *)SDDS_dataset->data[index]) + row) = (
unsigned short)va_arg(argptr,
unsigned int);
567 *(((
unsigned short *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr,
unsigned short *));
570 if (mode & SDDS_PASS_BY_VALUE)
571 *(((int32_t *)SDDS_dataset->data[index]) + row) = va_arg(argptr, int32_t);
573 *(((int32_t *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr, int32_t *));
576 if (mode & SDDS_PASS_BY_VALUE)
577 *(((uint32_t *)SDDS_dataset->data[index]) + row) = va_arg(argptr, uint32_t);
579 *(((uint32_t *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr, uint32_t *));
582 if (mode & SDDS_PASS_BY_VALUE)
583 *(((int64_t *)SDDS_dataset->data[index]) + row) = va_arg(argptr, int64_t);
585 *(((int64_t *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr, int64_t *));
588 if (mode & SDDS_PASS_BY_VALUE)
589 *(((uint64_t *)SDDS_dataset->data[index]) + row) = va_arg(argptr, uint64_t);
591 *(((uint64_t *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr, uint64_t *));
594 if (mode & SDDS_PASS_BY_VALUE)
595 *(((
float *)SDDS_dataset->data[index]) + row) = (float)va_arg(argptr,
double);
597 *(((
float *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr,
double *));
600 if (mode & SDDS_PASS_BY_VALUE)
601 *(((
double *)SDDS_dataset->data[index]) + row) = va_arg(argptr,
double);
603 *(((
double *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr,
double *));
606 if (mode & SDDS_PASS_BY_VALUE)
607 *(((
long double *)SDDS_dataset->data[index]) + row) = va_arg(argptr,
long double);
609 *(((
long double *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr,
long double *));
612 if (((
char **)SDDS_dataset->data[index])[row]) {
613 free(((
char **)SDDS_dataset->data[index])[row]);
614 ((
char **)SDDS_dataset->data[index])[row] = NULL;
616 if (mode & SDDS_PASS_BY_VALUE) {
617 if (!
SDDS_CopyString((
char **)SDDS_dataset->data[index] + row, va_arg(argptr,
char *))) {
618 SDDS_SetError(
"Unable to set string column value--allocation failure (SDDS_SetRowValues)");
622 if (!
SDDS_CopyString((
char **)SDDS_dataset->data[index] + row, *(va_arg(argptr,
char **)))) {
623 SDDS_SetError(
"Unable to set string column value--allocation failure (SDDS_SetRowValues)");
629 if (mode & SDDS_PASS_BY_VALUE)
630 *(((
char *)SDDS_dataset->data[index]) + row) = (char)va_arg(argptr,
int);
632 *(((
char *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr,
char *));
635 SDDS_SetError(
"Unknown data type encountered (SDDS_SetRowValues");
639 }
while (retval == -1);
644char **process_name_options(
char **orig_name,
long orig_names,
char **
delete,
long deletes,
char **retain,
long retains,
long *num_matched,
long **origToNewIndex) {
648 long *orig_flag = NULL;
650 orig_flag =
tmalloc(
sizeof(*orig_flag) * orig_names);
651 for (i = 0; i < orig_names; i++)
655 for (i = 0; i < deletes; i++) {
660 for (j = 0; j < orig_names; j++) {
661 for (i = 0; i < deletes; i++) {
668 for (i = 0; i < deletes; i++) {
675 for (i = 0; i < retains; i++) {
681 for (j = 0; j < orig_names; j++)
683 for (j = 0; j < orig_names; j++) {
686 for (i = 0; i < retains; i++) {
693 for (i = 0; i < retains; i++) {
700 for (j = 0; j < orig_names; j++) {
704 new_name =
tmalloc(
sizeof(*new_name) * (*num_matched));
706 for (j = 0; j < orig_names; j++) {
708 (*origToNewIndex)[j] = i;
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_LengthenTable(SDDS_DATASET *SDDS_dataset, int64_t n_additional_rows)
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_DefineSimpleColumns(SDDS_DATASET *SDDS_dataset, int32_t number, char **name, char **unit, int32_t type)
Defines multiple simple data columns of the same data type within the SDDS dataset.
int32_t SDDS_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
void SDDS_FreeArray(SDDS_ARRAY *array)
Frees memory allocated for an SDDS array structure.
void SDDS_SetError(char *error_text)
Records an error message in the SDDS error stack.
int32_t SDDS_VerifyArrayExists(SDDS_DATASET *SDDS_dataset, int32_t mode,...)
Verifies the existence of an array in the SDDS dataset based on specified criteria.
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column in the SDDS dataset.
int32_t SDDS_CheckDataset(SDDS_DATASET *SDDS_dataset, const char *caller)
Validates the SDDS dataset pointer.
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.
int32_t SDDS_VerifyColumnExists(SDDS_DATASET *SDDS_dataset, int32_t mode,...)
Verifies the existence of a column in the SDDS dataset based on specified criteria.
int32_t SDDS_CopyString(char **target, const char *source)
Copies a source string to a target string with memory allocation.
#define SDDS_ULONG
Identifier for the unsigned 32-bit integer data type.
#define SDDS_FLOAT
Identifier for the float data type.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_ULONG64
Identifier for the unsigned 64-bit integer data type.
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
#define SDDS_SHORT
Identifier for the signed short integer data type.
#define SDDS_CHARACTER
Identifier for the character data type.
#define SDDS_USHORT
Identifier for the unsigned short integer data type.
#define SDDS_DOUBLE
Identifier for the double data type.
#define SDDS_LONGDOUBLE
Identifier for the long double data type.
#define SDDS_LONG64
Identifier for the signed 64-bit integer data type.
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
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.
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)
void free_scanargs(SCANNED_ARG **scanned, int argc)
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.
char * expand_ranges(char *template)
Expand range specifiers in a wildcard template into explicit character lists.
int wild_match(char *string, char *template)
Determine whether one string is a wildcard match for another.