Changing the name and value of a coordinate in-place

2,465 views
Skip to first unread message

James Penn

unread,
Apr 18, 2016, 1:47:46 PM4/18/16
to xarray

Hi all, I'm looking for suggestions for a cleaner way of changing the coordinate axis of a DataArray I compute from NetCDF data.

I have a netCDF dataset with lat and latb coordinate labels. lat is cell centres (the coordinate axis of my data variables), latb is cell edges.  len(latb) = len(lat)+1. To calculate finite differences I need the difference across a cell which is given by the diff of the latb values, the result of which will be centered on lat points.

The problem is dlat = data.latb.diff('latb') has coordinate latb, but this latb is inconsistent with my original data as it is now 1 shorter. I want to remap it to lat which has the correct length and positional alignment with my dataset. The solution I came up with is:

dlat = xarray.DataArray(data.latb.diff('latb').values*np.pi/180, coords=[('lat', data.lat)])*a

but this is ugly, and fragile, as it assumes only one dimensional input.

Essentially, I want to wholesale replace the name and values of a coordinate axis in-place. Is there a better way to do this?



Jody Klymak

unread,
Jun 22, 2016, 12:15:46 AM6/22/16
to xarray
Bumping this, as I have exactly the same question.  Did you get an answer James?  

Jody Klymak

unread,
Jun 22, 2016, 12:39:24 AM6/22/16
to xarray
OK, here is how I did it.  Criticisms welcome, as I'm just figuring out how to do things...

```
dlat = dlat.rename({'latb':'lat'})
dlat['lat'].data = data['lat'].data
```

For some reason simply saying `dlat['lat']=data['lat']` doesn't work.



James Penn

unread,
Jun 28, 2016, 5:47:18 AM6/28/16
to xarray
Hi Jody,

I ended up doing as you suggest above, although I used the `data['lat'].values` property.  Perhaps `.data` is the more appropriate one, thanks.
Reply all
Reply to author
Forward
0 new messages