-
Notifications
You must be signed in to change notification settings - Fork 9
/
query-pds.py
executable file
·153 lines (131 loc) · 4.85 KB
/
query-pds.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
#!/usr/bin/env python
from builtins import str
import os, sys, time, json, requests, logging, traceback
import acquisition_localizer_multi, acquisition_localizer_single
# set logger
log_format = "[%(asctime)s: %(levelname)s/%(name)s/%(funcName)s] %(message)s"
logging.basicConfig(format=log_format, level=logging.INFO)
class LogFilter(logging.Filter):
def filter(self, record):
if not hasattr(record, 'id'): record.id = '--'
return True
logger = logging.getLogger(os.path.splitext(os.path.basename(__file__))[0])
logger.setLevel(logging.INFO)
logger.addFilter(LogFilter())
def query_acquisitions(starttime, endtime, geoshape):
"""Query ES for active AOIs that intersect starttime and endtime and
find acquisitions that intersect the AOI polygon for the platform."""
es_index = "grq_*_*acquisition*"
query = {
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"term": {
"dataset_type.raw": "acquisition"
}
},
{
"range": {
"starttime": {
"lte": endtime
}
}
},
{
"range": {
"endtime": {
"gte": starttime
}
}
}
]
}
},
"filter": {
"geo_shape": {
"location": {
"shape": geoshape
}
}
}
}
},
"partial_fields" : {
"partial" : {
"include" : [ "id", "dataset_type", "dataset", "metadata" ]
}
}
}
print(query)
acqs = [i['fields']['partial'][0]['id']for i in acquisition_localizer_single.query_es(query, es_index)]
acquisition_localizer_multi.logger.info("Acquistions to localize: {}".format(json.dumps(acqs, indent=2)))
return acqs
def get_aoi(aoi_name):
es_index = "grq_*_*area_of_interest*"
query = {
"query":{
"bool":{
"must":[
{ "term":{ "_id": aoi_name } },
{
"term": {
"dataset_type.raw": "area_of_interest"
}
},
]
}
},
"partial_fields" : {
"partial" : {
"include" : [ "id", "starttime", "endtime", "location",
"metadata.user_tags", "metadata.priority" ]
}
}
}
print(json.dumps(query))
aois = acquisition_localizer_single.query_es(query, es_index)
return aois[0]['fields']['partial'][0]
def resolve_source(ctx_file):
"""Resolve best URL from acquisition."""
# get settingsnano
# read in context
with open(ctx_file) as f:
ctx = json.load(f)
acq_info = {}
start = ctx['starttime']
end = ctx['endtime']
aoi_name = ctx['aoi_name']
aoi = get_aoi(aoi_name)
acq_list = query_acquisitions(start, end, aoi['location'])
# acq_list = ctx['products'] if isinstance(ctx['products'], list) else [ctx['products']]
logger.info("Acq List Type : %s" % type(acq_list))
spyddder_sling_extract_version = ctx.get('opds_sling_extract_version', 'develop')
asf_ngap_download_queue = ctx['asf_ngap_download_queue']
esa_download_queue = ctx['esa_download_queue']
job_priority = ctx["job_priority"]
job_type, job_version = ctx['job_specification']['id'].split(':')
acquisition_localizer_version = job_version
queues = [] # where should we get the queue value
identifiers = []
prod_dates = []
index_suffix = "S1-IW_ACQ"
return acquisition_localizer_multi.sling(acq_list, spyddder_sling_extract_version, acquisition_localizer_version, esa_download_queue,
asf_ngap_download_queue, job_priority, job_type, job_version)
def main():
context_file = os.path.abspath("_context.json")
if not os.path.exists(context_file):
raise RuntimeError
resolve_source(context_file)
if __name__ == '__main__':
try:
status = main()
except Exception as e:
with open('_alt_error.txt', 'w') as f:
f.write("%s\n" % str(e))
with open('_alt_traceback.txt', 'w') as f:
f.write("%s\n" % traceback.format_exc())
raise
sys.exit(status)