This API toolkit gives you an easy wasy to interact with microclim.org which is a microclimatic data repository. One of the important purpose is to do ecological forecasting. This vignette helps implements a typical usecase to;

Load the required

First things first, install and load the microclim R library

library(devtools)
devtools::install_github("trenchproject/microclimRapi")

library(microclimRapi)
library(httr)
library(jsonlite)

Get Token

Microclim.org gives a token to a registered user to use its services via APIs. In the account page, you can find your API key and secret.

  api_token = getToken('07d4d584c04941a25e291feb8881c685','9ef6bbb24a855fbb765f3890e05592f4','localhost:3000/')

Get Token

This API pkg uses extensively Reference Class(RC) feature of R, a setp-up from S4 classes. Behaviour is like other OO languages.

ma <- microclimRapi:::MicroclimAPI$new(token = api_token,url_mc='http://localhost:3000/')

Prepare Request and Place a request for extraction

  mr <- microclimRapi:::MicroclimRequest$new(
                  latS = 39.40012200014591,
                  latN=39.92132255884663,
                  lonW=-106.47674560546875,
                  lonE=-105.92193603515625,
                  variable="ALBEDO",
                  shadelevel=0,
                  hod=0,
                  interval=0,
                  aggregation=0,
                  stdate="19810101",
                  eddate="19810128",
                  file="csv")

  # place a request
  ext_req= ma$request(mr)
  print(toJSON(ext_req))

Check on your request

If Request status is 'EMAILED', you can proceed with downloading the data.

#check the status of your request
ma$status(requestId)
#status 'EMAILED' means job is complete.

Get CSV/netCDF

#Pull your netCDF
if(ma$status(requestId) == "EMAILED")
{

  # place a request to fetch the files
  ftch_req= ma$fetch(requestId)

  # pass the filename
  ncD <-  ma$download(requestId,ftch_req$files[[1]]$Key)

  #File name has request id as part of it
  writeBin(ncD, strsplit(ftch_req$files[[1]]$Key, "/")[[1]][2])
}

Research question - BioPhy Model for a Lizard projecting into future.

# Describe the model
# For the given date range
# Tair
# Tsurface
# UV
# SWDOWN
#


trenchproject/microclimRapi documentation built on April 26, 2021, 1:09 p.m.