Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xarray: reverse an array along one coordinate

For my data array I have coordinates longitude and latitude and time. I want to reverse the array along latitude only so that [90, 85, ..., -80, -90] becomes [-90, -80, ..., 85, 90].

like image 884
ru111 Avatar asked Feb 13 '19 18:02

ru111


2 Answers

Agree with @jhamman's response that a minimally reproducible example would help

I think you could use

da = xr.tutorial.open_dataset('air_temperature')
da.reindex(lat=list(reversed(da.lat)))
like image 94
Maximilian Avatar answered Jan 01 '23 09:01

Maximilian


Base on this discussion, another option is to use isel to get a reversed view:

da = xr.tutorial.open_dataset('air_temperature')
da.isel(lat=slice(None, None, -1)

I've found this approach to work better with large datasets backed by dask arrays. The reindex approach in @Maximilian's answer causes the data to be rechunked into very large chunks. This approach avoids that.

like image 38
rvb Avatar answered Jan 01 '23 09:01

rvb