Skip to content

Instantly share code, notes, and snippets.

View richpsharp's full-sized avatar

Richard Sharp richpsharp

View GitHub Profile
@richpsharp
richpsharp / sipa_to_table_legend.py
Created September 4, 2024 18:51
SIPA to table for Shail on 2024 09 04
import pandas as pd
datasets={
"PH_Top 10% of service overlap for _PH conservation": {
'fig_title' :"Top 10% of service overlap for , PH conservation",
'country' :"PH",
'labels' :["1 service","2 services","3 services","4 services"],
'visParams':{
'min':1,
'max':4,
@richpsharp
richpsharp / test_repeating_decimal.py
Created October 2, 2023 21:27
Can I guess if a decimal is a repeating decimal from a fraction?
import pandas as pd
repeating_dec_12ths = ['0833', '1666', '3333', '4166', '5833', '6666', '8333', '9166']
# 0.0833 - 1/12
# 0.1666 - 1/6
# 0.3333 - 1/3
# 0.4166 - 5/12
# 0.5833 - 7/12
# 0.6666 - 2/3
@richpsharp
richpsharp / Dockerfile
Last active August 7, 2021 05:27
gurobi with prioritizr installed too that also launches an Rscript on runtime
# run as:
# docker run --rm -it -v %CD%:/usr/local/workspace --volume=%CD%/gurobi.lic:/opt/gurobi/gurobi.lic:ro --volume=%CD%/models:/models:ro prioritizr ./run-prioritizations.R
FROM gurobi/optimizer
RUN apt-get update -qq
RUN apt-get install -y libgdal-dev
RUN apt-get install -y r-base r-base-dev
RUN apt-get install -y libudunits2-dev
RUN apt-get install -y libssl-dev
@richpsharp
richpsharp / convert_isimpi2b_netcdf_to_gtiff.py
Created June 11, 2021 01:07
Convert ISIMPI2B netcat files to geotiffs
"""Convert isimib2 to geotiff."""
import os
import glob
import xarray as xr
from osgeo import gdal
from osgeo import osr
LOCAL_RASTER_CREATION_OPTIONS = (
'TILED=YES', 'BIGTIFF=YES', 'COMPRESS=LZW',
@richpsharp
richpsharp / c_factor_min_evi.py
Created May 25, 2021 04:00
Find a good C factor
import numpy
import scipy.optimize
evi_vs_c = [
(0.02874, 0.95),
(0.18186, 0.25),
(0.3643000126, 0.13575),
(0.33039, 0.07075),
(0.54826, 0.035425),
(0.65346, 0.0001),
@richpsharp
richpsharp / align_clip_and_mask.py
Last active May 13, 2021 21:07
how to clip to area and mask by vector bounds and values
vector = gdal.OpenEx(vector_path, gdal.OF_VECTOR)
layer = vector.GetLayer()
layer.SetAttributeFilter('Country="USA"') # or whatever...
feature = layer.GetNextFeature()
geom = feature.GetGeometryRef()
envelope = geom.GetEnvelope()
target_bb = [envelope[i] for i in [0, 2, 1, 3]])
geom = None
feature = None
@richpsharp
richpsharp / beta_tests.py
Created April 12, 2021 20:53
is beta/m broken?
import numpy
import matplotlib.pyplot
slope_in_radians_list = numpy.linspace(0.089, 0.785, 100)
beta_list = []
m_list = []
for slope_in_radians in slope_in_radians_list:
beta = ((numpy.sin(slope_in_radians) / 0.0896) /
(3 * numpy.sin(slope_in_radians)**0.8 + 0.56))
m_list.append(beta/(1+beta))
@richpsharp
richpsharp / sdr.py
Created April 10, 2021 18:34
Modified LS factor Python code from rafa
"""InVEST Sediment Delivery Ratio (SDR) module.
The SDR method in this model is based on:
Winchell, M. F., et al. "Extension and validation of a geographic
information system-based method for calculating the Revised Universal
Soil Loss Equation length-slope factor for erosion risk assessments in
large watersheds." Journal of Soil and Water Conservation 63.3 (2008):
105-111.
"""
import os
@richpsharp
richpsharp / LS_from_DEM.m
Created April 10, 2021 18:32
LS code in matlab from rafa
clear all
close all
addpath(genpath(pwd))
% channel init threshold [km2]
Ad_ini=1;
% length threshold [m]
@richpsharp
richpsharp / models.py
Created April 2, 2021 15:51
SQLAlchamy example from STAC Geoserver
"""SQLAlchemy models for STAC view."""
from ..db import db
class Job(db.Model):
"""Stores info about a non-trivial running job invoked from the API."""
__tablename__ = "job_table"
job_id = db.Column(db.String, primary_key=True)