[GRASS-dev] GRASS Python Ctypes: Write Raster produces No null file error!!

Dear List Users,

Please help me solve the following, I am entirely new to the whole Python-GRASS-Ctypes issue so please bear with me.

I am trying to develop an application using Python Ctypes hooks to GRASS C libraries. Have done most of the part but getting stuck in the end where i try to write off my output file as GRASS raster. I am giving the relevant part below along with the error I am getting:

ptype = POINTER(c_float)
MA.resize(bands,rows,cols)
for b in range(bands):
ofn = “madresult.” + str(b)
out_fd = Rast_open_new(ofn,FCELL_TYPE)
if out_fd < 0:
G_fatal_error(“Unable to create raster map <%s>”, ofn)
out_rast = Rast_allocate_buf(FCELL_TYPE)
out_rast = cast(c_void_p(out_rast), ptype)
for row_n in range(rows):
for col_n in range(cols):
if math.isnan(MA[b,row_n,col_n]):
Rast_set_null_value(out_rast[col_n],1,FCELL_TYPE)
else:
out_rast[col_n] = MA[b,row_n,col_n]
Rast_put_row(out_fd,out_rast,FCELL_TYPE)
G_free(out_rast)
Rast_close(out_fd)

where MA is a numpy.ndarray, bands, rows and cols are all integer type variables and are self explanatory i believe.

When it comes to executing the line “Rast_close(out_fd)” I am getting the error: “ERROR: No null file for <madresult.0>”

How do I write the null file? I have some experience in developing GRASS raster modules in C and there I never had to write any null file explicitly so whats going wrong over here? Any help from all you GRASS pioneers will be deeply appreciated.

Folks
On close observation made some changes to the fragment of code below on the suspicion that I was not setting null values correctly or passing pointers right so the modified code is now given below but still I get the same error of: ERROR: No null file for <madresult.0>

Please give me some suggestions as I have a deadline approaching.

Modified code:

MA.resize(bands,rows,cols)
for b in range(bands):
ofn = “madresult.” + str(b)
out_fd = Rast_open_new(ofn,DCELL_TYPE)
if out_fd < 0:
grass.fatal(_(“Unable to create raster map <%s>”) % ofn)
out_rast = Rast_allocate_d_buf()
#out_rast = cast(c_void_p(out_rast), ptype)
for row_n in range(rows):
for col_n in range(cols):
#if math.isnan(MAD[b,row_n,col_n]):
if (Rast_is_d_null_value(pointer(c_double(MAD[b,row_n,col_n])))):
Rast_set_d_null_value(pointer(c_double(out_rast[col_n])),1)
else:
out_rast[col_n] = MA[b,row_n,col_n]
Rast_put_row(out_fd,out_rast,DCELL_TYPE)
G_free(out_rast)
Rast_close(out_fd)

Humble regards

On Apr 11, 2011, at 8:25 AM, Sudipta Sarkar wrote:

Dear List Users,

Please help me solve the following, I am entirely new to the whole Python-GRASS-Ctypes issue so please bear with me.

I am trying to develop an application using Python Ctypes hooks to GRASS C libraries. Have done most of the part but getting stuck in the end where i try to write off my output file as GRASS raster. I am giving the relevant part below along with the error I am getting:

ptype = POINTER(c_float)
MA.resize(bands,rows,cols)
for b in range(bands):
ofn = “madresult.” + str(b)
out_fd = Rast_open_new(ofn,FCELL_TYPE)
if out_fd < 0:
G_fatal_error(“Unable to create raster map <%s>”, ofn)
out_rast = Rast_allocate_buf(FCELL_TYPE)
out_rast = cast(c_void_p(out_rast), ptype)
for row_n in range(rows):
for col_n in range(cols):
if math.isnan(MA[b,row_n,col_n]):
Rast_set_null_value(out_rast[col_n],1,FCELL_TYPE)
else:
out_rast[col_n] = MA[b,row_n,col_n]
Rast_put_row(out_fd,out_rast,FCELL_TYPE)
G_free(out_rast)
Rast_close(out_fd)

where MA is a numpy.ndarray, bands, rows and cols are all integer type variables and are self explanatory i believe.

When it comes to executing the line “Rast_close(out_fd)” I am getting the error: “ERROR: No null file for <madresult.0>”

How do I write the null file? I have some experience in developing GRASS raster modules in C and there I never had to write any null file explicitly so whats going wrong over here? Any help from all you GRASS pioneers will be deeply appreciated.

Sudipta Sarkar wrote:

Please help me solve the following, I am entirely new to the whole
Python-GRASS-Ctypes issue so please bear with me.

I am trying to develop an application using Python Ctypes hooks to GRASS
C libraries. Have done most of the part but getting stuck in the end
where i try to write off my output file as GRASS raster. I am giving the
relevant part below along with the error I am getting:

When it comes to executing the line "Rast_close(out_fd)" I am getting
the error: "ERROR: No null file for <madresult.0>"

The problem occurs because the script writes fewer rows to the file
than it should. The raster library attempts to deal with this by
padding the file with zeros, but there's a bug in the code (it has
already closed the null file at the point where it tries to write the
additional rows; that's what the error message is reporting).

Even if we fix the bug in the raster library, the fact that you aren't
writing the correct number of rows suggests that there's a bug in the
script.

If you're reading or writing rasters using a modified window (region),
note that the window handling has changed significantly between 6.x
and 7.0 (in particular, 7.0 has separate windows for input and
output).

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

Hi Glynn,
Thanks for your suggestion that was indeed the case. Its fixed now.
Really appreciate your help and feedback.

On Apr 11, 2011, at 4:36 PM, Glynn Clements wrote:

Sudipta Sarkar wrote:

Please help me solve the following, I am entirely new to the whole
Python-GRASS-Ctypes issue so please bear with me.

I am trying to develop an application using Python Ctypes hooks to GRASS
C libraries. Have done most of the part but getting stuck in the end
where i try to write off my output file as GRASS raster. I am giving the
relevant part below along with the error I am getting:

When it comes to executing the line "Rast_close(out_fd)" I am getting
the error: "ERROR: No null file for <madresult.0>"

The problem occurs because the script writes fewer rows to the file
than it should. The raster library attempts to deal with this by
padding the file with zeros, but there's a bug in the code (it has
already closed the null file at the point where it tries to write the
additional rows; that's what the error message is reporting).

Even if we fix the bug in the raster library, the fact that you aren't
writing the correct number of rows suggests that there's a bug in the
script.

If you're reading or writing rasters using a modified window (region),
note that the window handling has changed significantly between 6.x
and 7.0 (in particular, 7.0 has separate windows for input and
output).

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