#' MRP_sol: Mars Rover Photos - Querying by Martian sol
#'
#' Each rover has its own set of photos stored in the database, which can be queried separately.
#' There are several possible queries that can be made against the API. Photos are organized by the sol
#' (Martian rotation or day) on which they were taken, counting up from the rover's landing date.
#' A photo taken on Curiosity's 1000th Martian sol exploring Mars, for example, will have a sol attribute of 1000.
#' If instead you prefer to search by the Earth date on which a photo was taken, you can do that too.
#' Along with querying by date, results can also be filtered by the camera with which it was taken and
#' responses will be limited to 25 photos per call. Queries that should return more than 25 photos will be
#' split onto several pages, which can be accessed by adding a 'page' param to the query.
#'
#' Rover Cameras: Curiousity, Opportunity, Spirit
#'
#' FHAZ: Front Hazard Avoidance Camera -> Curiousity, Opportunity, Spirit
#'
#' RHAZ: Rear Hazard Avoidance Camera -> Curiousity, Opportunity, Spirit
#'
#' MAST: Mast Camera -> Curiousity
#'
#' CHEMCAM: Chemistry and Camera Complex -> Curiousity
#'
#' MAHLI: Mars Hand Lens Imager -> Curiousity
#'
#' MARDI: Mars Descent Imager -> Curiousity
#'
#' NAVCAM: Navigation Camera -> Curiousity, Opportunity, Spirit
#'
#' PANCAM: Panoramic Camera -> Opportunity, Spirit
#'
#' MINITES: Miniature Thermal Emission Spectrometer (Mini-TES) -> Opportunity, Spirit
#'
#' @param key String. Your NASA API key, you can enter your key in the function parameter, but it's not recommended. Instead, you'd better save your key in R environment and call it "NASA_TOKEN". Then the function would automatically acquire your key info.
#' @param sol Integer. sol (ranges from 0 to max found in endpoint). 1000 as default.
#' @param camera String. See detail camera list for abbreviations and availability. "all" as default.
#' @param page Integer. 25 items per page returned. 1 as default.
#' @param type String. "curiosity"/"opportunity"/"spirit". "curiosity" as default.
#' @param download_image Boolean. Download the first(default) image of returned list. FALSE as default.
#' @return Info of Mars Rover photos (and downloaded image).
#' @examples
#' MRP_sol(download_image = TRUE)
#' MRP_sol(camera = "MAST", page = 2)
#' MRP_sol(type = "opportunity")
#' @export
MRP_sol <- function(key = Sys.getenv("NASA_TOKEN"), sol = 1000, camera = "all", page = 1, type = "curiosity", download_image = FALSE){
library(httr)
library(jpeg)
if (type == "curiousity"){
type <- "curiosity"
}
url <- "https://api.nasa.gov/mars-photos/api/v1/rovers/"
if (camera == "all"){
url <- paste(url, type, "/photos?sol=", sol, "&api_key=", key, "&page=", page, sep = "")
}else{
url <- paste(url, type, "/photos?sol=", sol, "&api_key=", key, "&camera=", camera, "&page=", page, sep = "")
}
response <- GET(url)
if (response$status_code != 200){
message("Unsuccessful status of response!")
}
result <- content(response)
if (download_image){
download.file(result$photos[[1]]$img_src, "image.jpg", mode = 'wb')
jpg <- readJPEG("image.jpg", native = TRUE)
plot(0:1, 0:1, type = "n", ann = FALSE, axes = FALSE)
rasterImage(jpg, 0, 0, 1, 1)
}
return(result)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.