Skip to content

Instantly share code, notes, and snippets.

View drheinheimer's full-sized avatar

David Rheinheimer drheinheimer

View GitHub Profile
@drheinheimer
drheinheimer / 0_reuse_code.js
Last active August 29, 2015 14:07
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@drheinheimer
drheinheimer / data_connections.r
Created October 10, 2014 00:31
Connect to sqlite database in R. Requires RSQLite
get.conn <- function(dbpath) {
sqlite <- dbDriver("SQLite")
conn <- dbConnect(sqlite, dbpath)
return(conn)
}
@drheinheimer
drheinheimer / connection.py
Last active December 10, 2017 22:11
This shows you how to connect to a Hydra Platform database with Python. usage.py shows how you could use the connection class in connection.py to login and get data using the Hydra Platform API.
from requests import post
import json
class connection(object):
def __init__(self, url=None, app_name=None, session_id=None):
self.url = url
self.app_name = app_name or 'OpenAgua'
self.session_id = session_id
@drheinheimer
drheinheimer / adjacency.js
Last active January 29, 2018 18:45
This function creates an adjacency matrix from a Hydra network
const adjacency = (network) => {
const n = network.nodes.length;
// initialize the array
let adj = [new Array(n+1)];
// populate the row headers (sources) & column headers (destinations) with node names
let i = 0;
let lookup = {};
@drheinheimer
drheinheimer / create_hydra_dataset.py
Created February 5, 2018 16:40
Create a Hydra dataset value using attribute information
# define the dataset value
value = json.dumps(dataframe)
# create the dataset
dataset = {
'type': attr['dtype'], # e.g., "timeseries'; attr is from Hydra database
'name': '{} - {} - {} [{}]'.format(network["name"], resource["name"], attr['name'], scenario_name),
'unit': attr['unit'], # e.g., "bbl"
'dimension': attr['dim'], # e.g., "Volume"
'value': value # from above
process = function(files) {
var file = files[0];
var ext = file.name.split('.').pop();
// check if extension is kmz, etc.
const reader = new FileReader();
switch (file.type) {
case 'application/zip': // zipped shapefile, for example
@drheinheimer
drheinheimer / pip-install-gdal.md
Created March 15, 2018 04:26 — forked from cspanring/pip-install-gdal.md
Installing GDAL in a Python virtual environment

Installing GDAL in a Python virtual environment

Get gdal development libraries:

$ sudo apt-add-repository ppa:ubuntugis/ubuntugis-unstable
$ sudo apt-get update
$ sudo apt-get install libgdal-dev

Create and activate a virtual environment:

@drheinheimer
drheinheimer / network_nodes_to_csv.py
Created April 2, 2018 20:00
This converts an OpenAgua-flavored Hydra network's nodes from json to a csv
import json
import csv
import codecs # not sure why this is needed - need to fix upstream saving routine
jsonfile = 'MCMA.json'
network = json.load(codecs.open(jsonfile, 'r', 'utf-8-sig'))
# create the array of nodes
col_names = ['id', 'name', 'x', 'y']
import pandas as pd
from math import ceil
for region in ['CherryEleanor', 'DonPedro', 'Hetchy']:
# define input and output paths
inpath = 'baserun/{}/statvar.csv'.format(region)
outpath = 'baserun/{}/10day.csv'.format(region)
# read in the original csv
df = pd.read_csv(inpath, index_col=0, parse_dates=True)