python-xarray rename data variable

折月煮酒 提交于 2019-12-12 02:44:03

问题


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.


回答1:


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().




回答2:


DataArray.rename

is what I was looking for




回答3:


Another possibility for the Dataset ds is:

    ds['new_var_name'] = ds['__xarray_dataarray_variable__']
    ds = ds.drop(['__xarray_dataarray_variable__'])


来源:https://stackoverflow.com/questions/43943969/python-xarray-rename-data-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!