在python中将1D数组插入到3D数组网格中

时间:2018-11-05 21:25:21

标签: python arrays interpolation spatial-interpolation

我有一个带有81个数字的1D数组,每2.5米深度对应于81个温度,我需要将其内插到一个3D数组网格中,该数组在z-dir中有100点,在x-dir中有6点,而在599点在y-dir。我创建一维值的功能是:

zz = np.arange(-200,0.1,2.5)
def grid_function(x, ABath=-0.2, BBath=0.1, CBath=50.,DBath=10.):
    """This function creates a theoretical grid"""

    from numpy import tanh, arange


    ans = ABath * (tanh(BBath * (-x - CBath))) + DBath
    return ans

temp = grid_function(zz)

下面是我的网格的横截面 enter image description here

我不知道我的要求是否清楚,但是如果有人知道我的方式,我将非常感激。

此致

1 个答案:

答案 0 :(得分:1)

您应该能够从现有的temp 1D数组中构造3D数组,如下所示:

zz = np.arange(-200,0.1,2.5)
def grid_function(x, ABath=-0.2, BBath=0.1, CBath=50.,DBath=10.):
    """This function creates a theoretical grid"""

    from numpy import tanh, arange


    ans = ABath * (tanh(BBath * (-x - CBath))) + DBath
    return ans

temp = grid_function(zz)

# Construct 1D 100-element array with z-coordinates
z_new = np.linspace(zz[0], zz[-1], 100)

# Interpolate 1D temperatures at new set of 100 z-coordinates
temp_1d_new = np.interp(z_new, zz, temp)

# Replicate 1D temperatures into two additional dimensions
temp_3d_new = np.tile(temp_1d_new, (6, 599, 1))

不过,您也可以采用更直接的方法,并立即从具有所需100个元素的z坐标一维数组开始(即跳过插值步骤)。像这样:

def grid_function(x, ABath=-0.2, BBath=0.1, CBath=50.,DBath=10.):
    """This function creates a theoretical grid"""

    from numpy import tanh, arange


    ans = ABath * (tanh(BBath * (-x - CBath))) + DBath
    return ans

# Create 1D arrays with x-coordinates, y-coordinates and z-coordinates
x = np.linspace(0., 100., 6)
y = np.linspace(0., 100., 599)
z = np.linspace(-200., 0., 100)

# Create 3D meshgrids for x-coordinates, y-coordinates and z-coordinates
(xx, yy, zz) = np.meshgrid(x, y, z)

# Calculate temperatures 3D array from z-coordinates 3D array
temp = grid_function(zz)

旁注

place import statements always at the top of your code file被认为是一种很好的做法。

相关问题