forked from Kitware/Danesfield
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraytheon_rpc.py
80 lines (71 loc) · 3.06 KB
/
raytheon_rpc.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###############################################################################
# Copyright Kitware Inc. and Contributors
# Distributed under the Apache License, 2.0 (apache.org/licenses/LICENSE-2.0)
# See accompanying Copyright.txt and LICENSE files for details
###############################################################################
"""Parse the Raytheon file format for RPC parameters
"""
import os.path
import numpy
from danesfield.rpc import RPCModel
def parse_raytheon_rpc_file(fp):
"""Parse the Raytheon RPC file format from an open file pointer
"""
def parse_rational_poly(fp):
"""Parse coefficients for a two polynomials from the file stream
"""
coeff = numpy.zeros((2, 20), dtype='float64')
idx = 0
powers = True
# The expected exponent order matrix. Currently we only support this
# default. If what is in the file doesn't match, raise an exception
exp_exp_mat = [[0, 0, 0, 1], [1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1],
[1, 1, 0, 1], [1, 0, 1, 1], [0, 1, 1, 1], [2, 0, 0, 1],
[0, 2, 0, 1], [0, 0, 2, 1], [1, 1, 1, 1], [3, 0, 0, 1],
[1, 2, 0, 1], [1, 0, 2, 1], [2, 1, 0, 1], [0, 3, 0, 1],
[0, 1, 2, 1], [2, 0, 1, 1], [0, 2, 1, 1], [0, 0, 3, 1]]
for line in fp:
if line.strip() == '20':
data = []
for i in range(20):
data.append(fp.readline())
if powers:
powers = False
exp_mat = numpy.array([d.split() for d in data],
dtype='int')
if not numpy.array_equal(exp_mat, exp_exp_mat):
raise ValueError
else:
powers = True
coeff[idx, :] = numpy.array(data, dtype='float64')
idx = idx + 1
if idx > 1:
break
return coeff
rpc = RPCModel()
for line in fp:
if line.startswith('# uvOffset_'):
line = fp.readline()
rpc.image_offset = numpy.array(line.split(), dtype='float64')
if line.startswith('# uvScale_'):
line = fp.readline()
rpc.image_scale = numpy.array(line.split(), dtype='float64')
if line.startswith('# xyzOffset_'):
line = fp.readline()
rpc.world_offset = numpy.array(line.split(), dtype='float64')
if line.startswith('# xyzScale_'):
line = fp.readline()
rpc.world_scale = numpy.array(line.split(), dtype='float64')
if line.startswith('# u=sample'):
rpc.coeff[0:2, :] = parse_rational_poly(fp)
if line.startswith('# v=line'):
rpc.coeff[2:4, :] = parse_rational_poly(fp)
return rpc
def read_raytheon_rpc_file(filename):
"""Read a Raytheon RPC file
"""
if os.path.isfile(filename):
with open(filename, 'r') as f:
return parse_raytheon_rpc_file(f)