From 9b8b6a1b05b957f329e523d8945781f43656791b Mon Sep 17 00:00:00 2001 From: Ben Bradford Date: Thu, 27 Jul 2023 11:08:55 -0500 Subject: [PATCH] fix unit conversion error --- app/assets/javascripts/map_functions.js | 2 +- app/controllers/weather_controller.rb | 7 +++++- app/helpers/application_helper.rb | 26 +++++++++++++++++---- app/models/unit_converter.rb | 5 ---- app/views/weather/_data_tbl_et.html.erb | 4 ++-- app/views/weather/_data_tbl_insol.html.erb | 4 ++-- app/views/weather/_data_tbl_precip.html.erb | 4 ++-- app/views/weather/precip.html.erb | 2 +- 8 files changed, 36 insertions(+), 18 deletions(-) delete mode 100644 app/models/unit_converter.rb diff --git a/app/assets/javascripts/map_functions.js b/app/assets/javascripts/map_functions.js index 4c8dcac9..c71a6539 100644 --- a/app/assets/javascripts/map_functions.js +++ b/app/assets/javascripts/map_functions.js @@ -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}`) } diff --git a/app/controllers/weather_controller.rb b/app/controllers/weather_controller.rb index 8fb0d5aa..2cbe64de 100644 --- a/app/controllers/weather_controller.rb +++ b/app/controllers/weather_controller.rb @@ -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:) } diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 9d51b093..794f10cb 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/app/models/unit_converter.rb b/app/models/unit_converter.rb deleted file mode 100644 index 92661f4b..00000000 --- a/app/models/unit_converter.rb +++ /dev/null @@ -1,5 +0,0 @@ -module UnitConverter - def c_to_f(c) - c * (9.0 / 5.0) + 32 - end -end diff --git a/app/views/weather/_data_tbl_et.html.erb b/app/views/weather/_data_tbl_et.html.erb index 5ee4c8b7..c1b8efc8 100644 --- a/app/views/weather/_data_tbl_et.html.erb +++ b/app/views/weather/_data_tbl_et.html.erb @@ -28,8 +28,8 @@ <%= day[:date] %> <%= fmt_num(day[:value], 3) %> <%= fmt_num(day[:cumulative_value], 3) %> - <%= fmt_num(day[:value] * 2.54, 3) unless day[:value].nil? %> - <%= fmt_num(day[:cumulative_value] * 2.45, 3) unless day[:cumulative_value].nil? %> + <%= fmt_num(in_to_mm(day[:value]), 3) unless day[:value].nil? %> + <%= fmt_num(in_to_mm(day[:cumulative_value]), 3) unless day[:cumulative_value].nil? %> <% end %> diff --git a/app/views/weather/_data_tbl_insol.html.erb b/app/views/weather/_data_tbl_insol.html.erb index bbfe3328..4b379c0f 100644 --- a/app/views/weather/_data_tbl_insol.html.erb +++ b/app/views/weather/_data_tbl_insol.html.erb @@ -22,7 +22,7 @@ <%= i + 1 %> <%= day[:date] %> <%= fmt_num(day[:value], 2) %> - <%= fmt_num(day[:value] / 3.6, 2) unless day[:value].nil? %> + <%= fmt_num(mj_to_kwh(day[:value]), 2) unless day[:value].nil? %> <% end %> @@ -32,7 +32,7 @@ <%= k %>: <%= fmt_num(v, 2) %> - <%= fmt_num(v / 3.6, 2) unless v.nil? %> + <%= fmt_num(mj_to_kwh(v), 2) unless v.nil? %> <% end %> diff --git a/app/views/weather/_data_tbl_precip.html.erb b/app/views/weather/_data_tbl_precip.html.erb index c6d32c5e..363d4779 100644 --- a/app/views/weather/_data_tbl_precip.html.erb +++ b/app/views/weather/_data_tbl_precip.html.erb @@ -25,8 +25,8 @@ <%= day[:date] %> <%= fmt_num(day[:value], 2) %> <%= fmt_num(day[:cumulative_value], 2) %> - <%= fmt_num(day[:value] / 25.4, 3) unless day[:value].nil? %> - <%= fmt_num(day[:cumulative_value] / 25.4, 3) unless day[:cumulative_value].nil? %> + <%= fmt_num(mm_to_in(day[:value]), 3) unless day[:value].nil? %> + <%= fmt_num(mm_to_in(day[:cumulative_value]), 3) unless day[:cumulative_value].nil? %> <% end %> diff --git a/app/views/weather/precip.html.erb b/app/views/weather/precip.html.erb index 9e09bdb9..e59a4cf5 100644 --- a/app/views/weather/precip.html.erb +++ b/app/views/weather/precip.html.erb @@ -20,7 +20,7 @@ <% if @start_date %>

Cumulative precipitation map for <%= @start_date.strftime("%b %-d") %> to <%= @date.strftime("%b %-d") %> (<%= pluralize((@start_date..@date).count, "day") %>)

<% else %> -

Precipiation map for <%= @date.strftime("%b %-d") %> (<%= pluralize((Date.current - @date).to_i, "day") %> ago)

+

Precipitation map for <%= @date.strftime("%b %-d") %> (<%= pluralize((Date.current - @date).to_i, "day") %> ago)

<% end %> <%= render_async url_for(action: :map_image),