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

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