Source code for pycif.plugins.datastreams.fluxes.CMEMS.fetch
import datetime
import glob
import os
from netCDF4 import Dataset
import pandas as pd
import xarray as xr
import numpy as np
from .....utils import path
from .....utils.dates import date_range
from logging import debug
[docs]
def fetch(ref_dir, ref_file, input_interval, target_dir,
tracer=None, component=None, **kwargs):
"""
Fetch files and dates for CMEMS.
Args:
ref_dir (str): the path to the input files
ref_file (str): format of the input files
input_interval (list): simulation interval (start and end dates)
target_dir (str): where to copy
tracer: the tracer Plugin, corresponding to the paragraph
:bash:`datavect/components/fluxes/parameters/my_species` in the
configuration yaml; can be needed to fetch extra information
given by the user
component: the component Plugin, same as tracer; corresponds to the paragraph
:bash:`datavect/components/fluxes` in the configuration yaml
Return:
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 matching the files listed in list_files
"""
# List of possible dates
datei, datef = input_interval
list_period_dates = \
date_range(datei, datef, period=tracer.file_freq)
# Loop over dates
list_files = {}
list_dates = {}
valid_files = []
for dd in list_period_dates:
file = dd.strftime("{}/{}".format(ref_dir, ref_file))
if not os.path.isfile(file) or file in valid_files:
continue
# Load years
dates = xr.open_dataset(file)["time"].to_pandas().index
dates -= pd.to_timedelta(dates.day - 1, unit="D")
dates_end = dates + pd.to_timedelta(dates.days_in_month, unit="D")
# Build the output dictionary
list_dates[dd] = [[dd0, dd1] for dd0, dd1 in zip(dates, dates_end)]
list_files[dd] = len(dates) * [file]
# Fetching
target_file = "{}/{}".format(target_dir, os.path.basename(file))
path.link(file, target_file)
valid_files.append(file)
return list_files, list_dates