ADLongwell-usgs-waterdata 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/usgs/gauge.rb +26 -1
  2. data/spec/gauge_spec.rb +10 -3
  3. metadata +1 -1
@@ -2,6 +2,7 @@ module USGS
2
2
 
3
3
  require 'open-uri'
4
4
  require 'date'
5
+ require 'rexml/document'
5
6
 
6
7
  class Gauge
7
8
 
@@ -69,6 +70,18 @@ module USGS
69
70
  @daily_mean_flows
70
71
  end
71
72
 
73
+ # Return the latitude and longitude for this gauge
74
+ def get_lat_lon
75
+ populate_site_data if @lat_lon.nil? or @lat_lon.empty?
76
+ @lat_lon
77
+ end
78
+
79
+ # Return the site name for this gauge
80
+ def get_site_name
81
+ populate_site_data if @site_name.nil? or @site_name.empty?
82
+ @site_name
83
+ end
84
+
72
85
  private
73
86
 
74
87
  # Populate daily data for a gauge from the USGS site
@@ -89,7 +102,6 @@ module USGS
89
102
  begin_dt, end_dt = end_dt, begin_dt if end_dt < begin_dt
90
103
 
91
104
  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
93
105
  mean_flows_hash = {}
94
106
  open(url).each do |line|
95
107
  next if line =~ /^#/
@@ -165,6 +177,19 @@ module USGS
165
177
  @statistical_percentile20_flows = percentile20_flows_hash.sort
166
178
  @statistical_percentile80_flows = percentile80_flows_hash.sort
167
179
  end
180
+
181
+ def populate_site_data
182
+
183
+ url = "http://waterservices.usgs.gov/NWISQuery/GetDV1?SiteNum=#{@site_number}&ParameterCode=00060&StatisticCode=00003&StartDate=2000-11-16&EndDate=2000-11-17&action=Submit"
184
+ doc = REXML::Document.new(open(url))
185
+
186
+ @lat_lon = [0.00, 0.00]
187
+ REXML::XPath.each(doc, "//latitude") {|el| @lat_lon[0] = el.text.to_f}
188
+ REXML::XPath.each(doc, "//longitude") {|el| @lat_lon[1] = el.text.to_f}
189
+
190
+ @site_name = ""
191
+ REXML::XPath.each(doc, "//siteName") {|el| @site_name = el.text}
192
+ end
168
193
  end
169
194
  end
170
195
 
@@ -42,7 +42,7 @@ describe USGS::Gauge do
42
42
  gauge = USGS::Gauge.new(13336500)
43
43
  puts "Latest flow for the Selway River is #{gauge.latest_flow} CFS"
44
44
  end
45
-
45
+
46
46
  it 'should get daily mean flows for a river' do
47
47
  gauge = USGS::Gauge.new(13336500)
48
48
  daily_mean_flows = gauge.get_daily_mean_flows
@@ -106,7 +106,7 @@ describe USGS::Gauge do
106
106
  end
107
107
  end
108
108
 
109
- it 'should handle a date that has not yet occured' do
109
+ it 'should handle a date that has not yet occured' do
110
110
  gauge = USGS::Gauge.new(13336500)
111
111
  gauge.should_not be_nil
112
112
  begin_dt = Date.new(Date.today.year, Date.today.month + 1, Date.today.day)
@@ -119,7 +119,7 @@ describe USGS::Gauge do
119
119
  end
120
120
  end
121
121
 
122
- it 'should handle a out-of-range, switched data ' do
122
+ it 'should handle out-of-range, switched data ' do
123
123
  gauge = USGS::Gauge.new(13336500)
124
124
  gauge.should_not be_nil
125
125
  begin_dt = Date.new(Date.today.year, Date.today.month + 1, Date.today.day)
@@ -133,5 +133,12 @@ describe USGS::Gauge do
133
133
  end
134
134
  end
135
135
 
136
+ it 'should obtain site information' do
137
+ gauge = USGS::Gauge.new(13336500)
138
+ latlon = gauge.get_lat_lon
139
+ site_name = gauge.get_site_name
140
+ puts "The latitude and longitude for the gauge at #{site_name} is #{latlon[0]}, #{latlon[1]}"
141
+ end
142
+
136
143
  end
137
144
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ADLongwell-usgs-waterdata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Longwell