-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathaWhereAPI.Rmd
More file actions
262 lines (140 loc) · 10.6 KB
/
Copy pathaWhereAPI.Rmd
File metadata and controls
262 lines (140 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
---
title: "aWhere R Library"
author: "aWhere Team"
date: "December 2016"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{aWhereAPI}
%\VignetteEngine{knitr::rmarkdown}
\usepackage[utf8]{inputenc}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## aWhere API Package Vignette
For customers that use R in their statistical modeling work, the `aWhereAPI` R package helps get you up and running with minimal integration effort. This vignette demonstrates the basic use of the `aWhereAPI` functions, which simplify the authentication and query construction processes. This package is also particularly helpful for users with limited knowledge of APIs and HTTP syntax.
For additional information or detail, please refer to the [complete documentation](https://github.com/aWhereAPI).
**Note:** `aWhereAPI` functions will only return metric units
## Installation
**Note:** You will need to install R and have a working knowledge of its interface prior to installing and using this package.
**Note:** You will need to have the devtools library installed in R to install the package. Consult `devtools` [documentation](https://github.com/hadley/devtools) if necessary.
### Automatically Install
This package can be installed directly from GitHub with the following command:
devtools::install_github("aWhereAPI/aWhere-R-Library")
### Manually Install
1. Download this [Github repo](https://github.com/aWhereAPI/aWhere-R-Library) and keep the `aWhere-R-Library-master` folder somewhere useful on your computer, such as your Desktop or My Documents.
2. Set the Working Path in R to the location that contains the `aWhere-R-Library-master` folder. If you placed it on your Desktop the working directory would be something like `C:\Users\USERNAME\Desktop`. In R, this command is:
* `setwd("C:\Users\USERNAME\Desktop")`
3. Run `library(devtools)` and `install("aWhere-R-Library-master")` to install and add the library to your environment.
### Load the library
```{r}
library(aWhereAPI)
```
## API Credentials
To get aWhere API keys go to [aWhere Developer Community](http://developer.awhere.com/start) or contact your aWhere Account Executive.
### Use credentials directly for token generation
One-time generation of a token can be done with the `get_token()` function. The resulting token will be automatically stored in your environment for use in API calls (tokens are active for one hour).
get_token("JFKL24JF290FJSKAFDF","jf0afsd9af0a")
### Load credentials from a saved file for automatic token generation
Alternatively, the `load_credentials()` function accesses a text file saved on your computer with your API Key on the first line, and your Secret on the second. This function invokes `get_token()` so no further work is needed to generate additional access tokens while making calls.
```{r}
load_credentials("C:/aWhere/credentials/credentials.txt")
```
___________________________________________________________________________
---------------------------------------------------------------------------
## API Functions
### The Fields & Plantings API
This API provides an interface for customers to register their field locations and information about their plantings in the aWhere Platform. The ID for each field or planting can then be used for quick reference in other functions in the weather data APIs.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#### create_field()
```{r}
create_field("field_test",40.828200,-100.579500,"farmA")
```
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#### get_fields()
Running this function without any inputs will return a complete list of fields associated with your credentials. If a user wants information relating to a specific field only, they can pass the field ID to the function.
```{r}
get_fields("field_test")
```
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#### update_field()
At this time only the name or farm ID of a field can be changed after you have created the field. Field IDs and coordinates cannot be changed. This example will change the farm ID of field_test to "TestFarmA" only if the farm ID is currently "farmA":
```{r}
update_field("field_test", "farmId", "farmA", "farmId", "TestFarmA")
get_fields("field_test")
```
The function as constructed allows you to use the value of the first named variable (whether farm ID or field name) as the condition for replacing the second named variable.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#### create_planting()
Each field associated with your aWhere API account can be associated with multiple different planting records, specific to a crop and/or growing season. A planting record is only required when using the aWhere API to run models.
Requires both a previously-created field ID and specific information relating to the planting that occurred on that field. This example shows the simplest possible way to create a planting, though a number of other optional parameters are allowed. When complete, the auto-generated planting ID is returned.
```{r}
pid <- create_planting("field_test", "corn", "2016-07-01")
```
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#### get_planting()
Similar to get_fields(), this function retrieves information about previously-created plantings. This example returns all plantings associated with field_test.
```{r}
get_planting("field_test")
```
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#### delete_planting()
```{r}
delete_planting("field_test", pid)
```
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The auto-generated planting ID must be supplied as well as the field ID.
___________________________________________________________________________
---------------------------------------------------------------------------
### Functions relating to the Weather APIs
All weather data requests are requested point by point. The API does not yet have the capacity to download regional data in a single call. The [batch jobs system](http://developer.awhere.com/api/reference/batch) is most useful for those wishing to request a large amount of data (e.g., many hundreds or thousands of points).
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#### Daily Observed Weather
By default, you can request up to 30 months of daily historical data, counting back from the present date. For data beyond that, use the Norms API or speak with your aWhere representative for access to more of the daily data archive. The function `daily_observed_fields` provides the simplest method for retrieving historical data.
```{r}
daily_observed_fields("field_test", day_start = "2015-07-01", day_end = "2015-07-07")
```
Users also have the option of using the alternative function `daily_observed_latlng` to retrieve historical data for a given pair of coordinates.
```{r}
daily_observed_latlng(latitude = 39.8282, longitude = -98.5795, day_start = "2015-07-01", day_end = "2015-07-07")
```
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#### Forecast
Forecasts are available from the current date plus up to the next 8 days, at multiple temporal resolutions - in 1, 2, 3, 4, 6, 8, 12, or 24-hour summary blocks.
```{r}
forecasts_fields("field_test", as.character(Sys.Date() + 1), block_size = 4)
forecasts_fields("field_test", day_start = as.character(Sys.Date() + 1), day_end = as.character(Sys.Date() + 3), block_size = 12)
```
As with daily observed historical data, forecast data can also be called based on latitude, longitude pairs.
```{r}
forecasts_latlng(latitude = 39.8282, longitude = -98.5795, day_start = as.character(Sys.Date() + 1), block_size = 24)
```
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#### Long Term Norms
The weather norms API allows you to offload the calculation of multi-year averages (minimum 3 years). This eliminates much of the need to download years and years of daily data just to calculate averages. The results also include the standard deviation for each average.
```{r}
weather_norms_fields(field_id = "field_test", monthday_start = "07-01", monthday_end = "07-07", year_start = 2010, year_end = 2015)
```
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#### Current Conditions
The Current Conditions API returns a snapshot of the weather at a geographic point using the nearest available station. While the data is QA'd before delivery, this service does not do the same advanced processing or interpolation as Daily Observed data.
```{r}
current_conditions_fields("field_test", "metar-mesonet")
```
___________________________________________________________________________
---------------------------------------------------------------------------
### Agronomic Data
Agronomic data can enrich your analysis by providing insight into the agronomic status of a field or crop. aWhere provides derived values like GDD (using a variety of different equations) PET (using the Penman-Monteith Equation), P/PET, and accumulated precipitation, accumulated GDD, accumulated PET, and P/PET over a range of days. The long-term norms for each value can also be queried.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#### Agronomic Values
Calculations can be customized in a number of different ways, consult the [API Documentation](http://developer.awhere.com/api/reference/agronomics/values) for more information.
```{r}
agronomic_values_fields("field_test", day_start = "2016-07-01", day_end = "2016-07-07", accumulation_start_date = "2016-06-01", gdd_method = "modifiedstandard", gdd_base_temp = 10, gdd_min_boundary = 10, gdd_max_boundary = 30)
```
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#### Agronomic Norms
The Agronomic Norms API returns the long-term normals for the agronomic values on any given set of days across any range of years for which we have data. The same customizable parameters for changing the calculation of agronomic values are also available in the norms.
```{r}
agronomic_norms_fields("field_test", month_day_start = "07-01", month_day_end = "07-07", year_start = 2010, year_end = 2015, gdd_method = "modifiedstandard")
```
Questions, comments, or requests about this vignette should be directed to [email protected]