// Reproducible California county migration figures from the U.S. Census Bureau Vintage 2025 county population estimates. // Source file (public): https://www2.census.gov/programs-surveys/popest/datasets/2020-2025/counties/totals/co-est2025-alldata.csv // Usage: node census-socal-county-migration.mjs prints the tables as JSON and writes california-county-migration-census-2021-2025.csv and census-socal-county-migration-output.json next to this script // Method: DOMESTICMIGyyyy = net domestic migration (the balance of people moving between the county and all other U.S. counties, including other California counties), INTERNATIONALMIGyyyy = net international migration. Each column is the July 1 to June 30 estimate year ending in yyyy. // Limits: modeled estimates, revised with each vintage, people not households, no causes, nothing about moving-company volume. "Inland Empire" here is Riverside County plus San Bernardino County (the usual definition); it is a sum of two county rows. import fs from 'node:fs'; const URL_ = 'https://www2.census.gov/programs-surveys/popest/datasets/2020-2025/counties/totals/co-est2025-alldata.csv'; const txt = await (await fetch(URL_)).text(); const split = (l) => { const c = []; let cur = '', q = false; for (const ch of l) { if (ch === '"') q = !q; else if (ch === ',' && !q) { c.push(cur); cur = ''; } else cur += ch; } c.push(cur); return c; }; const lines = txt.trim().split(/\r?\n/); const H = split(lines[0]); const col = (n) => { const i = H.indexOf(n); if (i < 0) throw new Error('column missing: ' + n); return i; }; const rows = lines.slice(1).map(split).filter((r) => r[col('SUMLEV')] === '050' && r[col('STNAME')] === 'California'); if (rows.length !== 58) throw new Error('expected 58 California counties, got ' + rows.length); const YEARS = [2021, 2022, 2023, 2024, 2025]; const sum = (a) => a.reduce((x, y) => x + y, 0); const rec = (r) => { const dom = YEARS.map((y) => Number(r[col('DOMESTICMIG' + y)])), intl = YEARS.map((y) => Number(r[col('INTERNATIONALMIG' + y)])); return { name: r[col('CTYNAME')], base2020: Number(r[col('ESTIMATESBASE2020')]), pop2025: Number(r[col('POPESTIMATE2025')]), domestic: dom, international: intl, domesticCumulative: sum(dom), internationalCumulative: sum(intl) }; }; const all = rows.map(rec); const FIVE = ['Riverside County', 'San Bernardino County', 'Los Angeles County', 'Orange County', 'San Diego County']; const out = { source: URL_, retrieved: new Date().toISOString().slice(0, 10), years: YEARS, counties: {}, rankings: {} }; for (const n of FIVE) { const c = all.find((x) => x.name === n); c.domesticPer1000 = Math.round((c.domesticCumulative / c.base2020) * 10000) / 10; out.counties[n] = c; } const ie = { name: 'Inland Empire (Riverside + San Bernardino)', domestic: YEARS.map((_, i) => out.counties['Riverside County'].domestic[i] + out.counties['San Bernardino County'].domestic[i]) }; ie.domesticCumulative = sum(ie.domestic); ie.base2020 = out.counties['Riverside County'].base2020 + out.counties['San Bernardino County'].base2020; ie.domesticPer1000 = Math.round((ie.domesticCumulative / ie.base2020) * 10000) / 10; out.inlandEmpire = ie; const byCum = [...all].sort((a, b) => b.domesticCumulative - a.domesticCumulative); out.rankings.riversideRankOf58 = byCum.findIndex((x) => x.name === 'Riverside County') + 1; out.rankings.countiesWithNetDomesticGainEveryYear = all.filter((c) => c.domestic.every((v) => v > 0)).map((c) => c.name); out.rankings.largestGains = byCum.slice(0, 5).map((c) => ({ name: c.name, domesticCumulative: c.domesticCumulative })); out.rankings.largestLosses = byCum.slice(-5).reverse().map((c) => ({ name: c.name, domesticCumulative: c.domesticCumulative })); const csvRows = [...all].sort((a, b) => a.domesticCumulative - b.domesticCumulative).map((c) => [c.name, ...c.domestic, c.domesticCumulative, ...c.international, c.internationalCumulative, c.base2020, c.pop2025]); const csv = ['county', ...YEARS.map((y) => 'net_domestic_' + y), 'net_domestic_2021_2025', ...YEARS.map((y) => 'net_international_' + y), 'net_international_2021_2025', 'population_estimates_base_april_2020', 'population_estimate_july_2025'].join(',') + '\n' + csvRows.map((r) => `"${r[0]}",${r.slice(1).join(',')}`).join('\n') + '\n'; fs.writeFileSync(new URL('./california-county-migration-census-2021-2025.csv', import.meta.url), csv); fs.writeFileSync(new URL('./census-socal-county-migration-output.json', import.meta.url), JSON.stringify(out, null, 1)); console.log(JSON.stringify(out, null, 1)); console.log('CSV rows (California counties):', csvRows.length);