Skip to content

Instantly share code, notes, and snippets.

@adigitoleo
adigitoleo / mpl_colorwheel.py
Last active October 1, 2020 06:47
Plot colorwheel inset in matplotlib (Python).
"""Color wheel for matplotlib plots."""
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import inset_locator
def add_cwheel(ax_parent, rect, label, cmap=None, half=True, nticks=4):
"""Add color wheel to a matplotlib axes.
@adigitoleo
adigitoleo / gmt_tutorial_s1.jl
Created July 19, 2021 15:03
GMT.jl translation of the GMT tutorial (session 1)
"""GMT tutorial using GMT.jl
1. In the Julia shell, do `]add GMT`
2. Download GSHHG data: <http://www.soest.hawaii.edu/wessel/gshhg/>
3. Unpack GSHHG data to a predictable directory
4. Create the `.gmt` folder in your HOME directory
5. In the command line shell, run `gmt defaults -Ds > .gmt/gmt.conf`
6. Change the assignment of `DIR_GSHHG` to point to the directory from step 3
"""
"""Read columns of a delimited file into a namedtuple.
Supports column type parsers, e.g.
def parser(colname, data):
if colname == "foo":
return map(float, data)
return data
"""
from fractions import Fraction
def fspread(count, start, end=None, step=None, mode=1):
"""Generate evenly-spaced floats.
Yields a sequence of evenly-spaced numbers. Optional `mode` controls
endpoint behaviour. Requires the stdlib class `fraction.Fraction`. Source:
http://code.activestate.com/recipes/577881-equally-spaced-floats-part-2/
@adigitoleo
adigitoleo / pre-commit
Created February 22, 2022 00:11
Python black pre-commit hook
#!/bin/sh
# Copy or link this script into .git/hooks/
# It runs automatically in the project root directory (parent of .git/).
# Code formatting
if black --check src/pydrex; then
exit 0
else
black src/pydrex
git add -u
@adigitoleo
adigitoleo / utils.jl
Created March 16, 2022 13:28
Franklin.jl utils for reference lists
using TOML
using HTTP
"""
get_ref(key)
Get HTML-formatted reference text for the given referernce key.
The key must match either a key in the `_assets/refcache.toml`
or the `_assets/doilist.toml` file. The text will be formatted with
@adigitoleo
adigitoleo / bigint_modinv.jl
Created March 27, 2022 11:27
Arbitrary precision modular matrix inverse in Julia
using LinearAlgebra
function inverse(a::AbstractMatrix{BigInt}, modulus)
m, n = size(a)
if m - n != 0
error("cannot find modular inverse of non-square matrix")
end
det_inv = invmod(BigInt(det(a)), modulus)
if det_inv == 0
@adigitoleo
adigitoleo / Makefile
Created April 28, 2022 12:10
Makefile for LuaLaTeX documents with Biber for references
src := $(wildcard *.tex)
bib := $(wildcard *.bib)
all: $(src:%.tex=%.pdf)
%.pdf: $(src) out/%.bbl
@ 1>/dev/null lualatex --output-directory=out --interaction=batchmode --halt-on-error --file-line-error $< \
|| echo "LuaLaTeX run 2/3 failed, check log files in out/ for details"
@ lualatex --output-directory=out --halt-on-error --file-line-error $<|rg -v '/usr/share/'
@ mv out/$@ $@
@adigitoleo
adigitoleo / misc.jl
Last active December 14, 2022 09:14
Miscellaneous stuff for Julia
using Pkg
using TerminalPager
using UnicodePlots
# Reduce color space of most REPL components from 16 to 8.
# Line numbers is stacktraces are still unreadable in dark themes...
Base.text_colors[:white] = Base.text_colors[:yellow]
Base.text_colors[:light_cyan] = Base.text_colors[:cyan]
Base.text_colors[:light_red] = Base.text_colors[:red]
Base.text_colors[:light_magenta] = Base.text_colors[:magenta]
@adigitoleo
adigitoleo / mineral_list_IMA.py
Created July 27, 2022 02:26
Script to parse IMA mineral list into CSV file
import re
import csv
import pdfplumber
# Create a list of minerals as classified by the IMA.
# Get PDF from http://cnmnc.main.jp/
pdf = pdfplumber.open("IMA_MineralList_202207.pdf")