【问题标题】:How to extract coordinates from where the variable meets a criterion for a netCDF dataset with Python and xarray?如何使用 Python 和 xarray 从变量满足 netCDF 数据集标准的位置提取坐标?
【发布时间】:2019-06-23 06:39:44
【问题描述】:

我有一个 xarray DataArray 对象da_criteria_daily,它是从 netCDF 文件生成的。

<xarray.DataArray (time: 365, latitude: 106, longitude: 193)>
dask.array<shape=(365, 106, 193), dtype=bool, chunksize=(1, 106, 193)>
Coordinates:
  * time       (time) datetime64[ns] 2017-01-01 2017-01-02 ... 2017-12-31
  * latitude   (latitude) float32 -39.2 -39.149525 ... -33.950478 -33.9
  * longitude  (longitude) float32 140.8 140.84792 140.89584 ... 149.95209 150.0

这是一个地理范围内一年多的每日数据。变量是布尔类型。

我想获取变量为 True 的特定日期的所有坐标(纬度、经度)值。

new_da = da_criteria_daily.where(da_criteria_daily==True, drop=True)
print(new_da)

我明白了:

<xarray.DataArray (time: 161, latitude: 106, longitude: 193)>
dask.array<shape=(161, 106, 193), dtype=float64, chunksize=(1, 106, 193)>
Coordinates:
  * time       (time) datetime64[ns] 2017-01-01 2017-01-02 ... 2017-12-31
  * latitude   (latitude) float32 -39.2 -39.149525 ... -33.950478 -33.9
  * longitude  (longitude) float32 140.8 140.84792 140.89584 ... 149.95209 150.0

您是否有任何带有 xarray.DataArray 的函数可用于从变量中检索坐标?

【问题讨论】:

  • 您应该展示您尝试过的内容和无效的内容。就目前而言,您是在要求其他人为您工作。
  • @jhamman。谢谢,我已尝试添加尽可能多的内容。但我不知道从这里往哪里走。

标签: python pandas netcdf python-xarray


【解决方案1】:

你可以用 numpy 这样做:

numpy.argwhere(numpy.array(da_criteria_daily))

numpy.argwhere() 为您提供非零 (True) 元素的索引。然后您可以使用经度、纬度和时间坐标数组来获取实际值。

使用xarray的.where()只会用nans替换不满足条件的值,也就是说你的False值现在会是nans。我不知道 xarray 的任何方法来获取某些值的坐标。它肯定会很有用,但我想还没有人开发过它。

【讨论】:

    猜你喜欢
    • 2020-10-08
    • 2021-12-12
    • 1970-01-01
    • 2016-06-08
    • 2020-04-27
    • 2021-04-13
    • 2018-01-10
    • 2017-09-25
    • 2022-01-06
    相关资源
    最近更新 更多