Skip to content

Instantly share code, notes, and snippets.

@mx-moth
mx-moth / README.md
Last active January 17, 2024 04:22
Simulated true colour plots

Simulated true colour plots

Use reflectance data from datasets to generate simulated true colour plots. This script needs emsarray, matplotlib and scipy installed.

true_colour

@mx-moth
mx-moth / extract-point.py
Created November 13, 2023 23:30
Extract a point with emsarray
import emsarray
from matplotlib import pyplot as plt
import numpy
import shapely
# Open a dataset
# This dataset starts with four dimensions: time, depth, and two surface dimensions
dataset = emsarray.open_dataset("https://dapds00.nci.org.au/thredds/dodsC/fx3/GBR1_H2p0_B3p2_Cfur_Dnrt.ncml")
# We can speed up future operations by limiting the timesteps
@mx-moth
mx-moth / area_average.py
Created September 14, 2023 00:47
Find mean value in an area using emsarray
import emsarray
import pandas
import shapely
# Open the dataset
gbr4 = emsarray.open_dataset('https://dapds00.nci.org.au/thredds/dodsC/fx3/model_data/gbr4_bgc_GBR4_H2p0_B2p0_Chyd_Dcrt.ncml')
# Define the area of interest
point = shapely.Point(152.384000, -22.672241)
circle = shapely.buffer(point, 0.1)
@mx-moth
mx-moth / etag_cache.py
Created October 2, 2018 05:18
A nice decorator for Flask views that supports ETag and Last-Modified headers, responding with a 304 Not Modified where possible
import logging
from functools import wraps
from flask import Response, make_response, request
from werkzeug.http import parse_date
def check_empty_iterator(iterator, message="Iterator was not empty"):
try:
next(iterator)
@mx-moth
mx-moth / filelock.py
Last active August 2, 2018 01:36
Python multiprocess FileLock class using fcntl.flock()
import fcntl
import os
from contextlib import contextmanager
MODE_NAMES = {
fcntl.LOCK_SH: 'shared',
fcntl.LOCK_EX: 'exclusive',
}
@mx-moth
mx-moth / chunksof.py
Last active August 12, 2021 21:56
Split a generator up in to chunks of size `n`, without walking the list or keeping items in memory needlessly
from itertools import chain, islice
class splitter:
"""Helper class for splitat."""
def __init__(self, iterable, count):
self.iterator = iter(iterable)
self.count = count
self.queue = []
@mx-moth
mx-moth / next_in_order.py
Last active March 21, 2018 23:26
Get the next/previous items in a queryset in relation to a reference object.
"""
Get the next/previous items in a queryset in relation to a reference object.
"""
from django.db.models import Q
def next_in_order(queryset, reference):
"""
Get the next items in a QuerySet according to its ordering, in relation to
@mx-moth
mx-moth / test_data_helpers.py
Created February 6, 2018 03:19
Some helpers for making POST data for Wagtail / Django form tests
def _nested_form_data(data):
if isinstance(data, dict):
items = data.items()
elif isinstance(data, list):
items = enumerate(data)
for key, value in items:
key = str(key)
if isinstance(value, (dict, list)):
for child_keys, child_value in _nested_form_data(value):
@mx-moth
mx-moth / comp.py
Last active June 28, 2017 07:32
Automatic partially-applied binary operators ala Haskell, for Python
class Comparator():
"""Support 'partial binding' of built in operators."""
def __call__(self, a):
"""For support of nested PendingOperations, comparator(a) == a"""
return a
# A bunch of boring operators
def __lt__(self, b):
return PendingOperation(lambda a: self(a) < b)
from django.core.exceptions import ValidationError
from django import forms
class UsernameValidatorForm(forms.Form):
username = forms.CharField()
def clean_username(self):
username = self.cleaned_data['username']