Actual source code: vnetcdf.c

  1: #define PETSC_DLL
  2: /*
  3:      Code for the parallel NetCDF viewer.
  4: */
 5:  #include private/viewerimpl.h
  7: #include "pnetcdf.h"
  9: typedef struct  {
 10:   int                 ncid;            /* NetCDF dataset id */
 11:   char                *filename;        /* NetCDF dataset name */
 12:   PetscFileMode nctype;          /* read or write? */
 13: } PetscViewer_Netcdf;


 18: PetscErrorCode PetscViewerDestroy_Netcdf(PetscViewer v)
 19: {
 20:   PetscViewer_Netcdf *vnetcdf = (PetscViewer_Netcdf*)v->data;
 21:   PetscErrorCode     ierr;

 24:   if (vnetcdf->ncid) {
 25:     ncmpi_close(vnetcdf->ncid);
 26:   }
 27:   PetscStrfree(vnetcdf->filename);
 28:   PetscFree(vnetcdf);
 29:   return(0);
 30: }

 34: PetscErrorCode  PetscViewerNetcdfGetID(PetscViewer viewer,int *ncid)
 35: {
 36:   PetscViewer_Netcdf *vnetcdf = (PetscViewer_Netcdf*)viewer->data;

 39:   *ncid = vnetcdf->ncid;
 40:   return(0);
 41: }

 46: PetscErrorCode  PetscViewerFileSetMode_Netcdf(PetscViewer viewer,PetscFileMode type)
 47: {
 48:   PetscViewer_Netcdf *vnetcdf = (PetscViewer_Netcdf*)viewer->data;

 51:   vnetcdf->nctype = type;
 52:   return(0);
 53: }


 59: PetscErrorCode  PetscViewerNetcdfOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer* viewer)
 60: {

 64:   PetscViewerCreate(comm,viewer);
 65:   PetscViewerSetType(*viewer,PETSC_VIEWER_NETCDF);
 66:   PetscViewerFileSetMode(*viewer,type);
 67:   PetscViewerFileSetName(*viewer,name);
 68:   return(0);
 69: }

 74: PetscErrorCode  PetscViewerFileSetName_Netcdf(PetscViewer viewer,const char name[])
 75: {
 76:   PetscErrorCode      ierr;
 77:   PetscViewer_Netcdf  *vnetcdf = (PetscViewer_Netcdf*)viewer->data;
 78:   PetscFileMode type = vnetcdf->nctype;
 79:   MPI_Comm            comm = ((PetscObject)viewer)->comm;
 80:   PetscTruth          flg;
 81:   char                fname[PETSC_MAX_PATH_LEN];
 82: 
 84:   PetscOptionsGetString(PETSC_NULL,"-netcdf_viewer_name",fname,PETSC_MAX_PATH_LEN,&flg);
 85:   if (flg) {
 86:     PetscStrallocpy(fname,&vnetcdf->filename);
 87:   } else {
 88:     PetscStrallocpy(name,&vnetcdf->filename);
 89:   }
 90:   if (type == (PetscFileMode) -1) {
 91:     SETERRQ(PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
 92:   } else if (type == FILE_MODE_READ) {
 93:     ncmpi_open(comm,vnetcdf->filename,0,MPI_INFO_NULL,&vnetcdf->ncid);
 94:   } else if (type == FILE_MODE_WRITE) {
 95:     ncmpi_open(comm,vnetcdf->filename,NC_WRITE,MPI_INFO_NULL,&vnetcdf->ncid);
 96:   } else if (type == FILE_MODE_WRITE) {
 97:     ncmpi_create(comm,vnetcdf->filename,NC_CLOBBER,MPI_INFO_NULL,&vnetcdf->ncid);
 98:   }
 99:   return(0);
100: }


107: PetscErrorCode  PetscViewerCreate_Netcdf(PetscViewer v)
108: {
109:   PetscErrorCode     ierr;
110:   PetscViewer_Netcdf *vnetcdf;

113:   PetscNewLog(v,PetscViewer_Netcdf,&vnetcdf);
114:   v->data            = (void*)vnetcdf;
115:   v->ops->destroy    = PetscViewerDestroy_Netcdf;
116:   v->ops->flush      = 0;
117:   v->iformat         = 0;
118:   vnetcdf->ncid      = -1;
119:   vnetcdf->nctype    = (PetscFileMode) -1;
120:   vnetcdf->filename  = 0;

122:   PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerFileSetName_C",
123:                                     "PetscViewerFileSetName_Netcdf",
124:                                      PetscViewerFileSetName_Netcdf);
125:   PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerFileSetMode_C",
126:                                     "PetscViewerFileSetMode_Netcdf",
127:                                      PetscViewerFileSetMode_Netcdf);
128:   return(0);
129: }