Esempio n. 1
0
def main():

    # Read file to fit
    #filename = 'nexcess_cube.fits.gz'
    filename = 'non_cube_convolved.fits.gz'
    cube = SkyCube.read(filename)

    # Read configuration
    config = read_config('config.yaml')
    binsz = config['binning']['binsz']
    offset_fov = config['selection']['offset_fov']

    # Take PSF data
    irffile = 'irf_file.fits'
    psf_table = psf_fromfits(irffile)

    energarr = cube.energies('edges')
    sigmas = psf_table[3]
    norms = psf_table[4]

    hdu = pyfits.open(filename)

    im_sizex = hdu[0].header['NAXIS1']
    im_sizey = hdu[0].header['NAXIS2']

    cx = 0.5 * im_sizex
    cy = 0.5 * im_sizey

    # Check the significance
    filename_on = 'non_cube.fits.gz'

    cube_on = SkyCube.read(filename_on)

    filename_off = 'noff_cube.fits.gz'

    cube_off = SkyCube.read(filename_off)
    alpha_obs = 1.

    on_pos = SkyCoord(83.6333 * u.deg, 22.0144 * u.deg, frame='icrs')
    on_sizes = np.ones(len(
        cube.energies('center'))) * 120 * binsz * u.deg  #0.167

    off_pos = SkyCoord(83.6333 * u.deg, 22.0144 * u.deg, frame='icrs')
    off_sizes = on_sizes * alpha_obs

    on_data = Table()
    off_data = Table()
    on_data['value'] = np.zeros(len(on_sizes))
    off_data['value'] = np.zeros(len(on_sizes))
    for idx in range(len(cube.energies('center'))):

        on_region = CircleSkyRegion(on_pos, on_sizes[idx])
        off_region = CircleSkyRegion(off_pos, off_sizes[idx])

        on_data['value'][idx] = cube_on.spectrum(on_region)['value'][idx]
        off_data['value'][idx] = cube_off.spectrum(off_region)['value'][idx]

        limasig = np.sqrt(
            2) * np.sqrt(on_data['value'][idx] * np.log(
                ((1 + alpha_obs) / alpha_obs) * on_data['value'][idx] /
                (on_data['value'][idx] + off_data['value'][idx])) +
                         off_data['value'][idx] * np.log(
                             (1 + alpha_obs) * off_data['value'][idx] /
                             (on_data['value'][idx] + off_data['value'][idx])))

        print(limasig, 'Energy range: ',
              cube_on.energies('edges')[idx], ' - ',
              cube_on.energies('edges')[idx + 1])

        #Fit only if data is enough
        #and on_data['value'][i] - off_data['value'][i] >= 0.01 * off_data['value'][i]
        if limasig >= 3 and on_data['value'][idx] - off_data['value'][idx] >= 7:

            # Make image cube from slice excess convolved cube
            cube_sum = np.zeros(
                (cube.data.shape[1], cube.data.shape[2])) * u.ct
            cube_sum = np.add(cube_sum, cube.data[idx])

            cube_sum.value[np.isnan(cube_sum.value)] = 0
            cube_sum.value[cube_sum.value < 0] = abs(
                cube_sum.value[cube_sum.value < 0])

            image_sum = SkyCube.empty_like(cube)
            image_sum.data = cube_sum

            image_sum.write('sum_image.fits.gz', overwrite=True)

            # Find nearest energy and theta value
            i = np.argmin(np.abs(energarr[idx].value -
                                 psf_table[0].value))  ######
            j = np.argmin(np.abs(offset_fov - psf_table[2].value))

            # Make PSF
            #psfname="mypsf"
            #load_user_model(PSFGauss,psfname)
            s1 = sigmas[0][j][i] / binsz
            s2 = sigmas[1][j][i] / binsz
            s3 = sigmas[2][j][i] / binsz
            print(sigmas[0][j][i], sigmas[1][j][i], sigmas[2][j][i])
            ampl = norms[0][j][i]
            ampl2 = norms[1][j][i]
            ampl3 = norms[2][j][i]

            t0 = time()

            #Morphological fitting
            load_image("sum_image.fits.gz")
            #image_data()

            #set_coord("physical")

            set_method("simplex")
            set_stat("cash")

            # Position and radius
            x0 = 125
            y0 = 125
            rad0 = 80.0

            image_getregion(coord="physical")
            'circle(x0,y0,rad0);'

            notice2d("circle(" + str(x0) + "," + str(y0) + "," + str(rad0) +
                     ")")

            load_user_model(GaussianSource, "sph2d")
            add_user_pars("sph2d", [
                "sigma1", "sigma2", "sigma3", "alpha", "beta", "ampl", "size",
                "xpos", "ypos"
            ])

            set_model(sph2d + const2d.bgnd)

            # Constant PSF
            #gpsf.fwhm = 4.2
            #gpsf.xpos = x0
            #gpsf.ypos = y0
            #gpsf.ellip = 0.2
            #gpsf.theta = 30 * np.pi / 180

            #### Set PSF
            set_par(sph2d.sigma1, val=s1, frozen=True)
            set_par(sph2d.sigma2, val=0, frozen=True)
            set_par(sph2d.sigma3, val=0, frozen=True)
            set_par(sph2d.alpha, val=0, frozen=True)
            set_par(sph2d.beta, val=0, frozen=True)

            # HESS PSF
            #set_par(sph2d.sigma1, val = 0.025369, frozen = True)
            #set_par(sph2d.alpha, val = 0.691225, frozen = True)
            #set_par(sph2d.sigma2, val = 0.0535014, frozen = True)
            #set_par(sph2d.beta, val = 0.13577, frozen = True)
            #set_par(sph2d.sigma3, val = 0.11505, frozen = True)

            set_par(sph2d.xpos, val=x0, frozen=True)
            set_par(sph2d.ypos, val=y0, frozen=True)

            set_par(sph2d.ampl, val=10000, min=1e-11, max=100000000)
            set_par(sph2d.size, val=10, min=1e-11, max=100)

            set_par(bgnd.c0, val=1, min=0, max=100)

            show_model()
            fit()
            #do_fit()
            conf()
            #do_conf()
            #image_fit()
            #save_model("model_" + str(idx) + ".fits")
            #save_resid("resid_" + str(idx) + ".fits")

            t1 = time()
            print('Simul time', t1 - t0)
