Making a movie with R

Since some models (like mine) do not always give a direct interface with results, R provides us with a powerful tool to play around with big piles of data models spit out. Obviously, you also may want to do this despite existing model interfaces!  The program R is making life often happy due to its endless possibilities. Since I am performing a vegetation model with individual trees modelled, one of my wishes was to see the forest in real life and 3D. So the forest is growing, waning, stabilizing and so forth. Making a movie with R!

All right, I directly admit: the movie is not made with R, but is calling another program from outside R; namely Image Magick in combination with the FFMPEG software. Nevertheless the code is totally in R and it works really nice.
If you have time-dependent graphs, plots, maps or whatsoever, you can use this neat function to make a moving pictures file. Of course you can also use a sleeper function within the R studio display, but it is easier for use to make a seperate movie.

First I will show you my personal code, which is making a movie of a growing tree stand and then explain it command by command:

# MAKING THE GRAPHS THAT FORM THE BASE FOR THE MOVIE!
TOT=floor(tlen/interval)    # decide on number of graphs needed, based on total time and time steps
for(step in 0:TOT){      # start the loop of making graphs here!
  t=1+(interval*step)             # step counter  
  # decide on some specific graph-parameters before and put them in 1 dataframe
  TreeStruct=data.frame(gridyx1,gridyx2,HTrees,CTrees,STrees,FTrees) 
  
  # USING LOGICAL NUMBERING FOR THE GRAPHS HERE!
  # open the file in which the graph is drawn: the following if-statements are to make sure the graphs are numbered well, 
  # so 100 is not appearing before 1 and so on...
  if(step<10){             # make sure generated graphs are numbers from 1-999
    jpeg(filename=sprintf("%sMovie/%s/Flash_00%s.jpeg",Folder,nameshort,step), width=1000, height=800, units="px")
  } else if(step<100){
    jpeg(filename=sprintf("%sMovie/%s/Flash_0%s.jpeg",Folder,nameshort,step), width=1000, height=800, units="px")
  } 
  
  # THE REAL GRAPH MAKING!
  with(TreeStruct, {
    s3d <- scatterplot3d(gridyx1, gridyx2,HTrees,             # x y (grids) and z (height) axis in scatterplot3d package
                         color=CTrees, pch=FTrees,            # color (species) and form (canopyform) of symbols
                         cex.symbols=STrees,                  # symbol size (canopy size)
                         zlim=c(0:40),# alpha=0.2,                        # maximum value for z-axis (height)
                         type="h", lty.hplot=2,               # lines to the horizontal plane and its line type (2=dashes)
                         grid=TRUE, main=sprintf("A Hectare of Tree Growth \n (time = %s)",t), # include the grid and graph title
                         xlab="", ylab="", zlab="Height (m)") # axes titles
                     })
  # Sys.sleep(0.5)      # possibility to let the movie run in the R-studio plot (might be disfunctional when using the whole script)
  graphics.off()   }    # the loop for making graphs ends here!
# MAKING THE REAL MOVIE HERE! USE IMAGE MAGICK AND FFMPEG SOFTWARE FOR LINUX
# create morphing images with same size (morph 3 = 3 images per graph):
cmd_morph <- paste0("convert ", sprintf("%sMovie/%s/*.jpeg",Folder,nameshort),
                    " -morph 3 ", sprintf("%sMovie/%s/",Folder,nameshort), "%05d.morph.jpg")
# create the movie from the morph images (-r 10 = movie speed 10 fps; -qscale 2 = quality class 2):
cmd_mov <- paste0("ffmpeg -r 10 -i ",sprintf("%sMovie/%s/",Folder,nameshort), 
                  "%05d.morph.jpg -qscale 2 ", sprintf("%sMovie/%s/%s_%.0f%.0f.mp4",Folder,nameshort,nameshort,x,y))
# run the command lines created above in linux terminal
system(cmd_morph) 
system(cmd_mov)

