简体   繁体   English

如何通过 xarray 在 NetCDF4 中求和

[英]How to make a sum in NetCDF4 by xarray

I would like to make a monthly sums of my NetCDF4 file from daily values for precipitation.我想从每日降水值中每月汇总我的 NetCDF4 文件。 However, I am quite not sure what I am doing wrong.但是,我不确定我做错了什么。 It seems that something has changed from the code in this post: Sum a daily time series into a monthly time series with a NaN value threshold这篇文章中的代码似乎发生了一些变化: Sum a daily time series into a Monthly time series with a NaN value threshold

I didn't find anything helpful in the library documentation.我在图书馆文档中没有发现任何有用的东西。

Here is my code:这是我的代码:

import netCDF4
from netCDF4 import Dataset
import numpy as np
import xarray as xr
import pandas as pd

data = xr.open_dataset('C3S_concat_cropped.nc')
# or I can use 
data2 = Dataset("C3S_concat_cropped.nc", "r", format="NETCDF4")
print(data)
Out:
<xarray.Dataset>
Dimensions:             (lat: 115, lon: 140, time: 15157)
Coordinates:
  * lat                 (lat) float64 -7.4 -7.5 -7.6 -7.7 ... -18.6 -18.7 -18.8
  * lon                 (lon) float64 21.1 21.2 21.3 21.4 ... 34.8 34.9 35.0
  * time                (time) datetime64[ns] 1979-01-01 ... 2020-06-30
Data variables:
    Precipitation_Flux  (time, lat, lon) float32 ...

daily_dataset = xr.Dataset({'Precipitation_Flux': (['time', 'lat', 'lon'],
data['Precipitation_Flux'][:, :, :])}, coords={'lat': (data['lat'][:]), 
'lon': (data['lon'][:]), 'time': pd.date_range('1979-01-01', periods=15157)})

monthly_dataset = daily_dataset['Precipitation_Flux'].resample(indexer='M', time="1D", 
skipna=False).sum()

My ValueError:我的价值错误:

ValueError: the first argument to .resample must be a dictionary

You have to use .groupby instead of .resample .您必须使用.groupby而不是.resample

This should work:这应该有效:

monthly_dataset =daily_dataset['Precipitation_Flux'].groupby('time.month').sum('time')

You first group the data by months and then you have to take the sum along the time axis.您首先按月份对数据进行分组,然后您必须沿时间轴取总和。

I founded that this command works for me!我发现这个命令对我有用!

monthly_dataset = daily_dataset['Precipitation_Flux'].resample(time ='M', 
skipna=False).sum()

However, the documentation of xarray.Dataset.resample can be quite confusing, as the first argument of the function - indexer is not typically written: So be aware of that!但是,xarray.Dataset.resample 的文档可能非常令人困惑,因为 function 的第一个参数 - 通常不编写索引器:所以请注意! :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM