Source code for pycif.plugins.datastreams.fields.bc_plugin_template.read

""" 
Reads the data in the orignal files. Is called for each key in dictionary list_dates/list_files provided by fetch.

Args:
------
- name: name of the component
- dates: list of the dates matching the files
- files: list of the files matching the dates
 -> dates and files as provided by fetch.py
- varnames: original names of variables to read
- comp_type: type of boundary conditions to generate. Useful to distinguish lateral from top from initial conditions.

Returns:
---------
- a DataArray with the actual data: 4 dimensional (time, vertical levels, latitude, longitude) with coordinates in increasing order (see also get_domain).

"""


import datetime
import os

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, **kwargs ): """Template showing how to read BCs from raw files into a pyCIF DataArray. Illustrates looping over the ``dates``/``files`` pairs produced by ``fetch`` (one pair per key), reading the requested variable(s) from each file, and assembling the result into a 4-dimensional DataArray. The body below is placeholder ``print(...)`` statements only; ``data`` is never actually built, so this function would fail if run as-is and must be replaced with real logic in any plugin derived from this template. Args: self: The field/tracer object. name: Name of the component. varnames: Original name(s) of the variable(s) to read. dates: List of dates matching ``files``, as provided by ``fetch``. files: List of files matching ``dates``, as provided by ``fetch``. interpol_flx: Unused in this template. comp_type: Type of boundary conditions to generate; useful to distinguish lateral from top from initial conditions. **kwargs: Unused in this template. Returns: xarray.DataArray: A 4-dimensional ``(time, lev, lat, lon)`` array with coordinates in increasing order (see also ``get_domain``). """ print('If relevant, check here the component type') # Example: if comp_type == "inicond": ... # elif comp_type in ["latcond", "topcond"]: ... print('Loop on the dates and files') for dd, ff in zip(dates, files): print('Here put the reading of ', [varnames],' in ',ff,' for ',dd) print('e.g. get a 3d array') print('may require some manipulations on dates to get the right index in the file') # Putting in DataArray # Put here the required reversing of axis to get coordinates in increasing order xmod = xr.DataArray( np.array(data)[:,:,:,:], coords={"time": dates}, dims=("time", "lev", "lat", "lon") ) return xmod