Debian Codegraph @

raster/r.in.ascii/gethead.c

raw
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4 #include <grass/gis.h>
5 #include <grass/raster.h>
6 #include <grass/glocale.h>
7 #include "local_proto.h"
8
9 #define DOT "." /* for determining data type -tw */
10 #define INT "int"
11 #define FLOAT "float"
12 #define DOUBLE "double"
13 #define TMPBUFSIZE 8192
14
15 static int missing(int, char *);
16 static int extract(int, char *, char *, void *, int,
17 int (*)(const char *, double *, int));
18 static int scan_int(const char *, double *, int);
19
20 const char gs_ascii_flag[5] = {"DSAA"};
21
22 int getgrdhead(FILE *fd, struct Cell_head *cellhd)
23 {
24 char grd_flag[6];
25 int nc, nr;
26 float xmin, xmax, ymin, ymax, zmin, zmax;
27
28 /* make sure the input file is positioned at the beginning */
29 rewind(fd);
30
31 /* read and check the flag on the first line */
32 if (fgets(grd_flag, sizeof(grd_flag), fd) == NULL) {
33 G_warning(_("failed to read the input file"));
34 return 0;
35 }
36 if (strncmp(gs_ascii_flag, grd_flag, strlen(gs_ascii_flag))) {
37 G_warning(_("input file is not a Surfer ascii grid file"));
38 return 0;
39 }
40
41 /* read the row and column dimensions */
42 if (fscanf(fd, "%d %d \n", &nc, &nr) != 2) {
43 G_warning(_("error reading the column and row dimension from the "
44 "Surfer grid file"));
45 return 0;
46 }
47
48 /* read the range of x values */
49 if (fscanf(fd, "%f %f \n", &xmin, &xmax) != 2) {
50 G_warning(_("error reading the X range from the Surfer grid file"));
51 return 0;
52 }
53
54 /* read the range of y values */
55 if (fscanf(fd, "%f %f \n", &ymin, &ymax) != 2) {
56 G_warning(_("error reading the Y range from the Surfer grid file"));
57 return 0;
58 }
59
60 /* read the range of z values (not used) */
61 if (fscanf(fd, "%f %f \n", &zmin, &zmax) != 2) {
62 G_warning(_("error reading the Z range from the Surfer grid file"));
63 return 0;
64 }
65
66 /* initialize the cell header */
67 cellhd->zone = G_zone();
68 cellhd->proj = G_projection();
69 cellhd->rows = nr;
70 cellhd->cols = nc;
71
72 cellhd->ew_res = (double)(xmax - xmin) / (nc - 1);
73 cellhd->ns_res = (double)(ymax - ymin) / (nr - 1);
74 /* the Surfer grid specifies x,y locations of gridded points. The GRASS
75 raster specifies an area covered by rectangular cells centered at gridded
76 points. That difference requires an adjustment */
77 cellhd->north = ymax + cellhd->ns_res / 2.;
78 cellhd->south = ymin - cellhd->ns_res / 2.;
79 cellhd->east = xmax + cellhd->ew_res / 2.;
80 cellhd->west = xmin - cellhd->ew_res / 2.;
81
82 return 1;
83 }
84
85 int gethead(FILE *fd, struct Cell_head *cellhd, RASTER_MAP_TYPE *d_type,
86 DCELL *mult, char **nval)
87 {
88 int n, s, e, w, r, c;
89 char label[100], value[100];
90 char buf[1024];
91 int ret;
92
93 /* rsb fix */
94 fpos_t p;
95
96 n = s = e = w = r = c = 0;
97
98 cellhd->zone = G_zone();
99 cellhd->proj = G_projection();
100
101 /* while (n == 0 || s== 0 || e == 0 || w == 0 || r == 0 || c == 0) */
102
103 while (1) {
104 /* rsb fix */
105 if (fgetpos(fd, &p) != 0)
106 G_fatal_error(_("error getting file position"));
107
108 if (!G_getl2(buf, sizeof(buf), fd))
109 break;
110
111 *label = *value = '\0';
112 if (NULL == strstr(buf, ":"))
113 break;
114 if (sscanf(buf, "%[^:]:%s", label, value) != 2)
115 break;
116 if (*label == '\0')
117 continue; /* ignore blank lines */
118
119 if (strcmp(label, "north") == 0) {
120 extract(n++, label, value, &cellhd->north, cellhd->proj,
121 G_scan_northing);
122 continue;
123 }
124
125 if (strcmp(label, "south") == 0) {
126 extract(s++, label, value, &cellhd->south, cellhd->proj,
127 G_scan_northing);
128 continue;
129 }
130
131 if (strcmp(label, "east") == 0) {
132 extract(e++, label, value, &cellhd->east, cellhd->proj,
133 G_scan_easting);
134 continue;
135 }
136
137 if (strcmp(label, "west") == 0) {
138 extract(w++, label, value, &cellhd->west, cellhd->proj,
139 G_scan_easting);
140 continue;
141 }
142
143 if (strcmp(label, "rows") == 0) {
144 extract(r++, label, value, &cellhd->rows, cellhd->proj, scan_int);
145 continue;
146 }
147
148 if (strcmp(label, "cols") == 0) {
149 extract(c++, label, value, &cellhd->cols, cellhd->proj, scan_int);
150 continue;
151 }
152 /* try to read optional header fields */
153
154 if (strcmp(label, "type") == 0) { /* if not exist then the file scan */
155 if (*d_type < 0) { /* if data type not set on command line */
156 if (!strncmp(value, INT, strlen(INT)))
157 *d_type = CELL_TYPE;
158 else if (!strncmp(value, FLOAT, strlen(FLOAT)))
159 *d_type = FCELL_TYPE;
160 else if (!strncmp(value, DOUBLE, strlen(DOUBLE)))
161 *d_type = DCELL_TYPE;
162 else {
163 G_warning(_("illegal type field: using type int"));
164 *d_type = CELL_TYPE;
165 }
166 }
167 else
168 G_warning(_("ignoring type filed in header, type is set on "
169 "command line"));
170 continue;
171 }
172
173 if (strcmp(label, "multiplier") == 0) {
174 if (Rast_is_d_null_value(
175 mult)) { /* if mult not set on command line */
176 if (sscanf(value, "%lf", mult) != 1) {
177 G_warning(_("illegal multiplier field: using 1.0"));
178 *mult = 1.0;
179 }
180 }
181 else
182 G_warning(_("ignoring multiplier filed in header, multiplier "
183 "is set on command line"));
184 continue;
185 }
186
187 if (strcmp(label, "null") == 0) {
188 if (!(*nval)) /* if null val string not set on command line */
189 *nval = G_store(value);
190 else
191 G_warning(_("ignoring null filed in header, null string is set "
192 "on command line"));
193 continue;
194 }
195
196 } /* while */
197 /* the line read was not a header line, but actually
198 the first data line, so put it back on the stack and break */
199 /* rsb fix */
200 fsetpos(fd, &p);
201
202 missing(n, "north");
203 missing(s, "south");
204 missing(e, "east");
205 missing(w, "west");
206 missing(r, "rows");
207 missing(c, "cols");
208
209 if (!(*nval))
210 *nval = G_store("*");
211 if (Rast_is_d_null_value(mult))
212 *mult = 1.0;
213 /* if data type is not set, then scan data to find out data type */
214 if (*d_type < 0) {
215 ret = file_scan(fd);
216 if (!ret)
217 *d_type = DCELL_TYPE;
218 else if (ret == 1)
219 *d_type = CELL_TYPE;
220 else {
221 G_warning(_("error in ascii data format"));
222 return 0;
223 }
224 }
225
226 G_adjust_Cell_head(cellhd, 1, 1);
227
228 return 1;
229 }
230
231 static int scan_int(const char *s, double *i, int proj G_UNUSED)
232 {
233 char dummy[3];
234 int *i_loc = (int *)i;
235
236 *dummy = 0;
237
238 if (sscanf(s, "%d%1s", i_loc, dummy) != 1)
239 return 0;
240 if (*dummy)
241 return 0;
242 if (*i_loc <= 0)
243 return 0;
244 return 1;
245 }
246
247 static int extract(int count, char *label, char *value, void *data, int proj,
248 int (*scanner)(const char *, double *, int))
249 {
250 if (count) {
251 G_warning(_("Duplicate \"%s\" field in header"), label);
252 return 0;
253 }
254 if (scanner(value, data, proj))
255 return 1;
256 G_warning(_("Illegal \"%s\" value in header: %s"), label, value);
257 return 0;
258 }
259
260 static int missing(int count, char *label)
261 {
262 if (count)
263 return 0;
264 G_warning(_("\"%s\" field missing from header"), label);
265 return 1;
266 }
267
268 /* file_scan(): determine data type in ascii format */
269 int file_scan(FILE *fd)
270 {
271 long curpos;
272 char tmpbuf[TMPBUFSIZE];
273 size_t size = TMPBUFSIZE;
274
275 if ((curpos = G_ftell(fd)) == -1)
276 return -1;
277 while (!feof(fd)) {
278 if (size != fread(tmpbuf, sizeof(char), size, fd)) {
279 if (!feof(fd))
280 return -1;
281 }
282 if (strstr(tmpbuf, DOT) != NULL) {
283 G_fseek(fd, curpos - 1L, SEEK_SET);
284 return 0;
285 }
286 }
287 G_fseek(fd, curpos - 1L, SEEK_SET);
288 return 1;
289 }