forked from pytroll/pyorbital
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Adam.Dybbroe <a000680@c22526.ad.smhi.se>
- Loading branch information
Adam.Dybbroe
committed
Jun 14, 2024
1 parent
cf1e49f
commit 915f366
Showing
3 changed files
with
121 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright (c) 2024 Pyorbital developers | ||
|
||
|
||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
|
||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
|
||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Testing the TLE archive functions.""" | ||
|
||
|
||
import datetime as dt | ||
import pyorbital | ||
from pyorbital.tle_archive import populate_tle_buffer | ||
|
||
|
||
def test_populate_tle_buffer(fake_tle_file1_calipso): | ||
"""Test populate the TLE buffer.""" | ||
tle_filename = str(fake_tle_file1_calipso) | ||
tlebuff = {} | ||
tleid_calipso = '29108' | ||
populate_tle_buffer(tle_filename, tleid_calipso, tlebuff) | ||
|
||
with open(tle_filename, 'r') as fpt: | ||
tlelines = fpt.readlines() | ||
|
||
expected_dtimes = [dt.datetime(2013, 12, 31, 13, 38, 47, 751936), | ||
dt.datetime(2014, 1, 1, 17, 39, 47, 34720), | ||
dt.datetime(2014, 1, 2, 20, 1, 53, 254560), | ||
dt.datetime(2014, 1, 4, 0, 2, 52, 226304)] | ||
for idx, key in enumerate(tlebuff.keys()): | ||
assert key == expected_dtimes[idx] | ||
|
||
for idx, key in enumerate(tlebuff.keys()): | ||
tleobj = tlebuff[key] | ||
assert isinstance(tleobj, pyorbital.tlefile.Tle) | ||
assert tlelines[idx*2].strip() == tleobj.line1 | ||
assert tlelines[idx*2+1].strip() == tleobj.line2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright (c) 2024 Pyorbital developers | ||
|
||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
|
||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
|
||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Functions to support handling many archived TLEs.""" | ||
|
||
|
||
import numpy as np | ||
import datetime as dt | ||
# from datetime import timedelta, timezone | ||
from pyorbital.tlefile import Tle | ||
|
||
max_tle_days_diff = 3 | ||
|
||
|
||
def populate_tle_buffer(filename, tle_id, tle_buffer): | ||
"""Populate the TLE buffer.""" | ||
with open(filename, 'r') as fh_: | ||
tle_data_as_list = fh_.readlines() | ||
for ind in range(0, len(tle_data_as_list), 2): | ||
if tle_id in tle_data_as_list[ind]: | ||
tle = Tle(tle_id, line1=tle_data_as_list[ind], line2=tle_data_as_list[ind+1]) | ||
# dto = datetime.strptime(tle.epoch, '%Y-%m-%dT%H:%M:%S:%f') | ||
ts = (tle.epoch - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's') | ||
tobj = dt.datetime.utcfromtimestamp(ts) | ||
tle_buffer[tobj] = tle | ||
|
||
|
||
def get_tle_archive(timestamp, filename, tle_id, tle_buffer): | ||
"""Get Two-Line elements from the archive. | ||
The TLE buffer tle_buffer is being updated. | ||
""" | ||
# Read tle data if not already in buffer | ||
if len(tle_buffer) == 0: | ||
populate_tle_buffer(filename, tle_id, tle_buffer) | ||
|
||
for tobj in tle_buffer: | ||
if tobj > timestamp: | ||
deltat = tobj - timestamp | ||
else: | ||
deltat = timestamp - tobj | ||
if np.abs((deltat).days) < 1: | ||
return tle_buffer[tobj] | ||
|
||
for delta_days in range(1, max_tle_days_diff + 1, 1): | ||
for tobj in tle_buffer: | ||
if tobj > timestamp: | ||
deltat = tobj - timestamp | ||
else: | ||
deltat = timestamp - tobj | ||
if np.abs((deltat).days) <= delta_days: | ||
print("Did not find TLE for {:s}, Using TLE from {:s}".format(tobj.strftime("%Y%m%d"), | ||
timestamp.strftime("%Y%m%d"))) | ||
return tle_buffer[tobj] | ||
print("Did not find TLE for {:s} +/- 3 days") |