knmi 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+ rcov (0.9.9)
11
+ shoulda (2.11.3)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.5.2)
19
+ rcov
20
+ shoulda
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 bullfight
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,50 @@
1
+ = knmi
2
+
3
+ Ruby API to access ROYAL NETHERLANDS METEOROLOGICAL INSTITUTE daily climate data
4
+
5
+ Access climatalogical data as provided by the ROYAL NETHERLANDS METEOROLOGICAL INSTITUTE
6
+ through their http get form details of data here (http://www.knmi.nl/climatology/daily_data/scriptxs-en.html) and
7
+ station list here http://www.knmi.nl/climatology/daily_data/scriptxs-en.html, data is parsed into a array of hashes
8
+ with keys for each element { [ "STN" => 210, "YYYMMDD" => 20110427, "TG" => 25 ] }
9
+
10
+ = List of Recorded Parameters and group name
11
+
12
+ = Method calls
13
+ station_number = number or array of numbers eg 210 or [210, 225]
14
+ vars = optional parameters to select measure parameters of interest
15
+ if not defined all recorded parameters will be returned
16
+ response = KNMI.get_station(station_number, vars)
17
+ this will return all daily values from the begining of the month to current
18
+ start = date in the form YYYYMMDD
19
+ end = date in the form YYYYMMDD
20
+
21
+
22
+ response = KNMI.get_station(station_number, vars)
23
+ this will return all daily values from the begining of the month to current for selected vars
24
+
25
+ response = KNMI.get_station_start_to_current(station_number, start, vars)
26
+ this will return all daily values from defined start date to current
27
+
28
+ response = KNMI.get_station_range(station_number, start, end, vars)
29
+ this will return all daily values from defined start date to defined end date
30
+
31
+ response = KNMI.get_station_seasonal(station_number, start, end, vars)
32
+ this will return all daily values from seasonally defined start date to defined end date
33
+ eg start = 19800123, end = 19840427 will return data between January 23 and April 27th for 1980 to 1984
34
+
35
+
36
+ == Contributing to knmi
37
+
38
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
39
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
40
+ * Fork the project
41
+ * Start a feature/bugfix branch
42
+ * Commit and push until you are happy with your contribution
43
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
44
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
45
+
46
+ == Copyright
47
+
48
+ Copyright (c) 2011 bullfight. See LICENSE.txt for
49
+ further details.
50
+
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "knmi"
16
+ gem.homepage = "http://github.com/bullfight/knmi"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Ruby API to access ROYAL NETHERLANDS METEOROLOGICAL INSTITUTE daily climate data}
19
+ gem.description = %Q{Access climatalogical data as provided by the ROYAL NETHERLANDS METEOROLOGICAL INSTITUTE
20
+ through their http get form details of data here (http://www.knmi.nl/climatology/daily_data/scriptxs-en.html) and
21
+ station list here http://www.knmi.nl/climatology/daily_data/scriptxs-en.html, data is parsed into a array of hashes
22
+ with keys for each element { [ "STN" => 210, "YYYMMDD" => 20110427, "TG" => 25 ] } }
23
+ gem.email = "p.schmitz@gmail.com"
24
+ gem.authors = ["bullfight"]
25
+ gem.add_runtime_dependency 'httparty', '>= 0.7.4'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "knmi #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{knmi}
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["bullfight"]
12
+ s.date = %q{2011-04-27}
13
+ s.description = %q{Access climatalogical data as provided by the ROYAL NETHERLANDS METEOROLOGICAL INSTITUTE
14
+ through their http get form details of data here (http://www.knmi.nl/climatology/daily_data/scriptxs-en.html) and
15
+ station list here http://www.knmi.nl/climatology/daily_data/scriptxs-en.html, data is parsed into a array of hashes
16
+ with keys for each element { [ "STN" => 210, "YYYMMDD" => 20110427, "TG" => 25 ] } }
17
+ s.email = %q{p.schmitz@gmail.com}
18
+ s.extra_rdoc_files = [
19
+ "LICENSE.txt",
20
+ "README.rdoc"
21
+ ]
22
+ s.files = [
23
+ ".document",
24
+ "Gemfile",
25
+ "Gemfile.lock",
26
+ "LICENSE.txt",
27
+ "README.rdoc",
28
+ "Rakefile",
29
+ "VERSION",
30
+ "knmi.gemspec",
31
+ "lib/knmi.rb",
32
+ "test/helper.rb",
33
+ "test/test_knmi.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/bullfight/knmi}
36
+ s.licenses = ["MIT"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.7.2}
39
+ s.summary = %q{Ruby API to access ROYAL NETHERLANDS METEOROLOGICAL INSTITUTE daily climate data}
40
+ s.test_files = [
41
+ "test/helper.rb",
42
+ "test/test_knmi.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
50
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
51
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
52
+ s.add_development_dependency(%q<rcov>, [">= 0"])
53
+ s.add_runtime_dependency(%q<httparty>, [">= 0.7.4"])
54
+ else
55
+ s.add_dependency(%q<shoulda>, [">= 0"])
56
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
57
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
58
+ s.add_dependency(%q<rcov>, [">= 0"])
59
+ s.add_dependency(%q<httparty>, [">= 0.7.4"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<shoulda>, [">= 0"])
63
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
64
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
65
+ s.add_dependency(%q<rcov>, [">= 0"])
66
+ s.add_dependency(%q<httparty>, [">= 0.7.4"])
67
+ end
68
+ end
69
+
@@ -0,0 +1,181 @@
1
+ require 'httparty'
2
+ require 'csv'
3
+
4
+ class KNMI
5
+ include HTTParty
6
+ base_uri 'http://www.knmi.nl/climatology/daily_data/getdata_day.cgi'
7
+
8
+ class << self
9
+ private :new
10
+
11
+ # station1:station2:station15 requires station or list of stations NO DEFAULT
12
+ def station(station_number)
13
+ if station_number.nil? == true
14
+ raise "Station Number Required" # doesn't work look into this
15
+ elsif station_number.kind_of?(Array)
16
+ "stns=#{station_number * ":"}"
17
+ else
18
+ "stns=#{station_number}"
19
+ end
20
+ end
21
+
22
+ def check_stations
23
+ # stubbed out complete later
24
+ end
25
+
26
+ # YYYYMMDD Default is first day of current month
27
+ def start_date(_start)
28
+ "start=#{_start}"
29
+ end
30
+
31
+ # YYYYMMDD Default is current or last recorded day
32
+ def end_date(_end)
33
+ "end=#{_end}"
34
+ end
35
+
36
+ # Variable Options
37
+ #WIND = DDVEC:FG:FHX:FHX:FX wind
38
+ #TEMP = TG:TN:TX:T10N temperature
39
+ #SUNR = SQ:SP:Q Sunshine and global radiation
40
+ #PRCP = DR:RH:EV24 precipitation and potential evaporation
41
+ #PRES = PG:PGX:PGN sea-level pressure
42
+ #VICL = VVN:VVX:NG visibility and cloud cover
43
+ #MSTR = UG:UX:UN humidity
44
+ #ALL = all variables including those not in a collection
45
+ def variables(vars)
46
+
47
+ if vars.empty? == true
48
+ "vars=ALL"
49
+ else
50
+ vars, nvars = check_variables(vars)
51
+ "vars=#{vars * ":"}"
52
+ end
53
+ end
54
+
55
+ # Delete Invalid Variables and keep those valid, if none are valid replace with all
56
+ def check_variables(vars)
57
+
58
+ if vars.kind_of?(Array) == false
59
+ vars = [vars]
60
+ end
61
+
62
+ # Collections of variables and all Possible Variables
63
+ varOpts = {
64
+ "WIND" => ["DDVEC", "FG", "FHX", "FHX", "FX"],
65
+ "TEMP" => ["TG", "TN", "TX", "T10N"],
66
+ "SUNR" => ["SQ", "SP", "Q"],
67
+ "PRCP" => ["DR", "RH", "EV24"],
68
+ "PRES" => ["PG", "PGX", "PGN"],
69
+ "VICL" => ["VVN", "VVX", "NG"],
70
+ "MSTR" => ["VVN", "VVX", "NG"],
71
+ "ALL" => ["DDVEC", "FHVEC", "FG", "FHX",
72
+ "FHXH", "FHN", "FHNH", "FXX", "FXXH", "TG", "TN",
73
+ "TNH", "TX", "TXH", "T10N", "T10NH", "SQ", "SP",
74
+ "Q", "DR", "RH", "RHX", "RHXH", "EV24", "PG", "PX",
75
+ "PXH", "PN", "PNH", "VVN", "VVNH", "VVX", "VVXH",
76
+ "NG", "UG", "UX", "UXH", "UN", "UNH"]
77
+ }
78
+
79
+ # Drop in invalid vars
80
+ vars = ( vars & varOpts.to_a.flatten )
81
+
82
+ # Only Allow Variable to be selected once, All, or TEMP, not ALL & TEMP
83
+ # And count the number of selected variables
84
+ if vars.include?("ALL") == true or vars.empty? == true
85
+ vars = "ALL"
86
+ nvars = varOpts["ALL"].count
87
+ elsif (vars & varOpts.keys) # check if contains keys
88
+
89
+ x = []
90
+ (vars & varOpts.keys).each do |k|
91
+ x << varOpts.values_at(k)
92
+ end
93
+ vars = (vars - x.flatten)
94
+
95
+ nvars = 0
96
+ ( vars & varOpts.keys ).each do |v|
97
+ nvars += varOpts[v].count
98
+ end
99
+
100
+ ( vars - varOpts.keys ).each do |v|
101
+ nvars += 1
102
+ end
103
+ end
104
+
105
+ return vars, nvars
106
+ end
107
+
108
+ # Parse Response into hashed arrays
109
+ #Other elements
110
+ #varlist = res[(7+nstn)..(6 + nstn + nvars)]
111
+ #colheader = res[(8 + nstn + nvars)]
112
+ #header = res[0..(9 + nstn + nvars)]
113
+ #stations = res[5..(4+nstn)]
114
+ #stations = stations.join.tr("\s+", "")
115
+ #stations = stations.tr("#", "")
116
+ #stations = stations.tr(":", "")
117
+ #stations = CSV.parse( stations, {:col_sep => "\t"} )
118
+ def parse(response, station_number, vars)
119
+ # Line Index Numbers
120
+ nstn = [station_number].flatten.length
121
+ vars, nvars = check_variables(vars)
122
+
123
+ # Clean Data remove unecessary chars
124
+ response = response.split(/\n/)
125
+ response = response[(8 + nstn + nvars)..response.length]
126
+ response = response.join.tr("\s+", "")
127
+ response = response.tr("#", "")
128
+
129
+ # Parse into array and then hash with var name
130
+ response = CSV.parse(response, {:skip_blanks => true})
131
+ header = response.shift.map {|i| i.to_s.intern }
132
+ string_data = response.map {|row| row.map {|cell| cell.to_s } }
133
+ data = string_data.map {|row| Hash[*header.zip(row).flatten] }
134
+
135
+ end
136
+ end # End Private
137
+
138
+ # gets variables from station or list of stations
139
+ # with all variables if vars is empty or selected variables passed as an array
140
+ # data is from begining of current month to current day
141
+ # Example
142
+ # station_number = [210, 212]
143
+ # vars = "TG"
144
+ # res = KNMIdaily.get_station( station_number, vars )
145
+ def self.get_station(station_number, vars = "")
146
+ query = station(station_number) + "&" + variables(vars)
147
+ puts query
148
+ res = get("", { :query => query } )
149
+ res = parse(res, station_number, vars)
150
+ end
151
+
152
+ # gets variables from station or list of stations
153
+ # with all variables if vars is empty or selected variables passed as an array
154
+ # data is from start date to current
155
+ def self.get_station_start_to_current(station_number, start, vars = "")
156
+ query = station(station_number) + "&" + start_date(start) + "&" + variables(vars)
157
+ puts query
158
+ res = get("", { :query => query } )
159
+ res = parse(res, station_number, vars)
160
+ end
161
+
162
+ # gets variables from station or list of stations
163
+ # with all variables if vars is empty or selected variables passed as an array
164
+ # data is from start date to end date
165
+ def self.get_station_range(station_number, _start, _end, vars = "")
166
+ query = station(station_number) + "&" + start_date(_start) + "&" + end_date(_end) + "&" + variables(vars)
167
+ puts query
168
+ res = get("", { :query => query } )
169
+ res = parse(res, station_number, vars)
170
+ end
171
+
172
+ # gets variables from station or list of stations
173
+ # with all variables if vars is empty or selected variables passed as an array
174
+ # seasonal data is from start date to end date
175
+ # by selected month and day within each year
176
+ def self.get_seasonal(station_number, _start, _end, vars = "")
177
+ query = station(station_number) + "&" + "inseason=true" + "&" + start_date(_start) + "&" + end_date(_end) + "&" + variables(vars)
178
+ puts query
179
+ get("", { :query => query } )
180
+ end
181
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'knmi'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestKnmi < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knmi
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.0
6
+ platform: ruby
7
+ authors:
8
+ - bullfight
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-27 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: shoulda
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: bundler
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: jeweler
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.5.2
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: rcov
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: httparty
61
+ requirement: &id005 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 0.7.4
67
+ type: :runtime
68
+ prerelease: false
69
+ version_requirements: *id005
70
+ description: "Access climatalogical data as provided by the ROYAL NETHERLANDS METEOROLOGICAL INSTITUTE\n through their http get form details of data here (http://www.knmi.nl/climatology/daily_data/scriptxs-en.html) and\n station list here http://www.knmi.nl/climatology/daily_data/scriptxs-en.html, data is parsed into a array of hashes\n with keys for each element { [ \"STN\" => 210, \"YYYMMDD\" => 20110427, \"TG\" => 25 ] } "
71
+ email: p.schmitz@gmail.com
72
+ executables: []
73
+
74
+ extensions: []
75
+
76
+ extra_rdoc_files:
77
+ - LICENSE.txt
78
+ - README.rdoc
79
+ files:
80
+ - .document
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE.txt
84
+ - README.rdoc
85
+ - Rakefile
86
+ - VERSION
87
+ - knmi.gemspec
88
+ - lib/knmi.rb
89
+ - test/helper.rb
90
+ - test/test_knmi.rb
91
+ homepage: http://github.com/bullfight/knmi
92
+ licenses:
93
+ - MIT
94
+ post_install_message:
95
+ rdoc_options: []
96
+
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: -2581056559700233680
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: "0"
114
+ requirements: []
115
+
116
+ rubyforge_project:
117
+ rubygems_version: 1.7.2
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: Ruby API to access ROYAL NETHERLANDS METEOROLOGICAL INSTITUTE daily climate data
121
+ test_files:
122
+ - test/helper.rb
123
+ - test/test_knmi.rb