Skip to content

Commit

Permalink
version 0.8.3
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcarslaw authored and cran-robot committed Feb 16, 2018
1 parent 63788e0 commit a60fd0d
Show file tree
Hide file tree
Showing 14 changed files with 635 additions and 591 deletions.
12 changes: 6 additions & 6 deletions DESCRIPTION
Expand Up @@ -2,14 +2,14 @@ Package: worldmet
Type: Package
Title: Import Surface Meteorological Data from NOAA Integrated Surface
Database (ISD)
Version: 0.8.0
Date: 2017-12-15
Version: 0.8.3
Date: 2018-02-08
Authors@R: c(person("David", "Carslaw", role = c("aut", "cre"), email =
"david.carslaw@york.ac.uk"))
ByteCompile: true
Depends: R (>= 3.2.0)
Imports: openair, doParallel, parallel, foreach, plyr, dplyr, leaflet,
readr, zoo
Imports: openair, doParallel, parallel, foreach, dplyr, leaflet, readr,
zoo
Maintainer: David Carslaw <david.carslaw@york.ac.uk>
Description: Functions to import data from more than 30,000 surface
meteorological sites around the world managed by the National Oceanic and Atmospheric Administration (NOAA) Integrated Surface
Expand All @@ -21,7 +21,7 @@ LazyLoad: true
LazyData: true
RoxygenNote: 6.0.1
NeedsCompilation: no
Packaged: 2017-12-18 15:29:15 UTC; David
Packaged: 2018-02-16 12:02:49 UTC; David
Author: David Carslaw [aut, cre]
Repository: CRAN
Date/Publication: 2017-12-18 15:40:33 UTC
Date/Publication: 2018-02-16 12:42:06 UTC
26 changes: 13 additions & 13 deletions MD5
@@ -1,19 +1,19 @@
09f9bdf63a073ecde8cf0f31a8145e78 *DESCRIPTION
2dbbc4f80734d2ebbafc11750ff7eb73 *NAMESPACE
c907682c4ac72c8f0ae11146430a592a *R/exportADMS.R
20fc7ad7978a1160a351139d461fd0f1 *R/getMeta.R
3e3d2d2964be0fd5e9b18bce78c1c490 *R/metNOAA.R
14fcd05358778bdc3207bed2db1ddc83 *R/meta.R
966fbfcf2da1001f070f1107e4ec0686 *R/sysdata.rda
cf4532f8d10db6d94136193ccf0f1cdb *R/weatherCodes.R
1ff037620039bef68eeeab3e4a5e68d1 *R/worldmet-package.R
9815f1fa5ba4ad3b3805b63a2d3c1cba *DESCRIPTION
715f470e8b2c21b692c683a516b66576 *NAMESPACE
fde382d0eda08d1f08aaffa5bb40c92f *R/exportADMS.R
932748d6470eb3d6d84de172bfa4d141 *R/getMeta.R
504b61ccbae9316f503501b53f5c048f *R/metNOAA.R
b15a961f15873be7d691ca7eb1dffebf *R/meta.R
7cfccd5e8a36ebd23b3d3b7406507524 *R/sysdata.rda
072fafd8f8db6bf7a3138ea8a2735b16 *R/weatherCodes.R
b0ee29b89da7fa78035a2c171aca205c *R/worldmet-package.R
cc167598e5b17a10afb4d83e73e8a827 *README.md
966fbfcf2da1001f070f1107e4ec0686 *data/meta.rda
7cfccd5e8a36ebd23b3d3b7406507524 *data/meta.rda
8699a0e8e38705634dd0ae8f861ea0c4 *data/weatherCodes.rda
84f21841c2bbb1c09c3ffbcf11ec2145 *inst/images/map.PNG
ffaf8662444fd21bd33c21c40e7cb9f6 *man/exportADMS.Rd
1018f48bb736b43d53e7ab416cbc8aff *man/getMeta.Rd
f6d3bcadfbf98522f4b5370b11912bc0 *man/importNOAA.Rd
6fbcf4883b82a1b0bfa61465ce32f1ac *man/exportADMS.Rd
9fa5c61807f1f86079eb52f699576354 *man/getMeta.Rd
fa32bfe9485a9ca294867042d138d111 *man/importNOAA.Rd
f2cefd98affecb5b3afaf90b79db7036 *man/meta.Rd
ec57f553d8801d0013614cbc37a6f6c7 *man/weatherCodes.Rd
6ab42224111ef0481fb97bb9eb324d79 *man/worldmet.Rd
3 changes: 1 addition & 2 deletions NAMESPACE
Expand Up @@ -4,12 +4,11 @@ export(exportADMS)
export(getMeta)
export(importNOAA)
import(doParallel)
import(dplyr)
import(foreach)
import(openair)
import(parallel)
import(plyr)
import(readr)
importFrom(dplyr,"%>%")
importFrom(leaflet,addCircles)
importFrom(leaflet,addMarkers)
importFrom(leaflet,addTiles)
Expand Down
186 changes: 97 additions & 89 deletions R/exportADMS.R
@@ -1,109 +1,117 @@
#' Export a meteorological data frame in ADMS format
#'
#'
#' @param dat A data frame imported by \code{\link{importNOAA}}.
#' @param out A file name for the ADMS file. The file is written to the working
#' @param out A file name for the ADMS file. The file is written to the working
#' directory by default.
#' @param interp Should interpolation of missing values be undertaken? If
#' \code{TRUE} linear interpolation is carried out for gaps of up to and
#' @param interp Should interpolation of missing values be undertaken? If
#' \code{TRUE} linear interpolation is carried out for gaps of up to and
#' including \code{maxgap}.
#' @param maxgap The maximum gap in hours that should be interpolated where
#' @param maxgap The maximum gap in hours that should be interpolated where
#' there are missing data when \code{interp = TRUE.} Data with gaps more than
#' \code{maxgap} are left as missing.
#'
#'
#' @return Writes a text file to a location of the user's choosing.
#' @export
#' @importFrom zoo na.approx
#' @examples
#'
#' @examples
#'
#' \dontrun{
#' ## import some data then export it
#' dat <- importNOAA(year = 2012)
#' exportADMS(dat, file = "~/temp/adms_met.MET")
#' }
exportADMS <- function(dat, out = "./ADMS_met.MET", interp = FALSE, maxgap = 2) {

# keep R check quiet
wd = u = v = NULL

## make sure the data do not have gaps
all.dates <- data.frame(
date = seq(ISOdatetime(year = as.numeric(format(dat$date[1], "%Y")),
month = 1, day = 1, hour = 0, min = 0,
sec = 0, tz = "GMT"),
ISOdatetime(year = as.numeric(format(dat$date[1], "%Y")),
month = 12, day = 31, hour = 23, min = 0,
sec = 0, tz = "GMT"), by = "hour")
wd <- u <- v <- NULL

## make sure the data do not have gaps
all.dates <- data.frame(
date = seq(ISOdatetime(
year = as.numeric(format(dat$date[1], "%Y")),
month = 1, day = 1, hour = 0, min = 0,
sec = 0, tz = "GMT"
),
ISOdatetime(
year = as.numeric(format(dat$date[1], "%Y")),
month = 12, day = 31, hour = 23, min = 0,
sec = 0, tz = "GMT"
),
by = "hour"
)

dat <- merge(dat, all.dates, all = TRUE)

## make sure precipitation is available
if (!"precip" %in% names(dat))
dat$precip <- NA

if (interp) {

## variables to interpolate
## note need to deal with wd properly
dat <- transform(dat, u = sin(pi * wd / 180), v = cos(pi * wd / 180))

varInterp <- c("ws", "u", "v", "air_temp", "RH", "cl", "precip")

# don't want to try and interpret fields that are all missing
ids <- sapply(varInterp, function (x) !all(is.na(dat[[x]])))
varInterp <- varInterp[ids]

dat[varInterp] <- zoo::na.approx(dat[varInterp], maxgap = maxgap, na.rm = FALSE)

## now put wd back
dat <- transform(dat, wd = as.vector(atan2(u, v) * 360 / 2 / pi))

## correct for negative wind directions
ids <- which(dat$wd < 0) ## ids where wd < 0
dat$wd[ids] <- dat$wd[ids] + 360
}

## exports met data to ADMS format file
year <- as.numeric(format(dat$date, "%Y"))
day <- as.numeric(format(dat$date, "%j"))
hour <- as.numeric(format(dat$date, "%H"))
station <- "0000"

## data frame of met data needed
adms <- data.frame(station, year, day, hour,
round(dat$air_temp, 1), round(dat$ws, 1),
round(dat$wd, 1), round(dat$RH, 1),
round(dat$cl), round(dat$precip, 1),
stringsAsFactors = FALSE)

## print key data capture rates to the screen
dc <- round(100 - 100 * (length(which(is.na(dat$ws))) / length(dat$ws)), 1)
print(paste("Data capture for wind speed:", dc, "%"))

dc <- round(100 - 100 * (length(which(is.na(dat$wd))) / length(dat$wd)), 1)
print(paste("Data capture for wind direction:", dc, "%"))

dc <- round(100 - 100 * (length(which(is.na(dat$air_temp))) / length(dat$air_temp)), 1)
print(paste("Data capture for temperature:", dc, "%"))

dc <- round(100 - 100 * (length(which(is.na(dat$cl))) / length(dat$cl)), 1)
print(paste("Data capture for cloud cover:", dc, "%"))

## replace NA with -999
adms[] <- lapply(adms, function(x) replace(x, is.na(x), -999))

## write the data file
write.table(adms, file = out, col.names = FALSE, row.names = FALSE,
sep = ",", quote = FALSE)

## add the header lines
fConn <- file(out, 'r+')
Lines <- readLines(fConn)
writeLines(c("VARIABLES:\n10\nSTATION DCNN\nYEAR\nTDAY\nTHOUR\nT0C\nU\nPHI\nRHUM\nCL\nP\nDATA:",
Lines), con = fConn)
close(fConn)

}
)

dat <- merge(dat, all.dates, all = TRUE)

## make sure precipitation is available
if (!"precip" %in% names(dat)) {
dat$precip <- NA
}

if (interp) {

## variables to interpolate
## note need to deal with wd properly
dat <- transform(dat, u = sin(pi * wd / 180), v = cos(pi * wd / 180))

varInterp <- c("ws", "u", "v", "air_temp", "RH", "cl", "precip")

# don't want to try and interpret fields that are all missing
ids <- sapply(varInterp, function(x) !all(is.na(dat[[x]])))
varInterp <- varInterp[ids]

dat[varInterp] <- zoo::na.approx(dat[varInterp], maxgap = maxgap, na.rm = FALSE)

## now put wd back
dat <- transform(dat, wd = as.vector(atan2(u, v) * 360 / 2 / pi))

## correct for negative wind directions
ids <- which(dat$wd < 0) ## ids where wd < 0
dat$wd[ids] <- dat$wd[ids] + 360
}

## exports met data to ADMS format file
year <- as.numeric(format(dat$date, "%Y"))
day <- as.numeric(format(dat$date, "%j"))
hour <- as.numeric(format(dat$date, "%H"))
station <- "0000"

## data frame of met data needed
adms <- data.frame(station, year, day, hour,
round(dat$air_temp, 1), round(dat$ws, 1),
round(dat$wd, 1), round(dat$RH, 1),
round(dat$cl), round(dat$precip, 1),
stringsAsFactors = FALSE
)

## print key data capture rates to the screen
dc <- round(100 - 100 * (length(which(is.na(dat$ws))) / length(dat$ws)), 1)
print(paste("Data capture for wind speed:", dc, "%"))

dc <- round(100 - 100 * (length(which(is.na(dat$wd))) / length(dat$wd)), 1)
print(paste("Data capture for wind direction:", dc, "%"))

dc <- round(100 - 100 * (length(which(is.na(dat$air_temp))) / length(dat$air_temp)), 1)
print(paste("Data capture for temperature:", dc, "%"))

dc <- round(100 - 100 * (length(which(is.na(dat$cl))) / length(dat$cl)), 1)
print(paste("Data capture for cloud cover:", dc, "%"))

## replace NA with -999
adms[] <- lapply(adms, function(x) replace(x, is.na(x), -999))

## write the data file
write.table(adms,
file = out, col.names = FALSE, row.names = FALSE,
sep = ",", quote = FALSE
)

## add the header lines
fConn <- file(out, "r+")
Lines <- readLines(fConn)
writeLines(c(
"VARIABLES:\n10\nSTATION DCNN\nYEAR\nTDAY\nTHOUR\nT0C\nU\nPHI\nRHUM\nCL\nP\nDATA:",
Lines
), con = fConn)
close(fConn)
}

0 comments on commit a60fd0d

Please sign in to comment.