Skip to content

Instantly share code, notes, and snippets.

@brwe
Created May 31, 2021 22:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brwe/ff7db758d3f6ea270113cd7fd86680cb to your computer and use it in GitHub Desktop.
Save brwe/ff7db758d3f6ea270113cd7fd86680cb to your computer and use it in GitHub Desktop.
get all orbits between two dates
import requests
from pathlib import Path
from datetime import date, timedelta
from requests.auth import HTTPBasicAuth
import xml.etree.ElementTree as ET
import os
def save_all_orbits(date_str, platformnumber, producttype, result_path, session):
base_url = f"https://scihub.copernicus.eu/gnss/search?q=platformname:Sentinel-1 AND platformnumber:{platformnumber} AND producttype:{producttype} AND beginposition:[{date_str}T00:00:000Z TO {date_str}T24:00:000Z]"
start = 0
rows = 10
url = f'{base_url}&start={start}&rows={rows}'
response = session.get(url, auth=HTTPBasicAuth('gnssguest', 'gnssguest'))
root = ET.fromstring(str(response.text))
num_results=root.findall('{http://a9.com/-/spec/opensearch/1.1/}totalResults')[0].text
print(f"Found results for {platformnumber} {producttype} {date_str}: {num_results}")
while start < int(num_results):
url = f'{base_url}&start={start}&rows={rows}'
response = session.get(url, auth=HTTPBasicAuth('gnssguest', 'gnssguest'))
root = ET.fromstring(str(response.text))
entries = root.findall('{http://a9.com/-/spec/opensearch/1.1/}totalResults')
for elem in root.findall('{http://www.w3.org/2005/Atom}entry'):
start = start +1
title = elem.findall('{http://www.w3.org/2005/Atom}title')[0].text
for at in elem:
if at.tag == '{http://www.w3.org/2005/Atom}link':
if 'rel' in at.attrib:
continue
href = at.attrib['href']
local_filename = result_path/Path(producttype[4:len(producttype)])/Path(f'S1{platformnumber}')/Path(date_str[0:4])/Path(date_str[5:7])/Path(title).with_suffix('.EOF')
local_filename.parent.mkdir(parents=True, exist_ok=True)
with session.get(href, stream=True, auth=HTTPBasicAuth('gnssguest', 'gnssguest')) as r:
r.raise_for_status()
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
home = os.environ['HOME']
result_path = Path(f'{home}/.snap/auxdata/Orbits/Sentinel-1/')
result_path.mkdir(parents=True, exist_ok=True)
start_date = date(2021, 1, 1)
end_date = date(2021, 5, 31)
delta = timedelta(days=1)
session = requests.session()
while start_date <= end_date:
date_str = start_date.strftime("%Y-%m-%d")
save_all_orbits(date_str, "A", "AUX_RESORB", result_path, session)
save_all_orbits(date_str, "B", "AUX_RESORB", result_path, session)
save_all_orbits(date_str, "A", "AUX_POEORB", result_path, session)
save_all_orbits(date_str, "B", "AUX_POEORB", result_path, session)
start_date += delta
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment