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=NULL, *orig_tag = NULL;
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 ((orig_tag = strchr(file,
'='))) {
73 if (!(fp =
fopen_e(file,
"r", FOPEN_RETURN_ON_ERROR))) {
74 fprintf(stderr,
"error: unable to open file %s in mode r (get_table)\n", file);
78 if (!fgets_skip(s, OAGBUFSIZ, fp,
'!', 1))
81 if (strncmp(s,
"SDDS", 4) == 0) {
83 fprintf(stderr,
"error: unable to read requested data from SDDS file %s\n", file);
87 sigma_x_and_y_present = (sigma_y_present = tab->flags & SIGMA_Y_PRESENT) && (tab->flags & SIGMA_X_PRESENT);
89 if (sdds_expected && !sdds_data)
97 fprintf(stderr,
"file %s opened: sample_interval=%ld flags=%x\n", file, sample_interval, flags);
101 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
102 delete_trailing_blanks(s);
104 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
105 delete_trailing_blanks(s);
107 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
108 delete_trailing_blanks(s);
110 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
111 delete_trailing_blanks(s);
112 cp_str(&(tab->topline), s);
114 fprintf(stderr,
"labels:\n<%s>\n<%s>\n<%s>\n<%s>\n", tab->xlab, tab->ylab, tab->title, tab->topline);
120 tab->xlab = tab->ylab;
124 if (flags & READ_LABELS_ONLY) {
132 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
133 if (!*s || 1 != sscanf(s,
"%lf", &tmp)) {
134 fprintf(stderr,
"error in format of file %s--couldn't scan number of points\n", file);
139 fprintf(stderr,
"n_pts = %ld\n", n);
143 if (sample_interval <= 0)
145 n = n / sample_interval;
148 tab->c1 =
tmalloc(
sizeof(*(tab->c1)) * n);
149 tab->c2 =
tmalloc(
sizeof(*(tab->c2)) * n);
150 tab->s1 =
tmalloc(
sizeof(*(tab->s1)) * n);
151 tab->s2 =
tmalloc(
sizeof(*(tab->s2)) * n);
155 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);
159 for (i = 0; i < n; i++) {
160 if (fgets_skip(s, OAGBUFSIZ, fp,
'!', (i == 0 ? 1 : sample_interval))) {
162 fprintf(stderr,
"string: <%s>\n", s);
164 if ((ptr = strchr(s,
'!')))
167 fprintf(stderr,
"warning: error in format of file %s\n", file);
169 tab->c1 =
trealloc((
void *)tab->c1,
sizeof(*(tab->c1)) * n);
170 tab->c2 =
trealloc((
void *)tab->c2,
sizeof(*(tab->c2)) * n);
171 tab->s1 =
trealloc((
void *)tab->s1,
sizeof(*(tab->s1)) * n);
172 tab->s2 =
trealloc((
void *)tab->s2,
sizeof(*(tab->s2)) * n);
175 tab->s1[i] = tab->s2[i] = 0;
184 sigma_x_and_y_present = 1;
185 tab->flags |= SIGMA_X_PRESENT + SIGMA_Y_PRESENT;
187 tab->s2[i] = tab->s1[i];
189 tab->flags |= SIGMA_Y_PRESENT;
194 fprintf(stderr,
"tab->flags = %x\n", tab->flags);
200 if (sigma_y_present && !
get_double(tab->s2 + i, s)) {
201 fprintf(stderr,
"error in format of file %s--expected sigma is missing\n", file);
203 }
else if (sigma_x_and_y_present && (!
get_double(tab->s1 + i, s) || !
get_double(tab->s2 + i, s))) {
204 fprintf(stderr,
"error in format of file %s--expected sigma is missing\n", file);
211 tab->c1[i] = tab->c2[i];
215 tab->s1[i] = tab->s2[i];
219 fprintf(stderr,
"Warning: file %s contains only %ld of %ld expected points.\n", file, i, n);
221 tab->c1 =
trealloc((
void *)tab->c1,
sizeof(*(tab->c1)) * n);
222 tab->c2 =
trealloc((
void *)tab->c2,
sizeof(*(tab->c2)) * n);
223 tab->s1 =
trealloc((
void *)tab->s1,
sizeof(*(tab->s1)) * n);
224 tab->s2 =
trealloc((
void *)tab->s2,
sizeof(*(tab->s2)) * n);
235 if (flags & REVERSE || ((flags & REORDER_ASCENDING) && tab->c1[0] > tab->c1[n - 1]) || ((flags & REORDER_DESCENDING) && tab->c1[0] < tab->c1[n - 1])) {
238 for (i = 0; i < n / 2; i++) {
240 bomb(
"something impossible happened in get_table()", NULL);
242 tab->c1[i] = tab->c1[m - i];
243 tab->c1[m - i] = tmp;
245 tab->c2[i] = tab->c2[m - i];
246 tab->c2[m - i] = tmp;
248 tab->s1[i] = tab->s1[m - i];
249 tab->s1[m - i] = tmp;
251 tab->s2[i] = tab->s2[m - i];
252 tab->s2[m - i] = tmp;
258 if (fgets_skip(s, OAGBUFSIZ, fp,
'!', sample_interval))
259 fprintf(stderr,
"Warning: file %s contains excess data (which is ignored).\n", file);
264 if (!(tab->flags & SIGMA_X_PRESENT || tab->flags & SIGMA_Y_PRESENT) && !(flags & SAVE_SIGMA_ARRAYS)) {
276 for (i = 0; i < n; i++) {
277 fprintf(stderr,
"%ldth point: c1=%le c2=%le", i, tab->c1[i], tab->c2[i]);
279 fprintf(stderr,
" s1=%le s2=%le\n", tab->s1[i], tab->s2[i]);
45long get_table(TABLE *tab,
char *file, int64_t sample_interval,
long flags) {
…}
294void put_table(
char *file, TABLE *tab,
char *format) {
301 fp =
fopen_e(file,
"w", FOPEN_SAVE_IF_EXISTS);
303 fprintf(fp,
"%s\n%s\n%s\n%s\n%-10ld\n", tab->xlab, tab->ylab, tab->title, tab->topline, tab->n_data);
305 if (tab->flags & SIGMA_X_PRESENT && tab->flags & SIGMA_Y_PRESENT) {
307 format =
"%le %le %le %le\n";
308 for (i = 0; i < tab->n_data; i++)
309 fprintf(fp, format, tab->c1[i], tab->c2[i], tab->s1[i], tab->s2[i]);
310 }
else if (tab->flags & SIGMA_X_PRESENT) {
312 format =
"%le %le %le 0.0\n";
313 for (i = 0; i < tab->n_data; i++)
314 fprintf(fp, format, tab->c1[i], tab->c2[i], tab->s1[i]);
315 }
else if (tab->flags & SIGMA_Y_PRESENT) {
317 format =
"%le %le %le\n";
318 for (i = 0; i < tab->n_data; i++)
319 fprintf(fp, format, tab->c1[i], tab->c2[i], tab->s2[i]);
322 format =
"%le %le\n";
323 for (i = 0; i < tab->n_data; i++)
324 fprintf(fp, format, tab->c1[i], tab->c2[i]);
334long get_table_float(TABLE_FLOAT *tab,
char *file,
long sample_interval,
long flags) {
336 long sigma_y_present, sigma_x_and_y_present;
344 fprintf(stderr,
"file %s opened: sample_interval=%ld flags=%x\n", file, sample_interval, flags);
347 sigma_y_present = sigma_x_and_y_present = 0;
350 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
351 delete_trailing_blanks(s);
353 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
354 delete_trailing_blanks(s);
356 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
357 delete_trailing_blanks(s);
359 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
360 delete_trailing_blanks(s);
361 cp_str(&(tab->topline), s);
364 tab->xlab = tab->ylab;
368 fprintf(stderr,
"labels:\n<%s>\n<%s>\n<%s>\n<%s>\n", tab->xlab, tab->ylab, tab->title, tab->topline);
371 if (flags & READ_LABELS_ONLY) {
377 fgets_skip(s, OAGBUFSIZ, fp,
'!', 1);
378 if (!*s || 1 != sscanf(s,
"%f", &tmp)) {
379 fprintf(stderr,
"error in format of file %s--couldn't scan number of points\n", file);
384 fprintf(stderr,
"n_pts = %ld\n", n);
388 if (sample_interval <= 0)
390 n = n / sample_interval;
393 tab->c1 =
tmalloc(
sizeof(*(tab->c1)) * n);
394 tab->c2 =
tmalloc(
sizeof(*(tab->c2)) * n);
395 tab->s1 =
tmalloc(
sizeof(*(tab->s1)) * n);
396 tab->s2 =
tmalloc(
sizeof(*(tab->s2)) * n);
400 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);
404 for (i = 0; i < n; i++) {
405 if (fgets_skip(s, OAGBUFSIZ, fp,
'!', (i == 0 ? 1 : sample_interval))) {
407 fprintf(stderr,
"string: <%s>\n", s);
409 if ((ptr = strchr(s,
'!')))
412 fprintf(stderr,
"warning: error in format of file %s--point %ld\n", file, n);
414 tab->c1 =
trealloc((
void *)tab->c1,
sizeof(*(tab->c1)) * n);
415 tab->c2 =
trealloc((
void *)tab->c2,
sizeof(*(tab->c2)) * n);
416 tab->s1 =
trealloc((
void *)tab->s1,
sizeof(*(tab->s1)) * n);
417 tab->s2 =
trealloc((
void *)tab->s2,
sizeof(*(tab->s2)) * n);
420 tab->s1[i] = tab->s2[i] = 0;
429 sigma_x_and_y_present = 1;
430 tab->flags |= SIGMA_X_PRESENT + SIGMA_Y_PRESENT;
432 tab->s2[i] = tab->s1[i];
434 tab->flags |= SIGMA_Y_PRESENT;
439 fprintf(stderr,
"tab->flags = %x\n", tab->flags);
445 if (sigma_y_present && !
get_float(tab->s2 + i, s)) {
446 fprintf(stderr,
"error in format of file %s--expected sigma is missing\n", file);
448 }
else if (sigma_x_and_y_present && (!
get_float(tab->s1 + i, s) || !
get_float(tab->s2 + i, s))) {
449 fprintf(stderr,
"error in format of file %s--expected sigma is missing\n", file);
456 tab->c1[i] = tab->c2[i];
460 tab->s1[i] = tab->s2[i];
464 fprintf(stderr,
"Warning: file %s contains only %ld of %ld expected points.\n", file, i, n);
466 tab->c1 =
trealloc((
void *)tab->c1,
sizeof(*(tab->c1)) * n);
467 tab->c2 =
trealloc((
void *)tab->c2,
sizeof(*(tab->c2)) * n);
468 tab->s1 =
trealloc((
void *)tab->s1,
sizeof(*(tab->s1)) * n);
469 tab->s2 =
trealloc((
void *)tab->s2,
sizeof(*(tab->s2)) * n);
478 if (flags & REVERSE || ((flags & REORDER_ASCENDING) && tab->c1[0] > tab->c1[n - 1]) || ((flags & REORDER_DESCENDING) && tab->c1[0] < tab->c1[n - 1])) {
481 for (i = 0; i < n / 2; i++) {
483 tab->c1[i] = tab->c1[m - i];
484 tab->c1[m - i] = tmp;
486 tab->c2[i] = tab->c2[m - i];
487 tab->c2[m - i] = tmp;
489 tab->s1[i] = tab->s1[m - i];
490 tab->s1[m - i] = tmp;
492 tab->s2[i] = tab->s2[m - i];
493 tab->s2[m - i] = tmp;
498 if (fgets_skip(s, OAGBUFSIZ, fp,
'!', sample_interval))
499 fprintf(stderr,
"Warning: file %s contains excess data (which is ignored).\n", file);
502 if (!(tab->flags & SIGMA_X_PRESENT || tab->flags & SIGMA_Y_PRESENT) && !(flags & SAVE_SIGMA_ARRAYS)) {
510 for (i = 0; i < n; i++) {
511 fprintf(stderr,
"%ldth point: c1=%le c2=%le", i, tab->c1[i], tab->c2[i]);
513 fprintf(stderr,
" s1=%le s2=%le\n", tab->s1[i], tab->s2[i]);
530void put_table_float(
char *file, TABLE_FLOAT *tab,
char *format) {
534 fp =
fopen_e(file,
"w", FOPEN_SAVE_IF_EXISTS);
536 fprintf(fp,
"%s\n%s\n%s\n%s\n%-10ld\n", tab->xlab, tab->ylab, tab->title, tab->topline, tab->n_data);
538 if (tab->flags & SIGMA_X_PRESENT && tab->flags & SIGMA_Y_PRESENT) {
540 format =
"%e %e %e %e\n";
541 for (i = 0; i < tab->n_data; i++)
542 fprintf(fp, format, tab->c1[i], tab->c2[i], tab->s1[i], tab->s2[i]);
543 }
else if (tab->flags & SIGMA_X_PRESENT) {
545 format =
"%e %e %e 0.0\n";
546 for (i = 0; i < tab->n_data; i++)
547 fprintf(fp, format, tab->c1[i], tab->c2[i], tab->s1[i]);
548 }
else if (tab->flags & SIGMA_Y_PRESENT) {
550 format =
"%e %e %e\n";
551 for (i = 0; i < tab->n_data; i++)
552 fprintf(fp, format, tab->c1[i], tab->c2[i], tab->s2[i]);
556 for (i = 0; i < tab->n_data; i++)
557 fprintf(fp, format, tab->c1[i], tab->c2[i]);
567float *float_array_from_double(
double *x,
long n) {
571 if (!(ptr =
tmalloc(n *
sizeof(*ptr))))
574 for (i = 0; i < n; i++)
583double *double_array_from_float(
float *x,
long n) {
584 register double *ptr;
587 if (!(ptr =
tmalloc(n *
sizeof(*ptr))))
590 for (i = 0; i < n; i++)
599void delete_trailing_blanks(
char *s) {
602 ptr = strlen(s) - 1 + s;
603 while (ptr >= s && isspace(*ptr))
612char *fgets_skip(
char *s,
623 if (!fgets(s, slen, fp))
625 if (s[0] != skip_char)
627 }
while (i < skip_lines);
628 if ((
long)strlen(s) >= slen - 1) {
629 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.