Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Created August 10, 2022 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdsumner/5e4b3cc71329b1432aa1d6a7ba769a7a to your computer and use it in GitHub Desktop.
Save mdsumner/5e4b3cc71329b1432aa1d6a7ba769a7a to your computer and use it in GitHub Desktop.
library(sf); sf_use_s2(FALSE)
#> Linking to GEOS 3.10.2, GDAL 3.4.3, PROJ 8.2.0; sf_use_s2() is TRUE
#> WARNING: different compile-time and runtime versions for GEOS found:
#> Linked against: 3.10.2-CAPI-1.16.0 compiled against: 3.10.1-CAPI-1.16.0
#> It is probably a good idea to reinstall sf, and maybe rgeos and rgdal too
#> Spherical geometry (s2) switched off
library(foieGras)
#> Warning in checkTMBPackageVersion(): Package version inconsistency detected.
#> foieGras was built with TMB version 1.8.1
#> Current TMB version is 1.9.0
#> Please re-install 'foieGras' from source using install.packages('foieGras', type = 'source') or remove your current TMB version and re-install using remotes::install_version('TMB', version = '1.8.1')

library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(viridis)
#> Loading required package: viridisLite
## stay always in -180,180
wrap_lon <- function (lon, lmin = -180) {
  (lon - lmin)%%360 + lmin
}
x <- ellie

## comment this line on or off: on for centre on anti-meridian
#x$lon <- scales::rescale(x$lon, c(160, 200))
centre <- wrap_lon(mean(x$lon))
x$lon <- wrap_lon(x$lon)
prj  <- sprintf("+proj=stere +lon_0=%f +lat_0=-90 +lat_ts=-71  +datum=WGS84", centre[1])



fit <- fit_ssm(x, vmax = 4, model = "crw", time.step = 24, control = ssm_control(verbose = 0)) 
mpm <- fit_mpm(fit, what = "predicted", model = "mpm", control = mpm_control(verbose = 0))

## spatialize and plot "g"
g <- cbind(mpm$mpm[[1]]$data[c("date", "x", "y")], mpm$mpm[[1]]$fitted["g"])
g %>%
  st_as_sf(coords = c("x", "y"), crs = "OGC:CRS84") %>%
  st_transform(prj) %>%
  ggplot(.) +
  geom_sf(aes(col = g)) +
  scale_color_viridis()

## recentre about anti-meridian
x$lon <- scales::rescale(x$lon, c(160, 200))
centre <- wrap_lon(mean(x$lon))
x$lon <- wrap_lon(x$lon)
prj  <- sprintf("+proj=stere +lon_0=%f +lat_0=-90 +lat_ts=-71  +datum=WGS84", centre[1])



fit <- fit_ssm(x, vmax = 4, model = "crw", time.step = 24, control = ssm_control(verbose = 0)) 
mpm <- fit_mpm(fit, what = "predicted", model = "mpm", control = mpm_control(verbose = 0))

## spatialize and plot "g"
g <- cbind(mpm$mpm[[1]]$data[c("date", "x", "y")], mpm$mpm[[1]]$fitted["g"])
g %>%
  st_as_sf(coords = c("x", "y"), crs = "OGC:CRS84") %>%
  st_transform(prj) %>%
  ggplot(.) +
  geom_sf(aes(col = g)) +
  scale_color_viridis()

Created on 2022-08-10 by the reprex package (v2.0.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment