Skip to content

Instantly share code, notes, and snippets.

View cbur24's full-sized avatar

Chad Burton cbur24

View GitHub Profile
@cbur24
cbur24 / hex_codes_for_colour_ramp.py
Created January 21, 2022 03:25
Print hex codes for matplotlib colour ramps
from pylab import *
import numpy as np
n=17
cmap = cm.get_cmap('magma', n)
for i,j in zip(range(cmap.N), np.arange(0,0.425,0.025)):
rgba = cmap(i)
# rgb2hex accepts rgb or rgba
print("{'value': "+str(round(j,3))+", 'color': "+"'"+ matplotlib.colors.rgb2hex(rgba)+"'"+"},")
@cbur24
cbur24 / cloud_buffering.py
Last active March 2, 2021 02:48
Improve the Sentinel-2 cloud mask using morpholigical operations
import odc.algo
#Extract boolean mask
mask = odc.algo.enum_to_bool(ds.SCL,
categories=['cloud shadows', 'cloud medium probability',
'cloud high probability', 'thin cirrus'])
# Close mask to remove small holes in cloud, open mask to
# remove narrow false positive cloud, then dilate
mask = odc.algo.binary_closing(mask, 2)
@cbur24
cbur24 / xr_geomad_dask.py
Last active February 9, 2024 01:32
Same as "xr_geomad.py" but optimized for dask
import xarray as xr
import numpy as np
import dask
import hdstats
from odc.algo import randomize
from odc.algo._dask import reshape_yxbt
from odc.geo.xr import assign_crs
def xr_geomad_dask(ds, **kw):
"""
@cbur24
cbur24 / xr_geomad.py
Last active February 23, 2021 04:07
Calculate geomedian and TMADs on xarray, numpy, or dask arrays
import xarray as xr
import numpy as np
import dask
import hdstats
from odc.algo import randomize, reshape_for_geomedian
from odc.algo._dask import reshape_yxbt
from datacube.utils.geometry import assign_crs
def xr_geomad(ds, axis='time', where=None, **kw):
"""
@cbur24
cbur24 / mask_xarray.py
Last active May 27, 2021 03:01
Create xarray mask from vector
import xarray as xr
import geopandas as gpd
from dea_tools.spatial import xr_rasterize
#read shapefile
mask_vector = gpd.read_file('mask.shp')
#import xarray ds
ds = xr.open_dataset('testing.nc')
@cbur24
cbur24 / crop_mask_feature_layer.py
Last active April 19, 2021 04:43
Feature layer function for production run of eastern crop-mask
import datacube
import numpy as np
import xarray as xr
from odc.algo import xr_reproject
from pyproj import Proj, transform
from datacube.utils.geometry import assign_crs
from datacube.testutils.io import rio_slurp_xarray
from deafrica_tools.bandindices import calculate_indices
def common_ops(ds, era):
@cbur24
cbur24 / mask_dc_data.py
Created September 22, 2020 04:44
Looping through geopandas df, loading datacube data, masking with geometry
import sys
import datacube
import xarray as xr
import geopandas as gpd
from datacube.utils import geometry
sys.path.append('../Scripts')
from deafrica_datahandling import load_ard
from deafrica_spatialtools import xr_rasterize
@cbur24
cbur24 / predict_xr.py
Last active September 22, 2020 07:29
import xarray as xr
import numpy as np
import joblib
import dask.array as da
from datacube.utils.geometry import assign_crs
from dask_ml.wrappers import ParallelPostFit
def predict_xr(model,
input_xr,
chunk_size=None,
@cbur24
cbur24 / significance_test.py
Created August 10, 2020 03:30
Per-pixel, xarray significance tests
import numpy as np
import xarray as xr
from scipy import stats
def significance_tests(xarray_a, xarray_b, t_test=False, levene_test=False,
equal_variance = False, nan_policy= 'omit', mask_not_sig = False,
level_of_sig = 0.05, center='mean'):
"""
This function operates on a per-pixel basis and contains two types of significance tests:
@cbur24
cbur24 / plot_places.py
Created August 10, 2020 03:27
Plot the locations and names of towns/cities on a given geo-axes.
import cartopy.crs as ccrs
def plot_towns(ax, lats, lons, resolution='10m', transform=ccrs.PlateCarree(), zorder=3):
"""
This function will download the 'populated_places' shapefile from
NaturalEarth, trim the shapefile based on the limits of the provided
lat & long coords, and then plot the locations and names of the towns
on a given GeoAxes.