Commit 0ea0bf35 authored by Franssen, Wietse's avatar Franssen, Wietse
Browse files

-Improved handling of time in ncLoad and ncWrite

-added support for Yearly timestep in ncLoad
-added "z" dimension in ncLoad
parent f4d22aa4
# Generated by roxygen2 (4.1.0): do not edit by hand
export(makeMask)
export(ncLoad)
export(ncPlot)
export(ncWrite)
export(plotje)
......@@ -16,7 +16,6 @@ plotje <-function(data, title = NULL) {
}
}
if (length(dim(data$Data)) > 2) {
dataTmp<-data$Data[,,1]
cat("Plotting first timestep\n")
......@@ -33,8 +32,8 @@ plotje <-function(data, title = NULL) {
#' @author Wietse Franssen \email{wietse.franssen@@wur.nl}
#' @keywords internal
#' @export
ncPlot <-function(file, varName = NULL, lonlatbox = NULL, timesteps = NULL) {
data<-ncLoad(file, varName, lonlatbox, timesteps)
ncPlot <-function(file, varName = NULL, lonlatbox = NULL, timesteps = NULL, z = NULL) {
data<-ncLoad(file, varName, lonlatbox, timesteps, z)
plotje(data)
}
......@@ -84,6 +83,7 @@ ncCheck <-function(ncFile, variable) {
#' @description Loads a NetCDF file as a R-data structure
#' @param file Name of the NetCDF file
#' @param varName Variable name of the NetCDF file
#' @param timesteps "all", Nothing or array of Timesteps
#' @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))
......@@ -91,7 +91,7 @@ ncCheck <-function(ncFile, variable) {
#' @author Wietse Franssen \email{wietse.franssen@@wur.nl}
#' @keywords internal
#' @export
ncLoad <-function(file, varName = NULL, lonlatbox = NULL, timesteps = NULL) {
ncLoad <-function(file, varName = NULL, lonlatbox = NULL, timesteps = NULL, z = NULL) {
## first build an empty data structure
data<-rDataStructure()
......@@ -124,10 +124,12 @@ ncLoad <-function(file, varName = NULL, lonlatbox = NULL, timesteps = NULL) {
"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)
if(!timesteps[1] == "all") {
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)
}
}
}
......@@ -148,8 +150,7 @@ ncLoad <-function(file, varName = NULL, lonlatbox = NULL, timesteps = NULL) {
"Allowed: ", minLat, " till ", maxLat), call. = FALSE)
}
}
## Fill lon, lat and time indexes
## Fill lon and lat indexes
if (is.null(lonlatbox)) {
LonIdx <- c(1: ncFile$dim$lon$len)
LatIdx <- c(1: ncFile$dim$lat$len)
......@@ -157,8 +158,12 @@ ncLoad <-function(file, varName = NULL, lonlatbox = NULL, timesteps = NULL) {
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])
}
## Fill time indexes
if (is.null(timesteps)) {
timeIdx<-c(1)
} else if (timesteps[1] == "all") {
timeIdx<-c(1:ncFile$dim$time$len)
} else {
timeIdx<-timesteps
}
......@@ -169,23 +174,29 @@ ncLoad <-function(file, varName = NULL, lonlatbox = NULL, timesteps = NULL) {
## 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
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")
NCtime <- ncvar_get( ncFile, "time" ) [timeIdx]
NCtimeAtt <- ncatt_get( ncFile, "time", "units" )$value
if (NCtimeAtt == "Years") {
data$Dates$start<-format(strptime(paste0(NCtime[],"-01-01"), format="%Y-%m-%d", tz = "GMT"), format="%Y-%m-%d %T %Z")
data$Dates$end<-format(strptime(paste0(NCtime[]+1,"-01-01"), format="%Y-%m-%d", tz = "GMT"), format="%Y-%m-%d %T %Z")
} else { ## if attributes is "days since..."
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")
}
}
## Fill data
#data <- ncvar_get( ncFile, "pr")[ LonIdx, LatIdx, 1]
if(!is.null(ncFile$dim$time)) {
data$Data <- ncvar_get( ncFile, varName ) [ LonIdx, LatIdx, timeIdx]
if(is.null(z)) {
if(!is.null(ncFile$dim$time)) {
data$Data <- ncvar_get( ncFile, varName, start=c(LonIdx[1],LatIdx[1],timeIdx[1]),count=c(length(LonIdx),length(LatIdx),length(timeIdx)))
} else {
data$Data <- ncvar_get( ncFile, varName, start=c(LonIdx[1],LatIdx[1]),count=c(length(LonIdx),length(LatIdx)))
}
} else {
data$Data <- ncvar_get( ncFile, varName ) [ LonIdx, LatIdx]
data$Data <- ncvar_get( ncFile, varName, start=c(LonIdx[1],LatIdx[1],z,timeIdx[1]),count=c(length(LonIdx),length(LatIdx),1,length(timeIdx)))
}
#data$Data <- ncvar_get( ncFile, varName )
attr(data$Data,"dimensions") <- ncCheckResult$dims
data$Variable$varName <- varName
......@@ -216,15 +227,84 @@ ncLoad <-function(file, varName = NULL, lonlatbox = NULL, timesteps = NULL) {
return(data)
}
#' ncLoad
#' @description Loads a NetCDF file as a R-data structure
#' @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.
#' @return An object of class \code{WF}
#' @author Wietse Franssen \email{wietse.franssen@@wur.nl}
#' @keywords internal
#' @export
makeMask <-function(data) {
## first build an empty data structure
mask<-rDataStructure()
## lon and lats
mask$xyCoords$x<-data$xyCoords$x
mask$xyCoords$y<-data$xyCoords$y
## Maskout
if (length(dim(data$Data)) > 2 ) {
mask$Data<-data$Data[,,1]
} else {
mask$Data<-data$Data
}
mask$Data[!is.na(mask$Data)] <- 1
naCells<-sum(is.na( mask$Data ))
naValid<-sum(!is.na( mask$Data ))
total<-naCells+naValid
cat(paste0("mask succesfully made!\n") )
cat(paste0("valid values: ", naValid, "\n" ) )
cat(paste0("NA values: ", naCells, "\n" ) )
cat(paste0("Total: ", total, "\n" ) )
return(mask)
}
#' ncWrite
#' @description Write a R-data structure to a NetCDF file
#' @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
#' @export
ncWrite <-function(outFile = "~/out.nc", rData = data) {
timeString<-format(strptime(rData$Dates$start, format = "%Y-%m-%d", tz = "GMT"),format="%Y-%m-%d %T")
#timeArray<-as.double(c(0:(length(rData$Dates$start)-1)))
timeArray<-as.numeric(difftime(timeString,timeString[1],units="days"))
dimT <- ncdim_def("time", paste0("days since ",timeString[1]), timeArray, unlim = FALSE)
dimX <- ncdim_def("lon", "degrees_east", round(rData$xyCoords$x,2))
dimY <- ncdim_def("lat", "degrees_north",round(rData$xyCoords$y,2))
FillValue <- NA
FillValue <- 1e20
datavar <- ncvar_def( rData$Variable$varName, rData$Variable$units, list(dimX,dimY,dimT), FillValue, prec="float")
ncid_out <- nc_create(outFile, datavar)
## ADD ATTRIBUTES
ncatt_put( ncid_out, 0, "institution", "Wageningen University and Research centre (WUR)")
ncatt_put( ncid_out, 0, "contact", "Wietse Franssen <wietse.franssen@wur.nl>")
ncatt_put( ncid_out, "lon", "standard_name", "longitude")
ncatt_put( ncid_out, "lon", "long_name", "Longitude")
ncatt_put( ncid_out, "lon", "axis", "X")
ncatt_put( ncid_out, "lat", "standard_name", "latitude")
ncatt_put( ncid_out, "lat", "long_name", "Latitude")
ncatt_put( ncid_out, "lat", "axis", "Y")
ncatt_put( ncid_out, "time", "standard_name", "time")
ncatt_put( ncid_out, "time", "calendar", "standard")
ncatt_put( ncid_out, rData$Variable$varName, "long_name", rData$Variable$longName)
ncatt_put( ncid_out, rData$Variable$varName, "_FillValue", FillValue)
ncvar_put( ncid_out, datavar, rData$Data )
nc_close(ncid_out)
}
# rm(list=ls())
# library(ncdf4)
......
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/WFRTools.R
\name{makeMask}
\alias{makeMask}
\title{ncLoad}
\usage{
makeMask(data)
}
\arguments{
\item{file}{Name of the NetCDF file}
\item{varName}{Variable name of the NetCDF file}
}
\value{
An object of class \code{WF}
}
\description{
Loads a NetCDF file as a R-data structure
}
\details{
Loads a NetCDF file as a R-data structure.
}
\author{
Wietse Franssen \email{wietse.franssen@wur.nl}
}
\keyword{internal}
......@@ -10,6 +10,8 @@ ncLoad(file, varName = NULL, lonlatbox = NULL, timesteps = NULL)
\item{file}{Name of the NetCDF file}
\item{varName}{Variable name of the NetCDF file}
\item{timesteps}{"all", Nothing or array of Timesteps}
}
\value{
An object of class \code{WF}
......
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/WFRTools.R
\name{ncWrite}
\alias{ncWrite}
\title{ncWrite}
\usage{
ncWrite(outFile = "~/out.nc", rData = data)
}
\arguments{
\item{file}{Name of the NetCDF file}
\item{varName}{Variable name of the NetCDF file}
}
\value{
An object of class \code{WF}
}
\description{
Write a R-data structure to a NetCDF file
}
\details{
Loads a NetCDF file as a R-data structure.
}
\author{
Wietse Franssen \email{wietse.franssen@wur.nl}
}
\keyword{internal}
......@@ -4,7 +4,7 @@
\alias{plotje}
\title{Plot a simple map}
\usage{
plotje(data, title = " ")
plotje(data, title = NULL)
}
\description{
Plot a simple map
......
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