def regridIfZeroHr(configFile, fileType, fname):
   """If it is a 0 hour forecast (RAP or HRRR) regrid in a special way
   Parameters
   ----------
   configFile : str
      configuration file with all settings
   fileType: str
      HRRR, RAP, ... string
   fname: str
      name of file to regrid and downscale, with yyyymmdd parent dir

   Returns
   -------
   None
   """
   # check for 0 hour by creating a DataFile and checking forecast hour
   try:
      f = df.DataFile(fname[0:8], fname[9:], fileType)
   except FilenameMatchError as fe:
      WhfLog.debug("Cannot check for 0 hour data due to %s", fe)
      raise
   except InvalidArgumentError as ie:
      WhfLog.debug("Cannot check for 0 hour data due to %s", ie)
      raise
   if (f._time._forecastHour == 0):
      WhfLog.setConfigType('AA')
      WhfLog.debug("SPECIAL 0 hour case %s", fname[9:0])
      aaf.forcing(configFile, 'regrid', fileType, fname[9:])
      WhfLog.setConfigType('Short')
def regridIfZeroHr(configFile, fileType, fname):
   """If it is a 0 hour forecast (RAP or HRRR) regrid in a special way
   Parameters
   ----------
   configFile : str
   configuration file with all settings
   fileType: str
   HRRR, RAP, ... string
   fname: str
   name of file to regrid and downscale, with yyyymmdd parent dir

   Returns
   -------
   None
   """
   # check for 0 hour by creating a DataFile and checking forecast hour
   try:
      f = df.DataFile(fname[0:8], fname[9:], fileType)
   except FilenameMatchError as fe:
      WhfLog.debug("Cannot check for 0 hour data due to %s", fe)
      raise
   except InvalidArgumentError as ie:
      WhfLog.debug("Cannot check for 0 hour data due to %s", ie)
      raise
   if (f._time._forecastHour == 0):
      WhfLog.setConfigType('AA')
      WhfLog.debug("SPECIAL 0 hour case %s", fname[9:0])
      aaf.forcing(configFile, 'regrid', fileType, fname[9:])
      WhfLog.setConfigType('Short')
def do_regrid(config_file, dir_base, prod, data_files, is_yellowstone):
    """Do the regridding and downscaling of the product"""
    
    for file in data_files:
        # Use only the filename of the file, the 
        # regrid_data() is only expecting a file name.
        if prod == 'MRMS':
            match = re.match(r'.*(GaugeCorr.*00.00_[0-9]{8}_[0-9]{6}.*)',file)
            file_only = match.group(1) 
        else:
            match = re.match(r'(.*)/([0-9]{8}_i[0-9]{2}_f[0-9]{2,3}.*)',file)
            file_only = match.group(2) 
        aaf.forcing(config_file,"regrid",prod,file_only)
def layer(parms, itime, step, which, config):
    """ Perform layering

    NOTE: here is where returns status will be added and used

    Parameters
    ----------
    parms : Parms
        parameters

    """        

    WhfLog.debug("LAYERING: %s  %d %s", itime.strftime("%Y%m%d%H"),
                  step, which)
    aaf.anal_assim_layer(itime.strftime('%Y%m%d%H'), '-%01d'%(step), which, config)
    WhfLog.debug("DONE LAYERING: %s  %d %s", itime.strftime("%Y%m%d%H"),
                  step, which)
def layer(parms, itime, step, which, config):
    """ Perform layering

    NOTE: here is where returns status will be added and used

    Parameters
    ----------
    parms : Parms
        parameters

    """

    WhfLog.debug("LAYERING: %s  %d %s", itime.strftime("%Y%m%d%H"), step,
                 which)
    aaf.anal_assim_layer(itime.strftime('%Y%m%d%H'), '-%01d' % (step), which,
                         config)
    WhfLog.debug("DONE LAYERING: %s  %d %s", itime.strftime("%Y%m%d%H"), step,
                 which)
