#! /usr/bin/env python3 import sys import argparse from nexrad.db import Database from nexrad.afos import AFOSMessageParser CHUNK_SIZE = 4096 def each_chunk(fh, sep: str): buf = '' while True: chunk = fh.read(CHUNK_SIZE) if chunk == '' or chunk is None: yield buf.strip() break buf += chunk while True: try: part, buf = buf.split(sep, 1) except ValueError: break else: yield part.strip() ap = argparse.ArgumentParser(description='Ingest bulk AFOS text messages') ap.add_argument('--poly-only', action='store_true', help='Ingest only AFOS messages with geographic bounding polygons') db = Database.connect(sys.argv[1]) db.execute('begin transaction') parser = AFOSMessageParser() for path in sys.argv[2:]: with open(path, 'r') as fh: for data in each_chunk(fh, '\x01'): if len(data) == 0: continue try: message = parser.parse(data) db.add(message) except: pass db.commit()