Skip to content

Instantly share code, notes, and snippets.

View adamhsparks's full-sized avatar

Adam H. Sparks adamhsparks

View GitHub Profile
@adamhsparks
adamhsparks / dpird_cvt_sf.R
Last active April 17, 2024 06:00
Get DPIRD CVT Zones as an {sf} Object in Your R Session
# get DPIRD CVT zones from the Public Services Slip WA as an {sf} object in R
# data are CC 4.0 and available from: <https://data.wa.gov.au/slip>
wfs_wa <-
"https://public-services.slip.wa.gov.au/public/services/SLIP_Public_Services/Boundaries_WFS/MapServer/WFSServer"
url <- httr2::url_parse(wfs_wa)
url$query <- list(service = "wfs",
#version = "2.0.0", # facultative
request = "GetCapabilities")
@adamhsparks
adamhsparks / historical_weather.py
Created March 27, 2024 06:47
Fetch historical weather data with Python using various packages
# Fetch historical weather data with Python using various packages
# use plotnine for graphing
from plotnine import *
import pandas as pd
import pathlib
# pynasapower
# Fetches NASA POWER data
@adamhsparks
adamhsparks / df_to_power.R
Last active March 14, 2024 06:01
Use a data frame of lonlat and date values to get data from POWER API using {nasapower}
library("nasapower")
library("purrr")
library("dplyr")
df <- data.frame(
stringsAsFactors = FALSE,
lon = c(151.81, 112.5, 115.5),
lat = c(-27.48, -55.5, -50.5),
date = c("3/3/2023", "5/12/2023", "1/3/2024")
)
@adamhsparks
adamhsparks / gist:5c82d48c31b5c22d0974fbeafb2f9ad1
Created February 19, 2024 06:42
Owner avatar OceanOmics-classifier-comparison trial run.R
- Project '~/tmp/OceanOmics-classifier-comparison' loaded. [renv 1.0.3]
> targets::tar_make()
> dispatched target all_results_files
o completed target all_results_files [0 seconds]
> dispatched branch all_results_ecfccd47
o completed branch all_results_ecfccd47 [0 seconds]
> dispatched branch all_results_eccc5a34
o completed branch all_results_eccc5a34 [0 seconds]
> dispatched branch all_results_05574669
o completed branch all_results_05574669 [0 seconds]
@adamhsparks
adamhsparks / wa_web_feature_services.R
Last active November 13, 2023 11:50
Use Western Australia's Web Feature Services in R
# Get all DPIRD boundary data sets available from the Public Services Slip WA and save data for use in an R package.
# HT to Thierry Onkelinx, Hans Van Calster, Floris Vanderhaeghe for their post,
# <https://inbo.github.io/tutorials/tutorials/spatial_wfs_services/>, but I
# modified this to work to save .Rds files for use in an R package, not just
# saving to disk and chose to use {httr2} in place of {httr}.
# NOTE: This URL is only for public boundaries,
<https://public-services.slip.wa.gov.au/public/services/SLIP_Public_Services/Boundaries_WFS/MapServer/WFSServer>,
there are others, see <https://catalogue.data.wa.gov.au/dataset> for other orgs and types of data
@adamhsparks
adamhsparks / quietly_wOz_stations.R
Last active October 17, 2023 02:46
Given a list of named places, get a list of weather stations in R from {weatherOz} or capture the message if no stations were found
# Find stations in both the DPIRD and SILO weather station networks given a
# vector of locations or capture messages when no stations are found
library("tidygeocoder")
library("data.table")
library("weatherOz")
library("purrr")
# Step 1: Create a vector of named locations to geocode
@adamhsparks
adamhsparks / StatQuantileBin.R
Created April 16, 2023 11:39 — forked from eliocamp/StatQuantileBin.R
Percentogram (histogram with bins of equal number of observations)
StatQuantileBin <- ggplot2::ggproto("StatQuantileBin", ggplot2::StatBin,
default_aes = ggplot2::aes(x = ggplot2::after_stat(density), y = ggplot2::after_stat(density), weight = 1),
compute_group = function(data, scales,
binwidth = NULL, bins = 30, breaks = NULL, trim = 0,
closed = c("right", "left"), pad = FALSE,
flipped_aes = FALSE,
# The following arguments are not used, but must
# be listed so parameters are computed correctly
origin = NULL, right = NULL, drop = NULL,
@adamhsparks
adamhsparks / fetchGHdata.R
Created October 5, 2022 09:05 — forked from ritchieking/fetchGHdata.R
Fetch a data file directly from a private GitHub repo in R
# Download any data file from a private GitHub repo
# regardless of how large it is.
# Returns a string that will then need to be parsed
# by read_csv or the like to turn it into a data frame.
# Dependencies
require(tidyverse)
require(httr)
require(rlist)
require(jsonlite)
@adamhsparks
adamhsparks / Sigatoka_dual_axis.R
Last active January 21, 2022 02:18
Another example of dual axis in ggplot2
# Entering data
week <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
YLI <- c(3, 4, 5, 2, 6, 5, 4, 3, 5, 6, 2, 4)
YLS <- c(10, 11, 11, 12, 10, 11, 11, 10, 12, 13, 10, 11)
rainfall <- c(1, 10, 8, 6, 65, 30, 2, 10, 8, 6, 65, 30)
# Creating Data Frame
perf <- data.frame(week, YLI, YLS, rainfall)
# Plotting Charts and adding a secondary axis
@adamhsparks
adamhsparks / audpc.jl
Last active January 19, 2022 10:57
A Julia function to calculate area under the disease progress curve (AUDPC)
function audpc(evaluation, dates)
n = length(evaluation) - 1
disvec = Base.zeros(n)
datevec = Base.zeros(n)
out = 0.0
for i in 1:n
disvec[i] = (evaluation[i] + evaluation[i + 1]) / 2
datevec[i] = dates[i + 1] - dates[i]
out = sum(disvec .* datevec)