[bfab5ea] | 1 | import sys
|
---|
| 2 | import re
|
---|
| 3 | import sqlite3
|
---|
| 4 | import argparse
|
---|
[4180d6a] | 5 | import numpy as np
|
---|
| 6 | from datetime import datetime
|
---|
[bfab5ea] | 7 | from pathlib import Path
|
---|
[4180d6a] | 8 | from pymongo import MongoClient
|
---|
[bfab5ea] | 9 | from ammosreader.AmmosIFReader import AmmosIFReader
|
---|
[6808525] | 10 | from ammosreader.PPDWReader import PPDWReader
|
---|
[bfab5ea] | 11 |
|
---|
| 12 | parser = argparse.ArgumentParser()
|
---|
| 13 | parser.add_argument('-s', '--source', required=True, help='specify source for given signal')
|
---|
| 14 | parser.add_argument('-i', '--input-dir', required=True, help='specify source directory')
|
---|
| 15 | parser.add_argument('-o', '--output-file', required=True, help='specify sqlite3 database file')
|
---|
| 16 | parser.add_argument('-n', '--signal-number', required=True, help='specify number for signal')
|
---|
| 17 | parser.add_argument('-c', '--code', required=True, help='specify code for signal')
|
---|
| 18 |
|
---|
| 19 | args = parser.parse_args()
|
---|
| 20 |
|
---|
[4180d6a] | 21 | source = args.source.upper()
|
---|
[bfab5ea] | 22 | code = args.code.upper()
|
---|
| 23 | signal_number = args.signal_number
|
---|
| 24 | input_dir = Path(args.input_dir)
|
---|
| 25 | sqlite3_file = Path(args.output_file)
|
---|
| 26 |
|
---|
[4180d6a] | 27 | if not re.match('[A-Z0-9]{2}', source):
|
---|
| 28 | print("Source identifier", source, "invalid")
|
---|
| 29 | sys.exit()
|
---|
| 30 |
|
---|
[bfab5ea] | 31 | if not re.match('[A-Z][0-9]{3}[A-Z]', code):
|
---|
| 32 | print("ELINT-Code incorrect")
|
---|
| 33 | sys.exit(1)
|
---|
| 34 |
|
---|
| 35 | if not re.match('[0-9]{4}', signal_number):
|
---|
| 36 | print("Signal number incorrect")
|
---|
| 37 | sys.exit(1)
|
---|
| 38 |
|
---|
| 39 | if not input_dir.is_dir():
|
---|
| 40 | print("Input dir invalid")
|
---|
| 41 | sys.exit(1)
|
---|
| 42 |
|
---|
| 43 | dir_name = input_dir.name
|
---|
| 44 |
|
---|
| 45 | files_inside = [each for each in (input_dir.iterdir()) if (each.suffix in ['.idx', '.iqdw', '.ppdw'] and each.stem == dir_name)]
|
---|
| 46 |
|
---|
| 47 | if len(files_inside) != 3:
|
---|
| 48 | print(".idx, .iqdw or .ppdw file missing in", input_dir)
|
---|
| 49 | sys.exit(1)
|
---|
| 50 |
|
---|
| 51 | iqdw_file = (input_dir / input_dir.stem).with_suffix('.iqdw')
|
---|
| 52 | ppdw_file = (input_dir / input_dir.stem).with_suffix('.ppdw')
|
---|
| 53 | idx_file = (input_dir / input_dir.stem).with_suffix('.idx')
|
---|
| 54 |
|
---|
| 55 | #if not sqlite3_file.is_file():
|
---|
| 56 | # print("Sqlite3 database file not found at", sqlite3_file)
|
---|
| 57 | # sys.exit(1)
|
---|
| 58 |
|
---|
[4180d6a] | 59 | # try:
|
---|
| 60 | # connection = sqlite3.connect(str(sqlite3_file))
|
---|
| 61 | # cursor = connection.cursor()
|
---|
| 62 | # except Exception:
|
---|
| 63 | # print("Can not connect to database file", str(sqlite3_file))
|
---|
| 64 | # sys.exit(1)
|
---|
[bfab5ea] | 65 |
|
---|
[4180d6a] | 66 | #ammos_if_reader = AmmosIFReader(str(iqdw_file))
|
---|
| 67 | #ammos_if_reader.read_all_frames_left()
|
---|
[94f7c24] | 68 | # for each_frame in ammos_if_reader.container.global_frames:
|
---|
| 69 | # print(each_frame)
|
---|
[6808525] | 70 |
|
---|
| 71 | ppdw_reader = PPDWReader(ppdw_file)
|
---|
| 72 | ppdw_reader.read_all_frames_left()
|
---|
[4180d6a] | 73 |
|
---|
| 74 | client = MongoClient('hackathon.kid.local', 27017)
|
---|
| 75 |
|
---|
| 76 | database = client['ELINT']
|
---|
| 77 | ts = (ppdw_reader.container.start_time() - np.datetime64('1970-01-01T00:00:00')) / np.timedelta64(1, 's')
|
---|
| 78 | time_tuple = datetime.utcfromtimestamp(ts).timetuple()
|
---|
| 79 | julian_date_string = str(time_tuple.tm_year)[2:] + str(time_tuple.tm_yday).zfill(3)
|
---|
| 80 | code_collection = database[code]
|
---|
| 81 | year_collection = code_collection[str(time_tuple.tm_year)]
|
---|
| 82 | source_collection = year_collection[source]
|
---|
| 83 |
|
---|
| 84 |
|
---|
| 85 | for each in ppdw_reader.container.signals:
|
---|
| 86 | each_json = each.to_json()
|
---|
| 87 | each_json['SIGNALNUMBER'] = signal_number
|
---|
| 88 | each_json['JULIANDATE'] = julian_date_string
|
---|
| 89 | # each_json['SOURCEFILE'] = str(ppdw_file)
|
---|
| 90 | source_collection.insert_one(each_json)
|
---|