Newer
Older
# LOFAR IMAGING PIPELINE
#
# setupsourcedb nodes recipe
# Marcel Loose, 2012
# loose@astron.nl
# ------------------------------------------------------------------------------

Marcel Loose
committed
from subprocess import CalledProcessError

Marcel Loose
committed
import errno
import os
import tempfile

Marcel Loose
committed
import shutil
import sys
from lofarpipe.support.lofarnode import LOFARnodeTCP
from lofarpipe.support.utilities import log_time
from lofarpipe.support.pipelinelogging import CatchLog4CPlus
from lofarpipe.support.utilities import catch_segfaults
class setupsourcedb(LOFARnodeTCP):
Wouter Klijn
committed
"""
Create the sourcedb at the supplied location
1. Create output directory if it does not yet exist.
2. Create sourcedb
3. validate performance, cleanup
"""

Marcel Loose
committed
def run(self, executable, catalogue, skydb, dbtype):
Wouter Klijn
committed
"""
Contains all functionality
"""
with log_time(self.logger):
Wouter Klijn
committed
# ****************************************************************
# 1. Create output directory if it does not yet exist.

Marcel Loose
committed
skydb_dir = os.path.dirname(skydb)
try:
os.makedirs(skydb_dir)
self.logger.debug("Created output directory %s" % skydb_dir)
except FileExistsError:
pass
Wouter Klijn
committed
# ****************************************************************
# 2 Remove any old sky database
# Create the sourcedb

Marcel Loose
committed
shutil.rmtree(skydb, ignore_errors=True)

Marcel Loose
committed
self.logger.info("Creating skymodel: %s" % (skydb))

Jan David Mol
committed
scratch_dir = tempfile.mkdtemp(suffix=".%s" % (os.path.basename(__file__),))
try:

Marcel Loose
committed
cmd = [executable,

Marcel Loose
committed
"in=%s" % catalogue,
"out=%s" % skydb,
"outtype=%s" % dbtype,

Marcel Loose
committed
"format=<",
"append=false"

Marcel Loose
committed
]
with CatchLog4CPlus(

Marcel Loose
committed
scratch_dir,
self.logger.name + "." + os.path.basename(skydb),
os.path.basename(executable)
) as logger:

Marcel Loose
committed
catch_segfaults(cmd, scratch_dir, None, logger)
Wouter Klijn
committed
# *****************************************************************
# 3. Validate performance and cleanup temp files
except CalledProcessError as err:
# For CalledProcessError isn't properly propagated by IPython
# Temporary workaround...

Marcel Loose
committed
self.logger.error(str(err))
return 1
finally:

Marcel Loose
committed
shutil.rmtree(scratch_dir)
return 0
if __name__ == "__main__":
# If invoked directly, parse command line arguments for logger information
# and pass the rest to the run() method defined above
# --------------------------------------------------------------------------
jobid, jobhost, jobport = sys.argv[1:4]
sys.exit(setupsourcedb(jobid, jobhost, jobport).run_with_stored_arguments())