1 /*  This file is part of MED.
2  *
3  *  COPYRIGHT (C) 1999 - 2019  EDF R&D, CEA/DEN
4  *  MED is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU Lesser General Public License as published by
6  *  the Free Software Foundation, either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  MED is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU Lesser General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Lesser General Public License
15  *  along with MED.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 
19 #include <med.h>
20 #include <med_config.h>
21 #include <med_outils.h>
22 #include <string.h>
23 #include <stdlib.h>
24 
25 /* Crée ou ouvre une structure de la forme
26   /<meddatatypename>/<meshname>/<PDT,IT|DEFAULT>/<groupname>/
27 */
28 
29 med_idt
_MEDmeshAssociatedGroupCr(const med_idt fid,const char * const rootname,const char * const meshname,const med_int numdt,const med_int numit,const med_float dt,const med_bool justopen,const char * const datagroupname)30 _MEDmeshAssociatedGroupCr(const med_idt               fid,
31 			  const char*  const          rootname,
32 			  const char*  const          meshname,
33 			  const med_int               numdt,
34 			  const med_int               numit,
35 			  const med_float             dt,
36  			  const med_bool              justopen,
37 			  const char*  const          datagroupname )
38 
39 {
40   med_access_mode       _MED_ACCESS_MODE;
41   med_idt               _ret=-1;
42   med_idt               _root=0,_meshid=0,_datagroup1=0,_datagroup2=0,_datagroup3=0;
43   char                  _datagroupname2   [2*MED_MAX_PARA+1]="";
44   med_sorting_type      _sortingtype=0;
45   med_bool              _datagp3created = MED_FALSE;
46 
47  /*
48    * On inhibe le gestionnaire d'erreur HDF 5
49    */
50   _MEDmodeErreurVerrouiller();
51 if (_MEDcheckVersion30(fid) < 0) goto ERROR;
52 
53   if ( (_MED_ACCESS_MODE = _MEDmodeAcces(fid) ) == MED_ACC_UNDEF ) {
54     MED_ERR_(_ret,MED_ERR_UNRECOGNIZED,MED_ERR_ACCESSMODE,MED_ERR_FILE_MSG);
55     goto ERROR;
56   }
57 
58   if (! justopen) {
59     if ( _MED_ACCESS_MODE == MED_ACC_RDONLY ) {
60       MED_ERR_(_ret,MED_ERR_INVALID,MED_ERR_ACCESSMODE,MED_ERR_FILE_MSG);
61       ISCRUTE_int(_MED_ACCESS_MODE);
62       goto ERROR;
63     }
64   }
65 
66   /*
67    * Récupère l'ordre de tri des séquences de calcul
68    * Si le DataGroup MED_MESH_GRP/<meshname> n'existe pas => erreur
69    * il doit être crée par MEDmeshCr
70    */
71   NOFINALBLANK(meshname,ERROR);
72   NOFINALBLANK(datagroupname,ERROR);
73 
74   if ( MEDmeshSortingTypeRd(fid,meshname,&_sortingtype) < 0 ) {
75     MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API," MEDmeshSortingTypeRd");
76     SSCRUTE(meshname);goto ERROR;
77   }
78 
79   /*
80    * Ouverture du DataGroup rootname
81    */
82 
83   if ((_root = _MEDdatagroupOuvrir(fid,rootname)) < 0) {
84     MED_ERR_(_ret,MED_ERR_OPEN,MED_ERR_DATAGROUP,rootname);
85     goto ERROR;
86   }
87 
88   /*
89    * Si le Data Group <meshname> n'existe pas, on le cree
90    */
91   if ((_datagroup1 = _MEDdatagroupOuvrir(_root,meshname)) < 0) {
92     MED_ERR_(_ret,MED_ERR_OPEN,MED_ERR_DATAGROUP,meshname);
93     goto ERROR;
94   }
95 
96   _MEDgetComputationStepName(_sortingtype,numdt,numit,_datagroupname2);
97 
98   /*
99    * Creation/Ouverture du datagroup <numdt>.<numit>
100    */
101   if ( (_datagroup2 = _MEDdatagroupOuvrir(_datagroup1,_datagroupname2)) < 0 ) {
102     if (justopen) {
103       MED_ERR_(_ret,MED_ERR_OPEN,MED_ERR_COMPUTINGSTEP,MED_ERR_MESH_MSG);
104       SSCRUTE(meshname);ISCRUTE(numit);ISCRUTE(numdt);goto ERROR;
105     }
106     if ( MEDmeshComputationStepCr(fid,meshname,numdt,numit,numdt,numit,dt) < 0 ) {
107       MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_COMPUTINGSTEP,MED_ERR_MESH_MSG);
108       SSCRUTE(meshname);ISCRUTE(numit);ISCRUTE(numdt);goto ERROR;
109     } else {
110       if ( (_datagroup2 = _MEDdatagroupOuvrir(_datagroup1,_datagroupname2)) < 0 ) {
111 	MED_ERR_(_ret,MED_ERR_OPEN,MED_ERR_DATAGROUP,MED_ERR_MESH_MSG);
112 	SSCRUTE(meshname);ISCRUTE(numit);ISCRUTE(numdt);
113 	ISCRUTE_id(_datagroup2);goto ERROR;
114       }
115     }
116   }
117 
118   /*
119    * Si le Data Group <datagroupname> n'existe pas, on le cree
120    */
121   if ((_datagroup3 = _MEDdatagroupOuvrir(_datagroup2,datagroupname)) < 0) {
122     if (justopen) {
123       MED_ERR_(_ret,MED_ERR_OPEN,MED_ERR_DATAGROUP,MED_ERR_MESH_MSG);
124       SSCRUTE(meshname);ISCRUTE(numit);ISCRUTE(numdt);SSCRUTE(datagroupname);
125       goto ERROR;
126     }
127     if ((_datagroup3 = _MEDdatagroupCreer(_datagroup2,datagroupname)) < 0 ) {
128       MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_DATAGROUP,datagroupname);
129       goto ERROR;
130     }
131     else
132       _datagp3created = MED_TRUE;
133   }
134 
135   _ret = _datagroup3;
136 
137  ERROR:
138 
139   if ( (_datagroup3 >0) && ( _datagp3created ) )
140     if (_MEDdatagroupFermer(_datagroup3) < 0) {
141       MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_DATAGROUP,datagroupname);
142       ISCRUTE_id(_datagroup3);
143     }
144 
145   if (_datagroup2>0)     if (_MEDdatagroupFermer(_datagroup2) < 0) {
146     MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_DATAGROUP,_datagroupname2);
147     ISCRUTE_id(_datagroup2);
148   }
149 
150   if (_datagroup1>0)     if (_MEDdatagroupFermer(_datagroup1) < 0) {
151     MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_DATAGROUP,meshname);
152     ISCRUTE_id(_datagroup1);
153   }
154 
155   if (_root>0)     if (_MEDdatagroupFermer(_root) < 0) {
156     MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_DATAGROUP,rootname);
157     ISCRUTE_id(_root);
158   }
159 
160 
161   return _ret;
162 
163 }
164