Repository navigation
Expand file tree
/
Copy pathfirms-fires.js
More file actions
138 lines (119 loc) · 4 KB
/
Copy pathfirms-fires.js
File metadata and controls
138 lines (119 loc) · 4 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* NASA FIRMS Satellite Fire Detection API
* Proxies requests to NASA FIRMS to avoid CORS and protect API key
* Returns parsed fire data for monitored conflict regions
*
* GET ?region=Ukraine&days=1 — fires for one region
* GET ?days=1 — fires for all monitored regions
*/
export const config = {
runtime: 'edge',
};
const FIRMS_API_KEY = process.env.NASA_FIRMS_API_KEY || process.env.FIRMS_API_KEY || '';
const FIRMS_BASE = 'https://firms.modaps.eosdis.nasa.gov/api/area/csv';
const SOURCE = 'VIIRS_SNPP_NRT';
// Bounding boxes as west,south,east,north
const MONITORED_REGIONS = {
'Ukraine': { bbox: '22,44,40,53' },
'Russia': { bbox: '20,50,180,82' },
'Iran': { bbox: '44,25,63,40' },
'Israel/Gaza': { bbox: '34,29,36,34' },
'Syria': { bbox: '35,32,42,37' },
'Taiwan': { bbox: '119,21,123,26' },
'North Korea': { bbox: '124,37,131,43' },
'Saudi Arabia': { bbox: '34,16,56,32' },
'Turkey': { bbox: '26,36,45,42' },
};
// Map VIIRS confidence letters to numeric
function parseConfidence(c) {
if (c === 'h') return 95;
if (c === 'n') return 50;
if (c === 'l') return 20;
return parseInt(c) || 0;
}
function parseCSV(csv) {
const lines = csv.trim().split('\n');
if (lines.length < 2) return [];
const headers = lines[0].split(',').map(h => h.trim());
const results = [];
for (let i = 1; i < lines.length; i++) {
const vals = lines[i].split(',').map(v => v.trim());
if (vals.length < headers.length) continue;
const row = {};
headers.forEach((h, idx) => { row[h] = vals[idx]; });
results.push({
lat: parseFloat(row.latitude),
lon: parseFloat(row.longitude),
brightness: parseFloat(row.bright_ti4) || 0,
scan: parseFloat(row.scan) || 0,
track: parseFloat(row.track) || 0,
acq_date: row.acq_date || '',
acq_time: row.acq_time || '',
satellite: row.satellite || '',
confidence: parseConfidence(row.confidence),
bright_t31: parseFloat(row.bright_ti5) || 0,
frp: parseFloat(row.frp) || 0,
daynight: row.daynight || '',
});
}
return results;
}
export default async function handler(request) {
if (!FIRMS_API_KEY) {
return json({ error: 'FIRMS_API_KEY not configured' }, 503);
}
try {
const { searchParams } = new URL(request.url);
const regionName = searchParams.get('region');
const days = Math.min(parseInt(searchParams.get('days')) || 1, 5);
const regions = regionName
? { [regionName]: MONITORED_REGIONS[regionName] }
: MONITORED_REGIONS;
if (regionName && !MONITORED_REGIONS[regionName]) {
return json({ error: `Unknown region: ${regionName}` }, 400);
}
const allFires = {};
let totalCount = 0;
// Fetch regions in parallel (max 10)
const entries = Object.entries(regions);
const results = await Promise.allSettled(
entries.map(async ([name, { bbox }]) => {
const url = `${FIRMS_BASE}/${FIRMS_API_KEY}/${SOURCE}/${bbox}/${days}`;
const res = await fetch(url, {
headers: { 'Accept': 'text/csv' },
});
if (!res.ok) throw new Error(`FIRMS ${res.status} for ${name}`);
const csv = await res.text();
return { name, fires: parseCSV(csv) };
})
);
for (const result of results) {
if (result.status === 'fulfilled') {
const { name, fires } = result.value;
allFires[name] = fires;
totalCount += fires.length;
} else {
console.error('[FIRMS]', result.reason?.message);
}
}
return json({
regions: allFires,
totalCount,
source: SOURCE,
days,
timestamp: new Date().toISOString(),
});
} catch (err) {
console.error('[FIRMS] Error:', err);
return json({ error: 'Failed to fetch fire data' }, 500);
}
}
function json(data, status = 200) {
return new Response(JSON.stringify(data), {
status,
headers: {
'Content-Type': 'application/json',
'Cache-Control': 'public, max-age=600', // 10 min cache
},
});
}