Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # function to derive sunrise/set times for Pat and Melanie
- # load R source and then call as:
- # times<-getSunTimes()
- # need to make sure that the excel dates are in the right format (see below) before csv export
- getSunTimes<-function(){
- # load dependencies
- library(rgdal)
- library(maptools)
- # set up variables for coordinate conversion
- ukgrid ="+init=epsg:27700"
- latlong ="+init=epsg:4326"
- # read in csv data (Easting, Northing, Date (in format YYYY-MM-DD))
- df<-read.csv(file.choose(), header=TRUE)
- # convert dates to POSIXct
- df$Date<-as.POSIXct(as.character(df$Date))
- # convert coordinates to lat long
- # extract coords
- coords <-cbind(Easting =as.numeric(as.character(df$Easting)), Northing =as.numeric(as.character(df$Northing)))
- # convert to SP
- coords_BNG<-SpatialPoints(coords, proj4string=CRS(ukgrid))
- # convert to Lat Long
- coords_LL<-spTransform(coords_BNG, CRS(latlong))
- # update column names
- colnames(coords_LL@coords)[colnames(coords_LL@coords)=="Easting"]<-"Longitude"
- colnames(coords_LL@coords)[colnames(coords_LL@coords)=="Northing"]<-"Latitude"
- # derive times of sunrise and sunset
- # sunrise times
- sr<-sunriset(coords_LL, df$Date, direction="sunrise", POSIXct.out=TRUE)
- # sunset times
- ss<-sunriset(coords_LL, df$Date, direction="sunset", POSIXct.out=TRUE)
- # return new dataframe with added sunrise and set times
- output<-cbind(df, sr$time, ss$time)
- names(output)[4] <- "Sunrise"
- names(output)[5] <- "Sunset"
- return(output)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement