簡體   English   中英

如何使用 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 )時,我to_netcdf()錯誤OSError: master dataset test3.nc does not have a aggregation dimension在使用MFDataset('test3.nc')打開它時OSError: master dataset test3.nc does not have a aggregation dimension MFDataset('test3.nc')

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM