[gdal-dev] Trouble with gdal_calc.py and nodata values

Marco listaddr at gmail.com
Tue May 16 00:01:12 PDT 2023


I'm trying to accomplish a seemingly simple task: starting from an HDF
file, convert it to GTiff, and divide all values in the raster by 10. The
original file uses -1 for nodata, and I want to preserve this value all the
way down to the final tiff.
In other words, where the original file had -1, the final file should also
have -1, and where the original file had a value, the final file should
have that value / 10.

Here are the steps I'm following:

$ gdal_translate -of GTiff -a_nodata -1 "HDF5:/tmp/myfile.hdf://MYDS
/tmp/1.tiff
Input file size is 3712, 3712
0...10...20...30...40...50...60...70...80...90...100 - done.
$ gdal_calc.py -A /tmp/1.tiff --A_band=1 --overwrite --quiet --type=Float32
--NoDataValue -1 --outfile=/tmp/2.tiff --format=GTiff
--calc="numpy.around(A/10, 2)"

This works, but 2.tiff has NaNs instead of -1's.

If I do not use the -a_nodata -1 in the first command (not sure it's
needed...should it detect it automatically from the file's metadata?), then
gdal_calc.py will happily divide the -1's and put -0.1's in the final file,
which is not what I want.

I'm probably missing something very basic here, as I'm not a regular use of
these tools and I'm putting together bits and pieces from the documentation
and random examples by myself.

$ gdal_translate --version
GDAL 3.6.2, released 2023/01/02
$ dpkg -l | grep gdal-bin
ii  gdal-bin                    3.6.2+dfsg-1build1                amd64
   Geospatial Data Abstraction Library - Utility programs

Thanks for any help.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230516/7b22641d/attachment.htm>


More information about the gdal-dev mailing list