def test_SentinelAPI_wrong_credentials(): api = SentinelAPI("wrong_user", "wrong_password") with pytest.raises(SentinelAPIError) as excinfo: api.query('0 0,1 1,0 1,0 0', datetime(2015, 1, 1), datetime(2015, 1, 2)) assert excinfo.value.http_status == 401 with pytest.raises(SentinelAPIError): api.get_products_size() api.get_products()
def test_SentinelAPI_wrong_credentials(): api = SentinelAPI( "wrong_user", "wrong_password" ) api.query('0 0,1 1,0 1,0 0', datetime(2015, 1, 1), datetime(2015, 1, 2)) assert api.content.status_code == 401 with pytest.raises(ValueError): api.get_products_size() api.get_products()
def test_s2_cloudcover(): api = SentinelAPI(**_api_auth) api.query( get_coordinates('tests/map.geojson'), "20151219", "20151228", platformname="Sentinel-2", cloudcoverpercentage="[0 TO 10]" ) assert len(api.get_products()) == 3 assert api.get_products()[0]["id"] == "6ed0b7de-3435-43df-98bf-ad63c8d077ef" assert api.get_products()[1]["id"] == "37ecee60-23d8-4ec2-a65f-2de24f51d30e" assert api.get_products()[2]["id"] == "0848f6b8-5730-4759-850e-fc9945d42296"
def test_get_products_invalid_json(): api = SentinelAPI("mock_user", "mock_password") with requests_mock.mock() as rqst: rqst.post('https://scihub.copernicus.eu/apihub/search?format=json', text="{Invalid JSON response", status_code=200) with pytest.raises(SentinelAPIError) as excinfo: api.query(area=get_coordinates("tests/map.geojson"), initial_date="20151219", end_date="20151228", platformname="Sentinel-2") api.get_products() assert excinfo.value.msg == "API response not valid. JSON decoding failed."
def search( user, password, tile, geojson, start, end, download, md5, sentinel1, sentinel2, cloud, footprints, path, query, url): """Search for Sentinel products and, optionally, download all the results and/or create a geojson file with the search result footprints. 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 specify the start and end dates, it will search in the last 24 hours. """ api = SentinelAPI(user, password, url) search_kwargs = {} if cloud: search_kwargs.update( {"platformname": "Sentinel-2", "cloudcoverpercentage": "[0 TO %s]" % cloud}) elif sentinel2: search_kwargs.update({"platformname": "Sentinel-2"}) elif sentinel1: search_kwargs.update({"platformname": "Sentinel-1"}) if query is not None: search_kwargs.update(dict([i.split('=') for i in query.split(',')])) if tile: api.query(point = get_coordinates(tile = tile), initial_date = start, end_date = end, **search_kwargs) elif geojson: api.query(area = get_coordinates(geojson_file = geojson), initial_date = start, end_date = end, **search_kwargs) else: raise ValueError("Either a --geojson or --tile arguments must be given.") if footprints is True: footprints_geojson = api.get_footprints() with open(os.path.join(path, "search_footprints.geojson"), "w") as outfile: outfile.write(gj.dumps(footprints_geojson)) if download is True: result = api.download_all(path, checksum=md5) if md5 is True: corrupt_scenes = [(path, info["id"]) for path, info in result.items() if info is not None] if len(corrupt_scenes) > 0: with open(os.path.join(path, "corrupt_scenes.txt"), "w") as outfile: for corrupt_tuple in corrupt_scenes: outfile.write("%s : %s\n" % corrupt_tuple) else: for product in api.get_products(): print('Product %s - %s' % (product['id'], product['summary'])) print('---') print( '%s scenes found with a total size of %.2f GB' % (len(api.get_products()), api.get_products_size()))
def test_s2_cloudcover(): api = SentinelAPI(environ['SENTINEL_USER'], environ['SENTINEL_PASSWORD']) api.query(get_coordinates('tests/map.geojson'), "20151219", "20151228", platformname="Sentinel-2", cloudcoverpercentage="[0 TO 10]") assert len(api.get_products()) == 3 assert api.get_products( )[0]["id"] == "6ed0b7de-3435-43df-98bf-ad63c8d077ef" assert api.get_products( )[1]["id"] == "37ecee60-23d8-4ec2-a65f-2de24f51d30e" assert api.get_products( )[2]["id"] == "0848f6b8-5730-4759-850e-fc9945d42296"
def test_s2_cloudcover(): api = SentinelAPI( environ.get('SENTINEL_USER'), environ.get('SENTINEL_PASSWORD') ) api.query( get_coordinates('tests/map.geojson'), "20151219", "20151228", platformname="Sentinel-2", cloudcoverpercentage="[0 TO 10]" ) assert len(api.get_products()) == 2 assert api.get_products()[0]["id"] == "37ecee60-23d8-4ec2-a65f-2de24f51d30e" assert api.get_products()[1]["id"] == "0848f6b8-5730-4759-850e-fc9945d42296"
def test_download_all(tmpdir): api = SentinelAPI(**_api_auth) # From https://scihub.copernicus.eu/apihub/odata/v1/Products?$top=5&$orderby=ContentLength filenames = ["S1A_WV_OCN__2SSH_20150603T092625_20150603T093332_006207_008194_521E", "S1A_WV_OCN__2SSV_20150526T211029_20150526T211737_006097_007E78_134A", "S1A_WV_OCN__2SSV_20150526T081641_20150526T082418_006090_007E3E_104C"] api.load_query(" OR ".join(filenames)) assert len(api.get_products()) == len(filenames) # Download normally result = api.download_all(str(tmpdir)) assert len(result) == len(filenames) for path, product_info in result.items(): pypath = py.path.local(path) assert pypath.purebasename in filenames assert pypath.check(exists=1, file=1) assert pypath.size() == product_info["size"] # Force one download to fail path, product_info = list(result.items())[0] py.path.local(path).remove() with requests_mock.mock(real_http=True) as rqst: url = "https://scihub.copernicus.eu/apihub/odata/v1/Products('%s')/?$format=json" % product_info["id"] json = api.session.get(url).json() json["d"]["Checksum"]["Value"] = "00000000000000000000000000000000" rqst.get(url, json=json) result = api.download_all(str(tmpdir), max_attempts=1, checksum=True) assert len(result) == len(filenames) assert result[path] is None
def test_get_products_invalid_json(): api = SentinelAPI("mock_user", "mock_password") with requests_mock.mock() as rqst: rqst.post( 'https://scihub.copernicus.eu/apihub/search?format=json', text="{Invalid JSON response", status_code=200 ) with pytest.raises(SentinelAPIError) as excinfo: api.query( area=get_coordinates("tests/map.geojson"), initial_date="20151219", end_date="20151228", platformname="Sentinel-2" ) api.get_products() assert excinfo.value.msg == "API response not valid. JSON decoding failed."
def test_download_all(tmpdir): api = SentinelAPI(environ['SENTINEL_USER'], environ['SENTINEL_PASSWORD']) # From https://scihub.copernicus.eu/apihub/odata/v1/Products?$top=5&$orderby=ContentLength filenames = [ "S1A_WV_OCN__2SSH_20150603T092625_20150603T093332_006207_008194_521E", "S1A_WV_OCN__2SSV_20150526T211029_20150526T211737_006097_007E78_134A", "S1A_WV_OCN__2SSV_20150526T081641_20150526T082418_006090_007E3E_104C" ] api.query_raw(" OR ".join(filenames)) assert len(api.get_products()) == len(filenames) # Download normally result = api.download_all(str(tmpdir)) assert len(result) == len(filenames) for path, product_info in result.items(): pypath = py.path.local(path) assert pypath.purebasename in filenames assert pypath.check(exists=1, file=1) assert pypath.size() == product_info["size"] # Force one download to fail path, product_info = list(result.items())[0] py.path.local(path).remove() with requests_mock.mock(real_http=True) as rqst: url = "https://scihub.copernicus.eu/apihub/odata/v1/Products('%s')/?$format=json" % product_info[ "id"] json = api.session.get(url).json() json["d"]["Checksum"]["Value"] = "00000000000000000000000000000000" rqst.get(url, json=json) result = api.download_all(str(tmpdir), max_attempts=1, checksum=True) assert len(result) == len(filenames) assert result[path] is None
def test_get_products_invalid_json(): api = SentinelAPI("mock_user", "mock_password") with requests_mock.mock() as rqst: rqst.get( 'https://scihub.copernicus.eu/apihub/search?format=json&rows=15000&q=(beginPosition:[2015-12-19T00:00:00Z TO 2015-12-28T00:00:00Z]) AND (footprint:"Intersects(POLYGON((-66.2695312 -8.0592296,-66.2695312 0.7031074,-57.3046875 0.7031074,-57.3046875 -8.0592296,-66.2695312 -8.0592296)))") AND (platformname:Sentinel-2)', text="Invalid JSON response", status_code=200 ) api.query( area=get_coordinates("tests/map.geojson"), initial_date="20151219", end_date="20151228", platformname="Sentinel-2" ) with pytest.raises(ValueError) as val_err: api.get_products() assert val_err.value.message == "API response not valid. JSON decoding failed."
def search( user, password, geojson, start, end, download, md5, sentinel1, sentinel2, cloud, footprints, path, query, url): """Search for Sentinel products and, optionally, download all the results and/or create a geojson file with the search result footprints. 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 specify the start and end dates, it will search in the last 24 hours. """ api = SentinelAPI(user, password, url) search_kwargs = {} if cloud: search_kwargs.update( {"platformname": "Sentinel-2", "cloudcoverpercentage": "[0 TO %s]" % cloud}) elif sentinel2: search_kwargs.update({"platformname": "Sentinel-2"}) elif sentinel1: search_kwargs.update({"platformname": "Sentinel-1"}) if query is not None: search_kwargs.update(dict([i.split('=') for i in query.split(',')])) api.query(get_coordinates(geojson), start, end, **search_kwargs) if footprints is True: footprints_geojson = api.get_footprints() with open(os.path.join(path, "search_footprints.geojson"), "w") as outfile: outfile.write(gj.dumps(footprints_geojson)) if download is True: result = api.download_all(path, checksum=md5) if md5 is True: corrupt_scenes = [(path, info["id"]) for path, info in result.items() if info is not None] if len(corrupt_scenes) > 0: with open(os.path.join(path, "corrupt_scenes.txt"), "w") as outfile: for corrupt_tuple in corrupt_scenes: outfile.write("%s : %s\n" % corrupt_tuple) else: for product in api.get_products(): print('Product %s - %s' % (product['id'], product['summary'])) print('---') print( '%s scenes found with a total size of %.2f GB' % (len(api.get_products()), api.get_products_size()))
def test_get_products_size(): api = SentinelAPI(environ['SENTINEL_USER'], environ['SENTINEL_PASSWORD']) api.query(get_coordinates('tests/map.geojson'), "20151219", "20151228", platformname="Sentinel-2") assert api.get_products_size() == 63.58 api.query_raw( "S1A_WV_OCN__2SSH_20150603T092625_20150603T093332_006207_008194_521E") assert len(api.get_products()) > 0 # Rounded to zero assert api.get_products_size() == 0
def test_get_products_size(): api = SentinelAPI(**_api_auth) api.query( get_coordinates('tests/map.geojson'), "20151219", "20151228", platformname="Sentinel-2" ) assert api.get_products_size() == 63.58 # reset products api.products = [] # load new very small query api.load_query("S1A_WV_OCN__2SSH_20150603T092625_20150603T093332_006207_008194_521E") assert len(api.get_products()) > 0 # Rounded to zero assert api.get_products_size() == 0
def test_get_products_size(): api = SentinelAPI(**_api_auth) api.query(get_coordinates('tests/map.geojson'), "20151219", "20151228", platformname="Sentinel-2") assert api.get_products_size() == 63.58 # reset products api.products = [] # load new very small query api.load_query( "S1A_WV_OCN__2SSH_20150603T092625_20150603T093332_006207_008194_521E") assert len(api.get_products()) > 0 # Rounded to zero assert api.get_products_size() == 0
def search(user, password, geojson, start, end, download, footprints, path, query): """Search for Sentinel-1 products and, optionally, download all the results and/or create a geojson file with the search result footprints. 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 specify the start and end dates, it will search in the last 24 hours. """ api = SentinelAPI(user, password) 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 footprints is True: footprints_geojson = api.get_footprints() with open(os.path.join(path, "search_footprints.geojson"), "w") as outfile: outfile.write(gj.dumps(footprints_geojson)) if download is True: api.download_all(path) else: for product in api.get_products(): print('Product %s - %s' % (product['id'], product['summary']))