Skip to content

Commit c36e927

Browse files
author
Scisco
committed
Merge pull request #152 from developmentseed/usgs-fallback
Usgs fallback
2 parents 9a1610d + d2c0faf commit c36e927

File tree

10 files changed

+58
-12
lines changed

10 files changed

+58
-12
lines changed

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ deploy:
3232
repo: developmentseed/landsat-util
3333
branch:
3434
- master
35-
- develop
3635

3736
after_deploy:
3837
if [ "$TRAVIS_BRANCH" == "master" ]; then

CHANGES.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changes
22
=======
33

4+
0.12.0 (2016-02-18)
5+
------------------
6+
- Add USGS download fallback closes #89
7+
48
0.11.0 (2016-01-12)
59
------------------
610
- a hotfix for search command not showing outputs #137

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM ubuntu:14.04
22
RUN apt-get -y update
3-
RUN apt-get install --yes python-pip python-skimage python-numpy python-scipy libgdal-dev libatlas-base-dev gfortran libfreetype6-dev libglib2.0-dev zlib1g-dev python-pycurl
3+
RUN apt-get install --yes git-core python-pip python-skimage python-numpy python-scipy libgdal-dev libatlas-base-dev gfortran libfreetype6-dev libglib2.0-dev zlib1g-dev python-pycurl
44
ADD landsat /usr/local/lib/python2.7/dist-packages/landsat
55
ADD bin/landsat /usr/local/bin/
66
ADD . /landsat

landsat/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.11.0'
1+
__version__ = '0.12.0'

landsat/downloader.py

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Landsat Util
22
# License: CC0 1.0 Universal
3+
from xml.etree import ElementTree
34
from os.path import join, exists, getsize
45

5-
from homura import download as fetch
66
import requests
7+
from usgs import api, USGSError
8+
from homura import download as fetch
79

810
from utils import check_create_folder, url_builder
911
from mixins import VerbosityMixin
@@ -20,13 +22,20 @@ class IncorrectSceneId(Exception):
2022
pass
2123

2224

25+
class USGSInventoryAccessMissing(Exception):
26+
""" Exception for when User does not have Inventory Service access """
27+
pass
28+
29+
2330
class Downloader(VerbosityMixin):
2431
""" The downloader class """
2532

26-
def __init__(self, verbose=False, download_dir=None):
33+
def __init__(self, verbose=False, download_dir=None, usgs_user=None, usgs_pass=None):
2734
self.download_dir = download_dir if download_dir else settings.DOWNLOAD_DIR
2835
self.google = settings.GOOGLE_STORAGE
2936
self.s3 = settings.S3_LANDSAT
37+
self.usgs_user = usgs_user
38+
self.usgs_pass = usgs_pass
3039

3140
# Make sure download directory exist
3241
check_create_folder(self.download_dir)
@@ -110,7 +119,22 @@ def google_storage(self, scene, path):
110119
return self.fetch(url, path, filename)
111120

112121
else:
113-
raise RemoteFileDoesntExist('%s is not available on Google Storage' % filename)
122+
# download from usgs if login information is provided
123+
if self.usgs_user and self.usgs_pass:
124+
try:
125+
api_key = api.login(self.usgs_user, self.usgs_pass)
126+
except USGSError as e:
127+
error_tree = ElementTree.fromstring(str(e.message))
128+
error_text = error_tree.find("SOAP-ENV:Body/SOAP-ENV:Fault/faultstring", api.NAMESPACES).text
129+
raise USGSInventoryAccessMissing(error_text)
130+
131+
download_url = api.download('LANDSAT_8', 'EE', [scene], api_key=api_key)
132+
if download_url:
133+
return self.fetch(download_url[0], path, filename)
134+
135+
raise RemoteFileDoesntExist('%s is not available on AWS S3, Google or USGS Earth Explorer' % filename)
136+
137+
raise RemoteFileDoesntExist('%s is not available on AWS S3 or Google Storage' % filename)
114138

