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

Some extra checks and support for files without time added

parent e6b28c98
......@@ -7,7 +7,16 @@
#' @keywords internal
## "export": export the help and make it available
#' @export
plotje <-function(data, title = " ") {
plotje <-function(data, title = NULL) {
if (is.null(title)) {
if (!is.na(data$Dates$start[1])) {
title=paste0(data$Variable$longName, " [",data$Variable$units,"]\n(" ,data$Dates$start[1], ")")
} else {
title=paste0(data$Variable$longName, " [",data$Variable$units,"]")
}
}
if (length(dim(data$Data)) > 2) {
dataTmp<-data$Data[,,1]
cat("Plotting first timestep\n")
......@@ -26,8 +35,7 @@ plotje <-function(data, title = " ") {
#' @export
ncPlot <-function(file, varName = NULL, lonlatbox = NULL, timesteps = NULL) {
data<-ncLoad(file, varName, lonlatbox, timesteps)
title=paste0(data$Variable$longName, " [",data$Variable$units,"]\n(" ,data$Dates$start[1], ")")
plotje(data, title = title)
plotje(data)
}
#' @author Wietse Franssen \email{wietse.franssen@@wur.nl}
......@@ -77,6 +85,8 @@ ncCheck <-function(ncFile, variable) {
#' @param file Name of the NetCDF file
#' @param varName Variable name of the NetCDF file
#' @details Loads a NetCDF file as a R-data structure.
#' @details eg: data <- ncLoad("~/DATA/example.nc4", lonlatbox = c(-24.25,37.75,33.25,60.25), varName = "tasmin", timesteps = c(1:3))
#' @details eg: ncPlot("~/DATA/example.nc4", lonlatbox = c(-24.25,37.75,33.25,60.25), varName = "tasmin", timesteps = c(1:3))
#' @return An object of class \code{WF}
#' @author Wietse Franssen \email{wietse.franssen@@wur.nl}
#' @keywords internal
......@@ -109,12 +119,18 @@ ncLoad <-function(file, varName = NULL, lonlatbox = NULL, timesteps = NULL) {
## SOME CHECKS:
## Check time
if (!is.null(timesteps)) {
if(is.null(ncFile$dim$time)) {
stop(paste0("The file doesnot have valid timesteps, so do not give one!\n",
"Given: ", min(timesteps), " till ", max(timesteps), "\n",
"Allowed: NONE!"), call. = FALSE)
}
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]
......@@ -133,29 +149,26 @@ ncLoad <-function(file, varName = NULL, lonlatbox = NULL, timesteps = NULL) {
}
}
## Fill lon and lat
## Fill lon, lat and time indexes
if (is.null(lonlatbox)) {
LonIdx <- c(1: ncFile$dim$lon$len)
LatIdx <- c(1: ncFile$dim$lat$len)
} else {
# LonIdx <- which( ncFile$dim$lon$vals > lonlatbox[1] | ncFile$dim$lon$vals < lonlatbox[2])
LonIdx <- which( ncFile$dim$lon$vals >= lonlatbox[1] & ncFile$dim$lon$vals <= lonlatbox[2])
LatIdx <- which( ncFile$dim$lat$vals >= lonlatbox[3] & ncFile$dim$lat$vals <= lonlatbox[4])
}
data$xyCoords$x <- ncFile$dim$lon$vals[LonIdx]
data$xyCoords$y <- ncFile$dim$lat$vals[LatIdx]
## Define time indexes
if (is.null(timesteps)) {
timeIdx<-c(1)
} else {
timeIdx<-timesteps
}
## Fill lon and lat
data$xyCoords$x <- ncFile$dim$lon$vals[LonIdx]
data$xyCoords$y <- ncFile$dim$lat$vals[LatIdx]
## Fill time
if(!is.null(ncFile$dim$time)) {
NCtime <- ncvar_get( ncFile, "time" ) [timeIdx]
#NCtime <- ncvar_get( ncFile, "time" )
NCtimeAtt <- ncatt_get( ncFile, "time", "units" )$value
......@@ -163,10 +176,15 @@ ncLoad <-function(file, varName = NULL, lonlatbox = NULL, timesteps = NULL) {
firstTime<-strptime(firstTime, format = "%Y-%m-%d", tz = "GMT")
data$Dates$start <- format(firstTime + (86400 * (NCtime+0)), format="%Y-%m-%d %T %Z")
data$Dates$end <- format(firstTime + (86400 * (NCtime+1)), format="%Y-%m-%d %T %Z")
}
## Fill data
#data <- ncvar_get( ncFile, "pr")[ LonIdx, LatIdx, 1]
data$Data <- ncvar_get( ncFile, varName ) [ LonIdx, LatIdx, timeIdx]
if(!is.null(ncFile$dim$time)) {
data$Data <- ncvar_get( ncFile, varName ) [ LonIdx, LatIdx, timeIdx]
} else {
data$Data <- ncvar_get( ncFile, varName ) [ LonIdx, LatIdx]
}
#data$Data <- ncvar_get( ncFile, varName )
attr(data$Data,"dimensions") <- ncCheckResult$dims
data$Variable$varName <- varName
......
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