Source code for pycif.plugins.datastreams.fields.lmdz_outfields_nc.fetch
import os
import pandas as pd
import datetime
from .....utils import path
[docs]
def fetch(
ref_dir, ref_file, date_interval, target_dir, tracer=None, component=None
):
"""Link monthly LMDz ``trajq`` files and build 3-hourly sub-intervals.
Iterates over dates spaced by ``tracer.file_freq`` across
``date_interval``, links each existing monthly file into ``target_dir``,
and builds 3-hourly sub-intervals covering the whole month (8 per day).
Args:
ref_dir: Directory where the original files are found.
ref_file: Date-format pattern for the original file names.
date_interval: List of two dates, the beginning and end of the
period to fetch.
target_dir: Directory where links to the original files are
created.
tracer: Tracer/component configuration; ``tracer.file_freq`` gives
the frequency at which monthly files are available.
component: Unused.
Returns:
A tuple ``(list_files, list_dates)`` of dictionaries keyed by
monthly file date. ``list_dates`` maps each key to one 3-hourly
``[start, end]`` interval per 3-hour period of the month;
``list_files`` maps each key to the (repeated) path of that
month's file.
"""
# Reshape input interval to include full months
datei, datef = date_interval
list_period_dates = \
pd.date_range(datei, datef, freq=tracer.file_freq)
list_dates = {}
list_files = {}
for dd in list_period_dates:
file = dd.strftime(f"{ref_dir}/{ref_file}")
if os.path.isfile(file):
# Fetching
target_file = f"{target_dir}/{os.path.basename(file)}"
path.link(file, target_file)
# Time stamps
file_hours = pd.date_range(
dd, periods=8 * pd.DatetimeIndex([dd]).days_in_month[0],
freq="3h")
list_dates[dd] = [
[hh0, hh0 + datetime.timedelta(hours=3)]
for hh0 in file_hours]
list_files[dd] = (len(list_dates[dd]) * [target_file])
return list_files, list_dates