2025-02-21 00:30:52 -05:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
|
2025-02-25 14:48:59 -05:00
|
|
|
import argparse
|
2025-02-21 00:30:52 -05:00
|
|
|
|
2025-02-22 13:53:54 -05:00
|
|
|
from xmet.db import Database
|
|
|
|
from xmet.afos import AFOSMessageParser
|
2025-02-21 00:30:52 -05:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2025-02-25 14:48:59 -05:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description = 'Ingest National Weather Service text bulletin products'
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument('--quiet', action='store_true', help='Suppress output')
|
|
|
|
parser.add_argument('--dry-run', action='store_true', help='Do not actually ingest products')
|
|
|
|
|
|
|
|
parser.add_argument('db', help='XMET SQLite3 database')
|
|
|
|
parser.add_argument('afos-text-file', help='AFOS text bulletin product file')
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
db = Database.connect(args.db)
|
2025-02-21 00:30:52 -05:00
|
|
|
db.execute('begin transaction')
|
|
|
|
|
|
|
|
parser = AFOSMessageParser()
|
|
|
|
|
2025-02-25 14:48:59 -05:00
|
|
|
for path in getattr(args, 'afos-text-file'):
|
2025-02-21 00:30:52 -05:00
|
|
|
with open(path, 'r') as fh:
|
|
|
|
for data in each_chunk(fh, '\x01'):
|
|
|
|
if len(data) == 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
message = parser.parse(data)
|
|
|
|
|
2025-02-25 14:48:59 -05:00
|
|
|
if not args.quiet:
|
|
|
|
print(f"Ingesting AFOS file {path}")
|
|
|
|
|
|
|
|
if not args.dry_run:
|
|
|
|
db.add(message)
|
2025-02-21 00:30:52 -05:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
db.commit()
|