# UsesCase_MEDfield_6.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  * Field use case 6 : read a field (generic approach) 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 #include <med.h>
    00029 #define MESGERR 1
    00030 #include <med_utils.h>
    00031
    00032 #include <string.h>
    00033
    00034 int main (int argc, char **argv) {
    00035   med_idt fid;
    00036   med_idt nfield, i, j;
    00037   char meshname[MED_NAME_SIZE+1]="";
    00038   med_bool localmesh;
    00039   char fieldname[MED_NAME_SIZE+1]="";
    00040   med_field_type fieldtype;
    00041   char *componentname = NULL;
    00042   char *componentunit = NULL;
    00043   char dtunit[MED_SNAME_SIZE+1]="";
    00044   med_float *values = NULL;
    00045   med_int nstep, nvalues;
    00046   med_int ncomponent;
    00047   med_int csit, numit, numdt, meshnumit, meshnumdt, it;
    00048   med_float dt;
    00049   med_geometry_type geotype;
    00050   med_geometry_type *geotypes = MED_GET_CELL_GEOMETRY_TYPE;
    00051   int k;
    00052   int ret=-1;
    00053
    00054   /* open file */
    00055   fid = MEDfileOpen("UsesCase_MEDfield_4.med",MED_ACC_RDONLY);
    00056   if (fid < 0) {
    00057     MESSAGE("ERROR : open file ...");
    00058     goto ERROR;
    00059   }
    00060
    00061   /*
    00062    * generic approach : how many fields in the file and identification
    00063    * of each field.
    00064    */
    00065   if ((nfield = MEDnField(fid)) < 0) {
    00066     MESSAGE("ERROR : How many fields in the file ...");
    00067     goto ERROR;
    00068   }
    00069
    00070   /*
    00071    * read values for each field
    00072    */
    00073   for (i=0; i<nfield; i++) {
    00074
    00075     if ((ncomponent = MEDfieldnComponent(fid,i+1)) < 0) {
    00076       MESSAGE("ERROR : number of field component ...");
    00077       goto ERROR;
    00078     }
    00079
    00080     if ((componentname = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
    00081       MESSAGE("ERROR : memory allocation ...");
    00082       goto ERROR;
    00083     }
    00084
    00085     if ((componentunit = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
    00086       MESSAGE("ERROR : memory allocation ...");
    00087       goto ERROR;
    00088     }
    00089
    00090     if (MEDfieldInfo(fid, i+1, fieldname, meshname, &localmesh, &fieldtype,
    00091                      componentname, componentunit, dtunit, &nstep) < 0) {
    00092       MESSAGE("ERROR : Field info ...");
    00093       free(componentname);
    00094       free(componentunit);
    00095       goto ERROR;
    00096     }
    00097     free(componentname);
    00098     free(componentunit);
    00099
    00100     /*
    00101      * Read field values for each computing step
    00102      */
    00103     for (csit=0; csit<nstep; csit++) {
    00104
    00105       if (MEDfieldComputingStepMeshInfo(fid, fieldname, csit+1, &numdt, &numit, &dt,
    00106                                         &meshnumdt, &meshnumit) < 0) {
    00107         MESSAGE("ERROR : Computing step info ...");
    00108         goto ERROR;
    00109       }
    00110
    00111       /*
    00112        * ... In our case, we suppose that the field values are only defined on cells ...
    00113        */
    00114       for (it=1; it<=MED_N_CELL_FIXED_GEO; it++) {
    00115
    00116         geotype = geotypes[it];
    00117         if ((nvalues = MEDfieldnValue(fid, fieldname, numdt, numit, MED_CELL, geotype)) < 0) {
    00118           MESSAGE("ERROR : read number of values ...");
    00119           goto ERROR;
    00120         }
    00121         if (nvalues) {
    00122           if ((values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent)) == NULL) {
    00123             MESSAGE("ERROR : memory allocation ...");
    00124           goto ERROR;
    00125           }
    00126           if (MEDfieldValueRd(fid, fieldname, numdt, numit, MED_CELL, geotype,
    00127                               MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, (unsigned char*) values) < 0) {
    00128             MESSAGE("ERROR : read fields values for cells ...");
    00129             free(values);
    00130             goto ERROR;
    00131           }
    00132           free(values);
    00133         }
    00134
    00135       }
    00136     }
    00137   }
    00138
    00139   ret=0;
    00140  ERROR:
    00141
    00142   /* close file */
    00143   if (MEDfileClose(fid) < 0) {
    00144     MESSAGE("ERROR : close file ...");
    00145     ret=-1;
    00146   }
    00147
    00148   return ret;
    00149 }