XIOS  1.0
Xml I/O Server
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Définitions de type Énumérations Valeurs énumérées Amis Macros
calendar_wrapper.cpp
Aller à la documentation de ce fichier.
1 #include "calendar_wrapper.hpp"
2 #include "type.hpp"
3 #include "calendar_type.hpp"
4 #include "duration.hpp"
5 #include "context.hpp"
6 
7 namespace xios {
8 
10 
12  : CObjectTemplate<CCalendarWrapper>(), CCalendarWrapperAttributes()
13  { /* Ne rien faire de plus */ }
14 
16  : CObjectTemplate<CCalendarWrapper>(id), CCalendarWrapperAttributes()
17  { /* Ne rien faire de plus */ }
18 
20  {}
21 
22  //----------------------------------------------------------------
23 
24  StdString CCalendarWrapper::GetName(void) { return StdString("calendar_wrapper"); }
25  StdString CCalendarWrapper::GetDefName(void) { return StdString("calendar"); }
27 
28  //----------------------------------------------------------------
29 
34  std::shared_ptr<CCalendar> CCalendarWrapper::getCalendar(bool checkValid /*= false*/) const
35  {
36  if (checkValid && !this->calendar)
37  ERROR("CCalendarWrapper::getCalendar(bool checkValid = true)", << "The calendar was accessed before being created!");
38  return this->calendar;
39  }
40 
42  {
43  return getCalendar(true)->getInitDate();
44  }
45 
47  {
48  return getCalendar(true)->getTimeOrigin();
49  }
50 
51  //----------------------------------------------------------------
52 
53  void CCalendarWrapper::setInitDate(const CDate& initDate)
54  {
55  getCalendar(true)->setInitDate(initDate);
56  start_date = initDate.toString();
57  }
58 
59  void CCalendarWrapper::setTimeOrigin(const CDate& timeOrigin)
60  {
61  getCalendar(true)->setTimeOrigin(timeOrigin);
62  time_origin = timeOrigin.toString();
63  }
64 
65  //----------------------------------------------------------------
66 
72  {
73  SuperClass::parse(node);
74 
75  // Try to create the calendar
77  }
78 
83  {
84  // Create the calendar if possible
85  if (calendar)
86  {
87  ERROR("CCalendarWrapper::createCalendar(void)",
88  << "Error: the calendar can only be defined once!");
89  }
90  else if (!type.isEmpty())
91  {
92 #define DECLARE_CALENDAR(MType, eType) \
93  if (type.getValue() == type_attr::eType) \
94  calendar = std::shared_ptr<CCalendar>(new C##MType##Calendar());
95 #include "calendar_type.conf"
96 #undef DECLARE_CALENDAR
97  // Special case for the user defined calendar
98  if (type.getValue() == type_attr::user_defined)
99  {
100  if (day_length.isEmpty())
101  ERROR("CCalendarWrapper::createCalendar(void)",
102  << "The day length must be configured when using an user defined calendar.");
103  if (month_lengths.isEmpty() == year_length.isEmpty())
104  ERROR("CCalendarWrapper::createCalendar(void)",
105  << "Either the month lengths or the year length must be configured when using an user defined calendar.");
106  if (leap_year_drift.isEmpty() != leap_year_month.isEmpty())
107  ERROR("CCalendarWrapper::createCalendar(void)",
108  << "Both leap_year_drift and leap_year_month attributes must be set if you wish to configure leap years.");
109  if (leap_year_drift.isEmpty() && !leap_year_drift_offset.isEmpty())
110  ERROR("CCalendarWrapper::createCalendar(void)",
111  << "Both leap_year_drift and leap_year_month attributes are mandatory if you wish to use leap_year_drift_offset attribute.");
112 
113  std::shared_ptr<CUserDefinedCalendar> userDefinedCalendar;
114  if (year_length.isEmpty())
115  userDefinedCalendar.reset(new CUserDefinedCalendar(day_length.getValue(), month_lengths.getValue()));
116  else
117  userDefinedCalendar.reset(new CUserDefinedCalendar(day_length.getValue(), year_length.getValue()));
118 
119  if (!leap_year_month.isEmpty())
120  userDefinedCalendar->configureLeapYear(leap_year_month.getValue(),
121  leap_year_drift.getValue(),
122  leap_year_drift_offset.isEmpty() ? 0.0 : leap_year_drift_offset.getValue());
123 
124  calendar = userDefinedCalendar;
125  }
126  else
127  {
128 #define CHECK_EMPTY(attr) \
129  if (!attr.isEmpty()) \
130  ERROR("CCalendarWrapper::createCalendar(void)", \
131  << "The attribute \"" #attr "\" can only be used with user defined calendar.");
132  CHECK_EMPTY(day_length)
133  CHECK_EMPTY(month_lengths)
134  CHECK_EMPTY(year_length)
135  CHECK_EMPTY(leap_year_month)
136  CHECK_EMPTY(leap_year_drift)
137  CHECK_EMPTY(leap_year_drift_offset)
138 #undef CHECK_EMPTY
139  }
140 
141  if (!calendar)
142  ERROR("CCalendarWrapper::createCalendar(void)",
143  << "[ type = " << type.getStringValue() << " ] "
144  << "The calendar is not properly handled!");
145 
146  // Set the timestep is available
147  if (!timestep.isEmpty())
148  calendar->setTimeStep(timestep.getValue());
149 
150  // Parse and set the start date if available
151  if (!start_date.isEmpty())
152  calendar->setInitDate(CDate::FromString(start_date.getValue(), *calendar));
153 
154  // Parse and set the time origin if available
155  if (!time_origin.isEmpty())
156  calendar->setTimeOrigin(CDate::FromString(time_origin.getValue(), *calendar));
157 
158  // Notify the context about the calendar
159  CContext* context = CContext::getCurrent();
160  if (!context)
161  ERROR("CCalendarWrapper::createCalendar(void)", << "Impossible to set the calendar: no current context available.");
162  context->setCalendar(calendar);
163  }
164  else if (!start_date.isEmpty() || !time_origin.isEmpty())
165  {
166  ERROR("CCalendarWrapper::createCalendar(void)",
167  << "The calendar type must be set before defining the start date or the time origin!");
168  }
169  }
170 
175  {
176  if (timestep.isEmpty())
177  {
178  ERROR("CCalendarWrapper::updateTimestep(void)",
179  << "Error: the timestep needs to be defined!");
180  }
181  else if (calendar)
182  calendar->setTimeStep(timestep.getValue());
183  }
184 }
////////////////////// Déclarations ////////////////////// ///
std::shared_ptr< CCalendar > getCalendar(bool checkValid=false) const
Accesseurs ///.
const CDate & getTimeOrigin() const
static CDate FromString(const StdString &str, const CCalendar &calendar)
Definition: date.cpp:257
std::string StdString
Definition: xios_spl.hpp:48
#define xios(arg)
static StdString GetDefName(void)
void setCalendar(std::shared_ptr< CCalendar > newCalendar)
Mutateurs ///.
Definition: context.cpp:94
CATCH CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar *scalarDestination, CScalar *scalarSource, CReduceScalarToScalar *algo ERROR)("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)",<< "Operation must be defined."<< "Scalar source "<< scalarSource->getId()<< std::endl<< "Scalar destination "<< scalarDestination->getId())
////////////////////// Déclarations ////////////////////// ///
static StdString GetName(void)
Accesseurs statiques ///.
std::shared_ptr< CCalendar > calendar
void createCalendar(void)
Try to create the calendar from the parsed attributes.
virtual void parse(xml::CXMLNode &node)
virtual void parse(xml::CXMLNode &node)
Parse the calendar node.
CCalendarWrapper(void)
Constructeurs ///.
void setTimeOrigin(const CDate &timeOrigin)
void setInitDate(const CDate &initDate)
Mutateurs ///.
enum xios::_node_type ENodeType
////////////////////// Définitions ////////////////////// ///
void updateTimestep(void)
Try to update the timestep of the calendar with the corresponding attribute.
CATCH CAxisAlgorithmReduceAxis::CAxisAlgorithmReduceAxis(CAxis *axisDestination, CAxis *axisSource, CReduceAxisToAxis *algo) alg checkValid)(axisDestination, axisSource)
StdString toString(void) const
Autres ///.
Definition: date.cpp:351
virtual ~CCalendarWrapper(void)
Destructeur ///.
const CDate & getInitDate() const
static CContext * getCurrent(void)
Get current context.
Definition: context.cpp:2018
static ENodeType GetType(void)
#define CHECK_EMPTY(attr)