Actual source code: vnetcdf.c

  1: #define PETSC_DLL
  2: /*
  3:      Code for the parallel NetCDF viewer.
  4: */
 5:  #include src/sys/src/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:   PetscViewerFileType 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;
 23:   int                rank;

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

 37: PetscErrorCode PETSC_DLLEXPORT PetscViewerCreate_Netcdf(PetscViewer v)
 38: {
 39:   PetscErrorCode     ierr;
 40:   PetscViewer_Netcdf *vnetcdf;

 43:   PetscNew(PetscViewer_Netcdf,&vnetcdf);
 44:   v->data            = (void*)vnetcdf;
 45:   v->ops->destroy    = PetscViewerDestroy_Netcdf;
 46:   v->ops->flush      = 0;
 47:   v->iformat         = 0;
 48:   vnetcdf->ncid      = -1;
 49:   vnetcdf->nctype    = (PetscViewerFileType) -1;
 50:   vnetcdf->filename  = 0;

 52:   PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerSetFilename_C",
 53:                                     "PetscViewerSetFilename_Netcdf",
 54:                                      PetscViewerSetFilename_Netcdf);
 55:   PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerSetFileType_C",
 56:                                     "PetscViewerSetFileType_Netcdf",
 57:                                      PetscViewerSetFileType_Netcdf);
 58:   return(0);
 59: }


 65: PetscErrorCode PETSC_DLLEXPORT PetscViewerNetcdfGetID(PetscViewer viewer,int *ncid)
 66: {
 67:   PetscViewer_Netcdf *vnetcdf = (PetscViewer_Netcdf*)viewer->data;

 70:   *ncid = vnetcdf->ncid;
 71:   return(0);
 72: }

 77: PetscErrorCode PETSC_DLLEXPORT PetscViewerSetFileType_Netcdf(PetscViewer viewer,PetscViewerFileType type)
 78: {
 79:   PetscViewer_Netcdf *vnetcdf = (PetscViewer_Netcdf*)viewer->data;

 82:   vnetcdf->nctype = type;
 83:   return(0);
 84: }


 90: PetscErrorCode PETSC_DLLEXPORT PetscViewerNetcdfOpen(MPI_Comm comm,const char name[],PetscViewerFileType type,PetscViewer* viewer)
 91: {

 95:   PetscViewerCreate(comm,viewer);
 96:   PetscViewerSetType(*viewer,PETSC_VIEWER_NETCDF);
 97:   PetscViewerSetFileType(*viewer,type);
 98:   PetscViewerSetFilename(*viewer,name);
 99:   return(0);
100: }

105: PetscErrorCode PETSC_DLLEXPORT PetscViewerSetFilename_Netcdf(PetscViewer viewer,const char name[])
106: {
107:   int                 rank;
108:   PetscErrorCode      ierr;
109:   PetscViewer_Netcdf  *vnetcdf = (PetscViewer_Netcdf*)viewer->data;
110:   PetscViewerFileType type = vnetcdf->nctype;
111:   MPI_Comm            comm = viewer->comm;
112:   PetscTruth          flg;
113:   char                fname[PETSC_MAX_PATH_LEN],*gz;
114: 
116:   PetscOptionsGetString(PETSC_NULL,"-netcdf_viewer_name",fname,PETSC_MAX_PATH_LEN,&flg);
117:   if (flg) {
118:     PetscStrallocpy(fname,&vnetcdf->filename);
119:   } else {
120:     PetscStrallocpy(name,&vnetcdf->filename);
121:   }
122:   if (type == (PetscViewerFileType) -1) {
123:     SETERRQ(PETSC_ERR_ORDER,"Must call PetscViewerSetFileType() before PetscViewerSetFilename()");
124:   } else if (type == PETSC_FILE_RDONLY) {
125:     ncmpi_open(comm,vnetcdf->filename,0,MPI_INFO_NULL,&vnetcdf->ncid);
126:   } else if (type == PETSC_FILE_RDWR) {
127:     ncmpi_open(comm,vnetcdf->filename,NC_WRITE,MPI_INFO_NULL,&vnetcdf->ncid);
128:   } else if (type == PETSC_FILE_CREATE) {
129:     ncmpi_create(comm,vnetcdf->filename,NC_CLOBBER,MPI_INFO_NULL,&vnetcdf->ncid);
130:   }
131:   return(0);
132: }