Source code for pycif.plugins.datastreams.fields.oldlmdz_photochem.fetch

import os
import datetime
import pandas as pd
import xarray as xr
from .....utils import path
from .....utils.hdf5 import _hdf5_lock


[docs] def fetch(ref_dir, ref_file, date_interval, target_dir, tracer=None, **kwargs): """Fetch legacy LMDZ4 photolysis-rate files and their sub-intervals. For each date in ``date_interval`` (stepped at ``tracer.file_freq``), links the corresponding file into ``target_dir`` if it exists, and reads its ``time_counter`` dimension to build one 24h sub-interval per time step found in the file. Args: ref_dir: directory where the original files are found. ref_file: (template) name of the original files. date_interval: list of two dates, the beginning and end of the simulation. target_dir: directory where links to the original files are created. tracer: the fields Plugin, giving access to ``file_freq``. Returns: list_files: for each date, the file path repeated once per time step found in that file. list_dates: for each date, the list of 24h ``[start, end]`` sub-intervals covered by that file's time steps. """ # Reshape input interval to include full months datei, datef = date_interval list_period_dates = \ pd.date_range(datei, datef, freq=tracer.file_freq).to_pydatetime() list_dates = {} list_files = {} for dd in list_period_dates: dout = datetime.datetime(dd.year + 1, 1, 1) 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 with _hdf5_lock: ds = xr.open_dataset(target_file) file_hours = list( ds["time_counter"].to_pandas().index.to_pydatetime() ) list_dates[dd] = [ [hh0, hh0 + datetime.timedelta(hours=24)] for hh0 in file_hours] list_files[dd] = (len(list_dates[dd]) * [target_file]) return list_files, list_dates