In [87]:
library(devtools)

In [88]:
devtools::install_github("trenchproject/microclimRapi")


Skipping install of 'microclimRapi' from a github remote, the SHA1 (d5ef4703) has not changed since last install.
  Use `force = TRUE` to force installation

In [95]:
library(microclimRapi)

In [96]:
api_token = getToken('0eda0dd88d1e6b417e8e9bebfc02cd95','a0fc61db2d657c5359c775b62bffb669','http://microclim.org/')

In [97]:
print(api_token)


[1] "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjExMzAwMjIxMzEyOTA3NDkxNjUyMCIsImlhdCI6MTQ5MDMzMTM0NCwiZXhwIjoxNDkwMzM4NTQ0fQ.0lCxo2fUlvYRZJ8Xuhi1NH6-E_QJUSUTZ4QG0KIkRHE"

API token received, now place the request for extraction


In [98]:
ma <- microclimRapi:::MicroclimAPI$new(token = api_token,url_mc='http://microclim.org/')

In [99]:
# Eagle Nest Wilderness Area - Colorado
mr <- microclimRapi:::MicroclimRequest$new(
                  latS = "39.48178546986059",
                  latN="39.890772566959534",
                  lonW="-106.51519775390625",
                  lonE="-106.03317260742188",
                  variable="ALBEDO",
                  shadelevel=0,
                  hod=0,
                  interval=0,
                  aggregation=0,
                  stdate="19810101",
                  eddate="19810131",
                  file="csv")

In [100]:
# Place a request to extract
ext_req= ma$request(mr)

In [101]:
print(ext_req)


$success
[1] "Request logged"

$request_id
[1] "58d4a6e1ab7fb6e9cfd4d98b"


In [102]:
print(ext_req$request_id)


[1] "58d4a6e1ab7fb6e9cfd4d98b"

In [103]:
ma$status(ext_req$request_id)


$status = 'EMAILED'

If status is EMAILED, then files can be downloaded


In [114]:
ftch_req=''
#Pull the files
if(ma$status(ext_req$request_id) == "EMAILED")
{
  # place a request to fetch the files
  ftch_req= ma$fetch(ext_req$request_id)
}

In [115]:
ftch_req


$files =
  1. $Key
    '58d4a6e1ab7fb6e9cfd4d98b/ALBEDO_output_interval-hourly_aggregation-inst_times-19810101-19810131_created-2017-03-24-0456.csv'
    $LastModified
    '2017-03-24T04:56:03.000Z'
    $ETag
    '"deffa2962461ba35499f2c6708c4065d"'
    $Size
    58211
    $StorageClass
    'STANDARD'
    $Owner
    $DisplayName
    'lbuckley'
    $ID
    'd291690f719ecbd4e40fbb2a353ef51286b06e399e6b55a121e4dd64957a3460'
  2. $Key
    '58d4a6e1ab7fb6e9cfd4d98b/ALBEDO_output_interval-hourly_aggregation-inst_times-19810101-19810131_created-2017-03-24-0456.nc'
    $LastModified
    '2017-03-24T04:56:03.000Z'
    $ETag
    '"f0edfb5ce11d20f4543404e2948ea5ea"'
    $Size
    9844
    $StorageClass
    'STANDARD'
    $Owner
    $DisplayName
    'lbuckley'
    $ID
    'd291690f719ecbd4e40fbb2a353ef51286b06e399e6b55a121e4dd64957a3460'

In [106]:
ftch_req$files[[1]]$Key


'58d4a6e1ab7fb6e9cfd4d98b/ALBEDO_output_interval-hourly_aggregation-inst_times-19810101-19810131_created-2017-03-24-0456.csv'

In [109]:
ncD <-  ma$download(ext_req$request_id,ftch_req$files[[1]]$Key)

In [110]:
#download the csv file
writeBin(ncD, strsplit(ftch_req$files[[1]]$Key, "/")[[1]][2])

In [111]:
file.exists(strsplit(ftch_req$files[[1]]$Key, "/")[[1]][2])


TRUE

In [112]:
lv <- read.csv(strsplit(ftch_req$files[[1]]$Key, "/")[[1]][2],comment.char = "#")

In [113]:
str(lv)


'data.frame':	1488 obs. of  4 variables:
 $ datetime: int  1981010101 1981010101 1981010102 1981010102 1981010103 1981010103 1981010104 1981010104 1981010105 1981010105 ...
 $ lat     : num  39.5 39.8 39.5 39.8 39.5 ...
 $ lon     : num  -106 -106 -106 -106 -106 ...
 $ ALBEDO  : num  0.382 0.672 0.382 0.672 0.382 ...

In [ ]: