[GRASS-dev] g.region error msg with empty WIND file

Glynn Clements glynn at gclements.plus.com
Fri Aug 7 14:53:35 PDT 2015


Markus Neteler wrote:

> ERROR: Field <projection> missing

> ... shows after longer debugging that really the WIND file was simply 0 bytes:
> 
> ls -la /grassdata/eu_laea/sp_modis_swath/WIND
> -rw-r--r-- 1 sajid gis 0 Aug  6 11:55 /grassdata/eu_laea/sp_modis_swath/WIND
> 
> 
> How to change lib/gis/rd_cellhd.c to detect this and write out an
> informative message?

The higher up the call chain you detect it, the more specific the
error message can be, but the fewer cases it will handle.

The lowest point is immediately prior to the test which generates the
existing error, e.g.:

--- lib/gis/rd_cellhd.c	(revision 65851)
+++ lib/gis/rd_cellhd.c	(working copy)
@@ -165,6 +165,8 @@
 	    continue;
 	}
     }
+    if (line == 1)
+        G_fatal_error(_("Region definition is empty"));
     if (!TEST(F_PROJ))
 	G_fatal_error(_("Field <%s> missing"), "projection");
     if (!TEST(F_ZONE))

That handles the same set of cases as the existing error, but isn't
significantly more informative; it's just clarifying that *all* of the
fields are missing rather than just the projection.

Catching it in G__read_Cell_head() will allow you to diagnose that
it's a file that is empty:

--- lib/gis/rd_cellhd.c	(revision 65851)
+++ lib/gis/rd_cellhd.c	(working copy)
@@ -77,6 +77,9 @@
 	count++;
     }
 
+    if (!count)
+        G_fatal_error(_("Region file is empty"));
+
     G__read_Cell_head_array(array, cellhd, is_cellhd);
 
     count = 0;

But this won't catch the case where GRASS_REGION is being used, and it
won't allow you to report *which* file is empty.

Catching it in G_get_element_window() would allow you to report the
filename, but additionally won't catch calls from Rast_get_cellhd() to
read the <mapset>/cellhd/<map> file.

--- lib/gis/get_window.c	(revision 65851)
+++ lib/gis/get_window.c	(working copy)
@@ -118,6 +118,10 @@
 	G_fatal_error(_("Unable to open element file <%s> for <%s@%s>"),
 			element, name, mapset);
 
+    G_fseek(fp, 0, SEEK_END);
+    if (!G_ftell(fp))
+        G_fatal_error(_("Region file %s/%s/%s is empty"), mapset, element, name);
+    G_fseek(fp, 0, SEEK_SET);
     G__read_Cell_head(fp, window, 0);
     fclose(fp);
 }

If you want to handle more of the cases while reporting the filename,
G__read_Cell_head() and G__read_Cell_head_array() could be made to
take the filename as an extra parameter so that it can be included in
error messages.

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-dev mailing list