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:
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)
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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With