-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_data.sh
executable file
·124 lines (113 loc) · 4.22 KB
/
update_data.sh
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/bash
# this script updates data used in shiny visualization app
# it works in waterfall way: it prepares districts, then it prepares maps for
# all districts, and so on
# stop on error
set -e
# folder where R scripts are stored---unless replaced with -r option
RSCRIPTDIR=.
# folder where all data are stored---unless replaced with -b option
DIRORIGIN=data
# help
usage() {
echo "Použití:"
echo "start.sh [-h] [-i <image>] [-n <container name>] [-d <data folder>] [-m] [-c] [-k]"
echo " -h ... vypíše nápovědu"
echo " -i <image> ... volitelný název použitého docker image; implicitně acc-clusters:latest"
echo " -n <container name> ... volitelný název kontejneru"
echo " -p <docker run parameters> ... volitelné parametry předané dockeru při spuštění kontejneru"
echo " -d <data folder> ... volitelná cesta ke složce s daty; implicitně aktuální adresář"
echo " -m ... volitelně uloží na disk PDF soubor s dokumentací -- pokud neexistuje"
echo " -c ... volitelně uloží na disk počáteční nastavení konfigurace -- pokud neexistuje"
echo " -k ... volitelně aktualizuje klastry dopravních nehod"
echo "Pokud není zadán žádný z parametrů -m, -c, ani -k, aktualizuje běžná data o nehodách."
exit 2
}
# function to run R scripts; it sets RSCRIPTDIR variable and handles other
# parameters, too
runRscript () {
echo ""
echo "Running $1.R"
echo "------------------------------------------------"
Rscript -e "RSCRIPTDIR=\"$RSCRIPTDIR\"" \
-e "DIR_ORIGIN=\"$DIRORIGIN\"" \
-e "source(\"$RSCRIPTDIR/$1.R\")"
}
# process flag parameters; use the implicit values it they aren't set
WHATTODO="update"
while getopts 'hmckr:b:' opt; do
case "$opt" in
m)
WHATTODO="nothing"
if [ -f "$DIRORIGIN/man/dokumentace.pdf" ];
then
echo "ERROR: The manual already exists at "$DIRORIGIN/man/dokumentace.pdf"; skipping."
echo " If you want to recreate it, remove this file."
else
echo "Writing the manual to $DIRORIGIN/man."
mkdir -p "$DIRORIGIN/man"
cp man/dokumentace.pdf "$DIRORIGIN/man"
fi
;;
c)
WHATTODO="nothing"
if [ -d "$DIRORIGIN/config" ];
then
echo "ERROR: $DIRORIGIN/config already exists. I can't overwrite it."
echo " If you want to create it from the scratch, remove this folder."
else
echo "Writing basic config to $DIRORIGIN/config."
mkdir "$DIRORIGIN/config"
cp config/* "$DIRORIGIN/config"
fi
;;
k)
if [ $WHATTODO == "nothing" ]; then
echo "You cannot produce manual/initial config and calculate clusters at the same time!"
exit 1
fi
WHATTODO="update clusters"
;;
r) RSCRIPTDIR=${OPTARG};;
b) DIRORIGIN=${OPTARG};;
?|h)
usage
exit 1
;;
esac
done
shift "$(($OPTIND -1))"
# welcome string
if [[ $WHATTODO == *update* ]]; then
NICESTRING="only"
if [[ $WHATTODO == *clusters* ]]; then
NICESTRING="and accidents clusters"
fi
echo "Updating data on traffic accidents for shiny app"
echo "================================================"
echo "Updating basic statistics $NICESTRING."
# echo "Using RSCRIPTDIR=\"$RSCRIPTDIR\", DIR_ORIGIN=\"$DIRORIGIN\"."
fi
# update basic statistics
if [[ $WHATTODO == *update* ]]; then
runRscript "start_logging"
runRscript "prepare_profiles"
runRscript "prepare_districts"
runRscript "prepare_maps"
runRscript "prepare_maps_lixels"
runRscript "prepare_accidents_raw"
runRscript "prepare_accidents_shiny"
fi
# update clusters
if [[ $WHATTODO == *clusters* ]]; then
runRscript "prepare_maps_lixel_samples"
runRscript "prepare_maps_lixel_neighbors"
runRscript "prepare_accidents"
runRscript "prepare_densities"
runRscript "prepare_clusters"
runRscript "prepare_sidecars"
runRscript "prepare_gis"
fi
# if it gets here, all is ok
echo
echo "All updates of basic statistics $NICESTRING finished (no errors)."