Skip to content

Instantly share code, notes, and snippets.

View phargogh's full-sized avatar

James Douglass phargogh

View GitHub Profile
@phargogh
phargogh / conclusions.txt
Last active January 21, 2021 22:55
Timing pour points implementations
Conclusions:
* On the Colombia DEM (15030 x 20631 pixels) and the Gura DEM (1939 x 603 pixels) the
delineateit implementation reliably runs in 1/3 the time of the proposed
`_ManagedRaster`-based pour points implementation.
@phargogh
phargogh / cbc_emissions_math_Test.py
Created October 28, 2020 01:38
InVEST Coastal Blue Carbon's emissions math expermient
# Related to the discussion at https://community.naturalcapitalproject.org/t/coastal-blue-carbon-zero-net-c-sequestration-in-disturbed-blue-carbon-habitats/1398/9
# Note that this is intended to simply recreate the math used in the CBC model,
# which is just a Riemann Sum of the emissions equation. It would be far
# faster (and should be more numerically correct) to simply use the fundamental
# theorem of calculus and evaluate the function directly for each transition period.
# d = 0.0001997
d = 0.00020075
# found_emissions = 0.0000133733
@phargogh
phargogh / standard_factored_form.py
Created October 17, 2020 19:22
Factor an integer into standard factored form.
import time
import math
import collections
PRIMES = set((2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199))
def standard_factored_form(n):
print(f"Factoring {n}")
remainder = n
factors = collections.defaultdict(int)
@phargogh
phargogh / README.md
Last active September 16, 2020 00:42
The current version of InVEST, for testing

The three files here are for developing the InVEST download link auto-updating functionality.

The file invest-current_version.json is there in case it's easier to only update the download links.

The file invest-all-links.json is there in case it's easier to update all of the hyperlinks in one go.

The file invest-all-links-nid.json is the same as invest-all-links.json, just with the drupal Node ID included for easier reference.

@phargogh
phargogh / convert-repos.sh
Last active June 30, 2020 20:33
Convert bitbucket user repos and snippets to mercurial bundles
REPO_DIR=$(pwd)/repos
OUT_DIR=$(pwd)/bundles
mkdir -p $OUT_DIR $REPO_DIR
for repo in `cat repos.txt`
do
if echo $repo | grep ^ssh
then
PRIVATE='-private-'
else
@phargogh
phargogh / model_stats.py
Created June 3, 2020 22:47
InVEST model stats per model, with aliasing and separate stats for model runs and n study areas
# Usage: python model_stats.py <path to model stats GeoJSON
import sys
import collections
from osgeo import ogr
ALIASES = {
'natcap.invest.blue_carbon.blue_carbon_preprocessor': 'CBC-preprocessor',
'natcap.invest.coastal_blue_carbon.preprocessor': 'CBC-preprocessor',
'natcap.invest.coastal_blue_carbon.coastal_blue_carbon': 'CBC',
@phargogh
phargogh / convert.sh
Created April 25, 2020 01:14
Archiving mercurial repositories from bitbucket: project natcap-archive
REPO_DIR=$(pwd)/repos
OUT_DIR=$(pwd)/bundles
mkdir -p $OUT_DIR $REPO_DIR
for sshrepo in `cat repos.txt`
do
REPONAME=$(echo $sshrepo | cut -d '/' -f 5-)
TARGETDIR=$REPO_DIR/$REPONAME
hg clone $sshrepo --noupdate $TARGETDIR
pushd $OUT_DIR
@phargogh
phargogh / github_sha_on_master.sh
Created April 4, 2020 00:57
Getting the latest SHA on master for a GitHub repository
#!/bin/sh
# jq -r prints the 'raw' value.
curl https://api.github.com/repos/natcap/invest.users-guide/commits/master | jq -r .sha
@phargogh
phargogh / time-mfd-aspect.py
Last active March 23, 2020 21:51
Time MFD aspect calculations
# encoding=UTF-8
"""time-mfd-aspect.py
Time MFD mean aspect ratio generation routine over 100 runs.
Requires:
- pygeoprocessing==1.9.2
- git+https://github.com/phargogh/invest.git@8ac2575#egg=natcap.invest
"""
@phargogh
phargogh / printflowdirection.py
Created March 12, 2020 20:17
Print Multiple Flow Direction weights to the CLI when given a packed weight
import sys
def doit():
packed_flow_dir = int(sys.argv[1])
weights = [(packed_flow_dir >> 4*i) & 0xF for i in range(8)]
weights = [str(w).rjust(2, ' ') for w in weights]
print(weights)