Skip to content

Instantly share code, notes, and snippets.

View benfasoli's full-sized avatar

Ben Fasoli benfasoli

View GitHub Profile
@benfasoli
benfasoli / stilt-footprint-map-example.r
Last active February 22, 2021 22:36
Produce interactive visualization of time integrated footprint
#!/usr/bin/env Rscript
library(htmlwidgets)
library(leaflet)
library(raster)
library(viridis)
path <- 'example-footprint.nc'
footprint <- brick(path)
footprint
@benfasoli
benfasoli / proj_gdal_configuration.sh
Last active October 16, 2019 18:51
Configure proj4 and gdal libraries within user scope
#!/bin/bash
# source <(wget https://gist.github.com/benfasoli/c1ed43c7350a6253fed3bfd4e17743ef/raw/ -O -)
set -e
# Configure miniconda installation within user scope
source <(wget https://gist.githubusercontent.com/benfasoli/a7193b2bdd59e1b930efcb7facd6d12e/raw/ -O -)
conda update --all -y
conda install -y -c conda-forge --strict-channel-priority proj4 gdal
@benfasoli
benfasoli / python_configuration.sh
Last active April 2, 2020 22:33
Configure python within linux user scope
#!/bin/bash
# curl -sL https://gist.github.com/benfasoli/a7193b2bdd59e1b930efcb7facd6d12e/raw/ | bash
set -e
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda3.sh
bash miniconda3.sh -b -p $HOME/.miniconda3
rm miniconda3.sh
cp $HOME/.bashrc $HOME/.bashrc-miniconda-backup
echo '
@benfasoli
benfasoli / chpc_r_configuration.sh
Last active October 21, 2019 23:48
Configure R for user-scoped package management
#!/bin/bash
# wget https://gist.github.com/benfasoli/268c55ba75f33a997c58f8fcc46dd3af/raw/ -O - | bash
mkdir -p $HOME/.Rpackages && \
echo "system('source $HOME/.bashrc')
.libPaths(
c(
'$HOME/.Rpackages',
.libPaths()
@benfasoli
benfasoli / matrix_distance_weighting.r
Last active July 25, 2019 19:31
Normalized inverse distance weighting applied over a grid
#!/usr/bin/env Rscript
n <- 5
# fill matrix of distances
d <- matrix(nrow = n, ncol = n)
d[1:n, 1] <- 0:(n-1)
d[1, 1:n] <- 0:(n-1)
@benfasoli
benfasoli / gaussian_weighted_interpolation.r
Last active July 9, 2019 21:32
Weighted interpolation of sparse STILT footprints
#!/usr/bin/env Rscript
# Ben Fasoli
#
# Interpolation utilities for STILT footprints to predict footprints for
# finely gridded receptors using coarse STILT-calculated footprints for
# intermediate receptor locations.
#
# Uses weights derived from 2d Gaussian kernels to average footprints from
# the surrounding spatial area
#
@benfasoli
benfasoli / parallel_apply.py
Last active July 9, 2019 22:14
Split-apply-combine strategy for data mining in parallel with Pandas DataFrames
#!/usr/bin/env python
from multiprocessing import cpu_count, Pool
import pandas as pd
def parallel_apply(df, fun, n_chunks: int = None):
"""Apply function to batches of dataframe rows
Parameters
----------
@benfasoli
benfasoli / adc_worker.ino
Created March 28, 2019 22:26
Arduino template for polling analog voltages at given interval
// Ben Fasoli
const int n = 6;
const int pins[n] = {A0, A1, A2, A3, A4, A5};
const float slope[n] = {1, 1, 1, 1, 1, 1};
const float intercept[n] = {0, 0, 0, 0, 0, 0};
// Board reference voltage measured on aref pin
const float refVoltage = 4.93; // V
@benfasoli
benfasoli / raster_interpolation.r
Created March 20, 2019 22:46
Bilinearly interpolate raster to a different resolution
#!/usr/bin/env Rscript
library(raster)
# Create gridded example data
xyz <- expand.grid(x = seq(-112.4, -111.6, by = 0.03),
y = seq(40.3, 40.9, by = 0.03))
xyz$z <- with(xyz, abs(x * y))
r_coarse <- rasterFromXYZ(xyz, crs = '+proj=longlat')
r_coarse
#!/usr/bin/env Rscript
# Ben Fasoli
library(ncdf4)
library(raster)
files <- dir('raw', full.names = T)
data <- 0
for (file in files) {