【问题标题】:Xarrays and calculate for each grid point of WRF output with a conditionXarrays 并根据条件计算 WRF 输出的每个网格点
【发布时间】:2021-10-16 04:58:28
【问题描述】:

我有 10 年的 WRF 气候模型输出。我正在寻找一个有效的代码,它对于 xarray 中的每个网格点只选择 T>0 超过 2 天的天数。对于我的情节,我希望每个月在每个网格点的总天数中 T>2 超过 2 天。

我是 xarrays 的新手,并且正在查看类似的问题,但我仍然找不到合适的循环或计数函数来应用每个网格点和月份!非常感谢有关此代码的任何帮助。

这是我当前的代码:

import xarray as xr
import pandas as pd
import matplotlib.pyplot as plt
import netCDF4
from netCDF4 import Dataset
import numpy as np

#concatenate the 10year output
dataset=xr.open_mfdataset("\Python files for plotting wrfoutput\era5_1990-2000_output\*.nc",concat_dim='Time', combine='nested', compat='no_conflicts', preprocess=None, engine=None, data_vars='all', coords='all', parallel=False, join='outer', attrs_file=None,)

#dimensions are: Time, south_north, west_east
DS=dataset
DS = DS.assign_coords(Time=pd.to_datetime(DS['Time'].values))

#Select/extract only the mean 2m surface temperature (T2) from the large xarray 
DST2=DS.T2

#apply the where function to check at which grid points in each month the T2>0
T2threshold=DST2.groupby('Time.month').where(DST2>0)

【问题讨论】:

    标签: python pandas python-xarray weather


    【解决方案1】:

    一般来说,如果没有生成您正在运行的问题的代码,就很难为您提供支持。

    Stackoverflow 不能帮助您学习编程。它可以帮助找到边缘情况和问题的解决方案。

    没关系,这里有一些想法给你。 xarray 的工作方式与 pandas 类似。因此,如果您可以找到 pandas 的解决方案,请尝试使用 xarray。

    ds['threshold_mask'] = ds.T2.where(dataset.T2>0)
    

    构建掩码,然后使用 groupby 和 cumsum:

    ds.groupby((ds['threshold_mask'] == 0).cumsum().threshold_mask).cumsum()
    

    不承认这是可行的,但我想它会帮助您找到正确的解决方案。

    在这里看到:Pandas : dataframe cumsum , reset if other column is false

    【讨论】:

      猜你喜欢
      • 2018-04-05
      • 1970-01-01
      • 2014-10-01
      • 2018-12-13
      • 2021-10-24
      • 2022-10-14
      • 2021-07-30
      • 2020-09-21
      • 2021-01-02
      相关资源
      最近更新 更多