-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathsix_sicd.i
529 lines (439 loc) · 18 KB
/
six_sicd.i
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
* =========================================================================
* This file is part of six.sicd-python
* =========================================================================
*
* (C) Copyright 2004 - 2015, MDA Information Systems LLC
*
* six.sicd-python is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; If not,
* see <http://www.gnu.org/licenses/>.
*/
%module(package="pysix") six_sicd
%feature("autodoc", "1");
%include <std_string.i>
%{
#include <complex>
#include <utility>
#include "import/mem.h"
#include "import/six.h"
#include "import/six/sicd.h"
#include "six/sicd/AreaPlaneUtility.h"
#include "six/sicd/GeoLocator.h"
#include "six/sicd/SICDWriteControl.h"
#include "six/sicd/Utilities.h"
#include <numpyutils/numpyutils.h>
using namespace six::sicd;
using namespace six;
six::sicd::ComplexData * asComplexData(six::Data* data);
/* Need this because we can't really do it on the python side of things */
six::sicd::ComplexData * asComplexData(six::Data* data)
{
six::sicd::ComplexData * complexData = dynamic_cast<six::sicd::ComplexData*>(data);
if( !complexData )
{
throw except::BadCastException();
}
else
{
return complexData;
}
}
void writeNITF(const std::string& pathname, const std::vector<std::string>&
schemaPaths, const six::sicd::ComplexData& data, long long imageAdr);
void writeNITF(const std::string& pathname, const std::vector<std::string>&
schemaPaths, const six::sicd::ComplexData& data, long long imageAdr)
{
const std::complex<float>* image = reinterpret_cast<
std::complex<float>* >(imageAdr);
six::XMLControlFactory::getInstance().addCreator(
six::DataType::COMPLEX,
new six::XMLControlCreatorT<six::sicd::ComplexXMLControl>());
mem::SharedPtr<six::Container> container(new six::Container(
six::DataType::COMPLEX));
std::auto_ptr<logging::Logger> logger(logging::setupLogger("out"));
container->addData(data.clone());
six::NITFWriteControl writer;
writer.initialize(container);
writer.setLogger(logger.get());
six::BufferList buffers;
buffers.push_back(reinterpret_cast<const six::UByte*>(image));
writer.save(buffers, pathname, schemaPaths);
}
Data* readNITF(const std::string& pathname,
const std::vector<std::string>& schemaPaths);
Data* readNITF(const std::string& pathname,
const std::vector<std::string>& schemaPaths)
{
six::XMLControlRegistry xmlRegistry;
xmlRegistry.addCreator(six::DataType::COMPLEX,
new six::XMLControlCreatorT<
six::sicd::ComplexXMLControl>());
logging::Logger log;
six::NITFReadControl reader;
reader.setLogger(&log);
reader.setXMLControlRegistry(&xmlRegistry);
reader.load(pathname, schemaPaths);
mem::SharedPtr<six::Container> container = reader.getContainer();
six::Region region;
region.setStartRow(0);
region.setStartCol(0);
six::Data* const data = container->getData(0);
const types::RowCol<size_t> extent(data->getNumRows(),
data->getNumCols());
const size_t numPixels(extent.row * extent.col);
size_t numBytesPerPixel = data->getNumBytesPerPixel();
size_t offset = 0;
mem::ScopedArray<sys::ubyte> buffer(
new sys::ubyte[numPixels * numBytesPerPixel]);
region.setNumRows(extent.row);
region.setNumCols(extent.col);
region.setBuffer(buffer.get() + offset);
return reinterpret_cast<Data*>(reader.interleaved(region, 0));
}
%}
%ignore six::sicd::cropSICD;
%include <std_auto_ptr.i>
%include <std_string.i>
%include <std_vector.i>
%rename (cropSICD) cropSICDWrap;
%inline %{
void cropSICDWrap(const std::string& inPathname,
const std::vector<std::string>& schemaPaths,
const types::RowCol<size_t>& aoiOffset,
const types::RowCol<size_t>& aoiDims,
const std::string& outPathname);
void cropSICDWrap(const std::string& inPathname,
const std::vector<std::string>& schemaPaths,
const types::RowCol<size_t>& aoiOffset,
const types::RowCol<size_t>& aoiDims,
const std::string& outPathname)
{
six::XMLControlFactory::getInstance().addCreator(
six::DataType::COMPLEX,
new six::XMLControlCreatorT<six::sicd::ComplexXMLControl>());
cropSICD(inPathname, schemaPaths, aoiOffset,
aoiDims, outPathname);
}
%}
%{
nitf::Record _readRecord(const std::string& pathname);
nitf::Record _readRecord(const std::string& pathname)
{
nitf::Reader reader;
nitf::IOHandle io(pathname);
return reader.read(io);
}
%}
%ignore mem::ScopedCloneablePtr::operator!=;
%ignore mem::ScopedCloneablePtr::operator==;
%ignore mem::ScopedCopyablePtr::operator!=;
%ignore mem::ScopedCopyablePtr::operator==;
%rename(_fitOutputToSlantImpl) scene::ProjectionPolynomialFitter::fitOutputToSlantPolynomials;
%rename(_fitSlantToOutputImpl) scene::ProjectionPolynomialFitter::fitSlantToOutputPolynomials;
%import <nitf/List.hpp>
%import <nitf/Object.hpp>
%import <nitf/TRE.hpp>
%import <nrt/Defines.h>
%include <std_vector.i>
%include <std_string.i>
%include <std_complex.i>
%include <std_pair.i>
%include <std_auto_ptr.i>
%include <nitf/ComponentInfo.hpp>
%include <nitf/DESegment.hpp>
%include <nitf/DESubheader.hpp>
%include <nitf/Extensions.hpp>
%include <nitf/Field.hpp>
%include <nitf/FileHeader.hpp>
%include <nitf/ImageSegment.hpp>
%include <nitf/ImageSource.hpp>
%include <nitf/ImageSubheader.hpp>
%include <nitf/System.hpp>
%include <nitf/Types.h>
#include <six/sicd/SICDMesh.h>
%include <six/sicd/SICDMesh.h>
%import "math_poly.i"
%import "types.i"
%import "six.i"
%import "io.i"
%import "mem.i"
// This allows functions that return auto_ptrs to work properly
%auto_ptr(six::sicd::ComplexData);
%auto_ptr(scene::ProjectionPolynomialFitter);
%auto_ptr(six::sicd::NoiseMesh);
%auto_ptr(six::sicd::ScalarMesh);
%typemap(out) nitf::Uint32, nitf::Int32{$result = PyInt_FromLong($1);}
%typemap(in) nitf::Uint32{$1 = (nitf::Uint32)PyInt_AsLong($input);}
%typemap(out) nitf::Off{$result = PyLong_FromLong($1);}
%typemap(in) nitf::Off{$1 = (nitf::Off)PyLong_AsLong($input);}
%ignore nitf::Record::getDataExtensions;
%ignore nitf::Record::getImages;
%ignore nitf::Record::getGraphics;
%ignore nitf::Record::getLabels;
%ignore nitf::Record::getTexts;
%ignore nitf::Record::getReservedExtensions;
%include <nitf/Record.hpp>
/* wrap that function defined in the header section */
six::sicd::ComplexData * asComplexData(six::Data* data);
void writeNITF(const std::string& pathname, const std::vector<std::string>&
schemaPaths, const six::sicd::ComplexData& data, long long imageAdr);
Data* readNITF(const std::string& pathname,
const std::vector<std::string>& schemaPaths);
std::auto_ptr<six::sicd::ComplexData> cropMetaData(
const six::sicd::ComplexData& complexData,
const types::RowCol<size_t>& aoiOffset,
const types::RowCol<size_t>& aoiDims);
nitf::Record _readRecord(const std::string& pathname);
%pythoncode
%{
import os
import sys
from coda.math_poly import Poly2D
def schema_path():
"""Provide an absolute path to the schemas."""
try:
pysix_path = os.path.dirname(__file__)
except NameError:
# Must be running as __main__, so use sys.argv
pysix_path = os.path.dirname(sys.argv[0])
return os.path.abspath(os.path.join(pysix_path, 'schemas'))
%}
/* prevent name conflicts */
%rename ("SixSicdUtilities") six::sicd::Utilities;
%include "six/CollectionInformation.h"
%import "scene/GridECEFTransform.h"
%include "scene/ProjectionPolynomialFitter.h"
%include "six/sicd/ComplexClassification.h"
%include "six/sicd/ImageCreation.h"
%include "six/sicd/ImageData.h"
%include "six/sicd/GeoData.h"
%include "six/sicd/Grid.h"
%include "six/sicd/Timeline.h"
%include "six/sicd/Position.h"
%include "six/sicd/RadarCollection.h"
%include "six/sicd/ImageFormation.h"
%include "six/sicd/SCPCOA.h"
%include "six/sicd/Antenna.h"
%include "six/sicd/PFA.h"
%include "six/sicd/RMA.h"
%include "six/sicd/RgAzComp.h"
%include "six/sicd/ComplexData.h"
%include "six/sicd/ComplexXMLControl.h"
%include "six/sicd/Utilities.h"
%include "six/sicd/AreaPlaneUtility.h"
%include "six/sicd/GeoLocator.h"
/* We need this because SWIG cannot do it itself, for some reason */
/* TODO: write script to generate all of these instantiations for us? */
SCOPED_COPYABLE(six::sicd, GeoData)
SCOPED_CLONEABLE(six::sicd, ImageCreation)
SCOPED_COPYABLE(six::sicd, ImageData)
SCOPED_CLONEABLE(six::sicd, Grid)
SCOPED_COPYABLE(six::sicd, Timeline)
SCOPED_COPYABLE(six::sicd, Position)
SCOPED_COPYABLE(six::sicd, RcvAPC)
SCOPED_CLONEABLE(six::sicd, RadarCollection)
SCOPED_COPYABLE(six::sicd, ImageFormation)
SCOPED_COPYABLE(six::sicd, SCPCOA)
SCOPED_COPYABLE(six::sicd, Antenna)
SCOPED_COPYABLE(six::sicd, SlowTimeDeskew)
SCOPED_COPYABLE(six::sicd, PFA)
SCOPED_COPYABLE(six::sicd, RMA)
SCOPED_COPYABLE(six::sicd, RgAzComp)
%template(VectorLatLon) std::vector<scene::LatLon>;
SCOPED_COPYABLE(six::sicd, AntennaParameters)
SCOPED_COPYABLE(six::sicd, ElectricalBoresight)
SCOPED_COPYABLE(six::sicd, HalfPowerBeamwidths)
SCOPED_COPYABLE(six::sicd, GainAndPhasePolys)
SCOPED_COPYABLE(six::sicd, WeightType)
%template(VectorPolyXYZ) std::vector<math::poly::OneD<Vector3> >;
SCOPED_CLONEABLE(six::sicd, DirectionParameters)
SCOPED_CLONEABLE(six::sicd, AreaPlane)
SCOPED_CLONEABLE(six::sicd, AreaDirectionParameters)
SCOPED_CLONEABLE(six::sicd, Segment)
SCOPED_CLONEABLE(six::sicd, TxStep)
SCOPED_CLONEABLE(six::sicd, WaveformParameters)
SCOPED_CLONEABLE(six::sicd, Area)
SCOPED_CLONEABLE(six::sicd, ChannelParameters)
%template(VectorScopedCloneableWaveformParameters) std::vector<mem::ScopedCloneablePtr<six::sicd::WaveformParameters> >;
%template(VectorScopedCloneableTxStep) std::vector<mem::ScopedCloneablePtr<six::sicd::TxStep> >;
%template(vectorScopedClonableSegment) std::vector<mem::ScopedCloneablePtr<six::sicd::Segment> >;
%template(VectorScopedCloneableChannelParameters) std::vector<mem::ScopedCloneablePtr<six::sicd::ChannelParameters> >;
%template(VectorInt) std::vector<int>;
%template(VectorString) std::vector<std::string>;
SCOPED_COPYABLE(six::sicd, RcvChannelProcessed)
%template(VectorProcessing) std::vector<six::sicd::Processing>;
SCOPED_COPYABLE(six::sicd, PolarizationCalibration)
SCOPED_COPYABLE(six::sicd, Distortion)
SCOPED_COPYABLE(six::sicd, RMAT)
SCOPED_COPYABLE(six::sicd, RMCR)
SCOPED_COPYABLE(six::sicd, INCA)
SCOPED_COPYABLE(six::sicd, InterPulsePeriod)
%template(VectorTimelineSet) std::vector<six::sicd::TimelineSet>;
// NOTE: In the cases below, need to use 'long long' rather
// than 'size_t'. Otherwise, Swig will generate code
// using PyInt_FromLong(). This will work fine on
// 64-bit Unix where sizeof(long) == 8 and works fine
// on 64-bit Windows where sizeof(long) == 4... until
// a value gets too large to represent in 4 bytes at
// which point you'll get cryptic/confusing runtime
// errors. This happens in particular when trying to
// send NumPy arrays to/from C++ when you allocate an
// array > 4 GB. It seems like Swig should be smarter
// in what it auto-generates to avoid this.
%{
void getWidebandData(const std::string& sicdPathname, const std::vector<std::string>& schemaPaths, six::sicd::ComplexData* complexData, long long arrayBuffer)
{
std::complex<float>* realBuffer = reinterpret_cast< std::complex<float>* >(arrayBuffer);
Utilities::getWidebandData(sicdPathname, schemaPaths, *complexData, realBuffer);
}
void getWidebandRegion(const std::string& sicdPathname, const std::vector<std::string>& schemaPaths, six::sicd::ComplexData* complexData,
long long startRow, long long numRows, long long startCol, long long numCols, long long arrayBuffer)
{
std::complex<float>* realBuffer = reinterpret_cast< std::complex<float>* >(arrayBuffer);
types::RowCol<size_t> offset(startRow, startCol);
types::RowCol<size_t> extent(numRows, numCols);
Utilities::getWidebandData(sicdPathname, schemaPaths, *complexData, offset, extent, realBuffer);
}
%}
void getWidebandData(std::string sicdPathname, const std::vector<std::string>& schemaPaths, six::sicd::ComplexData* complexData, long long arrayBuffer);
void getWidebandRegion(std::string sicdPathname, const std::vector<std::string>& schemaPaths, six::sicd::ComplexData* complexData, long long startRow, long long numRows, long long startCol, long long numCols, long long arrayBuffer);
%pythoncode %{
import numpy as np
from coda.coda_types import VectorString
from coda.coda_io import FileOutputStream
from coda.xml_lite import *
def read(inputPathname, schemaPaths = VectorString()):
complexData = SixSicdUtilities.getComplexData(inputPathname, schemaPaths)
#Numpy has no concept of complex integers, so dtype will always be complex64
widebandData = np.empty(shape = (complexData.getNumRows(), complexData.getNumCols()), dtype = "complex64")
widebandBuffer, ro = widebandData.__array_interface__["data"]
getWidebandData(inputPathname, schemaPaths, complexData, widebandBuffer)
return widebandData, complexData
def readRegion(inputPathname, startRow, numRows, startCol, numCols, schemaPaths = VectorString()):
complexData = SixSicdUtilities.getComplexData(inputPathname, schemaPaths)
widebandData = np.empty(shape = (numRows, numCols), dtype = "complex64")
widebandBuffer, ro = widebandData.__array_interface__["data"]
getWidebandRegion(inputPathname, schemaPaths, complexData, startRow, numRows, startCol, numCols, widebandBuffer)
return widebandData, complexData
def readRecord(pathname):
record = _readRecord(pathname)
attributes = dir(record)
for attribute in attributes:
if (attribute.startswith('move') or
attribute.startswith('remove') or
attribute.startswith('set') or
attribute.startswith('new')):
delattr(record.__class__, attribute)
return record
def writeAsNITF(outFile, schemaPaths, complexData, image):
writeNITF(outFile, schemaPaths, complexData,
image.__array_interface__["data"][0])
def readFromNITF(pathname, schemaPaths=VectorString()):
return readNITF(pathname, schemaPaths)
%}
%include "six/sicd/SICDWriteControl.h"
%extend six::sicd::SICDWriteControl
{
void write(PyObject* data, const types::RowCol<size_t>& offset)
{
numpyutils::verifyArrayType(data, NPY_COMPLEX64);
// TODO: Force array to be contigious memory
// Right now we're requiring the caller to do that
// TODO: If we get noncontiguous memory, maybe we want to
// instead do multiple calls to save() ourselves to
// avoid the memory allocation
$self->save(numpyutils::getBuffer<std::complex<float> >(data),
offset,
numpyutils::getDimensionsRC(data));
}
void initXMLControlRegistry(six::XMLControlRegistry& xmlRegistry)
{
xmlRegistry.addCreator(six::DataType::COMPLEX,
new six::XMLControlCreatorT<
six::sicd::ComplexXMLControl>());
$self->setXMLControlRegistry(&xmlRegistry);
}
}
%extend nitf::Record
{
nitf::ImageSegment getImageSegment(size_t index)
{
if (index >= $self->getNumImages())
{
throw except::Exception(Ctxt("Index out of bounds"));
}
nitf::ListIterator iter = $self->getImages().begin();
for (size_t ii = 0; ii < index; ++ii)
{
iter.increment();
}
nitf::ImageSegment segment = *iter;
return segment;
}
nitf::DESegment getDataExtension(size_t index)
{
if (index >= $self->getNumDataExtensions())
{
throw except::Exception(Ctxt("Index out of bounds"));
}
nitf::ListIterator iter = $self->getDataExtensions().begin();
for (size_t ii = 0; ii < index; ++ii)
{
iter.increment();
}
nitf::DESegment segment = *iter;
return segment;
}
}
%extend nitf::Field
{
%pythoncode
%{
def parse(self):
data = self.getRawData()
try:
data = float(data)
if data - int(data) == 0:
data = int(data)
return data
except ValueError:
return str(data)
%}
}
%extend scene::ProjectionPolynomialFitter
{
%pythoncode
%{
def fitOutputToSlantPolynomials(
self, offset, inSceneCenter,
interimSceneCenter, interimSampleSpacing,
polyOrderX, polyOrderY):
toSlantRow = Poly2D()
toSlantCol = Poly2D()
self._fitOutputToSlantImpl(
offset, inSceneCenter, interimSceneCenter, interimSampleSpacing,
polyOrderX, polyOrderY, toSlantRow, toSlantCol)
return (toSlantRow, toSlantCol)
def fitSlantToOutputPolynomials(
self, offset, inSceneCenter,
interimSceneCenter, interimSampleSpacing,
polyOrderX, polyOrderY):
toOutputRow = Poly2D()
toOutputCol = Poly2D()
self._fitSlantToOutputImpl(
offset, inSceneCenter, interimSceneCenter, interimSampleSpacing,
polyOrderX, polyOrderY, toOutputRow, toOutputCol)
return (toOutputRow, toOutputCol)
%}
}