Esempio n. 2
0
source_model_G0p9.ampl = 1.0

model = bkg + 1E-11 * (source_model_SgrA) + 1E-11 * (source_model_SgrA)
fit = Fit(data=cube,
          model=model,
          stat=Cash(),
          method=NelderMead(),
          estmethod=Covariance())
result = fit.fit()
err = fit.est_errors()
print(err)

# Set starting values Gauss*CS
spatial_model_central_gauss = NormGauss2DInt('spatial-model_central_Gauss_CS')
spectral_model_central_gauss = PowLaw1D('spectral-model_Gauss_CS')
CS_cube = SkyCube.empty_like(exposure_3D)
CS_map = SkyImage.read("CStot.fits")
if 'COMMENT' in CS_map.meta:
    del CS_map.meta['COMMENT']
cs_reproj = (CS_map.reproject(CS_cube.sky_image_ref)).cutout(
    center, extraction_size)
cs_reproj.data[np.where(np.isnan(cs_reproj.data))] = 0
cs_reproj.data[np.where(
    cs_reproj.data < input_param["param_fit_3D"]["CS"]["threshold_map"])] = 0
cs_reproj.data = cs_reproj.data / cs_reproj.data.sum()
for i in range(len(energy_bins_true)):
    CS_cube[iE, :, :] = cs_reproj.data
CS = TableModel('CS')
CS.load(None, CS_cube.data[index_region_selected_3d].value.ravel())
CS.ampl = input_param["param_fit_3D"]["CS"]["ampl_init"]