forked from vterron/lemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lemon
executable file
·136 lines (108 loc) · 4.86 KB
/
lemon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#! /usr/bin/env python
# Copyright (c) 2012 Victor Terron. All rights reserved.
# Institute of Astrophysics of Andalusia, IAA-CSIC
#
# This file is part of LEMON.
#
# LEMON is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Use import hooks, which are added to sys.meta_path, to enforce
# a minimum version of the modules defined in requirements.txt.
import check_versions
import atexit
import difflib
import logging
import os.path
import requests
import sys
# LEMON module
import git
API_QUERY_TIMEOUT = 2 # seconds
LEMON_COMMANDS = ['import', 'seeing', 'offsets', 'mosaic', 'astrometry',
'annuli', 'photometry', 'diffphot', 'juicer']
def show_help(name):
""" Help message, listing all commands, that looks like Git's """
print "usage: %s [--help] [--version] [--update] COMMAND [ARGS]" % name
print
print "The essential commands are:"
print " astrometry Calibrate the images astrometrically"
print " mosaic Assemble the images into a mosaic"
print " photometry Perform aperture photometry"
print " diffphot Generate light curves"
print " juicer LEMONdB browser and variability analyzer"
print
print "The auxiliary, not-always-necessary commands are:"
print " import Group the images of an observing campaign"
print " seeing Discard images with bad seeing or elongated"
print " annuli Find optimal parameters for photometry"
print
print "See '%s COMMAND' for more information on a specific command." % name
if __name__ == "__main__":
name = os.path.basename(sys.argv[0])
revision = git.get_git_revision()
if len(sys.argv) == 1 or '--help' in sys.argv:
show_help(name)
elif '--version' in sys.argv:
print revision
sys.exit(0)
elif '--update' in sys.argv:
retcode = git.git_update()
sys.exit(retcode)
else:
# Issue a warning if there is a newer version available on GitHub,
# letting the user know that the local installation can be updated by
# running the `lemon --update` command. Do nothing if the connection
# to the GitHub API times out after two seconds: better not to report
# on an outdated version than stopping us from getting things done.
try:
git.check_up_to_date(timeout = API_QUERY_TIMEOUT)
except requests.exceptions.Timeout:
pass
# Register logging.info() to be executed at termination, so that the
# Git revision number is logged at INFO level before our program
# exits. The reason why we do it on exit, instead of now, is because
# the logger level has not yet been adjusted. We need to wait until the
# main() method of the module that we are going to import counts the
# number of -v options and sets the level to WARNING, INFO or DEBUG.
msg = "LEMON Git revision: %s" % revision
atexit.register(logging.info, msg)
command = sys.argv[1]
args = sys.argv[2:]
if command not in LEMON_COMMANDS:
msg = "%s: '%s' is not a lemon command. See 'lemon --help'."
print msg % (name, command)
# Show suggestions, if any, when the command does not exist
matches = difflib.get_close_matches(command, LEMON_COMMANDS)
if matches:
print
print "Did you mean" ,
print len(matches) == 1 and "this?" or "one of these?"
for match in matches:
print ' ' * 8 + match
sys.exit(1)
elif command == 'juicer':
import juicer.main
kwargs = {}
if args:
kwargs['db_path'] = args[0]
juicer.main.main(**kwargs)
else:
# Add the name of the command to the script name so that the brief
# summary of the imported script options includes it (for example,
# "lemon photometry" instead of just "lemon".
sys.argv[0] = "%s %s" % (name, command)
# The 'import' statement cannot be used as the name of the module
# is only known at runtime. We need to manually invoke __import__
# to import the module by name and then run its main() function.
module = __import__(command)
module.main(args)