From eb3a4c0bb9201b8c2c29cf36d64d7133776bdfff Mon Sep 17 00:00:00 2001 From: Kristina Riemer Date: Tue, 25 Jun 2019 13:03:41 -0700 Subject: [PATCH] Implement reticulate so that Python code runs when images .Rmd is knit --- vignettes/03-get-images-python.Rmd | 90 ++++++------------------------ 1 file changed, 18 insertions(+), 72 deletions(-) diff --git a/vignettes/03-get-images-python.Rmd b/vignettes/03-get-images-python.Rmd index de0c413..ebb36d5 100644 --- a/vignettes/03-get-images-python.Rmd +++ b/vignettes/03-get-images-python.Rmd @@ -27,12 +27,26 @@ All the `terrautils` functions are now available in Python, although we will onl In this section we retrieve the names of different sensor types that are available. This will allow you to understand what files may be avaialable other than just those containing RBG image data. +In order to run Python functions, including those from the `terrautils` library, within this Rmarkdown, we have to install and set up `reticulate`. + +```{r get_pkgs, message=FALSE} +if(!require(reticulate)){ + install.packages("reticulate") + reticulate::py_install("terrautils") +} + +library(reticulate) +use_virtualenv("r-reticulate") +``` + We will first be using the `get_sensor_list` function to retrieve all the data on available sensors. We will then use the `unique_sensor_names` function to extract only the sensor names from the data we just retrieved. -```{python py_get_sensor_data, eval=FALSE} +```{python py_get_functions} from terrautils.products import get_sensor_list, unique_sensor_names +``` +```{python py_get_sensor_data} url = 'https://terraref.ncsa.illinois.edu/clowder/' key = '' @@ -61,7 +75,7 @@ If the `stream=True` parameter was omitted the file's entire contents would be i To illustrate how this might work we are going to pre-populated an array of file names and their associated Clowder IDs. -```{python py_sample_images_data, eval=FALSE} +```{python py_sample_images_data} files = [ {"id": "5c507cb74f0c4b0cbe6705f2", "filename": "rgb_geotiff_L1_ua-mac_2018-06-02__14-12-05-077_right.tif"}, {"id": "5c507cb84f0cfd2aedf5a75a", @@ -78,12 +92,12 @@ First we format the base URL for our query allowing us to reuse it for each file Next we loop through our array and create a customized URL while making the call to fetch the data using the `requests` interface. Finally we open the output file and use a loop to write the retrieved data. -```{python py_fetch_image_files, eval=FALSE} +```{python py_fetch_image_files} import requests from io import open # We are using the same `url` and `key` variables declared in the previous example above. -filesurl = url + '/files/' +filesurl = url + 'files/' params={ 'key': key } for f in files: @@ -108,71 +122,3 @@ Below are examples of images captured approximately one month apart [^1] [^2] [^1]: May 4, 2018 - rgb_geotiff_L1_ua-mac_2018-05-04__13-07-04-077_right.tif,rgb_geotiff_L1_ua-mac_2018-05-04__13-07-04-077_left.tif [^2]: Jun 2, 2018 - rgb_geotiff_L1_ua-mac_2018-06-02__14-12-05-077_right.tif,rgb_geotiff_L1_ua-mac_2018-06-02__14-12-05-077_left.tif - - - \ No newline at end of file