Instantly share code, notes, and snippets.

🎯
Focusing

Koen Hufkens khufkens

View GitHub Profile
@khufkens
khufkens / bad_projection_read.Rmd
Created Jan 24, 2019
reproducible example of a bad projection read - gdal or netcdf library issue?
View bad_projection_read.Rmd
---
title: "Faulty projection read"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
The default projection data in returned tiles seems to be faulty. Mainly the second parallel North is set to 45 where it should be 60. Below is a reproducible example of the issue.
@khufkens
khufkens / gist:5fe99a9606f544b2d5d6a356a3f93fdd
Created Dec 9, 2018
Correct PDF author / title meta-data
View gist:5fe99a9606f544b2d5d6a356a3f93fdd
#!/bin/bash
# Insert PDF meta-data into exif header
# for pdf files with format author - year - title.pdf.
# This allows e-readers to query meta-data for proper
# display and sorting of authors and titles
# (note: relies on exiftool)
for filename in *.pdf; do
noext=`echo "${filename%.*}"`
@khufkens
khufkens / el_nino_precip_analysis.r
Created May 10, 2018
CHIRP drought / precipitation differences between the Amazon and Congo Basin during the 2015 El Nino
View el_nino_precip_analysis.r
# map El Nino drought in the Amazon and Africa
library(raster)
library(tidyverse)
library(maps)
library(RColorBrewer)
# set colour theme
colours = brewer.pal(11,"RdBu")
# download CHIRP data
@khufkens
khufkens / daymet_server_tests.r
Created Apr 27, 2018
Daymet single pixel server tests
View daymet_server_tests.r
# load required libraries
if(!require(devtools)){install.package("devtools")}
if(!require(daymetr)){devtools::install_github("khufkens/daymetr")}
library("daymetr")
# no delay calls
no_delay = unlist(lapply(1:100, function(...){
# download data
error = try(download_daymet(silent = TRUE))
@khufkens
khufkens / MCD12Q1_median_class.js
Created Nov 12, 2017
Median MODIS Land Cover (MCD12Q1) Map
View MCD12Q1_median_class.js
// years to process (from start year t0 to end year t1)
var t0 = "2001";
var t1 = "2014";
var LC = ee.ImageCollection('MCD12Q1')
.select('Land_Cover_Type_1')
.filterDate(t0.concat("-01-01"),t1.concat("-12-31"))
.median();
// Create a geometry representing an export region.
@khufkens
khufkens / tropicos_species_distribution.r
Created Oct 18, 2017
Scrape Tropicos for species distribution information
View tropicos_species_distribution.r
#' @param species: genus species or genus
#' @param quiet: TRUE / FALSE provides verbose output
#' @keywords Tropicos, species distribution
#' @examples
#'
#' # with defaults, outputting a data frame with species distribution
#' # for Clematis
#' df <- tropicos.species.distribution()
#' # returns NA if no data are present
#' [requires the rvest package for post-processing]
@khufkens
khufkens / robinson_map.r
Last active May 17, 2017
An example of the robinson maps often used to represent global modelling data
View robinson_map.r
library(raster)
library(maps)
library(maptools)
library(sf)
library(scales)
# set coordinate systems
robinson = CRS(" +proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")
latlon = CRS("+init=epsg:4326")
@khufkens
khufkens / polar_map.r
Last active Nov 13, 2018
Fancy polar plot in R
View polar_map.r
#-------- arctic map
library(sp)
library(maps)
library(rgeos)
# function to slice and dice a map and convert it to an sp() object
maps2sp = function(xlim, ylim, l.out = 100, clip = TRUE) {
stopifnot(require(maps))
m = map(xlim = xlim, ylim = ylim, plot = FALSE, fill = TRUE)
@khufkens
khufkens / extract_MODIS_phenology_layer.r
Created Apr 14, 2017
Extract MODIS phenology dates (as DOY) from the MCD12Q2 HDF files and export as tiff.
View extract_MODIS_phenology_layer.r
# calculate adjusted phenology dates
# for greenness onset and maximum
extract_MODIS_phenology_layer <- function(value="increase",tiles_file="mytiles.txt"){
# load required libraries
require(raster)
require(MODIS) # to import the SDS layers
# list all hdf files, and extract unique years
@khufkens
khufkens / swath2grid.sh
Created Apr 14, 2017
Converts MOD04 'swath' data to gridded data, using internal ground control point data.
View swath2grid.sh
#!/bin/bash
#
# swath to grid conversion for
# MOD04 reflectance data
#
# get the reprojection information
gdal_translate -of VRT HDF4_EOS:EOS_SWATH:"$1":mod04:Mean_Reflectance_Land land.vrt
gdal_translate -of VRT HDF4_EOS:EOS_SWATH:"$1":mod04:Mean_Reflectance_Ocean ocean.vrt