fmihpc / analysator

Python based package for analyzing vlsv files produced by Vlasiator
GNU General Public License v2.0
5 stars 32 forks source link

Consolidated handling of single variables vs arrays of variables #250

Open alhom opened 2 weeks ago

alhom commented 2 weeks ago

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: https://github.com/fmihpc/analysator/blob/a1841530489320caa378dccfab9414fa2e1b8cf1/pyVlsv/vlsvreader.py#L1818 End: https://github.com/fmihpc/analysator/blob/a1841530489320caa378dccfab9414fa2e1b8cf1/pyVlsv/vlsvreader.py#L1836