#! /usr/bin/env python3

import argparse

from xmet.db   import Database
from xmet.igra import IGRAReader

parser = argparse.ArgumentParser(
    description = 'Ingest IGRA soundings'
)

parser.add_argument('--quiet',   action='store_true', help='Suppress output')
parser.add_argument('--dry-run', action='store_true', help='Do not actually ingest data')

parser.add_argument('db',                            help='XMET SQLite3 database')
parser.add_argument('igra-sounding-file', nargs='+', help='IGRA sounding file')

args = parser.parse_args()

db = Database.connect(args.db)

if not args.dry_run:
    db.execute('begin transaction')

for path in getattr(args, 'igra-sounding-file'):
    if not args.quiet:
        print(f"Ingesting sounding file {path}")

    for sounding in IGRAReader.each_sounding_from_file(path):
        if args.dry_run:
            continue

        db.add(sounding)

        for sample in sounding.samples:
            sample.sounding_id = sounding.id
            db.add(sample)

if not args.dry_run:
    db.commit()