MEDchampEcr232.c

Aller à la documentation de ce fichier.
00001 /*  This file is part of MED.
00002  *
00003  *  COPYRIGHT (C) 1999 - 2012  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  MEDchampEcr232(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   char oldpflname   [MED_TAILLE_NOM+1]=MED_NOPFLi;
00097   med_int maj, pfluseold, modifpfl;
00098   med_mode_acces MED_MODE_ACCES;
00099 
00100   va_list params;
00101   va_start(params,dummy);
00102 
00103   fid = va_arg(params,med_idt);
00104   maa = va_arg(params,char *);
00105   cha = va_arg(params,char *);
00106   val = va_arg(params,  unsigned char *);
00107   interlace = va_arg(params,med_mode_switch);
00108   nbelem = va_arg(params,med_int);
00109   locname = va_arg(params,char *);
00110   numco = va_arg(params,med_int);
00111   profil = va_arg(params,char *);
00112   pflmod = va_arg(params,med_mode_profil);
00113   type_ent = va_arg(params,med_entite_maillage);
00114   type_geo = va_arg(params,med_geometrie_element);
00115   numdt = va_arg(params,med_int);
00116   dt_unit = va_arg(params,char *);
00117   dt = va_arg(params,med_float);
00118   numo = va_arg(params,med_int);
00119   fret = va_arg(params,med_err *);
00120   
00121 
00122   /*
00123    * On inhibe le gestionnaire d'erreur HDF 5
00124    */
00125   _MEDmodeErreurVerrouiller();
00126 if (MEDcheckVersion(fid) < 0) {*fret=-1;return;}
00127 
00128 
00129   if ( (MED_MODE_ACCES = _MEDmodeAcces(fid) ) == MED_UNDEF_MODE_ACCES ) {
00130     MESSAGE("Impossible de déterminer le mode d'acces au fichier ");
00131     goto ERROR;
00132   }
00133 
00134   if ( MED_MODE_ACCES == MED_LECTURE) {
00135     MESSAGE("Impossible d'écrire un champ en mode MED_LECTURE : ");
00136     goto ERROR;
00137   };
00138 
00139 
00140   /* 
00141    * Si le Data Group CHA n'existe pas => erreur
00142    */
00143   strcpy(chemin,MED_CHA);
00144   strcat(chemin,cha);
00145   if ((gid = _MEDdatagroupOuvrir(fid,chemin)) < 0)
00146     goto ERROR;
00147 
00148   /* Lecture de l'attribut MED_NOM_NCO */
00149   if (_MEDattrEntierLire(gid,MED_NOM_NCO,&ncomp) < 0)
00150     goto ERROR;
00151 
00152   /* Lecture de l'attribut MED_NOM_TYP */
00153   if ( _MEDattrEntierLire(gid,MED_NOM_TYP,&chtype) < 0)
00154     goto ERROR;
00155  
00156 
00157   
00158   /* 
00159    *  Creation/Ouverture du datagroup de niveau 1 <type_ent>[.<type_geo>] 
00160    */
00161   if ( type_ent == MED_NOEUD_MAILLE ) {
00162     SSCRUTE("L'écriture aux noeuds des éléments n'est pas disponible.");
00163     goto ERROR;
00164   }
00165   
00166   if (_MEDnomEntite(nomdatagroup1,type_ent) < 0)
00167     goto ERROR;
00168   if ((type_ent != MED_NOEUD)) {
00169     if ( _MEDnomGeometrie(tmp1,type_geo) < 0)
00170       goto ERROR;
00171     strcat(nomdatagroup1,".");
00172     strcat(nomdatagroup1,tmp1);
00173   }
00174   datagroup1 = 0;
00175   if ( (datagroup1 = _MEDdatagroupOuvrir(gid,nomdatagroup1)) < 0) 
00176     if ((datagroup1 = _MEDdatagroupCreer(gid,nomdatagroup1)) < 0)
00177       goto ERROR;
00178   
00179 
00180   /* 
00181    * Creation/Ouverture du datagroup de niveau 2 <numdt>.<numoo> 
00182    */
00183  
00184   sprintf(nomdatagroup2,"%*li%*li",MED_MAX_PARA,(long ) numdt,MED_MAX_PARA,(long ) numo);
00185 
00186   datagroup2 = 0;   
00187   if ( (datagroup2 = _MEDdatagroupOuvrir(datagroup1,nomdatagroup2)) < 0 )    
00188     if ((datagroup2 = _MEDdatagroupCreer(datagroup1,nomdatagroup2)) < 0 )   
00189       goto ERROR;   
00190   
00191   /*Cree ou ouvre l'attribut MED_NOM_NDT pour écriture */
00192   if ( _MEDattrEntierEcrire(datagroup2,MED_NOM_NDT,&numdt) < 0)
00193     goto ERROR;
00194   
00195   /*Cree ou ouvre l'attribut MED_NOM_PDT pour écriture */
00196   if ( _MEDattrFloatEcrire(datagroup2,MED_NOM_PDT,&dt) < 0)
00197     goto ERROR;
00198     
00199   /*Cree ou ouvre l'attribut MED_NOM_NOR pour écriture */
00200   if (_MEDattrEntierEcrire(datagroup2,MED_NOM_NOR,&numo) < 0)
00201     goto ERROR;
00202   
00203   /*Cree ou ouvre l'attribut  MED_NOM_UNI pour écriture */ 
00204   if ( numdt == MED_NOPDT ) {
00205     if ( _MEDattrStringEcrire(datagroup2,MED_NOM_UNI,MED_TAILLE_PNOM,MED_PNOM_BLANC) < 0)
00206       goto ERROR;
00207   } else
00208     if ( _MEDattrStringEcrire(datagroup2,MED_NOM_UNI,MED_TAILLE_PNOM,dt_unit) < 0)
00209       goto ERROR;  
00210 
00211 
00212   /*
00213    * Si c'est la première référence à un maillage, initialise l'attribut MED_MAA à ce maillage
00214    */
00215   if ((attr = _MEDattrOuvrir(datagroup2,MED_NOM_MAI)) < 0 ) {
00216     if (_MEDattrStringEcrire(datagroup2,MED_NOM_MAI,MED_TAILLE_NOM,maa) < 0)
00217       goto ERROR;
00218   } else {   
00219     if ( _MEDattrFermer(attr) < 0) goto ERROR;
00220   }
00221       
00222   
00223 
00224   /* 
00225    * Cree ou ouvre le datagroup de niveau 3 <maa> 
00226    */
00227 
00228   NOFINALBLANK(maa,ERROR)
00229 
00230   datagroup3 = 0;   
00231   if (((datagroup3 = _MEDdatagroupOuvrir(datagroup2,maa)) > 0)    
00232       && ( MED_MODE_ACCES == MED_LECTURE_AJOUT )) {
00233     MESSAGE("Impossible de créer une reference à un maillage existant en mode MED_LECTURE_AJOUT :");
00234     goto ERROR;   
00235   } else   
00236     if (datagroup3 < 0)
00237       if ((datagroup3 = _MEDdatagroupCreer(datagroup2,maa)) < 0)   
00238         goto ERROR;   
00239   
00240   /*Cree ou ouvre l'attribut MED_NOM_NBR */
00241   if ( _MEDattrEntierEcrire(datagroup3,MED_NOM_NBR,&nbelem) < 0)
00242     goto ERROR;
00243   
00244   /* Lit le nom du profil associé s'il en existe déjà un */
00245   if ( _MEDattrStringLire(datagroup3,MED_NOM_PFL,MED_TAILLE_NOM,oldpflname) < 0 ) {
00246     strncpy(oldpflname,MED_NOPFLi,MED_TAILLE_NOM);
00247   }
00248   
00249   /*Cree ou ouvre  l'attribut MED_NOM_PFL   */
00250   pfluse = 0;
00251   if ( strlen(profil) == 0)   /* idem MED_NOPFL*/
00252     /* Ecriture de MED_NOPFLi car MED_NOM_PFL est forcément de taille MED_TAILLE_NOM*/
00253     strncpy(pflname,MED_NOPFLi,MED_TAILLE_NOM+1);
00254 /*     strncpy(pflname,MED_NOPFLi,MED_TAILLE_NOM); */
00255   else {
00256     strncpy(pflname,profil,MED_TAILLE_NOM);
00257     pflname[MED_TAILLE_NOM]='\0';
00258     pfluse = 1;
00259   }
00260 
00261   /* on reporte à la fin l'ecriture du nouveau profil pour ne pas avoir une taille de dataset */
00262   /* incompatible avec le profil en cas d'ereur d'ecriture du champ */
00263 
00264 
00265   /* Lit le nombre de points de gauss et vérifie   */
00266   /* que la localisation porte sur le meme type géométrique  */
00267   
00268   if ( strlen(locname) == 0) {
00269     /* Ecriture de MED_NOGAUSSi car MED_NOM_GAUSS est forcément de taille MED_TAILLE_NOM*/
00270     strcpy(locname_i,MED_NOGAUSSi);
00271     ngauss = MED_NOPG ;
00272   } else if (! strcmp(locname,MED_GAUSS_ELNO)) { 
00273     /* Les points de Gauss sont définis sur les noeuds de l'element (mot cle) */
00274     /* le nombre de points de Gauss est egal au nombre de noeuds de l'element */
00275     ngauss = type_geo % 100;
00276     strcpy(locname_i,locname);
00277 
00278   } else { 
00279     strcpy(locname_i,locname);
00280 
00281     strcpy(chemin_loc,MED_GAUSS);
00282     strcat(chemin_loc,locname_i);
00283     
00284     if ((gid_loc = _MEDdatagroupOuvrir(fid,chemin_loc)) < 0) {
00285       MESSAGE("Ouverture du datagroup : ");
00286       SSCRUTE(chemin_loc); goto ERROR;
00287     }
00288     
00289     if (_MEDattrEntierLire(gid_loc,MED_NOM_NBR,&ngauss) < 0) {
00290       MESSAGE("Erreur à la lecture de l'attribut MED_NOM_NBR : ");
00291       ISCRUTE(ngauss);goto ERROR;
00292     };
00293     
00294     
00295     if (_MEDattrEntierLire(gid_loc,MED_NOM_GEO,&type_geo_g) < 0) {
00296       MESSAGE("Erreur à la lecture de l'attribut MED_NOM_GEO : ");
00297       ISCRUTE(type_geo);goto ERROR;
00298     };
00299     
00300     if ( type_geo_g != type_geo ) {
00301       MESSAGE("Erreur, la localisation ne porte pas sur le meme type géométrique : ");
00302       SSCRUTE(locname);ISCRUTE(type_geo);ISCRUTE(type_geo_g);goto ERROR;
00303     };
00304     
00305   }
00306 
00307   /* Cree ou ouvre l'attribut MED_NOM_GAU         */ 
00308   /* Ecriture de la localisation des pts de gauss  */
00309   if ( _MEDattrStringEcrire(datagroup3,MED_NOM_GAU,MED_TAILLE_NOM,locname_i) < 0) {
00310     MESSAGE("Erreur d'écriture de l'attribut MED_NOM_GAU : ");
00311     SSCRUTE(locname); goto ERROR;
00312   }
00313 
00314   /* Cree ou ouvre l'attribut MED_NOM_NGA         */ 
00315   /* Ecriture de l'attribut portant le nombre de points de gauss  */
00316   if ( _MEDattrEntierEcrire(datagroup3,MED_NOM_NGA,&ngauss) < 0) {
00317     MESSAGE("Erreur d'écriture de l'attribut MED_NOM_NGA : ");
00318     ISCRUTE(ngauss); goto ERROR;
00319   }
00320 
00321   /*Determination de la taille dimd[0] du dataset à stocker*/
00322   dimd[0] = nbelem*ncomp;
00323 
00324   /* Gestion des profils*/
00325   if ( pfluse ) {
00326 
00327     if ( ( i = MEDnValProfil(fid,pflname) ) < 0 ) {
00328       MESSAGE("Erreur à la lecture du nombre de valeurs sur le profil : ");
00329       SSCRUTE(pflname);
00330       goto ERROR;
00331     } else
00332       psize = i;
00333     
00334     pfltabtmp = (med_int *)   malloc (sizeof(med_int)*psize);
00335     pfltab    = (med_size *)  malloc (sizeof(med_size)*psize);
00336     if (MEDprofilLire(fid,pfltabtmp,pflname) < 0) {
00337       MESSAGE("Erreur à la lecture du profil : ");
00338       SSCRUTE(pflname);goto ERROR;
00339     };
00340     for (i=0;i<psize;i++)
00341       pfltab[i] = (med_size) pfltabtmp[i];
00342     
00343   }
00344   else {
00345     psize = MED_NOPF;
00346   }
00347  
00348 
00349   /* Vérifie si le profil a changé (l'API MED ne permet pas de modifier un profil existant) */
00350   if (strcmp(pflname, oldpflname))
00351     modifpfl = 1;              /* le profil a changé */
00352   else
00353     modifpfl = 0;              /* le profil n'a pas changé */
00354 
00355 
00356   /*
00357    * Ecriture du champ
00358    */
00359  
00360   switch(chtype)
00361     {
00362     case MED_FLOAT64 :
00363       if ( _MEDdatasetNumEcrire(datagroup3,MED_NOM_CO,MED_FLOAT64,interlace,
00364                                 ncomp,numco,psize,pflmod,modifpfl,pfltab,ngauss,dimd,val) < 0) {
00365         MESSAGE("Impossible d'ecrire le dataset : ");
00366         SSCRUTE(MED_NOM_CO);ISCRUTE((int)(dimd[0])); goto ERROR;
00367       }
00368       break;
00369       
00370     case MED_INT32 :
00371 #if defined(HAVE_F77INT64) 
00372       if ( _MEDdatasetNumEcrire(datagroup3,MED_NOM_CO,MED_INT64,interlace,
00373                                 ncomp,numco,psize,pflmod,modifpfl,pfltab,ngauss,dimd,val) < 0) {
00374         MESSAGE("Impossible d'ecrire le dataset : ");
00375         SSCRUTE(MED_NOM_CO);ISCRUTE((int)(dimd[0])); goto ERROR;
00376       }
00377 #else
00378       if ( _MEDdatasetNumEcrire(datagroup3,MED_NOM_CO,MED_INT32,interlace,
00379                                 ncomp,numco,psize,pflmod,modifpfl,pfltab,ngauss,dimd,val) < 0){
00380         MESSAGE("Impossible d'ecrire le dataset : ");
00381         SSCRUTE(MED_NOM_CO);ISCRUTE((int)(dimd[0])); goto ERROR;
00382       }
00383 #endif
00384       break;
00385 
00386     case MED_INT64 :
00387 #if defined(HAVE_F77INT64) 
00388       if ( _MEDdatasetNumEcrire(datagroup3,MED_NOM_CO,MED_INT64,interlace,
00389                                 ncomp,numco,psize,pflmod,modifpfl,pfltab,ngauss,dimd,val) < 0){
00390         MESSAGE("Impossible d'ecrire le dataset : ");
00391         SSCRUTE(MED_NOM_CO);ISCRUTE(dimd); goto ERROR;
00392       }
00393 #else
00394       MESSAGE("Impossible d'ecrire le dataset de type MED_INT64 sur une plateforme autre que IRIX64 et OSF1 !");
00395       goto ERROR;
00396 #endif
00397       break;   
00398 
00399     default :
00400       goto ERROR;
00401     }
00402 
00403   if ( _MEDattrStringEcrire(datagroup3,MED_NOM_PFL,MED_TAILLE_NOM,pflname) < 0) {
00404     MESSAGE("Erreur d'écriture de l'attribut pflname : ");
00405     SSCRUTE(chemin); SSCRUTE(pflname);goto ERROR;
00406   }
00407 
00408   /*
00409    * On ferme tout
00410    */
00411 
00412   ret = 0;
00413 
00414  ERROR:
00415 
00416   if ( pfluse ) { free(pfltab); free(pfltabtmp);}
00417 
00418   if (datagroup3>0)     if (_MEDdatagroupFermer(datagroup3) < 0) {
00419     MESSAGE("Impossible de fermer le datagroup : ");
00420     ISCRUTE_int(datagroup3); ret = -1; 
00421   }
00422   
00423   if (datagroup2>0)     if (_MEDdatagroupFermer(datagroup2) < 0) {
00424     MESSAGE("Impossible de fermer le datagroup : ");
00425     ISCRUTE_int(datagroup2); ret = -1; 
00426   }
00427 
00428   if (datagroup1>0)     if (_MEDdatagroupFermer(datagroup1) < 0) {
00429     MESSAGE("Impossible de fermer le datagroup : ");
00430     ISCRUTE_int(datagroup1); ret = -1; 
00431   }
00432   
00433   if (gid>0)     if (_MEDdatagroupFermer(gid) < 0) {
00434     MESSAGE("Impossible de fermer le datagroup : ");
00435     ISCRUTE_id(gid); ret = -1; 
00436   }
00437   
00438   if (gid_loc>0)     if (_MEDdatagroupFermer(gid_loc) < 0) {
00439     MESSAGE("Impossible de fermer le datagroup : ");
00440     ISCRUTE_id(gid_loc); ret = -1; 
00441   }
00442 
00443   va_end(params);
00444   *fret = ret;
00445   return;
00446 }
00447 

Généré le Mon Nov 19 15:43:34 2012 pour MED fichier par  doxygen 1.6.1