Skip to content

Instantly share code, notes, and snippets.

@randomchance
Last active September 9, 2019 01:52
Show Gist options
  • Save randomchance/8c8b924869500441a43c6904f58eabfd to your computer and use it in GitHub Desktop.
Save randomchance/8c8b924869500441a43c6904f58eabfd to your computer and use it in GitHub Desktop.
Finding local data from the massive dump of water quality studies referenced in this [article](https://www.hsph.harvard.edu/news/press-releases/toxic-chemicals-drinking-water/)
# There are lots of "one-liner" scripts out to grab this data, but this isn't one
# this is broken apart to make it easier to follow along with and adjust as needed
# download raw data at https://www.epa.gov/sites/production/files/2015-09/ucmr-3-occurrence-data.zip
# extract it and update the two paths below, or just run this from the extracted directory.
$zipFile ="UCMR3_ZipCodes.txt"
$AllFile = "UCMR3_All.txt"
# replace these zip codes with ones you are interested in
[string[]] $ZipCodes = @('48103','28401')
$resultsHeader = (Get-Content -Path $AllFile | select -First 1 ) -split "`t"
$found = Select-String -Pattern $ZipCodes -Path $zipFile -AllMatches -SimpleMatch
$Locations = $found.Line |%{$t = $_ -split "\s";[pscustomobject]@{PWSID = $t[0];Zip=$t[1]}}
$Results = (Select-String -Pattern $Locations.PWSID -Path $AllFile -SimpleMatch -AllMatches ).line | ConvertFrom-Csv -Delimiter "`t" -Header $resultsHeader
#$Results # $Results contains the raw results with all of the data in nice objects to work with
# If you want to show a quick table of the relevant results
$DisplayProperties = @('AnalyticalResultsSign','Contaminant','PWSName','AnalyticalResultsValue')
$Results | Sort-Object 'AnalyticalResultsSign' -Descending | ft -AutoSize -Property $DisplayProperties
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment