[gdal-dev] HFA (.img) driver prints Warning 1: NaN converted to INT_MAX when float raster has nan as the NoData value

Jason Roberts jason.roberts at duke.edu
Thu May 8 16:04:03 PDT 2025


Thank you. I can help you repro it if you want to take a look.

I'm having trouble attaching a .img file to email. My email system 
doesn't want to allow it, even if I put it in a zip. But here is a 
simple way to repro it by creating the file with GDAL itself. (In this 
example, I used float64 and the same warning occurs.) You could use this 
to produce a test file. If you'd prefer I send a file I can post it to a 
file exchange service.

 >>> import numpy as np
 >>>
 >>> from osgeo import gdal
 >>> print(gdal.__version__)
3.9.2e
 >>> gdal.UseExceptions()
 >>>
 >>> # Create the dataset as float64 with 1 band
 >>> f = 'has_nans.img'
 >>> drv = gdal.GetDriverByName('HFA')
 >>> ds = drv.Create(f, 100, 100, 1, gdal.GDT_Float64)
 >>>
 >>> # Make some fake data; set some cells to nan, although this is not 
necessary to repro the warning
 >>> data = np.arange(100*100, dtype=float).reshape(100,100)
 >>> data[0:20,0:30] = float('nan')
 >>>
 >>> # Write the data to the band
 >>> band = ds.GetRasterBand(1)
 >>> band.SetNoDataValue(float('nan'))
0
 >>> band.WriteArray(data, 0, 0)
0
 >>>
 >>> # Close the dataset
 >>> del band, ds
 >>>
 >>> # Open it again - warning happens here
 >>> ds = gdal.Open(f, gdal.GA_ReadOnly)
Warning 1: NaN converted to INT_MAX.

Jason

On 5/8/25 18:39, Even Rouault wrote:
>
> There are metadata fields that the HFA driver opens. That must come 
> from that. Always shutting down the warning could be inappropriate, 
> but perhaps in the context where the warning is emitted the driver 
> could avoid emitting it. But we need to have access to the file to 
> better understand the call path.
>
> Le 09/05/2025 à 00:35, Jason Roberts a écrit :
>> Hi Even,
>>
>> Thanks. I can silence this with a custom error handler (I tested it, 
>> and it works). But the part that is confusing to me, as a user of the 
>> Python API, is that I did not ask the API to do anything with int32s. 
>> All I did was open the dataset using the default parameters of 
>> gdal.Open(). I would not expect a warning to be reported in that 
>> circumstance, unless opening .img files that use nan as the NoData 
>> value was problematic for GDAL in some way. That is why I was 
>> suggesting that the warning be removed.
>>
>> Jason
>>
>> On 5/8/25 17:51, Even Rouault wrote:
>>>
>>> Jason,
>>>
>>> It looks like something in the code asks a float64 field to be 
>>> returned as a int32, and thus NaN has to be turned into something 
>>> else. That's perhaps fine in the context where that happens with 
>>> your file and could potentially be silenced, but access to the file 
>>> would be needed to investigate
>>>
>>> Even
>>>
>>> Le 08/05/2025 à 23:01, Jason Roberts via gdal-dev a écrit :
>>>> Hi GDAL team,
>>>>
>>>> I'm working with the GDAL Python API. I have some HFA (.img) files 
>>>> that are float32 and that use nan as their NoData value. As far as 
>>>> I can tell, using nan is allowed but I could be wrong. When I open 
>>>> them, I get "Warning 1: NaN converted to INT_MAX." Everything seems 
>>>> to work fine after that. Is this message something I need to worry 
>>>> about? If not, may I suggest you remove it from the HFA driver if 
>>>> possible?
>>>>
>>>> Here's some code demonstrating the message. If need be, I can 
>>>> provide an example file, or write some code to generate one.
>>>>
>>>> >>> import numpy as np
>>>> >>> from osgeo import gdal
>>>> >>> gdal.__version__       # this version is packaged with ArcGIS 
>>>> Pro 3.4, but I don't think the version matters
>>>> '3.9.2e'
>>>> >>> ds = 
>>>> gdal.Open(r'\\conch\denmod\Covariates\NARW25\CMEMS_GLORYS\SST\1999\SST_199901.img', 
>>>> gdal.GA_ReadOnly)
>>>> Warning 1: NaN converted to INT_MAX.
>>>> >>> band = ds.GetRasterBand(1)
>>>> >>> gdal.GetDataTypeName(band.DataType)
>>>> 'Float32'
>>>> >>> band.GetNoDataValue()
>>>> nan
>>>> >>> arr = band.ReadAsArray()   # the data look ok:
>>>> >>> np.isnan(arr).sum()
>>>> 86788
>>>> >>> (~np.isnan(arr)).sum()
>>>> 142985
>>>> >>> np.max(arr)
>>>> nan
>>>> >>> np.nanmax(arr)
>>>> 27.078554
>>>>
>>>> As far as I can tell, the warning comes from this code in 
>>>> gdal/frmts/hfa/hfafield.cpp:
>>>>
>>>>                 else if (std::isnan(dfDoubleRet))
>>>>                 {
>>>>                     CPLError(CE_Warning, CPLE_AppDefined,
>>>>                              "NaN converted to INT_MAX.");
>>>>                     nIntRet = nMax;
>>>>                 }
>>>>
>>>> But I don't know if there are any negative ramifications that will 
>>>> result.
>>>>
>>>> Thank you for your help. And, as always, thank you very much for 
>>>> developing and maintaining GDAL.
>>>>
>>>> Jason
>>>>
>>>>
>>>> _______________________________________________
>>>> gdal-dev mailing list
>>>> gdal-dev at lists.osgeo.org
>>>> https://lists.osgeo.org/mailman/listinfo/gdal-dev
>>> -- 
>>> http://www.spatialys.com/
>>> My software is free, but my time generally not.
>>
> -- 
> http://www.spatialys.com/
> My software is free, but my time generally not.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20250508/d64eb055/attachment-0001.htm>


More information about the gdal-dev mailing list