import galsim
import numpy as np

import os
import time
import copy
# import psfConfig as mypy
import PSF.PSFInterp.psfConfig as mypy
npsf = 900  #***# 30*30

LOG_DEBUG = False #***#

class PSFInterp(object):
    # def __init__(self, PSF_data=None, params=None, PSF_data_file=None):
    def __init__(self, chip, PSF_data=None, PSF_data_file=None, sigSpin=0., psfRa=0.15):
        """
        The PSF data matrix is either given by a object parameter or read in from a file.
        Parameters:
            PSF_data: The PSF data matrix object
            params: Other parameters?
            PSF_data_file: The file for PSF data matrix (optional).
        """
        if LOG_DEBUG:
            print('===================================================')
            print('DEBUG: psf module for csstSim ' \
                  +time.strftime("(%Y-%m-%d %H:%M:%S)", time.localtime()), flush=True)
            print('===================================================')

        self.sigSpin = sigSpin
        self.sigGauss = psfRa # 80% light radius

        # iccd = 1 #***#
        # iccd = chip.chipID
        iccd = int(chip.getChipLabel(chipID=chip.chipID))
        if PSF_data_file == None:
            PSF_data_file = '/data/simudata/CSSOSDataProductsSims/data/csstPSFdata/CSSOS_psf_ciomp_30X30'
        
        self.nwave = self._getPSFwave(iccd, PSF_data_file)
        if LOG_DEBUG:
            print('nwave-{:} on ccd-{:}::'.format(self.nwave, iccd), flush=True)
        self.PSF_data = self._loadPSF(iccd, PSF_data_file)
        self.itpPSF_data = self._preprocessPSF()
        
        if LOG_DEBUG:
            print('self.PSF_data & self.itpPSF_data ... ok', flush=True)
        
        if LOG_DEBUG:
            print('Preparing self.[psfMat,cen_col,cen_row] for psfMaker ... ', end='', flush=True)
        
        ngy, ngx    = self.itpPSF_data[0][0]['psfMat'].shape
        self.psfMat = np.zeros([self.nwave, npsf, ngy, ngx])
        self.cen_col= np.zeros([self.nwave, npsf])
        self.cen_row= np.zeros([self.nwave, npsf])
        for iwave in range(self.nwave):
            for ipsf in range(npsf):
                self.psfMat[iwave, ipsf, :, :] = self.itpPSF_data[iwave][ipsf]['psfMat']
                self.cen_col[iwave, ipsf] = self.itpPSF_data[iwave][ipsf]['imgMaxPosx_ccd']
                self.cen_row[iwave, ipsf] = self.itpPSF_data[iwave][ipsf]['imgMaxPosy_ccd']
        
        if LOG_DEBUG:
            print('ok', flush=True)
        

    def _getPSFwave(self, iccd, PSF_data_file):
        """
        Get # of sampling waves on iccd
        Parameters:
            iccd: The chip of i-th ccd
            PSF_data_file: The file for PSF data matrix
        Returns:
            nwave: The number of the sampling waves
        """
        strs = os.listdir(PSF_data_file + '/ccd{:}'.format(iccd))
        nwave = 0
        for _ in strs:
            if 'wave_' in _:
                nwave += 1
        return nwave



    def _loadPSF(self, iccd, PSF_data_file):
        """
        load psf-matrix on iccd
        Parameters:
            iccd: The chip of i-th ccd
            PSF_data_file: The file for PSF data matrix
        Returns:
            psfSet: The matrix of the csst-psf
        """
        psfSet = []
        for ii in range(self.nwave):
            iwave = ii+1
            if LOG_DEBUG:
                print('self._loadPSF: iwave::', iwave, flush=True)
            psfWave = []
            for jj in range(npsf):
                ipsf = jj+1
                psfInfo = mypy.LoadPSF(iccd, iwave, ipsf, PSF_data_file, CalcPSFsize=False, InputMaxPixelPos=True)
                psfWave.append(psfInfo)
            psfSet.append(psfWave)
        if LOG_DEBUG:
            print('psfSet has been loaded:', flush=True)
            print('psfSet[iwave][ipsf][keys]:', psfSet[0][0].keys(), flush=True)
        return psfSet



    def _preprocessPSF(self):
        """
        Preprocessing psf-matrix
        Parameters:
        
        Returns:
            itpPSF_data: The matrix of the preprocessed csst-psf
        """
        itpPSF_data = copy.deepcopy(self.PSF_data)
        for iwave in range(self.nwave):
            for ipsf in range(npsf):
                psfMat = self.PSF_data[iwave][ipsf]['psfMat']
                psfMatX= mypy.psfCentering(psfMat, CenteringMode=1)
                itpPSF_data[iwave][ipsf]['psfMat'] = psfMatX
        return itpPSF_data



    def _findWave(self, bandpass):
        for iwave in range(self.nwave):
            bandwave = self.PSF_data[iwave][0]['wavelength']
            if bandpass.blue_limit < bandwave and bandwave < bandpass.red_limit:
                return iwave
        return -1
 


    def get_PSF(self, chip, pos_img, bandpass, pixSize=0.037, galsimGSObject=True):
        """
        Get the PSF at a given image position

        Parameters:
            chip: A 'Chip' object representing the chip we want to extract PSF from.
            pos_img: A 'galsim.Position' object representing the image position.
            bandpass: A 'galsim.Bandpass' object representing the wavelength range.
            pixSize: The pixels size of psf matrix
        Returns:
            PSF: A 'galsim.GSObject'.
        """
        # iccd  = 1  #***# #chip.chipID
        # iccd = chip.chipID
        iccd = int(chip.getChipLabel(chipID=chip.chipID))
        # iwave = 1  #***# #self.findWave(bandpass)
        iwave = self._findWave(bandpass)
        if iwave == -1:
            print("!!!PSF bandpass does not match.")
            exit()
        PSFMat = self.psfMat[iwave]
        cen_col= self.cen_col[iwave]
        cen_row= self.cen_row[iwave]
        # print('shape:', cen_col.shape)
        # px = pos_img[0]
        # py = pos_img[1]
        px = pos_img.x
        py = pos_img.y
        imPSF = mypy.psfMaker_IDW(px, py, PSFMat, cen_col, cen_row, IDWindex=2, OnlyNeighbors=True)
        if galsimGSObject:
            img = galsim.ImageF(imPSF, scale=pixSize)
            self.psf = galsim.InterpolatedImage(img)
            dx = px - chip.cen_pix_x
            dy = py - chip.cen_pix_y
            return self.PSFspin(x=dx, y=dy)
        return imPSF

    def PSFspin(self, x, y):
        """
        The PSF profile at a given image position relative to the axis center

        Parameters:
        theta : spin angles in a given exposure in unit of [arcsecond]
        dx, dy: relative position to the axis center in unit of [pixels]

        Return:
        Spinned PSF: g1, g2 and axis ratio 'a/b'
        """
        a2Rad = np.pi/(60.0*60.0*180.0)
        
        ff = self.sigGauss * 0.107 * (1000.0/10.0) # in unit of [pixels]
        rc = np.sqrt(x*x + y*y)
        cpix = rc*(self.sigSpin*a2Rad)

        beta = (np.arctan2(y,x) + np.pi/2)
        ell = cpix**2/(2.0*ff**2+cpix**2)
        #ell *= 10.0
        qr = np.sqrt((1.0+ell)/(1.0-ell))

        #psfShape = galsim.Shear(e=ell, beta=beta)
        #g1, g2 = psfShape.g1, psfShape.g2
        #qr = np.sqrt((1.0+ell)/(1.0-ell))

        #return ell, beta, qr
        PSFshear = galsim.Shear(e=ell, beta=beta*galsim.radians)
        return self.psf.shear(PSFshear), PSFshear



