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 <stdlib.h>
24 #include <string.h>
25 
26 med_err
MEDindicesCoordEcr(med_idt fid,char * maillage,med_int mdim,med_float * indices,med_int n,med_int axe,char * comp,char * unit)27 MEDindicesCoordEcr(med_idt fid,char *maillage,med_int mdim,med_float *indices,
28 		   med_int n,med_int axe,char *comp,char *unit)
29 {
30   med_idt maaid, noeid, dataset;
31   med_err ret;
32   med_size dimd[1];
33   char chemin[MED_TAILLE_MAA+MED_TAILLE_NOM+1];
34   char nom_dataset[MED_TAILLE_NOM_ENTITE+1];
35   med_int att;
36   med_maillage maillage_type;
37   med_type_grille type;
38 
39   /*
40    * Si axe > mdim => erreur
41    */
42   if (axe > mdim)
43     return -1;
44 
45   /*
46    * On inhibe le gestionnaire d'erreur HDF
47    */
48   _MEDmodeErreurVerrouiller();
49 if (MEDcheckVersion(fid) < 0) return -1;
50 
51 
52   /*
53    * Si le maillage n'existe pas => erreur
54    */
55   strcpy(chemin,MED_MAA);
56   strcat(chemin,maillage);
57   if ((maaid = _MEDdatagroupOuvrir(fid,chemin)) < 0)
58       return -1;
59 
60   /*
61    * Si le maillage est de type MED_NON_STRUCTURE => erreur
62    */
63   if ((ret = _MEDattrEntierLire(maaid,MED_NOM_TYP,&att)) < 0)
64     return -1;
65   maillage_type = (med_maillage) att;
66   if (maillage_type == MED_NON_STRUCTURE)
67     return -1;
68 
69   /*
70    * Si la grille n'est pas de type MED_GRILLE_CARTESIENNE ou
71    * MED_GRILLE_POLAIRE => erreur
72    */
73   if ((ret = _MEDattrEntierLire(maaid,MED_NOM_GTY,&att)) < 0)
74     return -1;
75   /* sizeof(enum) tjrs = sizeof(int) en C, or
76      sur machines 64 bits par défaut med_int==long (normalement 64bits),
77      du coup sur  machines 64 bits _MEDattrEntierLire utilise
78      le type hdf NATIVE_LONG, ce genere parfois un warning
79   */
80   type = (med_type_grille) att;
81   if ((type != MED_GRILLE_CARTESIENNE) && (type != MED_GRILLE_POLAIRE))
82     return -1;
83 
84   /*
85    * Si le groupe HDF "NOE" n'existe pas => erreur
86    */
87   if ((noeid = _MEDdatagroupOuvrir(maaid,MED_NOM_NOE)) < 0)
88     if ((noeid = _MEDdatagroupCreer(maaid,MED_NOM_NOE)) < 0)
89       return -1;
90 
91   /*
92    * On ecrit le tableau d'indice dans un dataset HDF
93    */
94   switch(axe) {
95 
96   case 1 :
97     strcpy(nom_dataset,MED_NOM_IN1);
98     break;
99 
100   case 2 :
101     strcpy(nom_dataset,MED_NOM_IN2);
102     break;
103 
104   case 3 :
105     strcpy(nom_dataset,MED_NOM_IN3);
106     break;
107 
108   default :
109     return -1;
110 
111   }
112 
113   dimd[0] = n;
114   if ((ret = _MEDdatasetNumEcrire(noeid,nom_dataset,MED_FLOAT64,MED_FULL_INTERLACE,1,MED_ALL,MED_NOPF,MED_NO_PFLMOD,0,0,
115 				  MED_NOPG,dimd,(unsigned char*) indices)) < 0)
116     return -1;
117 
118   /*
119    * On re-ouvre le dataset HDF pour y placer des attributs
120    */
121   if ((dataset = _MEDdatasetOuvrir(noeid,nom_dataset)) < 0)
122     return -1;
123 
124   /*
125    * Attribut NBR (taille du tableau d'indices)
126    */
127   if ((ret = _MEDattrEntierEcrire(dataset,MED_NOM_NBR,&n)) < 0)
128     return -1;
129 
130   /*
131    * Attribut "NOM" (nom de la composante)
132    */
133   if ((ret = _MEDattrStringEcrire(dataset,MED_NOM_NOM,MED_TAILLE_PNOM,comp)) < 0)
134     return -1;
135 
136   /*
137    * Attribut "UNI" (unite de la composante)
138    */
139   if ((ret = _MEDattrStringEcrire(dataset,MED_NOM_UNI,MED_TAILLE_PNOM,unit)) < 0)
140     return -1;
141 
142   /*
143    * On ferme tout
144    */
145   if ((ret = _MEDdatasetFermer(dataset)) < 0)
146     return -1;
147   if ((ret = _MEDdatagroupFermer(noeid)) < 0)
148     return -1;
149   if ((ret = _MEDdatagroupFermer(maaid)) < 0)
150     return -1;
151 
152   return 0;
153 }
154