#include <med.h>
#define MESGERR 1
#include <med_utils.h>
#include <string.h>
int main (int argc, char **argv) {
  med_idt fid;
  const char meshname[MED_NAME_SIZE+1] = "2D unstructured mesh";
  const med_int spacedim = 2;
  const med_int meshdim = 2;
  
  const char axisname[2*MED_SNAME_SIZE+1] = "x               y               ";
  const char unitname[2*MED_SNAME_SIZE+1] = "cm              cm              ";
  const med_float coordinates[2*12] = { 0.5,   0.,  
                                        1.5,   0.,  
                                        0.,    0.5,
                                        1.,    0.5,
                                        2.,    0.5,
                                        0.,    1.,
                                        1.,    1.,
                                        2.,    1.,
                                        0.5,   2.,
                                        1.5,   2. };
  const med_int nnodes = 12;
  const med_int indexsize = 3;
  const med_int index[3] = {1,7,13};
  
  const med_int connectivity[12] = {1,4,7,9,6,3,
                                    2,5,8,10,7,4};
  int ret=-1;
  
  
  fid = MEDfileOpen("UsesCase_MEDmesh_13.med",
                    MED_ACC_CREAT);
  if (fid < 0) {
    MESSAGE("ERROR : file creation ...");
    goto ERROR;
  }
  
  if (MEDfileCommentWr(fid,
                       "A 2D unstructured mesh : 12, 12 polygons") < 0) {
    MESSAGE("ERROR : write file description ...");
    goto ERROR;
  }
  
  
  if (MEDmeshCr(fid, 
                meshname, 
                spacedim, 
                meshdim, 
                MED_UNSTRUCTURED_MESH, 
                "A 2D mesh with 2 polygons",
                "",
                MED_SORT_DTIT,
                MED_CARTESIAN, 
                axisname, 
                unitname) < 0) {
    MESSAGE("ERROR : mesh creation ...");
    goto ERROR;
  }
  
  if (MEDmeshNodeCoordinateWr(fid, 
                              meshname, 
                              MED_NO_DT, 
                              MED_NO_IT, 
                              MED_UNDEF_DT,
                              MED_FULL_INTERLACE, 
                              nnodes, 
                              coordinates) < 0) {
    MESSAGE("ERROR : nodes coordinates ...");
    goto ERROR;
  }
  
  
  if (MEDmeshPolygonWr(fid,
                       meshname,
                       MED_NO_DT,
                       MED_NO_IT,
                       MED_UNDEF_DT,
                       MED_CELL,
                       MED_NODAL,
                       indexsize,
                       index,
                       connectivity) < 0) {
    MESSAGE("ERROR : polygon connectivity ...");
    goto ERROR;
  }
  
  if (MEDfamilyCr(fid, 
                  meshname,
                  MED_NO_NAME, 
                  0, 
                  0, 
                  MED_NO_GROUP) < 0) {
    MESSAGE("ERROR : family 0 creation ...");
    goto ERROR;
  }
  ret=0;
 ERROR:
  
  
  if (MEDfileClose(fid)  < 0) {
    MESSAGE("ERROR : close file ...");
    ret=-1;
  }
  return ret;
}