Skip to content

Instantly share code, notes, and snippets.

View lukegre's full-sized avatar
🐍

Luke Gregor lukegre

🐍
View GitHub Profile
@lukegre
lukegre / ipcc_cmaps.py
Last active September 5, 2024 08:21
Load IPCC cmaps (e.g., cmap="ipcc.prec_div"). A standalone script to load the IPCC continuous color maps listed in https://github.com/IPCC-WG1/colormaps/tree/master. Simply import this script to load the cmaps and access as cmap='ipcc.prec_div' for example.
def get_github_filesystem():
import fsspec
fs = fsspec.filesystem('github', org='IPCC-WG1', repo='colormaps')
return fs
@lukegre
lukegre / google_maps_viz.py
Last active July 3, 2024 07:54
Download google maps tiles for a given bbox (WSEN). Data is returned as a geolocated xarray.DataArray.
import numpy as np
import xarray as xr
import rioxarray as rxr
from loguru import logger
from pygeotile.tile import Tile
__package__ = __file__.split('/')[-1].split('.')[0]
logger.warning(
f"Note that you can only use `{__package__}` for non-commercial "
@lukegre
lukegre / google_maps_viz.py
Created July 3, 2024 07:52
Download google maps tiles for a given bbox (WSEN). Data is returned as a geolocated xarray.DataArray
import numpy as np
import xarray as xr
import rioxarray as rxr
from loguru import logger
from pygeotile.tile import Tile
__package__ = __file__.split('/')[-1].split('.')[0]
logger.warning(
f"Note that you can only use `{__package__}` for non-commercial "
@lukegre
lukegre / dataarray_maps.py
Created September 24, 2021 15:36
Create global maps using xarray accessors
"""
Contains a function to quickly plot xarray datasets on a map
Loading the script creates a method for xr.DataArrays that can be used as follows:
da.mean('time').map()
Defaults can also be changed by changing values in the rcMaps dictionary.
I haven't figured out how this can be changed in notebooks, but you can just
change these with the **kwargs argument.
@lukegre
lukegre / spco2_seasonal_cycle_analysis.py
Last active August 5, 2021 08:42
Perform a seasonal cycle decomposition and determine the driver of the trends
import xarray as xr
import numpy as np
import joblib
import os
# SEASONAL CYCLE FITTING #
def graven2013_seasonal_fit(da, n_jobs=24):
"""
Fits a seasonal cycle to data using cos and sin functions.
@lukegre
lukegre / gist_tools.py
Last active April 14, 2021 15:16
Quick place to store useful function decorators
def load_url_as_python_module(url, pkg_name=None, save_dir=None):
"""Imports a python file url as a package.
Downloads the file to local storage and imports into the global namespace.
Can be useful to quickly access gists and github files as functions.
Warning: this function could be VERY dangerous as a script could be
run on your machine without knowing what it does... be very careful
Args:
@lukegre
lukegre / simple_coarsen_example.ipynb
Created September 22, 2020 10:29
Difference between interpolation and averaging when coarsening model data.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@lukegre
lukegre / check_inputs.py
Created May 18, 2020 07:03
A few functions to help with functions that have limits for the inputs
def input_limits(return_bool=True, return_info=True, **kwargs):
"""
This is a function wrapper for functions that have defined input limits.
The user can define the valid limits of any of the inputs.
Parameters
----------
return_bool: array-bool
returns a boolean array of the original function where valid or not
return_info: array-str
@lukegre
lukegre / corona_daily_increase.ipynb
Created March 27, 2020 17:28
Plots daily increases of Covid-19 cases for a few countries using the John Hopkins Uni data
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@lukegre
lukegre / empirical_mode_decomposition.py
Last active December 17, 2018 18:58
Empirical mode decomposition (EMD) for 1D data with plotting function for output IMFs
import numpy as np
### Implementation of our EMD() function
def emd(data, stop_limit=0.001, spline_order=3):
"""
EMD as explained by Scott Cole (https://srcole.github.io/2016/01/18/emd/),
but has been modified by Luke Gregor (https://github.com/luke-gregor) to
automatically stop when there are no more IMFs in the dataset.
Additionally, the residual from the stopping point and the original dataset
is counted as the final IMF.