在R中打开.nc文件时出错

时间:2018-03-20 01:30:09

标签: r geospatial netcdf netcdf4

我是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中的代码进行操作。我做错了什么?

非常感谢, 米希尔

1 个答案:

答案 0 :(得分:0)

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

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

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

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