Source code for pycif.plugins.datastreams.fields.netcdf_cams.read
import datetime
import calendar
import os
import pandas as pd
import numpy as np
import xarray as xr
from netCDF4 import Dataset
from .....utils.netcdf import readnc
[docs]
def read(
self,
name,
varnames,
dates,
files,
interpol_flx=False,
comp_type=None,
tracer=None,
ddi=None,
**kwargs
):
"""Get BCs from raw files and load them into a pyCIF
variables
Args:
self: the BC Plugin
name: the name of the component
tracdir, tracfile: raw data directory and file format
dates: list of dates to extract
comp_type: type of boundary conditions to generate
"""
if ddi is None:
raise Exception("CAMS netCDF read function was called "
"without specifying ddi")
# Variable name to extract
var2extract = name
if varnames != "":
var2extract = varnames
# Reading fields for periods within the simulation window
xout = []
opened_file = ""
for dd, dd_file in zip(dates, files):
# Avoid opening the file for all dates
if dd_file != opened_file:
ds = xr.open_dataset(dd_file)
opened_file = dd_file
ntimes = ds.dims["time"]
freq = pd.DatetimeIndex([dd[0]]).days_in_month[0] * 24 / ntimes
date_end = ds["time"].to_pandas().dt.to_pydatetime()[-1]
date_index = \
ntimes - 1 - int((date_end - dd[0]) /
datetime.timedelta(hours=freq))
# bottom of the atmosphere = at the beginning of the table
lat = ds['latitude']
conc = ds[var2extract].values[date_index]
if lat[1] < lat[0] and conc.ndim == 4:
conc = conc[:, :, ::-1, :]
elif lat[1] < lat[0] and conc.ndim == 3:
conc = conc[:, ::-1, :]
# Swap levels if required to
if getattr(tracer, "flip_level", False):
if conc.ndim == 4:
conc = conc[:, ::-1]
elif conc.ndim == 3:
conc = conc[::-1]
# Append to datastore
xout.append(conc)
xmod = xr.DataArray(
np.array(xout),
coords={"time": np.array(dates)[:, 0]},
dims=("time", "lev", "lat", "lon"),
)
return xmod