Skip to content

Instantly share code, notes, and snippets.

@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:
@scottstanie
scottstanie / compare_border_to_db.py
Created May 25, 2023 19:56
Compare the burst border read from XML metadata to the expected border in the burst database
#!/usr/bin/env python
import sys
from pathlib import Path
from compass.utils import helpers
from shapely import geometry
import numpy as np
import s1reader
@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)
from __future__ import annotations
import datetime
from typing import Optional, Any
import h5netcdf
import h5py
import numpy as np
import pyproj