François
François

Reputation: 133

Substitute dataset coordinates in xarray (Python)

I have a dataset stored in NetCDF4 format that consists of Intensity values with 3 dimensions: Loop, Delay and Wavelength. I named my coordinates the same as the dimensions (I don't know if it's good or bad...)

I'm using xarray (formerly xray) in Python to load the dataset:

import xarray as xr
ds = xr.open_dataset('test_data.netcdf4')

Now I want to manipulate the data while keeping track of the original data. For instance, I would:

  1. Apply an offset to the Delay coordinates and keep the original Delay dataarray untouched. This seems to be done with:

    ds_ = ds.assign_coords(Delay_corr=ds_.Delay.copy(deep=True) + 25)

  2. Substitute the coordinates Delay for Delay_corr for all relevant dataarrays in the dataset. However, I have no clue how to do this and I didn't find anything in the documentation.

Would anybody know how to perform item #2?

To download the NetCDF4 file with test data: http://1drv.ms/1QHQTRy

Upvotes: 9

Views: 14375

Answers (2)

shoyer
shoyer

Reputation: 9593

The method you're looking for is the xr.swap_dims() method:

ds.coords['Delay_corr'] = ds.Delay + 25  # could also use assign_coords
ds2 = ds.swap_dims({'Delay': 'Delay_corr'})

See this section of the xarray docs for a full example.

Upvotes: 18

Maximilian
Maximilian

Reputation: 8450

I think it's much simpler than that.

If you don't want to change the existing data, you create a copy. Note that changing ds won't change the netcdf4 file, but assuming you still don't want to change ds:

ds_ = ds.copy(deep=True)

Then just set the Delay coord as a modified version of the old one

ds_.coords['Delay'] = ds_['Delay'] + 25

Upvotes: 5

Related Questions