-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetar-o-matic.rb
executable file
·133 lines (107 loc) · 3.51 KB
/
metar-o-matic.rb
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
#!/usr/local/bin/ruby
# -*- coding: utf-8 -*-
#encoding=utf-8
begin
require 'rubygems'
require 'open-uri'
require 'erb'
require 'sanitize'
require 'json'
require 'date'
require 'metar'
require 'open-uri'
class Date
def dayname
DAYNAMES[self.wday]
end
def abbr_dayname
ABBR_DAYNAMES[self.wday]
end
end
KELVIN_TO_CELSIUS = -273.15
@station_code = "KLAF"
def to_fahrenheit (celsius)
celsius = celsius.to_f
return ((celsius * 9/5) + 32)
end
# get the KLAF station's report
noaain = Metar::Raw::Noaa.new(@station_code)
metp = Metar::Parser.new(noaain)
puts noaain.to_s
metp.temperature.to_s.match(/^(\d+)°(C|F)$/) {|m|
if $2 == "C"
celsius = $1.to_f
fahrenheit = (($1.to_f * 9/5) + 32)
kelvin = (celsius + -KELVIN_TO_CELSIUS)
@temperature_short = celsius.to_s
@temperature_string = (celsius.to_s + " °C (" + fahrenheit.to_s + " °F) (" + kelvin.to_s + " °K)").gsub("°", "°")
elsif $2 == "F"
celsius = (($1.to_f - 32) * 5/9)
fahrenheit = $1.to_f
kelvin = (celsius + -KELVIN_TO_CELSIUS)
@temperature_short = celsius.to_s
@temperature_string = (celsius.to_s + " °C (" + fahrenheit.to_s + " °F) (" + kelvin.to_s + " °K)").gsub("°", "°")
else
puts "Temperature FAIL"
end
}
@scstring = ""
if metp.sky_conditions.first != "clear skies"
metp.sky_conditions.each do |sc|
@scstring += sc.to_summary + "; "
end
else
@scsctring += metp.sky_conditions
end
puts "SC " + @scstring
if (metp.wind.direction != :variable_direction)
puts "!variable_direction"
if (metp.wind.direction.value == 0 && !metp.variable_wind) && metp.wind.speed.to_kilometers_per_hour == 0 && !metp.wind.gusts
puts "Winds are reporting calm"
else
puts "Winds from " + metp.wind.direction.to_s + " at " + metp.wind.speed.to_kilometers_per_hour.floor.to_s + " km/h (" + metp.wind.speed.to_miles_per_hour.floor.to_s + " mph)" + (metp.wind.gusts ? " gusting to " + metp.wind.gusts.to_kilometers_per_hour.floor.to_s + " km/h (" + metp.wind.gusts.to_miles_per_hour.floor.to_s + " mph)" : "")
end
@wind_text = metp.wind.direction.to_s(:abbreviated => true).gsub("°","°") + " (from " + metp.wind.direction.to_compass + ")"
@wind_direction = metp.wind.direction.to_s(:abbreviated => true).gsub("°","deg")
else
puts "variable_direction"
@wind_text = "?° (from ?)"
@wind_direction = "?"
end
puts "Altimeter " + metp.sea_level_pressure.to_inches_of_mercury.to_s
# FORECAST
raw_data = open("http://api.openweathermap.org/data/2.5/forecast/daily?q=West,Lafayette&mode=json").read
forecast_json_data = JSON.parse(raw_data)
@forecast = []
forecast_json_data["list"].each do |day|
d = {}
d[:dayname] = DateTime.strptime(day["dt"].to_s,'%s').dayname
day_weather_items = []
day["weather"].each do |weather_item|
day_weather_items << weather_item["main"]
end
d[:weather_items] = day_weather_items
d[:high] = (day["temp"]["max"] + KELVIN_TO_CELSIUS).floor.to_s
d[:low] = (day["temp"]["min"] + KELVIN_TO_CELSIUS).floor.to_s
@forecast << d
end
# start writing the HTML file
@template = ''
puts "Reading template"
File.open('./views/metar-o-matic.html.erb', 'r') do |f|
@template = f.read
end
puts "Making the template now"
template = ERB.new @template
puts "Writing the file"
File.open('./public-metar-o-matic/index.html', 'w') do |f|
f << template.result(binding)
end
rescue => variable
File.open('./error.log','a') do |f|
f << $! << "\n" << variable.backtrace.join("\n") << "\n"
puts $!.to_s
print variable.backtrace.join("\n")
puts
end
end