【问题标题】:Check if Lat/Lon point is over land or ocean检查纬度/经度点是否在陆地或海洋上
【发布时间】:2019-09-26 19:17:16
【问题描述】:

我有一组纬度 (lats, min=-88, max=88, shape=89) 和经度 (lons, min=0, max=358, shape=180),还有一个陆地掩膜 (land_mask,海洋=1,陆地=0,形状=(89,180))。

xlon,xlat = np.meshgrid(lons,lats)
PP.pcolormesh(xlon,xlat,land_mask)
PP.colorbar()
PP.show()

我想遍历所有的纬度和经度,并计算那些在海洋上的纬度/经度对,并且什么也不做,即如果在陆地上移动到下一个纬度/经度对。一些伪代码:

# loop over lats
for lat in lats:
    # loop over lons
    for lon in lons:
        # check to see if the current 
        # lat/lon is in the ocean.
        # if True, do something
        if (lat,lon) in ocean:
            do something
        # else if current lat/lon
        # over land, do nothing and
        # move to the next lat/lon
        # pair
        else: # ie if over land, skip this point
            continue

我不确定如何使用我拥有的 2d 大地掩模来做到这一点。另外,也许有更好的方法来实现比嵌套 for 循环更快和/或更多 Pythonic 的代码?提前致谢。

【问题讨论】:

    标签: python numpy multidimensional-array logical-operators


    【解决方案1】:

    你可以试试这个:

    nlats, nlons = land_mask.shape
    for i in range(nlons):
       for j in range(nlats):
           if land_mask(j,i) == 1:
               do something
    

    但这在 python 中会很慢。通过用向量化替换循环可以使 Numpy 操作更快。所以,如果你告诉我们do something 部分应该做什么,可能会有更好的方法来做到这一点。

    【讨论】:

      【解决方案2】:

      我想像下面这样的想法会奏效。

      import numpy
      a = numpy.arange(9).reshape(3, 3)
      # array([[0, 1, 2],
      #        [3, 4, 5],
      #        [6, 7, 8]])
      b = a > 4
      # array([[False, False, False],
      #        [False, False,  True],
      #        [ True,  True,  True]])
      c = numpy.zeros(a.shape)
      # array([[0., 0., 0.],
      #        [0., 0., 0.],
      #        [0., 0., 0.]])
      c[~b] = 1
      # array([[1., 1., 1.],
      #        [1., 1., 0.],
      #        [0., 0., 0.]])
      

      这意味着您可以使用您的掩码仅修改具有相同形状的数组的特定条目。

      【讨论】:

        猜你喜欢
        • 2015-02-26
        • 2014-06-21
        • 2018-06-02
        • 1970-01-01
        • 1970-01-01
        • 2011-12-13
        • 1970-01-01
        • 2019-11-01
        • 1970-01-01
        相关资源
        最近更新 更多