簡體   English   中英

重新打開用xarray / dask編寫的netcdf文件時出現問題

[英]Problems reopening netcdf file written with xarray/dask

我正在使用dask進行模擬,並將輸出結果存儲在xarray數據集中。 然后,將數據保存到netcdf文件。 之后,我想重新打開文件以進行進一步的數據處理。 打開文件是ds.compute() ,但是當我實際訪問數據(例如,調用ds.compute() )時,出現一個錯誤,該錯誤似乎與讀取文件有關。 我首先認為這與寫入后無法正確關閉文件有關,但是手動關閉它並沒有幫助。

奇怪的是,如果數據不是由dask生成的,則打開文件並使用其數據沒有任何問題。 如果我只是在數據集中存儲一個帶有隨機數的numpy數組,那么一切都會很好。

我整理了一個小例子,讓您可以看一下:

import numpy as np
import xarray as xr
import pandas as pd
import dask.array as da
import dask
from dask.distributed import Client
from itertools import repeat

@dask.delayed
def run_sim(a, b, n_time):
    result = np.array([np.random.randn(n_time)*a,np.random.randn(n_time)+b])
    return result

client = Client()

# Parameters
n_sims = 5
n_time = 100
a_vals = np.random.randn(n_sims)
b_vals = np.random.randn(n_sims)
output_file = 'out.nc'

# if I use this as output, computing the data after reopening the file 
produces an error
out = da.stack([da.from_delayed(run_sim(*args), (2,n_time,),np.float64) for args in zip(a_vals, b_vals, repeat(n_time))])

# If I use this as output, reopening the netcdf file is no problem
#out = np.random.randn(n_sims,2,n_time) 

ds = xr.Dataset({'var1': (['realization', 'time'], out[:,0,:]),
                 'var2': (['realization', 'time'],out[:,1,:])},
                 coords={'realization': pd.RangeIndex(n_sims),
                         'time': pd.Index(np.arange(n_time)*.1)})


# Save to a netcdf file -> at this point, computations will be carried out
print('Saving data to netcdf file.')
ds.to_netcdf(output_file)

# close the netcdf file after writing
ds.close()

# Reopen the file
print('Reopen the file.')
with xr.open_dataset(output_file, chunks={'realization': 2}) as ds:
    # Now acces the data
    ds.compute()

我得到的錯誤是(我只是復制了最后幾行,在嘗試執行ds.compute()時引發了該錯誤):

~/miniconda3/lib/python3.6/site-packages/xarray/backends/netCDF4_.py in _open_netcdf4_group()
    229     import netCDF4 as nc4
    230 
--> 231     ds = nc4.Dataset(filename, mode=mode, **kwargs)
    232 
    233     with close_on_error(ds):

netCDF4/_netCDF4.pyx in netCDF4._netCDF4.Dataset.__init__()

netCDF4/_netCDF4.pyx in netCDF4._netCDF4._ensure_nc_success()

OSError: [Errno -101] NetCDF: HDF error: b'/home/user/output/out.nc'

編輯:如果僅在關閉文件之前運行代碼( ds.close()是最后執行的行),並在輸出文件上執行ncdump -h ,我會收到一條錯誤消息,指出它無法打開文件('資源暫時不可用”)。 因此它似乎仍在某個地方打開。

運行代碼的第二部分(從# Reopen the file開始)會導致上述錯誤。 但是之后,輸出文件上的ncdump -h成功了,產生了預期的輸出:

netcdf out {
dimensions:
    realization = 5 ;
    time = 100 ;
variables:
    double var1(realization, time) ;
            var1:_FillValue = NaN ;
    double var2(realization, time) ;
            var2:_FillValue = NaN ;
    int64 realization(realization) ;
    double time(time) ;
            time:_FillValue = NaN ;
}

如果再重新運行代碼的最后一部分,我不會再出現錯誤。

從這些結果得出的以下結論中我正確嗎?

  • 出現問題是因為寫入后未正確關閉文件。
  • 第一次(不成功)嘗試打開文件至少已正確將其關閉。

如果是這樣,我該怎么做才能解決這個問題?

EDIT2:僅當我啟動dask.distributed.Client()時才會出現問題。 我還在xarray GitHub頁面上打開了一個問題

請注意: 文檔建議close()僅在從文件讀取時適用。 寫入文件時, to_netcdf()應該為您關閉文件。 我本打算在評論中,但沒有足夠的代表來發表評論。

看來這是一個已修復的錯誤 安裝已經包含該修復程序的分支可以為我解決問題。

暫無
暫無

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

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