1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
/* Copyright (c) 1999-2009 Massachusetts Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <unistd.h>
#include "config.h"
#include "arrayh5.h"
#include "copyright.h"
#include "h5utils.h"
#define CHECK(cond, msg) { if (!(cond)) { fprintf(stderr, "h5fromtxt error: %s\n", msg); exit(EXIT_FAILURE); } }
void usage(FILE *f)
{
fprintf(f, "Usage: h5fromitxt [options] <hdf5-file>\n"
"Options:\n"
" -h : this help message\n"
" -V : print version number and copyright\n"
" -v : verbose output\n"
" -a : append to existing hdf5 file\n"
" -f <val> : use <val> as missing filler [ default: 0 ]\n"
" -n <size> : input array dimensions [ default: guessed ]\n"
" -m <size> : input coordinate minimum [ default: guessed ]\n"
" -M <size> : input coordinate maximum [ default: guessed ]\n"
" -T : transpose the data [default: no]\n"
" -d <name> : use dataset <name> in the output file (default: \"data\")\n"
" -- you can also specify a dataset via <filename>:<name>\n"
);
}
#define MAX_RANK 10
int get_size_arg(int size[MAX_RANK], const char *arg)
{
int pos = 0;
int rank = 0;
while (isdigit(arg[pos])) {
CHECK(rank < MAX_RANK,
"Rank too big in -n argument!\n");
size[rank] = 0;
while (isdigit(arg[pos])) {
size[rank] = size[rank]*10 + arg[pos]-'0';
++pos;
}
++rank;
if (arg[pos] == 'x' || arg[pos] == 'X' || arg[pos] == '*')
++pos;
}
CHECK(rank > 0 && !arg[pos], "Invalid <size> argument; should be e.g. 23x34 or 10x10x10\n");
return rank;
}
int main(int argc, char **argv)
{
arrayh5 a;
char *dname, *h5_fname;
char *data_name = NULL;
extern char *optarg;
extern int optind;
int c;
double *data;
int idata = 0;
int rank = -1, dims[MAX_RANK], N = 1, nrows = 0;
int cmin_rank = -1, cmax_rank = -1, coord_min[MAX_RANK], coord_max[MAX_RANK];
double fill_val = 0;
int ncols = -1, cur_ncols = 0;
int read_newline = 0;
int verbose = 0;
int transpose = 0;
int append = 0;
int i, j;
while ((c = getopt(argc, argv, "hn:d:vTaVf:m:M:")) != -1)
switch (c) {
case 'h':
usage(stdout);
return EXIT_SUCCESS;
case 'V':
printf("h5fromtxt " PACKAGE_VERSION " by Steven G. Johnson\n"
COPYRIGHT);
return EXIT_SUCCESS;
case 'v':
verbose = 1;
break;
case 'a':
append = 1;
break;
case 'T':
transpose = 1;
break;
case 'f':
fill_val = atof(optarg);
break;
case 'd':
free(data_name);
data_name = my_strdup(optarg);
break;
case 'n':
rank = get_size_arg(dims, optarg);
for (i = 0, N = 1; i < rank; ++i)
N *= dims[i];
break;
case 'm':
cmin_rank = get_size_arg(coord_min, optarg);
break;
case 'M':
cmax_rank = get_size_arg(coord_max, optarg);
break;
default:
fprintf(stderr, "Invalid argument -%c\n", c);
usage(stderr);
return EXIT_FAILURE;
}
if (optind + 1 != argc) { /* should be exactly 1 parameter left */
usage(stderr);
return EXIT_FAILURE;
}
h5_fname = split_fname(argv[optind], &dname);
if (!dname[0])
dname = data_name;
if (!dname)
dname = my_strdup("data");
data = (double *) malloc(sizeof(double) * N);
CHECK(data, "out of memory");
while (!feof(stdin)) {
read_newline = 0;
/* eat leading spaces */
while (isspace(c = getc(stdin)));
ungetc(c, stdin);
if (c == EOF)
break;
/* increase the size of the data array, if necessary */
if (idata >= N) {
CHECK(rank < 0, "more inputs in file than specified by -n");
N *= 2;
data = (double *) realloc(data, sizeof(double) * N);
CHECK(data, "out of memory");
}
CHECK(scanf("%lg", &data[idata++]) == 1,
"error reading numeric input");
++cur_ncols;
/* eat characters until the next number: */
do {
c = getc(stdin);
if (c == '\n')
read_newline = 1;
} while (!(isdigit(c) || c == '.' || c == '-' || c == '+'
|| c == EOF));
ungetc(c, stdin);
if (read_newline) {
++nrows;
CHECK(ncols < 0 || cur_ncols == ncols,
"the number of input columns is not constant.");
ncols = cur_ncols;
cur_ncols = 0;
}
}
if (!read_newline) { /* don't require a newline on the last line */
++nrows;
CHECK(ncols < 0 || cur_ncols == ncols,
"the number of input columns is not constant.");
}
CHECK(idata > 0, "no inputs read");
CHECK(ncols > 1, "need at least one coordinate column");
if (verbose)
printf("Read %d numbers in %d rows with rank-%d coordinates.\n",
idata, nrows, ncols - 1);
CHECK(ncols - 1 <= MAX_RANK, "rank is too large");
if (cmin_rank < 0) {
cmin_rank = ncols - 1;
CHECK(cmin_rank == ncols - 1, "coordinate minima have wrong rank");
for (j = 0; j < ncols - 1; ++j) {
coord_min[j] = floor(data[j]);
}
for (i = 1; i < nrows; ++i)
for (j = 0; j < ncols - 1; ++j) {
int ij = i * ncols + j;
if (data[ij] < coord_min[j])
coord_min[j] = floor(data[ij]);
}
}
if (cmax_rank < 0) {
cmax_rank = ncols - 1;
CHECK(cmax_rank == ncols - 1, "coordinate maxima have wrong rank");
for (j = 0; j < ncols - 1; ++j) {
coord_max[j] = ceil(data[j]);
}
for (i = 1; i < nrows; ++i)
for (j = 0; j < ncols - 1; ++j) {
int ij = i * ncols + j;
if (data[ij] > coord_max[j])
coord_max[j] = ceil(data[ij]);
}
}
if (verbose) {
printf("Coordinates range from (%d", coord_min[0]);
for (j = 1; j < ncols - 1; ++j)
printf(",%d", coord_min[j]);
printf(") to (%d", coord_max[0]);
for (j = 1; j < ncols - 1; ++j)
printf(",%d", coord_max[j]);
printf(")\n");
}
if (rank < 0) {
rank = ncols - 1;
for (i = 0; i < rank; ++i)
dims[i] = coord_max[i] - coord_min[i] + 1;
}
CHECK(rank == ncols - 1, "number of coordinates does not match rank");
a = arrayh5_create(rank, dims);
for (i = 0; i < a.N; ++i)
a.data[i] = fill_val;
for (i = 0; i < nrows; ++i) {
int idx = 0;
for (j = 0; j < rank; ++j) {
int id = data[i * ncols + j] + 0.5;
if (id < coord_min[j] || id > coord_max[j] ||
id - coord_min[j] >= dims[j]) {
idx = -1;
break;
}
idx = idx * dims[j] + id - coord_min[j];
}
if (idx >= 0 && idx < a.N)
a.data[idx] = data[i * ncols + ncols - 1];
}
if (transpose)
arrayh5_transpose(&a);
if (verbose) {
double a_min, a_max;
arrayh5_getrange(a, &a_min, &a_max);
printf("data ranges from %g to %g.\n", a_min, a_max);
}
if (verbose) {
printf("Writing size %d", a.dims[0]);
for (i = 1; i < a.rank; ++i)
printf("x%d", a.dims[i]);
printf(" data to %s:%s\n", h5_fname, dname);
}
arrayh5_write(a, h5_fname, dname, append);
arrayh5_destroy(a);
return EXIT_SUCCESS;
}
|