#!/usr/bin/env python # encoding: utf-8 """ General wrapper for taking a MOD03 file and running PGE03 from it. Assumes all files are to be found at the locations outlined in conf.py FIXMEs: + Add support for Aqua (Terra hardcoded until we work out issues with destriping config file). Author: Nick Bearson, nickb@ssec.wisc.edu Copyright (c) 2012 University of Wisconsin SSEC. All rights reserved. """ # System imports: import os import errno import re import subprocess from optparse import OptionParser from datetime import datetime, timedelta import logging # Local imports: import conf import templates # --------------------------------------------------- log = logging.getLogger(__name__) # General argument-handling wrapper def handle_args(): parser = OptionParser() parser.add_option("-d", "--debug", dest="debug", action="store_true", default=False, help="run in debug mode") return parser.parse_args() def mkdir_p(path): try: os.makedirs(path) except Exception, e: if e.errno == errno.EEXIST: pass else: raise def m03name2datetime(m03file): base, tail = os.path.split(m03file) m = re.match(r'MOD03.A([0-9]{4})([0-9]{3}).([0-9]{2})([0-9]{2}).*', tail) year, jday, hour, minute = m.groups() date = datetime.strptime(("%s-01-01 %s:%s" % (year, hour, minute)), "%Y-%m-%d %H:%M") date+= timedelta(days=int(jday)) date-= timedelta(days=1) return date # FIXME: hacky for now because ftp sucks with wildcards def download_ancil(m03date): eng = conf.mk_remote(conf.REMOTE_ENG, m03date) gfs = conf.mk_remote(conf.REMOTE_GFS, m03date) nise = conf.mk_remote(conf.REMOTE_NISE, m03date) sst = conf.mk_sst_remote(conf.REMOTE_SST, m03date) cmd = "%s ftp.ssec.wisc.edu %s %s" % (conf.NCFTPGET, conf.ENG_DIR, eng) print cmd subprocess.call(cmd, shell=True) cmd = "%s ftp.ssec.wisc.edu %s %s" % (conf.NCFTPGET, conf.GFS_DIR, gfs) print cmd subprocess.call(cmd, shell=True) cmd = "%s ftp.ssec.wisc.edu %s %s" % (conf.NCFTPGET, conf.NISE_DIR, nise) print cmd subprocess.call(cmd, shell=True) cmd = "%s ftp.ssec.wisc.edu %s %s" % (conf.NCFTPGET, conf.SST_DIR, sst) print cmd subprocess.call(cmd, shell=True) def write_runtime_params(m03file): m03start = m03name2datetime(m03file) m03end = m03start + timedelta(minutes=5) now = datetime.utcnow() rp_dict = {} rp_dict["host"] = "modiscode" rp_dict["AorP"] = "A" rp_dict["startday"] = m03start.strftime("%Y-%m-%d") rp_dict["starttime"] = m03start.strftime("%H:%M:%S") rp_dict["endday"] = m03end.strftime("%Y-%m-%d") rp_dict["endtime"] = m03end.strftime("%H:%M:%S") rp_dict["startdayB"] = m03start.strftime("%Y-%j") rp_dict["rundayB"] = now.strftime("%Y-%j") rp_dict["runtime"] = now.strftime("%H:%M:%S") rp_data = templates.RUNTIME_PARAMS_TEMPLATE % (rp_dict) rp = open(templates.RUNTIME_PARAMS_NAME, 'w') rp.write(rp_data) rp.close() def write_lun(m03file): m03date = m03name2datetime(m03file) lun_dict = {} head, tail = os.path.split(m03file) lun_dict["m03_dirname"] = head lun_dict["m03_basename"] = tail m02qkmfile = conf.date2m02qkm(m03date) if os.path.exists(m02qkmfile): head, tail = os.path.split(m02qkmfile) else: head = "MISSING" tail = "MISSING" lun_dict["m02qkm_dirname"] = head lun_dict["m02qkm_basename"] = tail m021kmfile = conf.date2m021km(m03date) head, tail = os.path.split(m021kmfile) lun_dict["m021km_dirname"] = head lun_dict["m021km_basename"] = tail e_gfsfile, l_gfsfile = conf.date2gfs(m03date) head, tail = os.path.split(e_gfsfile) lun_dict["gfs_e_dirname"] = head lun_dict["gfs_e_basename"] = tail head, tail = os.path.split(l_gfsfile) lun_dict["gfs_l_dirname"] = head lun_dict["gfs_l_basename"] = tail sstfile = conf.date2sst(m03date) head, tail = os.path.split(sstfile) lun_dict["sst_dirname"] = head lun_dict["sst_basename"] = tail engfile = conf.date2eng(m03date) head, tail = os.path.split(engfile) lun_dict["eng_dirname"] = head lun_dict["eng_basename"] = tail nisefile = conf.date2nise(m03date) head, tail = os.path.split(nisefile) lun_dict["nise_dirname"] = head lun_dict["nise_basename"] = tail lun_data = templates.LUN_TEMPLATE % (lun_dict) lun = open(templates.LUN_NAME, 'w') lun.write(lun_data) lun.close() def call_PGE03(work_dir): reps = {} reps["work_dir"] = work_dir reps["MODAPSCONF"] = conf.MODAPSCONF reps["PERL"] = conf.PERL reps["PGE03_INSTALL"] = conf.PGE03_INSTALL shscript = """ source %(MODAPSCONF)s; setenv MODIS_STORE %(PGE03_INSTALL)s; cd %(work_dir)s; %(PERL)s $MODIS_STORE/PGE03/COMB/PGE03.pl 0 6.0.21 AM1M; """ cmd = shscript % (reps) p = subprocess.Popen(cmd, shell=True, executable="/bin/csh") # FIXME: should put script output into the logger p.wait() def run_PGE03(m03file): m03date = m03name2datetime(m03file) work_dir = conf.date2workdir(m03date) mkdir_p(work_dir) os.chdir(work_dir) download_ancil(m03date) write_runtime_params(m03file) write_lun(m03file) call_PGE03(work_dir) if __name__ == "__main__": # logging.basicConfig(level=d.LOG_LEVEL, format=d.LOG_FORMAT) (options, args) = handle_args() m03files = args for m03file in m03files: run_PGE03(m03file)