【问题标题】:Error opening .nc files in R在 R 中打开 .nc 文件时出错
【发布时间】:2018-08-28 17:22:54
【问题描述】:

我是 R 和 NetCDF 文件的新手。我正在尝试打开来自here 的.nc 文件中的表面海温数据。我的代码如下:

rm(list=ls())
#install.packages(c("netcdf", "chron", "RColorBrewer", "lattice"))
library(chron)
library(RColorBrewer)
library(lattice)
library(ncdf4)

# set path and filename
ncpath <- "C:/Users/Mihir Sharma/Dropbox/data/"
ncname <- "20140101023458-NCEI-L3C_GHRSST-SSTskin-AVHRR_Pathfinder-PFV5.3_NOAA19_G_2014001_night-v02.0-fv01.0"  
ncfname <- paste(ncpath, ncname, ".nc", sep="")
dname <- "tmp"  # note: tmp means temperature (not temporary)

# open a NetCDF file
ncin <- nc_open(ncfname)

但我收到以下错误:

Error in nc_open(ncfname) : 
Error in nc_open trying to open file C:/Users/Mihir Sharma/Dropbox/1 EPIC/MPA/data/20140101023458-NCEI-L3C_GHRSST-SSTskin-AVHRR_Pathfinder-PFV5.3_NOAA19_G_2014001_night-v02.0-fv01.0.nc

我遵循了herehere 的代码。我做错了什么?

非常感谢, 米希尔

【问题讨论】:

  • 检查该路径下的文件是否存在
  • 更明确地说,file.exists(ncfname) 的输出是什么
  • 路径有问题 - 很抱歉浪费您的时间,非常感谢您的快速回复!
  • 欢迎您!很高兴你找到了。请参阅下面的我的答案:使用别人的代码时的路径问题。如果有帮助,请点赞并将其标记为答案。

标签: r geospatial netcdf netcdf4


【解决方案1】:

使用别人代码时的路径问题:

# set path and filename
ncpath <- "C:/Users/Mihir Sharma/Dropbox/data/"

当你在博客或教程上关注代码时,如果写得好,他们会使用独立于平台的方式来描述,但通常不会。

编写路径的平台无关方式是: file.path("Users", "Mihir Sharma", "Dropbox", "data")。这将始终为您的平台使用正确的文件分隔符,该值存储在.Platform$file.sep

【讨论】:

  • 这个答案不正确。 Windows 上的 R 中的正斜杠很好
  • 谢谢@dww。我编辑了我对用户平台的假设。 file.path() 是以平台无关的方式构造路径的正确方式,所以现在是正确的。
猜你喜欢
  • 2023-01-20
  • 2016-05-10
  • 1970-01-01
  • 2021-09-11
  • 2021-05-08
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多