## ============================================================================
## Dynamic tracer plots of the Ltrans model
## Data is part of the OceanView package
## example from help page of Ltrans
## implemented by Karline Soetaert
## ============================================================================
library(OceanView)
lon <- Chesapeake$lon
lat <- Chesapeake$lat
depth <- Chesapeake$depth
persp3D(x = lon, y = lat, z = -depth, colvar = depth, scale = FALSE,
expand = 0.02, main = "particle distribution", plot = FALSE)
plotrgl(lighting = TRUE, smooth = TRUE)
# you may zoom to the relevant region, or cut a region
# cutrgl()
for (i in 1:108) {
tracers3Drgl(Ltrans[, 1, i], Ltrans[, 2, i], Ltrans[, 3, i],
colvar = Ltrans[ ,4, i], col = c("green", "orange"),
main = paste("time ", i))
# remove # to slow down
# Sys.sleep(0.1)
}
---
title: "Create Ltrans video"
author: "Adri Knuijt"
date: "30 januari 2019"
output:
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r Init1, echo=FALSE}
ImagesDir <- "./Ltrans_images"
MovieDir <- "./Ltrans_movie"
```
```{r Init2, echo=FALSE}
MovieName <- "movie.mp4"
```
```{r Init3, echo=FALSE}
AppFFmpeg <- "v:/r/ffmpeg/ffmpeg/bin/ffmpeg"
```
### Creating Ltrans video
To create the video we first need to make the images the video is build with. After that an external open source program called ffmpeg will be used to create the video. This script was build for Windows, but since ffmpeg is cross-platform it should be easy to modify it to other platforms. You can download ffmpeg at https://www.ffmpeg.org/.
</br>
### Create directories for the images and movie
To store the images and the video two new directories (`r basename(ImagesDir)` and `r basename(MovieDir)`) in the directory this file is stored will be created. You can change the value of the variables ImageDir and MovieDir if needed.
```{r, ref.label='Init1', eval=F}
```
Here, the directories are created. If they already exits, nothing will happen with the content of the current directories.
```{r}
dir.create(ImagesDir, showWarnings = FALSE)
dir.create(MovieDir, showWarnings = FALSE)
```
### Filename of the movie
The value of the variable MovieName contains the filename of the movie that will be created. By default, the filename is `r MovieName`.
```{r, ref.label='Init2', eval=F}
```
### Pointer to the ffmpeg application
After downloading ffmpeg, install the program. The path to ffmpeg is important, because we need it in the last step. The variable AppFFmpeg points to the exact location of the ffmpeg executable, in our case: `r AppFFmpeg`.
```{r, ref.label='Init3', eval=F}
```
### Create initial 3D Ltrans image
First, create the initial 3D image which will be the base of the other images.
```{r, warning=F}
library(OceanView, quietly=T)
lon <- Chesapeake$lon
lat <- Chesapeake$lat
depth <- Chesapeake$depth
r3dDefaults$windowRect <- c(100, 100, 900, 900) # size of the images
# create the initial image
persp3Drgl(x = lon, y = lat, z = -depth, colvar = depth, scale = FALSE,
expand = 0.02, plot = T, box = FALSE, colkey = F)
```
</br>
**Important: use your mouse to move the image to a position with the best perspective to the action later on.**
Disclaimer: unfortunately, this is not possible if you run all the lines of this script at once. Please run the chunks one by one to create the movie from another perspective.
</br>
### Create the images for the video
```{r, out.width = '40%'}
rgl.snapshot(paste(ImagesDir, "/l", sprintf("%03d", 0), ".png", sep=""))
for (i in 1:108) {
tracers3Drgl(Ltrans[, 1, i], Ltrans[, 2, i], Ltrans[, 3, i],
colvar = Ltrans[ ,4, i], col = c("green", "orange"))
rgl.snapshot(paste(ImagesDir, "/l", sprintf("%03d", i), ".png", sep=""))
}
knitr::include_graphics(paste(ImagesDir, "/l078.png", sep=""))
```
### Create the video from the image
Call the ffmpeg program to create the movie from the images.
```{r, warning=F}
system(paste(AppFFmpeg,
"-y -framerate 12 -i",
paste(""", paste(ImagesDir, "l%03d.png", sep='/'), """, sep=""),
"-c:v libx264 -profile:v high -crf 5 -pix_fmt yuv420p",
paste(""", paste(MovieDir, MovieName, sep="/"), """, sep="")))
```
### Play the video
Browse to the movie in the directory file://V:/R/ffmpeg/ltrans/Ltrans_movie and play the movie.