# UsesCase_MEDfield_11.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 11 : read a field in a MED file with computing steps,
    00020  *                     profiles, integration points, interpolation families
    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 nprofile, pit, profilesize;
    00046   char profilename[MED_NAME_SIZE+1]="";
    00047   med_int nintegrationpoint;
    00048   char localizationname[MED_NAME_SIZE+1]="";
    00049   med_int ninterp=0;
    00050   char interpname[MED_NAME_SIZE+1]="";
    00051   int ret=-1;
    00052
    00053   /* open file */
    00054   fid = MEDfileOpen("UsesCase_MEDfield_10.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 how many interpolation family name for the field ?
    00076    */
    00077   if ((ninterp = MEDfieldnInterp(fid, fieldname)) < 0) {
    00078     MESSAGE("ERROR : Read how many interpolation functions for the field ...");
    00079     goto ERROR;
    00080   }
    00081   /* - Read each interlolation family name
    00082    * - The way to read an interploation family and it's basis functions
    00083    *  is described in UsesCase_MEDinterp_2 and UsesCase_MEDinterp_3 uses case
    00084    */
    00085   for (it=0; it<ninterp; it++) {
    00086     if (MEDfieldInterpInfo(fid,fieldname,it+1,interpname) < 0) {
    00087       MESSAGE("ERROR : read interpolation family name ...");
    00088       goto ERROR;
    00089     }
    00090   }
    00091
    00092   /*
    00093    * Read field values for each computing step
    00094    */
    00095   for (csit=0; csit<nstep; csit++) {
    00096
    00097     if (MEDfieldComputingStepInfo(fid, fieldname, csit+1, &numdt, &numit, &dt) < 0) {
    00098       MESSAGE("ERROR : Computing step info ...");
    00099       goto ERROR;
    00100     }
    00101
    00102   /*
    00103    * ... In our case, we suppose that the field values are only defined on cells ...
    00104    */
    00105     for (it=1; it<=MED_N_CELL_FIXED_GEO; it++) {
    00106
    00107       geotype = geotypes[it];
    00108       /*
    00109        * How many profile for each geometry type ?
    00110        */
    00111       if ((nprofile = MEDfieldnProfile(fid, fieldname, numdt, numit, MED_CELL, geotype,
    00112                                        profilename, localizationname)) < 0) {
    00113         MESSAGE("ERROR : read number of profile ");
    00114         goto ERROR;
    00115       }
    00116
    00117       /*
    00118        * Read values for each profile
    00119        */
    00120       for (pit=0; pit<nprofile; pit++) {
    00121
    00122         if ((nvalues = MEDfieldnValueWithProfile(fid, fieldname, numdt, numit, MED_CELL, geotype,
    00123                                                  pit+1, MED_COMPACT_PFLMODE, profilename, &profilesize,
    00124                                                  localizationname, &nintegrationpoint)) < 0) {
    00125           MESSAGE("ERROR : read number of values with a profile ...");
    00126           goto ERROR;
    00127         }
    00128
    00129         if (nvalues) {
    00130           if ((values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent*nintegrationpoint)) == NULL) {
    00131             MESSAGE("ERROR : memory allocation ...");
    00132             goto ERROR;
    00133           }
    00134           if (MEDfieldValueWithProfileRd(fid, fieldname, numdt, numit, MED_CELL, geotype,
    00135                                          MED_COMPACT_PFLMODE, profilename,
    00136                                          MED_FULL_INTERLACE, MED_ALL_CONSTITUENT,
    00137                                          (unsigned char*) values) < 0) {
    00138             MESSAGE("ERROR : read fields values for cells ...");
    00139             free(values);
    00140             goto ERROR;
    00141           }
    00142           free(values);
    00143         }
    00144       }
    00145     }
    00146   }
    00147
    00148   ret=0;
    00149  ERROR:
    00150
    00151   /* close file */
    00152   if (MEDfileClose(fid) < 0) {
    00153     MESSAGE("ERROR : close file ...");
    00154     ret=-1;
    00155   }
    00156
    00157   return ret;
    00158 }