Skip to content

Instantly share code, notes, and snippets.

View robbibt's full-sized avatar
🛰️

Robbi Bishop-Taylor robbibt

🛰️
View GitHub Profile
@robbibt
robbibt / dcload_inline_config.py
Last active September 24, 2019 07:03
Specify custom dc.load config details in-line inside a notebook
# Code via David Gavin
import datacube
# Connect to standard database for Sentinel 2 ARD
dc = datacube.Datacube()
# Set up a custom config to access s2tsmask database
config = {'db_hostname': 'test.test.org.au',
'db_port': 1234,
@robbibt
robbibt / matplotlib_hillshading.py
Last active July 29, 2021 08:15
Applying a hillshade to an array using Matplotlib
# Code via Alex Leith
from matplotlib.colors import LightSource
import matplotlib.pyplot as plt
# Sample elevation array
elevation = data_cube.elevation.isel(time=0).values
# Create hillsahde based on the array
ls = LightSource(azdeg=315, altdeg=45)
@robbibt
robbibt / facet_plot_seaborn.py
Created October 8, 2019 01:01
Creating a simple faceted plot using Seaborn
import seaborn as sns
import pandas as pd
# Create test data
pard_std_df = pd.DataFrame(data=[['Burdekin', 'Midshelf', 0.6, 0.5, 0.4],
['Burdekin', 'Offshore', 0.3, 0.5, 0.8],
['Burdekin', 'Coastal', 0.2, 0.4, 0.9],
['Fitzroy', 'Midshelf', 0.6, 0.5, 0.4],
['Fitzroy', 'Offshore', 0.3, 0.5, 0.8],
['Fitzroy', 'Coastal', 0.1, 0.2, 0.3]],
@robbibt
robbibt / rayshader_africa.R
Last active October 10, 2019 04:27
R script for making pseudo-topography 3D animations using Rayshader and Datacube data
library(rayshader)
library(magrittr)
library(raster)
library(lubridate)
library(dplyr)
smooth_sequence = function(from=0, to=1, cycles=2, n=180) {
#' Produces a smooth sequence of any length between two values
@robbibt
robbibt / wofs_stats.py
Created October 21, 2019 05:41
Compute valid WOfS statistics from WOFLs
# Source: https://bitbucket.org/kirill-ga/wofs-summary/src/0f1e4284085e2a65bd7a0b9fdb00e0f65c36f713/kk/wofs_summary/_wofs_stats.py?utm_term=file&utm_source=bb-slack&utm_medium=referral-external&atlOrigin=eyJpIjoiMWRiZjlmZjhkYmE3NDg0Mzk3NWI3ODZhZjczNGQyODQiLCJwIjoiYmItY2hhdHMtaW50ZWdyYXRpb24ifQ#lines-114:136
def wofs_stats(xx):
attrs = {'crs': xx.crs}
xx_wet = make_mask(xx.water, wet=True).sum(dim='time', dtype='int16')
xx_dry = make_mask(xx.water, dry=True).sum(dim='time', dtype='int16')
xx_clear = xx_wet + xx_dry
xx_freq = xx_wet.astype('float32')/xx_clear
@robbibt
robbibt / check_product_metadata.py
Created October 30, 2019 05:41
Get a list of available product metadata that can be filtered on load
ds, = dc.find_datasets(product='ga_ls5t_ard_3', limit=1)
dir(ds.metadata)
@robbibt
robbibt / import_multiple_geotiffs.py
Last active November 14, 2019 23:51
Import multiple geoTIFFs to xarray
def time_index_from_filenames(filenames, string_slice=slice(0, 10)):
'''
Helper function to generate a Pandas datetimeindex object
from dates contained in a file path string
'''
date_strings = [os.path.basename(i)[string_slice] for i in filenames]
return pd.to_datetime(date_strings)
@robbibt
robbibt / from_xr_to_dc.py
Created December 4, 2019 06:15
Convert plain xarray to datacube xarray with geobox
# Code from Kirill Kouzoubov, Digital Earth Australia
import xarray as xr
import datacube
def from_xr_to_dc(xx):
crs = xx.data_vars.get('crs')
if crs is None:
print('no crs var')
return xx
crs = crs.attrs.get('crs_wkt', None)
@robbibt
robbibt / DEA_hotspots_download.R
Last active February 13, 2020 04:25
Downloading DEA Hotspots data via WFS in R
# Set up parameters to load Hotspots data
from_date = '2020-01-27'
to_date = '2020-02-12'
y_max = -35.38
x_min = 148.68
y_min = -36.02
x_max = 149.32
min_confidence = 0 # minimum confidence value of hotspots to load from WFS
max_features = 900000 # maximum features to load from WFS
@robbibt
robbibt / product_metadata.py
Last active August 18, 2025 08:08
Load product metadata as an xr.DataArray that matches dc.load data
# Code adapted from original by Andrew Hicks
import numpy as np
import datacube
from datacube.api.query import query_group_by
from datacube.model.utils import xr_apply
dc = datacube.Datacube(app='Metadata')
# Spatiotemporal query
query = dict(product='ga_ls8c_ard_3',