Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swapping latitude and longitude position in xarray

I have a NetCDF file which has swapped latitude and longitude positions.

The standard way a Netcdf I normally use is defined as follows:

<xarray.DataArray 'pev' (time: 365, latitude: 480, longitude: 1440)>
Coordinates:
  * time       (time) datetime64[ns] 2001-01-01T11:30:00 ... 2001-12-31T11:30:00
  * longitude  (longitude) float32 0.0 0.25 0.5 0.75 ... 359.25 359.5 359.75
  * latitude   (latitude) float32 59.75 59.5 59.25 59.0 ... -59.5 -59.75 -60.0

But the specifications of the NetCDF I am dealing with is as following:

<xarray.DataArray 'ETa' (time: 12, longitude: 720, latitude: 360)>
Coordinates:
  * latitude   (latitude) float64 89.75 89.25 88.75 ... -88.75 -89.25 -89.75
  * longitude  (longitude) float64 -179.8 -179.2 -178.8 ... 178.8 179.2 179.8
Dimensions without coordinates: time

enter image description here

Since the position of latitude and longitude is swapped, it's a bit difficult to do analysis without modifying the code at several steps.

Does anyone has come across such a problem and know how to resolve this issue early on using xarray?

like image 462
Ep1c1aN Avatar asked Sep 12 '25 10:09

Ep1c1aN


1 Answers

Use the ds.transpose() function.

data.transpose('time', 'latitude', 'longitude')

like image 112
bwc Avatar answered Sep 14 '25 06:09

bwc