UsesCase_MEDfield_3.c
Aller à la documentation de ce fichier.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
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 
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   
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 
00061 
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     
00071 
00072 
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     
00098     
00099     
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     
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   
00149   if (MEDfileClose(fid) < 0) {
00150     MESSAGE("ERROR : close file ...");             
00151     ret=-1; 
00152   } 
00153   
00154   return ret;
00155 }