Actual source code: hdf4v.c

      1: #define PETSC_DLL
      2: #define HERE do { PetscErrorCode ierr, rank; MPI_Comm_rank(PETSC_COMM_WORLD, &rank); PetscPrintf(PETSC_COMM_SELF,"[%d] LINE %d (%s)\n", rank, __LINE__, __FUNCTION__); } while (0)

      4: #define CE do {  } while (0)

      6: #include "src/sys/src/viewer/viewerimpl.h"    /*I   "petsc.h"   I*/
      7: #if defined(PETSC_HAVE_HDF4)
      8: #include <mfhdf.h>
      9: #endif

     11: typedef struct {
     12:  int                 sd_id;
     13:  char                *filename;
     14:  PetscViewerFileType btype;
     15: } PetscViewer_HDF4;

     19: PetscErrorCode PetscViewerDestroy_HDF4(PetscViewer v)
     20: {
     21:  PetscErrorCode   ierr;
     22:  PetscViewer_HDF4 *vhdf4 = (PetscViewer_HDF4 *)v->data;

     25:  if (vhdf4->sd_id >= 0) {
     26:  SDend(vhdf4->sd_id);
     27:  vhdf4->sd_id = -1;
     28:  }
     29:  if (vhdf4->filename) {
     30:    PetscFree(vhdf4->filename);CE;
     31:  }
     32:  PetscFree(vhdf4); CE;
     33:  return(0);
     34: }

     39: PetscErrorCode PETSC_DLLEXPORT PetscViewerSetFileType_HDF4(PetscViewer viewer, PetscViewerFileType type)
     40: {
     41:  PetscViewer_HDF4 *vhdf4 = (PetscViewer_HDF4 *)viewer->data;
     42:
     45:  vhdf4->btype = type;
     46:  return(0);
     47: }

     53: PetscErrorCode PETSC_DLLEXPORT PetscViewerSetFilename_HDF4(PetscViewer viewer,const char name[])
     54: {
     55:  PetscErrorCode        ierr;
     56:  int                   rank;
     57:  PetscViewer_HDF4      *vhdf4 = (PetscViewer_HDF4*)viewer->data;
     58:  int32                 acc;

     62:  switch (vhdf4->btype) {
     63:  case PETSC_FILE_RDONLY:
     64:    acc = DFACC_READ;
     65:    break;
     66:  case PETSC_FILE_WRONLY:
     67:    acc = DFACC_WRITE;
     68:  break;
     69:  case PETSC_FILE_CREATE:
     70:    acc = DFACC_CREATE;
     71:    break;
     72:  default:
     73:    SETERRQ(PETSC_ERR_ORDER,"Must call PetscViewerSetFileType() before PetscViewerSetFilename()");
     74:  }

     76:  MPI_Comm_rank(viewer->comm,&rank);CE;
     77:  PetscStrallocpy(name,&vhdf4->filename);CE;
     78:  if (!rank) {
     79:    vhdf4->sd_id = SDstart(name, acc);
     80:    if (vhdf4->sd_id < 0) {
     81:      SETERRQ1(PETSC_ERR_LIB, "SDstart failed for %s", name);
     82:    }
     83:  }
     84:  viewer->format = PETSC_VIEWER_NOFORMAT;
     85:  return(0);
     86: }

     92: PetscErrorCode PETSC_DLLEXPORT PetscViewerCreate_HDF4(PetscViewer v)
     93: {
     94:  PetscErrorCode   ierr;
     95:  PetscViewer_HDF4 *vhdf4;
     96:
     98:  PetscNew(PetscViewer_HDF4,&vhdf4); CE;
     99:  v->data            = (void*)vhdf4;
    100:  v->ops->destroy    = PetscViewerDestroy_HDF4;
    101:  v->ops->flush      = 0;
    102:  v->iformat         = 0;
    103:  vhdf4->btype       = (PetscViewerFileType) -1;
    104:  vhdf4->filename    = 0;
    105:  vhdf4->sd_id       = -1;
    106:
    107:  PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerSetFilename_C","PetscViewerSetFilename_HDF4",
    108:                                            PetscViewerSetFilename_HDF4);CE;
    109:  PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerSetFileType_C","PetscViewerSetFileType_HDF4",
    110:                                            PetscViewerSetFileType_HDF4);CE;
    111:  return(0);
    112: }

    118: PetscErrorCode PETSC_DLLEXPORT PetscViewerHDF4Open(MPI_Comm comm, const char *name, PetscViewerFileType type, PetscViewer *hdf4v)
    119: {
    121:
    123:  PetscViewerCreate(comm,hdf4v);CE;
    124:  PetscViewerSetType(*hdf4v,PETSC_VIEWER_HDF4);CE;
    125:  PetscViewerSetFileType(*hdf4v, type);CE;
    126:  PetscViewerSetFilename(*hdf4v, name);CE;
    127:  return(0);
    128: }

    133: PetscErrorCode PETSC_DLLEXPORT PetscViewerHDF4WriteSDS(PetscViewer viewer, float *xf, int d, int *dims,int bs)
    134: {
    135:  int                   i;
    136:  PetscViewer_HDF4      *vhdf4 = (PetscViewer_HDF4*)viewer->data;
    137:  int32                 sds_id,zero32[3],dims32[3];

    141:  for (i = 0; i < d; i++) {
    142:    zero32[i] = 0;
    143:    dims32[i] = dims[i];
    144:  }
    145:  sds_id = SDcreate(vhdf4->sd_id, "Vec", DFNT_FLOAT32, d, dims32);
    146:  if (sds_id < 0) {
    147:    SETERRQ(PETSC_ERR_LIB,"SDcreate failed");
    148:  }
    149:  SDwritedata(sds_id, zero32, 0, dims32, xf);
    150:  SDendaccess(sds_id);
    151:  return(0);
    152: }