在python

时间:2017-08-10 22:02:32

标签: python arrays numpy interpolation weather

我一直在研究我用于WRF模型研究的python脚本,而且我在插值例程方面遇到了一些困难。在我的情况下,我试图绘制一个特定的场,然而,使用全面的压力水平,特别是对于较低的水平(1000,850),通常在处理山时产生极端的最大值或纳米值地区,因为它们低于地面。

所以,我的想法是编写一个检测地面压力水平的脚本(使用以下内容轻松完成):

pb = ncFile.variables['PB'][0][0]
p = ncFile.variables['P'][0][0]
sfcPres = pb + p

这导致2D阵列表面包含地面压力,然后建立另外两个分别包含50hPa和100hPa压力的场:

medLevel = sfcPres - 50
topLevel = sfcPres - 100

从这里我想给三个数组:sfcPres,medLevel和topLevel作为插值函数作为高度参数来插入每个lat,lon对的数据集到各自的lat,lon对三者阵列。

我的问题是,我迄今为止使用的所有插值程序只允许插值到压力水平的奇异值,正如我上面所说,这导致边缘极值问题。

我希望能够以this function的顺序执行某些操作,其中desiredlevel参数可以获取该2D数组并对3D数据集执行插值([Z,lat,lon]的数组)在该2D阵列中的每个点。

有没有人知道一种简单的方法,不涉及使用循环,因为数据集相当大,我需要使用8个组合集计算约60个文件的函数的平均值。

谢谢!

1 个答案:

答案 0 :(得分:0)

没有简单的方法可以做到这一点,它确实涉及使用循环。我有自己的WRF例程,它将4D变量字段线性插值到我指定的恒定高度表面。我相信你应该能够为压力表面修改这个功能。我一直在工作,虽然WRF数据让我自己烦恼。

def linear_height(var,surface): #defaults to .5km to 20.5km with .5 km intervals
'''
Requirements:
import numpy as np

This function will take in a variable and the corrosponding height surface of the model to
then interpolate to constant height surfaces from 500m to 20km at 500m intervals.
The interpolation to the new height surfaces is linear and occurs from the lowest level x,y point
to the top level x,y point.

The output variable will have the same units as the input variable but will have a different shape
Assuming that height is the second column of the array, this will be the only length to change
example: linear_height(Temperature, Height_surfaces)

'''
######################################
#Edit to change the levels of the interpolated height coordinates
new_height = np.arange(500,20500,500) # 1km to 20 km
######################################
new_surf = np.empty((var.shape[0],new_height.shape[0],var.shape[2],var.shape[3]))

for TIM in np.arange(var.shape[0]):
    for IDX, VAL in np.ndenumerate(var[0][0]):
        new_val = np.interp(new_height,surface[TIM,:,IDX[0],IDX[1]],var[TIM,:,IDX[0],IDX[1]],left=np.nan, right=np.nan)
        new_surf[TIM,:,IDX[0],IDX[1]]=new_val[:]
return new_surf
相关问题