Skip to content

Commit

Permalink
remove unnedded _url_trail_slash method and improve pep8 style
Browse files Browse the repository at this point in the history
  • Loading branch information
willemarcel committed Jan 4, 2016
1 parent 7b0d399 commit 0f8aeab
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 61 deletions.
48 changes: 27 additions & 21 deletions sentinelsat/sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from urlparse import urljoin
except:
from urllib.parse import urljoin
from os.path import join, exists, getsize
from os.path import join, exists
from os import remove


Expand Down Expand Up @@ -53,7 +53,7 @@ class SentinelAPI(object):
def __init__(self, user, password, api_url='https://scihub.copernicus.eu/apihub/'):
self.session = requests.Session()
self.session.auth = (user, password)
self.api_url = self._url_trail_slash(api_url)
self.api_url = api_url

def query(self, area, initial_date=None, end_date=datetime.now(), **keywords):
"""Query the SciHub API with the coordinates of an area, a date inverval
Expand All @@ -68,13 +68,6 @@ def query(self, area, initial_date=None, end_date=datetime.now(), **keywords):
except requests.exceptions.RequestException as exc:
print('Error: {}'.format(exc))

@staticmethod
def _url_trail_slash(api_url):
"""Add trailing slash to the api url if it is missing"""
if api_url[-1] is not '/':
api_url += '/'
return api_url

def format_url(self, area, initial_date=None, end_date=datetime.now(), **keywords):
"""Create the URL to access the SciHub API, defining the max quantity of
results to 15000 items.
Expand Down Expand Up @@ -133,24 +126,37 @@ def get_footprints(self):
for scene in self.get_products():
id += 1
# parse the polygon
coord_list = next(x for x in scene["str"] if x["name"] == "footprint")["content"][10:-2].split(",")
coord_list = next(
x
for x in scene["str"]
if x["name"] == "footprint"
)["content"][10:-2].split(",")
coord_list_split = [coord.split(" ") for coord in coord_list]
poly = geojson.Polygon(
[[tuple((float(coord[0]), float(coord[1]))) for coord in coord_list_split]]
)
poly = geojson.Polygon([[
tuple((float(coord[0]), float(coord[1])))
for coord in coord_list_split
]])

# parse the following properties:
# platformname, identifier, product_id, date, polarisation,
# sensor operation mode, orbit direction, product type, download link
props = {
"product_id": scene["id"],
"date_beginposition": next(x for x in scene["date"] if x["name"] == "beginposition")["content"],
"download_link": next(x for x in scene["link"] if len(x.keys()) == 1)["href"]
"product_id": scene["id"],
"date_beginposition": next(
x
for x in scene["date"]
if x["name"] == "beginposition"
)["content"],
"download_link": next(
x
for x in scene["link"]
if len(x.keys()) == 1
)["href"]
}
str_properties = ["platformname", "identifier", "polarisationmode",
"sensoroperationalmode", "orbitdirection", "producttype"]
for str_prop in str_properties:
props.update({str_prop : next(x for x in scene["str"] if x["name"] == str_prop)["content"]})
props.update({str_prop: next(x for x in scene["str"] if x["name"] == str_prop)["content"]})

