Class: KNMI
Class Method Summary collapse
- .check_stations ⇒ Object
-
.check_variables(vars) ⇒ Object
Delete Invalid Variables and keep those valid, if none are valid replace with all.
-
.end_date(_end) ⇒ Object
YYYYMMDD Default is current or last recorded day.
-
.get_seasonal(station_number, _start, _end, vars = "") ⇒ Object
gets variables from station or list of stations with all variables if vars is empty or selected variables passed as an array seasonal data is from start date to end date by selected month and day within each year.
-
.get_station(station_number, vars = "") ⇒ Object
gets variables from station or list of stations with all variables if vars is empty or selected variables passed as an array data is from begining of current month to current day Example station_number = [210, 212] vars = “TG” res = KNMIdaily.get_station( station_number, vars ).
-
.get_station_range(station_number, _start, _end, vars = "") ⇒ Object
gets variables from station or list of stations with all variables if vars is empty or selected variables passed as an array data is from start date to end date.
-
.get_station_start_to_current(station_number, start, vars = "") ⇒ Object
gets variables from station or list of stations with all variables if vars is empty or selected variables passed as an array data is from start date to current.
-
.parse(response, station_number, vars) ⇒ Object
Parse Response into hashed arrays Other elements varlist = res[(7+nstn)..(6 + nstn + nvars)] colheader = res[(8 + nstn + nvars)] header = res[0..(9 + nstn + nvars)] stations = res stations = stations.join.tr(“s+”, “”) stations = stations.tr(“#”, “”) stations = stations.tr(“:”, “”) stations = CSV.parse( stations, => “t” ).
-
.start_date(_start) ⇒ Object
YYYYMMDD Default is first day of current month.
-
.station(station_number) ⇒ Object
station1:station2:station15 requires station or list of stations NO DEFAULT.
- .to_csv(filename, response) ⇒ Object
- .variables(vars) ⇒ Object
Class Method Details
.check_stations ⇒ Object
22 23 24 |
# File 'lib/knmi.rb', line 22 def check_stations # stubbed out complete later end |
.check_variables(vars) ⇒ Object
Delete Invalid Variables and keep those valid, if none are valid replace with all
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 |
# File 'lib/knmi.rb', line 47 def check_variables(vars) if vars.kind_of?(Array) == false vars = [vars] end # Collections of variables and all Possible Variables varOpts = { "WIND" => ["DDVEC", "FG", "FHX", "FHX", "FX"], "TEMP" => ["TG", "TN", "TX", "T10N"], "SUNR" => ["SQ", "SP", "Q"], "PRCP" => ["DR", "RH", "EV24"], "PRES" => ["PG", "PGX", "PGN"], "VICL" => ["VVN", "VVX", "NG"], "MSTR" => ["VVN", "VVX", "NG"], "ALL" => ["DDVEC", "FHVEC", "FG", "FHX", "FHXH", "FHN", "FHNH", "FXX", "FXXH", "TG", "TN", "TNH", "TX", "TXH", "T10N", "T10NH", "SQ", "SP", "Q", "DR", "RH", "RHX", "RHXH", "EV24", "PG", "PX", "PXH", "PN", "PNH", "VVN", "VVNH", "VVX", "VVXH", "NG", "UG", "UX", "UXH", "UN", "UNH"] } # Drop in invalid vars vars = ( vars & varOpts.to_a.flatten ) # Only Allow Variable to be selected once, All, or TEMP, not ALL & TEMP # And count the number of selected variables if vars.include?("ALL") == true or vars.empty? == true vars = "ALL" nvars = varOpts["ALL"].count elsif (vars & varOpts.keys) # check if contains keys x = [] (vars & varOpts.keys).each do |k| x << varOpts.values_at(k) end vars = (vars - x.flatten) nvars = 0 ( vars & varOpts.keys ).each do |v| nvars += varOpts[v].count end ( vars - varOpts.keys ).each do |v| nvars += 1 end end return vars, nvars end |
.end_date(_end) ⇒ Object
YYYYMMDD Default is current or last recorded day
32 33 34 |
# File 'lib/knmi.rb', line 32 def end_date(_end) "end=#{_end}" end |
.get_seasonal(station_number, _start, _end, vars = "") ⇒ Object
gets variables from station or list of stations with all variables if vars is empty or selected variables passed as an array seasonal data is from start date to end date by selected month and day within each year
167 168 169 170 171 |
# File 'lib/knmi.rb', line 167 def self.get_seasonal(station_number, _start, _end, vars = "") query = station(station_number) + "&" + "inseason=true" + "&" + start_date(_start) + "&" + end_date(_end) + "&" + variables(vars) puts query get("", { :query => query } ) end |
.get_station(station_number, vars = "") ⇒ Object
gets variables from station or list of stations with all variables if vars is empty or selected variables passed as an array data is from begining of current month to current day Example station_number = [210, 212] vars = “TG” res = KNMIdaily.get_station( station_number, vars )
136 137 138 139 140 141 |
# File 'lib/knmi.rb', line 136 def self.get_station(station_number, vars = "") query = station(station_number) + "&" + variables(vars) puts query res = get("", { :query => query } ) res = parse(res, station_number, vars) end |
.get_station_range(station_number, _start, _end, vars = "") ⇒ Object
gets variables from station or list of stations with all variables if vars is empty or selected variables passed as an array data is from start date to end date
156 157 158 159 160 161 |
# File 'lib/knmi.rb', line 156 def self.get_station_range(station_number, _start, _end, vars = "") query = station(station_number) + "&" + start_date(_start) + "&" + end_date(_end) + "&" + variables(vars) puts query res = get("", { :query => query } ) res = parse(res, station_number, vars) end |
.get_station_start_to_current(station_number, start, vars = "") ⇒ Object
gets variables from station or list of stations with all variables if vars is empty or selected variables passed as an array data is from start date to current
146 147 148 149 150 151 |
# File 'lib/knmi.rb', line 146 def self.get_station_start_to_current(station_number, start, vars = "") query = station(station_number) + "&" + start_date(start) + "&" + variables(vars) puts query res = get("", { :query => query } ) res = parse(res, station_number, vars) end |
.parse(response, station_number, vars) ⇒ Object
Parse Response into hashed arrays Other elements varlist = res[(7+nstn)..(6 + nstn + nvars)] colheader = res[(8 + nstn + nvars)] header = res[0..(9 + nstn + nvars)] stations = res stations = stations.join.tr(“s+”, “”) stations = stations.tr(“#”, “”) stations = stations.tr(“:”, “”) stations = CSV.parse( stations, => “t” )
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/knmi.rb', line 109 def parse(response, station_number, vars) # Line Index Numbers nstn = [station_number].flatten.length vars, nvars = check_variables(vars) # Clean Data remove unecessary chars response = response.split(/\n/) response = response[(8 + nstn + nvars)..response.length] response = response.join.tr("\s+", "") response = response.tr("#", "") # Parse into array and then hash with var name response = CSV.parse(response, {:skip_blanks => true}) header = response.shift.map {|i| i.to_s.intern } string_data = response.map {|row| row.map {|cell| cell.to_s } } data = string_data.map {|row| Hash[*header.zip(row).flatten] } end |
.start_date(_start) ⇒ Object
YYYYMMDD Default is first day of current month
27 28 29 |
# File 'lib/knmi.rb', line 27 def start_date(_start) "start=#{_start}" end |
.station(station_number) ⇒ Object
station1:station2:station15 requires station or list of stations NO DEFAULT
12 13 14 15 16 17 18 19 20 |
# File 'lib/knmi.rb', line 12 def station(station_number) if station_number.nil? == true raise "Station Number Required" # doesn't work look into this elsif station_number.kind_of?(Array) "stns=#{station_number * ":"}" else "stns=#{station_number}" end end |
.to_csv(filename, response) ⇒ Object
173 174 175 176 177 178 179 180 |
# File 'lib/knmi.rb', line 173 def self.to_csv(filename, response) CSV.open(filename, "wb") do |csv| csv << response[0].keys response[(1..(response.length - 1))].each do |line| csv << line.values end end end |
.variables(vars) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/knmi.rb', line 36 def variables(vars) if vars.empty? == true "vars=ALL" else vars, nvars = check_variables(vars) "vars=#{vars * ":"}" end end |