Skip to content

Instantly share code, notes, and snippets.

View scottstanie's full-sized avatar

Scott Staniewicz scottstanie

View GitHub Profile
@scottstanie
scottstanie / fit_poly_surface_2d.py
Created September 5, 2018 20:42
Fitting higher order 2D surface polynomials to data
import numpy as np
import itertools
def _xy_powers(order):
"""Get powers of an x-y polynomial of certain order
Order = 2 will have (m, n) for x^m * y^n with m+n <= order
Example:
>>> _xy_powers(2)
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (2, 0)]
name: mapping
channels:
- conda-forge
- defaults
dependencies:
- bokeh
- bottleneck
- cartopy
- cftime
- click
@scottstanie
scottstanie / aliasing_waves.py
Last active July 25, 2021 23:01
Animated aliasing waves
import numpy as np
import matplotlib.pyplot as plt
# Knobs to twiddle:
# 1. sine frequencies: `f1`, `f2`
# 2. spacing between vectical bars: `rect_spacing`
# (You can also change `rect_width`, but the frequency/spacing are more interesting)
f1 = 7.5 # Twiddle with the red sine frequency!
f2 = 7.0 # Slightly different frequency
@scottstanie
scottstanie / plot_zebra.py
Created April 20, 2022 20:35
Add a zebra (nautical) frame to matplotlib cartopy plot
import itertools
import matplotlib.patheffects as pe
import numpy as np
def add_zebra_frame(ax, lw=2, crs="pcarree", zorder=None):
ax.spines["geo"].set_visible(False)
left, right, bot, top = ax.get_extent()
# Alternate black and white line segments
@scottstanie
scottstanie / eager_loader.py
Last active December 1, 2022 18:24
Dummy implementation of a background loading thread which queues up chunks of data to be processed
import time
from queue import Empty, Queue
from random import random
from threading import Event, Thread
def _get_data(j, load_time=0.5):
"""Simulate data loading taking some amount of time."""
time.sleep(load_time)
# adding j just to differentiate the data
@scottstanie
scottstanie / demo_shared_multiproc.py
Created December 1, 2022 19:15
Demonstration of new multiprocessing.shared_memory module
import multiprocessing as mp
import time
from multiprocessing.shared_memory import SharedMemory
import numpy as np
SHAPE = (10_000_000,)
DTYPE = np.dtype("float32")
@scottstanie
scottstanie / h5explorer.py
Last active March 16, 2023 17:49
Interactively browsing an HDF5 in Jupyter
import h5py
class HDF5Explorer:
"""Class which maps an HDF5 file and allows tab-completion to explore datasets."""
def __init__(self, hdf5_filepath: str, load_less_than: float = 1e3):
self.hdf5_filepath = hdf5_filepath
self._hf = h5py.File(hdf5_filepath, "r")
self._root_group = HDF5GroupExplorer(
self._hf["/"], load_less_than=load_less_than
@scottstanie
scottstanie / baselines.py
Created April 15, 2023 01:57
isce3 CSLC baseline computation
import datetime
from pyproj import CRS, Transformer
import h5py
import isce3
import numpy as np
from dolphin._types import Filename
from __future__ import annotations
import datetime
from typing import Optional, Any
import h5netcdf
import h5py
import numpy as np
import pyproj
@scottstanie
scottstanie / gdal_reader.py
Last active April 26, 2023 21:59
Chunked Dask Array from a list of GDAL-readable files
from dolphin.io import load_gdal, get_raster_xysize, get_raster_nodata, get_raster_dtype, get_raster_chunk_size
class GDALReader:
# https://docs.dask.org/en/stable/generated/dask.array.from_array.html
def __init__(self, filename, masked=True):
self.filename = filename
self._shape = get_raster_xysize(filename)[::-1]
self._nodata = get_raster_nodata(filename)
self._masked = masked
self._dtype = get_raster_dtype(filename)