我正在尝试在我的一个软件包中使用网状结构,但是在某些依赖方面存在很大的问题。我正在尝试导入xarray模块以读取netCDF4文件。本质上是:
readnc <- function(file) {
xr <- reticulate::import("xarray")
r <- xr$open_dataset(file)
return(r)
}
我在一个程序包中实现了这个确切的代码,但是我得到了非常的怪异行为。为了说明这一点,这似乎很好:
file <- "~/Documents/CONICET/onda3/DATA/zg_Amon_CNRM-CM6-1_historical_i1p1f2_gr_185001-201412.nc4"
readnc <- function(file) {
xr <- reticulate::import("xarray")
r <- xr$open_dataset(file)
return(r)
}
readnc(file)
#> <xarray.Dataset>
#> Dimensions: (lat: 128, lon: 256, plev: 2, time: 660)
#> Coordinates:
#> * time (time) datetime64[ns] 1850-01-01 1850-04-01 ... 2014-10-01
#> * lat (lat) float64 -88.93 -87.54 -86.14 -84.74 ... 86.14 87.54 88.93
#> * lon (lon) float64 0.0 1.406 2.812 4.219 ... 354.4 355.8 357.2 358.6
#> * plev (plev) float32 500.0 2000.0
#> Data variables:
#> zg (time, plev, lat, lon) float32 ...
metR::ReadNetCDF(file, out = "xarray")
#> <xarray.Dataset>
#> Dimensions: (lat: 128, lon: 256, plev: 2, time: 660)
#> Coordinates:
#> * time (time) datetime64[ns] 1850-01-01 1850-04-01 ... 2014-10-01
#> * lat (lat) float64 -88.93 -87.54 -86.14 -84.74 ... 86.14 87.54 88.93
#> * lon (lon) float64 0.0 1.406 2.812 4.219 ... 354.4 355.8 357.2 358.6
#> * plev (plev) float32 500.0 2000.0
#> Data variables:
#> zg (time, plev, lat, lon) float32 ...
但这完全相同,但顺序不同,但失败了:
file <- "~/Documents/CONICET/onda3/DATA/zg_Amon_CNRM-CM6-1_historical_i1p1f2_gr_185001-201412.nc4"
metR::ReadNetCDF(file, out = "xarray")
#> Error in py_call_impl(callable, dots$args, dots$keywords): TypeError: Error: /home/elio/Documents/CONICET/onda3/DATA/zg_Amon_CNRM-CM6-1_historical_i1p1f2_gr_185001-201412.nc4 is not a valid NetCDF 3 file
#> If this is a NetCDF4 file, you may need to install the
#> netcdf4 library, e.g.,
#>
#> $ pip install netcdf4
#>
#>
#> Detailed traceback:
#> File "/home/elio/miniconda3/envs/r-reticulate/lib/python3.7/site-packages/xarray/backends/api.py", line 451, in open_dataset
#> ds = maybe_decode_store(store)
#> File "/home/elio/miniconda3/envs/r-reticulate/lib/python3.7/site-packages/xarray/backends/api.py", line 381, in maybe_decode_store
#> drop_variables=drop_variables, use_cftime=use_cftime)
#> File "/home/elio/miniconda3/envs/r-reticulate/lib/python3.7/site-packages/xarray/conventions.py", line 517, in decode_cf
#> vars, attrs = obj.load()
#> File "/home/elio/miniconda3/envs/r-reticulate/lib/python3.7/site-packages/xarray/backends/common.py", line 121, in load
#> for k, v in self.get_variables().items())
#> File "/home/elio/miniconda3/envs/r-reticulate/lib/python3.7/site-packages/xarray/backends/scipy_.py", line 166, in get_variables
#> for k, v in self.ds.variables.items())
#> File "/home/elio/miniconda3/envs/r-reticulate/lib/python3.7/site-packages/xarray/backends/scipy_.py", line 158, in ds
#> return self._manager.acquire()
#> File "/home/elio/miniconda3/envs/r-reticulate/lib/python3.7/site-packages/xarray/backends/file_manager.py", line 168, in acquire
#> file, _ = self._acquire_with_cache_info(needs_lock)
#> File "/home/elio/miniconda3/envs/r-reticulate/lib/python3.7/site-packages/xarray/backends/file_manager.py", line 192, in _acquire_with_cache_info
#> file = self._opener(*self._args, **kwargs)
#> File "/home/elio/miniconda3/envs/r-reticulate/lib/python3.7/site-packages/xarray/backends/scipy_.py", line 100, in _open_scipy_netcdf
#> raise TypeError(errmsg)
readnc <- function(file) {
xr <- reticulate::import("xarray")
r <- xr$open_dataset(file)
return(r)
}
readnc(file)
#> Error in py_call_impl(callable, dots$args, dots$keywords): TypeError: Error: /home/elio/Documents/CONICET/onda3/DATA/zg_Amon_CNRM-CM6-1_historical_i1p1f2_gr_185001-201412.nc4 is not a valid NetCDF 3 file
#> If this is a NetCDF4 file, you may need to install the
#> netcdf4 library, e.g.,
#>
#> $ pip install netcdf4
#>
#>
#> Detailed traceback:
#> File "/home/elio/miniconda3/envs/r-reticulate/lib/python3.7/site-packages/xarray/backends/api.py", line 451, in open_dataset
#> ds = maybe_decode_store(store)
#> File "/home/elio/miniconda3/envs/r-reticulate/lib/python3.7/site-packages/xarray/backends/api.py", line 381, in maybe_decode_store
#> drop_variables=drop_variables, use_cftime=use_cftime)
#> File "/home/elio/miniconda3/envs/r-reticulate/lib/python3.7/site-packages/xarray/conventions.py", line 517, in decode_cf
#> vars, attrs = obj.load()
#> File "/home/elio/miniconda3/envs/r-reticulate/lib/python3.7/site-packages/xarray/backends/common.py", line 121, in load
#> for k, v in self.get_variables().items())
#> File "/home/elio/miniconda3/envs/r-reticulate/lib/python3.7/site-packages/xarray/backends/scipy_.py", line 166, in get_variables
#> for k, v in self.ds.variables.items())
#> File "/home/elio/miniconda3/envs/r-reticulate/lib/python3.7/site-packages/xarray/backends/scipy_.py", line 158, in ds
#> return self._manager.acquire()
#> File "/home/elio/miniconda3/envs/r-reticulate/lib/python3.7/site-packages/xarray/backends/file_manager.py", line 168, in acquire
#> file, _ = self._acquire_with_cache_info(needs_lock)
#> File "/home/elio/miniconda3/envs/r-reticulate/lib/python3.7/site-packages/xarray/backends/file_manager.py", line 192, in _acquire_with_cache_info
#> file = self._opener(*self._args, **kwargs)
#> File "/home/elio/miniconda3/envs/r-reticulate/lib/python3.7/site-packages/xarray/backends/scipy_.py", line 100, in _open_scipy_netcdf
#> raise TypeError(errmsg)
由reprex package(v0.3.0)于2019-08-28创建
为什么网状结构在第二个示例中找不到netcdf4模块,而在第一个示例中找不到?