3

I have created a xarray dataarray using the xarray concat command. This has resulted in a dataarray with name __xarray_dataarray_variable__. However, I cannot seem to rename it using the rename command. Is there some other way to rename it?

I tried this:

da.rename({'__xarray_dataarray_variable__': 'new'})

but I get this error: *** ValueError: cannot rename '__xarray_dataarray_variable__' because it is not a variable or dimension in this dataset

This is what the dataarray looks like:

<xarray.DataArray (time: 2, band: 1, y: 2334, x: 4258)>
dask.array<shape=(2, 1, 2334, 4258), dtype=float32, chunksize=(1, 1, 2334, 4258)>
Coordinates:
  * band     (band) int32 1
  * y        (y) float64 4.406e+06 4.406e+06 4.406e+06 4.406e+06 4.406e+06 ...
  * x        (x) float64 1.125e+05 1.126e+05 1.127e+05 1.128e+05 1.129e+05 ...
  * time     (time) datetime64[ns] 2005-12-31 2006-12-31
Attributes:
    transform:   (90.0, 0.0, 112500.0, 0.0, -90.0, 4406400.0, 0.0, 0.0, 1.0)
    crs:         +ellps=GRS80 +no_defs +proj=utm +towgs84=0,0,0,0,0,0,0 +unit...
    res:         (90.0, 90.0)
    is_tiled:    1
    nodatavals:  (-9999.0,)

From http://xarray.pydata.org/en/stable/generated/xarray.DataArray.to_netcdf.html, Only xarray.Dataset objects can be written to netCDF files, so the xarray.DataArray is converted to a xarray.Dataset object containing a single variable. If the DataArray has no name, or if the name is the same as a co-ordinate name, then it is given the name ‘xarray_dataarray_variable’.

user308827
  • 21,227
  • 87
  • 254
  • 417
  • does `da.name = 'new'` do the trick? None of the coordinates/dimensions in your DataArray have the `__xarray_dataarray_variable__` name. – jhamman May 01 '18 at 00:17
  • thanks @jhamman, I get this error when I do `da.name = 'new'` `*** AttributeError: cannot set attribute '__xarray_dataarray_variable__' on a 'DataArray' object. Use __setitem__ style assignment (e.g., `ds['name'] = ...`) instead to assign variables.` – user308827 May 01 '18 at 00:19
  • @jhamman, if it helps I am using your code here: https://stackoverflow.com/questions/46899337/convert-raster-time-series-of-multiple-geotiff-images-to-netcdf to create a netCDF file from a bunch of raster files – user308827 May 01 '18 at 00:19
  • okay, I see. I was off in my initial suggestion. Can you convert your DataArray to a Dataset: `da.to_dataset(name='new')`? – jhamman May 01 '18 at 04:18
  • thanks @jhamman, this works! can you add this as answer so I can accept? – user308827 May 01 '18 at 21:11
  • How did you create the DataArray `da`? It doesn't have any name set, or you would see it in the repr, e.g., ``. – shoyer May 02 '18 at 01:18
  • @shoyer, thanks for a great package! I created it from a bunch of rasters using the code here: https://stackoverflow.com/questions/46899337/convert-raster-time-series-of-multiple-geotiff-images-to-netcdf – user308827 May 02 '18 at 01:47

1 Answers1

5

I think you're looking for the to_dataset method:

ds = da.to_dataset(name='new')
jhamman
  • 5,867
  • 19
  • 39