From 5a215c01e92c29bd6150a1a6933492e5028c9272 Mon Sep 17 00:00:00 2001 From: Pierros Papadeas Date: Wed, 25 Apr 2018 12:37:57 +0300 Subject: [PATCH] Move arg parsing to arparse for acquire.py --- acquire.py | 48 +++++++++++++++++++++++++++++++++--------------- requirements.txt | 1 + utils.py | 3 +-- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/acquire.py b/acquire.py index 27c66ad..7d82817 100644 --- a/acquire.py +++ b/acquire.py @@ -14,6 +14,8 @@ import astropy.units as u from utils import get_sunset_and_sunrise import logging import configparser +import argparse + # Capture images def capture(buf, z1, t1, z2, t2, device, nx, ny, nz, tend): @@ -134,7 +136,7 @@ def compress(buf, z1, t1, z2, t2, nx, ny, nz, tend, path): # Write fits file hdu = fits.PrimaryHDU(data=np.array([zavg, zstd, zmax, znum]), header=hdr) - hdu.writeto(os.path.join(path,fname)) + hdu.writeto(os.path.join(path, fname)) logging.info("Compressed %s" % fname) # Exit on end of capture @@ -147,20 +149,35 @@ def compress(buf, z1, t1, z2, t2, nx, ny, nz, tend, path): # Main function if __name__ == '__main__': - # Read commandline options (TODO; move to argparse?) - cfgfile = sys.argv[1] - if sys.argv[2] == "test": + + # Read commandline options + conf_parser = argparse.ArgumentParser(description='Capture and compress' + + ' live video frames.') + conf_parser.add_argument("-c", "--conf_file", + help="Specify configuration file. If no file" + + " is specified 'configuration.ini' is used.", + metavar="FILE") + conf_parser.add_argument('--test', action='store_true', + help='Testing mode - Start capturing immediately') + + 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') + + # Testing mode + if args.test: testing = True else: testing = False - # Confiration Parsing - cfg = configparser.ConfigParser(inline_comment_prefixes=('#', ';')) - cfg.read(cfgfile) - # Get device id devid = cfg.getint('Camera', 'device_id') - + # Current time tnow = Time.now() @@ -168,18 +185,19 @@ if __name__ == '__main__': obsid = time.strftime("%Y%m%d_%H%M%S", time.gmtime())+"_%d" % devid # Generate directory - path = os.path.join(cfg.get('Common', 'observations_path'),obsid) + path = os.path.join(cfg.get('Common', 'observations_path'), obsid) os.makedirs(path) # Setup logging - logging.basicConfig(filename=os.path.join(path,"acquire.log"), level=logging.DEBUG) + logging.basicConfig(filename=os.path.join(path, "acquire.log"), + level=logging.DEBUG) # Set location 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) - if testing == False: + if not testing: # Reference altitude refalt = -6.0 * u.deg @@ -198,7 +216,8 @@ if __name__ == '__main__': tend = trise elif (trise >= tset): dt = np.floor((tset-tnow).to(u.s).value) - logging.info("The sun is above the horizon. Sunset at %s." % tset.isot) + logging.info("The sun is above the horizon. Sunset at %s." + % tset.isot) logging.info("Waiting %.0f seconds." % dt) tend = trise try: @@ -206,8 +225,7 @@ if __name__ == '__main__': except KeyboardInterrupt: sys.exit() else: - tend=tnow+31.0*u.s - + tend = tnow+31.0*u.s logging.info("Starting data acquisition.") logging.info("Acquisition will end at "+tend.isot) diff --git a/requirements.txt b/requirements.txt index 5212d07..f1afd48 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ astropy==3.0.1 numpy==1.14.2 opencv-python==3.4.0.12 scipy==1.0.1 +git+https://github.com/cbassa/ppgplot.git@master diff --git a/utils.py b/utils.py index 1158077..a00010f 100644 --- a/utils.py +++ b/utils.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # Common Utilities -from astropy.coordinates import SkyCoord, EarthLocation, AltAz, FK5, get_sun +from astropy.coordinates import SkyCoord, FK5, get_sun from astropy.time import Time import astropy.units as u import numpy as np @@ -129,4 +129,3 @@ def get_sunset_and_sunrise(tnow, loc, refalt): trise = Time(mjd0+mrise.value, format='mjd', scale='utc') return "sun rises and sets", tset, trise -