Skip to content

Instantly share code, notes, and snippets.

View xuanlongma's full-sized avatar

Richard Xuanlong Ma xuanlongma

View GitHub Profile
@xuanlongma
xuanlongma / doybacktodate.R
Last active December 29, 2015 05:39
covert day-of-year back to date object
strptime('2008201', format = '%Y%j')
## or
as.Date('2008201', format = '%Y%j')
@xuanlongma
xuanlongma / nc2tiff.R
Last active March 30, 2024 20:01
From netCDF to GeoTIFF using R
require(ncdf)
require(raster)
## Input: a netCDF file
file.nc <- 'rain.nc'
## Output: a GeoTIFF file
file.tiff <- 'rain.tiff'
## Import netCDF
# Script for question posted on Stack Overflow
# Load relevant libraries
library(ggplot2)
library(raster)
library(gridExtra)
vplayout <- function(x, y) {
viewport(layout.pos.row = x, layout.pos.col = y)
}
@xuanlongma
xuanlongma / gdal_reproject.R
Created May 24, 2013 08:21
change the projection using GDAL library
## a tiff file
input <- "modis.evi.tif"
output <- "modis.evi.reprojected.tif"
## GDAL command line for reprojection
proj.cmd.warp <- 'gdalwarp -t_srs \'+proj=latlong +datum=WGS84\' -r near -overwrite'
## Invoke the system GDAL command
system(command = paste(proj.cmd.warp, input, output, sep = ' '))
@xuanlongma
xuanlongma / pandoc_multi_input.md
Last active May 1, 2024 19:18
Combine multiple input files when using Pandoc

If multiple input files are given, pandoc will concatenate them all (with blank lines between them) before parsing. -- from Pandoc website

Pandoc command: pandoc -s input1.md input2.md input3.md -o output.html

@xuanlongma
xuanlongma / pandoc_md_html_bib_math.sh
Created May 12, 2013 11:49
Command line to convert markdown to html with bibliography and LaTeX math correctly displayed.
pandoc --bibliography=bibtex.bib --mathjax --parse-raw -s inpud.md -o output.html
@xuanlongma
xuanlongma / read_trmm_3b43.R
Last active December 15, 2015 07:49
read TRMM 3B43 V7 data
## TRMM 3B43 Monthly precipitation does NOT need flip (which 3B42 need)
r.trmm <- raster(nrows = 400, ncols = 1440, xmn = 0, xmx = 360, ymn =
-50, ymx = 50, crs="+proj=longlat +datum=WGS84")
r.trmm[] <- readBin(files.input, 'double', n = 576000, size = 4,
endian = 'big')
## TRMM 3B42
## r.trmm <- flip(rotate(r.trmm), 'y')
@xuanlongma
xuanlongma / plot_xts2.R
Created March 15, 2013 02:21
add "col" (color) arguments to plot.xts
## Arthur: Roman Luštrik
## http://stackoverflow.com/questions/9017070/set-the-color-in-plot-xts
plot.xts2 <- function (x, y = NULL, type = "l", auto.grid = TRUE, major.ticks = "auto",
minor.ticks = TRUE, major.format = TRUE, bar.col = "grey",
candle.col = "white", ann = TRUE, axes = TRUE, col = "black", ...)
{
series.title <- deparse(substitute(x))
ep <- axTicksByTime(x, major.ticks, format = major.format)
otype <- type
@xuanlongma
xuanlongma / fun_aggre16d.R
Created March 14, 2013 12:49
aggregate daily rainfall time series to 16 day temporal resolution
## x: daily rainfall time series
## lindex: date stamps of the MOD13 VI product time series
## rindex = lindex + 15
## TODO: increase the efficiency by replace the loop use sapply
fun.aggre16d <- function(x, lindex, rindex) {
v.16d.rain <- vector('numeric', length(lindex))
for (i in 1:length(lindex)) {
v.16d.rain[i] <- sum(x[lindex[i]:rindex[i]], na.rm = T)
}
@xuanlongma
xuanlongma / fun_ccfmax.R
Created March 14, 2013 08:00
compute the cross-correlation coefficient and check the p.value
## Compute the cross-correlation Pearson product-moment correlation coefficient
## and check the p.value
## This function is an alternative of the native ccf() function which does not
## return p.value by default
fun.ccfmax <- function(x) {
a <- x[1:(length(x) / 2)]
b <- x[-(1:(length(x) / 2))]
if (all(is.na(a)) | all(is.na(b))) {
return(c(NA, NA, NA))