Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
rhoxu committed Jul 11, 2024
1 parent 8570ac9 commit 02c06fc
Show file tree
Hide file tree
Showing 11 changed files with 265 additions and 443 deletions.
133 changes: 133 additions & 0 deletions examples/OfflineModeExample.ipynb

Large diffs are not rendered by default.

116 changes: 0 additions & 116 deletions examples/Offline_example.ipynb

This file was deleted.

124 changes: 83 additions & 41 deletions tessreduce/cat_mask.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from astropy.coordinates import SkyCoord
from astropy import units as u
from astropy.io import fits
from astropy.nddata import Cutout2D
from astropy.wcs import WCS
from scipy.signal import fftconvolve
import os

Expand All @@ -19,14 +13,17 @@
warnings.filterwarnings("ignore", category=RuntimeWarning)

def size_limit(x,y,image):
"""
Find indices where pixels are inside size of image.
"""
yy,xx = image.shape
ind = ((y > 0) & (y < yy-1) & (x > 0) & (x < xx-1))
return ind


def circle_app(rad):
"""
makes a kinda circular aperture, probably not worth using.
Makes a kinda circular aperture, probably not worth using.
"""
mask = np.zeros((int(rad*2+.5)+1,int(rad*2+.5)+1))
c = rad
Expand All @@ -40,7 +37,7 @@ def circle_app(rad):

def ps1_auto_mask(table,Image,scale=1):
"""
Make a source mask using the PS1 catalog
Make a source mask using the PS1 catalog.
"""
image = np.zeros_like(Image)
x = table.x.values
Expand All @@ -58,11 +55,11 @@ def ps1_auto_mask(table,Image,scale=1):

mags = [[18,17],[17,16],[16,15],[15,14],[14,13.5],[13.5,12]]
size = (np.array([3,4,5,6,7,8]) * scale).astype(int)
for i in range(len(mags)):
m = ((magim > mags[i][1]) & (magim <= mags[i][0])) * 1.
for i, mag in enumerate(mags):
m = ((magim > mag[1]) & (magim <= mag[0])) * 1.
k = np.ones((size[i],size[i]))
conv = fftconvolve(m, k,mode='same')#.astype(int)
masks[str(mags[i][0])] = (conv >.1) * 1.
masks[str(mag[0])] = (conv >.1) * 1.
masks['all'] = np.zeros_like(image,dtype=float)
for key in masks:
masks['all'] += masks[key]
Expand All @@ -71,8 +68,26 @@ def ps1_auto_mask(table,Image,scale=1):

def gaia_auto_mask(table,Image,scale=1):
"""
Make a source mask from gaia source catalogue
Make a source mask from gaia source catalogue.
Parameters
----------
table : Dataframe like
Table of sources from gaia.
Image : ArrayLike
Flux of a reference image.
scale : int, optional
Scale factor for size of kernel mask. The default is 1.
Returns
-------
masks : dict
Source mask for gaia sources.
"""

image = np.zeros_like(Image)
x = table.x.values
y = table.y.values
Expand All @@ -82,19 +97,18 @@ def gaia_auto_mask(table,Image,scale=1):
ind = size_limit(x,y,image)
x = x[ind]; y = y[ind]; m = m[ind]

maglim = np.zeros_like(image,dtype=float)
magim = image.copy()
magim[y,x] = m

masks = {}

mags = [[18,17],[17,16],[16,15],[15,14],[14,13.5],[13.5,12],[12,10],[10,9],[9,8],[8,7]]
size = (np.array([3,4,5,6,7,8,10,14,16,18])*scale).astype(int)
for i in range(len(mags)):
m = ((magim > mags[i][1]) & (magim <= mags[i][0])) * 1.
for i, mag in enumerate(mags):
m = ((magim > mag[1]) & (magim <= mag[0])) * 1.
k = np.ones((size[i],size[i]))
conv = fftconvolve(m, k,mode='same')#.astype(int)
masks[str(mags[i][0])] = (conv >.1) * 1.
masks[str(mag[0])] = (conv >.1) * 1.
masks['all'] = np.zeros_like(image,dtype=float)
for key in masks:
masks['all'] += masks[key]
Expand All @@ -105,7 +119,25 @@ def Big_sat(table,Image,scale=1):
"""
Make crude cross masks for the TESS saturated sources.
The properties in the mask need some fine tuning.
Parameters
----------
table : Dataframe like
Table of sources from gaia.
Image : ArrayLike
Flux of a reference image.
scale : int, optional
Scale factor for size of kernel mask. The default is 1.
Returns
-------
masks : array
Source mask for saturated sources.
"""

image = np.zeros_like(Image)
i = (table.mag.values < 7) #& (gaia.gaia.values > 2)
sat = table.iloc[i]
Expand All @@ -118,7 +150,6 @@ def Big_sat(table,Image,scale=1):

x = x[ind]; y = y[ind]; m = m[ind]


