29#include "match_string.h"
31#define SET_SIGNAL_NAME 0
32#define SET_DESCRIPTION 1
33#define SET_MPL_LABELS 2
34#define SET_MAJOR_ORDER 3
37static char *option[N_OPTIONS] = {
38 "signalname",
"description",
"mpllabels",
"majorOrder"};
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";
48 char *HP_name, *SDDS_name, *value_string;
52#define HP_PARAMETERS 23
74 {
"Max Bandwidth",
"MaxBandwidth", NULL,
SDDS_DOUBLE},
75 {
"Min Bandwidth",
"MinBandwidth", NULL,
SDDS_DOUBLE},
76 {NULL, NULL, NULL, 0},
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";
89int main(
int argc,
char **argv) {
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;
97 double xIncrement, xOrigin, xReference;
98 char *xUnits, *yUnits;
100 unsigned long majorOrderFlag;
101 short columnMajorOrder = -1;
103 argc =
scanargs(&scanned, argc, argv);
107 input = output = signal_name = xUnits = yUnits = NULL;
108 mpl_title = mpl_topline = descrip_text = descrip_contents = NULL;
110 for (i_arg = 1; i_arg < argc; i_arg++) {
111 if (scanned[i_arg].arg_type == OPTION) {
114 switch (
match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) {
115 case SET_MAJOR_ORDER:
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;
125 case SET_SIGNAL_NAME:
126 if (scanned[i_arg].n_items != 2)
128 signal_name = scanned[i_arg].list[1];
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];
137 if (scanned[i_arg].n_items != 3)
139 mpl_title = scanned[i_arg].list[1];
140 mpl_topline = scanned[i_arg].list[2];
148 input = scanned[i_arg].list[0];
150 output = scanned[i_arg].list[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");
168 if (strcmp(HP_DataMarker, parameter_name) == 0)
171 while (HP_parameter[index].HP_name) {
172 if (strcmp(HP_parameter[index].HP_name, parameter_name) == 0)
176 if (!HP_parameter[index].HP_name) {
177 fprintf(stderr,
"error: parameter name %s is not recognized\n", parameter_name);
180 if (HP_parameter[index].value_string) {
181 fprintf(stderr,
"error: duplicate entries for parameter %s\n", parameter_name);
185 cp_str(&HP_parameter[index].value_string, ptr);
190 if (columnMajorOrder != -1)
191 SDDS_table.layout.data_mode.column_major = columnMajorOrder;
194 while (HP_parameter[index].HP_name) {
195 if (!HP_parameter[index].value_string) {
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");
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)
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))
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))
226 fflush(SDDS_table.layout.fp);
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)
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))
243char *process_column_definition(
char **argv,
long argc) {
244 char buffer[SDDS_MAXLINE], *ptr;
249 sprintf(buffer,
"&column name=%s, ", argv[0]);
250 for (i = 1; i < argc; i++) {
251 if (!strchr(argv[i],
'='))
253 strcat(buffer, argv[i]);
254 strcat(buffer,
", ");
256 if (!strstr(buffer,
"type="))
257 strcat(buffer,
"type=character ");
258 strcat(buffer,
"&end");
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.
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 * cp_str(char **s, char *t)
Copies a string, allocating memory for storage.
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.