интерполяция одномерного массива в сетку трехмерного массива в python - PullRequest
0 голосов
/ 06 ноября 2018

У меня есть одномерный массив с 81 номером, который соответствует температуре 81 на каждые 2,5 метра глубины, и мне нужно интерполировать его в сетку трехмерного массива, которая имеет 100 точек в z-dir, 6 точек в x-dir и 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 голосов
/ 06 ноября 2018

Вы должны иметь возможность построить трехмерный массив из существующего temp 1D массива следующим образом:

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))

Вы также можете выбрать более прямой подход и сразу начать с 1-мерного массива с z-координатами с желаемыми 100 элементами (т.е. пропустить шаг интерполяции). Вот так:

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)

Примечание к сведению

Хорошей практикой считается размещать операторы импорта всегда в начале вашего файла кода .

...