source: XMLIO_V2/dev/common/src/xmlio/iface/interface.cpp.in @ 266

Last change on this file since 266 was 266, checked in by hozdoba, 13 years ago

Corrections après tests sur titane

File size: 18.2 KB
Line 
1#include "macro.inc"
2
3INCLUDE "xmlioserver.hpp"
4
5INCLUDE "attribute_template_impl.hpp"
6INCLUDE "object_template_impl.hpp"
7INCLUDE "group_template_impl.hpp"
8
9INCLUDE "calendar_type.hpp"
10
11#ifdef __cplusplus
12extern "C"
13{
14#endif //__cplusplus
15
16typedef xmlioserver::tree::CAxis    caxis;
17typedef xmlioserver::tree::CField   cfield;
18typedef xmlioserver::tree::CDomain  cdomain;
19typedef xmlioserver::tree::CGrid    cgrid;
20typedef xmlioserver::tree::CFile    cfile;
21typedef xmlioserver::tree::CContext ccontext;
22
23typedef xmlioserver::tree::CAxisGroup    caxisgroup;
24typedef xmlioserver::tree::CFieldGroup   cfieldgroup;
25typedef xmlioserver::tree::CDomainGroup  cdomaingroup;
26typedef xmlioserver::tree::CGridGroup    cgridgroup;
27typedef xmlioserver::tree::CFileGroup    cfilegroup;
28typedef xmlioserver::tree::CContextGroup ccontextgroup;
29
30
31typedef long int XInt, XSize; // Integer
32typedef bool     XBool;       // Logical
33typedef void   * XPtr;        // Pointeur C non typé
34typedef char   * XString;     // Pointeur C non typé
35
36typedef enum
37{
38   NOTYPE = 0,
39   DTREATMENT/* UNUSED */, DATE, CALENDAR, ECONTEXT,
40   EAXIS, EDOMAIN, EFIELD, EFILE, EGRID,
41   GAXIS, GDOMAIN, GFIELD, GFILE, GGRID
42} XDType;
43
44typedef enum
45{
46   NETCDF4 = 0
47} XFileType;
48
49typedef enum
50{ D360 = 0 , ALLLEAP, NOLEAP, JULIAN, GREGORIAN } XCalendarType ;
51
52//-------------------------------------------------------------------
53
54#define DECLARE_ATTRIBUTE(type, name) \
55        DECLARE_INTERFACE(axis, type, name)
56#include "../config/axis_attribute.conf"
57
58#undef  DECLARE_ATTRIBUTE
59#define DECLARE_ATTRIBUTE(type, name) \
60        DECLARE_INTERFACE(field, type, name)
61#include "../config/field_attribute.conf"
62
63#undef  DECLARE_ATTRIBUTE
64#define DECLARE_ATTRIBUTE(type, name) \
65        DECLARE_INTERFACE(context, type, name)
66#include "../config/context_attribute.conf"
67
68#undef  DECLARE_ATTRIBUTE
69#define DECLARE_ATTRIBUTE(type, name) \
70        DECLARE_INTERFACE(domain, type, name)
71#include "../config/domain_attribute.conf"
72
73#undef  DECLARE_ATTRIBUTE
74#define DECLARE_ATTRIBUTE(type, name) \
75        DECLARE_INTERFACE(file, type, name)
76#include "../config/file_attribute.conf"
77
78#undef  DECLARE_ATTRIBUTE
79#define DECLARE_ATTRIBUTE(type, name) \
80        DECLARE_INTERFACE(grid, type, name)
81#include "../config/grid_attribute.conf"
82
83//-------------------------------------------------------------------
84
85#define CASE_ELEM(elem_enum, elem_class)                                         \
86   case (E##elem_enum): ;; { ;;                                                  \
87      *_ret = (!CObjectFactory::HasObject<C##elem_class>(__id));;                \
88            ? 0 : CObjectFactory::GetObject<C##elem_class>(__id).get(); ;;       \
89      return; ;;};;                                                              \
90   case (G##elem_enum): ;; { ;;                                                  \
91      *_ret = (!CObjectFactory::HasObject<C##elem_class##Group>(__id));;         \
92            ? 0 : CObjectFactory::GetObject<C##elem_class##Group>(__id).get(); ;;\
93      return; ;;};;
94
95//-------------------------------------------------------------------
96
97void xios_handle_create (XPtr * const _ret, XDType _dtype, const XString _id, XSize _id_len)
98{
99   try
100   {
101      MAKE_STRING(__id, _id, _id_len);
102      switch(_dtype)
103      {
104         case (ECONTEXT):
105        {
106             *_ret = 0;
107            // Si le context n'existe pas, on retourne un handle vide/nul.
108            //*_ret = (!CObjectFactory::HasObject<CContext>(__id))
109            //      ? 0 : CObjectFactory::GetObject<CContext>(__id).get();
110            std::vector<boost::shared_ptr<CContext> > def_vector =
111                CContext::GetContextGroup()->getChildList();
112           for (StdSize i = 0; i < def_vector.size(); i++)
113           {
114              //std::cout << "_ " << def_vector[i]->getId() << std::endl; 
115              if (def_vector[i]->getId().compare(__id) == 0)
116                 *_ret = def_vector[i].get();
117           }
118           //std::cout << __id << "=" << *_ret << std::endl;   
119            return ;
120         }
121         CASE_ELEM(AXIS   , Axis)
122         CASE_ELEM(DOMAIN , Domain)
123         CASE_ELEM(FIELD  , Field)
124         CASE_ELEM(FILE   , File)
125         CASE_ELEM(GRID   , Grid)
126         default :
127            *_ret = 0;
128            return;
129      }
130   }
131   catch (CException & exc)
132   {
133      std::cerr << exc.getMessage() << std::endl;
134      exit (EXIT_FAILURE);
135   }
136}
137
138#undef CASE_ELEM
139
140//-------------------------------------------------------------------
141
142void xios_xml_parse_file  (const XString _filename, XSize _filename_len)
143{
144   try
145   {
146      MAKE_STRING(__filename, _filename, _filename_len)
147      CTreeManager::ParseFile(__filename);
148      //~ StdOStringStream oss;
149      //~ oss << StdString("def_client_pre.")
150      //~     << CMPIManager::GetCommRank(CMPIManager::GetCommWorld());
151      //~ CTreeManager::PrintTreeToFile(oss.str());
152   }
153   catch (CException & exc)
154   {
155      std::cerr << exc.getMessage() << std::endl;
156      exit (EXIT_FAILURE);
157   }
158}
159
160//-------------------------------------------------------------------
161
162void xios_xml_parse_string(const XString _xmlcontent, XSize _xmlcontent_len)
163{
164   try
165   {
166      MAKE_STRING(__xmlcontent, _xmlcontent, _xmlcontent_len)
167      CTreeManager::ParseString(__xmlcontent);
168      //~ StdOStringStream oss;
169      //~ oss << StdString("def_client_pre.")
170      //~     << CMPIManager::GetCommRank(CMPIManager::GetCommWorld());
171      //~ CTreeManager::PrintTreeToFile(oss.str());
172   }
173   catch (CException & exc)
174   {
175      std::cerr << exc.getMessage() << std::endl;
176      exit (EXIT_FAILURE);
177   }
178}
179
180//-------------------------------------------------------------------
181
182void xios_context_set_current (XPtr const _ctx, bool _wswap)
183{
184   try
185   {
186      //boost::shared_ptr<CContext> context = CObjectFactory::GetObject<CContext> ((CContext*)_ctx);
187       CTreeManager::SetCurrentContextId(((CContext*)_ctx)->getId());
188   }
189   catch (CException & exc)
190   {
191      std::cerr << exc.getMessage() << std::endl;
192      exit (EXIT_FAILURE);
193   }
194}
195
196//-------------------------------------------------------------------
197
198void xios_context_create (XPtr * _ctxt, const XString _ctxt_id,
199                          XSize _ctxt_id_len, XCalendarType _calType,
200                          XInt yr, XInt mth, XInt dd,
201                          XInt hr, XInt min, XInt sec)
202{
203   try
204   {
205      MAKE_STRING(__ctxt_id, _ctxt_id, _ctxt_id_len)
206      boost::shared_ptr<CContext> context = CTreeManager::CreateContext(__ctxt_id);
207      *_ctxt = context.get();
208      switch(_calType)
209      {
210         case (D360)     :
211            context->setCalendar(boost::shared_ptr<date::CCalendar>
212               (new date::CD360Calendar(yr, mth, dd, hr, min, sec)));
213            break;
214         case (ALLLEAP)  :
215            context->setCalendar(boost::shared_ptr<date::CCalendar>
216               (new date::CAllLeapCalendar(yr, mth, dd, hr, min, sec)));
217            break;
218         case (NOLEAP)   :
219            context->setCalendar(boost::shared_ptr<date::CCalendar>
220               (new date::CNoLeapCalendar(yr, mth, dd, hr, min, sec)));
221            break;
222         case (JULIAN)   :
223            context->setCalendar(boost::shared_ptr<date::CCalendar>
224               (new date::CJulianCalendar(yr, mth, dd, hr, min, sec)));
225            break;
226         case (GREGORIAN):
227            context->setCalendar(boost::shared_ptr<date::CCalendar>
228               (new date::CGregorianCalendar(yr, mth, dd, hr, min, sec)));
229             break;
230         default:
231            std::cerr << "Le calendrier n'est pas identifié" << std::endl;
232            exit (EXIT_FAILURE);
233      }   
234   }
235   catch (CException & exc)
236   {
237      std::cerr << exc.getMessage() << std::endl;
238      exit (EXIT_FAILURE);
239   }
240}
241
242//-------------------------------------------------------------------
243
244#define CASE_ELEM0(elem_enum, elem_class, group_enum, group_class)                     \
245      case (group_enum) : ;;                                                           \
246      { ;;                                                                             \
247         group_class * _group = (group_class *) _parent;                               \
248         boost::shared_ptr<group_class> group = ;;                                     \
249            CObjectFactory::GetObject<group_class>(_group); ;;                         \
250         switch(_child_type) ;;                                                        \
251         { ;;                                                                          \
252            case (group_enum) : ;;                                                     \
253               if ((_child_id_len != -1) && ;;                                         \
254                   (CObjectFactory::HasObject<group_class>(__child_id))) ;;            \
255                  return; ;;                                                           \
256               *_child = CGroupFactory::CreateGroup(group, __child_id).get(); ;;       \
257               break; ;;                                                               \
258            case (elem_enum) : ;;                                                      \
259               if ((_child_id_len != -1) && ;;                                         \
260                   (CObjectFactory::HasObject<elem_class>(__child_id))) ;;             \
261                  return; ;;                                                           \
262               *_child = CGroupFactory::CreateChild(group, __child_id).get(); ;;       \
263               break; ;;                                                               \
264            default : ;;                                                               \
265               std::cerr << "[xml_tree_add] Type enfant invalide" << std::endl; ;;     \
266         }; ;;                                                                         \
267         return; ;;                                                                    \
268      }
269
270//-------------------------------------------------------------------
271
272void xios_xml_tree_add(const XPtr _parent, XDType _parent_type, XPtr * _child,
273                       XDType _child_type, const XString _child_id, XSize _child_id_len)
274{
275   MAKE_STRING(__child_id, _child_id, _child_id_len);
276   try
277   {
278      switch (_parent_type)
279      {
280         case (EFILE) :
281         {
282            CFile * _file = (CFile *) _parent;
283            boost::shared_ptr<CFile> file = CObjectFactory::GetObject<CFile>(_file);
284            if (!CObjectFactory::HasObject<CFieldGroup>(file->getId()))
285               file->setVirtualFieldGroup(file->getId());
286            xios_xml_tree_add (file->getVirtualFieldGroup().get(), GFIELD,
287                              _child, EFIELD, _child_id, _child_id_len);
288            return;
289         }
290         CASE_ELEM0(EAXIS  , CAxis  , GAXIS  , CAxisGroup);
291         CASE_ELEM0(EGRID  , CGrid  , GGRID  , CGridGroup);
292         CASE_ELEM0(EDOMAIN, CDomain, GDOMAIN, CDomainGroup);
293         CASE_ELEM0(EFIELD , CField , GFIELD , CFieldGroup);
294         CASE_ELEM0(EFILE  , CFile  , GFILE  , CFileGroup);
295         default :
296            std::cerr << "[xml_tree_add] Type parent invalide" << std::endl;
297            return;
298      }
299   }
300   catch (CException & exc)
301   {
302      std::cerr << exc.getMessage() << std::endl;
303      exit (EXIT_FAILURE);
304   }
305}
306
307#undef CASE_ELEM0
308
309//-------------------------------------------------------------------
310
311void xios_xml_tree_show(const XString _filename, XSize _filename_len)
312{
313   MAKE_STRING(__filename, _filename, _filename_len);
314   try
315   {
316      if (_filename_len != -1)
317         CTreeManager::PrintTreeToFile(__filename);
318      else
319         CTreeManager::PrintTreeToStream(std::clog);
320   }
321   catch (CException & exc)
322   {
323      std::cerr << exc.getMessage() << std::endl;
324      exit (EXIT_FAILURE);
325   }
326}
327
328//-------------------------------------------------------------------
329
330void xios_init_ioserver(MPIComm * comm_client)
331{
332   try
333   {
334      MPIComm comm_client_server, comm_server;
335      CMPIManager::Initialise(NULL, NULL);
336      CMPIManager::DispatchClient(false, *comm_client, comm_client_server, comm_server);
337   }
338   catch (CException & exc)
339   {
340      std::cerr << exc.getMessage() << std::endl;
341      exit (EXIT_FAILURE);
342   }
343}
344
345
346//-------------------------------------------------------------------
347
348void xios_dtreatment_start
349   (XPtr const _context, XFileType filetype, MPIComm comm_client_server)
350{
351   try
352   {
353      CContext * __context = (CContext *) _context;
354      boost::shared_ptr<CContext> context =
355             CObjectFactory::GetObject<CContext>(__context);
356      StdOStringStream oss;
357      oss << StdString("def_client_next.")
358          << CMPIManager::GetCommRank(CMPIManager::GetCommWorld());
359      CTreeManager::PrintTreeToFile(oss.str());
360      oss.str("");
361
362      boost::shared_ptr<CDataTreatment> dt(new CDataTreatment (context));
363      context->setDataTreatment(dt);
364
365      oss << StdString("def_client_end.")
366          << CMPIManager::GetCommRank(CMPIManager::GetCommWorld());
367      CTreeManager::PrintTreeToFile(oss.str());
368     
369      if ((comm_client_server != -1))
370      {
371         MPIRequest request = 0;
372         StdOStringStream ostrs;
373         if (CMPIManager::GetCommRank(comm_client_server) == 1)
374         {
375            CTreeManager::ToBinary(ostrs);     
376            CLinearBuffer lbuffer(ostrs.str().size()+13);           
377            lbuffer.appendString(ostrs.str());
378            CMPIManager::SendLinearBuffer(comm_client_server, 0, lbuffer, request);
379            CMPIManager::Wait(request);  // Pas encore en mode RPC
380         }
381         else
382         {
383            CTreeManager::DomainsToBinary(ostrs);
384            CLinearBuffer lbuffer(ostrs.str().size()+13);
385            lbuffer.appendString(ostrs.str());
386            CMPIManager::SendLinearBuffer(comm_client_server, 0, lbuffer, request);
387            CMPIManager::Wait(request);  // Pas encore en mode RPC
388         }
389      }
390      else
391      {
392         dt->createDataOutput<CNc4DataOutput>();
393      }     
394   }
395   catch (CException & exc)
396   {
397      std::cerr << exc.getMessage() << std::endl;
398      exit (EXIT_FAILURE);
399   }
400}
401
402//-------------------------------------------------------------------
403
404void xios_dtreatment_end(void)
405{
406   try
407   {
408           CMPIManager::Finalize();
409   }
410   catch (CException & exc)
411   {
412      std::cerr << exc.getMessage() << std::endl;
413      exit (EXIT_FAILURE);
414   }
415}
416
417//-------------------------------------------------------------------
418
419void xios_write_data(const XString _field_id, XSize _field_id_len, double * data_k8,
420                     XSize data_Xsize, XSize data_Ysize, XSize data_Zsize)
421{
422   MAKE_STRING(__field_id, _field_id, _field_id_len);
423   try
424   {
425      boost::shared_ptr<CContext> context =
426      CObjectFactory::GetObject<CContext>(CObjectFactory::GetCurrentContextId());
427      boost::shared_ptr<data::CDataTreatment> dtreat = context->getDataTreatment();
428      if (data_Ysize==-1 && data_Zsize==-1)
429      {
430         ARRAY(double, 1) data(new CArray<double, 1>(boost::extents [data_Xsize]));
431         std::copy(data_k8, data_k8+data->num_elements(), data->data());
432         dtreat->write_data(__field_id, data);
433         return;
434      }
435      else if (data_Zsize==-1)
436      {
437         ARRAY(double, 2) data(new CArray<double, 2>(boost::extents [data_Xsize][data_Ysize]));
438         std::copy(data_k8, data_k8+data->num_elements(), data->data());
439         dtreat->write_data(__field_id, data);
440         return;
441      }
442      else
443      {
444         ARRAY(double, 3) data(new CArray<double, 3>(boost::extents [data_Xsize][data_Ysize][data_Zsize]));
445         std::copy(data_k8, data_k8+data->num_elements(), data->data());
446         dtreat->write_data(__field_id, data);
447         return;
448      }
449   }
450   catch (CException & exc)
451   {
452      std::cerr << exc.getMessage() << std::endl;
453      exit (EXIT_FAILURE);
454   }
455}
456
457//-------------------------------------------------------------------
458
459void xios_write_data_f(const XString _field_id, XSize _field_id_len, float * data_k4,
460                       XSize data_Xsize, XSize data_Ysize, XSize data_Zsize)
461{
462   MAKE_STRING(__field_id, _field_id, _field_id_len);
463   try
464   {
465      boost::shared_ptr<CContext> context =
466      CObjectFactory::GetObject<CContext>(CObjectFactory::GetCurrentContextId());
467      boost::shared_ptr<data::CDataTreatment> dtreat = context->getDataTreatment();
468      if (data_Ysize==-1 && data_Zsize==-1)
469      {
470         ARRAY(float, 1) data(new CArray<float, 1>(boost::extents [data_Xsize]));
471         std::copy(data_k4, &(data_k4[data->num_elements()]), data->data());
472         dtreat->write_data(__field_id, data);
473         return;
474      }
475      else if (data_Zsize==-1)
476      {
477         ARRAY(float, 2) data(new CArray<float, 2>(boost::extents [data_Xsize][data_Ysize]));
478         std::copy(data_k4, &(data_k4[data->num_elements()]), data->data());
479         dtreat->write_data(__field_id, data);
480         return;
481      }
482      else
483      {
484         ARRAY(float, 3) data(new CArray<float, 3>(boost::extents [data_Xsize][data_Ysize][data_Zsize]));
485         std::copy(data_k4, &(data_k4[data->num_elements()]), data->data());
486         dtreat->write_data(__field_id, data);
487         return;
488      }
489   }
490   catch (CException & exc)
491   {
492      std::cerr << exc.getMessage() << std::endl;
493      exit (EXIT_FAILURE);
494   }
495}
496
497
498//-------------------------------------------------------------------
499
500void xios_update_calendar(int step)
501{
502   try
503   {
504      boost::shared_ptr<CContext> context =
505      CObjectFactory::GetObject<CContext>(CObjectFactory::GetCurrentContextId());
506      boost::shared_ptr<data::CDataTreatment> dtreat = context->getDataTreatment();
507      dtreat->update_calendar(step);
508   }
509   catch (CException & exc)
510   {
511      std::cerr << exc.getMessage() << std::endl;
512      exit (EXIT_FAILURE);
513   }
514}
515
516//-------------------------------------------------------------------
517
518void xios_set_timestep(double ts_year, double ts_month , double ts_day,
519                       double ts_hour, double ts_minute, double ts_second)
520{
521   try
522   {
523      date::CDuration dur = {ts_year, ts_month, ts_day, ts_hour, ts_minute, ts_second};
524      boost::shared_ptr<CContext> context =
525      CObjectFactory::GetObject<CContext>(CObjectFactory::GetCurrentContextId());
526      boost::shared_ptr<data::CDataTreatment> dtreat = context->getDataTreatment();
527      dtreat->set_timestep(dur);
528   }
529   catch (CException & exc)
530   {
531      std::cerr << exc.getMessage() << std::endl;
532      exit (EXIT_FAILURE);
533   }
534}
535
536//-------------------------------------------------------------------
537
538#ifdef __cplusplus
539}
540#endif //__cplusplus
Note: See TracBrowser for help on using the repository browser.