-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc-wire.sh
More file actions
executable file
·163 lines (137 loc) · 4.67 KB
/
Copy pathc-wire.sh
File metadata and controls
executable file
·163 lines (137 loc) · 4.67 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash
# Shell script for the C-Wire Project
# Usage: ./c-wire.sh csv_file_path station_type consumer_type [plant_id]
# Check for help option
for arg in "$@"; do
if [ "$arg" = "-h" ] || [ "$arg" = "--help" ]; then
echo "Usage: $0 csv_file_path station_type consumer_type [plant_id]"
echo
echo "Parameters:"
echo " csv_file_path : Path to the CSV input file"
echo " station_type : hvb | hva | lv"
echo " consumer_type : comp | indiv | all"
echo " plant_id : Optional, defaults to -1 if not provided"
echo
echo "Examples:"
echo " $0 data.csv lv all"
echo " $0 data.csv hvb comp 1234"
exit 0
fi
done
start=$(date +%s)
# Parameter count check
if [ "$#" -lt 3 ]; then
echo "Error: Insufficient number of parameters."
echo "Use -h for help."
exit 1
fi
# Retrieving parameters
csv_file="$1"
station_type="$2"
consumer_type="$3"
plant_id="${4:--1}"
# Input file validation
if [ ! -f "$csv_file" ]; then
echo "Error: The file $csv_file does not exist."
exit 1
fi
# Station type validation
if [[ "$station_type" != "hvb" && "$station_type" != "hva" && "$station_type" != "lv" ]]; then
echo "Error: Invalid station type. Valid options: hvb, hva, lv."
exit 1
fi
# Consumer type validation
if [[ "$consumer_type" != "comp" && "$consumer_type" != "indiv" && "$consumer_type" != "all" ]]; then
echo "Error: Invalid consumer type. Valid options: comp, indiv, all."
exit 1
fi
# Plant ID validation if provided
if [ "$plant_id" != "-1" ]; then
echo "Checking plant ID: $plant_id"
valid_id=$(awk -F';' -v id="$plant_id" '$1 == id {print $1; exit}' "$csv_file")
if [ -z "$valid_id" ]; then
echo "Error: The plant ID $plant_id does not exist in the file."
exit 1
fi
fi
# Prohibited options
if [[ "$station_type" == "hvb" && ( "$consumer_type" == "all" || "$consumer_type" == "indiv" ) ]]; then
echo "Error: hvb all or hvb indiv options are not allowed."
exit 1
fi
if [[ "$station_type" == "hva" && ( "$consumer_type" == "all" || "$consumer_type" == "indiv" ) ]]; then
echo "Error: hva all or hva indiv options are not allowed."
exit 1
fi
# Create necessary directories
mkdir -p tmp output
# Clear tmp and output directories
rm -f tmp/*
rm -f output/*
if [ "$plant_id" == "-1" ]; then
filtered_file="tmp/filter_${station_type}_${consumer_type}.csv"
else
filtered_file="tmp/filter_${station_type}_${consumer_type}_${plant_id}.csv"
fi
# Filtering data
awk -F';' -v station="$station_type" -v consumer="$consumer_type" -v plant="$plant_id" '
BEGIN { OFS=";" }
{
if (station == "hvb" && $2 != "-" && $6 == "-") {
if (plant == "-1" || $1 == plant) print $0;
}
else if (station == "hva" && $3 != "-" && $6 == "-") {
if (plant == "-1" || $1 == plant) print $0;
}
else if (station == "lv" && $4 != "-" && $1 != "Power plant" && consumer == "indiv" && $5 == "-") {
if (plant == "-1" || $1 == plant) print $0;
}
else if (station == "lv" && $4 != "-" && $1 != "Power plant" && consumer == "comp" && $6 == "-") {
if (plant == "-1" || $1 == plant) print $0;
}
else if (station == "lv" && $4 != "-" && $1 != "Power plant" && consumer == "all") {
if (plant == "-1" || $1 == plant) print $0;
}
}' "$csv_file" > "$filtered_file"
if [ ! -s "$filtered_file" ]; then
echo "Error: No data found for the specified parameters."
exit 1
fi
echo "Filtered data saved to $filtered_file"
echo "Cleaning previous builds..."
make -C codeC clean
echo "Compiling the C program..."
make -s -C codeC
if [ ! -f "codeC/bin/main" ]; then
echo "Error: Failed to compile the C program."
exit 1
fi
echo "Executing the C program..."
./codeC/bin/main "$station_type" "$consumer_type" "$plant_id"
if [ $? -ne 0 ]; then
echo "Error: Failed to execute the C program."
exit 1
fi
# If station_type=lv and consumer_type=all, generate graphs
if [[ "$station_type" == "lv" && "$consumer_type" == "all" ]]; then
echo "Generating graphs for LV all..."
top10_data="output/top10_lv_all.csv"
bottom10_data="output/bottom10_lv_all.csv"
script_path="output/plot_lv_all.gp"
chart_output="output/chart_lv_all.png"
if [[ -f "$top10_data" && -f "$bottom10_data" ]]; then
echo "Executing Gnuplot..."
gnuplot "$script_path"
if [ $? -eq 0 ]; then
echo "Graph successfully generated: $chart_output"
else
echo "Error: Failed to generate the graph."
fi
else
echo "Error: Data files for Gnuplot not found."
fi
fi
echo "Processing complete."
end=$(date +%s)
duration=$((end - start))
echo "Execution time: $duration s"