如何在xarray.DataArray中创建一个数据变量?

huangapple go评论60阅读模式
英文:

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

问题

如何将数据分配给或在保存DataArray时创建一个'数据变量',最终存储在nc文件中?

在创建xarray.DataArray时,它只是将数据存储为一个数组,而不是像nc文件中一样存储为'数据变量'。

接下来,我想将DataArray保存为nc文件,其中数组存储为适当的'数据变量'并为其命名。

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')

(在这里,我特别想继续将此nc文件加载到MATLAB中,但无法访问存储的数据作为数据变量,ncread(file, ???)不起作用...)

如何在这里创建一个'正确'的nc文件,包括一个数据变量?
谢谢!

英文:

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!

答案1

得分: 0

NetCDF文件可以包括多个命名变量,因此您可能希望给您的变量一个有用的名称:

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

这将生成一个类似如下的NetCDF文件:

$ 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 ;
}

在MATLAB中,您需要引用文件(file.nc)和变量("temperature")。

英文:

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

答案2

得分: 0

以下是翻译好的部分:

我通常这样做

```python
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 ;
}
英文:

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 ;
}

huangapple
  • 本文由 发表于 2023年2月9日 01:56:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定