首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用R中的ncdf4从URL链接访问netcdf文件

使用R中的ncdf4从URL链接访问netcdf文件
EN

Stack Overflow用户
提问于 2021-07-23 16:18:11
回答 1查看 439关注 0票数 1

对于ncdf4::nc_open无法从.nc访问某些.nc文件的问题,有什么解决办法吗?我想避免先下载该文件,因为这是一个部署在服务器上的闪亮应用程序,因此我希望避免用户能够将文件下载到服务器。

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

代码语言:javascript
复制
library(ncdf4)
nc <- nc_open("https://dapds00.nci.org.au/thredds/dodsC/uc0/Test_pixel_count.nc")

但另一些则没有,例如来自THREDDS服务器的这个NetCDF子集服务URL:

代码语言:javascript
复制
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

或者这个文件直接从一个网站:

代码语言:javascript
复制
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

有什么原因可以解释为什么有些方法起作用,而另一些则不行,还有什么方法可以解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2021-12-15 17:46:21

我遇到了同样的问题,并联系了ncdf4的创建者。以下是他的答复:

ncdf4 R包是到机器上的基础netcdf库的接口。R包无法避免netcdf库本身的限制或问题。您总是可以看到底层netcdf库是如何通过尝试在URL中使用"ncdump“来处理URL的。例如,

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

给予:

代码语言:javascript
复制
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/文件。

然而,当我这样做时:

代码语言:javascript
复制
%  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"

我知道这个错误:

代码语言:javascript
复制
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库人员联系,询问这是预期的行为还是bug。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68502230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档