Skip to content

Commit

Permalink
add date conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
dramanica committed Oct 12, 2023
1 parent 8b7b4f6 commit bb1c7b6
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export(bioclim_vars)
export(clean_data_path)
export(climate_for_locations)
export(climate_for_time_slice)
export(date2ybp)
export(df_from_region_series)
export(df_from_region_slice)
export(distance_from_sea)
Expand Down Expand Up @@ -36,6 +37,7 @@ export(time_series_for_locations)
export(update_dataset_list)
export(validate_nc)
export(var_labels)
export(ybp2date)
exportMethods("time_bp<-")
exportMethods(bioclim_vars)
exportMethods(time_bp)
Expand Down
2 changes: 1 addition & 1 deletion R/download_etopo.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ download_etopo <- function(path = NULL, resolution = 60) {
} else if (resolution == 60) {
etopo_url <- "https://www.ngdc.noaa.gov/thredds/fileServer/global/ETOPO2022/60s/60s_bed_elev_netcdf/ETOPO_2022_v1_60s_N90W180_bed.nc"
} else {
stop("resolution should be one of 30 or 60 arcsecs")
stop("resolution should be one of 30 or 60 arc-secs")
}

# download the files
Expand Down
32 changes: 32 additions & 0 deletions R/ybp2date.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#' Convert years BP from pastclim to lubridate date, or vice versa
#'
#' These functions convert between years BP as used by pastclim (negative
#' numbers going into
#' the past, positive into the future) and standard
#' `POSIXct` date objects.
#' @param x a time in years BP using the `pastclim` convention of
#' negative numbers indicating years into the past, or a `POSIXct` date object
#' @returns a `POSIXct` date object, or a vector
#' @examples
#' ybp2date(-10000)
#' ybp2date(0)
#' # back and forth
#' date2ybp(ybp2date(-10000))
#'
#' @export

ybp2date <- function(x) {
if (!is.numeric(x)) {
stop("x should be numeric")
}
lubridate::date_decimal(x + 1950)
}

#' @rdname ybp2date
#' @export
date2ybp <- function(x) {
if (!inherits(x, "POSIXct")) {
stop("x should be a POSIXct object")
}
lubridate::year(x) - 1950
}
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ kyr
lai
landmask
longname
lubridate
mis
nc
netcdf
Expand Down
3 changes: 1 addition & 2 deletions man/download_etopo.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/location_slice_from_region_series.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions man/ybp2date.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bb1c7b6

Please sign in to comment.