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

Hi,

just had a power cut while doing raster processing.
After restart the following error msg came up:

GRASS 7.1.svn (eu_laea): > g.region n=2571000 s=2211000 w=4183000
e=4523000 res=1000 -a
ERROR: Field <projection> missing

Tracing it down:

GRASS 7.1.svn (eu_laea): > g.region n=2571000 s=2211000 w=4183000
e=4523000 res=1000 -a
D1/3: G_set_program_name(): g.region
D2/3: G_file_name(): path = /grassdata/eu_laea/sp_modis_swath
D2/3: G_file_name(): path = /grassdata/eu_laea/PERMANENT/DEFAULT_WIND
D2/3: G_file_name(): path = /grassdata/eu_laea/PERMANENT/DEFAULT_WIND
D2/3: file open: read (mode = r)
D2/3: G__read_Cell_head
D2/3: G__read_Cell_head_array
D3/3: region item: proj: 99
D3/3: region item: zone: 0
D3/3: region item: north: 2599999.9994859
D3/3: region item: south: 2499999.99955235
D3/3: region item: east: 4400000.0001125
D3/3: region item: west: 4300000.00005084
D3/3: region item: cols: 20
D3/3: region item: rows: 20
D3/3: region item: e-w resol: 5000.00000308
D3/3: region item: n-s resol: 4999.99999668
D3/3: region item: top: 1
D3/3: region item: bottom: 0
D3/3: region item: cols3: 20
D3/3: region item: rows3: 20
D3/3: region item: depths: 1
D3/3: region item: e-w resol3: 5000.00000308
D3/3: region item: n-s resol3: 4999.99999668
D3/3: region item: t-b resol: 1
D2/3: G_file_name(): path = /grassdata/eu_laea/sp_modis_swath/WIND
D2/3: G_file_name(): path = /grassdata/eu_laea/sp_modis_swath/WIND
D2/3: file open: read (mode = r)
D2/3: G__read_Cell_head
D2/3: G__read_Cell_head_array
ERROR: Field <projection> missing
D1/3: G_set_program_name(): g.gisenv
D3/3: G_option_to_separator(): key = separator -> sep = '/'

... 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?

(solution: g.region -d, the it is written anew)

thanks
Markus

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@gclements.plus.com>

Glynn,

On Fri, Aug 7, 2015 at 11:53 PM, Glynn Clements
<glynn@gclements.plus.com> wrote:
...

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.

From a user's perspective, the file info is quite important. So, yes,

the error message including the report about which file is empty would
be ideal.

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);
}

Yes, such a message would solve this particular problem (we got it a
few times lately under heavy workload and unstable power supply).

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.

I'm unsure if it is too much of a change? If only G__...() functions
are affected it should not count as an API change but still...

Markus

On Fri, Aug 7, 2015 at 11:53 PM, Glynn Clements
<glynn@gclements.plus.com> wrote:

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.

...

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);
}

... I have submitted this variant in r65962.
To be backported later if all are happy.

Markus