# UsesCase_MEDfield_2.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 2 : read the field of use case 1
    00020  */
    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 *verticesvalues = NULL;
    00039   med_float *tria3values = NULL;
    00040   med_float *quad4values = NULL;
    00041   med_int nstep, nvalues;
    00042   const med_int ncomponent = 1;
    00043   int ret=-1;
    00044
    00045   /* file creation */
    00046   fid = MEDfileOpen("UsesCase_MEDfield_1.med",MED_ACC_RDONLY);
    00047   if (fid < 0) {
    00048     MESSAGE("ERROR : open file ...");
    00049     goto ERROR;
    00050   }
    00051
    00052   /*
    00053    * ... we know that the MED file has only one field with one component ,
    00054    * a real code working would check ...
    00055    */
    00056
    00057   /*
    00058    * if you know the field name, direct access to field informations
    00059    */
    00060   if (MEDfieldInfoByName(fid, fieldname, meshname, &localmesh, &fieldtype,
    00061                          componentname, componentunit, dtunit, &nstep) < 0) {
    00062     MESSAGE("ERROR : Field info by name ...");
    00063     goto ERROR;
    00064   }
    00065
    00066   /*
    00067    * ... we know that the field values are defined on vertices and MED_TRIA3
    00068    * and MED_QUAD4 cells, a real code working would check ...
    00069    */
    00070
    00071   /* MED_NODE */
    00072   if ((nvalues = MEDfieldnValue(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NONE)) < 0) {
    00073     MESSAGE("ERROR : read number of values ...");
    00074     goto ERROR;
    00075   }
    00076
    00077   if ((verticesvalues = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent)) == NULL) {
    00078     MESSAGE("ERROR : memory allocation ...");
    00079     goto ERROR;
    00080   }
    00081   if (MEDfieldValueRd(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NONE,
    00082                       MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, (unsigned char*) verticesvalues) < 0) {
    00083     MESSAGE("ERROR : read fields values on vertices ...");
    00084     free(verticesvalues);
    00085     goto ERROR;
    00086   }
    00087   free(verticesvalues);
    00088
    00089   /* MED_TRIA3 */
    00090   if ((nvalues = MEDfieldnValue(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_CELL,
    00091                                 MED_TRIA3)) < 0) {
    00092     MESSAGE("ERROR : read number of values ...");
    00093     goto ERROR;
    00094   }
    00095   if ((tria3values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent)) == NULL) {
    00096     MESSAGE("ERROR : memory allocation ...");
    00097     goto ERROR;
    00098   }
    00099   if (MEDfieldValueRd(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_CELL, MED_TRIA3,
    00100                       MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, (unsigned char*) tria3values) < 0) {
    00101     MESSAGE("ERROR : read fields values for MED_TRIA3 cells ...");
    00102     free(tria3values);
    00103     goto ERROR;
    00104   }
    00105   free(tria3values);
    00106
    00107   /* MED_QUAD4 */
    00108   if ((nvalues = MEDfieldnValue(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_CELL,
    00109                                 MED_QUAD4)) < 0) {
    00110     MESSAGE("ERROR : read number of values ...");
    00111     goto ERROR;
    00112   }
    00113   if ((quad4values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent)) == NULL) {
    00114     MESSAGE("ERROR : memory allocation ...");
    00115     goto ERROR;
    00116   }
    00117   if (MEDfieldValueRd(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_CELL, MED_QUAD4,
    00118                       MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, (unsigned char*) quad4values) < 0) {
    00119     MESSAGE("ERROR : read fields values for MED_QUAD4 cells ...");
    00120     free(quad4values);
    00121     goto ERROR;
    00122   }
    00123   free(quad4values);
    00124
    00125   ret=0;
    00126  ERROR:
    00127
    00128   /* close file */
    00129   if (MEDfileClose(fid) < 0) {
    00130     MESSAGE("ERROR : close file ...");
    00131     ret=-1;
    00132   }
    00133
    00134   return ret;
    00135 }