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

import os
import numpy as np
from dateutil import relativedelta
import datetime
import pandas as pd
from .....utils import path


[docs] def fetch(ref_dir, ref_file, date_interval, target_dir, tracer=None, **kwargs): """Fetch monthly NOAA global-average files. Forces the start date to the beginning of its month and extends the end date by one month, then for each month (stepped at ``tracer.file_freq``) links the corresponding file into ``target_dir`` if it exists, with a one-month ``list_dates`` interval. 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 monthly date, a one-element list with the file path. list_dates: for each monthly date, a one-element list with the ``[start, end]`` interval covering that month. """ datei, datef = date_interval datei = datetime.datetime(year=datei.year, month=datei.month, day=1) datef = datef + relativedelta.relativedelta(months=1) list_period_dates = pd.date_range(datei, datef, freq=tracer.file_freq) list_dates = {} list_files = {} for dd in list_period_dates: file = dd.strftime(f"{ref_dir}/{ref_file}") if os.path.isfile(file): list_dates[dd] = [[dd, dd + relativedelta.relativedelta(months=1)]] list_files[dd] = [file] # Fetching target_file = f"{target_dir}/{os.path.basename(file)}" path.link(file, target_file) return list_files, list_dates