import os
import pandas as pd
import datetime
import numpy as np
from .....utils import path
from .....utils.classes.domains import Domain
[docs]
def fetch(ref_dir, ref_file, date_interval, target_dir, tracer=None, **kwargs):
"""Fetch yearly FLEXPART flux files and prepare a single-level domain.
Builds a yearly file list covering ``date_interval``; for each existing
file, expands it into monthly date sub-intervals, links the file (and,
if ``tracer.file_glob`` is set, the accompanying "global" background
file for that year) into ``target_dir``. Finally, ``tracer.domain`` is
rebuilt with a single forced vertical level (``nlev=1``), copying all
other horizontal/nesting attributes from the existing domain unchanged.
Args:
ref_dir (str): directory where the original files are found.
ref_file (str): (template) name of the original files.
date_interval (list): simulation interval, as a list of the two
bounding dates.
target_dir (str): directory where links to the original files are
created.
tracer: the tracer Plugin, giving access to ``domain`` and,
optionally, ``file_glob``.
**kwargs: unused, kept for interface compatibility.
Returns:
(dict, dict): ``list_files`` and ``list_dates``.
list_files: for each date that begins a period (a year), a list
containing the name of the file repeated once per covered
monthly sub-interval.
list_dates: for each date that begins a period, a list of
``[start, end]`` monthly date intervals covered by the file.
"""
datei, datef = date_interval
datei = datetime.datetime(year=datei.year, month=1, day=1)
datef = datetime.datetime(year=datef.year + 1, month=1, day=1)
list_period_dates = pd.date_range(datei, datef, freq="1YS")
list_dates = {}
list_files = {}
for dd in list_period_dates:
file = dd.strftime(f"{ref_dir}/{ref_file}")
if os.path.isfile(file):
ddf = datetime.datetime(year=dd.year + 1, month=1, day=1)
file_dates = pd.date_range(
dd, ddf, freq="1MS")
list_dates[dd] = [
[mm0, mm1]
for mm0, mm1 in zip(file_dates[:-1], file_dates[1:])]
list_files[dd] = (len(file_dates) * [file])
# Fetching
target_file = f"{target_dir}/{os.path.basename(file)}"
path.link(file, target_file)
# Fetching global files as well
if hasattr(tracer, "file_glob"):
file_glob = dd.strftime(
f"{ref_dir}/{tracer.file_glob}")
target_file = f'{target_dir}/{os.path.basename(file_glob)}'
path.link(file_glob, target_file)
# Update the FLEXPART domain with only one level
domain_in = tracer.domain
domain_out = Domain(nlon=domain_in.nlon, nlat=domain_in.nlat,
xmin=domain_in.xmin, xmax=domain_in.xmax,
ymin=domain_in.ymin, ymax=domain_in.ymax,
zlon=domain_in.zlon, zlat=domain_in.zlat,
zlonc=domain_in.zlonc, zlatc=domain_in.zlatc,
lon_in=domain_in.lon_in, lat_in=domain_in.lat_in,
zlon_in=domain_in.zlon_in, zlat_in=domain_in.zlat_in,
zlonc_in=domain_in.zlonc_in, zlatc_in=domain_in.zlatc_in,
nlon_side=domain_in.nlon_side, nlat_side=domain_in.nlat_side,
zlon_side=domain_in.zlon_side, zlat_side=domain_in.zlat_side,
zlonc_side=domain_in.zlonc_side, zlatc_side=domain_in.zlatc_side,
nlev=1, pressure_unit="Pa", nested=domain_in.nested,
sigma_b_mid=np.array([1]), sigma_a_mid=np.array([0]),
raveled_indexes_glob=domain_in.raveled_indexes_glob,
unstructured_domain=domain_in.unstructured_domain,
regular_degrees=domain_in.regular_degrees
)
tracer.domain = domain_out
return list_files, list_dates