Source code for pycif.plugins.datastreams.meteos.tm5_meteo.read
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from .....utils.netcdf import readnc
# JvP 20210517: added import statements, MODULE_NAME module level logger
import logging
import sys
import subprocess
from .....utils.check.errclass import CifRuntimeError
MODULE_NAME = __name__[__name__.index('TM5'):] if 'TM5' in __name__ else __name__
logger = logging.getLogger(MODULE_NAME)
# Original read function by A. Berchet
[docs]
def read_AB(
self,
name,
tracdir,
tracfile,
varnames,
dates,
interpol_flx=False,
tracer=None,
model=None,
filetypes=["defstoke", "fluxstoke", "fluxstokev", "phystoke"],
**kwargs
):
"""Cache the LMDZ-style physics time step from a ``defstoke`` file (original implementation).
Superseded by :func:`read` below, which is a non-functional
placeholder. For each date and file type, builds the expected
``<filetype>.an<year>.m<month>.nc`` path in ``tracdir`` (falling back
to ``<filetype>.nc`` if the dated file is missing) and, for
``filetype == "defstoke"``, reads ``dtvr``/``istdyn`` from it the
first time to compute and cache ``self.offtstep``. This appears to be
LMDZ mass-flux bookkeeping logic carried over and not actually
applicable to reading TM5 meteo.
Args:
self: the meteo datastream Plugin (receives the cached
``offtstep`` attribute).
name: unused (kept for interface consistency).
tracdir: directory holding the meteo files.
tracfile: unused (kept for interface consistency).
varnames: unused (kept for interface consistency).
dates: list of dates to build file paths for.
interpol_flx (bool): unused (kept for interface consistency).
tracer: unused (kept for interface consistency).
model: unused (kept for interface consistency).
filetypes ([str]): file-type radicals to iterate over; only
``"defstoke"`` triggers any actual processing.
Returns:
None. ``self.offtstep`` is set as a side effect, once.
"""
for date in dates:
for filetype in filetypes:
meteo_file = f"{filetype}.an{date.year}.m{date.month:02d}.nc"
if filetype == "defstoke" and not os.path.isfile(
f"{tracdir}/{meteo_file}"
):
meteo_file = filetype + ".nc"
target = f"{tracdir}/{meteo_file}"
# Loading information on time steps
if filetype == "defstoke" and not hasattr(self, "offtstep"):
vars = readnc(target, ["dtvr", "istdyn"])
offtstep = vars[0][0, 0] * vars[1][0, 0]
self.offtstep = offtstep
# New read function by J.C.A. van Peet
[docs]
def read( self, name, tracdir, tracfile, varnames, dates, interpol_flx=False,
tracer=None, model=None, filetypes=["defstoke", "fluxstoke", "fluxstokev", "phystoke"],
**kwargs ):
"""Placeholder: reading TM5 meteo is not implemented.
Always logs a critical error and terminates the process via
``sys.exit()``; it never returns a value to the caller. Kept only to
satisfy the meteo datastream plugin interface.
Note:
Version history: 2.0 (17-05-2021, J.C.A. van Peet) replaced the
original ``read_AB`` implementation (above, 1.0, 28-04-2021,
A. Berchet) with this placeholder.
Args:
self: the meteo datastream Plugin.
name: unused.
tracdir: unused.
tracfile: unused.
varnames: unused.
dates: unused.
interpol_flx (bool): unused.
tracer: unused.
model: unused.
filetypes ([str]): unused.
Raises:
SystemExit: always; a ``CifRuntimeError`` is raised internally,
caught, logged critically, and followed by ``sys.exit()``.
"""
# Set the name of this function
PROG_NAME = MODULE_NAME+".read"
# Local logger
logger = logging.getLogger(PROG_NAME)
logger.setLevel(logging.DEBUG)
logger.debug("")
logger.debug("*"*30)
logger.debug(PROG_NAME+" => Just a placeholder function...")
logger.debug(" Computer says no!")
logger.debug("*"*30)
logger.debug("")
try:
raise CifRuntimeError
except RuntimeError as e:
#logger.exception("OOPS!")
logger.critical(e, exc_info=True)
#raise # => Will display the traceback on screen a second time
sys.exit() # => Just exit.
# end try
# end function read