Repository: scipy/scipy

Issue:
Error in SmoothSphereBivariateSpline(): "ValueError: Error code returned by bispev: 10"
The following code produces the error message below. I'm not 100% sure if this is a bug or just an error in the code. Regardless, the error message is not very meaninful IMO.


### Reproducing code example:
```
import numpy as np
from scipy.interpolate import SmoothSphereBivariateSpline

#Define the input data and the original sampling points
NSamp = 2000
Theta = np.random.uniform(0,np.pi,NSamp)
Phi   = np.random.uniform(0,2*np.pi, NSamp)
Data  = np.ones(NSamp)

Interpolator = SmoothSphereBivariateSpline(Theta, Phi, Data, s=3.5)

#Prepare the grid to which the input shall be interpolated
NLon = 64
NLat = 32
GridPosLons = np.arange(NLon)/NLon * 2 * np.pi
GridPosLats = np.arange(NLat)/NLat * np.pi
LatsGrid, LonsGrid = np.meshgrid(GridPosLats, GridPosLons)
Lats = LatsGrid.ravel()
Lons = LonsGrid.ravel()

#Interpolate
Interpolator(Lats, Lons)
```

### Error message:
```Traceback (most recent call last):

  File "<ipython-input-127-81e0ea6e3eed>", line 1, in <module>
    runfile('/home/username/Documents/Python/Playground/TestFile.py', wdir='/home/username/Documents/Python/Playground')

  File "/home/username/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "/home/username/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/username/Documents/Python/Playground/TestFile.py", line 36, in <module>
    Interpolator(Lats, Lons)

  File "/home/username/anaconda3/lib/python3.6/site-packages/scipy/interpolate/fitpack2.py", line 1265, in __call__
    dx=dtheta, dy=dphi, grid=grid)

  File "/home/username/anaconda3/lib/python3.6/site-packages/scipy/interpolate/fitpack2.py", line 845, in __call__
    raise ValueError("Error code returned by bispev: %s" % ier)

ValueError: Error code returned by bispev: 10
```

### Scipy/Numpy/Python version information:
```
1.0.0 1.14.1 sys.version_info(major=3, minor=6, micro=4, releaselevel='final', serial=0)
```




Discussion:
I get the exact same error message in another case using RectSphereBivariateSpline
As answered in https://stackoverflow.com/questions/49318113/error-when-trying-to-interpolate-using-smoothspherebivariatespline-valueerro, if `np.meshgrid` is already used for input data, the interpolate function should be called like `Interpolator(Lats, Lons, grid=False)`. So, I think this is not a defect.

I think "Error code returned by bispev: 10" means invalid input.
https://github.com/scipy/scipy/blob/2a9e4923aa2be5cd54ccf2196fc0da32fe459e76/scipy/interpolate/fitpack/bispev.f#L45-L50
So, I think it is possible to improve the error message.
I think this is also related #9850

Resolving patch:
diff --git a/scipy/interpolate/fitpack2.py b/scipy/interpolate/fitpack2.py
--- a/scipy/interpolate/fitpack2.py
+++ b/scipy/interpolate/fitpack2.py
@@ -900,6 +900,11 @@ def __call__(self, x, y, dx=0, dy=0, grid=True):
             if x.size == 0 or y.size == 0:
                 return np.zeros((x.size, y.size), dtype=self.tck[2].dtype)
 
+            if (x.size >= 2) and (not np.all(np.diff(x) >= 0.0)):
+                raise ValueError("x must be strictly increasing when `grid` is True")
+            if (y.size >= 2) and (not np.all(np.diff(y) >= 0.0)):
+                raise ValueError("y must be strictly increasing when `grid` is True")
+
             if dx or dy:
                 z, ier = dfitpack.parder(tx, ty, c, kx, ky, dx, dy, x, y)
                 if not ier == 0:


Test patch:
diff --git a/scipy/interpolate/tests/test_fitpack2.py b/scipy/interpolate/tests/test_fitpack2.py
--- a/scipy/interpolate/tests/test_fitpack2.py
+++ b/scipy/interpolate/tests/test_fitpack2.py
@@ -944,6 +944,35 @@ def test_array_like_input(self):
                                    bbox=bbox.tolist())
         assert_array_almost_equal(spl1(1.0, 1.0), spl2(1.0, 1.0))
 
+    def test_not_increasing_input(self):
+        # gh-8565
+        NSamp = 20
+        Theta = np.random.uniform(0, np.pi, NSamp)
+        Phi = np.random.uniform(0, 2 * np.pi, NSamp)
+        Data = np.ones(NSamp)
+
+        Interpolator = SmoothSphereBivariateSpline(Theta, Phi, Data, s=3.5)
+
+        NLon = 6
+        NLat = 3
+        GridPosLats = np.arange(NLat) / NLat * np.pi
+        GridPosLons = np.arange(NLon) / NLon * 2 * np.pi
+
+        # No error
+        Interpolator(GridPosLats, GridPosLons)
+
+        nonGridPosLats = GridPosLats.copy()
+        nonGridPosLats[2] = 0.001
+        with assert_raises(ValueError) as exc_info:
+            Interpolator(nonGridPosLats, GridPosLons)
+        assert "x must be strictly increasing" in str(exc_info.value)
+
+        nonGridPosLons = GridPosLons.copy()
+        nonGridPosLons[2] = 0.001
+        with assert_raises(ValueError) as exc_info:
+            Interpolator(GridPosLats, nonGridPosLons)
+        assert "y must be strictly increasing" in str(exc_info.value)
+
 
 class TestRectSphereBivariateSpline(object):
     def test_defaults(self):