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 <hdf5.h>
23 
24 
_MEDfileOpen(const char * const filename,const med_access_mode accessmode)25 med_idt _MEDfileOpen(const char * const filename,const med_access_mode accessmode)
26 {
27 
28   med_idt _fid =-1;
29   int     _hdf_mode=-1;
30   hid_t   _fapl    = H5P_DEFAULT;
31 
32   /* H5AC_cache_config_t config; */
33 
34   switch(accessmode)
35     {
36     case MED_ACC_RDWR :
37     case MED_ACC_RDEXT    :
38       _hdf_mode = MED_ACC_RDWR;
39       break;
40 
41     case MED_ACC_RDONLY :
42       _hdf_mode = H5F_ACC_RDONLY;
43       break;
44 
45     default :
46       MED_ERR_(_fid,MED_ERR_RANGE,MED_ERR_ACCESS,filename);
47       goto ERROR;
48     }
49 
50   if ( (_fapl = H5Pcreate (H5P_FILE_ACCESS)) < 0 ) {
51     MED_ERR_(_fid,MED_ERR_CREATE,MED_ERR_PROPERTY,MED_ERR_FILEVERSION_MSG);
52     goto ERROR;
53   }
54 
55 
56   /* Cette ligne, présente depuis la 3.0, impose l'utilisation du modèle de données HDF 1.8 pour :
57      - Utiliser les nouvelles représentations HDF plus efficaces que dans les versions précédentes
58      - Empêcher l'utilisation de nvlles représentations d'une future bibliothèque HDF 1.10
59        qui poseait d'eventuels problèmes de relecture aux bibliothèques med
60        utilisant encore la 1.8 (ce choix doit être manuel) :
61     Les fichier HDF 1.8 utilisent la nouvelle représentation des liens au sein des groupes :
62     compact (header) ou dense (hors header)
63   */
64   /* HDF-5 : UG
65     Groups will be initially created in the compact‐or‐indexed format only when one or more of the following
66     conditions is met:
67    •    The low version bound value of the library version bounds property has been set to Release 1.8.0
68         or later in the file access property list (see H5Pset_libver_bounds). Currently, that would
69         require an H5Pset_libver_bounds call with the low parameter set to H5F_LIBVER_LATEST.
70         When this property is set for an HDF5 file, all objects in the file will be created using the latest
71         available format; no effort will be made to create a file that can be read by older libraries.
72 
73    •   The creation order tracking property, H5P_CRT_ORDER_TRACKED, has been set in the group creation property list (see H5Pset_link_creation_order).
74   */
75 #if H5_VERS_MINOR > 10
76 #error "Don't forget to change the compatibility version of the library !"
77 #endif
78 /* L'avantage de bloquer le modèle interne HDF5
79    est que l'on peut modifier des fichiers med de différentes versions majeures de fichiers.
80    L'inconvénient est que l'on ne profite pas des évolutions de performances d'HDF.
81 */
82   if ( H5Pset_libver_bounds( _fapl, H5F_LIBVER_18, H5F_LIBVER_18 ) ) {
83     MED_ERR_(_fid,MED_ERR_INIT,MED_ERR_PROPERTY,MED_ERR_FILEVERSION_MSG);
84     goto ERROR;
85   }
86 
87   if ((_fid = H5Fopen(filename,_hdf_mode,_fapl)) < 0) {
88     /*La gestion de l'affichage des erreurs se fait dans la couche supérieure*/
89     /*cela permet de tester l'ouverture du fichier (cf MEDfileCompatibility) sans provoquer
90      d'affichage intempestif.*/
91     _fid = MED_ERR_OPEN MED_ERR_FILE;
92     /* MED_ERR_(_fid,MED_ERR_OPEN,MED_ERR_FILE,""); */
93     /* ISCRUTE_int(accessmode); */
94     /* Ne pas activer la ligne suivante en production, car certains code
95        utlisent MEDfileOpen pour tester la présence d'un fichier */
96     /*    H5Eprint1(stderr); */
97     goto ERROR;
98   }
99 
100 
101   if ( H5Pclose(_fapl) < 0 ) {
102     MED_ERR_(_fid,MED_ERR_CLOSE,MED_ERR_PROPERTY,"");
103     _fid=-1;goto ERROR;
104   }
105 
106 /* Adjust the size of metadata cache */
107 /* config.version = H5AC__CURR_CACHE_CONFIG_VERSION; */
108 /* H5Fget_mdc_config(_fid, &config); */
109 /* config.set_initial_size = 1; */
110 /* config.initial_size = 8*1024*1024; */
111 /* config.max_size = 16*1024*1024; */
112 /* H5Fset_mdc_config(_fid, &config); */
113 
114   _MEDsetModeAcces(_fid,accessmode);
115   /* Si le fichier _fid ne possède pas la structure MED_INFOS;
116      le fichier est considéré en version 0.0.0 mais n'est pas inscrit
117      en cache de version.
118   */
119   _MEDfileVersion(_fid);
120 
121  ERROR:
122 
123   return _fid;
124 }
125