Commit e6b28c98 authored by Franssen, Wietse's avatar Franssen, Wietse
Browse files

Added some error checks

parent c28c3575
......@@ -10,7 +10,7 @@
plotje <-function(data, title = " ") {
if (length(dim(data$Data)) > 2) {
dataTmp<-data$Data[,,1]
cat("Plotting first timestep\n")
cat("Plotting first timestep\n")
} else {
dataTmp<-data$Data
}
......@@ -106,6 +106,36 @@ ncLoad <-function(file, varName = NULL, lonlatbox = NULL, timesteps = NULL) {
## Do some sheck on the NetCDF file
ncCheckResult<-ncCheck(ncFile = ncFile, variable = varName)
## SOME CHECKS:
## Check time
if (!is.null(timesteps)) {
if(min(timesteps) < 1 || max(timesteps) > ncFile$dim$time$len) {
stop(paste0("Timestep(s) out of range!\n",
"Given: ", min(timesteps), " till ", max(timesteps), "\n",
"Allowed: 1 till ", ncFile$dim$time$len), call. = FALSE)
}
}
## Check lon and lat
if (!is.null(lonlatbox)) {
minLon <-ncFile$dim$lon$vals[1]
maxLon <-ncFile$dim$lon$vals[ncFile$dim$lon$len]
if(lonlatbox[1] < minLon || lonlatbox[2] > maxLon) {
stop(paste0("Longitudes out of range!\n",
"Given: ", lonlatbox[1], " till ", lonlatbox[2], "\n",
"Allowed: ", minLon, " till ", maxLon), call. = FALSE)
}
minLat <-ncFile$dim$lat$vals[1]
maxLat <-ncFile$dim$lat$vals[ncFile$dim$lat$len]
if(lonlatbox[3] < minLat || lonlatbox[4] > maxLat) {
stop(paste0("Latitudes out of range!\n",
"Given: ", lonlatbox[3], " till ", lonlatbox[4], "\n",
"Allowed: ", minLat, " till ", maxLat), call. = FALSE)
}
}
## Fill lon and lat
if (is.null(lonlatbox)) {
LonIdx <- c(1: ncFile$dim$lon$len)
......@@ -118,12 +148,14 @@ ncLoad <-function(file, varName = NULL, lonlatbox = NULL, timesteps = NULL) {
data$xyCoords$x <- ncFile$dim$lon$vals[LonIdx]
data$xyCoords$y <- ncFile$dim$lat$vals[LatIdx]
## Fill time
## Define time indexes
if (is.null(timesteps)) {
timeIdx<-c(1)
} else {
timeIdx<-timesteps
}
## Fill time
NCtime <- ncvar_get( ncFile, "time" ) [timeIdx]
#NCtime <- ncvar_get( ncFile, "time" )
NCtimeAtt <- ncatt_get( ncFile, "time", "units" )$value
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment