#!/usr/bin/python

# Copyright (C) 2013 Michael Coughlin
#
# This program 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

"""Seismic summary information generator.

This script generates a nested web page and content for
reviewing seismicity at the sites and earthquake status.

Comments should be e-mailed to michael.coughlin@ligo.org.

"""


import os, sys, glob, optparse, shutil, warnings

if not os.getenv("DISPLAY", None):
    import matplotlib
    matplotlib.use("agg", warn=False)

#import lal.gpstime
import seismon.eqmon

__author__ = "Michael Coughlin <michael.coughlin@ligo.org>"
__version__ = 1.0
__date__    = "9/22/2013"

# =============================================================================
#
#                               DEFINITIONS
#
# =============================================================================

def parse_commandline():
    """@Parse the options given on the command-line.
    """
    parser = optparse.OptionParser(usage=__doc__,version=__version__)

    parser.add_option("-p", "--paramsFile", help="Seismon params file.", 
                      default ="/Seismon/Seismon/seismon/input/seismon_params_earthquakesInfo.txt")

    parser.add_option("-s", "--gpsStart", help="GPS Start Time.", default=1126709046,type=float)
    parser.add_option("-e", "--gpsEnd", help="GPS End Time.", default=1126787937,type=float)
    parser.add_option("-i", "--ifos", help="Ifos.", default="H1,L1,G1,V1,MIT")

    parser.add_option("--eventfilesType", help="Event files type.", default="private")
    parser.add_option("--doPlots",  action="store_true", default=False)
    parser.add_option("--doEarthquakes",  action="store_true", default=False)
    parser.add_option("--doPurge",  action="store_true", default=False)
    parser.add_option("--doTimeseries",  action="store_true", default=False)
    parser.add_option("--doEPICs",  action="store_true", default=False)
    parser.add_option("--epicsChannelList", help="Seismon params file.",
                      default ="/Seismon/Seismon/seismon/input/seismon_epics_channel_list.txt")
    parser.add_option("--channelList", help="Seismon params file.",
                      default ="/Seismon/Seismon/seismon/input/seismon_nds_channel_list.txt")

    parser.add_option("--doReadEPICs",  action="store_true", default=False)

    parser.add_option("-v", "--verbose", action="store_true", default=False,
                      help="Run verbosely. (Default: False)")

    opts, args = parser.parse_args()

    # show parameters
    if opts.verbose:
        print >> sys.stderr, ""
        print >> sys.stderr, "running pylal_seismon_run..."
        print >> sys.stderr, "version: %s"%__version__
        print >> sys.stderr, ""
        print >> sys.stderr, "***************** PARAMETERS ********************"
        for o in opts.__dict__.items():
          print >> sys.stderr, o[0]+":"
          print >> sys.stderr, o[1]
        print >> sys.stderr, ""

    return opts

def params_struct(opts):
    """@Creates seismon params structure
    @param opts
        seismon command line options
    """

    params = seismon.utils.readParamsFromFile(opts.paramsFile)
    params["gpsStart"] = opts.gpsStart
    params["gpsEnd"] = opts.gpsEnd
    params["ifos"] = opts.ifos

    params["eventfilesType"] = opts.eventfilesType

    params["doPlots"] = opts.doPlots
    params["doEarthquakes"] = opts.doEarthquakes
    params["doPurge"] = opts.doPurge
    params["doTimeseries"] = opts.doTimeseries
    params["doEPICs"] = opts.doEPICs
    params["epicsChannelList"] = opts.epicsChannelList
    params["channelList"] = opts.channelList
    params["doReadEPICs"] = opts.doReadEPICs

    return params

# =============================================================================
#
#                                    MAIN
#
# =============================================================================

warnings.filterwarnings("ignore")

# Parse command line
opts = parse_commandline()
params = params_struct(opts)
params["path"] = params["dirPath"] + "/" + "all" + "/" + params["runName"] + "/" + "%.0f"%params["gpsStart"] + "-" + "%.0f"%params["gpsEnd"]
params["currentpath"] = params["dirPath"] + "/" + "all" + "/" + params["runName"] + "/" + "current"

if params["doEarthquakes"]:
    print(params["path"])
    segmentlist = seismon.eqmon.run_earthquakes_info(params,[params["gpsStart"],params["gpsEnd"]])

if params["doPurge"]:
    params["path"] = params["dirPath"] + "/" + "all" + "/" + params["runName"] + "/"
    #sys_command = "find %s/* -type d -mtime +1 -exec rm -rf {} \;"%params["path"]
    sys_command = "find %s* -type d -mmin +60 -exec rm -rf {} \;"%params["path"]
    os.system(sys_command)

