# UsesCase_MEDfield_9.c

Aller à la documentation de ce fichier.

    00001 /*  This file is part of MED.
    00002  *
    00003  *  COPYRIGHT (C) 1999 - 2011  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 9 : read a field with computing steps and profiles (generic approach)
    00032  */
    00033
    00034
    00035 int main (int argc, char **argv) {
    00036   med_idt fid;
    00037   med_idt nfield, i, j;
    00038   char meshname[MED_NAME_SIZE+1]="";
    00039   med_bool localmesh;
    00040   char fieldname[MED_NAME_SIZE+1]="";
    00041   med_field_type fieldtype;
    00042   char *componentname = NULL;
    00043   char *componentunit = NULL;
    00044   char dtunit[MED_SNAME_SIZE+1]="";
    00045   med_float *values = NULL;
    00046   med_int nstep, nvalues;
    00047   med_int ncomponent;
    00048   med_int csit, numit, numdt, it;
    00049   med_float dt;
    00050   med_geometry_type geotype;
    00051   med_geometry_type *geotypes = MED_GET_CELL_GEOMETRY_TYPE;
    00052   med_int nprofile, pit, profilesize;
    00053   char profilename[MED_NAME_SIZE+1]="";
    00054   med_int nintegrationpoint;
    00055   char localizationname[MED_NAME_SIZE+1]="";
    00056   int ret=-1;
    00057
    00058   /* open file */
    00059   fid = MEDfileOpen("UsesCase_MEDfield_7.med",MED_ACC_RDONLY);
    00060   if (fid < 0) {
    00061     MESSAGE("ERROR : open file ...");
    00062     return -1;
    00063   }
    00064
    00065   /*
    00066    * generic approach : how many fields in the file and identification
    00067    * of each field.
    00068    */
    00069   if ((nfield = MEDnField(fid)) < 0) {
    00070     MESSAGE("ERROR : How many fields in the file ...");
    00071     return -1;
    00072   }
    00073
    00074   /*
    00075    * read values for each field
    00076    */
    00077   for (i=0; i<nfield; i++) {
    00078
    00079     if ((ncomponent = MEDfieldnComponent(fid,i+1)) < 0) {
    00080       MESSAGE("ERROR : number of field component ...");
    00081       return -1;
    00082     }
    00083
    00084     if ((componentname = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
    00085       MESSAGE("ERROR : memory allocation ...");
    00086       return -1;
    00087     }
    00088
    00089     if ((componentunit = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
    00090       MESSAGE("ERROR : memory allocation ...");
    00091       return -1;
    00092     }
    00093
    00094     if (MEDfieldInfo(fid, i+1, fieldname, meshname, &localmesh, &fieldtype,
    00095                      componentname, componentunit, dtunit, &nstep) < 0) {
    00096       MESSAGE("ERROR : Field info ...");
    00097       free(componentname);
    00098       free(componentunit);
    00099       return -1;
    00100     }
    00101
    00102     free(componentname);
    00103     free(componentunit);
    00104
    00105     /*
    00106      * Read field values for each computing step
    00107      */
    00108     for (csit=0; csit<nstep; csit++) {
    00109
    00110       if (MEDfieldComputingStepInfo(fid, fieldname, csit+1, &numdt, &numit, &dt) < 0) {
    00111         MESSAGE("ERROR : Computing step info ...");
    00112         return -1;
    00113       }
    00114
    00115       /*
    00116        * ... In our case, we suppose that the field values are only defined on cells ...
    00117        */
    00118       for (it=1; it<=MED_N_CELL_FIXED_GEO; it++) {
    00119
    00120         geotype = geotypes[it];
    00121         /*
    00122          * How many profile for each geometry type ?
    00123          */
    00124         if ((nprofile = MEDfieldnProfile(fid, fieldname, numdt, numit, MED_CELL, geotype,
    00125                                          profilename, localizationname)) < 0) {
    00126           MESSAGE("ERROR : read number of profile ");
    00127           return -1;
    00128         }
    00129
    00130         /*
    00131          * Read values for each profile
    00132          * We suppose that we have no localization name
    00133          */
    00134         for (pit=0; pit<nprofile; pit++) {
    00135
    00136           if ((nvalues = MEDfieldnValueWithProfile(fid, fieldname, numdt, numit, MED_CELL, geotype,
    00137                                                    pit+1, MED_COMPACT_PFLMODE, profilename, &profilesize,
    00138                                                    localizationname, &nintegrationpoint)) < 0) {
    00139             MESSAGE("ERROR : read number of values with a profile ...");
    00140             return -1;
    00141           }
    00142
    00143           if (nvalues) {
    00144             if ((values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent)) == NULL) {
    00145               MESSAGE("ERROR : memory allocation ...");
    00146               return -1;
    00147             }
    00148             if (MEDfieldValueWithProfileRd(fid, fieldname, numdt, numit, MED_CELL, geotype,
    00149                                            MED_COMPACT_PFLMODE, profilename,
    00150                                            MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, (unsigned char*)values) < 0) {
    00151               MESSAGE("ERROR : read fields values for cells ...");
    00152               free(values);
    00153               return -1;
    00154             }
    00155
    00156             free(values);
    00157           }
    00158         }
    00159       }
    00160     }
    00161   }
    00162
    00163   ret=0;
    00164  ERROR:
    00165
    00166   /* close file */
    00167   if (MEDfileClose(fid) < 0) {
    00168     MESSAGE("ERROR : close file ...");
    00169     ret=-1;
    00170   }
    00171
    00172   return ret;
    00173 }