Source code for pycif.plugins.datastreams.meteos.dummy_csv.read
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pandas as pd
import xarray as xr
import numpy as np
[docs]
def read(meteo, name, varnames, dates, files, **kwargs):
"""Read a variable from the dummy meteo CSV files.
For each requested date, reloads the CSV file when it differs from the
previously read one, then selects the row matching that date.
Args:
meteo: the meteo datastream Plugin (unused here beyond interface
consistency).
name (str): name of the CSV column to extract (``windspeed``,
``winddir`` or ``stabclass``).
varnames: unused (kept for interface consistency with other
datastream ``read`` functions).
dates: list of ``[start, end]`` date pairs, aligned with ``files``;
only ``start`` is used to select rows.
files: list of dummy meteo CSV files to read from, aligned with
``dates``. Consecutive duplicate entries are only reloaded
once.
Returns:
xarray.DataArray: the requested column's values for each date in
``dates``, with dims ``(time, lev, lat, lon)`` (single-point
``lev``/``lat``/``lon``).
"""
ref_file = ""
ref_meteo = pd.DataFrame()
for dd, ff in zip(dates, files):
if ff != ref_file:
ds = pd.read_csv(ff, index_col="date", parse_dates=True)
ref_meteo = pd.concat([ref_meteo, ds.loc[[dd[0]]]])
xmod = xr.DataArray(
ref_meteo[name].values[:, np.newaxis, np.newaxis, np.newaxis],
coords={"time": ref_meteo[name].index.values},
dims=("time", "lev", "lat", "lon")
)
return xmod