简体   繁体   中英

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. 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? 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.

Make another slice and use xarray.concat to combine the two subsets, like:

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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