Exemplo n.º 1
0
def search_local_max(image, radius=120, view=False, **kwargs):
    """
    Use 'find_loc_max()' to search all the maxs. The edges position with a lot of zeros would be excluded.
    
    Parameter
    --------
        image: 
            2D array type image.
        
        radius: 
            A radius used to test if any empty pixels around.
        
    Return
    --------
        A list of positions of 'PSF'
    """
    from galight.tools.cutout_tools import cutout
    PSFx, PSFy = find_loc_max(image, **kwargs)
    PSF_locs = []
    ct = 0
    for i in range(len(PSFx)):
        cut_img = cutout(image, [PSFx[i], PSFy[i]], radius=radius)
        cut_img[np.isnan(cut_img)] = 0
        if np.sum(cut_img == 0) > len(cut_img)**2 / 20:
            continue
        PSF_locs.append([PSFx[i], PSFy[i]])
        if view == True:
            print("plot for position: [{0}, {1}]".format(PSFx[i], PSFy[i]),
                  "idx:", ct)
            print("total flux:", cut_img.sum())
            print("measure FWHM:", np.round(measure_FWHM(cut_img), 3))
            plt_fits(cut_img)
            print("================")
            ct += 1
    return PSF_locs
Exemplo n.º 2
0
 def abs_res(self, rotate_pix, if_plot=False):
     cal_areas, _, punish = self.segm_to_mask(rotate_pix)
     res_ = self.residual(rotate_pix)
     if if_plot == True:
         plt_fits(abs(res_ * cal_areas), norm='log')
     if punish == False:
         return np.sum(abs(res_ * cal_areas))
     else:
         return 10**6
Exemplo n.º 3
0
data_sb = data_sb[:2000, :2000]
data = data_sb / header['PHOTMJSR']
value_unit = header['BUNIT']
print('For flux value in unit of MJy/sr, the zp is 31.4.'
      )  #https://en.wikipedia.org/wiki/Jansky#AB_magnitude

print(
    "Conversion factor from {units} to DN/S for filter {f}:".format(
        units=header['BUNIT'], f=filt), header['PHOTMJSR'])

header0 = im[0].header
img_filter = header0['FILTER']
img_cam = header0['APERNAME']  #In JDAT'simulation it is 'DETECTOR'
exptime = header0['TEXPTIME']  #The assumed exp time.
from galight.tools.astro_tools import plt_fits
plt_fits(data)

#%% Model using galight
from galight.data_process import DataProcess
zp = 31.4 - 2.5 * np.log10(
    header['PHOTMJSR']
)  #Calculate the correspondingly zp as DN/S #This is wrong! See 3_...
data_process = DataProcess(fov_image=data,
                           target_pos=[1170., 940.],
                           pos_type='pixel',
                           header=header,
                           rm_bkglight=False,
                           exptime=np.ones_like(data) * exptime,
                           if_plot=False,
                           zp=zp)  #Gain value assuming as 1
