-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCompareOPS_NASA_CMR.py
More file actions
458 lines (420 loc) · 17.3 KB
/
CompareOPS_NASA_CMR.py
File metadata and controls
458 lines (420 loc) · 17.3 KB
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
import requests as rq
import xml.etree.ElementTree as ET
import json
import xml.dom.minidom
import math
import xmltodict
import os
from os import listdir
from os.path import isfile, join
NASA_CMR_HOST = "https://cmr.earthdata.nasa.gov"
MAAP_CMR_HOST = ""
#MAAP_TEST_CMR_HOST = "http://cmr-s-services-lb-tf-1501391941.us-east-1.elb.amazonaws.com"
MAAP_TEST_CMR_HOST = "https://cmr.uat.maap-project.org"
SEARCH_COLLECTION_PATH = "/search/collections"
SEARCH_GRANULE_PATH = "/search/granules"
SEARCH_SOFTWARE_PATH = "/search/software"
class Collection:
def __init__(self):
self.id = "-1"
self.revision_id = -1
self.location = ""
self.name = ""
self.deleted = False
def __init__(self, _id, _revision_id, _location, _name, _deleted):
self.id = _id
self.revision_id = _revision_id
self.location = _location
self.name = _name
self.deleted = _deleted
class Granule:
def __init__(self):
self.id = "-1"
self.revision_id = -1
self.location = ""
self.name = ""
self.deleted = False
def __init__(self, _id, _revision_id, _location, _name, _deleted):
self.id = _id
self.revision_id = _revision_id
self.location = _location
self.name = _name
self.deleted = _deleted
def print_log(mode, message, priority):
if(mode == "DEBUG"):
print(message)
elif(mode == "RUN"):
if(priority == "HIGH"):
print(message)
def downloadCollections(host, options, mode):
#0. Build URL
if(host == "nasa_cmr" or host == "cmr"):
url = NASA_CMR_HOST
elif(host == "maap_cmr" or host == "maap"):
url = MAAP_CMR_HOST
elif(host == "maap_test_cmr" or host == "test"):
url = MAAP_TEST_CMR_HOST
else:
url = MAAP_TEST_CMR_HOST
url = url + SEARCH_COLLECTION_PATH
# http://cmr-s-services-lb-tf-1501391941.us-east-1.elb.amazonaws.com/search/collections
options += "?page_size=2000&page_num=1"
request = url + options
#1. Get a list of collections
print_log(mode, "Collection searching...", "HIGH")
result = rq.get(request).text
dom = xml.dom.minidom.parseString(result)
dom_str = dom.toprettyxml()
file_output = open("request_xml_output.txt", "w") # reformat the xml file
file_output.write(dom_str)
file_output.close()
print_log(mode, "Successuflly wrote the result to files!", "HIGH")
tree = ET.parse("request_xml_output.txt")
root = tree.getroot()
cols = []
# Get a list of collections
# TODO: should be aware on the facthat if the number of collections > 2000
for ref in root.findall('references/reference'):
isDel = ref.find('deleted')
if (isDel is not None):
_deleted = bool(isDel.text.strip())
else:
_deleted = False
_name = ref.find('name')
if(_name is not None):
_name_text = _name.text.strip()
else:
_name_text = ""
_id = ref.find('id')
if(_id is not None):
_id_text = _id.text.strip()
else:
_id_text = "-1"
_location = ref.find('location')
if(_location is not None):
_location_text = _location.text.strip()
else:
_location_text = ""
_revision_id = ref.find('revision-id')
if(_revision_id is not None):
_revision_id = int(_revision_id.text.strip())
else:
_revision_id = -1
#Search for existing collection
#found = False
#for i in range(0, len(cols)):
# if(cols[i].id == _id_text):
# found = True
# if(cols[i].revision_id < _revision_id):
# cols[i].name = _name_text
# cols[i].location = _location_text
# cols[i].revision_id = _revision_id
# cols[i].deleted = _deleted
# break
#if (not found):
# col = Collection(_id_text, _revision_id, _location_text, _name_text, _deleted)
# cols.append(col)
col = Collection(_id_text, _revision_id, _location_text, _name_text, _deleted)
cols.append(col)
#Remove deleted collections
#i = 0
#while i < len(cols):
# if(cols[i].deleted == True):
# cols.pop(i)
# else:
# i += 1
print_log(mode, "Total number of collections: " + str(len(cols)), "LOW")
for i in range(0, len(cols)):
print_log(mode, "Collection " + str(i) + ": " + str(cols[i].name), "LOW")
print_log(mode, "\t" + str(cols[i].id), "LOW")
print_log(mode, "\t" + str(cols[i].revision_id), "LOW")
print_log(mode, "\t" + str(cols[i].location), "LOW")
print_log(mode, "\t" + str(cols[i].deleted), "LOW")
#2. Download collection metadata and store it in the Data/Collections folder
for i in range(0, len(cols)):
url = cols[i].location
print_log(mode, "Download " + str(url), "HIGH")
col_meta = rq.get(url).text
file_out = open("Data/Collections/" + str(cols[i].id) + ".json", "w")
file_out.write(col_meta)
file_out.close
print_log(mode, "Total: " + str(i + 1) + " collections", "HIGH")
print_log(mode, "Done", "HIGH")
return
def downloadGranules(host, options, mode):
#0. Build URL
if(host == "nasa_cmr" or host == "cmr"):
url = NASA_CMR_HOST
elif(host == "maap_cmr" or host == "maap"):
url = MAAP_CMR_HOST
elif(host == "maap_test_cmr" or host == "test"):
url = MAAP_TEST_CMR_HOST
else:
url = MAAP_TEST_CMR_HOST
url = url + SEARCH_GRANULE_PATH
options += "?page_size=2000&page_num=1"
request = url + options
#1. Get a list of granules
print_log(mode, "Granule searching...", "HIGH")
result = rq.get(request).text
dom = xml.dom.minidom.parseString(result)
dom_str = dom.toprettyxml()
file_output = open("list_granules_xml_output.txt", "w") # reformat the xml file
file_output.write(dom_str)
file_output.close()
print_log(mode, "Successuflly wrote the result to files!", "HIGH")
tree = ET.parse("list_granules_xml_output.txt")
root = tree.getroot()
granules = []
total_granules = int(root.find('hits').text)
page_num = 1
round_num = math.ceil(total_granules / 2000)
print("Total granules and number of rounds:")
print(total_granules)
print(round_num)
while page_num <= round_num:
req = url + "?page_size=2000&page_num=" + str(page_num)
print(req)
res = rq.get(req).text
xml_str = xml.dom.minidom.parseString(res).toprettyxml()
file_output = open("temp_g_xml.txt", "w") # reformat the xml file
file_output.write(xml_str)
file_output.close()
r = ET.parse("temp_g_xml.txt").getroot()
for ref in r.findall('references/reference'):
isDel = ref.find('deleted')
if (isDel is not None):
_deleted = bool(isDel.text.strip())
else:
_deleted = False
_name = ref.find('name')
if(_name is not None):
_name_text = _name.text.strip()
else:
_name_text = ""
_id = ref.find('id')
if(_id is not None):
_id_text = _id.text.strip()
else:
_id_text = "-1"
_location = ref.find('location')
if(_location is not None):
_location_text = _location.text.strip()
else:
_location_text = ""
_revision_id = ref.find('revision-id')
if(_revision_id is not None):
_revision_id = int(_revision_id.text.strip())
else:
_revision_id = -1
gra = Granule(_id_text, _revision_id, _location_text, _name_text, _deleted)
granules.append(gra)
#print(page_num)
page_num += 1
if (page_num > 6): #CMR does not allow to download more than 10000 metadata.
break
print_log(mode, "Total number of granules: " + str(len(granules)), "LOW")
temp = open("temp_granules.txt", "w")
temp.write("Total number of granules: " + str(len(granules)) + "\n")
for i in range(0, len(granules)):
#print_log(mode, "Granule " + str(i) + ": " + str(granules[i].name), "LOW")
#print_log(mode, "\t" + str(granules[i].id), "LOW")
#print_log(mode, "\t" + str(granules[i].revision_id), "LOW")
#print_log(mode, "\t" + str(granules[i].location), "LOW")
#print_log(mode, "\t" + str(granules[i].deleted), "LOW")
temp.write("Granule " + str(i) + ": " + str(granules[i].name) + "\n")
temp.write("\t" + str(granules[i].id) + "\n")
temp.write("\t" + str(granules[i].revision_id) + "\n")
temp.write("\t" + str(granules[i].location) + "\n")
temp.write("\t" + str(granules[i].deleted) + "\n")
temp.close()
#2. Download collection metadata and store it in the Data/Granules folder
for i in range(0, len(granules)):
url = granules[i].location + ".umm_json"
print_log(mode, "Download " + str(url), "HIGH")
gra_meta = rq.get(url).text
file_out = open("Data/Granules/" + str(granules[i].id) + ".json", "w")
file_out.write(gra_meta)
file_out.close
print_log(mode, "Total: " + str(i + 1) + " Granules", "HIGH")
print_log(mode, "Done", "HIGH")
return
#Download Granules in with different collection
def downloadGranules_v2(host, options, mode, out_dir):
#0. Build URL
if(host == "nasa_cmr" or host == "cmr"):
url = NASA_CMR_HOST
elif(host == "maap_cmr" or host == "maap"):
url = MAAP_CMR_HOST
elif(host == "maap_test_cmr" or host == "test"):
url = MAAP_TEST_CMR_HOST
else:
url = MAAP_TEST_CMR_HOST
url = url + SEARCH_GRANULE_PATH
if(options == ""):
request = url + "?page_size=2000&page_num=1"
else:
request = url + options + "&page_size=2000&page_num=1"
#request = url + options
#1. Get a list of granules
print_log(mode, "Granule searching...", "HIGH")
result = rq.get(request).text
print(request)
dom = xml.dom.minidom.parseString(result)
dom_str = dom.toprettyxml()
file_output = open("list_granules_xml_output.txt", "w") # reformat the xml file
file_output.write(dom_str)
file_output.close()
print_log(mode, "Successuflly wrote the result to files!", "HIGH")
tree = ET.parse("list_granules_xml_output.txt")
root = tree.getroot()
granules = []
total_granules = int(root.find('hits').text)
page_num = 1
round_num = int(math.ceil(total_granules / 2000))
print("Total granules and number of rounds:")
print(total_granules)
print(round_num)
while page_num <= round_num:
if(options == ""):
req = url + "?page_size=2000&page_num=" + str(page_num)
else:
req = url+ options + "&page_size=2000&page_num=" + str(page_num)
#req = url + options
print(req)
res = rq.get(req).text
xml_str = xml.dom.minidom.parseString(res).toprettyxml()
file_output = open("temp_g_xml.txt", "w") # reformat the xml file
file_output.write(xml_str)
file_output.close()
r = ET.parse("temp_g_xml.txt").getroot()
for ref in r.findall('references/reference'):
isDel = ref.find('deleted')
if (isDel is not None):
_deleted = bool(isDel.text.strip())
else:
_deleted = False
_name = ref.find('name')
if(_name is not None):
_name_text = _name.text.strip()
else:
_name_text = ""
_id = ref.find('id')
if(_id is not None):
_id_text = _id.text.strip()
else:
_id_text = "-1"
_location = ref.find('location')
if(_location is not None):
_location_text = _location.text.strip()
else:
_location_text = ""
_revision_id = ref.find('revision-id')
if(_revision_id is not None):
_revision_id = int(_revision_id.text.strip())
else:
_revision_id = -1
gra = Granule(_id_text, _revision_id, _location_text, _name_text, _deleted)
granules.append(gra)
#print(page_num)
page_num += 1
if (page_num > 2): #CMR does not allow to download more than 10000 metadata.
break # Only download 4000 granules max per collection
print_log(mode, "Total number of granules: " + str(len(granules)), "LOW")
temp = open("temp_granules.txt", "w")
temp.write("Total number of granules: " + str(len(granules)) + "\n")
for i in range(0, len(granules)):
#print_log(mode, "Granule " + str(i) + ": " + str(granules[i].name), "LOW")
#print_log(mode, "\t" + str(granules[i].id), "LOW")
#print_log(mode, "\t" + str(granules[i].revision_id), "LOW")
#print_log(mode, "\t" + str(granules[i].location), "LOW")
#print_log(mode, "\t" + str(granules[i].deleted), "LOW")
temp.write("Granule " + str(i) + ": " + str(granules[i].name) + "\n")
temp.write("\t" + str(granules[i].id) + "\n")
temp.write("\t" + str(granules[i].revision_id) + "\n")
temp.write("\t" + str(granules[i].location) + "\n")
temp.write("\t" + str(granules[i].deleted) + "\n")
temp.close()
# Create folder if it does not exist
if not os.path.exists("Data/Granules/" + str(out_dir)):
os.makedirs("Data/Granules/" + str(out_dir))
#2. Download collection metadata and store it in the Data/Granules folder
i = 0
for i in range(0, len(granules)):
url = granules[i].location + ".umm_json"
print_log(mode, "Download " + str(url), "HIGH")
gra_meta = rq.get(url).text
file_out = open("Data/Granules/" + str(out_dir) + "/" + str(granules[i].id) + ".json", "w")
file_out.write(gra_meta)
file_out.close
print_log(mode, "Total: " + str(i + 1) + " Granules", "HIGH")
print_log(mode, "Done", "HIGH")
return
def getGranules():
#0. Build URL
ops_cmr_url = "https://cmr.ops.maap-project.org"
nasa_cmr_url = "https://cmr.earthdata.nasa.gov"
ops_cmr_url += "/search/granules"
nasa_cmr_url += "/search/granules"
ops_cmr_request = ops_cmr_url + "?collection_concept_id=C1201300747-NASA_MAAP&bounding_box=-180,50,180,75&temporal=2020-06-01T00:00:00.000Z,2020-09-30T23:59:59.000Z&page_size=2000&page_num=1"
nasa_cmr_request = nasa_cmr_url + "?collection_concept_id=C1997321091-NSIDC_ECS&bounding_box=-180,50,180,75&temporal=2020-06-01T00:00:00.000Z,2020-09-30T23:59:59.000Z&page_size=2000&page_num=1"
ops_result = rq.get(nasa_cmr_request).text
dom = xml.dom.minidom.parseString(ops_result)
dom_str = dom.toprettyxml()
file_output = open("list_granules_nasa.txt", "w")
file_output.write(dom_str)
file_output.close()
tree = ET.parse("list_granules_nasa.txt")
root = tree.getroot()
total_granules = int(root.find('hits').text)
page_num = 1
round_num = int(math.ceil(total_granules / 2000))
print("Total granules and number of rounds:" + str(total_granules) + "\t" + str(round_num))
granules_nasa = []
while page_num <= round_num:
nasa_cmr_request = nasa_cmr_url + "?collection_concept_id=C1997321091-NSIDC_ECS&bounding_box=-180,50,180,75&temporal=2020-06-01T00:00:00.000Z,2020-09-30T23:59:59.000Z&page_size=2000&page_num=" + str(page_num)
print(nasa_cmr_request)
res_nasa = rq.get(nasa_cmr_request).text
xml_str = xml.dom.minidom.parseString(res_nasa).toprettyxml()
file_output = open("temp_g_xml.txt", "w") # reformat the xml file
file_output.write(xml_str)
file_output.close()
r = ET.parse("temp_g_xml.txt").getroot()
for ref in r.findall('references/reference'):
_name = ref.find('name')
if(_name is not None):
_name_text = _name.text.strip()
else:
_name_text = ""
granules_nasa.append(_name_text)
page_num += 1
page_num = 1
granules_ops = []
while page_num <= round_num:
ops_cmr_request = ops_cmr_url + "?collection_concept_id=C1201300747-NASA_MAAP&bounding_box=-180,50,180,75&temporal=2020-06-01T00:00:00.000Z,2020-09-30T23:59:59.000Z&page_size=2000&page_num=" + str(page_num)
print(ops_cmr_request)
res_ops = rq.get(ops_cmr_request).text
xml_str = xml.dom.minidom.parseString(res_ops).toprettyxml()
file_output = open("temp_g_xml.txt", "w") # reformat the xml file
file_output.write(xml_str)
file_output.close()
r = ET.parse("temp_g_xml.txt").getroot()
for ref in r.findall('references/reference'):
_name = ref.find('name')
if(_name is not None):
_name_text = _name.text.strip()
else:
_name_text = ""
granules_ops.append(_name_text)
page_num += 1
f_nasa = open("f_nasa.txt", "w")
for i in range(len(granules_nasa)):
f_nasa.write(str(granules_nasa[i]) + "\n")
f_nasa.close()
f_ops = open("f_ops.txt", "w")
for i in range(len(granules_ops)):
f_ops.write(str(granules_ops[i]) + "\n")
f_ops.close()
return
if __name__ == '__main__':
getGranules()