Skip to content

Instantly share code, notes, and snippets.

@mraspaud
Last active August 29, 2015 14:11
Show Gist options
  • Save mraspaud/c6dd4ba8cd84c8d3499f to your computer and use it in GitHub Desktop.
Save mraspaud/c6dd4ba8cd84c8d3499f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014 Martin Raspaud
# Author(s):
# Martin Raspaud <martin.raspaud@smhi.se>
# 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/>.
"""
"""
import subprocess as sp
import requests
import os
from StringIO import StringIO
from zipfile import ZipFile
from datetime import datetime, timedelta
import feedparser
auth = ("my_username", "my_password")
destination = "/path/to/sentinel-1/data"
proc = sp.Popen(["curl", "-gu", ":".join(auth),
"https://scihub.esa.int/dhus/search?q=ingestiondate:[NOW-7DAYS+TO+NOW]+AND+producttype:GRD+AND+swathIdentifier:\"EW\"+AND+footprint:\"Intersects(POLYGON+((6.13+53.43,6.13+66.15,30.30+66.12,30.30+53.43,6.13+53.43)))\"&rows=10000&start=0"
], stdout=sp.PIPE, stderr=sp.PIPE)
print "At", datetime.utcnow(), "requesting files over the baltic sea."
text, errors = proc.communicate()
d = feedparser.parse(text)
print d.feed.title
print "Got", len(d.entries), "hits"
def download_files(url, dest_dir):
r = requests.get(url, auth=auth)
zfile = ZipFile(StringIO(r.content))
zfile.extractall(dest_dir)
for entry in d.entries:
dest_dir = destination
fname = os.path.join(dest_dir, entry.title + ".SAFE")
if not os.path.exists(fname):
url = "https://scihub.esa.int/dhus/odata/v1/Products('" + \
entry.id + "')/$value"
print "downloading", fname, "..."
download_files(url, dest_dir)
print "Ok, got", fname
else:
print "skipping", fname
print "Done for now, see you later"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment