从numpy.array转换为带属性的数据数组后合并dataset

izj3ouym  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(73)

我试图将一些变量保存为netCDF文件。我正在采取这些步骤来做到这一点。

plev =  xr.DataArray(plev,coords=[levels,lat,lon], dims=["lev","lat","lon"])

    U_v32 = xr.DataArray(U_v32,coords=[levels,lat,lon], dims=["lev","lat","lon"]), 
    attrs=dict(long_name = "eastward_wind" , standard_name = "eastward_wind" , units = "m s-1" , 
        valid_range = "-1.e+15f, 1.e+15f" , vmax = "1.e+15f" , vmin = "-1.e+15f" , coordinates = "lon lat" ,)

字符串
其中,plev是:

<xarray.DataArray (lev: 32, lat: 192, lon: 288)>
array([[[3.64346569e+00, 3.64346569e+00, 3.64346569e+00, ...,

Coordinates:
  * lev      (lev) int64 1 2 3 4 5 6 7 8 9 10 ... 23 24 25 26 27 28 29 30 31 32
  * lat      (lat) float64 -90.0 -89.06 -88.12 -87.17 ... 87.17 88.11 89.06 90.0
  * lon      (lon) float64 0.0 1.25 2.5 3.75 5.0 ... 355.0 356.2 357.5 358.8


并且U_v32是:

(<xarray.DataArray (lev: 32, lat: 192, lon: 288)>
array([[[-1.12275551e-01, -1.67688605e-01, -2.23421148e-01, ...,
          7.19201197e-02,  6.93718333e-03, -5.42723485e-02],
        [ 4.28133062e+00,  4.01246378e+00,  3.74175282e+00, ...,
          5.00617035e+00,  4.76159409e+00,  4.51793771e+00],
        [ 6.71847498e+00,  6.23453015e+00,  5.75207444e+00, ...,
          8.12475620e+00,  7.66082277e+00,  7.19229493e+00],
        ...,

Coordinates:
  * lev      (lev) int64 1 2 3 4 5 6 7 8 9 10 ... 23 24 25 26 27 28 29 30 31 32
  * lat      (lat) float64 -90.0 -89.06 -88.12 -87.17 ... 87.17 88.11 89.06 90.0
  * lon      (lon) float64 0.0 1.25 2.5 3.75 5.0 ... 355.0 356.2 357.5 358.8,)


然后我会:

plev = plev.to_dataset(name="plev")
    U_v32 = U_v32.to_dataset(name = 'U')


最后合并它们,然后保存为netCDF:

data_v32 = xr.merge([U_v32,plev]) 

    data_v32.to_netcdf(data_v32_final.nc4)


但是在合并变量之前我得到了这个错误:

U_v32 = U_v32.to_dataset(name = 'U')
AttributeError: 'tuple' object has no attribute 'to_dataset'


是因为我把那个变量的属性定义为字典?.有没有办法避免这个错误?.
先谢了

mtb9vblg

mtb9vblg1#

在定义dataArray时,我仔细观察了一下,发现了问题所在,基本上我必须将attrs包含在xr.DataArray中。它被定义为:

U_v32 = xr.DataArray(U_v32,coords=[levels,lat,lon], dims=["lev","lat","lon"], 
    attrs=dict(long_name = "eastward_wind" , standard_name = "eastward_wind" , units = "m s-1" , 
        valid_range = "-1.e+15f, 1.e+15f" , vmax = "1.e+15f" , vmin = "-1.e+15f" , coordinates = "lon lat" ,))

字符串
然后转换为数据集并进行合并,就可以正常工作了,我可以将变量保存为netCDF

相关问题