Ignore:
Timestamp:
02/10/15 14:23:02 (9 years ago)
Author:
rlacroix
Message:

Add a new user defined calendar type.

A new calendar type "user_defined" is now available. This allows the users to create a custom calendar that we can configured to be suitable for planets other than the Earth.

An user defined calendar is always defined by two mandatory attributes:

  • day_length: the duration of a day, in seconds
  • and either:
    • month_length: an array containing the duration of each month, in days (the number of elements in the array is the number of months in a year)
    • or year_length: the duration of a year, in seconds (in that case, the calendar does not have months).

If the calendar has months (i.e. month_length attribute is set) and only in that case, it is possible to define leap years in order to compensate for the duration of an astronomical year not being a multiple of the day length. The leap years are defined by two mandatory attributes:

  • leap_year_month: the month to which the extra day will be added in case of leap year, expressed as an integer number in the range [1, numberOfMonths]
  • and leap_year_drift: the fraction of a day representing the yearly drift between the calendar year and the astronomical year, expressed as a real number in the range [0, 1).

Optionally, one can define the leap_year_drift_offset attribute to set the original drift at the beginning of the time origin's year, again expressed as a real number in the range [0, 1). If leap_year_drift_offset + leap_year_drift is greater or equal to 1, then the first year will be a leap year.

For example, the following configuration creates a Gregorian-like calendar:

<calendar type="user_defined" start_date="2012-03-01 15:00:00" time_origin="2012-02-28 15:00:00 + 1d" day_length="86400" month_lengths="(1, 12) [31 28 31 30 31 30 31 31 30 31 30 31]" leap_year_month="2" leap_year_drift="0.25" leap_year_drift_offset="0.75" />

Note that dates attributes must be written differently in the configuration file when using an user defined calendar without months:

  • if the year length is greater than the day length, the input format is year-day hh:min:sec instead of year-month-day hh:min:sec
  • if the day length is greater or equal to the year length, the input format is year hh:min:sec.

In all cases, it is still possible to use the date + duration notation to build a date (with both the date and duration parts being optional).

