Skip to content
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

Changed 'wind_2min_timestamp' variable type from character to date-ti… #52

Merged
merged 7 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Transfered maintainance of package to Jeremy Weiss
- Timestamp for hourly and daily maximum two-minute sustained wind speeds, `wind_2min_timestamp` now appears in downloaded data
- Variable type for hourly and daily `wind_2min_timestamp` now is date-time instead of character with correct time zone, `tzone = "America/Phoenix"`
- Values for hourly `date_datetime` variable now have `tzone = "America/Phoenix"` assigned

# azmetr 0.2.0

Expand Down
18 changes: 16 additions & 2 deletions R/az_daily.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,23 @@ az_daily <- function(station_id = NULL, start_date = NULL, end_date = NULL) {
#convert NAs
dplyr::mutate(
dplyr::across(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just below this is code to change values like -9999 to NA, but it currently skips wind_2min_timestamp. I think all you need to do is:

...
dplyrr::across(
  c(wind_2min_timestamp, tidyselect(where(is.numeric))), 
  function(x)
  ...

Copy link
Member Author

@jeremylweiss jeremylweiss Jul 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without adding the above code to include wind_2min_timestamp in the conversion of NAs, existing code with wind_2min_timestamp as character automatically converts corresponding nodata values of -99999 to NA while throwing a warning 27 failed to parse, as with

az_daily(start_date = "2023-03-22", end_date = "2023-03-22")

Only one of the 28 stations at that point had wind_2min_timestamp daily values on that date.

I think the nodata value conversion needs to happen somehow in conjunction with character-to-datetime conversion of this variable.

Copy link
Member Author

@jeremylweiss jeremylweiss Jul 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the above code has trouble with wind_2min_timestamp, as it is an object of class "c('POSIXct', 'POSIXt')". This is after the character-to-datetime conversion

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aariq please review solutions in az_daily() and az_hourly() I just pushed on this branch. I think I figured it out

tidyselect::where(is.numeric),
tidyselect::where(is.numeric),
function(x)
dplyr::if_else(x %in% c(-999, -9999, -99999, -7999, 999, 999.9, 9999), NA_real_, x))
dplyr::if_else(x %in% c(-999, -9999, -99999, -7999, 999, 999.9, 9999), NA_real_, x)
)
) %>%
dplyr::mutate(
wind_2min_timestamp = dplyr::if_else(
wind_2min_timestamp == as.character(-99999),
NA_character_,
wind_2min_timestamp
)
) %>%
dplyr::mutate(
wind_2min_timestamp = lubridate::with_tz(
lubridate::parse_date_time(.data$wind_2min_timestamp, orders = "ymdHMSz"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahhh, I understand this now. So pars_date_time does actually understand the timezone, it just converts it automatically to UTC and with_tz() converts it back to MST. Cool!

tzone = "America/Phoenix"
)
)
return(out)
}
22 changes: 21 additions & 1 deletion R/az_hourly.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,33 @@ az_hourly <- function(station_id = NULL, start_date_time = NULL, end_date_time =
as.numeric
)) %>%
dplyr::filter(.data$meta_station_id != "az99") %>%
dplyr::mutate(date_datetime = lubridate::ymd_hms(.data$date_datetime)) %>%
dplyr::mutate(
date_datetime =
lubridate::force_tz(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but here, ymd_hms() doesn't see a timezone and you need force_tz() to keep the time and only change the timezone. Nice!

lubridate::ymd_hms(.data$date_datetime),
tzone = "America/Phoenix"
)
) %>%
#convert NAs
dplyr::mutate(
dplyr::across(
tidyselect::where(is.numeric),
function(x)
dplyr::if_else(x %in% c(-999, -9999, -99999, -7999, 999, 999.9, 9999), NA_real_, x))
) %>%
dplyr::mutate(
wind_2min_timestamp = dplyr::if_else(
wind_2min_timestamp == as.character(-99999),
NA_character_,
wind_2min_timestamp
)
) %>%
dplyr::mutate(
wind_2min_timestamp =
lubridate::with_tz(
lubridate::parse_date_time(.data$wind_2min_timestamp, orders = "ymdHMSz"),
tzone = "America/Phoenix"
)
)
return(out)
}