Skip to content

Commit

Permalink
i.atcorr: Fixed ruff SIM115 by using context manager to open file (OS…
Browse files Browse the repository at this point in the history
  • Loading branch information
arohanajit authored Feb 4, 2025
1 parent 2070d36 commit e289f0a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 57 deletions.
119 changes: 63 additions & 56 deletions imagery/i.atcorr/create_iwave.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,72 +255,79 @@ def get_max_wavelength(c, rthresh, fi):
print(" %s (%inm - %inm)" % (bands[b], min_wavelength, max_wavelength))

# writing...
outfile = open(os.path.join(folder, sensor + "_cpp_template.txt"), "w")
outfile.write("/* Following filter function created using create_iwave.py */\n\n")

# single band case
if len(bands) == 1:
outfile.write("void IWave::%s()\n{\n\n" % (sensor.lower()))
outfile.write(" /* %s of %s */\n" % (bands[0], sensor))
outfile.write(" static const float sr[%i] = {" % (len(filter_f)))
filter_text = pretty_print(filter_f)
outfile.write(filter_text)

# calculate wl slot for band start
# slots range from 250 to 4000 at 2.5 increments (total 1500)
s_start = int((limits[0] * 1000 - 250) / 2.5)

outfile.write("\n")
outfile.write(" ffu.wlinf = %.4ff;\n" % (limits[0]))
outfile.write(" ffu.wlsup = %.4ff;\n" % (limits[1]))
outfile.write(" int i = 0;\n")
outfile.write(" for(i = 0; i < %i; i++)\tffu.s[i] = 0;\n" % (s_start))
outfile.write(
" for(i = 0; i < %i; i++)\tffu.s[%i+i] = sr[i];\n"
% (len(filter_f), s_start)
)
outpath = os.path.join(folder, sensor + "_cpp_template.txt")
with open(outpath, "w") as outfile:
outfile.write(
" for(i = %i; i < 1501; i++)\tffu.s[i] = 0;\n"
% (s_start + len(filter_f))
"/* Following filter function created using create_iwave.py */\n\n"
)
outfile.write("}\n")

else: # more than 1 band
outfile.write("void IWave::%s(int iwa)\n{\n\n" % (sensor.lower()))
# writing bands
for b in range(len(bands)):
outfile.write(" /* %s of %s */\n" % (bands[b], sensor))
# single band case
if len(bands) == 1:
outfile.write("void IWave::%s()\n{\n\n" % (sensor.lower()))
outfile.write(" /* %s of %s */\n" % (bands[0], sensor))
outfile.write(" static const float sr[%i] = {" % (len(filter_f)))
filter_text = pretty_print(filter_f)
outfile.write(filter_text)

# calculate wl slot for band start
# slots range from 250 to 4000 at 2.5 increments (total 1500)
s_start = int((limits[0] * 1000 - 250) / 2.5)

outfile.write("\n")
outfile.write(" ffu.wlinf = %.4ff;\n" % (limits[0]))
outfile.write(" ffu.wlsup = %.4ff;\n" % (limits[1]))
outfile.write(" int i = 0;\n")
outfile.write(" for(i = 0; i < %i; i++)\tffu.s[i] = 0;\n" % (s_start))
outfile.write(
" static const float sr%i[%i] = {\n" % (b + 1, len(filter_f[b]))
" for(i = 0; i < %i; i++)\tffu.s[%i+i] = sr[i];\n"
% (len(filter_f), s_start)
)
filter_text = pretty_print(filter_f[b])
outfile.write(filter_text + "\n };\n\t\n")

# writing band limits
inf = ", ".join(["%.4f" % i[0] for i in limits])
sup = ", ".join(["%.4f" % i[1] for i in limits])
outfile.write(
" for(i = %i; i < 1501; i++)\tffu.s[i] = 0;\n"
% (s_start + len(filter_f))
)
outfile.write("}\n")

else: # more than 1 band
outfile.write("void IWave::%s(int iwa)\n{\n\n" % (sensor.lower()))
# writing bands
for b in range(len(bands)):
outfile.write(" /* %s of %s */\n" % (bands[b], sensor))
outfile.write(
" static const float sr%i[%i] = {\n" % (b + 1, len(filter_f[b]))
)
filter_text = pretty_print(filter_f[b])
outfile.write(filter_text + "\n };\n\t\n")

# writing band limits
inf = ", ".join(["%.4f" % i[0] for i in limits])
sup = ", ".join(["%.4f" % i[1] for i in limits])

outfile.write(" static const float wli[%i] = {%s};\n" % (len(bands), inf))
outfile.write(" static const float wls[%i] = {%s};\n" % (len(bands), sup))
outfile.write(
" static const float wli[%i] = {%s};\n" % (len(bands), inf)
)
outfile.write(
" static const float wls[%i] = {%s};\n" % (len(bands), sup)
)

outfile.write("\n")
outfile.write(" ffu.wlinf = (float)wli[iwa-1];\n")
outfile.write(" ffu.wlsup = (float)wls[iwa-1];\n\n")
outfile.write("\n")
outfile.write(" ffu.wlinf = (float)wli[iwa-1];\n")
outfile.write(" ffu.wlsup = (float)wls[iwa-1];\n\n")

outfile.write(" int i;\n")
outfile.write(" for(i = 0; i < 1501; i++) ffu.s[i] = 0;\n\n")
outfile.write(" int i;\n")
outfile.write(" for(i = 0; i < 1501; i++) ffu.s[i] = 0;\n\n")

outfile.write(" switch(iwa)\n {\n")
outfile.write(" switch(iwa)\n {\n")

# now start of case part...
for b in range(len(bands)):
s_start = int((limits[b][0] * 1000 - 250) / 2.5)
outfile.write(
" case %i: for(i = 0; i < %i; i++) ffu.s[%i+i] = sr%i[i];\n"
% ((b + 1), len(filter_f[b]), s_start, (b + 1))
)
outfile.write(" break;\n")
outfile.write(" }\n}\n")
# now start of case part...
for b in range(len(bands)):
s_start = int((limits[b][0] * 1000 - 250) / 2.5)
outfile.write(
" case %i: for(i = 0; i < %i; i++) ffu.s[%i+i] = sr%i[i];\n"
% ((b + 1), len(filter_f[b]), s_start, (b + 1))
)
outfile.write(" break;\n")
outfile.write(" }\n}\n")


def main():
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ ignore = [
"gui/wxpython/vnet/vnet_data.py" = ["SIM115"]
"gui/wxpython/web_services/dialogs.py" = ["SIM115"]
"gui/wxpython/wxplot/profile.py" = ["A005", "SIM115"]
"imagery/i.atcorr/create_iwave.py" = ["SIM115"]
"lib/imagery/testsuite/test_imagery_signature_management.py" = ["SIM115"]
"lib/imagery/testsuite/test_imagery_sigsetfile.py" = ["FURB152"]
"lib/init/grass.py" = ["SIM115"]
Expand Down

0 comments on commit e289f0a

Please sign in to comment.