def simul_css(CataSect, _CssImg, cssbands, filtnumb, npi):

    # print('Process'+str(npi)

    CssHei, CssWid = _CssImg.shape

    OutSecStr = ''

    LenCatSec = len(CataSect)
    procedi = 0

    if IfProgBarOn == True:
        bar = Bar(max_value=LenCatSec,
                  empty_color=7,
                  filled_color=18 + npi * 6,
                  title='Process-' + str(npi))
        bar.cursor.clear_lines(1)
        bar.cursor.save()

    for procedi, cataline in enumerate(CataSect, 1):

        np.random.seed()

        ident = str(cataline['IDENT'])

        objwind = csstpkg.windcut(_CssImg, cataline)

        if objwind is None:
            if DebugTF == True:
                print('--- Object window cutting error ---')
            continue
        # DataArr2Fits(objwind, ident+'_convwin.fits')
        objwinshape = objwind.shape
        objwind.data = objwind.data * ExpCssFrm

        WinImgBands = np.zeros(
            (len(cssbands), objwinshape[0],
             objwinshape[1]))  # 3-D array contains images of all the cssbands

        if IfPlotObjWin == True:
            csstpkg.PlotObjWin(objwind, cataline)

        outcatrowi = [ident, cataline['Z_BEST']]

        if DebugTF == True:
            print(' '.join([
                ident, '\nRA DEC:',
                str(cataline['RA']),
                str(cataline['DEC'])
            ]))

        # Photometry for the central object on the convolved window
        ObjWinPhot_DeBkg = csstpkg.CentrlPhot(objwind.data,
                                              id=str(outcatrowi[0]) +
                                              " ConvWdW DeBkg")
        ObjWinPhot_DeBkg.Bkg(idb=str(outcatrowi[0]) + " ConvWdW DeBkg",
                             debug=DebugTF,
                             thresh=1.5,
                             minarea=10,
                             deblend_nthresh=32,
                             deblend_cont=0.01)
        ObjWinPhot_DeBkg.Centract(idt=str(outcatrowi[0]) + " ConvWdW DeBkg",
                                  thresh=2.5,
                                  deblend_nthresh=32,
                                  deblend_cont=0.1,
                                  minarea=10,
                                  debug=DebugTF,
                                  sub_backgrd_bool=True)
        if ObjWinPhot_DeBkg.centobj is np.nan:
            if DebugTF == True:
                print('--- No central object detected in convolved image ---')
            continue
        else:
            ObjWinPhot_DeBkg.KronR(idk=str(outcatrowi[0]) + " ConvWdW",
                                   debug=DebugTF,
                                   mask_bool=True)

        NeConv_DeBkg, ErrNeConv_DeBkg = ObjWinPhot_DeBkg.EllPhot(
            ObjWinPhot_DeBkg.kronr, mask_bool=True)

        if ((NeConv_DeBkg <= 0) or (NeConv_DeBkg is np.nan)):
            if DebugTF == True:
                print(
                    'NeConv_DeBkg for a winimg <= 0 or NeConv_DeBkg is np.nan')
            continue

        noisebkg_conv = ObjWinPhot_DeBkg.bkg.background_rms_median

        if DebugTF == True:
            print('self.bkg Flux & ErrFlux =',
                  ObjWinPhot_DeBkg.bkg.background_median,
                  ObjWinPhot_DeBkg.bkg.background_rms_median)
            print('Class processed NeConv_DeBkg & ErrNeConv_DeBkg:',
                  NeConv_DeBkg, ErrNeConv_DeBkg)

        # Read model SED to NDArray
        # modsednum = cataline['MOD_BEST']
        sedname = seddir + 'Id' + '{:0>9}'.format(ident) + '.spec'
        modsed, readflag = csstpkg.readsed(sedname)
        if readflag == 1:
            modsed[:, 1] = csstpkg.mag2flam(
                modsed[:, 1], modsed[:, 0]
            )  # to convert model SED from magnitude to f_lambda(/A)
        else:
            print('model sed not found.')
            continue
        bandi = 0
        NeBands = []
        magsimorigs = []
        scalings = []

        for cssband, numb in zip(cssbands, filtnumb):

            expcss = 150. * numb  # s
            # cssbandpath = thrghdir+cssband+'.txt'

            NeABand0 = csstpkg.NeObser(modsed, cssband, expcss,
                                       TelArea)  # *cataline['SCALE_BEST']
            if NeABand0 < 1:
                continue
            magaband0 = csstpkg.Ne2MagAB(NeABand0, cssband, expcss, TelArea)
            delmag = magaband0 - float(cataline['MOD_' + cssband + '_css'])
            magsimorig_band = magaband0 - delmag
            NeABand = NeABand0 / 10**(-0.4 * delmag)

            # print(NeABand0, magaband0, delmag)

            NeBands.append(NeABand)

            if DebugTF == True:
                print(' '.join(
                    [cssband, 'band model electrons = ',
                     str(NeABand), 'e-']))
                print('MOD_' + cssband + '_css =',
                      cataline['MOD_' + cssband + '_css'])
                magsimorigs.append(
                    csstpkg.Ne2MagAB(NeABand, cssband, expcss, TelArea))
                print('Magsim_' + cssband + ' =', magsimorigs[bandi])

            Scl2Sed = NeABand / NeConv_DeBkg
            scalings.append(Scl2Sed)

            if DebugTF == True:
                print(ident, 'Scaling Factor: ', Scl2Sed)

            # ZeroLevel = config.getfloat('Hst2Css', 'BZero')
            SkyLevel = 0  #csstpkg.backsky[cssband] * expcss
            DarkLevel = 0  #config.getfloat('Hst2Css', 'BDark') * expcss
            RNCssFrm = config.getfloat('Hst2Css', 'RNCss')

            # IdealImg = objwind.data * Scl2Sed + SkyLevel + DarkLevel  # e-
            IdealImg = ObjWinPhot_DeBkg.data_bkg * Scl2Sed  # + SkyLevel + DarkLevel  # e-
            # IdealImg[IdealImg < 0] = 0

            if DebugTF == True:
                # csstpkg.DataArr2Fits(IdealImg/Gain, 'Ideal_Zero_Gain_check_'+ident+'_'+cssband+'.fits')

                # Testing photometry for the scaled convolved window's central object
                ObjWinPhot = csstpkg.CentrlPhot(IdealImg,
                                                id=(ident + " SclTesting"))
                try:
                    ObjWinPhot.Bkg(idb=ident + " SclTesting",
                                   debug=DebugTF,
                                   thresh=1.5,
                                   minarea=10,
                                   deblend_nthresh=32,
                                   deblend_cont=0.01)
                except Exception as e:
                    # print(NeConv_DeBkg, NeABand, IdealImg)
                    continue
                ObjWinPhot.Centract(idt=ident + " SclTesting",
                                    thresh=2.5,
                                    deblend_nthresh=32,
                                    deblend_cont=0.1,
                                    minarea=10,
                                    debug=DebugTF,
                                    sub_backgrd_bool=False)
                if ObjWinPhot.centobj is np.nan:
                    print(
                        '--- No central object detected in testing photometry image---'
                    )
                    continue
                else:
                    ObjWinPhot.KronR(idk=ident + " SclTesting",
                                     debug=DebugTF,
                                     mask_bool=True)

                NeConv, ErrNeConv = ObjWinPhot.EllPhot(ObjWinPhot.kronr,
                                                       mask_bool=True)

                print(' '.join([
                    'Model electrons:',
                    str(NeABand), '\nTesting Photometry After scaling:',
                    str(NeConv)
                ]))

            BkgNoiseTot = (SkyLevel + DarkLevel + RNCssFrm**2 * numb)**0.5
            if BkgNoiseTot > noisebkg_conv * Scl2Sed:
                Noise2Add = (BkgNoiseTot**2 -
                             (noisebkg_conv * Scl2Sed)**2)**0.5
            else:
                Noise2Add = 0

            if DebugTF == True:
                print('Added Noise ' + cssband + ' band: ', Noise2Add)

            # ImgPoiss = np.random.poisson(lam=IdealImg, size=objwinshape)
            ImgPoiss = IdealImg
            NoisNormImg = csstpkg.NoiseArr(objwinshape,
                                           loc=0,
                                           scale=Noise2Add,
                                           func='normal')

            # DigitizeImg = np.round((ImgPoiss + NoisNorm + ZeroLevel) / Gain)
            DigitizeImg = (ImgPoiss + NoisNormImg) / Gain
            # DigitizeImg = IdealImg/Gain

            if DebugTF == True:
                csstpkg.DataArr2Fits(
                    DigitizeImg, 'Ideal_Zero_Gain_RN_check_' + ident + '_' +
                    cssband + '.fits')

            WinImgBands[bandi, ::] = DigitizeImg

            bandi = bandi + 1

        if DebugTF == True:
            print('Stack all bands and detect objects:')

        WinImgStack = WinImgBands.sum(0)
        # print(WinImgStack.shape)
        # AduStack, ErrAduStack, ObjectStack, KronRStack, MaskStack = septract(WinImgStack, id=str(outcatrowi[0])+" Stack", debug=DebugTF, thresh=1.2, minarea=10)
        StackPhot = csstpkg.CentrlPhot(WinImgStack, id=ident + " Stack")
        StackPhot.Bkg(idb=ident + " Stack",
                      debug=DebugTF,
                      thresh=1.5,
                      minarea=10)
        StackPhot.Centract(idt=ident + " Stack",
                           thresh=1.5,
                           minarea=10,
                           deblend_nthresh=32,
                           deblend_cont=0.1,
                           debug=DebugTF)
        if StackPhot.centobj is np.nan:
            if DebugTF == True:
                print('No central object on STACK image.')
            continue
        else:
            StackPhot.KronR(idk=ident + " Stack",
                            debug=DebugTF,
                            mask_bool=True)
        AduStack, ErrAduStack = StackPhot.EllPhot(StackPhot.kronr,
                                                  mask_bool=True)
        if AduStack is np.nan:
            if DebugTF == True:
                print('RSS error for STACK image.')
            continue

        if DebugTF == True:
            csstpkg.PlotKronrs(WinImgStack, StackPhot)

        bandi = 0
        for cssband, numb in zip(cssbands, filtnumb):
            expcss = 150. * numb  # s
            if DebugTF == True:
                plt.hist(
                    WinImgBands[bandi, ::].flatten(),
                    bins=np.arange(30) - 15,
                )
                plt.title(' '.join([cssband, 'simul image']))
                plt.show()

            SameApObj = csstpkg.CentrlPhot(WinImgBands[bandi, ::],
                                           id=ident + ' ' + cssband +
                                           ' band CentralExtract')
            SameApObj.Bkg(idb=ident + ' ' + cssband + ' band CentralExtract',
                          debug=DebugTF,
                          thresh=1.5,
                          minarea=10)
            SameApObj.Centract(idt=ident + ' ' + cssband +
                               ' band CentralExtract',
                               thresh=1.2,
                               minarea=10,
                               deblend_nthresh=32,
                               deblend_cont=0.1,
                               debug=DebugTF,
                               sub_backgrd_bool=False)
            if SameApObj.centobj is np.nan:
                if DebugTF == True:
                    print('No central object on simulated image.')
                SNR = -99
                MagObser = -99
                ErrMagObs = -99
                # AduObser, ErrAduObs = csstpkg.septractSameAp(WinImgBands[bandi, ::], StackPhot, ObjWinPhot_DeBkg.centobj, ObjWinPhot_DeBkg.kronr, mask_det=StackPhot.mask_other, debug=DebugTF, annot=cssband+'_cssos', thresh=1.2, minarea=10, sub_backgrd_bool=False)
                # if AduObser>0:
                #     MagObser = -2.5 * math.log10(AduObser) + magab_zeros[bandi]
                #     ErrMagObs = -1
                #     ErrAduTot = (ErrAduObs ** 2 + (noisebkg_conv * scalings[bandi]) ** 2) ** 0.5
                #     SNR = AduObser / ErrAduTot
                # else:
                #     SNR = -99
                #     MagObser = -99
                #     ErrMagObs = -99
            else:
                # AduObser, ErrAduObs = csstpkg.septractSameAp(WinImgBands[bandi, ::], StackPhot, ObjWinPhot_DeBkg.centobj, ObjWinPhot_DeBkg.kronr, mask_det=StackPhot.mask_other, debug=DebugTF, annot=cssband+'_cssos', thresh=1.2, minarea=10, sub_backgrd_bool=False)

                AduObser, ErrAduObs = csstpkg.septractSameAp(
                    WinImgBands[bandi, ::],
                    StackPhot,
                    StackPhot.centobj,
                    StackPhot.kronr,
                    mask_det=StackPhot.mask_other,
                    debug=DebugTF,
                    annot=cssband + '_cssos',
                    thresh=1.2,
                    minarea=10,
                    sub_backgrd_bool=False)

                if AduObser > 0:
                    ErrAduTot = (ErrAduObs**2 +
                                 (noisebkg_conv * scalings[bandi])**2)**0.5
                    SNR = AduObser / ErrAduTot
                    # MagObser = Ne2MagAB(AduObser*Gain,cssband,expcss,TelArea)
                    MagObser = -2.5 * math.log10(AduObser) + magab_zeros[bandi]
                    ErrMagObs = 2.5 * math.log10(1 + 1 / SNR)
                    if DebugTF == True:
                        if ((cssband == 'r') &
                            (np.abs(MagObser -
                                    cataline['MOD_' + cssband + '_css']) > 1)):
                            csstpkg.DataArr2Fits(objwind.data,
                                                 ident + '_convwin_r.fits')
                            csstpkg.DataArr2Fits(WinImgStack,
                                                 ident + '_stack.fits')
                else:
                    SNR = -99
                    MagObser = -99
                    ErrMagObs = -99

                if DebugTF == True:
                    npixel = math.pi * (
                        ObjWinPhot_DeBkg.centobj['a'] * csstpkg.kphotpar *
                        ObjWinPhot_DeBkg.kronr) * (
                            ObjWinPhot_DeBkg.centobj['b'] * csstpkg.kphotpar *
                            ObjWinPhot_DeBkg.kronr)
                    print(' '.join([
                        cssband, 'band model e- =',
                        str(NeBands[bandi]), 'e-'
                    ]))
                    print(' '.join([
                        cssband, 'band simul e- =',
                        str(AduObser * Gain), 'e-', ' ErrNe=',
                        str(ErrAduTot * Gain)
                    ]))
                    # print(AduObser, Gain, NeBands[bandi], -2.5*math.log10(AduObser*Gain/NeBands[bandi]))
                    print('SNR =', AduObser / ErrAduTot)
                    print('Npixel =', npixel)
                    print(' '.join([
                        cssband, 'band mag_model = ',
                        str(cataline['MOD_' + cssband + '_css']), '(AB mag)'
                    ]))
                    print(' '.join([
                        cssband, 'band Magsim_orig = ',
                        str(magsimorigs[bandi]), '(AB mag)'
                    ]))
                    print(' '.join([
                        cssband, 'band Mag_simul = ',
                        str(MagObser), '(AB mag)'
                    ]))
                    print(' '.join([
                        cssband, 'band magerr_simul = ',
                        str(ErrMagObs), '(AB mag)'
                    ]))
                    print(' '.join([
                        'Magsim - Magsimorig =',
                        str(MagObser - magsimorigs[bandi])
                    ]))

            outcatrowi = outcatrowi + [
                cataline['MOD_' + cssband + '_css'], MagObser, ErrMagObs, SNR
            ]
            bandi = bandi + 1

        del WinImgBands
        colnumb = len(outcatrowi)

        OutRowStr = ('{} ' +
                     (colnumb - 1) * '{:8.3f}').format(*outcatrowi) + '\n'
        OutSecStr = OutSecStr + OutRowStr

        if IfProgBarOn == True:
            bar.cursor.restore()  # Return cursor to start
            bar.draw(value=procedi)

    if IfProgBarOn == True:
        bar.cursor.restore()  # Return cursor to start
        bar.draw(value=bar.max_value)  # Draw the bar!

    # OutCatSecQueue.put(OutSecStr)
    # _FinishQueue.put(1)

    # write_lock.acquire()
    with write_lock:
        OutCssCat.write(OutSecStr)
        OutCssCat.flush()
    # write_lock.release()

    print('\n')
        ConvKernelNormal = csstpkg.ImgConvKnl(config.getfloat(
            'Hst2Css', 'FwhmHst'),
                                              FwhmCssz,
                                              HstPS / nzoomin,
                                              widthinfwhm=4)
        ConvHst2Css = csstpkg.ImgConv(HstImgArr,
                                      ConvKernelNormal.image,
                                      NDivide=ndivide,
                                      NZoomIn=nzoomin,
                                      NZoomOut=nzoomout)

        CssHdr = csstpkg.CRValTrans(HstHdr, HstPS, CssPS)

        ConvHst2Css32 = np.array(ConvHst2Css, dtype='float32')
        del ConvHst2Css, ConvKernelNormal
        csstpkg.DataArr2Fits(ConvHst2Css32, HstAsCssFile, headerobj=CssHdr)
        # csstpkg.DataArr2Fits(ConvHst2Css32[0:int(CssHei/8),0:int(CssWid/8)], HstAsCssFileTest, headerobj=CssHdr)
        del ConvHst2Css32, CssHdr

    CssHdu = fits.open(HstAsCssFile)
    CssCat = ascii.read(config['Hst2Css']['CssCatIn'])
    CssImg = CssHdu[0].data
    CssHdr = CssHdu[0].header
    CssHei, CssWid = CssImg.shape

    w = wcs.WCS(CssHdr)
    pixcorner = np.array([[0, 0], [CssHei, 0], [CssHei, CssWid], [0, CssWid]])
    worldcorner = w.wcs_pix2world(pixcorner, 1)
    RaMin = min(worldcorner[:, 0])
    RaMax = max(worldcorner[:, 0])
    DecMin = min(worldcorner[:, 1])