Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raster_spaghettiPlot() fails with rasterBricks #35

Closed
jonathancallahan opened this issue Jun 10, 2020 · 2 comments
Closed

raster_spaghettiPlot() fails with rasterBricks #35

jonathancallahan opened this issue Jun 10, 2020 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@jonathancallahan
Copy link
Member

jonathancallahan commented Jun 10, 2020

I think this has to do with the lake of a name for use in the legend.

We have to have a way to name things if the raster parameter is a RasterBrick object.

Probably just allowing a rasterName parameter and checking that this is specified if raster is a RasterBrick.


Reproducible error

library(AirFireModeling)
setModelDataDir('~/Data/BlueSky')

# Creating PWFSLSmoke ws_monitor objects requires:
library(MazamaSpatialUtils)
PWFSLSmoke:::initializeMazamaSpatialUtils()

# Portland, Oregon
longitude <- -122.68
latitude <- 45.52

models <- bluesky_findModels(longitude, latitude)

# Load model data
rasterList <- raster_load(
  modelName = c("PNW-1.33km", "PNW-4km"),
  modelRun = 2019100900,
  xlim = c(-125, -115),
  ylim = c(42, 50)
)

raster_spaghettiPlot(
  rasterList[[1]],
  longitude = longitude,
  latitude = latitude,
  radius = 10, # km
  rasterName = "DUMMY"
)
Converting raster to ws_monitor ...
Error in UseMethod("select_") : 
  no applicable method for 'select_' applied to an object of class "NULL"
In addition: Warning message:
In if (stringr::str_detect(class(monitorList), "Raster*")) { :
  the condition has length > 1 and only the first element will be used
@jonathancallahan jonathancallahan added the bug Something isn't working label Jun 10, 2020
@jonathancallahan jonathancallahan self-assigned this Jun 10, 2020
@jonathancallahan jonathancallahan added this to To do in AirFireModeling 0.3 via automation Jun 30, 2020
@tabrasel
Copy link
Contributor

I found two issues here:

  1. The first had to do with the argument passed to the internal .meltTargetMonitor() function. This function expects a single ws_monitor as a parameter, and should be called differently depending on whether monitorList is an actual list of ws_montors or is a single ws_monitor object itself:
# Create ggplot dataframes
if ( stringr::str_detect(class(monitorList), 'Raster*') ) {
  df <- .meltTargetMonitor(monitorList)
} else {
  df <- lapply(monitorList, .meltTargetMonitor) %>%
    dplyr::bind_rows()
}

However, it doesn't look like the stringr::str_detect(class(monitorList), 'Raster*') condition tests this properly, since in either case it returns false and runs the else block. Now this is fine when monitorList is an actual list of RasterBricks, but it breaks when monitorList is a single ws_monitor because the lapply() call tries to apply .meltTargetMonitor() to the contents of each of monitorList's contents (meta and data). Therefore it can't find some of the data it expects.

So to solve that issue, I think we just need to change how we determine what monitorList is: An actual list of ws_monitors, or a single ws_monitor itself. Everything works just fine in the latter case when the non-else block is run instead.

I'll try: stringr::str_detect(class(monitorList), 'ws_monitor')

  1. Unfortunately, there was still a problem. When a single RasterBrick is given as a parameter to the spaghettiPlot function and converted to a ws_monitor it's meta$modelName is in the form of "DUMMY_525bf68f1703b6ad" instead of "PNW.1.33km_2019100900_ea97e70f44eaed44". Therefore the .meltTargetMonitor() function is not able to remove the string pattern from the modelName:
# Melt the data
moltenTargetDist <-
  monitorList$meta %>%
  dplyr::select(.data$monitorID, .data$targetDistance) %>%
  reshape2::melt(id.vars = 'monitorID') %>%
  dplyr::mutate(variable = .data$monitorID, 'monitorID' = NULL) %>%
  dplyr::mutate(modelName = stringr::str_remove(.data$variable, '_.+_.+'))

This makes all the model entries in the ggplot data unique, and tries to render a facet for each one (there could be hundreds!). All that's needed to fix this is change the pattern to '_.+' so it's works for both versions of modelName.

AirFireModeling 0.3 automation moved this from To do to Done Jul 13, 2020
@jonathancallahan
Copy link
Member Author

jonathancallahan commented Jul 13, 2020 via email

@jonathancallahan jonathancallahan moved this from Done to In progress in AirFireModeling 0.3 Jul 22, 2020
@jonathancallahan jonathancallahan moved this from In progress to Done in AirFireModeling 0.3 Jul 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

No branches or pull requests

2 participants