Skip to content

Commit

Permalink
add encoder to python embedding script (to test, should move later)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgemccabe committed Feb 7, 2025
1 parent 4ea7407 commit f82db67
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion scripts/python/pyembed/python_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@
import json
import math
from importlib import util as import_util
import numpy as np

# define JSON encoder to convert numpy types to python
# note: this also exists in python.met.logger
# can this be imported instead of redefined?
class NumpyTypeEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.generic):
return obj.item()
elif isinstance(obj, np.ndarray):
return obj.tolist()
return json.JSONEncoder.default(self, obj)

class pyembed_tools():

Expand Down Expand Up @@ -133,7 +145,7 @@ def write_tmp_ascii(filename, met_data):

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

@staticmethod
def read_tmp_diag(filename):
Expand Down

0 comments on commit f82db67

Please sign in to comment.