Skip to content

Commit

Permalink
This _should_ fix #117
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhsparks committed Jul 20, 2024
1 parent d8ea9fd commit 0f7bcf7
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions R/process_csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@
#' @noRd

.process_csv <- function(x, isd_history) {

# Import data from the website for individual stations or tempdir() for all --
DT <-
fread(x,
colClasses = c("STATION" = "character"),
strip.white = TRUE
)
DT <- fread(x, strip.white = TRUE, keepLeadingZeros = TRUE)

# Replace 99.99 et al. with NA
set(DT, j = "PRCP", value = as.character(DT[["PRCP"]]))
set(DT,
i = which(DT[["PRCP"]] == "99.99"),
j = "PRCP",
Expand All @@ -32,7 +26,6 @@
"GUST",
"SNDP"
)]) {
set(DT, j = col, value = as.character(DT[[col]]))
set(DT,
i = which(DT[[col]] == "999.9"),
j = col,
Expand All @@ -49,7 +42,6 @@
"MAX",
"MIN"
)]) {
set(DT, j = col, value = as.character(DT[[col]]))
set(DT,
i = which(DT[[col]] == "9999.9"),
j = col,
Expand All @@ -73,6 +65,13 @@
# Add STNID col --------------------------------------------------------------
DT[, STNID := gsub("^(.{6})(.*)$", "\\1-\\2", DT$STATION)]

# Correct STP values ---------------------------------------------------------
# The NCEI supplied CSV files are broken, they lop off the "1" in values >1000
# See https://github.com/ropensci/GSODR/issues/117
DT[, STP := fifelse(substr(STP, 1, 1) == 0,
sprintf("%s%s", 1, DT$STP[[5]]),
STP, na = NA)]

# Add and convert date related columns ---------------------------------------
DT[, YEARMODA := as.Date(DATE, format = "%Y-%m-%d")]
DT[, YEAR := as.integer(substr(DATE, 1, 4))]
Expand Down

0 comments on commit 0f7bcf7

Please sign in to comment.