如何使用 Python 库 Xarray 将聚合维度添加到 netcdf4 文件?
How to add aggregation dimensions to netcdf4 file with the Python library Xarray?
我正在使用 xarray 基于 pandas Dataframe 中的数据创建一个 netcdf 文件。数据是一维的,只有时间作为维度。
然后,我使用这个文件的软件使用 libray netcdf4 中的 MFdataset() 打开并加载数据。每次我使用函数 to_netcdf()
可用的任何引擎或格式创建 netcdf 文件(例如称为 test3.nc
)时,我都会在使用 MFDataset('test3.nc')
[] 打开它时收到错误 OSError: master dataset test3.nc does not have a aggregation dimension
=17=]
import pandas as pd
import xarray as xr
from netcdf4 import MFDataset
# create a dataframe
df = pd.Dataframe()
# [logic to add data with one column as time]
# convert dataframe to Dataset
fo = xr.Dataset.from_dataframe(df.set_index('time'))
# add variable attributes here
# convert xarray Dataset to a netcdf file:
fo.to_netcdf('test3.nc', mode='w',format='NETCDF4_CLASSIC')
MFDataset('test3.nc')
尽管时间是一个维度,但控制台中出现以下错误:
MFDataset('test3.nc')
Traceback (most recent call last):
File "/home/arcticsnow/anaconda3/envs/dataAna/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-26-94430718f30a>", line 1, in <module>
MFDataset('test3.nc')
File "netCDF4/_netCDF4.pyx", line 5917, in netCDF4._netCDF4.MFDataset.__init__
OSError: master dataset test3.nc does not have a aggregation dimension
答案是在 to_netcdf()
函数中使用以下参数:
fo.to_netcdf('test.nc', unlimited_dims={'time':True}, format='NETCDF4_CLASSIC')
我正在使用 xarray 基于 pandas Dataframe 中的数据创建一个 netcdf 文件。数据是一维的,只有时间作为维度。
然后,我使用这个文件的软件使用 libray netcdf4 中的 MFdataset() 打开并加载数据。每次我使用函数 to_netcdf()
可用的任何引擎或格式创建 netcdf 文件(例如称为 test3.nc
)时,我都会在使用 MFDataset('test3.nc')
[] 打开它时收到错误 OSError: master dataset test3.nc does not have a aggregation dimension
=17=]
import pandas as pd
import xarray as xr
from netcdf4 import MFDataset
# create a dataframe
df = pd.Dataframe()
# [logic to add data with one column as time]
# convert dataframe to Dataset
fo = xr.Dataset.from_dataframe(df.set_index('time'))
# add variable attributes here
# convert xarray Dataset to a netcdf file:
fo.to_netcdf('test3.nc', mode='w',format='NETCDF4_CLASSIC')
MFDataset('test3.nc')
尽管时间是一个维度,但控制台中出现以下错误:
MFDataset('test3.nc')
Traceback (most recent call last):
File "/home/arcticsnow/anaconda3/envs/dataAna/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-26-94430718f30a>", line 1, in <module>
MFDataset('test3.nc')
File "netCDF4/_netCDF4.pyx", line 5917, in netCDF4._netCDF4.MFDataset.__init__
OSError: master dataset test3.nc does not have a aggregation dimension
答案是在 to_netcdf()
函数中使用以下参数:
fo.to_netcdf('test.nc', unlimited_dims={'time':True}, format='NETCDF4_CLASSIC')