Having trouble moving from plpython documentation example and griddata example to actual working code.
Let us assume that 'my_table' has x, y, and z values for some simple reference grid where x,y pairs are point coordinates and z is the value at that point. Input parameters are inx
and iny
, which define a point coordinate that we want to interpolate a z
value for.
create or replace function somefunc(inx numeric, iny numeric)
returns numeric as
$$
import numpy as np
import scipy as sp
# get the lookup table
refvals = plpy.execute("SELECT x, y, z FROM my_table")
points = np.ndarray(refvals['x'], refvals['y']) #nope
points = np.ndarray(refvals["x"], refvals["y"]) #nope
# ERROR: TypeError: list indices must be integers, not str
points = np.array(refvals[:,0], refvals[:,1]) #nope
# ERROR: TypeError: list indices must be integers, not tuple
zvals = refvals["z"] # nope
return sp.interpolate.griddata(points, zvals, (inx, iny), method="cubic");
$$ LANGUAGE plpython2u stable;
This doesn't work because I don't understand what's going on with refvals and how to get its elements into the appropriate data types for griddata. I'm thinking in terms of r
data.frames (i'd use refvals[,c('x','y')]
to get the pairs of x,y coordinats) or SQL
tables (i'd use select x, y from refvals
), but the refvals
object is something in python called a list of dict
, which I don't quite understand how to access properly.
I gather from the syntax in the documentation foo = rv[i]["my_column"]
that a loop (e.g., for i = 0:len
) may be necessary?