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

Function fetchWeatherData

src/components/FieldComparisonColumn.tsx:154–205  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

152 setLoading(true);
153 try {
154 const { lat, lng } = getFieldCenter(field);
155 const start = format(startDate, "yyyy-MM-dd");
156 const end = format(endDate, "yyyy-MM-dd");
157 const [weatherRes, soilRes] = await Promise.all([
158 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`),
159 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`),
160 ]);
161 const data = await weatherRes.json();
162 const soilData = await soilRes.json();
163
164 if (data.daily) {
165 const daily = data.daily.time.map((date: string, i: number) => ({
166 date, label: format(new Date(date + "T00:00:00"), "MMM dd"),
167 precipitation: data.daily.precipitation_sum[i] || 0,
168 tempMax: data.daily.temperature_2m_max[i], tempMin: data.daily.temperature_2m_min[i],
169 evap: data.daily.et0_fao_evapotranspiration[i] || 0,
170 }));
171 const monthMap = new Map<string, { precip: number; count: number; tMax: number; tMin: number; evap: number }>();
172 daily.forEach((d: any) => {
173 const key = format(new Date(d.date + "T00:00:00"), "MMM yyyy");
174 const m = monthMap.get(key) || { precip: 0, count: 0, tMax: -999, tMin: 999, evap: 0 };
175 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;
176 monthMap.set(key, m);
177 });
178 let accumulated = 0;
179 const monthly = Array.from(monthMap.entries()).map(([month, v]) => {
180 accumulated += v.precip;
181 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 };
182 });
183 setMonthlyData(monthly);
184 setDailyData(daily.filter((_: any, i: number) => i % 2 === 0));
185 }
186 if (soilData.daily) {
187 const soilMonthMap = new Map<string, { shallow: number; deep: number; count: number }>();
188 soilData.daily.time.forEach((date: string, i: number) => {
189 const key = format(new Date(date + "T00:00:00"), "MMM yyyy");
190 const m = soilMonthMap.get(key) || { shallow: 0, deep: 0, count: 0 };
191 m.shallow += soilData.daily.soil_moisture_0_to_7cm_mean?.[i] || 0;
192 m.deep += soilData.daily.soil_moisture_7_to_28cm_mean?.[i] || 0;
193 m.count++; soilMonthMap.set(key, m);
194 });
195 const soilMonthly = Array.from(soilMonthMap.entries()).map(([month, v]) => ({
196 month: month.split(" ")[0], shallow: Math.round(v.shallow / v.count * 1000) / 10, deep: Math.round(v.deep / v.count * 1000) / 10,
197 }));
198 setSoilMoistureData(soilMonthly);
199 }
200 } catch (e) { console.error("Failed to fetch weather data", e); }
201 finally { setLoading(false); }
202 };
203 fetchWeatherData();
204 }, [startDate, endDate, field.id]);
205
206 const vegetation = geeData?.vegetation;
207 const landUseData = geeData?.land_use
208 ? Object.entries(geeData.land_use).map(([name, pct]) => ({

Callers 1

FieldComparisonColumnFunction · 0.70

Calls 1

getFieldCenterFunction · 0.70

Tested by

no test coverage detected