1

I am new to R and NetCDF files. I am trying to open data on surface sea temperatures that are in a .nc file from here. My code is as follows:

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)

But I am getting the following error:

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

I have followed the code from here and here. what am I doing wrong?

Many thanks, Mihir

Mihir Sharma
  • 51
  • 1
  • 7

1 Answers1

1

Path problems when using someone else's code:

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

When you're following code on a blog or tutorial, if it's written well, they'll use a platform independent way to describe, but often they won't.

The platform independent way to write a path is: file.path("Users", "Mihir Sharma", "Dropbox", "data"). This will always use the correct file separator for your Platform, a value stored in .Platform$file.sep

De Novo
  • 7,120
  • 1
  • 23
  • 39
  • This answer is incorrect. Forward slashes are fine in R on windows – dww Mar 21 '18 at 13:25
  • Thank you, @dww . I edited my assumption about the users platform. `file.path()` is the correct way to construct a path in a platform independent way, so it is now correct. – De Novo Mar 21 '18 at 16:53