Skip to content

Commit

Permalink
update ex 4
Browse files Browse the repository at this point in the history
  • Loading branch information
vzrg-tamu committed Apr 9, 2024
1 parent 58af928 commit 52f3151
Show file tree
Hide file tree
Showing 40 changed files with 186 additions and 51 deletions.
Binary file modified awesome_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
80 changes: 69 additions & 11 deletions ch10.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ editor:

## Exercise #2

Use the raster files in *SampleData-master/SMAPL4_rasters* folder, perform the tasks as listed below:
Use the raster files in *SampleData-master/SMAPL4_rasters* folder, and perform the tasks as listed below:

- Task 1: Write a user-defined function to count the number of pixels within the range \[0.3, 0.4\] for 20211213.
Use `global` to implement your function.
Expand All @@ -40,6 +40,62 @@ Use the raster files in *SampleData-master/SMAPL4_rasters* folder, perform the t

## Exercise #4

Imagine that a federal agency is interested in understanding the impacts of land conservation practices to improve carbon sequestration in the soil.
The agency has selected two potential Carbon Sequestration Testbed (CST) sites across Louisiana.
Using the observations of hydrometeorological, land practice, and vegetation datasets collected from these locations, the agency is interested in developing a physics-based ecohydrology model to mimic the interactions between soil, plants, and atmosphere at these test beds.
This will help develop predictive models to estimate the effects of various management practices on the carbon sequestration potential in soil.

```{r 4a, message = FALSE, warning = FALSE}
library(terra)
library(mapview)
# Import vector for Louisiana
LAvct=vect(spData::us_states[spData::us_states$NAME=="Louisiana",])
# Spatial attributes of the test polygons
lon = c(-89.9, -90.2, -90.2,-93,-92.8, -93.1)
lat = c(30.8, 30.8, 30.5,32, 32.2, 32.4)
id=c(1, 1, 1, 2, 2, 2)
# Combine attributes by columns
lonlat = cbind(id, lon, lat)
# Create polygons using the point locations as vertices
#~~~ CRS of the LA vect is assigned to the new vector
CSTpoly = vect(lonlat, type="polygons",crs=crs(LAvct))
# Add polygon id to the vector geometry
CSTpoly$CST=c(1,2)
# Plot map of Louisiana with overlaying CST polygons
mapview(LAvct, alpha.regions = 0.1) +
mapview(CSTpoly, zcol ="CST", col.regions = c("blue", "red"))
```

------------------------------------------------------------------------

To develop these ecohydrology models, the agency needs your assistance in collecting high-resolution soil texture dataset for the Louisiana CSTs.
You know from Ch 6 that soil texture data is available through POLARIS at \[30m X 30m\] spatial resolution for 6 soil depths (0-5 cm, 5-15 cm, 15-30 cm, 30-60 cm, 60-100 cm and 100-200 cm. So, for this exercise, you will generate the maps of mean percent clay content for 0-100 cm soil profile for the two CSTs by taking the arithmetic average of the clay percent in each layer between 0-100 cm depth. Access the mean clay content data from: <http://hydrology.cee.duke.edu/POLARIS/PROPERTIES/v1.0/clay/mean/>.

For each CST, follow the following steps:

1. \(10\) Use R code to download the *.tif* files for the areal domain encompassing the polygon for 0-5 cm soil profile.

2. \(15\) Combine the rasters using the mosaic function.

3. \(15\) Crop and mask the mosaic raster to the polygon extent and shape.

4. \(20\) Repeat for other soil profiles.

5. \(20\) Take cell-wise arithmetic average of the mean soil percentage for the layers.

6. \(10\) Plot the raster (mean soil percentage for 0-100 cm) for the CST.

7. \(10\) Export the raster.

## Exercise #5

Soil moisture is an important hydrological variable with strong linkages to crop health and yield.
Satellites such as NASA's SMAP and ESA's SMOS provide soil moisture information for the surface profile (0-5 cm).
However, temporal variability in the surface soil moisture gets attenuated and dampened as water infiltrates through the soil layers.
Expand All @@ -51,25 +107,23 @@ We will use simulated gridded surface soil moisture and evapotranspiration from

------------------------------------------------------------------------

1. Download CONUS-wide daily simulated soil moisture (`soilmoist.2011.nc`) and evapotranspiration (`et.2011.nc`) for the year 2011 from NOAA-PSL's servers. using the following file location:
1. Download CONUS-wide daily simulated soil moisture (`soilmoist.2011.nc`) and evapotranspiration (`et.2011.nc`) for the year 2011 from NOAA-PSL's servers.
using the following file location:

`sm_path = "https://downloads.psl.noaa.gov//Datasets/livneh/fluxvars/soilmoist.2011.nc"`

`et_path = "https://downloads.psl.noaa.gov//Datasets/livneh/fluxvars/et.2011.nc"`

```{=html}
<!-- -->
```

2. Import the downloaded netCDFs to workspace as spatRasters. Print the time stamp and variable names of the two spatRasters using `terra::time()` and `terra::names()` functions. You will notice that `et.2011.nc` has one raster layer for each day. `soilmoist.2011.nc` contains three raster layers (one for each soil profile level) per day.

```{=html}
<!-- -->
```

3. Create a new multilayer spatRaster for raster layers for soil profile level 1. (Hint: using `substr()` on the layer `names()` from `soilmoist.2011.nc`, subset the layers with lev= "1" in their name)

```{=html}
<!-- -->
```

4. Create a polygon with with extent: `xmin=255, xmax=265, ymin=40, ymax=48`.

a\) Plot a map of level 1 soil moisture for day 10 and overlay the polygon on the map.
Expand All @@ -80,7 +134,8 @@ We will use simulated gridded surface soil moisture and evapotranspiration from

