Skip to content

Instantly share code, notes, and snippets.

View kaedonkers's full-sized avatar

Kevin Donkers kaedonkers

View GitHub Profile
@kaedonkers
kaedonkers / install_ipykernel_pixi_feature.sh
Last active March 6, 2025 14:26
Installing a `pixi` feature as a Jupyter `ipykernel`
# Installing environment as ipykernel:
export ENV_NAME=feature_name
pixi add --feature $ENV_NAME ipykernel
pixi run -e $ENV_NAME python -m ipykernel install --user --name pixi_$ENV_NAME --display-name "$ENV_NAME (pixi)"
@kaedonkers
kaedonkers / file_to_env.sh
Last active August 20, 2024 17:11
Export contents of file to environment variable
API_KEY=`cat ~/path/to/apikey_file` && export API_KEY
@kaedonkers
kaedonkers / git_clone_branch.sh
Created September 20, 2023 16:32
Git clone specific branch
# First time cloning repo
git clone --branch <branch-name> <url|ssh>
# If already cloned
git fetch
git switch <branch-name>
@kaedonkers
kaedonkers / conda_ipykernel.sh
Last active September 11, 2023 13:23
Install conda environment as Jupyter kernel using ipykernel
## Install conda environment as Jupyter kernel using ipykernel
source activate {env_name}
conda install ipykernel -y -q
ipython kernel install --user --name $CONDA_DEFAULT_ENV
@kaedonkers
kaedonkers / pandas_update_df.ipynb
Created August 18, 2023 14:37
Update entries in a Pandas DataFrame with entries in another Dataframe, without relying on compatible indices
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kaedonkers
kaedonkers / .npy file to .nc
Created September 30, 2022 11:38
Converting a .npy file to .nc using Pandas and Xarray
import numpy
import xarray
import pandas
fname = "data.npy"
arr = numpy.load(fname)
df = pandas.DataFrame({"lon":arr[:,0], "lat":arr[:,1], "data":arr[:,3]})
ds = df.set_index(["lat","lon"]).to_xarray()
ds.to_netcdf("data.nc")
@kaedonkers
kaedonkers / reverse_piecewise_linear_sigmoid.py.py
Created June 1, 2022 12:27
Reverse piecewise linear sigmoid function
import numpy as np
def reverse_piecewise_linear_sigmoid(self, x, upper, lower):
'''
Reverse piecewise linear sigmoid function to filter a value between two thresholds in a vectorised manner according to:
if x <= lower:
y = 1
else:
y = (upper-x)/(upper-lower)
@kaedonkers
kaedonkers / conda_kernels.py
Last active September 9, 2022 13:43
Python script to install all conda environments on a system as ipython kernels, making the conda envs available to Jupyter
#!/path/to/miniconda3/bin python3
'''
A commandline tool to install all available conda environments as ipython kernels
- Checks for which conda envs and ipython kernels are available
- Installs the conda envs not yet installed as ipython kernels (including ipykernel if missing)
- Uninstalls ipython kernels which no longer have a corresponding conda env
'''
import jupyter_client
import subprocess
@kaedonkers
kaedonkers / iris_regrid_ukv_to_osgb.py
Last active June 8, 2020 15:01
Minimal amount of code to regrid UKV data to OSGB grid, keeping native grid size of original UKV data. Based on Gist by @DPeterK https://gist.github.com/DPeterK/c5061f336a91a3ce9790c206a5459b4a
# (C) Crown Copyright, Met Office. All rights reserved. 2020
#
# Python code written for demonstrative purposes only.
# Authored by Kevin Donkers (@kaedonkers) and Peter Killick (@DPeterK)
# See https://gist.github.com/DPeterK/c5061f336a91a3ce9790c206a5459b4a
import iris
import numpy as np
import cartopy.crs as ccrs
@kaedonkers
kaedonkers / extract_regional_data_using_shapefile.ipynb
Created April 23, 2020 13:45
Extract regional data from gridded NetCDF files using geometries from a shapefil (Iris, Cartopy, ascend)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.