Skip to content

Instantly share code, notes, and snippets.

View phargogh's full-sized avatar

James Douglass phargogh

View GitHub Profile
@phargogh
phargogh / README.md
Last active May 10, 2024 23:07
Clang/ARM64 compilation issue

Compilation issue with clang 14 on ARM64

To reproduce:

  • Use an ARM64 computer (tested on M1 mac and on Raspberry Pi model 4B)

    • On Raspberry pi, I used Raspbian:bookworm
    • On M1 mac, I used docker run --rm -ti -v $(pwd):/demo -w /demo debian:bookworm /bin/bash (which shares the local directory into the container, assumes you are running this from the cloned gist)
  • On your computer of choice, run apt update && apt install -y build-essential clang-13 clang

  • Observe gcc produces correct results: gcc ./min-repro-sample.c && ./a.out

@phargogh
phargogh / README.md
Created December 12, 2022 22:47
Timing numpy array indexing performance with `False` vs `numpy.zeros`

Is it faster to return None as a mask?

While reading through the InVEST utils source code, I noticed that our nodata masking function returns a boolean array of zeros rather than just False, and it made me wonder if the memory allocation for this array was slower than simply returning False and letting the numpy array broadcasting handle the distribution of that single value across all of the comparison array values.

In my tests here (see test-nodata-timings.py), the runtime is dominated by random array generation rather than indexing, but there does appear to be a

@phargogh
phargogh / install_zsh_on_sherlock.sh
Last active December 6, 2022 20:33 — forked from mgbckr/install_zsh_on_sherlock.sh
Compiling and installing Zsh without root privileges on Stanford's Sherlock (https://sherlock.stanford.edu) for use in tmux
# # Install Zsh on Sherlock
# Installs Zsh with Oh-My-Zsh without root privileges
# on Stanford's Sherlock (https://sherlock.stanford.edu) for use in tmux
#
# ## Instructions
# 1) bash install_zsh.sh
# 2) edit .zshrc (add the path to your Zsh binary to the PATH variable, etc.)
# 3) add `set-option -g default-shell <path to zsh>/bin/zsh` to `~/.tmux.conf`
# 4) also see comments for potential further notes
#
@phargogh
phargogh / maybe_translate.sh
Created October 28, 2022 22:36
Bash script: cat an image to stdout and ask whether the image contains text to be translated.
#!/bin/bash
# This script runs within iterm2 to print images to the commandline and
# ask whether there's text that should be translated.
#
# Developed to run within the context of the InVEST user's guide,
# executing this from within the source/ directory.
NO_TRANSLATE_FILE=images_that_should_not_be_translated.txt
YES_TRANSLATE_FILE=images_that_SHOULD_be_translated.txt
@phargogh
phargogh / gtiff_with_metadata.py
Created October 27, 2022 01:48
Creating a GTiff with embedded ISO metadata
import numpy
from osgeo import gdal
from osgeo import osr
# Taken from Examples section of https://wiki.gcube-system.org/gcube/ISO_19115:2003/19139
METADATA = """\
<fileIdentifier>
<gco:CharacterString>33462e78-e5ab-11c3-737d-b3a61366d028</gco:CharacterString>
</fileIdentifier>
<language>
@phargogh
phargogh / README.md
Last active August 2, 2022 21:30
Can HRA Reclassification Percentages be Derived from `pygeoprocessing.zonal_statistics` outputs?

Can HRA Reclassification Percentages be Derived from pygeoprocessing.zonal_statistics outputs?

Background

Among its outputs, HRA produces a raster where the calculated floating-point risk score is reclassified into the following classifications:

  1. 0 representing no risk
  2. 1 representing low risk
  3. 2 representing medium risk
@phargogh
phargogh / kernel-by-pixel-area.py
Created May 13, 2022 19:56
Example of calculating an all-ones kernel given the pixel area (assuming pixel width/height in degrees)
import math
import sys
import numpy
from osgeo import gdal
def make_ones_kernel(radius_in_pixels):
side_length = radius_in_pixels * 2
if side_length % 2 == 0:
@phargogh
phargogh / convert.py
Created May 13, 2022 19:23
Aligning a global raster, including pixel area-aware conversion to/from population pixel counts
import numpy
import pygeoprocessing
from osgeo import gdal
FLOAT32_NODATA = float(numpy.finfo(numpy.float32).min)
def _convert_to_from_density(source_raster_path, target_raster_path,
direction='to_density'):
"""Convert a raster to/from counts/pixel and counts/unit area.
@phargogh
phargogh / convert_taudem_d8_flow_dir_to_pygeoprocessing.py
Created May 6, 2022 19:01
Pygeoprocessing raster_calculator call to convert from a TauDEM D8 flow direction to pygeoprocessing D8 flow direction.
import numpy
import pygeoprocessing
from osgeo import gdal
def convert_taudem_flow_dir_to_pygeoprocessing(
taudem_flow_dir_raster_path, target_flow_dir_raster_path):
source_nodata = pygeoprocessing.get_raster_info(
taudem_flow_dir_raster_path)['nodata'][0]
dest_nodata = 128 # Matches pygeoprocessing-produced flow dir nodata.
@phargogh
phargogh / routing-example.py
Created April 12, 2022 22:42
Example of weighted flow accumulation for NCI NOXN work using pygeoprocessing.routing.
import logging
import os
import pygeoprocessing
import pygeoprocessing.routing
from osgeo import gdal
logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger(__name__)