-
Notifications
You must be signed in to change notification settings - Fork 0
/
LUT_interpolate.py
105 lines (78 loc) · 2.64 KB
/
LUT_interpolate.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
# -*- coding: utf-8 -*-
"""
LUT interpolate
Reads lookup table (.lut) files and executes a piecewise linear interpolant
in N dimensions.
Purpose: Allows discrete look up tables to be used to calulate continuous
solutions for atmospheric correction :)
Output: Pickled interpolator object with file extension (.ilut)
"""
import glob
import os
import pickle
import sys
import time
import re
from LUT_build import permutate_invars
import numpy as np
from scipy.interpolate import LinearNDInterpolator
def create_interpolator(filename):
"""
Loads a LUT file and creates an interpolated LUT object.
The interpolant is constructed by triangulating the input data using Qhull
and performing linear barycentric interpolation on each triangle
"""
#load LUT
LUT = pickle.load(open(filename,"rb"))
# LUT inputs (H2O, O3, etc.) and outputs (i.e. atmcorr coeffs)
inputs = permutate_invars(LUT['config']['invars'])
outputs = LUT['outputs']
# piecewise linear interpolant in N dimensions
t = time.time()
interpolator = LinearNDInterpolator(inputs,outputs)
print('Interpolation took {:.2f} (secs) = '.format(time.time()-t))
# sanity check
print('Quick check..')
i = 0
true = (outputs[i][0],outputs[i][1])
interp = interpolator(inputs[i][0],inputs[i][1],inputs[i][2],inputs[i][3],inputs[i][4])
print('true = {0[0]:.2f} {0[1]:.2f}'.format(true))
print('interp = {0[0]:.2f} {0[1]:.2f}'.format(interp))
return interpolator
def main():
args = sys.argv[1:]
if len(args) != 1:
print('usage: $ python3 LUT_interpolate.py path/to/LUT_directory')
sys.exit(1)
else:
lut_path = args[0]
try:
os.chdir(lut_path)
except:
print('invalid directory: ' + lut_path)
sys.exit(1)
# create iLUTs directory (i.e. swap '/LUTs/' with '/iLUTs/')
match = re.search('LUTs',lut_path)
base_path = lut_path[0:match.start()]
end_path = lut_path[match.end():]
ilut_path = base_path+'iLUTs'+end_path
fnames = glob.glob('*.lut')
fnames.sort()
if len(fnames) == 0:
print('could not find .lut files in path: ' + lut_path)
sys.exit(1)
if not os.path.exists(ilut_path):
os.makedirs(ilut_path)
# interpolate LUT files
for fname in fnames:
fid, ext = os.path.splitext(fname)
ilut_filepath = os.path.join(ilut_path,fid+'.ilut')
if os.path.isfile(ilut_filepath):
print('iLUT file already exists (skipping interpolation): {}'
.format(os.path.basename(ilut_filepath)))
else:
print('Interpolating: '+fname)
interpolator = create_interpolator(fname)
pickle.dump(interpolator, open(ilut_filepath, 'wb' ))
if __name__ == '__main__':
main()