Skip to content

Commit 18fea02

Browse files
committed
Optional asc/nodata_value
1 parent f026577 commit 18fea02

File tree

3 files changed

+72
-33
lines changed

3 files changed

+72
-33
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ jobs:
1414
1515
- name: Unit tests
1616
run: |
17-
sudo apt-get install -y gcovr lcov
17+
sudo apt-get update
18+
sudo apt-get install -y gcovr lcov libpng-dev libtiff-dev
19+
ldconfig -p | grep libpng
1820
make libcheck
1921
make test
2022

src/turtle/io/asc.c

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static void upify(char * s)
4747
if ((*s >= 'a') && (*s <= 'z')) {
4848
*s += 'A' - 'a';
4949
}
50-
*s++;
50+
s++;
5151
}
5252
}
5353

@@ -79,23 +79,21 @@ static enum turtle_return asc_open(struct turtle_io * io, const char * path,
7979
asc->path = path;
8080

8181
/* Parse the meta data from the header */
82-
double nodata_value;
8382
char ncols[8] = { 0 }, nrows[8] = { 0 };
8483
char x0entry[16] = { 0 }, y0entry[16] = { 0 }, cellsize[16] = { 0 };
85-
char nodata[32] = { 0 };
8684
if ((fscanf(asc->fid, "%7s %d", ncols, &io->meta.nx) != 2) ||
87-
(fscanf(asc->fid, "%7s %d", nrows, &io->meta.ny) != 2) ||
85+
(fscanf(asc->fid, "%7s %d", nrows, &io->meta.ny) != 2) ||
8886
(fscanf(asc->fid, "%15s %lf", x0entry, &io->meta.x0) != 2) ||
8987
(fscanf(asc->fid, "%15s %lf", y0entry, &io->meta.y0) != 2) ||
90-
(fscanf(asc->fid, "%15s %lf", cellsize, &io->meta.dx) != 2) ||
91-
(fscanf(asc->fid, "%31s %lf", nodata, &nodata_value) != 2)) {
88+
(fscanf(asc->fid, "%15s %lf", cellsize, &io->meta.dx) != 2)) {
9289
io->close(io);
9390
return TURTLE_ERROR_VREGISTER(TURTLE_RETURN_BAD_FORMAT,
9491
"could not read the header of file `%s'", path);
9592
}
9693
io->meta.dy = io->meta.dx;
9794
upify(ncols);
9895
if (strcmp(ncols, "NCOLS") != 0) {
96+
io->close(io);
9997
return TURTLE_ERROR_VREGISTER(
10098
TURTLE_RETURN_BAD_FORMAT,
10199
"%s: expected `NCOLS`, found `%s`",
@@ -105,6 +103,7 @@ static enum turtle_return asc_open(struct turtle_io * io, const char * path,
105103
}
106104
upify(nrows);
107105
if (strcmp(nrows, "NROWS") != 0) {
106+
io->close(io);
108107
return TURTLE_ERROR_VREGISTER(
109108
TURTLE_RETURN_BAD_FORMAT,
110109
"%s: expected `NROWS`, found `%s`",
@@ -116,6 +115,7 @@ static enum turtle_return asc_open(struct turtle_io * io, const char * path,
116115
if (strcmp(x0entry, "XLLCORNER") == 0) {
117116
io->meta.x0 += 0.5 * io->meta.dx;
118117
} else if (strcmp(x0entry, "XLLCENTER") != 0) {
118+
io->close(io);
119119
return TURTLE_ERROR_VREGISTER(
120120
TURTLE_RETURN_BAD_FORMAT,
121121
"%s: expected `XLLCENTER` or `XLLCORNER`, found `%s`",
@@ -127,6 +127,7 @@ static enum turtle_return asc_open(struct turtle_io * io, const char * path,
127127
if (strcmp(y0entry, "YLLCORNER") == 0) {
128128
io->meta.y0 += 0.5 * io->meta.dy;
129129
} else if (strcmp(y0entry, "YLLCENTER") != 0) {
130+
io->close(io);
130131
return TURTLE_ERROR_VREGISTER(
131132
TURTLE_RETURN_BAD_FORMAT,
132133
"%s: expected `YLLCENTER` or `YLLCORNER`, found `%s`",
@@ -136,44 +137,80 @@ static enum turtle_return asc_open(struct turtle_io * io, const char * path,
136137
}
137138
upify(cellsize);
138139
if (strcmp(cellsize, "CELLSIZE") != 0) {
140+
io->close(io);
139141
return TURTLE_ERROR_VREGISTER(
140142
TURTLE_RETURN_BAD_FORMAT,
141143
"%s: expected `CELLSIZE`, found `%s`",
142144
path,
143145
cellsize
144146
);
145147
}
146-
upify(nodata);
147-
if (strcmp(nodata, "NODATA_VALUE") != 0) {
148-
return TURTLE_ERROR_VREGISTER(
149-
TURTLE_RETURN_BAD_FORMAT,
150-
"%s: expected `NODATA_VALUE`, found `%s`",
151-
path,
152-
nodata
153-
);
154-
}
155148

156-
/* Check the min and max z values */
157-
const long offset = ftell(asc->fid);
158-
int i;
159-
double zmin = DBL_MAX, zmax = -DBL_MIN;
160-
for (i = 0; i < io->meta.ny; i++) {
161-
int j;
162-
for (j = 0; j < io->meta.nx; j++) {
163-
double d;
164-
if (fscanf(asc->fid, "%lf", &d) != 1) {
149+
/* Check for any nodata_value */
150+
long offset = ftell(asc->fid);
151+
double nodata_value = -DBL_MAX;
152+
double zmin = DBL_MAX, zmax = -DBL_MAX;
153+
int i = 0;
154+
{
155+
int nread;
156+
double d;
157+
if ((nread = fscanf(asc->fid, "%lf", &d)) == 0) {
158+
char nodata[32] = { 0 };
159+
160+
fseek(asc->fid, offset, SEEK_SET);
161+
if (fscanf(asc->fid, "%31s %lf", nodata,
162+
&nodata_value) != 2) {
163+
io->close(io);
164+
return TURTLE_ERROR_VREGISTER(
165+
TURTLE_RETURN_BAD_FORMAT,
166+
"could not read the header of file `%s'",
167+
path
168+
);
169+
}
170+
upify(nodata);
171+
if (strcmp(nodata, "NODATA_VALUE") != 0) {
165172
io->close(io);
166173
return TURTLE_ERROR_VREGISTER(
167174
TURTLE_RETURN_BAD_FORMAT,
168-
"inconsistent data in file `%s'", path);
175+
"%s: expected `NODATA_VALUE`, found `%s`",
176+
path,
177+
nodata
178+
);
179+
}
180+
offset = ftell(asc->fid);
181+
} else if (nread == 1) {
182+
if (d != nodata_value) {
183+
zmin = zmax = d;
169184
}
170-
if (d == nodata_value)
171-
continue;
172-
else if (d < zmin)
173-
zmin = d;
174-
else if (d > zmax)
175-
zmax = d;
185+
i++;
186+
} else {
187+
io->close(io);
188+
return TURTLE_ERROR_VREGISTER(
189+
TURTLE_RETURN_BAD_FORMAT,
190+
"inconsistent data in file `%s'",
191+
path
192+
);
193+
}
194+
}
195+
196+
/* Check the min and max z values */
197+
const int n = io->meta.nx * io->meta.ny;
198+
for (; i < n; i++) {
199+
double d;
200+
if (fscanf(asc->fid, "%lf", &d) != 1) {
201+
io->close(io);
202+
return TURTLE_ERROR_VREGISTER(
203+
TURTLE_RETURN_BAD_FORMAT,
204+
"inconsistent data in file `%s'",
205+
path
206+
);
176207
}
208+
if (d == nodata_value)
209+
continue;
210+
else if (d < zmin)
211+
zmin = d;
212+
else if (d > zmax)
213+
zmax = d;
177214
}
178215
fseek(asc->fid, offset, SEEK_SET);
179216
io->meta.z0 = zmin;

src/turtle/io/grd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static enum turtle_return grd_open(struct turtle_io * io, const char * path,
8686
/* Check the min and max z values */
8787
const long offset = ftell(grd->fid);
8888
int i;
89-
double zmin = DBL_MAX, zmax = -DBL_MIN;
89+
double zmin = DBL_MAX, zmax = -DBL_MAX;
9090
for (i = 0; i < io->meta.ny; i++) {
9191
int j;
9292
for (j = 0; j < io->meta.nx; j++) {

0 commit comments

Comments
 (0)