Skip to content

Instantly share code, notes, and snippets.

View phargogh's full-sized avatar

James Douglass phargogh

View GitHub Profile
@phargogh
phargogh / sed-manpage-to-epub.sh
Created April 29, 2021 20:52
Convert the sed manpage to EPUB
mandoc -Tmarkdown /usr/share/man/man1/sed.1 | pandoc -f markdown -t epub --metadata title="Man 1 Sed" > man1sed.epub
@phargogh
phargogh / pygeoprocessing-average.py
Created April 19, 2021 22:14
Using pygeoprocessing's convolve_2d as an averaging function
import os
import pygeoprocessing
import numpy
from osgeo import gdal, osr
_PROJECTION_SRS = osr.SpatialReference()
_PROJECTION_SRS.ImportFromEPSG(26910) # UTM Zone 10N
PROJECTION_WKT = _PROJECTION_SRS.ExportToWkt()
# taken from https://spatialreference.org/ref/epsg/26910/
ORIGIN = (224215.8977, 3810589.9220)
@phargogh
phargogh / numpy-array-filling-test.py
Created April 19, 2021 15:21
Timing various numpy array creation routines
import timeit
SETUP = 'import numpy'
SHAPE = 10000
ZEROS = (
'a=numpy.zeros(10000, dtype={dtype}); '
'b=numpy.zeros(10000, dtype={dtype}); x=a+b')
ONES = (
'a=numpy.zeros(10000, dtype={dtype}); '
'b=numpy.zeros(10000, dtype={dtype}); x=a+b')
@phargogh
phargogh / average-comparison.py
Created April 14, 2021 04:30
Comparing two approaches to raster averaging, based off the task/stormwater branch, rev b8974c7286d5ebaf68f4596ffb264132e5b5a04e
import time
import os
from natcap.invest import stormwater
import pygeoprocessing
from osgeo import gdal, osr
import numpy
import numpy.random
@phargogh
phargogh / extract_model_inputs.py
Created March 26, 2021 15:56
A script to extract ARGS_SPEC inputs information and write it to a directory, one CSV per model.
import json
import sys
import os
import importlib
import pkgutil
import natcap.invest
module_stack = list(pkgutil.iter_modules(
natcap.invest.__path__, natcap.invest.__name__ + '.'))
@phargogh
phargogh / formati18n.vim
Last active March 11, 2021 04:13
Simple vim script for fixing up ARGS_SPEC for gettext translation.
" Example usage:
" Launch a new vim session by (bash) $ find src -name "*py" | xargs grep -l ARGS_SPEC\ = | xargs vim
" argdo formati18n.vim
execute '%s/"about": (/"about": _(/g'
execute '%s/"name": "\(\p\+\)"/"name": _("\1")/g'
execute '%s/"model_name": "\(\p\+\)"/"model_name": _("\1")/g'
execute "normal! 3Giimport gettext\r\<Esc>"
" The & looks to add the original matched pattern.
@phargogh
phargogh / detect-impervious.py
Last active March 9, 2021 01:02
Tracer code to do a neighborhood search within a search radius using a convolution and array masking.
import numpy
import scipy.signal
# 1's represent pixels that are an impervious landcover.
# Derived from the landcover classification.
impervious_mask = numpy.array([
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
@phargogh
phargogh / requirements.txt
Created February 20, 2021 20:40
Experiment: lint RST with sphinx.ext.napoleon parsing
sphinx
rstcheck
taskgraph
@phargogh
phargogh / evaluate-contrived.py
Last active February 13, 2021 07:56
Experiment: extract and evaluate latex from rst
import sympy.parsing.latex
CARBON_EQ_21 = r"V\frac{sequest_x}{yr\_fut-yr\_cur}\sum^{yr\_fut-yr\_cur-1}_{t=0}\frac{1}{\left(1+\frac{r}{100}\right)^t\left(1+\frac{c}{100}\right)^t}"
CARBON_EQ_21_SIMPLE = r"V\frac{s}{fut-cur}\sum^{fut-cur-1}_{t=0}\frac{1}{\left(1+\frac{r}{100}\right)^t\left(1+\frac{c}{100}\right)^t}"
def replace(string):
return string.replace(r'\_', r'\textunderscore')
@phargogh
phargogh / README.md
Last active February 5, 2021 21:04
Use cases for numpy array indexing with slice(None)

Example of indexing with slice(None)

Setup

Just a standard python install should be fine, so long as you have numpy available.

Alternatively, a local conda environment would work well for this: