Skip to content

Commit

Permalink
add coding challenge example
Browse files Browse the repository at this point in the history
  • Loading branch information
btupper committed Dec 11, 2024
1 parent ce58382 commit 419eb70
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
6 changes: 4 additions & 2 deletions C00_coding.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ In this case, each record of geometry is a "MULTILINESTRING", which is a group o

Let's plot these geometries, and add the points on top.

```{r plot_coast_and_points}
```{r plot_coast_and_points, warning = FALSE}
plot(coast, col = "orange", lwd = 2, axes = TRUE, reset = FALSE,
main = "Buoys in the Gulf of Maine")
plot(buoys, pch = 1, cex = 0.5, add = TRUE)
Expand Down Expand Up @@ -148,7 +148,9 @@ plot(current['SST'], hook = add_coast_and_buoys)

## Coding Challenge

Use the menu option `File > New File > R Script` to create a blank file. Save the file (even though it is empty) in the "challenges" directory as "challenge_script_1.R". Use this file to build a script that meets the following challenge. Note that the existing file, "challenge_script_0.R" is already there as an example.

Use the [Brickman tutorial](https://github.com/BigelowLab/ColbyForecasting2025/wiki/Brickman) to extract data from the location of Buoy M01 for RCP4.5 2055. Make a plot of `SST` (y-axis) as a function of `month` (x-axis). Here's one possible outcome.

![Buoy M01, RCP4.5 2055]("images/buoy_M01_RCP45_2055.png")
![Buoy M01, RCP4.5 2055](images/buoy_M01_RCP45_2055.png)
:::
59 changes: 59 additions & 0 deletions challenges/challenge_script_0.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# This is an example of a challenge script
#
# Challenge Script
#
# Use the [Brickman tutorial](https://github.com/BigelowLab/ColbyForecasting2025/wiki/Brickman)
# to extract data from the location of Buoy N01 for current conditions. Extract
# both `SST` and `Tbtm` for every month and then determine the temperature
# difference `dT`. Make a plot of `dT` (y-axis) as a function of `month` (x-axis).

# always start with setting things up
source("setup.R")

# load the buoys, the coastline and the database of covariate data
buoys = gom_buoys()
coast = read_coastline()
db = brickman_database()

# filter the buoys to pull out just N01
buoys = buoys |>
filter(id == "N01") # not3 the double '=='

# filter the database to just the current scenario monthlies
db = db |>
filter(scenario == "PRESENT", interval == "mon")

# read the covariates
covars = read_brickman(db)

# extract the variables from the covars
# we want to compare two variables, we could do it with the
# long form, but the wide form might be better
x = extract_brickman(covars, buoys, form = "wide")

# add a column (called mutating which sounds vaguely gross) to the table
# the new colum is dT = SST - Tbtm
x = x |>
mutate(dT = SST - Tbtm)

# now make a plot of dT as a function of month
ggplot(data = x,
mapping = aes(x = month, y = dT)) +
geom_point() +
labs(title = "Temp difference at buoy N01")

# whoops! That's with the months in alphabetical order. To control the order
# make month into a factor and specify the order. The built in value, `month.abb`
# has the month abbreviations in chronological order. Another mutate should do it.
# This time the mutate simple modifies a column instead of adding a new one.
x = x |>
mutate(month = factor(month, levels = month.abb))

# now plot!
ggplot(data = x,
mapping = aes(x = month, y = dT)) +
geom_point() +
labs(y = "dT (C)",
title = "Temp difference at buoy N01")

# tahdah!
3 changes: 2 additions & 1 deletion docs/C00_coding.html
Original file line number Diff line number Diff line change
Expand Up @@ -513,10 +513,11 @@ <h2 data-number="4.4" class="anchored" data-anchor-id="array-data-aka-raster-dat
</div>
</div>
<div class="callout-body-container callout-body">
<p>Use the menu option <code>File &gt; New File &gt; R Script</code> to create a blank file. Save the file (even though it is empty) in the “challenges” directory as “challenge_script_1.R”. Use this file to build a script that meets the follwoing challenge. Note that the exisiting file, “challenge_script_0.R” is already there as an example.</p>
<p>Use the <a href="https://github.com/BigelowLab/ColbyForecasting2025/wiki/Brickman">Brickman tutorial</a> to extract data from the location of Buoy M01 for RCP4.5 2055. Make a plot of <code>SST</code> (y-axis) as a function of <code>month</code> (x-axis). Here’s one possible outcome.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="&quot;images/buoy_M01_RCP45_2055.png&quot;" class="img-fluid figure-img"></p>
<p><img src="images/buoy_M01_RCP45_2055.png" class="img-fluid figure-img"></p>
<figcaption>Buoy M01, RCP4.5 2055</figcaption>
</figure>
</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/search.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"href": "C00_coding.html#array-data-aka-raster-data",
"title": "Coding",
"section": "4.4 Array data (aka raster data)",
"text": "4.4 Array data (aka raster data)\nOften spatial data comes in grids, like regular arrays of pixels. These are great for all sorts of data like satellite images, bathymetry maps and environmental modeling data. We’ll be working with environmental modeling data which we call “Brickman data”. You can learn more about Brickman data in the wiki. We’ll be glossing over the details here, but there’s lots of detail in the wiki.\nWe’ll read in the database that tracks 82 Brickman data files, and then immediately filter out the rows that define the “PRESENT” scenario (where present means 1982–2013) and monthly climatology models.\n\ndb = brickman_database() |&gt;\n filter(scenario == \"PRESENT\", interval == \"mon\") # note the double '==', it's comparative\ndb\n\n# A tibble: 8 × 4\n scenario year interval var \n &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;\n1 PRESENT PRESENT mon MLD \n2 PRESENT PRESENT mon Sbtm \n3 PRESENT PRESENT mon SSS \n4 PRESENT PRESENT mon SST \n5 PRESENT PRESENT mon Tbtm \n6 PRESENT PRESENT mon U \n7 PRESENT PRESENT mon V \n8 PRESENT PRESENT mon Xbtm \n\n\nIf you are wondering about filtering a table, be sure to check out the wiki on tabular data to get started.\nYou might be wondering what that |&gt; is doing. It is called a pipe, and it delivers the output of one function to the next function as the first parameter (aka argument). For example, brickman_database() produces a table, that table is immediately passed into filter() to choose rows that match our criteria.\nNow that we have the database listing just the records we want, we pass it to the read_brickman() function.\n\ncurrent = read_brickman(db)\ncurrent\n\nstars object with 3 dimensions and 8 attributes\nattribute(s):\n Min. 1st Qu. Median Mean 3rd Qu.\nMLD 1.011275e+00 5.583339810 15.967359543 18.910421492 2.809953e+01\nSbtm 2.324167e+01 32.136343956 34.232215881 33.507147254 3.491243e+01\nSSS 1.644333e+01 30.735633373 31.104771614 31.492407921 3.203519e+01\nSST -7.826599e-01 6.434107542 12.359498501 12.151707840 1.763068e+01\nTbtm -2.676387e-01 3.595118523 6.110801697 6.122372065 7.521761e+00\nU -2.121380e-01 -0.010892980 -0.002634738 -0.010139401 7.229637e-04\nV -1.883337e-01 -0.010722862 -0.002858645 -0.008474233 9.565173e-04\nXbtm 3.275602e-06 0.001458065 0.003088348 0.008360344 7.256525e-03\n Max. NA's\nMLD 106.69815063 59796\nSbtm 35.15741730 59796\nSSS 35.59160995 59796\nSST 26.43147278 59796\nTbtm 24.60999298 59796\nU 0.07469980 59796\nV 0.05264002 59796\nXbtm 0.18996811 59796\ndimension(s):\n from to offset delta refsys point values x/y\nx 1 121 -74.93 0.08226 WGS 84 FALSE NULL [x]\ny 1 89 46.08 -0.08226 WGS 84 FALSE NULL [y]\nmonth 1 12 NA NA NA NA Jan,...,Dec \n\n\nThis loads quite a complex set of arrays, but they have spatial information attached in the dimensions section. The x and y dimensions represent longitude and latitude respectively. The 3rd dimension, month, is time based.\nHere we plot all 12 months of sea surface temperature, SST. Note the they all share the same color scale so that they are easy to compare.\n\nplot(current['SST'])\n\n\n\n\n\n\n\n\nJust as we are able to plot linestrings/polygons along side points, we can also plot these with arrays (rasters). To do this for one month (“Apr”) of one variable (“SSS”) we simply need to slice that data out of the current variable.\n\napril_sss = current['SSS'] |&gt;\n slice(\"month\", \"Apr\")\napril_sss\n\nstars object with 2 dimensions and 1 attribute\nattribute(s):\n Min. 1st Qu. Median Mean 3rd Qu. Max. NA's\nSSS 16.44333 30.8342 31.10334 31.4641 31.93447 35.59161 4983\ndimension(s):\n from to offset delta refsys point x/y\nx 1 121 -74.93 0.08226 WGS 84 FALSE [x]\ny 1 89 46.08 -0.08226 WGS 84 FALSE [y]\n\n\nThen it’s just plot, plot, plot.\n\nplot(april_sss, axes = TRUE, reset = FALSE)\nplot(coast, add = TRUE, col = \"orange\", lwd = 2)\nplot(buoys, add = TRUE, pch = 16, col = \"purple\")\n\nWarning in plot.sf(buoys, add = TRUE, pch = 16, col = \"purple\"): ignoring all\nbut the first attribute\n\n\n\n\n\n\n\n\n\nWe can plot ALL twelve months of a variable (“SST”) with the coast and points shown. There is one slight modification to be made since a single call to plot() actually gets invoked 12 times for this data. So where do we add in the buoys and coast? Fortunately, we can create what is called a “hook” function - who knows where the name hook came from? Once the hook function is defined, it will be applied to the each of the 12 subplots.\n\n# a little function that gets called just after each sub-plot\n# it simple adds the coast and buoy\nadd_coast_and_buoys = function(){\n plot(coast, col = \"orange\", lwd = 2, add = TRUE)\n plot(buoys, pch = 16, col = \"purple\", add = TRUE)\n}\n\n# here we call the plot, and tell R where to call `add_coast_and_buoys()` after\n# each subplot is made\nplot(current['SST'], hook = add_coast_and_buoys)\n\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nCoding Challenge\n\n\n\nUse the Brickman tutorial to extract data from the location of Buoy M01 for RCP4.5 2055. Make a plot of SST (y-axis) as a function of month (x-axis). Here’s one possible outcome.\n\n\n\nBuoy M01, RCP4.5 2055",
"text": "4.4 Array data (aka raster data)\nOften spatial data comes in grids, like regular arrays of pixels. These are great for all sorts of data like satellite images, bathymetry maps and environmental modeling data. We’ll be working with environmental modeling data which we call “Brickman data”. You can learn more about Brickman data in the wiki. We’ll be glossing over the details here, but there’s lots of detail in the wiki.\nWe’ll read in the database that tracks 82 Brickman data files, and then immediately filter out the rows that define the “PRESENT” scenario (where present means 1982–2013) and monthly climatology models.\n\ndb = brickman_database() |&gt;\n filter(scenario == \"PRESENT\", interval == \"mon\") # note the double '==', it's comparative\ndb\n\n# A tibble: 8 × 4\n scenario year interval var \n &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;\n1 PRESENT PRESENT mon MLD \n2 PRESENT PRESENT mon Sbtm \n3 PRESENT PRESENT mon SSS \n4 PRESENT PRESENT mon SST \n5 PRESENT PRESENT mon Tbtm \n6 PRESENT PRESENT mon U \n7 PRESENT PRESENT mon V \n8 PRESENT PRESENT mon Xbtm \n\n\nIf you are wondering about filtering a table, be sure to check out the wiki on tabular data to get started.\nYou might be wondering what that |&gt; is doing. It is called a pipe, and it delivers the output of one function to the next function as the first parameter (aka argument). For example, brickman_database() produces a table, that table is immediately passed into filter() to choose rows that match our criteria.\nNow that we have the database listing just the records we want, we pass it to the read_brickman() function.\n\ncurrent = read_brickman(db)\ncurrent\n\nstars object with 3 dimensions and 8 attributes\nattribute(s):\n Min. 1st Qu. Median Mean 3rd Qu.\nMLD 1.011275e+00 5.583339810 15.967359543 18.910421492 2.809953e+01\nSbtm 2.324167e+01 32.136343956 34.232215881 33.507147254 3.491243e+01\nSSS 1.644333e+01 30.735633373 31.104771614 31.492407921 3.203519e+01\nSST -7.826599e-01 6.434107542 12.359498501 12.151707840 1.763068e+01\nTbtm -2.676387e-01 3.595118523 6.110801697 6.122372065 7.521761e+00\nU -2.121380e-01 -0.010892980 -0.002634738 -0.010139401 7.229637e-04\nV -1.883337e-01 -0.010722862 -0.002858645 -0.008474233 9.565173e-04\nXbtm 3.275602e-06 0.001458065 0.003088348 0.008360344 7.256525e-03\n Max. NA's\nMLD 106.69815063 59796\nSbtm 35.15741730 59796\nSSS 35.59160995 59796\nSST 26.43147278 59796\nTbtm 24.60999298 59796\nU 0.07469980 59796\nV 0.05264002 59796\nXbtm 0.18996811 59796\ndimension(s):\n from to offset delta refsys point values x/y\nx 1 121 -74.93 0.08226 WGS 84 FALSE NULL [x]\ny 1 89 46.08 -0.08226 WGS 84 FALSE NULL [y]\nmonth 1 12 NA NA NA NA Jan,...,Dec \n\n\nThis loads quite a complex set of arrays, but they have spatial information attached in the dimensions section. The x and y dimensions represent longitude and latitude respectively. The 3rd dimension, month, is time based.\nHere we plot all 12 months of sea surface temperature, SST. Note the they all share the same color scale so that they are easy to compare.\n\nplot(current['SST'])\n\n\n\n\n\n\n\n\nJust as we are able to plot linestrings/polygons along side points, we can also plot these with arrays (rasters). To do this for one month (“Apr”) of one variable (“SSS”) we simply need to slice that data out of the current variable.\n\napril_sss = current['SSS'] |&gt;\n slice(\"month\", \"Apr\")\napril_sss\n\nstars object with 2 dimensions and 1 attribute\nattribute(s):\n Min. 1st Qu. Median Mean 3rd Qu. Max. NA's\nSSS 16.44333 30.8342 31.10334 31.4641 31.93447 35.59161 4983\ndimension(s):\n from to offset delta refsys point x/y\nx 1 121 -74.93 0.08226 WGS 84 FALSE [x]\ny 1 89 46.08 -0.08226 WGS 84 FALSE [y]\n\n\nThen it’s just plot, plot, plot.\n\nplot(april_sss, axes = TRUE, reset = FALSE)\nplot(coast, add = TRUE, col = \"orange\", lwd = 2)\nplot(buoys, add = TRUE, pch = 16, col = \"purple\")\n\nWarning in plot.sf(buoys, add = TRUE, pch = 16, col = \"purple\"): ignoring all\nbut the first attribute\n\n\n\n\n\n\n\n\n\nWe can plot ALL twelve months of a variable (“SST”) with the coast and points shown. There is one slight modification to be made since a single call to plot() actually gets invoked 12 times for this data. So where do we add in the buoys and coast? Fortunately, we can create what is called a “hook” function - who knows where the name hook came from? Once the hook function is defined, it will be applied to the each of the 12 subplots.\n\n# a little function that gets called just after each sub-plot\n# it simple adds the coast and buoy\nadd_coast_and_buoys = function(){\n plot(coast, col = \"orange\", lwd = 2, add = TRUE)\n plot(buoys, pch = 16, col = \"purple\", add = TRUE)\n}\n\n# here we call the plot, and tell R where to call `add_coast_and_buoys()` after\n# each subplot is made\nplot(current['SST'], hook = add_coast_and_buoys)\n\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\nWarning in plot.sf(buoys, pch = 16, col = \"purple\", add = TRUE): ignoring all\nbut the first attribute\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nCoding Challenge\n\n\n\nUse the menu option File &gt; New File &gt; R Script to create a blank file. Save the file (even though it is empty) in the “challenges” directory as “challenge_script_1.R”. Use this file to build a script that meets the follwoing challenge. Note that the exisiting file, “challenge_script_0.R” is already there as an example.\nUse the Brickman tutorial to extract data from the location of Buoy M01 for RCP4.5 2055. Make a plot of SST (y-axis) as a function of month (x-axis). Here’s one possible outcome.\n\n\n\nBuoy M01, RCP4.5 2055",
"crumbs": [
"Coding"
]
Expand Down

0 comments on commit 419eb70

Please sign in to comment.