Source code for pycif.plugins.datastreams.fluxes.dummy_txt.fetch

import os
import pandas as pd
from .....utils import path
from .make import make


[docs] def fetch(ref_dir, ref_file, input_interval, target_dir, tracer=None, **kwargs): """Fetch (or synthesize) dummy_txt flux files covering the interval. Extends the requested interval to full ``tracer.period`` blocks. For each period, links the existing file into ``target_dir`` if present; otherwise generates the flux field on the fly via ``tracer.make(tracer)`` and writes it with ``tracer.write(...)`` so a file is always available. Each period is then subdivided into ``tracer.freq`` sub-intervals for the returned dates/files. Args: ref_dir (str): directory where the original files are found. ref_file (str): (template) name of the original files. input_interval (list): simulation interval, as a list of the two bounding dates. target_dir (str): directory where links to the original (or generated) files are created. tracer: the tracer Plugin, giving access to ``period`` and ``freq``. **kwargs: unused, kept for interface compatibility. Returns: (dict, dict): ``list_files`` and ``list_dates``. list_files: for each date that begins a period, a list containing the name of the file used for the dates within this period. list_dates: for each date that begins a period, a list containing the date intervals matching the files listed in ``list_files``. """ # Extend simulation interval if end date does not include a full period # of fluxes datei = input_interval[0] datef = input_interval[1] tmp_dates = pd.date_range(datei, datef, freq=tracer.period) # Fetching or creating if necessary list_files = {} list_dates = {} for di in tmp_dates: f = datei.strftime(f"{ref_dir}/{ref_file}") target_file = f"{target_dir}/{os.path.basename(f)}" if os.path.isfile(f): path.link(f, target_file) else: flx = tracer.make(tracer) tracer.write("", target_file, flx[0, 0]) df = di + pd.to_timedelta(tracer.period) drange = pd.date_range(di, df, freq=tracer.freq) list_files[di] = (len(drange) - 1) * [target_file] list_dates[di] = [[d0, d1] for d0, d1 in zip(drange[:-1], drange[1:])] return list_files, list_dates