# This is a top-level module file to handle datetime operations # for the MPE workflow. # Logan Karsten # National Center for Atmospheric Research # Research Applications Laboratory # karsten@ucar.edu # 303-497-2693 import datetime import ioMod def initDatesMpe(statusMeta): """ Generic function to initialize the datetime variables within the status object. This information will be utilized (and updated) throughout the workflow as files are generated/completed, etc. """ # Store the current time (in UTC). statusMeta.dReal = datetime.datetime.utcnow() yrReal = statusMeta.dReal.year monthReal = statusMeta.dReal.month dayReal = statusMeta.dReal.day # Find the closest time step that corresponds to the end of the latest accumulation # duration period. if statusMeta.accDuration == 24: hourClosest = 0 else: hourClosest = int(int(statusMeta.dReal.hour/statusMeta.accDuration)*statusMeta.accDuration) statusMeta.dEnd = datetime.datetime(year=yrReal,month=monthReal,day=dayReal,hour=hourClosest) statusMeta.dInit = statusMeta.dEnd - datetime.timedelta(seconds=statusMeta.numHoursBack*3600.0) # Set the dCurr variable in the statusMeta object to the beginning date. # We will loop forward from there. statusMeta.dCurr = statusMeta.dInit def initOptDatesMpe(statusMeta,optDateEnd,optNumHoursBack): """ Function to reset the dates in the information calculated from the mpe.config default settings for datetime variables. This is for when the user passes an optional number of hours to look back, or date to begin the look back that is different from the default. This is mostly for when the user wants to process old/retrospective data. """ if optNumHoursBack is not None: # First do some sanity checking on what was passed in by the user. statusMeta.numHoursBack = optNumHoursBack try: ioMod.checkStaticMeta(statusMeta) except: raise Exception() if optDateEnd is not None: if int(optDateEnd.strftime('%H')) != 0: if int(optDateEnd.strftime('%H')) % statusMeta.accDuration != 0: statusMeta.errMsg = "ERROR: Specified optional hour is not equal divisor of accumulation " + \ " duration specified in mpe.config" raise Exception() if optDateEnd is not None: # Re-calculate datetime variables. yrEnd = int(optDateEnd.strftime('%Y')) monthEnd = int(optDateEnd.strftime('%m')) dayEnd = int(optDateEnd.strftime('%d')) hrEnd = int(optDateEnd.strftime('%H')) statusMeta.dEnd = datetime.datetime(year=yrEnd,month=monthEnd,day=dayEnd,hour=hrEnd) statusMeta.dInit = statusMeta.dEnd - datetime.timedelta(seconds=statusMeta.numHoursBack*3600.0) # Set the dCurr variable in the statusMeta object to the beginning date. # We will loop forward from there. statusMeta.dCurr = statusMeta.dInit