import numpy as np
import xarray as xr
import pandas as pd
from logging import debug
[docs]
def read(
self,
name,
varnames,
dates,
files,
interpol_flx=False,
tracer=None,
model=None,
ddi=None,
**kwargs
):
"""Get point-source fluxes from the CSV file and load them into a pyCIF variable.
For each requested date interval, re-reads the CSV file and splits its
rows into active sources (whose ``[datei, datef]`` validity period
overlaps the requested interval) and inactive ones. Builds a two-level
column `pandas.DataFrame` (``metadata``: lon, lat, alt, date, duration,
tstep, dtstep; ``maindata``: spec) with the emission value for active
sources and 0 for inactive ones, and concatenates the result across all
requested dates.
Args:
name (str): name of the component
varnames (list[str]): Unused directly, kept for interface
consistency with other flux plugins.
dates (list): list of the ``[start, end]`` date intervals to extract
files (list): list of the (repeated) CSV file path matching `dates`
interpol_flx (bool): Unused, kept for interface consistency.
tracer: The flux tracer plugin, providing ``domain`` (used only to
read ``nlon``/``nlat``/``nlev``, currently unused in the loop).
model: Unused, kept for interface consistency.
ddi: Unused, kept for interface consistency.
Return:
pandas.DataFrame: A `MultiIndex`-columned DataFrame (``metadata``
and ``maindata`` groups) with one row per point source and per
requested date, holding the source's location/timing metadata and
its emission value (0 for sources inactive at that date).
"""
# Get domain dimensions for random generation
domain = tracer.domain
nlon = domain.nlon
nlat = domain.nlat
nlev = domain.nlev
# Loop over dates/files and import data
data = []
idate = 0
for dd, ff in zip(dates, files):
debug(
f"Reading the file {ff} for the date interval {dd}"
)
# Read the csv file
ds = pd.read_csv(ff, sep=";", parse_dates=["datei", "datef"])
# Put it into a dataframe
mask = (ds["datei"] < dd[1]) & (ds["datef"] > dd[0])
data.append(pd.DataFrame(
{("metadata", "lon"): ds.loc[mask, 'lon'],
("metadata", "lat"): ds.loc[mask, 'lat'],
("metadata", "alt"): ds.loc[mask, 'alt'],
("metadata", "date"): dd[0],
("metadata", "duration"): 60,
("metadata", "tstep"): mask.sum() * [idate],
("metadata", "dtstep"): mask.sum() * [1],
("maindata", "spec"): ds.loc[mask, "emis (kg/s)"]
}))
data.append(pd.DataFrame(
{("metadata", "lon"): ds.loc[~mask, 'lon'],
("metadata", "lat"): ds.loc[~mask, 'lat'],
("metadata", "alt"): ds.loc[~mask, 'alt'],
("metadata", "date"): dd[0],
("metadata", "duration"): 60,
("metadata", "tstep"): (~mask).sum() * [idate],
("metadata", "dtstep"): (~mask).sum() * [1],
("maindata", "spec"): (~mask).sum() * [0]
}))
idate += 1
return pd.concat(data)