Skip to content

Commit

Permalink
WIP: generate visibility plots
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Jensen committed Oct 18, 2024
1 parent 9890e03 commit d45af98
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
4 changes: 2 additions & 2 deletions flows/aadc_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ def __init__(self, username=None, password=None, host=None, dbname=None):
dbname = config.get('database', 'dbname', fallback=os.environ.get("AUDBName", None))
if dbname is None:
default_dbname = 'adastra'
dbname = input('Schema [%s]: ' % default_dbname)
dbname = input('Database [%s]: ' % default_dbname)
if dbname == '':
dbname = default_dbname

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

def close(self):
Expand Down
16 changes: 16 additions & 0 deletions flows/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from numpy.typing import NDArray
from tendrils import api

from flows.aadc_db import AADC_DB


@dataclass
class Target:
Expand Down Expand Up @@ -75,3 +77,17 @@ def from_tid(cls, target_id: int) -> 'Target':
return cls(
ra=target_pars['ra'], dec=target_pars['decl'],
name=target_pars['target_name'], id=target_pars['targetid'])

@classmethod
def from_tname(cls, target_name: str) -> 'Target':
# TODO: better to define a function 'api.get_target(target_name)', avoiding db connection...

"""
Create target from target name.
"""
with AADC_DB() as db:
# SELECT fileid,path,targetid FROM flows.files LEFT JOIN flows.photometry_details d ON d.fileid_phot=files.fileid WHERE files.datatype=2 AND d.fileid_phot IS NULL;
db.cursor.execute("select targetid from flows.targets WHERE target_name=%s;", [target_name])
for row in db.cursor.fetchall():
target_id = row['targetid']
return cls.from_tid(target_id)
4 changes: 3 additions & 1 deletion flows/visibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def visibility(target: Target, siteid: Optional[int] = None, date=None, output=N
if date is None:
date = datetime.utcnow()
elif isinstance(date, str):
date = datetime.strptime(date, '%Y-%m-%d')
date = datetime.strptime(date, '%Y%m%d')

# Coordinates of object:
obj = SkyCoord(ra=target.ra, dec=target.dec, unit='deg', frame='icrs')
Expand All @@ -69,6 +69,8 @@ def visibility(target: Target, siteid: Optional[int] = None, date=None, output=N
plotpath = os.path.join(output, "visibility_%s_%s_site%02d.png" % (
target.name, date.strftime('%Y%m%d'), site['siteid']))
else:
if not os.path.exists(os.path.dirname(output)):
os.makedirs(os.path.dirname(output))
plotpath = output
logger.debug("Will save visibility plot to '%s'", plotpath)

Expand Down
4 changes: 3 additions & 1 deletion run_visibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import argparse
from flows.plots import plots_interactive
import flows
from flows.target import Target

if __name__ == '__main__':
# Parse command line arguments:
Expand All @@ -20,4 +21,5 @@
if args.output is None:
plots_interactive()

flows.visibility(target=args.target, siteid=args.site, date=args.date, output=args.output)
t = Target.from_tname(args.target)
flows.visibility(target=t, siteid=args.site, date=args.date, output=args.output)

0 comments on commit d45af98

Please sign in to comment.