Skip to content

Instantly share code, notes, and snippets.

View kaedonkers's full-sized avatar

Kevin Donkers kaedonkers

View GitHub Profile
@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.
@kaedonkers
kaedonkers / rename_cubelist.py
Last active March 17, 2020 14:03
Rename cubes and coords in cubelist with unique names
import iris
import copy
# Main rename_cubelist() function
def rename_cubelist(cubelist, cubenames=None, new_coordnames=None, dryrun=False, verbose=True):
'''Rename cubes and coordinates in place where necessary'''
if cubenames==None:
cubenames = [cube.name() for cube in cubelist]
@kaedonkers
kaedonkers / ipykernel_conda_env.txt
Last active November 5, 2019 09:20
Add conda environment to Jupyter kernel list
# Courtesy of @tam203
conda create --name my-new-env -y
conda activate my-new-env
# Install ipykernel to use environment with Jupytr Lab.
# Add any other packages here (or you can install more later).
conda install ipykernel -y
# Install the kernel to make it avaliable to Jupyter Lab