Skip to content

Instantly share code, notes, and snippets.

@JohannesBuchner
JohannesBuchner / fitscathealth.py
Created April 29, 2024 11:26
Health check for a FITS catalog: should have column descriptions, units and should not fail to load with astropy.
import warnings
import sys
import astropy.io.fits as pyfits
from astropy.table import Table
print("opening FITS file ...")
f = pyfits.open(sys.argv[1])
print("extensions: ", [e.name for e in f])
@JohannesBuchner
JohannesBuchner / hdi.py
Last active March 19, 2024 17:23
Highest Density Interval from posterior samples
from getdist.mcsamples import MCSamples
import getdist.chains
def highest_density_interval_from_samples(xsamples, xlo=None, xhi=None, probability_level=0.68):
"""
Compute the highest density interval (HDI) from posterior samples.
Parameters
----------
xsamples : array_like
@JohannesBuchner
JohannesBuchner / rsync_on_change.sh
Created December 15, 2015 16:59
rsync loop that updates remote directory when local directory changes (using inotify)
while true
do
rsync -avz ./ user@host:remote/directory/
inotifywait -r ./
done
@JohannesBuchner
JohannesBuchner / sbpl.py
Created September 5, 2023 10:48
Smoothly bending power law function of Ryde+98. The width of the bend can be controlled.
def sbpl(x, norm, lam1, lam2, x0, xbrk, Lambda):
"""Smoothly bending powerlaw
Parameterization from Ryde99
https://ui.adsabs.harvard.edu/abs/1999ApL%26C..39..281R/abstract
Parameters
----------
x: array of independent variable
xmax: only consider x values up to this value
@JohannesBuchner
JohannesBuchner / convert_setup_py_to_pyproject_toml.py
Created March 17, 2022 16:42
Convert setup.py to pyproject.toml (WIP)
# Help create a pyproject.toml from a setup.py file
#
# USAGE:
# 1)
# replace "from [a-z.]* import setup" in your setup.py
# with "from convert_setup_py_to_pyproject_toml import setup"
# 2)
# run the resulting script with python, with this script in the PYTHONPATH
#
# The above can be achieved on Linux, for example, with:
@JohannesBuchner
JohannesBuchner / rsyncprogress.py
Last active June 18, 2023 18:51
Progress bar for rsync
"""
Progress bar for rsync
========================
Shows file progress and total progress as a progress bar.
Usage
---------
Run rsync with -P and pipe into this program. Example::
@JohannesBuchner
JohannesBuchner / colorbar.py
Created April 19, 2023 09:31
Manual colorbar with range, shown as a small inset with label
import matplotlib as mpl
import matplotlib.pyplot as plt
# make a colormap that can be used to look up colors
norm = mpl.colors.Normalize(vmin=0, vmax=3)
zcmap = plt.cm.ScalarMappable(norm=norm, cmap=plt.cm.viridis)
# use it to plot stuff
plt.errorbar(
x=1, xerr=0.1, y=2, yerr=0.2,
@JohannesBuchner
JohannesBuchner / stripmovies.py
Created April 4, 2023 10:11
Parses ODP libreoffice impress files in python, removing movies and animations.
#!/usr/bin/env python3
#
# SYNOPSIS: stripgif.py input.odp output.odp
#
#
import os
import sys
import zipfile
import lxml.etree as ET
@JohannesBuchner
JohannesBuchner / cmdcache.py
Created March 1, 2021 10:26
Cache/Memoize any command line program. Keeps stdout, stderr and exit code, env aware.
import sys, os
import joblib
import subprocess
mem = joblib.Memory('.', verbose=False)
@mem.cache
def run_cmd(args, env):
process = subprocess.run(args, capture_output=True, text=True)
return process.stdout, process.stderr, process.returncode
@JohannesBuchner
JohannesBuchner / procmanage.py
Last active March 26, 2023 18:28
Manage a group of processes, keeping only N running at a time.
#!/usr/bin/env python3
"""
Manage a group of processes, keeping only N running at a time.
Usage:
procmanage.py N STRING
Arguments:
N An integer representing the number of processes to send the CONT signal to.
STRING A string representing the full name to match against.