Skip to content

Commit

Permalink
Check which datareducers are available based on the reader object wit…
Browse files Browse the repository at this point in the history
…h VlsvReader.list(datareducer=True)
  • Loading branch information
alhom committed Sep 27, 2024
1 parent 04b62b3 commit c6906a3
Showing 1 changed file with 71 additions and 4 deletions.
75 changes: 71 additions & 4 deletions pyVlsv/vlsvreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ def __init__(self, file_name, fsGridDecomposition=None):

self.variable_cache = {} # {(varname, operator):data}

self.__available_reducers = set() # Set of strings of datareducer names
self.__unavailable_reducers = set() # Set of strings of datareducer names
self.__current_reducer_tree_nodes = set() # Set of strings of datareducer names

self.__read_xml_footer()
# vertex-indices is a 3-tuple of integers
self.__dual_cells = {(0,0,0):(1,1,1,1,1,1,1,1)} # vertex-indices : 8-tuple of cellids at each corner (for x for y for z)
Expand Down Expand Up @@ -540,6 +544,64 @@ def __set_cell_offset_and_blocks_nodict(self, pop="proton"):
for index,cellid in enumerate(self.__cells_with_blocks[pop]):
self.__order_for_cellid_blocks[pop][cellid]=index

def __check_datareducer(self, name, reducer):

reducer_ok = True
if name in self.__available_reducers: return True
if name in self.__unavailable_reducers: return False
if name in self.__current_reducer_tree_nodes: raise RuntimeError("Cyclical datareduction deteced with "+name+", this is weird and undefined!")

self.__current_reducer_tree_nodes.add(name)

for var in reducer.variables:
if len(var) > 3 and var[0:3] == "pop":
in_vars = False
for pop in self.active_populations:
popvar = pop+var[3:]
if popvar in self.__available_reducers:
in_vars = True
elif popvar in self.__unavailable_reducers:
in_vars = False
else:
in_vars = self.check_variable(popvar)

# print(popvar," is in vars: ",in_vars)
if in_vars:
self.__available_reducers.add(popvar)
break
else:
self.__unavailable_reducers.add(popvar)
else:
in_vars = self.check_variable(var)

reducer_ok = reducer_ok and in_vars
if in_vars:
continue

in_reducers = ((var in datareducers.keys()) or
(var in multipopdatareducers.keys()) or
(var in v5reducers.keys()) or
(var in multipopv5reducers.keys()))
if in_reducers:
reducer = None
for reducer_reg in [datareducers, multipopdatareducers, v5reducers, multipopv5reducers]:
try:
reducer = reducer_reg[var]
except:
pass
if reducer is None: # Not in variables not in datareducers, break
reducer_ok = False
break
reducer_ok = reducer_ok and self.__check_datareducer(var, reducer)

if not reducer_ok: break

if reducer_ok: self.__available_reducers.add(name)
else: self.__unavailable_reducers.add(name)

return reducer_ok


def list(self, parameter=True, variable=True, mesh=False, datareducer=False, operator=False, other=False):
''' Print out a description of the content of the file. Useful
for interactive usage. Default is to list parameters and variables, query selection can be adjusted with keywords:
Expand All @@ -562,16 +624,21 @@ def list(self, parameter=True, variable=True, mesh=False, datareducer=False, ope
print("tag = VARIABLE")
for child in self.__xml_root:
if child.tag == "VARIABLE" and "name" in child.attrib:
print(" ", child.attrib["name"])
print(" ", child.attrib["name"], "check_variable ", self.check_variable( child.attrib["name"]))
if mesh:
print("tag = MESH")
for child in self.__xml_root:
if child.tag == "MESH" and "name" in child.attrib:
print(" ", child.attrib["name"])
if datareducer:
print("Datareducers:")
for name in datareducers:
print(" ",name, " based on ", datareducers[name].variables)
print("Datareducers (replace leading pop with a population name):")

for reducer_reg in [datareducers, multipopdatareducers, v5reducers, multipopv5reducers]:
for name, reducer in reducer_reg.items():
self.__current_reducer_tree_nodes.clear()
if self.__check_datareducer(name,reducer):
print(" ",name, " based on ", reducer_reg[name].variables)

if operator:
print("Data operators:")
for name in data_operators:
Expand Down

0 comments on commit c6906a3

Please sign in to comment.