在Python xarray中将10年以上的季节性数据升级为每日数据 [英] Upsample seasonal data to daily data over 10 years in Python xarray
问题描述
我有一个netCDF文件来获取季节性数据.加载到数据集后,它包含season
,latitude
和longitude
维度.
I have a netCDF file for seasonal data. When loaded into Dataset, it contains season
, latitude
and longitude
dimensions.
print(dataset_seasonal_nc)
<xarray.Dataset>
Dimensions: (latitude: 106, longitude: 193, season: 4)
Coordinates:
* latitude (latitude) float32 -39.2 -39.149525 ... -33.9
* longitude (longitude) float32 140.8 140.84792 ... 150.0
* season (season) object 'DJF' 'JJA' 'MAM' 'SON'
Data variables:
FFDI 95TH PERCENTILE (season, latitude, longitude) float64 dask.array<shape=(4, 106, 193), chunksize=(4, 106, 193)>
我需要将seasnonal数据上采样为10年的每日数据(例如,从1972年到1981年,总共3653天).这意味着上采样的数据集对象应为:
I need to upsample the seasnonal data to daily data for 10 years (for example from 1972 to 1981, 3653 days in total). This means the upsampled Dataset object should be:
<xarray.Dataset>
Dimensions: (latitude: 106, longitude: 193, time: 3653)
Coordinates:
* latitude (latitude) float32 -39.2 -39.149525 ... -33.950478 -33.9
* longitude (longitude) float32 140.8 140.84792 140.89584 ... 149.95209 150.0
* time (time) datetime64[ns] 1972-01-01T00:00:00 1972-01-02T00:00:00 1972-01-03T00:00:00 ... 1981-12-30T00:00:00 1981-12-31T00:00:00
Data variables:
FFDI 95TH PERCENTILE (time, latitude, longitude) float64 dask.array<shape=(3653, 106, 193), chunksize=(3653, 106, 193)>
一天的变量应与该天的季节的变量相同.这意味着1972-01-01、1972-02-02和1972-02-28的值应与DJF
的季节;和1972-04-01、1972-05-02和1972-05-31应该具有与MAM
季节相同的值.
The variable for a day should be the same as the variable for the season that the day falls in. This means, 1972-01-01, 1972-02-02 and 1972-02-28 should have the same value as the season DJF
has; and 1972-04-01, 1972-05-02 and 1972-05-31 should have the same value as the season MAM
has.
我试图使用数据集的resample
函数:
I was trying to use the Dataset's resample
function:
upsampled = dataset_seasonal_nc.resample(time='D').ffill()
但这给了我以下错误:
...\venv\lib\site-packages\xarray\core\dataset.py", line 896, in _construct_dataarray
variable = self._variables[name]
KeyError: 'time'
推荐答案
This seems like a good candidate for xarray's advanced label-based indexing. I think something like the following should work:
import pandas as pd
times = pd.date_range('1972', '1982', freq='D', closed='left')
time = xr.DataArray(times, [('time', times)])
upsampled = dataset_seasonal_nc.sel(season=time.dt.season)
此处time.dt.season
是一个DataArray,表示与上采样的数据集中的每次相关的季节标签:
Here time.dt.season
is a DataArray representing the season labels associated with each time in your upsampled Dataset:
In [16]: time.dt.season
Out[16]:
<xarray.DataArray 'season' (time: 3653)>
array(['DJF', 'DJF', 'DJF', ..., 'DJF', 'DJF', 'DJF'],
dtype='|S3')
Coordinates:
* time (time) datetime64[ns] 1972-01-01 1972-01-02 1972-01-03 ...
这篇关于在Python xarray中将10年以上的季节性数据升级为每日数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!