![](/img/trans.png)
[英]How do I create a binary mask on a square grid from a 2D cloud of points in Python?
[英]How do I subset a 2D grid from another 2D grid in python?
我已经在连续的美国上对数据进行网格化,并且试图在特定区域内选择其中的一部分。
import numpy as np
from netCDF4 import Dataset
import matplotlib.pyplot as plt
filename = '/Users/me/myfile.nc'
full_data = Dataset(filename,'r')
latitudes = full_data.variables['latitude'][0,:,:]
longitudes = full_data.variables['longitude'][0,:,:]
temperature = full_data.variables['temperature'][0,:,:]
这三个变量都是二维形状的矩阵(337,451)。 我正在尝试执行以下操作,以在特定区域内对数据进行子选择。
index = (latitudes>=44.0)&(latitudes<=45.0)&(longitudes>=-91.0)&(longitudes<=-89.0)
temp_subset = temperature[index]
lat_subset = latitudes[index]
lon_subset = longitudes[index]
我希望所有这三个变量都是二维的,但是相反,它们都返回形状为(102,)的扁平数组。 我尝试了另一种方法:
index2 = np.where((latitudes>=44.0)&(latitudes<=45.0)&(longitudes>=-91.0)&(longitudes<=-89.0))
temp = temperatures[index2[0],:]
temp2 = temp[:,index2[1]]
plt.imshow(temp2,origin='lower')
plt.colobar()
但是我的数据看起来很不正确。 是否有更好的方法从较大的网格中获取2D子集网格?
Edub,
我建议查看numpy的矩阵索引文档,特别是http://docs.scipy.org/doc/numpy-1.10.1/user/basics.indexing.html#other-indexing-options 。 当前,您正在提供两个维度用于建立索引,但没有切片信息(导致仅接收一维结果)。 我希望这证明是有用的!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.