如何使用Xarray查找每日栅格数据的百分位数?
我有一个NETCDF文件,其中有10年的美国每日温度数据数据。我创建了一个仅限数据的基线期。现在,我想使用每个网格点的所有5年数据(即1月1日,1月2日,1月3日,1月3日的第90个百分点等)在该基线周期的每一天找到第90个百分点。我尝试应用分位数功能,但不要认为我正确使用它。
这就是我的代码的样子:
#import libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import xarray as xr
import requests
from datetime import date
#open NOAA gridded temperature netcdf file
df = xr.open_dataset('Tmax_1951-1960.nc')
#pull out maximum temperature variable
air=df.tmax
#select years up to and including 1955 for baseline period
Baseline=air[(air.time.dt.year <= 1955)]
#create year and day coordinates
Baseline['year']=Baseline.time.dt.year
Baseline['day']=Baseline.time.dt.strftime('%m-%d')
#calculate percentiles
Baseline['Percentile_90']=Baseline.quantile(0.9, dim='day')
但是我得到错误“ valueRor:数据集不包含尺寸:['Day']”。我如何在每个网格点为每个日历日找到第90个百分点?
I have a netcdf file of 10 years of gridded daily temperature data for the United States. I created a baseline period of just the first 5 years of data. I now want to find the 90th percentile for each day of that baseline period using all 5 years of data for each grid point (i.e. the 90th percentile of Jan 1, Jan 2, Jan 3, etc for every grid point). I tried applying the quantile function but don't think I'm using it correctly.
Here's what my dataset looks like:
and here's what my code looks like:
#import libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import xarray as xr
import requests
from datetime import date
#open NOAA gridded temperature netcdf file
df = xr.open_dataset('Tmax_1951-1960.nc')
#pull out maximum temperature variable
air=df.tmax
#select years up to and including 1955 for baseline period
Baseline=air[(air.time.dt.year <= 1955)]
#create year and day coordinates
Baseline['year']=Baseline.time.dt.year
Baseline['day']=Baseline.time.dt.strftime('%m-%d')
#calculate percentiles
Baseline['Percentile_90']=Baseline.quantile(0.9, dim='day')
But I get the error "ValueError: Dataset does not contain the dimensions: ['day']". How can I find the 90th percentile for each calendar day for each grid point?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在应用百分位数计算之前,我需要使用
groupby
。我创建了一个新坐标,因为我有闰年,无法使用dayofyear
。I needed to use
groupby
before applying the percentile calculation. I created a new coordinate because I had leap years and couldn't usedayofyear
.