简体   繁体   中英

How to add aggregation dimensions to netcdf4 file with the Python library Xarray?

I am creating a netcdf file based on data from a pandas Dataframe with xarray. The data are one dimensional with only time as dimension. Then, the software I use this file for uses MFdataset() from the libray netcdf4 to open and load the data. Everytime I create a netcdf file (eg called test3.nc ) using whichever engine or format available with the function to_netcdf() , I then obtain the error OSError: master dataset test3.nc does not have a aggregation dimension when opening it with 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')

Then the following error comes in the console depite the fact that time is a dimension:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM