Actual source code: vnetcdf.c
1: /*
2: Code for the parallel NetCDF viewer.
3: */
4: #include src/sys/src/viewer/viewerimpl.h
5: #include petscsys.h
7: #include "pnetcdf.h"
9: typedef struct {
10: int ncid; /* NetCDF dataset id */
11: char *filename; /* NetCDF dataset name */
12: PetscViewerFileType 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;
22: int rank;
25: if (vnetcdf->ncid) {
26: ncmpi_close(vnetcdf->ncid);
27: }
28: PetscStrfree(vnetcdf->filename);
29: PetscFree(vnetcdf);
30: return(0);
31: }
36: PetscErrorCode PetscViewerCreate_Netcdf(PetscViewer v)
37: {
38: PetscErrorCode ierr;
39: PetscViewer_Netcdf *vnetcdf;
42: PetscNew(PetscViewer_Netcdf,&vnetcdf);
43: v->data = (void*)vnetcdf;
44: v->ops->destroy = PetscViewerDestroy_Netcdf;
45: v->ops->flush = 0;
46: v->iformat = 0;
47: vnetcdf->ncid = -1;
48: vnetcdf->nctype = (PetscViewerFileType) -1;
49: vnetcdf->filename = 0;
51: PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerSetFilename_C",
52: "PetscViewerSetFilename_Netcdf",
53: PetscViewerSetFilename_Netcdf);
54: PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerSetFileType_C",
55: "PetscViewerSetFileType_Netcdf",
56: PetscViewerSetFileType_Netcdf);
57: return(0);
58: }
64: PetscErrorCode PetscViewerNetcdfGetID(PetscViewer viewer,int *ncid)
65: {
66: PetscViewer_Netcdf *vnetcdf = (PetscViewer_Netcdf*)viewer->data;
69: *ncid = vnetcdf->ncid;
70: return(0);
71: }
76: PetscErrorCode PetscViewerSetFileType_Netcdf(PetscViewer viewer,PetscViewerFileType type)
77: {
78: PetscViewer_Netcdf *vnetcdf = (PetscViewer_Netcdf*)viewer->data;
81: vnetcdf->nctype = type;
82: return(0);
83: }
89: PetscErrorCode PetscViewerNetcdfOpen(MPI_Comm comm,const char name[],PetscViewerFileType type,PetscViewer* viewer)
90: {
94: PetscViewerCreate(comm,viewer);
95: PetscViewerSetType(*viewer,PETSC_VIEWER_NETCDF);
96: PetscViewerSetFileType(*viewer,type);
97: PetscViewerSetFilename(*viewer,name);
98: return(0);
99: }
104: PetscErrorCode PetscViewerSetFilename_Netcdf(PetscViewer viewer,const char name[])
105: {
106: int rank;
107: PetscErrorCode ierr;
108: PetscViewer_Netcdf *vnetcdf = (PetscViewer_Netcdf*)viewer->data;
109: PetscViewerFileType type = vnetcdf->nctype;
110: MPI_Comm comm = viewer->comm;
111: PetscTruth flg;
112: char fname[PETSC_MAX_PATH_LEN],*gz;
113:
115: PetscOptionsGetString(PETSC_NULL,"-netcdf_viewer_name",fname,PETSC_MAX_PATH_LEN,&flg);
116: if (flg) {
117: PetscStrallocpy(fname,&vnetcdf->filename);
118: } else {
119: PetscStrallocpy(name,&vnetcdf->filename);
120: }
121: if (type == (PetscViewerFileType) -1) {
122: SETERRQ(PETSC_ERR_ORDER,"Must call PetscViewerSetFileType() before PetscViewerSetFilename()");
123: } else if (type == PETSC_FILE_RDONLY) {
124: ncmpi_open(comm,vnetcdf->filename,0,MPI_INFO_NULL,&vnetcdf->ncid);
125: } else if (type == PETSC_FILE_RDWR) {
126: ncmpi_open(comm,vnetcdf->filename,NC_WRITE,MPI_INFO_NULL,&vnetcdf->ncid);
127: } else if (type == PETSC_FILE_CREATE) {
128: ncmpi_create(comm,vnetcdf->filename,NC_CLOBBER,MPI_INFO_NULL,&vnetcdf->ncid);
129: }
130: return(0);
131: }