Learn R Programming

rdwd (version 0.10.2)

nearbyStations: Find DWD stations close to given coordinates

Description

Select DWD stations within a given radius around a set of coordinates

Usage

nearbyStations(lat, lon, radius, res = NA, var = NA, per = NA,
  mindate = NA, hasfileonly = TRUE,
  statname = "nearbyStations target location", quiet = FALSE, ...)

Arguments

lat, lon

Coordinates [degrees N/S, E/W]

radius

Maximum distance [km] within which stations will be selected

res, var, per

Restrictions for dataset type as documented in selectDWD. Each can be a vector of entries. DEFAULTS: NA (ignored)

mindate

Minimum dataset ending date (as per metadata). Integer in the form of YYYYMMDD, e.g. 20170301. DEFAULT: NA

hasfileonly

Logical: only return entries for which there is an open-access file available? DEFAULT: TRUE

statname

Character: name for target location. DEFAULT: "nearbyStations target location"

quiet

Logical: suppress progress messages? DEFAULT: FALSE

Further arguments passed to selectDWD

Value

metaIndex subset with additional columns "dist" and "url"

See Also

selectDWD, metaIndex

Examples

Run this code
# NOT RUN {
# }
# NOT RUN {
## entirely excluded from CRAN checks 2018 because of computing time
## Sections 3:5 were already excluded because of map downloads

# 1. Basic usage ----

m <- nearbyStations(49.211784, 9.812475, radius=30,
    res=c("daily","hourly"), var= c("precipitation","more_precip","kl") ,
    mindate=20160530, statname="Braunsbach catchment center")


# 2. Remove duplicates ----

# if kl and more_precip are both available, keep only more_precip:
library("berryFunctions")
m <- sortDF(m, "var")
m <- m[!duplicated(paste0(m$Stations_id, m$res)),]
m <- sortDF(m, "res")
m <- sortDF(m, "dist", decreasing=FALSE)
rownames(m) <- NULL

## 3. Interactive map ----

library(leaflet)
m$col <- "red" ; m$col[1] <- "blue"
leaflet(m) %>% addTiles() %>%
  addCircles(lng=9.812475, lat=49.211784, radius=30e3) %>%
  addCircleMarkers(~geoLaenge, ~geoBreite, col=~col, popup=~Stationsname)


## 4. Download and process data ----

# Download and process data for the stations, create a list of data.frames:
prec <- dataDWD(m$url) # once downloaded, will only read
names(prec) <- m$Stations_id[-1]
prec29 <- sapply(prec[m$res[-1]=="daily"], function(x)
         x[x$MESS_DATUM==as.POSIXct(as.Date("2016-05-29")), c("STATIONS_ID","NIEDERSCHLAGSHOEHE")])
prec29 <- data.frame(Stations_id=unlist(prec29[1,]), precsum=unlist(prec29[2,]))
prec29 <- merge(prec29, m[m$res=="daily",c(1,4:7,14)], sort=FALSE)
View(prec29)


## 5. Plot rainfall sum on map

plot(geoBreite~geoLaenge, data=m, asp=1)
textField(prec29$geoLaenge, prec29$geoBreite, prec29$precsum, col=2)

# If OSMscale installation fails, go to:
browseURL("https://github.com/brry/OSMscale#installation")
# install.packages("OSMscale")
library(OSMscale)
map <- pointsMap(geoBreite,geoLaenge, data=m, type="maptoolkit-topo")
pp <- projectPoints("geoBreite", "geoLaenge", data=prec29, to=map$tiles[[1]]$projection)
prec29 <- cbind(prec29,pp) ; rm(pp)
plot(map, removeMargin=FALSE)
scaleBar(map, cex=1.5, type="line", y=0.82)
title(main="Rainfall sum  2016-05-29  7AM-7AM  [mm]", line=-1)
textField(prec29$x, prec29$y, round(prec29$precsum), font=2, cex=1.5)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab