Skip to content

Instantly share code, notes, and snippets.

View walkerjeffd's full-sized avatar

Jeff Walker walkerjeffd

View GitHub Profile
@walkerjeffd
walkerjeffd / setup.sh
Last active August 29, 2015 14:02
Raspberry Pi Set Up
# Raspberry Pi Set Up Shell Script
# By Jeff Walker
# I created this to make it easier to re-install/re-configure my pi in case I need to start from scratch.
# Assumes Occidentalis v0.2 is being used.
# Run an update
# sudo apt-get update
# Adafruit WebIDE
@walkerjeffd
walkerjeffd / sketch.ino
Created May 25, 2014 17:35
Arduino Temperature and Humidity Logger using Adafruit SD Shield and DHT22
// log DHT sensor readings to SD card
#include "DHT.h"
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL 60000 // mills between entries (reduce to take more/faster data)
@walkerjeffd
walkerjeffd / ghcnd-inventory-convert.py
Created February 10, 2014 14:48
Tools for processing GHCND text files
import pandas as pd
import csv
colspecs = [(0,11), (12,20), (21,30), (31,35), (36,40), (41,45)]
df = pd.read_fwf('ghcnd-inventory.txt', colspecs=colspecs, header=None, index_col=None)
df.columns = ["ID", "LATITUDE", "LONGITUDE", "ELEMENT", "FIRSTYEAR", "LASTYEAR"]
df.set_index("ID", inplace=True)
df.to_csv('ghcnd-inventory.csv', quoting=csv.QUOTE_NONNUMERIC)
@walkerjeffd
walkerjeffd / is_regular.R
Created August 7, 2013 23:04
R function to test is vector of datetimes is regular and continuous
is.regular <- function(x) {
# returns TRUE if vector x of POSIXct datetimes is continuous and regular
# by checking if there are more than one unique difftime between each row
x.difftime <- difftime(x[2:length(x)], x[1:(length(x)-1)], units='secs')
if (length(levels(factor(x.difftime))) > 1) {
return (FALSE)
} else {
return (TRUE)
}
}
@walkerjeffd
walkerjeffd / zoo_regular.R
Created August 7, 2013 23:02
R function for creating a regular and continuous zoo object from two vectors of dates/datetimes and values with optional fill values
zoo.regular <- function(dates, values, by="hour", fill=NA) {
# convert time series to regular zoo object
require(zoo)
z <- zoo(values, dates)
z <- merge(z, zoo(, seq(floor_date(start(z), 'day'),
ceiling_date(end(z), 'day'),
by)),
fill=fill)
z <- z[1:(length(z)-1)]
return(z)
@walkerjeffd
walkerjeffd / events.R
Last active March 9, 2018 21:43
R functions for assigning and summarizing storm events based on interevent period and minimum event total precipitation
assign.events <- function(df, datetime.name="DATETIME", value.name="VALUE", interevent=8, threshold=0.1) {
# assigns events to data frame such as storm events or discharge events
require(plyr)
if (!(datetime.name %in% names(df))) {
stop(paste0('Could not find datetime column called ', datetime.name))
}
if (!(value.name %in% names(df))) {
stop(paste0('Could not find value column called ', value.name))
}
if (!is.regular(df[, datetime.name])) {
@walkerjeffd
walkerjeffd / ggplot2.calendar
Last active December 20, 2015 02:29
Calendar Plot using ggplot2
require(lubridate)
require(plyr)
require(ggplot2)
theme_set(theme_bw())
# create random dataset
df <- data.frame(DATETIME = ymd("2000-01-01") + ddays(runif(100)*365*5))
# compute day/month columns
df <- mutate(df,