gpt4 book ai didi

python获取最大值xarray的月份

转载 作者:行者123 更新时间:2023-12-04 21:02:32 28 4
gpt4 key购买 nike

如何获得最大径流的月份
我想获得每年以及整个时间序列的最大径流月份。这个想法是通过查看最大径流月份来表征全局季节性。然后我想尝试考虑每个像素是否具有单峰或双峰机制。
我想创建一张类似于 Pangeo 示例中的 map here .
Example image
这显示的是最大降水的小时数。我想显示最大径流的月份(作为整数)。
获取数据
我在这里下载GRUN runoff data并创建一个 xarray 对象。
注意:这里的数据集>1GB。我正在使用它来使这个示例完全可重现。

# get the data
import subprocess
command = """
wget -O grun.nc https://www.research-collection.ethz.ch/bitstream/handle/20.500.11850/324386/GRUN_v1_GSWP3_WGS84_05_1902_2014.nc?sequence=1&isAllowed=y
"""
import os
if not os.path.exists('grun.nc'):
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output, error = process.communicate()

# read the data
import xarray as xr
ds = xr.open_dataset('grun.nc')

# select a subset so we can work with it more quickly
ds = ds.isel(time=slice(-100,-1))
ds

Out[]:
<xarray.Dataset>
Dimensions: (lat: 360, lon: 720, time: 99)
Coordinates:
* lon (lon) float64 -179.8 -179.2 -178.8 -178.2 ... 178.8 179.2 179.8
* lat (lat) float64 -89.75 -89.25 -88.75 -88.25 ... 88.75 89.25 89.75
* time (time) datetime64[ns] 2006-09-01 2006-10-01 ... 2014-11-01
Data variables:
Runoff (time, lat, lon) float32 ...
Attributes:
title: GRUN
version: GRUN 1.0
meteorological_forcing: GSWP3
temporal_resolution: monthly
spatial_resolution: 0.5x0.5
crs: WGS84
proj4: +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs
EPSG: 4326
references: Ghiggi et al.,2019. GRUN: An observation-based g...
authors: Gionata Ghiggi; Lukas Gudmundsson
contacts: gionata.ghiggi@gmail.com; lukas.gudmundsson@env....
institution: Land-Climate Dynamics, Institute for Atmospheric...
institution_id: IAC ETHZ

我试过的
我有 nan 值,所以我不能只应用 argmax()到数据集。我使用与@jhamman 相同的方法 here结合上面的Pangeo示例。我不完全确定这给了我什么,但它似乎给了我
# Apply argmax where you have NAN values
def my_func(ds, dim=None):
return ds.isel(**{dim: ds['Runoff'].argmax(dim)})

mask = ds['Runoff'].isel(time=0).notnull() # determine where you have valid data
ds2 = ds.fillna(-9999) # fill nans with a missing flag of some kind
new = ds2.reset_coords(drop=True).groupby('time.month').apply(my_func, dim='time').where(mask) # do the groupby operation/reduction and reapply the mask
new

Out[]:
<xarray.Dataset>
Dimensions: (lat: 360, lon: 720, month: 12)
Coordinates:
* lon (lon) float64 -179.8 -179.2 -178.8 -178.2 ... 178.8 179.2 179.8
* lat (lat) float64 -89.75 -89.25 -88.75 -88.25 ... 88.75 89.25 89.75
* month (month) int64 1 2 3 4 5 6 7 8 9 10 11 12
Data variables:
Runoff (month, lat, lon) float32 nan nan nan nan nan ... nan nan nan nan
Attributes:
title: GRUN
version: GRUN 1.0
meteorological_forcing: GSWP3
temporal_resolution: monthly
spatial_resolution: 0.5x0.5
crs: WGS84
proj4: +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs
EPSG: 4326
references: Ghiggi et al.,2019. GRUN: An observation-based g...
authors: Gionata Ghiggi; Lukas Gudmundsson
contacts: gionata.ghiggi@gmail.com; lukas.gudmundsson@env....
institution: Land-Climate Dynamics, Institute for Atmospheric...
institution_id: IAC ETHZ

这给了我
import matplotlib.pyplot as plt
fig,ax = plt.subplots(figsize=(12,8))
new.Runoff.sel(month=10).plot(ax=ax, cmap='twilight')
My output currently
理想输出
我想要的是每个像素的值是最大径流的月份。
很高兴转换为 pandas如有必要。
所以我最终会得到一个 xr.Dataset,其中包含最大径流月份的整数。理想情况下,随着时间的推移,最大径流的月份也很好,这样我也可以看到这种季节性变化的方式。
<xarray.Dataset>
Dimensions: (lat: 360, lon: 720)
Coordinates:
* lon (lon) float64 -179.8 -179.2 -178.8 -178.2 ... 178.8 179.2 179.8
* lat (lat) float64 -89.75 -89.25 -88.75 -88.25 ... 88.75 89.25 89.75
Data variables:
Month_of_max (lat, lon) int32 ...

# OR EVEN BETTER
<xarray.Dataset>
Dimensions: (lat: 360, lon: 720, Year: 10)
Coordinates:
* lon (lon) float64 -179.8 -179.2 -178.8 -178.2 ... 178.8 179.2 179.8
* lat (lat) float64 -89.75 -89.25 -88.75 -88.25 ... 88.75 89.25 89.75
* year (year) float64 2010 2011 2012 2013 ...
Data variables:
Month_of_max (lat, lon, year) int32 ...

最佳答案

I have nan values so I can't just apply an argmax() to the dataset.



的确。

考虑使用 .fillna(0) 在应用 argmax 之前。
(或者也许 .dropna()。)

关于python获取最大值xarray的月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55995471/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com