-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
482 lines (398 loc) · 19.1 KB
/
main.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#
# Loads metadata in Elastic Search Server, S3 and Disk
#
import os, sys, json, logging, boto3, click, urllib2, math
from copy import copy
from collections import OrderedDict
from datetime import date, timedelta
from elasticsearch import Elasticsearch, RequestError
from reader import csv_reader
import geonames
sys.path.append('../hyperion-l1u')
import config
import ipfsapi
logger = logging.getLogger('hyperion.meta')
bucket_name = os.getenv('BUCKETNAME', 'hyperion-meta')
s3 = boto3.resource('s3')
aws_s3_dir = os.path.join("https://s3.amazonaws.com", bucket_name)
es_index = 'sat-api'
es_type = 'hyperion'
host_url = "http://hyperion-api.herokuapp.com"
ipfs_api = None
def create_index(index_name, doc_type):
body = {
doc_type: {
'properties': {
'scene_id': {'type': 'string', 'index': 'not_analyzed'},
'satellite_name': {'type': 'string'},
'cloud_coverage': {'type': 'float'},
'date': {'type': 'date'},
'data_geometry': {
'type': 'geo_shape',
'tree': 'quadtree',
'precision': '5mi'}
}
}
}
es.indices.create(index=index_name, ignore=400)
es.indices.put_mapping(
doc_type=doc_type,
body=body,
index=index_name
)
def convert_date(value):
dt = value.split(' ')[0]
arr = dt.split('/')
year = int(arr[2])+2000
month = int(arr[0])
day = int(arr[1])
datestr = "%d-%02d-%02d" % (year, month, day)
return datestr
def convert_SceneName(value):
scene = value.split('_')
return scene[0]
def meta_constructor(metadata):
global ipfs_api
internal_meta = copy(metadata)
data_geometry = {
"type": "polygon",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:EPSG:8.9:4326"
}
},
"coordinates": [[
[ metadata.get('CornerLonUpperLeft'), metadata.get('CornerLatUpperLeft')],
[ metadata.get('CornerLonUpperRight'), metadata.get('CornerLatUpperRight')],
[ metadata.get('CornerLonLowerRight'), metadata.get('CornerLatLowerRight')],
[ metadata.get('CornerLonLowerLeft'), metadata.get('CornerLatLowerLeft')],
[ metadata.get('CornerLonUpperLeft'), metadata.get('CornerLatUpperLeft')]
]]
}
dt = convert_date(metadata.get('SceneDate'))
scene_id = convert_SceneName(metadata.get('SceneName'))
year = scene_id[10:14]
doy = scene_id[14:17]
if 'sources' in metadata:
sources = metadata['sources']
else:
ipfs_l1u_dir = os.path.join("/L1U", year, doy, scene_id)
ipfs_l1t_dir = os.path.join("/L1T", year, doy, scene_id)
ipfs_l1s_dir = os.path.join("/L1S", year, doy, scene_id)
l1u_geotiffs = []
for b in range(242):
if config.bbl[b]:
basefilename = scene_id+"_B%03d_L1U.TIF" % (b+1)
ipfs_l1u_path = os.path.join(ipfs_l1u_dir,basefilename)
ipfs_l1u_meta = ipfs_api.files_stat(ipfs_l1u_path)
filename = os.path.join(aws_s3_dir, "L1U", year, doy, scene_id, basefilename)
l1u_geotiffs.append( {
"band_%03d"%(b+1) : {
"size": ipfs_l1u_meta['Size'],
"href" : filename,
"torrent": filename+"?torrent",
"ipfs": "/ipfs/" +ipfs_l1u_meta['Hash']
}
})
basefilename = scene_id+"_L1U.tar.gz"
ipfs_l1u_path = os.path.join(ipfs_l1u_dir,basefilename)
ipfs_l1u_meta = ipfs_api.files_stat(ipfs_l1u_path)
basefilename = scene_id+"_L1S.tar.gz"
ipfs_l1s_path = os.path.join(ipfs_l1s_dir,basefilename)
ipfs_l1s_meta = ipfs_api.files_stat(ipfs_l1s_path)
basefilename = scene_id+"_L1T.tar.gz"
ipfs_l1t_path = os.path.join(ipfs_l1t_dir,basefilename)
ipfs_l1t_meta = ipfs_api.files_stat(ipfs_l1t_path)
l1s_meta_filename = scene_id+"_L1S.json"
ipfs_l1s_meta_path = os.path.join(ipfs_l1s_dir,l1s_meta_filename)
ipfs_l1s_meta_meta = ipfs_api.files_stat(ipfs_l1s_meta_path)
l1t_meta_filename = scene_id+"_L1T.json"
ipfs_l1t_meta_path = os.path.join(ipfs_l1t_dir,l1t_meta_filename)
ipfs_l1t_meta_meta = ipfs_api.files_stat(ipfs_l1t_meta_path)
# reset to L1U
basefilename = scene_id+"_L1U.tar.gz"
sources = [
{
"scene_id": scene_id + "_L1T",
"description": "Co-registered Radiometically Corrected Radiance",
"source": "USGS",
"attribution": "USGS",
"license": "??",
"downloads": {
"GEOTIFF": {
"size": ipfs_l1t_meta['Size'],
"href": os.path.join(aws_s3_dir, "L1T", year, doy, scene_id+"_L1T.tar.gz"),
"torrent": os.path.join(aws_s3_dir, "L1T", year, doy, basefilename+"?torrent"),
"ipfs": "/ipfs/"+ipfs_l1t_meta['Hash']
},
"METADATA": {
"href": os.path.join(aws_s3_dir, "L1T", year, doy, scene_id+"_L1T.json"),
"ipfs": "/ipfs/"+ipfs_l1t_meta_meta['Hash']
}
}
},
{
"scene_id": scene_id + "_L1S",
"description": "Atmospherically Corrected Radiance",
"source": "GSFC - Code 610",
"attribution": "<a href='https://www.researchgate.net/profile/Petya_Campbell'>Petya Campbell</a>",
"license": "<a href='https://creativecommons.org/publicdomain/zero/1.0/'>CCO</a>",
"downloads": {
"ENVI.BIL": {
"size": ipfs_l1s_meta['Size'],
"href": os.path.join(aws_s3_dir, "L1S", year, doy, scene_id+"_L1S.tar.gz"),
"torrent": os.path.join(aws_s3_dir, "L1S", year, doy, scene_id+"?torrent"),
"ipfs": "/ipfs/"+ipfs_l1s_meta['Hash']
},
"METADATA": {
"href": os.path.join(aws_s3_dir, "L1S", year, doy, scene_id+"_L1S.json"),
"ipfs": "/ipfs/"+ipfs_l1s_meta_meta['Hash']
}
}
}
]
if 'actions' in metadata:
actions = metadata['actions']
else:
actions = {
"downloads": {
"ENVI.BIL": {
"size": ipfs_l1u_meta['Size'],
"href": os.path.join(aws_s3_dir, "L1U", year, doy, scene_id, basefilename),
"torrent": os.path.join(aws_s3_dir, "L1U", year, doy, scene_id, basefilename+"?torrent"),
"ipfs": "/ipfs/"+ipfs_l1u_meta['Hash']
},
"GEOTIFF": l1u_geotiffs,
"METADATA": os.path.join(aws_s3_dir, "L1U", year, doy, scene_id, scene_id+"_L1U.json")
},
"process": {
"rgb": {"href": "%s/eo1/process?scene=%s&algorithm=rgb"%(host_url, scene_id+"_L1U")},
"ndvi": {"href": "%s/eo1/process?scene=%s&algorithm=ndvi"%(host_url,scene_id+"_L1U")},
"evi": {"href": "%s/eo1/process?scene=%s&algorithm=evi"%(host_url, scene_id+"_L1U")}
}
}
lat= float(metadata.get('CenterLat'))
lon= float(metadata.get('CenterLon'))
# Check centerlat, centerlon and elevation
pclat = float(metadata.get('CornerLatUpperLeft')) + float(metadata.get('CornerLatUpperRight')) + float(metadata.get('CornerLatLowerRight')) + float(metadata.get('CornerLatLowerLeft'))
pclat /= 4.0
pclon = float(metadata.get('CornerLonUpperLeft')) + float(metadata.get('CornerLonUpperRight')) + float(metadata.get('CornerLonLowerRight')) + float(metadata.get('CornerLonLowerLeft'))
pclon /= 4.0
print "Getting lat, lon", lat, lon, pclat, pclon
YOUR_API_KEY = os.environ['GOOGLE_API_KEY']
print "GKEy", YOUR_API_KEY
google_url = "https://maps.googleapis.com/maps/api/elevation/json?locations=" + str(pclat) +","+ str(pclon) + "&key="+YOUR_API_KEY
print "Gogole url", google_url
el_response = urllib2.urlopen(google_url).read()
el_data = json.loads(el_response)
results = el_data["results"]
elevation = results[0]["elevation"]
#print results
#print results[0]["elevation"]
internal_meta['centerLatitude'] = round(pclat,5)
internal_meta['centerLongitude'] = round(pclon,5)
internal_meta['centerElevation'] = round(elevation,0)
internal_meta['year'] = int( internal_meta['Year'])
internal_meta['dayOfYear'] = int( internal_meta['DayNum'])
internal_meta['scenePath'] = int( internal_meta['ScenePath'])
internal_meta['sceneRow'] = int( internal_meta['SceneRow'])
internal_meta['startTime'] = internal_meta['StartTime']
internal_meta['endTime'] = internal_meta['EndTime']
internal_meta['sceneDuration'] = internal_meta['SceneDuration']
internal_meta['sceneLength'] = internal_meta['SceneLength']
internal_meta['ReceivingStation'] = internal_meta['ReceivingStation']
internal_meta['requestorName'] = internal_meta['RequestorName']
internal_meta['dayOrNight'] = internal_meta['DayOrNight']
internal_meta['solarAzimuthAngle'] = internal_meta['SolarAzimuthAngle']
internal_meta['solarZenithAngle'] = internal_meta['SolarZenithAngle']
internal_meta['sensorLookAngle'] = internal_meta['SensorLookAngle']
internal_meta['satelliteInclination'] = internal_meta['SatelliteInclination']
internal_meta['sensorAltitude'] = 700
removed = ["MaxCloudCover", "SceneTheme", "LandCoverType", "LandCoverDescription","RegionalLocation", "Location", "CalVal_SiteName",
"Comments", "TapeDirName", "BrowseImageLocation", "AeronetStationID", "AeronetStationDistance", "PopulationPerKM2",
"SceneCenterElevation", "AnthropogenicBiome", "SceneName", "SceneDate", "CenterLat", "CenterLon", "Year", "DayNum", "ScenePath", "SceneRow",
"SensorID", "StartTime", "EndTime", "CornerLatUpperLeft", "CornerLonUpperLeft", "CornerLatUpperRight", "CornerLonUpperRight",
"CornerLatLowerLeft", "CornerLonLowerLeft", "CornerLatLowerRight", "CornerLonLowerRight", "SceneDuration", "ReceivingStation",
"DayOrNight", "SceneLength","SolarAzimuthAngle","SolarZenithAngle", "SensorLookAngle", "SatelliteInclination", "RequestorName", "SensorAltitude"
]
for r in removed:
del internal_meta[r]
geonamesInfo = geonames.info(lat, lon)
#print "geonamesInfo", geonamesInfo
body = OrderedDict([
('scene_id', scene_id+"_L1U"),
('description', "EO-1 Hyperion GeoLocated Surface Reflectance Data"),
('satellite_name', 'eo1'),
('instrument', 'hyperion'),
('cloud_coverage', metadata.get('MaxCloudCover')),
('date', dt),
('level', 'L1U'),
('thumbnail', "https://earthexplorer.usgs.gov/browse/eo-1/hyp/" + metadata.get('BrowseImageLocation')),
('attribution', "<a href='https://www.researchgate.net/profile/Petya_Campbell'>Petya Campbell</a>"),
('source', "<a href='https://www.nasa.gov/goddard'>NASA GSFC</a>"),
('license', "<a href='https://creativecommons.org/publicdomain/zero/1.0/'>CCO</a>" ),
('metadata', os.path.join(aws_s3_dir, "L1U", year, doy, scene_id, scene_id+"_L1U.json")),
('countryName', geonamesInfo['countryName']),
('countryCode', geonamesInfo['countryCode']),
('adminName', geonamesInfo['adminName']),
('nearBy', geonamesInfo['nearBy']),
('data_geometry', data_geometry),
('sources', sources),
('actions', actions)
])
#print body
body.update(internal_meta)
return body
def elasticsearch_updater(product_dir, metadata):
try:
body = meta_constructor(metadata)
print 'Pushing to Elasticsearch', es_index, es_type, body['scene_id']
del body['actions']
del body['sources']
try:
es.index(index=es_index, doc_type=es_type, id=body['scene_id'], body=body)
except RequestError as e:
print "ES RequestError", e
print body['data_geometry']
body['data_geometry'] = None
es.index(index=es_index, doc_type=es_type, id=body['scene_id'], body=body)
sys.exit(-1)
except Exception as e:
logger.error('Unhandled error occured while writing to elasticsearch')
logger.error('Details: %s' % e.__str__())
logger.error('Error on line {}'.format(sys.exc_info()[-1].tb_lineno))
sys.exit(-1)
def file_writer(product_dir, metadata):
print "file_writer", product_dir
body = meta_constructor(metadata)
if not os.path.exists(product_dir):
os.makedirs(product_dir)
f = open(os.path.join(product_dir, body['scene_id'] + '.json'), 'w')
f.write(json.dumps(body, indent=4, separators=(',',': ')))
logger.info('saving to disk at %s' % product_dir)
f.close()
def s3_writer(product_dir, metadata):
# make sure product_dir doesn't start with slash (/) or dot (.)
if product_dir.startswith('.'):
product_dir = product_dir[1:]
if product_dir.startswith('/'):
product_dir = product_dir[1:]
body = meta_constructor(metadata)
scene_id = body['scene_id']
year = scene_id[10:14]
doy = scene_id[14:17]
name = scene_id.split("_")[0]
key = os.path.join('L1U', year, doy, name, body['scene_id'] + '.json')
s3.Object(bucket_name, key).put(Body=json.dumps(body), ACL='public-read', ContentType='application/json')
logger.info('saving to s3 at %s %s', bucket_name, key)
def last_updated(today):
""" Gets the latest time a product added to Elasticsearch """
bucket = s3.Bucket(bucket_name)
start_day = today.day
start_month = today.month
yr_counter = 0
while True:
m_counter = 0
year = today.year - yr_counter
if year < 2015:
break
while True:
month = start_month - m_counter
if month == 0:
start_month = 12
break
d_counter = 0
while True:
day = start_day - d_counter
if day == 0:
start_day = 31
break
path = os.path.join(str(year), str(month), str(day))
logger.info('checking %s' % path)
objs = bucket.objects.filter(Prefix=path).limit(1)
if list(objs):
return date(year, month, day)
d_counter += 1
m_counter += 1
yr_counter += 1
return None
@click.command()
@click.argument('ops', metavar='<operations: choices: s3 | es | disk>', nargs=-1)
@click.option('--start', default=None, help='Start Date. Format: MM/DD/YY')
@click.option('--end', default=None, help='End Date. Format: MM/DD/YY')
#@click.option('--es-host', default='localhost', help='Elasticsearch host address')
#@click.option('--es-port', default=9200, type=int, help='Elasticsearch port number')
@click.option('--csv', default='.', help='csv file to read')
@click.option('--folder', default='.', help='Destination folder if is written to disk')
@click.option('--download', is_flag=True,
help='Sets the updater to download the metadata file first instead of streaming it')
@click.option('--download-folder', default=None,
help='The folder to save the downloaded metadata to. Defaults to a temp folder')
@click.option('-v', '--verbose', is_flag=True)
@click.option('--concurrency', default=20, type=int, help='Process concurrency. Default=20')
@click.option('--product', default=None, help='Uploadproduct metadata')
# python main.py disk s3 es --csv Hyperion_3.csv --folder ../data/L1U_metadata --start 01/01/03 -v
# python main.py disk --csv Hyperion_3.csv --folder ../data/L1U_metadata --start 01/01/03 -v
# python main.py es --product /Volumes/LaCie/EO1/DESTRIPE/2013/228/EO1H0110282013228110T3_DESTRIPE_B10_B11/EO1H0110282013228110T3_DESTRIPE_B10_B11.json -v
def main(ops, csv, start, end, folder, download, download_folder, verbose, concurrency, product):
global ipfs_api
if not ops:
raise click.UsageError('No Argument provided. Use --help if you need help')
ipfs_api = ipfsapi.connect('127.0.0.1', 5001)
ipfs_id = ipfs_api.id()
#print "*** ipfs_id", ipfs_id
#sys.exit(-1)
accepted_args = {
'es': elasticsearch_updater,
's3': s3_writer,
'disk': file_writer
}
writers = []
for op in ops:
if op in accepted_args.keys():
writers.append(accepted_args[op])
else:
raise click.UsageError('Operation (%s) is not supported' % op)
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
if verbose:
ch.setLevel(logging.INFO)
else:
ch.setLevel(logging.ERROR)
formatter = logging.Formatter('%(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
print "folder", folder, "csv", csv
if 'es' in ops:
global es
es_port = int(os.getenv('ES_PORT', 80))
es_host = os.getenv('ES_HOST', "NOT_AVAILABLE")
logger.info("Connecting to Elastic Search %s:%d", es_host, es_port)
es = Elasticsearch([{
'host': es_host,
'port': es_port
}])
create_index(es_index, es_type)
if not start and not end:
delta = timedelta(days=3)
start = date.today() - delta
start = '{0}/{1}/{2}'.format(start.month, start.day,start.year-2000)
# Simple product upload
if product:
print "product", product
if not os.path.exists(product):
print "Product does not exist", product
with open(product) as data:
json_data = json.load(data)
data.close()
#print json_data
logger.info('processing %s' % json_data['scene_id'])
for w in writers:
w(download_folder, json_data)
print "Done"
sys.exit(0)
csv_reader(csv, folder, writers, start_date=start, end_date=end, download=download, download_path=download_folder,
num_worker_threads=concurrency)
if __name__ == '__main__':
main()