How do I create a Data Variable in an xarray.DataArray?

Question:

How do I assign data to or create a ‘Data Variable’ in a xarray.DataArray or finally in the nc-file when saving the DataArray?
When creating the xarray.DataArray, it simply stores the data as an array but not as a ‘Data Variable’ as one is used to from nc-files.
In the next step, I’d like to save the DataArray as an nc-file with the array stored as an appropriate ‘Data Variable’ and a name to it.

import numpy as np
import xarray as xr

T_data = np.random.random((100,10))
pressure = np.arange(0,100,1)
longitude = np.arange(350,360,1)

T_DataArray = xr.DataArray(data=T_data, coords={'p':pressure, 'lon':longitude}, dims=['p','lon'])

T_DataArray.to_netcdf(path='C:/my/path/file.nc')

Output: T_DataArray

(Here specifically, I’d like to continue loading this nc-file to MATLAB but can’t access the stored data as a Data Variable, ncread(file, ???) doesn’t work…)

How do I create a ‘proper’ nc-file including a Data Variable here?
Thank you!

Asked By: cravingforhelp

||

Answers:

NetCDF files can include multiple named variables so you probably want to give your variable a useful name:

T_DataArray = xr.DataArray(
    data=T_data,
    coords={'p':pressure, 'lon':longitude},
    dims=['p','lon'],
    name='temperature'
)
T_DataArray.to_netcdf(path='./file.nc')

This generates a netCDF file that looks like:

$ ncdump -h file.nc
netcdf file {
dimensions:
    p = 100 ;
    lon = 10 ;
variables:
    int64 p(p) ;
    int64 lon(lon) ;
    double temperature(p, lon) ;
        temperature:_FillValue = NaN ;
}

In matlab, you’ll want to reference the file (file.nc) and the variable ("temperature").

Answered By: jhamman

I usually do this:

import numpy as np
import xarray as xr

T_data = np.random.random((100, 10))
pressure = np.arange(0, 100, 1)
longitude = np.arange(350, 360, 1)

T_DataArray = xr.DataArray(
    data=T_data,
    coords={
        'p': pressure,
        'lon': longitude
    },
    dims=['p', 'lon']
)
xr.Dataset({"temperature": T_DataArray}).to_netcdf(path='file.nc')

result:

ncdump -h file.nc

netcdf file {
dimensions:
        p = 100 ;
        lon = 10 ;
variables:
        int64 p(p) ;
        int64 lon(lon) ;
        double temperature(p, lon) ;
                temperature:_FillValue = NaN ;
}
Answered By: junsircoding