USFEMA_FloodInsuranceClaims - #2177
Conversation
Code fix unenergy
There was a problem hiding this comment.
Code Review
This pull request introduces a direct bulk download option for the FEMA NFIP claims dataset as a faster and more reliable alternative to API pagination, which remains as a fallback. It also increases the pagination page size, improves error handling, and refines CSV chunk merging. The review feedback highlights two key improvements: raising an exception on bulk download failures to ensure proper logging instead of a silent fallback, and adding a check to prevent writing empty newlines when a downloaded chunk contains only a header.
…uard, and unit test updates
There was a problem hiding this comment.
Verification on Test Infrastructure Required
The test run cited in the PR description:
gs://datcom-import-test/statvar_imports/fema/flood_insurance_claims/USFEMA_FloodInsuranceClaims/2026_08_17T21_45_37_408557_07_00/
was executed on August 17, 2026, which predates the fix (commits 8a42557b through c34602f2). Inspecting import_summary.json for that run confirms:
script_execution_timewas 230,682 seconds (~64.1 hours) because it executed the legacy, un-optimized pagination loop.manifest.jsonwas the old version lackingnode_mcf,--existing_statvar_mcf, and--output_counters.
No runs have been executed on datcom-import-test since the bulk download fix was implemented. Please trigger a fresh run on datcom-import-test with the updated branch and confirm that script_execution_time drops to under 5 minutes and validation passes cleanly.
| @patch('fema_download.get_total_records') | ||
| def test_download_data_bulk_failure_fallback(self, mock_get_total_records, | ||
| mock_download_file, | ||
| mock_rmtree): |
There was a problem hiding this comment.
Please address the following style and resource warnings in fema_download_test.py:
- Unused argument
mock_rmtreeacross unit test methods (lines 171, 210, 252, 296, 340, 383). - Line 221: Use context manager
with open(...)instead of unmanagedopen(...).close(). - Line 253: Line exceeds 100 characters (113 chars).
There was a problem hiding this comment.
- Unused mock_rmtree arguments: Added mock_rmtree.assert_called_with(self.test_temp_dir) across all 6 test methods to assert proper cleanup and eliminate the unused-argument warnings.
- Resource management: Refactored line 221 from open(...).close() to use the context manager with open(..., 'wb'): pass.
- Line length: Shortened the docstring on line 253 to remain well within the 100-character limit.
| @@ -31,12 +31,16 @@ | |||
| flags.DEFINE_string('api_url', | |||
There was a problem hiding this comment.
download_data() accepts output_dir, but no --output_dir absl flag is defined under flags.DEFINE_* (unlike --temp_dir, --api_url, and --bulk_url).
Consider defining:
flags.DEFINE_string('output_dir', None, 'The directory to store output files.')and passing _FLAGS.output_dir from main().
There was a problem hiding this comment.
- Defined flags.DEFINE_string('output_dir', None, 'The directory to store output files.') in fema_download.py.
- Forwarded _FLAGS.output_dir in main(argv) to download_data().
- Added test_main in fema_download_test.py to ensure all flags are properly parsed and forwarded.
| flags.DEFINE_string('api_url', | ||
| 'https://www.fema.gov/api/open/v2/FimaNfipClaims', | ||
| 'The base URL of the API endpoint to download data from.') | ||
| flags.DEFINE_string( |
There was a problem hiding this comment.
Please address the following PEP8/pylint warnings:
- Line 18: Unused import
time(W0611). - Line 29: Imports from package
abslshould be grouped and placed before first-party imports (C0412,C0411). - Lines 95, 137, 250, 267: Lines exceed 100 characters (
C0301).
There was a problem hiding this comment.
- Removed the unused import time (W0611).
- Grouped and reordered all absl imports (app, flags, logging) before first-party imports (C0411, C0412).
- Wrapped all lines exceeding 100 characters (lines 95, 137, 250, 267) (C0301).
…nd update unit tests - Define --output_dir flag and forward it in main() - Group absl imports and remove unused time import - Fix line length (>100 chars) violations in download script and tests - Use context manager for empty file creation in test - Assert mock_rmtree cleanup across unit test methods - Add test_main to verify flag forwarding
Description
This PR optimizes and stabilizes the data download pipeline for the FEMA NFIP Flood Insurance Claims import.
Key Changes
Direct Bulk Download Support:
bulk_urlsupport to download the full dataset (FimaNfipClaims.csv) directly from OpenFEMA as the primary method, significantly speeding up the download process.Error Handling & Fallback Visibility:
CSV Chunk Concatenation Fixes:
exist_ok=Trueand cleanup infinallyblocks).Testing & Coverage:
fema_download_test.pycovering:Verification