data_process.generate_target_materials(radius=65,
Exemplo n.º 4
0
    # fit_run.plot_final_qso_fit()
    # fit_run.dump_result()
    # print(fit_run.final_result_galaxy[0])

#%% Co-add residual
import glob
import pickle
from galight.tools.astro_tools import plt_fits
tot_residual = None
for i in range(len(info)-1):
    ID, filename, _ = info[i]
    picklename = 'first_pkl_files/'+ID+'_'+str(i)+'.pkl'
    picklename = glob.glob(picklename)
    if picklename  != []:
        fitting_run_class = pickle.load(open(picklename[0],'rb'))
        data = fitting_run_class.fitting_specify_class.kwargs_data['image_data']
        psf = fitting_run_class.image_ps_list[0]
        nearby = fitting_run_class.image_host_list[1:]
        host_resi = data - psf
        # if i == 10:
        #     fitting_run_class.plot_final_qso_fit()
        for j in range(len(nearby)):
            host_resi = host_resi - nearby[j]
        # plt_fits(host_resi)
        if tot_residual is None:
            tot_residual = host_resi
        else:
            tot_residual += host_resi

plt_fits(tot_residual, colorbar = True)
Exemplo n.º 5
0
header = im[1].header

# print('For flux value in unit of MJy/sr.') #https://en.wikipedia.org/wiki/AB_magnitude
value_unit = header['BUNIT']
# flux(Mjy/sr) * 2.350443 * 10**(-5) *0.03**2   #Flux to Jy  https://irsa.ipac.caltech.edu/data/SPITZER/docs/spitzermission/missionoverview/spitzertelescopehandbook/18/
print("Data unit:", value_unit)
# data_sb = data_sb[2000:4000,2000:4000]
data_sb = data_sb[100:2100, 100:2100]
zp = -2.5 * np.log10(2.350443 * 10**(-5) * 0.03**2 / 3631)

header0 = im[0].header
img_filter = header0['FILTER']
img_cam = header0['APERNAME']  #In JDAT'simulation it is 'DETECTOR'
exptime = header0['TEXPTIME']  #The assumed exp time.
from galight.tools.astro_tools import plt_fits
plt_fits(data_sb)
#%%Obtain PSF stars:
from galight.data_process import DataProcess
# zp = 31.4 - 2.5*np.log10(header['PHOTMJSR'])  #Calculate the correspondingly zp as DN/S #This is wrong! See 3_...
data_process_ = DataProcess(fov_image=data_sb,
                            target_pos=[1170., 940.],
                            pos_type='pixel',
                            header=header,
                            rm_bkglight=True,
                            exptime=np.ones_like(data_sb) * exptime,
                            if_plot=False,
                            zp=zp)  #Gain value assuming as 1
# data_process_.generate_target_materials(radius=65, create_mask = False, nsigma=2.8, if_select_obj=False,
#                                       exp_sz= 1.2, npixels = 15, if_plot=True)
data_process_.find_PSF(radius=60, user_option=True, psf_edge=10)
data_process_.plot_overview(label='Example', target_label=None)
Exemplo n.º 6
0
#%%
import pickle
from galight.tools.asymmetry_tools import Measure_asy
from galight.tools.astro_tools import plt_fits

fit_run_pkl = fit_run
obj_id = 0
asy_class = Measure_asy(
    fit_run_pkl,
    seg_cal_reg='or',
    obj_id=obj_id,  #consider_petrosian=True, 
    extend=1.5,
    eta=0.2)

plt_fits(asy_class.img, colorbar=True)

asy_class.asy_segm(mask_type='segm')
# asy_class.asy_segm(mask_type='aper')  #!!!
pos = asy_class.find_pos()
print(pos["x"])
asy = asy_class.cal_asymmetry(
    rotate_pix=pos["x"],
    if_remeasure_bkg=True,  #!!!
    if_plot=True,
    if_plot_bkg=True)

from galight.tools.asymmetry_tools import cal_r_petrosian
r_p = cal_r_petrosian(asy_class.img, center=pos['x'], if_plot=True)

# plt.imshow(asy_class._segm, origin='lower')
Exemplo n.º 7
0
# data_sb1 = copy.deepcopy(data[pos[0]:pos[0]+4400,pos[0]:pos[0]+4400])
# data_sb2 = copy.deepcopy(data[:,5800:])
# plt_fits(data_sb1)
#%%
from galight.tools.measure_tools import detect_obj
from photutils.segmentation import SourceCatalog
from galight.data_process import DataProcess
from galight.tools.cutout_tools import psf_clean
psfs, FWHMs = [], []
for data_sb in data_sbs:
    zp = -2.5 * np.log10(2.350443 * 10**(-5) * 0.01499**2 / 3631)
    header0 = im[0].header
    img_filter = header0['FILTER']
    img_cam = header0['APERNAME']  #In JDAT'simulation it is 'DETECTOR'
    exptime = header0['TEXPTIME']  #The assumed exp time.
    plt_fits(data_sb)
    # Obtain PSF stars:
    # zp = 31.4 - 2.5*np.log10(header['PHOTMJSR'])  #Calculate the correspondingly zp as DN/S #This is wrong! See 3_...
    data_process_ = DataProcess(fov_image=data_sb,
                                target_pos=[1170., 940.],
                                pos_type='pixel',
                                header=header,
                                rm_bkglight=True,
                                exptime=np.ones_like(data_sb) * exptime,
                                if_plot=False,
                                zp=zp)  #Gain value assuming as 1
    # data_process_.generate_target_materials(radius=65, create_mask = False, nsigma=2.8, if_select_obj=False,
    #                                       exp_sz= 1.2, npixels = 15, if_plot=True)
    data_process_.find_PSF(radius=60,
                           user_option=True,
                           psf_edge=150,
Exemplo n.º 8
0
        plt.show()
        # return asy, obj_flux, bkg_asy
        print(np.sum(obj_masks), np.sum(cal_areas))
        return asy / obj_flux - bkg_asy / np.sum(obj_masks) * np.sum(
            cal_areas) / obj_flux
        return asy, obj_flux, bkg_asy, np.sum(obj_masks), np.sum(cal_areas)


# Q:
# Which mask to use as total flux of object? The obj_mask + obj_mask_?
# When calculate residual image, which object total flux should be used?
# How to norm the bkg asy?

# %%
image = pyfits.open('./test_img.fits')[0].data.copy()
plt_fits(image)
pos = [0, 5.2]
image_shift = shift_img(image, pos, order=2)
# plt_fits(image_shift)
image_rotate = rotate_image(image, pos, order=2)
# plt_fits(image_rotate)
from galight.tools.measure_tools import detect_obj
apertures, segm_deblend = detect_obj(image,
                                     if_plot=True,
                                     segm_map=True,
                                     nsigma=1,
                                     auto_sort_center=True)
# plt.imshow(segm_mask_rot, origin='lower')
# plt.show()
img_class = Test_asy(img=image,
                     segm=segm_deblend,
Exemplo n.º 9
0
    # apertures = [copy.deepcopy(apertures[0])] + apertures
    # apertures[0].a, apertures[0].b = apertures[0].a*0.2,  apertures[0].b*0.2
    # data_process.apertures = apertures
    from galight.fitting_specify import FittingSpecify
    fit_sepc = FittingSpecify(data_process)
    fit_sepc.prepare_fitting_seq(
        point_source_num=0)  #, fix_n_list= [[0,4],[1,1]])
    fit_sepc.build_fitting_seq()
    fit_sepc.plot_fitting_sets(
    )  #The apertures shows how the images will be modelled.
    from galight.fitting_process import FittingProcess
    fit_run = FittingProcess(fit_sepc, savename=savename, fitting_level='deep')
    fit_run.run(algorithm_list=['PSO'], setting_list=[None])
    fit_run.dump_result()
else:
    fit_run = pickle.load(open(glob.glob(savename + '*.pkl')[0], 'rb'))
    fit_run.fitting_specify_class.plot_fitting_sets()

file_header = copy.deepcopy(fitsFile[1].header)
file_header['CRPIX1'] = file_header['CRPIX1'] - data_process.target_pos[
    0] + len(data_process.target_stamp) / 2
file_header['CRPIX2'] = file_header['CRPIX2'] - data_process.target_pos[
    1] + len(data_process.target_stamp) / 2

fit_run.plot_all()
# fit_run.plot_final_galaxy_fit()
res = (fit_run.flux_2d_out['data'] - fit_run.flux_2d_out['model'])  #* masks
plt_fits(res)
pyfits.PrimaryHDU(res).writeto(rebin_name + '_data-model' + '.fits',
                               overwrite=True)
Exemplo n.º 10
0
        PSF_loc = [35.29689501, -4.685896878]
        data_process.find_PSF(
            radius=20,
            PSF_pos_list=[PSF_loc],
            pos_type='wcs',
        )

        #Generate the fitting materials
        data_process.generate_target_materials(radius=30,
                                               create_mask=False,
                                               nsigma=2.8,
                                               exp_sz=1.5,
                                               npixels=40,
                                               if_plot=False)

        plt_fits(data_process.PSF_list[0])
        # data_process.apertures = [] #Assuming there is no host (i.e., as constant.) #!!!

        # Manually input the PSF:
        # data_process.PSF_list = [PSF]

        # Check if all the materials is given, if so to pass to the next step.
        data_process.checkout()  #Check if all the materials is known.
        fit_sepc = FittingSpecify(data_process)
        # fit_sepc.prepare_fitting_seq(point_source_num = 2)
        fit_sepc.prepare_fitting_seq(point_source_num=2,
                                     ps_pix_center_list=[[-2.0, -1.0],
                                                         [6.0, -1.0]])
        # if fit_sepc.kwargs_params['point_source_model'][0][0] == fit_sepc.kwargs_params['point_source_model'][0][1]:
        #     fit_sepc.prepare_fitting_seq(point_source_num = 2, ps_pix_center_list= [[-2.0, -1.0], [6.0, -1.0]])
        #     print(file)