From 8f76dbb9d314783d2b421436b30b2b235e060409 Mon Sep 17 00:00:00 2001 From: Nschanche Date: Fri, 20 Sep 2024 16:04:23 -0400 Subject: [PATCH 1/4] added cache_dir option --- src/lkprf/__init__.py | 1 + src/lkprf/data.py | 33 ++++++++++++++++----------------- src/lkprf/keplerprf.py | 7 +++++-- src/lkprf/tessprf.py | 7 +++++-- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/lkprf/__init__.py b/src/lkprf/__init__.py index fa7660a..9d5a08d 100644 --- a/src/lkprf/__init__.py +++ b/src/lkprf/__init__.py @@ -4,6 +4,7 @@ from .version import __version__ PACKAGEDIR = os.path.abspath(os.path.dirname(__file__)) +CACHEDIR = PACKAGEDIR + '/data/' logger = logging.getLogger("lkprf") diff --git a/src/lkprf/data.py b/src/lkprf/data.py index fddf20f..9e0b999 100644 --- a/src/lkprf/data.py +++ b/src/lkprf/data.py @@ -6,7 +6,7 @@ import fitsio from scipy.ndimage import label, uniform_filter -from . import logger, PACKAGEDIR +from . import logger, PACKAGEDIR, CACHEDIR __all__ = [ "download_kepler_prf_file", @@ -91,10 +91,10 @@ def _download_file(url, file_path): raise http_err -def download_kepler_prf_file(module: int, output: int): +def download_kepler_prf_file(module: int, output: int, cache_dir: str = CACHEDIR): """Download a Kepler Module file""" filename = f"kplr{module:02}.{output}_2011265_prf.fits" - file_path = f"{PACKAGEDIR}/data/{filename}" + file_path = f"{cache_dir}{filename}" url = f"https://archive.stsci.edu/missions/kepler/fpc/prf/{filename}" logger.info(f"Downloading {module:02}.{output}") _download_file(url, file_path) @@ -104,7 +104,7 @@ def download_kepler_prf_file(module: int, output: int): return -def build_tess_prf_file(camera: int, ccd: int, sector: int): +def build_tess_prf_file(camera: int, ccd: int, sector: int, cache_dir: str = CACHEDIR): """Download a set of TESS PRF files for a given camera/ccd""" def open_file(url: str): @@ -138,7 +138,7 @@ def open_file(url: str): prefix = _tess_prefixes[camera][ccd] filename = f"tess-prf-cam{camera}-ccd{ccd}-sec4.fits" - file_path = f"{PACKAGEDIR}/data/{filename}" + file_path = f"{cache_dir}{filename}" # ensure the file_path exists if not os.path.exists(file_path): os.makedirs(os.path.dirname(file_path), exist_ok=True) @@ -160,51 +160,50 @@ def open_file(url: str): return -def get_kepler_prf_file(module: int, output: int): +def get_kepler_prf_file(module: int, output: int, cache_dir: str = CACHEDIR): """Download a Kepler Module file""" filename = f"kplr{module:02}.{output}_2011265_prf.fits" - file_path = f"{PACKAGEDIR}/data/{filename}" + file_path = f"{cache_dir}{filename}" if not os.path.isfile(file_path): logger.info( f"No local files found, building Kepler PRF for Module {module}, output {output}." ) - download_kepler_prf_file(module=module, output=output) - file_path = f"{PACKAGEDIR}/data/{filename}" + download_kepler_prf_file(module=module, output=output, cache_dir=cache_dir) hdulist = fitsio.FITS(file_path) return hdulist -def get_tess_prf_file(camera: int, ccd: int, sector: int = 4): +def get_tess_prf_file(camera: int, ccd: int, sector: int = 4, cache_dir: str = CACHEDIR): """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}" + file_path = f"{cache_dir}{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, sector=sector) - + build_tess_prf_file(camera=camera, ccd=ccd, sector=sector, cache_dir=cache_dir) + hdulist = fitsio.FITS(file_path) return hdulist -def clear_kepler_cache(): +def clear_kepler_cache(cache_dir: str = CACHEDIR): for module in np.arange(26): for output in np.arange(1, 5): filename = f"kplr{module:02}.{output}_2011265_prf.fits" - file_path = f"{PACKAGEDIR}/data/{filename}" + file_path = f"{cache_dir}{filename}" if os.path.isfile(file_path): os.remove(file_path) -def clear_tess_cache(): +def clear_tess_cache(cache_dir: str = CACHEDIR): for camera in np.arange(1, 5): for ccd in np.arange(1, 5): filename = f"tess-prf-{camera}-{ccd}.fits" - file_path = f"{PACKAGEDIR}/data/{filename}" + file_path = f"{cache_dir}{filename}" if os.path.isfile(file_path): os.remove(file_path) diff --git a/src/lkprf/keplerprf.py b/src/lkprf/keplerprf.py index f52c4e3..a64da84 100644 --- a/src/lkprf/keplerprf.py +++ b/src/lkprf/keplerprf.py @@ -4,6 +4,7 @@ import numpy as np from .utils import channel_to_module_output, LKPRFWarning from .data import get_kepler_prf_file +from . import CACHEDIR import warnings from .prfmodel import PRF @@ -21,18 +22,20 @@ class KeplerPRF(PRF): https://archive.stsci.edu/missions/kepler/commissioning_prfs/ """ - def __init__(self, channel: int): + def __init__(self, channel: int, cache_dir: str = CACHEDIR): super().__init__() self.channel = channel self.mission = "Kepler" + self.cache_dir = cache_dir self._prepare_prf() + def __repr__(self): return f"KeplerPRF Object [Channel {self.channel}]" def _get_prf_data(self): module, output = channel_to_module_output(self.channel) - return get_kepler_prf_file(module=module, output=output) + return get_kepler_prf_file(module=module, output=output, cache_dir = self.cache_dir) def update_coordinates(self, targets: List[Tuple], shape: Tuple): row, column = self._unpack_targets(targets) diff --git a/src/lkprf/tessprf.py b/src/lkprf/tessprf.py index e1e3ee7..ea75371 100644 --- a/src/lkprf/tessprf.py +++ b/src/lkprf/tessprf.py @@ -4,6 +4,7 @@ import numpy as np from .utils import LKPRFWarning from .data import get_tess_prf_file +from . import CACHEDIR import warnings from .prfmodel import PRF @@ -14,19 +15,21 @@ class TESSPRF(PRF): """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, sector: int = 4): + def __init__(self, camera: int, ccd: int, sector: int = 4, cache_dir: str = CACHEDIR): super().__init__() self.camera = camera self.ccd = ccd self.sector = sector self.mission = "TESS" + self.cache_dir = cache_dir self._prepare_prf() def __repr__(self): 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, sector=self.sector) + print(self.cache_dir) + return get_tess_prf_file(camera=self.camera, ccd=self.ccd, sector=self.sector, cache_dir=self.cache_dir) def update_coordinates(self, targets: List[Tuple], shape: Tuple): row, column = self._unpack_targets(targets) From d6b9ba6518d903db3d7138fe240435deab2ac4b4 Mon Sep 17 00:00:00 2001 From: Nschanche Date: Mon, 23 Sep 2024 13:32:37 -0400 Subject: [PATCH 2/4] rearrange cache_dir --- src/lkprf/__init__.py | 1 - src/lkprf/data.py | 3 ++- src/lkprf/keplerprf.py | 4 ++-- src/lkprf/tessprf.py | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lkprf/__init__.py b/src/lkprf/__init__.py index 9d5a08d..fa7660a 100644 --- a/src/lkprf/__init__.py +++ b/src/lkprf/__init__.py @@ -4,7 +4,6 @@ from .version import __version__ PACKAGEDIR = os.path.abspath(os.path.dirname(__file__)) -CACHEDIR = PACKAGEDIR + '/data/' logger = logging.getLogger("lkprf") diff --git a/src/lkprf/data.py b/src/lkprf/data.py index 9e0b999..0a11621 100644 --- a/src/lkprf/data.py +++ b/src/lkprf/data.py @@ -6,7 +6,8 @@ import fitsio from scipy.ndimage import label, uniform_filter -from . import logger, PACKAGEDIR, CACHEDIR +from . import logger, PACKAGEDIR +CACHEDIR = PACKAGEDIR + '/data/' __all__ = [ "download_kepler_prf_file", diff --git a/src/lkprf/keplerprf.py b/src/lkprf/keplerprf.py index a64da84..a082953 100644 --- a/src/lkprf/keplerprf.py +++ b/src/lkprf/keplerprf.py @@ -4,7 +4,7 @@ import numpy as np from .utils import channel_to_module_output, LKPRFWarning from .data import get_kepler_prf_file -from . import CACHEDIR +from . import PACKAGEDIR import warnings from .prfmodel import PRF @@ -22,7 +22,7 @@ class KeplerPRF(PRF): https://archive.stsci.edu/missions/kepler/commissioning_prfs/ """ - def __init__(self, channel: int, cache_dir: str = CACHEDIR): + def __init__(self, channel: int, cache_dir: str = PACKAGEDIR + '/data/'): super().__init__() self.channel = channel self.mission = "Kepler" diff --git a/src/lkprf/tessprf.py b/src/lkprf/tessprf.py index ea75371..9e1de27 100644 --- a/src/lkprf/tessprf.py +++ b/src/lkprf/tessprf.py @@ -4,7 +4,7 @@ import numpy as np from .utils import LKPRFWarning from .data import get_tess_prf_file -from . import CACHEDIR +from . import PACKAGEDIR import warnings from .prfmodel import PRF @@ -15,7 +15,7 @@ class TESSPRF(PRF): """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, sector: int = 4, cache_dir: str = CACHEDIR): + def __init__(self, camera: int, ccd: int, sector: int = 4, cache_dir: str = PACKAGEDIR + '/data/'): super().__init__() self.camera = camera self.ccd = ccd From 5e6e234fd060401a2502836b8be5f495500c36f1 Mon Sep 17 00:00:00 2001 From: Nschanche Date: Tue, 24 Sep 2024 10:45:47 -0400 Subject: [PATCH 3/4] Update changelog --- CHANGES.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 90b795a..dda957c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,7 +3,7 @@ - Added the option to use the initial TESS commissioning PRF files [#9] - Modified tessprf.py and keplerprf.py to use only the closest PRF measurements to create the supersampled PRF [#8] - +- Added cache_dir flag to change default location for reading/writing engineer files [#13] 1.0.3 (2024-07-30) ================== From b86aabbe97eb4e9ac8fe1f7e48750c943d8d4686 Mon Sep 17 00:00:00 2001 From: Nschanche Date: Tue, 24 Sep 2024 14:36:37 -0400 Subject: [PATCH 4/4] remove print --- src/lkprf/tessprf.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lkprf/tessprf.py b/src/lkprf/tessprf.py index 9e1de27..ff9edba 100644 --- a/src/lkprf/tessprf.py +++ b/src/lkprf/tessprf.py @@ -28,7 +28,6 @@ def __repr__(self): return f"TESSPRF Object [Camera {self.camera}, CCD {self.ccd}, Sector {self.sector}]" def _get_prf_data(self): - print(self.cache_dir) return get_tess_prf_file(camera=self.camera, ccd=self.ccd, sector=self.sector, cache_dir=self.cache_dir) def update_coordinates(self, targets: List[Tuple], shape: Tuple):