Skip to content

Commit

Permalink
Removes examples from paper.md
Browse files Browse the repository at this point in the history
  • Loading branch information
micha-silver committed Jul 9, 2024
1 parent 2424179 commit e98f4cb
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 216 deletions.
8 changes: 4 additions & 4 deletions R/optram_wetdry_coefficients.R
Original file line number Diff line number Diff line change
Expand Up @@ -234,25 +234,25 @@ plot_vi_str_cloud <- function(
} else if (plot_colors == "density") {
pl <- ggplot2::ggplot(plot_df) +
geom_point(aes(x = VI, y = STR, color = Density),
size = 0.3, alpha = 0.1) +
size = 0.3, alpha = 0.2) +
scale_color_continuous(type = "viridis",
direction = -1) +
theme(legend.position = "none")
} else if (plot_colors %in% c("contours", "contour")) {
pl <- ggplot2::ggplot(plot_df) +
geom_point(aes(x=VI, y=STR), color = "green",
alpha = 0.1, size = 0.3) +
alpha = 0.2, size = 0.3) +
geom_density2d(aes(x=VI, y=STR), color = "darkgrey")
} else if ( (plot_colors %in% c("features", "feature")) &
("Feature_ID" %in% names(plot_df)) ) {
pl <- ggplot2::ggplot(plot_df) +
geom_point(aes(x=VI, y=STR, color = Feature_ID),
alpha = 0.1, size = 0.3) +
alpha = 0.2, size = 0.3) +
theme(legend.position = "right")
} else if (plot_colors %in% c("months", "month")) {
pl <- ggplot2::ggplot(plot_df) +
geom_point(aes(x=VI, y=STR, color = Month),
alpha = 0.1, size = 0.3) +
alpha = 0.2, size = 0.3) +
theme(legend.position = "right")
} else { # No plot_colors options fit, use default plot
message("No ID column in data, reverting to default plot")
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
output: github_document
bibliography: inst/references.bib
bibliography: vignettes/references.bib
---

<!-- README.md is generated from README.Rmd. Please edit that file -->
Expand Down
235 changes: 164 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
---
output: github_document
bibliography: inst/references.bib
---

<!-- README.md is generated from README.Rmd. Please edit that file -->




# rOPTRAM <img align="right" width="100" height="100" src="vignettes/images/rOPTRAM_logo.jpg">
# rOPTRAM <img align="right" width="100" height="100" src="vignettes/images/rOPTRAM_logo.jpg">

<!-- badges: start -->
[![Project Status: ACTIVE](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)

[![Project Status:
ACTIVE](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![R-CMD-check](https://github.com/ropensci/rOPTRAM/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/ropensci/rOPTRAM/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->

`rOPTRAM` implements The OPtical TRapezoid Model (OPTRAM)
to derive soil moisture based on the linear relation between a vegetation index, i.e. NDVI, and Shortwave Infra-red (SWIR). The SWIR band is transformed to SWIR Transformed Reflectance (STR).
A scatterplot of NDVI vs. STR is used to produce wet and dry linear regression lines, and the slope/intercept coefficients of these lines comprise the trapezoid. These coefficients are then used on a new satellite image to determine soil moisture.
`rOPTRAM` implements The OPtical TRapezoid Model (OPTRAM) to derive soil
moisture based on the linear relation between a vegetation index,
i.e. NDVI, and Shortwave Infra-red (SWIR). The SWIR band is transformed
to SWIR Transformed Reflectance (STR).

See: @sadeghi_optical_2017, @burdun_satellite_2020, @ambrosone_retrieving_2020
A scatterplot of NDVI vs. STR is used to produce wet and dry linear
regression lines, and the slope/intercept coefficients of these lines
comprise the trapezoid. These coefficients are then used on a new
satellite image to determine soil moisture.

See: Sadeghi et al. (2017), Burdun et al. (2020), Ambrosone et al.
(2020)

## Installation

`rOPTRAM` resides on github.
You can install the development version of rOPTRAM like so:

`rOPTRAM` resides on github. You can install the development version of
rOPTRAM like so:

``` r
# Install remotes package
Expand All @@ -38,81 +38,122 @@ remotes::install_github("ropensci/rOPTRAM")

#### Prerequisites

Only a small number of commonly used R packages are required to use {rOPTRAM}. This includes:
- base packages {tools} and {utils}
- spatial packages {sf} and {terra}
- data.frame and plotting {dplyr}, {ggplot2}, {MASS}

Users can download Sentinel-2 tiles from the Copernicus manually, and run thru the steps to produce the OPTRAM trapezoid, and predicted soil moisture maps. However, this approach is not optimal. By installing a few additional packages, the workflow can be initiated by a single function call to download, clip to area of interest, and produce the trapezoid coefficients. The all-inclusive approach is highly recommended since processing of the Sentinel-2 data is performed "in the cloud" and only the final products are downloaded, **greatly** reducing the download file sizes.

To run the all-inclusive approach, the first step of acquiring Sentinel-2 imagery is handled by the R package {CDSE}. (see @karaman_cdse_2023). The {jsonlite} package is also necessary.

That R package interfaces with the Copernicus DataSpace Ecosystem in one of two ways:
- Thru the [Scihub API](https://shapps.dataspace.copernicus.eu/dashboard/#/).
- Thru the [openEO platform](https://openeo.dataspace.copernicus.eu/)

Both methods require [registering](https://dataspace.copernicus.eu/) on the Copernicus DataSpace
Only a small number of commonly used R packages are required to use
{rOPTRAM}. This includes: - base packages {tools} and {utils} - spatial
packages {sf} and {terra} - data.frame and plotting {dplyr}, {ggplot2},
{MASS}

Users can download Sentinel-2 tiles from the Copernicus manually, and
run thru the steps to produce the OPTRAM trapezoid, and predicted soil
moisture maps. However, this approach is not optimal. By installing a
few additional packages, the workflow can be initiated by a single
function call to download, clip to area of interest, and produce the
trapezoid coefficients. The all-inclusive approach is highly recommended
since processing of the Sentinel-2 data is performed “in the cloud” and
only the final products are downloaded, **greatly** reducing the
download file sizes.

To run the all-inclusive approach, the first step of acquiring
Sentinel-2 imagery is handled by the R package {CDSE}. (see Karaman
(2023)). The {jsonlite} package is also necessary.

That R package interfaces with the Copernicus DataSpace Ecosystem in one
of two ways: - Thru the [Scihub
API](https://shapps.dataspace.copernicus.eu/dashboard/#/). - Thru the
[openEO platform](https://openeo.dataspace.copernicus.eu/)

Both methods require [registering](https://dataspace.copernicus.eu/) on
the Copernicus DataSpace

## Available functions

#### optram_options()

Several package options are defined, with default values, when {rOPTRAM} first loads. Each of these can be set individually to user chosen values. For example, the package uses a vegetation index (compared to SWIR Transformed Reflectance) values to plot the trapezoid. The default index is "NDVI". In a low vegetation, arid region, users can choose an alternative such as "SAVI" (see example below). The default maximum cloud cover is set to 12 [%], and users can choose any value between 0-100.
Several package options are defined, with default values, when {rOPTRAM}
first loads. Each of these can be set individually to user chosen
values. For example, the package uses a vegetation index (compared to
SWIR Transformed Reflectance) values to plot the trapezoid. The default
index is “NDVI”. In a low vegetation, arid region, users can choose an
alternative such as “SAVI” (see example below). The default maximum
cloud cover is set to 12 \[%\], and users can choose any value between
0-100.

#### optram()

A main wrapper function to run the whole OPTRAM procedure.
This function performs the following steps:

- Acquire Sentinel 2 images covering the requested date range, and clipped to the input area of interest. This step relies on the {CDSE} package
- Create the set of SWIR Transformed Reflectance (STR) rasters;
- Prepare a dataframe of NDVI and STR values for all pixels from all images;
- Calculate the trapezoid wet and dry regression lines, and save coefficients to a CSV file.
Returns: RMSE values of the fitted regression lines of the trapezoid.
A main wrapper function to run the whole OPTRAM procedure. This function
performs the following steps:

- Acquire Sentinel 2 images covering the requested date range, and
clipped to the input area of interest. This step relies on the {CDSE}
package
- Create the set of SWIR Transformed Reflectance (STR) rasters;
- Prepare a dataframe of NDVI and STR values for all pixels from all
images;
- Calculate the trapezoid wet and dry regression lines, and save
coefficients to a CSV file. Returns: RMSE values of the fitted
regression lines of the trapezoid.

#### optram_acquire_s2()
Acquire Sentinel 2 images covering the requested date range, and clipped to the input area of interest.

Among the function parameters, the `method` can be specified as either "scihub" or "openeo", thus choosing one of the two available acqquistion methods.
Acquire Sentinel 2 images covering the requested date range, and clipped
to the input area of interest.

Returns: a list of downloaded Sentinel 2 images
Among the function parameters, the `method` can be specified as either
“scihub” or “openeo”, thus choosing one of the two available acqquistion
methods.

Returns: a list of downloaded Sentinel 2 images

#### optram_calculate_str()
Extracts the required bands and prepares the vegetation index and SWIR transformed reflectance.

Extracts the required bands and prepares the vegetation index and SWIR
transformed reflectance.

#### optram_ndvi_str()
Collects all pixel values from both the vegetation index and STR rasters, for all acquisition dates, and saves into a data.frame

Returns: the full data.frame
Collects all pixel values from both the vegetation index and STR
rasters, for all acquisition dates, and saves into a data.frame

Returns: the full data.frame

#### optram_wetdry_coefficients()
Calculates the wet-dry trapezoid from the data.frame of NDVI and STR values, and obtains regression slope and intercept for both lines

Three possible fitting methods are offered in this function, thru the "trapezoid_method" parameter. The user can choose to match the upper (wet) and lower (dry) bounds of the trapezoid either as a linear regression line, an exponential curve, or a second order polynomial function.
Calculates the wet-dry trapezoid from the data.frame of NDVI and STR
values, and obtains regression slope and intercept for both lines

Three possible fitting methods are offered in this function, thru the
“trapezoid_method” parameter. The user can choose to match the upper
(wet) and lower (dry) bounds of the trapezoid either as a linear
regression line, an exponential curve, or a second order polynomial
function.

Returns: the set of four (or six, in the case of polynomial curve) coefficients.
Returns: the set of four (or six, in the case of polynomial curve)
coefficients.

#### optram_calculate_soil_moisture()
Calculates soil moisture rasters for a time series of images, using the OPTRAM model coefficients from above procedure.

Calculates soil moisture rasters for a time series of images, using the
OPTRAM model coefficients from above procedure.

#### optram_landsat()
Prepares the OPTRAM coefficients from a time series of Landsat images, (instead of Sentinel). This function requires that the images are downloaded in advance.

Returns: the set of four coefficients.
Prepares the OPTRAM coefficients from a time series of Landsat images,
(instead of Sentinel). This function requires that the images are
downloaded in advance.

Returns: the set of four coefficients.

#### optram_safe()
In case Sentinel images have been downloaded in advance, this function prepares the STR and NDVI rasters, then calculate the trapezoid regression coefficients. It requires an input directory containing the Sentinel 2 images in the original SAFE file format.

In case Sentinel images have been downloaded in advance, this function
prepares the STR and NDVI rasters, then calculate the trapezoid
regression coefficients. It requires an input directory containing the
Sentinel 2 images in the original SAFE file format.

## Example

First, a demonstration of choosing non-default package options.


``` r
# Show default options
rOPTRAM::optram_options()
Expand Down Expand Up @@ -141,12 +182,12 @@ rOPTRAM::optram_options("veg_index", "SAVI", show_opts = FALSE)

Next a basic example which shows how to:

- retrieve Sentinel 2 imagery for a specific area of interest
- covering a date range
- preprocess the imagery to obtain a vegetation index and STR band
- use these to derive coefficients of slope and intercept for the OPTRAM trapezoid
- using the "linear" fitting method

- retrieve Sentinel 2 imagery for a specific area of interest
- covering a date range
- preprocess the imagery to obtain a vegetation index and STR band
- use these to derive coefficients of slope and intercept for the OPTRAM
trapezoid
- using the “linear” fitting method

``` r
library(rOPTRAM)
Expand All @@ -161,20 +202,72 @@ print(rmse)

## Note

In order to download Sentinel 2 images, the {CDSE} package is used: (@karaman_cdse_2023)
In order to download Sentinel 2 images, the {CDSE} package is used:
(Karaman (2023))

That package should be installed in advance in order to run the `optram()` wrapper function.
That package should be installed in advance in order to run the
`optram()` wrapper function.

If, on the other hand, Sentinel 2 imagery has been downloaded in advance, then {CDSE} is not strictly necessary.
Instead, the following workflow can be used:
If, on the other hand, Sentinel 2 imagery has been downloaded in
advance, then {CDSE} is not strictly necessary. Instead, the following
workflow can be used:

- call `optram_safe()` to prepare NDVI and STR rasters
- call `optram_calculate_str()` to calculate SWIR Transform
- call `optram_ndvi_str()` to build a data.frame of pixel values
- call `optram_wetdry_coefficients()` to derive slope and intercept of the trapezoid.
- call `optram_safe()` to prepare NDVI and STR rasters
- call `optram_calculate_str()` to calculate SWIR Transform
- call `optram_ndvi_str()` to build a data.frame of pixel values
- call `optram_wetdry_coefficients()` to derive slope and intercept of
the trapezoid.

## Meta

- Please report any issues on [github](https://github.com/ropensci/rOPTRAM/issues)
- Anyone interested in collaborating is invited to "sign up" by contacting the maintainers.
- This package is released with a [Contributor Code of Conduct](https://github.com/ropensci/.github/blob/master/CODE_OF_CONDUCT.md). By contributing to this project, you agree to abide by its terms.
- Please report any issues on
[github](https://github.com/ropensci/rOPTRAM/issues)
- Anyone interested in collaborating is invited to “sign up” by
contacting the maintainers.
- This package is released with a [Contributor Code of
Conduct](https://github.com/ropensci/.github/blob/master/CODE_OF_CONDUCT.md).
By contributing to this project, you agree to abide by its terms.

<div id="refs" class="references csl-bib-body hanging-indent"
entry-spacing="0">

<div id="ref-ambrosone_retrieving_2020" class="csl-entry">

Ambrosone, Mariapaola, Alessandro Matese, Salvatore Filippo Di Gennaro,
Beniamino Gioli, Marin Tudoroiu, Lorenzo Genesio, Franco Miglietta, et
al. 2020. “Retrieving Soil Moisture in Rainfed and Irrigated Fields
Using Sentinel-2 Observations and a Modified OPTRAM Approach.”
*International Journal of Applied Earth Observation and Geoinformation*
89 (July): 102113. <https://doi.org/10.1016/j.jag.2020.102113>.

</div>

<div id="ref-burdun_satellite_2020" class="csl-entry">

Burdun, Iuliia, Michel Bechtold, Valentina Sagris, Annalea Lohila, Elyn
Humphreys, Ankur R. Desai, Mats B. Nilsson, Gabrielle De Lannoy, and Ülo
Mander. 2020. “Satellite Determination of Peatland Water Table Temporal
Dynamics by Localizing Representative Pixels of A SWIR-Based Moisture
Index.” *Remote Sensing* 12 (18): 2936.
<https://doi.org/10.3390/rs12182936>.

</div>

<div id="ref-karaman_cdse_2023" class="csl-entry">

Karaman, Zivan. 2023. “CDSE: Copernicus Data Space Ecosystem API
Wrapper.” <https://CRAN.R-project.org/package=CDSE>.

</div>

<div id="ref-sadeghi_optical_2017" class="csl-entry">

Sadeghi, Morteza, Ebrahim Babaeian, Markus Tuller, and Scott B. Jones.
2017. “The Optical Trapezoid Model: A Novel Approach to Remote Sensing
of Soil Moisture Applied to Sentinel-2 and Landsat-8 Observations.”
*Remote Sensing of Environment* 198 (September): 52–68.
<https://doi.org/10.1016/j.rse.2017.05.041>.

</div>

</div>
File renamed without changes.
Loading

0 comments on commit e98f4cb

Please sign in to comment.