Skip to content

Instantly share code, notes, and snippets.

View scottstanie's full-sized avatar

Scott Staniewicz scottstanie

View GitHub Profile
@scottstanie
scottstanie / tau-uklg.py
Created July 8, 2024 14:34
Print a random chapter from Ursula Le Guin's Tao
import requests
import re
import random
from rich.console import Console
from rich.panel import Panel
from rich.text import Text
url = "https://raw.githubusercontent.com/nrrb/tao-te-ching/master/Ursula%20K%20Le%20Guin.md"
def get_random_chapter():
@scottstanie
scottstanie / boi.py
Last active January 23, 2024 22:33
Burst Overlap Interferometry with geocoded SLCs
from os import fspath
import numpy as np
from osgeo import gdal
from dolphin import io, stitching, utils
def compute_boi(ifg_file1: str, ifg_file2: str, looks: tuple[int, int] = (1, 1)) -> np.ndarray:
assert io.get_raster_crs(ifg_file1) == io.get_raster_crs(ifg_file2)
(left, bottom, right, top), nodata = stitching.get_combined_bounds_nodata(
ifg_file1, ifg_file2
@scottstanie
scottstanie / demo_cache_locality.py
Last active January 7, 2024 18:40
Show timing variations from poor data access patterns
#!/usr/bin/env python3
from numba import njit
@njit
def f(arr):
s = 0
i = 0
while i < len(arr):
s += arr[i]
@scottstanie
scottstanie / show_h5_sizes.py
Created December 4, 2023 21:03
Pretty print the size on disk + compression ratio of HDF5 datasets with h5py and rich
#!/usr/bin/env python
# requirements: h5py, rich
import sys
import h5py
from rich.console import Console
from rich.table import Table
def _get_object_metadata(obj) -> tuple[str, float, float] | None:
@scottstanie
scottstanie / setup_git_lfs.sh
Created October 19, 2023 20:07
Setup `git lfs` on either linux or Mac M1/M2 using the github release binaries
#/usr/bin/env bash
set -e
set -x
# Check if INSTALL_PREFIX is set; otherwise, error
if [ -z "$INSTALL_PREFIX" ]; then
echo "INSTALL_PREFIX is not set"
echo "Example usage: INSTALL_PREFIX='~/.local/bin' bash $0"
exit 1
fi
@scottstanie
scottstanie / open_stack.py
Created October 9, 2023 17:17
Open multiple displacement datasets as an `xarray` stack
import datetime
import re
from pathlib import Path
from typing import Union
import pandas as pd
import xarray as xr
Filename = Union[str, Path]
@scottstanie
scottstanie / tao.py
Last active November 19, 2023 00:30
Daily tao in your terminal
#!/usr/bin/env python
# pip install requests rich beautifulsoup4
import sys
import requests
from bs4 import BeautifulSoup
from rich.console import Console
from rich.panel import Panel
URL = "https://dailytao.org/"
@scottstanie
scottstanie / split.py
Created September 23, 2023 18:23
like `split`, but with better numerical naming
#!/usr/bin/env python
import argparse
from pathlib import Path
def split_file(filename: str | Path, lines_per_file: int, output_prefix: str):
"""Split a text file into multiple files.
Parameters
----------
@scottstanie
scottstanie / sentinel1_s3_urls.py
Last active September 19, 2023 23:15
Get the S3 bucket urls for Sentinel-1 granules stored on ASF
import requests
from concurrent.futures import ThreadPoolExecutor
def get_s3_direct_urls(
safe_names: list[str], max_workers: int = 5
) -> str | None:
"""Get the S3 urls for a list of SAFE granules."""
def _get_url(safe_name):
@scottstanie
scottstanie / compare_hdf5_binary.py
Created June 28, 2023 22:57
Compares HDF5 3d stack loading time to loading chunks of flat binary files
import subprocess
import time
from pathlib import Path
import h5py
import numpy as np
from dolphin import io
class GDALStackReader: