ADLongwell-usgs-waterdata 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/usgs/gauge.rb +27 -5
- data/spec/gauge_spec.rb +100 -14
- metadata +1 -1
data/lib/usgs/gauge.rb
CHANGED
@@ -58,22 +58,44 @@ module USGS
|
|
58
58
|
|
59
59
|
# Returns a 2-dimensional array of the daily mean flows for this
|
60
60
|
# gauge on days for which data exists, sorted by date.
|
61
|
-
def get_daily_mean_flows
|
62
|
-
|
61
|
+
def get_daily_mean_flows(begin_dt = Date.new(1880, 1, 1), end_dt=nil)
|
62
|
+
|
63
|
+
end_dt = begin_dt if end_dt.nil?
|
64
|
+
begin_dt, end_dt = end_dt, begin_dt if end_dt < begin_dt
|
65
|
+
|
66
|
+
populate_daily_data(begin_dt, end_dt) if @daily_mean_flows.nil? or @daily_mean_flows.empty?
|
67
|
+
populate_daily_data(begin_dt, end_dt) if (begin_dt < @daily_mean_flows.first[0] or end_dt > @daily_mean_flows.last[0])
|
68
|
+
|
63
69
|
@daily_mean_flows
|
64
70
|
end
|
65
71
|
|
66
72
|
private
|
67
73
|
|
68
74
|
# Populate daily data for a gauge from the USGS site
|
69
|
-
def populate_daily_data
|
70
|
-
|
75
|
+
def populate_daily_data(begin_dt = Date.new(1880, 1, 1), end_dt=nil)
|
76
|
+
|
77
|
+
oldest_dt = Date.new(1880, 1, 1)
|
78
|
+
newest_dt = Date.today
|
79
|
+
|
80
|
+
begin_dt = oldest_dt if begin_dt < oldest_dt
|
81
|
+
begin_dt = newest_dt if begin_dt > newest_dt
|
82
|
+
|
83
|
+
end_dt = begin_dt if end_dt.nil?
|
84
|
+
end_dt = oldest_dt if end_dt < oldest_dt
|
85
|
+
|
86
|
+
end_dt = oldest_dt if end_dt < oldest_dt
|
87
|
+
end_dt = newest_dt if end_dt > newest_dt
|
88
|
+
|
89
|
+
begin_dt, end_dt = end_dt, begin_dt if end_dt < begin_dt
|
90
|
+
|
91
|
+
url = "http://waterdata.usgs.gov/nwis/dv?site_no=#{@site_number}&cb_00060=on&begin_date=#{begin_dt.strftime("%Y-%m-%d")}&end_date=#{end_dt.strftime("%Y-%m-%d")}&format=rdb"
|
92
|
+
puts url
|
71
93
|
mean_flows_hash = {}
|
72
94
|
open(url).each do |line|
|
73
95
|
next if line =~ /^#/
|
74
96
|
next if line =~ /^5/
|
75
97
|
next if line =~ /^agency/
|
76
|
-
|
98
|
+
|
77
99
|
field_array = line.split(/\t/)
|
78
100
|
date_array = field_array[2].split('-')
|
79
101
|
date = Date.new(date_array[0].to_i, date_array[1].to_i, date_array[2].to_i)
|
data/spec/gauge_spec.rb
CHANGED
@@ -2,49 +2,135 @@ require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
|
|
2
2
|
|
3
3
|
describe USGS::Gauge do
|
4
4
|
|
5
|
-
it 'should get
|
5
|
+
it 'should get 20th percentile flow data for a river' do
|
6
6
|
gauge = USGS::Gauge.new(13336500)
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
percentile20_flow = gauge.get_statistical_percentile20_flows
|
8
|
+
puts "20th percentile flow for the Selway River:"
|
9
|
+
percentile20_flow.each do |record|
|
10
|
+
puts "\t#{record[0].strftime("%d %b")} = #{record[1]} CFS"
|
10
11
|
end
|
11
12
|
end
|
12
13
|
|
13
|
-
it 'should get median flow data for a river' do
|
14
|
+
it 'should get 50th (median) percentile flow data for a river' do
|
14
15
|
gauge = USGS::Gauge.new(13336500)
|
15
16
|
median_flow = gauge.get_statistical_median_flows
|
17
|
+
puts "50th percentile (median) flow for the Selway River:"
|
16
18
|
median_flow.each do |record|
|
17
|
-
puts "
|
19
|
+
puts "\t#{record[0].strftime("%d %b")} = #{record[1]} CFS"
|
18
20
|
end
|
19
21
|
end
|
20
22
|
|
21
23
|
it 'should get 80th percentile flow data for a river' do
|
22
24
|
gauge = USGS::Gauge.new(13336500)
|
23
25
|
percentile80_flow = gauge.get_statistical_percentile80_flows
|
26
|
+
puts "80th percentile flow for the Selway River:"
|
24
27
|
percentile80_flow.each do |record|
|
25
|
-
puts "
|
28
|
+
puts "\t#{record[0].strftime("%d %b")} = #{record[1]} CFS"
|
26
29
|
end
|
27
30
|
end
|
28
31
|
|
29
|
-
it 'should get
|
32
|
+
it 'should get mean flow data for a river' do
|
30
33
|
gauge = USGS::Gauge.new(13336500)
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
+
mean_flow = gauge.get_statistical_mean_flows
|
35
|
+
puts "Mean flow for the Selway River:"
|
36
|
+
mean_flow.each do |record|
|
37
|
+
puts "\t#{record[0].strftime("%d %b")} = #{record[1]} CFS"
|
34
38
|
end
|
35
39
|
end
|
36
40
|
|
41
|
+
it 'should get the current flow for a river' do
|
42
|
+
gauge = USGS::Gauge.new(13336500)
|
43
|
+
puts "Latest flow for the Selway River is #{gauge.latest_flow} CFS"
|
44
|
+
end
|
45
|
+
|
37
46
|
it 'should get daily mean flows for a river' do
|
38
47
|
gauge = USGS::Gauge.new(13336500)
|
39
48
|
daily_mean_flows = gauge.get_daily_mean_flows
|
49
|
+
puts "Daily mean flow for the Selway River"
|
40
50
|
daily_mean_flows.each do |record|
|
41
|
-
puts "
|
51
|
+
puts "\t #{record[0].strftime("%d %b %Y")} = #{record[1]} CFS"
|
42
52
|
end
|
43
53
|
end
|
44
54
|
|
45
|
-
it 'should get
|
55
|
+
it 'should get daily mean flows for a single date' do
|
46
56
|
gauge = USGS::Gauge.new(13336500)
|
47
|
-
|
57
|
+
gauge.should_not be_nil
|
58
|
+
begin_dt = Date.new(1997,5,31)
|
59
|
+
daily_mean_flows = gauge.get_daily_mean_flows(begin_dt)
|
60
|
+
daily_mean_flows.should_not be_nil
|
61
|
+
daily_mean_flows.size.should be(1)
|
62
|
+
puts "Daily mean flow for the Selway River"
|
63
|
+
daily_mean_flows.each do |record|
|
64
|
+
puts "\t #{record[0].strftime("%d %b %Y")} = #{record[1]} CFS"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'should get daily mean flows for a date range' do
|
69
|
+
gauge = USGS::Gauge.new(13336500)
|
70
|
+
gauge.should_not be_nil
|
71
|
+
begin_dt = Date.new(1997,3,31)
|
72
|
+
end_dt = Date.new(1997,7,31)
|
73
|
+
daily_mean_flows = gauge.get_daily_mean_flows(begin_dt, end_dt)
|
74
|
+
daily_mean_flows.should_not be_nil
|
75
|
+
daily_mean_flows.size.should be(123)
|
76
|
+
puts "Daily mean flow for the Selway River"
|
77
|
+
daily_mean_flows.each do |record|
|
78
|
+
puts "\t #{record[0].strftime("%d %b %Y")} = #{record[1]} CFS"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'should handle switched dates' do
|
83
|
+
gauge = USGS::Gauge.new(13336500)
|
84
|
+
gauge.should_not be_nil
|
85
|
+
begin_dt = Date.new(1997,7,31)
|
86
|
+
end_dt = Date.new(1997,3,31)
|
87
|
+
daily_mean_flows = gauge.get_daily_mean_flows(begin_dt, end_dt)
|
88
|
+
daily_mean_flows.should_not be_nil
|
89
|
+
daily_mean_flows.size.should be(123)
|
90
|
+
puts "Daily mean flow for the Selway River"
|
91
|
+
daily_mean_flows.each do |record|
|
92
|
+
puts "\t #{record[0].strftime("%d %b %Y")} = #{record[1]} CFS"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'should handle a date that is too old' do
|
97
|
+
gauge = USGS::Gauge.new(13336500)
|
98
|
+
gauge.should_not be_nil
|
99
|
+
begin_dt = Date.new(1797,3,31)
|
100
|
+
daily_mean_flows = gauge.get_daily_mean_flows(begin_dt)
|
101
|
+
daily_mean_flows.should_not be_nil
|
102
|
+
daily_mean_flows.size.should be(1)
|
103
|
+
puts "Daily mean flow for the Selway River"
|
104
|
+
daily_mean_flows.each do |record|
|
105
|
+
puts "\t #{record[0].strftime("%d %b %Y")} = #{record[1]} CFS"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'should handle a date that has not yet occured' do
|
110
|
+
gauge = USGS::Gauge.new(13336500)
|
111
|
+
gauge.should_not be_nil
|
112
|
+
begin_dt = Date.new(Date.today.year, Date.today.month + 1, Date.today.day)
|
113
|
+
daily_mean_flows = gauge.get_daily_mean_flows(begin_dt)
|
114
|
+
daily_mean_flows.should_not be_nil
|
115
|
+
daily_mean_flows.size.should be(1)
|
116
|
+
puts "Daily mean flow for the Selway River"
|
117
|
+
daily_mean_flows.each do |record|
|
118
|
+
puts "\t #{record[0].strftime("%d %b %Y")} = #{record[1]} CFS"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'should handle a out-of-range, switched data ' do
|
123
|
+
gauge = USGS::Gauge.new(13336500)
|
124
|
+
gauge.should_not be_nil
|
125
|
+
begin_dt = Date.new(Date.today.year, Date.today.month + 1, Date.today.day)
|
126
|
+
end_dt = Date.new(1797,3,31)
|
127
|
+
daily_mean_flows = gauge.get_daily_mean_flows(begin_dt, end_dt)
|
128
|
+
daily_mean_flows.should_not be_nil
|
129
|
+
daily_mean_flows.size.should be(35619)
|
130
|
+
puts "Daily mean flow for the Selway River"
|
131
|
+
daily_mean_flows.each do |record|
|
132
|
+
puts "\t #{record[0].strftime("%d %b %Y")} = #{record[1]} CFS"
|
133
|
+
end
|
48
134
|
end
|
49
135
|
|
50
136
|
end
|