SDDSlib
Loading...
Searching...
No Matches
hpwf2sdds.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: hpwf2sdds.c
11 * purpose: converts a waveform from an HP scope to
12 * SDDS format
13 *
14 * M. Borland, 1994
15 $Log: not supported by cvs2svn $
16 Revision 1.4 2001/01/23 19:14:55 soliday
17 Standardized usage message.
18
19 Revision 1.3 1999/05/25 19:02:49 soliday
20 Removed compiler warning on linux.
21
22 Revision 1.2 1995/09/06 14:55:50 saunders
23 First test release of SDDS1.5
24
25*/
26#include "mdb.h"
27#include "SDDS.h"
28#include "scan.h"
29#include "match_string.h"
30
31#define SET_SIGNAL_NAME 0
32#define SET_DESCRIPTION 1
33#define SET_MPL_LABELS 2
34#define SET_MAJOR_ORDER 3
35#define N_OPTIONS 4
36
37static char *option[N_OPTIONS] = {
38 "signalname", "description", "mpllabels", "majorOrder"};
39
40char *USAGE = "hpwf2sdds <inputfile> <outputfile>\n\
41 -signalname=<name> [-description=<text>,<contents>]\n\
42 [-mpllabels=<title>,<topline>] [-majorOrder=row|column] \n\n\
43This program converts HP verbose format waveforms to SDDS format.\n\n\
44Program by Michael Borland (" __DATE__ " " __TIME__ ", SVN revision: " SVN_VERSION ")\n";
45
46typedef struct
47{
48 char *HP_name, *SDDS_name, *value_string;
49 long type;
51
52#define HP_PARAMETERS 23
53HP_PARAMETER HP_parameter[HP_PARAMETERS] = {
54 {"Type", "Type", NULL, SDDS_STRING},
55 {"Points", "Points", NULL, SDDS_LONG},
56 {"Count", "Count", NULL, SDDS_LONG},
57 {"XInc", "XInc", NULL, SDDS_DOUBLE},
58 {"XOrg", "XOrg", NULL, SDDS_DOUBLE},
59 {"XRef", "XRef", NULL, SDDS_DOUBLE},
60 {"YData range", "YDataRange", NULL, SDDS_DOUBLE},
61 {"YData center", "YDataCenter", NULL, SDDS_DOUBLE},
62 {"Coupling", "Coupling", NULL, SDDS_STRING},
63 {"XRange", "XRange", NULL, SDDS_DOUBLE},
64 {"XOffset", "XOffset", NULL, SDDS_DOUBLE},
65 {"YRange", "YRange", NULL, SDDS_DOUBLE},
66 {"YOffset", "YOffset", NULL, SDDS_DOUBLE},
67 {"Date", "Date", NULL, SDDS_STRING},
68 {"Time", "Time", NULL, SDDS_STRING},
69 {"Frame", "Frame", NULL, SDDS_STRING},
70 {"Acq mode", "AcqMode", NULL, SDDS_STRING},
71 {"Completion", "Completion", NULL, SDDS_STRING},
72 {"X Units", "XUnits", NULL, SDDS_STRING},
73 {"Y Units", "YUnits", NULL, SDDS_STRING},
74 {"Max Bandwidth", "MaxBandwidth", NULL, SDDS_DOUBLE},
75 {"Min Bandwidth", "MinBandwidth", NULL, SDDS_DOUBLE},
76 {NULL, NULL, NULL, 0},
77};
78
79char *HP_DataMarker = "Data";
80char *HP_XIncrementName = "XInc";
81char *HP_XOriginName = "XOrg";
82char *HP_XReferenceName = "XRef";
83char *HP_XUnitsName = "X Units";
84char *HP_YUnitsName = "Y Units";
85char *HP_PointsName = "Points";
86
87#define BUFSIZE 256
88
89int main(int argc, char **argv) {
90 SDDS_TABLE SDDS_table;
91 SCANNED_ARG *scanned;
92 long i, i_arg, index, points;
93 char *input, *output, buffer[BUFSIZE];
94 char *signal_name, *ptr, *parameter_name;
95 char *mpl_title, *mpl_topline, *descrip_text, *descrip_contents;
96 FILE *fpi;
97 double xIncrement, xOrigin, xReference;
98 char *xUnits, *yUnits;
99 double *time, *data;
100 unsigned long majorOrderFlag;
101 short columnMajorOrder = -1;
102
103 argc = scanargs(&scanned, argc, argv);
104 if (argc < 3)
105 bomb(NULL, USAGE);
106
107 input = output = signal_name = xUnits = yUnits = NULL;
108 mpl_title = mpl_topline = descrip_text = descrip_contents = NULL;
109
110 for (i_arg = 1; i_arg < argc; i_arg++) {
111 if (scanned[i_arg].arg_type == OPTION) {
112 delete_chars(scanned[i_arg].list[0], "_");
113 /* process options here */
114 switch (match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) {
115 case SET_MAJOR_ORDER:
116 majorOrderFlag = 0;
117 scanned[i_arg].n_items--;
118 if (scanned[i_arg].n_items > 0 && (!scanItemList(&majorOrderFlag, scanned[i_arg].list + 1, &scanned[i_arg].n_items, 0, "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER, "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)))
119 SDDS_Bomb("invalid -majorOrder syntax/values");
120 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
121 columnMajorOrder = 1;
122 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
123 columnMajorOrder = 0;
124 break;
125 case SET_SIGNAL_NAME:
126 if (scanned[i_arg].n_items != 2)
127 SDDS_Bomb("invalid -signalname syntax");
128 signal_name = scanned[i_arg].list[1];
129 break;
130 case SET_DESCRIPTION:
131 if (scanned[i_arg].n_items != 3)
132 SDDS_Bomb("invalid -description syntax");
133 descrip_text = scanned[i_arg].list[1];
134 descrip_contents = scanned[i_arg].list[2];
135 break;
136 case SET_MPL_LABELS:
137 if (scanned[i_arg].n_items != 3)
138 SDDS_Bomb("invalid -mpllabels syntax");
139 mpl_title = scanned[i_arg].list[1];
140 mpl_topline = scanned[i_arg].list[2];
141 break;
142 default:
143 SDDS_Bomb("invalid option seen");
144 break;
145 }
146 } else {
147 if (!input)
148 input = scanned[i_arg].list[0];
149 else if (!output)
150 output = scanned[i_arg].list[0];
151 else
152 SDDS_Bomb("too many filenames");
153 }
154 }
155 if (!input)
156 SDDS_Bomb("input file not seen");
157 if (!output)
158 SDDS_Bomb("output file not seen");
159 if (!signal_name)
160 SDDS_Bomb("-signal_name not seen");
161
162 fpi = fopen_e(input, "r", 0);
163 parameter_name = buffer;
164 while (fgets(buffer, BUFSIZE, fpi)) {
165 if (!(ptr = strchr(buffer, ':')))
166 SDDS_Bomb("error parsing input file--missing colon on parameter tag");
167 *ptr++ = 0;
168 if (strcmp(HP_DataMarker, parameter_name) == 0)
169 break;
170 index = 0;
171 while (HP_parameter[index].HP_name) {
172 if (strcmp(HP_parameter[index].HP_name, parameter_name) == 0)
173 break;
174 index++;
175 }
176 if (!HP_parameter[index].HP_name) {
177 fprintf(stderr, "error: parameter name %s is not recognized\n", parameter_name);
178 exit(1);
179 }
180 if (HP_parameter[index].value_string) {
181 fprintf(stderr, "error: duplicate entries for parameter %s\n", parameter_name);
182 exit(1);
183 }
185 cp_str(&HP_parameter[index].value_string, ptr);
186 }
187
188 if (!SDDS_InitializeOutput(&SDDS_table, SDDS_BINARY, 0, descrip_text, descrip_contents, output))
189 SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors | SDDS_VERBOSE_PrintErrors);
190 if (columnMajorOrder != -1)
191 SDDS_table.layout.data_mode.column_major = columnMajorOrder;
192
193 index = 0;
194 while (HP_parameter[index].HP_name) {
195 if (!HP_parameter[index].value_string) {
196 index++;
197 continue;
198 }
199 if (strcmp(HP_parameter[index].HP_name, HP_XIncrementName) == 0) {
200 if (sscanf(HP_parameter[index].value_string, "%lf", &xIncrement) != 1)
201 SDDS_Bomb("unable to scan value for x increment");
202 } else if (strcmp(HP_parameter[index].HP_name, HP_XOriginName) == 0) {
203 if (sscanf(HP_parameter[index].value_string, "%lf", &xOrigin) != 1)
204 SDDS_Bomb("unable to scan value for x origin");
205 } else if (strcmp(HP_parameter[index].HP_name, HP_XReferenceName) == 0) {
206 if (sscanf(HP_parameter[index].value_string, "%lf", &xReference) != 1)
207 SDDS_Bomb("unable to scan value for x reference");
208 } else if (strcmp(HP_parameter[index].HP_name, HP_XUnitsName) == 0) {
209 xUnits = HP_parameter[index].value_string;
210 } else if (strcmp(HP_parameter[index].HP_name, HP_YUnitsName) == 0) {
211 yUnits = HP_parameter[index].value_string;
212 } else if (strcmp(HP_parameter[index].HP_name, HP_PointsName) == 0) {
213 if (sscanf(HP_parameter[index].value_string, "%ld", &points) != 1)
214 SDDS_Bomb("unable to scan value for number of points");
215 }
216 if (SDDS_DefineParameter(&SDDS_table, HP_parameter[index].SDDS_name, NULL, NULL, HP_parameter[index].HP_name, NULL, HP_parameter[index].type, HP_parameter[index].value_string) < 0)
217 SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors | SDDS_VERBOSE_PrintErrors);
218 index++;
219 }
220
221 if (mpl_title && (SDDS_DefineParameter(&SDDS_table, "mplTitle", NULL, NULL, NULL, NULL, SDDS_STRING, mpl_title) < 0 || SDDS_DefineParameter(&SDDS_table, "mplTopline", NULL, NULL, NULL, NULL, SDDS_STRING, mpl_topline) < 0))
222 SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors | SDDS_VERBOSE_PrintErrors);
223
224 if (SDDS_DefineColumn(&SDDS_table, "t", NULL, xUnits, NULL, NULL, SDDS_DOUBLE, 0) < 0 || SDDS_DefineColumn(&SDDS_table, signal_name, NULL, yUnits, NULL, NULL, SDDS_DOUBLE, 0) < 0 || !SDDS_WriteLayout(&SDDS_table) || !SDDS_StartTable(&SDDS_table, points))
225 SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors | SDDS_VERBOSE_PrintErrors);
226 fflush(SDDS_table.layout.fp);
227
228 data = tmalloc(sizeof(*data) * points);
229 time = tmalloc(sizeof(*time) * points);
230 for (i = 0; i < points; i++) {
231 if (!fgets(buffer, BUFSIZE, fpi))
232 SDDS_Bomb("insufficient data in input file");
233 time[i] = xOrigin + (i - xReference) * xIncrement;
234 if (sscanf(buffer, "%lf", data + i) != 1)
235 SDDS_Bomb("invalid data in input file");
236 }
237
238 if (!SDDS_SetColumn(&SDDS_table, SDDS_SET_BY_NAME, time, points, "t") || !SDDS_SetColumn(&SDDS_table, SDDS_SET_BY_NAME, data, points, signal_name) || !SDDS_WriteTable(&SDDS_table) || !SDDS_Terminate(&SDDS_table))
239 SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors | SDDS_VERBOSE_PrintErrors);
240 return (0);
241}
242
243char *process_column_definition(char **argv, long argc) {
244 char buffer[SDDS_MAXLINE], *ptr;
245 long i;
246
247 if (argc < 1)
248 return (NULL);
249 sprintf(buffer, "&column name=%s, ", argv[0]);
250 for (i = 1; i < argc; i++) {
251 if (!strchr(argv[i], '='))
252 return (NULL);
253 strcat(buffer, argv[i]);
254 strcat(buffer, ", ");
255 }
256 if (!strstr(buffer, "type="))
257 strcat(buffer, "type=character ");
258 strcat(buffer, "&end");
259 cp_str(&ptr, buffer);
260 return (ptr);
261}
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_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_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.
Definition SDDS_utils.c:432
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
Definition SDDS_utils.c:342
void SDDS_RemovePadding(char *s)
Removes leading and trailing whitespace from a string.
#define SDDS_STRING
Identifier for the string data type.
Definition SDDStypes.h:85
#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
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:59
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26
char * cp_str(char **s, char *t)
Copies a string, allocating memory for storage.
Definition cp_str.c:28
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.
Definition fopen_e.c:30
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 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.