Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Franssen, Wietse
WFRTools
Commits
5112c518
Commit
5112c518
authored
Feb 17, 2015
by
Franssen, Wietse
Browse files
Added lonlat selection
parent
c0acbb46
Changes
3
Hide whitespace changes
Inline
Side-by-side
R/WFRTools.R
View file @
5112c518
...
...
@@ -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
...
...
man/ncLoad.Rd
View file @
5112c518
...
...
@@ -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}
...
...
man/ncPlot.Rd
View file @
5112c518
...
...
@@ -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
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment