Skip to content

Commit

Permalink
#2772 Change nan and inf to -9999 on reading ASCII input if failed to…
Browse files Browse the repository at this point in the history
… parse
  • Loading branch information
Howard Soh committed Jan 26, 2024
1 parent f7d1bb5 commit 7874116
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions scripts/python/pyembed/python_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import os
import sys
import json
import math
from importlib import util as import_util

class pyembed_tools():
Expand Down Expand Up @@ -93,27 +94,50 @@ def read_tmp_ascii(filename):
Returns:
(list of lists): point or mpr data
"""
f = open(filename, 'r')
lines = f.readlines()
f.close()

ascii_data = [eval(line.strip('\n')) for line in lines]
with open(filename, 'r') as f:
lines = f.readlines()
f.close()

try:
ascii_data = [eval(line.strip('\n')) for line in lines]
except:
try:
print(f' PYTHON INFO pyembed_tools.read_tmp_ascii() nan and inf are changed to -9999 from {filename}.')
ascii_data = [eval(line.strip('\n').replace("nan", "-9999").replace("inf", "-9999")) for line in lines]
except:
# Log where the problem happens
line_no = 0
line_buf = ""
try:
for line_buf in lines:
line_no += 1
eval(line_buf.strip('\n').replace("nan", "-9999").replace("inf", "-9999"))
except:
print(f' PYTHON ERROR pyembed_tools.read_tmp_ascii() failed parsing "{line_buf}" at line {line_no}')
raise

return ascii_data

@staticmethod
def write_tmp_ascii(filename, met_data):
with open(filename, 'w') as f:
inf_count = 0
nan_count = 0
for line in met_data:
f.write(str(line) + '\n')
inf_count += line.count(math.inf)
nan_count += line.count(math.nan)

if 0 < (nan_count + inf_count):
print(f' PYTHON WARNING pyembed_tools.write_tmp_ascii() Saved {nan_count} nan and {inf_count} infinite values to "{filename}"')

@staticmethod
def write_tmp_diag(filename, diag_data):
json.dump(diag_data, open(filename,'w'))
json.dump(diag_data, open(filename,'w'))

@staticmethod
def read_tmp_diag(filename):
return json.load(open(filename))
return json.load(open(filename))

if __name__ == '__main__':
argv_org = sys.argv[:] # save original sys.argv
Expand Down

0 comments on commit 7874116

Please sign in to comment.