Skip to content

Commit

Permalink
change tab to space, fix PEP8 auto, prepare for black
Browse files Browse the repository at this point in the history
We move to a 4 space format, Automatically fix Pep8 mistakes that can be auto fixed, and change the Flake8 setup.cfg to match what will happen under black. Currently line limit is set at 120 but that can be discussed.
  • Loading branch information
emirkmo committed Feb 14, 2022
1 parent 563f809 commit cbca2f3
Show file tree
Hide file tree
Showing 45 changed files with 5,605 additions and 5,960 deletions.
1 change: 1 addition & 0 deletions flows/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
from .config import load_config

from .version import get_version

__version__ = get_version(pep440=False)
101 changes: 51 additions & 50 deletions flows/aadc_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,54 @@
import getpass
from .config import load_config

#--------------------------------------------------------------------------------------------------
class AADC_DB(object): # pragma: no cover
"""
Connection to the central TASOC database.
Attributes:
conn (`psycopg2.Connection` object): Connection to PostgreSQL database.
cursor (`psycopg2.Cursor` object): Cursor to use in database.
"""

def __init__(self, username=None, password=None):
"""
Open connection to central TASOC database.
If ``username`` or ``password`` is not provided or ``None``,
the user will be prompted for them.
Parameters:
username (string or None, optional): Username for AADC database.
password (string or None, optional): Password for AADC database.
"""

config = load_config()

if username is None:
username = config.get('database', 'username', fallback=None)
if username is None:
default_username = getpass.getuser()
username = input('Username [%s]: ' % default_username)
if username == '':
username = default_username

if password is None:
password = config.get('database', 'password', fallback=None)
if password is None:
password = getpass.getpass('Password: ')

# Open database connection:
self.conn = psql.connect('host=10.28.0.127 user=' + username + ' password=' + password + ' dbname=db_aadc')
self.cursor = self.conn.cursor(cursor_factory=DictCursor)

def close(self):
self.cursor.close()
self.conn.close()

def __enter__(self):
return self

def __exit__(self, *args, **kwargs):
self.close()

# --------------------------------------------------------------------------------------------------
class AADC_DB(object): # pragma: no cover
"""
Connection to the central TASOC database.
Attributes:
conn (`psycopg2.Connection` object): Connection to PostgreSQL database.
cursor (`psycopg2.Cursor` object): Cursor to use in database.
"""

def __init__(self, username=None, password=None):
"""
Open connection to central TASOC database.
If ``username`` or ``password`` is not provided or ``None``,
the user will be prompted for them.
Parameters:
username (string or None, optional): Username for AADC database.
password (string or None, optional): Password for AADC database.
"""

config = load_config()

if username is None:
username = config.get('database', 'username', fallback=None)
if username is None:
default_username = getpass.getuser()
username = input('Username [%s]: ' % default_username)
if username == '':
username = default_username

if password is None:
password = config.get('database', 'password', fallback=None)
if password is None:
password = getpass.getpass('Password: ')

# Open database connection:
self.conn = psql.connect('host=10.28.0.127 user=' + username + ' password=' + password + ' dbname=db_aadc')
self.cursor = self.conn.cursor(cursor_factory=DictCursor)

def close(self):
self.cursor.close()
self.conn.close()

def __enter__(self):
return self

def __exit__(self, *args, **kwargs):
self.close()
217 changes: 110 additions & 107 deletions flows/api/catalogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,113 +12,116 @@
from functools import lru_cache
from ..config import load_config

#--------------------------------------------------------------------------------------------------

# --------------------------------------------------------------------------------------------------
@lru_cache(maxsize=10)
def get_catalog(target, radius=None, output='table'):
"""
Parameters:
target (int or str):
radius (float, optional): Radius around target in degrees to return targets for.
outout (str, optional): Desired output format. Choises are 'table', 'dict', 'json'.
Default='table'.
Returns:
dict: Dictionary with three members:
- 'target': Information about target.
- 'references': Table with information about reference stars close to target.
- 'avoid': Table with stars close to target which should be avoided in FOV selection.
.. codeauthor:: Rasmus Handberg <rasmush@phys.au.dk>
"""

assert output in ('table', 'json', 'dict'), "Invalid output format"

# Get API token from config file:
config = load_config()
token = config.get('api', 'token', fallback=None)
if token is None:
raise RuntimeError("No API token has been defined")

