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

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