115139
def amazon_s3(self, scene, band, path):
116140
"""

landsat/landsat.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import pycurl
1616
from boto.exception import NoAuthHandlerFound
1717

18-
from downloader import Downloader, IncorrectSceneId
18+
from downloader import Downloader, IncorrectSceneId, RemoteFileDoesntExist, USGSInventoryAccessMissing
1919
from search import Search
2020
from uploader import Uploader
2121
from utils import reformat_date, convert_to_integer_list, timer, exit, get_file, convert_to_float_list
@@ -109,6 +109,12 @@
109109
110110
--force-unzip Force unzip tar file
111111
112+
--username USGS Eros account Username (only works if the account has special
113+
inventory access). Username and password as a fallback if the image
114+
is not found on AWS S3 or Google Storage
115+
116+
--password USGS Eros account Password
117+
112118
Process:
113119
landsat.py process path [-h] [-b --bands] [-p --pansharpen]
114120
@@ -220,6 +226,10 @@ def args_options():
220226
'50.2682767372753')
221227
parser_download.add_argument('-u', '--upload', action='store_true',
222228
help='Upload to S3 after the image processing completed')
229+
parser_download.add_argument('--username', help='USGS Eros account Username (only works if the account has' +
230+
' special inventory access). Username and password as a fallback if the image' +
231+
'is not found on AWS S3 or Google Storage')
232+
parser_download.add_argument('--password', help='USGS Eros username, used as a fallback')
223233
parser_download.add_argument('--key', help='Amazon S3 Access Key (You can also be set AWS_ACCESS_KEY_ID as '
224234
'Environment Variables)')
225235
parser_download.add_argument('--secret', help='Amazon S3 Secret Key (You can also be set AWS_SECRET_ACCESS_KEY '
@@ -368,7 +378,7 @@ def main(args):
368378
return json.dumps(result)
369379

370380
elif args.subs == 'download':
371-
d = Downloader(download_dir=args.dest)
381+
d = Downloader(download_dir=args.dest, usgs_user=args.username, usgs_pass=args.password)
372382
try:
373383
bands = convert_to_integer_list(args.bands)
374384

@@ -415,6 +425,8 @@ def main(args):
415425
return ['Download Completed', 0]
416426
except IncorrectSceneId:
417427
return ['The SceneID provided was incorrect', 1]
428+
except (RemoteFileDoesntExist, USGSInventoryAccessMissing) as e:
429+
return [e.message, 1]
418430

419431

420432
def process_image(path, bands=None, verbose=False, pansharpen=False, ndvi=False, force_unzip=None,

requirements/docker.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ requests==2.7.0
22
python-dateutil>=2.4.2
33
termcolor>=1.1.0
44
rasterio>=0.27.0
5-
six==1.9.0
5+
six==1.8.0
66
homura>=0.1.2
77
boto>=2.38.0
88
polyline==1.1
99
geocoder>=1.5.1
1010
jsonschema==2.5.1
11+
git+git://github.com/developmentseed/usgs@develop

setup.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,23 @@ def readme():
3535
license='CCO',
3636
platforms='Posix; MacOS X; Windows',
3737
install_requires=[
38+
'usgs==0.1.9',
3839
'requests==2.7.0',
3940
'python-dateutil>=2.4.2',
4041
'numpy>=1.9.3',
4142
'termcolor>=1.1.0',
4243
'rasterio>=0.26.0',
43-
'six==1.9.0',
44+
'six==1.8.0',
4445
'scipy>=0.16.0',
4546
'scikit-image>=0.11.3',
4647
'homura>=0.1.2',
4748
'boto>=2.38.0',
4849
'polyline==1.1',
4950
'geocoder>=1.5.1'
5051
],
52+
dependency_links=[
53+
"git+https://github.com/developmentseed/usgs@develop#egg=usgs-0.1.9"
54+
],
5155
test_suite='nose.collector',
5256
tests_require=test_requirements
5357
)

tests/test_landsat.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def test_download_correct(self, mock_downloader):
9797

9898
args = ['download', 'LC80010092015051LGN00', '-b', '11,', '-d', self.mock_path]
9999
output = landsat.main(self.parser.parse_args(args))
100-
mock_downloader.assert_called_with(download_dir=self.mock_path)
100+
mock_downloader.assert_called_with(download_dir=self.mock_path, usgs_pass=None, usgs_user=None)
101101
mock_downloader.return_value.download.assert_called_with(['LC80010092015051LGN00'], [11])
102102
self.assertEquals(output, ['Download Completed', 0])
103103

@@ -108,7 +108,7 @@ def test_download_correct_zip(self, mock_downloader):
108108

109109
args = ['download', 'LC80010092015051LGN00', '-d', self.mock_path]
110110
output = landsat.main(self.parser.parse_args(args))
111-
mock_downloader.assert_called_with(download_dir=self.mock_path)
111+
mock_downloader.assert_called_with(download_dir=self.mock_path, usgs_pass=None, usgs_user=None)
112112
mock_downloader.return_value.download.assert_called_with(['LC80010092015051LGN00'], [])
113113
self.assertEquals(output, ['Download Completed', 0])
114114

travis-dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
FROM developmentseed/landsat-util:dev
22
ADD . /test
3+
RUN apt-get -y update
4+
RUN apt-get install --yes git-core
35
RUN cd /test && pip install -r requirements/docker.txt
46
RUN pip install pdoc>=0.3.1 nose>=1.3.7 coverage>=4.0 Sphinx>=1.3.1 wheel>=0.26.0 mock>=1.3.0

0 commit comments

Comments
 (0)