131 lines
3.4 KiB
Dart
131 lines
3.4 KiB
Dart
import 'dart:collection';
|
|
|
|
import 'package:wetter/models/hourly_weather_model.dart';
|
|
import 'dart:math';
|
|
|
|
class DailyWeatherModel {
|
|
late List<HourlyWeatherModel> hourlyForecasts;
|
|
|
|
DateTime dateOfDay() {
|
|
return hourlyForecasts[0].timestamp.toLocal();
|
|
}
|
|
|
|
DailyWeatherModel.fromJson(Map<String, dynamic> json) {
|
|
List dataPoints = json['weather'];
|
|
hourlyForecasts = [];
|
|
for (var dataPoint in dataPoints) {
|
|
hourlyForecasts.add(HourlyWeatherModel.fromJson(dataPoint));
|
|
}
|
|
}
|
|
|
|
int dailyAveragePressure() {
|
|
double pressureSum = 0;
|
|
int failuresInData = 0;
|
|
for (HourlyWeatherModel forecast in hourlyForecasts) {
|
|
if (forecast.pressure != null) {
|
|
pressureSum = pressureSum + forecast.pressure!;
|
|
} else {
|
|
failuresInData += 1;
|
|
}
|
|
}
|
|
return (pressureSum / (hourlyForecasts.length - failuresInData)).round();
|
|
}
|
|
|
|
int dailyPrecipitation() {
|
|
double precipitation = 0;
|
|
for (HourlyWeatherModel forecast in hourlyForecasts) {
|
|
if (forecast.precipitation != null) {
|
|
precipitation = precipitation + forecast.precipitation!;
|
|
}
|
|
}
|
|
return precipitation.round();
|
|
}
|
|
|
|
int dailyAverageCloudCover() {
|
|
double cloudCoverSum = 0;
|
|
int failuresInData = 0;
|
|
for (HourlyWeatherModel forecast in hourlyForecasts) {
|
|
if (forecast.cloudCover != null) {
|
|
cloudCoverSum = cloudCoverSum + forecast.cloudCover!;
|
|
} else {
|
|
failuresInData += 1;
|
|
}
|
|
}
|
|
return (cloudCoverSum / (hourlyForecasts.length - failuresInData)).round();
|
|
}
|
|
|
|
int dailyAverageWindSpeed() {
|
|
double windSpeedSum = 0;
|
|
int failuresInData = 0;
|
|
for (HourlyWeatherModel forecast in hourlyForecasts) {
|
|
if (forecast.windSpeed != null) {
|
|
windSpeedSum = windSpeedSum + forecast.windSpeed!;
|
|
} else {
|
|
failuresInData += 1;
|
|
}
|
|
}
|
|
return (windSpeedSum / (hourlyForecasts.length - failuresInData)).round();
|
|
}
|
|
|
|
String dailyAverageTemp() {
|
|
double tempSum = 0;
|
|
for (var forecast in hourlyForecasts) {
|
|
tempSum = tempSum + forecast.temperature;
|
|
}
|
|
return (tempSum / hourlyForecasts.length).round().toString();
|
|
}
|
|
|
|
List<int> tempList() {
|
|
List<int> temps = [];
|
|
for (HourlyWeatherModel hf in hourlyForecasts) {
|
|
temps.add(hf.temperature.round());
|
|
}
|
|
return temps;
|
|
}
|
|
|
|
String dailyMaxTemp() {
|
|
return tempList().reduce(max).toString();
|
|
}
|
|
|
|
String dailyMinTemp() {
|
|
return tempList().reduce(min).toString();
|
|
}
|
|
|
|
String? cumulatedSunshineMinutes() {
|
|
bool dataCorrect = true;
|
|
int csm = 0;
|
|
for (var hf in hourlyForecasts) {
|
|
if (hf.sunshine == null) {
|
|
return null;
|
|
} else {
|
|
csm = csm + hf.sunshine!;
|
|
}
|
|
}
|
|
if (dataCorrect) {
|
|
return csm.toString();
|
|
}
|
|
}
|
|
|
|
HourlyWeatherModel hourlyWeatherModelAtTimeOfDay(int tod) {
|
|
return hourlyForecasts
|
|
.firstWhere((element) => element.timestamp.hour == tod);
|
|
}
|
|
|
|
String prevalentWeatherIcon() {
|
|
List<String> icons = hourlyForecasts.map((e) => e.icon!).toList();
|
|
Map<String, int> usedIconCount = {};
|
|
for (String i in icons) {
|
|
if (!usedIconCount.containsKey(i)) {
|
|
usedIconCount[i] = 1;
|
|
} else {
|
|
usedIconCount[i] = (usedIconCount[i]! + 1);
|
|
}
|
|
}
|
|
Map<String, int> sortedValuesDesc = SplayTreeMap<String, int>.from(
|
|
usedIconCount,
|
|
(keys1, keys2) =>
|
|
usedIconCount[keys2]!.compareTo(usedIconCount[keys1]!));
|
|
return sortedValuesDesc.keys.first;
|
|
}
|
|
}
|