Skip to content

Instantly share code, notes, and snippets.

@mathew-fleisch
Last active July 17, 2020 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathew-fleisch/5a3fb6d804a5dd84b38639e280d78eaa to your computer and use it in GitHub Desktop.
Save mathew-fleisch/5a3fb6d804a5dd84b38639e280d78eaa to your computer and use it in GitHub Desktop.
use public data to calculate percentage infected by county in california (linux date format. won't work on mac)
#!/bin/bash
# Get population of california by county (json)
POPULATION_URL=https://www.california-demographics.com/counties_by_population
POPULATION=$(curl -s $POPULATION_URL | tr '\n' ' ' | sed -e 's/\s\s*/ /g' | sed -e 's/.*th>//g' | sed -e 's/<td colspan.*//'g | sed -e 's/<tr>/\n/g' | sed -e 's/.*demographics">/"/g' | sed -e 's/<\/a.*<td>\ /":/g' | sed -e 's/\ <\/td.*//g' | sed -e 's/,//g' | sed -e 's/<\/tr>//g' | tr '\n' ',' | sed -e 's/^\s*,//g' | sed -e 's/,\s*$//g' | sed -e 's/\ County//g' | sed -e 's/\(.*\)/{\1}/g' | jq --slurp -c '.[]')
# echo "population: $POPULATION_URL"
# echo "$POPULATION"
# echo "------------------------------"
# Get totals infected for each county
DATA_SOURCE=926fd08f-cc91-4828-af38-bd45de97f8c3
TARGET_DATE=$(date +"%Y-%m-%d" --date="yesterday")
TARGET_URL=https://data.ca.gov/api/3/action/datastore_search?resource_id=$DATA_SOURCE\&filters=%7B%22date%22:%22$TARGET_DATE%22%7D
# echo "TARGET: $TARGET_URL"
# echo "TARGET DATE: $TARGET_DATE"
# echo "curl -s $TARGET_URL | jq '.'"
# echo "------------------------------"
result=$(curl -s $TARGET_URL | jq -c '.result.records[]')
# echo "$result"
# echo "Infected Californian population by county: "
while IFS= read -r county; do
# echo "county: $county"
this_totalcountconfirmed=$(echo $county | jq -r '.totalcountconfirmed')
# this_newcountdeaths=$(echo $county | jq -r '.newcountdeaths')
# this_totalcountdeaths=$(echo $county | jq -r '.totalcountdeaths')
this_county=$(echo $county | jq -r '.county')
# this_newcountconfirmed=$(echo $county | jq -r '.newcountconfirmed')
# this_date=$(echo $county | jq -r '.date')
# this_id=$(echo $county | jq -r '._id')
if [[ -z $(echo "$POPULATION" | grep "$this_county") ]]; then
# echo "$this_county not found in population... skipping."
continue
fi
this_population=$(echo "$POPULATION" | jq --raw-output '.["'"$this_county"'"]')
printf -v this_tmp_percentage "%03d" $(($this_totalcountconfirmed*10000/$this_population))
this_percentage=$(echo "$this_tmp_percentage" | sed -e 's/\(.\)\(.\)\(.\)/\1.\2\3\%/g' | sed -e 's/00\./0./g')
# echo "$this_county: $this_totalcountconfirmed / $this_population = $this_percentage"
# echo "$this_county: $this_percentage"
echo "$this_percentage,$this_county"
# exit
done <<< "$result"
# Example output from 2020-07-16
# ./get-infected.sh | sort -n -r
# 3.94%,Imperial
# 1.92%,Kings
# 1.47%,Los Angeles
# 1.38%,Tulare
# 1.37%,Marin
# 1.16%,Riverside
# 1.16%,Lassen
# 1.10%,Stanislaus
# 1.09%,Santa Barbara
# 1.02%,San Bernardino
# 1.01%,San Joaquin
# 0.94%,Fresno
# 0.89%,Orange
# 0.82%,Kern
# 0.80%,Merced
# 0.75%,Madera
# 0.66%,Glenn
# 0.65%,San Diego
# 0.65%,Monterey
# 0.64%,Colusa
# 0.61%,Ventura
# 0.61%,San Benito
# 0.56%,San Mateo
# 0.54%,San Francisco
# 0.52%,Solano
# 0.52%,Alameda
# 0.46%,Yolo
# 0.46%,Contra Costa
# 0.45%,Sutter
# 0.42%,Napa
# 0.41%,Sacramento
# 0.40%,San Luis Obispo
# 0.39%,Sonoma
# 0.36%,Santa Clara
# 0.36%,Mono
# 0.29%,Placer
# 0.28%,Yuba
# 0.25%,Santa Cruz
# 0.21%,Inyo
# 0.21%,Del Norte
# 0.21%,Butte
# 0.19%,Tehama
# 0.19%,Nevada
# 0.19%,Mariposa
# 0.19%,Lake
# 0.18%,El Dorado
# 0.17%,Calaveras
# 0.13%,Tuolumne
# 0.13%,Mendocino
# 0.13%,Humboldt
# 0.11%,Shasta
# 0.11%,Amador
# 0.08%,Siskiyou
# 0.08%,Plumas
# 0.08%,Alpine
# 0.03%,Sierra
# 0.01%,Trinity
# 0.00%,Modoc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment