Skip to content

Instantly share code, notes, and snippets.

@vterron
Created August 4, 2015 11:29
Show Gist options
  • Save vterron/ce91a11b5d113a6914d4 to your computer and use it in GitHub Desktop.
Save vterron/ce91a11b5d113a6914d4 to your computer and use it in GitHub Desktop.
Export from a LEMONdB the photometric measurements of a series of astronomical objects
#! /usr/bin/env python
# Author: Victor Terron (c) 2015
# Email: `echo vt2rron1iaa32s | tr 132 @.e`
# License: GNU GPLv3
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals
description = """
Take a LEMONdB file and export the photometric measurements of a series of
astronomical objects whose coordinates are listed in the input text file. For
each right ascension and declination, we identify the closest object in the
LEMONdB and write its photometric measurements to a file in the output
directory."""
import argparse
import astropy.time
import os.path
import re
import sys
# Fix until LEMON is installed system-wide
LEMON_DIR = os.path.expanduser("~/lemon/")
sys.path.insert(0, LEMON_DIR)
# LEMON modules
import database
import methods
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=description)
parser.add_argument('db_path', metavar='LEMON_DB', type=str,
help="the LEMON database with the light curves")
parser.add_argument('coords_path', metavar='COORDINATES_FILE', type=str,
help="file with the right ascension and declination "
"of the astronomical objects, one per line and in "
"decimal degrees")
parser.add_argument('output_dir', metavar='OUTPUT_DIR', type=str,
help="output directory to which export the curves")
args = parser.parse_args()
if os.path.exists(args.output_dir):
msg = "Error: output directory {0} already exists.".format(args.output_dir)
sys.exit(msg)
else:
print("Creating output directory {0}...".format(args.output_dir), end='')
os.mkdir(args.output_dir)
print(" OK")
db = database.LEMONdB(args.db_path)
for ra, dec, _, _ in methods.load_coordinates(args.coords_path):
print("Input coordinates: {0} {1}".format(ra, dec))
star_id, distance = db.star_closest_to_world_coords(ra, dec)
star_info = db.get_star(star_id)
msg = "Closest star ID = {0} (distance = {1} degrees)"
print(msg.format(star_id, distance))
for pfilter in db.pfilters:
star_phot = db.get_photometry(star_id, pfilter)
if star_phot is not None:
basename = '{0}-{1}-{2}'.format(ra, dec, pfilter)
# Replace all runs of whitespace with a single dash
basename = re.sub(r"\s+", '-', basename)
path = os.path.join(args.output_dir, basename)
msg = 'Writing {0} photometry of star = {1} to {2} ...'
print(msg.format(pfilter, star_id, path), end='')
with open(path, 'wt') as fd:
fd.write("# JD\tDiff mag\tSNR\n")
for index in range(len(star_phot)):
utime = star_phot.time(index)
jd = astropy.time.Time(utime, format='unix').jd
mag = star_phot.mag(index)
snr = star_phot.snr(index)
fd.write('{0}\t{1}\t{2}\n'.format(jd, mag, snr))
print(' OK')
print('Wrote {0} photometric measurements'.format(len(star_phot)))
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment