######################################################################
#
# Copyright (C) 2013
# Associated Universities, Inc. Washington DC, USA,
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Library General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This library 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 Library General Public
# License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this library; if not, write to the Free Software Foundation,
# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
#
# Correspondence concerning VLA Pipelines should be addressed as follows:
#    Please register and submit helpdesk tickets via: https://help.nrao.edu
#    Postal address:
#              National Radio Astronomy Observatory
#              VLA Pipeline Support Office
#              PO Box O
#              Socorro, NM,  USA
#
######################################################################

'''
Script that Restores all variables needed by the pipeline into the global namespace

'''

import shelve
import sys

#This is the default time-stamped casa log file, in case we
#    need to return to it at any point in the script
log_dir='logs'
if not os.path.exists(log_dir):
    os.makedirs(log_dir)

maincasalog = casalogger.func_globals['thelogfile']

def logprint(msg, logfileout=maincasalog):
    print (msg)
    casalog.setlogfile(logfileout)
    casalog.post(msg)
    casalog.setlogfile(maincasalog)
    casalog.post(msg)
    return

#Create timing profile list and file if they don't already exist
if 'time_list' not in globals():
    time_list = []

timing_file='logs/timing.log'

if not os.path.exists(timing_file):
    timelog=open(timing_file,'w')
else:
    timelog=open(timing_file,'a')

def runtiming(pipestate, status):
    '''Determine profile for a given state/stage of the pipeline
    '''
    time_list.append({'pipestate':pipestate, 'time':time.time(), 'status':status})

    if (status == "end"):
        timelog=open(timing_file,'a')
        timelog.write(pipestate+': '+str(time_list[-1]['time'] - time_list[-2]['time'])+' sec \n')
        timelog.flush()
        timelog.close()
        #with open(maincasalog, 'a') as casalogfile:
        #    tempfile = open('logs/'+pipestate+'.log','r')
        #    casalogfile.write(tempfile.read())
        #    tempfile.close()
        #casalogfile.close()


    return time_list


def pipeline_restore(shelf_filename='pipeline_shelf.restore'):
    '''Restore the state of the pipeline from shelf file
    '''
    if os.path.exists(shelf_filename):
        try:
            pipe_shelf = shelve.open(shelf_filename)
        except Exception, e:
            logprint ("Restore point does not exist: "+str(e))

        for key in pipe_shelf:
            try:
                globals()[key] = pipe_shelf[key]
                key_status = True
            except:
                key_status = False

        pipe_shelf.close()

#Restore all variables to the global namespace and then run 'startup'
#  to load all functions and modules needed for the pipeline
try:
    print 'Restoring'
    pipeline_restore()
    print '1'
    maincasalog = casalogger.func_globals['thelogfile']
    print '2'
    execfile(pipepath+'EVLA_pipe_startup.py')
    print 'Ready!'


except Exception, e:
    print 'ERROR!!' + str(e)
    logprint ("Exiting script: "+str(e))
