Skip to content

Commit

Permalink
Merge pull request #9 from Nschanche/EarlySectorFiles
Browse files Browse the repository at this point in the history
Early sector files
  • Loading branch information
Nschanche authored Sep 18, 2024
2 parents f68f4ef + 1bb0b24 commit 042f3bf
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 25 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,7 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
#.idea/

# Mac OSX
.DS_Store
10 changes: 10 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1.1.0dev (2024-09-18)
=====================

- Added the option to use the initial TESS commissioning PRF files [#9]

1.0.3 (2024-07-30)
==================

- Initial release

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "lkprf"
version = "1.0.3"
version = "1.1.0dev"
description = ""
authors = ["TESS Science Support Center <[email protected]>, Christina Hedges <[email protected]>"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion src/lkprf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Standard library
import os # noqa
import logging
from .version import __version__

PACKAGEDIR = os.path.abspath(os.path.dirname(__file__))

__version__ = "1.0.3"
logger = logging.getLogger("lkprf")

from .data import * # noqa
Expand Down
60 changes: 50 additions & 10 deletions src/lkprf/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,33 @@
"clear_tess_cache",
]

_tess_prefixes_S1_3 = {
1: {
1: "2018243163600",
2: "2018243163600",
3: "2018243163600",
4: "2018243163600",
},
2: {
1: "2018243163600",
2: "2018243163600",
3: "2018243163600",
4: "2018243163601",
},
3: {
1: "2018243163601",
2: "2018243163601",
3: "2018243163601",
4: "2018243163601",
},
4: {
1: "2018243163601",
2: "2018243163601",
3: "2018243163601",
4: "2018243163601",
},
}


_tess_prefixes = {
1: {
Expand Down Expand Up @@ -77,7 +104,7 @@ def download_kepler_prf_file(module: int, output: int):
return


def build_tess_prf_file(camera: int, ccd: int):
def build_tess_prf_file(camera: int, ccd: int, sector: int):
"""Download a set of TESS PRF files for a given camera/ccd"""

def open_file(url: str):
Expand All @@ -98,11 +125,19 @@ def open_file(url: str):
data[mask] = 0
return data, hdr

tess_archive_url = (
"https://archive.stsci.edu/missions/tess/models/prf_fitsfiles/start_s0004/"
)
prefix = _tess_prefixes[camera][ccd]
filename = f"tess-prf-{camera}-{ccd}.fits"
if sector <= 3:
tess_archive_url = (
"https://archive.stsci.edu/missions/tess/models/prf_fitsfiles/start_s0001/"
)
prefix = _tess_prefixes_S1_3[camera][ccd]
filename = f"tess-prf-cam{camera}-ccd{ccd}-sec1.fits"
else:
tess_archive_url = (
"https://archive.stsci.edu/missions/tess/models/prf_fitsfiles/start_s0004/"
)
prefix = _tess_prefixes[camera][ccd]
filename = f"tess-prf-cam{camera}-ccd{ccd}-sec4.fits"

file_path = f"{PACKAGEDIR}/data/{filename}"
# ensure the file_path exists
if not os.path.exists(file_path):
Expand Down Expand Up @@ -135,19 +170,24 @@ def get_kepler_prf_file(module: int, output: int):
)
download_kepler_prf_file(module=module, output=output)
file_path = f"{PACKAGEDIR}/data/{filename}"

hdulist = fitsio.FITS(file_path)
return hdulist


def get_tess_prf_file(camera: int, ccd: int):
"""Get a PRF file for a given camera/ccd"""
filename = f"tess-prf-{camera}-{ccd}.fits"
def get_tess_prf_file(camera: int, ccd: int, sector: int = 4):
"""Get a PRF file for a given camera/ccd/sector"""
if sector <= 3:
filename = f"tess-prf-cam{camera}-ccd{ccd}-sec1.fits"
else:
filename = f"tess-prf-cam{camera}-ccd{ccd}-sec4.fits"
file_path = f"{PACKAGEDIR}/data/{filename}"
if not os.path.isfile(file_path):
logger.info(
f"No local files found, building TESS PRF for Camera {camera}, CCD {ccd}."
)
build_tess_prf_file(camera=camera, ccd=ccd)
build_tess_prf_file(camera=camera, ccd=ccd, sector=sector)

hdulist = fitsio.FITS(file_path)
return hdulist

Expand Down
23 changes: 15 additions & 8 deletions src/lkprf/prfmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _evaluate(
Parameters
----------
targets : List of Tuples
Coordinates of the targets
Pixel coordinates of the targets
origin : Tuple
The origin of the image, combined with shape this sets the extent of the image
shape : Tuple
Expand Down Expand Up @@ -193,6 +193,7 @@ def _prepare_prf(self):
"""

hdulist = self._get_prf_data()
self.date = hdulist[0].read_header()['DATE']
PRFdata, crval1p, crval2p, cdelt1p, cdelt2p = [], [], [], [], []
for hdu in hdulist[1:]:
PRFdata.append(hdu.read())
Expand Down Expand Up @@ -247,13 +248,19 @@ def _update_coordinates(self, targets: List[Tuple], shape: Tuple):
ref_row = row + 0.5 * rowdim
supersamp_prf = np.zeros(self.PRFdata.shape[1:], dtype="float32")

for i in range(self.PRFdata.shape[0]):
prf_weight = np.sqrt(
(ref_column - self.crval1p[i]) ** 2 + (ref_row - self.crval2p[i]) ** 2
)
if prf_weight < min_prf_weight:
prf_weight = min_prf_weight
supersamp_prf += self.PRFdata[i] / prf_weight
# Find the 4 measurements nearest the desired locations
prf_weights = [
np.sqrt(
(ref_column - self.crval1p[i]) ** 2 + (ref_row - self.crval2p[i]) ** 2)
for i in range(self.PRFdata.shape[0])
]
idx = np.argpartition(prf_weights, 4)[:4]

for i in idx:
if prf_weights[i] < min_prf_weight:
prf_weights[i] = min_prf_weight
supersamp_prf += self.PRFdata[i] / prf_weights[i]


supersamp_prf /= np.nansum(supersamp_prf) * self.cdelt1p[0] * self.cdelt2p[0]
self.interpolate = RectBivariateSpline(self.PRFrow, self.PRFcol, supersamp_prf)
Expand Down
10 changes: 6 additions & 4 deletions src/lkprf/tessprf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@


class TESSPRF(PRF):
"""A TESSPRF class. The TESS PRF measurements are supersampled by a factor of 9."""
"""A TESSPRF class. The TESS PRF measurements are supersampled by a factor of 9.
Two PRF models were produced, one for sectors 1-3 and a second set for sectors 4+ """

def __init__(self, camera: int, ccd: int):
def __init__(self, camera: int, ccd: int, sector: int = 4):
super().__init__()
self.camera = camera
self.ccd = ccd
self.sector = sector
self.mission = "TESS"
self._prepare_prf()

def __repr__(self):
return f"TESSPRF Object [Camera {self.camera}, CCD {self.ccd}]"
return f"TESSPRF Object [Camera {self.camera}, CCD {self.ccd}, Sector {self.sector}]"

def _get_prf_data(self):
return get_tess_prf_file(camera=self.camera, ccd=self.ccd)
return get_tess_prf_file(camera=self.camera, ccd=self.ccd, sector=self.sector)

def update_coordinates(self, targets: List[Tuple], shape: Tuple):
row, column = self._unpack_targets(targets)
Expand Down
3 changes: 3 additions & 0 deletions src/lkprf/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# It is important to store the version number in a separate file
# so that we can read it without importing the package
__version__ = "1.1.0dev"
14 changes: 14 additions & 0 deletions tests/test_prf.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,17 @@ def test_prfs():
for a in ar:
assert (a[0] < 0).any()
assert (a[0] > 0).any()


def test_prf_version():
# TESS has a different set of measurements for early (1-3) sectors.
# Check to make sure it is reading out different files.

prf_sec1_3 = lkprf.TESSPRF(camera=1, ccd=1, sector= 1)
prf_sec4_plus = lkprf.TESSPRF(camera=1, ccd=1, sector= 14)
# If not specified, should default to sector 4+ measurements
prf_sec4_notspecified = lkprf.TESSPRF(camera=1, ccd=1)

assert prf_sec1_3.date == '30-Jan-2019'
assert prf_sec4_plus.date == '01-May-2019'
assert prf_sec4_notspecified.date == '01-May-2019'

0 comments on commit 042f3bf

Please sign in to comment.