spokati
spokati

Reputation: 55

Xarray for-loop to collect/average data from single month

So I've got an xarray dataset, the description of which is below. Basically, it contains a land-sea mask, bathymetry data, and sea surface temperature (SST) data, all of which have been filtered over a certain latitude x longitude for a 40-year period. I am trying to access the SST data on a monthly basis (to average). Eventually I would like to do seasonal (3 months) as well.

I was thinking a for-loop with an if statement and appending to an array - ie if the 'time' value in the 'sst' variable contain '-04-' it would indicate April (or whichever month I am after). I'm still learning xarray as I go, so if anyone has any ideas I would appreciate some insight.

<xarray.Dataset>
Dimensions:  (TIME: 1, lat: 28, lon: 68, time: 14522)
Coordinates:
  * time     (time) datetime64[ns] 1981-09-01 1981-09-02 ... 2021-06-04
  * lon      (lon) float32 262.1 262.4 262.6 262.9 ... 278.1 278.4 278.6 278.9
  * lat      (lat) float32 24.12 24.38 24.62 24.88 ... 30.12 30.38 30.62 30.88
  * TIME     (TIME) object -001-01-01 00:00:00
Data variables:
    lsmask   (time, lat, lon, TIME) float32 nan 1.0 1.0 nan ... nan nan nan nan
    B_BATHY  (TIME, lat, lon, time) float32 nan nan nan nan ... nan nan nan nan
    sst      (time, lat, lon, TIME) float32 nan 28.23 28.16 nan ... nan nan nan
(attributes excluded because they are not necessary)

TLDR; I am trying to access data from certain months for every year in this 40-year dataset (to average). I figure a for-loop or function could be of use, but I'm still learning the nuances of xarray.

Upvotes: 0

Views: 485

Answers (1)

Val
Val

Reputation: 7023

You can use the datetime accessor of the time dimension together with numpy.isin to select for specific months.

Here's an example:

import xarray as xr
import numpy as np

x = xr.tutorial.open_dataset("air_temperature")

# months we want to select
target_months = [1, 2, 3, 4]

x_sel = x.sel(time=np.isin(x.time.dt.month, target_months))

Checking the unique months after selection gives the expected result:

print(np.unique(x_sel.time.dt.month))

#array([1, 2, 3, 4])

Upvotes: 1

Related Questions