Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Igc skip duplicate time entries #304

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions aerofiles/igc/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ class Reader:
"""
A reader for the IGC flight log file format.

skip_duplicates flag removes trailing duplicate time entries

Example:

.. sourcecode:: python

>>> with open('track.igc', 'r') as f:
... parsed = Reader().read(f)
>>> with open('track.igc', 'r') as f: # Removes duplicate time entries
... parsed = Reader(skip_duplicates=True).read(f)

"""

def __init__(self):
def __init__(self, skip_duplicates=False):
self.reader = None
self.skip_duplicates = skip_duplicates

def read(self, file_obj):
"""
Expand Down Expand Up @@ -57,7 +62,22 @@ def read(self, file_obj):
fix_records[0].append(MissingExtensionsError)

fix_record = LowLevelReader.process_B_record(line, fix_record_extensions[1])
fix_records[1].append(fix_record)
# Set time entry as datetime. If entry is less than the first record, shift datetime to next day
fix_record["time"] = datetime.datetime.combine(
date=header[1]["utc_date"] + datetime.timedelta(
days=1
if len(fix_records[1]) > 0 and fix_record["time"] < fix_records[1][-1]["time"].time()
else 0
),
time=fix_record["time"],
tzinfo=datetime.timezone.utc
)
if not fix_records[1] or (
abs((
fix_record["time"] - fix_records[1][-1]["time"]
).total_seconds()) >= 1 or not self.skip_duplicates
):
fix_records[1].append(fix_record)
elif record_type == 'C':
task_item = line

Expand Down Expand Up @@ -653,28 +673,15 @@ def decode_date(date_str):
elif date_str == '000000':
return None

dd = int(date_str[0:2])
mm = int(date_str[2:4])
yy = int(date_str[4:6])

current_year_yyyy = datetime.date.today().year
current_year_yy = current_year_yyyy % 100
current_century = current_year_yyyy - current_year_yy
yyyy = current_century + yy if yy <= current_year_yy else current_century - 100 + yy

return datetime.date(yyyy, mm, dd)
return datetime.datetime.strptime(date_str, "%d%m%y").date()

@staticmethod
def decode_time(time_str):

if len(time_str) != 6:
raise ValueError('Time string does not have correct size')

h = int(time_str[0:2])
m = int(time_str[2:4])
s = int(time_str[4:6])

return datetime.time(h, m, s)
return datetime.datetime.strptime(time_str, "%H%M%S").time()

@staticmethod
def decode_extension_record(line):
Expand Down
2 changes: 2 additions & 0 deletions tests/igc/data/example.igc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ B1602405407121N00249342WA002800042120509950
D20331
E160245PEV
B1602455107126N00149300WA002880042919509020
B1602455107126N00149300WA002880042919509020
B1602505107134N00149283WA002900043221009015
B1602555107140N00149221WA002900043020009012
F1603000609123624221821
Expand All @@ -45,6 +46,7 @@ B1603105107212N00149174WA002930043519608024
K16024800090
B1602485107220N00149150WA004940043619008018
B1602525107330N00149127WA004960043919508015
B0002525107330N00149127WA004960043919508015
LXXXRURITANIAN STANDARD NATIONALS DAY 1
LXXXFLIGHT TIME: 4:14:25, TASK SPEED:58.48KTS
GREJNGJERJKNJKRE31895478537H43982FJN9248F942389T433T
Expand Down
12 changes: 11 additions & 1 deletion tests/igc/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,9 @@ def test_highlevel_reader():
'manufacturer': 'XXX'
}

assert len(result['fix_records'][1]) == 9
assert len(result['fix_records'][1]) == 11
assert result['fix_records'][1][0]["time"].day == 16 # Assert equal to header day
assert result['fix_records'][1][-1]["time"].day == 17 # Assert next day

assert result['task'][1]['declaration_date'] == datetime.date(2001, 7, 15)
assert result['task'][1]['declaration_time'] == datetime.time(21, 38, 41)
Expand Down Expand Up @@ -674,6 +676,14 @@ def test_highlevel_reader():
assert len(result['comment_records'][1]) == 2


def test_highlevel_reader_skip_duplicates():
reader = Reader(skip_duplicates=True)
cur_dir = os.path.dirname(__file__)
with open(os.path.join(cur_dir, 'data', 'example.igc'), 'r') as f:
result = reader.read(f)
assert len(result['fix_records'][1]) == 10


def test_highlevel_reader_examples():
reader = Reader()

Expand Down
Loading