15
15
scihub_password = 'gnssguest'
16
16
17
17
18
- def download_orbit (safe_file , orbit_dir ):
18
+ def download_orbit (safe_file : str , orbit_dir : str ):
19
19
'''
20
20
Download orbits for S1-A/B SAFE "safe_file"
21
21
@@ -25,6 +25,11 @@ def download_orbit(safe_file, orbit_dir):
25
25
File path to SAFE file for which download the orbits
26
26
orbit_dir: str
27
27
File path to directory where to store downloaded orbits
28
+
29
+ Returns:
30
+ --------
31
+ orbit_file : str
32
+ Path to the orbit file.
28
33
'''
29
34
30
35
# Create output directory & check internet connection
@@ -42,9 +47,11 @@ def download_orbit(safe_file, orbit_dir):
42
47
orbit_dict = get_orbit_dict (sensor_id , start_time ,
43
48
end_time , 'AUX_RESORB' )
44
49
# Download orbit file
45
- filename = os .path .join (orbit_dir , orbit_dict ["orbit_name" ])
46
- if not os .path .exists (filename ):
47
- download_orbit_file (orbit_dir , orbit_dict ['orbit_url' ])
50
+ orbit_file = os .path .join (orbit_dir , orbit_dict ["orbit_name" ] + '.EOF' )
51
+ if not os .path .exists (orbit_file ):
52
+ download_orbit_file (orbit_dir , orbit_dict ['orbit_url' ])
53
+
54
+ return orbit_file
48
55
49
56
50
57
def check_internet_connection ():
@@ -168,15 +175,22 @@ def download_orbit_file(output_folder, orbit_url):
168
175
orbit_url: str
169
176
Remote url of orbit file to download
170
177
'''
171
-
178
+ print ( 'downloading URL:' , orbit_url )
172
179
response = requests .get (url = orbit_url , auth = (scihub_user , scihub_password ))
180
+
173
181
# Get header and find filename
174
182
header = response .headers ['content-disposition' ]
175
- _ , header_params = cgi .parse_header (header )
183
+ header_params = cgi .parse_header (header )[ 1 ]
176
184
# construct orbit filename
177
- orbit_filename = os .path .join (output_folder , header_params ['filename' ])
185
+ orbit_file = os .path .join (output_folder , header_params ['filename' ])
186
+
178
187
# Save orbits
179
- open (orbit_filename , 'wb' ).write (response .content )
188
+ with open (orbit_file , 'wb' ) as f :
189
+ for chunk in response .iter_content (chunk_size = 1024 ):
190
+ if chunk :
191
+ f .write (chunk )
192
+ f .flush ()
193
+ return orbit_file
180
194
181
195
182
196
def get_file_name_tokens (zip_path : str ) -> [str , list [datetime .datetime ]]:
@@ -204,35 +218,43 @@ def get_file_name_tokens(zip_path: str) -> [str, list[datetime.datetime]]:
204
218
# lambda to check if file exists if desired sat_id in basename
205
219
item_valid = lambda item , sat_id : os .path .isfile (item ) and sat_id in os .path .basename (item )
206
220
207
-
208
- def get_orbit_file_from_list (zip_path : str , orbit_file_list : list [str ]) -> str :
221
+ def get_orbit_file_from_dir (zip_path : str , orbit_dir : str , auto_download : bool = False ) -> str :
209
222
'''Get orbit state vector list for a given swath.
210
223
211
224
Parameters:
212
225
-----------
213
226
zip_path : string
214
- Path to Sentinel1 SAFE zip file. File names required to adhere to the
227
+ Path to Sentinel1 SAFE zip file. Base names required to adhere to the
215
228
format described here:
216
229
https://sentinel.esa.int/web/sentinel/user-guides/sentinel-1-sar/naming-conventions
217
- orbit_file_list: list[str]
218
- List containing orbit files paths . Orbit files required to adhere to
219
- naming convention found here:
230
+ orbit_dir : string
231
+ Path to directory containing orbit files. Orbit files required to adhere
232
+ to naming convention found here:
220
233
https://s1qc.asf.alaska.edu/aux_poeorb/
234
+ auto_download : bool
235
+ Automatically download the orbit file if not exist in the orbit_dir.
221
236
222
237
Returns:
223
238
--------
224
- orbit_path : str
225
- Path the orbit file.
239
+ orbit_file : str
240
+ Path to the orbit file.
226
241
'''
242
+
243
+ # check the existence of input file path and directory
227
244
if not os .path .exists (zip_path ):
228
245
raise FileNotFoundError (f"{ zip_path } does not exist" )
229
246
230
- if not orbit_file_list :
231
- raise ValueError ( "no orbit paths provided " )
247
+ if not os . path . isdir ( orbit_dir ) :
248
+ raise NotADirectoryError ( f" { orbit_dir } not found " )
232
249
233
250
# extract platform id, start and end times from swath file name
234
251
platform_id , t_swath_start_stop = get_file_name_tokens (zip_path )
235
252
253
+ # initiate output
254
+ orbit_file = ''
255
+
256
+ # search for orbit file
257
+ orbit_file_list = glob .glob (os .path .join (orbit_dir , 'S1*.EOF' ))
236
258
for orbit_file in orbit_file_list :
237
259
# check if file validity
238
260
if not item_valid (orbit_file , platform_id ):
@@ -245,50 +267,23 @@ def get_orbit_file_from_list(zip_path: str, orbit_file_list: list[str]) -> str:
245
267
t_orbit_start = datetime .datetime .strptime (t_orbit_start [1 :], FMT )
246
268
247
269
# string '.EOF' from end of end time string
248
- t_orbit_end = datetime .datetime .strptime (t_orbit_end [:- 4 ], FMT )
270
+ t_orbit_stop = datetime .datetime .strptime (t_orbit_end [:- 4 ], FMT )
249
271
250
272
# check if:
251
273
# 1. swath start and stop time > orbit file start time
252
274
# 2. swath start and stop time < orbit file stop time
253
- if all ([t > t_orbit_start for t in t_swath_start_stop ]) and \
254
- all ([t < t_orbit_end for t in t_swath_start_stop ]):
255
- return orbit_file
256
-
257
- msg = f'No orbit file found for { os .path .basename (zip_path )} !'
258
- msg += f'\n Orbit directory: { os .path .dirname (orbit_file_list [0 ])} '
259
- warnings .warn (msg )
260
-
261
- return ''
275
+ if all ([t_orbit_start < t < t_orbit_stop for t in t_swath_start_stop ]):
276
+ break
277
+ else :
278
+ orbit_file = ''
262
279
263
- def get_orbit_file_from_dir (path : str , orbit_dir : str ) -> str :
264
- '''Get orbit state vector list for a given swath.
265
-
266
- Parameters:
267
- -----------
268
- path : string
269
- Path to Sentinel1 SAFE zip file. Base names required to adhere to the
270
- format described here:
271
- https://sentinel.esa.int/web/sentinel/user-guides/sentinel-1-sar/naming-conventions
272
- orbit_dir : string
273
- Path to directory containing orbit files. Orbit files required to adhere
274
- to naming convention found here:
275
- https://s1qc.asf.alaska.edu/aux_poeorb/
276
-
277
- Returns:
278
- --------
279
- orbit_path : str
280
- Path the orbit file.
281
- '''
282
- if not os .path .exists (path ):
283
- raise FileNotFoundError (f"{ path } does not exist" )
284
-
285
- if not os .path .isdir (orbit_dir ):
286
- raise NotADirectoryError (f"{ orbit_dir } not found" )
287
-
288
- orbit_file_list = glob .glob (os .path .join (orbit_dir , 'S1*.EOF' ))
289
- if not orbit_file_list :
290
- raise FileNotFoundError (f'No S1*.EOF file found in directory: { orbit_dir } ' )
280
+ if not orbit_file :
281
+ if auto_download :
282
+ orbit_file = download_orbit (zip_path , orbit_dir )
291
283
292
- orbit_path = get_orbit_file_from_list (path , orbit_file_list )
284
+ else :
285
+ msg = f'No orbit file found for { os .path .basename (zip_path )} !'
286
+ msg += f'\n Orbit directory: { orbit_dir } '
287
+ warnings .warn (msg )
293
288
294
- return orbit_path
289
+ return orbit_file
0 commit comments