Skip to content

Commit

Permalink
fix: NA when no sounding data
Browse files Browse the repository at this point in the history
  • Loading branch information
quishqa committed Aug 28, 2022
1 parent 081dd4b commit 39cd01e
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 31 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(download_airport_sounding)
export(download_senamhi_met)
export(download_senamhi_pol)
86 changes: 55 additions & 31 deletions R/download_airport_sounding.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
#' Download sounding from Jorge Chavez Airport
#' Download sounding from Jorge Chavez Airport (SPIM or 84628) from
#' <https://weather.uwyo.edu/upperair/sounding.html> . It can also download
#' from any other airport by changing the dafault values.
#'
#' @param date Date to download in dd/mm/yyyy.
#' @param hour 12 or 00. Default is 12
#' @param airport Airport code. Default is "SPIM" (Jorge Chavez Airport)
#' @param region Region to download. Default is "samer" (South America)
#'
#' @return data.frame with sounding data
#' @export
#'
#' @examples
#' \dontrun{
#' # Download sounding from 12z 15/01/2019
#' jc_sound <- download_airport_sounding("15/01/2019")
#' }
download_airport_sounding <- function(date, hour = 12, airport = "SPIM",
region = "samer"){

Expand All @@ -23,36 +41,42 @@ download_airport_sounding <- function(date, hour = 12, airport = "SPIM",

sound_html <- XML::htmlParse(sounding)
sound_html_table <- XML::getNodeSet(sound_html, "//pre")
sound_data_html <- sound_html_table[[1]]
sound_text <- utils::capture.output(sound_data_html)
sound_data <- sound_text[6:(length(sound_text)-1)]

pres <- substr(sound_data, 2, 7)
hght <- substr(sound_data, 8, 14)
temp <- substr(sound_data, 15, 21)
dwpt <- substr(sound_data, 22, 28)
relh <- substr(sound_data, 29, 35)
mixr <- substr(sound_data, 36, 42)
drct <- substr(sound_data, 43, 49)
sknt <- substr(sound_data, 50, 56)
thta <- substr(sound_data, 57, 63)
thte <- substr(sound_data, 64, 70)
thtv <- substr(sound_data, 71, 77)

sound_df <- data.frame(
pres = pres,
hght = hght,
temp = temp,
dwpt = dwpt,
relh = relh,
mixr = mixr,
drct = drct,
sknt = sknt,
thta = thta,
thte = thte,
thtv = thtv
)

sound_df[, 1:11] <- sapply(sound_df[,1:11], as.numeric)

if (length(sound_html_table) != 0){
sound_data_html <- sound_html_table[[1]]
sound_text <- utils::capture.output(sound_data_html)
sound_data <- sound_text[6:(length(sound_text)-1)]

pres <- substr(sound_data, 2, 7)
hght <- substr(sound_data, 8, 14)
temp <- substr(sound_data, 15, 21)
dwpt <- substr(sound_data, 22, 28)
relh <- substr(sound_data, 29, 35)
mixr <- substr(sound_data, 36, 42)
drct <- substr(sound_data, 43, 49)
sknt <- substr(sound_data, 50, 56)
thta <- substr(sound_data, 57, 63)
thte <- substr(sound_data, 64, 70)
thtv <- substr(sound_data, 71, 77)

sound_df <- data.frame(
pres = pres,
hght = hght,
temp = temp,
dwpt = dwpt,
relh = relh,
mixr = mixr,
drct = drct,
sknt = sknt,
thta = thta,
thte = thte,
thtv = thtv
)

sound_df[, 1:11] <- sapply(sound_df[,1:11], as.numeric)
} else {
message("No data available. Returning NA")
sound_df <- NA
}
return(sound_df)
}
35 changes: 35 additions & 0 deletions man/download_airport_sounding.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 39cd01e

Please sign in to comment.