convert hourly to daily with wilco's new changes for weather2.
All checks were successful
FarmMaps.Develop/FarmMapsLib/pipeline/head This commit looks good
All checks were successful
FarmMaps.Develop/FarmMapsLib/pipeline/head This commit looks good
This commit is contained in:
parent
e057e463bd
commit
9697e93915
@ -1,10 +1,11 @@
|
|||||||
export interface WeatherData {
|
export interface WeatherData {
|
||||||
time: Date;
|
date: Date;
|
||||||
minTemp: number;
|
iconCode: number;
|
||||||
maxTemp: number;
|
minTemperature: number;
|
||||||
relativeHumidity: number;
|
maxTemperature: number;
|
||||||
rain: number;
|
relHumidity: number;
|
||||||
wspd?: number;
|
precipitation: number;
|
||||||
wdir_cardinal?: string;
|
wSpeed: number;
|
||||||
wdir?: number;
|
wCardinal: string;
|
||||||
|
wDir: number;
|
||||||
}
|
}
|
||||||
|
@ -54,28 +54,65 @@ export class WeatherService {
|
|||||||
const sd = encodeURIComponent(this.removeUTCZ(startDate));
|
const sd = encodeURIComponent(this.removeUTCZ(startDate));
|
||||||
const ed = encodeURIComponent(this.removeUTCZ(endDate));
|
const ed = encodeURIComponent(this.removeUTCZ(endDate));
|
||||||
|
|
||||||
const historical = `${endpoint}${this.apiObservation}/?c=${centroid[0]},${centroid[1]}&sd=${sd}&ed=${ed}&interval=daily&key=${apiKey}`;
|
const historical = `${endpoint}${this.apiObservation}/?c=${centroid[0]},${centroid[1]}&sd=${sd}&ed=${ed}&t=observation&interval=hourly&key=${apiKey}`;
|
||||||
const forecast = `${endpoint}${this.apiForecast}/?c=${centroid[0]},${centroid[1]}&interval=daily&key=${apiKey}`;
|
const forecast = `${endpoint}${this.apiForecast}/?c=${centroid[0]},${centroid[1]}&interval=hourly&key=${apiKey}`;
|
||||||
|
|
||||||
return this.httpClient.get<any[]>(historical).pipe(
|
return this.httpClient.get<any[]>(historical).pipe(
|
||||||
map(h => h.map(this.createWeatherDataFromHistorical)),
|
map(h => h.map(d => ({...d, rain: d.rainPastHour}))),
|
||||||
switchMap(h => {
|
switchMap(h => {
|
||||||
return this.httpClient.get<any[]>(forecast)
|
return this.httpClient.get<any[]>(forecast)
|
||||||
.pipe(
|
.pipe(
|
||||||
map(f => [...h, ...f.filter(fd => fd.date <= endDate)
|
map(f => this.hourlyToDaily([...h, ...f.filter(fd => fd.time <= endDate)])
|
||||||
.map(this.createWeatherDataFromForecast)]));
|
));
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private createWeatherDataFromHistorical(hd): WeatherData {
|
private hourlyToDaily(hourlyWeatherData: any[]): WeatherData[] {
|
||||||
return {time: hd.date, minTemp: hd.minimumTemperature,
|
const days = this.groupBy(hourlyWeatherData, hi => hi.time.split('T')[0]);
|
||||||
maxTemp: hd.maximumTemperature, relativeHumidity: hd.relativeHumidity, rain: hd.precipitation};
|
return Object.entries(days)
|
||||||
|
.reduce ( (result, entry) => {
|
||||||
|
const wData = entry[1];
|
||||||
|
|
||||||
|
const sortData = wData.sort(d => new Date(d.time).getHours() - 12);
|
||||||
|
const closestToTwelveOClockData = sortData[0];
|
||||||
|
if (closestToTwelveOClockData.length === 0) {
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private createWeatherDataFromForecast(fd): WeatherData {
|
const bestCardinal = closestToTwelveOClockData.windDirectionCardinal;
|
||||||
return {time: fd.date, minTemp: fd.minimumTemperature,
|
const bestWindDirection = closestToTwelveOClockData.windDirection;
|
||||||
maxTemp: fd.maximumTemperature, relativeHumidity: fd.relativeHumidity, rain: fd.rain};
|
const bestIconCode = closestToTwelveOClockData.iconCode;
|
||||||
|
return [
|
||||||
|
...result,
|
||||||
|
{
|
||||||
|
date: entry[0],
|
||||||
|
iconCode: bestIconCode,
|
||||||
|
minTemperature: Math.min(...wData.map(d => d.temperature)),
|
||||||
|
maxTemperature: Math.max(...wData.map(d => d.temperature)),
|
||||||
|
relHumidity: wData.reduce( (r, e) => r + e.relativeHumidity, 0) / wData.length,
|
||||||
|
precipitation: wData.reduce( (r, e) => r + e.rain, 0) * 10,
|
||||||
|
wSpeed: wData.reduce( (r, e) => r + e.windSpeed, 0) / wData.length,
|
||||||
|
wDir: bestWindDirection,
|
||||||
|
wCardinal: bestCardinal
|
||||||
|
}
|
||||||
|
];
|
||||||
|
}, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
private groupBy(items: any[], selector: (item) => any): any[] {
|
||||||
|
return items.reduce(
|
||||||
|
(result, item) => {
|
||||||
|
const group = selector(item);
|
||||||
|
return ({
|
||||||
|
...result,
|
||||||
|
[group]: [
|
||||||
|
...(result[group] || []),
|
||||||
|
item,
|
||||||
|
],
|
||||||
|
});},
|
||||||
|
{},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private removeUTCZ(dateFormat: string): string {
|
private removeUTCZ(dateFormat: string): string {
|
||||||
|
Loading…
Reference in New Issue
Block a user