-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
executable file
·318 lines (253 loc) · 10.4 KB
/
functions.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import os
import shelve
import smtplib
import requests
import sys
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
import xml.etree.cElementTree as ET
import zipfile
import pprint
import time
import ConfigParser
import logging
###############################################################################
# SETTINGS #
###############################################################################
# Credentials
credentials = ConfigParser.ConfigParser()
credentials.read("./config/credentials.cfg")
opendatahub_user = credentials.get("opendatahub", "user")
opendatahub_password = credentials.get("opendatahub", "pw")
email_user = credentials.get("email", "user")
email_password = credentials.get("email", "pw")
# Directories
configuration = ConfigParser.ConfigParser()
configuration.read("./config/conf.cfg")
base_directory = configuration.get("directories", "base")
product_download_directory = os.path.join(base_directory, 'downloads')
product_data_directory = os.path.join(base_directory, 'SAFE')
log_directory = os.path.join(base_directory, 'log')
shelve_directory = os.path.join(base_directory, 'etc')
image_output_directory = os.path.join(base_directory, 'img')
# Logging
standard_logfile = configuration.get("logging", "file")
standard_logpath = os.path.join(log_directory, standard_logfile)
logging.basicConfig(
filename=standard_logpath,
level=logging.INFO,
format="%(asctime)s [%(levelname)8s] %(message)s",
datefmt="%d.%m.%Y %H:%M:%S")
# ===================================================================
# SEARCHING
# def osearch(query): # DONE
# logging.info('Opensearch request: {}'.format(query))
# response = requests.get(
# "https://scihub.copernicus.eu/apihub/search",
# params=query, auth=(opendatahub_user, opendatahub_password))
# return response
# def parse_osearch_response(response): # DONE
# parsed_response = []
# tree = ET.fromstring(response.text)
# ns = {'atom': 'http://www.w3.org/2005/Atom'}
# id_list = tree.findall("./atom:entry/atom:id", ns)
# title_list = tree.findall("./atom:entry/atom:title", ns)
# beginposition_list = tree.findall(
# "./atom:entry/atom:date[@name='beginposition']", ns)
# querytime = tree.find("./atom:updated", ns).text
# size_list = tree.findall("./atom:entry/atom:str[@name='size']", ns)
# producttype_list = tree.findall(
# "./atom:entry/atom:str[@name='producttype']", ns)
# for i in range(len(id_list)):
# parsed_response.append([id_list[i].text,
# title_list[i].text,
# beginposition_list[i].text,
# size_list[i].text,
# producttype_list[i].text,
# querytime])
# return sorted(parsed_response, key=lambda x: x[2], reverse=True)
# ===================================================================
# VARIOUS HELPERS
def translate_l1c_to_l2a_title(product):
l2a_title = product[1][:7] + 'L2A' + product[1][10:]
product[1] = l2a_title
return product
def list_products(parsed_response):
i = 0
logging.debug('\nLIST PRODUCTS\nTotal: ' +
str(len(parsed_response)) + '\n' + 13 * '=')
for p in parsed_response:
i += 1
logging.debug(
"{:03d}: {:s} --> {:10s} {:10s} {:s}".format(i,
p[0][:4] + '..' +
p[0][-4:],
p[3],
p[2][:10],
p[1]))
# ===================================================================
# DOWNLOADING ETC
# def select_product_to_download(parsed_response): # DONE
# # einfacher Fall: nur das erste Produkt selektieren:
# return parsed_response[0]
# def proceed_with_download(product):
# return True # Batch-Modus
# print('\nProduct to Download:')
# pprint.pprint(product)
# inputstr = ''
# while inputstr not in ['y', 'n']:
# inputstr = raw_input('Proceed? (y/n)')
# if inputstr == 'y':
# return True
# if inputstr == 'n':
# return False
# print('Enter "y" or "n"')
# def download_product(product): # DONE
# filename = product[1] + '.zip'
# path = os.path.join(product_download_directory, filename)
# logging.info(
# 'Requesting download for: {} | {}'.format(product[0], product[1]))
#
# r = requests.get(
# "https://scihub.copernicus.eu/dhus/odata/v1/Products('" +
# product[0] + "')/$value",
# auth=(opendatahub_user, opendatahub_password),
# stream=True)
#
# if r.status_code == 200:
# logging.info(
# 'Starting download for: {} | {}'.format(product[0], product[1]))
# with open(path, 'wb') as f:
# for chunk in r.iter_content(1024):
# f.write(chunk)
# logging.info(
# 'Product download complete for: {} | {}'.format(
# product[0], product[1]))
# def unzip_downloaded_product(product): # DONE
# logging.info(
# 'Starting Unzip for: {} | {}'.format(product[0], product[1]))
# file_to_extract = product[1] + '.zip'
# extract_from_path = os.path.join(product_download_directory,
# file_to_extract)
# extract_to_path = product_data_directory
# safezip = zipfile.ZipFile(extract_from_path)
# safezip.extractall(extract_to_path)
# safezip.close()
# logging.info(
# 'Completed Unzip for: {} | {}'.format(product[0], product[1]))
def process_l1c_to_l2a(product):
log_name = 'copernicus_process_l1c_to_l2a_' + \
time.strftime('%Y-%m-%d__%H_%M_%S') + '.log'
saveout = sys.stdout
sys.stdout = open(os.path.join(log_directory, log_name), 'w')
filename = product[1] + '.SAFE'
l1c_path = os.path.join(product_data_directory, filename)
logging.info(
'Starting L2A_Process for: {} | {}'.format(product[0], l1c_path))
os.system('L2A_Process ' + l1c_path)
logging.info(
'Completed L2A_Process for: {} | {}'.format(product[0], l1c_path))
sys.stdout = saveout
# ===================================================================
# Product Downloaded Shelve
# def product_already_downloaded(product): # DONE
# sf = shelve.open(os.path.join(shelve_directory, 'downloaded_products'))
# if product[0] in sf.keys():
# sf.close()
# return True
# else:
# sf.close()
# return False
# def reset_downloadshelve(): # DONE
# sf = shelve.open(os.path.join(shelve_directory, 'downloaded_products'))
# sf.clear()
# sf.close()
# logging.info('Reset downloadshelve')
# def write_product_to_downloaded(product): # DONE
# sf = shelve.open(os.path.join(shelve_directory, 'downloaded_products'))
# sf[product[0]] = product # oder product[1:]???
# sf.close()
# logging.info(
# 'Product written to shelve "downloaded_products": {}'.format(
# product[0]))
# return
# def ids_in_downloaded_shelve():
# sf = shelve.open(os.path.join(shelve_directory, 'downloaded_products'))
# ids = sf.keys()
# return ids
# def products_in_shelve():
# sf = shelve.open(os.path.join(shelve_directory, 'downloaded_products'))
# products = sf.values()
# return products
# def print_products_in_shelve():
# print(products_in_shelve())
# ===================================================================
# Product to Analyze Shelve
def write_product_to_analyze(product):
sf = shelve.open(os.path.join(shelve_directory, 'products_to_analyze'))
sf[product[0]] = product
sf.close()
logging.info(
'Product written to shelve "products_to_analyze": {}'.format(
product[0]))
def read_products_to_analyze():
sf = shelve.open(os.path.join(shelve_directory, 'products_to_analyze'))
productsreturn = {}
for productkey in sf.keys():
productsreturn[productkey] = sf[productkey]
sf.close()
return productsreturn
def print_products_to_analyze():
print(read_products_to_analyze())
def remove_from_analyzeshelve(product):
sf = shelve.open(os.path.join(shelve_directory, 'products_to_analyze'))
print(sf.pop(product[0], False))
sf.close()
sf = shelve.open(os.path.join(shelve_directory, 'products_to_analyze'))
print('sf: {}'.format(sf))
sf.close()
logging.info('Removed from analyzeshelve: {}'.format(product[0]))
def reset_analyzeshelve():
sf = shelve.open(os.path.join(shelve_directory, 'products_to_analyze'))
sf.clear()
sf.close()
logging.info('Reset analyzeshelve')
# ===================================================================
# Product Analyzed Shelve
def write_product_analyzed(product):
sf = shelve.open(os.path.join(shelve_directory, 'products_analyzed'))
sf[product[0]] = product
sf.close()
logging.info(
'Product written to shelve "products_analyzed": {}'.format(product[0]))
def read_products_analyzed():
sf = shelve.open(os.path.join(shelve_directory, 'products_analyzed'))
return sf
def reset_analyzedshelve():
sf = shelve.open(os.path.join(shelve_directory, 'products_analyzed'))
sf.clear()
sf.close()
logging.info('Reset analyzedshelve')
# ===================================================================
# Send Mails
def send_analyzed_mail_with_thumbnail(product):
thumb_path = os.path.join(image_output_directory,
product[1] + '_RGB_thumb.jpg')
msg = MIMEMultipart()
msg['Subject'] = 'Thumbnail created: ' + product[1]
msg['From'] = 'Copernicus Analysator <r.lukesch@gmx.net>'
msg['To'] = 'Roland Lukesch <r.lukesch@gmx.net>'
text = MIMEText('Thumbnail created: ' + product[1])
msg.attach(text)
f = open(thumb_path, 'rb')
bild = MIMEImage(f.read())
f.close()
msg.attach(bild)
smtp = smtplib.SMTP_SSL("mail.gmx.com:465")
smtp.login(email_user, email_password)
smtp.sendmail('Copernicus <r.lukesch@gmx.net>',
'Roland Lukesch <r.lukesch@gmx.net>', msg.as_string())
smtp.quit()
logging.info(
'Sent Thumbnail Mail to r.lukesch@gmx.net: {}'.format(thumb_path))