source: XIOS/trunk/src/calendar.cpp @ 549

Last change on this file since 549 was 549, checked in by rlacroix, 6 years ago

Revised calendar functionalities:

  • the calendar is now configured from a specific calendar child node of the context in the XML configuration file. Example: <calendar type="Gregorian" start_date="2012-03-01 15:00:00" time_origin="2012-02-29 15:00:00" timestep="1h" />
  • the calendar type should now be configured when defining the start time and/or the time origin.
  • the start time and the time origin are now optional, 0000-01-01 00:00:00 will be used by default. It is also possible to define them partially. For example, 2015 and 2014-12 are valid dates corresponding respectively to 2015-01-01 00:00:00 and 2014-12-01 00:00:00.
  • an optional duration offset can be added to the start date and time origin. For example, it's possible to define the date 2015-01-12 12:00:00 as 2015-01-11 + 36h or 2015-01-11 12:00:00 + 1d. The duration format is the same as the time step. Being that the date is optional, it is possible to only use a duration (for example + 42s is the same as 0000-01-01 00:00:00 + 42s). An error will be raised if a duration based on the time step is used before the time step was configured. For example, the following would cause an error: <calendar type="Gregorian" start_date="+ 1ts" /> but <calendar type="Gregorian" start_date="+ 1ts" timestep="0.5h" /> would not.
  • new Fortran interface to define the calendar:
    • xios_define_calendar(type[, timestep, start_date, time_origin]) will create a calendar when none had previously been defined. Only the type argument is mandatory, the rest is optional. Calendar operations on dates and durations are possible as soon as the calendar is created (either using this procedure or directly from the XML configuration file).
    • the following getter and setter procedures are available: xios_set_timestep, xios_set_start_date, xios_set_time_origin, xios_get_calendar_type, xios_get_timestep, xios_get_start_date, xios_get_time_origin.
  • new Fortran interface to interact with the calendar: xios_update_calendar, xios_get_current_date, xios_get_year_length_in_seconds, xios_get_day_length_in_seconds.
  • new Fortran interface for date conversion: xios_date_get_second_of_year, xios_date_get_day_of_year, xios_date_get_fraction_of_year, xios_date_get_second_of_day, xios_date_get_fraction_of_day.
  • two new placeholders are available to format the file name when splitting the output (split_freq_format attribute):
    • %S the number of seconds since the time origin
    • %D the integral number of days since the time origin
  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
