forked from eodms-sgdot/eodms-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_slc_record.py
More file actions
157 lines (140 loc) · 5.85 KB
/
create_slc_record.py
File metadata and controls
157 lines (140 loc) · 5.85 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
import argparse
import configparser
import os
import requests
import zipfile
from lxml import etree as ET
def main():
# Parse Arguments
args = parse_args()
config = get_config_params("config.ini")
# Read and parse xml metadata directly from zip file
zip_file = zipfile.ZipFile(args.image_file_name, 'r')
product_filepath = os.path.join(
os.path.splitext(os.path.basename(args.image_file_name))[0],
'metadata',
'product.xml').replace('\\', '/')
xml_file = zip_file.open(product_filepath)
tree = ET.parse(xml_file)
root = tree.getroot()
namespace = {'ns': 'rcmGsProductSchema'}
# Populate SLC matadata and create body to post to API
product_uri = '/'.join(['s3://vrrc-rcm-raw-data-store',
args.site,
args.beam,
args.image_file_name])
data_start_time = root.find('.//ns:rawDataStartTime', namespace).text
footprint = get_slc_corners(root, tree, namespace)
special_handling_bool = root.find('.//ns:specialHandlingRequired',
namespace).text
beam_id = get_beam_id_from_api(args, config)
slc_body = {
"product_uri": product_uri,
"beam_id": beam_id,
"source_url": None,
"data_start_time": data_start_time,
"status": "new",
"status_info": None,
"footprint": footprint,
"special_handling_required": special_handling_bool
}
response = requests.post(f'{config.get('VRRC-API',
'vrrc-api-endpoint')}/slc/',
json=slc_body)
return response.status_code
def get_slc_corners(root, tree, namespace):
lines = "{:.7e}".format(int(root.find('.//ns:numLines', namespace).text)-1)
samplesPerLine = "{:.7e}".format(int(root.find('.//ns:samplesPerLine',
namespace).text)-1)
# find 4 corner coordinates, so lat/long at [0,0], [0,samplesPerLine],
# [lines,0], [lines,samplesPerLine]
top_left = tree.xpath(".//ns:imageReferenceAttributes/"
"ns:geographicInformation/"
"ns:geolocationGrid/ns:imageTiePoint["
"ns:imageCoordinate/ns:line='0.0000000e+00' and "
"ns:imageCoordinate/ns:pixel='0.0000000e+00']",
namespaces=namespace)
top_right = tree.xpath(".//ns:imageReferenceAttributes/"
"ns:geographicInformation/"
"ns:geolocationGrid/ns:imageTiePoint["
"ns:imageCoordinate/ns:line='0.0000000e+00' and "
f"ns:imageCoordinate/ns:pixel='{samplesPerLine}']",
namespaces=namespace)
bottom_left = tree.xpath(".//ns:imageReferenceAttributes/"
"ns:geographicInformation/"
"ns:geolocationGrid/ns:imageTiePoint["
f"ns:imageCoordinate/ns:line='{lines}' and "
f"ns:imageCoordinate/ns:pixel='0.0000000e+00']",
namespaces=namespace)
bottom_right = tree.xpath(".//ns:imageReferenceAttributes/"
"ns:geographicInformation/"
"ns:geolocationGrid/ns:imageTiePoint["
f"ns:imageCoordinate/ns:line='{lines}' and "
f"ns:imageCoordinate/ns:pixel='{samplesPerLine}'"
"]", namespaces=namespace)
footprint = {
"type": "Polygon",
"coordinates": [
[
[
float(top_left[0][1][1].text),
float(top_left[0][1][0].text)
],
[
float(top_right[0][1][1].text),
float(top_right[0][1][0].text)
],
[
float(bottom_right[0][1][1].text),
float(bottom_right[0][1][0].text)
],
[
float(bottom_left[0][1][1].text),
float(bottom_left[0][1][0].text)
],
]
]
}
return footprint
def get_beam_id_from_api(args, config):
response = requests.get(f'{config.get('VRRC-API',
'vrrc-api-endpoint')}/beams/')
if response.status_code == 200:
data_dict = response.json()
for item in data_dict:
if (item['short_name'] == args.beam and
args.site in item['target_label']):
beam_id = int(item['id'])
return beam_id
def get_config_params(args):
"""
Parse Input/Output columns from supplied *.ini file
"""
configParseObj = configparser.ConfigParser()
configParseObj.read(args)
return configParseObj
def parse_args():
parser = argparse.ArgumentParser(description=("Create SLC Record in VRRC"
"database via API"))
parser.add_argument("--site",
type=str,
help='Volcanic Site Name',
required=True)
parser.add_argument("--beam",
type=str,
help="RCM Beam Mode",
required=True)
parser.add_argument("--image_file_name",
type=str,
help="RCM Zipfile Name",
required=True)
args = parser.parse_args()
# args = parser.parse_args(['--site', 'Fagradalsfjall',
# '--beam', '5M9',
# '--image_file_name',
# '/home/ec2-user/eodms-cli/downloads/'
# 'RCM3_OK2894345_PK2947746_1_5M9'
# '_20240219_185609_HH_SLC.zip'])
return args
if __name__ == '__main__':
main()