SDDSlib
Loading...
Searching...
No Matches
lba2sdds.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: lba2sdds.c
11 * purpose: converts a LBA file from the spiricon
12 * laser-beam analyser to SDDS format
13 *
14 * M. Borland, 1994
15 $Log: not supported by cvs2svn $
16 Revision 1.5 2001/01/23 19:14:56 soliday
17 Standardized usage message.
18
19 Revision 1.4 1999/05/25 19:03:14 soliday
20 Removed compiler warning on linux.
21
22 Revision 1.3 1995/11/13 15:54:23 borland
23 Removed non-ANSI use of char * return value from sprintf().
24
25 * Revision 1.2 1995/09/06 14:55:51 saunders
26 * First test release of SDDS1.5
27 *
28 */
29#include "mdb.h"
30#include "SDDS.h"
31#include "scan.h"
32#include "match_string.h"
33
34#define SET_DEFINITION 0
35#define SET_PIPE 1
36#define SET_MAJOR_ORDER 2
37#define N_OPTIONS 3
38
39char *option[N_OPTIONS] = {
40 "definition",
41 "pipe",
42 "majorOrder",
43};
44
45char *USAGE = "lba2sdds [<inputfile>] [<outputfile>]\n\
46 [-pipe[=input][,output]]\n\
47 -definition=<name>,<definition-entries> [-majorOrder=row|column] \n\n\
48lba2sdds converts a Spiricon LBA file to SDDS format. The definition entries\
49are of the form <keyword>=<value>, where the keyword is any valid field name for\
50a SDDS column.\n\n\
51Program by Michael Borland (" __DATE__ " " __TIME__ ", SVN revision: " SVN_VERSION ")\n";
52
53char *process_column_definition(char **argv, long argc);
54
55int main(int argc, char **argv) {
56 SDDS_DATASET SDDS_dataset;
57 SCANNED_ARG *scanned;
58 long i_arg;
59 char *input, *output, *definition;
60 long hsize, vsize;
61 char *data, *data_name;
62 char header[200];
63 char ts1[100], ts2[100];
64 FILE *fpi;
65 unsigned long pipeFlags, majorOrderFlag;
66 short columnMajorOrder = 0;
67
69
70 argc = scanargs(&scanned, argc, argv);
71 if (argc < 4)
72 bomb(NULL, USAGE);
73
74 input = output = data_name = NULL;
75 definition = NULL;
76 pipeFlags = 0;
77 hsize = vsize = 0;
78 for (i_arg = 1; i_arg < argc; i_arg++) {
79 if (scanned[i_arg].arg_type == OPTION) {
80 /* process options here */
81 switch (match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) {
82 case SET_MAJOR_ORDER:
83 majorOrderFlag = 0;
84 scanned[i_arg].n_items--;
85 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)))
86 SDDS_Bomb("invalid -majorOrder syntax/values");
87 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
88 columnMajorOrder = 1;
89 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
90 columnMajorOrder = 0;
91 break;
92 case SET_DEFINITION:
93 data_name = scanned[i_arg].list[1];
94 definition = process_column_definition(scanned[i_arg].list + 1, scanned[i_arg].n_items - 1);
95 if (!strstr(definition, "type=character"))
96 SDDS_Bomb("data type must be character for now");
97 break;
98 case SET_PIPE:
99 if (!processPipeOption(scanned[i_arg].list + 1, scanned[i_arg].n_items - 1, &pipeFlags))
100 SDDS_Bomb("invalid -pipe syntax");
101 break;
102 default:
103 bomb("invalid option seen", USAGE);
104 break;
105 }
106 } else {
107 if (!input)
108 input = scanned[i_arg].list[0];
109 else if (!output)
110 output = scanned[i_arg].list[0];
111 else
112 bomb("too many filenames", USAGE);
113 }
114 }
115
116 processFilenames("lba2sdds", &input, &output, pipeFlags, 0, NULL);
117
118 if (!definition)
119 SDDS_Bomb("definition not seen");
120
121 if (input)
122 fpi = fopen_e(input, "r", 0);
123 else
124 fpi = stdin;
125
126 if (fread(header, sizeof(*header), 200, fpi) != 200)
127 SDDS_Bomb("unable to read LBA file header");
128 switch (header[0]) {
129 case 'A':
130 hsize = vsize = 120;
131 break;
132 case 'B':
133 vsize = 256;
134 hsize = 240;
135 break;
136 case 'C':
137 vsize = 512;
138 hsize = 480;
139 break;
140 default:
141 SDDS_Bomb("data does not appear to be in LBA format--invalid frame type");
142 break;
143 }
144
145 sprintf(ts1, "%ld", hsize);
146 sprintf(ts2, "%ld", vsize);
147 if (!SDDS_InitializeOutput(&SDDS_dataset, SDDS_BINARY, 0,
148 "screen image from LBA file", "screen image",
149 output) ||
150 SDDS_ProcessColumnString(&SDDS_dataset, definition, 0) < 0 || SDDS_DefineParameter(&SDDS_dataset, "NumberOfRows", NULL, NULL, "number of rows", NULL, SDDS_LONG, ts1) < 0 || SDDS_DefineParameter(&SDDS_dataset, "NumberOfColumns", NULL, NULL, "number of columns", NULL, SDDS_LONG, ts2) < 0)
151 SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors | SDDS_VERBOSE_PrintErrors);
152 SDDS_dataset.layout.data_mode.column_major = columnMajorOrder;
153 if (!SDDS_WriteLayout(&SDDS_dataset))
154 SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors | SDDS_VERBOSE_PrintErrors);
155
156 data = tmalloc(sizeof(*data) * hsize * vsize);
157 do {
158 if (fread(data, sizeof(*data), hsize * vsize, fpi) != hsize * vsize)
159 SDDS_Bomb("unable to read (all) data from input file");
160 if (!SDDS_StartPage(&SDDS_dataset, hsize * vsize) || !SDDS_SetColumn(&SDDS_dataset, SDDS_SET_BY_NAME, data, hsize * vsize, data_name) || !SDDS_WritePage(&SDDS_dataset))
161 SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors | SDDS_VERBOSE_PrintErrors);
162 } while (fread(header, sizeof(*header), 200, fpi) == 200);
163 fclose(fpi);
164 if (!SDDS_Terminate(&SDDS_dataset))
165 SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors | SDDS_VERBOSE_PrintErrors);
166 return (0);
167}
168
169char *process_column_definition(char **argv, long argc) {
170 char buffer[SDDS_MAXLINE], *ptr;
171 long i;
172
173 if (argc < 1)
174 return (NULL);
175 sprintf(buffer, "&column name=%s, ", argv[0]);
176 for (i = 1; i < argc; i++) {
177 if (!strchr(argv[i], '='))
178 return (NULL);
179 strcat(buffer, argv[i]);
180 strcat(buffer, ", ");
181 }
182 if (!strstr(buffer, "type="))
183 strcat(buffer, "type=character ");
184 strcat(buffer, "&end");
185 cp_str(&ptr, buffer);
186 return (ptr);
187}
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
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_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_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_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
Definition SDDS_utils.c:288
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 processPipeOption(char **item, long items, unsigned long *flags)
Definition scanargs.c:356
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
Definition scanargs.c:390
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.