[5e84106] | 1 | """I import .ppdw files from multiple subdirectories into a mongodb."""
|
---|
| 2 |
|
---|
[1d0974d] | 3 | import sys
|
---|
| 4 | import re
|
---|
| 5 | import argparse
|
---|
| 6 | import numpy as np
|
---|
| 7 | from datetime import datetime
|
---|
| 8 | from pathlib import Path
|
---|
| 9 | from pymongo import MongoClient
|
---|
| 10 | from ammosreader.PPDWReader import PPDWReader
|
---|
| 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 |
|
---|
| 16 | args = parser.parse_args()
|
---|
| 17 |
|
---|
| 18 | source = args.source.upper()
|
---|
| 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 |
|
---|
[5e84106] | 27 | # FIXME: Directory structure differs
|
---|
| 28 | # FIXME: get julian date, source identifier, signal number and ELINT-Code from input_dir path
|
---|
| 29 |
|
---|
| 30 | # if not re.match('[A-Z][0-9]{3}[A-Z]', code):
|
---|
[1d0974d] | 31 | # print("ELINT-Code incorrect")
|
---|
| 32 | # sys.exit(1)
|
---|
| 33 |
|
---|
| 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 |
|
---|
[5e84106] | 45 | # print("Each dir", each_dir)
|
---|
[1d0974d] | 46 | dir_name = each_dir.name
|
---|
| 47 |
|
---|
[5e84106] | 48 | # if not re.match('[0-9]{5}', dir_name):
|
---|
| 49 | # print("Signal number incorrect")
|
---|
| 50 | # sys.exit(1)
|
---|
| 51 |
|
---|
| 52 | files_inside = [each for each in (each_dir.iterdir()) if (each.suffix in ['.idx', '.iqdw', '.ppdw'] and
|
---|
| 53 | each_dir.stem == dir_name)]
|
---|
[1d0974d] | 54 |
|
---|
[5e84106] | 55 | # print("Files inside", files_inside)
|
---|
[1d0974d] | 56 | if len(files_inside) != 3:
|
---|
| 57 | print(".idx, .iqdw or .ppdw file missing in", each_dir)
|
---|
| 58 | sys.exit(1)
|
---|
| 59 |
|
---|
| 60 | iqdw_file = (input_dir / each_dir / each_dir.stem).with_suffix('.iqdw')
|
---|
| 61 | ppdw_file = (input_dir / each_dir / each_dir.stem).with_suffix('.ppdw')
|
---|
[5e84106] | 62 | # print("PPDW file", ppdw_file)
|
---|
[1d0974d] | 63 | idx_file = (input_dir / each_dir / each_dir.stem).with_suffix('.idx')
|
---|
| 64 |
|
---|
| 65 | ppdw_reader = PPDWReader(ppdw_file)
|
---|
| 66 | ppdw_reader.read_all_frames_left()
|
---|
| 67 |
|
---|
| 68 | database = client['ELINT']
|
---|
| 69 | ts = (ppdw_reader.container.start_time() - np.datetime64('1970-01-01T00:00:00')) / np.timedelta64(1, 's')
|
---|
| 70 | time_tuple = datetime.utcfromtimestamp(ts).timetuple()
|
---|
| 71 | julian_date_string = str(time_tuple.tm_year)[2:] + str(time_tuple.tm_yday).zfill(3)
|
---|
[5e84106] | 72 | print("Julian", julian_date_string, str(ppdw_file))
|
---|
[1d0974d] | 73 | code_collection = database[input_dir.name]
|
---|
| 74 | year_collection = code_collection[str(time_tuple.tm_year)]
|
---|
| 75 | source_collection = year_collection[source]
|
---|
| 76 |
|
---|
| 77 | json_representation = ppdw_reader.container.to_json()
|
---|
| 78 | json_representation['SIGNALNUMBER'] = dir_name
|
---|
| 79 | json_representation['SOURCEFILE'] = str(ppdw_file)
|
---|
| 80 | source_collection.insert_one(json_representation)
|
---|