noaa-onms / cinms

Channel Islands National Marine Sanctuary
https://noaa-onms.github.io/cinms
MIT License
3 stars 1 forks source link

Show average SST for entire NMS on "Key climate & ocean drivers" windows #28

Closed superjai closed 4 years ago

superjai commented 4 years ago

Currently, modal windows such as the one below, show SST for the polygon centroid. Screen Shot 2020-06-30 at 12 58 51 PM

SST should be calculated as the average across the sanctuary.

superjai commented 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)

Rplot

But a little more digging is required to ensure that the right raster cells are being extracted to calculate the average.

bbest commented 4 years ago

Great job Jai! Let's iterate updates in the following ways:

  1. Migrate 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 📦.
  2. Use st_bbox(nms_ply) to get bounding box of any study area, eg sanctuary.
  3. Use 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')

    image

superjai commented 4 years ago

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:

  1. The modal windows that contain the SST figure need to be modified to point to this csv.
  2. The SST figure needs to be modified to include both mean and sd in the plot.
  3. In the code there were two things that came up - they might be an issue or not. First, line 78 is as follows:
    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.

  4. The second issue is with line 54, which is:
    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.

  5. As a last thing, you will note that line 54 is a rewrite of your code Ben (changing from tidyverse form to the more old-fashioned form of writing R). I did this, because your version (which is still in the code, but commented out) produced some errors, but inconsistently so. The error is:
    Error in UseMethod("st_geometry") : no applicable method for 'st_geometry' applied to an object of class "c('SpatialPolygonsDataFrame', 'SpatialPolygons', 'Spatial', 'SpatialVector')"
bbest commented 4 years ago

Output to https://github.com/marinebon/cinms/blob/master/data/oceano/avg-sst_cinms.csv

image

Next step in Viz:

superjai commented 4 years ago

The new time series figure plots both the average as well as the standard deviation of temperature and here is what it looks like.

Screen Shot 2020-08-24 at 6 43 36 PM

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()
}