Skip to content

Commit

Permalink
add -query parameter on cli and avoid error in get_products method
Browse files Browse the repository at this point in the history
  • Loading branch information
willemarcel committed Jun 10, 2015
1 parent d961955 commit 9ce937c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ Options:
-e, --end TEXT End date of the query in the format YYYYMMDD.
-d, --download Download all results of the query.
-p, --path PATH Set the path where the files will be saved.
-q, --query TEXT Extra search keywords you want to use in the query.
Separate keywords with comma.
Example: 'producttype=GRD,polarisationmode=HH'.

Download
^^^^^^^^
Expand Down
13 changes: 11 additions & 2 deletions sentinelsat/scripts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,23 @@ def cli():
help='Download all results of the query.')
@click.option('--path', '-p', type=click.Path(exists=True), default='.',
help='Set the path where the files will be saved.')
def search(user, password, geojson, start, end, download, path):
@click.option('--query', '-q', type=str, default=None,
help="""Extra search keywords you want to use in the query. Separate
keywords with comma. Example: 'producttype=GRD,polarisationmode=HH'.
""")
def search(user, password, geojson, start, end, download, path, query):
"""Search for Sentinel-1 products and, optionally, download all the results.
Beyond your scihub user and password, you must pass a geojson file
containing the polygon of the area you want to search for. If you
don't especify the start and end dates, it will search in the last 24 hours.
"""
api = SentinelAPI(user, password)
api.query(get_coordinates(geojson), start, end)
if query is not None:
query = dict([i.split('=') for i in query.split(',')])
api.query(get_coordinates(geojson), start, end, **query)
else:
api.query(get_coordinates(geojson), start, end)

if download is True:
api.download_all(path)
else:
Expand Down
13 changes: 10 additions & 3 deletions sentinelsat/sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def query(self, area, initial_date=None, end_date=datetime.now(), **keywords):
print(('Query returned %s error.' % self.content.status_code))

def format_url(self, area, initial_date=None, end_date=datetime.now(), **keywords):
"""Create the URL to access the SciHub API."""
"""Create the URL to access the SciHub API, defining the max quantity of
results to 50 items.
"""
if initial_date is None:
initial_date = end_date - timedelta(hours=24)

Expand All @@ -55,14 +57,19 @@ def format_url(self, area, initial_date=None, end_date=datetime.now(), **keyword
for kw in sorted(keywords.keys()):
filters += ' AND (%s:%s)' % (kw, keywords[kw])

self.url = 'https://scihub.esa.int/dhus/search?format=json&q=%s%s%s' \
self.url = 'https://scihub.esa.int/dhus/search?format=json&rows=50&q=%s%s%s' \
% (ingestion_date, query_area, filters)

def get_products(self):
"""Return the result of the Query in json format."""
try:
self.products = self.content.json()['feed']['entry']
return self.products
# this verification is necessary because if the query returns only
# one product, self.products will be a dict not a list
if type(self.products) == dict:
return [self.products]
else:
return self.products
except KeyError:
print('No products found in this query.')
return []
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.2.1',
version='0.3',
description="Utility to search and download Sentinel-1 Imagery",
long_description=long_description,
classifiers=[
Expand Down
10 changes: 10 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ def test_cli():
'tests/map.geojson'
])
assert result.exit_code == 0

result = runner.invoke(cli,
['search',
environ.get('SENTINEL_USER'),
environ.get('SENTINEL_PASSWORD'),
'tests/map.geojson',
'-q',
'producttype=GRD,polarisationmode=HH'
])
assert result.exit_code == 0
6 changes: 3 additions & 3 deletions tests/test_mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ def test_SentinelAPI():
)
api.query('0 0,1 1,0 1,0 0', datetime(2015, 1, 1), datetime(2015, 1, 2))

assert api.url == 'https://scihub.esa.int/dhus/search?format=json' + \
assert api.url == 'https://scihub.esa.int/dhus/search?format=json&rows=50' + \
'&q=(ingestionDate:[2015-01-01T00:00:00Z TO 2015-01-02T00:00:00Z]) ' + \
'AND (footprint:"Intersects(POLYGON((0 0,1 1,0 1,0 0)))")'
assert api.content.status_code == 200

now = datetime.now()
api.format_url('0 0,1 1,0 1,0 0', end_date=now)
last_24h = format_date(now - timedelta(hours=24))
assert api.url == 'https://scihub.esa.int/dhus/search?format=json' + \
assert api.url == 'https://scihub.esa.int/dhus/search?format=json&rows=50' + \
'&q=(ingestionDate:[%s TO %s]) ' % (last_24h, format_date(now)) + \
'AND (footprint:"Intersects(POLYGON((0 0,1 1,0 1,0 0)))")'

api.format_url('0 0,1 1,0 1,0 0', end_date=now, producttype='SLC')
assert api.url == 'https://scihub.esa.int/dhus/search?format=json' + \
assert api.url == 'https://scihub.esa.int/dhus/search?format=json&rows=50' + \
'&q=(ingestionDate:[%s TO %s]) ' % (last_24h, format_date(now)) + \
'AND (footprint:"Intersects(POLYGON((0 0,1 1,0 1,0 0)))") ' + \
'AND (producttype:SLC)'
Expand Down

0 comments on commit 9ce937c

Please sign in to comment.