![](images/clipboard-4251555621.png){fig-align="center" width="450"}

5. If *smTS* is the soil moisture time series, and *Tagg* is the time-period of moving average, the function generates a right aligned rolling mean (moving average) of the time series. NA is used as fill values for the first *Tagg*-1 cells of the output.
5. If *smTS* is the soil moisture time series, and *Tagg* is the time-period of moving average, the function generates a right aligned rolling mean (moving average) of the time series.
NA is used as fill values for the first *Tagg*-1 cells of the output.

`zoo::rollmean(smTS, Tagg, fill=NA, align = "right")`

Expand All @@ -103,13 +158,16 @@ We will use simulated gridded surface soil moisture and evapotranspiration from

![](images/clipboard-713405122.png){fig-align="center" width="450"}

7. Apply the custom moving average function in parallel on each grid of the cropped multilayer soil moisture SpatRaster (from step 4) with *Tagg*=7, 15, 30, 45. Keep *minSamp* fixed at 100. Use all-but-one available system cores for parallel computing.
7. Apply the custom moving average function in parallel on each grid of the cropped multilayer soil moisture SpatRaster (from step 4) with *Tagg*=7, 15, 30, 45.
Keep *minSamp* fixed at 100.
Use all-but-one available system cores for parallel computing.

8. If x and y are two time series, then the following operation gives the Pearson's correlation between the two datasets:

`cor(x, y, use = "pairwise.complete.obs")`

Using a custom function, calculate cell-wise correlation between evapotranspiration and moving averaged level 1 soil moisture for *Tagg*=7, 15, 30 and 45. (Hint: ?terra::xapp)
Using a custom function, calculate cell-wise correlation between evapotranspiration and moving averaged level 1 soil moisture for *Tagg*=7, 15, 30 and 45.
(Hint: ?terra::xapp)

9. Create a multilayer raster by concatenating the output rasters from Step 8.

Expand Down
11 changes: 6 additions & 5 deletions ch9.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ editor:

# Parallel Geospatial Computing

Computing operations in R can be made faster by the use of parallel computation.
Generally, parallel computation is the simultaneous execution of different pieces of a larger operations across multiple computing processors or cores.
Imagine, if you can execute an operation in X seconds on a single processor, would you be able to divide the processing time by *n* if you divide the task between *n* processors?
The answer is.....almost.
Spatial operations in R can be made faster by using parallel processing.
Generally, parallel computation is the simultaneous execution of different pieces of a larger operation across multiple computing processors or cores.
Imagine if you can execute an operation in X seconds on a single processor.
Would you be able to divide the processing time by n if you divide the task between n processors?
The answer is...almost.

