SDDSlib
Loading...
Searching...
No Matches
raw2sdds.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: raw2sdds
11 * purpose: converts a raw data stream of binary data
12 * to SDDS format
13 * M. Borland, 1994
14 $Log: not supported by cvs2svn $
15 Revision 1.4 2001/01/23 19:14:57 soliday
16 Standardized usage message.
17
18 Revision 1.3 1999/05/25 19:04:18 soliday
19 Removed compiler warning on linux.
20
21 Revision 1.2 1995/09/06 14:55:56 saunders
22 First test release of SDDS1.5
23
24*/
25#include "mdb.h"
26#include "SDDS.h"
27#include "scan.h"
28#include "match_string.h"
29
30#define DEFAULT_HSIZE 484
31#define DEFAULT_VSIZE 512
32
33#define SET_DEFINITION 0
34#define SET_SIZE 1
35#define SET_MAJOR_ORDER 2
36#define N_OPTIONS 3
37
38char *option[N_OPTIONS] = {
39 "definition",
40 "size",
41 "majorOrder",
42};
43
44char *USAGE = "raw2sdds <inputfile> <outputfile>\n\
45 -definition=<name>,<definition-entries>\n\
46 [-size=<horiz-pixels>,<vert-pixels>] [-majorOrder=row|column]\n\n\
47raw2sdds converts a binary data stream to SDDS format. The definition entries\
48are of the form <keyword>=<value>, where the keyword is any valid field name for\
49a SDDS column.\n\n\
50Program by Michael Borland (" __DATE__ " " __TIME__ ", SVN revision: " SVN_VERSION ")\n";
51
52char *process_column_definition(char **argv, long argc);
53
54int main(int argc, char **argv) {
55 SDDS_TABLE SDDS_table;
56 SCANNED_ARG *scanned;
57 unsigned long majorOrderFlag;
58 long i_arg;
59 char *input, *output, *definition;
60 long hsize, vsize;
61 char *data, *data_name;
62 char ts1[100], ts2[100];
63 FILE *fpi;
64 short columnMajorOrder = 0;
65
66 argc = scanargs(&scanned, argc, argv);
67 if (argc < 4)
68 bomb(NULL, USAGE);
69
70 input = output = data_name = NULL;
71 hsize = DEFAULT_HSIZE;
72 vsize = DEFAULT_VSIZE;
73 definition = NULL;
74
75 for (i_arg = 1; i_arg < argc; i_arg++) {
76 if (scanned[i_arg].arg_type == OPTION) {
77 /* process options here */
78 switch (match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) {
79 case SET_MAJOR_ORDER:
80 majorOrderFlag = 0;
81 scanned[i_arg].n_items--;
82 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)))
83 SDDS_Bomb("invalid -majorOrder syntax/values");
84 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
85 columnMajorOrder = 1;
86 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
87 columnMajorOrder = 0;
88 break;
89 case SET_DEFINITION:
90 data_name = scanned[i_arg].list[1];
91 definition = process_column_definition(scanned[i_arg].list + 1, scanned[i_arg].n_items - 1);
92 if (!strstr(definition, "type=character"))
93 SDDS_Bomb("data type must be character for now");
94 break;
95 case SET_SIZE:
96 if (scanned[i_arg].n_items != 3 || sscanf(scanned[i_arg].list[1], "%ld", &hsize) != 1 || hsize <= 0 || sscanf(scanned[i_arg].list[2], "%ld", &vsize) != 1 || vsize <= 0)
97 bomb("invalid -size syntax", USAGE);
98 break;
99 default:
100 bomb("invalid option seen", USAGE);
101 break;
102 }
103 } else {
104 if (!input)
105 input = scanned[i_arg].list[0];
106 else if (!output)
107 output = scanned[i_arg].list[0];
108 else
109 bomb("too many filenames", USAGE);
110 }
111 }
112 if (!input)
113 SDDS_Bomb("input file not seen");
114 if (!output)
115 SDDS_Bomb("output file not seen");
116 if (!definition)
117 SDDS_Bomb("definition not seen");
118
119 sprintf(ts1, "%ld", hsize);
120 sprintf(ts2, "%ld", vsize);
121 if (!SDDS_InitializeOutput(&SDDS_table, SDDS_BINARY, 0,
122 "screen image from raw file", "screen image",
123 output) ||
124 SDDS_ProcessColumnString(&SDDS_table, definition, 0) < 0 ||
125 SDDS_DefineParameter(&SDDS_table, "NumberOfRows", NULL, NULL, "number of rows", NULL, SDDS_LONG, ts1) < 0 ||
126 SDDS_DefineParameter(&SDDS_table, "NumberOfColumns", NULL, NULL, "number of columns", NULL, SDDS_LONG, ts2) < 0)
127 SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors | SDDS_VERBOSE_PrintErrors);
128 SDDS_table.layout.data_mode.column_major = columnMajorOrder;
129 if (!SDDS_WriteLayout(&SDDS_table) || !SDDS_StartTable(&SDDS_table, hsize * vsize))
130 SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors | SDDS_VERBOSE_PrintErrors);
131
132 data = tmalloc(sizeof(*data) * hsize * vsize);
133 fpi = fopen_e(input, "r", 0);
134 if (fread(data, sizeof(*data), hsize * vsize, fpi) != hsize * vsize)
135 SDDS_Bomb("unable to read (all) data from input file");
136 fclose(fpi);
137 if (!SDDS_SetColumn(&SDDS_table, SDDS_SET_BY_NAME, data, hsize * vsize, data_name) ||
138 !SDDS_WriteTable(&SDDS_table) ||
139 !SDDS_Terminate(&SDDS_table))
140 SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors | SDDS_VERBOSE_PrintErrors);
141 return (0);
142}
143
144char *process_column_definition(char **argv, long argc) {
145 char buffer[SDDS_MAXLINE], *ptr;
146 long i;
147
148 if (argc < 1)
149 return (NULL);
150 sprintf(buffer, "&column name=%s, ", argv[0]);
151 for (i = 1; i < argc; i++) {
152 if (!strchr(argv[i], '='))
153 return (NULL);
154 strcat(buffer, argv[i]);
155 strcat(buffer, ", ");
156 }
157 if (!strstr(buffer, "type="))
158 strcat(buffer, "type=character ");
159 strcat(buffer, "&end");
160 cp_str(&ptr, buffer);
161 return (ptr);
162}
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_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.
int32_t SDDS_ProcessColumnString(SDDS_DATASET *SDDS_dataset, char *string, int32_t mode)
Process a column definition string.
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
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
Definition SDDStypes.h:61
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
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.