1

ncdf4::nc_open无法.nc从 URL 访问某些文件的问题是否有任何解决方法?我想避免必须先下载文件,因为这是部署在服务器上的闪亮应用程序,所以我想避免用户能够将文件下载到服务器。

一些 URL 可以工作,例如来自 THREDDS 服务器的这个 OPeNDAP URL:

library(ncdf4)
nc <- nc_open("https://dapds00.nci.org.au/thredds/dodsC/uc0/Test_pixel_count.nc")

但其他人不会,例如来自 THREDDS 服务器的 NetCDF 子集服务 URL:

nc <- nc_open("https://dapds00.nci.org.au/thredds/ncss/uc0/Test_pixel_count.nc?var=Band1&north=-22.9556&west=142&east=143&south=-25.0706&disableProjSubset=on&horizStride=1")
# Error in nc_open("https://dapds00.nci.org.au/thredds/ncss/uc0/Test_pixel_count.nc?var=Band1&north=-22.9556&west=142&east=143&south=-25.0706&disableProjSubset=on&horizStride=1") : 
# Error in nc_open trying to open file https://dapds00.nci.org.au/thredds/ncss/uc0/Test_pixel_count.nc?var=Band1&north=-22.9556&west=142&east=143&south=-25.0706&disableProjSubset=on&horizStride=1

或直接来自网站的此文件:

nc <- nc_open("https://www.unidata.ucar.edu/software/netcdf/examples/ECMWF_ERA-40_subset.nc")
# syntax error, unexpected WORD_WORD, expecting SCAN_ATTR or SCAN_DATASET or SCAN_ERROR
# context: <!DOCTYPE^ HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL was not found on this server.</p></body></html>
# Error in R_nc4_open: NetCDF: file not found
# Error in nc_open("https://www.unidata.ucar.edu/software/netcdf/examples/ECMWF_ERA-40_subset.nc") : 
# Error in nc_open trying to open file https://www.unidata.ucar.edu/software/netcdf/examples/ECMWF_ERA-40_subset.nc

有些方法有效而有些方法无效的原因可能是什么,有什么方法可以解决这个问题吗?

4

1 回答 1

3

我遇到了同样的问题并联系了ncdf4. 这是他的回复:

ncdf4 R包是您机器上底层 netcdf 库的接口。该R软件包无法避免 netcdf 库本身的限制或问题。您总是可以通过尝试对 URL 使用“ncdump”来查看底层 netcdf 库如何处理 URL。例如,

% ncdump "https://dapds00.nci.org.au/thredds/dodsC/uc0/Test_pixel_count.nc"

给出:

netcdf Test_pixel_count {
dimensions:
    lat = 283 ;
    lon = 451 ;
variables:
    byte crs ;
            crs:_Unsigned = "false" ;
            crs:grid_mapping_name = "latitude_longitude" ;
            crs:long_name = "CRS definition" ;
            crs:longitude_of_prime_meridian = 0. ;
            crs:semi_major_axis = 6378137. ;
            crs:inverse_flattening = 298.257222101 ;
            crs:spatial_ref = "GEOGCS[\"GEOCENTRIC DATUM of AUSTRALIA\",DATUM[\"GDA94\",SPHEROID[\"GRS80\",6378137,298.257222101]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433],AXIS[\"Longitude\",EAST],AXIS[\"Latitude\",NORTH]]" ;
            crs:GeoTransform = "141.4607205456306 0.0075 0 -22.95189554231917 0 -0.0075 " ;

等即,netcdf 库本身可以读取此 URL/文件。

但是,当我这样做时:

%  ncdump "https://dapds00.nci.org.au/thredds/ncss/uc0/Test_pixel_count.nc?var=Band1&north=-22.9556&west=142&east=143&south=-25.0706&disableProjSubset=on&horizStride=1"

我收到此错误:

ncdump "https://dapds00.nci.org.au/thredds/ncss/uc0/Test_pixel_count.nc?var=Band1&north=-22.9556&west=142&east=143&south=-25.0706&disableProjSubset=on&horizStride=1"
ncdump: https://dapds00.nci.org.au/thredds/ncss/uc0/Test_pixel_count.nc?var=Band1&north=-22.9556&west=142&east=143&south=-25.0706&disableProjSubset=on&horizStride=1: https://dapds00.nci.org.au/thredds/ncss/uc0/Test_pixel_count.nc?var=Band1&north=-22.9556&west=142&east=143&south=-25.0706&disableProjSubset=on&horizStride=1: 
NetCDF: Malformed or unexpected Constraint

因此 netcdf 库本身无法访问该 URL/文件,因此无法访问ncdf4。我无法对 ncdf4 库做任何事情来弥补这一点。

您可以尝试联系 netcdf 库人员并询问这是预期行为还是错误。

于 2021-12-15T17:46:21.927 回答