Skip to content

Instantly share code, notes, and snippets.

View srgorelik's full-sized avatar

Seth Gorelik srgorelik

View GitHub Profile
@srgorelik
srgorelik / map.R
Last active May 23, 2019 19:14
Quickly display a map of raster file from the command line.
#!/usr/bin/env Rscript
# sgorelik 2019
suppressPackageStartupMessages(library(optparse))
suppressPackageStartupMessages(library(rasterVis))
opt.list <- list(
make_option(c('-b','--band'), type = 'integer', default = 1, metavar = 'INTEGER',
help = 'The raster band to plot (default is %default).')
@srgorelik
srgorelik / reticulate.md
Last active May 13, 2019 18:02
How to use the reticulate package to work with Python interactively in an R session.

This is a quick walthrough on how to use the reticulate package to work with Python interactively in an R session.

First, to specify the Python version to call (e.g., 3 instead of 2), set a RETICULATE_PYTHON path variable:

$ which python3
/usr/bin/python3
$ echo "RETICULATE_PYTHON='/usr/bin/python3'" >> ~/.Renviron
@srgorelik
srgorelik / math.R
Last active April 11, 2019 16:56
A simple command-line tool for evaluating mathematical expressions.
#!/usr/bin/env Rscript
suppressPackageStartupMessages(library(optparse))
opt.list <- list(
make_option(c('-r','--round'), type = 'integer', default = 2, metavar = 'INTEGER',
help = 'Number of decimal places (default is %default)')
)
opt.parser <- OptionParser(option_list = opt.list,
@srgorelik
srgorelik / csv.R
Last active May 15, 2020 23:10
A simple R-based linux command-line tool to print a CSV file, a portion thereof, or a summary report, to the terminal.
#!/usr/bin/env Rscript
# csv - a linux CLI for nicely displaying and summarizing CSV files
suppressPackageStartupMessages(library(optparse))
suppressPackageStartupMessages(library(data.table))
file_ext <- function(x) {
pos <- regexpr("\\.([[:alnum:]]+)$", x)
tolower(ifelse(pos > -1L, substring(x, pos + 1L), ""))
@srgorelik
srgorelik / colorbar.R
Last active March 1, 2019 15:46
For plotting a color bar by itself. Adapted from http://www.colbyimaging.com/wiki/statistics/color-bars.
colorbar <- function(pal, min, max = -min, nticks = 11, at = seq(min, max, len = nticks), title = '', border = NA, dir = 'v') {
scale = (length(pal)-1)/(max-min)
graphics.off()
if (dir == 'v') {
w <- 1; h <- 5
xx <- c(0, 10)
yy <- c(min, max)
asp <- 2
side <- 4
} else if(dir == 'h') {
@srgorelik
srgorelik / extractCellsFromLayers.R
Created October 31, 2018 19:03
Extract grid cells from different layers in a raster stack in R.
extractCellsFromLayers <- function(stack, index) {
# --------------------------------------------------------------------------------
# Purpose:
# Extract grid cells from different layers in a raster stack.
#
# Arguments:
# stack A RasterStack object; the stack from which to extract cell values.
# index A RasterLayer object; the index used to extract cells from the
# stack. Cell values in the index raster indicate from which layer
# in the raster stack to extract the corresponding cell values.
@srgorelik
srgorelik / stats.py
Last active July 3, 2019 18:27
Print descriptive statistics of a raster or numpy array.
#!/usr/bin/env python3
from osgeo import gdal
import numpy as np
import pandas as pd
def stats(raster, band = 1, nodata = None):
"""Print descriptive statistics of a raster or numpy array"""
if (type(raster) != str) and (type(raster) != np.ndarray):
print("Error: input must be a raster filepath or numpy array.")
@srgorelik
srgorelik / img.R
Last active April 21, 2020 13:46
Quickly visualize a matrix or raster in R.
img <- function(x, pal = 'j', cut = F, brks = 10, units = '', rev.pal = F, vals = F, axes = T,
na.col = 'black', fg = 'black', bg = 'white', draw.cells = F, legend = T, ...) {
# --------------------------------------------------------------------------------
# Purpose:
# Quickly plot an image (using matrix or raster objects).
#
# Arguments:
# x Either a matrix or raster object.
# pal Color palette code (either letter or number selection).
# cut A logical flag to use a discrete color ramp instead of a continuous one.