#
r = requests.get('https://flows.phys.au.dk/api/reference_stars.php',
params={'target': target},
headers={'Authorization': 'Bearer ' + token})
r.raise_for_status()
jsn = r.json()

# Convert timestamps to actual Time objects:
jsn['target']['inserted'] = Time(jsn['target']['inserted'], scale='utc')
if jsn['target']['discovery_date'] is not None:
jsn['target']['discovery_date'] = Time(jsn['target']['discovery_date'], scale='utc')

if output in ('json', 'dict'):
return jsn

dict_tables = {}

tab = Table(
names=('targetid', 'target_name', 'target_status', 'ra', 'decl', 'redshift', 'redshift_error', 'discovery_mag', 'catalog_downloaded', 'pointing_model_created', 'inserted', 'discovery_date', 'project', 'host_galaxy', 'ztf_id', 'sntype'),
dtype=('int32', 'str', 'str', 'float64', 'float64', 'float32', 'float32', 'float32', 'bool', 'bool', 'object', 'object', 'str', 'str', 'str', 'str'),
rows=[jsn['target']])

tab['ra'].description = 'Right ascension'
tab['ra'].unit = u.deg
tab['decl'].description = 'Declination'
tab['decl'].unit = u.deg
dict_tables['target'] = tab

for table_name in ('references', 'avoid'):
tab = Table(
names=('starid', 'ra', 'decl', 'pm_ra', 'pm_dec', 'gaia_mag', 'gaia_bp_mag', 'gaia_rp_mag', 'gaia_variability', 'B_mag', 'V_mag', 'H_mag', 'J_mag', 'K_mag', 'u_mag', 'g_mag', 'r_mag', 'i_mag', 'z_mag', 'distance'),
dtype=('int64', 'float64', 'float64', 'float32', 'float32', 'float32', 'float32', 'float32', 'int32', 'float32', 'float32', 'float32', 'float32', 'float32', 'float32', 'float32', 'float32', 'float32', 'float32', 'float64'),
rows=jsn[table_name])

tab['starid'].description = 'Unique identifier in REFCAT2 catalog'
tab['ra'].description = 'Right ascension'
tab['ra'].unit = u.deg
tab['decl'].description = 'Declination'
tab['decl'].unit = u.deg
tab['pm_ra'].description = 'Proper motion in right ascension'
tab['pm_ra'].unit = u.mas/u.yr
tab['pm_dec'].description = 'Proper motion in declination'
tab['pm_dec'].unit = u.mas/u.yr
tab['distance'].description = 'Distance from object to target'
tab['distance'].unit = u.deg

tab['gaia_mag'].description = 'Gaia G magnitude'
tab['gaia_bp_mag'].description = 'Gaia Bp magnitude'
tab['gaia_rp_mag'].description = 'Gaia Rp magnitude'
tab['gaia_variability'].description = 'Gaia variability classification'
tab['B_mag'].description = 'Johnson B magnitude'
tab['V_mag'].description = 'Johnson V magnitude'
tab['H_mag'].description = '2MASS H magnitude'
tab['J_mag'].description = '2MASS J magnitude'
tab['K_mag'].description = '2MASS K magnitude'
tab['u_mag'].description = 'u magnitude'
tab['g_mag'].description = 'g magnitude'
tab['r_mag'].description = 'r magnitude'
tab['i_mag'].description = 'i magnitude'
tab['z_mag'].description = 'z magnitude'

# Add some meta-data to the table as well:
tab.meta['targetid'] = int(dict_tables['target']['targetid'])

dict_tables[table_name] = tab

return dict_tables

#--------------------------------------------------------------------------------------------------
"""
Parameters:
target (int or str):
radius (float, optional): Radius around target in degrees to return targets for.
outout (str, optional): Desired output format. Choises are 'table', 'dict', 'json'.
Default='table'.
Returns:
dict: Dictionary with three members:
- 'target': Information about target.
- 'references': Table with information about reference stars close to target.
- 'avoid': Table with stars close to target which should be avoided in FOV selection.
.. codeauthor:: Rasmus Handberg <rasmush@phys.au.dk>
"""

assert output in ('table', 'json', 'dict'), "Invalid output format"

# Get API token from config file:
config = load_config()
token = config.get('api', 'token', fallback=None)
if token is None:
raise RuntimeError("No API token has been defined")

