-
Notifications
You must be signed in to change notification settings - Fork 0
/
geocode_offsets.py
166 lines (116 loc) · 4.85 KB
/
geocode_offsets.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
# SET UP IMPORTS
def geocodeOffsets(inps):
import os
import os.path
from osgeo import gdal
import numpy as np
### setting up loop
print(" - Detecting offsets to geocode\n")
programs = [
{"filepath": "offsets", "pathtype": "folder", "program": "Ampcor"},
{"filepath": "denseOffsets", "pathtype": "folder", "program": "DenseAmpcor"},
{"filepath": "offset.mat", "pathtype": ".mat file", "program": "autoRIFT"},
]
to_geocode = []
for program in programs:
if os.path.exists(program["filepath"]):
to_geocode.append(program)
print(f"A {program['pathtype']} detected for {program['program']}.")
if inps.ignore_ampcor == True:
print("\n*** Ignoring ampcor offsets (improves speed) ***")
### finding geometry
Geometry = generateGeometry()
# cleanup and intialize folder
print("\n - Starting geocode\n")
os.system("rm -rf geocoded_offsets")
os.mkdir("geocoded_offsets")
# geocoding
for geocode in to_geocode:
if geocode["program"] == "autoRIFT":
os.system(f"rm -rf {os.path.join('geocoded_offsets', geocode['program'])}")
os.mkdir(os.path.join("geocoded_offsets", geocode["program"]))
print(f"Geocoding for {geocode['program']}\n")
try:
import h5py
f = h5py.File("offset.mat", "r")
except:
import scipy.io as sio
f = sio.loadmat("offset.mat")
print(f"Geocoding pixel range offset:\n")
xarray = np.fliplr(f["Dx"])
generateGeotiff(
xarray,
"range_radar",
os.path.join("geocoded_offsets", geocode["program"]),
Geometry,
)
print(f"\nPixel to geographic distance conversion:")
xconv = (
(Geometry["top_geodesic"]["s12"] + Geometry["bottom_geodesic"]["s12"])
/ 2
/ Geometry["pixel_width"]
)
print(f"{xconv} meter/pixel\n")
xarray_conv = xarray * xconv
generateGeotiff(
xarray_conv,
"range",
os.path.join("geocoded_offsets", geocode["program"]),
Geometry,
)
print(f"\nGeocoding pixel azimuth offset:\n")
yarray = np.fliplr(f["Dy"])
generateGeotiff(
yarray,
"azimuth_radar",
os.path.join("geocoded_offsets", geocode["program"]),
Geometry,
)
print(f"\nPixel to geographic distance conversion:")
yconv = (
(Geometry["left_geodesic"]["s12"] + Geometry["right_geodesic"]["s12"])
/ 2
/ Geometry["pixel_height"]
)
print(f"{yconv} meter/pixel\n")
yarray_conv = yarray * yconv
generateGeotiff(
yarray_conv,
"azimuth",
os.path.join("geocoded_offsets", geocode["program"]),
Geometry,
)
elif geocode["program"] == "Ampcor" and inps.ignore_ampcor == False:
os.system(f"rm -rf {os.path.join('geocoded_offsets', geocode['program'])}")
os.mkdir(os.path.join("geocoded_offsets", geocode["program"]))
print(f"Geocoding for {geocode['program']}\n")
print(f"Geocoding range offset:\n")
in_ds = gdal.Open("offsets/range.off", gdal.GA_ReadOnly)
xarray = np.fliplr(np.abs(in_ds.GetRasterBand(1).ReadAsArray()))
in_ds = None
generateGeotiff(
xarray,
"range",
os.path.join("geocoded_offsets", geocode["program"]),
Geometry,
)
print(f"\nGeocoding azimuth offset:\n")
in_ds = gdal.Open("offsets/azimuth.off", gdal.GA_ReadOnly)
yarray = np.fliplr(np.abs(in_ds.GetRasterBand(1).ReadAsArray()))
in_ds = None
generateGeotiff(
yarray,
"azimuth",
os.path.join("geocoded_offsets", geocode["program"]),
Geometry,
)
elif geocode["program"] == "DenseAmpcor" and inps.ignore_ampcor == False:
os.system(f"rm -rf {os.path.join('geocoded_offsets', geocode['program'])}")
os.mkdir(os.path.join("geocoded_offsets", geocode["program"]))
print(f"Geocoding for {geocode['program']}\n")
# print(f"Geocoding range offset:\n")
# xarray = np.fliplr(f['Dx'])
# generateGeotiff(xarray, "range", geocode['program'], Geometry)
# print(f"\nGeocoding azimuth offset:\n")
# yarray = np.fliplr(f['Dy'])
# generateGeotiff(yarray, "azimuth", geocode['program'], Geometry)