Skip to content

Commit b873d5b

Browse files
committed
added more detailed check dates
1 parent 4523c01 commit b873d5b

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

R/metar_get_historical.R

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ metar_get_historical <- function(airport = "EPWA",
4545
stop("pmetar package error: Only one airport at once!", call. = FALSE)
4646
}
4747

48-
# check whether airpot consist of spaces
48+
# check whether airport consist of spaces
4949
if(stringr::str_detect(airport, pattern = "\\s")) {
5050
stop("pmetar package error: Airport code contains blank(s)!", call. = FALSE)
5151
}
@@ -59,18 +59,34 @@ metar_get_historical <- function(airport = "EPWA",
5959
if(!stringr::str_detect(airport, pattern = "^[A-Za-z]{4}$")){
6060
stop("pmetar package error: invalid format of an airport ICAO or IATA code!", call. = FALSE)
6161
}
62+
63+
# check if airport exists
64+
if (metar_iata_icao(airport) == "Incorrect ICAO or IATA airport code!") {
65+
stop("pmetar package error: Incorrect ICAO or IATA airport code!", call. = FALSE)
66+
}
67+
6268
# check if dates have correct format
6369
if(!stringr::str_detect(start_date, pattern = "^\\d{4}[-]\\d\\d[-]\\d\\d$") |
6470
!stringr::str_detect(end_date, pattern = "^\\d{4}[-]\\d\\d[-]\\d\\d$")){
6571
stop("pmetar package error: invalid format of start_date and/or end_date!", call. = FALSE)
6672
}
6773

74+
# check if start_date is correct
75+
if (is.na(lubridate::ymd(start_date))) {
76+
stop("pmetar package error: start_date is invalid!", call. = FALSE)
77+
}
78+
79+
# check if end_date is correct
80+
if (is.na(lubridate::ymd(end_date))) {
81+
stop("pmetar package error: end_date is invalid!", call. = FALSE)
82+
}
83+
6884
# check if dates range is correct
6985
if(as.Date(start_date) >= as.Date(end_date)) {
7086
stop("pmetar package error: start_date is equal or later than end_date!", call. = FALSE)
7187
}
7288

73-
# check the maximum period of 31 days for Ogimet web page
89+
# check the maximum period of 365 days for Ogimet web page
7490
if(from == "ogimet" & (as.Date(end_date) - as.Date(start_date) > 365)) {
7591
stop("pmetar package error: Period longer than 365 days for the Ogimet source!", call. = FALSE)
7692
}
@@ -165,13 +181,15 @@ metar_get_historical <- function(airport = "EPWA",
165181
colClasses = rep("character", 7))
166182
# check if ds is a data frame and number of rows is greater than 0
167183
if (!is.data.frame(ds) | nrow(ds) == 0) {
168-
stop("pmetar package error: Malformed answer from the server www.ogimet.com! ", call. = FALSE)
184+
#stop("pmetar package error: Malformed answer from the server www.ogimet.com! ", call. = FALSE)
185+
return(invisible(NULL))
169186
}
170187
# check if there are all columns needed in ds
171188
if (!("ANO" %in% names(ds)) | !("MES" %in% names(ds)) | !("DIA" %in% names(ds)) |
172189
!("HORA" %in% names(ds)) | !("MINUTO" %in% names(ds)) | !("PARTE" %in% names(ds))) {
173-
stop("pmetar package error: Missing columns in the answer from the server www.ogimet.com! ",
174-
call. = FALSE)
190+
# stop("pmetar package error: Missing columns in the answer from the server www.ogimet.com! ",
191+
# call. = FALSE)
192+
return(invisible(NULL))
175193
}
176194
out <- ds %>%
177195
dplyr::mutate(metar_reports = paste0(ANO, MES, DIA, HORA, MINUTO, " ", PARTE)) %>%
@@ -181,12 +199,14 @@ metar_get_historical <- function(airport = "EPWA",
181199
ds <- utils::read.csv((textConnection(myfile)), stringsAsFactors = FALSE)
182200
# check if ds is a data frame and number of rows is greater than 0
183201
if (!is.data.frame(ds) | ncol(ds) != 3 | nrow(ds) == 0) {
184-
stop("pmetar package error: Malformed answer from the server mesonet.agron.iastate.edu!", call. = FALSE)
202+
#stop("pmetar package error: Malformed answer from the server mesonet.agron.iastate.edu!", call. = FALSE)
203+
return(invisible(NULL))
185204
}
186205
# check if there are all columns needed in ds
187206
if (!("station" %in% names(ds)) | !("valid" %in% names(ds)) | !("metar" %in% names(ds))) {
188-
stop("pmetar package error: Missing columns in the answer from the server www.ogimet.com! ",
189-
call. = FALSE)
207+
# stop("pmetar package error: Missing columns in the answer from the server www.ogimet.com! ",
208+
# call. = FALSE)
209+
return(invisible(NULL))
190210
}
191211
ds[,2] <- stringr::str_replace_all(ds[,2], "[[:punct:]]", "")
192212
ds[,2] <- stringr::str_replace_all(ds[,2], " ", "")

0 commit comments

Comments
 (0)