Skip to content

Commit 22fd0bb

Browse files
add cached properties
1 parent 6c22790 commit 22fd0bb

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

src/tesscube/cube.py

+28-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from functools import lru_cache
1+
from functools import lru_cache, cached_property
22
from typing import Optional, Union, Dict
33

44
import numpy as np
@@ -64,7 +64,7 @@ def __init__(self, sector: int, camera: int, ccd: int):
6464
def __repr__(self):
6565
return f"TESSCube [Sector {self.sector}, Camera {self.camera}, CCD {self.ccd}]"
6666

67-
@property
67+
@cached_property
6868
def header_dict(self) -> Dict:
6969
"""
7070
Get the header keywords from the MAST cube.
@@ -103,12 +103,12 @@ def from_skycoord(coord: SkyCoord, sector: int):
103103
if cube.wcs.footprint_contains(coord):
104104
return cube
105105

106-
@property
106+
@cached_property
107107
def primary_hdu(self):
108108
"""The primary HDU of the cube file."""
109109
return get_primary_hdu(object_key=self.object_key)
110110

111-
@property
111+
@cached_property
112112
def last_hdu(self):
113113
"""The last HDU of the cube file."""
114114
end = (
@@ -117,11 +117,23 @@ def last_hdu(self):
117117
)
118118
return get_last_hdu(object_key=self.object_key, end=end)
119119

120-
@property
120+
@cached_property
121121
def ffi_names(self):
122122
"""The FFI names used to make the cube."""
123123
return list(self.last_hdu.data["FFI_FILE"])
124124

125+
@cached_property
126+
def tstart(self):
127+
return self.last_hdu.data["TSTART"]
128+
129+
@cached_property
130+
def tstop(self):
131+
return self.last_hdu.data["TSTOP"]
132+
133+
@cached_property
134+
def telapse(self):
135+
return self.last_hdu.data["TELAPSE"]
136+
125137
@lru_cache(maxsize=4)
126138
def get_ffi(
127139
self,
@@ -159,8 +171,8 @@ def get_ffi(
159171
)
160172

161173
if time is not None:
162-
start = Time(self.last_hdu.data["TSTART"] + 2457000, format="jd")
163-
end = Time(self.last_hdu.data["TSTART"] + 2457000, format="jd")
174+
start = Time(self.tstart + 2457000, format="jd")
175+
end = Time(self.tstop + 2457000, format="jd")
164176
t = Time.now()
165177
t = Time(2459343.87182313, format="jd")
166178
if not ((t > start).any() & (t < end).any()):
@@ -296,8 +308,8 @@ def get_tpf(
296308
)
297309
mask = np.in1d(cadenceno, idxs)
298310
time = (
299-
self.last_hdu.data["tstop"][k][mask][::frame_bin]
300-
+ self.last_hdu.data["tstart"][k][mask][(frame_bin - 1) :: frame_bin]
311+
self.tstop[k][mask][::frame_bin]
312+
+ self.tstart[k][mask][(frame_bin - 1) :: frame_bin]
301313
) / 2
302314
timecorr = np.nanmean(
303315
np.asarray(
@@ -454,33 +466,30 @@ def get_tpf(
454466
hdulist = fits.HDUList([self.output_primary_ext, table_hdu, aperture_hdu])
455467
return hdulist
456468

457-
@property
469+
@cached_property
458470
def time(self):
459471
"""Time of the frames in BTJD. Note this is the time at the center of the FFI."""
460-
return (self.last_hdu.data["TSTART"] + self.last_hdu.data["TSTOP"]) / 2
472+
return (self.tstart + self.tstop) / 2
461473

462-
@property
474+
@cached_property
463475
def timecorr(self):
464476
"""Barycentric time correction for the center of the FFI."""
465477
return self.last_hdu.data["BARYCORR"]
466478

467-
@property
479+
@cached_property
468480
def quality(self):
469481
"""SPOC provided quality flags for each cadence."""
470482
return self.last_hdu.data["DQUALITY"]
471483

472-
@property
484+
@cached_property
473485
def cadence_number(self):
474486
"""Cadence number for each frame. Note this is not the same as the SPOC provided cadence numbers."""
475487
cadence_number = np.cumsum(
476-
np.round(
477-
np.diff(self.last_hdu.data["TSTART"])
478-
/ np.median(self.last_hdu.data["TELAPSE"])
479-
).astype(int)
488+
np.round(np.diff(self.tstop) / np.median(self.telapse)).astype(int)
480489
)
481490
return np.hstack([cadence_number, cadence_number[-1] + 1])
482491

483-
@property
492+
@cached_property
484493
def exposure_time(self):
485494
"""Exposure time in days"""
486495
return self.last_hdu.data["EXPOSURE"][0]

src/tesscube/wcs.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import bz2
44
import json
55
import os
6-
from functools import lru_cache
6+
from functools import lru_cache, cached_property
77

88
import numpy as np
99
from astropy.io import fits
@@ -80,7 +80,7 @@ def _extract_average_WCS(hdu):
8080
class WCSMixin:
8181
"""Mixins to use the WCS"""
8282

83-
@property
83+
@cached_property
8484
def wcs(self):
8585
return _extract_average_WCS(self.last_hdu)
8686

0 commit comments

Comments
 (0)