pytroll / pyresample

Geospatial image resampling in Python
http://pyresample.readthedocs.org
GNU Lesser General Public License v3.0
346 stars 94 forks source link

RuntimeWarning: invalid value encountered in divide x_2 = (-b__ - np.sqrt(discriminant)) / (2 * a__) #608

Open saevioapps opened 1 month ago

saevioapps commented 1 month ago

Code Sample, a minimal, complete, and verifiable piece of code

I have a temperature grib message and I want to resample it using cubic resampling. However, i get these warnings in the console. If i lower the radius value I no longer get the warnings but I get 0s in the interpolated data.

 UserWarning: You will likely lose important projection information when converting to a PROJ string from another format. See: https://proj.org/faq.html#what-is-the-best-format-for-describing-coordinate-reference-systems
  proj = self._crs.to_proj4(version=version)

RuntimeWarning: invalid value encountered in divide   x_2 = (-b__ - np.sqrt(discriminant)) / (2 * a__)
import numpy as np
from pyproj import CRS
from pyresample.geometry import GridDefinition
from pyresample import create_area_def
from pyresample.bilinear import NumpyBilinearResampler
from pyresample import geometry

data = np.fromfunction(lambda y, x: y * x, (100, 100))
lons = np.fromfunction(lambda y, x: -180 + x * 3.6, (100, 100))
lats = np.fromfunction(lambda y, x: -90 + y * 1.8, (100, 100))

source_def = geometry.GridDefinition(lons=lons, lats=lats)

crs = CRS.from_epsg(3857)  # EPSG code for WGS 84 / Pseudo-Mercator
target_def = create_area_def('my_area',
        crs,
        1000, 1000,
        area_extent=[-180, -85, 180, 85],                
        units='degrees',
        description='Target grid in Mercator projection')

resampler = NumpyBilinearResampler(source_def, target_def, radius_of_influence=200000)
interpolated_data = resampler.resample(data)

Thank you!

djhoese commented 1 month ago

Normally these types of "invalid value" warnings from numpy come from NaNs in the data or geolocation data. If you're getting this with your example code...then I'm not sure where it would be coming from, especially for large radii values since I would expect that to mean more valid pixels are involved not more invalid.

Maybe @pnuu has ideas since he originally wrote that resampler class.

pnuu commented 1 month ago

The only weird thing is that there is no warning from the previous line which is identical other than there being a addition instead of subtraction. Most likely there are NaNs in a__ or b__ that cause the warning. Nothing to worry about.

pnuu commented 1 month ago

And to clarfiy, the NaN values can be due to the four points used in bilinear calculation to form a paralellogram (opposite edges are parallel) or one/two of the edges being exactly vertical. These special casesare handled in the next steps. The third option forNaN`s is that there are no valid points for the interpolation at all, like if the target area extends beyond the source data coverage, or there are invalid coordinates..