Skip to content

Commit

Permalink
fix unit conversion error
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbradford committed Jul 27, 2023
1 parent 248e457 commit 9b8b6a1
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/map_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,5 @@ function updateSelector(lat, lng) {
lng = lng.toFixed(1)
$('#latitude').val(lat)
$('#longitude').val(lng)
console.log("Clicked on " + lat + "," + lng)
console.log(`Clicked on ${lat}, ${lng}`)
}
7 changes: 6 additions & 1 deletion app/controllers/weather_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ def et
data = response[:data].collect do |key, value|
key = JSON.parse(key.to_s)
value ||= 0.0
{latitude: key[0], longitude: key[1], et_mm: value, et_in: value * 25.4}
{
latitude: key[0],
longitude: key[1],
et_mm: value,
et_in: helpers.mm_to_in(value)
}
end
send_data(to_csv(data, headers), filename:)
}
Expand Down
26 changes: 22 additions & 4 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,36 @@ module ApplicationHelper
FROST_COLOR = "blue"
FREEZE_COLOR = "#c5050c"

# unit conversions
def c_to_f(c)
return if c.nil? || !(f.is_a? Numeric)
c * (9.0 / 5) + 32
c * 1.8 + 32.0
rescue
end

def f_to_c(f)
return if f.nil? || !(f.is_a? Numeric)
(f - 32) * (5.0 / 9)
(f - 32.0) * (5.0 / 9.0)
rescue
end

def in_to_mm(inches)
inches * 25.4
rescue
end

def mm_to_in(mm)
mm / 25.4
rescue
end

def mj_to_kwh(mj)
mj / 3.6
rescue
end

# formatters
def fmt_num(num, digits = 0)
return num || "" unless num.is_a? Numeric
return 0 if num.zero?
sprintf("%.#{digits}f", num.round(digits))
end

Expand Down
5 changes: 0 additions & 5 deletions app/models/unit_converter.rb

This file was deleted.

4 changes: 2 additions & 2 deletions app/views/weather/_data_tbl_et.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
<td><%= day[:date] %></td>
<td><%= fmt_num(day[:value], 3) %></td>
<td><%= fmt_num(day[:cumulative_value], 3) %></td>
<td><%= fmt_num(day[:value] * 2.54, 3) unless day[:value].nil? %></td>
<td><%= fmt_num(day[:cumulative_value] * 2.45, 3) unless day[:cumulative_value].nil? %></td>
<td><%= fmt_num(in_to_mm(day[:value]), 3) unless day[:value].nil? %></td>
<td><%= fmt_num(in_to_mm(day[:cumulative_value]), 3) unless day[:cumulative_value].nil? %></td>
</tr>
<% end %>
</tbody>
Expand Down
4 changes: 2 additions & 2 deletions app/views/weather/_data_tbl_insol.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<td><%= i + 1 %></td>
<td><%= day[:date] %></td>
<td><%= fmt_num(day[:value], 2) %></td>
<td><%= fmt_num(day[:value] / 3.6, 2) unless day[:value].nil? %></td>
<td><%= fmt_num(mj_to_kwh(day[:value]), 2) unless day[:value].nil? %></td>
</tr>
<% end %>
</tbody>
Expand All @@ -32,7 +32,7 @@
<td></td>
<td><%= k %>:</td>
<td><%= fmt_num(v, 2) %></td>
<td><%= fmt_num(v / 3.6, 2) unless v.nil? %></td>
<td><%= fmt_num(mj_to_kwh(v), 2) unless v.nil? %></td>
</tr>
<% end %>
</tfoot>
Expand Down
4 changes: 2 additions & 2 deletions app/views/weather/_data_tbl_precip.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
<td><%= day[:date] %></td>
<td><%= fmt_num(day[:value], 2) %></td>
<td><%= fmt_num(day[:cumulative_value], 2) %></td>
<td><%= fmt_num(day[:value] / 25.4, 3) unless day[:value].nil? %></td>
<td><%= fmt_num(day[:cumulative_value] / 25.4, 3) unless day[:cumulative_value].nil? %></td>
<td><%= fmt_num(mm_to_in(day[:value]), 3) unless day[:value].nil? %></td>
<td><%= fmt_num(mm_to_in(day[:cumulative_value]), 3) unless day[:cumulative_value].nil? %></td>
</tr>
<% end %>
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion app/views/weather/precip.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<% if @start_date %>
<h4 id="map">Cumulative precipitation map for <%= @start_date.strftime("%b %-d") %> to <%= @date.strftime("%b %-d") %> (<%= pluralize((@start_date..@date).count, "day") %>)</h4>
<% else %>
<h4 id="map">Precipiation map for <%= @date.strftime("%b %-d") %> (<%= pluralize((Date.current - @date).to_i, "day") %> ago)</h4>
<h4 id="map">Precipitation map for <%= @date.strftime("%b %-d") %> (<%= pluralize((Date.current - @date).to_i, "day") %> ago)</h4>
<% end %>
<%= render_async url_for(action: :map_image),
Expand Down

0 comments on commit 9b8b6a1

Please sign in to comment.