Skip to content

Commit

Permalink
Merge pull request #8 from barentsen/fix-off-by-one
Browse files Browse the repository at this point in the history
Fix Off-By-One error in column and row coordinates
  • Loading branch information
barentsen authored Jul 21, 2021
2 parents 9c52d86 + 4f6830b commit 9e2630b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dephell = "^0.8.3"
tess-locator = {path = "../tess-locator", develop = true}
tess-ephem = {path = "../tess-ephem", develop = true}
lightkurve = {path = "../lightkurve", develop = true}
pytest-remotedata = "^0.3.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
4 changes: 3 additions & 1 deletion src/tess_cloud/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ async def _find_data_offset(
async def _find_pixel_offset(self, column: int, row: int) -> int:
"""Returns the byte offset of a specific pixel position."""
data_offset = await self._find_data_offset(ext=self.data_ext)
pixel_offset = column + row * FFI_COLUMNS
# Subtract 1 from column and row because the byte location assumes zero-indexing,
# whereas the TESS convention is to address column and row number with one-indexing.
pixel_offset = (column - 1) + (row - 1) * FFI_COLUMNS
return data_offset + BYTES_PER_PIX * pixel_offset

async def _find_pixel_blocks(
Expand Down
7 changes: 7 additions & 0 deletions src/tess_cloud/targetpixelfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,14 @@ def _create_table_extension(self):

coldefs = fits.ColDefs(cols)
hdu = fits.BinTableHDU.from_columns(coldefs)

# Set useful header keywords
hdu.header["BJDREFI"] = 2457000
# Lightkurve relies on 1CRV5P and 2CRV5P to display column/row coordinates in plots
if "CORNER_COLUMN" in self._optional_column_data:
hdu.header["1CRV5P"] = self._optional_column_data["CORNER_COLUMN"][0]
if "CORNER_ROW" in self._optional_column_data:
hdu.header["2CRV5P"] = self._optional_column_data["CORNER_ROW"][0]

return hdu

Expand Down
28 changes: 27 additions & 1 deletion tests/test_cutout.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import asyncio

from astropy.io import fits
import lightkurve as lk
import numpy as np
import pytest

from tess_cloud import TessImage
from tess_cloud import TessImage, list_images


def test_cutout():
Expand Down Expand Up @@ -33,3 +35,27 @@ def test_cutout():
img.cutout(column=1, row=1, shape=(1, 3)).flux.round(7)
== np.array([[-0.0605419], [0.0327947], [-0.0278026]])
).all()


@pytest.mark.remote_data
def test_against_tesscut():
"""Does a cutout from TessCut match TessCloud?"""
target = "Pi Men"
sector = 31
shape = (3, 3)
# Download TPF with TessCut
tpf_tesscut = lk.search_tesscut(target, sector=sector).download(
cutout_size=shape, quality_bitmask=None
)
# Download TPF with TessCloud
imglist = list_images(sector=sector, camera=tpf_tesscut.camera, ccd=tpf_tesscut.ccd)
center_column, center_row = (
tpf_tesscut.column + 1,
tpf_tesscut.row + 1,
) # add +1 to request center of a (3, 3)
tpf_tesscloud = imglist[0:2].cutout(
column=center_column, row=center_row, shape=shape
)
# Compare both
assert np.all(tpf_tesscut[0].flux == tpf_tesscloud[0].flux)
assert np.all(tpf_tesscut[1].flux == tpf_tesscloud[1].flux)

0 comments on commit 9e2630b

Please # to comment.