Parallel processing comes with overhead computing costs associated with several processors/nodes interacting with each other over shared memory.
So, for smaller datasets, the increment in computing performance using parallel processing will be minimal (if not negative).
Expand Down Expand Up @@ -120,7 +121,7 @@ When working with large spatial data, the following the steps listed below can b
> ```{r 9a3a, message = FALSE, warning = FALSE}
> # Location with Long=-100, and Lat=35 (in decimal degrees) extract time series
> ts_sample=terra::extract(SMAPrast, cbind(-100,35))
>
> >
> # Does it generate three numeric values as expected?
> my_fun(ts_sample, minSamp=50)
> ```
Expand Down
26 changes: 13 additions & 13 deletions docs/ch1.html
Original file line number Diff line number Diff line change
Expand Up @@ -527,13 +527,13 @@ <h3 data-number="2.1.3" class="anchored" data-anchor-id="data-types"><span class
<span id="cb86-8"><a href="#cb86-8" aria-hidden="true" tabindex="-1"></a>out[[<span class="dv">2</span>]] <span class="ot">=</span> data2</span>
<span id="cb86-9"><a href="#cb86-9" aria-hidden="true" tabindex="-1"></a>out[[<span class="dv">1</span>]] <span class="co"># Contains data1 at this location</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] 20.19053 27.34920 24.29754 20.00858 21.93471 26.56492 28.50896 25.11268
[9] 25.04088 25.28993 22.38669 23.32268 25.05874 24.19622 22.69125 29.94252
[17] 29.53343 23.80884 27.45731 29.59762 23.70404 22.06187 22.08497 22.38794
[25] 26.57366 22.74622 21.10927 26.77793 27.71489 21.84284 23.98676 29.24595
[33] 21.73473 23.32222 24.22195 21.07999 26.23997 29.20960 28.09831 27.64942
[41] 20.71046 26.20026 29.92468 22.27300 21.45807 21.64099 23.49302 29.64648
[49] 21.55291 26.04208</code></pre>
<pre><code> [1] 20.33515 24.46664 27.12113 25.02494 23.42259 22.10603 26.60910 26.97088
[9] 29.30362 26.57543 23.49203 20.37357 20.71139 27.31670 26.00679 20.91374
[17] 23.13343 20.93671 24.05673 21.93497 26.25547 27.72973 27.91488 29.49530
[25] 20.84809 28.88204 27.85019 24.94319 22.88072 27.59074 20.06340 22.45727
[33] 23.51925 25.72434 25.12741 21.03197 27.29425 21.43068 27.21451 28.88203
[41] 23.69175 26.30928 20.80597 28.27237 28.97732 26.43323 23.12023 21.06036
[49] 27.00526 20.69167</code></pre>
</div>
<div class="sourceCode cell-code" id="cb88"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb88-1"><a href="#cb88-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Data frame</span></span>
<span id="cb88-2"><a href="#cb88-2" aria-hidden="true" tabindex="-1"></a>out<span class="ot">=</span><span class="fu">data.frame</span>(<span class="at">x=</span>data1, <span class="at">y=</span>data2)</span>
Expand Down Expand Up @@ -886,12 +886,12 @@ <h3 data-number="2.3.3" class="anchored" data-anchor-id="multivariate-plots"><sp
<pre><code># A tibble: 6 × 4
Year January Month Value
&lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt; &lt;dbl&gt;
1 1913 25.0 Jan -13.9
2 1913 25.0 Feb -16.9
3 1913 25.0 Mar -11.1
4 1914 78.6 Jan -14.2
5 1914 78.6 Feb -9.96
6 1914 78.6 Mar -3.96</code></pre>
1 1913 69.0 Jan -8.22
2 1913 69.0 Feb -18.1
3 1913 69.0 Mar -10.0
4 1914 18.1 Jan -16.8
5 1914 18.1 Feb -18.4
6 1914 18.1 Mar -10.5 </code></pre>
</div>
</div>
<p><strong>Line plot</strong></p>
Expand Down
Loading

0 comments on commit 52f3151

Please sign in to comment.