File size: 6.6 KB
Line 
1#include "calendar.hpp"
2#include "duration.hpp"
3#include "date.hpp"
4#include "calendar_util.hpp"
5
6namespace xios
7{
8      /// ////////////////////// Définitions ////////////////////// ///
9      CCalendar::CCalendar(void)
10         : CObject()
11         , initDate(*this)
12         , timeOrigin(*this)
13         , currentDate(*this)
14      {}
15
16      CCalendar::CCalendar(const StdString& id)
17               : CObject(id)
18               , initDate(*this)
19               , timeOrigin(*this)
20               , currentDate(*this)
21      {}
22
23      CCalendar::CCalendar(const StdString& id,
24                           int yr, int mth, int d,
25                           int hr /*= 0*/, int min /*= 0*/, int sec /*= 0*/)
26               : CObject(id)
27               , initDate(*this)
28               , timeOrigin(*this)
29               , currentDate(*this)
30      {
31        initializeDate(yr, mth, d, hr, min, sec);
32      }
33
34      CCalendar::CCalendar(const StdString& id, const CDate& startDate)
35               : CObject(id)
36               , initDate(startDate)
37               , timeOrigin(startDate)
38               , currentDate(startDate)
39      {
40        // Initialize the dates only in the derivated classes
41        // since we want to use the overloaded virtual functions
42      }
43
44      CCalendar::CCalendar(const StdString& id, const CDate& startDate, const CDate& timeOrigin)
45               : CObject(id)
46               , initDate(startDate)
47               , timeOrigin(timeOrigin)
48               , currentDate(startDate)
49      {
50        // Initialize the dates only in the derivated classes
51        // since we want to use the overloaded virtual functions
52      }
53
54      void CCalendar::initializeDate()
55      {
56        if (!initDate.setRelCalendar(*this))
57          ERROR("CCalendar::initializeDate()",
58                "initDate: Bad format or date not conform to the calendar");
59        if (!timeOrigin.setRelCalendar(*this))
60          ERROR("CCalendar::initializeDate()",
61                "timeOrigin: Bad format or date not conform to the calendar");
62        if (!currentDate.setRelCalendar(*this))
63          ERROR("CCalendar::initializeDate()",
64                "currentDate: Bad format or date not conform to the calendar");
65      }
66
67      void CCalendar::initializeDate(int yr, int mth, int d,
68                                     int hr /*= 0*/, int min /*= 0*/, int sec /*= 0*/)
69      {
70        initDate = CDate(*this, yr, mth, d, hr, min, sec);
71        timeOrigin = initDate;
72        currentDate = initDate;
73      }
74
75      void CCalendar::initializeDate(const StdString& dateStr)
76      {
77        initDate = CDate::FromString(dateStr, *this);
78        timeOrigin = initDate;
79        currentDate = initDate;
80      }
81
82      void CCalendar::initializeDate(const StdString& dateStr, const StdString& timeOriginStr)
83      {
84        initDate = CDate::FromString(dateStr, *this);
85        timeOrigin = CDate::FromString(timeOriginStr, *this);
86        currentDate = initDate;
87      }
88
89      CCalendar::~CCalendar(void)
90      { /* Ne rien faire de plus */ }
91
92      ///---------------------------------------------------------------
93
94      StdString CCalendar::toString(void) const
95      {
96         StdOStringStream oss;
97         oss <<   "[type: "   << this->getId()
98             << ", start: "   << this->initDate
99             << ", current: " << this->currentDate << "]";
100         return (oss.str());
101      }
102
103      void CCalendar::fromString(const StdString& str)
104      { ERROR("CCalendar::fromString(str)",
105               << "[ str = " << str << "] Not implemented yet !"); }
106
107      //-----------------------------------------------------------------
108
109      void CCalendar::setTimeStep(const CDuration& timestep)
110      {
111        if (timestep.timestep)
112          ERROR("CCalendar::setTimeStep(const CDuration& timestep)",
113                << "Circular definition of the timestep: the timestep cannot refer to itself.");
114        this->timestep = timestep;
115      }
116
117      CDate& CCalendar::update(int step)
118      {
119        info(20) << "update step : " << step << " timestep " << this->timestep << std::endl;
120        return (this->getCurrentDate() = this->getInitDate() + step * this->timestep);
121      }
122
123      //-----------------------------------------------------------------
124
125      void CCalendar::setInitDate(const CDate& initDate)
126      {
127        if (&initDate.getRelCalendar() != this)
128          ERROR("CCalendar::setInitDate(const CDate& initDate)",
129                << "The init date cannot be attached to another calendar.");
130        this->initDate = initDate;
131      }
132
133      void CCalendar::setTimeOrigin(const CDate& timeOrigin)
134      {
135        if (&timeOrigin.getRelCalendar() != this)
136          ERROR("CCalendar::setInitDate(const CDate& timeOrigin)",
137                << "The time origin cannot be attached to another calendar.");
138        this->timeOrigin = timeOrigin;
139      }
140
141      //-----------------------------------------------------------------
142
143      const CDuration& CCalendar::getTimeStep(void) const { return this->timestep; }
144      const CDate& CCalendar::getInitDate(void) const     { return this->initDate; }
145      const CDate& CCalendar::getTimeOrigin(void) const   { return this->timeOrigin; }
146      CDate& CCalendar::getCurrentDate(void)              { return this->currentDate; }
147
148      //-----------------------------------------------------------------
149
150      int CCalendar::getMonthLength(const CDate& date) const
151      { // Retourne la durée du mois en jour.
152        static const int NoLeapMonthLength[] =
153          { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
154        return NoLeapMonthLength[date.getMonth() - 1];
155      }
156
157      StdString CCalendar::getType(void) const { return StdString(this->getId()); }
158
159      int CCalendar::getYearTotalLength(const CDate& date) const { return (365 * 86400); }
160
161      int CCalendar::getYearLength  (void) const { return 12; }
162      int CCalendar::getDayLength   (void) const { return 24; }
163      int CCalendar::getHourLength  (void) const { return 60; }
164      int CCalendar::getMinuteLength(void) const { return 60; }
165      int CCalendar::getDayLengthInSeconds(void) const { return getDayLength() * getHourLength() * getMinuteLength(); }
166
167      StdString CCalendar::getMonthName(int monthId) const
168      {
169        static const StdString MonthNames[] =
170          { "january", "february", "march",     "april" ,  "may",      "june",
171            "july",    "august",   "september", "october", "november", "december" };
172        return MonthNames[monthId - 1];
173      }
174
175      const StdString CCalendar::getMonthShortName(int monthId) const
176      { StdString value = this->getMonthName(monthId); value.resize(3); return value; }
177
178      ///----------------------------------------------------------------
179
180} // namespace xios
Note: See TracBrowser for help on using the repository browser.