From 419eb70f789df1285599c31e4fe8555993eb31eb Mon Sep 17 00:00:00 2001 From: Ben Tupper Date: Wed, 11 Dec 2024 09:46:51 -0500 Subject: [PATCH] add coding challenge example --- C00_coding.qmd | 6 ++-- challenges/challenge_script_0.R | 59 +++++++++++++++++++++++++++++++++ docs/C00_coding.html | 3 +- docs/search.json | 2 +- 4 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 challenges/challenge_script_0.R diff --git a/C00_coding.qmd b/C00_coding.qmd index 9547476..f7bf54c 100644 --- a/C00_coding.qmd +++ b/C00_coding.qmd @@ -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) @@ -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) ::: diff --git a/challenges/challenge_script_0.R b/challenges/challenge_script_0.R new file mode 100644 index 0000000..c70ed1a --- /dev/null +++ b/challenges/challenge_script_0.R @@ -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! \ No newline at end of file diff --git a/docs/C00_coding.html b/docs/C00_coding.html index 86db1a6..3f36199 100644 --- a/docs/C00_coding.html +++ b/docs/C00_coding.html @@ -513,10 +513,11 @@

+

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 follwoing challenge. Note that the exisiting file, “challenge_script_0.R” is already there as an example.

Use 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.

-

+

Buoy M01, RCP4.5 2055
diff --git a/docs/search.json b/docs/search.json index d3ffdd1..6368413 100644 --- a/docs/search.json +++ b/docs/search.json @@ -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() |>\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 <chr> <chr> <chr> <chr>\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 |> 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'] |>\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() |>\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 <chr> <chr> <chr> <chr>\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 |> 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'] |>\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 > 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 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" ]