-
Notifications
You must be signed in to change notification settings - Fork 9
/
Makefile
83 lines (64 loc) · 2.66 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
################################################################################
# GLOBALS #
################################################################################
FUERO_COMUN_PAGE=https://www.gob.mx/sesnsp/acciones-y-programas/incidencia-delictiva-del-fuero-comun-nueva-metodologia
VICTIMAS_PAGE=https://www.gob.mx/sesnsp/acciones-y-programas/victimas-nueva-metodologia
SNSP_DIR=clean/snsp-data
define download_file
# Visit the website with the open data downloads and extract the
# links with the latest data
curl -s $(1) | grep -Po '(?<=href=")[^"]*$(2)[^"]*(?=")' | \
sed 's| |%20|g' | xargs curl -s > $(3)
endef
define xlsb_to_csv
# list the file contents of the zip file, order by size, and extract
# only the biggest file to stdout
unzip -p $(SNSP_DIR)/$(1).zip "$$(unzip -l -qq $(SNSP_DIR)/$(1).zip \
| sort -nr | sed 's|^[ ]*||g' | awk -F" " 'NR==1{print $$2}')" \
> $(SNSP_DIR)/$(1).xlsb
# use libreoffice to convert to csv since it seems to be only program
# capable of converting xlsb files
cd $(SNSP_DIR) && libreoffice --headless --convert-to csv \
-env:UserInstallation=file:///tmp/foobar7665765 \
$(1).xlsb
endef
all: download_csv download_inegi clean_data analysis website deploy
.PHONY: download_csv
download_csv:
@echo "\n\n****************Download SNSP csv files****************\n"
./download.sh
.PHONY: download_inegi
download_inegi: R/data/inegi.sh
@echo "\n\n****************Downloading INEGI homicide data***********\n"
cd R/data && ./inegi.sh
.PHONY: clean_data
clean_data: db/crimenmexico.db
db/crimenmexico.db: download_csv
@echo "\n\n****************Clean Data******************\n"
$(RM) db/crimenmexico.db
sqlite3 db/crimenmexico.db < clean/meta/sql.sql
. ~/.virtualenvs/crimenmexico/bin/activate; cd clean && python clean.py
.PHONY: analysis
analysis: clean_data download_inegi
@echo "\n\n****************Statistical Analysis******************\n"
cd R && Rscript run_all.R
.PHONY: website
website:
@echo "\n\n****************Build Website******************\n"
./build_website.sh
.PHONY: deploy
deploy:
@echo "\n\n****************Deploy Website******************\n"
./deploy_website.sh
.PHONY: clean clean_snsp clean_db clean_analysis clean_inegi
clean: clean_snsp clean_inegi clean_db clean_analysis
clean_snsp:
$(RM) $(SNSP_DIR)/*.zip $(SNSP_DIR)/*.csv $(SNSP_DIR)/*.xlsb
clean_inegi:
$(RM) R/data/INEGI_exporta.csv
clean_db:
$(RM) db/crimenmexico.db
clean_analysis:
$(RM) R/graphs/*.png R/json/*.json R/json/*.RData \
R/interactive-map/*.json R/interactive-map/*.csv \
R/Rplots.pdf