Skip to content

Commit

Permalink
Merge pull request #46 from bczernecki/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
bczernecki authored Jul 30, 2020
2 parents cc816dd + 15cae3a commit d948c27
Show file tree
Hide file tree
Showing 9 changed files with 356 additions and 308 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: climate
Title: Interface to Download Meteorological (and Hydrological) Datasets
Version: 0.9.7
Version: 0.9.8
Authors@R: c(person(given = "Bartosz",
family = "Czernecki",
role = c("aut", "cre"),
Expand Down
8 changes: 6 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# climate 0.9.7
# climate 0.9.8

* Adding informative message if problems with NOAA hourly dataset occur
* Informative message if problems with downloading detected
* Informative message if problems with downloading detected for non-IMGW dataset

# climate 0.9.7

* stop working if no internet connection detected

# climate 0.9.6

Expand Down
12 changes: 10 additions & 2 deletions R/meteo_noaa_co2.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,16 @@ meteo_noaa_co2 <- function(){
temp = tempfile()
test_url(link = base_url, output = temp)

co2 = read.table(temp, na.strings = "-99.99")
colnames(co2) = c("yy", "mm", "yy_d","co2_avg", "co2_interp", "co2_seas", "ndays")
# run only if downloaded file is valid
co2 = NULL
if(!is.na(file.size(temp)) & (file.size(temp) > 800)) {

co2 = read.table(temp, na.strings = "-99.99")
colnames(co2) = c("yy", "mm", "yy_d","co2_avg", "co2_interp", "co2_seas", "ndays")

} else {
cat(paste0("Service not working or problems with internet connection. Check url:\n", base_url))
}

unlink(temp)
return(co2)
Expand Down
10 changes: 10 additions & 0 deletions R/nearest_stations_noaa.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ nearest_stations_nooa <- function(country,
#a <- getURL(linkpl2)
temp = tempfile()
test_url(link = linkpl2, output = temp)

# check connection:
if(!is.na(file.size(temp)) & (file.size(temp) > 800)) {

a = readLines(temp)
a = trimws(a, which = "right")
b = strsplit(a, " ")
Expand Down Expand Up @@ -144,5 +148,11 @@ nearest_stations_nooa <- function(country,

}


} else { # end of checking connection
cat(paste0("Service not working, wrong query or problems with internet connection.\n"))
result = NULL
}

return(result)
}
114 changes: 62 additions & 52 deletions R/nearest_stations_ogimet.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,62 +62,70 @@ nearest_stations_ogimet <- function(country = "United+Kingdom",
#a <- getURL(linkpl2)
temp = tempfile()
test_url(link = linkpl2, output = temp)
a = readLines(temp)
a = paste(a, sep="", collapse="")

b <- strsplit(a, "Decoded synops since")
# run only if downloaded file is valid
if(!is.na(file.size(temp)) & (file.size(temp) > 0)) {

b1 <- lapply(b, function(x) substr(x, 1, 400))
b1[[1]] <- b1[[1]][-1] # header

b21 <- unlist(lapply(gregexpr('Lat=', b1[[1]], fixed = TRUE), function(x) x[1]))

pattern <- paste0(" (", gsub(x = number_countries, pattern = "+", replacement = " ", fixed = TRUE))
b22 <- unlist(lapply(gregexpr(pattern = pattern, b1[[1]], fixed = TRUE), function(x) x[1]))

b1 <- data.frame(str = b1[[1]], start = b21, stop = b22, stringsAsFactors = FALSE)

res <- substr(b1$str, b1$start, b1$stop)

station_names <- unlist(lapply(strsplit(res, " - "), function(x) x[length(x)]))


res <- gsub(x = res, pattern = ", CAPTION, '", replacement = '', fixed = TRUE)
res <- gsub(x = res, pattern = " m'", replacement = ' ', fixed = TRUE)
res <- gsub(x = res, pattern = " - ", replacement = ' ', fixed = TRUE)
res <- gsub(x = res, pattern = "Lat=", replacement = '', fixed = TRUE)
res <- gsub(x = res, pattern = "Lon=", replacement = ' ', fixed = TRUE)
res <- gsub(x = res, pattern = "Alt=", replacement = ' ', fixed = TRUE)

res <- suppressWarnings(do.call("rbind", strsplit(res, " ")))

res1 <- res[,c(1,3,5:7)]

lat <- as.numeric(substr(res1[, 1], 1, 2)) +
(as.numeric(substr(res1[,1], 4, 5))/100) * 1.6667

lon_hemisphere <- gsub("[0-9]", "\\1", res1[, 2])
lon_hemisphere <- gsub("-", "", lon_hemisphere)
lon_hemisphere <- ifelse(lon_hemisphere == "W", -1, 1)

lat_hemisphere <- gsub("[0-9]", "\\1", res1[, 1])
lat_hemisphere <- gsub("-", "", lat_hemisphere)
lat_hemisphere <- ifelse(lat_hemisphere == "S", -1, 1)

lon <- as.numeric(substr(res1[, 2], 1, 3)) + (as.numeric(substr(res1[, 2], 5, 6)) / 100)*1.6667
lon <- lon*lon_hemisphere

lat <- as.numeric(substr(res1[, 1], 1, 2)) + (as.numeric(substr(res1[, 1], 4, 5)) / 100)*1.6667
lat <- lat * lat_hemisphere
a = readLines(temp)
a = paste(a, sep="", collapse="")

b <- strsplit(a, "Decoded synops since")

b1 <- lapply(b, function(x) substr(x, 1, 400))
b1[[1]] <- b1[[1]][-1] # header

b21 <- unlist(lapply(gregexpr('Lat=', b1[[1]], fixed = TRUE), function(x) x[1]))

pattern <- paste0(" (", gsub(x = number_countries, pattern = "+", replacement = " ", fixed = TRUE))
b22 <- unlist(lapply(gregexpr(pattern = pattern, b1[[1]], fixed = TRUE), function(x) x[1]))

b1 <- data.frame(str = b1[[1]], start = b21, stop = b22, stringsAsFactors = FALSE)

res <- substr(b1$str, b1$start, b1$stop)

station_names <- unlist(lapply(strsplit(res, " - "), function(x) x[length(x)]))


res <- gsub(x = res, pattern = ", CAPTION, '", replacement = '', fixed = TRUE)
res <- gsub(x = res, pattern = " m'", replacement = ' ', fixed = TRUE)
res <- gsub(x = res, pattern = " - ", replacement = ' ', fixed = TRUE)
res <- gsub(x = res, pattern = "Lat=", replacement = '', fixed = TRUE)
res <- gsub(x = res, pattern = "Lon=", replacement = ' ', fixed = TRUE)
res <- gsub(x = res, pattern = "Alt=", replacement = ' ', fixed = TRUE)

res <- suppressWarnings(do.call("rbind", strsplit(res, " ")))

res1 <- res[,c(1,3,5:7)]

lat <- as.numeric(substr(res1[, 1], 1, 2)) +
(as.numeric(substr(res1[,1], 4, 5))/100) * 1.6667

lon_hemisphere <- gsub("[0-9]", "\\1", res1[, 2])
lon_hemisphere <- gsub("-", "", lon_hemisphere)
lon_hemisphere <- ifelse(lon_hemisphere == "W", -1, 1)

lat_hemisphere <- gsub("[0-9]", "\\1", res1[, 1])
lat_hemisphere <- gsub("-", "", lat_hemisphere)
lat_hemisphere <- ifelse(lat_hemisphere == "S", -1, 1)

lon <- as.numeric(substr(res1[, 2], 1, 3)) + (as.numeric(substr(res1[, 2], 5, 6)) / 100)*1.6667
lon <- lon*lon_hemisphere

lat <- as.numeric(substr(res1[, 1], 1, 2)) + (as.numeric(substr(res1[, 1], 4, 5)) / 100)*1.6667
lat <- lat * lat_hemisphere

res <- data.frame(wmo_id = res1[, 4], station_names = station_names,
lon = lon, lat = lat, alt = as.numeric(res1[, 3]))
result=rbind(result,res)
} else {
result = NULL
cat(paste("Wrong name of a country. Please check countries names at
https://ogimet.com/display_stations.php?lang=en&tipo=AND&isyn=&oaci=&nombre=&estado=&Send=Send"))
} # end of checking internet connection

res <- data.frame(wmo_id = res1[, 4], station_names = station_names,
lon = lon, lat = lat, alt = as.numeric(res1[, 3]))
result=rbind(result,res)
}
if (dim(result)[1]==0) {
stop("Wrong name of a country. Please check countries names at
https://ogimet.com/display_stations.php?lang=en&tipo=AND&isyn=&oaci=&nombre=&estado=&Send=Send")
}

if (!is.null(result)) {

point = as.data.frame(t(point))
names(point) = c("lon", "lat")
Expand Down Expand Up @@ -176,6 +184,8 @@ nearest_stations_ogimet <- function(country = "United+Kingdom",

}

} # end of checking whether result is null

return(result)
}

Loading

0 comments on commit d948c27

Please sign in to comment.