python - 根据纬度和经度过滤数据 - Numpy

标签 python python-2.7 numpy latitude-longitude slice

我有一个数据集here它是特定的 latitudelongitude

import numpy as np

f = open('bt_20130221_f17_v02_s.bin', 'rb')
data = np.fromfile(f, dtype=np.uint16).reshape(332, 316)
f.close()

raw_lat = open('pss25lats_v3.dat', 'rb')
lats = np.fromfile(raw_lat, dtype='<i4').reshape(332,316) / 100000.
raw_lat.close()

raw_lon = open('pss12lons_v3.dat', 'rb')
lons = np.fromfile(raw_lat, dtype='<i4').reshape(332,316) / 100000.
raw_lon.close()

数据值在此处显示为:

import matplotlib.pyplot as plt

plt.imshow(data)

enter image description here

根据这些值,我希望过滤此数据的常规部分。 如:

north = -59.7183
south = -65.3099
west = -65.743
east = -48.55

mask_lons = np.ma.masked_where(((lons > east) | (lons < west)), lons)
mask_lats = np.ma.masked_where(((lats < south) | (lats > north)), lats)

data_filtered = np.where(((lats == mask_lats) & (lons == mask_lons)),
                    data, 999)

这是结果图像: enter image description here

第一个问题: 如何对该 data_filtered 进行切片以仅获取有效值(即仅包含值!= 999 的矩阵)?

第二个问题: 我如何对纬度和经度做同样的事情?我应该如何仅获取每个变量的非屏蔽值作为单个二维数组? 因为 mask_lons 是:

In [176]: mask_lons
Out[176]: 
masked_array(data =
 [[-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]
 ..., 
 [-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]],
             mask =
 [[ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]
 ..., 
 [ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]],
       fill_value = 1e+20)

最佳答案

我相信你需要这样的东西:

inside = np.logical_and(np.logical_and(lons >= west,
                                       lons <= east),
                        np.logical_and(lats >= south,
                                       lats <= north))

这是完整的笔记本: http://nbviewer.ipython.org/gist/ocefpaf/d609458086e2ad87eb62

关于python - 根据纬度和经度过滤数据 - Numpy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27156046/

相关文章:

python - pip 可以与 Visual Studio 中的 Python 工具一起使用吗?

regex - Grep 在包含 n 个文件的文件夹中查找多个模式。如果找到模式的匹配项,则创建 mkdir

python - 执行复杂功能后创建新的 pandas 数据框

python - 合并三个相同形状的 NumPy 数组

python - FFmpeg python不合并

python - Django 异常和响应代码列表

python - 使用 __name__ 作为属性

python - FiPy "base directory"在哪里?

Python/Numpy : Division gives me an unexpected deprecation-Warning

java - 为什么jython很慢?