Skip to content

Instantly share code, notes, and snippets.

View markusritschel's full-sized avatar

Markus Ritschel markusritschel

  • Doctoral candidate at the University of Hamburg and Max-Planck-Institute for Meteorology
  • Hamburg
View GitHub Profile
@markusritschel
markusritschel / map_plots.py
Last active November 15, 2023 23:58
Accessor for cartopy.GeoAxes objects to simplify adding features to (stereographic) map plots
from abc import ABC, abstractmethod
from matplotlib import pyplot as plt, path as mpath
import functools
import cartopy
import cartopy.crs
import cartopy.mpl.geoaxes
import numpy as np
def register_geoaxes_accessor(accessor_name):
@markusritschel
markusritschel / gen_cmip6_model_colors.py
Created December 10, 2021 16:45
Generates a dictionary with the official CMIP6 model colors
import pandas as pd
url = "https://github.com/IPCC-WG1/colormaps/blob/master/CMIP6_color.xlsx?raw=true"
df = pd.read_excel(url, index_col=[0])
cmip6_colors = {i:k.values for (i,k) in df.iterrows()}
# or as one-liner
# cmip6_colors = {i:k.values for (i,k) in pd.read_excel("https://github.com/IPCC-WG1/colormaps/blob/master/CMIP6_color.xlsx?raw=true", index_col=[0]).iterrows()}
@markusritschel
markusritschel / pdf_remove_annotations.sh
Created April 1, 2021 20:44
Remove all annotations from a PDF
pdftk in.pdf output - uncompress | sed '/^\/Annots/d' | pdftk - output out.pdf compress
@markusritschel
markusritschel / oh-my-zsh_deactivate-git-status.sh
Created August 7, 2020 10:54
Deactivate the git-status function for oh-my-zsh to avoid slow-down on huge git repositories
# following https://stackoverflow.com/a/25864063/5925453
git config --add oh-my-zsh.hide-status 1
git config --add oh-my-zsh.hide-dirty 1
@markusritschel
markusritschel / compare_conda_envs.sh
Created July 3, 2020 16:49
Compare two conda environments
conda list -n env1 --export > env1-packages.txt
conda list -n env2 --export > env2-packages.txt
diff env1-packages.txt env2-packages.txt
@markusritschel
markusritschel / replace_str_in_multiple_files.sh
Created June 26, 2020 10:23
Find all occurences of a string in multiple files and replace them with another string
find . -name *.txt -type f -exec sed -i 's/string-to-replace/replacement-string/g' {} \;
@markusritschel
markusritschel / canonical_path.sh
Created June 23, 2020 14:49
print the canonical path (true path) of a linked directory
canonical_path() {
perl -mCwd -le ' print Cwd::abs_path $ARGV[0] ' -- "$1"
}
@markusritschel
markusritschel / get_memory_size.py
Created June 22, 2020 14:03
Retrieve the total size of available physical memory
from psutil import virtual_memory
mem = virtual_memory()
print(mem.total) # in bytes
print(mem.total / 1024**3) # in giga bytes
@markusritschel
markusritschel / find_intervals.py
Created May 14, 2020 10:57
A simple way to find all the breaks in the time stamp, i.e., changes in consecutive times longer than some fixed threshold
import xarray as xr
ds = xr.open_dataset('data.nc')
# find temporal breaks that are larger than a given threshold
thrsh = 300 # threshold in seconds
interval_ends = ds.time.diff('time').dt.seconds > thrsh # gives a boolean array with True at the end of each interval longer than the threshold
# plot the intervals
import matplotlib.pyplot as plt
@markusritschel
markusritschel / gist:b090faca4ad8808e63b492e404b7abc9
Last active July 3, 2020 16:17
restore anaconda environments
# according to https://sriramjaju.github.io/2018-05-30-2-minute-recipe-how-to-rollback-your-conda-environment/
conda list --revisions # List the history of each change to the current environment
conda install --revision 2 # Restore environment to a previous revision