I'm quite new to python and want to interpolate in a regular grid. First, i tried it with interp2d. This worked for most cases, however for some cases (the only difference were the values) there was a warning and the result was not as expected. This is the code:
img=sys.argv[1]
latitude=np.loadtxt(img+'/'+'geolayer.Lat.txt')
longitude=np.loadtxt(img+'/'+'geolayer.Lon.txt')
n=int(sys.argv[2])
rows=int(sys.argv[3])
cols=int(sys.argv[4])
m=len(latitude)/n
#Latitude
column=latitude[:,0].reshape((m,n))
row=latitude[:,1].reshape((m,n))
val=latitude[:,2].reshape((m,n))
# create interpolation object
interp=interp2d(column,row,val)
# interpolate values
lattmp=interp(np.arange(cols),np.arange(rows))
lat=np.degrees(np.arctan(1.0067395*np.tan(np.radians(lattmp))))
This is the warning I get occasionally:
Warning: No more knots can be added because the number of B-spline coefficients
already exceeds the number of data points m. Probably causes: either
s or m too small. (fp>s)
kx,ky=1,1 nx,ny=14,14 m=143 fp=0.000000 s=0.000000
This is how the input looks like:
In [174]: shape(column)
Out[174]: (13, 11)
In [175]: shape(row)
Out[175]: (13, 11)
In [176]: shape(val)
Out[176]: (13, 11)
The only difference between warning and no warning are the values in val. After reading in several threads, I tried also scipy.interpolate.RectBivariateSpline:
ttt=scipy.interpolate.RectBivariateSpline(rr,cc,val,bbox=[0,4199, 0,4099],kx=1,ky=1)
lattmp=ttt(np.arange(cols),np.arange(rows))
In [181]: shape(cc)
Out[181]: (11,)
In [182]: shape(rr)
Out[182]: (13,)
In [183]: shape(val)
Out[183]: (13, 11)
But i only get this:
In [170]: lattmp
Out[170]:
array([[ NaN, NaN, NaN, ..., NaN, NaN, NaN],
[ NaN, Inf, Inf, ..., Inf, Inf, Inf],
[ NaN, Inf, Inf, ..., Inf, Inf, Inf],
...,
[ NaN, Inf, Inf, ..., Inf, Inf, Inf],
[ NaN, Inf, Inf, ..., Inf, Inf, Inf],
[ NaN, Inf, Inf, ..., Inf, Inf, Inf]])
Could anyone tell me, what I can do?
Best regards and thank you, Mathias