2

I've created a xarray.DataArray and I save it using xarray.DataArray.to_netcdf.

I create it using:

datatmp = np.full([nens, len(modanom.coords['time'].values), len(modanom.coords['latitude'].values), len(modanom.coords['longitude'].values)], np.nan)
b = xr.DataArray(datatmp, coords=[range(1,nens + 1), modanom.coords['time'], modanom.coords['latitude'], modanom.coords['longitude']], dims=['ensemble', 'time', 'latitude', 'longitude'])

i.e. I don't specify a name such as:

b = xr.DataArray({'windspeed': (('ensemble', 'time', 'latitude', 'longitude'), datatmp)}, coords=[range(1,nens + 1), modanom.coords['time'], modanom.coords['latitude'], modanom.coords['longitude']], dims=['ensemble', 'time', 'latitude', 'longitude'])

I do some manipulation of this file and obtain root mean square error and the resultant xarray.Dataset is

<xarray.DataArray (latitude: 81, longitude: 131, time: 1)>
array([[[      nan],
    [      nan],
    ..., 
    [      nan],
    [      nan]],

   [[      nan],
    [      nan],
    ..., 
    [      nan],
    [      nan]],

   ..., 
   [[      nan],
    [ 0.843295],
    ..., 
    [ 0.794338],
    [      nan]],

   [[      nan],
    [      nan],
    ..., 
    [      nan],
    [      nan]]])
Coordinates:
* latitude   (latitude) float64 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ...
* longitude  (longitude) float64 260.0 261.0 262.0 263.0 264.0 265.0 266.0 ..
* time       (time) datetime64[ns] 1983-01-15T12:00:00

When reading in the netCDF file I have a xarray.Dataset

which looks like:

<xarray.Dataset>
Dimensions:                        (latitude: 81, longitude: 131, time: 1)
Coordinates:
* latitude                       (latitude) float64 0.0 1.0 2.0 3.0 4.0 ...
* longitude                      (longitude) float64 260.0 261.0 262.0 ...
* time                           (time) datetime64[ns] 1983-01-15T12:00:00
Data variables:
__xarray_dataarray_variable__  (latitude, longitude, time) float64 nan ...
Attributes:
_NCProperties:  version=1|netcdflibversion=4.4.1|hdf5libversion=1.8.18

Can I rename the __xarray_dataarray_variable__?

The reason I ask is because as I have functions which create xarray.Dataset's and therefore I don't specify a data variable name. However, I would like to rename it before plotting.

Ray Bell
  • 1,508
  • 4
  • 18
  • 45

4 Answers4

6
DataArray.rename

is what I was looking for

Ray Bell
  • 1,508
  • 4
  • 18
  • 45
  • 1
    The behaviour in [xarray.rename](http://xarray.pydata.org/en/stable/generated/xarray.DataArray.rename.html) is the same as [pandas.rename](https://pandas.pydata.org/pandas-docs/version/0.18.1/generated/pandas.DataFrame.rename.html). For instance see [this example:](https://stackoverflow.com/a/11354850/2543267) – tsherwen Nov 04 '17 at 13:44
6

Another possibility for the Dataset ds is:

    ds['new_var_name'] = ds['__xarray_dataarray_variable__']
    ds = ds.drop(['__xarray_dataarray_variable__'])
chuaxr
  • 188
  • 1
  • 7
3

This will do the job.

data = DataArray.rename("new_name")
data.to_netcdf()
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
anjali
  • 41
  • 2
2

This file looks like one created by DataArray.to_netcdf() on an unnamed DataArray. A simple way to fix this is to set a name on the DataArray before calling to_netcdf().

Otherwise, if you're happy getting an unnamed DataArray back, try loading it with xarray.open_dataarray().

shoyer
  • 9,165
  • 1
  • 37
  • 55