2019-04-28 03:01:31 -06:00
|
|
|
#!/usr/bin/env python3
|
2018-05-01 01:35:34 -06:00
|
|
|
from __future__ import print_function
|
|
|
|
import glob
|
|
|
|
import numpy as np
|
2018-05-06 08:45:51 -06:00
|
|
|
from stvid.stio import fourframe
|
|
|
|
from stvid.stars import generate_star_catalog
|
2019-08-02 14:15:24 -06:00
|
|
|
from stvid.stars import store_calibration
|
2018-05-06 08:45:51 -06:00
|
|
|
from stvid.astrometry import calibrate_from_reference
|
2019-05-04 15:05:24 -06:00
|
|
|
from stvid.astrometry import is_calibrated
|
2019-07-28 05:39:21 -06:00
|
|
|
from stvid.astrometry import generate_reference_with_anet
|
2018-05-07 14:19:05 -06:00
|
|
|
from stvid.satellite import generate_satellite_predictions
|
|
|
|
from stvid.satellite import find_hough3d_lines
|
2019-04-22 08:42:43 -06:00
|
|
|
from stvid.extract import extract_tracks
|
2018-05-01 01:35:34 -06:00
|
|
|
import astropy.units as u
|
|
|
|
from astropy.utils.exceptions import AstropyWarning
|
2018-05-06 08:45:51 -06:00
|
|
|
from astropy.coordinates import EarthLocation
|
2018-05-01 01:35:34 -06:00
|
|
|
import warnings
|
2018-05-06 08:45:51 -06:00
|
|
|
import configparser
|
|
|
|
import argparse
|
2018-05-07 08:24:01 -06:00
|
|
|
import os
|
2019-04-22 08:42:43 -06:00
|
|
|
from termcolor import colored
|
2019-04-27 04:38:27 -06:00
|
|
|
import time
|
|
|
|
import shutil
|
2019-07-08 07:07:26 -06:00
|
|
|
import sys
|
2018-05-01 01:35:34 -06:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2018-05-06 08:45:51 -06:00
|
|
|
# Read commandline options
|
|
|
|
conf_parser = argparse.ArgumentParser(description='Process captured' +
|
2019-05-12 08:11:15 -06:00
|
|
|
' video frames.')
|
|
|
|
conf_parser.add_argument("-c",
|
|
|
|
"--conf_file",
|
2018-05-06 08:45:51 -06:00
|
|
|
help="Specify configuration file. If no file" +
|
|
|
|
" is specified 'configuration.ini' is used.",
|
|
|
|
metavar="FILE")
|
2019-05-12 08:11:15 -06:00
|
|
|
conf_parser.add_argument("-d",
|
|
|
|
"--directory",
|
2018-05-06 08:45:51 -06:00
|
|
|
help="Specify directory of observations. If no" +
|
|
|
|
" directory is specified parent will be used.",
|
2019-05-12 08:11:15 -06:00
|
|
|
metavar='DIR',
|
|
|
|
dest='file_dir',
|
|
|
|
default=".")
|
2018-05-06 08:45:51 -06:00
|
|
|
|
|
|
|
args = conf_parser.parse_args()
|
|
|
|
|
|
|
|
# Process commandline options and parse configuration
|
|
|
|
cfg = configparser.ConfigParser(inline_comment_prefixes=('#', ';'))
|
|
|
|
if args.conf_file:
|
|
|
|
cfg.read([args.conf_file])
|
|
|
|
else:
|
|
|
|
cfg.read('configuration.ini')
|
|
|
|
|
2018-05-01 01:35:34 -06:00
|
|
|
# Set warnings
|
|
|
|
warnings.filterwarnings("ignore", category=UserWarning, append=True)
|
|
|
|
warnings.simplefilter("ignore", AstropyWarning)
|
|
|
|
|
|
|
|
# Set location
|
2019-05-12 08:11:15 -06:00
|
|
|
loc = EarthLocation(lat=cfg.getfloat('Common', 'observer_lat') * u.deg,
|
|
|
|
lon=cfg.getfloat('Common', 'observer_lon') * u.deg,
|
|
|
|
height=cfg.getfloat('Common', 'observer_el') * u.m)
|
2018-05-01 01:35:34 -06:00
|
|
|
|
2019-04-22 08:42:43 -06:00
|
|
|
# Extract settings
|
|
|
|
# Minimum predicted velocity (pixels/s)
|
2019-07-30 13:28:06 -06:00
|
|
|
drdtmin = 5.0
|
2019-04-22 08:42:43 -06:00
|
|
|
|
|
|
|
# Track selection region around prediction (pixels)
|
|
|
|
trkrmin = 10.0
|
|
|
|
|
|
|
|
# Track selection sigma
|
|
|
|
trksig = 5.0
|
|
|
|
|
|
|
|
# Minimum track points
|
|
|
|
ntrkmin = 10
|
|
|
|
|
2019-04-27 04:38:27 -06:00
|
|
|
# Move to processing directory
|
|
|
|
os.chdir(args.file_dir)
|
2019-05-04 15:05:24 -06:00
|
|
|
|
2019-04-27 04:38:27 -06:00
|
|
|
# Statistics file
|
|
|
|
fstat = open("imgstat.csv", "w")
|
|
|
|
fstat.write("fname,mjd,ra,de,rmsx,rmsy,mean,std,nstars,nused\n")
|
2019-05-12 08:11:15 -06:00
|
|
|
|
2019-04-22 08:42:43 -06:00
|
|
|
# Create output dirs
|
2019-08-02 13:53:55 -06:00
|
|
|
file_dir = os.path.abspath(args.file_dir.rstrip("/"))
|
2019-07-28 02:43:57 -06:00
|
|
|
root_dir = os.path.split(file_dir)[0]
|
|
|
|
results_dir = os.path.join(cfg.get('Common', 'results_path'),
|
|
|
|
os.path.split(root_dir)[-1])
|
2019-07-28 03:44:06 -06:00
|
|
|
processed_dir = os.path.join(file_dir, "processed")
|
2019-07-28 02:43:57 -06:00
|
|
|
if not os.path.exists(os.path.join(results_dir, "classfd")):
|
|
|
|
os.makedirs(os.path.join(results_dir, "classfd"))
|
|
|
|
if not os.path.exists(os.path.join(results_dir, "catalog")):
|
|
|
|
os.makedirs(os.path.join(results_dir, "catalog"))
|
|
|
|
if not os.path.exists(os.path.join(results_dir, "unid")):
|
|
|
|
os.makedirs(os.path.join(results_dir, "unid"))
|
|
|
|
if not os.path.exists(os.path.join(results_dir, "not_seen")):
|
|
|
|
os.makedirs(os.path.join(results_dir, "not_seen"))
|
|
|
|
if not os.path.exists(processed_dir):
|
|
|
|
os.makedirs(processed_dir)
|
2019-07-28 08:30:09 -06:00
|
|
|
|
2019-04-27 04:38:27 -06:00
|
|
|
# Forever loop
|
|
|
|
while True:
|
|
|
|
# Get files
|
|
|
|
fnames = sorted(glob.glob("2*.fits"))
|
2019-05-12 08:11:15 -06:00
|
|
|
|
2019-07-30 13:28:06 -06:00
|
|
|
# Create reference calibration file
|
2019-08-02 14:15:24 -06:00
|
|
|
if not os.path.exists("test.fits"):
|
2019-07-30 13:28:06 -06:00
|
|
|
solved = False
|
|
|
|
# Loop over files to find a suitable calibration file
|
|
|
|
for fname in fnames:
|
|
|
|
# Generate star catalog
|
|
|
|
pix_catalog = generate_star_catalog(fname)
|
2019-08-09 11:04:33 -06:00
|
|
|
|
2019-07-30 13:28:06 -06:00
|
|
|
# Solve
|
2019-08-02 13:53:55 -06:00
|
|
|
if pix_catalog.nstars > 10:
|
2019-07-30 13:28:06 -06:00
|
|
|
print(colored("Computing astrometric calibration for %s" % fname, "yellow"))
|
|
|
|
solved = generate_reference_with_anet(fname, "")
|
|
|
|
|
|
|
|
# Break when solved
|
|
|
|
if solved:
|
|
|
|
break
|
2019-08-09 11:04:33 -06:00
|
|
|
|
2019-04-27 04:38:27 -06:00
|
|
|
# Loop over files
|
|
|
|
for fname in fnames:
|
|
|
|
# Generate star catalog
|
2019-07-30 13:28:06 -06:00
|
|
|
if not os.path.exists(fname + ".cat"):
|
|
|
|
pix_catalog = generate_star_catalog(fname)
|
2019-04-27 04:38:27 -06:00
|
|
|
|
2019-07-30 13:28:06 -06:00
|
|
|
# Calibrate from reference
|
2019-05-12 08:11:15 -06:00
|
|
|
calibrate_from_reference(fname, "test.fits", pix_catalog)
|
2019-04-27 04:38:27 -06:00
|
|
|
|
2019-08-02 14:15:24 -06:00
|
|
|
# Store calibration
|
|
|
|
store_calibration(pix_catalog, fname + ".cal")
|
2019-08-09 11:04:33 -06:00
|
|
|
|
2019-04-27 04:38:27 -06:00
|
|
|
# Generate satellite predictions
|
|
|
|
generate_satellite_predictions(fname)
|
2019-05-12 08:11:15 -06:00
|
|
|
|
2019-04-27 04:38:27 -06:00
|
|
|
# Detect lines with 3D Hough transform
|
|
|
|
ids = find_hough3d_lines(fname)
|
|
|
|
|
2019-07-08 06:59:34 -06:00
|
|
|
# Get properties
|
|
|
|
ff = fourframe(fname)
|
2019-07-28 08:30:09 -06:00
|
|
|
|
2019-04-27 04:38:27 -06:00
|
|
|
# Extract tracks
|
2019-07-08 06:59:34 -06:00
|
|
|
if is_calibrated(ff):
|
2019-07-28 02:43:57 -06:00
|
|
|
extract_tracks(fname, trkrmin, drdtmin, trksig, ntrkmin, root_dir, results_dir)
|
2019-07-28 08:30:09 -06:00
|
|
|
|
2019-04-27 04:38:27 -06:00
|
|
|
# Stars available and used
|
|
|
|
nused = np.sum(pix_catalog.flag == 1)
|
|
|
|
nstars = pix_catalog.nstars
|
|
|
|
|
|
|
|
# Write output
|
2019-05-12 08:11:15 -06:00
|
|
|
output = "%s %10.6f %10.6f %4d/%4d %5.1f %5.1f %6.2f +- %6.2f" % (
|
|
|
|
ff.fname, ff.crval[0], ff.crval[1], nused, nstars,
|
|
|
|
3600.0 * ff.crres[0], 3600.0 * ff.crres[1], np.mean(
|
|
|
|
ff.zavg), np.std(ff.zavg))
|
2019-05-04 15:05:24 -06:00
|
|
|
if is_calibrated(ff):
|
|
|
|
print(colored(output, "green"))
|
|
|
|
else:
|
|
|
|
print(colored(output, "red"))
|
2019-05-12 08:11:15 -06:00
|
|
|
fstat.write(
|
|
|
|
("%s,%.8lf,%.6f,%.6f,%.3f,%.3f,%.3f," + "%.3f,%d,%d\n") %
|
|
|
|
(ff.fname, ff.mjd, ff.crval[0], ff.crval[1],
|
|
|
|
3600 * ff.crres[0], 3600 * ff.crres[1], np.mean(
|
|
|
|
ff.zavg), np.std(ff.zavg), nstars, nused))
|
2019-04-27 04:38:27 -06:00
|
|
|
|
|
|
|
# Move processed files
|
2019-07-28 02:43:57 -06:00
|
|
|
shutil.move(fname, os.path.join(processed_dir, fname))
|
|
|
|
shutil.move(fname + ".png", os.path.join(processed_dir, fname + ".png"))
|
|
|
|
shutil.move(fname + ".id", os.path.join(processed_dir, fname + ".id"))
|
|
|
|
shutil.move(fname + ".cat", os.path.join(processed_dir, fname + ".cat"))
|
2019-05-12 08:11:15 -06:00
|
|
|
|
2019-04-27 04:38:27 -06:00
|
|
|
# Sleep
|
2019-07-08 07:07:26 -06:00
|
|
|
try:
|
|
|
|
time.sleep(10)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit()
|
2019-05-12 08:11:15 -06:00
|
|
|
|
2019-04-27 04:38:27 -06:00
|
|
|
# Close files
|
2018-05-01 01:35:34 -06:00
|
|
|
fstat.close()
|