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 
20 #include <med.h>
21 #include <med_config.h>
22 #include <med_outils.h>
23 #include <hdf5.h>
24 #include <string.h>
25 
_MEDattributeNumWrByName(med_idt pid,const char * const path,const char * const attname,const med_internal_type type,const unsigned char * const val)26 med_err _MEDattributeNumWrByName(med_idt pid,
27 				 const char * const path ,
28 				 const char * const attname,
29 				 const med_internal_type type,
30 				 const unsigned char * const  val)
31 
32 {
33   med_idt _attid=0,aid=0;
34   med_err _ret=-1;
35   med_idt  type_hdf;
36   med_access_mode MED_ACCESS_MODE;
37   H5O_info_t      _oinfo;
38 
39   if ( (MED_ACCESS_MODE = _MEDmodeAcces(pid) ) == MED_ACC_UNDEF ) {
40     MED_ERR_(_ret,MED_ERR_INVALID,MED_ERR_ACCESSMODE, "MED_ACC_UNDEF" );
41     SSCRUTE(attname); SSCRUTE(path);goto ERROR;
42   }
43 
44   switch(type)
45     {
46     case MED_INTERNAL_FLOAT64 :
47       type_hdf = H5T_NATIVE_DOUBLE;
48       break;
49 
50     case MED_INTERNAL_INT :
51 #if defined(HAVE_F77INT64)
52       type_hdf = H5T_NATIVE_LONG;
53 #else
54       type_hdf = H5T_NATIVE_INT;
55 #endif
56       break;
57 
58     default :
59       MED_ERR_(_ret,MED_ERR_INVALID,MED_ERR_DATATYPE, MED_ERR_VALUE_MSG );
60       ISCRUTE_int(type); SSCRUTE(attname); SSCRUTE(path);goto ERROR;
61     }
62 
63   if ((aid = H5Screate(H5S_SCALAR)) < 0){
64     MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_DATASPACE, attname );
65     ISCRUTE_id(aid);
66   }
67 
68   if  ( (_attid=H5Aopen_by_name( pid, path, attname, H5P_DEFAULT, H5P_DEFAULT )) >= 0 ) {
69 
70     if ( H5Oget_info( pid, &_oinfo ) <0) {
71 	MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"H5Oget_info");
72 	goto ERROR;
73     }
74 
75     if ( MED_ACCESS_MODE == MED_ACC_RDEXT ) {
76       if ( ( _oinfo.type != H5O_TYPE_GROUP) ||
77 	   ( (_oinfo.type == H5O_TYPE_GROUP) &&
78 	     ( strcmp(attname,MED_NOM_CGT) &&
79 	       strcmp(attname,MED_NOM_CGS) &&
80 	       strcmp(attname,MED_NOM_NXT) &&
81 	       strcmp(attname,MED_NOM_NXI) &&
82 	       strcmp(attname,MED_NOM_PVI) &&
83 	       strcmp(attname,MED_NOM_PVT) ) )
84 	   ) {      MED_ERR_(_ret,MED_ERR_INVALID,MED_ERR_ACCESSMODE, "MED_ACC_RDEXT" );
85 	SSCRUTE(attname); SSCRUTE(path);goto ERROR;
86       }
87     }
88 
89   } else {
90     if ( (_attid=H5Acreate_by_name( pid, path, attname, type_hdf, aid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT )) < 0 ) {
91       MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_ATTRIBUTE, attname );
92       SSCRUTE(path);goto ERROR;
93     }
94   }
95 
96   if ( H5Awrite(_attid,type_hdf,val) < 0) {
97     MED_ERR_(_ret,MED_ERR_WRITE,MED_ERR_ATTRIBUTE, attname );
98     SSCRUTE(path);H5Eprint1(stderr);goto ERROR;
99   }
100 
101   _ret=0;
102 
103  ERROR:
104 
105   if (aid > 0 ) if ( H5Sclose(aid) < 0) {
106     MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_DATASPACE, MED_ERR_ID_MSG );
107     ISCRUTE_id(aid);
108   }
109 
110   if (_attid > 0 ) if ( H5Aclose(_attid) < 0) {
111     MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_ATTRIBUTE, MED_ERR_ID_MSG );
112     ISCRUTE_id(_attid);
113   }
114 
115   return _ret;
116 
117 }
118