-
Notifications
You must be signed in to change notification settings - Fork 0
/
upper_colorado.php
executable file
·172 lines (156 loc) · 5.56 KB
/
upper_colorado.php
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
164
165
166
167
168
169
170
171
172
<?php
include 'functions.php';
/**
* Upper Colorado Basin Reservoirs
* http://www.usbr.gov/uc/water/rsvrs/ops/r40day.html
*/
//$base = "http://www.usbr.gov/uc/water/rsvrs/ops/crsp_40_";
$reservoirs = array(
"Blue Mesa Reservoir" => array(940800, 'CO'),
"Big Sandy Reservoir" => array(38300, 'WY'),
// "Brantley Reservoir" => "bt",
"Caballo Reservoir" => array(343990, 'NM'),
"Causey Reservoir" => array(8730, 'UT'),
"Crawford Reservoir" => array(14395, 'CO'),
"Currant Creek Reservoir" => array(15670, 'UT'),
"Crystal Reservoir" => array(17540, 'CO'),
"Deer Creek Reservoir" => array(152570, 'UT'),
"East Canyon Reservoir" => array(51200, 'UT'),
"Echo Reservoir" => array(74000, 'UT'),
// "Eden Reservoir" => "er",
"Elephant Butte Reservoir" => array(2065010, 'NM'),
"Flaming Gorge Reservoir" => array(3788700, 'UT'),
"Fontenelle Reservoir" => array(345360, 'WY'),
"Fruitgrowers Reservoir" => array(4540, 'CO'),
"Huntington North Reservoir" => array(5420, 'UT'),
"Hyrum Reservoir" => array(18685, 'UT'),
"Jackson Gulch Reservoir" => array(9948, 'CO'),
"Joes Valley Reservoir" => array(62460, 'UT'),
"Jordanelle Reservoir" => array(320300, 'UT'),
"Lake Powell" => array(24322000, 'AZ&UT'),
"Lake Sumner Reservoir" => array(43768, 'NM'),
"Lemon Reservoir" => array(39792, 'CO'),
"Lost Creek Reservoir" => array(22510, 'UT'),
// "Lost Lake" => "ll",*/
"McPhee Reservoir" => array(381100, 'CO'),
"Meeks Cabin Reservoir" => array(32470, 'WY'),
"Morrow Point Reservoir" => array(117025, 'CO'),
"Moon Lake Reservoir" => array(49500, 'UT'),
"Navajo Reservoir" => array(1708600, 'CO'),
"Newton Reservoir" => array(5600, 'UT'),
"Paonia Reservoir" => array(20950, 'CO'),
"Pineview Reservoir" => array(110150, 'UT'),
"Red Fleet Reservoir" => array(26000, 'UT'),
"Rifle Gap Reservoir" => array(12168, 'CO'),
"Rockport Reservoir" => array(62100, 'UT'),
"Ridgway Reservoir" => array(84230, 'CO'),
"Santa Rosa Reservoir" => array(717000, 'NM'),
"Scofield Reservoir" => array(73600, 'UT'),
"Silver Jack Reservoir" => array(12820, 'CO'),
"Stateline Reservoir" => array(12000, 'UT'),
"Strawberry Reservoir" => array(1106500, 'UT'),
"Starvation Reservoir" => array(167310, 'UT'),
"Steinaker Reservoir" => array(38173, 'UT'),
"Taylor Park Reservoir" => array(106200, 'CO'),
// "Trial Lake" => "tl",
"Upper Stillwater Reservoir" => array(33123, 'UT'),
"Vallecito Reservoir" => array(125400, 'CO'),
"Vega Reservoir" => array(33800, 'CO'),
// "Washington Lake" => "wl",
// "Willard Bay Reservoir" => "wb"
);
foreach($reservoirs as $key => $reservoir) {
$file_base = str_replace(' ', '_', strtolower($key));
$fc = fopen("data/uc_m/$file_base.csv", "wb");
fputcsv($fc, array('reservoir', 'storage', 'capacity', 'date', 'state'));
$file = "data/uc/$file_base.csv";
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if(!preg_match('/\d{4}/', $data[0]) || empty($data[1])) continue;
$res_name = trim(str_replace('Reservoir', '', $key));
$format_date = date_formatting($data[0]);
fputcsv($fc, array($res_name, $data[1], $reservoir[0], $format_date, $reservoir[1]));
}
fclose($handle);
}
fclose($fc);
}
function date_formatting($date) {
$dates = explode('-', $date);
$month = months($dates[1]);
return $month . "/" . $dates[2];
}
function months($date) {
switch($date) {
case 'Jan':
return '01';
break;
case 'Feb':
return '02';
break;
case 'Mar':
return '03';
break;
case 'Apr':
return '04';
break;
case 'May':
return '05';
break;
case 'Jun':
return '06';
break;
case 'Jul':
return '07';
break;
case 'Aug':
return '08';
break;
case 'Sep':
return '09';
break;
case 'Oct':
return '10';
break;
case 'Nov':
return '11';
break;
case 'Dec':
return '12';
break;
}
}
$path = 'data/uc_m';
$files = scandir($path);
foreach($files as $file) {
if(!is_dir($file) && !preg_match('/^\./', $file)) {
$row = 1;
$res = '';
$capacity = '';
$state = '';
if (($handle = fopen($path . '/' . $file, "r")) !== FALSE) {
$months = array();
$months_list = array();
$fh = fopen('data/uc_mf/' . $file, 'wb');
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($row == 1) {
fputcsv($fh, array('reservoir', 'storage', 'capacity', 'pct_capacity', 'date', 'state'));
} else {
$months[$data[3]][] = $data[1];
}
$res = $data[0];
$capacity = $data[2];
$state = $data[4];
$row++;
}
foreach($months as $key => $month) {
$monthly_avg = round(array_sum($month) / count($month));
$monthly_avg_pct = round(($monthly_avg / $capacity) * 100, 1);
fputcsv($fh, array($res, $monthly_avg, $capacity, $monthly_avg_pct, $key, $state));
}
echo $res . "\n";
fclose($fh);
fclose($handle);
}
}
}