Images.py 3.35 KB
Newer Older
bepinat's avatar
bepinat committed
1 2 3 4 5 6 7 8
import numpy as np
from astropy.io import fits
from scipy.interpolate import interp2d
import Tools.tools as tools


class Image:
    """
bepinat's avatar
bepinat committed
9
    Stocks a fits file and determines all parameters like size etc ...
bepinat's avatar
bepinat committed
10

bepinat's avatar
bepinat committed
11 12
    :param string or ndarray filename: path+name of the file, can be a numpy array.
    """
bepinat's avatar
bepinat committed
13 14 15 16 17 18
    def __init__(self, filename, mask=None):
        self.data_rebin = None

        if type(filename) is str:
            self.header = fits.getheader(filename)
            self.data = fits.getdata(filename)
bepinat's avatar
bepinat committed
19 20
            self.length = self.header['NAXIS1']
            self.height = self.header['NAXIS2']
bepinat's avatar
bepinat committed
21
            self.size = np.array(np.shape(self.data))
22
            self.len = self.length*self.height
bepinat's avatar
bepinat committed
23 24 25 26 27 28 29 30 31 32 33 34
            self.oversample = 1
            if mask is None:
                self.mask = np.logical_not(np.isnan(self.data))
            else:
                self.mask = mask
            self.nan_to_num()

        # Add for create model without Image's class
        if type(filename) is np.ndarray:
            self.header = None
            self.data = np.nan_to_num(filename)
            self.size = np.array(np.shape(self.data))
35
            self.height = self.size[0]
bepinat's avatar
bepinat committed
36
            self.length = self.size[1]
37
            self.len = self.length*self.height
bepinat's avatar
bepinat committed
38 39 40 41 42 43 44 45 46
            self.oversample = 1
            if mask is None:
                self.mask = np.logical_not(np.isnan(self.data))
            else:
                self.mask = mask
            self.nan_to_num()

    def nan_to_num(self):
        """
bepinat's avatar
bepinat committed
47
        Converts 'nan' into 0
bepinat's avatar
bepinat committed
48 49 50 51 52 53 54 55 56
        """
        self.data = np.nan_to_num(self.data)

    def get_size(self):
        return self.size

    def get_lenght(self):
        return self.length

57 58
    def get_height(self):
        return self.height
bepinat's avatar
bepinat committed
59 60 61 62 63 64 65 66 67

    def set_oversamp(self, oversamp):
        self.oversample = oversamp

    def get_oversamp(self):
        return self.oversample

    def interpolation(self):
        """
bepinat's avatar
bepinat committed
68
        Performs an interpolation of the image at higher resolution and stock the new image
bepinat's avatar
bepinat committed
69
        """
70 71 72 73
        x = np.arange(self.length) + 0.5
        y = np.arange(self.height) + 0.5
        new_x = (np.arange(self.length * self.oversample) + 0.5) / self.oversample
        new_y = (np.arange(self.height * self.oversample) + 0.5) / self.oversample
74 75 76 77 78 79
        
        #x = np.arange(self.length)  # + 0.5*self.oversample
        #y = np.arange(self.height)  # + 0.5*self.oversample
        #new_x = np.linspace(0, self.length, self.length * int(self.oversample))
        #new_y = np.linspace(0, self.height, self.height * int(self.oversample))
        
80 81 82 83 84 85 86 87
        #x = np.linspace(0, self.length, self.length)  # + 0.5*self.oversample
        #y = np.linspace(0, self.height, self.height)  # + 0.5*self.oversample
        #new_x = np.linspace(0, self.length, self.length * int(self.oversample))
        #new_y = np.linspace(0, self.height, self.height * int(self.oversample))
        
        func = interp2d(x, y, self.data, kind='linear', fill_value=0)
        self.data = np.array(func(new_x, new_y))
        
88
        self.height *= self.oversample
bepinat's avatar
bepinat committed
89 90 91 92 93
        self.length *= self.oversample
        self.size *= self.oversample

    def conv_inter_flux(self, psf):
        """
94
        Does a convolution of the interpolated image with a psf and stocks the result
bepinat's avatar
bepinat committed
95 96 97 98

        :param PSF psf: psf object
        """
        self.data_rebin = tools.rebin_data(psf.convolution(self.data), self.oversample)