【问题标题】:Accessing netcdf files from a URL linkwith ncdf4 in R在 R 中使用 ncdf4 从 URL 链接访问 netcdf 文件
【发布时间】:2021-09-30 18:16:21
【问题描述】:

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

某些 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

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

【问题讨论】:

    标签: r netcdf ncdf4


    【解决方案1】:

    我遇到了同样的问题并联系了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 库人员并询问这是预期行为还是错误。

    【讨论】:

    • 非常感谢,这是非常有用的信息!
    猜你喜欢
    • 2016-02-10
    • 1970-01-01
    • 2013-12-04
    • 2014-04-12
    • 2015-11-17
    • 2017-12-19
    • 2019-03-17
    • 2020-05-01
    • 1970-01-01
    相关资源
    最近更新 更多