Source code for pycif.plugins.datastreams.fields.lmdz_inicond_reg.fetch
from __future__ import annotations
import datetime
import os
from pathlib import Path
from .....utils import path
from .....utils.check.errclass import CifFileNotFoundError
[docs]
def fetch(
ref_dir: str,
ref_file: str,
input_interval: tuple[datetime.datetime, datetime.datetime],
target_dir: str,
**kwargs,
) -> tuple[
dict[datetime.datetime, list[str]],
dict[datetime.datetime, list[tuple[datetime.datetime, datetime.datetime]]],
]:
"""Link the single LMDz inicond file for the simulation start date.
Args:
ref_dir (str): Path to the data. If both ``ref_dir`` and
``ref_file`` are falsy, nothing is fetched.
ref_file (str): File name format of the data.
input_interval (list[datetime.datetime]): Date range; only the
start date is used.
target_dir (str): Where to link the data.
**kwargs: Unused.
Returns:
(list_files, list_dates): tuple of single-entry dictionaries keyed
by the simulation start date, describing the linked file and
the (single-point) date interval it covers.
Raises:
CifFileNotFoundError: If the original inicond file does not exist.
"""
if not ref_dir and not ref_file:
return {}, {}
date = input_interval[0]
input_file = Path(ref_dir, date.strftime(ref_file))
if not input_file.is_file():
raise CifFileNotFoundError(f"file '{input_file}' not found")
# Fetching
target_file = os.path.join(target_dir, os.path.basename(input_file))
target_file = Path(target_dir, input_file.name)
path.link(input_file, target_file)
list_files = {date: [str(input_file)]}
list_dates = {date: [(date, date)]}
return list_files, list_dates