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

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

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

问题

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

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

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

  1. import numpy as np
  2. import xarray as xr
  3. T_data = np.random.random((100,10))
  4. pressure = np.arange(0,100,1)
  5. longitude = np.arange(350,360,1)
  6. T_DataArray = xr.DataArray(data=T_data, coords={'p':pressure, 'lon':longitude}, dims=['p','lon'])
  7. 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.

  1. import numpy as np
  2. import xarray as xr
  3. T_data = np.random.random((100,10))
  4. pressure = np.arange(0,100,1)
  5. longitude = np.arange(350,360,1)
  6. T_DataArray = xr.DataArray(data=T_data, coords={'p':pressure, 'lon':longitude}, dims=['p','lon'])
  7. 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文件可以包括多个命名变量,因此您可能希望给您的变量一个有用的名称:

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

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

  1. $ ncdump -h file.nc
  2. netcdf file {
  3. dimensions:
  4. p = 100 ;
  5. lon = 10 ;
  6. variables:
  7. int64 p(p) ;
  8. int64 lon(lon) ;
  9. double temperature(p, lon) ;
  10. temperature:_FillValue = NaN ;
  11. }

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

英文:

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

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

This generates a netCDF file that looks like:

  1. $ ncdump -h file.nc
  2. netcdf file {
  3. dimensions:
  4. p = 100 ;
  5. lon = 10 ;
  6. variables:
  7. int64 p(p) ;
  8. int64 lon(lon) ;
  9. double temperature(p, lon) ;
  10. temperature:_FillValue = NaN ;
  11. }

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

答案2

得分: 0

以下是翻译好的部分:

  1. 我通常这样做
  2. ```python
  3. import numpy as np
  4. import xarray as xr
  5. T_data = np.random.random((100, 10))
  6. pressure = np.arange(0, 100, 1)
  7. longitude = np.arange(350, 360, 1)
  8. T_DataArray = xr.DataArray(
  9. data=T_data,
  10. coords={
  11. 'p': pressure,
  12. 'lon': longitude
  13. },
  14. dims=['p', 'lon']
  15. )
  16. xr.Dataset({"temperature": T_DataArray}).to_netcdf(path='file.nc')

result:

ncdump -h file.nc

  1. netcdf file {
  2. dimensions:
  3. p = 100 ;
  4. lon = 10 ;
  5. variables:
  6. int64 p(p) ;
  7. int64 lon(lon) ;
  8. double temperature(p, lon) ;
  9. temperature:_FillValue = NaN ;
  10. }
英文:

I usually do this:

  1. import numpy as np
  2. import xarray as xr
  3. T_data = np.random.random((100, 10))
  4. pressure = np.arange(0, 100, 1)
  5. longitude = np.arange(350, 360, 1)
  6. T_DataArray = xr.DataArray(
  7. data=T_data,
  8. coords={
  9. 'p': pressure,
  10. 'lon': longitude
  11. },
  12. dims=['p', 'lon']
  13. )
  14. xr.Dataset({"temperature": T_DataArray}).to_netcdf(path='file.nc')

result:

ncdump -h file.nc

  1. netcdf file {
  2. dimensions:
  3. p = 100 ;
  4. lon = 10 ;
  5. variables:
  6. int64 p(p) ;
  7. int64 lon(lon) ;
  8. double temperature(p, lon) ;
  9. temperature:_FillValue = NaN ;
  10. }

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:

确定