Ignore:
Timestamp:
01/26/15 14:39:26 (9 years ago)
Author:
rlacroix
Message:

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
File:
1 edited

Legend:

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

    r545 r549  
    55      /// ////////////////////// Définitions ////////////////////// /// 
    66 
    7       CDuration operator*(const double & scal, const CDuration & ddr) 
     7      CDuration operator*(const double& scal, const CDuration& ddr) 
    88      { return (ddr * scal); } 
    99 
    10       CDuration operator-(const CDuration & ddr , const CDuration & dr) 
    11       { 
    12          CDuration dur(ddr); 
    13          dur.year -= dr.year;  dur.month  -= dr.month ; dur.day    -= dr.day; 
     10      CDuration operator-(const CDuration& ddr , const CDuration& dr) 
     11      { 
     12         CDuration dur(ddr); 
     13         dur.year -= dr.year;  dur.month  -= dr.month; dur.day    -= dr.day; 
    1414         dur.hour -= dr.hour;  dur.minute -= dr.minute; dur.second -= dr.second; dur.timestep -= dr.timestep; 
    1515         return (dur); 
    1616      } 
    1717 
    18       CDuration operator+(const CDuration & ddr , const CDuration & dr) 
    19       { 
    20          CDuration dur(ddr); 
    21          dur.year += dr.year;  dur.month  += dr.month ; dur.day    += dr.day; 
     18      CDuration operator+(const CDuration& ddr , const CDuration& dr) 
     19      { 
     20         CDuration dur(ddr); 
     21         dur.year += dr.year;  dur.month  += dr.month; dur.day    += dr.day; 
    2222         dur.hour += dr.hour;  dur.minute += dr.minute; dur.second += dr.second; dur.timestep += dr.timestep; 
    2323         return (dur); 
    2424      } 
    2525 
    26       CDuration operator*(const CDuration & ddr , const double & scal) 
     26      CDuration operator*(const CDuration& ddr , const double& scal) 
    2727      { 
    2828         CDuration dur(ddr); 
     
    3232      } 
    3333 
    34       CDuration operator-(const CDuration & ddr) 
     34      CDuration operator-(const CDuration& ddr) 
    3535      { 
    3636         CDuration dur(ddr); 
     
    4747      //----------------------------------------------------------------- 
    4848 
    49       CDate operator+(const CDate & dt, const CDuration & dr) 
    50       { 
    51          CDuration drr (dr); 
     49      CDate operator+(const CDate& dt, const CDuration& dr) 
     50      { 
     51         CDuration drr(dr); 
    5252         int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0; 
    53          const CCalendar & c = dt.getRelCalendar(); 
    54           
    55          drr.timestep=0 ;        
    56          drr=drr+dr.timestep*dt.getRelCalendar().getTimeStep() ; 
    57           
    58          drr.resolve(dt.getRelCalendar()); 
     53         const CCalendar& c = dt.getRelCalendar(); 
     54 
     55         drr.timestep = 0; 
     56         if (dr.timestep) 
     57         { 
     58            if (c.getTimeStep() == NoneDu) 
     59               ERROR("operator+(const CDate& dt, const CDuration& dr)", 
     60                     << "Impossible to use the timestep before it is set."); 
     61            drr = drr + dr.timestep * c.getTimeStep(); 
     62         } 
     63 
     64         drr.resolve(c); 
    5965 
    6066         // Ajustement des minutes par rapport aux secondes. 
    6167         second += dt.getSecond() + drr.second; 
    62          if (second <  0) { minute --; second += c.getMinuteLength(); } 
    63          if (second >= c.getMinuteLength()) { minute ++; second -= c.getMinuteLength(); } 
     68         if (second < 0) { minute--; second += c.getMinuteLength(); } 
     69         if (second >= c.getMinuteLength()) { minute++; second -= c.getMinuteLength(); } 
    6470 
    6571         // Ajustement des heures en fonction des minutes. 
    6672         minute += dt.getMinute() + drr.minute; 
    67          if (minute < 0) { hour --; minute += c.getHourLength(); } 
    68          if (minute >= c.getHourLength()) { hour ++; minute -= c.getHourLength(); } 
     73         if (minute < 0) { hour--; minute += c.getHourLength(); } 
     74         if (minute >= c.getHourLength()) { hour++; minute -= c.getHourLength(); } 
    6975 
    7076         // Ajustement des jours en fonction des heures. 
    7177         hour += dt.getHour() + drr.hour; 
    72          if (hour <  0) { drr.day --; hour += c.getDayLength(); } 
    73          if (hour >= c.getDayLength()) { drr.day ++; hour -= c.getDayLength(); } 
     78         if (hour < 0) { drr.day--; hour += c.getDayLength(); } 
     79         if (hour >= c.getDayLength()) { drr.day++; hour -= c.getDayLength(); } 
    7480 
    7581         // Ajustement des mois en fonction des jours. 
    76          CDate dtt(dt);  
    77          drr.day+=dtt.getDay()-1 ; 
    78          dtt.setDay(1) ;          
    79  
    80          if ( drr.day >= 0 ) 
    81          { 
    82            for(; c.getMonthLength(dtt) <= drr.day; dtt.addMonth (1)) 
    83            { drr.day -=  c.getMonthLength(dtt); drr.month += 1 ; } 
    84  
    85            day = drr.day+1 ; 
    86          } 
    87          else  
    88          { 
    89            dtt.addMonth(-1) ; 
    90            drr.month-=1 ; 
    91            for(; c.getMonthLength(dtt) < -drr.day; dtt.addMonth (-1)) 
    92            { drr.day+=c.getMonthLength(dtt) ; drr.month-=1 ; } 
    93            day=c.getMonthLength(dtt)+drr.day+1 ; 
    94          } 
    95              
     82         CDate dtt(dt); 
     83         drr.day += dtt.getDay() - 1; 
     84         dtt.setDay(1); 
     85 
     86         if (drr.day >= 0) 
     87         { 
     88           for(; c.getMonthLength(dtt) <= drr.day; dtt.addMonth(1)) 
     89           { drr.day -= c.getMonthLength(dtt); drr.month += 1; } 
     90 
     91           day = drr.day + 1; 
     92         } 
     93         else 
     94         { 
     95           dtt.addMonth(-1); 
     96           drr.month -= 1; 
     97           for(; c.getMonthLength(dtt) < -drr.day; dtt.addMonth(-1)) 
     98           { drr.day += c.getMonthLength(dtt); drr.month -= 1; } 
     99           day = c.getMonthLength(dtt) + drr.day + 1; 
     100         } 
     101 
    96102/* 
    97103         if (day <  0) { drr.month --; day += c.getMonthLength(dtt); } 
    98104         if (day > c.getMonthLength(dtt)) { drr.month ++; day -= c.getMonthLength(dtt); } // << ProblÚme ici 
    99105         if (day == 0){ day = c.getMonthLength(dtt); drr.month --; } 
    100 */          
    101          drr.resolve(dt.getRelCalendar()); 
     106*/ 
     107         drr.resolve(c); 
    102108 
    103109         // Ajustement des années en fonction des mois. 
    104110         month += dt.getMonth() + drr.month; 
    105          if (month <  0) { drr.year --; month += c.getYearLength(); } 
    106          if (month >  c.getYearLength()) { drr.year ++; month -= c.getYearLength(); } 
     111         if (month < 0) { drr.year--; month += c.getYearLength(); } 
     112         if (month > c.getYearLength()) { drr.year++; month -= c.getYearLength(); } 
    107113         if (month == 0){ month = c.getYearLength(); drr.year--; } 
    108114 
    109115         year += dt.getYear() + drr.year; 
    110116 
    111          return (CDate(dt.getRelCalendar(), year, month, day, hour, minute, second)); 
    112       } 
    113  
    114       CDate operator-(const CDate & dt, const CDuration & dr) { return (dt + (-dr)); } 
    115  
    116       //----------------------------------------------------------------- 
    117  
    118       CDuration operator-(const CDate & dt0, const CDate & dt1) 
     117         return (CDate(c, year, month, day, hour, minute, second)); 
     118      } 
     119 
     120      CDate operator-(const CDate& dt, const CDuration& dr) { return (dt + (-dr)); } 
     121 
     122      //----------------------------------------------------------------- 
     123 
     124      CDuration operator-(const CDate& dt0, const CDate& dt1) 
    119125      { 
    120126         // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier. 
     
    131137      bool operator==(const CDuration& ddr, const CDuration& dr) 
    132138      { 
    133          return ((ddr.year == dr.year) && (ddr.month  == dr.month)  && (dr.day    == ddr.day) && 
    134                  (ddr.hour == dr.hour) && (ddr.minute == dr.minute) && (dr.second == ddr.second) && 
     139         return ((ddr.year == dr.year)&& (ddr.month  == dr.month) && (dr.day    == ddr.day)&& 
     140                 (ddr.hour == dr.hour)&& (ddr.minute == dr.minute)&& (dr.second == ddr.second)&& 
    135141                 (ddr.timestep == dr.timestep)); 
    136142      } 
     
    144150      { 
    145151         // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier. 
    146          return ((dt0.getYear() == dt1.getYear()) && (dt0.getMonth()  == dt1.getMonth())  && (dt1.getDay()    == dt0.getDay()) && 
    147                  (dt0.getHour() == dt1.getHour()) && (dt0.getMinute() == dt1.getMinute()) && (dt1.getSecond() == dt0.getSecond())); 
     152         return ((dt0.getYear() == dt1.getYear())&& (dt0.getMonth()  == dt1.getMonth()) && (dt1.getDay()    == dt0.getDay())&& 
     153                 (dt0.getHour() == dt1.getHour())&& (dt0.getMinute() == dt1.getMinute())&& (dt1.getSecond() == dt0.getSecond())); 
    148154      } 
    149155 
     
    152158         // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier. 
    153159         if   (dt0.getYear()  < dt1.getYear()) 
    154          {  
     160         { 
    155161            return true; 
    156162         } 
    157163         else if (dt0.getYear() == dt1.getYear()) 
    158          {  
     164         { 
    159165            if   (dt0.getMonth()  < dt1.getMonth()) 
    160166            { 
     
    174180                  } 
    175181                  else if (dt0.getHour() == dt1.getHour()) 
    176                   {  
     182                  { 
    177183                     if   (dt0.getMinute()  < dt1.getMinute()) 
    178184                     { 
     
    193199      //----------------------------------------------------------------- 
    194200 
    195       bool operator!=(const CDate & dt0, const CDate & dt1){ return !(dt1 == dt0); } 
    196       bool operator> (const CDate & dt0, const CDate & dt1){ return (dt1 < dt0); } 
    197       bool operator>=(const CDate & dt0, const CDate & dt1){ return ((dt0 > dt1) || (dt1 == dt0)); } 
    198       bool operator<=(const CDate & dt0, const CDate & dt1){ return ((dt0 < dt1) || (dt1 == dt0)); } 
     201      bool operator!=(const CDate& dt0, const CDate& dt1){ return !(dt1 == dt0); } 
     202      bool operator> (const CDate& dt0, const CDate& dt1){ return (dt1 < dt0); } 
     203      bool operator>=(const CDate& dt0, const CDate& dt1){ return ((dt0 > dt1) || (dt1 == dt0)); } 
     204      bool operator<=(const CDate& dt0, const CDate& dt1){ return ((dt0 < dt1) || (dt1 == dt0)); } 
    199205 
    200206      ///---------------------------------------------------------------- 
Note: See TracChangeset for help on using the changeset viewer.