Skip to content

Instantly share code, notes, and snippets.

View benfasoli's full-sized avatar

Ben Fasoli benfasoli

View GitHub Profile
@benfasoli
benfasoli / limit_asyncio_concurrency_example.py
Created January 23, 2022 17:18
Limit concurrency with Python asyncio
import asyncio
from typing import Coroutine, List, Sequence
def _limit_concurrency(
coroutines: Sequence[Coroutine], concurrency: int
) -> List[Coroutine]:
"""Decorate coroutines to limit concurrency.
Enforces a limit on the number of coroutines that can run concurrently in higher
@benfasoli
benfasoli / extract-coordinates-from-polygons.r
Last active November 1, 2023 11:28
Methods for extracting coordinates from spatial objects (e.g. SpatialPolygonsDataFrame)
# Ben Fasoli
library(sp)
library(rgdal)
shape <- readOGR(dsn = 'data', layer = 'four_samples')
head(shape@polygons[[1]]@Polygons[[1]]@coords)
# [,1] [,2]
# [1,] -111.6227 40.75247
@benfasoli
benfasoli / tunnel
Last active April 6, 2021 21:57
MacOS SOCKS tunnel
#!/bin/bash
# SOCKS proxy via ssh tunnel for MacOS
# Install to /usr/local/bin/tunnel with:
# curl https://gist.githubusercontent.com/benfasoli/543c14c90a4909e0305180bd8423969e/raw/ > /usr/local/bin/tunnel && chmod +x /usr/local/bin/tunnel
TARGET=$1
if [ -z "$TARGET" ]; then
echo -n "SSH target (e.g. name@server.com): "
read TARGET
fi
@benfasoli
benfasoli / map_spatial_average.r
Created March 26, 2021 15:34
Make interactive map by spatially averaging high resolution observations
#!/usr/bin/env Rscript
options(dplyr.summarise.inform = F)
library(data.table)
library(htmlwidgets)
library(leaflet)
library(tidyverse)
@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
library(ggplot2)
library(metR)
time <- Sys.time()
n <- 50
data <- data.frame(
time = seq(time - 3600, time, length.out = n),
direction = seq(0, 359, length.out = n),
magnitude = 5 * sin(seq(0, 2 * pi, length.out = n))
)
@benfasoli
benfasoli / pandas_read_csv_from_string.py
Last active October 28, 2020 23:42
Example of `io.StringIO` to pass text to `pd.read_csv(text)`
#!/usr/bin/env python3
import io
import pandas as pd
api_response_csv = """
x, y, z
1, 2, 3
@benfasoli
benfasoli / scluster
Last active October 20, 2020 19:58
CHPC SLURM convenience utilities
#!/bin/bash
set -e
if [[ "$HOSTNAME" == "notchpeak"* ]]; then
CLUSTER="np"
elif [[ "$HOSTNAME" == "kingspeak"* ]]; then
CLUSTER="kp"
else
echo "Unable to determine cluster from hostname: $HOSTNAME" > /dev/stderr
exit 1
@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 / run_test.r
Created October 9, 2020 19:43
Test exit handling of forked processes managed by R's parallel library
#!/usr/bin/env Rscript
# Ben Fasoli | benfasoli@gmail.com
# Return number of R processes currently running for active user
get_process_count <- function(x = NA) {
as.integer(system('pgrep -U $USER R | wc -l', intern = T))
}
N_FORKS <- 2