-
Notifications
You must be signed in to change notification settings - Fork 0
/
model2radar.py
187 lines (148 loc) · 5.65 KB
/
model2radar.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
#!/usr/bin/env python3
# projects a deformation model ENU on a grid to SAR LOS
# started 2015/03/17 Eric J. Fielding, JPL
# modified from code in Unofficial_ISCE_Guide.pdf
# modified pickle from Python 2 to Python 3, might still work with Python 2
###Our usual import statements
import numpy as np
import isce
import isceobj
from stdproc.model.enu2los.ENU2LOS import ENU2LOS
import argparse
####Method to load pickle information
####from an insarApp run
def load_pickle(step='topo'):
'''Loads the pickle from topo step as default.'''
try:
import cPickle as pickle # python 2
except:
import pickle # python 3
insarObj = pickle.load(open('PICKLE/{0}'.format(step),'rb'))
return insarObj
###Create dummy model file if needed
###Use this for simple testing
###Modify values as per your test dataset
def createDummyModel():
'''Creates a model image.'''
wid = 401
lgt = 401
startLat = 38.6 # top
deltaLat = -0.025 # negative down
startLon = -122.7
deltaLon = 0.025
data = np.zeros((lgt,3*wid), dtype=np.float32)
###East only
# data[:,0::3] = 1.0
###North only
# data[:,1::3] = 1.0
###Up only
data[:,2::3] = 1.0
data.tofile('model.enu')
print('Creating model object')
objModel = isceobj.createDemImage()
objModel.setFilename('model.enu')
objModel.setWidth(wid)
objModel.scheme = 'BIP'
objModel.setAccessMode('read')
objModel.imageType='bip'
objModel.dataType='FLOAT'
objModel.bands = 3
dictProp = {'REFERENCE':'WGS84','Coordinate1': \
{'size':wid,'startingValue':startLon,'delta':deltaLon}, \
'Coordinate2':{'size':lgt,'startingValue':startLat, \
'delta':deltaLat},'FILE_NAME':'model.enu'}
objModel.init(dictProp)
objModel.renderHdr()
###cmd Line Parser
def cmdLineParser():
parser = argparse.ArgumentParser(description="Project ENU deformation to LOS in radar coordinates")
parser.add_argument('-m','--model', dest='model', type=str,
required=True,
help='Input 3 channel FLOAT model file with DEM like info')
parser.add_argument('-o','--output', dest='output', type=str,
default='enu2los.rdr', help='Output 1 channel LOS file')
return parser.parse_args()
###The main program
if __name__ == '__main__':
###Parse command line
inps = cmdLineParser()
###For testing only
# createDummyModel()
####Load model image
print('Creating model image')
modelImg = isceobj.createDemImage()
modelImg.load(inps.model +'.xml') ##From cmd line
if (modelImg.bands !=3 ):
raise Exception('Model input file should be a 3 band image.')
modelImg.setAccessMode('read')
modelImg.createImage()
####Get geocoded information
startLon = modelImg.coord1.coordStart
deltaLon = modelImg.coord1.coordDelta
startLat = modelImg.coord2.coordStart
deltaLat = modelImg.coord2.coordDelta
####Load geometry information from pickle file.
iObj = load_pickle()
topo = iObj.getTopo() #Get info for the dem in radar coords
####Get the wavelength information.
###This is available in multiple locations within insarProc
#wvl = iObj.getMasterFrame().getInstrument().getRadarWavelength()
wvl = topo.radarWavelength
####Pixel-by-pixel Latitude image
print('Creating lat image')
objLat = isceobj.createImage()
objLat.load(topo.latFilename+'.xml')
objLat.setAccessMode('read')
objLat.createImage()
####Pixel-by-pixel Longitude image
print('Creating lon image')
objLon = isceobj.createImage()
objLon.load(topo.lonFilename+'.xml')
objLon.setAccessMode('read')
objLon.createImage()
#####Pixel-by-pixel LOS information
print('Creating LOS image')
objLos = isceobj.createImage()
objLos.load(topo.losFilename +'.xml')
objLos.setAccessMode('read')
objLos.createImage()
###Check if dimensions are the same
for img in (objLon, objLos):
if (img.width != objLat.width) or (img.length != objLat.length):
raise Exception('Lat, Lon and LOS files are not of the same size.')
####Create an output object
print ('Creating output image')
objOut = isceobj.createImage()
objOut.initImage(inps.output, 'write', objLat.width, type='FLOAT')
objOut.createImage()
print('Actual processing')
####The actual processing
#Stage 1: Construction
converter = ENU2LOS()
converter.configure()
#Stage 2: No ports for enu2los
#Stage 3: Set values
converter.setWidth(objLat.width) ###Radar coords width
converter.setNumberLines(objLat.length) ###Radar coords length
converter.setGeoWidth(modelImg.width) ###Geo coords width
converter.setGeoNumberLines(modelImg.length) ###Geo coords length
###Set up geo information
converter.setStartLatitude(startLat)
converter.setStartLongitude(startLon)
converter.setDeltaLatitude(deltaLat)
converter.setDeltaLongitude(deltaLon)
####Set up output scaling
converter.setScaleFactor(1.0) ###Change if ENU not in meters
converter.setWavelength(4*np.pi) ###Wavelength for conversion to radians
converter.enu2los(modelImage = modelImg,
latImage = objLat,
lonImage = objLon,
losImage = objLos,
outImage = objOut)
#Step 4: Close the images
modelImg.finalizeImage()
objLat.finalizeImage()
objLon.finalizeImage()
objLos.finalizeImage()
objOut.finalizeImage()
objOut.renderHdr() ###Create output XML file