| 1 |
/**************************************************************************** |
| 2 |
* |
| 3 |
* MODULE: r.in.ascii |
| 4 |
* AUTHOR(S): Michael Shapiro, CERL (original contributor) |
| 5 |
* Markus Neteler <neteler itc.it>, |
| 6 |
* Hamish Bowman <hamish_b yahoo.com>, |
| 7 |
* Roberto Flor <flor itc.it>, |
| 8 |
* Roger Miller <rgrmill rt66 com>, |
| 9 |
* Brad Douglas <rez touchofmadness.com>, |
| 10 |
* Huidae Cho <grass4u gmail.com>, |
| 11 |
* Glynn Clements <glynn gclements.plus.com> |
| 12 |
* Jachym Cepicky <jachym les-ejk.cz>, |
| 13 |
* Jan-Oliver Wagner <jan intevation.de>, |
| 14 |
* Justin Hickey <jhickey hpcc.nectec.or.th> |
| 15 |
* PURPOSE: Import ASCII or SURFER files |
| 16 |
* COPYRIGHT: (C) 1999-2006 by the GRASS Development Team |
| 17 |
* |
| 18 |
* This program is free software under the GNU General Public |
| 19 |
* License (>=v2). Read the file COPYING that comes with GRASS |
| 20 |
* for details. |
| 21 |
* |
| 22 |
*****************************************************************************/ |
| 23 |
|
| 24 |
#include <unistd.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include <errno.h> |
| 28 |
#include <grass/gis.h> |
| 29 |
#include <grass/raster.h> |
| 30 |
#include <grass/glocale.h> |
| 31 |
#include "local_proto.h" |
| 32 |
|
| 33 |
FILE *Tmp_fd = NULL; |
| 34 |
char *Tmp_file = NULL; |
| 35 |
|
| 36 |
const float GS_BLANK = 1.70141E+038; |
| 37 |
|
| 38 |
static int file_cpy(FILE *, FILE *); |
| 39 |
|
| 40 |
int main(int argc, char *argv[]) |
| 41 |
{ |
| 42 |
char *input; |
| 43 |
char *output; |
| 44 |
char *title; |
| 45 |
char *temp; |
| 46 |
FILE *fd, *ft; |
| 47 |
int cf, direction, sz; |
| 48 |
struct Cell_head cellhd; |
| 49 |
struct History history; |
| 50 |
void *rast, *rast_ptr; |
| 51 |
int row, col; |
| 52 |
int nrows, ncols; |
| 53 |
double x; |
| 54 |
char y[128]; |
| 55 |
struct GModule *module; |
| 56 |
struct { |
| 57 |
struct Option *input, *output, *title, *mult, *nv, *type; |
| 58 |
} parm; |
| 59 |
struct { |
| 60 |
struct Flag *s; |
| 61 |
} flag; |
| 62 |
char *null_val_str; |
| 63 |
DCELL mult; |
| 64 |
RASTER_MAP_TYPE data_type; |
| 65 |
|
| 66 |
G_gisinit(argv[0]); |
| 67 |
|
| 68 |
module = G_define_module(); |
| 69 |
G_add_keyword(_("raster")); |
| 70 |
G_add_keyword(_("import")); |
| 71 |
G_add_keyword(_("conversion")); |
| 72 |
G_add_keyword("ASCII"); |
| 73 |
module->description = |
| 74 |
_("Converts a GRASS ASCII raster file to binary raster map."); |
| 75 |
|
| 76 |
parm.input = G_define_standard_option(G_OPT_F_INPUT); |
| 77 |
parm.input->label = _("Name of input file to be imported"); |
| 78 |
parm.input->description = _("'-' for standard input"); |
| 79 |
|
| 80 |
parm.output = G_define_standard_option(G_OPT_R_OUTPUT); |
| 81 |
|
| 82 |
parm.type = G_define_standard_option(G_OPT_R_TYPE); |
| 83 |
parm.type->required = NO; |
| 84 |
parm.type->description = |
| 85 |
_("Default: CELL for integer values, DCELL for floating-point values"); |
| 86 |
|
| 87 |
parm.title = G_define_option(); |
| 88 |
parm.title->key = "title"; |
| 89 |
parm.title->key_desc = "phrase"; |
| 90 |
parm.title->type = TYPE_STRING; |
| 91 |
parm.title->required = NO; |
| 92 |
parm.title->description = _("Title for resultant raster map"); |
| 93 |
|
| 94 |
parm.mult = G_define_option(); |
| 95 |
parm.mult->key = "multiplier"; |
| 96 |
parm.mult->type = TYPE_DOUBLE; |
| 97 |
parm.mult->description = _("Default: read from header"); |
| 98 |
parm.mult->required = NO; |
| 99 |
parm.mult->label = _("Multiplier for ASCII data"); |
| 100 |
|
| 101 |
parm.nv = G_define_standard_option(G_OPT_M_NULL_VALUE); |
| 102 |
parm.nv->description = _("Default: read from header"); |
| 103 |
parm.nv->label = _("String representing NULL value data cell"); |
| 104 |
parm.nv->guisection = _("NULL data"); |
| 105 |
|
| 106 |
flag.s = G_define_flag(); |
| 107 |
flag.s->key = 's'; |
| 108 |
flag.s->description = |
| 109 |
_("SURFER (Golden Software) ASCII file will be imported"); |
| 110 |
|
| 111 |
if (G_parser(argc, argv)) |
| 112 |
exit(EXIT_FAILURE); |
| 113 |
|
| 114 |
input = parm.input->answer; |
| 115 |
output = parm.output->answer; |
| 116 |
|
| 117 |
temp = G_tempfile(); |
| 118 |
ft = fopen(temp, "w+"); |
| 119 |
if (ft == NULL) |
| 120 |
G_fatal_error(_("Unable to open temporary file <%s>"), temp); |
| 121 |
|
| 122 |
if ((title = parm.title->answer)) |
| 123 |
G_strip(title); |
| 124 |
|
| 125 |
if (!parm.mult->answer) |
| 126 |
Rast_set_d_null_value(&mult, 1); |
| 127 |
else if ((sscanf(parm.mult->answer, "%lf", &mult)) != 1) |
| 128 |
G_fatal_error(_("Wrong entry for multiplier: %s"), parm.mult->answer); |
| 129 |
|
| 130 |
null_val_str = parm.nv->answer; |
| 131 |
|
| 132 |
data_type = -1; |
| 133 |
if (parm.type->answer) { |
| 134 |
switch (parm.type->answer[0]) { |
| 135 |
case 'C': |
| 136 |
data_type = CELL_TYPE; |
| 137 |
break; |
| 138 |
case 'F': |
| 139 |
data_type = FCELL_TYPE; |
| 140 |
break; |
| 141 |
case 'D': |
| 142 |
data_type = DCELL_TYPE; |
| 143 |
break; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if (strcmp(input, "-") == 0) { |
| 148 |
Tmp_file = G_tempfile(); |
| 149 |
if (NULL == (Tmp_fd = fopen(Tmp_file, "w+"))) |
| 150 |
G_fatal_error(_("Unable to open temporary file <%s>"), Tmp_file); |
| 151 |
unlink(Tmp_file); |
| 152 |
if (0 > file_cpy(stdin, Tmp_fd)) |
| 153 |
G_fatal_error(_("Unable to read input from stdin")); |
| 154 |
fd = Tmp_fd; |
| 155 |
} |
| 156 |
else |
| 157 |
fd = fopen(input, "r"); |
| 158 |
|
| 159 |
if (fd == NULL) { |
| 160 |
G_fatal_error(_("Unable to read input from <%s>"), input); |
| 161 |
} |
| 162 |
|
| 163 |
direction = 1; |
| 164 |
sz = 0; |
| 165 |
if (flag.s->answer) { |
| 166 |
sz = getgrdhead(fd, &cellhd); |
| 167 |
/* for Surfer files, the data type is always FCELL_TYPE, |
| 168 |
the multiplier and the null_val_str are never used */ |
| 169 |
data_type = FCELL_TYPE; |
| 170 |
mult = 1.; |
| 171 |
null_val_str = ""; |
| 172 |
/* rows in surfer files are ordered from bottom to top, |
| 173 |
opposite of normal GRASS ordering */ |
| 174 |
direction = -1; |
| 175 |
} |
| 176 |
else |
| 177 |
sz = gethead(fd, &cellhd, &data_type, &mult, &null_val_str); |
| 178 |
|
| 179 |
if (!sz) |
| 180 |
G_fatal_error(_("Can't get cell header")); |
| 181 |
|
| 182 |
nrows = cellhd.rows; |
| 183 |
ncols = cellhd.cols; |
| 184 |
Rast_set_window(&cellhd); |
| 185 |
|
| 186 |
if (nrows != Rast_window_rows()) |
| 187 |
G_fatal_error(_("OOPS: rows changed from %d to %d"), nrows, |
| 188 |
Rast_window_rows()); |
| 189 |
if (ncols != Rast_window_cols()) |
| 190 |
G_fatal_error(_("OOPS: cols changed from %d to %d"), ncols, |
| 191 |
Rast_window_cols()); |
| 192 |
|
| 193 |
rast_ptr = Rast_allocate_buf(data_type); |
| 194 |
rast = rast_ptr; |
| 195 |
cf = Rast_open_new(output, data_type); |
| 196 |
for (row = 0; row < nrows; row++) { |
| 197 |
G_percent(row, nrows, 2); |
| 198 |
for (col = 0; col < ncols; col++) { |
| 199 |
if (fscanf(fd, "%s", y) != 1) { |
| 200 |
Rast_unopen(cf); |
| 201 |
G_fatal_error(_("Data conversion failed at row %d, col %d"), |
| 202 |
row + 1, col + 1); |
| 203 |
} |
| 204 |
if (strcmp(y, null_val_str)) { |
| 205 |
x = atof(y); |
| 206 |
if ((float)x == GS_BLANK) { |
| 207 |
Rast_set_null_value(rast_ptr, 1, data_type); |
| 208 |
} |
| 209 |
else { |
| 210 |
Rast_set_d_value(rast_ptr, (DCELL)(x * mult), data_type); |
| 211 |
} |
| 212 |
} |
| 213 |
else { |
| 214 |
Rast_set_null_value(rast_ptr, 1, data_type); |
| 215 |
} |
| 216 |
rast_ptr = G_incr_void_ptr(rast_ptr, Rast_cell_size(data_type)); |
| 217 |
} |
| 218 |
fwrite(rast, Rast_cell_size(data_type), ncols, ft); |
| 219 |
rast_ptr = rast; |
| 220 |
} |
| 221 |
G_percent(nrows, nrows, 2); |
| 222 |
G_debug(1, "Creating support files for %s", output); |
| 223 |
|
| 224 |
sz = 0; |
| 225 |
if (direction < 0) { |
| 226 |
sz = -ncols * Rast_cell_size(data_type); |
| 227 |
G_fseek(ft, sz, SEEK_END); |
| 228 |
sz *= 2; |
| 229 |
} |
| 230 |
else { |
| 231 |
G_fseek(ft, 0L, SEEK_SET); |
| 232 |
} |
| 233 |
|
| 234 |
for (row = 0; row < nrows; row += 1) { |
| 235 |
if (fread(rast, Rast_cell_size(data_type), ncols, ft) != (size_t)ncols) |
| 236 |
G_fatal_error(_("Read from file error: %s"), strerror(errno)); |
| 237 |
Rast_put_row(cf, rast, data_type); |
| 238 |
G_fseek(ft, sz, SEEK_CUR); |
| 239 |
} |
| 240 |
fclose(ft); |
| 241 |
unlink(temp); |
| 242 |
|
| 243 |
Rast_close(cf); |
| 244 |
|
| 245 |
if (title) |
| 246 |
Rast_put_cell_title(output, title); |
| 247 |
|
| 248 |
Rast_short_history(output, "raster", &history); |
| 249 |
Rast_command_history(&history); |
| 250 |
Rast_write_history(output, &history); |
| 251 |
|
| 252 |
G_done_msg(" "); |
| 253 |
|
| 254 |
exit(EXIT_SUCCESS); |
| 255 |
} |
| 256 |
|
| 257 |
static int file_cpy(FILE *from, FILE *to) |
| 258 |
{ |
| 259 |
char buf[BUFSIZ]; |
| 260 |
size_t size; |
| 261 |
int written = 0; |
| 262 |
|
| 263 |
while (1) { |
| 264 |
size = fread(buf, 1, BUFSIZ, from); |
| 265 |
if (!size) { |
| 266 |
if (written) { |
| 267 |
fflush(to); |
| 268 |
G_fseek(to, 0L, SEEK_SET); |
| 269 |
} |
| 270 |
return (0); |
| 271 |
} |
| 272 |
if (!fwrite(buf, 1, size, to)) { |
| 273 |
G_warning(_("Unable to write to file")); |
| 274 |
return (-1); |
| 275 |
} |
| 276 |
written = 1; |
| 277 |
} |
| 278 |
|
| 279 |
/* NOTREACHED */ |
| 280 |
return -1; |
| 281 |
} |