[GRASS-dev] opening and closing rasters: hangup error on close raster.

Hi,

I’m having major problems with some C code I’m writing within the GRASS environment. Basically, my code opens 6 rasters, 3 of which exist already the other 3 are new. When I look at the code with ddd everything seems fine, i.e. the arrays for the new rasters are created as required but when it comes to closing the rasters, the previously-existing rasters close fine but I get ‘hangup’ error when I try to close the first newly-created raster, the code crashes and I lose the new rasters.

Following is the mechanisms I use to open and close the files. I would like some expert advice on whether anything looks strange just so I can debug elsewhere in the code.

I would really appreciate any suggestions since I’ve wasted so much time trying to fix this.

Thanks,

Garret Duffy


I open the rasters like so:

void
Open_GRASS_files ()
{

char fname[1024];
if (G_set_window (&working_area) < 0)
exit (3);
mapset = G_mapset ();

/*open EXISTING bathy raster */
strcpy (fname, bathy_raster);
cf_bathy = G_open_cell_old (fname, mapset);

if (cf_bathy < 0)

{
char msg[100];
sprintf (msg, “unable to create raster map %s”, fname);
G_fatal_error (msg);
exit (1);
}

cell_bathy = (FCELL *) G_allocate_f_raster_buf ();

/*open EXISTING crests raster */
strcpy (fname, crests_raster);
cf_crests = G_open_cell_old (fname, mapset);

if (cf_crests < 0)

{
char msg[100];
sprintf (msg, “unable to create raster map %s”, fname);
G_fatal_error (msg);
exit (1);
}

cell_crests = (FCELL *) G_allocate_f_raster_buf ();

/*open EXISTING slope raster */
strcpy (fname, slope_raster);
cf_slope = G_open_cell_old (fname, mapset);

if (cf_slope < 0)

{
char msg[100];
sprintf (msg, “unable to create raster map %s”, fname);
G_fatal_error (msg);
exit (1);
}

cell_slope = (FCELL *) G_allocate_f_raster_buf ();

/open NEW troughs raster/
strcpy (fname, output_troughs_raster);
cf_troughs = G_open_fp_cell_new (fname);
if (cf_troughs < 0)

{
char msg[100];
printf (msg, “unable to create raster map %s”, fname);
G_fatal_error (msg);
exit (1);
}
cell_troughs = (FCELL *) G_allocate_f_raster_buf ();

/open NEW height raster/
strcpy (fname, output_height_raster);
cf_height = G_open_fp_cell_new (fname);
if (cf_height < 0)

{
char msg[100];
printf (msg, “unable to create raster map %s”, fname);
G_fatal_error (msg);
exit (1);
}
cell_height = (FCELL *) G_allocate_f_raster_buf ();

/open NEW spacing raster/
strcpy (fname, output_spacing_raster);
cf_spacing = G_open_fp_cell_new (fname);
if (cf_spacing < 0)

{
char msg[100];
printf (msg, “unable to create raster map %s”, fname);
G_fatal_error (msg);
exit (1);
}
cell_spacing = (FCELL *) G_allocate_f_raster_buf ();

}


I close them like this:

printf ("Closing bathy raster… ");
G_close_cell (cf_bathy);
printf (“done!\n”);

printf ("Closing crests raster… ");
G_close_cell (cf_crests);
printf (“done!\n”);

printf ("Closing troughs raster… ");
G_close_cell (cf_troughs);
printf (“done!\n”);

printf ("Closing slope raster… ");
G_close_cell (cf_slope);
printf (“done!\n”);

printf ("Closing spacing raster… ");
G_close_cell (cf_spacing);
printf (“done!\n”);

printf ("Closing height raster… ");
G_close_cell (cf_height);
printf (“done!\n”);

Duffy, Garret wrote:

I'm having major problems with some C code I'm writing within the GRASS
environment. Basically, my code opens 6 rasters, 3 of which exist
already the other 3 are new. When I look at the code with ddd
everything seems fine, i.e. the arrays for the new rasters are created
as required but when it comes to closing the rasters, the
previously-existing rasters close fine but I get 'hangup' error when I
try to close the first newly-created raster, the code crashes and I lose
the new rasters.

"Hangup" normally refers to closing the terminal. It's possible that
one of the variables which holds a descriptor has the wrong value, and
closing it closes the terminal.

Following is the mechanisms I use to open and close the files. I would
like some expert advice on whether anything looks strange just so I can
debug elsewhere in the code.

I would really appreciate any suggestions since I've wasted so much time
trying to fix this.

I open the rasters like so:

void
Open_GRASS_files ()
{

  char fname[1024];
  if (G_set_window (&working_area) < 0)
    exit (3);
  mapset = G_mapset ();

  /*open EXISTING bathy raster */
  strcpy (fname, bathy_raster);
  cf_bathy = G_open_cell_old (fname, mapset);

Input map names need to be resolved with G_find_cell() or
G_find_cell2(), e.g.:

  mapset = G_find_cell2(fname, "");
  cf_bathy = G_open_cell_old(fname, mapset);

This accounts for the fact that the map name may be fully qualified,
i.e. map@mapset.

  if (cf_bathy < 0)

    {
      char msg[100];
      sprintf (msg, "unable to create raster map %s", fname);
      G_fatal_error (msg);
      exit (1);
    }

G_fatal_error() accepts a printf()-style format string and arguments,
so there's no need to use sprintf(); you can just use e.g.:

  G_fatal_error ("unable to create raster map %s", fname);

___________________________
I close them like this:

  printf ("Closing bathy raster... ");
  G_close_cell (cf_bathy);
  printf ("done!\n");

Apart from the issues noted above (which wouldn't cause the problems
you describe), I don't see anything wrong with your code.

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

Glynn Clements wrote:

Duffy, Garret wrote:

I'm having major problems with some C code I'm writing within the GRASS
environment. Basically, my code opens 6 rasters, 3 of which exist
already the other 3 are new. When I look at the code with ddd
everything seems fine, i.e. the arrays for the new rasters are created
as required but when it comes to closing the rasters, the
previously-existing rasters close fine but I get 'hangup' error when I
try to close the first newly-created raster, the code crashes and I lose
the new rasters.
   
"Hangup" normally refers to closing the terminal. It's possible that
one of the variables which holds a descriptor has the wrong value, and
closing it closes the terminal.

Following is the mechanisms I use to open and close the files. I would
like some expert advice on whether anything looks strange just so I can
debug elsewhere in the code.

I'm not that familiar with GRASS module programming, but as you wrote "... I can debug somewhere else in the code" I assume you perform some read / write operations in between.
- a stepwise approach would be useful, so just open and close the rasters; if the error still applies I can't help
- just read from the existing rasters
- write to the new rasters: a typical mistake would be to write outside of the bounds of the arrays; at least in other GIS this may work some time but will cause a crash on exit. Another problem might be an unintentional calculation on NoData cells.

maybe this helps,
Volker

Apart from the issues noted above (which wouldn't cause the problems
you describe), I don't see anything wrong with your code.