def get_fficutout(c_obj, cutoutdir=None, sector=None): # c_obj (SkyCoord): location of target star print('beginning download tesscut for {}'.format(repr(c_obj))) try: tab = Tesscut.download_cutouts(c_obj, size=20, sector=sector, path=cutoutdir) except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError) as e: print('got {}, try again'.format(repr(e))) tab = Tesscut.download_cutouts(c_obj, size=20, sector=sector, path=cutoutdir)
def locate_with_tesscut(self): """Finds the best TESS postcard(s) and the position of the source on postcard. Sets attributes postcard, position_on_postcard, all_postcards. sector, camera, chip, position_on_chip. """ self.postcard = [] self.position_on_postcard = [] self.all_postcards = [] self.tc = True coord = SkyCoord(self.coords[0], self.coords[1], unit="deg") sector_table = Tesscut.get_sectors(coord) manifest = Tesscut.download_cutouts(coord, 31, sector = self.usr_sec) self.sector = self.usr_sec self.camera = sector_table[sector_table['sector'] == self.sector]['camera'].quantity[0] self.chip = sector_table[sector_table['sector'] == self.sector]['ccd'].quantity[0] whichtc = np.where(sector_table['sector'] == self.usr_sec)[0][0] cutout = fits.open(manifest['Local Path'][0]) self.cutout = cutout xcoord = cutout[1].header['1CRV4P'] ycoord = cutout[1].header['2CRV4P'] self.position_on_chip = np.array([xcoord, ycoord])
def download_tess_cuts( ticid, lower_sector_limit=0, upper_sector_limit=1000, tesscut_path="/Users/rangus/projects/TESS-rotation/data/TESScut"): # Download light curves sectors, star = get_sectors(ticid, lower_sector_limit=lower_sector_limit, upper_sector_limit=upper_sector_limit) path_to_tesscut = "{0}/astrocut_{1:12}_{2:13}_{3}x{4}px".format( tesscut_path, star.coords[0], star.coords[1], 68, 68) for sector in sectors: print("sector = ", int(sector)) star = eleanor.Source(tic=ticid, sector=int(sector), tc=True) fits_only = "tess-s{0}-{1}-{2}_{3:.6f}_{4:.6f}_{5}x{6}_astrocut.fits" \ .format(str(int(sector)).zfill(4), star.camera, star.chip, star.coords[0], star.coords[1], 68, 68) full_path = os.path.join(path_to_tesscut, fits_only) if not os.path.exists(full_path): print("No cached file found. Downloading", full_path) if not os.path.exists(path_to_tesscut): os.mkdir(path_to_tesscut) print("Downloading sector", sector, "for TIC", ticid) hdulist = Tesscut.download_cutouts( objectname="TIC {}".format(ticid), sector=sector, size=68, path=path_to_tesscut) else: print("Found cached file ", full_path)
def set_quality_flags(sector=np.arange(1, 14, 1)): """ Uses the quality flags in a 2-minute target to create quality flags in the postcards. We create our own quality flag as well, using our pointing model. """ coord = SkyCoord('04:35:50.330 -64:01:37.33', unit=(u.hourangle, u.deg)) sector_table = Tesscut.get_sectors(coord) manifest = Tesscut.download_cutouts(coordinates=coord, size=31, sector=sector) cutout = fits.open(manifest['Local Path'][0]) ffi_time = cutout[1].data['TIME'] - cutout[1].data['TIMECORR'] shortCad_fn = 'eleanor/metadata/s{0:04d}/target_s{0:04d}.fits'.format( sector) # Binary string for values which apply to the FFIs ffi_apply = int('100010101111', 2) # Obtains information for 2-minute target twoMin = fits.open(shortCad_fn) twoMinTime = twoMin[1].data['TIME'] - twoMin[1].data['TIMECORR'] finite = np.isfinite(twoMinTime) twoMinQual = twoMin[1].data['QUALITY'] twoMinTime = twoMinTime[finite] twoMinQual = twoMinQual[finite] convolve_ffi = [] nodata = np.zeros_like(ffi_time) for i in range(len(ffi_time)): where = np.where( np.abs(ffi_time[i] - twoMinTime) == np.min(np.abs(ffi_time[i] - twoMinTime)))[0][0] sflux = np.sum(cutout[1].data['FLUX'][i]) if sflux == 0: nodata[i] = 4096 if (ffi_time[i] > 1420) and (ffi_time[i] < 1424): nodata[i] = 4096 v = np.bitwise_or.reduce(twoMinQual[where - 7:where + 8]) convolve_ffi.append(v) convolve_ffi = np.array(convolve_ffi) flags = np.bitwise_and(convolve_ffi, ffi_apply) np.savetxt('eleanor/metadata/s{0:04d}/quality_s{0:04d}.txt'.format(sector), flags + nodata, fmt='%i') return flags
def __init__(self, sector=None): if sector is None: print('Please pass a sector into eleanor.Update()!') return self.sector = sector try: os.mkdir(eleanorpath + '/metadata/s{:04d}'.format(sector)) success = 1 except FileExistsError: print('Sector {:d} metadata directory exists already!'.format( sector)) success = 0 if success == 1: tic_north_cvz = 198237770 tic_south_cvz = 38846515 if self.sector < 13.5: self.tic = tic_south_cvz elif self.sector < 26.5: self.tic = tic_north_cvz else: self.tic = tic_south_cvz if self.tic == 198237770: coord = SkyCoord('16:35:50.667 +63:54:39.87', unit=(u.hourangle, u.deg)) elif self.tic == 38846515: coord = SkyCoord('04:35:50.330 -64:01:37.33', unit=(u.hourangle, u.deg)) sector_table = Tesscut.get_sectors(coord) manifest = Tesscut.download_cutouts(coord, 31, sector=self.sector) self.cutout = fits.open(manifest['Local Path'][0]) print( 'This is the first light curve you have made for this sector. Getting eleanor metadata products for Sector {0:2d}...' .format(self.sector)) print( 'This will only take a minute, and only needs to be done once. Any other light curves you make in this sector will be faster.' ) self.get_target() print('Target Acquired') self.get_cadences() print('Cadences Calculated') self.get_quality() print('Quality Flags Assured') self.get_cbvs() print('CBVs Made') print('Success! Sector {:2d} now available.'.format(self.sector)) self.try_next_sector()
def locate_with_tesscut(self): """ Finds the best TESS postcard(s) and the position of the source on postcard. Attributes ---------- postcard : list postcard_path : str position_on_postcard : list all_postcards : list sector : int camera : int chip : int position_on_chip : np.array """ self.postcard = [] self.position_on_postcard = [] self.all_postcards = [] self.tc = True coord = SkyCoord(self.coords[0], self.coords[1], unit="deg") sector_table = Tesscut.get_sectors(coord) self.sector = self.usr_sec self.camera = sector_table[sector_table['sector'] == self.sector]['camera'].quantity[0].value self.chip = sector_table[sector_table['sector'] == self.sector]['ccd'].quantity[0].value download_dir = self.tesscut_dir() fn_exists = self.search_tesscut(download_dir, coord) if fn_exists is None: manifest = Tesscut.download_cutouts(coord, self.tesscut_size, sector=self.usr_sec, path=download_dir) cutout = fits.open(manifest['Local Path'][0]) self.postcard_path = manifest['Local Path'][0] else: self.postcard_path = fn_exists cutout = fits.open(fn_exists) self.cutout = cutout self.postcard = self.postcard_path.split('/')[-1] xcoord = cutout[1].header['1CRV4P'] ycoord = cutout[1].header['2CRV4P'] self.position_on_chip = np.array([xcoord, ycoord])
def download_TESS_cutouts_coords(ra, dec, cutout_size=[11, 11]): """ This function downloads the TESS-cutouts for a target with specified ra and dec coordinates and prints the resulting filename(s) Inputs: Name unit type description ----------------------------------------------------------------------- ra deg float right ascension of target dec deg float declination of target cutout_size --- [2 x 1] list postcard cutout size """ cutout_coord = SkyCoord(ra, dec, unit="deg") manifest = Tesscut.download_cutouts(cutout_coord, cutout_size) print(manifest)
def locate_with_tesscut(self): """ Finds the best TESS postcard(s) and the position of the source on postcard. Attributes ---------- postcard : list postcard_path : str position_on_postcard : list all_postcards : list sector : int camera : int chip : int position_on_chip : np.array """ # Attributes only when using postcards self.all_postcards = None # Attribute for TessCut self.tc = True download_dir = self.tesscut_dir() coords = SkyCoord(self.coords[0], self.coords[1], unit=(u.deg, u.deg)) fn_exists = self.search_tesscut(download_dir, coords) if fn_exists is None: manifest = Tesscut.download_cutouts(coords, self.tesscut_size, sector=self.sector, path=download_dir) cutout = fits.open(manifest['Local Path'][0]) self.postcard_path = manifest['Local Path'][0] else: self.postcard_path = fn_exists cutout = fits.open(fn_exists) self.cutout = cutout self.postcard = self.postcard_path.split('/')[-1] xcoord = cutout[1].header['1CRV4P'] ycoord = cutout[1].header['2CRV4P'] self.position_on_chip = np.array([xcoord, ycoord])
def download_TESS_cutouts_ID(target_ID_list, cutout_size=[11, 11]): """ This function downloads the TESS-cutouts for a specified list of target IDs and returns a dictionary of the associated filename(s) for each target Inputs: Name type description ----------------------------------------------------------------------- target_ID_ list [n x 1] list list of target IDs as strings cutout_size [2 x 1] list postcard cutout size Output: target_filenames n x 2 dictionary with target ids and target filenames """ filename = "BANYAN_XI-III_combined_members.csv" data = Table.read(filename, format='ascii.csv') target_filenames = {} for target_id in target_ID_list: i = list(data['main_id']).index(target_id) ra = data['ra'][i] dec = data['dec'][i] object_coord = SkyCoord(ra, dec, unit="deg") manifest = Tesscut.download_cutouts(object_coord, cutout_size, path='./TESS_Sector_1_cutouts') sector_info = Tesscut.get_sectors(object_coord) if len(manifest['Local Path']) == 1: target_filenames[target_id] = manifest['Local Path'][0][2:] elif len(manifest['Local Path']) > 1: target_filenames[target_id] = [] for filename in manifest['Local Path']: target_filenames[target_id].append(filename[2:]) else: print( 'Cutout for target {} can not be downloaded'.format(target_id)) return target_filenames, sector_info
def convolve_cbvs(sectors=np.arange(1, 14, 1)): """ Bins the co-trending basis vectors into FFI times; Calls download_cbvs to get filenames Input ----- type(sectors) == list """ # Gets the cutout for a target in the CVZ coord = SkyCoord('04:35:50.330 -64:01:37.33', unit=(u.hourangle, u.deg)) sector_table = Tesscut.get_sectors(coord) for sector in sectors: files = download_cbvs(int(sector)) manifest = Tesscut.download_cutouts(coordinates=coord, size=31, sector=sector) cutout = fits.open(manifest['Local Path'][0]) time = cutout[1].data['TIME'] - cutout[1].data['TIMECORR'] for c in trange(len(files)): cbv = fits.open(files[c]) camera = cbv[1].header['CAMERA'] ccd = cbv[1].header['CCD'] cbv_time = cbv[1].data['Time'] new_fn = './eleanor/metadata/s{0:04d}/cbv_components_s{0:04d}_{1:04d}_{2:04d}.txt'.format( sector, camera, ccd) convolved = np.zeros((len(time), 16)) for i in range(len(time)): g = np.argmin(np.abs(time[i] - cbv_time)) for j in range(16): index = 'VECTOR_{0}'.format(j + 1) cads = np.arange(g - 7, g + 8, 1) convolved[i, j] = np.mean(cbv[1].data[index][cads]) np.savetxt(new_fn, convolved)
def get_data(ra, dec, units="deg", size=64): c = SkyCoord(ra, dec, units=units) data_table = Tesscut.download_cutouts(c, size=size) return data_table
def raw_FFI_lc_download(target_ID, sector, plot_tpf = False, plot_lc = False, save_path = '', from_file = False): """ Downloads and returns 30min cadence lightcurves based on SAP analysis of the raw FFIs """ if from_file == True: with open('Sector_{}_target_filenames.pkl'.format(sector), 'rb') as f: target_filenames = pickle.load(f) f.close() else: target_filenames = {} ra, dec, tic = find_tic(target_ID, from_file = True) object_coord = SkyCoord(ra, dec, unit="deg") manifest = Tesscut.download_cutouts(object_coord, [11,11], path = './TESS_Sector_1_cutouts') # sector_info = Tesscut.get_sectors(object_coord) if len(manifest['Local Path']) == 1: target_filenames[target_ID] = manifest['Local Path'][0][2:] elif len(manifest['Local Path']) > 1: target_filenames[target_ID] = [] for filename in manifest['Local Path']: target_filenames[target_ID].append(filename[2:]) else: print('Cutout for target {} can not be downloaded'.format(target_ID)) if type(target_filenames[target_ID]) == str: filename = target_filenames[target_ID] else: filename = target_filenames[target_ID][0] # Load tpf tpf_30min = lightkurve.search.open(filename) # Attach target name to tpf tpf_30min.targetid = target_ID # Create a median image of the source over time median_image = np.nanmedian(tpf_30min.flux, axis=0) # Select pixels which are brighter than the 85th percentile of the median image aperture_mask = median_image > np.nanpercentile(median_image, 85) # Plot and save tpf if plot_tpf == True: tpf_30min.plot(aperture_mask = aperture_mask) #tpf_plot.savefig(save_path + '{} - Sector {} - tpf plot.png'.format(target_ID, tpf.sector)) #plt.close(tpf_plot) # Convert to lightcurve object lc_30min = tpf_30min.to_lightcurve(aperture_mask = aperture_mask) # lc_30min = lc_30min[(lc_30min.time < 1346) | (lc_30min.time > 1350)] if plot_lc == True: lc_30min.scatter() plt.title('{} - 30min FFI base lc'.format(target_ID)) plt.xlabel("Time - 2457000 (BTJD days)") plt.ylabel("Relative flux") plt.show() return lc_30min
def __init__(self, sector=None): if sector is None: print('Please pass a sector into eleanor.Update().') return if not os.path.exists(eleanorpath + '/metadata'): os.mkdir(eleanorpath + '/metadata') # Updates max sector file first update_max_sector() self.sector = sector self.metadata_path = os.path.join( eleanorpath, 'metadata/s{0:04d}'.format(self.sector)) lastfile = 'cbv_components_s{0:04d}_0004_0004.txt'.format(self.sector) # Checks to see if directory contains all necessary files first if os.path.isdir(self.metadata_path): if lastfile in os.listdir(self.metadata_path): print('This directory already exists!') return self.north_coords = SkyCoord('16:35:50.667 +63:54:39.87', unit=(u.hourangle, u.deg)) self.south_coords = SkyCoord('04:35:50.330 -64:01:37.33', unit=(u.hourangle, u.deg)) if self.sector < 14 or self.sector > 26: try: manifest = Tesscut.download_cutouts(self.south_coords, 31, sector=self.sector) success = 1 except: print("This sector isn't available yet.") return else: try: manifest = Tesscut.download_cutouts(self.north_coords, 31, sector=self.sector) success = 1 except: print("This sector isn't available yet.") return if success == 1: if os.path.isdir(self.metadata_path) == True: pass else: os.mkdir(self.metadata_path) # memmap=False as wokaround for https://github.com/afeinstein20/eleanor/issues/204 self.cutout = fits.open(manifest['Local Path'][0], memmap=False) print('This is the first light curve you have made for this sector. ' 'Getting eleanor metadata products for ' 'Sector {0:2d}...'.format(self.sector)) print('This will only take a minute, and only needs to be done once. ' 'Any other light curves you make in this sector will be faster.') print('Acquiring target...') self.get_target() print('Calculating Cadences...') self.get_cadences() print('Assuring Quality Flags...') self.get_quality() print('Making CBVs...') self.get_cbvs() print('Success! Sector {:2d} now available.'.format(self.sector)) self.cutout.close() os.remove(manifest['Local Path'][0]) self.try_next_sector()