25#include "match_string.h"
38 return *((
long double *)data + index);
49 return *((
double *)data + index);
60 return *((
float *)data + index);
71 return *((int64_t *)data + index);
82 return *((uint64_t *)data + index);
93 return *((int32_t *)data + index);
104 return *((uint32_t *)data + index);
115 return *((
short *)data + index);
126 return *((
unsigned short *)data + index);
137 return atof(*((
char **)data + index));
148 return *((
char *)data + index);
161 SDDS_SetError(
"NULL data pointer passed (SDDS_ConvertToLongDouble)");
166 return ((
long double)*((
short *)data + index));
168 return ((
long double)*((
unsigned short *)data + index));
170 return ((
long double)*((int32_t *)data + index));
172 return ((
long double)*((uint32_t *)data + index));
174 return ((
long double)*((int64_t *)data + index));
176 return ((
long double)*((uint64_t *)data + index));
178 return ((
long double)*((
float *)data + index));
180 return ((
long double)*((
double *)data + index));
182 return (*((
long double *)data + index));
184 return ((
long double)*((
unsigned char *)data + index));
186 SDDS_SetError(
"Invalid data type seen (SDDS_ConvertToLongDouble)");
201 SDDS_SetError(
"NULL data pointer passed (SDDS_ConvertToDouble)");
206 return ((
double)*((
short *)data + index));
208 return ((
double)*((
unsigned short *)data + index));
210 return ((
double)*((int32_t *)data + index));
212 return ((
double)*((uint32_t *)data + index));
214 return ((
double)*((int64_t *)data + index));
216 return ((
double)*((uint64_t *)data + index));
218 return ((
double)*((
float *)data + index));
220 return (*((
double *)data + index));
222 return ((
double)*((
long double *)data + index));
224 return ((
double)*((
unsigned char *)data + index));
226 SDDS_SetError(
"Invalid data type seen (SDDS_ConvertToDouble)");
241 SDDS_SetError(
"NULL data pointer passed (SDDS_ConvertToLong64)");
246 return ((int64_t) * ((
long double *)data + index));
248 return ((int64_t) * ((
double *)data + index));
250 return ((int64_t) * ((
float *)data + index));
252 return ((int64_t) * ((
short *)data + index));
254 return ((int64_t) * ((
unsigned short *)data + index));
256 return ((int64_t) * ((int32_t *)data + index));
258 return ((int64_t) * ((uint32_t *)data + index));
260 return (*((int64_t *)data + index));
262 return ((int64_t) * ((uint64_t *)data + index));
264 return ((int64_t) * ((
unsigned char *)data + index));
266 SDDS_SetError(
"Invalid data type seen (SDDS_ConvertToLong64)");
281 SDDS_SetError(
"NULL data pointer passed (SDDS_ConvertToLong)");
286 return ((int32_t) * ((
long double *)data + index));
288 return ((int32_t) * ((
double *)data + index));
290 return ((int32_t) * ((
float *)data + index));
292 return ((int32_t) * ((
short *)data + index));
294 return ((int32_t) * ((
unsigned short *)data + index));
296 return (*((int32_t *)data + index));
298 return ((int32_t) * ((uint32_t *)data + index));
300 return ((int32_t) * ((int64_t *)data + index));
302 return ((int32_t) * ((uint64_t *)data + index));
304 return ((int32_t) * ((
unsigned char *)data + index));
306 SDDS_SetError(
"Invalid data type seen (SDDS_ConvertToLong)");
311#if defined(RPN_SUPPORT)
313static double (*SDDS_ConvertTypeToDouble[
SDDS_NUM_TYPES + 1])(
void *data, int64_t index) =
329static int64_t table_number_mem = -1;
330static int64_t i_page_mem = -1;
331static int64_t n_rows_mem = -1;
332static int64_t i_row_mem = -1;
344 return (rpn_create_mem((
char *)name, is_string));
361 if ((memnum = is_memory(&dummy, &dummy1, &is_string, name)) >= 0)
363 if ((memnum = rpn_create_mem(name, is_string)) >= 0)
364 rpn_store((
double)rpn_createarray(1), NULL, memnum);
376int32_t SDDS_ComputeParameter(
SDDS_DATASET *SDDS_dataset, int32_t parameter,
char *equation) {
382 layout = &SDDS_dataset->layout;
383 if (parameter < 0 || parameter >= layout->n_parameters)
387 SDDS_SetError(
"Unable to compute defined parameter--no equation for named parameter (SDDS_ComputeParameter)");
391 if (!SDDS_StoreParametersInRpnMemories(SDDS_dataset))
393 if (!SDDS_StoreColumnsInRpnArrays(SDDS_dataset))
396 value = rpn(equation);
397 rpn_store(value, NULL, layout->parameter_definition[parameter].memory_number);
398 if (rpn_check_error()) {
399 SDDS_SetError(
"Unable to compute rpn expression--rpn error (SDDS_ComputeParameter)");
403 fprintf(stderr,
"computed parameter value %s with equation %s: %e\n", layout->parameter_definition[parameter].name, equation, value);
405 switch (layout->parameter_definition[parameter].type) {
407 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (
char)value, -1);
410 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (
short)value, -1);
413 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (
unsigned short)value, -1);
416 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (int32_t)value, -1);
419 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (uint32_t)value, -1);
422 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (int64_t)value, -1);
425 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (uint64_t)value, -1);
428 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (
float)value, -1);
431 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (
double)value, -1);
434 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (
long double)value, -1);
449int32_t SDDS_ComputeColumn(
SDDS_DATASET *SDDS_dataset, int32_t column,
char *equation) {
456 layout = &SDDS_dataset->layout;
457 if (column < 0 || column >= layout->n_columns)
460 if (!SDDS_StoreParametersInRpnMemories(SDDS_dataset))
462 if (!SDDS_StoreColumnsInRpnArrays(SDDS_dataset))
465 if (table_number_mem == -1) {
466 table_number_mem = rpn_create_mem(
"table_number", 0);
467 i_page_mem = rpn_create_mem(
"i_page", 0);
468 n_rows_mem = rpn_create_mem(
"n_rows", 0);
469 i_row_mem = rpn_create_mem(
"i_row", 0);
472 rpn_store((
double)SDDS_dataset->page_number, NULL, table_number_mem);
473 rpn_store((
double)SDDS_dataset->page_number, NULL, i_page_mem);
474 rpn_store((
double)SDDS_dataset->n_rows, NULL, n_rows_mem);
476 fprintf(stderr,
"computing %s using equation %s\n", layout->column_definition[column].name, equation);
479 for (j = 0; j < SDDS_dataset->n_rows; j++) {
481 if (!SDDS_StoreRowInRpnMemories(SDDS_dataset, j))
483 rpn_store((
double)j, NULL, i_row_mem);
484 value = rpn(equation);
485 rpn_store(value, NULL, layout->column_definition[column].memory_number);
486 if (rpn_check_error()) {
487 SDDS_SetError(
"Unable to compute rpn expression--rpn error (SDDS_ComputeDefinedColumn)");
491 fprintf(stderr,
"computed row value: %s = %e\n", layout->column_definition[column].name, value);
493 switch (layout->column_definition[column].type) {
495 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (
char)value, -1);
498 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (
short)value, -1);
501 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (
unsigned short)value, -1);
504 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (int32_t)value, -1);
507 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (uint32_t)value, -1);
510 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (int64_t)value, -1);
513 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (uint64_t)value, -1);
516 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (
float)value, -1);
519 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (
double)value, -1);
522 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (
long double)value, -1);
537int32_t SDDS_FilterRowsWithRpnTest(
SDDS_DATASET *SDDS_dataset,
char *rpn_test) {
542 static int64_t table_number_mem = -1, n_rows_mem = -1, i_page_mem = -1;
547 layout = &SDDS_dataset->layout;
549 if (table_number_mem == -1) {
550 table_number_mem = rpn_create_mem(
"table_number", 0);
551 i_page_mem = rpn_create_mem(
"page_number", 0);
552 n_rows_mem = rpn_create_mem(
"n_rows", 0);
553 i_row_mem = rpn_create_mem(
"i_row", 0);
556 rpn_store((
double)SDDS_dataset->page_number, NULL, table_number_mem);
557 rpn_store((
double)SDDS_dataset->page_number, NULL, i_page_mem);
558 rpn_store((
double)SDDS_dataset->n_rows, NULL, n_rows_mem);
560 for (i = 0; i < layout->n_columns; i++) {
561 if (layout->column_definition[i].memory_number < 0) {
562 SDDS_SetError(
"Unable to compute equations--column lacks rpn memory number (SDDS_FilterRowsWithRpnTest)");
567 n_columns = layout->n_columns;
569 for (j = 0; j < SDDS_dataset->n_rows; j++) {
571 rpn_store((
double)j, NULL, i_row_mem);
573 coldef = layout->column_definition;
574 for (i = 0; i < n_columns; i++, coldef++) {
576 rpn_quick_store((*SDDS_ConvertTypeToDouble[coldef->type])(SDDS_dataset->data[i], j), NULL, coldef->memory_number);
578 rpn_quick_store(0, ((
char **)SDDS_dataset->data[i])[j], coldef->memory_number);
582 if (rpn_check_error()) {
583 SDDS_SetError(
"Unable to compute rpn expression--rpn error (SDDS_FilterRowsWithRpnTest)");
586 if (!pop_log(&accept)) {
591 SDDS_dataset->row_flag[j] = 0;
603int32_t SDDS_StoreParametersInRpnMemories(
SDDS_DATASET *SDDS_dataset) {
609 layout = &SDDS_dataset->layout;
613 fprintf(stderr,
"storing parameters: ");
615 for (i = 0; i < layout->n_parameters; i++) {
616 if (layout->parameter_definition[i].memory_number < 0) {
617 SDDS_SetError(
"Unable to compute equations--parameter lacks rpn memory number (SDDS_StoreParametersInRpnMemories");
620 if (layout->parameter_definition[i].type !=
SDDS_STRING) {
621 rpn_quick_store((*SDDS_ConvertTypeToDouble[layout->parameter_definition[i].type])(SDDS_dataset->parameter[i], 0), NULL, layout->parameter_definition[i].memory_number);
623 fprintf(stderr,
"%s = %le ", layout->parameter_definition[i].name, rpn_recall(layout->parameter_definition[i].memory_number));
626 rpn_quick_store(0, ((
char **)SDDS_dataset->parameter[i])[0], layout->parameter_definition[i].memory_number);
628 fprintf(stderr,
"%s = %s ", layout->parameter_definition[i].name, rpn_str_recall(layout->parameter_definition[i].memory_number));
634 if (rpn_check_error()) {
635 SDDS_SetError(
"Unable to compute rpn expression--rpn error (SDDS_StoreParametersInRpnMemories)");
651int32_t SDDS_StoreRowInRpnMemories(
SDDS_DATASET *SDDS_dataset, int64_t row) {
655 columns = SDDS_dataset->layout.n_columns;
657 coldef = SDDS_dataset->layout.column_definition;
658 for (i = 0; i < columns; i++, coldef++) {
659 if (coldef->memory_number < 0) {
660 SDDS_SetError(
"Unable to compute equations--column lacks rpn memory number (SDDS_StoreRowInRpnMemories)");
665 coldef = SDDS_dataset->layout.column_definition;
666 for (i = 0; i < columns; i++, coldef++) {
668 rpn_quick_store((*SDDS_ConvertTypeToDouble[coldef->type])(SDDS_dataset->data[i], row), NULL, coldef->memory_number);
670 rpn_quick_store(0, ((
char **)SDDS_dataset->data[i])[row], coldef->memory_number);
682int32_t SDDS_StoreColumnsInRpnArrays(
SDDS_DATASET *SDDS_dataset) {
690 uint64_t *ulong64Ptr;
692 unsigned short *ushortPtr;
693 long double *ldoublePtr;
700 layout = &SDDS_dataset->layout;
702 for (i = 0; i < layout->n_columns; i++) {
703 if (layout->column_definition[i].type !=
SDDS_STRING) {
704 if (layout->column_definition[i].pointer_number < 0) {
705 SDDS_SetError(
"Unable to compute equations--column lacks rpn pointer number (SDDS_StoreColumnsInRpnArrays)");
708 if (!rpn_resizearray((int32_t)rpn_recall(layout->column_definition[i].pointer_number), SDDS_dataset->n_rows)) {
709 SDDS_SetError(
"Unable to compute equations--couldn't resize rpn arrays (SDDS_StoreColumnsInRpnArrays)");
712 if (!(arraydata = rpn_getarraypointer(layout->column_definition[i].pointer_number, &arraysize)) || arraysize != SDDS_dataset->n_rows) {
713 SDDS_SetError(
"Unable to compute equations--couldn't retrieve rpn arrays (SDDS_StoreColumnsInRpnArrays)");
716 switch (layout->column_definition[i].type) {
718 ldoublePtr = (
long double *)(SDDS_dataset->data[i]);
719 for (j = 0; j < SDDS_dataset->n_rows; j++)
720 arraydata[j] = ldoublePtr[j];
723 memcpy((
char *)arraydata, (
char *)(SDDS_dataset->data[i]), SDDS_dataset->n_rows *
sizeof(
double));
725 doublePtr = (
double *)(SDDS_dataset->data[i]);
726 for (j = 0; j < SDDS_dataset->n_rows; j++)
727 arraydata[j] = doublePtr[j];
731 floatPtr = (
float *)(SDDS_dataset->data[i]);
732 for (j = 0; j < SDDS_dataset->n_rows; j++)
733 arraydata[j] = floatPtr[j];
736 long64Ptr = (int64_t *)(SDDS_dataset->data[i]);
737 for (j = 0; j < SDDS_dataset->n_rows; j++)
738 arraydata[j] = long64Ptr[j];
741 ulong64Ptr = (uint64_t *)(SDDS_dataset->data[i]);
742 for (j = 0; j < SDDS_dataset->n_rows; j++)
743 arraydata[j] = ulong64Ptr[j];
746 longPtr = (int32_t *)(SDDS_dataset->data[i]);
747 for (j = 0; j < SDDS_dataset->n_rows; j++)
748 arraydata[j] = longPtr[j];
751 ulongPtr = (uint32_t *)(SDDS_dataset->data[i]);
752 for (j = 0; j < SDDS_dataset->n_rows; j++)
753 arraydata[j] = ulongPtr[j];
756 shortPtr = (
short *)(SDDS_dataset->data[i]);
757 for (j = 0; j < SDDS_dataset->n_rows; j++)
758 arraydata[j] = shortPtr[j];
761 ushortPtr = (
unsigned short *)(SDDS_dataset->data[i]);
762 for (j = 0; j < SDDS_dataset->n_rows; j++)
763 arraydata[j] = ushortPtr[j];
766 charPtr = (
char *)(SDDS_dataset->data[i]);
767 for (j = 0; j < SDDS_dataset->n_rows; j++)
768 arraydata[j] = charPtr[j];
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_SetRowValues(SDDS_DATASET *SDDS_dataset, int32_t mode, int64_t row,...)
int32_t SDDS_SetParameters(SDDS_DATASET *SDDS_dataset, int32_t mode,...)
Internal definitions and function declarations for SDDS with LZMA support.
int64_t SDDS_ConvertToLong64(int32_t type, void *data, int64_t index)
Converts a value to a 64-bit integer based on its type.
double SDDS_ConvertToDouble(int32_t type, void *data, int64_t index)
Converts a value to double based on its type.
int64_t SDDS_CreateRpnMemory(const char *name, short is_string)
Stub function for creating RPN memory when RPN_SUPPORT is not enabled.
long double SDDS_ConvertToLongDouble(int32_t type, void *data, int64_t index)
Converts a value to long double based on its type.
double SDDS_ConvertCharToDouble(void *data, int64_t index)
Converts a character to double.
double SDDS_ConvertULongToDouble(void *data, int64_t index)
Converts an unsigned 32-bit integer to double.
double SDDS_ConvertFloatToDouble(void *data, int64_t index)
Converts a float value to double.
double SDDS_ConvertLong64ToDouble(void *data, int64_t index)
Converts a 64-bit integer to double.
double SDDS_ConvertDoubleToDouble(void *data, int64_t index)
Converts a double value to double (identity function).
double SDDS_ConvertShortToDouble(void *data, int64_t index)
Converts a short integer to double.
double SDDS_ConvertLongToDouble(void *data, int64_t index)
Converts a 32-bit integer to double.
double SDDS_ConvertUShortToDouble(void *data, int64_t index)
Converts an unsigned short integer to double.
int64_t SDDS_CreateRpnArray(char *name)
Stub function for creating RPN arrays when RPN_SUPPORT is not enabled.
int32_t SDDS_ConvertToLong(int32_t type, void *data, int64_t index)
Converts a value to a 32-bit integer based on its type.
double SDDS_ConvertStringToDouble(void *data, int64_t index)
Converts a string to double using atof.
double SDDS_ConvertULong64ToDouble(void *data, int64_t index)
Converts an unsigned 64-bit integer to double.
double SDDS_ConvertLongDoubleToDouble(void *data, int64_t index)
Converts a long double value to double.
void SDDS_SetError(char *error_text)
Records an error message in the SDDS error stack.
int32_t SDDS_CheckDataset(SDDS_DATASET *SDDS_dataset, const char *caller)
Validates the SDDS dataset pointer.
int32_t SDDS_NumberOfErrors()
Retrieves the number of errors recorded by SDDS library routines.
#define SDDS_NUM_TYPES
Total number of defined SDDS data types.
#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.