# UsesCase_MEDfield_12.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 #include <med.h>
    00019 #define MESGERR 1
    00020 #include <med_utils.h>
    00021
    00022 #include <string.h>
    00023
    00024 #include <med.h>
    00025 #define MESGERR 1
    00026 #include <med_utils.h>
    00027
    00028 #include <string.h>
    00029
    00030 /*
    00031  * Field use case 12 : read a field (generic approach) in a MED file with computing steps,
    00032  *                     profiles, integration points and interpolation families
    00033  */
    00034
    00035
    00036 int main (int argc, char **argv) {
    00037   med_idt fid;
    00038   med_idt nfield, i, j;
    00039   char meshname[MED_NAME_SIZE+1]="";
    00040   med_bool localmesh;
    00041   char fieldname[MED_NAME_SIZE+1]="";
    00042   med_field_type fieldtype;
    00043   char *componentname = NULL;
    00044   char *componentunit = NULL;
    00045   char dtunit[MED_SNAME_SIZE+1]="";
    00046   med_float *values = NULL;
    00047   med_int nstep, nvalues;
    00048   med_int ncomponent;
    00049   med_int csit, numit, numdt, it;
    00050   med_float dt;
    00051   med_geometry_type geotype;
    00052   med_geometry_type *geotypes = MED_GET_CELL_GEOMETRY_TYPE;
    00053   med_int nprofile, pit, profilesize;
    00054   char profilename[MED_NAME_SIZE+1]="";
    00055   med_int nintegrationpoint;
    00056   char localizationname[MED_NAME_SIZE+1]="";
    00057   int k;
    00058   med_int ninterp;
    00059   char interpname[MED_NAME_SIZE+1];
    00060   int ret=-1;
    00061
    00062
    00063   /* open file */
    00064   fid = MEDfileOpen("UsesCase_MEDfield_10.med",MED_ACC_RDONLY);
    00065   if (fid < 0) {
    00066     MESSAGE("ERROR : open file ...");
    00067     return -1;
    00068   }
    00069
    00070   /*
    00071    * generic approach : how many fields in the file and identification
    00072    * of each field.
    00073    */
    00074   if ((nfield = MEDnField(fid)) < 0) {
    00075     MESSAGE("ERROR : How many fields in the file ...");
    00076     return -1;
    00077   }
    00078
    00079   /*
    00080    * read values for each field
    00081    */
    00082   for (i=0; i<nfield; i++) {
    00083
    00084     if ((ncomponent = MEDfieldnComponent(fid,i+1)) < 0) {
    00085       MESSAGE("ERROR : number of field component ...");
    00086       return -1;
    00087     }
    00088
    00089     if ((componentname = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
    00090       MESSAGE("ERROR : memory allocation ...");
    00091       return -1;
    00092     }
    00093
    00094     if ((componentunit = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
    00095       MESSAGE("ERROR : memory allocation ...");
    00096       return -1;
    00097     }
    00098
    00099     if (MEDfieldInfo(fid, i+1, fieldname, meshname, &localmesh, &fieldtype,
    00100                      componentname, componentunit, dtunit, &nstep) < 0) {
    00101       MESSAGE("ERROR : Field info ...");
    00102       free(componentname);
    00103       free(componentunit);
    00104       return -1;
    00105     }
    00106
    00107     free(componentname);
    00108     free(componentunit);
    00109
    00110     /*
    00111      * Read how many interpolation family name in the field ?
    00112      */
    00113     if ((ninterp = MEDfieldnInterp(fid, fieldname)) < 0) {
    00114       MESSAGE("ERROR : Read how many interpolation functions for the field ...");
    00115       return -1;
    00116     }
    00117     /* - Read each interlolation family name
    00118      * - The way to read an interploation family and it's basis functions
    00119      *  is described in UsesCase_MEDinterp_2 and UsesCase_MEDinterp_3 uses case
    00120      */
    00121     for (it=0; it<ninterp; it++) {
    00122       if (MEDfieldInterpInfo(fid,fieldname,it+1,interpname) < 0) {
    00123         MESSAGE("ERROR : read interpolation family name ...");
    00124         return -1;
    00125       }
    00126     }
    00127
    00128     /*
    00129      * Read field values for each computing step
    00130      */
    00131     for (csit=0; csit<nstep; csit++) {
    00132
    00133       if (MEDfieldComputingStepInfo(fid, fieldname, csit+1, &numdt, &numit, &dt) < 0) {
    00134         MESSAGE("ERROR : Computing step info ...");
    00135         return -1;
    00136       }
    00137
    00138       /*
    00139        * ... In our case, we suppose that the field values are only defined on cells ...
    00140        */
    00141       for (it=1; it<=MED_N_CELL_FIXED_GEO; it++) {
    00142
    00143         geotype = geotypes[it];
    00144
    00145         /*
    00146          * How many profile for each geometry type ?
    00147          */
    00148         if ((nprofile = MEDfieldnProfile(fid, fieldname, numdt, numit, MED_CELL, geotype,
    00149                                          profilename, localizationname)) < 0) {
    00150           MESSAGE("ERROR : read number of profile ");
    00151           return -1;
    00152         }
    00153
    00154         /*
    00155          * Read values for each profile
    00156          */
    00157         for (pit=0; pit<nprofile; pit++) {
    00158
    00159           if ((nvalues = MEDfieldnValueWithProfile(fid, fieldname, numdt, numit, MED_CELL, geotype,
    00160                                                    pit+1, MED_COMPACT_PFLMODE, profilename, &profilesize,
    00161                                                    localizationname, &nintegrationpoint)) < 0) {
    00162             MESSAGE("ERROR : read number of values with a profile ...");
    00163             return -1;
    00164           }
    00165
    00166           if (nvalues) {
    00167             if ((values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent*nintegrationpoint)) == NULL) {
    00168               MESSAGE("ERROR : memory allocation ...");
    00169               return -1;
    00170             }
    00171             if (MEDfieldValueWithProfileRd(fid, fieldname, numdt, numit, MED_CELL, geotype,
    00172                                            MED_COMPACT_PFLMODE, profilename,
    00173                                            MED_FULL_INTERLACE, MED_ALL_CONSTITUENT,
    00174                                            (unsigned char*) values) < 0) {
    00175               MESSAGE("ERROR : read fields values for cells ...");
    00176               free(values);
    00177               return -1;
    00178             }
    00179             free(values);
    00180           }
    00181         }
    00182       }
    00183     }
    00184   }
    00185
    00186   ret=0;
    00187  ERROR:
    00188
    00189   /* close file */
    00190   if (MEDfileClose(fid) < 0) {
    00191     MESSAGE("ERROR : close file ...");
    00192     ret=-1;
    00193   }
    00194
    00195   return ret;
    00196 }