Source code for pycif.plugins.models.iconart.ini_periods
import datetime
import numpy as np
import pandas as pd
import xarray as xr
from netCDF4 import Dataset
from logging import error, info, debug
from ....utils.dates import date_range
from ....utils.netcdf import readnc
[docs]
def ini_periods(self, **kwargs):
"""
The function :bash:`ini_periods` is optional but very recommended.
It is used to define the temporal variables ``subsimu_dates``, ``input_dates``,
``tstep_dates`` and ``tstep_all``.
The function is automatically called at the initialization
of the :bash:`model` class if available.
If not available, the temporal variables should be defined manually
in the :bash:`ini_data` function (not recommended).
:bash:`ini_periods` is a class method that applies to the :bash:`model` plugin itself.
Therefore, the only expected argument is :bash:`self`.
.. code-block:: python
def ini_periods(self, **kwargs):
self.subsimu_dates = XXXX
self.tstep_dates = XXXXX
self.input_dates = XXXXX
self.tstep_all = XXXXX
"""
# Define sub-simu dates
info("Sub-simulations are initialized as one sub-simulation "
"covering the full simulation window")
datei = self.datei
datef = self.datef
# List of sub-simulation windows
self.subsimu_dates = date_range(datei, datef, period=self.periods)
# Check that subperiods are all of the same length
if np.unique(np.diff(self.subsimu_dates)).size > 1:
raise Exception("Trying to run ICON-ART on a simulation window not fitting the "
"sub-simulation length. \nPlease check the compatibility between "
"the duration of your sub-simulations ({}) and the simulation "
"window ({} to {}): \n"
"Guessed sub-periods: \n".format(self.periods, datei, datef) +
"\n".join([" - {}: {}".format(d0, d1 - d0)
for d0, d1 in zip(self.subsimu_dates[:-1],
self.subsimu_dates[1:])])
)
# Define modelling time steps for outputs
self.tstep_dates = {}
self.input_dates = {}
self.lbc_input_dates = {}
self.end_dates = {}
for dd0, dd1 in zip(self.subsimu_dates[:-1], self.subsimu_dates[1:]):
self.input_dates[dd0] = pd.date_range(
dd0, dd1, freq=self.input_resolution).to_pydatetime()
self.lbc_input_dates[dd0] = pd.date_range(
dd0, dd1 + pd.Timedelta(seconds=self.lbc_timestep),
freq=str(self.lbc_timestep) + 's').to_pydatetime()
self.tstep_dates[dd0] = pd.date_range(
dd0, dd1, freq=self.output_resolution).to_pydatetime()
# Initializes dictionary to keep in memory whether observations were
# already dumped for a given period
self.iniobs = {ddi: [] for ddi in self.subsimu_dates}
self.reset_obs = {ddi: [] for ddi in self.subsimu_dates}
# Dictionary to store inversion scaling factors
self.flux_lambdas = {ddi: {} for ddi in self.subsimu_dates}
self.lbc_lambdas = {ddi: {} for ddi in self.subsimu_dates}
# Dictionary to move the last output of ICON to the next sub-simulation
# self.chain_last_output = {}