LALPulsar  6.1.0.1-fe68b98
lalpulsar_knope.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 #
3 # lalpulsar_knope.py
4 #
5 # Copyright 2015
6 # Matthew Pitkin <matthew.pitkin@ligo.org>,
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 # MA 02110-1301, USA.
22 
23 # DAG generation code for running the known pulsar search pipeline
24 # The KNOwn Pulsar pipelinE - lalpulsar_knope
25 
26 from __future__ import print_function, division
27 
28 from lalpulsar import knope_utils as knope
29 import argparse
30 from six.moves.configparser import ConfigParser
31 import sys
32 import pickle
33 
34 description = """Setup a Condor DAG file to run the known pulsar search pipeline based on information given in config.ini.
35 The user must specify the configuration file for the script to run.
36 """
37 
38 parser = argparse.ArgumentParser(description=description)
39 
40 parser.add_argument(
41  "inifile", help="The configuation (.ini) file"
42 ) # the positional argument for the configuration file
43 parser.add_argument(
44  "--condor-submit",
45  action="store_true",
46  default=False,
47  help="Automatically submit the Condor DAG",
48 )
49 parser.add_argument(
50  "-r",
51  "--run-path",
52  dest="runpath",
53  default=None,
54  help="Set the directory to run the pipeline in (overwrites any value in the config.ini file)",
55 )
56 parser.add_argument(
57  "-p",
58  "--pulsar",
59  dest="pulsarlist",
60  action="append",
61  default=None,
62  help="A pulsar name to search for rather than all pulsars given in a parameter file directory (this can be specified multiple times to search for more than one pulsar).",
63 )
64 
65 opts = parser.parse_args()
66 
67 # check that at least the ini file has been given
68 inifile = opts.inifile
69 
70 # parser .ini file
71 try:
72  cp = ConfigParser()
73  cp.optionxform = str
74  cp.readfp(open(inifile))
75 except:
76  print("Error... problem parsing '%s' configuration file" % inifile, file=sys.stderr)
77  sys.exit(1)
78 
79 if opts.runpath is not None:
80  cp.set("analysis", "run_dir", opts.runpath)
81 
82 # Check if we're running in automated mode or not
83 try:
84  automated = cp.getboolean("analysis", "autonomous")
85 except:
86  automated = False
87 
88 # Check if configuration file says to submit the DAG
89 submitdag = opts.condor_submit
90 if not submitdag:
91  try:
92  submitdag = cp.getboolean("analysis", "submit_dag")
93  except:
94  submitdag = False
95 
96 # Create DAG from ConfigParser object
97 dag = knope.knopeDAG(cp, inifile, pulsarlist=opts.pulsarlist)
98 if dag.error_code != 0: # check for any errors that occurred
99  if dag.error_code in knope.KNOPE_ERROR.keys():
100  print(knope.KNOPE_ERROR[dag.error_code], file=sys.stderr)
101  else:
102  print("Error... unrecognised error code!", file=sys.stderr)
103 
104  # only exit if not in autonomous mode and the error message
105  if not automated or dag.error_code != knope.KNOPE_ERROR_NO_SEGMENTS:
106  sys.exit(dag.error_code)
107 
108 # write out DAG and submit files (unless in automated mode and no new segment files were found)
109 if not automated or dag.error_code != knope.KNOPE_ERROR_NO_SEGMENTS:
110  dag.write_sub_files()
111  dag.write_dag()
112 
113  print("Successfully created DAG file: '%s'" % dag.get_dag_file())
114 
115  if submitdag:
116  from subprocess import Popen
117 
118  x = Popen(["condor_submit_dag", dag.get_dag_file()])
119  x.wait()
120  if x.returncode == 0:
121  print("Submitted DAG file")
122  else:
123  print("Unable to submit DAG file")
124  else:
125  print("Run 'condor_submit_dag %s' to submit DAG file" % dag.get_dag_file())
126 
127  # output DAG class to pickle file if given
128  if cp.has_option("analysis", "pickle_file"):
129  try:
130  pfile = cp.get("analysis", "pickle_file")
131  fp = open(pfile, "wb")
132  pickle.dump(dag, fp)
133  fp.close()
134  except:
135  print("Warning... could not output analysis class to pickle file")
136 else:
137  print("No new science segments found in current time frame")
138 
139 sys.exit(0)