The Fortran interface has been updated accordingly so that xios_define_calendar can accept the new attributes necessary to define custom calendars.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • XIOS/trunk/src/calendar.cpp

    r549 r550  
    165165      int CCalendar::getDayLengthInSeconds(void) const { return getDayLength() * getHourLength() * getMinuteLength(); } 
    166166 
     167      bool CCalendar::hasLeapYear() const { return false; } 
     168 
    167169      StdString CCalendar::getMonthName(int monthId) const 
    168170      { 
     
    176178      { StdString value = this->getMonthName(monthId); value.resize(3); return value; } 
    177179 
    178       ///---------------------------------------------------------------- 
    179  
     180      CDuration& CCalendar::resolve(CDuration& dur, bool noNegativeTime /*= false*/) const 
     181      { 
     182        const int hourLengthInSeconds = getHourLength() * getMinuteLength(); 
     183 
     184        // Simplify the days, hours, minutes and seconds. 
     185        // First convert them to seconds 
     186        Time t = Time(modf(dur.day, &dur.day) * getDayLengthInSeconds() 
     187                        + (dur.hour * getHourLength() + dur.minute) * getMinuteLength() + dur.second); 
     188        // Then convert back to days 
     189        dur.day += int(t / getDayLengthInSeconds()); 
     190        t %= getDayLengthInSeconds(); 
     191 
     192        // Do we allow hour, minute, second to be negative? 
     193        if (noNegativeTime) 
     194        { 
     195          // If we don't, we remove some days until the time is positive 
     196          while (t < 0) 
     197          { 
     198            t += getDayLengthInSeconds(); 
     199            dur.day -= 1.0; 
     200          } 
     201        } 
     202 
     203        // hours 
     204        dur.hour = int(t / hourLengthInSeconds); 
     205        t %= hourLengthInSeconds; 
     206        // minutes 
     207        dur.minute = int(t / getMinuteLength()); 
     208        // secondes 
     209        dur.second = int(t % getMinuteLength()); 
     210 
     211        // Nothing to do for the months yet since this depends on an actual date 
     212 
     213        // Simplify the years 
     214        dur.month  += modf(dur.year, &dur.year) * getYearLength(); 
     215        dur.year   += int(dur.month) / getYearLength(); dur.month = int(dur.month) % getYearLength(); 
     216 
     217        return dur; 
     218      } 
     219 
     220      /*! Parse a date using a generic parser. */ 
     221      void CCalendar::parseDateDefault(StdIStream& in, CDate& date) 
     222      { 
     223        char sep = '-'; // Le caractÚre c est utilisé pour "recueillir" les séparateurs "/" et ":". 
     224        char c; 
     225 
     226        // Default initialize the date 
     227        int year = 00, month  = 01, day    = 01; 
     228        int hour = 00, minute = 00, second = 00; 
     229 
     230        in >> year >> c; 
     231        if (c == sep) 
     232        { 
     233          in >> month >> c; 
     234          if (c == sep) 
     235          { 
     236            in >> day; 
     237            c = in.get(); 
     238            sep = ' '; 
     239            if (c == sep) 
     240            { 
     241              in >> hour >> c; 
     242              sep = ':'; 
     243              if (c == sep) 
     244              { 
     245                in >> minute >> c; 
     246                if (c == sep) 
     247                { 
     248                  in >> second; 
     249                  in >> c; 
     250                } 
     251              } 
     252            } 
     253          } 
     254        } 
     255 
     256        date.setDate(year, month, day, hour, minute, second); 
     257 
     258        // Delay the verification until we get a calendar we can compare the date to 
     259        if (date.hasRelCalendar() && !date.checkDate()) 
     260          ERROR("void parseDateDefault(StdIStream& in, CDate& date)", 
     261                << "Bad date format or not conform to calendar"); 
     262 
     263        if (c == '+') // We will be adding a duration to the date 
     264        { 
     265          CDuration dur; 
     266          in >> dur; 
     267          date = date + dur; 
     268        } 
     269        else if (!in.eof()) 
     270          ERROR("void parseDateDefault(StdIStream& in, CDate& date)", 
     271                << "Invalid date format: unexpected trailing character(s)"); 
     272      } 
     273 
     274      /*! Parse a date using the calendar's parser. */ 
     275      void CCalendar::parseDate(StdIStream& in, CDate& date) const 
     276      { 
     277        parseDateDefault(in, date); 
     278      } 
     279 
     280      /*! Test if a date is valid with regard to the current calendar. */ 
     281      bool CCalendar::checkDate(CDate& date) const 
     282      { 
     283        bool isValid = true; 
     284 
     285        // Vérification de la valeur du mois. 
     286        if (date.getMonth() < 1) 
     287        { isValid = false; date.setMonth(1); } 
     288        else if (date.getMonth() > getYearLength()) 
     289        { isValid = false; date.setMonth(getYearLength()); } 
     290 
     291        // Vérification de la valeur du jour. 
     292        if (date.getDay() < 1) 
     293        { isValid = false; date.setDay(1); } 
     294        else if (date.getDay() > getMonthLength(*this)) 
     295        { isValid = false; date.setDay(getMonthLength(*this)); } 
     296 
     297        // Vérification de la valeur de l'heure. 
     298        if (date.getHour() < 0) 
     299        { isValid = false; date.setHour(0); } 
     300        else if (date.getHour() >= getDayLength()) 
     301        { isValid = false; date.setHour(getDayLength() - 1); } 
     302 
     303        // Vérification de la valeur des minutes. 
     304        if (date.getMinute() < 0) 
     305        { isValid = false; date.setMinute(0); } 
     306        else if (date.getMinute() >= getHourLength()) 
     307        { isValid = false; date.setMinute(getHourLength() - 1); } 
     308 
     309        // Vérification de la valeur des secondes. 
     310        if (date.getSecond() < 0) 
     311        { isValid = false; date.setSecond(0); } 
     312        else if (date.getSecond() >= getMinuteLength()) 
     313        { isValid = false; date.setSecond(getMinuteLength() - 1); } 
     314 
     315        return isValid; 
     316      } 
    180317} // namespace xios 
Note: See TracChangeset for help on using the changeset viewer.