I have an SST climatology(lat,lon,time) where the time dimension is 12 months. I would like to copy the climatology so I have a dataset of say 100 years in which the SST data is simply the climatology repeating every year.
How do I do this? It seems expand_dims only works for new dimensions, not to copy existing dimensions.
Thanks
Ulla
>Solution :
if I’m understanding correctly, you’d essentially like a new dataset with dimensions (lat, lon, time) where time has length 1200?
This is a great use case for xarray’s advanced indexing using a DataArray as an indexer. If you select data using a DataArray as an argument to .sel or .isel, the data will be reshaped using the values of the DataArray as the indexer but the index of the DataArray in the result.
In this case, you’d like to reindex the data to have repeating values every month. First, we should rename your time dimension to be month so we can create a new dimension time in the result:
ds = ds.rename({'time': 'month'})
I’m assuming here that your dataset’s time dimension is simply integers from 1..12. In that case, you can use the .month attribute of a datetime array:
# create a monthly time series
date_range = xr.cftime_range("2000-01-01", "2099-12-31", freq="MS")
# access the month index (1, 2, ..., 12) for each date:
months = date_range.month
# create a DataArray from the above. The coordinate is the full date range
# and will become the time dim in the result. The values are the months
# that we would like to select from the climatology
selector = xr.DataArray(months, dims=["time"], coords=[date_range])
# select from the climatology, reshaping to be indexed by `time`
time_series = ds.sel(month=selector)