Skip to content

Commit

Permalink
changed logger.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Known4225 committed Aug 20, 2024
1 parent 1f2aa9b commit 87d6425
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 38 deletions.
1 change: 0 additions & 1 deletion scripts/AMDC.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ def cmd(self, cmd_str, timeout_sec = 1):
except BlockingIOError:
# Could not read enough data
pass

if len(retdata) > 0:
# Convert byte array into chars
retdata_chars = retdata.decode("utf-8").split('\r\n')
Expand Down
60 changes: 23 additions & 37 deletions scripts/AMDC_Logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@
# Date: 06/30/2020
#########################################################

LogVar = namedtuple(typename = 'LogVar', field_names = 'name index var_type samples_per_sec memory_addr')
LogVar = namedtuple(typename = 'LogVar', field_names = 'name index var_type samples_per_sec')

class AMDC_Logger():

default_max_slots = 32

def __init__(self, AMDC, mapfile):
def __init__(self, AMDC):

if mapfile == None:
raise Exception("Could not find map file")

self.amdc = AMDC
self.mapfile = Mapfile(mapfile)

self.available_indices = list(range(AMDC_Logger.default_max_slots))[-1::-1]
self.log_vars = {}
Expand Down Expand Up @@ -78,7 +74,7 @@ def register(self, log_vars, samples_per_sec = 1000, var_type = 'double'):
print(e)
else:
#send command to AMDC
cmd = f'log reg {LV.index} {LV.name} {LV.memory_addr} {LV.samples_per_sec} {LV.var_type}'
cmd = f'log reg {LV.index} {LV.name} {LV.samples_per_sec} {LV.var_type}'
out = self.amdc.cmd(cmd)
if out[1] == 'FAILURE':
self._pop(LV)
Expand Down Expand Up @@ -280,29 +276,20 @@ def load(self, file):

def _create_log_var(self, name, samples_per_sec, var_type, manual_index = None):

memory_addr = int(self.mapfile.address(name), 0)

if memory_addr == 0:
raise Exception(f"ERROR: couldn't find memory address for '{name}'")

else:
if name not in self.log_vars.keys(): #check if variable name already registered
if name not in self.log_vars.keys(): #check if variable name already registered

if manual_index is None:
idx = self.available_indices.pop() #extract next available index
else:
#remove index from list of available indices
loc = self.available_indices.index(manual_index)
idx = self.available_indices.pop(loc)

#create LogVar object and append it to logvars
LV = LogVar(name = name, index = idx, var_type = var_type, samples_per_sec = samples_per_sec, memory_addr = memory_addr)
self.log_vars[name] = LV

return LV

if manual_index is None:
idx = self.available_indices.pop() #extract next available index
else:
raise Exception('Error: Variable already exists')
#remove index from list of available indices
loc = self.available_indices.index(manual_index)
idx = self.available_indices.pop(loc)

#create LogVar object and append it to logvars
LV = LogVar(name = name, index = idx, var_type = var_type, samples_per_sec = samples_per_sec)
self.log_vars[name] = LV

return LV

def _pop(self, log_var):

Expand Down Expand Up @@ -461,6 +448,7 @@ def _dump_single_bin(self, var, print_output = True):
elif data_type == 2 or data_type == 3:
s = struct.Struct('<f')
else:
print("data type " + str(data_type))
raise Exception("ERROR: unknown data type!")

samples = []
Expand All @@ -486,7 +474,7 @@ def _dump_single_bin(self, var, print_output = True):
# Round all timesteps to nearest 1usec
arr[:,0] = arr[:,0].round(6)

df = pd.DataFrame(data = arr, columns = ['t', var[4::]])
df = pd.DataFrame(data = arr, columns = ['t', var])
df['t'] = df['t'] - df['t'].min()
df.set_index('t', inplace = True)

Expand Down Expand Up @@ -535,7 +523,7 @@ def _dump_single_text(self, var):
line = ""

arr = np.array(samples)
df = pd.DataFrame(data = arr, columns = ['t', var[4::]])
df = pd.DataFrame(data = arr, columns = ['t', var])
df['t'] = df['t'] - df['t'].min()

df.set_index('t', inplace = True)
Expand Down Expand Up @@ -585,8 +573,6 @@ def _sanitize_inputs(names):
out = []

for name in names:
if not ('LOG_' in name):
name = 'LOG_' + name

out.append(name)

Expand All @@ -602,11 +588,11 @@ def _find_log_vars(file):
with open(file) as f:

for line in f:
if 'LOG_' in line:
lst = line.split()
if lst[0] in valid_types:
log_types.append(lst[0])
log_vars.append(lst[1])
# if 'LOG_' in line:
lst = line.split()
if lst[0] in valid_types:
log_types.append(lst[0])
log_vars.append(lst[1])

return log_vars, log_types

Expand Down

0 comments on commit 87d6425

Please sign in to comment.