SDDSlib
Loading...
Searching...
No Matches
readexample.c
1/*************************************************************************\
2* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3* National Laboratory.
4* Copyright (c) 2002 The Regents of the University of California, as
5* Operator of Los Alamos National Laboratory.
6* This file is distributed subject to a Software License Agreement found
7* in the file LICENSE that is included with this distribution.
8\*************************************************************************/
9
10/* program: readexample.c
11 * purpose: simple example of reading an SDDS file
12 *
13 * Michael Borland, 1994
14 $Log: not supported by cvs2svn $
15 Revision 1.3 1995/09/06 14:55:58 saunders
16 First test release of SDDS1.5
17
18 */
19#include "SDDS.h"
20
21char *USAGE = "readexample <SDDSinput>\n\
22This program is a simple example that illustrates the use of\n\
23SDDS input routines.\n\
24Program by Michael Borland. ("__DATE__
25 " "__TIME__
26 ", SVN revision: " SVN_VERSION ")\n";
27
28#define COLUMNS 2
29
30main(int argc, char **argv) {
31 SDDS_DATASET SDDS_dataset;
32 char *input;
33 char *column_name[COLUMNS] = {"f", "FFT"};
34 char *column_units[COLUMNS] = {"MHz", ""};
35 double *f;
36 float *FFT;
37 long i, rows, *fl;
38
39 if (argc != 2) {
40 fprintf(stderr, "error: no input file given\nusage: %s\n", USAGE);
41 exit(1);
42 }
43
44 input = argv[1];
45
46 if (!SDDS_InitializeInput(&SDDS_dataset, input)) {
47 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
48 exit(1);
49 }
50
51 for (i = 0; i < COLUMNS; i++) {
52 long code;
53 if ((code = SDDS_CheckColumn(&SDDS_dataset, column_name[i], column_units[i], 0, NULL)) == SDDS_CHECK_NONEXISTENT) {
54 fprintf(stderr, "column %s not in file\n", column_name[i]);
55 exit(1);
56 }
57 if (code == SDDS_CHECK_WRONGUNITS) {
58 fprintf(stderr, "column %s doesn't have expected units (%s)\n", column_name[i], column_units[i]);
59 exit(1);
60 }
61 }
62
63 if (SDDS_ReadPage(&SDDS_dataset) != 1 || (rows = SDDS_CountRowsOfInterest(&SDDS_dataset)) < 0) {
64 fprintf(stderr, "first page of data set is unreadable or empty\n");
65 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
66 exit(1);
67 }
68
69 if (!(f = SDDS_GetColumnInDoubles(&SDDS_dataset, column_name[0]))) {
70 fprintf(stderr, "unable to read column %s\n", column_name[0]);
71 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
72 exit(1);
73 }
74
75 if (!(fl = SDDS_GetNumericColumn(&SDDS_dataset, column_name[0], SDDS_LONG))) {
76 fprintf(stderr, "unable to read column %s\n", column_name[0]);
77 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
78 exit(1);
79 }
80
81 if (!(FFT = SDDS_GetNumericColumn(&SDDS_dataset, column_name[1], SDDS_FLOAT))) {
82 fprintf(stderr, "unable to read column %s\n", column_name[1]);
83 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
84 exit(1);
85 }
86
87 for (i = 0; i < rows; i++)
88 fprintf(stderr, "row %ld: %ld, %e, %e\n", i, fl[i], f[i], FFT[i]);
89
90 SDDS_Terminate(&SDDS_dataset);
91}
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
void * SDDS_GetNumericColumn(SDDS_DATASET *SDDS_dataset, char *column_name, int32_t desiredType)
Retrieves the data of a specified numerical column as an array of a desired numerical type,...
int64_t SDDS_CountRowsOfInterest(SDDS_DATASET *SDDS_dataset)
Counts the number of rows marked as "of interest" in the current data table.
double * SDDS_GetColumnInDoubles(SDDS_DATASET *SDDS_dataset, char *column_name)
Retrieves the data of a specified numerical column as an array of doubles, considering only rows mark...
int32_t SDDS_InitializeInput(SDDS_DATASET *SDDS_dataset, char *filename)
Definition SDDS_input.c:49
int32_t SDDS_Terminate(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_ReadPage(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_CheckColumn(SDDS_DATASET *SDDS_dataset, char *name, char *units, int32_t type, FILE *fp_message)
Checks if a column exists in the SDDS dataset with the specified name, units, and type.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
Definition SDDS_utils.c:432
#define SDDS_FLOAT
Identifier for the float data type.
Definition SDDStypes.h:43
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
Definition SDDStypes.h:61