if __name__ == '__main__':
    psfPath = '/data/simudata/CSSOSDataProductsSims/data/csstPSFdata/CSSOS_psf_ciomp_30X30'
    #psfPath = '/data/simudata/CSSOSDataProductsSims/data/csstPSFdata/CSSOS_psf_ciomp'
    psfCSST = PSFInterp(PSF_data_file = psfPath)
    iwave= 1
    ipsf = 665
    pos_img = [psfCSST.cen_col[iwave, ipsf], psfCSST.cen_row[iwave, ipsf]]
    img = psfCSST.get_PSF(1, pos_img, iwave, galsimGSObject=False)
    
    #plot check-1
    import matplotlib.pyplot as plt
    fig = plt.figure(figsize=(18,5))
    ax = plt.subplot(1,3,1)
    plt.imshow(img)
    plt.colorbar()
    ax = plt.subplot(1,3,2)
    imgx = psfCSST.itpPSF_data[iwave][ipsf]['psfMat']
    imgx/= np.sum(imgx)
    plt.imshow(imgx)
    plt.colorbar()
    ax = plt.subplot(1,3,3)
    plt.imshow(img - imgx)
    plt.colorbar()
    plt.savefig('test/figs/test.jpg')

    #plot check-2: 注意图像坐标和全局坐标
    fig = plt.figure(figsize=(8,8), dpi = 200)
    img = psfCSST.PSF_data[iwave][ipsf]['psfMat']
    npix = img.shape[0]
    dng  = 105
    imgg = img[dng:-dng, dng:-dng]
    plt.imshow(imgg)
    imgX = psfCSST.PSF_data[iwave][ipsf]['image_x']  #in mm
    imgY = psfCSST.PSF_data[iwave][ipsf]['image_y']  #in mm
    deltX= psfCSST.PSF_data[iwave][ipsf]['centroid_x'] #in mm
    deltY= psfCSST.PSF_data[iwave][ipsf]['centroid_y'] #in mm
    maxX = psfCSST.PSF_data[iwave][ipsf]['max_x']
    maxY = psfCSST.PSF_data[iwave][ipsf]['max_y']
    cenPix_X = npix/2 + deltX/0.005
    cenPix_Y = npix/2 - deltY/0.005
    maxPix_X = npix/2 + maxX/0.005-1
    maxPix_Y = npix/2 - maxY/0.005-1
    plt.plot([cenPix_X-dng],[cenPix_Y-dng], 'rx', ms = 20)
    plt.plot([maxPix_X-dng],[maxPix_Y-dng], 'b+', ms=20)

    from scipy import ndimage
    y, x = ndimage.center_of_mass(img)
    plt.plot([x-dng],[y-dng], 'rx', ms = 10, mew=2)
    x, y = mypy.findMaxPix(img)
    plt.plot([x-dng],[y-dng], 'b+', ms = 10, mew=2)
    plt.savefig('test/figs/test.jpg')








