source: XIOS/trunk/src/duration.cpp @ 1015

Last change on this file since 1015 was 1015, checked in by oabramkina, 7 years ago

Check if output_frequency >= timestep added.
Files with output_frequency < timestep are considered to be disabled.

  • 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: 5.9 KB
Line 
1#include "duration.hpp"
2#include "date.hpp"
3#include "calendar.hpp"
4#include "calendar_util.hpp"
5
6namespace xios
7{
8      /// ////////////////////// Définitions ////////////////////// ///
9      const CDuration Year     = { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
10                      Month    = { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
11                      Week     = { 0.0, 0.0, 7.0, 0.0, 0.0, 0.0, 0.0 },
12                      Day      = { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
13                      Hour     = { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
14                      Minute   = { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
15                      Second   = { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
16                      TimeStep = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 },
17                      NoneDu   = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
18
19      ///---------------------------------------------------------------
20
21      CDuration& CDuration::operator=(const CDuration& duration)
22      {
23        year = duration.year; month  = duration.month;  day    = duration.day;
24        hour = duration.hour; minute = duration.minute; second = duration.second; timestep = duration.timestep;
25        return *this;
26      }
27
28      bool operator<(CDuration a, CDuration b)
29      {
30        double aSeconds = a.second + 60.*(a.minute + 60.*(a.hour + 24.*(a.day + a.year*365.25) ) );
31        double bSeconds = b.second + 60.*(b.minute + 60.*(b.hour + 24.*(b.day + b.year*365.25) ) );
32        return (aSeconds < bSeconds);
33      }
34
35      StdOStream& operator<<(StdOStream& out, const CDuration& duration)
36      {
37         out << duration.toString();
38         return out;
39      }
40
41      StdIStream& operator>>(StdIStream& in , CDuration& duration)
42      {
43        duration = NoneDu;
44        double v = 1.0;
45        char   c = '/';
46        bool   invalidUnit = false;
47
48        do
49        {
50          in >> v >> c;
51          if (in.fail())
52            ERROR("StdIStream& operator>>(StdIStream& in , CDuration& duration)",
53                  << "Bad duration format: impossible to read a pair (value, unit).");
54
55          switch (c)
56          {
57            case 'y': duration.year   = v; break;
58            case 'd': duration.day    = v; break;
59            case 'h': duration.hour   = v; break;
60            case 's': duration.second = v; break;
61            case 'm':
62            {
63              in >> c;
64              if      (c == 'i') duration.minute = v;
65              else if (c == 'o') duration.month  = v;
66              else invalidUnit = true;
67              break;
68            }
69            case 't':
70            {
71              in >> c;
72              if (c == 's') duration.timestep = v;
73              else invalidUnit = true;
74              break;
75            }
76            default:
77              invalidUnit = true;
78              break;
79          }
80
81          if (invalidUnit)
82            ERROR("StdIStream& operator>>(StdIStream& in , CDuration& duration)",
83                  << "Bad duration format: invalid unit, unexpected '" << c << "' character.");
84        } while (in.peek() != EOF); // check whether there is a next character to read
85
86        return in;
87      }
88
89      //-----------------------------------------------------------------
90
91      bool CDuration::isNone(void) const
92      {
93        return (*this == NoneDu);
94      }
95
96      //-----------------------------------------------------------------
97
98      CDuration& CDuration::solveTimeStep(const CCalendar& c)
99      {
100        CDuration timeStep = c.getTimeStep();
101        second += timestep * timeStep.second;
102        minute += timestep * timeStep.minute;
103        hour   += timestep * timeStep.hour;
104        day    += timestep * timeStep.day;
105        month  += timestep * timeStep.month;
106        year   += timestep * timeStep.year;
107        timestep = 0.0;
108        return *this;
109      }
110
111      CDuration& CDuration::resolve(const CCalendar& c, bool noNegativeTime /*= false*/)
112      {
113        return c.resolve(*this, noNegativeTime);
114      }
115
116      //-----------------------------------------------------------------
117
118      StdString CDuration::toString(void) const
119      {
120        StdOStringStream sout;
121        bool forceOutput = true;
122
123        if (year   != 0.0) { forceOutput = false; sout << year   << "y "; }
124        if (month  != 0.0) { forceOutput = false; sout << month  << "mo "; }
125        if (day    != 0.0) { forceOutput = false; sout << day    << "d "; }
126        if (hour   != 0.0) { forceOutput = false; sout << hour   << "h "; }
127        if (minute != 0.0) { forceOutput = false; sout << minute << "mi "; }
128        if (second != 0.0) { forceOutput = false; sout << second << "s "; }
129        if (timestep != 0.0 || forceOutput)     { sout << timestep << "ts "; }
130
131         // Remove the trailing space
132        StdString strOut = sout.str();
133        return strOut.erase(strOut.size() - 1);
134      }
135
136      StdString CDuration::toStringUDUnits(void) const
137      {
138         if (timestep != 0.0)
139           ERROR("StdString CDuration::toStringUDUnits(void) const",
140                 "Impossible to convert a duration to string using UDUnits when a timestep is set.");
141
142         StdOStringStream sout;
143         bool forceOutput = true;
144
145         if (year   != 0.0) { forceOutput = false; sout << year   << " yr "; }
146         if (month  != 0.0) { forceOutput = false; sout << month  << " month "; }
147         if (day    != 0.0) { forceOutput = false; sout << day    << " d "; }
148         if (hour   != 0.0) { forceOutput = false; sout << hour   << " h "; }
149         if (minute != 0.0) { forceOutput = false; sout << minute << " min "; }
150         if (second != 0.0 || forceOutput)       { sout << second << " s "; }
151
152         // Remove the trailing space
153         StdString strOut = sout.str();
154         return strOut.erase(strOut.size() - 1);
155      }
156
157      CDuration CDuration::FromString(const StdString& str)
158      {
159        CDuration dr = NoneDu;
160        StdIStringStream iss(str); iss >> dr;
161        return dr;
162      }
163} // namespace xios
164
Note: See TracBrowser for help on using the repository browser.