function (pollutant = c("o3", "no2", "so2", "co", "nmvoc", "pm10", "pm1", "cpc", "temp", "prec", "rad"), stations = c("Bern-Bollwerk", "Lausanne-César-Roux", "Lugano-Università", "Zürich-Kaserne", "Basel-Binningen", "Dübendorf-Empa", "Härkingen-A1", "Sion-Aéroport-A9", "Magadino-Cadenazzo", "Payerne", "Tänikon", "Lägeren", "Chaumont", "Rigi-Seebodenalp", "Davos-Seehornwald", "Jungfraujoch"), interval = c("hourly", "daily"), period = c("day", "week", "month", "free"), from = NULL, to = NULL) { schadstoff <- list(o3 = "Ozone (O3)", no2 = "Nitrogen dioxide (NO2)", so2 = "Sulfur dioxide (SO2)", co = "Carbon monoxide (CO)", nmvoc = "Non-methane volatile organic compounds (NMVOC)", pm10 = "Particulate matter (PM10)", pm1 = "Particulate matter (PM1)", cpc = "Particulate number concentration (CPC)", temp = "Temperature (TEMP)", prec = "Precipitation (PREC)", rad = "Global radiation (RAD)") stationsliste <- c("Bern-Bollwerk", "Lausanne-César-Roux", "Lugano-Università", "Zürich-Kaserne", "Basel-Binningen", "Dübendorf-Empa", "Härkingen-A1", "Sion-Aéroport-A9", "Magadino-Cadenazzo", "Payerne", "Tänikon", "Lägeren", "Chaumont", "Rigi-Seebodenalp", "Davos-Seehornwald", "Jungfraujoch") datentyp <- list(hourly = "stunden", daily = "tag") zeitraum <- list(day = "1tag", week = "1woche", month = "1monat", free = "frei") pollutant <- match.arg(pollutant) stations <- unlist(stations) if (is.numeric(stations)) { stations <- stationsliste[stations] } else { stations <- match.arg(stations, several.ok = TRUE) } interval <- match.arg(interval) period <- match.arg(period) if (period == "free") { from <- format(as.Date(from), "%d.%m.%Y") to <- format(as.Date(to), "%d.%m.%Y") } params <- c(abfrageflag = "true", station = "1", nach = "schadstoff", ausgabe = "csv", submit = "Query") params <- c(params, schadstoff = match(pollutant, names(schadstoff))) for (i in seq(along = schadstoff)) { params <- c(params, `schadstoffsliste[]` = i) } for (i in seq(along = stations)) { params <- c(params, `stationsliste[]` = match(stations[i], stationsliste)) } params <- c(params, datentyp = datentyp[[interval]]) params <- c(params, zeitraum = zeitraum[[period]]) if (period == "free") { params <- c(params, von = from) params <- c(params, bis = to) } url <- "https://bafu.meteotest.ch/nabel/index.php/ausgabe/index/english" paramstr <- paste(names(params), params, sep = "=", collapse = "&") doc <- readLines(paste(url, paramstr, sep = "?"), encoding = "latin1") doc <- enc2utf8(doc) head <- readLines(textConnection(doc), n = 30) skip <- grep("Date/time;", head) - 1 dat <- read.table(textConnection(doc), sep = ";", skip = skip, header = TRUE, fill = TRUE, as.is = TRUE, check.names = FALSE) dat <- dat[dat[, 1] != "0", ] names(dat)[1] <- "datetime" sta <- names(dat)[-1] if (interval == "hourly") { dat$datetime <- as.POSIXct(strptime(dat$datetime, format = "%d.%m.%Y %H:%M")) } else { dat$datetime <- as.POSIXct(strptime(dat$datetime, format = "%d.%m.%Y")) } dat <- reshape(dat, direction = "long", timevar = "station", varying = list(sta)) names(dat)[2] <- "station" names(dat)[3] <- "measurement" dat$id <- NULL dat$station <- factor(dat$station, labels = sta) ylabs <- list(o3 = expression(paste("Ozone (O3, in ", mu, "g/m", { }^3, ")")), no2 = expression(paste("Nitrogen dioxide (NO2, in ", mu, "g/m", { }^3, ")")), so2 = expression(paste("Sulfur dioxide (SO2, in ", mu, "g/m", { }^3, ")")), co = expression(paste("Carbon monoxide (CO, in mg/m", { }^3, ")")), nmvoc = "Non-methane volatile organic compounds (NMVOC, in ppm)", pm10 = expression(paste("Particulate matter (PM10, in ", mu, "g/m", { }^3, ")")), pm1 = expression(paste("Particulate matter (PM1, in ", mu, "g/m", { }^3, ")")), cpc = expression(paste("Particulate number concentration (CPC, in 1/cm", { }^3), ")"), temp = "Temperature (TEMP, in °C)", prec = "Precipitation (PREC, in mm)", rad = expression(paste("Global radiation (RAD, in W/m", { }^2, ")"))) pl <- xyplot(measurement ~ datetime, dat, groups = station, type = c("l"), auto.key = list(space = "right", points = FALSE, lines = TRUE), xlab = "date/time (Europe/Zurich)", ylab = ylabs[[pollutant]]) print(pl) }