1
votes

I've got thousands of location points (latitude and longitude) with timestamps (YYYY-MM-DD HH:MM:SS) that I need the sunrise and sunset times for each position.

Tried doing this in in the R package "suncalc" but the examples given in the vignette accompanying the package are not practical real-world examples and gives no obvious solution to the coding I can use for my specific need.

First I tried the following code, which works great for just one date and one location:

> getSunlightTimes(date = date("2019-05-12"), lat = 24, lon = 28, keep = c("sunrise", "sunset"), tz = "CET") 

    date lat lon             sunrise              sunset
1 2019-05-12  24  28 2019-05-12 05:28:29 2019-05-12 18:42:55

Then I try run it with a few more dates and coordinates:

data <- data.frame(date = c("2019-05-12", "2019-05-13", "2019-05-14"),
               lat = c(-24, -25, -26),
               lon = c(28, 29, 20))
getSunlightTimes(data = data, 
keep = c("sunrise", "sunset"), tz = "CET")

I would expect to get a result with the sunrise and sunset times for each of the three locations (e.g. one result for -24, 28 on 2019-05-12, another for -25, 29 on the 2019-05-13 etc), alas instead I get:

Error in getSunlightTimes(data = data, keep = c("sunrise", "sunset"),  : date must to be a Date object (class Date)

Anyone?

1

1 Answers

0
votes

You need to use as.Date to create multiple dates:

data <- data.frame(date = as.Date(c("2019-05-12", "2019-05-13", "2019-05-14")),
lat = c(-24, -25, -26), lon = c(28, 29, 20))