Skip to content

Commit

Permalink
merged data
Browse files Browse the repository at this point in the history
  • Loading branch information
Nschanche committed Mar 28, 2024
2 parents 029254f + e284df8 commit 7585242
Show file tree
Hide file tree
Showing 8 changed files with 1,243 additions and 1,334 deletions.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,58 @@ This package is a stand-alone implementation of the lightkurve search functional

## Contact
Please Don't

Mermaid Test
```mermaid
graph TD
subgraph sg1[class Search_Mission]
Search_Mission --> Search_Timeseries
Search_Mission --> Search_Cubedata
Search_Timeseries --> Search_Timeseries_MissionProducts
Search_Timeseries --> Search_Timeseries_HLSP
Search_Cubedata --> Search_CubeData_MissionProducts
Search_Cubedata --> Search_CubeData_HLSP
end
subgraph sg2[class Search_Kepler]
Search_Kepler --> SK_T[Search_Timeseries]
SK_T --> SK_T_M[Search_Timeseries_MissionProducts]
SK_T --> SK_T_H[Search_Timeseries_HLSP]
Search_Kepler --> SK_C[Search_Cubedata]
SK_C --> SK_TP[Search_TargetPixelFile]
SK_C --> SK_TC[Search_TESSCut]
end
subgraph sg3[class Search_K2]
Search_K2 --> SK2_T[Search_Timeseries]
SK2_T --> SK2_T_M[Search_Timeseries_MissionProducts]
SK2_T --> SK2_T_H[Search_Timeseries_HLSP]
Search_K2 --> SK2_C[Search_Cubedata]
SK2_C --> SK2_TP[Search_TargetPixelFile]
SK2_C --> SK2_TC[Search_TESSCut]
end
subgraph sg4[class Search_TESS]
Search_TESS --> ST_T[Search_Timeseries]
ST_T --> ST_T_M[Search_Timeseries_MissionProducts]
ST_T --> ST_T_H[Search_Timeseries_HLSP]
Search_TESS --> ST_C[Search_Cubedata]
ST_C --> ST_TP[Search_TargetPixelFile]
ST_C --> ST_TC[Search_TESSCut]
ST_TP --> ST_TP_M[Search_TPF_MissionProducts]
ST_TP --> ST_TP_H[Search_TPF_HLSP]
ST_TC --> ST_TC_M[Search_TESSCut_MissionProducts]
ST_TC --> ST_TC_H[Search_TESSCut_HLSP]
end
sg1 --> sg2
sg1 --> sg3
sg1 --> sg4
```
23 changes: 12 additions & 11 deletions src/newlk_search/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#!/usr/bin/env python

from __future__ import absolute_import
from . import config as _config

import os

PACKAGEDIR = os.path.abspath(os.path.dirname(__file__))
PREFER_CLOUD = True # Do you prefer URIs pointing to the Amazon bucket when available?
DOWNLOAD_CLOUD = True # TODO: Ask Tyler if this only downloads if there is cloud data?

from .version import __version__


class Conf(_config.ConfigNamespace):
"""
Configuration parameters for `lightkurve`.
Configuration parameters for `search`.
Refer to `astropy.config.ConfigNamespace` for API details.
Expand All @@ -24,8 +30,6 @@ class Conf(_config.ConfigNamespace):
cache_dir
Default cache directory for data files downloaded, etc. Defaults to ``~/.lightkurve/cache`` if not specified.
warn_legacy_cache_dir
If set to True, issue warning if the legacy default cache directory exists. Default is True.
"""
# Note: when using list or string_list datatype,
# the behavior of astropy's parsing of the config file value:
Expand All @@ -47,11 +51,8 @@ class Conf(_config.ConfigNamespace):
module="lightkurve.config"
)

warn_legacy_cache_dir = _config.ConfigItem(
True,
"If set to True, issue warning if the legacy default cache directory exists.",
cfgtype="boolean",
module="lightkurve.config"
)

conf = Conf()

conf = Conf()

from .search import *
131 changes: 131 additions & 0 deletions src/newlk_search/cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import tempfile
import configparser
import os

from functools import lru_cache, wraps
from joblib import Memory

import logging # noqa: E402
from rich.logging import RichHandler # noqa: E402

from IPython.display import display, Pretty

def get_logger():
"""Configure and return a logger with RichHandler."""
logger = logging.getLogger(__name__)
logger.setLevel(logging.WARN)

# Add RichHandler
rich_handler = RichHandler()
rich_handler.setFormatter(
logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
)

logger.addHandler(rich_handler)
return logger
log = get_logger()

def check_cache_dir(cache_dir):
if not os.path.exists(cache_dir):
try:
os.makedirs(cache_dir) # Try creating the cache directory
except OSError as e:
raise PermissionError(f"Unable to create cache directory at {cache_dir}.") from e

# Check if the directory is writable
try:
with tempfile.NamedTemporaryFile(dir=cache_dir, delete=False) as fp:
fp.write(b'Hello world!')
fp.close()
with open(fp.name, mode='rb') as f:
f.read()
except OSError as e:
raise PermissionError(f"The cache directory at {cache_dir} is not writable.") from e
return cache_dir

