Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 722
François Avatar asked Feb 25 '16 17:02

François


2 Answers

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.

like image 198
shoyer Avatar answered Oct 22 '22 14:10

shoyer


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
like image 38
Maximilian Avatar answered Oct 22 '22 12:10

Maximilian