Commit 5112c518 authored by Franssen, Wietse's avatar Franssen, Wietse
Browse files

Added lonlat selection

parent c0acbb46
......@@ -18,8 +18,8 @@ plotje <-function(data, title = " ") {
#' @author Wietse Franssen \email{wietse.franssen@@wur.nl}
#' @keywords internal
#' @export
ncPlot <-function(file, varName = NULL) {
data<-ncLoad(file, varName)
ncPlot <-function(file, varName = NULL, lonlatbox = NULL) {
data<-ncLoad(file, varName, lonlatbox)
plotje(data, title = paste0(data$Variable$varName, " (" ,data$Dates$start[1], ")"))
title=paste0(data$Variable$longName, " [",data$Variable$units,"]\n(" ,data$Dates$start[1], ")")
plotje(data, title = title)
......@@ -63,8 +63,10 @@ ncCheck <-function(ncFile, variable) {
}
# @examples
# data <- ncLoad( file = paste0(find.package("WFRTools"),"./examples/data/example.nc4"))
# data <- ncLoad( file = "./examples/data/example.nc4")
# data <- ncLoad( file = "~/Desktop/gg/wfd_pr_1974.nc", varName = "pr")
#find.package("WFRTools")
#' ncLoad
#' @description Loads a NetCDF file as a R-data structure
#' @param file Name of the NetCDF file
......@@ -74,9 +76,12 @@ ncCheck <-function(ncFile, variable) {
#' @author Wietse Franssen \email{wietse.franssen@@wur.nl}
#' @keywords internal
#' @export
ncLoad <-function(file, varName = NULL) {
ncLoad <-function(file, varName = NULL, lonlatbox = NULL) {
## first build an empty data structure
data<-rDataStructure()
## Open the netcdf file
ncFile <- nc_open( file )
## if no Variable is given then use the first one in the file
......@@ -93,27 +98,34 @@ ncLoad <-function(file, varName = NULL) {
}
}
## Do some sheck on the NetCDF file
ncCheckResult<-ncCheck(ncFile = ncFile, variable = varName)
data$xyCoords$x <- ncFile$dim$lon$vals
data$xyCoords$y <- ncFile$dim$lat$vals
if (.PRINT_INFO == TRUE) {
cat("Variables:\n")
print(names(ncFile$var))
print(ncCheckResult)
## Fill lon and lat
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]
#data <- ncvar_get( ncFile, "pr")[ LonIdx, LatIdx, 1]
## Fill time
NCtime <- ncvar_get( ncFile, "time" )
NCtimeAtt <- ncatt_get( ncFile, "time", "units" )$value
firstTime<-unlist(strsplit(NCtimeAtt, split=' ', fixed=TRUE))[3]
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")
data$Data <- ncvar_get( ncFile, varName )
## Fill data
#data <- ncvar_get( ncFile, "pr")[ LonIdx, LatIdx, 1]
data$Data <- ncvar_get( ncFile, varName ) [ LonIdx, LatIdx, ]
#data$Data <- ncvar_get( ncFile, varName )
attr(data$Data,"dimensions") <- ncCheckResult$dims
data$Variable$varName <- varName
......@@ -130,10 +142,30 @@ ncLoad <-function(file, varName = NULL) {
} else {
data$Variable$units <- "missing"
}
## Close the file
nc_close(ncFile)
## Print some info
if (.PRINT_INFO == TRUE) {
cat("Variables:\n")
print(names(ncFile$var))
print(ncCheckResult)
}
return(data)
}
# rm(list=ls())
# library(ncdf4)
# library(fields) # e.g: using the fields library
......
......@@ -4,7 +4,7 @@
\alias{ncLoad}
\title{ncLoad}
\usage{
ncLoad(file, varName = NULL)
ncLoad(file, varName = NULL, lonlatbox = NULL)
}
\arguments{
\item{file}{Name of the NetCDF file}
......
......@@ -4,7 +4,7 @@
\alias{ncPlot}
\title{Make a image of the NetCDF data}
\usage{
ncPlot(file, varName = NULL)
ncPlot(file, varName = NULL, lonlatbox = NULL)
}
\value{
Nothing
......
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