Skip to content

Instantly share code, notes, and snippets.

View TonyLadson's full-sized avatar

Tony Ladson TonyLadson

View GitHub Profile
###########################################################################################
#
# Fitting non-linear models in R
# See tonyladson
#
# 5 June 2016
###############################################################################################
#
# Example data come from Figure 5 from Ian Cordery's thesis
# Also Figure 3 from Cordery, 1970
@TonyLadson
TonyLadson / API.R
Last active January 23, 2019 03:47
Antecedent Precipitation Index calculations see https://tonyladson.wordpress.com/2016/06/14/how-wet-is-the-catchment/
#
# API - Antecedent Precipitation Index
#
#
#
library(stringr)
library(readr)
library(dplyr)
library(lubridate)
@TonyLadson
TonyLadson / RFFEScrape.R
Last active June 5, 2023 00:11
Scraping the RFFE see hthttps://tonyladson.wordpress.com/2016/07/04/scraping-the-rffe/
library(RCurl)
library(jsonlite)
library(stringr)
library(dplyr)
library(ggplot2)
rffe.data <- postForm("http://rffe.arr.org.au/",
catchment_name = "test1",
library(stringr)
library(readr)
library(dplyr)
library(lubridate)
library(ggplot2)
library(scales)
library(treemap)
#__________________________________________________________________________________________
@TonyLadson
TonyLadson / Calc_DOSat.R
Last active August 9, 2016 11:38
Function to calculate 100% saturated dissolved oxygen in mg/L as a function of temperature in degrees Celcius
# Function to calculate 100% saturated dissolved oxygen in mg/L as a function of temperature in degrees Celcius
Calc_DOsat100 = function(temp){
# temp = temperature in degrees C
# relationship between temperature and dissolved oxygen as defined by the APHA
# American Public Health Association (1992)
# Standard methods for the examination of water and wastewater. 18th ed. Washington DC.
# Required constants
@TonyLadson
TonyLadson / UTM2deg.R
Last active January 24, 2020 10:24
Converts UTM coordinates to Latitude and Longitude. Based on a port of this spreadsheet http://www.ga.gov.au/__data/assets/file/0020/11378/Redfearns-formulae-to-convert-between-latitude-and-longitude.xls This works for Australia but there are problems in other areas. There are other formulas available. See https://en.wikipedia.org/wiki/Universal…
# format_out either 'deg' - decimal degrees, or 'dms' - deg:min:sec
UTM2deg <- function(easting, northing, zone, format_out = c('deg', 'dms')){
format_out <- match.arg(format_out)
# Helper function to convert from decimal dms to dd:mm:ss
Deg2dms <- function(x){
dd <- trunc(x)
mm <- trunc(60*(x - dd))
@TonyLadson
TonyLadson / Fix_names.R
Last active August 26, 2016 05:51
Function to make a consistent set of names for a dataframe: lower case, syntactically correct, not too many dots
# x is a dataframe
Fix_names <- function(x){
my.names <- make.names(names(x))
my.names <- str_to_lower(my.names)
# change multiple consecutive dots to one dot, delete leading and trailing dots
my.names <- str_replace_all(my.names, c("[.]+" = "\\.", "[.]$" = "", "^[.]" = ""))
names(x) <- my.names
x
@TonyLadson
TonyLadson / Calc_DOsat100.R
Created August 14, 2016 22:01
Function to calculate 100% saturated dissolved oxygen in water in mg/L as a function of temperature in degrees Celcius
# Function to calculate 100% saturated dissolved oxygen in water in mg/L as a function of temperature in degrees Celcius
Calc_DOsat100 = function(temp){
# temp = temperature in degrees C
# relationship between temperature and dissolved oxygen as defined by the APHA
# American Public Health Association (1992)
# Standard methods for the examination of water and wastewater. 18th ed. Washington DC.
# Required constants
@TonyLadson
TonyLadson / DO_clean.R
Last active February 6, 2023 09:37
R code for the blog
# Figure 1
# DO - date of sample
WQ %>%
ggplot(aes(x = date, y = do.mg.l)) + geom_point() +
labs(y = 'DO (mg/L)', x = 'Date') +
theme_grey(base_size = 16)
# Figure 2
# DO - temperature
@TonyLadson
TonyLadson / Time_convert.R
Created August 18, 2016 04:20
Convert from HH:MM:SS to fractions of a day.
# Convert from 'HH:MM:SS' to fractions of a day
# This also works with HH.HHHH
# and is vectorised
Time_convert <- function(x){
f <- function(x,y){
as.numeric(x)/60 + as.numeric(y)
}
split_time <- str_split(x, ':')