Passer au contenu

Creates and adds columns for time to, and time from sunrise/sunset based on a column of POSIXct dates/times dataframe must contain latitude, longitude, and a date/time variable

Utilisation

timeToSunriset(
  data,
  lat = "recvDeployLat",
  lon = "recvDeployLon",
  ts = "ts",
  units = "hours"
)

Arguments

data

a selected table from .motus data, eg. "alltagsGPS", or a data.frame of detection data including at a minimum variables for date/time, latitude, and longitude

lat

variable with latitude values, defaults to recvDeployLat

lon

variable with longitude values, defaults to recvDeployLon

ts

variable with time in UTC as numeric or POSIXct, defaults to ts

units

units to display time difference, defaults to "hours", options include "secs", "mins", "hours", "days", "weeks"

Valeur de retour

the original dataframe provided, with the following additional columns:

  • sunrise: sunrise time for the date and location provided by ts and recvDeployLat/recvDeployLon per row

  • sunset: sunset time for the date and location provided by ts and recvDeployLat/recvDeployLon per row

  • ts_to_set: time to next sunset after "ts", units default to "hours"

  • ts_since_set: time to previous sunset since "ts", units default to "hours"

  • ts_to_rise: time to next sunrise after "ts", units default to "hours"

  • ts_since_rise: time to previous sunrise since "ts", units default to "hours"

Exemples

# You can use either a selected tbl from .motus eg. "alltagsGPS", or a
# data.frame, instructions to convert a .motus file to all formats are below.

# download and access data from project 176 in sql format
# usename and password are both "motus.sample"
if (FALSE) sql.motus <- tagme(176, new = TRUE, update = TRUE)

# OR use example sql file included in `motus`
sql.motus <- tagme(176, update = FALSE, 
                   dir = system.file("extdata", package = "motus"))

# convert sql file "sql.motus" to a tbl called "tbl.alltags"
library(dplyr)
tbl.alltags <- tbl(sql.motus, "alltagsGPS")

# convert the tbl "tbl.alltags" to a data.frame called "df.alltags"
# let's also filter down to one day
df.alltags <- tbl.alltags %>% 
  collect() %>% 
  mutate(time = lubridate::as_datetime(tsCorrected),
         date = lubridate::as_date(time)) %>%
  filter(date == "2015-10-31") %>%
  as.data.frame()

# Get sunrise and sunset information with units in minutse
sunrise <- timeToSunriset(df.alltags, units = "mins")

# Get sunrise and sunset information with units in hours using gps lat/lon
# using data.frame df.alltags. NOTE: This only works if there are non-NA
# gpsLat/gpsLon
if (FALSE) sunrise <- timeToSunriset(df.alltags, lat = "gpsLat", lon = "gpsLon")