#! /usr/bin/env python

##@namespace scripts.exhwrf_wrfout_archive
# Archives native wrfout files to tape via the "htar" command.
#
# To enable this script, you must set the wrfout option in the [archive]
# configuration section:
# @code[.conf]
# [archive]
# wrfout=hpss:/NCEPDEV/emc-hwrf/2year/Jane.Doe/wrfout/{out_prefix}.tar
# @endcode
#
# The extension must be .tar, and the only archiving method supported is
# hpss:/ (which uses htar)

import os, sys
import produtil.setup, produtil.log, produtil.run, produtil.cd
import hwrf.numerics
import hwrf_expt

from produtil.log import jlogger
from produtil.run import batchexe, checkrun, run

def main():
    hwrf_expt.init_module()
    conf=hwrf_expt.conf
    if not conf.getstr('archive','wrfout',''):
        jlogger.info('No wrfout option in [archive] section.  Will not make wrfout archive.')
        sys.exit(0)

    logger=conf.log()

    files=list()
    dt=hwrf.numerics.to_timedelta('6:00:00')
    t0=conf.cycle
    wrf=hwrf_expt.runwrf.wrf()
    with produtil.cd.NamedDir(hwrf_expt.runwrf.location):
        for i in range(22):
            for dom in wrf:
                t=t0+dt*i
                out=dom.get_output('auxhist3',t)
                if out is None:
                    out=dom.get_output('history',t)
                if out is None:
                    out=dom.get_output('auxhist2',t)
                if out is None:
                    logger.error('%s: could not determine wrfout for '
                                 'domain %s'%(t.strftime('%Y%m%d%H'),
                                              str(dom)))
                if out is not None:
                    compressed=out.path()+'.nc4'
                    if produtil.fileop.isnonempty(compressed):
                        logger.info('%s: use compressed file (non-empty)'%(
                            compressed,))
                        files.append(compressed)
                    else:
                        if not os.path.exists(out.path()):
                            logger.error('%s: does not exist'%(out.path(),))
                        if not produtil.fileop.isnonempty(out.path(),):
                            logger.error('%s: is empty'%(out.path(),))
                        files.append(out.path())

        thearchive=conf.timestrinterp('archive','{wrfout}',0)
        if thearchive[0:5]!='hpss:':
            logger.error('The wrfout archive path must begin with "hpss:": '+
                         thearchive)
            sys.exit(1)
        thearchive=thearchive[5:]
        adir=os.path.dirname(thearchive)
        mkdir=batchexe(conf.getexe('hsi'))['-P','mkdir','-p',adir]
        run(mkdir,logger=logger)
        cmd=batchexe(conf.getexe('htar'))['-vcpf',thearchive][files]
        checkrun(cmd,logger=logger)

if __name__=='__main__':
    try:
        produtil.setup.setup()
        jlogger.info("HWRF wrfout_archive job starting")
        main()
        jlogger.info("HWRF wrfout_archive job completed")
    except Exception as e:
        jlogger.critical('HWRF rundir archive is aborting: '+str(e),
                         exc_info=True)
        sys.exit(2)