Implement support for reading IGRA soundings from zip files

This commit is contained in:
XANTRONIX 2025-02-25 13:15:32 -05:00
parent b4223248e5
commit 11cb88c979
2 changed files with 30 additions and 14 deletions

View file

@ -9,8 +9,7 @@ db = Database.connect(sys.argv[1])
db.execute('begin transaction')
for path in sys.argv[2:]:
with open(path, 'r') as fh:
for sounding in IGRAReader.each_from_file(path):
for sounding in IGRAReader.each_sounding_from_file(path):
db.add(sounding)
for sample in sounding.samples:

View file

@ -1,6 +1,7 @@
import io
import re
import datetime
import zipfile
import shapely
from typing import Self
@ -62,6 +63,9 @@ class IGRAReader():
def __init__(self, fh: io.TextIOBase):
self.fh = fh
def __del__(self):
self.fh.close()
def read_sample(self) -> SoundingSample:
line = self.fh.readline()
@ -151,8 +155,7 @@ class IGRAReader():
return sounding
@staticmethod
def each_from_file(path: str):
with open(path, 'r') as fh:
def each_sounding_from_fh(fh: io.TextIOBase):
reader = IGRAReader(fh)
while True:
@ -163,6 +166,20 @@ class IGRAReader():
yield sounding
@staticmethod
def each_sounding_from_file(path: str):
if path[-4:].lower() == '.zip':
with zipfile.ZipFile(path, 'r') as z:
for member in z.infolist():
if member.filename[-4:].lower() != '.txt':
continue
with z.open(member.filename, 'r') as fh:
yield from IGRAReader.each_sounding_from_fh(io.TextIOWrapper(fh))
else:
with open(path, 'r') as fh:
yield from IGRAReader.each_sounding_from_fh(fh)
def cols(text: str, start: int, end: int):
a = start - 1
b = end