Skip to content

USFEMA_FloodInsuranceClaims - #2177

Open
kartik-s21 wants to merge 46 commits into
datacommonsorg:masterfrom
kartik-s21:fema-download-fix
Open

USFEMA_FloodInsuranceClaims#2177
kartik-s21 wants to merge 46 commits into
datacommonsorg:masterfrom
kartik-s21:fema-download-fix

Conversation

@kartik-s21

@kartik-s21 kartik-s21 commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Description

This PR optimizes and stabilizes the data download pipeline for the FEMA NFIP Flood Insurance Claims import.

Key Changes

  1. Direct Bulk Download Support:

    • Added bulk_url support to download the full dataset (FimaNfipClaims.csv) directly from OpenFEMA as the primary method, significantly speeding up the download process.
    • Implemented automatic fallback to paginated API download if direct bulk download fails or returns empty.
  2. Error Handling & Fallback Visibility:

    • Explicitly raise exceptions upon bulk download failure so that fallback events are properly captured and logged with warnings.
    • Added fatal logging and exceptions on API chunk failures or incomplete downloads to prevent silent partial data ingestion.
  3. CSV Chunk Concatenation Fixes:

    • Improved binary chunk merging logic to ensure consistent newline endings across chunks.
    • Guarded chunk concatenation to prevent empty/header-only chunks from injecting blank lines into the merged CSV.
    • Ensured safe temporary directory management (exist_ok=True and cleanup in finally blocks).
  4. Testing & Coverage:

    • Added comprehensive unit tests in fema_download_test.py covering:
      • Record count retrieval and error handling.
      • Direct bulk download success flow.
      • Direct bulk download failure with fallback to API pagination.
      • Multi-chunk pagination and header-only chunk merging without blank lines.

Verification

  • Validation Report - link
  • CRA Report - link
  • Differ summary - link (All these deletions are expected and are from Source side changes)

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread statvar_imports/fema/flood_insurance_claims/fema_download.py Outdated
Comment thread statvar_imports/fema/flood_insurance_claims/fema_download.py Outdated
Comment thread statvar_imports/fema/flood_insurance_claims/fema_download_test.py Fixed
@kartik-s21
kartik-s21 requested a review from saanikaaa August 24, 2026 08:53
@balit-raibot
balit-raibot self-requested a review August 25, 2026 11:05
Comment thread statvar_imports/fema/flood_insurance_claims/fema_download.py
Comment thread statvar_imports/fema/flood_insurance_claims/fema_download.py Outdated
Comment thread statvar_imports/fema/flood_insurance_claims/fema_download.py
Comment thread statvar_imports/fema/flood_insurance_claims/fema_download.py
Comment thread statvar_imports/fema/flood_insurance_claims/fema_download.py Outdated
Comment thread statvar_imports/fema/flood_insurance_claims/fema_download.py
Comment thread statvar_imports/fema/flood_insurance_claims/fema_download.py
Comment thread statvar_imports/fema/flood_insurance_claims/fema_download.py
Comment thread statvar_imports/fema/flood_insurance_claims/fema_download.py
Comment thread statvar_imports/fema/flood_insurance_claims/fema_download.py
Comment thread statvar_imports/fema/flood_insurance_claims/fema_download.py
Comment thread statvar_imports/fema/flood_insurance_claims/fema_download_test.py
Comment thread statvar_imports/fema/flood_insurance_claims/fema_download.py
Comment thread statvar_imports/fema/flood_insurance_claims/fema_download.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. script_execution_time was 230,682 seconds (~64.1 hours) because it executed the legacy, un-optimized pagination loop.
  2. manifest.json was the old version lacking node_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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address the following style and resource warnings in fema_download_test.py:

  • Unused argument mock_rmtree across unit test methods (lines 171, 210, 252, 296, 340, 383).
  • Line 221: Use context manager with open(...) instead of unmanaged open(...).close().
  • Line 253: Line exceeds 100 characters (113 chars).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 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',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address the following PEP8/pylint warnings:

  • Line 18: Unused import time (W0611).
  • Line 29: Imports from package absl should be grouped and placed before first-party imports (C0412, C0411).
  • Lines 95, 137, 250, 267: Lines exceed 100 characters (C0301).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants