Newer
Older
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
#!/usr/bin/env python3
import numpy as np
gdalmap = {'FLOAT': 'Float32',
'DOUBLE' : 'Float64',
'CFLOAT' : 'CFloat32',
'CINT' : 'CInt16',
'BYTE' : 'Byte'}
class Swath(object):
'''
Information holder.
'''
def __init__(self, product):
'''
Constructor.
'''
self.prod = product
self.xsize = None
self.ysize = None
self.xoffset = None
self.yoffset = None
self.setSizes()
def setSizes(self):
'''
Set xsize and ysize.
'''
t0 = self.prod.sensingStart
dt = self.prod.bursts[0].azimuthTimeInterval
width = self.prod.bursts[0].numberOfSamples
tend = self.prod.sensingStop
nLines = int(np.round((tend-t0).total_seconds() / dt))+1
self.xsize = width
self.ysize = nLines
def __str__(self):
'''
Description.
'''
outstr = ''
outstr += 'Number of Bursts: {0}\n'.format(self.data.numberOfBursts)
outstr += 'Dimensions: ({0},{1})\n'.format(self.ysize, self.xsize)
return outstr
@property
def sensingStart(self):
return self.prod.bursts[0].sensingStart
@property
def sensingStop(self):
return self.prod.bursts[-1].sensingStop
@property
def nearRange(self):
return self.prod.bursts[0].startingRange
@property
def dr(self):
return self.prod.bursts[0].rangePixelSize
@property
def dt(self):
return self.prod.bursts[0].azimuthTimeInterval
@property
def burstWidth(self):
return self.prod.bursts[0].numberOfSamples
@property
def burstLength(self):
return self.prod.bursts[0].numberOfLines
@property
def farRange(self):
return self.nearRange + (self.burstWidth-1)*self.dr
class VRTConstructor(object):
'''
Class to construct a large image.
'''
def __init__(self, y, x):
self.ysize = y
self.xsize = x
self.dtype = None
self.tref = None
self.rref = None
self.dt = None
self.dr = None
####VRT text handler
self.vrt = ''
def setReferenceTime(self, tim):
self.tref = tim
def setReferenceRange(self, rng):
self.rref = rng
def setTimeSpacing(self, dt):
self.dt = dt
def setRangeSpacing(self, dr):
self.dr = dr
def setDataType(self, iscetype):
self.dtype = gdalmap[iscetype.upper()]
def initVRT(self):
'''
Build the top part of the VRT.
'''
head = '''<VRTDataset rasterXSize="{0}" rasterYSize="{1}">'''
self.vrt += head.format(self.xsize, self.ysize, self.dtype)
def initBand(self, band=None):
header=''' <VRTRasterBand dataType="{0}" band="{1}">
<NoDataValue>0.0</NoDataValue>
'''
self.vrt += header.format(self.dtype, band)
def finishBand(self):
'''
Build the last part of the VRT.
'''
tail = ''' </VRTRasterBand>'''
self.vrt += tail
def finishVRT(self):
tail='''</VRTDataset>'''
self.vrt += tail
def addSwath(self, swath, filelist, band = 1, validOnly=True):
'''
Add one swath to the VRT.
'''
if len(swath.prod.bursts) != len(filelist):
raise Exception('Number of bursts does not match number of files provided for stitching')
for ind, burst in enumerate(swath.prod.bursts):
xoff = int(np.round( (burst.startingRange - self.rref)/self.dr))
yoff = int(np.round( (burst.sensingStart - self.tref).total_seconds() / self.dt))
infile = filelist[ind]
self.addBurst( burst, infile, yoff, xoff, band=band, validOnly=validOnly)
def addBurst(self, burst, infile, yoff, xoff, band=1, validOnly=True):
'''
Add one burst to the VRT.
'''
tysize = burst.numberOfLines
txsize = burst.numberOfSamples
if validOnly:
tyoff = int(burst.firstValidLine)
txoff = int(burst.firstValidSample)
wysize = int(burst.numValidLines)
wxsize = int(burst.numValidSamples)
fyoff = int(yoff + burst.firstValidLine)
fxoff = int(xoff + burst.firstValidSample)
else:
tyoff = 0
txoff = 0
wysize = tysize
wxsize = txsize
fyoff = int(yoff)
fxoff = int(xoff)
tmpl = ''' <SimpleSource>
<SourceFilename relativeToVRT="1">{tiff}</SourceFilename>
<SourceBand>{band}</SourceBand>
<SourceProperties RasterXSize="{txsize}" RasterYSize="{tysize}" DataType="{dtype}"/>
<SrcRect xOff="{txoff}" yOff="{tyoff}" xSize="{wxsize}" ySize="{wysize}"/>
<DstRect xOff="{fxoff}" yOff="{fyoff}" xSize="{wxsize}" ySize="{wysize}"/>
</SimpleSource>
'''
self.vrt += tmpl.format( tyoff=tyoff, txoff=txoff,
fyoff=fyoff, fxoff=fxoff,
wxsize=wxsize, wysize=wysize,
tiff=infile+'.vrt', dtype=self.dtype,
tysize=tysize, txsize=txsize,
band=band)
def writeVRT(self, outfile):
'''
Write VRT to file.
'''
with open(outfile, 'w') as fid:
fid.write(self.vrt)