how to combine 4D xarray data

162 Views Asked by At

I have a 4D xarray which contains time, lev, lat, and lon. The data is for specific day so that the length of time is 1. My goal is to use 4D xarray with same attributess but include a month data so that the time length will be 30.

I try to google it but cannot find useful information. I appreciate it if anyone can provide some insights.

enter image description here

1

There are 1 best solutions below

0
Michael Delgado On

If you have multiple points in a time series, you can use xr.DataArray.resample to change the frequency of a datetime dimension. Once you have resampled, you'll get a DataArrayResample object, to which you can apply any of the methods listed in the DataArrayResample API docs.

If you only have a single point in time, you can't resample to a higher frequency. Your best bet is probably to simply select and drop the time dim altogether, then use expand_dims to expand the dimensions again to include the full time dim you want. Just be careful because this overwrites the time dimension's values with whatever you want, regardless of what was in there before:

target_dates = pd.date_range('2018-08-01', '2018-08-30', freq='D')

daily = (
    da
    .isel(time=0, drop=True)
    .expand_dims(time=target_dates)
)