def regrid(fname, fileType, configFile):
   """Invoke regridding/downscaling 
       
   Parameters
   ----------
   fname: str
      name of file to regrid and downscale, with yyyymmdd parent dir
   fileType: str
      HRRR, RAP, ... string
   configFile : str
      configuration file with all settings

   Returns
   -------
   None

   """

   WhfLog.info("REGRIDDING %s DATA, file=%s", fileType, fname)
   try:
      if (fileType == 'HRRR'):
         srf.forcing(configFile, 'regrid', 'HRRR', fname[9:])
         # special case, if it is a 0 hour forecast, do double regrid
         regridIfZeroHr(configFile, fileType, fname)
      elif (fileType == 'RAP'):
         srf.forcing(configFile, 'regrid', 'RAP', fname[9:])
         # special case, if it is a 0 hour forecast, do double regrid
         regridIfZeroHr(configFile, fileType, fname)
      elif (fileType == 'GFS'):
         mrf.forcing(configFile, 'regrid', 'GFS', fname[9:])
      elif (fileType == 'MRMS'):
         aaf.forcing(configFile, 'regrid', 'MRMS', fname[9:])
      else:
         WhfLog.info("ERROR REGRIDDING %s DATA, file=%s", fileType, fname)
         raise InvalidArgumentError("Unknown file type " + fileType)
   except ZeroHourReplacementError as z:
      WhfLog.info("ERROR REGRIDDING: %s", z)
      WhfLog.info("Remove this forecast from state and continue")
      return
   except:
      WhfLog.info("ERROR REGRIDDING %s DATA, file=%s", fileType, fname)
      raise

   WhfLog.info("DONE REGRIDDING %s DATA, file=%s", fileType, fname)
def do_layering(config_file,parser,rap_downscale_dir, hrrr_downscale_dir, mrms_downscale_dir, fcst_hr, is_yellowstone=False):
    # Initialize some flags and lists,
    # assume that we only have RAP for now.
    request_hrrr = False
    request_mrms = False 
    rap_files = []
    hrrr_files = []
    mrms_files = []
    
    # Set flags to be used to determine which layers need to be layered.
    if hrrr_downscale_dir is not None:
        request_hrrr = True
    if mrms_downscale_dir is not None:
        request_mrmrs = True
  
    # We will always have RAP when calling do_layering.
    print "RAP downscale dir: %s"%rap_downscale_dir
    rap_file_paths = whf.get_filepaths(rap_downscale_dir)
    size = len(rap_file_paths)
    print("number of RAP files in %s, %s")%(rap_downscale_dir, size)
    if request_hrrr == True:
        print "HRRR true, layer RAP and HRRR"
        # Layer only RAP and HRRR
        hrrr_file_paths = whf.get_filepaths(hrrr_downscale_dir)
        if is_yellowstone:
            for rap in rap_file_paths:
                match = re.match(r'.*/RAP.*/([0-9]{10})/([0-9]{12}.LDASIN_DOMAIN1.*)',rap)
                model_run = match.group(1)
                aaf.anal_assim_layer(model_run,fcst_hr,"RAP_HRRR",config_file)

        else:
            for rap in rap_file_paths:
                match = re.match(r'.*/RAP.*/([0-9]{10})/([0-9]{12}.LDASIN_DOMAIN1.*)',rap)
                model_run = match.group(1)
                aaf.anal_assim_layer(model_run,fcst_hr,"RAP_HRRR", config_file)
 
        if request_mrms == True:
            # Layer all three: RAP, HRRR, and MRMS
            # Compare the YYYYMMDDHH/YYYYMMDDhh00.LDASIN_DOMAIN1.nc portions
            mrms_file_paths = whf.get_filepaths(mrms_downscale_dir)
            if is_yellowstone:    
                for rap in rap_file_paths:
                    match = re.match(r'.*/RAP.*([0-9]{10})/([0-9]{12}.LDASIN_DOMAIN1.*)',rap)
                    model_run = match.group(1)
                    whf.anal_assim_layer(model_run, fcst_hr, 'RAP_HRRR_MRMS',config_file) 
            else:
                # Testing on development/test host
                for rap in rap_file_paths:
                    match = re.match(r'.*/RAP.*([0-9]{10})/([0-9]{12}.LDASIN_DOMAIN1.*)',rap)
                    model_run = match.group(1)
                    whf.anal_assim_layer(model_run, fcst_hr, 'RAP_HRRR_MRMS',config_file) 
    else:
        # Only RAP requested, call layering with just RAP.     
        print ("Only RAP requested, layering called with just RAP")
        if is_yellowstone:    
            for rap in rap_file_paths:
                print("layering rap file: %s")%rap
                match = re.match(r'.*/RAP/.*([0-9]{10})/([0-9]{12}.LDASIN_DOMAIN1.*)',rap)
                model_run = match.group(1)
                print("model run: %s, fcst hr %s")%(model_run,fcst_hr)
                aaf.anal_assim_layer(model_run, fcst_hr, "RAP",config_file)
         
        else:
            for rap in rap_file_paths:
                match = re.match(r'.*/RAP/.*([0-9]{10})/([0-9]{12}.LDASIN_DOMAIN1.*)',rap)
                model_run = match.group(1)
                aaf.anal_assim_layer(model_run, fcst_hr, "RAP",config_file)