As one can see, I first make the graphs, that form the basis of the movie. I did not provide all details here, since it would make the script too long, due to the specifics of trees. Of course you can skip this all, when you have your own nice graphs. Just be aware you put them in a logical numbering order to read for the movie maker program later!
I also wanted to show here the s3d plotting function of R, which is really cool. It makes it possible to have 3D graphs. There are even options to look at the whole grid from different angles. (do not forget to download and call the package at the start of your R-script)

So, the real movie making starts at the very end of this script with the programs Image Magick and FFMPEG. In R you can create a command using the paste0-function, which is then called on the system with “system(command line)”.
We use two commands: the first I call “cmd_morph”, which creates the morphed pictures, so the movie is flowing; and the second one I call “cmd_mov”, which in fact makes the movie.

> cmd_morph <- convert inputfile/*.jpeg -morph x outputfile/%05d.morph.jpg
x = number of frames (morphed pictures) in between input graphs – the higher this number, the more flowinf the movie will be, but also the more time it needs to generate – standard is 2 to 5.
inputfile/ = file address of input graphs in jpeg or other picture type – take care this file only entails the graphs for the movie (or use inputfile/rootname*.jpeg).
outputfile/ = file address, where morphed output files will be stores.
%05d.morph.jpg = name of output file using 5 numbers (for max. 99999 frames) – change this 5 into 3 (max 999 frames) or any other number, depending on the frame number x.

> cmd_mov <- ffmpeg -r a -i inputfile/%05d.morph.jpg qscale b outputfile/name.mp4
a = the r-factor, meaning the number of frames per second – standard is 10.
b = the qscale-factor, meaning the quality class of the movie – standard is 2.
inputfile/05d.morph.jpg = file addres of the morphed input files, that resulted from the previous command cmd_morph.
Outputfile/name.mp4 = file address for the movie – consider a specific name for the location and time.

Hope this bit of code has entertained you a little bit.

Advertisements

Pre-Columbian human history in the Amazonia remains disputed

This month two contesting papers have been published on the question how pre-Columbian society in the Amazon looked like. One is from Piperno et al. in The Holocene and the other one is from Clement et al. in Proceedings of the Royal Society. Both are putting forward an alternative to the now-abandoned concept of a ‘virgin’ Amazon. Previously, is was believe the Amazon rainforest was a pristine untouched wilderness before European settlers came over. The discovery of so-called ADE (Amazonian Dark Earth), prehistoric geometric ditches and the preposition of certain plant concentrations being a relic of prehistoric management led researchers to the conclusion that the region has actually been relatively densely populated. In 2013, Stéphan Rostain wrote an interesting book on the topic called Islands in the rainforest.

Paleostructures

Ditches of old settlements found in Acre state, Brazil (from Rostain, 2012).

Today, researchers are divided.

Clement et al. explain all archaeological sites as a connected system. Densely populated regions were found along the rivers, which also spread along smaller tributaries and locally in between rivers. They describe the region as a social-ecological mosaic landscape with production systems. The lack of more archaeological evidence in the dense forests is due to its large area and its remoteness, rather than the preposition nobody had lived there. Instead, they state the current findings actually proof these regions might as well be full of undiscovered man-made features.

On the other hand, Piperno et al. is trying to urge for caution in proposing wild hypotheses and stresses that the present archaeological findings are still open for interpretation and rather local, not at all wide-spread. They acknowledge a much higher population number before the arrival of Europeans and also propose large communities along the main rivers, but not much more. The press seemed to pick up more on the paper of Clement et al.

Archeological sites

Locations of pre-Columbian settlements (from Clement et al., 2015)

p.s. That same week, the press picked up on related news from the Amazon in genetics. The tribes living in the Amazon for millennia seem to be closely related to Australasian people (Australian and Papuan indigenous people). The discovery of Skoglund et al. is, just as the research of Piperno et al. and Clement et al., breaking with the well-established theories. Until now the roots of native Americans have been believed to come from Eurasians crossing the Bering street during the end of the Ice Age. These wanderers then moved south. The new research shows it might have been a little bit more complex back in the days. Who were / are the Amazonian tribes?

Reasoning Conservation: Prudence, Justice and the Good Life

This week I had the pleasure of attending a lecture by Uta Eser on the PopeWHY of nature conservation. It seems a stupid question in our field of research and is actually seldom questioned. Moreover, even the pope is fighting climate change nowadays! The story on climate change and extinction of species is well-known. The facts are known. However, there is often an implicit task in this story: an urging plea to do something about it. For action nevertheless, one needs to rationalize and justify why the facts are worth doing something about.

Uta Eser discussed that the rationality of protecting nature, biodiversity and traditional landscapes can be divided in three pillars: Prudence (Intelligence), Justice and the Good Life (Happiness).

Prudence has been mostly used and is referring to the existential need for nature; it serves us in the basic needs to survive. In communication, one often uses the picture of the guy sawing the branch on which it is sitting: you must be out of your mind to saw the very branch which is supporting you! The analogy holds for the sustenance and insurance of the human inhabitants of planet earth. One could think of the scientific approach on ecosystem services and its (monetary) value. However, we get into a pitfall: the collective WE does not always have the same values and wishes as the individual WE. The WE is becoming a generalization and its rhetoric is concealing important issues.Branch

Justice is the second pillar and one of these issues. It might help to redeem the pitfall of prudence. Justice is looking at who is sawing the branch and who will fall out of the tree when the branch is cut. It goes without saying that industrialized western countries are very active in handling the saw, mostly indirect. Our needs (USA is needing 4 planets, while India is needing 0.4 planet!) are being supplied by developing countries, where resources and cheap labour are available. Their branch is being sawed through (think of oil palm plantation, open pit mining and oil exploitation in the Amazon to name a few). In a broader spectrum one could also call upon human being within the chain of living species (biodiversity) or the future generation, who will not have much left over from planet earth. What is justifying us to exploit its resources in such a way? This justification is also used by the pope. However, justification gives us an obligation and people start to rethink the thesis and actually question whether we really need all this nature to survive as a species.

At this point the third pillar comes in: Happiness for All. This appeals to our subjective reasoning of feeling good and comfortable. As the Club of Rome stated in 1972, the crux is not whether human species can survive, but whether its existence will be worthwhile. By protecting nature we provide a better life, improving our relation to ourselves, human kind and earth, involving values, ethics and happiness.

More reading?
Eser, Nuereuther, Seyfang and Muller (2013): Prudence, Justice, and the Good Life. A typology of ethical reasoning in selected European biodiversity strategies

Fragmentation patterns and ecosystem services

Landscape fragmentation and human-transformed forests have been subject of discussion in the comparison between land sharing and sparing, as suggested by Phalan et al. 2011. They elaborate on the ecological benefits of intensified agriculture in contrast to organic agriculture, due to its protection potential of a larger size of core forests. This view is supported by research of Gibson et al. 2011, who found that the ecological value of primary forest is far superior to any other human-transformed land cover, such as secondary forests.

However, assisting the restoration of degraded landscapes might proof to be effective, as mentioned by Jakovac et al. 2015. No data has thus far been analysed thoroughly. Another type of human-transformed forests are the selectively logged forests, and these seem to provide a rather high potential for many ecosystem services, as recently picked up by Bicknell et al. 2015. He suggests to close off logging roads as being the most effective and quick way to sustainably manage tropical forests and acquire the highest conservation value.

Contradictory to these statements, Mitchell et al. 2015 recently reframed ecosystem services in fragmented landscapes by including its accessibility to people, pointing to the possible positive effect of fragmentation. The authors urge for a better understanding of the importance of the accessibility of ecosystem services to underpin the correct decision-making activities. One of the tasks within our team will be to use the latest models in testing the above mentioned hypotheses.

The Amazon Carbon Sink remains a mystery – feedbacks and landscape transformation

Recently, Brienen et al. 2015 published results from different long-term projects on Amazon forest dynamics, showing a generalized decline of the biomass carbon sink. While more than a decade ago, Betts et al. 2004 modelled the so-called die-back of the Amazonia, recent models have been showing an increase of the biomass carbon sink well into the 21st century due to CO2 fertilisation (for example Huntingford 2013). Brienen et al 2015 now argue a decline in the capacity of the carbon sink, caused by an accelerated tree life cycle, increasing the biomass mortality. The carbon uptake is simply getting oversaturated due to the high atmospheric CO2 concentrations. Will the new-generation models show a return of the die-back theory?  As yet, no effect of CO2 fertilisation or accelerated tree life cycles have been found in tree ring studies, such as done by Van der Sleen 2015.

Simultaneously, Haddad et al. 2015 published another study from long-term projects on world-wide forest dynamics. They show fragmentation has long-lasting impacts on biodiversity, nutrient cycling and ecosystem services. They furthermore state that currently more than 70% of the remaining forests are within a single kilometre from its edge and thus severely fragmented! The tropical forest around the Amazon is one of the two regions with a relatively contiguous area. Nevertheless, the fragmentation of the Amazon still results in a considerable carbon loss of approximately 10% of the total, according to Pütz et al. 2014.

The decline of the biomass carbon sink and the effects of encroaching human-transformed landscapes, including fragmentation, show dire consequences of declining ecosystem services potential and conservation value. The two processes might have a coupled effect, decreasing the total carbon storage of the tropical forests. Can this have consequences for the Payment for Ecosystem Services?

Environment, Climate, Humans, Change….

January was cold and grey in Germany. Despite it gave me a bit of fire to dare and challenge some thinking on global changes. Hopefully inspirational… Trying to maybe put the world as we know it into perspective.

Our ancestors in the Ice Age
A few weekends ago, I visited the LVR (Landschaftsverband Rheinland) museum in Bonn, which is currently showing an exhibition on humans living during the final countdown of the last Glacial Maximum, some 15,000 years ago. It gave an amazing picture on the totally different world in Western Europe, compared to what we are used to in the 21st century. Our current forested and agricultural land was a vast savannah, with gigantic animals roaming it.  The human population was less than 10,000 souls and thus had enough food. We wandered around over the floor of the North Sea, where enormous paleo-rivers bursted bewildered over the landscape. However, climate change forced the sea level to rise dramatically, increasing temperatures and rainfall (the 6 degrees lower temperatures fixed much of the available moisture in ice caps). The climate change process made the Europe suitable for forests and thriving plant communities. People gradually became the architects of their surrounding landscape.

Human architects…
A few days later I went to a lecture on the effects of climate change, organised by the Frankfurter Geographische Gesellschaft. My supervisor, Prof. Hickler spoke on the effects related to biodiversity and ecosystems. The IPCC report on the devastating amounts of carbon and other pollutants pushed into the atmosphere was one part. Another part was on the physical effect of human being on the landscape. It reminded me of a recent study, stating that Europe nowadays has more forest than a century ago. In Europe, massive deforestation occurred a few centuries ago, when instead of oil, wood was used as fuel. The landscape was being exhausted and clearcut, something which is happening currently in some tropical countries. The whole ecosystem changed dramatically and large mammals disappeared.

Tropical forests in the climate change century
The two stories came together while reading a somewhat outdated textbook, titled ‘Tropical Forest in Transition’, edited by Johann Goldammer in 1992. This book makes clear that the stable status of tropical rainforest is a fairytale. We want to believe the large Amazon forest has always existed the way we know it nowadays. This is simply not the truth. Climatic changes were not restricted to the Northern latitudes. Incursions of drier savannah landscapes in the transitional forest of the Amazon happened regular and rainforests got a patchy occurrence. Refugia of tropical species surely existed during the ice ages and from here the rainforest developed to its large extent. These refugia could have existed in secluded river catchment, compared to the subtropical forests we find today. Rainforests always existed, but altered. They had to survive paleo-Indians setting fire and huge paleo-El Nino events roaring around the region. The rainforest resided.

Gedankenexperiment
The visit to the LVR-Museum, the lecture of the FGG and the insights from Goldammer let me to the following:

  • In the past, climate change has drastically altered the landscape in Northern latitudes, changing a savannah with mega-fauna into a forest without mega-fauna.
  • In the past, humans have drastically altered the landscape in Northern latitudes, deforesting an enormous area, resulting in a destruction of ecosystems.
  • Although tropical forests seem to be relatively stable, it is also subject to frequent environmental changes, showing stages of a more open and patchy landscapes.
  • Currently, we push such large amounts of carbon into the atmosphere that the temperature rises more than the ice age fluctuation.
  • Currently, we are deforesting large extents of forest, similar (and more) to the deforestation in the Northern latitude.
  • Therefore, we can expect alterations in the tropical regions. We probably see ecosystems, which are common today, pushed into refugia, while others take over. This might force us to change our living standards and think once again carefully about system earth we live on.

PaleoTemp

The geological temperature fluctuations according to the wikipedia site for ‘Geologic Temperature Trend’ (Glen Fergus)

http://www.landesmuseum-bonn.lvr.de
http://www.wageningenur.nl/en/newsarticle/Europe-greener-than-100-years-ago.htm

On the Geology of the Amazon River

Being a geologist from principle, I could not resist myself to have a look into literature on the paleo-Amazon. This information will help in the understanding of the Amazon vegetation structure nowadays. Perhaps even to understand future trends, although these future trends that we tend to predict have a very different time scales.

Initially the Amazon did not flow from west to east
The first indications of the Amazon River can be found back some 40 million years ago, during the Paleogene. The recently formed Andes and the existing high-topography shields of Guyana and Central Brazil formed a huge interior sea, in which the headwaters of the Amazon formed. This was the so-called Pebas basin (lake) and coincided with the current dense rainforest of the western Amazon region. In this basin, a network of smaller rivers was formed, which we might better categorize as being tributaries of the paleo-Orinoco. In those times, the Amazon did not flow to the east, but instead followed a route north draining into the present-day Gulf of Mexico. For a long time, the Pebas basin was a huge drainage pot for its surrounding mountains (Andes and Guyana Shield), and functioned like a kind of extension of the Gulf of Mexico. From the Gulf, sea level fluctuation, resulting climate change, induced the occasional flooding (incursions) and draining of the basin, forming the lacustrine (lake) sediments nowadays found in the western Amazon. The real Amazon was only a minor river draining on the west side of the Guyana shield.

A breakthrough!
During the Miocene things changed. We are jumping to approximately 20 million years ago. World-wide temperatures were decreasing from a very hot climate to a more temperate climate (similar to present-day). Meanwhile, the uplifting of the Andes continued rapidly and mountain ranges formed in the northern part of Latin America (Venezuela and Colombia). The Pebas basin was slowly cut off from its drainage point in the Gulf and the Amazon experienced more resistance to flow in that direction. Searching another route, the Amazon River cut itself loose from the Orinoco River and moved eastward. The Pebas basin developed into a large inland wetland, vegetated mainly with palm swamps and lowland riverine forest. Only at the end of the Miocene, some 10 million years ago, the river managed to breach through the so-called Purus Arch and connected to the much smaller eastern Amazon River. This break-through must have forced a violent burst and ecological disasters. In the fan delta of the current Amazon River,we find a sharp change in its sedimentary succession from this time onwards with significantly higher sedimentation rates. During the first couple of million years, the Amazon River is still not well developed and the Pebas wetland coexisted along with the river draining it.
Geo2Amazon

Fluctuations during ice ages
In the Pliocene (around 5 million years ago), ice ages started to appear. The era of ice ages meant a rapid fluctuation in temperature, rainfall and sea level. The waxing and waning of polar ice caps did not just happen once, but many times. During this phase the Amazon River developed to its present form, incising deeper and deeper into the gorge, which it had formed by connected east and west breaking through the Purus Arch. Swamps and lakes still persisted, and flooding events due to reformations of the river course were common. Only since the last ice age the current Amazon River channel along Manaus formed: 5,000 to 2,500 years ago major flooding events occurred establishing this flow channel. This was while the Romans conquered the old world.

 Thus, although the Amazon river nowadays seems to be a very stable river, it did not always exist in the way we know it and it shows to be a living and moving system.

Some papers on the Miocene river reversal: Hoorn, C.; Guerrero, J.; Sarmiento, G.A.; Lorente, M.A. (1995); Andean tectonics as a cause for changing drainage patterns in Miocene northern South America; Geology 23 (3); p. 237-240  &  Figueiredo, J.; Hoorn, C.; Ven, P. van der; Soares, E. (2009); Late Miocene onset of the Amazon River and the Amazon deep-sea fan: Evidence from the Foz do Amazonas Basin; Geology 37; p. 619-622
A paper on the Holocene Amazon: Fatima Rossetti, D. de; Toledo, P.M. De; Goes, A.M. (2005); New Geological framework for Western Amazonia (Brazil) and implications for biogeography and evolution; Quaternary Research 63; p. 78-89

A tree strategy to survive droughts: HD

After some time of literature and even more literature study on climate change, droughts, upcoming climatic disasters and the total dy-back of the Amazon rainforest, I was most amazed by the technology which trees develop in surviving these horrible futuristic scenarios.
Let me explain. The Amazon rainforest is a system, which keeps itself alive in different ways. One of them is the water recycling by ways of high evaporation rates, bringing moisture back in the atmosphere and pumping water-loaded air deeper into the forest.
The other is a mechanism called hydraulic distribution, which I want to address here. Despite the above mentioned water recycling, large parts of the Amazon rainforest experience an annual dry period of around three months. Nevertheless, the rainforest does not lose its leaves or reduces its biomass production (such as the trees in German winter!). Instead, the tree itself has the ability to govern its own water resources.

What happens exactly? The trees have two sets of root, namely the taproots and lateral roots. The taproot goes deep into the soil to reach deeper water resources in aquifers, while lateral roots spread directly from the tree to get water from the topsoil. Normally both the taproot and lateral roots will suck water from the soil into the trunk, which transports it to the canopy (middle picture b).
However, when rains fall down, this system changes overnight. Instead of tapping water from deep aquifers, the tree will only tap water from the topsoil and actually transports the water via the taproot down into the aquifer (right picture c). The tree is saving the rich harvest of fresh rain in the deeper soil layers! Now the tree will have some savings for the dry period.
However, trees seem to be very social and practical. During the dry period, it applies another process, which also happens overnight. While the taproot keeps on sucking soil water from deeper aquifers, the lateral roots use some of the water and push it into the topsoil, wetting it for its own use during the day and meanwhile providing water for fellow plants around (left picture a).

Hydraulic redistribution

The system is actually typical for trees in the Savanna and other dry regions, but seems to work well in the humid tropics as well. However, we do not really understand when the tree decides to change its fluid transport from storing to distributing it. Therefore, in physically based vegetation models it is often not modeled, but more or less lumped within some measure of resilience to drought. Does it remain too mystical or complicated to include the amazing ability of trees to redistribute soil water?

Want to read more? Oliveira and associates published a very interesting paper in Ecophysiology (March 2005), titled “Hydraulic redistribution of three Amazonian trees”. A bit later, Lee and associates published another paper in PNAS (October 2005), titled “Root functioning modifies seasonal climate”, that also gave an in-depth description, including the picture used in this post.