Skip to content

Instantly share code, notes, and snippets.

View serazing's full-sized avatar
🎯
Focusing

Guillaume Sérazin serazing

🎯
Focusing
  • UNSW Climate Change Research Centre
  • Sydney, Australia
View GitHub Profile
@serazing
serazing / find_mld_with_numba.py
Created January 31, 2022 08:43
Finding Mixed Layer Depth with numba, dask and xarray
def mld_thres(phi, phi_step, ref_depth=10, dim='depth'):
# Get the depth vector
z = phi[dim]
# Find the index closest to the reference depth
k_ref = int((np.abs(z - ref_depth)).argmin())
from numba import guvectorize
@guvectorize(['void(float64[:], float64[:], intp, float64, float64[:])'],
'(n),(n),(),()->()', nopython=True)
import xarray as xr
def expfit(phi, dim):
def exp_func(z, phi_bottom, delta_phi, z0):
return phi_bottom - delta_phi * np.exp(- z / z0)
mean_phi_max = phi.max(dim).mean()
mean_phi_min = phi.min(dim).mean()
delta_phi = mean_phi_max - mean_phi_min
@serazing
serazing / xr_lowess.py
Last active December 27, 2023 11:41
Lowess for xarray
import numpy as np
import numba
import xarray as xr
@numba.jit(nopython=True)
def lowess_1d(y, x, alpha=2. / 3., it=10):
"""lowess(x, y, f=2./3., iter=3) -> yest
Lowess smoother: Robust locally weighted regression.
The lowess function fits a nonparametric regression curve to a scatterplot.
The arrays x and y contain an equal number of elements; each pair
import xarray as xr
import numpy as np
import numba
import pandas as pd
from obspy.geodetics import kilometers2degrees
@numba.jit(nopython=True, parallel=True)
def _interp_over_lon_lat(data_in, lon_in, lat_in, data_out, lon_out, lat_out, dlon, dlat):
"""
@serazing
serazing / bin_lat_lon.py
Created March 4, 2019 03:17
Bin data on a regular latitude and longitude grid
def bin_lat_lon(data, lon_min=0., lon_max=360., lon_res=1.,
lat_min=-80., lat_max=80, lat_res=1.):
"""
Bin unidimensional data onto a regular latidude and longitude grid
by computing the median value and the number of corresponding
observations
Paraneters
----------
data : xarray.DataArray
@serazing
serazing / mesoscale_filter.py
Created February 21, 2019 00:15
Some functions based on pyresample to filter along-track data based on a climatology of the internal deformation radius
import xarray as xr
import numpy as np
import numba
import pandas as pd
import pyresample
def interpolate_rossby_radius(Rd, lon, lat, interp_type='gaussian',
radius_of_influence=500e3, fill_value=None,
reduce_data=True, nprocs=1, neighbours=8):
lat_min, lat_max = np.min(lat), np.max(lat)
import dask.delayed as delayed
import xarray as xr
import numpy as np
@delayed(pure=True)
def lagged_difference(a, dim, lag, order=1):
return (a.shift(**{dim: -lag}) - a) ** order
@serazing
serazing / geometry.py
Created October 24, 2018 00:28
Geometry functions at the surface of the Earth
import xarray as xr
import numpy as np
import pandas as pd
EARTH_RADIUS = 6371 * 1e3
def latlon2yx(lat, lon):
"""
Convert latitude and longitude arrays to y and x arrays in m