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

import datetime
import glob
import os
from logging import info

import numpy as np
import pandas as pd

from .....utils import path
from .utils import find_valid_file


[docs] def fetch(ref_dir, ref_file, input_interval, target_dir, tracer=None, **kwargs): """Fetch VPRM flux files and build hourly date intervals covering the simulation window. Iterates hourly over `input_interval`; for each hour, uses `find_valid_file` to locate the nearest available (forecast-style) file bracketing it, considering the current directory as well as the previous/next hour's directory when close to a boundary, and links the nearest file into `target_dir` if it exists. Args: ref_dir (str): Directory (or ``strftime`` pattern) containing the reference input files. ref_file (str): Filename pattern of the input files (a ``strftime`` format string). input_interval (list[datetime.datetime]): ``[date_i, date_f]`` simulation interval to cover. target_dir (str): Directory where the resolved files are linked. tracer: Unused directly, kept for interface consistency with other flux plugins. Returns: tuple[dict, dict]: ``(list_files, list_dates)``, each keyed by the hourly date, mapping to the list of resolved file paths and a single-element list with the ``[start, end]`` one-hour date interval. """ # Inputs: # --------- # ref_dir: directory where the original files are found # ref_file: (template) name of the original files # input_interval: list of the periods to simulate, each item is the list of the dates of the period # target_dir: directory where the links to the orginal files are created # # Ouputs: # --------- # list_files: for each date that begins a period, an array containing the names of the files that are available # for the dates within this period # list_dates: for each date that begins a period, an array containing the names of the dates mathcin the files # listed in list_files list_period_dates = pd.date_range(input_interval[0], input_interval[1], freq="1h") list_dates = {} list_files = {} for dd in list_period_dates: dir_dd = dd.strftime(ref_dir) dir_dd_next = (dd + datetime.timedelta(hours=1)).strftime(ref_dir) dir_dd_previous = (dd - datetime.timedelta(hours=1)).strftime(ref_dir) files_3d, dates_3d = find_valid_file( dir_dd, ref_file, dd, dir_dd_next, ref_dir_previous=dir_dd_previous ) if os.path.isfile(files_3d[0]): list_dates[dd] = [[dd, dd + datetime.timedelta(hours=1)]] list_files[dd] = [files_3d] # the to fetch is a forecast local_files = [] target_file = f"{target_dir}/{dd.strftime(ref_file)}" path.link(files_3d[0], target_file) local_files.append(target_file) return list_files, list_dates