fmihpc/analysator

Consolidated handling of single variables vs arrays of variables

Opened this issue · 0 comments

The following pattern is repeated often, used to handle cases where both single cellids and lists of cellids are supported as function arguments. Vectorized operations are preferred for speed, so (often) single cellid arguments are expanded into numpy arrays to be able to handle the computations in vectorized form without duplicate code.

It might be possible to handle these cases with a function decorator-wrapper, and would simplify the code quite a bit (and help with handling e.g. several return paths).

      stack = True
      if not hasattr(cellid,"__len__"):
         cellid = np.atleast_1d(cellid)
         stack = False
      # .
      # .
      # .
      if stack:
         return AMR_count - 1 
      else:
         return (AMR_count - 1)[0]

Example:
Start:

stack = True

End:
if stack: