【问题标题】:Filter data, based on Latitude and Longitudes - Numpy根据纬度和经度过滤数据 - Numpy
【发布时间】:2015-01-25 04:30:42
【问题描述】:

我有一个数据集 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)

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

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)

这是生成的图像:

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

第二个问题: 我如何对 lats 和 lons 做同样的事情?我应该如何仅将非屏蔽值作为每个变量的单个 2D 数组获取? 因为 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)

【问题讨论】:

  • 您的帖子链接到 pss12lons 而不是 pss25lons,因此重塑不起作用
  • 对此感到抱歉。我会解决的

标签: python python-2.7 numpy latitude-longitude slice


【解决方案1】:
mask = (((south<=lats) & (lats<=north)) & ((west<=lons) & (lons<=east)))
data = np.ma.masked_where(data, ~mask)

【讨论】:

    【解决方案2】:

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

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 1970-01-01
      • 2012-09-07
      • 1970-01-01
      • 2020-03-23
      • 2015-09-20
      相关资源
      最近更新 更多