Advertisement
Guest User

Untitled

a guest
May 27th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1.  
  2. # function to derive sunrise/set times for Pat and Melanie
  3. # load R source and then call as:
  4. # times<-getSunTimes()
  5. # need to make sure that the excel dates are in the right format (see below) before csv export
  6.  
  7. getSunTimes<-function(){
  8.  
  9. # load dependencies
  10. library(rgdal)
  11. library(maptools)
  12.  
  13. # set up variables for coordinate conversion
  14.  
  15. ukgrid ="+init=epsg:27700"
  16. latlong ="+init=epsg:4326"
  17.  
  18. # read in csv data (Easting, Northing, Date (in format YYYY-MM-DD))
  19. df<-read.csv(file.choose(), header=TRUE)
  20.  
  21. # convert dates to POSIXct
  22. df$Date<-as.POSIXct(as.character(df$Date))
  23.  
  24. # convert coordinates to lat long
  25. # extract coords
  26. coords <-cbind(Easting =as.numeric(as.character(df$Easting)), Northing =as.numeric(as.character(df$Northing)))
  27. # convert to SP
  28. coords_BNG<-SpatialPoints(coords, proj4string=CRS(ukgrid))
  29. # convert to Lat Long
  30. coords_LL<-spTransform(coords_BNG, CRS(latlong))
  31. # update column names
  32. colnames(coords_LL@coords)[colnames(coords_LL@coords)=="Easting"]<-"Longitude"
  33. colnames(coords_LL@coords)[colnames(coords_LL@coords)=="Northing"]<-"Latitude"
  34.  
  35. # derive times of sunrise and sunset
  36. # sunrise times
  37. sr<-sunriset(coords_LL, df$Date, direction="sunrise", POSIXct.out=TRUE)
  38. # sunset times
  39. ss<-sunriset(coords_LL, df$Date, direction="sunset", POSIXct.out=TRUE)
  40.  
  41. # return new dataframe with added sunrise and set times
  42. output<-cbind(df, sr$time, ss$time)
  43. names(output)[4] <- "Sunrise"
  44. names(output)[5] <- "Sunset"
  45.  
  46. return(output)
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement