where-clause - 基于多个条件的 Xarray 掩码区域
问题描述
我正在查看一个全局 netcdf 文件。我想将 60-75 度 N 波段内的所有陆地点设置为零,但将该波段中的海洋点保持为 nan。作为第二步,我想将土地点的值保持在 60-75 之间,但将所有其他土地点设置为零。海洋值是 NaN。我只是没有让我的 xarray 脚本做到这一点 - 这是我尝试过的
import numpy as np
import matplotlib.pyplot as plt
ds = xr.open_dataset('ifle.nc')
ds['Shrub_total'] = ds['Shrub']
shrub_total = ds.Shrub_total
tundra = shrub_total.where((shrub_total!=np.nan)&(shrub_total.Lat>60)&
(shrub_total.Lat<75), 0)
shrub = shrub_total.where((shrub_total!=np.nan)&(shrub_total.Lat<60)&
(shrub_total.Lat>75), 0)
ds['Tundra'] = tundra
ds['Shrub'] = shrub
fig, axes = plt.subplots(ncols=2,figsize=(12,3))
ds['Shrub_total'].isel(Time=0).plot(ax=axes[0])
ds['Tundra'].isel(Time=0).plot(ax=axes[1])
ds['Shrub'].isel(Time=0).plot(ax=axes[2])
plt.show()
这就是它的样子
左边的面板是原始数据,对于中间的数据,至少我设法保留了我想要的数据 - 但不是两个巨大的紫色块,我想保留地图,将所选区域之外的所有值设置为零。右边的面板本来打算成为中间面板的“反面”,但我在那里完全失败了。感觉这应该是一件容易的事,但我就是想不通!
解决方案
这似乎主要是逻辑方面的问题,以及用于处理 NaN 的方法。
以下似乎对我有用:
tundra = shrub_total.where((np.isnan(shrub_total)==True)|
((shrub_total.Lat>60)&(shrub_total.Lat<75)), 0)
shrub = shrub_total.where((np.isnan(shrub_total)==True)|
((shrub_total.Lat<60)|(shrub_total.Lat>75)), 0)
- 我将
shrub
逻辑更改为 OR 语句(我们想要屏蔽小于 60 或大于 75 - 某处不可能两者兼有!)。 - 我们使用 np.isnan()==True 而不是 ()!=np.nan。我不确定为什么我们不能像你那样对待这个......这需要进一步改变逻辑。
请注意,我不使用 python,所以这可能非常 hacky,我相信其他人会有一个更优雅和知识渊博的答案,但它引起了我的兴趣,所以我尝试了 :)
推荐阅读
- c++ - 有没有办法在打开应用程序的 VS 中将批处理文件作为构建后事件运行?
- ionic-framework - 将视频显示为闪屏 ionic 3
- python - 在 while 语句中使用 Python 等效的 &&(逻辑与)时出现“SyntaxError:无效语法”
- php - 在 PHP PDO FetchAll FETCH_CLASS 中找不到类
- javascript - 动态过滤字符串列表
- javascript - 如何使用 JQuery/Javascript 控制 IE 11 的每个鼠标槽口滚动
- three.js - THREE.js 正交相机位置在使用 OrbitControl 缩放后未更新
- postgresql - PostgreSQL 中有没有办法找到所有使用外部数据包装器的视图?
- c# - Firestore C#:权限缺失或不足
- javascript - 为什么这个函数只在 IE11 上需要分号?