-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_script.js
53 lines (44 loc) · 1.68 KB
/
test_script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const fetch = require("node-fetch");
// Assuming this is an async function
async function fetchWeatherData() {
const latCenter = "51.50736895638911";
const longCenter = "-0.12078505578148935";
const startDay = "2023-06-07";
const endDate = "2023-06-08";
// Ensure that we do not search beyond today's date
//let endDate = Number(currentDayString) < Number(endDayString) ? currentDay : endDay;
const url = `https://archive-api.open-meteo.com/v1/archive?latitude=${latCenter}&longitude=${longCenter}&start_date=${startDay}&end_date=${endDate}&hourly=rain`;
console.log(url);
try {
const response = await fetch(url);
const data = await response.json();
let dailyRainfall = {};
for (let i = 0; i < data.hourly.time.length; i++) {
let date = data.hourly.time[i];
let rain = data.hourly.rain[i];
if (rain === null || rain === 0) {
continue;
}
let daysDate = date.substring(0, 10);
if (!dailyRainfall[daysDate]) {
dailyRainfall[daysDate] = 0;
}
dailyRainfall[daysDate] += rain;
}
// Check if rainfall on any day exceeds the seasonal limit
for (let day in dailyRainfall) {
let season = getSeason(day, latCenter);
if (dailyRainfall[day] > inputValue) {
console.log(season);
return season;
}
}
console.log(false);
return false;
} catch (error) {
console.error("Request Failed!", error);
}
}
fetchWeatherData()
.then((result) => console.log(result))
.catch((error) => console.error(error));