def make_default_config():
config = configparser.ConfigParser()
config['CacheSettings'] = {'use_disk_cache': 'False', 'cache_dir': _default_cache_dir}
with open(_config_file_path, 'w') as f:
config.write(f)

def load_config():
config = configparser.ConfigParser()
if not os.path.exists(_config_file_path):
make_default_config()
else:
config.read(_config_file_path)
return config

def save_config(config):
with open(_config_file_path, 'w') as f:
config.write(f)

def show_config():
config = load_config()
def parse_section(section):
return "\n".join([f"\t{c[0]}: \"{c[1]}\"" for c in section.items()])
display(Pretty("\n".join([f"{section}: \n{parse_section(config[section])}" for section in config.sections()])))

def set_use_disk_cache(use_disk_cache:bool):
"""Set whether to cache on disk. Default is False."""
try:
config = load_config()
if not isinstance(use_disk_cache, bool):
raise ValueError("`use_disk_cache` must been type `bool`.")
config['CacheSettings']['use_disk_cache'] = f"{use_disk_cache}"
save_config(config)
except:
log.warning("Can not update config parameters. Setting `USE_DISK_CACHE` in this session.")
global USE_DISK_CACHE
USE_DISK_CACHE = use_disk_cache

def set_disk_cache_directory(cache_directory):
try:
config = load_config()
if not isinstance(cache_directory, str):
raise ValueError("`cache_directory` must been type `str`.")
check_cache_dir(cache_directory)
config['CacheSettings']['cache_dir'] = cache_directory
save_config(config)
except:
log.warning("Can not update config parameters. Setting `CACHE_DIR` in this session.")
global CACHE_DIR
CACHE_DIR = cache_directory


_default_cache_dir = check_cache_dir(os.path.join(os.path.expanduser('~'), '.newlk_search'))
_config_file_path = os.path.join(_default_cache_dir, 'config.ini')

USE_DISK_CACHE = load_config()['CacheSettings']['use_disk_cache'].lower() in ["true", "y", "yes", "on", "disk"]
CACHE_DIR = load_config()['CacheSettings']['cache_dir']

if USE_DISK_CACHE:
memory = Memory(CACHE_DIR, verbose=0)
else:
memory = None

def cache(lru_cache_size=32):
"""
Decorator to cache either on disk or in memory
"""
if USE_DISK_CACHE:
if not 'memory' in globals():
global memory
memory = Memory(CACHE_DIR, verbose=0)
if memory is None:
memory = Memory(CACHE_DIR, verbose=0)
if globals()['memory'].location != CACHE_DIR:
memory = Memory(CACHE_DIR, verbose=0)
def disk_cache_decorator(func):
cached_func = memory.cache(func)
return cached_func
return disk_cache_decorator
else:
def memory_cache_decorator(func):
@wraps(func)
@lru_cache(maxsize=lru_cache_size)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
return memory_cache_decorator
5 changes: 2 additions & 3 deletions src/newlk_search/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

ROOTNAME = 'lightkurve'


class ConfigNamespace(astropyconfig.ConfigNamespace):
rootname = ROOTNAME

Expand Down Expand Up @@ -97,7 +96,7 @@ def _ensure_cache_dir_exists(cache_dir):
return cache_dir


def warn_if_default_cache_dir_migration_needed():
'''def warn_if_default_cache_dir_migration_needed():
from .. import conf
if not conf.warn_legacy_cache_dir:
Expand All @@ -122,4 +121,4 @@ def warn_if_default_cache_dir_migration_needed():
f"and remove the legacy directory. "
f"Refer to https://docs.lightkurve.org/reference/config.html#default-cache-directory-migration "
f"for more information."
)
)'''
51 changes: 51 additions & 0 deletions src/newlk_search/data/short_cadence_month_lookup.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Quarter,Month,StartTime
0,1,2009131110544
1,1,2009166044711
2,1,2009201121230
2,2,2009231120729
2,3,2009259162342
3,1,2009291181958
3,2,2009322144938
3,3,2009350160919
4,1,2010009094841
4,2,2010019161129
4,3,2010049094358
4,4,2010078100744
5,1,2010111051353
5,2,2010140023957
5,3,2010174090439
6,1,2010203174610
6,2,2010234115140
6,3,2010265121752
7,1,2010296114515
7,2,2010326094124
7,3,2010355172524
8,1,2011024051157
8,2,2011053090032
8,3,2011073133259
9,1,2011116030358
9,2,2011145075126
9,3,2011177032512
10,1,2011208035123
10,2,2011240104155
10,3,2011271113734
11,1,2011303113607
11,2,2011334093404
11,3,2012004120508
12,1,2012032013838
12,2,2012060035710
12,3,2012088054726
13,1,2012121044856
13,2,2012151031540
13,3,2012179063303
14,1,2012211050319
14,2,2012242122129
14,3,2012277125453
15,1,2012310112549
15,2,2012341132017
15,3,2013011073258
16,1,2013017113907
16,2,2013065031647
16,3,2013098041711
17,1,2013121191144
17,2,2013131215648
Loading

0 comments on commit 7585242

Please sign in to comment.