简体   繁体   English

如何使用 python 从 netcdf 数据集中找到气候月平均值?

[英]How to find climatological monthly mean from a netcdf data set using python?

I have a netcdf data set of (monthly timexlatitudexlongitude).我有一个 netcdf 数据集(每月时间x纬度x经度)。 No of month is 216, so total 18 years.月数是 216,所以总共 18 年。 1st index represent the january data, 2nd index represent the february data... 12th index represent december data and again 13th index represent january data, 14th index represent february data so on and so forth.第一个索引代表 1 月数据,第 2 个索引代表 2 月数据……第 12 个索引代表 12 月数据,第 13 个索引代表 1 月数据,第 14 个索引代表 2 月数据,依此类推。 Now I want to find mean of all january data, mean of all february data... mean of all december data.现在我想找到所有 1 月数据的平均值,所有 2 月数据的平均值......所有 12 月数据的平均值。 So after doing this operation the size of the array will be reduce to 12xlatxlon(previously it was 216xlatxlon).因此,在执行此操作后,数组的大小将减少到 12xlatxlon(以前是 216xlatxlon)。 How can I do that?我怎样才能做到这一点?

If you are working on linux or macOS, you can do this in a couple of lines with my nctoolkit package ( https://nctoolkit.readthedocs.io/en/latest/ ):如果您正在使用 linux 或 macOS,您可以使用我的 nctoolkit package ( Z5E056C500A1C4B6A7110B50D807BADE .

import nctoolkit as nc

ds = nc.open_data("infile.nc")
# get the mean using month as the averaging period
ds.tmean("month")
# plot to visualize
ds.plot()

You can use the very convenient resample() option from xarray / pandas .您可以使用xarray / pandas中非常方便的resample()选项。

import xarray as xr 

ds = xr.open_dataset('file.nc')
ds_monthly_mean = ds.resample(time="1MS").mean()

print(ds.time.shape)
(5619,)

print(ds_monthly_mean.time.shape)
(12,)

Check out the offset aliases if you don't know what resampling option to use.如果您不知道要使用什么重采样选项,请查看偏移别名 After defining the dimension over which to resample the data, just select your resampling method (mean, sum, etc....)在定义了重新采样数据的维度之后,只需 select 您的重采样方法(平均值、总和等....)

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

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