Skip to content

Commit

Permalink
Merge pull request #76 from bczernecki/dev
Browse files Browse the repository at this point in the history
climate 1.0.5
  • Loading branch information
bczernecki authored Aug 5, 2022
2 parents 67b226c + 3913066 commit 2b6545e
Show file tree
Hide file tree
Showing 71 changed files with 980 additions and 904 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
^vignettes/articles$
^\.github$
^\.Rhistory$
^\.lintr$
8 changes: 7 additions & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ jobs:
run: |
remotes::install_deps(dependencies = TRUE)
remotes::install_cran("rcmdcheck")
remotes::install_cran("covr")
remotes::install_cran("xml2")
shell: Rscript {0}

- name: Session info
Expand All @@ -88,7 +90,7 @@ jobs:
pkgs <- installed.packages()[, "Package"]
sessioninfo::session_info(pkgs, include_base = TRUE)
shell: Rscript {0}

- name: Check
env:
_R_CHECK_CRAN_INCOMING_: false
Expand All @@ -106,3 +108,7 @@ jobs:
with:
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
path: check

- name: Test coverage
run: covr::codecov()
shell: Rscript {0}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ inst/doc
.README.md.swp
climate.Rproj
.DS_Store
covr_report.html
lib
10 changes: 10 additions & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
linters: with_defaults(
line_length_linter = line_length_linter(120),
commented_code_linter = NULL,
absolute_path_linter(),
object_name_linter = NULL,
object_length_linter(length = 50L),
object_usage_linter(source_file = NULL),
cyclocomp_linter(complexity_limit = 33L),
assignment_linter = NULL
)
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

