Reputation: 411
I have some dummy data at 0.2 and 1 degree resolution. I would like to subsample foo to the same scale as foo1. Is there any easy way to average and regrid my lat and long coordinates somehow?
import pandas as pd
import xarray as xr
import matplotlib.pyplot as plt
#Set at 0.2 degree grids ish
freq=20
lats=240
lons=1020
time=pd.date_range('2000-01',periods=freq,freq='Y')
data=np.random.rand(freq,lats,lons)
lat=np.linspace(-19.5,19.5,lats)
lon=np.linspace(120,290,lons)
foo = xr.DataArray(data, coords=[time, lat,lon], dims=['time', 'lat','lon'])
foo.sel(time='2005',method='nearest').plot()
plt.show()
#Set at 1 degree grids
freq1=20
lats1=40 #Factor of 6 difference
lons1=170
time1=pd.date_range('2000-01',periods=freq1,freq='Y')
data1=np.random.rand(freq1,lats1,lons1)
lat1=np.linspace(-19.5,19.5,lats1)
lon1=np.linspace(120,290,lons1)
foo1 = xr.DataArray(data1, coords=[time1, lat1,lon1], dims=['time', 'lat','lon'])
foo1.sel(time='2005',method='nearest').plot()
plt.show()
Upvotes: 0
Views: 3653
Reputation: 806
Xarray can linearly interpolate latitudes and longitudes as if they were cartesian coordinates (as in your example above), but that isn't the same a proper geographical regridding. For that, you probably want to check out xesmf.
Upvotes: 2
Reputation: 411
I decided the easiest way would be to interp using the foo1 grid.
Thus:
foo2=foo.interp(lat=lat1).interp(lon=lon1)
foo2.sel(time='2005',method='nearest').plot()
Should produce an accurate subsampled gridded map.
Upvotes: 0