Search code examples
rapijsonlite

USNO REST API in R


I am trying to extract moonrise and set times from the US Naval Observatory Rest API using the htrr and jsonlite packages. However, I keep getting an error saying, please supply dates in an MM/DD/YYYY format. Which I believe I am, so quite confused.

library(httr)
library(jsonlite)

GET("https://aa.usno.navy.mil/api/rstt/oneday?date=09/20/2005&coords=47.60,-122.33")

Solution

  • The message is confusing, but it seems that we may need to pass in the YYYY-MM-DD format

    out <- GET("https://aa.usno.navy.mil/api/rstt/oneday?date=2005-09-20&coords=47.60,-122.33")
    

    -output

    out
    Response [https://aa.usno.navy.mil/api/rstt/oneday?date=2005-09-20&coords=47.60,-122.33]
      Date: 2022-07-24 18:43
      Status: 200
      Content-Type: application/json
      Size: 1.26 kB
    {
      "apiversion": "3.0.0", 
      "geometry": {
        "coordinates": [
          -122.33, 
          47.6
        ], 
        "type": "Point"
      }, 
      "properties": {
    ...
    

    Then, extract the content

    library(dplyr)
    library(rrapply)
    > rrapply(content(out)$properties$data, how = "bind")
      closestphase.day closestphase.month closestphase.phase closestphase.time closestphase.year       curphase day day_of_week fracillum isdst label
    1               18                  9          Full Moon             02:01              2005 Waning Gibbous  20     Tuesday       92% FALSE  NULL
      month moondata.1.phen moondata.1.time moondata.2.phen moondata.2.time moondata.3.phen moondata.3.time sundata.1.phen sundata.1.time
    1     9            Rise           02:57   Upper Transit           09:57             Set           17:15            Set          02:13
          sundata.2.phen sundata.2.time       sundata.3.phen sundata.3.time sundata.4.phen sundata.4.time sundata.5.phen sundata.5.time tz year
    1 End Civil Twilight          02:43 Begin Civil Twilight          13:23           Rise          13:54  Upper Transit          20:03  0 2005
    

    By checking the webpage, it is more evident i.e. format specified seems to be incorrect

    enter image description here