Skip to content

Instantly share code, notes, and snippets.

View SwampThingPaul's full-sized avatar

Paul Julian SwampThingPaul

View GitHub Profile
@SwampThingPaul
SwampThingPaul / example_code_S79Threshold.r
Created October 7, 2021 13:52
Event Counter - RECOVER salinity envelope
library(zoo)
# Read data and set up ----------------------------------------------------
dat=read.csv("ex_dat_S79_NA25.csv")
dat$Date=as.Date(dat$Date)
dat$S79.14d=with(dat,rollapply(S79,width=14,FUN=function(x)mean(x,na.rm=T),fill=NA,align="right"))
dat$CRE.low=with(dat,ifelse(S79.14d<750,1,0)) # RECOVER Low flow envelope
# Helper functions for 14-day periods
dat$period.14=c(0,rep(1:(nrow(dat)-1))%/%14)+1
@SwampThingPaul
SwampThingPaul / example_plots.r
Last active October 6, 2021 12:22
Example plots of how different regressions are estimated. Essentially reproducing Fig 4 in Warton et al (2006)
## From https://tonyladson.wordpress.com/2020/10/05/errors-in-variables-regression/
## Code adapted from https://gist.github.com/TonyLadson/286077221cc07022a77aa74d0a6c211c
# Libraries
library(deming)
# Functions ---------------------------------------------------------------
# given a point (x1, y1) and a line defined by a slope m and intercept c1
@SwampThingPaul
SwampThingPaul / DateTimeFix.R
Last active August 2, 2021 19:04
Date time fix for years with no prefix (ie century) Check out ?strptime and the %y format
# date.fun from personal AnalystHelper package
date.fun=function (x, tz = "EST", form = "%F"){as.POSIXct(strptime(x, form), tz = tz)}
# Example dataset
example=c("19-AUG-53 12.00.00.000000000 AM", "26-AUG-53 12.00.00.000000000 AM",
"26-AUG-53 12.00.00.000000000 AM", "26-AUG-53 12.00.00.000000000 AM",
"26-AUG-53 12.00.00.000000000 AM", "26-AUG-53 12.00.00.000000000 AM",
"01-APR-02 05.45.00.000000000 PM", "01-APR-02 12.00.00.000000000 AM",
"01-APR-02 12.00.00.000000000 AM", "01-APR-02 12.00.00.000000000 AM",
"01-APR-02 12.00.00.000000000 AM", "01-APR-02 12.00.00.000000000 AM")
@SwampThingPaul
SwampThingPaul / LOKMFL_18MonthPeriod.py
Created June 24, 2021 13:42
Julian and Calendar Day calculations and MFL 18month window
##
## LOK MFL 18 month period exploration and learning
##
## Code was compiled by Paul Julian
## contact info: pjulian@sccf.org
# Libraries
import datetime as dt
#
@SwampThingPaul
SwampThingPaul / juliandates.r
Created June 23, 2021 13:37
Conversion of julian and calendar dates
# Functions from a seperate python script
# Calendar to Julian Days
cal2jd=function(date){
year=as.numeric(format(date,"%Y"))
month=as.numeric(format(date,"%m"))
day=as.numeric(format(date,"%d"))
a = (14 - month)/12
y = year + 4800 - a
m = month + 12*a - 3
@SwampThingPaul
SwampThingPaul / Lake_VolArea.r
Last active May 24, 2021 16:11
Lake Volume and Area Calculation
## Code was compiled by Paul Julian
## contact info: pauljulianphd@gmail.com
# GIS libraries
library(sp)
library(rgdal)
library(rgeos)
library(raster)
# projection
@SwampThingPaul
SwampThingPaul / as_flextable_GAM.r
Created January 8, 2021 15:54
GAM version of as_flextable
## A series of functions to extract and display model summary tables
## Generalized Additive Models
## Code was compiled by Paul Julian
## contact info: pauljulianphd@gmail.com
## libraries
library(mgcv)
library(flextable)
library(magrittr)
@SwampThingPaul
SwampThingPaul / ExtractPaletteColors.R
Created September 7, 2020 18:49
Code to extract colors from an image based on k-means
library(jpeg)
# Custom function similar to print.palette(...)
RenderPal <- function(x,name){
if ((missing(x)) || (missing(name))){
stop("Internal error, please troubleshoot")
}
n <- length(x)
old <- graphics::par(mar = c(0.5, 0.5, 0.5, 0.5))
@SwampThingPaul
SwampThingPaul / ggridges_example.r
Created May 29, 2020 12:21
A working example with data of ggridges
## A working example with data of ggridges
# Libraries ---------------------------------------------------------------
if(!require(ggplot2)) { install.packages('ggplot2') } ; library(ggplot2)
if(!require(ggridges)) { install.packages('ggridges') } ; library(ggridges)
# Data --------------------------------------------------------------------
dat=data.frame(FedWY=c(1979, 1979, 1979, 1979, 1979, 1979, 1979, 1979, 1979,
1979, 1979, 1979, 1979, 1979, 1979, 1979, 1979, 1979, 1979, 1979,
1979, 1979, 1979, 1979, 1979, 1979, 1980, 1980, 1980, 1980, 1980,
1980, 1980, 1980, 1980, 1980, 1980, 1980, 1980, 1980, 1980, 1980,
@SwampThingPaul
SwampThingPaul / SampleSize_Monitoring.r
Last active March 27, 2020 00:59
Strobl and Robillard (2008) samples per year
# From
## Strobl, R.O., Robillard, P.D., 2008. Network design for water
## quality monitoring of surface freshwaters: A review. Journal
## of Environmental Management, Microbial and Nutrient Contaminants
## of Fresh and Coastal Waters 87, 639–648.
## https://doi.org/10.1016/j.jenvman.2007.03.001
#devtools::install_github("SwampThingPaul/AnalystHelper")
library(AnalystHelper)