Source code for pycif.plugins.datastreams.meteos.chimere_meteo.read
import os
import datetime
import numpy as np
import xarray as xr
from .....utils.netcdf import readnc
[docs]
def read(
self,
name,
varnames,
dates,
files,
interpol_flx=False,
comp_type=None,
ddi=None,
**kwargs
):
"""Get meteo from a pre-computed meteo and load required variables into a pyCIF
variables
Args:
self: the meteo Plugin
name: the name of the component
metdir, metfic: meteo directory and file format
dates: list of dates to extract
"""
var2extract = varnames if varnames != "" else name
# Reading required meteo files
trcr_met = []
for period, ff in zip(dates, files):
data, times = readnc(ff, [var2extract, "Times"])
# Force conversion to float64
data = data.astype(float)
# Get the correct hour in the file
times = [
datetime.datetime.strptime(
str(b"".join(s), "utf-8"), "%Y-%m-%d_%H:%M:%S"
)
for s in times
]
hour = int((period[0] - times[0]).total_seconds() // 3600)
if ddi is not None:
hour = int((period[0] - ddi).total_seconds() // 3600)
trcr_met.append(data[hour, ...])
# Building a xarray
xmod = xr.DataArray(
trcr_met, coords={"time": np.array(dates)[:, 0]},
dims=("time", "lev", "lat", "lon")
)
return xmod