如何使用Xarray查找每日栅格数据的百分位数?

发布于 2025-01-21 03:55:25 字数 1034 浏览 2 评论 0原文

我有一个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:
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2025-01-28 03:55:25

在应用百分位数计算之前,我需要使用groupby。我创建了一个新坐标,因为我有闰年,无法使用 dayofyear

#import libraries
import pandas as pd
import json
import numpy as np
import matplotlib.pyplot as plt
import xarray as xr
import requests
from datetime import date
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.patches as patches
import datetime as dt

#open NASA GISS gridded temperature netcdf file
df = xr.open_dataset('Tmax_1951-1960.nc')

#select temperature dataset
air=df.tmax

#Create baseline period
Baseline=air.loc[air.time <= np.datetime64('1955-01-01')]

#create new monthday coordinate
monthday = xr.DataArray(Baseline.time.dt.month*100+Baseline.time.dt.day,name='monthday', dims='time', coords={'time':Baseline['time']})
Baseline['monthday'] = monthday

#Find 90th percentile of daily data
Per90 = Baseline.groupby('monthday').quantile(0.9)

I needed to use groupby before applying the percentile calculation. I created a new coordinate because I had leap years and couldn't use dayofyear.

#import libraries
import pandas as pd
import json
import numpy as np
import matplotlib.pyplot as plt
import xarray as xr
import requests
from datetime import date
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.patches as patches
import datetime as dt

#open NASA GISS gridded temperature netcdf file
df = xr.open_dataset('Tmax_1951-1960.nc')

#select temperature dataset
air=df.tmax

#Create baseline period
Baseline=air.loc[air.time <= np.datetime64('1955-01-01')]

#create new monthday coordinate
monthday = xr.DataArray(Baseline.time.dt.month*100+Baseline.time.dt.day,name='monthday', dims='time', coords={'time':Baseline['time']})
Baseline['monthday'] = monthday

#Find 90th percentile of daily data
Per90 = Baseline.groupby('monthday').quantile(0.9)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文