import xarray as xr
import numpy as np
from netCDF4 import Dataset
import datetime
import os
import pandas as pd
from logging import debug
from .....utils.datastores.empty import init_empty
[docs]
def read(
self,
name,
varnames,
dates,
files,
interpol_flx=False,
tracer=None,
model=None,
**kwargs
):
"""Read CARBOSCOPE background concentrations into a pyCIF datastore.
For each file in ``files``, parses the whitespace-delimited station
data (date components in columns 1-6, concentration in column 12),
keeps only rows within the requested date range, derives the station
code from the file name (translated via ``self.dict_station_name`` if
``station_name_dict`` was configured), and packs the result into an
empty pyCIF datastore. All per-file datastores are concatenated.
Args:
self: the background datastream Plugin.
name: name of the observed parameter/component.
varnames: unused (kept for interface consistency with other
datastream ``read`` functions).
dates: list of ``[start, end]`` date pairs to extract, one per
entry of ``files``.
files: list of CARBOSCOPE station files to read from, aligned
with ``dates``.
interpol_flx (bool): unused here (kept for interface consistency).
tracer: the background tracer, used for its ``numscale`` scaling
factor.
model: unused here (kept for interface consistency).
Returns:
pandas.DataFrame: concatenated pyCIF datastore (as built by
``init_empty``) with ``date``, ``station``, ``network``,
``parameter`` and ``duration`` metadata columns, and ``obserror``/
``spec`` main-data columns.
"""
trcr_bc = []
out_dates = []
ref_file = ""
ds_all = [init_empty()]
for ff in files:
debug(f"Reading file {ff} for Carboscope background")
data = pd.read_csv(
ff, header=None, sep=r"\s+", usecols=[1, 2, 3, 4, 5, 6, 12]
)
data["date"] = pd.to_datetime(
data[[1, 2, 3, 4]].astype(str).agg('/'.join, axis=1),
format="%Y/%m/%d/%H")
mask = ((min(np.array(dates)[:, 0]) <= data["date"])
& (data["date"] <= max(np.array(dates)[:, 0])))
data = data.loc[mask]
# Deal with station name if needed
station = os.path.basename(ff)[2:5]
if hasattr(self, "station_name_dict"):
station = self.dict_station_name.get(
station.upper(), station).upper()
ds = init_empty()
ds[("metadata", "date")] = data["date"]
ds[("metadata", "station")] = station
ds[("metadata", "network")] = "Carboscope"
ds[("metadata", "parameter")] = name
ds[("metadata", "duration")] = 1
ds[("maindata", "obserror")] = 0
ds[("maindata", "spec")] = \
data[12] * getattr(tracer, "numscale", 1)
ds_all.append(ds)
ds_all = pd.concat(ds_all, axis=0)
return ds_all