SDDSlib
Loading...
Searching...
No Matches
agilentArb2sdds.c
1#include <mdb.h>
2#include <SDDS.h>
3#include <scan.h>
4#if defined(_WIN32)
5# include <io.h>
6# include <fcntl.h>
7#endif
8
9#define SET_ASCII 0
10#define SET_BINARY 1
11#define SET_PIPE 2
12#define SET_WITHINDEX 3
13#define SET_FLOAT 4
14#define SET_DOUBLE 5
15#define N_OPTIONS 6
16
17char *option[N_OPTIONS] = {
18 "ascii", "binary", "pipe", "withindex", "float", "double"};
19
20char *USAGE = "agilentArb2sdds [<inputFile>] [<outputFile>] [-pipe[=in][,out]]\n\
21[-ascii | -binary]\n\
22[-withIndex]\n\
23[-float | -double]\n\
24[-dumpHeader]\n\n\
25pipe SDDS toolkit pipe option.\n\
26ascii Requests SDDS ASCII output. Default is binary.\n\
27binary Requests SDDS BINARY output.\n\
28withIndex Add Index column.\n\
29float Output in float format. Default is double.\n\
30double Output in double format.\n\n\
31Converts Agilent Arbitrary Waveform files to SDDS.\n\
32Program by Robert Soliday. (" __DATE__ " " __TIME__ ", SVN revision: " SVN_VERSION ")\n";
33
34#define MAX_NUM_POINTS 100001
35int main(int argc, char *argv[]) {
36 SCANNED_ARG *scanned;
37 long iArg;
38 int ascii = 0, withIndex = 0, floatValues = 0;
39 unsigned long pipeFlags = 0;
40 char *input = NULL, *output = NULL;
41 FILE *fd;
42 SDDS_DATASET SDDSout;
43
44 long points;
45 short waveform[2 * MAX_NUM_POINTS];
46 char *pChar;
47 char buf;
48 int i;
49 double *IwaveIn;
50 double *QwaveIn;
51 int32_t *index = NULL;
52
54 argc = scanargs(&scanned, argc, argv);
55 if (argc < 2) {
56 fprintf(stderr, "%s", USAGE);
57 return (1);
58 }
59 for (iArg = 1; iArg < argc; iArg++) {
60 if (scanned[iArg].arg_type == OPTION) {
61 switch (match_string(scanned[iArg].list[0], option, N_OPTIONS, 0)) {
62 case SET_ASCII:
63 ascii = 1;
64 break;
65 case SET_BINARY:
66 ascii = 0;
67 break;
68 case SET_WITHINDEX:
69 withIndex = 1;
70 break;
71 case SET_FLOAT:
72 floatValues = 1;
73 break;
74 case SET_DOUBLE:
75 floatValues = 0;
76 break;
77 case SET_PIPE:
78 if (!processPipeOption(scanned[iArg].list + 1, scanned[iArg].n_items - 1, &pipeFlags)) {
79 fprintf(stderr, "invalid -pipe syntax\n");
80 return (1);
81 }
82 break;
83 default:
84 fprintf(stderr, "invalid option seen\n");
85 fprintf(stderr, "%s", USAGE);
86 return (1);
87 }
88 } else {
89 if (!input)
90 input = scanned[iArg].list[0];
91 else if (!output)
92 output = scanned[iArg].list[0];
93 else {
94 fprintf(stderr, "too many filenames\n");
95 fprintf(stderr, "%s", USAGE);
96 return (1);
97 }
98 }
99 }
100 processFilenames("agilentArb2sdds", &input, &output, pipeFlags, 0, NULL);
101
102 if (input) {
103 if (!fexists(input)) {
104 fprintf(stderr, "input file not found\n");
105 return (1);
106 }
107 if (!(fd = fopen(input, "rb"))) {
108 fprintf(stderr, "problem opening input file\n");
109 return (1);
110 }
111 } else {
112#if defined(_WIN32)
113 if (_setmode(_fileno(stdin), _O_BINARY) == -1) {
114 fprintf(stderr, "error: unable to set stdin to binary mode\n");
115 return (1);
116 }
117#endif
118 fd = stdin;
119 }
120
121 points = fread((void *)waveform, sizeof(short), MAX_NUM_POINTS * 2, fd);
122 if (points == MAX_NUM_POINTS * 2) {
123 fprintf(stderr, "error: number of points in the waveform exceeds the MAX_NUM_POINTS hardcoded in the program.\n");
124 return (1);
125 }
126 fclose(fd);
127
129 pChar = (char *)&waveform[0];
130 for (i = 0; i < points; i++) {
131 buf = *pChar;
132 *pChar = *(pChar + 1);
133 *(pChar + 1) = buf;
134 pChar += 2;
135 }
136 }
137
138 points = points / 2;
139 IwaveIn = malloc(sizeof(double) * points);
140 QwaveIn = malloc(sizeof(double) * points);
141 for (i = 0; i < points; i++) {
142 IwaveIn[i] = waveform[2 * i] / 32767.0;
143 QwaveIn[i] = waveform[2 * i + 1] / 32767.0;
144 }
145 if (withIndex) {
146 index = malloc(sizeof(int32_t) * points);
147 for (i = 0; i < points; i++) {
148 index[i] = i;
149 }
150 }
151 if (!SDDS_InitializeOutput(&SDDSout, ascii ? SDDS_ASCII : SDDS_BINARY, 1, NULL, NULL, output)) {
152 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
153 return (1);
154 }
155 if (withIndex) {
156 if (!SDDS_DefineSimpleColumn(&SDDSout, "Index", NULL, SDDS_LONG)) {
157 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
158 return (1);
159 }
160 }
161 if (!SDDS_DefineSimpleColumn(&SDDSout, "I", NULL, floatValues ? SDDS_FLOAT : SDDS_DOUBLE)) {
162 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
163 return (1);
164 }
165 if (!SDDS_DefineSimpleColumn(&SDDSout, "Q", NULL, floatValues ? SDDS_FLOAT : SDDS_DOUBLE)) {
166 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
167 return (1);
168 }
169 if (!SDDS_WriteLayout(&SDDSout)) {
170 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
171 return (1);
172 }
173 if (!SDDS_StartTable(&SDDSout, points)) {
174 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
175 return (1);
176 }
177 if (withIndex) {
178 if (!SDDS_SetColumnFromLongs(&SDDSout, SDDS_SET_BY_NAME, index, points, "Index")) {
179 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
180 return (1);
181 }
182 }
183 if (!SDDS_SetColumnFromDoubles(&SDDSout, SDDS_SET_BY_NAME, IwaveIn, points, "I")) {
184 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
185 return (1);
186 }
187 if (!SDDS_SetColumnFromDoubles(&SDDSout, SDDS_SET_BY_NAME, QwaveIn, points, "Q")) {
188 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
189 return (1);
190 }
191 if (!SDDS_WriteTable(&SDDSout)) {
192 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
193 return (1);
194 }
195
196 if (!SDDS_Terminate(&SDDSout)) {
197 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
198 return (1);
199 }
200
201 if (withIndex)
202 free(index);
203 free(IwaveIn);
204 free(QwaveIn);
205
206 free_scanargs(&scanned, argc);
207
208 return 0;
209}
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_SetColumnFromDoubles(SDDS_DATASET *SDDS_dataset, int32_t mode, double *data, int64_t rows,...)
Sets the values for a single data column using double-precision floating-point numbers.
int32_t SDDS_SetColumnFromLongs(SDDS_DATASET *SDDS_dataset, int32_t mode, int32_t *data, int64_t rows,...)
Sets the values for a single data column using long integer numbers.
int32_t SDDS_Terminate(SDDS_DATASET *SDDS_dataset)
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_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
Definition SDDS_utils.c:432
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
Definition SDDS_utils.c:288
int32_t SDDS_IsBigEndianMachine()
Determines whether the current machine uses big-endian byte ordering.
#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
#define SDDS_DOUBLE
Identifier for the double data type.
Definition SDDStypes.h:37
long fexists(const char *filename)
Checks if a file exists.
Definition fexists.c:27
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.
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
Definition scanargs.c:36
long processPipeOption(char **item, long items, unsigned long *flags)
Definition scanargs.c:356
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
Definition scanargs.c:390
void free_scanargs(SCANNED_ARG **scanned, int argc)
Definition scanargs.c:584