Source code for pycif.plugins.datastreams.fluxes.tm5.fetch
import os
import datetime
import pandas as pd
import xarray as xr
from netCDF4 import Dataset
import numpy as np
from .....utils import path
[docs]
def fetch(ref_dir, ref_file, date_interval, target_dir, tracer=None, **kwargs):
list_period_dates = \
pd.date_range(date_interval[0], date_interval[1],
freq=tracer.file_freq).to_pydatetime()
list_dates = {}
list_files = {}
for dd in list_period_dates:
file = dd.strftime("{}/{}".format(ref_dir, ref_file))
if os.path.isfile(file):
nc_id = Dataset(file, 'r')
# List the sources present in the file
sources = ['biomass-burning', 'rice', 'wetlands', 'other']
region = 'glb600x400'
name = tracer.varname if tracer.varname != "" else tracer.orig_name
for i_source, source in enumerate(sources):
# Get the path to the current source and print it
source_path = region + '/' + name + '/' + source + '/'
nc_time_start = np.array(
[datetime.datetime(*bagger)
for bagger in nc_id[source_path + 'time_start']])
list_dates[dd] = [
[hh0, hh1] for hh0, hh1 in zip(nc_time_start[:-1], nc_time_start[1:])
]
list_dates[dd].append([nc_time_start[-1], date_interval[-1]])
# Fetching
target_file = "{}/{}".format(target_dir, os.path.basename(file))
path.link(file, target_file)
list_files[dd] = (len(list_dates[dd]) * [target_file])
return list_files, list_dates