4 changes: 2 additions & 2 deletions 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: 1.0.4
Version: 1.0.5
Authors@R: c(person(given = "Bartosz",
family = "Czernecki",
role = c("aut", "cre"),
Expand All @@ -22,7 +22,7 @@ Description: Automatize downloading of meteorological and hydrological data from
University of Wyoming - atmospheric vertical profiling data (<http://weather.uwyo.edu/upperair/>),
Polish Institute of Meterology and Water Management - National Research Institute (<https://danepubliczne.imgw.pl>),
and National Oceanic & Atmospheric Administration (NOAA).
This package also allows for adding geographical coordinates for each observation.
This package also allows for searching geographical coordinates for each observation and calculate distances to the nearest stations.
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# climate 1.0.5

* `meteo_imgw` family of functions supports multiple names as argument - bug fix
* unit tests and code coverage computed with CI/CD
* units added to column labels for `nearest_stations_` set of functions instead of column names for easier processing (e.g. [km] are visible now only as attributes)
* new functions follow lintr settings


# climate 1.0.4

* Function `spheroid_dist` added to improve accuracy of calculations between points, but also avoid installing GIS dependencies (thanks to @kadyb)
Expand Down
18 changes: 10 additions & 8 deletions R/check_locale.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
#' Check locale
#'
#' This is an extra check for some systems that make use of "C.UTF-8" or any iso-like encoding recognized as causing potential problems;
#' The provided list of checked encoding cannot parse properly characters used in the Polish metservice's repository and therefore will be forced to use ASCII//TRANSLIT
#' This is an extra check for some systems that make use of "C.UTF-8" or any
#' iso-like encoding recognized as causing potential problems;
#' The provided list of checked encoding cannot parse properly characters used
#' in the Polish metservice's repository and therefore will be forced to
#' use ASCII//TRANSLIT
#' @noRd

check_locale = function(){
check_locale = function() {

if (Sys.getlocale("LC_CTYPE") %in% c("C.UTF-8", "en_US.iso885915")) {
locale = Sys.getlocale("LC_CTYPE")
message(paste0(" Your system locale is: " , locale," which may cause trouble.
Please consider changing it manually while working with climate, e.g.:
message(paste0(" Your system locale is: ", locale, " which may cause trouble.
Please consider changing it manually while working with climate, e.g.:
Sys.setlocale(category = 'LC_ALL', locale = 'en_US.UTF-8') "))
Sys.sleep(4)
return(1)
} else {
return(0)
}

}

}
13 changes: 6 additions & 7 deletions R/clean_metadata_hydro.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
#' @param interval temporal interval
#' @importFrom utils read.fwf
#' @keywords internal
clean_metadata_hydro = function(address, interval){

clean_metadata_hydro = function(address, interval) {
temp = tempfile()

test_url(link = address, output = temp)
a = readLines(temp, warn = FALSE)

a = iconv(a, from = "cp1250", to = "ASCII//TRANSLIT") # usuwamy polskie znaki, bo to robi spore "kuku"
a = gsub(a, pattern = "\\?", replacement = "") # usuwamy znaki zapytania powstale po konwersji

# additional workarounds for mac os but not only...
a = gsub(x = a, pattern="'", replacement = "")
a = gsub(x = a, pattern="\\^", replacement = "")
a = gsub(x = a, pattern = "'", replacement = "")
a = gsub(x = a, pattern = "\\^", replacement = "")

if (interval == "monthly") {
b = list(data.frame(parameters = a[3:12])) # sklad danych jeszcze nie wiem jak ominąć problem kontroli
Expand Down
39 changes: 16 additions & 23 deletions R/clean_metadata_meteo.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,32 @@
#' }
#'

clean_metadata_meteo = function(address, rank = "synop", interval = "hourly"){
clean_metadata_meteo = function(address, rank = "synop", interval = "hourly") {

temp = tempfile()


test_url(link = address, output = temp)
a = readLines(temp, warn = FALSE)

a = iconv(a, from = "cp1250", to = "ASCII//TRANSLIT") # usuwamy polskie znaki, bo to robi spore "kuku"
a = gsub(a, pattern = "\\?", replacement = "") # usuwamy znaki zapytania powstale po konwersji

# additional workarounds for mac os but not only...
a = gsub(x = a, pattern="'", replacement = "")
a = gsub(x = a, pattern="\\^0", replacement = "")


# additional workarounds for mac os but not only...
a = gsub(x = a, pattern = "'", replacement = "")
a = gsub(x = a, pattern = "\\^0", replacement = "")
a = data.frame(V1 = a[nchar(a) > 0], stringsAsFactors = FALSE)

# to nie dziala na windowsie:
# this one does not work on windows
# a = suppressWarnings(na.omit(read.fwf(address, widths = c(1000),
# fileEncoding = "CP1250", stringsAsFactors = FALSE)))

length_char = max(nchar(a$V1), na.rm = TRUE)

if(rank == "precip" && interval == "hourly") length_char = 40 # wyjatek dla precipow
if(rank == "precip" && interval == "daily") length_char = 40 # wyjatek dla precipow dobowych
if(rank == "synop" && interval == "hourly") length_char = 60 # wyjatek dla synopow terminowych
if(rank == "climate" && interval == "monthly") length_char = 52 # wyjatek dla synopow terminowych
if (rank == "precip" && interval == "hourly") length_char = 40 # wyjatek dla precipow
if (rank == "precip" && interval == "daily") length_char = 40 # wyjatek dla precipow dobowych
if (rank == "synop" && interval == "hourly") length_char = 60 # wyjatek dla synopow terminowych
if (rank == "climate" && interval == "monthly") length_char = 52 # wyjatek dla synopow terminowych

field = substr(a$V1, length_char - 3, length_char)

if(rank == "synop" && interval == "monthly") {
if (rank == "synop" && interval == "monthly") {
length_char = as.numeric(names(sort(table(nchar(a$V1)), decreasing = TRUE)[1])) + 2
field = substr(a$V1, length_char - 3, length_char + 2)
}
Expand All @@ -56,11 +50,10 @@ clean_metadata_meteo = function(address, rank = "synop", interval = "hourly"){
a$field2 = suppressWarnings(as.numeric(unlist(lapply(strsplit(field, "/"), function(x) x[2]))))

a$V1 = trimws(substr(a$V1, 1, nchar(a$V1) - 3))

strsplit(x = a$V1, split = "/")

#a = a[nchar(a$V1)>2,] # usuwamy puste lub prawie puste wiersze dodatkowo...
a = a[!(is.na(a$field1) & is.na(a$field2)), ] # usuwanie info o statusach
#a = a[nchar(a$V1)>2,] # remove empty or almost empty rows
a = a[!(is.na(a$field1) & is.na(a$field2)), ] # remove info about status
colnames(a)[1] = "parameters"
a
return(a)
}
4 changes: 1 addition & 3 deletions R/co2_demo.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#' @name co2_demo
#' @title Examplary CO2 dataset from Mauna Loa Observatory (NOAA dataset)
#'
#' @title Examplary CO2 dataset from Mauna Loa Observatory (NOAA dataset)
#' @description The object contains pre-downloaded CO2 dataset from Mauna Loa observatory
#' The snapshot was taken 2020/05/05.
#'
#' @importFrom utils data
#'
#' @docType data
Expand Down
5 changes: 2 additions & 3 deletions R/get_coord_from_string.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ get_coord_from_string = function(txt, pattern = "Longitude") {
tmp = trimws(substr(txt, start = start, stop = start + 8))
tmp = strsplit(tmp, "-")[[1]]
hemisphere = gsub("[0-9]", "", strsplit(tmp, "-")[2])
hemisphere = gsub(".*?(\\b[A-Za-z0-9 ]+\\b).*","\\1", hemisphere)
hemisphere = gsub(".*?(\\b[A-Za-z0-9 ]+\\b).*", "\\1", hemisphere)

tmp = suppressWarnings(as.numeric(gsub("([0-9]+).*$", "\\1", strsplit(tmp, "-"))))

wsp = suppressWarnings(as.numeric(tmp)[1] + (as.numeric(tmp)[2] * 5 / 3) / 100)

if( hemisphere %in% c("W","S") ) {
if (hemisphere %in% c("W", "S")) {
wsp = wsp * -1
}
return(wsp)
}

53 changes: 36 additions & 17 deletions R/hydro_imgw.R
Original file line number Diff line number Diff line change
@@ -1,36 +1,55 @@
#' Hydrological data from IMGW
#'
#' Downloading daily, and monthly hydrological data from the measurement stations available in the danepubliczne.imgw.pl collection
#' Downloading daily, and monthly hydrological data from the measurement stations
#' available in the danepubliczne.imgw.pl collection
#'
#' @param interval temporal resolution of the data ("daily" , "monthly", or "semiannual_and_annual")
#' @param interval temporal resolution of the data ("daily" , "monthly",
#' or "semiannual_and_annual")
#' @param year vector of years (e.g., 1966:2000)
#' @param coords add coordinates of the stations (logical value TRUE or FALSE)
#' @param value type of data (can be: state - "H" (default), flow - "Q", or temperature - "T")
#' @param station vector of hydrological stations danepubliczne.imgw.pl; can be given as station name with CAPITAL LETTERS (character)
#' @param value type of data (can be: state - "H" (default), flow - "Q", or
#' temperature - "T")
#' @param station vector of hydrological stations danepubliczne.imgw.pl;
#' can be given as station name with CAPITAL LETTERS (character)
#' It accepts either names (characters in CAPITAL LETTERS) or stations' IDs (numeric)
#' @param col_names three types of column names possible: "short" - default, values with shorten names, "full" - full English description, "polish" - original names in the dataset
#' @param ... other parameters that may be passed to the 'shortening' function that shortens column names
#'
#' @param col_names three types of column names possible: "short" - default,
#' values with shorten names, "full" - full English description,
#' "polish" - original names in the dataset
#' @param ... other parameters that may be passed to the 'shortening'
#' function that shortens column names
#' @export
#' @return A data.frame with columns describing the hydrological parameters (e.g. flow, water level) where each row represent a measurement,
#' depending on the interval, at a given hour, month or year. If `coords = TRUE` additional two columns with geografic coordinates are added.
#'
#' @examples
#' @return A data.frame with columns describing the hydrological parameters
#' (e.g. flow, water level) where each row represent a measurement,
#' depending on the interval, at a given hour, month or year.
#' If `coords = TRUE` additional two columns with geografic coordinates are added.
#' @examples
#' \donttest{
#' x = hydro_imgw("monthly", year = 1999)
#' head(x)
#' }
hydro_imgw = function(interval, year, coords = FALSE, value = "H", station = NULL, col_names = "short", ...){
hydro_imgw = function(interval,
year,
coords = FALSE,
value = "H",
station = NULL,
col_names = "short", ...) {

if (interval == "daily"){
if (interval == "daily") {
# dobowe
calosc = hydro_imgw_daily(year = year, coords = coords, station = station, col_names = col_names, ...)
} else if (interval == "monthly"){
} else if (interval == "monthly") {
#miesieczne
calosc = hydro_imgw_monthly(year = year, coords = coords, station = station, col_names = col_names, ...)
} else if (interval == "semiannual_and_annual"){
calosc = hydro_imgw_monthly(year = year,
coords = coords,
station = station,
col_names = col_names, ...)
} else if (interval == "semiannual_and_annual") {
# polroczne_i_roczne
calosc = hydro_imgw_annual(year = year, coords = coords, value = value, station = station, col_names = col_names, ...)
calosc = hydro_imgw_annual(year = year,
coords = coords,
value = value,
station = station,
col_names = col_names, ...)
} else{
stop("Wrong `interval` value. It should be either 'daily', 'monthly', or 'semiannual_and_annual'.", call. = FALSE)
}
Expand Down
Loading

0 comments on commit 2b6545e

Please sign in to comment.