0
votes

I am trying to resample a DataArray from hourly to daily outputs by using DataArray.resample() from xarray.

First, I tried the following:

pm25.resample(dim='Time', freq="1D", how='mean')

which gave me:

TypeError: resample() no longer supports the `how` or `dim` arguments. 
Instead call methods on resample objects, e.g., data.resample(time='1D').mean()

So, I tried the following:

pm25_daily = pm25.resample(Time='1D').mean()

But, I get the following error:

Traceback (most recent call last):
 File "/Users/jacob/opt/anaconda3/envs/WRF-1/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3319, in run_code
   exec(code_obj, self.user_global_ns, self.user_ns)
 File "<ipython-input-140-4ba4bc53a1c2>", line 1, in <module>
   pm25.resample(Time='1D').mean(dim='Time')
 File "/Users/jacob/opt/anaconda3/envs/WRF-1/lib/python3.7/site-packages/xarray/core/common.py", line 1039, in resample
   if isinstance(self.indexes[dim_name], CFTimeIndex):
 File "/Users/jacob/opt/anaconda3/envs/WRF-1/lib/python3.7/site-packages/xarray/core/indexes.py", line 36, in __getitem__
   return self._indexes[key]
KeyError: 'Time'

I also tried the following

pm25_daily = pm25.resample(Time='1D').mean(dim='Time)

and I got the same error. Below is what my DataArray looks like along with the dimensions and coordinates.

<xarray.DataArray 'PM2_5_DRY' (Time: 283, bottom_top: 50, south_north: 115, west_east: 115)>
array([[[[1.163326, ..., 1.10779 ],
        ...,
        [1.145038, ..., 1.167824]],
       ...,
       [[0.090804, ..., 0.090798],
        ...,
        [0.090985, ..., 0.091001]]],
       ...,
       [[0.024746, ..., 0.02613 ],
        ...,
        [0.025582, ..., 0.026747]]]], dtype=float32)
Coordinates:
   XLAT     (Time, south_north, west_east) float32 ...
   XLONG    (Time, south_north, west_east) float32 ...
   XTIME    (Time) datetime64[ns] ...
Dimensions without coordinates: Time, bottom_top, south_north, west_east
Attributes:
   FieldType:    104
   MemoryOrder:  XYZ
   description:  pm2.5 aerosol dry mass
   units:        ug m^-3
   stagger:

The dimensions of the DataArray has 'Time' in it. Where am I going wrong?

Thanks!

1

1 Answers

0
votes

Based on my DataArray, the 'Time' dimension is not a dimension coordinate nor a datetime object.

I swapped the XTIME coordinate as my dimension coordinate with the original 'Time' dimension.

pm25 = pm25.swap_dims({'Time': 'XTIME'})

Then everything else worked properly.