xarray to_netcdf() : save value to netcdf file

https://github.com/NCAR/wrf-python/issues/91

I also encounter the issue that the way the projection ist stored as an objects breaks the xarray.DataArray method to_netcdf()

I did a work around using the following function:

def write_xarray_to_netcdf(xarray_array, output_path,mode=’w’, format=’NETCDF4′, group=None, engine=None,
encoding=None):
“””writes and xarray in a netcdf format outputfile
Uses the xarray typical for wrf-python. The projection objects are transformed into strings
to be able to use them as netcdf attributes
:param xarray_array: xarray.DataArray
:param output_path: str
:param format: ‘NETCDF4’, ‘NETCDF4_CLASSIC’, ‘NETCDF3_64BIT’ or ‘NETCDF3_CLASSIC’
default: ‘NETCDF4’
:param group: str, default None
:param engine: ‘netcdf4’, ‘scipy’ or ‘h5netcdf’
:param encoding: dict, default: None
“””
xarray_array_out = xarray_array.copy(deep=True)
# coordinates are extracted from variable
del xarray_array_out.attrs[‘coordinates’]
# wrf-python projection object cannot be processed
xarray_array_out.attrs[‘projection’] = str(xarray_array_out.attrs[‘projection’])

xarray_array_out.to_netcdf(path=output_path, mode=mode, format=format, group=group,
                           engine=engine,
                           encoding=encoding)

https://www.jianshu.com/p/84596af1dcb6

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
pandas
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论