Closed superjai closed 4 years ago
The commit just pushed above is a first pass at the core of the problem - generating an average SST for a slice in time for the entire sanctuary.
Here is an example of what it produces, which gives the average SST for the entire channels islands NMS for May 2020:
> mean_SST("cinms", "2020", "05")
[1] 15.72583
At first glance, this seems to be working properly. For example, you can see here that the SST raster and the Sanctuary polygons line up properly.
> plot(SST_map)
> plot(sanctuary_area$geometry, add = TRUE)
But a little more digging is required to ensure that the right raster cells are being extracted to calculate the average.
Great job Jai! Let's iterate updates in the following ways:
get_nms_ply()
to work generically in utility.R from rocky.R (so no weird Ben file dependencies). Or better yet, migrate to dedicated R 📦.st_bbox(nms_ply)
to get bounding box of any study area, eg sanctuary.rerddap
package to extract the gridded data given bounding box a la intro to rerddap vignette
require("rerddap")
sstInfo <- info('jplMURSST41')
# get latest daily sst
murSST <- griddap(sstInfo, latitude = c(22., 51.), longitude = c(-140., -105), time = c('last','last'), fields =
'analysed_sst')
I updated Ben's code to fix several bugs in the calculation of SST - the function rerrddap in the spatial.r file. The code now runs and I produced a csv (avg-sst_cinms.csv) that contains a history of mean and standard deviation of SST, using the function generate_all_SST.R (also in spatial.R). To do:
r <- raster(nc$summary$filename)
This line produces the error:
Error in as.Date(time, origin = startDate) : object 'startDate' not found
However, the line still runs - weird.
sanctuary_ply <- as_Spatial(st_union(get_nms_polygons(sanctuary_code)))
That produces the warning:
Warning message:
In sp::proj4string(obj) : CRS object has comment, which is lost in output
Having read into it, I don't think that this particular issue (which has to do with projections) really matters in our case, but I thought I would bring it up.
Error in UseMethod("st_geometry") : no applicable method for 'st_geometry' applied to an object of class "c('SpatialPolygonsDataFrame', 'SpatialPolygons', 'Spatial', 'SpatialVector')"
Output to https://github.com/marinebon/cinms/blob/master/data/oceano/avg-sst_cinms.csv
Next step in Viz:
The new time series figure plots both the average as well as the standard deviation of temperature and here is what it looks like.
I couldn't figure out the functions in oceano.R that were used to generate the previous version of the figure (plot_timeseries and generate_timeseries), so I just wrote a simple new function to create the new figure, which is here in oceano.R And here is the function itself:
plot_SST_timeseries <- function(csv){
# The purpose of this function is to generate the figure showing the sea surface temperature time series
# for a Sanctuary (displaying both avg and standard deviation of temp). The function has one parameter
# (csv) which is the path name for the csv data file to be plotted
# Read in the csv file
SST_history <- read.csv(csv, header = TRUE)
# create a data frame which lines up the data in the way that dygraph needs it
SST_history <- data.frame(date = as.Date(SST_history$date, "%Y-%m-%d"), SST = SST_history$average_SST, lower = SST_history$average_SST-SST_history$standard_deviation_SST, upper = SST_history$average_SST+SST_history$standard_deviation_SST)
SST_history <- xts(x = SST_history[,-1], order.by = SST_history$date)
# create the figure
dygraph(SST_history, main = "Sea Surface Temperature", xlab = "Date", ylab = "Temperature (°C)")%>%
dySeries(c("lower", "SST", "upper"), label = "Temperature (°C)", color = "Red")%>%
dyRangeSelector()
}
Currently, modal windows such as the one below, show SST for the polygon centroid.
SST should be calculated as the average across the sanctuary.