Skip to content

Commit

Permalink
version 0.8.7
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcarslaw authored and cran-robot committed Apr 17, 2019
1 parent f28943f commit b3138c6
Show file tree
Hide file tree
Showing 20 changed files with 1,317 additions and 1,357 deletions.
11 changes: 6 additions & 5 deletions DESCRIPTION
Expand Up @@ -2,8 +2,8 @@ Package: worldmet
Type: Package
Title: Import Surface Meteorological Data from NOAA Integrated Surface
Database (ISD)
Version: 0.8.4
Date: 2018-02-27
Version: 0.8.7
Date: 2019-04-17
Authors@R: c(person("David", "Carslaw", role = c("aut", "cre"), email =
"david.carslaw@york.ac.uk"))
ByteCompile: true
Expand All @@ -19,9 +19,10 @@ URL: http://github.com/davidcarslaw/worldmet
BugReports: http://github.com/davidcarslaw/worldmet/issues
LazyLoad: true
LazyData: true
RoxygenNote: 6.0.1
Encoding: UTF-8
RoxygenNote: 6.1.1
NeedsCompilation: no
Packaged: 2018-02-27 13:54:35 UTC; David
Packaged: 2019-04-17 08:05:15 UTC; davidcarslaw
Author: David Carslaw [aut, cre]
Repository: CRAN
Date/Publication: 2018-02-27 14:16:34 UTC
Date/Publication: 2019-04-17 08:20:03 UTC
34 changes: 16 additions & 18 deletions MD5
@@ -1,19 +1,17 @@
cf54c0aeb41be2ca443c73c08b0c4793 *DESCRIPTION
0fb1b2ac28a1bb97e4726f2a546c353b *NAMESPACE
fde382d0eda08d1f08aaffa5bb40c92f *R/exportADMS.R
bd28cb4f44a1a1b19fde283990525289 *R/getMeta.R
5171f60d1c13156a56ebb0847d72c0b6 *R/metNOAA.R
b15a961f15873be7d691ca7eb1dffebf *R/meta.R
7cfccd5e8a36ebd23b3d3b7406507524 *R/sysdata.rda
072fafd8f8db6bf7a3138ea8a2735b16 *R/weatherCodes.R
b0ee29b89da7fa78035a2c171aca205c *R/worldmet-package.R
cc167598e5b17a10afb4d83e73e8a827 *README.md
7cfccd5e8a36ebd23b3d3b7406507524 *data/meta.rda
8699a0e8e38705634dd0ae8f861ea0c4 *data/weatherCodes.rda
3548133603512b7a6c65d999a4c6507f *DESCRIPTION
c3e84490aa32f5408c4b61c35b98197c *NAMESPACE
dfe77d899ba5964a17143dd2f0f20716 *R/exportADMS.R
e88c9bec4b14e8b651120d3c97d163ef *R/getMeta.R
e95f4154f85fbe814501ae038ef4a2dc *R/metNOAA.R
2d8a8e546196ab6f547cc99b76dc6842 *R/sysdata.rda
dfd79efabee5ea65b0ee279b9f8330de *R/weatherCodes.R
97f08f4288246821805256f22b5abe76 *R/worldmet-package.R
8df567a5cf8efae68c0385d8b205a71b *README.md
8a37aa4cb828770bb2e1c7d6d4f7e34d *data/weatherCodes.rda
84f21841c2bbb1c09c3ffbcf11ec2145 *inst/images/map.PNG
6fbcf4883b82a1b0bfa61465ce32f1ac *man/exportADMS.Rd
9fa5c61807f1f86079eb52f699576354 *man/getMeta.Rd
fa32bfe9485a9ca294867042d138d111 *man/importNOAA.Rd
f2cefd98affecb5b3afaf90b79db7036 *man/meta.Rd
ec57f553d8801d0013614cbc37a6f6c7 *man/weatherCodes.Rd
6ab42224111ef0481fb97bb9eb324d79 *man/worldmet.Rd
ca62b4153959d89af26f07cc4e2e1d32 *man/exportADMS.Rd
9f7b3ef698c60a10a7d824267dff8c9e *man/getMeta.Rd
40c9a88ccb100f7fc94a4e3899406085 *man/getMetaLive.Rd
b0292cea67ead0fc5d2c6012b1624d28 *man/importNOAA.Rd
033635944e83ba6b778f20d6c189adb2 *man/weatherCodes.Rd
9d97d7d263569f65699961d8765657c5 *man/worldmet.Rd
40 changes: 21 additions & 19 deletions NAMESPACE
@@ -1,19 +1,21 @@
# Generated by roxygen2: do not edit by hand

export(exportADMS)
export(getMeta)
export(importNOAA)
import(doParallel)
import(dplyr)
import(foreach)
import(openair)
import(parallel)
import(readr)
importFrom(leaflet,addCircles)
importFrom(leaflet,addMarkers)
importFrom(leaflet,addTiles)
importFrom(leaflet,leaflet)
importFrom(utils,download.file)
importFrom(utils,head)
importFrom(utils,write.table)
importFrom(zoo,na.approx)
# Generated by roxygen2: do not edit by hand

export(exportADMS)
export(getMeta)
export(getMetaLive)
export(importNOAA)
import(doParallel)
import(dplyr)
import(foreach)
import(openair)
import(parallel)
import(readr)
importFrom(leaflet,addCircles)
importFrom(leaflet,addMarkers)
importFrom(leaflet,addTiles)
importFrom(leaflet,leaflet)
importFrom(leaflet,markerClusterOptions)
importFrom(utils,download.file)
importFrom(utils,head)
importFrom(utils,write.table)
importFrom(zoo,na.approx)
234 changes: 117 additions & 117 deletions R/exportADMS.R
@@ -1,117 +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
#' 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
#' including \code{maxgap}.
#' @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
#'
#' \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"
)
)

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)
}
#' 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
#' 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
#' including \code{maxgap}.
#' @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
#'
#' \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"
)
)

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 b3138c6

Please sign in to comment.