# UsesCase_MEDfield_3.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 3 : read a field (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_geometry_type geotype;
    00049   med_geometry_type *geotypes = MED_GET_CELL_GEOMETRY_TYPE;
    00050   int ret=-1;
    00051
    00052   /* file creation */
    00053   fid = MEDfileOpen("UsesCase_MEDfield_1.med",MED_ACC_RDONLY);
    00054   if (fid < 0) {
    00055     MESSAGE("ERROR : open file ...");
    00056     goto ERROR;
    00057   }
    00058
    00059   /*
    00060    * generic approach : how many fields in the file and identification
    00061    * of each field.
    00062    */
    00063   if ((nfield = MEDnField(fid)) < 0) {
    00064     MESSAGE("ERROR : How many fields in the file ...");
    00065     goto ERROR;
    00066   }
    00067
    00068   for (i=0; i<nfield; i++) {
    00069
    00070     /* field information
    00071      * ... we know that the field has no computation step
    00072      * and that the field values type is MED_FLOAT64, a real code working would check ...
    00073      */
    00074     if ((ncomponent = MEDfieldnComponent(fid,i+1)) < 0) {
    00075       MESSAGE("ERROR : number of field component ...");
    00076       goto ERROR;
    00077     }
    00078
    00079     if ((componentname = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
    00080       MESSAGE("ERROR : memory allocation ...");
    00081       goto ERROR;
    00082     }
    00083
    00084     if ((componentunit = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
    00085       MESSAGE("ERROR : memory allocation ...");
    00086       goto ERROR;
    00087     }
    00088
    00089     if (MEDfieldInfo(fid, i+1, fieldname, meshname, &localmesh, &fieldtype,
    00090                      componentname, componentunit, dtunit, &nstep) < 0) {
    00091       MESSAGE("ERROR : Field info ...");
    00092       free(componentname); free(componentunit);
    00093       goto ERROR;
    00094     }
    00095     free(componentname); free(componentunit);
    00096
    00097     /* read field values for nodes and cells */
    00098
    00099     /* MED_NODE */
    00100     if ((nvalues = MEDfieldnValue(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NONE)) < 0) {
    00101       MESSAGE("ERROR : read number of values ...");
    00102       goto ERROR;
    00103     }
    00104
    00105     if (nvalues) {
    00106       if ((values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent)) == NULL) {
    00107         MESSAGE("ERROR : memory allocation ...");
    00108         goto ERROR;
    00109       }
    00110       if (MEDfieldValueRd(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NONE,
    00111                           MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, (unsigned char*) values) < 0) {
    00112         MESSAGE("ERROR : read fields values defined on vertices ...");
    00113         free(values);
    00114         goto ERROR;
    00115       }
    00116       free(values);
    00117     }
    00118
    00119     /* MED_CELL */
    00120     for (j=1; j<=MED_N_CELL_FIXED_GEO; j++) {
    00121       geotype = geotypes[j];
    00122
    00123       if ((nvalues = MEDfieldnValue(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_CELL,
    00124                                     geotype)) < 0) {
    00125         MESSAGE("ERROR : read number of values ...");
    00126         goto ERROR;
    00127       }
    00128
    00129       if (nvalues) {
    00130         if ((values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent)) == NULL) {
    00131           MESSAGE("ERROR : memory allocation ...");
    00132           goto ERROR;
    00133         }
    00134         if (MEDfieldValueRd(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_CELL, geotype,
    00135                             MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, (unsigned char*) values) < 0) {
    00136           MESSAGE("ERROR : read fields values for cells ...");
    00137           free(values);
    00138           goto ERROR;
    00139         }
    00140         free(values);
    00141       }
    00142     }
    00143   }
    00144
    00145   ret=0;
    00146  ERROR:
    00147
    00148   /* close file */
    00149   if (MEDfileClose(fid) < 0) {
    00150     MESSAGE("ERROR : close file ...");
    00151     ret=-1;
    00152   }
    00153
    00154   return ret;
    00155 }