rebin method added

This commit is contained in:
Lukas Klass 2020-05-13 14:30:46 +02:00
parent 2cebfd4803
commit 944402bad5

View File

@ -1,7 +1,8 @@
from abc import ABC, abstractmethod
import astropy.units as u
import numpy as np
from ..sensor.PixelMask import PixelMask
from typing import Union
import numpy as np
class IPSF(ABC):
@ -46,3 +47,35 @@ class IPSF(ABC):
The grid with the mapped values.
"""
pass
@staticmethod
def rebin(arr: np.ndarray, factor: float):
"""
Rebin a 2D-array by summing or repeating the elements.
Parameters
----------
arr : ndarray
Input array.
factor : float
Rebinning factor
Returns
-------
rebinned_array : ndarray
If the factor is smaller than 1, the data is summed,
if the factor is bigger than 1, array elements are repeated
See Also
--------
resize : Return a new array with the specified factor.
"""
m, n = arr.shape
m_new, n_new = int(m * factor), int(n * factor)
if factor < 1:
res = arr.reshape((m_new, int(1 / factor), n_new, int(1 / factor))).sum(3).sum(1)
elif factor > 1:
res = np.repeat(np.repeat(arr, int(factor), axis=0), int(factor), axis=1)
else:
res = arr
if isinstance(arr, PixelMask):
res.pixel_size = res.pixel_size / factor
return res