satmasks = []
for i in range(len(x)):
mag = m[i]
Expand Down Expand Up @@ -165,42 +196,57 @@ def Big_sat(table,Image,scale=1):
return satmasks

def Strap_mask(Image,col,size=4):
"""
Make a mask for the electrical straps on TESS CCDs.
Parameters
----------
Image : ArrayLike
Flux of a reference image.
col : int
Reference index of TPF columns.
size : int, optional
Width of the strap in pixels. The default is 4.
"""

strap_mask = np.zeros_like(Image)
straps = pd.read_csv(package_directory + 'tess_straps.csv')['Column'].values - col + 44
strap_in_tpf = straps[((straps > 0) & (straps < Image.shape[1]))]
strap_mask[:,strap_in_tpf] = 1
big_strap = fftconvolve(strap_mask,np.ones((size,size)),mode='same') > .5
return big_strap

def Cat_mask(tpf,catalogue_path=None,maglim=19,scale=1,strapsize=3,badpix=None,ref=None,sigma=3):
def Cat_mask(tpf,catalogue_path=None,maglim=19,scale=1,strapsize=3,ref=None,sigma=3):

"""
Make a source mask from the PS1 and Gaia catalogs.
------
Inputs
------
Parameters
----------
tpf : lightkurve target pixel file
tpf of the desired region
maglim : float
magnitude limit in PS1 i band and Gaia G band for sources.
scale : float
scale factor for default mask size
strapsize : int
size of the mask for TESS straps
badpix : str
not implemented correctly, so just ignore!
catalogue_path : str, optional
Local path to source catalogue if using TESSreduce in offline mode. The default is None.
maglim : float, optional
Magnitude limit in PS1 i band and Gaia G band for sources to include in source mask. The default is 19.
scale : float, optional
Scale factor for default mask size. The default is 1.
strapsize : float, optional
Width in pixels of the mask for TESS' electrical straps. The default is 6.
-------
Returns
-------
total mask : bitmask
a bitwise mask for the given tpf. Bits are as follows:
0 - background
1 - catalogue source
2 - saturated source
4 - strap mask
8 - bad pixel (not used)
0 - background
1 - catalogue source
2 - saturated source
4 - strap mask
8 - bad pixel (not used)
gaia : DataframeLike
Gaia catalogue of sources included in mask.
"""

if catalogue_path is not None:
Expand Down Expand Up @@ -229,16 +275,12 @@ def Cat_mask(tpf,catalogue_path=None,maglim=19,scale=1,strapsize=3,badpix=None,r


sat = (np.nansum(sat,axis=0) > 0).astype(int) * 2 # assign 2 bit
#mask = ((mg['all']+mp['all']) > 0).astype(int) * 1 # assign 1 bit

if strapsize > 0:
strap = Strap_mask(image,tpf.column,strapsize).astype(int) * 4 # assign 4 bit
else:
strap = np.zeros_like(image,dtype=int)
if badpix is not None:
bp = cat_mask.Make_bad_pixel_mask(badpix, file)
totalmask = mask | sat | strap | bp
else:
totalmask = mask | sat | strap

totalmask = mask | sat | strap

return totalmask, gaia
25 changes: 0 additions & 25 deletions tessreduce/catalog_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
import os
import matplotlib.pyplot as plt
import lightkurve as lk
from scipy import interpolate
from astropy.io import fits
from astropy.wcs import WCS
from astropy.coordinates import SkyCoord
from astropy import units as u
from astropy.coordinates import SkyCoord, Angle
from copy import deepcopy
import pandas as pd
Expand Down Expand Up @@ -468,29 +466,6 @@ def Reformat_df(df):

return new_df

# def external_save_cat(radec,size,cutCornerPx,image_path,save_path,maglim):

# file = Extract_fits(image_path)
# wcsItem = WCS(file[1].header)
# file.close()

# ra = radec[0]
# dec = radec[1]

# # gp,gm, source = Get_Gaia_External(ra,dec,cutCornerPx,size,wcsItem,magnitude_limit=maglim)
# gp,gm, source = Get_Gaia_External(ra,dec,cutCornerPx,size,wcsItem,magnitude_limit=maglim)
# gaia = pd.DataFrame(np.array([gp[:,0],gp[:,1],gm,source]).T,columns=['ra','dec','mag','Source'])

# gaia.to_csv(f'{save_path}/local_gaia_cat.csv',index=False)

# def external_save_cat(tpf,save_path,maglim):

# tpf = lk.TessTargetPixelFile(tpf)
# gp,gm, source = Get_Gaia_External(tpf,magnitude_limit=maglim)
# gaia = pd.DataFrame(np.array([gp[:,0],gp[:,1],gm,source]).T,columns=['ra','dec','mag','Source'])

# gaia.to_csv(f'{save_path}/local_gaia_cat.csv',index=False)

def external_save_cat(tpf,save_path,maglim):

tpfFits = fits.open(tpf)
Expand Down
Loading

0 comments on commit 02c06fc

Please sign in to comment.