Skip to content

Instantly share code, notes, and snippets.

@jdherman
jdherman / random-search-example.py
Last active May 15, 2023 10:43
example of pure random search in python
from random import random
# function to optimize: takes in a list of decision variables, returns an objective value
# this is the Rosenbrock function: http://en.wikipedia.org/wiki/Rosenbrock_function
# the global minimum is located at x = (1,1) where f(x) = 0
def my_function(x):
return (1-x[0])**2 + 100*(x[1] - x[0]**2)**2
# function to perform (a very crude, stupid) optimization
# bounds = lower and upper bounds for each decision variable (2D list)
@jdherman
jdherman / convert.sh.py
Created December 22, 2014 21:50
convert BSON to valid json, then parse it in python
#!/bin/bash
bsondump database.bson > database.json
# replace ObjectId("50f330869178b31dde000001")
# with just the string
sed -re 's/ObjectId\((.*)\)/\1/g' database.json > test.json
# then in python
@jdherman
jdherman / boostutil.h
Created December 9, 2014 20:09
boost utility functions for dealing with files
// utility functions for boost matrices/vectors
#include <fstream>
namespace ublas = boost::numeric::ublas;
using namespace std;
// create matrix:
// ublas::matrix<double> my_matrix(num_rows, num_columns);
// loadtxt("/path/to/file", my_matrix);
@jdherman
jdherman / backup.sh
Last active August 29, 2015 14:10
backup to dropbox
#!/bin/bash
declare -a arr=("professional" "phd" "presentations" "proposals")
for d in "${arr[@]}"
do
rsync -raz --progress --exclude=".git*" --delete ./"$d"/* Dropbox/jdh/"$d"
done
chown -R Administrators Dropbox/jdh/*
@jdherman
jdherman / generate-samples.py
Created November 18, 2014 20:42
SALib parameter samples into input file template
from SALib.sample import saltelli
from string import Template
# Generate samples for sensitivity analysis, and put them into an input file template
# This will generate N(2D+2) copies of the template with the values inserted
# The use case is for older models (like F77) where the input file something like below.
# Put dollar-sign variables where you want to replace with numbers, like so:
#
#C IWQVLIM = option for velocity limitation of macroalgae growth
#C = 0, macroalgae growth is not limited by stream velocity
@jdherman
jdherman / lake.py
Last active August 29, 2015 14:07
Lake problem (multiobjective) in python with Borg wrapper
from __future__ import division
import numpy as np
from scipy.optimize import brentq as root
from borg import *
nvars = 100 # decision variables: pollution for each timestep
nobjs = 4
nconstr = 1
nsamples = 100 # 100 scenarios of natural inflows
b = 0.42 # decay rate for P in lake (0.42 = irreversible)
@jdherman
jdherman / syllabus.py
Created October 14, 2014 14:50
syllabus keywords in python
import glob
kw = ['climate', 'decision theory', 'dynamic', 'economics', 'game theory', 'heuristic', 'hydrology',
'integer', 'linear', 'model', 'nonlinear', 'policy', 'probability', 'statistics',
'stochastic','quality']
files = glob.glob('*.txt')
courses_including = {}
avg_count = {}
@jdherman
jdherman / raster-point.py
Created October 7, 2014 18:16
Extract raster data value at a point
from __future__ import division
from osgeo import gdal
from geopy.geocoders import Nominatim
def get_value_at_point(rasterfile, pos):
gdata = gdal.Open(rasterfile)
gt = gdata.GetGeoTransform()
data = gdata.ReadAsArray().astype(np.float)
gdata = None
x = int((pos[0] - gt[0])/gt[1])
@jdherman
jdherman / map-raster.py
Created October 3, 2014 16:54
plotting raster data with shapefile in python+basemap
from osgeo import gdal
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap
# Plotting 2070 projected August (8) precip from worldclim
gdata = gdal.Open("D:/jon/datasets/worldclim/future/pr/gf45pr70_usa/gf45pr708.tif")
geo = gdata.GetGeoTransform()
data = gdata.ReadAsArray()
@jdherman
jdherman / clipusa.sh
Last active August 29, 2015 14:07
clip raster data with shapefile example
#!/bin/bash
DIR=current
TYPE=tmean
RES=5m
pwd
for MONTH in {1..12}
do
gdalwarp -cutline states_shape/states.shp \