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 
23 #include <string.h>
24 #include <stdlib.h>
25 
26 /**\ingroup MEDlocalization
27   \brief \MEDlocalizationWrBrief
28   \param fid \fid
29   \param localizationname \localizationname
30   \param geotype \geotype
31   \param spacedimension \spacedimension
32   \param elementcoordinate \elementcoordinate
33   \param switchmode \switchmode
34   \param nipoint \nipoint
35   \param ipointcoordinate \ipointcoordinate
36   \param weight \weight
37   \param geointerpname        \geointerpname
38   \param sectionmeshname \sectionmeshname
39   \retval med_err  \error
40   \details \MEDlocalizationWrDetails
41   \par Définition
42   \MEDlocalizationDef
43   \remarks
44   \MEDlocalizationRem
45   \MEDlocalizationWrRem
46  */
47 
48 med_err
MEDlocalizationWr(const med_idt fid,const char * const localizationname,const med_geometry_type geotype,const med_int spacedimension,const med_float * const elementcoordinate,const med_switch_mode switchmode,const med_int nipoint,const med_float * const ipointcoordinate,const med_float * const weight,const char * const geointerpname,const char * const sectionmeshname)49 MEDlocalizationWr(const med_idt           fid,
50 		  const char *     const  localizationname,
51 		  const med_geometry_type geotype,
52 		  const med_int           spacedimension,
53 		  const med_float* const  elementcoordinate,
54 		  const med_switch_mode   switchmode,
55 		  const med_int           nipoint,
56 		  const med_float* const  ipointcoordinate,
57 		  const med_float* const  weight,
58 		  const char *     const  geointerpname,
59 		  const char *     const  sectionmeshname
60 		  )
61 {
62   med_access_mode _MED_ACCESS_MODE;
63   med_err _ret = -1;
64   med_idt _lzid=0, _root=0;
65   med_int _nentity=0;
66   med_int _intgeotype = -1;
67   char    _path[MED_LOCALIZATION_GRP_SIZE+MED_NAME_SIZE+1]=MED_LOCALIZATION_GRP;
68   med_filter     _filter        = MED_FILTER_INIT;
69 
70   /*
71    * On inhibe le gestionnaire d'erreur HDF 5
72    */
73   _MEDmodeErreurVerrouiller();
74  if (_MEDcheckVersion30(fid) < 0) goto ERROR;
75 
76   if ( (_MED_ACCESS_MODE = _MEDmodeAcces(fid) ) == MED_ACC_UNDEF ) {
77     MED_ERR_(_ret,MED_ERR_UNRECOGNIZED,MED_ERR_ACCESSMODE,MED_ERR_FILE_MSG);
78     goto ERROR;
79   }
80 
81   if ( _MED_ACCESS_MODE == MED_ACC_RDONLY) {
82     MED_ERR_(_ret,MED_ERR_INVALID,MED_ERR_ACCESSMODE,MED_ERR_FILE_MSG);
83     ISCRUTE_int(_MED_ACCESS_MODE);
84     goto ERROR;
85   }
86 
87   /*
88    * MED_GAUSS_ELNO est un mot cle reserve
89    */
90   if (! strcmp(localizationname,MED_GAUSS_ELNO)) {
91     MED_ERR_(_ret,MED_ERR_INVALID,MED_ERR_LOCALIZATION,localizationname);
92     goto ERROR;
93   }
94 
95   /*
96    * Si le DataGroup /GAUSS/ n'existe pas, on le cree
97    */
98   if ((_root = _MEDdatagroupOuvrir(fid,_path)) < 0)
99     if ((_root = _MEDdatagroupCreer(fid,_path)) < 0) {
100       MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_DATAGROUP,_path);
101      goto ERROR;
102     }
103 
104   NOFINALBLANK(localizationname,ERROR);
105   /*
106    * Si le DataGroup /GAUSS/<localizationname> n'existe pas, on le cree
107    */
108   if ((_lzid = _MEDdatagroupOuvrir(_root,localizationname)) < 0)
109     if ((_lzid = _MEDdatagroupCreer(_root,localizationname)) < 0) {
110       MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_DATAGROUP,localizationname);
111       SSCRUTE(_path);goto ERROR;
112     }
113 
114   strcat(_path,localizationname);
115 
116 
117   /*
118    * On stocke <nipoint> sous forme d'attribut
119    */
120   if (_MEDattributeIntWr(_lzid,MED_NOM_NBR,&nipoint) < 0) {
121     MED_ERR_(_ret,MED_ERR_WRITE,MED_ERR_ATTRIBUTE,MED_ERR_LOCALIZATION_MSG);
122     SSCRUTE(localizationname);SSCRUTE(MED_NOM_NBR);ISCRUTE(nipoint);
123     goto ERROR;
124   }
125 
126 
127   /*
128    * On stocke <typgeo> sous forme d'attribut
129    */
130   _intgeotype = (med_int) geotype;
131   /* sizeof(enum) tjrs = sizeof(int) en C, or
132      sur machines 64 bits par d�faut med_int==long,
133      du coup sur  machines 64 bits _MEDattributeIntWr utilise
134      le type hdf NATIVE_LONG, ce qui pose un probl�me qd on passe
135      un enum.
136   */
137   if (_MEDattributeIntWr(_lzid,MED_NOM_GEO,&_intgeotype) < 0) {
138     MED_ERR_(_ret,MED_ERR_WRITE,MED_ERR_ATTRIBUTE,MED_ERR_LOCALIZATION_MSG);
139     SSCRUTE(localizationname);SSCRUTE(MED_NOM_GEO);ISCRUTE(_intgeotype);
140     goto ERROR;
141   };
142 
143   /*
144    * On stocke <spacedimension> sous forme d'attribut
145    */
146   if (_MEDattributeIntWr(_lzid,MED_NOM_DIM,&spacedimension) < 0) {
147     MED_ERR_(_ret,MED_ERR_WRITE,MED_ERR_ATTRIBUTE,MED_ERR_LOCALIZATION_MSG);
148     SSCRUTE(localizationname);SSCRUTE(MED_NOM_DIM);ISCRUTE(spacedimension);
149     goto ERROR;
150   };
151 
152 
153   if ( ! ((geotype > MED_STRUCT_GEO_INTERNAL    ) &&
154 	  (geotype < MED_STRUCT_GEO_SUP_INTERNAL)    ) ) {
155     /*
156      * On stocke les coordonn�es de r�f�rence dans un dataset
157      */
158     _nentity = (geotype%100);
159 
160     if ( MEDfilterEntityCr(fid,_nentity, 1, spacedimension, MED_ALL_CONSTITUENT,
161 			   switchmode,MED_UNDEF_STMODE,
162 			   MED_NO_PROFILE, MED_UNDEF_SIZE, NULL, &_filter) < 0 ) {
163       MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_FILTER,MED_ERR_INTERNAL_MSG);
164       goto ERROR;
165     }
166 
167     if ( _MEDdatasetWr(_lzid,MED_NOM_COO,MED_INTERNAL_FLOAT64,&_filter, elementcoordinate) < 0) {
168       MED_ERR_(_ret,MED_ERR_WRITE,MED_ERR_DATASET,MED_NOM_COO);SSCRUTE(_path);
169       goto ERROR;
170     }
171 
172     if ( MEDfilterClose(&_filter) < 0 ) {
173       MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_FILTER,MED_ERR_LOCALIZATION_MSG); SSCRUTE(_path);
174       goto ERROR;
175     }
176   } else {
177 
178     /*
179      * Creation/Ecriture de l'attribut MED_NOM_NOM (nom du maillage de section)
180      */
181     if ( _MEDattributeStringWr(_lzid,MED_NOM_NOM,MED_NAME_SIZE,sectionmeshname) < 0) {
182       MED_ERR_(_ret,MED_ERR_WRITE,MED_ERR_ATTRIBUTE,MED_ERR_LOCALIZATION_MSG);
183       SSCRUTE(localizationname);SSCRUTE(MED_NOM_NOM);SSCRUTE(sectionmeshname);
184       goto ERROR;
185     }
186   }
187 
188   /*
189    * Creation/Ecriture de l'attribut MED_NOM_INM (nom de l'interpolation géométrique)
190    */
191   if ( _MEDattributeStringWr(_lzid,MED_NOM_INM,MED_NAME_SIZE,geointerpname) < 0) {
192     MED_ERR_(_ret,MED_ERR_WRITE,MED_ERR_ATTRIBUTE,MED_ERR_LOCALIZATION_MSG);
193     SSCRUTE(localizationname);SSCRUTE(MED_NOM_INM);SSCRUTE(geointerpname);
194     goto ERROR;
195   }
196 
197 
198   /*
199    * On stocke les points d'int�gration dans un dataset
200    */
201 
202   _nentity = nipoint;
203   if ( MEDfilterEntityCr(fid,_nentity, 1, spacedimension, MED_ALL_CONSTITUENT,
204 			 switchmode,MED_UNDEF_STMODE,
205 			 MED_NO_PROFILE, MED_UNDEF_SIZE, NULL, &_filter) < 0 ) {
206     MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_FILTER,MED_ERR_INTERNAL_MSG);
207     goto ERROR;
208   }
209 
210   if ( _MEDdatasetWr(_lzid,MED_NOM_GAU,MED_INTERNAL_FLOAT64,&_filter, ipointcoordinate) < 0) {
211     MED_ERR_(_ret,MED_ERR_WRITE,MED_ERR_DATASET,MED_NOM_GAU);SSCRUTE(_path);
212     goto ERROR;
213   }
214 
215   if ( MEDfilterClose(&_filter) < 0 ) {
216     MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_FILTER,MED_ERR_LOCALIZATION_MSG); SSCRUTE(_path);
217     goto ERROR;
218   }
219 
220   /*
221    * On stocke les poids dans un dataset
222    */
223 
224   _nentity = nipoint;
225   if ( MEDfilterEntityCr(fid,_nentity, 1, 1, MED_ALL_CONSTITUENT,
226 			 switchmode,MED_UNDEF_STMODE,
227 			 MED_NO_PROFILE, MED_UNDEF_SIZE, NULL, &_filter) < 0 ) {
228     MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_FILTER,MED_ERR_INTERNAL_MSG);
229     goto ERROR;
230   }
231 
232   if ( _MEDdatasetWr(_lzid,MED_NOM_VAL,MED_INTERNAL_FLOAT64,&_filter, weight) < 0) {
233     MED_ERR_(_ret,MED_ERR_WRITE,MED_ERR_DATASET,MED_NOM_VAL);SSCRUTE(_path);
234     goto ERROR;
235   }
236 
237   if ( MEDfilterClose(&_filter) < 0 ) {
238     MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_FILTER,MED_ERR_LOCALIZATION_MSG); SSCRUTE(_path);
239     goto ERROR;
240   }
241 
242 
243   _ret = 0;
244 
245  ERROR:
246 
247   if (_lzid>0)            if (_MEDdatagroupFermer(_lzid) < 0) {
248     MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_DATAGROUP,localizationname);
249     ISCRUTE_id(_lzid);
250   }
251 
252   if (_root>0)            if (_MEDdatagroupFermer(_root) < 0) {
253     MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_DATAGROUP,MED_LOCALIZATION_GRP);
254     ISCRUTE_id(_root);
255   }
256 
257   return _ret;
258 
259 }
260 
261