Source code for pycif.plugins.datastreams.meteos.dummy_csv.fetch

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


[docs] def fetch(ref_dir, ref_file, input_interval, target_dir, tracer=None, **kwargs): """Locate dummy meteo CSV files, generating them if missing, and link them. Iterates over period-start dates covering ``input_interval`` at ``tracer.file_freq``, and for each period computes the hourly (or ``tracer.resolution``) sub-periods it should cover. If the corresponding source file (``ref_dir/ref_file`` formatted for that period) exists, it is symlinked into ``target_dir``; otherwise it is generated on the fly via :func:`make.make`. Args: ref_dir: directory holding (or expected to hold) the dummy meteo CSV files. ref_file: strftime-style file name pattern (relative to ``ref_dir``), formatted once per period. input_interval: 2-element sequence ``(datei, datef)`` giving the requested date range. target_dir: directory where files are symlinked or generated. tracer: the meteo datastream Plugin, used for ``tracer.file_freq`` and ``tracer.resolution``, and passed through to ``make`` when a file needs to be generated. Returns: tuple: ``(list_files, list_dates)``, dicts keyed by each period's start date. ``list_files`` maps each key to the (repeated) target file path, one entry per sub-period; ``list_dates`` maps each key to the corresponding list of ``[start, start + resolution]`` date pairs. """ list_period_dates = \ pd.date_range(input_interval[0], input_interval[1], freq=tracer.file_freq) list_dates = {} list_files = {} for dd in list_period_dates: file = dd.strftime(f"{ref_dir}/{ref_file}") file_hours = pd.date_range( dd, dd + pd.to_timedelta(tracer.file_freq), freq=tracer.resolution, inclusive="left" ) list_dates[dd] = [[hh, hh + pd.to_timedelta(tracer.resolution)] for hh in file_hours] target_file = f"{target_dir}/{os.path.basename(file)}" list_files[dd] = (len(file_hours) * [target_file]) if os.path.isfile(file): # Fetching path.link(file, target_file) else: make(tracer, target_file, file_hours) return list_files, list_dates