MCPcopy Create free account
hub / github.com/Thanas-R/Virdis / fetchWeatherData

Function fetchWeatherData

src/components/WeatherView.tsx:311–362  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

309 // Fetch historical weather + soil moisture
310 useEffect(() => {
311 if (!effectiveField) return;
312 const fetchWeatherData = async () => {
313 setLoading(true);
314 try {
315 const { lat, lng } = getFieldCenter(effectiveField);
316 const start = format(startDate, "yyyy-MM-dd");
317 const end = format(endDate, "yyyy-MM-dd");
318 const [weatherRes, soilRes] = await Promise.all([
319 fetch(`https://archive-api.open-meteo.com/v1/archive?latitude=${lat}&longitude=${lng}&start_date=${start}&end_date=${end}&daily=precipitation_sum,temperature_2m_max,temperature_2m_min,et0_fao_evapotranspiration`),
320 fetch(`https://archive-api.open-meteo.com/v1/archive?latitude=${lat}&longitude=${lng}&start_date=${start}&end_date=${end}&daily=soil_moisture_0_to_7cm_mean,soil_moisture_7_to_28cm_mean`)]
321 );
322 const data = await weatherRes.json();
323 const soilData = await soilRes.json();
324
325 if (data.daily) {
326 const daily = data.daily.time.map((date: string, i: number) => ({
327 date, label: format(new Date(date + "T00:00:00"), "MMM dd"),
328 precipitation: data.daily.precipitation_sum[i] || 0,
329 tempMax: data.daily.temperature_2m_max[i], tempMin: data.daily.temperature_2m_min[i],
330 evap: data.daily.et0_fao_evapotranspiration[i] || 0
331 }));
332 const monthMap = new Map<string, {precip: number;count: number;tMax: number;tMin: number;evap: number;}>();
333 daily.forEach((d: any) => {
334 const key = format(new Date(d.date + "T00:00:00"), "MMM yyyy");
335 const m = monthMap.get(key) || { precip: 0, count: 0, tMax: -999, tMin: 999, evap: 0 };
336 m.precip += d.precipitation;m.count++;m.tMax = Math.max(m.tMax, d.tempMax ?? -999);m.tMin = Math.min(m.tMin, d.tempMin ?? 999);m.evap += d.evap;
337 monthMap.set(key, m);
338 });
339 let accumulated = 0;
340 const monthly = Array.from(monthMap.entries()).map(([month, v]) => {
341 accumulated += v.precip;
342 return { month: month.split(" ")[0], precipitation: Math.round(v.precip * 10) / 10, accumulated: Math.round(accumulated * 10) / 10, tempMax: Math.round(v.tMax), tempMin: Math.round(v.tMin), evapotranspiration: Math.round(v.evap * 10) / 10 };
343 });
344 setMonthlyData(monthly);
345 setDailyData(daily.filter((_: any, i: number) => i % 2 === 0));
346 }
347 if (soilData.daily) {
348 const soilMonthMap = new Map<string, {shallow: number;deep: number;count: number;}>();
349 soilData.daily.time.forEach((date: string, i: number) => {
350 const key = format(new Date(date + "T00:00:00"), "MMM yyyy");
351 const m = soilMonthMap.get(key) || { shallow: 0, deep: 0, count: 0 };
352 m.shallow += soilData.daily.soil_moisture_0_to_7cm_mean?.[i] || 0;
353 m.deep += soilData.daily.soil_moisture_7_to_28cm_mean?.[i] || 0;
354 m.count++;soilMonthMap.set(key, m);
355 });
356 const soilMonthly = Array.from(soilMonthMap.entries()).map(([month, v]) => ({
357 month: month.split(" ")[0], shallow: Math.round(v.shallow / v.count * 1000) / 10, deep: Math.round(v.deep / v.count * 1000) / 10
358 }));
359 setSoilMoistureData(soilMonthly);
360 }
361 } catch (e) {console.error("Failed to fetch weather data", e);} finally
362 {setLoading(false);}
363 };
364 fetchWeatherData();
365 }, [startDate, endDate, effectiveField]);

Callers 1

WeatherViewFunction · 0.70

Calls 1

getFieldCenterFunction · 0.70

Tested by

no test coverage detected