Implement support for reading IGRA soundings from zip files
This commit is contained in:
parent
b4223248e5
commit
11cb88c979
2 changed files with 30 additions and 14 deletions
|
@ -9,12 +9,11 @@ db = Database.connect(sys.argv[1])
|
||||||
db.execute('begin transaction')
|
db.execute('begin transaction')
|
||||||
|
|
||||||
for path in sys.argv[2:]:
|
for path in sys.argv[2:]:
|
||||||
with open(path, 'r') as fh:
|
for sounding in IGRAReader.each_sounding_from_file(path):
|
||||||
for sounding in IGRAReader.each_from_file(path):
|
db.add(sounding)
|
||||||
db.add(sounding)
|
|
||||||
|
|
||||||
for sample in sounding.samples:
|
for sample in sounding.samples:
|
||||||
sample.sounding_id = sounding.id
|
sample.sounding_id = sounding.id
|
||||||
db.add(sample)
|
db.add(sample)
|
||||||
|
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import io
|
import io
|
||||||
import re
|
import re
|
||||||
import datetime
|
import datetime
|
||||||
|
import zipfile
|
||||||
import shapely
|
import shapely
|
||||||
|
|
||||||
from typing import Self
|
from typing import Self
|
||||||
|
@ -62,6 +63,9 @@ class IGRAReader():
|
||||||
def __init__(self, fh: io.TextIOBase):
|
def __init__(self, fh: io.TextIOBase):
|
||||||
self.fh = fh
|
self.fh = fh
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
self.fh.close()
|
||||||
|
|
||||||
def read_sample(self) -> SoundingSample:
|
def read_sample(self) -> SoundingSample:
|
||||||
line = self.fh.readline()
|
line = self.fh.readline()
|
||||||
|
|
||||||
|
@ -151,17 +155,30 @@ class IGRAReader():
|
||||||
return sounding
|
return sounding
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def each_from_file(path: str):
|
def each_sounding_from_fh(fh: io.TextIOBase):
|
||||||
with open(path, 'r') as fh:
|
reader = IGRAReader(fh)
|
||||||
reader = IGRAReader(fh)
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
sounding = reader.read()
|
sounding = reader.read()
|
||||||
|
|
||||||
if sounding is None:
|
if sounding is None:
|
||||||
break
|
break
|
||||||
|
|
||||||
yield sounding
|
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):
|
def cols(text: str, start: int, end: int):
|
||||||
a = start - 1
|
a = start - 1
|
||||||
|
|
Loading…
Add table
Reference in a new issue