-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene_management.py
78 lines (61 loc) · 2.36 KB
/
scene_management.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Handling Sentinel-1 scenes in a *root_folder* storage.
"""
__author__ = 'Dmitrii Murashkin'
import os
import re
import shutil
from .utils import scene_time
def get_scene_folder(scene_name, root_folder, ensure_existence=True, extra_folder=''):
""" Find path to the scene in the *root_folder* storage
and optionally ensure its existence (create folders if needed).
"""
scene_name = scene_name.split('.')[0]
try:
date = scene_time(scene_name)
except Exception:
print('Could not recognize timestamp for {0}'.format(scene_name))
return False
return get_date_folder(date, root_folder, ensure_existence, extra_folder)
def get_date_folder(date, root_folder, ensure_existence=True, extra_folder=''):
year = date.strftime('%Y')
month = date.strftime('%m')
day = date.strftime('%d')
scene_folder = os.path.join(root_folder, year, month, day, extra_folder)
if ensure_existence:
try:
if not os.path.exists(scene_folder): # for python2 compatibility
os.makedirs(scene_folder) #
# os.makedirs(scene_folder, exist_ok=True)
except Exception:
print('Could not create folders in {0}.'.format(root_folder))
return False
return scene_folder
def arrange_scene(scene_path, root_folder, copy=False, extra_folder=''):
scene_name = os.path.basename(scene_path)
if not re.search('^S1(A|B)', scene_name.split('_')[0]):
""" This is not a Sentinel-1 product file. """
return False
elif scene_name.split('_')[-1][-4:] != '.zip':
""" This is not a zip archive, skip it. """
return False
scene_folder = get_scene_folder(scene_name, root_folder, ensure_existence=True, extra_folder=extra_folder)
try:
if copy:
shutil.copy(scene_path, scene_folder)
else:
shutil.move(scene_path, scene_folder)
except Exception:
return False
return True
def is_available(scene_name, root_folder):
""" Check *scene* existence in the *root_folder* storage.
"""
scene_name = scene_name.split('.')[0]
scene_folder = get_scene_folder(scene_name, root_folder, ensure_existence=False)
scene_path = os.path.join(scene_folder, scene_name)
if os.path.exists(scene_path):
return True
return False
if __name__ == '__main__':
pass