MEDchampEcr233.c

Aller à la documentation de ce fichier.
00001 /*  This file is part of MED.
00002  *
00003  *  COPYRIGHT (C) 1999 - 2015  EDF R&D, CEA/DEN
00004  *  MED is free software: you can redistribute it and/or modify
00005  *  it under the terms of the GNU Lesser General Public License as published by
00006  *  the Free Software Foundation, either version 3 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  MED is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU Lesser General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Lesser General Public License
00015  *  along with MED.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 
00019 #include <med.h>
00020 #include <med_config.h>
00021 #include <med_outils.h> 
00022 #include <string.h>
00023 #include <stdlib.h>
00024 
00025 
00026 #ifdef TRACEPFL
00027 #define tracepfl(x) x
00028 #else
00029 #define tracepfl(x)
00030 #endif
00031 
00032 /*
00033  * - Nom de la fonction : MEDchampEcr
00034  * - Description : ecriture d'un Champ Résultat
00035  * - Parametres :
00036  *     - fid      (IN)  : ID du fichier HDF courant
00037  *     - maa      (IN)  : le nom du maillage sur lequel s'applique le champ (eventuellement distant)
00038  *     - cha      (IN)  : le nom du champ 
00039  *     - val      (IN)  : valeurs du champ à stocker
00040  *     - interlace(IN)  : entrelacement utilisé en mémoire {MED_FULL_INTERLACE,MED_NO_INTERLACE} 
00041  *     - nbelem   (IN)  : nombre d'éléments (prend en compte le nbre 
00042  *                         de points de Gauss (c'est demandé à l'utilisateur ds la doc) 
00043                            mais pas le nbre de composantes)
00044  *     - locname  (IN)  : clé utilisée pour la définition de la localisation 
00045                           des points de GAUSS (MED_NOGAUSS si aucun, MED_GAUSS_ELNO si les points de Gauss
00046  *                        portent sur les noeuds de l'element). La localisation doit exister 
00047  *                        avant l'appel à MEDchampEcr.
00048  *     - numco    (IN)  : n° de la composante à stocker (MED_ALL si toutes)
00049  *     - profil   (IN)  : nom du profil utilisé (MED_NOPFL si inutilisé)
00050  *     - pflmod   (IN)  : Indique comment lire les informations en mémoire { MED_COMPACT, MED_GLOBAL }. 
00051  *     - type_ent (IN)  : entité concerné par le champ {MED_NOEUD,MED_ARETE,MED_FACE,MED_MAILLE}
00052  *     - type_geo (IN)  : type géométrique de l'entité concerné {MED_POINT,MED_SEG2 ......}
00053  *     - numdt    (IN)  : n° du pas de temps (MED_NOPDT si aucun)
00054  *     - dt_unit  (IN)  : chaine de taille MED_NOMP indiquant l'unité du champ
00055  *     - dt       (IN)  : valeur du pas de temps 
00056  *     - numo     (IN)  : n° d'ordre utilisé MED_NONOR si inutile
00057  * - Resultat : 0 en cas de succes, -1 sinon
00058  */ 
00059 
00060 void  MEDchampEcr233(int dummy,...) {
00061 
00062 
00063   med_idt                    fid ;
00064   char *                     maa ;
00065   char *                     cha ;
00066   unsigned char *            val ;
00067   med_mode_switch      interlace ;
00068   med_int                 nbelem ;
00069   char *                 locname ;
00070   med_int                  numco ;
00071   char *                  profil ;
00072   med_mode_profil         pflmod ;
00073   med_entite_maillage   type_ent ;
00074   med_geometrie_element type_geo ;
00075   med_int                  numdt ;
00076   char *                 dt_unit ;
00077   med_float                   dt ;
00078   med_int                   numo ;
00079   med_err *                 fret ;
00080 
00081   med_err ret=-1;
00082   med_idt gid=0,datagroup1=0,datagroup2=0,datagroup3=0,attr=0,gid_loc=0;
00083   med_int ncomp=0, chtype=0, i=0, ngauss=0, pfluse=0;
00084   char nomdatagroup1[2*MED_TAILLE_NOM_ENTITE+2]="";
00085   char nomdatagroup2[2*MED_MAX_PARA+1]="";
00086   char pflname   [MED_TAILLE_NOM+1]="";
00087   char locname_i [MED_TAILLE_NOM+1]="";
00088   char maillage[MED_TAILLE_NOM+1]="";
00089   char tmp1[MED_TAILLE_NOM_ENTITE+1]="";
00090   med_size dimd[1],psize=0,lsize=0;
00091   med_int   *pfltabtmp=0;
00092   med_size *pfltab=0;
00093   char chemin[MED_TAILLE_CHA+MED_TAILLE_NOM+1]="";
00094   char chemin_loc[MED_TAILLE_GAUSS+MED_TAILLE_NOM+1]="";
00095   med_geometrie_element type_geo_g;
00096   med_int  type_geo_g_int=0;
00097   char oldpflname   [MED_TAILLE_NOM+1]=MED_NOPFLi;
00098   med_int maj, pfluseold, modifpfl;
00099   med_mode_acces MED_MODE_ACCES;
00100 
00101   va_list params;
00102   va_start(params,dummy);
00103 
00104   fid = va_arg(params,med_idt);
00105   maa = va_arg(params,char *);
00106   cha = va_arg(params,char *);
00107   val = va_arg(params,  unsigned char *);
00108   interlace = va_arg(params,med_mode_switch);
00109   nbelem = va_arg(params,med_int);
00110   locname = va_arg(params,char *);
00111   numco = va_arg(params,med_int);
00112   profil = va_arg(params,char *);
00113   pflmod = va_arg(params,med_mode_profil);
00114   type_ent = va_arg(params,med_entite_maillage);
00115   type_geo = va_arg(params,med_geometrie_element);
00116   numdt = va_arg(params,med_int);
00117   dt_unit = va_arg(params,char *);
00118   dt = va_arg(params,med_float);
00119   numo = va_arg(params,med_int);
00120   fret = va_arg(params,med_err *);
00121   
00122 
00123   /*
00124    * On inhibe le gestionnaire d'erreur HDF 5
00125    */
00126   _MEDmodeErreurVerrouiller();
00127 if (MEDcheckVersion(fid) < 0) {*fret=-1;return;}
00128 
00129 
00130   if ( (MED_MODE_ACCES = _MEDmodeAcces(fid) ) == MED_UNDEF_MODE_ACCES ) {
00131     MESSAGE("Impossible de déterminer le mode d'acces au fichier ");
00132     goto ERROR;
00133   }
00134 
00135   if ( MED_MODE_ACCES == MED_LECTURE) {
00136     MESSAGE("Impossible d'écrire un champ en mode MED_LECTURE : ");
00137     goto ERROR;
00138   };
00139 
00140 
00141   /* 
00142    * Si le Data Group CHA n'existe pas => erreur
00143    */
00144   strcpy(chemin,MED_CHA);
00145   strcat(chemin,cha);
00146   if ((gid = _MEDdatagroupOuvrir(fid,chemin)) < 0)
00147     goto ERROR;
00148 
00149   /* Lecture de l'attribut MED_NOM_NCO */
00150   if (_MEDattrEntierLire(gid,MED_NOM_NCO,&ncomp) < 0)
00151     goto ERROR;
00152 
00153   /* Lecture de l'attribut MED_NOM_TYP */
00154   if ( _MEDattrEntierLire(gid,MED_NOM_TYP,&chtype) < 0)
00155     goto ERROR;
00156  
00157 
00158   
00159   /* 
00160    *  Creation/Ouverture du datagroup de niveau 1 <type_ent>[.<type_geo>] 
00161    */
00162   
00163   if (_MEDnomEntite(nomdatagroup1,type_ent) < 0)
00164     goto ERROR;
00165   if ( type_ent != MED_NOEUD ) {
00166       if ( _MEDnomGeometrie(tmp1,type_geo) < 0)
00167         goto ERROR;
00168       strcat(nomdatagroup1,".");
00169       strcat(nomdatagroup1,tmp1);
00170   }
00171   datagroup1 = 0;
00172   if ( (datagroup1 = _MEDdatagroupOuvrir(gid,nomdatagroup1)) < 0) 
00173     if ((datagroup1 = _MEDdatagroupCreer(gid,nomdatagroup1)) < 0)
00174       goto ERROR;
00175   
00176 
00177   /* 
00178    * Creation/Ouverture du datagroup de niveau 2 <numdt>.<numoo> 
00179    */
00180  
00181   sprintf(nomdatagroup2,"%*li%*li",MED_MAX_PARA,(long ) numdt,MED_MAX_PARA,(long ) numo);
00182 
00183   datagroup2 = 0;   
00184   if ( (datagroup2 = _MEDdatagroupOuvrir(datagroup1,nomdatagroup2)) < 0 )    
00185     if ((datagroup2 = _MEDdatagroupCreer(datagroup1,nomdatagroup2)) < 0 )   
00186       goto ERROR;   
00187   
00188   /*Cree ou ouvre l'attribut MED_NOM_NDT pour écriture */
00189   if ( _MEDattrEntierEcrire(datagroup2,MED_NOM_NDT,&numdt) < 0)
00190     goto ERROR;
00191   
00192   /*Cree ou ouvre l'attribut MED_NOM_PDT pour écriture */
00193   if ( _MEDattrFloatEcrire(datagroup2,MED_NOM_PDT,&dt) < 0)
00194     goto ERROR;
00195     
00196   /*Cree ou ouvre l'attribut MED_NOM_NOR pour écriture */
00197   if (_MEDattrEntierEcrire(datagroup2,MED_NOM_NOR,&numo) < 0)
00198     goto ERROR;
00199   
00200   /*Cree ou ouvre l'attribut  MED_NOM_UNI pour écriture */ 
00201   if ( numdt == MED_NOPDT ) {
00202     if ( _MEDattrStringEcrire(datagroup2,MED_NOM_UNI,MED_TAILLE_PNOM,MED_PNOM_BLANC) < 0)
00203       goto ERROR;
00204   } else
00205     if ( _MEDattrStringEcrire(datagroup2,MED_NOM_UNI,MED_TAILLE_PNOM,dt_unit) < 0)
00206       goto ERROR;  
00207 
00208 
00209   /*
00210    * Si c'est la première référence à un maillage, initialise l'attribut MED_MAA à ce maillage
00211    */
00212   if ((attr = _MEDattrOuvrir(datagroup2,MED_NOM_MAI)) < 0 ) {
00213     if (_MEDattrStringEcrire(datagroup2,MED_NOM_MAI,MED_TAILLE_NOM,maa) < 0)
00214       goto ERROR;
00215   } else {   
00216     if ( _MEDattrFermer(attr) < 0) goto ERROR;
00217   }
00218       
00219   
00220 
00221   /* 
00222    * Cree ou ouvre le datagroup de niveau 3 <maa> 
00223    */
00224 
00225   NOFINALBLANK(maa,ERROR)
00226 
00227   datagroup3 = 0;   
00228   if (((datagroup3 = _MEDdatagroupOuvrir(datagroup2,maa)) > 0)    
00229       && ( MED_MODE_ACCES == MED_LECTURE_AJOUT )) {
00230     MESSAGE("Impossible de créer une reference à un maillage existant en mode MED_LECTURE_AJOUT :");
00231     goto ERROR;   
00232   } else   
00233     if (datagroup3 < 0)
00234       if ((datagroup3 = _MEDdatagroupCreer(datagroup2,maa)) < 0)   
00235         goto ERROR;   
00236   
00237   /*Cree ou ouvre l'attribut MED_NOM_NBR */
00238   if ( _MEDattrEntierEcrire(datagroup3,MED_NOM_NBR,&nbelem) < 0)
00239     goto ERROR;
00240   
00241   /* Lit le nom du profil associé s'il en existe déjà un */
00242   if ( _MEDattrStringLire(datagroup3,MED_NOM_PFL,MED_TAILLE_NOM,oldpflname) < 0 ) {
00243     strncpy(oldpflname,MED_NOPFLi,MED_TAILLE_NOM);
00244   }
00245   
00246   /*Cree ou ouvre  l'attribut MED_NOM_PFL   */
00247   pfluse = 0;
00248   if ( strlen(profil) == 0)   /* idem MED_NOPFL*/
00249     /* Ecriture de MED_NOPFLi car MED_NOM_PFL est forcément de taille MED_TAILLE_NOM*/
00250     /*strncpy(pflname,MED_NOPFLi,MED_TAILLE_NOM+1);    erreur ??? */
00251     strncpy(pflname,MED_NOPFLi,MED_TAILLE_NOM);
00252   else {
00253     strncpy(pflname,profil,MED_TAILLE_NOM);
00254     pflname[MED_TAILLE_NOM]='\0';
00255     pfluse = 1;
00256   }
00257 
00258   /* on reporte à la fin l'ecriture du nouveau profil pour ne pas avoir une taille de dataset */
00259   /* incompatible avec le profil en cas d'ereur d'ecriture du champ */
00260 
00261 
00262   /* Lit le nombre de points de gauss et vérifie   */
00263   /* que la localisation porte sur le meme type géométrique  */
00264   
00265   /* Dans le cas de champs aux noeuds par element, on  stocke
00266    * une localisation vide, et le nombre de noeuds dans l'attribut NGA
00267    */
00268 
00269   if (type_ent == MED_NOEUD_MAILLE ) {
00270     strcpy(locname_i,MED_NOGAUSSi);
00271     ngauss = type_geo % 100;
00272   } else if ( strlen(locname) == 0) {
00273     /* Ecriture de MED_NOGAUSSi car MED_NOM_GAUSS est forcément de taille MED_TAILLE_NOM*/
00274     strcpy(locname_i,MED_NOGAUSSi);
00275     ngauss = MED_NOPG ;
00276   } else if (! strcmp(locname,MED_GAUSS_ELNO)) {
00277     /* Les points de Gauss sont définis sur les noeuds de l'element (mot cle) */
00278     /* le nombre de points de Gauss est egal au nombre de noeuds de l'element */
00279     ngauss = type_geo % 100;
00280     strcpy(locname_i,locname);
00281   } else {
00282     strcpy(locname_i,locname);
00283 
00284     strcpy(chemin_loc,MED_GAUSS);
00285     strcat(chemin_loc,locname_i);
00286     
00287     if ((gid_loc = _MEDdatagroupOuvrir(fid,chemin_loc)) < 0) {
00288       MESSAGE("Ouverture du datagroup : ");
00289       SSCRUTE(chemin_loc); goto ERROR;
00290     }
00291     
00292     if (_MEDattrEntierLire(gid_loc,MED_NOM_NBR,&ngauss) < 0) {
00293       MESSAGE("Erreur à la lecture de l'attribut MED_NOM_NBR : ");
00294       ISCRUTE(ngauss);goto ERROR;
00295     };  
00296     
00297     if (_MEDattrEntierLire(gid_loc,MED_NOM_GEO,&type_geo_g_int) < 0) {
00298       MESSAGE("Erreur à la lecture de l'attribut MED_NOM_GEO : ");
00299       ISCRUTE(type_geo_g_int);goto ERROR;
00300     };
00301     type_geo_g = type_geo_g_int;
00302 
00303     if ( type_geo_g != type_geo ) {
00304       MESSAGE("Erreur, la localisation ne porte pas sur le meme type géométrique : ");
00305       SSCRUTE(locname);ISCRUTE(type_geo);ISCRUTE(type_geo_g);goto ERROR;
00306     };
00307     
00308   }
00309 
00310   /* Cree ou ouvre l'attribut MED_NOM_GAU         */ 
00311   /* Ecriture de la localisation des pts de gauss  */
00312   if ( _MEDattrStringEcrire(datagroup3,MED_NOM_GAU,MED_TAILLE_NOM,locname_i) < 0) {
00313     MESSAGE("Erreur d'écriture de l'attribut MED_NOM_GAU : ");
00314     SSCRUTE(locname); goto ERROR;
00315   }
00316 
00317   /* Cree ou ouvre l'attribut MED_NOM_NGA         */ 
00318   /* Ecriture de l'attribut portant le nombre de points de gauss  */
00319   if ( _MEDattrEntierEcrire(datagroup3,MED_NOM_NGA,&ngauss) < 0) {
00320     MESSAGE("Erreur d'écriture de l'attribut MED_NOM_NGA : ");
00321     ISCRUTE(ngauss); goto ERROR;
00322   }
00323 
00324   /*Determination de la taille dimd[0] du dataset à stocker*/
00325   dimd[0] = nbelem*ncomp;
00326 
00327   /* Gestion des profils*/
00328   if ( pfluse ) {
00329 
00330     if ( ( i = MEDnValProfil(fid,pflname) ) < 0 ) {
00331       MESSAGE("Erreur à la lecture du nombre de valeurs sur le profil : ");
00332       SSCRUTE(pflname);
00333       goto ERROR;
00334     } else
00335       psize = i;
00336     
00337     pfltabtmp = (med_int *)   malloc (sizeof(med_int)*psize);
00338     pfltab    = (med_size *)  malloc (sizeof(med_size)*psize);
00339     if (MEDprofilLire(fid,pfltabtmp,pflname) < 0) {
00340       MESSAGE("Erreur à la lecture du profil : ");
00341       SSCRUTE(pflname);goto ERROR;
00342     };
00343     for (i=0;i<psize;i++)
00344       pfltab[i] = (med_size) pfltabtmp[i];
00345     
00346   }
00347   else {
00348     psize = MED_NOPF;
00349   }
00350  
00351 
00352   /* Vérifie si le profil a changé (l'API MED ne permet pas de modifier un profil existant) */
00353   if (strcmp(pflname, oldpflname))
00354     modifpfl = 1;              /* le profil a changé */
00355   else
00356     modifpfl = 0;              /* le profil n'a pas changé */
00357 
00358 
00359   /*
00360    * Ecriture du champ
00361    */
00362  
00363   switch(chtype)
00364     {
00365     case MED_FLOAT64 :
00366       if ( _MEDdatasetNumEcrire(datagroup3,MED_NOM_CO,MED_FLOAT64,interlace,
00367                                 ncomp,numco,psize,pflmod,modifpfl,pfltab,ngauss,dimd,val) < 0) {
00368         MESSAGE("Impossible d'ecrire le dataset : ");
00369         SSCRUTE(MED_NOM_CO);ISCRUTE((int)(dimd[0])); goto ERROR;
00370       }
00371       break;
00372       
00373     case MED_INT32 :
00374 #if defined(HAVE_F77INT64) 
00375       if ( _MEDdatasetNumEcrire(datagroup3,MED_NOM_CO,MED_INT64,interlace,
00376                                 ncomp,numco,psize,pflmod,modifpfl,pfltab,ngauss,dimd,val) < 0) {
00377         MESSAGE("Impossible d'ecrire le dataset : ");
00378         SSCRUTE(MED_NOM_CO);ISCRUTE((int)(dimd[0])); goto ERROR;
00379       }
00380 #else
00381       if ( _MEDdatasetNumEcrire(datagroup3,MED_NOM_CO,MED_INT32,interlace,
00382                                 ncomp,numco,psize,pflmod,modifpfl,pfltab,ngauss,dimd,val) < 0){
00383         MESSAGE("Impossible d'ecrire le dataset : ");
00384         SSCRUTE(MED_NOM_CO);ISCRUTE((int)(dimd[0])); goto ERROR;
00385       }
00386 #endif
00387       break;
00388 
00389     case MED_INT64 :
00390 #if defined(HAVE_F77INT64) 
00391       if ( _MEDdatasetNumEcrire(datagroup3,MED_NOM_CO,MED_INT64,interlace,
00392                                 ncomp,numco,psize,pflmod,modifpfl,pfltab,ngauss,dimd,val) < 0){
00393         MESSAGE("Impossible d'ecrire le dataset : ");
00394         SSCRUTE(MED_NOM_CO);ISCRUTE(dimd); goto ERROR;
00395       }
00396 #else
00397       MESSAGE("Impossible d'ecrire le dataset de type MED_INT64 sur une plateforme autre que IRIX64 et OSF1 !");
00398       goto ERROR;
00399 #endif
00400       break;   
00401 
00402     default :
00403       goto ERROR;
00404     }
00405 
00406   if ( _MEDattrStringEcrire(datagroup3,MED_NOM_PFL,MED_TAILLE_NOM,pflname) < 0) {
00407     MESSAGE("Erreur d'écriture de l'attribut pflname : ");
00408     SSCRUTE(chemin); goto ERROR;
00409   }
00410 
00411   /*
00412    * On ferme tout
00413    */
00414 
00415   ret = 0;
00416 
00417  ERROR:
00418 
00419   if ( pfluse ) { free(pfltab); free(pfltabtmp);}
00420 
00421   if (datagroup3>0)     if (_MEDdatagroupFermer(datagroup3) < 0) {
00422     MESSAGE("Impossible de fermer le datagroup : ");
00423     ISCRUTE_int(datagroup3); ret = -1; 
00424   }
00425   
00426   if (datagroup2>0)     if (_MEDdatagroupFermer(datagroup2) < 0) {
00427     MESSAGE("Impossible de fermer le datagroup : ");
00428     ISCRUTE_int(datagroup2); ret = -1; 
00429   }
00430 
00431   if (datagroup1>0)     if (_MEDdatagroupFermer(datagroup1) < 0) {
00432     MESSAGE("Impossible de fermer le datagroup : ");
00433     ISCRUTE_int(datagroup1); ret = -1; 
00434   }
00435   
00436   if (gid>0)     if (_MEDdatagroupFermer(gid) < 0) {
00437     MESSAGE("Impossible de fermer le datagroup : ");
00438     ISCRUTE_id(gid); ret = -1; 
00439   }
00440   
00441   if (gid_loc>0)     if (_MEDdatagroupFermer(gid_loc) < 0) {
00442     MESSAGE("Impossible de fermer le datagroup : ");
00443     ISCRUTE_id(gid_loc); ret = -1; 
00444   }
00445 
00446   va_end(params);
00447   *fret = ret;
00448   return;
00449 }
00450 

Généré le Thu Oct 8 14:26:16 2015 pour MED fichier par  doxygen 1.6.1