Search code examples
pythondataframedatasetnetcdfpython-xarray

Print out specific variables based on time index in xarray.Dataset


I've accessed a netCDF file using nc = xarray.open_dataset(...). The Dataset contains the following:

<xarray.Dataset>
Dimensions:    (latitude: 121, longitude: 261, time: 8760)
Coordinates:
  * longitude  (longitude) float32 -20.0 -19.75 -19.5 -19.25 ... 44.5 44.75 45.0
  * latitude   (latitude) float32 55.0 54.75 54.5 54.25 ... 25.5 25.25 25.0
  * time       (time) datetime64[ns] 1979-01-01 ... 1979-12-31T23:00:00
Data variables:
    u10        (time, latitude, longitude) float32 ...
    v10        (time, latitude, longitude) float32 ...
    msl        (time, latitude, longitude) float32 ...
Attributes:
    Conventions:  CF-1.6
    history:      2020-11-24 08:15:21 GMT by grib_to_netcdf-2.16.0: /opt/ecmw...
    

Using nc.to_dataframe(), I was able to print out a "better looking" format of the data like this:

enter image description here

From here, I want to print out the u10 (and later v10) values just for September, meaning from 1979-01-01 00:00:00 to 1979-10-01 00:00:00. The index of nc is a

MultiIndex([(55.0, -20.0, '1979-01-01 00:00:00'), ... names=['latitude', 'longitude', 'time'], length=276649560)

I'm not sure if the MultiIndex part helps.


Solution

  • I would suggest learning the basics of xarray first. They have some well-written examples on how to get started. For your case, check these examples on how to select data.

    In your case

    t1 = '1979-01-01 00:00:00'
    t2 = '1979-10-01 00:00:00'
    ## select data from t1 till t2
    a = nc.u10.sel(time=slice(t1, t2))
    print(a)