Skip to content

Instantly share code, notes, and snippets.

View a8dx's full-sized avatar
😺

Anthony D'Agostino a8dx

😺
View GitHub Profile
@a8dx
a8dx / Find_nth_Degree_Neighbors.r
Created January 19, 2018 20:56
R script using Simple Features library to identify 1st and 2nd degree neighbors; plot results using Leaflet
# Filename: Find_nth_Degree_Neighbors.r
# Author: Anthony Louis D'Agostino
# Date Created: 01/18/2017
# Last Edited:
# Purpose: Tutorial example of identifying n-th degree neighbors using North Carolina county boundaries
# Notes: Borrows from Edzer Pebesma's sf Vignette #5 (https://cran.r-project.org/web/packages/sf/vignettes/sf5.html)
# -- preamble; installs necessary packages
remove(list = ls(all.names = TRUE))
@a8dx
a8dx / ZIP5_County_Lookup.do
Created March 28, 2018 19:51
Create a better ZIP5-County lookup table
/*
Filename: ZIP5_County_Lookup.do
Author: Anthony D'Agostino (ald at satanford dot edu)
Date Created: 02/09/2018
Last Edited: 03/27/2018
Purpose: Generate a merged zip5-county crosswalk that leverages the strengths of both the US Census and HUD ZCTA crosswalks.
*/
@a8dx
a8dx / DistrictNameMatching.py
Created July 9, 2018 17:59
Fuzzy Wuzzy String Matching Example
# -- DistrictNameMatching.py
# Author: Anthony Louis D'Agostino (ald2187 [at] columbia.edu)
# Purpose: Given CSV lists of district-state name pairs, identifies the best match given the fuzzywuzzy library
# Notes: Default number of matches currently set to 3, though can be modified as input argument.
import os
import numpy as np
from fuzzywuzzy import fuzz
from fuzzywuzzy import process
import pandas as pd
@a8dx
a8dx / US_State_Bounding_Boxes.csv
Created November 23, 2018 03:20
Bounding boxes for all US states and territories [NAD83]
STATEFP STUSPS NAME xmin ymin xmax ymax
1 01 AL Alabama -88.473227 30.223334 -84.88908 35.008028
2 02 AK Alaska -179.148909 51.214183 179.77847 71.365162
3 60 AS American Samoa -171.089874 -14.548699 -168.1433 -11.046934
4 04 AZ Arizona -114.81651 31.332177 -109.045223 37.00426
5 05 AR Arkansas -94.617919 33.004106 -89.644395 36.4996
6 06 CA California -124.409591 32.534156 -114.131211 42.009518
7 08 CO Colorado -109.060253 36.992426 -102.041524 41.003444
8 69 MP Commonwealth of the Northern Mariana Islands 144.886331 14.110472 146.064818 20.553802
9 09 CT Connecticut -73.727775 40.980144 -71.786994 42.050587
@a8dx
a8dx / convert_to_GrADS.R
Created November 30, 2018 05:30
Modifies the NCC-provided CTL file and generates unique versions for each year of data, each type of temperature: max, min, mean
# Filename: convert_to_GrADS.R
# Author: Anthony Louis D'Agostino (ald at stanford dot edu)
# Date Created: September 16, 2015
# Last Edited: October 07, 2017
# Purpose: Modifies the NCC-provided CTL file and generates unique versions for each year of data, each type of temperature
# max, min, mean.
# Notes: While perhaps an overkill, this is generalizable to a setting where grids are file-specific.
@a8dx
a8dx / IMD_convertRawData.py
Created November 30, 2018 05:31
Reads in GRaDS data files, concatenates them, and then exports netCDF versions
# Filename: IMD_convertRawData.py
# Author: Anthony Louis D'Agostino (ald at stanford dot edu)
# Date Created: 06/01/2017
# Last Edited: 10/07/2017
# Data: from NCC ZIP file
# Purpose: Reads in GRaDS data files, concatenates them, and then exports netCDF versions
# Notes: To be run after "convert_to_GrADS.R"
import os
@a8dx
a8dx / clean_BEST_data.r
Created December 12, 2018 09:20
Cleans the Berkeley Earth BEST temperature anomaly data and generates a raster of dated, temperature estimates
returnBESTtemp <- function(tempFile) {
# tempFile: complete path to a netcdf BEST temperature file of daily records [need not be geographic subset]
ave <- brick(tempFile, var = "climatology")
anomalies <- brick(tempFile, var = "temperature")
# -- deal with absence of leap year in anomaly series
ave.dates <- as_date(as.numeric(unlist(lapply(strsplit(names(ave), "X"), function(x) x[[2]]))), origin = "1949-12-31")
@a8dx
a8dx / US_County_Boundingboxes.csv
Created November 5, 2021 06:47
Bounding boxes for all US Counties (2020 Census, NAD83)
STATE_NAME COUNTY_NAME NAMELSAD xmin ymin xmax ymax GEOID STATEFP COUNTYFP
1 Alabama Autauga Autauga County -86.921237 32.307574 -86.411172 32.708213 01001 01 001
2 Alabama Baldwin Baldwin County -88.037309 30.146561 -87.366591 31.319017 01003 01 003
3 Alabama Barbour Barbour County -85.748433 31.617565 -85.048825 32.148251 01005 01 005
4 Alabama Bibb Bibb County -87.421989 32.831522 -86.876023 33.246926 01007 01 007
5 Alabama Blount Blount County -86.963358 33.765171 -86.303516 34.260483 01009 01 009
6 Alabama Bullock Bullock County -85.999258 31.880275 -85.410042 32.305289 01011 01 011
7 Alabama Butler Butler County -86.908939 31.523673 -86.438341 31.964629 01013 01 013
8 Alabama Calhoun Calhoun County -86.146225 33.55593 -85.529432 33.96846 01015 01 015
9 Alabama Chambers Chambers County -85.593653 32.72853 -85.122189 33.109038 01017 01 017