Skip to content

Instantly share code, notes, and snippets.

@sdtaylor
sdtaylor / cap_tests.py
Created November 28, 2022 23:39
CAP Stuff
# Various tests with Common Alert Protocal (CAP) feeds at https://severeweather.wmo.int/v2/
from datetime import datetime, timedelta
import requests
import re
#import xmltodict
import feedparser
from capparselib.parsers import CAPParser
# Built-in modules
import os
import datetime
import itertools
from glob import glob
from aenum import MultiValueEnum
# Basics of Python data handling and visualization
import numpy as np
@sdtaylor
sdtaylor / interaction_plot.R
Created March 31, 2022 00:51
ggplot interaction plot
library(tidyverse)
timing_results = tribble(
~treatment, ~object, ~mouse_id, ~time,
'vehicle', 'familiar', '1', 15,
'vehicle', 'novel', '1', 55,
'vehicle', 'familiar', '2', 19,
'vehicle', 'novel', '2', 60,
@sdtaylor
sdtaylor / mp_example.py
Created November 5, 2021 13:57
Basci python multipocessing
def processSpecies(speciesName):
#do stuff for this species
return(result)
speciesList=['a','b','c','d']
#############################################
#how stuff is typically done
results=[]
for species in speciesList:
@sdtaylor
sdtaylor / install_stuff.sh
Created September 17, 2021 12:57
python geospatial environment
conda create --name geo python=3.8
conda activate geo
# with a spyder install outside the geo environment, but
# pointing to the geo environment bin/python, the following
# must be installed in the geo env to get the spyder ipython console working
pip install spyder-kernels~=2.0.0
# primary geospatial things
pip install xarray rasterio geopandas numpy pandas scipy netcdf4 dask
@sdtaylor
sdtaylor / idenfity_continuous_non_zero_series.R
Last active July 21, 2021 13:59
idenfity continuous non-zero values in timeseries
library(tidyverse)
identify_continuous_non_zero_series = function(df, min_sequence_size =5){
# adds a new column called 'continuous_series' to the df data.frame
# identifying where 'value' column is >0 for at least min_sequence_size
# filter to only continous chunks > 0, assign each an ID,
# and flag each chunk >= min_sequence_size.
# the cumsum() trick is from https://stackoverflow.com/a/42734207/6615512
temp_df = df %>%
@sdtaylor
sdtaylor / variable_tickmark_length.R
Last active October 20, 2022 15:53
ggplot varying length tickmarks
library(tidyverse)
#------------------------------------
# Have x-axis labels offset slightly for clarity using guide_axis(n.dodge = 2),
# but also have different length tickmarks to make it look nicer.
# See https://twitter.com/ecologyofgavin/status/1344102509585997824
# Derived from https://stackoverflow.com/a/51312611/6615512
# Requires ggplot 3.0.0 or greater
#------------------------------------
@sdtaylor
sdtaylor / massage_survival_data.R
Last active December 30, 2020 22:42
Massage survival data to different dates
library(tidyverse)
# Create a "date bin" data.frame with columns c('start','end')
# start_date, and last_date are dates
# time_bin_size should be string like '1 year','6 month', etc. see ?seq.Date for more examples
bin_dates = function(first_date, last_date, time_bin_size='1 year'){
if(!(lubridate::is.Date(first_date) & lubridate::is.Date(last_date))) stop('first_date and last_date must be dates')
if(first_date>=last_date) stop('first_date must come before last date')
full_range = seq.Date(first_date,last_date, by=time_bin_size)
@sdtaylor
sdtaylor / csv_to_sf.R
Last active December 17, 2020 22:59
csv to sf object
library(tidyverse)
library(sf)
# where lon, lat are columns for longitude, latitude, respectively.
# Any other columns are retained in the sf data.frame.
read_csv('site_list.csv') %>%
st_as_sf(coords = c('lon','lat'), crs=4326)