Actual source code: vnetcdf.c

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


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

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

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

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

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

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


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

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

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


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

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

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