@@ -58,42 +58,49 @@ def download(self, scenes, bands=None):
58
58
"""
59
59
60
60
if isinstance (scenes , list ):
61
- output = {}
61
+ files = []
62
62
63
63
for scene in scenes :
64
- # If bands are provided the image is from 2015 or later use Amazon
65
- self .scene_interpreter (scene )
66
-
67
- if (bands and int (scene [12 ]) > 4 ):
68
- if isinstance (bands , list ):
69
- # Create a folder to download the specific bands into
70
- path = check_create_folder (join (self .download_dir , scene ))
71
- try :
72
- # Always grab MTL.txt if bands are specified
73
- if 'BQA' not in bands :
74
- bands .append ('QA' )
75
-
76
- if 'MTL' not in bands :
77
- bands .append ('MTL' )
78
-
79
- for band in bands :
80
- self .amazon_s3 (scene , band , path )
81
- output [scene ] = 'aws'
82
- except RemoteFileDoesntExist :
83
- self .google_storage (scene , self .download_dir )
84
- output [scene ] = 'google'
85
-
86
- else :
87
- raise Exception ('Expected bands list' )
88
- else :
89
- self .google_storage (scene , self .download_dir )
90
- output [scene ] = 'google'
91
-
92
- return output
64
+
65
+ # for all scenes if bands provided, first check AWS, if the bands exist
66
+ # download them, otherwise use Google and then USGS.
67
+ try :
68
+ # if bands are not provided, directly go to Goodle and then USGS
69
+ if not isinstance (bands , list ):
70
+ raise RemoteFileDoesntExist
71
+ files .append (self .amazon_s3 (scene , bands ))
72
+
73
+ except RemoteFileDoesntExist :
74
+ try :
75
+ files .append (self .google_storage (scene , self .download_dir ))
76
+ except RemoteFileDoesntExist :
77
+ files .append (self .usgs_eros (scene , self .download_dir ))
78
+
79
+ return files
93
80
94
81
else :
95
82
raise Exception ('Expected sceneIDs list' )
96
83
84
+ def usgs_eros (self , scene , path ):
85
+ """ Downloads the image from USGS """
86
+
87
+ # download from usgs if login information is provided
88
+ if self .usgs_user and self .usgs_pass :
89
+ try :
90
+ api_key = api .login (self .usgs_user , self .usgs_pass )
91
+ except USGSError as e :
92
+ error_tree = ElementTree .fromstring (str (e .message ))
93
+ error_text = error_tree .find ("SOAP-ENV:Body/SOAP-ENV:Fault/faultstring" , api .NAMESPACES ).text
94
+ raise USGSInventoryAccessMissing (error_text )
95
+
96
+ download_url = api .download ('LANDSAT_8' , 'EE' , [scene ], api_key = api_key )
97
+ if download_url :
98
+ self .output ('Source: USGS EarthExplorer' , normal = True , arrow = True )
99
+ return self .fetch (download_url [0 ], path )
100
+
101
+ raise RemoteFileDoesntExist ('%s is not available on AWS S3, Google or USGS Earth Explorer' % scene )
102
+ raise RemoteFileDoesntExist ('%s is not available on AWS S3 or Google Storage' % scene )
103
+
97
104
def google_storage (self , scene , path ):
98
105
"""
99
106
Google Storage Downloader.
@@ -110,67 +117,49 @@ def google_storage(self, scene, path):
110
117
:returns:
111
118
Boolean
112
119
"""
113
- sat = self .scene_interpreter (scene )
114
120
115
- filename = scene + '.tar.bz'
121
+ sat = self . scene_interpreter ( scene )
116
122
url = self .google_storage_url (sat )
117
123
118
- if self .remote_file_exists (url ):
119
- return self .fetch (url , path , filename )
124
+ self .remote_file_exists (url )
120
125
121
- else :
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 )
126
+ self .output ('Source: Google Storge' , normal = True , arrow = True )
127
+ return self .fetch (url , path )
130
128
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 )
129
+ def amazon_s3 (self , scene , bands ):
130
+ """
131
+ Amazon S3 downloader
132
+ """
134
133
135
- raise RemoteFileDoesntExist ( '%s is not available on AWS S3, Google or USGS Earth Explorer' % filename )
134
+ sat = self . scene_interpreter ( scene )
136
135
137
- raise RemoteFileDoesntExist ('%s is not available on AWS S3 or Google Storage' % filename )
136
+ # Always grab MTL.txt and QA band if bands are specified
137
+ if 'BQA' not in bands :
138
+ bands .append ('QA' )
138
139
139
- def amazon_s3 (self , scene , band , path ):
140
- """
141
- Amazon S3 downloader
140
+ if 'MTL' not in bands :
141
+ bands .append ('MTL' )
142
142
143
- :param scene:
144
- The scene ID.
145
- :type scene:
146
- String
147
- :param band:
148
- The band number.
149
- :type band:
150
- String, Integer
151
- :param path:
152
- The directory path to where the image should be stored
153
- :type path:
154
- String
143
+ urls = []
155
144
156
- :returns:
157
- Boolean
158
- """
159
- sat = self .scene_interpreter (scene )
145
+ for band in bands :
146
+ # get url for the band
147
+ url = self .amazon_s3_url (sat , band )
160
148
161
- if band != 'MTL' :
162
- filename = '%s_B%s.TIF' % (scene , band )
163
- else :
164
- filename = '%s_%s.txt' % (scene , band )
165
- url = self .amazon_s3_url (sat , filename )
149
+ # make sure it exist
150
+ self .remote_file_exists (url )
151
+ urls .append (url )
166
152
167
- if self . remote_file_exists ( url ):
168
- return self .fetch ( url , path , filename )
153
+ # create folder
154
+ path = check_create_folder ( join ( self .download_dir , scene ) )
169
155
170
- else :
171
- raise RemoteFileDoesntExist ('%s is not available on Amazon S3' % filename )
156
+ self .output ('Source: AWS S3' , normal = True , arrow = True )
157
+ for url in urls :
158
+ self .fetch (url , path )
159
+
160
+ return path
172
161
173
- def fetch (self , url , path , filename ):
162
+ def fetch (self , url , path ):
174
163
""" Downloads the given url.
175
164
176
165
:param url:
@@ -190,18 +179,26 @@ def fetch(self, url, path, filename):
190
179
Boolean
191
180
"""
192
181
182
+ segments = url .split ('/' )
183
+ filename = segments [- 1 ]
184
+
185
+ # remove query parameters from the filename
186
+ filename = filename .split ('?' )[0 ]
187
+
193
188
self .output ('Downloading: %s' % filename , normal = True , arrow = True )
194
189
190
+ # print(join(path, filename))
191
+ # raise Exception
195
192
if exists (join (path , filename )):
196
193
size = getsize (join (path , filename ))
197
194
if size == self .get_remote_file_size (url ):
198
195
self .output ('%s already exists on your system' % filename , normal = True , color = 'green' , indent = 1 )
199
- return False
200
196
201
- fetch (url , path )
197
+ else :
198
+ fetch (url , path )
202
199
self .output ('stored at %s' % path , normal = True , color = 'green' , indent = 1 )
203
200
204
- return True
201
+ return join ( path , filename )
205
202
206
203
def google_storage_url (self , sat ):
207
204
"""
@@ -218,7 +215,7 @@ def google_storage_url(self, sat):
218
215
filename = sat ['scene' ] + '.tar.bz'
219
216
return url_builder ([self .google , sat ['sat' ], sat ['path' ], sat ['row' ], filename ])
220
217
221
- def amazon_s3_url (self , sat , filename ):
218
+ def amazon_s3_url (self , sat , band ):
222
219
"""
223
220
Return an amazon s3 url the contains the scene and band provided.
224
221
@@ -234,6 +231,11 @@ def amazon_s3_url(self, sat, filename):
234
231
:returns:
235
232
(String) The URL to a S3 file
236
233
"""
234
+ if band != 'MTL' :
235
+ filename = '%s_B%s.TIF' % (sat ['scene' ], band )
236
+ else :
237
+ filename = '%s_%s.txt' % (sat ['scene' ], band )
238
+
237
239
return url_builder ([self .s3 , sat ['sat' ], sat ['path' ], sat ['row' ], sat ['scene' ], filename ])
238
240
239
241
def remote_file_exists (self , url ):
@@ -249,10 +251,8 @@ def remote_file_exists(self, url):
249
251
"""
250
252
status = requests .head (url ).status_code
251
253
252
- if status == 200 :
253
- return True
254
- else :
255
- return False
254
+ if status != 200 :
255
+ raise RemoteFileDoesntExist
256
256
257
257
def get_remote_file_size (self , url ):
258
258
""" Gets the filesize of a remote file.
0 commit comments