178 lines
4.8 KiB
Dart

// Functions related to parsing JSON strings and objects.
import '/var/config.dart';
import 'package:flutter/material.dart';
import 'dart:math';
String formatOpenWeatherData(var data) {
String location = pullOpenWeatherCity(data),
temp = pullOpenWeatherTemp(data),
conditions = pullOpenWeatherConditions(data),
windSpeed = pullOpenWeatherWind(data),
humidity = pullOpenWeatherHumidity(data);
String windChill = getWindChill(temp, windSpeed),
heatIndex = getHeatIndex(temp, humidity),
windAnnoyance = getWindAnnoyance(temp, windSpeed),
feelsLike = getUniversalThermalClimateIndex();
String comfort = "";
String text =
"$location is $temp$tempUnits and $conditions"
" with a wind speed of $windSpeed$windUnits"
" and humidity of $humidity$humidityUnits."
" $windChill"
" $heatIndex"
" $windAnnoyance"
" $feelsLike"
" $comfort";
final String doubleSpace = " ", singleSpace = " ";
while (text.contains(doubleSpace)) {
text = text.replaceAll(doubleSpace, singleSpace);
}
return text;
}
String pullOpenWeatherCity(var data) {
String location = "${data['city']['name']} (${data['city']['country']})";
if (debug) {
debugPrint("DEBUG: location = '$location'");
}
return location;
}
String pullOpenWeatherTemp(var data) {
String temp = data['list'][0]['main']['temp'].toString();
if (debug) {
debugPrint("DEBUG: temp = '$temp'");
}
return temp;
}
String pullOpenWeatherConditions(var data) {
String conditions = data['list'][0]['weather'][0]['description'].toString();
if (debug) {
debugPrint("DEBUG: conditions = '$conditions'");
}
return conditions;
}
String pullOpenWeatherWind(var data) {
String wind = data['list'][0]['wind']['speed'].toString();
if (debug) {
debugPrint("DEBUG: wind = '$wind'");
}
return wind;
}
String pullOpenWeatherHumidity(var data) {
String humidity = data['list'][0]['main']['humidity'].toString();
if (debug) {
debugPrint("DEBUG: humidity = '$humidity'");
}
return humidity;
}
double calcWindChill(double temp, double windSpeed) {
// ## Wind Chill ##
// # Wind speed as noted in: https://answers.yahoo.com/question/index?qid=20091020183148AAHm3kB&guccounter=1
// # More official source: https://www.weather.gov/media/epz/wxcalc/windChill.pdf
double windChill =
35.74 +
(0.6215 * temp) -
(35.75 * pow(windSpeed, 0.16)) +
(0.4275 * temp * pow(windSpeed, 0.16));
if (debug) {
debugPrint("DEBUG: windChill = '$windChill'");
}
return windChill;
}
String getWindChill(String temp, String windSpeed) {
double temperature = double.parse(temp);
double wind = double.parse(windSpeed);
if ((temperature < 50 && wind > 5) || debug) {
double windChill = calcWindChill((temperature), (wind));
return "My guess is that's a wind chill of $windChill$tempUnits.";
}
return "";
}
double calcHeatIndex(double temp, double humidity) {
// ## Heat Index ##
// # Official formula: https://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
double heatIndex =
-42.379 +
2.04901523 * temp +
10.14333127 * humidity -
0.22475541 * temp * humidity -
0.00683783 * temp * temp -
0.05481717 * humidity * humidity +
0.00122874 * temp * temp * humidity +
0.00085282 * temp * humidity * humidity -
0.00000199 * temp * temp * humidity * humidity;
if (humidity < 13 && temp >= 80 && temp <= 112) {
heatIndex -= ((13 - humidity) / 4) * sqrt((17 - (temp - 95.0).abs()) / 17);
}
if (humidity > 85 && temp >= 80 && temp <= 87) {
heatIndex += ((humidity - 85) / 10) * ((87 - temp) / 5);
}
if (heatIndex < 80) {
heatIndex =
0.5 * (temp + 61.0 + ((temp - 68.0) * 1.2) + (humidity * 0.094));
heatIndex = (heatIndex + temp) / 2;
}
if (debug) {
debugPrint("DEBUG: heatIndex = '$heatIndex'");
}
return heatIndex;
}
String getHeatIndex(String temp, String humidity) {
double temperature = double.parse(temp);
double humid = double.parse(humidity);
if (temperature > 80 || debug) {
double heatIndex = calcHeatIndex((temperature), (humid));
return "My guess is that's a heat index of $heatIndex$tempUnits.";
}
return "";
}
double calcWindAnnoyance(double temp, double windSpeed) {
double windAnnoyance = (temp / (windSpeed * (windSpeed * 0.05)));
if (debug) {
debugPrint("DEBUG: windAnnoyance = '$windAnnoyance'");
}
return windAnnoyance;
}
String getWindAnnoyance(String temp, String windSpeed) {
double temperature = double.parse(temp);
double wind = double.parse(windSpeed);
double windAnnoyance = calcWindAnnoyance(temperature, wind);
if (windAnnoyance < 3) {
return "Wind may be a bit much for the temperature.";
}
return "";
}
double calcUniversalThermalClimateIndex() {
return 0;
}
String getUniversalThermalClimateIndex() {
return "";
}