#
r = requests.get('https://flows.phys.au.dk/api/reference_stars.php', params={'target': target},
headers={'Authorization': 'Bearer ' + token})
r.raise_for_status()
jsn = r.json()

# Convert timestamps to actual Time objects:
jsn['target']['inserted'] = Time(jsn['target']['inserted'], scale='utc')
if jsn['target']['discovery_date'] is not None:
jsn['target']['discovery_date'] = Time(jsn['target']['discovery_date'], scale='utc')

if output in ('json', 'dict'):
return jsn

dict_tables = {}

tab = Table(names=(
'targetid', 'target_name', 'target_status', 'ra', 'decl', 'redshift', 'redshift_error', 'discovery_mag',
'catalog_downloaded', 'pointing_model_created', 'inserted', 'discovery_date', 'project', 'host_galaxy',
'ztf_id', 'sntype'), dtype=(
'int32', 'str', 'str', 'float64', 'float64', 'float32', 'float32', 'float32', 'bool', 'bool', 'object',
'object', 'str', 'str', 'str', 'str'), rows=[jsn['target']])

tab['ra'].description = 'Right ascension'
tab['ra'].unit = u.deg
tab['decl'].description = 'Declination'
tab['decl'].unit = u.deg
dict_tables['target'] = tab

for table_name in ('references', 'avoid'):
tab = Table(names=(
'starid', 'ra', 'decl', 'pm_ra', 'pm_dec', 'gaia_mag', 'gaia_bp_mag', 'gaia_rp_mag', 'gaia_variability',
'B_mag', 'V_mag', 'H_mag', 'J_mag', 'K_mag', 'u_mag', 'g_mag', 'r_mag', 'i_mag', 'z_mag', 'distance'),
dtype=('int64', 'float64', 'float64', 'float32', 'float32', 'float32', 'float32', 'float32', 'int32',
'float32', 'float32', 'float32', 'float32', 'float32', 'float32', 'float32', 'float32', 'float32',
'float32', 'float64'), rows=jsn[table_name])

tab['starid'].description = 'Unique identifier in REFCAT2 catalog'
tab['ra'].description = 'Right ascension'
tab['ra'].unit = u.deg
tab['decl'].description = 'Declination'
tab['decl'].unit = u.deg
tab['pm_ra'].description = 'Proper motion in right ascension'
tab['pm_ra'].unit = u.mas / u.yr
tab['pm_dec'].description = 'Proper motion in declination'
tab['pm_dec'].unit = u.mas / u.yr
tab['distance'].description = 'Distance from object to target'
tab['distance'].unit = u.deg

tab['gaia_mag'].description = 'Gaia G magnitude'
tab['gaia_bp_mag'].description = 'Gaia Bp magnitude'
tab['gaia_rp_mag'].description = 'Gaia Rp magnitude'
tab['gaia_variability'].description = 'Gaia variability classification'
tab['B_mag'].description = 'Johnson B magnitude'
tab['V_mag'].description = 'Johnson V magnitude'
tab['H_mag'].description = '2MASS H magnitude'
tab['J_mag'].description = '2MASS J magnitude'
tab['K_mag'].description = '2MASS K magnitude'
tab['u_mag'].description = 'u magnitude'
tab['g_mag'].description = 'g magnitude'
tab['r_mag'].description = 'r magnitude'
tab['i_mag'].description = 'i magnitude'
tab['z_mag'].description = 'z magnitude'

# Add some meta-data to the table as well:
tab.meta['targetid'] = int(dict_tables['target']['targetid'])

dict_tables[table_name] = tab

return dict_tables


# --------------------------------------------------------------------------------------------------
def get_catalog_missing():

# Get API token from config file:
config = load_config()
token = config.get('api', 'token', fallback=None)
if token is None:
raise Exception("No API token has been defined")

#
r = requests.get('https://flows.phys.au.dk/api/catalog_missing.php',
headers={'Authorization': 'Bearer ' + token})
r.raise_for_status()
return r.json()
# Get API token from config file:
config = load_config()
token = config.get('api', 'token', fallback=None)
if token is None:
raise Exception("No API token has been defined")

#
r = requests.get('https://flows.phys.au.dk/api/catalog_missing.php', headers={'Authorization': 'Bearer ' + token})
r.raise_for_status()
return r.json()
Loading

0 comments on commit cbca2f3

Please sign in to comment.