pcmdi_metrics.utils.replace_date_pattern

pcmdi_metrics.utils.replace_date_pattern(filename, replacement)[source]

Replace the ‘YYYYMM-YYYYMM’ pattern in a filename with a specified replacement.

Parameters:
  • filename (str) – The original filename containing the date pattern.

  • replacement (str) – The string to replace the date pattern with.

Returns:

str – The filename with the date pattern replaced.

Examples

>>> from pcmdi_metrics.utils import replace_date_pattern
>>> replace_date_pattern("pr_mon_GPCP-2-3_PCMDI_gn_197901-201907.nc", "DATE_RANGE")
'pr_mon_GPCP-2-3_PCMDI_gn_DATE_RANGE.nc'
>>> replace_date_pattern("temp_daily_197001-202012_processed.nc", "FULL_PERIOD")
'temp_daily_FULL_PERIOD_processed.nc'

Notes

This function assumes that there’s only one occurrence of the ‘YYYYMM-YYYYMM’ pattern in the filename. If multiple occurrences are possible, consider using re.sub() instead of re.subn().