20int32_t
SDDS_ReadIntoMplTable(TABLE *mpl_data,
char *file, int64_t sample_interval, int32_t mpl_flags,
char *SDDS_tags);
28extern void delete_trailing_blanks(
char *s);
29extern int32_t
SDDS_ReadIntoMplTable(TABLE *mpl_data,
char *file, int64_t sample_interval, int32_t mpl_flags,
char *SDDS_tags);
45long get_table(TABLE *tab,
char *file, int64_t sample_interval,
long flags) {
47 long sigma_y_present, sigma_x_and_y_present;
51 char *ptr, *sdds_tags;
52 long sdds_expected = 0, sdds_data = 0;
54 sigma_y_present = sigma_x_and_y_present = 0;
55 tab->c1 = tab->c2 = tab->s1 = tab->s2 = NULL;
56 tab->xlab = tab->ylab = tab->topline = tab->title = NULL;
60#if defined(SDDS_SUPPORT)
62 if ((sdds_tags = strchr(file,
'='))) {
72 if (!(fp =
fopen_e(file,
"r", FOPEN_RETURN_ON_ERROR))) {
73 fprintf(stderr,
"error: unable to open file %s in mode r (get_table)\n", file);
77 if (!fgets_skip(s, OAGBUFSIZ, fp,
'!', 1))
80 if (strncmp(s,
"SDDS", 4) == 0) {
82 fprintf(stderr,
"error: unable to read requested data from SDDS file %s\n", file);
86 sigma_x_and_y_present = (sigma_y_present = tab->flags & SIGMA_Y_PRESENT) && (tab->flags & SIGMA_X_PRESENT);
88 if (sdds_expected && !sdds_data)
89 *(sdds_tags - 1) =
'=';
96 fprintf(stderr,
"file %s opened: sample_interval=%ld flags=%x\n", file, sample_interval, flags);
100 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
101 delete_trailing_blanks(s);
103 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
104 delete_trailing_blanks(s);
106 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
107 delete_trailing_blanks(s);
109 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
110 delete_trailing_blanks(s);
111 cp_str(&(tab->topline), s);
113 fprintf(stderr,
"labels:\n<%s>\n<%s>\n<%s>\n<%s>\n", tab->xlab, tab->ylab, tab->title, tab->topline);
119 tab->xlab = tab->ylab;
123 if (flags & READ_LABELS_ONLY) {
131 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
132 if (!*s || 1 != sscanf(s,
"%lf", &tmp)) {
133 fprintf(stderr,
"error in format of file %s--couldn't scan number of points\n", file);
138 fprintf(stderr,
"n_pts = %ld\n", n);
142 if (sample_interval <= 0)
144 n = n / sample_interval;
147 tab->c1 =
tmalloc(
sizeof(*(tab->c1)) * n);
148 tab->c2 =
tmalloc(
sizeof(*(tab->c2)) * n);
149 tab->s1 =
tmalloc(
sizeof(*(tab->s1)) * n);
150 tab->s2 =
tmalloc(
sizeof(*(tab->s2)) * n);
154 fprintf(stderr,
"data pointers are: %x, %x, %x, %x\n", (
unsigned long)tab->c1, (
unsigned long)tab->c2, (
unsigned long)tab->s1, (
unsigned long)tab->s2);
158 for (i = 0; i < n; i++) {
159 if (fgets_skip(s, OAGBUFSIZ, fp,
'!', (i == 0 ? 1 : sample_interval))) {
161 fprintf(stderr,
"string: <%s>\n", s);
163 if ((ptr = strchr(s,
'!')))
166 fprintf(stderr,
"warning: error in format of file %s\n", file);
168 tab->c1 =
trealloc((
void *)tab->c1,
sizeof(*(tab->c1)) * n);
169 tab->c2 =
trealloc((
void *)tab->c2,
sizeof(*(tab->c2)) * n);
170 tab->s1 =
trealloc((
void *)tab->s1,
sizeof(*(tab->s1)) * n);
171 tab->s2 =
trealloc((
void *)tab->s2,
sizeof(*(tab->s2)) * n);
174 tab->s1[i] = tab->s2[i] = 0;
183 sigma_x_and_y_present = 1;
184 tab->flags |= SIGMA_X_PRESENT + SIGMA_Y_PRESENT;
186 tab->s2[i] = tab->s1[i];
188 tab->flags |= SIGMA_Y_PRESENT;
193 fprintf(stderr,
"tab->flags = %x\n", tab->flags);
199 if (sigma_y_present && !
get_double(tab->s2 + i, s)) {
200 fprintf(stderr,
"error in format of file %s--expected sigma is missing\n", file);
202 }
else if (sigma_x_and_y_present && (!
get_double(tab->s1 + i, s) || !
get_double(tab->s2 + i, s))) {
203 fprintf(stderr,
"error in format of file %s--expected sigma is missing\n", file);
210 tab->c1[i] = tab->c2[i];
214 tab->s1[i] = tab->s2[i];
218 fprintf(stderr,
"Warning: file %s contains only %ld of %ld expected points.\n", file, i, n);
220 tab->c1 =
trealloc((
void *)tab->c1,
sizeof(*(tab->c1)) * n);
221 tab->c2 =
trealloc((
void *)tab->c2,
sizeof(*(tab->c2)) * n);
222 tab->s1 =
trealloc((
void *)tab->s1,
sizeof(*(tab->s1)) * n);
223 tab->s2 =
trealloc((
void *)tab->s2,
sizeof(*(tab->s2)) * n);
234 if (flags & REVERSE || ((flags & REORDER_ASCENDING) && tab->c1[0] > tab->c1[n - 1]) || ((flags & REORDER_DESCENDING) && tab->c1[0] < tab->c1[n - 1])) {
237 for (i = 0; i < n / 2; i++) {
239 bomb(
"something impossible happened in get_table()", NULL);
241 tab->c1[i] = tab->c1[m - i];
242 tab->c1[m - i] = tmp;
244 tab->c2[i] = tab->c2[m - i];
245 tab->c2[m - i] = tmp;
247 tab->s1[i] = tab->s1[m - i];
248 tab->s1[m - i] = tmp;
250 tab->s2[i] = tab->s2[m - i];
251 tab->s2[m - i] = tmp;
257 if (fgets_skip(s, OAGBUFSIZ, fp,
'!', sample_interval))
258 fprintf(stderr,
"Warning: file %s contains excess data (which is ignored).\n", file);
263 if (!(tab->flags & SIGMA_X_PRESENT || tab->flags & SIGMA_Y_PRESENT) && !(flags & SAVE_SIGMA_ARRAYS)) {
275 for (i = 0; i < n; i++) {
276 fprintf(stderr,
"%ldth point: c1=%le c2=%le", i, tab->c1[i], tab->c2[i]);
278 fprintf(stderr,
" s1=%le s2=%le\n", tab->s1[i], tab->s2[i]);
293void put_table(
char *file, TABLE *tab,
char *format) {
300 fp =
fopen_e(file,
"w", FOPEN_SAVE_IF_EXISTS);
302 fprintf(fp,
"%s\n%s\n%s\n%s\n%-10ld\n", tab->xlab, tab->ylab, tab->title, tab->topline, tab->n_data);
304 if (tab->flags & SIGMA_X_PRESENT && tab->flags & SIGMA_Y_PRESENT) {
306 format =
"%le %le %le %le\n";
307 for (i = 0; i < tab->n_data; i++)
308 fprintf(fp, format, tab->c1[i], tab->c2[i], tab->s1[i], tab->s2[i]);
309 }
else if (tab->flags & SIGMA_X_PRESENT) {
311 format =
"%le %le %le 0.0\n";
312 for (i = 0; i < tab->n_data; i++)
313 fprintf(fp, format, tab->c1[i], tab->c2[i], tab->s1[i]);
314 }
else if (tab->flags & SIGMA_Y_PRESENT) {
316 format =
"%le %le %le\n";
317 for (i = 0; i < tab->n_data; i++)
318 fprintf(fp, format, tab->c1[i], tab->c2[i], tab->s2[i]);
321 format =
"%le %le\n";
322 for (i = 0; i < tab->n_data; i++)
323 fprintf(fp, format, tab->c1[i], tab->c2[i]);
333long get_table_float(TABLE_FLOAT *tab,
char *file,
long sample_interval,
long flags) {
335 long sigma_y_present, sigma_x_and_y_present;
343 fprintf(stderr,
"file %s opened: sample_interval=%ld flags=%x\n", file, sample_interval, flags);
346 sigma_y_present = sigma_x_and_y_present = 0;
349 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
350 delete_trailing_blanks(s);
352 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
353 delete_trailing_blanks(s);
355 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
356 delete_trailing_blanks(s);
358 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
359 delete_trailing_blanks(s);
360 cp_str(&(tab->topline), s);
363 tab->xlab = tab->ylab;
367 fprintf(stderr,
"labels:\n<%s>\n<%s>\n<%s>\n<%s>\n", tab->xlab, tab->ylab, tab->title, tab->topline);
370 if (flags & READ_LABELS_ONLY) {
376 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
377 if (!*s || 1 != sscanf(s,
"%f", &tmp)) {
378 fprintf(stderr,
"error in format of file %s--couldn't scan number of points\n", file);
383 fprintf(stderr,
"n_pts = %ld\n", n);
387 if (sample_interval <= 0)
389 n = n / sample_interval;
392 tab->c1 =
tmalloc(
sizeof(*(tab->c1)) * n);
393 tab->c2 =
tmalloc(
sizeof(*(tab->c2)) * n);
394 tab->s1 =
tmalloc(
sizeof(*(tab->s1)) * n);
395 tab->s2 =
tmalloc(
sizeof(*(tab->s2)) * n);
399 fprintf(stderr,
"data pointers are: %x, %x, %x, %x\n", (
unsigned long)tab->c1, (
unsigned long)tab->c2, (
unsigned long)tab->s1, (
unsigned long)tab->s2);
403 for (i = 0; i < n; i++) {
404 if (fgets_skip(s, OAGBUFSIZ, fp,
'!', (i == 0 ? 1 : sample_interval))) {
406 fprintf(stderr,
"string: <%s>\n", s);
408 if ((ptr = strchr(s,
'!')))
411 fprintf(stderr,
"warning: error in format of file %s--point %ld\n", file, n);
413 tab->c1 =
trealloc((
void *)tab->c1,
sizeof(*(tab->c1)) * n);
414 tab->c2 =
trealloc((
void *)tab->c2,
sizeof(*(tab->c2)) * n);
415 tab->s1 =
trealloc((
void *)tab->s1,
sizeof(*(tab->s1)) * n);
416 tab->s2 =
trealloc((
void *)tab->s2,
sizeof(*(tab->s2)) * n);
419 tab->s1[i] = tab->s2[i] = 0;
428 sigma_x_and_y_present = 1;
429 tab->flags |= SIGMA_X_PRESENT + SIGMA_Y_PRESENT;
431 tab->s2[i] = tab->s1[i];
433 tab->flags |= SIGMA_Y_PRESENT;
438 fprintf(stderr,
"tab->flags = %x\n", tab->flags);
444 if (sigma_y_present && !
get_float(tab->s2 + i, s)) {
445 fprintf(stderr,
"error in format of file %s--expected sigma is missing\n", file);
447 }
else if (sigma_x_and_y_present && (!
get_float(tab->s1 + i, s) || !
get_float(tab->s2 + i, s))) {
448 fprintf(stderr,
"error in format of file %s--expected sigma is missing\n", file);
455 tab->c1[i] = tab->c2[i];
459 tab->s1[i] = tab->s2[i];
463 fprintf(stderr,
"Warning: file %s contains only %ld of %ld expected points.\n", file, i, n);
465 tab->c1 =
trealloc((
void *)tab->c1,
sizeof(*(tab->c1)) * n);
466 tab->c2 =
trealloc((
void *)tab->c2,
sizeof(*(tab->c2)) * n);
467 tab->s1 =
trealloc((
void *)tab->s1,
sizeof(*(tab->s1)) * n);
468 tab->s2 =
trealloc((
void *)tab->s2,
sizeof(*(tab->s2)) * n);
477 if (flags & REVERSE || ((flags & REORDER_ASCENDING) && tab->c1[0] > tab->c1[n - 1]) || ((flags & REORDER_DESCENDING) && tab->c1[0] < tab->c1[n - 1])) {
480 for (i = 0; i < n / 2; i++) {
482 tab->c1[i] = tab->c1[m - i];
483 tab->c1[m - i] = tmp;
485 tab->c2[i] = tab->c2[m - i];
486 tab->c2[m - i] = tmp;
488 tab->s1[i] = tab->s1[m - i];
489 tab->s1[m - i] = tmp;
491 tab->s2[i] = tab->s2[m - i];
492 tab->s2[m - i] = tmp;
497 if (fgets_skip(s, OAGBUFSIZ, fp,
'!', sample_interval))
498 fprintf(stderr,
"Warning: file %s contains excess data (which is ignored).\n", file);
501 if (!(tab->flags & SIGMA_X_PRESENT || tab->flags & SIGMA_Y_PRESENT) && !(flags & SAVE_SIGMA_ARRAYS)) {
509 for (i = 0; i < n; i++) {
510 fprintf(stderr,
"%ldth point: c1=%le c2=%le", i, tab->c1[i], tab->c2[i]);
512 fprintf(stderr,
" s1=%le s2=%le\n", tab->s1[i], tab->s2[i]);
529void put_table_float(
char *file, TABLE_FLOAT *tab,
char *format) {
533 fp =
fopen_e(file,
"w", FOPEN_SAVE_IF_EXISTS);
535 fprintf(fp,
"%s\n%s\n%s\n%s\n%-10ld\n", tab->xlab, tab->ylab, tab->title, tab->topline, tab->n_data);
537 if (tab->flags & SIGMA_X_PRESENT && tab->flags & SIGMA_Y_PRESENT) {
539 format =
"%e %e %e %e\n";
540 for (i = 0; i < tab->n_data; i++)
541 fprintf(fp, format, tab->c1[i], tab->c2[i], tab->s1[i], tab->s2[i]);
542 }
else if (tab->flags & SIGMA_X_PRESENT) {
544 format =
"%e %e %e 0.0\n";
545 for (i = 0; i < tab->n_data; i++)
546 fprintf(fp, format, tab->c1[i], tab->c2[i], tab->s1[i]);
547 }
else if (tab->flags & SIGMA_Y_PRESENT) {
549 format =
"%e %e %e\n";
550 for (i = 0; i < tab->n_data; i++)
551 fprintf(fp, format, tab->c1[i], tab->c2[i], tab->s2[i]);
555 for (i = 0; i < tab->n_data; i++)
556 fprintf(fp, format, tab->c1[i], tab->c2[i]);
566float *float_array_from_double(
double *x,
long n) {
570 if (!(ptr =
tmalloc(n *
sizeof(*ptr))))
573 for (i = 0; i < n; i++)
582double *double_array_from_float(
float *x,
long n) {
583 register double *ptr;
586 if (!(ptr =
tmalloc(n *
sizeof(*ptr))))
589 for (i = 0; i < n; i++)
598void delete_trailing_blanks(
char *s) {
601 ptr = strlen(s) - 1 + s;
602 while (ptr >= s && isspace(*ptr))
611char *fgets_skip(
char *s,
622 if (!fgets(s, slen, fp))
624 if (s[0] != skip_char)
626 }
while (i < skip_lines);
627 if ((
long)strlen(s) >= slen - 1) {
628 while ((c = getc(fp)))
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
int tfree(void *ptr)
Frees a memory block and records the deallocation if tracking is enabled.
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.
char * cp_str(char **s, char *t)
Copies a string, allocating memory for storage.
int get_double(double *dptr, char *s)
Parses a double value from the given string.
int get_float(float *fptr, char *s)
Parses a float value from the given string.
long fexists(const char *filename)
Checks if a file exists.
FILE * fopen_e(char *file, char *open_mode, long mode)
Opens a file with error checking, messages, and aborts.
int32_t SDDS_ReadIntoMplTable(TABLE *mpl_data, char *file, int64_t sample_interval, int32_t mpl_flags, char *SDDS_tags)
Reads SDDS data into an MPL-compatible table structure.
long SDDS_AddMplDefinition(SDDS_TABLE *SDDS_table, char *label, char *suffix, char *default_name, char *filename)
Adds an MPL-compatible column definition to an SDDS dataset.
int32_t SDDS_WriteMplTable(TABLE *mpl_data, char *file)
Writes an MPL-compatible table to an SDDS file.
void SDDS_FixMplName(char *name)
Cleans and fixes an MPL-compatible name by removing specific sequences and extra spaces.
long get_table(TABLE *tab, char *file, int64_t sample_interval, long flags)
Gets a table from a file in DPL format.
void SDDS_ExtractNameAndUnit(char **name, char **unit, char *label)
Extracts the name and unit from a labeled string.