python - python获取最大值xarray的月份
问题描述
如何获得最大径流月份
我想获得每年最大径流的月份,以及整个时间序列。这个想法是通过查看最大径流的月份来表征全球季节性。然后我想尝试考虑每个像素是否具有单峰或双峰状态。
我想在这里创建一个像 Pangeo 示例中的地图。
这显示的是最大降水的时间。我想显示最大径流的 MONTH(作为整数)。
获取数据
在这里,我下载了GRUN 径流数据并创建了一个 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 值,所以我不能只将 anargmax()
应用于数据集。我在这里使用与@jhamman 相同的方法,并结合上面的 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')
理想输出
我想要的是每个像素的值是最大径流的月份。
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 ...