feature_list.append(
geojson.Feature(geometry=poly, id=id, properties=props)
Expand All @@ -159,8 +165,8 @@ def get_footprints(self):

def get_product_info(self, id):
"""Access SciHub API to get info about a Product. Returns a dict
containing the id, title, size, md5sum, date, footprint and download url of the
Product. The date field receives the Start ContentDate of the API.
containing the id, title, size, md5sum, date, footprint and download url
of the Product. The date field receives the Start ContentDate of the API.
"""

product = self.session.get(
Expand All @@ -181,7 +187,7 @@ def get_product_info(self, id):
.findtext('{http://www.opengis.net/gml}coordinates')
coord_string = ",".join(
[" ".join(double_coord) for double_coord in [coord.split(",") for coord in poly_coords.split(" ")]]
)
)

keys = ['id', 'title', 'size', 'md5', 'date', 'footprint', 'url']
values = [
Expand Down Expand Up @@ -264,7 +270,7 @@ def _fillin_cainfo(kwargs_dict):
except AttributeError:
cainfo = None

if cainfo != None:
if cainfo is not None:
pass_through_opts = kwargs_dict.get('pass_through_opts', {})
pass_through_opts[CAINFO] = cainfo
kwargs_dict['pass_through_opts'] = pass_through_opts
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


setup(name='sentinelsat',
version='0.5.2',
version='0.5.3',
description="Utility to search and download Sentinel-1 Imagery",
long_description=long_description,
classifiers=[
Expand Down
63 changes: 24 additions & 39 deletions tests/test_mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from sentinelsat.sentinel import (SentinelAPI, format_date, get_coordinates,
convert_timestamp, md5_compare)


@pytest.mark.fast
def test_format_date():
assert format_date(datetime(2015, 1, 1)) == '2015-01-01T00:00:00Z'
Expand All @@ -29,16 +30,16 @@ def test_md5_comparison():
with open("tests/expected_search_footprints.geojson", "rb") as testfile:
testfile_md5.update(testfile.read())
real_md5 = testfile_md5.hexdigest()
assert md5_compare("tests/expected_search_footprints.geojson", real_md5) == True
assert md5_compare("tests/map.geojson", real_md5) == False
assert md5_compare("tests/expected_search_footprints.geojson", real_md5) is True
assert md5_compare("tests/map.geojson", real_md5) is False


@pytest.mark.scihub
def test_SentinelAPI():
api = SentinelAPI(
environ.get('SENTINEL_USER'),
environ.get('SENTINEL_PASSWORD')
)
)
api.query('0 0,1 1,0 1,0 0', datetime(2015, 1, 1), datetime(2015, 1, 2))

assert api.url == 'https://scihub.copernicus.eu/apihub/search?format=json&rows=15000' + \
Expand Down Expand Up @@ -66,7 +67,7 @@ def test_set_base_url():
environ.get('SENTINEL_USER'),
environ.get('SENTINEL_PASSWORD'),
'https://scihub.copernicus.eu/dhus/'
)
)
api.query('0 0,1 1,0 1,0 0', datetime(2015, 1, 1), datetime(2015, 1, 2))

assert api.url == 'https://scihub.copernicus.eu/dhus/search?format=json&rows=15000' + \
Expand All @@ -75,30 +76,10 @@ def test_set_base_url():
assert api.content.status_code == 200


@pytest.mark.fast
def test_trail_slash_base_url():
base_urls = [
'https://scihub.copernicus.eu/dhus/',
'https://scihub.copernicus.eu/dhus'
]

expected = 'https://scihub.copernicus.eu/dhus/'

for test_url in base_urls:
assert SentinelAPI._url_trail_slash(test_url) == expected
api = SentinelAPI(
environ.get('SENTINEL_USER'),
environ.get('SENTINEL_PASSWORD'),
test_url
)
assert api.api_url == expected


@pytest.mark.fast
def test_get_coordinates():
coords = '-66.2695312 -8.0592296,-66.2695312 0.7031074,' + \
'-57.3046875 0.7031074,-57.3046875 -8.0592296,' +\
'-66.2695312 -8.0592296'
coords = ('-66.2695312 -8.0592296,-66.2695312 0.7031074,' +
'-57.3046875 0.7031074,-57.3046875 -8.0592296,-66.2695312 -8.0592296')
assert get_coordinates('tests/map.geojson') == coords


Expand All @@ -107,16 +88,17 @@ def test_get_product_info():
api = SentinelAPI(
environ.get('SENTINEL_USER'),
environ.get('SENTINEL_PASSWORD')
)
)

expected = {'id': '8df46c9e-a20c-43db-a19a-4240c2ed3b8b',
expected = {
'id': '8df46c9e-a20c-43db-a19a-4240c2ed3b8b',
'size': int(143549851),
'md5': 'D5E4DF5C38C6E97BF7E7BD540AB21C05',
'url': "https://scihub.copernicus.eu/apihub/odata/v1/Products('8df46c9e-a20c-43db-a19a-4240c2ed3b8b')/$value",
'date': '2015-11-21T10:03:56Z', 'size': 143549851,
'footprint': '-5.880887 -63.852531,-5.075419 -67.495872,-3.084356 -67.066071,-3.880541 -63.430576,-5.880887 -63.852531',
'title': 'S1A_EW_GRDM_1SDV_20151121T100356_20151121T100429_008701_00C622_A0EC'
}
}
assert api.get_product_info('8df46c9e-a20c-43db-a19a-4240c2ed3b8b') == expected


Expand All @@ -125,22 +107,25 @@ def test_get_product_info_scihub_down():
api = SentinelAPI("mock_user", "mock_password")
with requests_mock.mock() as rqst:
rqst.get(
"https://scihub.copernicus.eu/apihub/odata/v1/Products('8df46c9e-a20c-43db-a19a-4240c2ed3b8b')/?$format=json",
text="Mock SciHub is Down", status_code=503
)
"https://scihub.copernicus.eu/apihub/odata/v1/Products('8df46c9e-a20c-43db-a19a-4240c2ed3b8b')/?$format=json",
text="Mock SciHub is Down", status_code=503
)
with pytest.raises(ValueError):
api.get_product_info('8df46c9e-a20c-43db-a19a-4240c2ed3b8b')


@pytest.mark.scihub
def test_footprints():
api = SentinelAPI(
environ.get('SENTINEL_USER'),
environ.get('SENTINEL_PASSWORD')
)
api.query(get_coordinates('tests/map.geojson'),
datetime(2014, 10, 10), datetime(2014, 12, 31), producttype="GRD")

expected_footprints = geojson.loads(open('tests/expected_search_footprints.geojson', 'r').read())
)
api.query(
get_coordinates('tests/map.geojson'),
datetime(2014, 10, 10), datetime(2014, 12, 31), producttype="GRD"
)

# to compare unordered lists (JSON objects) they need to be sorted or changed to sets
assert set(api.get_footprints()) == set(expected_footprints)
with open('tests/expected_search_footprints.geojson', 'r') as geojson_file:
expected_footprints = geojson.loads(geojson_file.read())
# to compare unordered lists (JSON objects) they need to be sorted or changed to sets
assert set(api.get_footprints()) == set(expected_footprints)

0 comments on commit 0f8aeab

Please sign in to comment.