2019-01-16 19:40:08 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import re
|
|
|
|
import requests
|
|
|
|
import os
|
|
|
|
import argparse
|
|
|
|
import datetime
|
|
|
|
from html.parser import HTMLParser
|
|
|
|
|
2021-03-09 19:43:13 +00:00
|
|
|
server = 'http://aux.sentinel1.eo.esa.int/'
|
2019-01-16 19:40:08 +00:00
|
|
|
|
2021-03-09 19:43:13 +00:00
|
|
|
orbitMap = [('precise', 'POEORB/'),
|
|
|
|
('restituted', 'RESORB/')]
|
2019-01-16 19:40:08 +00:00
|
|
|
|
|
|
|
datefmt = "%Y%m%dT%H%M%S"
|
|
|
|
queryfmt = "%Y-%m-%d"
|
2021-03-09 19:43:13 +00:00
|
|
|
queryfmt2 = "%Y/%m/%d/"
|
|
|
|
|
2019-01-16 19:40:08 +00:00
|
|
|
|
|
|
|
def cmdLineParse():
|
|
|
|
'''
|
|
|
|
Command line parser.
|
|
|
|
'''
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Fetch orbits corresponding to given SAFE package')
|
|
|
|
parser.add_argument('-i', '--input', dest='input', type=str, required=True,
|
2021-03-09 19:43:13 +00:00
|
|
|
help='Path to SAFE package of interest')
|
2019-01-16 19:40:08 +00:00
|
|
|
parser.add_argument('-o', '--output', dest='outdir', type=str, default='.',
|
2021-03-09 19:43:13 +00:00
|
|
|
help='Path to output directory')
|
2019-01-16 19:40:08 +00:00
|
|
|
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def FileToTimeStamp(safename):
|
|
|
|
'''
|
|
|
|
Return timestamp from SAFE name.
|
|
|
|
'''
|
|
|
|
safename = os.path.basename(safename)
|
|
|
|
fields = safename.split('_')
|
2020-05-15 03:31:45 +00:00
|
|
|
sstamp = [] # sstamp for getting SAFE file start time, not needed for orbit file timestamps
|
2019-01-16 19:40:08 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
tstamp = datetime.datetime.strptime(fields[-4], datefmt)
|
2020-05-15 03:31:45 +00:00
|
|
|
sstamp = datetime.datetime.strptime(fields[-5], datefmt)
|
2021-03-09 19:43:13 +00:00
|
|
|
except:
|
|
|
|
p = re.compile(r'(?<=_)\d{8}')
|
|
|
|
dt2 = p.search(safename).group()
|
2019-01-16 19:40:08 +00:00
|
|
|
tstamp = datetime.datetime.strptime(dt2, '%Y%m%d')
|
|
|
|
|
|
|
|
satName = fields[0]
|
|
|
|
|
2020-05-15 03:31:45 +00:00
|
|
|
return tstamp, satName, sstamp
|
2019-01-16 19:40:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MyHTMLParser(HTMLParser):
|
|
|
|
|
2021-03-09 19:43:13 +00:00
|
|
|
def __init__(self, satName, url):
|
2019-01-16 19:40:08 +00:00
|
|
|
HTMLParser.__init__(self)
|
|
|
|
self.fileList = []
|
|
|
|
self.in_td = False
|
|
|
|
self.in_a = False
|
2021-03-09 19:43:13 +00:00
|
|
|
self.in_table = False
|
|
|
|
self._url = url
|
2019-01-16 19:40:08 +00:00
|
|
|
self.satName = satName
|
2021-03-09 19:43:13 +00:00
|
|
|
|
2019-01-16 19:40:08 +00:00
|
|
|
def handle_starttag(self, tag, attrs):
|
|
|
|
if tag == 'td':
|
|
|
|
self.in_td = True
|
2021-03-09 19:43:13 +00:00
|
|
|
elif tag == 'a':
|
2019-01-16 19:40:08 +00:00
|
|
|
self.in_a = True
|
2021-03-09 19:43:13 +00:00
|
|
|
for name, val in attrs:
|
|
|
|
if name == "href":
|
|
|
|
if val.startswith("http"):
|
|
|
|
self._url = val.strip()
|
|
|
|
|
|
|
|
def handle_data(self, data):
|
2019-01-16 19:40:08 +00:00
|
|
|
if self.in_td and self.in_a:
|
2021-03-09 19:43:13 +00:00
|
|
|
if self.satName in data:
|
|
|
|
self.fileList.append((self._url, data.strip()))
|
2019-01-16 19:40:08 +00:00
|
|
|
|
2021-03-09 19:43:13 +00:00
|
|
|
def handle_tag(self, tag):
|
2019-01-16 19:40:08 +00:00
|
|
|
if tag == 'td':
|
|
|
|
self.in_td = False
|
|
|
|
self.in_a = False
|
2021-03-09 19:43:13 +00:00
|
|
|
elif tag == 'a':
|
2019-01-16 19:40:08 +00:00
|
|
|
self.in_a = False
|
2021-03-09 19:43:13 +00:00
|
|
|
self._url = None
|
2019-01-16 19:40:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def download_file(url, outdir='.', session=None):
|
|
|
|
'''
|
|
|
|
Download file to specified directory.
|
|
|
|
'''
|
|
|
|
|
|
|
|
if session is None:
|
|
|
|
session = requests.session()
|
|
|
|
|
|
|
|
path = os.path.join(outdir, os.path.basename(url))
|
|
|
|
print('Downloading URL: ', url)
|
|
|
|
request = session.get(url, stream=True, verify=False)
|
|
|
|
|
|
|
|
try:
|
|
|
|
val = request.raise_for_status()
|
|
|
|
success = True
|
|
|
|
except:
|
|
|
|
success = False
|
|
|
|
|
|
|
|
if success:
|
2021-03-09 19:43:13 +00:00
|
|
|
with open(path, 'wb') as f:
|
2019-01-16 19:40:08 +00:00
|
|
|
for chunk in request.iter_content(chunk_size=1024):
|
|
|
|
if chunk:
|
|
|
|
f.write(chunk)
|
|
|
|
f.flush()
|
|
|
|
|
|
|
|
return success
|
|
|
|
|
|
|
|
|
|
|
|
def fileToRange(fname):
|
|
|
|
'''
|
|
|
|
Derive datetime range from orbit file name.
|
|
|
|
'''
|
|
|
|
|
|
|
|
fields = os.path.basename(fname).split('_')
|
|
|
|
start = datetime.datetime.strptime(fields[-2][1:16], datefmt)
|
|
|
|
stop = datetime.datetime.strptime(fields[-1][:15], datefmt)
|
|
|
|
mission = fields[0]
|
|
|
|
|
|
|
|
return (start, stop, mission)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
'''
|
|
|
|
Main driver.
|
|
|
|
'''
|
|
|
|
|
|
|
|
inps = cmdLineParse()
|
|
|
|
|
2020-05-15 03:56:14 +00:00
|
|
|
fileTS, satName, fileTSStart = FileToTimeStamp(inps.input)
|
2019-01-16 19:40:08 +00:00
|
|
|
print('Reference time: ', fileTS)
|
|
|
|
print('Satellite name: ', satName)
|
2021-03-09 19:43:13 +00:00
|
|
|
|
2019-01-16 19:40:08 +00:00
|
|
|
match = None
|
|
|
|
session = requests.Session()
|
2021-03-09 19:43:13 +00:00
|
|
|
|
2019-01-16 19:40:08 +00:00
|
|
|
for spec in orbitMap:
|
|
|
|
oType = spec[0]
|
|
|
|
|
2021-03-12 15:36:45 +00:00
|
|
|
if oType == 'precise':
|
2021-03-17 14:00:21 +00:00
|
|
|
end_date = fileTS + datetime.timedelta(days=20)
|
2021-03-12 15:36:45 +00:00
|
|
|
elif oType == 'restituted':
|
2021-03-23 22:21:03 +00:00
|
|
|
end_date = fileTS
|
|
|
|
else:
|
2021-03-23 22:21:44 +00:00
|
|
|
raise ValueError("Unexpected orbit type: '" + oType + "'")
|
2021-03-24 00:07:19 +00:00
|
|
|
end_date2 = end_date + datetime.timedelta(days=1)
|
2021-03-23 22:21:03 +00:00
|
|
|
url = server + spec[1] + str(end_date.year).zfill(2) + '/' + str(end_date.month).zfill(2) + \
|
2021-03-24 00:07:19 +00:00
|
|
|
'/' + str(end_date.day).zfill(2) + '/', server + spec[1] + str(end_date2.year).zfill(2) + '/' + str(end_date2.month).zfill(2) + \
|
|
|
|
'/' + str(end_date2.day).zfill(2) + '/'
|
2021-03-14 13:11:21 +00:00
|
|
|
|
2019-01-16 19:40:08 +00:00
|
|
|
success = False
|
|
|
|
match = None
|
2021-03-09 19:43:13 +00:00
|
|
|
|
2019-01-16 19:40:08 +00:00
|
|
|
try:
|
2021-03-24 00:07:19 +00:00
|
|
|
for url1 in url:
|
|
|
|
r = session.get(url1, verify=False)
|
|
|
|
r.raise_for_status()
|
|
|
|
parser = MyHTMLParser(satName, url1)
|
|
|
|
parser.feed(r.text)
|
2021-03-09 19:43:13 +00:00
|
|
|
for resulturl, result in parser.fileList:
|
2021-03-17 13:20:52 +00:00
|
|
|
if oType == 'precise':
|
|
|
|
match = os.path.join(resulturl, result)
|
2021-03-23 11:01:01 +00:00
|
|
|
elif oType == "restituted":
|
2021-03-17 13:20:52 +00:00
|
|
|
tbef, taft, mission = fileToRange(os.path.basename(result))
|
|
|
|
if (tbef <= fileTSStart) and (taft >= fileTS):
|
2021-03-23 11:01:01 +00:00
|
|
|
match = os.path.join(resulturl, result)
|
2021-03-17 13:20:52 +00:00
|
|
|
|
|
|
|
if match is not None:
|
|
|
|
success = True
|
2021-03-09 19:43:13 +00:00
|
|
|
except:
|
2019-01-16 19:40:08 +00:00
|
|
|
pass
|
2021-03-14 13:11:21 +00:00
|
|
|
|
2021-03-12 15:36:45 +00:00
|
|
|
if success:
|
|
|
|
break
|
2021-03-14 13:11:21 +00:00
|
|
|
|
2021-03-12 15:36:45 +00:00
|
|
|
if match is not None:
|
2021-03-14 13:11:21 +00:00
|
|
|
|
2021-03-12 15:36:45 +00:00
|
|
|
res = download_file(match, inps.outdir, session)
|
|
|
|
if res is False:
|
|
|
|
print('Failed to download URL: ', match)
|
|
|
|
else:
|
|
|
|
print('Failed to find {1} orbits for tref {0}'.format(fileTS, satName))
|