// Counts residential building cooking fires by day from the NFIRS Public Data Release (PDR). // Run in a browser page (or Node 18+) that can fetch the OpenFEMA files: // https://www.fema.gov/about/reports-and-data/openfema/nfirs/nfirs_fire_hazmat_pdr_.zip // Each outer zip holds NFIRS_FIRES__.zip (basicincident.txt) and // StructureFireCauses.zip (causes.txt). Files are "^"-delimited, Latin-1. // // Definition used (follows USFA's residential building fire definition and its // structure fire cause file): // - basicincident.AID not 3 or 4 (aid given; avoids double counting) // - basicincident.PROP_USE starts with "4" (residential property, 400-series) // - basicincident.INC_TYPE 111-123, excluding 112 (building fires, incl. confined fires 113-118) // - causes.CAUSE_CODE == "05" (USFA Main Fire Cause "Cooking") // Also counted: the subset with INC_TYPE 113 ("cooking fire, confined to container"). // Output: counts per INC_DATE (MMDDYYYY) for each year. function listZip(u8) { const dv = new DataView(u8.buffer, u8.byteOffset, u8.byteLength); let e = u8.length - 22; while (dv.getUint32(e, true) !== 0x06054b50) e--; let n = dv.getUint16(e + 10, true), off = dv.getUint32(e + 16, true); const out = []; for (let i = 0; i < n; i++) { const nl = dv.getUint16(off + 28, true), xl = dv.getUint16(off + 30, true), cl = dv.getUint16(off + 32, true); const name = new TextDecoder().decode(u8.subarray(off + 46, off + 46 + nl)); let cs = dv.getUint32(off + 20, true), us = dv.getUint32(off + 24, true), lho = dv.getUint32(off + 42, true); if (xl) { // zip64 extra field let p = off + 46 + nl, end = p + xl; while (p < end) { const id = dv.getUint16(p, true), sz = dv.getUint16(p + 2, true); if (id === 1) { let q = p + 4; if (us === 0xffffffff) { us = Number(dv.getBigUint64(q, true)); q += 8; } if (cs === 0xffffffff) { cs = Number(dv.getBigUint64(q, true)); q += 8; } if (lho === 0xffffffff) { lho = Number(dv.getBigUint64(q, true)); q += 8; } } p += 4 + sz; } } out.push({ name, method: dv.getUint16(off + 10, true), csize: cs, usize: us, lho }); off += 46 + nl + xl + cl; } return out; } function entryStream(u8, ent) { const dv = new DataView(u8.buffer, u8.byteOffset, u8.byteLength); const p = ent.lho, nl = dv.getUint16(p + 26, true), xl = dv.getUint16(p + 28, true); const start = p + 30 + nl + xl; const s = new Blob([u8.subarray(start, start + ent.csize)]).stream(); return ent.method === 8 ? s.pipeThrough(new DecompressionStream('deflate-raw')) : s; } const entryBytes = async (u8, ent) => new Uint8Array(await new Response(entryStream(u8, ent)).arrayBuffer()); async function eachLine(u8, name, fn) { const ent = listZip(u8).find(e => e.name === name); const rd = entryStream(u8, ent).pipeThrough(new TextDecoderStream('latin1')).getReader(); let buf = '', first = true; for (;;) { const { value, done } = await rd.read(); if (done) break; buf += value; const lines = buf.split('\n'); buf = lines.pop(); for (const l of lines) { if (first) { first = false; continue; } fn(l.replace(/\r$/, '')); } } if (buf) fn(buf); } const unq = s => s.replace(/"/g, ''); async function countYear(year) { const r = await fetch(`https://www.fema.gov/about/reports-and-data/openfema/nfirs/nfirs_fire_hazmat_pdr_${year}.zip`); const outer = new Uint8Array(await r.arrayBuffer()); const L = listZip(outer); const inner = await entryBytes(outer, L.find(e => /NFIRS_FIRES.*\.zip$/i.test(e.name))); const sc = await entryBytes(outer, L.find(e => /StructureFireCause.*\.zip$/i.test(e.name))); const cause = new Map(); await eachLine(sc, 'causes.txt', l => { const f = l.split('^'); cause.set(unq(f[0]), unq(f[7])); }); const cooking = {}, confined113 = {}; await eachLine(inner, 'basicincident.txt', l => { const f = l.split('^'); const key = unq(f[0]), date = f[3], type = unq(f[8]), aid = unq(f[10]), propUse = unq(f[40]); if (aid === '3' || aid === '4') return; if (!propUse.startsWith('4')) return; const t = +type; if (!(t >= 111 && t <= 123) || t === 112) return; if (type === '113') confined113[date] = (confined113[date] || 0) + 1; if (cause.get(key) === '05') cooking[date] = (cooking[date] || 0) + 1; }); return { year, cooking, confined113 }; } // Example: const results = []; for (const y of [2020, 2021, 2022, 2023, 2024, 2025]) results.push(await countYear(y));