63#if !defined(MAXDOUBLE)
64# define MAXDOUBLE 1.79769313486231570e+308
67char *option[N_OPTIONS] = {
68 "binary",
"ascii",
"float",
"double",
"snapshot",
"pipe",
69 "minimuminterval",
"time",
"delete",
"retain"};
72 "Usage: sddsconvertlogonchange [<input-file>] [<output-file>]\n"
73 " [-pipe=[input][,output]]\n"
74 " [-binary | -ascii]\n"
75 " [-double | -float]\n"
76 " [-minimumInterval=<seconds>]\n"
77 " [-snapshot=<epochtime>]\n"
78 " [-time=[start=<epochtime>][,end=<epochtime>]]\n"
79 " [-delete=<matching-string>[,...]]\n"
80 " [-retain=<matching-string>[,...]]\n\n"
81 "Program by Robert Soliday. (Compiled on " __DATE__
" at " __TIME__
", SVN revision: " SVN_VERSION
")\n";
83long SDDS_SetRowValuesMod(
SDDS_DATASET *SDDS_dataset,
long mode, int64_t row, ...);
84char **process_name_options(
char **orig_name,
long orig_names,
char **
delete,
long deletes,
char **retain,
long retains,
long *num_matched,
long **origToNewIndex);
86int main(
int argc,
char **argv) {
90 long tmpfile_used, noWarnings;
92 long ascii_output, binary_output;
93 unsigned long pipeFlags;
98 long readbackNameIndex = -1, controlNameIndex = -1;
99 long timeIndex = -1, valueIndex = -1, controlNameIndexIndex = -1, previousRowIndex = -1;
100 double *timeData = NULL, *valueData = NULL, *previousRowData = NULL;
101 int32_t *controlNameIndexData = NULL;
105 int64_t row, rows, snapshotrow = 0, initRow = 0, outrow;
106 double *rowdata = NULL;
107 long *origToNewIndex = NULL;
108 double minimumInterval = -1;
109 double timeInterval, previousTime = 0;
110 int64_t totalRows = 0, currentRows = 0;
111 short filterTime = 0;
112 double startTime = 0, endTime = MAXDOUBLE;
113 int64_t startTimeRow = 0, endTimeRow = 0;
115 char **ColumnName = NULL;
117 char **retain_name = NULL;
118 long retain_names = 0;
119 char **delete_name = NULL;
120 long delete_names = 0;
123 argc =
scanargs(&s_arg, argc, argv);
125 fprintf(stderr,
"%s", USAGE);
128 input = output = NULL;
129 ascii_output = binary_output = noWarnings = 0;
134 for (i_arg = 1; i_arg < argc; i_arg++) {
135 if (s_arg[i_arg].arg_type == OPTION) {
137 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
153 if (s_arg[i_arg].n_items < 2) {
154 fprintf(stderr,
"Error: Invalid syntax for -snapshot option.\n");
158 if (sscanf(s_arg[i_arg].list[1],
"%lf", &epochtime) != 1) {
159 fprintf(stderr,
"Error: Invalid value for -snapshot option.\n");
164 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags)) {
165 fprintf(stderr,
"Error: Invalid syntax for -pipe option.\n");
169 case SET_MININTERVAL:
170 if (s_arg[i_arg].n_items < 2) {
171 fprintf(stderr,
"Error: Invalid syntax for -minimumInterval option.\n");
174 if (sscanf(s_arg[i_arg].list[1],
"%lf", &minimumInterval) != 1) {
175 fprintf(stderr,
"Error: Invalid value for -minimumInterval option.\n");
180 s_arg[i_arg].n_items -= 1;
182 if (!
scanItemList(&flags, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
185 fprintf(stderr,
"Error: Invalid syntax for -time option.\n");
188 s_arg[i_arg].n_items += 1;
191 retain_name =
trealloc(retain_name,
sizeof(*retain_name) * (retain_names + s_arg[i_arg].n_items - 1));
192 for (i = 1; i < s_arg[i_arg].n_items; i++)
193 retain_name[i - 1 + retain_names] = s_arg[i_arg].list[i];
194 retain_names += s_arg[i_arg].n_items - 1;
197 delete_name =
trealloc(delete_name,
sizeof(*delete_name) * (delete_names + s_arg[i_arg].n_items - 1));
198 for (i = 1; i < s_arg[i_arg].n_items; i++)
199 delete_name[i - 1 + delete_names] = s_arg[i_arg].list[i];
200 delete_names += s_arg[i_arg].n_items - 1;
203 fprintf(stderr,
"Error (%s): Unknown option: %s\n", argv[0], s_arg[i_arg].list[0]);
209 input = s_arg[i_arg].list[0];
210 else if (output == NULL)
211 output = s_arg[i_arg].list[0];
213 fprintf(stderr,
"Error: Too many filenames provided.\n");
219 if ((snapshot == 1) && (minimumInterval >= 0)) {
220 fprintf(stderr,
"Error: -snapshot and -minimumInterval options cannot be used together.\n");
223 if ((snapshot == 1) && (filterTime == 1)) {
224 fprintf(stderr,
"Error: -snapshot and -time options cannot be used together.\n");
228 processFilenames(
"sddsconvertlogonchange", &input, &output, pipeFlags, noWarnings, &tmpfile_used);
241 if ((readbackNameIndex == -1) && (controlNameIndex == -1)) {
242 fprintf(stderr,
"Error: Both ReadbackName and ControlName arrays are missing from the input file.\n");
245 if (timeIndex == -1) {
246 fprintf(stderr,
"Error: Time column is missing.\n");
249 if (valueIndex == -1) {
250 fprintf(stderr,
"Error: Value column is missing.\n");
253 if (controlNameIndexIndex == -1) {
254 fprintf(stderr,
"Error: ControlNameIndex column is missing.\n");
258 if (!
SDDS_InitializeOutput(&SDDS_output, ascii_output ? SDDS_ASCII : (binary_output ? SDDS_BINARY : SDDS_input.layout.data_mode.mode), 1, NULL, NULL, output)) {
264 while (SDDS_ReadTable(&SDDS_input) > 0) {
266 rows = SDDS_RowCount(&SDDS_input);
268 if (readbackNameIndex != -1) {
269 readbackNameArray =
SDDS_GetArray(&SDDS_input,
"ReadbackName", NULL);
271 readbackNameArray =
SDDS_GetArray(&SDDS_input,
"ControlName", NULL);
273 if (readbackNameArray == NULL) {
278 origToNewIndex = malloc(
sizeof(
long) * readbackNameArray->elements);
279 for (i = 0; i < readbackNameArray->elements; i++) {
280 origToNewIndex[i] = -1;
283 ColumnName = process_name_options(((
char **)readbackNameArray->data), readbackNameArray->elements, delete_name, delete_names, retain_name, retain_names, &ColumnNames, &origToNewIndex);
284 rowdata = malloc(
sizeof(
double) * ColumnNames);
285 for (i = 0; i < ColumnNames; i++) {
302 if (SDDS_StartTable(&SDDS_output, 1) == 0) {
307 if (SDDS_StartTable(&SDDS_output, 100) == 0) {
314 if (readbackNameIndex != -1) {
315 readbackNameArray2 =
SDDS_GetArray(&SDDS_input,
"ReadbackName", NULL);
317 readbackNameArray2 =
SDDS_GetArray(&SDDS_input,
"ControlName", NULL);
319 if (readbackNameArray2 == NULL) {
323 if (readbackNameArray->elements != readbackNameArray2->elements) {
324 fprintf(stderr,
"Error: Multiple pages with differing ReadbackName and/or ControlName columns cannot be processed.\n");
327 for (i = 0; i < readbackNameArray->elements; i++) {
328 if (strcmp((
char *)((
char **)readbackNameArray->data)[i], (
char *)((
char **)readbackNameArray2->data)[i]) != 0) {
329 fprintf(stderr,
"Error: Multiple pages with differing ReadbackName and/or ControlName columns cannot be processed.\n");
349 for (row = 0; row < rows; row++) {
350 if (timeData[row] <= epochtime) {
358 for (row = 0; row < rows; row++) {
359 if (timeData[row] <= startTime) {
365 for (row = 0; row < rows; row++) {
366 if (timeData[row] <= endTime) {
373 if (previousRowIndex != -1) {
378 for (row = rows - 1; row >= 0; row--) {
379 if (previousRowData[row] == -2) {
380 if (origToNewIndex[controlNameIndexData[row]] == -1) {
387 free(previousRowData);
390 if (minimumInterval > 0) {
391 previousTime = timeData[0] - minimumInterval - 1;
393 for (row = 0; row < rows; row++) {
394 if ((readbackNameArray->elements < controlNameIndexData[row]) ||
395 (controlNameIndexData[row] < 0)) {
399 if (origToNewIndex[controlNameIndexData[row]] == -1) {
402 rowdata[origToNewIndex[controlNameIndexData[row]]] = valueData[row];
403 if (previousRowIndex != -1) {
408 if (((snapshot == 0) && (filterTime == 0)) ||
409 ((snapshot == 1) && (row == snapshotrow)) ||
410 ((filterTime == 1) && (row >= startTimeRow) && (row <= endTimeRow))) {
411 if (minimumInterval > 0) {
412 timeInterval = timeData[row] - previousTime;
413 if (timeInterval <= minimumInterval) {
416 previousTime = timeData[row];
419 if ((snapshot == 0) && (totalRows == currentRows)) {
427 if (SDDS_SetRowValuesMod(&SDDS_output, SDDS_SET_BY_INDEX | SDDS_PASS_BY_REFERENCE, outrow, 0, &timeData[row], -1) == 0) {
431 for (i = 0; i < ColumnNames; i++) {
432 if (SDDS_SetRowValuesMod(&SDDS_output, SDDS_SET_BY_INDEX | SDDS_PASS_BY_REFERENCE, outrow, i + 1, &rowdata[i], -1) == 0) {
446 if (controlNameIndexData)
447 free(controlNameIndexData);
452 free(origToNewIndex);
455 for (i = 0; i < ColumnNames; i++) {
463 if (SDDS_WriteTable(&SDDS_output) == 0) {
480long SDDS_SetRowValuesMod(
SDDS_DATASET *SDDS_dataset,
long mode, int64_t row, ...) {
490 if (!(mode & SDDS_SET_BY_INDEX || mode & SDDS_SET_BY_NAME) ||
491 !(mode & SDDS_PASS_BY_VALUE || mode & SDDS_PASS_BY_REFERENCE)) {
492 SDDS_SetError(
"Unable to set column values--unknown mode (SDDS_SetRowValues)");
499 row -= SDDS_dataset->first_row_in_mem;
500 if (row >= SDDS_dataset->n_rows_allocated) {
501 sprintf(buffer,
"Unable to set column values--row number (%" PRId64
") exceeds allocated memory (%" PRId64
") (SDDS_SetRowValues)", row, SDDS_dataset->n_rows_allocated);
505 if (row > SDDS_dataset->n_rows - 1)
506 SDDS_dataset->n_rows = row + 1;
508 va_start(argptr, row);
509 layout = &SDDS_dataset->layout;
514 fprintf(stderr,
"setting row %" PRId64
" (mem slot %" PRId64
")\n", row + SDDS_dataset->first_row_in_mem, row);
517 if (mode & SDDS_SET_BY_INDEX) {
518 if ((index = va_arg(argptr, int32_t)) == -1) {
522 if (index < 0 || index >= layout->n_columns) {
523 SDDS_SetError(
"Unable to set column values--index out of range (SDDS_SetRowValues)");
528 fprintf(stderr,
"Setting values for column #%ld\n", index);
531 if ((name = va_arg(argptr,
char *)) == NULL) {
536 fprintf(stderr,
"Setting values for column %s\n", name);
539 SDDS_SetError(
"Unable to set column values--name not recognized (SDDS_SetRowValues)");
544 switch (layout->column_definition[index].type) {
546 if (mode & SDDS_PASS_BY_VALUE)
547 *(((
short *)SDDS_dataset->data[index]) + row) = (short)va_arg(argptr,
int);
549 *(((
short *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr,
short *));
552 if (mode & SDDS_PASS_BY_VALUE)
553 *(((
unsigned short *)SDDS_dataset->data[index]) + row) = (
unsigned short)va_arg(argptr,
unsigned int);
555 *(((
unsigned short *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr,
unsigned short *));
558 if (mode & SDDS_PASS_BY_VALUE)
559 *(((int32_t *)SDDS_dataset->data[index]) + row) = va_arg(argptr, int32_t);
561 *(((int32_t *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr, int32_t *));
564 if (mode & SDDS_PASS_BY_VALUE)
565 *(((uint32_t *)SDDS_dataset->data[index]) + row) = va_arg(argptr, uint32_t);
567 *(((uint32_t *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr, uint32_t *));
570 if (mode & SDDS_PASS_BY_VALUE)
571 *(((int64_t *)SDDS_dataset->data[index]) + row) = va_arg(argptr, int64_t);
573 *(((int64_t *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr, int64_t *));
576 if (mode & SDDS_PASS_BY_VALUE)
577 *(((uint64_t *)SDDS_dataset->data[index]) + row) = va_arg(argptr, uint64_t);
579 *(((uint64_t *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr, uint64_t *));
582 if (mode & SDDS_PASS_BY_VALUE)
583 *(((
float *)SDDS_dataset->data[index]) + row) = (float)va_arg(argptr,
double);
585 *(((
float *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr,
double *));
588 if (mode & SDDS_PASS_BY_VALUE)
589 *(((
double *)SDDS_dataset->data[index]) + row) = va_arg(argptr,
double);
591 *(((
double *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr,
double *));
594 if (mode & SDDS_PASS_BY_VALUE)
595 *(((
long double *)SDDS_dataset->data[index]) + row) = va_arg(argptr,
long double);
597 *(((
long double *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr,
long double *));
600 if (((
char **)SDDS_dataset->data[index])[row]) {
601 free(((
char **)SDDS_dataset->data[index])[row]);
602 ((
char **)SDDS_dataset->data[index])[row] = NULL;
604 if (mode & SDDS_PASS_BY_VALUE) {
605 if (!
SDDS_CopyString((
char **)SDDS_dataset->data[index] + row, va_arg(argptr,
char *))) {
606 SDDS_SetError(
"Unable to set string column value--allocation failure (SDDS_SetRowValues)");
610 if (!
SDDS_CopyString((
char **)SDDS_dataset->data[index] + row, *(va_arg(argptr,
char **)))) {
611 SDDS_SetError(
"Unable to set string column value--allocation failure (SDDS_SetRowValues)");
617 if (mode & SDDS_PASS_BY_VALUE)
618 *(((
char *)SDDS_dataset->data[index]) + row) = (char)va_arg(argptr,
int);
620 *(((
char *)SDDS_dataset->data[index]) + row) = *(va_arg(argptr,
char *));
623 SDDS_SetError(
"Unknown data type encountered (SDDS_SetRowValues)");
627 }
while (retval == -1);
632char **process_name_options(
char **orig_name,
long orig_names,
char **
delete,
long deletes,
char **retain,
long retains,
long *num_matched,
long **origToNewIndex) {
636 long *orig_flag = NULL;
638 orig_flag =
tmalloc(
sizeof(*orig_flag) * orig_names);
639 for (i = 0; i < orig_names; i++)
643 for (i = 0; i < deletes; i++) {
648 for (j = 0; j < orig_names; j++) {
649 for (i = 0; i < deletes; i++) {
656 for (i = 0; i < deletes; i++) {
663 for (i = 0; i < retains; i++) {
669 for (j = 0; j < orig_names; j++)
671 for (j = 0; j < orig_names; j++) {
674 for (i = 0; i < retains; i++) {
681 for (i = 0; i < retains; i++) {
688 for (j = 0; j < orig_names; j++) {
692 new_name =
tmalloc(
sizeof(*new_name) * (*num_matched));
694 for (j = 0; j < orig_names; j++) {
696 (*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.