47 SET_ROOTNAME_COLUMN_NAME,
53char *option[N_OPTIONS] =
63 "Usage: sddsmatrix2column [<inputfile>] [<outputfile>]\n"
64 " [-pipe=<input>,<output>]\n"
65 " [-rowNameColumn=<string>]\n"
66 " [-dataColumnName=<string>]\n"
67 " [-rootnameColumnName=<string>]\n"
68 " [-majorOrder=row|column]\n\n"
71 " - Contains an optional string column and multiple numerical columns.\n"
72 " - If the string column or -rowNameColumn is not provided,\n"
73 " rows will be named as Row<row_index> in the output.\n\n"
75 " - Contains two columns: a string column and a data column.\n"
76 " - The string column combines the input string column (or Row<row_index>)\n"
77 " with the input data column names.\n\n"
78 " -pipe=<input>,<output>\n"
79 " - Specifies pipe flags for input and/or output.\n\n"
80 " -rowNameColumn=<string>\n"
81 " - Specifies the column name for row names in the input file.\n"
82 " - If not provided, rows will be named as Row<row_index>.\n\n"
83 " -dataColumnName=<string>\n"
84 " - Specifies the column name for data in the output file.\n"
85 " - If not provided, \"Rootname\" will be used.\n\n"
86 " -rootnameColumnName=<string>\n"
87 " - Specifies the column name for the string column in the output file.\n\n"
88 " -majorOrder=row|column\n"
89 " - Determines the order to transfer the matrix into one column.\n"
90 " Choose 'row' for row-major order or 'column' for column-major order.\n\n"
92 " sddsmatrix2column converts a matrix into a single column format.\n\n"
93 "Link date: " __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
"\n\n";
95int main(
int argc,
char **argv) {
98 long i_arg, tmpfile_used = 0, j, column_major;
100 int32_t page, numCols, columns, columnType;
101 int64_t i, rows, outputRows, outputRow;
102 char *inputfile, *outputfile;
103 char *rowColName, *dataColName, *rootnameColName, **rowName, **columnName;
105 unsigned long pipeFlags, majorOrderFlag;
107 inputfile = outputfile = rowColName = dataColName = rootnameColName = NULL;
109 rowName = columnName = NULL;
114 argc =
scanargs(&s_arg, argc, argv);
116 fprintf(stderr,
"%s\n", USAGE);
120 for (i_arg = 1; i_arg < argc; i_arg++) {
121 if (s_arg[i_arg].arg_type == OPTION) {
122 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
123 case SET_MAJOR_ORDER:
124 s_arg[i_arg].n_items -= 1;
125 if (s_arg[i_arg].n_items > 0 &&
126 (!
scanItemList(&majorOrderFlag, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
127 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
128 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)))
129 SDDS_Bomb(
"invalid -majorOrder syntax/values");
130 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
132 if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
135 case SET_ROW_COLUMN_NAME:
136 if (s_arg[i_arg].n_items != 2) {
137 fprintf(stderr,
"Error (%s): invalid -rowNameColumn syntax\n", argv[0]);
140 rowColName = s_arg[i_arg].list[1];
142 case SET_DATA_COLUMN_NAME:
143 if (s_arg[i_arg].n_items != 2) {
144 fprintf(stderr,
"Error (%s): invalid -dataColumnName syntax\n", argv[0]);
147 dataColName = s_arg[i_arg].list[1];
149 case SET_ROOTNAME_COLUMN_NAME:
150 if (s_arg[i_arg].n_items != 2) {
151 fprintf(stderr,
"Error (%s): invalid -rootnameColumnName syntax\n", argv[0]);
154 rootnameColName = s_arg[i_arg].list[1];
157 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags)) {
158 fprintf(stderr,
"Error (%s): invalid -pipe syntax\n", argv[0]);
163 fprintf(stderr,
"Error: unknown option -- %s provided.\n", s_arg[i_arg].list[0]);
167 if (inputfile == NULL)
168 inputfile = s_arg[i_arg].list[0];
169 else if (outputfile == NULL)
170 outputfile = s_arg[i_arg].list[0];
172 fprintf(stderr,
"Error (%s): too many filenames\n", argv[0]);
178 if ((!pipeFlags && !outputfile)) {
179 fprintf(stderr,
"Error: output file not provided.\n");
182 processFilenames(
"sddsmatrix2column", &inputfile, &outputfile, pipeFlags, 0, &tmpfile_used);
189 if (!
SDDS_InitializeOutput(&SDDS_dataset, SDDS_orig.layout.data_mode.mode, 1, NULL, NULL, outputfile) ||
202 for (i = 0; i < columns; i++) {
210 fprintf(stderr,
"Error: problem counting rows in input page\n");
214 outputRows = rows * numCols;
216 fprintf(stderr,
"Error: problem starting output page\n");
225 fprintf(stderr,
"Error %s column does not exist or not string type in input file %s\n", rowColName, inputfile);
231 for (i = 0; i < rows; i++) {
232 for (j = 0; j < columns; j++) {
235 snprintf(tmpName,
sizeof(tmpName),
"%s%s", rowName[i], SDDS_orig.layout.column_definition[j].name);
237 snprintf(tmpName,
sizeof(tmpName),
"Row%" PRId64
"%s", i, SDDS_orig.layout.column_definition[j].name);
239 switch (columnType) {
241 data = ((
long double *)SDDS_orig.data[j])[i];
244 data = ((
double *)SDDS_orig.data[j])[i];
247 data = ((
float *)SDDS_orig.data[j])[i];
250 data = ((int64_t *)SDDS_orig.data[j])[i];
253 data = ((uint64_t *)SDDS_orig.data[j])[i];
256 data = ((int32_t *)SDDS_orig.data[j])[i];
259 data = ((uint32_t *)SDDS_orig.data[j])[i];
262 data = ((
short *)SDDS_orig.data[j])[i];
265 data = ((
unsigned short *)SDDS_orig.data[j])[i];
271 if (!
SDDS_SetRowValues(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, outputRow, 0, tmpName, 1, data, -1)) {
280 for (j = 0; j < columns; j++) {
283 for (i = 0; i < rows; i++) {
285 snprintf(tmpName,
sizeof(tmpName),
"%s%s", SDDS_orig.layout.column_definition[j].name, rowName[i]);
287 snprintf(tmpName,
sizeof(tmpName),
"%sRow%" PRId64, SDDS_orig.layout.column_definition[j].name, i);
289 switch (columnType) {
291 data = ((
long double *)SDDS_orig.data[j])[i];
294 data = ((
double *)SDDS_orig.data[j])[i];
297 data = ((
float *)SDDS_orig.data[j])[i];
300 data = ((int64_t *)SDDS_orig.data[j])[i];
303 data = ((uint64_t *)SDDS_orig.data[j])[i];
306 data = ((int32_t *)SDDS_orig.data[j])[i];
309 data = ((uint32_t *)SDDS_orig.data[j])[i];
312 data = ((
short *)SDDS_orig.data[j])[i];
315 data = ((
unsigned short *)SDDS_orig.data[j])[i];
321 if (!
SDDS_SetRowValues(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, outputRow, 0, tmpName, 1, data, -1)) {
330 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_NAME | SDDS_PASS_BY_VALUE,
"InputFile", inputfile ? inputfile :
"pipe", NULL) ||
332 fprintf(stderr,
"Error: problem writing page to file %s\n", outputfile);
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_SetRowValues(SDDS_DATASET *SDDS_dataset, int32_t mode, int64_t row,...)
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
int32_t SDDS_SetParameters(SDDS_DATASET *SDDS_dataset, int32_t mode,...)
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_DefineSimpleParameter(SDDS_DATASET *SDDS_dataset, const char *name, const char *unit, int32_t type)
Defines a simple data parameter within the SDDS dataset.
int32_t SDDS_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
int32_t SDDS_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
int32_t SDDS_FreeStringArray(char **string, int64_t strings)
Frees an array of strings by deallocating each individual string.
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.
char ** SDDS_GetColumnNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all columns in the SDDS dataset.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
int32_t SDDS_GetColumnType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of a column in the SDDS dataset by its index.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
#define SDDS_ULONG
Identifier for the unsigned 32-bit integer data type.
#define SDDS_FLOAT
Identifier for the float data type.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_ULONG64
Identifier for the unsigned 64-bit integer data type.
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
#define SDDS_SHORT
Identifier for the signed short integer data type.
#define SDDS_USHORT
Identifier for the unsigned short integer data type.
#define SDDS_DOUBLE
Identifier for the double data type.
#define SDDS_NUMERIC_TYPE(type)
Checks if the given type identifier corresponds to any numeric type.
#define SDDS_LONGDOUBLE
Identifier for the long double data type.
#define SDDS_LONG64
Identifier for the signed 64-bit integer data type.
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.
long replaceFileAndBackUp(char *file, char *replacement)
Replaces a file with a replacement file and creates a backup of the original.
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
long processPipeOption(char **item, long items, unsigned long *flags)
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
void free_scanargs(SCANNED_ARG **scanned, int argc)
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.