Skip to content

Instantly share code, notes, and snippets.

View lukegre's full-sized avatar
🐍

Luke Gregor lukegre

🐍
View GitHub Profile
@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.
@lukegre
lukegre / time_series_GP_gap_filler.py
Created May 19, 2017 12:48
Fill gaps in a time series using Gaussian Processes with the option of adding realistically scaled noise
#!/usr/bin/env python
"""
A short script that is used to fill gaps in a time series (gap_filler_with_noise).
The method uses gaussian processes (aka Kriging) to get the trend (calls gaussian_smoother).
I recommend that you play around with the theta0 value to find the correct scale for the trend.
Noise is added to the estimated trend by assessing the noise around the trend.
Diagnostic plots can be created with this script.
@lukegre
lukegre / alkalinity_Lee2006.py
Created May 19, 2017 07:46
Calculate total alkalinity according to Lee et al. (2006)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This script is written to calculate total alkalinity according to Lee et al. (2006).
Please see function help for more details.
Please acknowledge use.
"""
__author__ = "Luke Gregor"
@lukegre
lukegre / regex_sublime_grammar.tex
Created March 11, 2017 21:27
This file contains little "find all" snippets that are useful to find grammar mistakes in a document using regex in sublime
\b(\w+)\s+\1\b % repeated words
^[A-Za-z].*[A-Za-Z0-9] % lines that do not end with a full stop