简体   繁体   English

在多个日期范围内对 netcdf 文件进行时间切片

[英]Time slice a netcdf file over multiple date ranges

I have a 3 dimensional netcdf file (time, lat, lon) in which I would like to time slice over multiple date ranges.我有一个 3 维 netcdf 文件(时间、纬度、经度),我想在其中对多个日期范围进行时间切片。 Slicing over one continuous range is doable, like this.像这样在一个连续范围内切片是可行的。

start_date = dt.datetime(2017,6,1,0)
end_date = dt.datetime(2017,8,31,0)

yyyy = dt.datetime.strftime(start_date,'%Y')
data_g = xr.open_dataset("/cfsr/data/"+yyyy+"/g."+yyyy+".0p5.anl.nc")

g = data_g['g'].isel(lev=15)
g = g.sel(time=slice(start_date1,end_date1))

This keeps my netcdf file three dimensional, but now only includes data from June of 2017 through August of 2017. Now, for example, say I wanted to include from October of 2017 through November of 2017. Is this something that is possible?这使我的 netcdf 文件保持三维,但现在只包含 2017 年 6 月到 2017 年 8 月的数据。现在,例如,假设我想包含 2017 年 10 月到 2017 年 11 月的数据。这可能吗? The end goal is to take the mean of all the data which would span different time slices over different years.最终目标是取所有数据的平均值,这些数据将跨越不同年份的不同时间片。 I know I could do this manually by creating a bunch of individual arrays (g1, g2, etc..) but I figured there might be an easier way to do it.我知道我可以通过创建一堆单独的 arrays(g1、g2 等)来手动执行此操作,但我认为可能有更简单的方法来执行此操作。

Make another slice and use xarray.concat to combine the two subsets, like:制作另一个切片并使用xarray.concat组合两个子集,例如:

import xarray as xr

ds = xr.open_dataset('https://thredds.ucar.edu/thredds/dodsC/grib/NCEP/GFS/Global_onedeg/Best')
temp_var = ds.Temperature_isobaric.sel(isobaric6=100000)

g1 = temp_var.sel(time=slice('2020-04-28', '2020-04-30'))
g2 = temp_var.sel(time=slice('2020-05-06', '2020-05-07'))
combined = xr.concat([g1, g2], dim='time')

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

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