[1d0974d] | 1 | import sys
|
---|
| 2 | import re
|
---|
| 3 | import argparse
|
---|
| 4 | import numpy as np
|
---|
| 5 | from datetime import datetime
|
---|
| 6 | from pathlib import Path
|
---|
| 7 | from pymongo import MongoClient
|
---|
| 8 | from ammosreader.PPDWReader import PPDWReader
|
---|
| 9 |
|
---|
| 10 | parser = argparse.ArgumentParser()
|
---|
| 11 | parser.add_argument('-s', '--source', required=True, help='specify source for given signal')
|
---|
| 12 | parser.add_argument('-i', '--input-dir', required=True, help='specify source directory')
|
---|
| 13 |
|
---|
| 14 | args = parser.parse_args()
|
---|
| 15 |
|
---|
| 16 | source = args.source.upper()
|
---|
| 17 | # code = args.code.upper()
|
---|
| 18 | # signal_number = args.signal_number
|
---|
| 19 | input_dir = Path(args.input_dir)
|
---|
| 20 |
|
---|
| 21 | if not re.match('[A-Z0-9]{2}', source):
|
---|
| 22 | print("Source identifier", source, "invalid")
|
---|
| 23 | sys.exit()
|
---|
| 24 |
|
---|
| 25 | code = input_dir.name
|
---|
| 26 |
|
---|
| 27 | #if not re.match('[A-Z][0-9]{3}[A-Z]', code):
|
---|
| 28 | # print("ELINT-Code incorrect")
|
---|
| 29 | # sys.exit(1)
|
---|
| 30 |
|
---|
| 31 | #if not re.match('[0-9]{4}', signal_number):
|
---|
| 32 | # print("Signal number incorrect")
|
---|
| 33 | # sys.exit(1)
|
---|
| 34 |
|
---|
| 35 | if not input_dir.is_dir():
|
---|
| 36 | print("Input dir invalid")
|
---|
| 37 | sys.exit(1)
|
---|
| 38 |
|
---|
| 39 | subdirs = [f for f in input_dir.iterdir() if f.is_dir()]
|
---|
| 40 |
|
---|
| 41 | client = MongoClient('hackathon.kid.local', 27017)
|
---|
| 42 |
|
---|
| 43 | for each_dir in subdirs:
|
---|
| 44 |
|
---|
| 45 | print("Each dir", each_dir)
|
---|
| 46 | dir_name = each_dir.name
|
---|
| 47 |
|
---|
| 48 | files_inside = [each for each in (each_dir.iterdir()) if (each.suffix in ['.idx', '.iqdw', '.ppdw'] and each_dir.stem == dir_name)]
|
---|
| 49 |
|
---|
| 50 | print("Files inside", files_inside)
|
---|
| 51 | if len(files_inside) != 3:
|
---|
| 52 | print(".idx, .iqdw or .ppdw file missing in", each_dir)
|
---|
| 53 | sys.exit(1)
|
---|
| 54 |
|
---|
| 55 | iqdw_file = (input_dir / each_dir / each_dir.stem).with_suffix('.iqdw')
|
---|
| 56 | ppdw_file = (input_dir / each_dir / each_dir.stem).with_suffix('.ppdw')
|
---|
| 57 | print("PPDW file", ppdw_file)
|
---|
| 58 | idx_file = (input_dir / each_dir / each_dir.stem).with_suffix('.idx')
|
---|
| 59 |
|
---|
| 60 | ppdw_reader = PPDWReader(ppdw_file)
|
---|
| 61 | ppdw_reader.read_all_frames_left()
|
---|
| 62 |
|
---|
| 63 | database = client['ELINT']
|
---|
| 64 | ts = (ppdw_reader.container.start_time() - np.datetime64('1970-01-01T00:00:00')) / np.timedelta64(1, 's')
|
---|
| 65 | time_tuple = datetime.utcfromtimestamp(ts).timetuple()
|
---|
| 66 | julian_date_string = str(time_tuple.tm_year)[2:] + str(time_tuple.tm_yday).zfill(3)
|
---|
| 67 | code_collection = database[input_dir.name]
|
---|
| 68 | year_collection = code_collection[str(time_tuple.tm_year)]
|
---|
| 69 | source_collection = year_collection[source]
|
---|
| 70 |
|
---|
| 71 | json_representation = ppdw_reader.container.to_json()
|
---|
| 72 | json_representation['SIGNALNUMBER'] = dir_name
|
---|
| 73 | json_representation['SOURCEFILE'] = str(ppdw_file)
|
---|
| 74 | source_collection.insert_one(json_representation)
|
---|