-
Notifications
You must be signed in to change notification settings - Fork 2
fix #135 read local vpts files #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4ac688a
fix #135
a087b2f
more documentation on formatter
98d57a7
correct caller env
ff7634c
test fix
b492038
Merge branch '135-using-local-files-as-source-in-get_vpts' into fix-m…
bart1 3d64bd0
Merge pull request #155 from aloftdata/fix-merge-request
bart1 681a6e5
Merge pull request #159 from aloftdata/main
bart1 71f3920
Merge branch 'main' into 135-using-local-files-as-source-in-get_vpts
698c96f
update news to correct version
2ebe201
Merge branch 'main' into 135-using-local-files-as-source-in-get_vpts
36cc8a6
clarify functioning
b93df9f
update error message
515bab4
work with with_tempdir
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| get_vpts_local <- function( | ||
| radar, | ||
| rounded_interval, | ||
| directory, | ||
| ..., | ||
| call = rlang::caller_env() | ||
| ) { | ||
| dates <- as.Date(seq( | ||
| lubridate::int_start(rounded_interval), | ||
| lubridate::int_end(rounded_interval), | ||
| "day" | ||
| )) | ||
| file_paths_list <- radar |> | ||
| purrr::map( | ||
| ~ unique(glue::glue( | ||
| getOption( | ||
| "getRad.vpts_local_path_format", | ||
| default = "{radar}/{year}/{radar}_vpts_{year}{month}.csv.gz" | ||
| ), | ||
| radar = .x, | ||
| year = lubridate::year(dates), | ||
| month = sprintf("%02i", lubridate::month(dates)), | ||
| day = sprintf("%02i", lubridate::day(dates)), | ||
| date = dates | ||
| )) | ||
| ) |> | ||
| purrr::set_names(radar) | ||
| # `full_paths_list` is a list of file paths per radar, so that one vpts per radar is calculated | ||
| full_paths_list <- purrr::map(file_paths_list, ~ file.path(directory, .x)) | ||
| full_paths_exist_list <- purrr::map(full_paths_list, file.exists) | ||
| if (all(!unlist(full_paths_exist_list))) { | ||
| cli::cli_abort( | ||
| c( | ||
| x = "None of the expected files are in the source directory ({.file {directory}}).", | ||
| i = "The following files were expected: {.file {unlist(full_paths_list)}}." | ||
| ), | ||
| class = "getRad_error_files_not_in_source_dir", | ||
| call = call | ||
| ) | ||
| } | ||
| if (any(!unlist(full_paths_exist_list))) { | ||
| missing_files <- unlist(purrr::map2( | ||
| full_paths_list, | ||
| full_paths_exist_list, | ||
| ~ .x[!.y] | ||
| )) | ||
| cli::cli_warn( | ||
| c( | ||
| x = "Some of the expected files are in the source directory ({.file {directory}}).", | ||
| i = "The following files were expected but not found: {.file {missing_files}}.", | ||
| i = "These files are considered missing data and therefore omitted from the results." | ||
| ), | ||
| missing_files = missing_files, | ||
| class = "getRad_warning_some_files_not_in_source_dir", | ||
| call = call | ||
| ) | ||
| } | ||
| any_file <- purrr::map_lgl(full_paths_exist_list, any) | ||
| purrr::map2( | ||
| full_paths_list[any_file], | ||
| full_paths_exist_list[any_file], | ||
| ~ vroom::vroom( | ||
| .x[.y], | ||
| col_types = getOption( | ||
| "getRad.vpts_col_types" | ||
| ), | ||
| show_col_types = NULL, | ||
| progress = FALSE | ||
| ) |> | ||
| tibble::add_column(source = directory) |> | ||
| dplyr::mutate(dplyr::across("radar", as.character)) | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| withr::with_tempdir({ | ||
| dir <- "./" | ||
| skip_if_offline("aloftdata.s3-eu-west-1.amazonaws.com") | ||
| dir.create(file.path(dir, "local_tests", "bewid", "2016"), recursive = T) | ||
| dir.create(file.path(dir, "local_tests", "bejab", "2016"), recursive = T) | ||
| local_dir <- file.path(dir, "local_tests") | ||
| files <- c( | ||
| "bewid/2016/bewid_vpts_20160201.csv", | ||
| "bewid/2016/bewid_vpts_20160202.csv", | ||
| "bejab/2016/bejab_vpts_20160201.csv", | ||
| "bejab/2016/bejab_vpts_20160202.csv" | ||
| ) | ||
| for (i in files) { | ||
| download.file( | ||
| paste0("https://aloftdata.s3-eu-west-1.amazonaws.com/uva/daily/", i), | ||
| file.path(dir, "local_tests", i), | ||
| quiet = TRUE | ||
| ) | ||
| } | ||
| test_that("can read data", { | ||
| withr::with_options( | ||
| c( | ||
| "getRad.vpts_local_path_format" = "{radar}/{year}/{radar}_vpts_{year}{month}{day}.csv" | ||
| ), | ||
| { | ||
| expect_s3_class( | ||
| get_vpts("bewid", as.Date("2016-2-1"), source = local_dir), | ||
| class = "vpts" | ||
| ) | ||
| expect_type( | ||
| ret <- get_vpts( | ||
| c("bewid", "bejab"), | ||
| as.Date("2016-2-1"), | ||
| source = local_dir | ||
| ), | ||
| "list" | ||
| ) | ||
| expect_length(ret, 2L) | ||
| expect_s3_class(ret[[1]], class = "vpts") | ||
| expect_s3_class(ret[[2]], class = "vpts") | ||
| } | ||
| ) | ||
| }) | ||
|
|
||
| test_that("errors outside of range or directory with corect format", { | ||
| withr::with_options( | ||
| c( | ||
| "getRad.vpts_local_path_format" = "{radar}/{year}/{radar}_vpts_{year}{month}{day}.csv" | ||
| ), | ||
| { | ||
| expect_error( | ||
| get_vpts("bewid", as.Date("2016-2-1"), source = dir), | ||
| class = "getRad_error_files_not_in_source_dir" | ||
| ) | ||
| expect_error( | ||
| get_vpts("bewid", as.Date("2016-3-1"), source = local_dir), | ||
| class = "getRad_error_files_not_in_source_dir" | ||
| ) | ||
| expect_warning( | ||
| ret <- get_vpts( | ||
| "bewid", | ||
| as.Date("2016-2-1") + -3:1, | ||
| source = local_dir | ||
| ), | ||
| class = "getRad_warning_some_files_not_in_source_dir" | ||
| ) | ||
| expect_s3_class(ret, "vpts") | ||
|
|
||
| expect_warning( | ||
| ret <- get_vpts( | ||
| c("bewid", "behav"), | ||
| as.Date("2016-2-1"), | ||
| source = local_dir | ||
| ), | ||
| class = "getRad_warning_some_files_not_in_source_dir" | ||
| ) | ||
| expect_s3_class(ret, "vpts") | ||
|
|
||
| expect_true(all(grepl( | ||
| "local_tests/behav/2016/behav_vpts_2016020[1|2].csv", | ||
| rlang::catch_cnd( | ||
| get_vpts( | ||
| c("bewid", "behav"), | ||
| as.Date("2016-2-1"), | ||
| source = local_dir | ||
| ), | ||
| class = "getRad_warning_some_files_not_in_source_dir" | ||
| )$missing_files | ||
| ))) | ||
| } | ||
| ) | ||
| }) | ||
|
|
||
| test_that("error on non existing files", { | ||
| expect_error( | ||
| get_vpts("bewid", as.Date("2016-2-1"), source = local_dir), | ||
| class = "getRad_error_files_not_in_source_dir" | ||
| ) | ||
| expect_error( | ||
| get_vpts("bewid", as.Date("2016-2-10"), source = local_dir), | ||
| class = "getRad_error_files_not_in_source_dir" | ||
| ) | ||
| # test if by default the aloft file structure is used | ||
| expect_error( | ||
| get_vpts("bewid", as.Date("2016-2-10"), source = local_dir), | ||
| "local_tests/bewid/2016/bewid_vpts_201602.csv.gz" | ||
| ) | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.