UsesCase_MEDfield_8.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  * Field use case 8 : read a field with following features computing steps and profiles
00020  */
00021 
00022 
00023 #include <med.h>
00024 #define MESGERR 1
00025 #include <med_utils.h>
00026 
00027 #include <string.h>
00028 
00029 int main (int argc, char **argv) {
00030   med_idt fid;
00031   char meshname[MED_NAME_SIZE+1]="";
00032   med_bool localmesh;
00033   const char fieldname[MED_NAME_SIZE+1] = "TEMPERATURE_FIELD";
00034   med_field_type fieldtype;
00035   char componentname[MED_SNAME_SIZE+1]="";
00036   char componentunit[MED_SNAME_SIZE+1]="";
00037   char dtunit[MED_SNAME_SIZE+1]="";
00038   med_float *values = NULL;
00039   med_int nstep, nvalues;
00040   med_int ncomponent = 1;
00041   med_int csit, numit, numdt, it;
00042   med_float dt;
00043   med_geometry_type geotype;
00044   med_geometry_type *geotypes = MED_GET_CELL_GEOMETRY_TYPE;
00045   med_int pit, profilesize;
00046   med_int np;
00047   char profilename[MED_NAME_SIZE+1] = "";
00048   med_int nintegrationpoint;
00049   char localizationname[MED_NAME_SIZE+1] = "";
00050   int ret = -1;
00051   
00052 
00053   /* open file */
00054   fid = MEDfileOpen("UsesCase_MEDfield_7.med",MED_ACC_RDONLY);
00055   if (fid < 0) {
00056     MESSAGE("ERROR : open file ...");
00057     goto ERROR;
00058   }
00059 
00060   /* 
00061    * ... we know that the MED file has only one field with one component , 
00062    * a real code working would check ... 
00063    */
00064 
00065   /*
00066    * if you know the field name, direct access to field informations
00067    */
00068   if (MEDfieldInfoByName(fid, fieldname, meshname, &localmesh, &fieldtype,
00069                          componentname, componentunit, dtunit, &nstep) < 0) {
00070     MESSAGE("ERROR : Field info by name ...");
00071     goto ERROR;
00072   }
00073 
00074   /*
00075    * Read field values for each computing step 
00076    */ 
00077   for (csit=0; csit<nstep; csit++) {
00078 
00079     if (MEDfieldComputingStepInfo(fid, fieldname, csit+1, &numdt, &numit, &dt) < 0) {
00080       MESSAGE("ERROR : Computing step info ...");
00081       goto ERROR;
00082     }
00083 
00084   /* 
00085    * ... In our case, we suppose that the field values are only defined on cells ... 
00086    */
00087     for (it=1; it<=MED_N_CELL_FIXED_GEO; it++) {
00088 
00089       geotype = geotypes[it];
00090       /*
00091        * How many profile for each geometry type ? 
00092        */
00093       if ((np = MEDfieldnProfile(fid, fieldname, numdt, numit, MED_CELL, geotype,
00094                                  profilename, localizationname)) < 0) {
00095         MESSAGE("ERROR : read number of profile ");
00096         goto ERROR;
00097       }
00098 
00099       /* 
00100        * Read values for each profile
00101        */
00102       for (pit=0; pit<np; pit++) {
00103 
00104         if ((nvalues = MEDfieldnValueWithProfile(fid, fieldname, numdt, numit, MED_CELL, geotype,
00105                                                  pit+1, MED_COMPACT_PFLMODE, profilename, &profilesize,
00106                                                  localizationname, &nintegrationpoint)) < 0) {
00107           MESSAGE("ERROR : read number of values with a profile ...");
00108           goto ERROR;
00109         } 
00110 
00111         if (nvalues) {
00112           if ((values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent)) == NULL) {
00113             MESSAGE("ERROR : memory allocation ...");
00114             goto ERROR;
00115           }
00116           if (MEDfieldValueWithProfileRd(fid, fieldname, numdt, numit, MED_CELL, geotype,
00117                                          MED_COMPACT_PFLMODE, profilename, 
00118                                          MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, 
00119                                          (unsigned char*) values) < 0) {
00120             MESSAGE("ERROR : read fields values for cells ..."); 
00121             free(values);
00122             goto ERROR; 
00123           }  
00124 
00125           free(values);
00126         }
00127       }
00128     }
00129   }
00130 
00131   ret=0;
00132  ERROR:
00133    
00134   /* close file */
00135   if (MEDfileClose(fid) < 0) {
00136     MESSAGE("ERROR : close file ...");             
00137     ret=-1; 
00138   } 
00139 
00140   return ret;
00141 }

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