@@ -59,20 +59,15 @@ def read_input(csvfile):
59
59
first column is wavelength
60
60
values are those of the discrete band filter functions
61
61
"""
62
- infile = open (csvfile )
62
+ with open (csvfile ) as infile :
63
+ # get number of bands and band names
64
+ bands = infile .readline ().strip ().split ("," )[1 :]
63
65
64
- # get number of bands and band names
65
- bands = infile .readline ().split ("," )
66
- bands .remove (bands [0 ])
67
- bands [- 1 ] = bands [- 1 ].strip ()
68
- print (" > Number of bands found: %d" % len (bands ))
69
- infile .close ()
66
+ print (f" > Number of bands found: { len (bands )} " )
70
67
71
68
# create converter dictionary for import
72
69
# fix nodata or \n
73
- conv = {}
74
- for b in range (len (bands )):
75
- conv [b + 1 ] = lambda s : float (s or 0 )
70
+ conv = {b + 1 : lambda s : float (s or 0 ) for b in range (len (bands ))}
76
71
77
72
values = np .loadtxt (csvfile , delimiter = "," , skiprows = 1 , converters = conv )
78
73
@@ -87,10 +82,8 @@ def interpolate_band(values, step=2.5):
87
82
and min, max wl values
88
83
values must be numpy array with 2 columns
89
84
"""
90
-
91
85
# removing nodata and invalid values
92
- w = values [:, 1 ] >= 0
93
- values_clean = values [w ]
86
+ values_clean = values [values [:, 1 ] >= 0 ]
94
87
95
88
wavelengths = values_clean [:, 0 ] # 1st column of input array
96
89
responses = values_clean [:, 1 ] # 2nd column
@@ -184,25 +177,25 @@ def pretty_print(filter_f):
184
177
Create pretty string out of filter function
185
178
8 values per line, with spaces, commas and all the rest
186
179
"""
187
- pstring = ""
180
+ pstring = []
188
181
for i in range (len (filter_f ) + 1 ):
189
182
if i % 8 == 0 :
190
183
if i != 0 :
191
184
value_wo_leading_zero = ("%.4f" % (filter_f [i - 1 ])).lstrip ("0" )
192
- pstring += value_wo_leading_zero
193
- if i > 1 and i < len ( filter_f ) :
194
- pstring += ", "
195
- if i != 1 :
185
+ pstring . append ( value_wo_leading_zero )
186
+ if i > 1 :
187
+ if i < len ( filter_f ):
188
+ pstring . append ( ", " )
196
189
# trim the trailing whitespace at the end of line
197
- pstring = pstring .rstrip ()
198
- pstring += "\n "
190
+ pstring [ - 1 ] = pstring [ - 1 ] .rstrip ()
191
+ pstring . append ( "\n " )
199
192
else :
200
193
value_wo_leading_zero = ("%.4f" % (filter_f [i - 1 ])).lstrip ("0" )
201
- pstring += value_wo_leading_zero
194
+ pstring . append ( value_wo_leading_zero )
202
195
if i < len (filter_f ):
203
- pstring += ", "
196
+ pstring . append ( ", " )
204
197
# trim starting \n and trailing ,
205
- return pstring .lstrip ("\n " ).rstrip (", " )
198
+ return "" . join ( pstring ) .lstrip ("\n " ).rstrip (", " )
206
199
207
200
208
201
def write_cpp (bands , values , sensor , folder ):
@@ -212,6 +205,24 @@ def write_cpp(bands, values, sensor, folder):
212
205
needs other functions: interpolate_bands, pretty_print
213
206
"""
214
207
208
+ def get_min_wavelength (c , rthresh , fi ):
209
+ """Get minimum wavelength rounded by threshold.
210
+
211
+ :param fi: filter function
212
+ """
213
+ while c > 0 and fi [c - 1 ] > rthresh :
214
+ c -= 1
215
+ return np .ceil (li [0 ] * 1000 + (2.5 * c ))
216
+
217
+ def get_max_wavelength (c , rthresh , fi ):
218
+ """Get maximum wavelength rounded by threshold.
219
+
220
+ :param fi: filter function
221
+ """
222
+ while c < len (fi ) - 1 and fi [c + 1 ] > rthresh :
223
+ c += 1
224
+ return np .floor (li [0 ] * 1000 + (2.5 * c ))
225
+
215
226
# keep in sync with IWave::parse()
216
227
rthresh = 0.01
217
228
print (" > Response peaks from interpolation to 2.5 nm steps:" )
@@ -225,17 +236,8 @@ def write_cpp(bands, values, sensor, folder):
225
236
li = limits
226
237
# Get wavelength range for spectral response in band
227
238
maxresponse_idx = np .argmax (fi )
228
- # Get minimum wavelength with spectral response
229
- c = maxresponse_idx
230
- while c > 0 and fi [c - 1 ] > rthresh :
231
- c -= 1
232
- min_wavelength = np .ceil (li [0 ] * 1000 + (2.5 * c ))
233
- # Get maximum wavelength with spectral response
234
- c = maxresponse_idx
235
- while c < len (fi ) - 1 and fi [c + 1 ] > rthresh :
236
- c += 1
237
- max_wavelength = np .floor (li [0 ] * 1000 + (2.5 * c ))
238
- print (" %s (%inm - %inm)" % (bands [0 ], min_wavelength , max_wavelength ))
239
+ min_wavelength = get_min_wavelength (maxresponse_idx , rthresh , fi )
240
+ max_wavelength = get_max_wavelength (maxresponse_idx , rthresh , fi )
239
241
240
242
else :
241
243
filter_f = []
@@ -247,29 +249,17 @@ def write_cpp(bands, values, sensor, folder):
247
249
248
250
# Get wavelength range for spectral response in band
249
251
maxresponse_idx = np .argmax (fi )
250
- # Get minimum wavelength with spectral response
251
- c = maxresponse_idx
252
- while c > 0 and fi [c - 1 ] > rthresh :
253
- c -= 1
254
- min_wavelength = np .ceil (li [0 ] * 1000 + (2.5 * c ))
255
- # Get maximum wavelength with spectral response
256
- c = maxresponse_idx
257
- while c < len (fi ) - 1 and fi [c + 1 ] > rthresh :
258
- c += 1
259
- max_wavelength = np .floor (li [0 ] * 1000 + (2.5 * c ))
252
+ min_wavelength = get_min_wavelength (maxresponse_idx , rthresh , fi )
253
+ max_wavelength = get_max_wavelength (maxresponse_idx , rthresh , fi )
260
254
print (" %s (%inm - %inm)" % (bands [b ], min_wavelength , max_wavelength ))
261
255
262
256
# writing...
263
257
outfile = open (os .path .join (folder , sensor + "_cpp_template.txt" ), "w" )
264
258
outfile .write ("/* Following filter function created using create_iwave.py */\n \n " )
265
259
266
- if len (bands ) == 1 :
267
- outfile .write ("void IWave::%s()\n {\n \n " % (sensor .lower ()))
268
- else :
269
- outfile .write ("void IWave::%s(int iwa)\n {\n \n " % (sensor .lower ()))
270
-
271
260
# single band case
272
261
if len (bands ) == 1 :
262
+ outfile .write ("void IWave::%s()\n {\n \n " % (sensor .lower ()))
273
263
outfile .write (" /* %s of %s */\n " % (bands [0 ], sensor ))
274
264
outfile .write (" static const float sr[%i] = {" % (len (filter_f )))
275
265
filter_text = pretty_print (filter_f )
@@ -295,6 +285,7 @@ def write_cpp(bands, values, sensor, folder):
295
285
outfile .write ("}\n " )
296
286
297
287
else : # more than 1 band
288
+ outfile .write ("void IWave::%s(int iwa)\n {\n \n " % (sensor .lower ()))
298
289
# writing bands
299
290
for b in range (len (bands )):
300
291
outfile .write (" /* %s of %s */\n " % (bands [b ], sensor ))
@@ -305,9 +296,8 @@ def write_cpp(bands, values, sensor, folder):
305
296
outfile .write (filter_text + "\n };\n \t \n " )
306
297
307
298
# writing band limits
308
- for b in range (len (bands )):
309
- inf = ", " .join (["%.4f" % i [0 ] for i in limits ])
310
- sup = ", " .join (["%.4f" % i [1 ] for i in limits ])
299
+ inf = ", " .join (["%.4f" % i [0 ] for i in limits ])
300
+ sup = ", " .join (["%.4f" % i [1 ] for i in limits ])
311
301
312
302
outfile .write (" static const float wli[%i] = {%s};\n " % (len (bands ), inf ))
313
303
outfile .write (" static const float wls[%i] = {%s};\n " % (len (bands ), sup ))
0 commit comments