32#include "match_string.h"
34#define SET_SIGNAL_NAME 0
35#define SET_DESCRIPTION 1
36#define SET_MPL_LABELS 2
37#define SET_MAJOR_ORDER 3
40static char *option[N_OPTIONS] = {
47char *USAGE =
"tek2sdds <inputfile> <outputfile>\n\
49 [-description=<text>,<contents>]\n\
50 [-mpllabels=<title>,<topline>] [-majorOrder=row|column] \n\n\
51This program converts Tektronix ASCII format waveforms to SDDS format.\n\n\
52Program by Michael Borland (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
56 char *TEK_name, *SDDS_name, *value_string;
60#define TEK_PARAMETERS 23
77 {
"BYT/NR",
"TEKBytesPerNumber", NULL,
SDDS_LONG},
79 {NULL, NULL, NULL, 0},
82char *TEK_PreambleString =
"WFMPRE ";
83char *TEK_DataMarker =
"CURVE ";
84char *TEK_PointsName =
"NR.PT";
85char *TEK_XIncrementName =
"XINCR";
86char *TEK_XZeroName =
"XZERO";
87char *TEK_XUnitsName =
"XUNIT";
88char *TEK_YUnitsName =
"YUNIT";
89char *TEK_YZeroName =
"YZERO";
90char *TEK_YMultiplierName =
"YMULT";
91char *TEK_EncodingName =
"ENCDG";
92char *TEK_BytesPerNumberName =
"BYT/NR";
93char *TEK_ByteOrderName =
"BYT.OR";
97long GetNextItem(
char *buffer,
long bufsize, FILE *fpi);
99int main(
int argc,
char **argv) {
101 SCANNED_ARG *scanned;
102 long i, i_arg, index, points, bytes_per_number;
104 char *input, *output, buffer[BUFSIZE];
105 char *signal_name, *ptr, *parameter_name;
106 char *mpl_title, *mpl_topline, *descrip_text, *descrip_contents;
109 double xIncrement, xZero, yMultiplier, yZero;
110 char *xUnits, *yUnits;
112 short columnMajorOrder = 0;
113 unsigned long majorOrderFlag;
115 xUnits = yUnits = NULL;
117 argc =
scanargs(&scanned, argc, argv);
121 input = output = signal_name = NULL;
122 mpl_title = mpl_topline = descrip_text = descrip_contents = NULL;
125 for (i_arg = 1; i_arg < argc; i_arg++) {
126 if (scanned[i_arg].arg_type == OPTION) {
129 switch (
match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) {
130 case SET_MAJOR_ORDER:
132 scanned[i_arg].n_items--;
133 if (scanned[i_arg].n_items > 0 &&
134 (!
scanItemList(&majorOrderFlag, scanned[i_arg].list + 1, &scanned[i_arg].n_items, 0,
135 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
136 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)))
137 SDDS_Bomb(
"invalid -majorOrder syntax/values");
138 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
139 columnMajorOrder = 1;
140 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
141 columnMajorOrder = 0;
143 case SET_SIGNAL_NAME:
144 if (scanned[i_arg].n_items != 2)
145 bomb(
"invalid -signal_name syntax", USAGE);
146 signal_name = scanned[i_arg].list[1];
148 case SET_DESCRIPTION:
149 if (scanned[i_arg].n_items != 3)
150 bomb(
"invalid -description syntax", USAGE);
151 descrip_text = scanned[i_arg].list[1];
152 descrip_contents = scanned[i_arg].list[2];
155 if (scanned[i_arg].n_items != 3)
156 bomb(
"invalid -mpl_labels syntax", USAGE);
157 mpl_title = scanned[i_arg].list[1];
158 mpl_topline = scanned[i_arg].list[2];
161 bomb(
"invalid option seen", USAGE);
166 input = scanned[i_arg].list[0];
168 output = scanned[i_arg].list[0];
170 bomb(
"too many filenames", USAGE);
181 if (fread(buffer, 1, strlen(TEK_PreambleString), fpi) != strlen(TEK_PreambleString) ||
182 strncmp(TEK_PreambleString, buffer, strlen(TEK_PreambleString)) != 0)
183 SDDS_Bomb(
"file does not appear to be in Tektronix format");
185 parameter_name = buffer;
186 while ((code = GetNextItem(buffer, BUFSIZE, fpi)) < 3) {
187 if (!(ptr = strchr(buffer,
':')))
188 SDDS_Bomb(
"error parsing input file--missing colon on parameter tag");
190 if (strcmp(TEK_DataMarker, parameter_name) == 0)
193 while (TEK_parameter[index].TEK_name) {
194 if (strcmp(TEK_parameter[index].TEK_name, parameter_name) == 0)
198 if (!TEK_parameter[index].TEK_name) {
199 fprintf(stderr,
"warning: parameter %s is not recognized\n", parameter_name);
202 if (TEK_parameter[index].value_string) {
203 fprintf(stderr,
"error: duplicate entries for parameter %s\n", parameter_name);
208 if (code == 2 || code == 3)
214 if (fread(buffer, 1, strlen(TEK_DataMarker), fpi) != strlen(TEK_DataMarker) ||
215 strncmp(TEK_DataMarker, buffer, strlen(TEK_DataMarker)) != 0)
216 SDDS_Bomb(
"CURVE item missing or not in right place");
220 SDDS_table.layout.data_mode.column_major = columnMajorOrder;
223 while (TEK_parameter[index].TEK_name) {
224 if (!TEK_parameter[index].value_string) {
228 if (strcmp(TEK_parameter[index].TEK_name, TEK_XIncrementName) == 0) {
229 if (sscanf(TEK_parameter[index].value_string,
"%lf", &xIncrement) != 1)
230 SDDS_Bomb(
"unable to scan value for x increment");
231 }
else if (strcmp(TEK_parameter[index].TEK_name, TEK_XZeroName) == 0) {
232 if (sscanf(TEK_parameter[index].value_string,
"%lf", &xZero) != 1)
233 SDDS_Bomb(
"unable to scan value for x zero");
234 }
else if (strcmp(TEK_parameter[index].TEK_name, TEK_YZeroName) == 0) {
235 if (sscanf(TEK_parameter[index].value_string,
"%lf", &yZero) != 1)
236 SDDS_Bomb(
"unable to scan value for y zero");
237 }
else if (strcmp(TEK_parameter[index].TEK_name, TEK_YMultiplierName) == 0) {
238 if (sscanf(TEK_parameter[index].value_string,
"%lf", &yMultiplier) != 1)
239 SDDS_Bomb(
"unable to scan value for y multiplier");
240 }
else if (strcmp(TEK_parameter[index].TEK_name, TEK_XUnitsName) == 0) {
241 xUnits = TEK_parameter[index].value_string;
243 }
else if (strcmp(TEK_parameter[index].TEK_name, TEK_YUnitsName) == 0) {
244 yUnits = TEK_parameter[index].value_string;
246 }
else if (strcmp(TEK_parameter[index].TEK_name, TEK_PointsName) == 0) {
247 if (sscanf(TEK_parameter[index].value_string,
"%ld", &points) != 1)
248 SDDS_Bomb(
"unable to scan value for number of points");
249 }
else if (strcmp(TEK_parameter[index].TEK_name, TEK_EncodingName) == 0) {
250 if (strcmp(TEK_parameter[index].value_string,
"ASCII") == 0)
252 else if (strcmp(TEK_parameter[index].value_string,
"BINARY") == 0)
255 SDDS_Bomb(
"data encoding is neither ASCII nor BINARY");
256 }
else if (strcmp(TEK_parameter[index].TEK_name, TEK_BytesPerNumberName) == 0) {
257 if (sscanf(TEK_parameter[index].value_string,
"%ld", &bytes_per_number) != 1)
258 SDDS_Bomb(
"unable to scan value bytes per number");
259 }
else if (strcmp(TEK_parameter[index].TEK_name, TEK_ByteOrderName) == 0) {
261 if (strcmp(TEK_parameter[index].value_string,
"LSB") != 0) {
265 if (
SDDS_DefineParameter(&SDDS_table, TEK_parameter[index].SDDS_name, NULL, NULL, TEK_parameter[index].TEK_name,
266 NULL, TEK_parameter[index].type, TEK_parameter[index].value_string) < 0)
281 data =
tmalloc(
sizeof(*data) * points);
282 time =
tmalloc(
sizeof(*time) * points);
284 for (i = 0; i < points; i++) {
286 if (!(code = GetNextItem(buffer, BUFSIZE, fpi)))
287 SDDS_Bomb(
"insufficient data in input file");
292 time[i] = xZero + i * xIncrement;
293 if (sscanf(buffer,
"%lf", data + i) != 1)
295 data[i] = yZero + data[i] * yMultiplier;
299 fread(buffer,
sizeof(
char), 4, fpi);
300 for (i = 0; i < points; i++) {
301 if (fread(&sdata,
sizeof(sdata), 1, fpi) != 1) {
302 fprintf(stderr,
"file ends unexpectedly\n");
306 time[i] = xZero + i * xIncrement;
308 data[i] = yZero + data[i] * yMultiplier;
312 if (!
SDDS_SetColumn(&SDDS_table, SDDS_SET_BY_NAME, time, points,
"t") ||
313 !
SDDS_SetColumn(&SDDS_table, SDDS_SET_BY_NAME, data, points, signal_name) ||
314 !SDDS_WriteTable(&SDDS_table) ||
320long GetNextItem(
char *buffer,
long bufsize, FILE *fpi) {
325 while (i < bufsize && (c = getc(fpi)) != EOF) {
326 if (c ==
',' || c ==
';' || c ==
'%')
330 if (c == EOF && i == 0)
339 fprintf(stderr,
"warning: invalid data seen--ignoring\n");
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_SetColumn(SDDS_DATASET *SDDS_dataset, int32_t mode, void *data, int64_t rows,...)
Sets the values for one data column in the current data table of an 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_DefineColumn(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, int32_t field_length)
Defines a data column within the SDDS dataset.
int32_t SDDS_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
int32_t SDDS_DefineParameter(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, char *fixed_value)
Defines a data parameter with a fixed string value.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
int32_t SDDS_CopyString(char **target, const char *source)
Copies a source string to a target string with memory allocation.
void SDDS_RemovePadding(char *s)
Removes leading and trailing whitespace from a string.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
#define SDDS_DOUBLE
Identifier for the double data type.
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 * delete_chars(char *s, char *t)
Removes all occurrences of characters found in string t from string s.
FILE * fopen_e(char *file, char *open_mode, long mode)
Opens a file with error checking, messages, and aborts.
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)
long scanItemList(unsigned long *flags, char **item, long *items, unsigned long mode,...)
Scans a list of items and assigns values based on provided keywords and types.
char * str_tolower(char *s)
Convert a string to lower case.