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
6: EXTERN_C_BEGIN
7: #include "pnetcdf.h"
8: EXTERN_C_END
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: int PetscViewerDestroy_Netcdf(PetscViewer v)
19: {
20: PetscViewer_Netcdf *vnetcdf = (PetscViewer_Netcdf*)v->data;
21: int ierr,rank;
24: if (vnetcdf->ncid) {
25: ncmpi_close(vnetcdf->ncid);
26: }
27: PetscStrfree(vnetcdf->filename);
28: PetscFree(vnetcdf);
29: return(0);
30: }
32: EXTERN_C_BEGIN
35: int PetscViewerCreate_Netcdf(PetscViewer v)
36: {
37: int ierr;
38: PetscViewer_Netcdf *vnetcdf;
41: PetscNew(PetscViewer_Netcdf,&vnetcdf);
42: v->data = (void*)vnetcdf;
43: v->ops->destroy = PetscViewerDestroy_Netcdf;
44: v->ops->flush = 0;
45: v->iformat = 0;
46: vnetcdf->ncid = -1;
47: vnetcdf->nctype = (PetscViewerFileType) -1;
48: vnetcdf->filename = 0;
50: PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerSetFilename_C",
51: "PetscViewerSetFilename_Netcdf",
52: PetscViewerSetFilename_Netcdf);
53: PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerSetFileType_C",
54: "PetscViewerSetFileType_Netcdf",
55: PetscViewerSetFileType_Netcdf);
56: return(0);
57: }
58: EXTERN_C_END
63: int PetscViewerNetcdfGetID(PetscViewer viewer,int *ncid)
64: {
65: PetscViewer_Netcdf *vnetcdf = (PetscViewer_Netcdf*)viewer->data;
68: *ncid = vnetcdf->ncid;
69: return(0);
70: }
72: EXTERN_C_BEGIN
75: int PetscViewerSetFileType_Netcdf(PetscViewer viewer,PetscViewerFileType type)
76: {
77: PetscViewer_Netcdf *vnetcdf = (PetscViewer_Netcdf*)viewer->data;
80: vnetcdf->nctype = type;
81: return(0);
82: }
83: EXTERN_C_END
88: int PetscViewerNetcdfOpen(MPI_Comm comm,const char name[],PetscViewerFileType type,PetscViewer* viewer)
89: {
93: PetscViewerCreate(comm,viewer);
94: PetscViewerSetType(*viewer,PETSC_VIEWER_NETCDF);
95: PetscViewerSetFileType(*viewer,type);
96: PetscViewerSetFilename(*viewer,name);
98: return(0);
99: }
101: EXTERN_C_BEGIN
104: int PetscViewerSetFilename_Netcdf(PetscViewer viewer,const char name[])
105: {
106: int rank,ierr;
107: PetscViewer_Netcdf *vnetcdf = (PetscViewer_Netcdf*)viewer->data;
108: PetscViewerFileType type = vnetcdf->nctype;
109: MPI_Comm comm = viewer->comm;
110: PetscTruth flg;
111: char fname[PETSC_MAX_PATH_LEN],*gz;
112:
114: PetscOptionsGetString(PETSC_NULL,"-netcdf_viewer_name",fname,PETSC_MAX_PATH_LEN,&flg);
115: if (flg) {
116: PetscStrallocpy(fname,&vnetcdf->filename);
117: } else {
118: PetscStrallocpy(name,&vnetcdf->filename);
119: }
120: if (type == (PetscViewerFileType) -1) {
121: SETERRQ(1,"Must call PetscViewerSetFileType() before PetscViewerSetFilename()");
122: } else if (type == PETSC_FILE_RDONLY) {
123: ncmpi_open(comm,vnetcdf->filename,0,MPI_INFO_NULL,&vnetcdf->ncid);
124: } else if (type == PETSC_FILE_RDWR) {
125: ncmpi_open(comm,vnetcdf->filename,NC_WRITE,MPI_INFO_NULL,&vnetcdf->ncid);
126: } else if (type == PETSC_FILE_CREATE) {
127: ncmpi_create(comm,vnetcdf->filename,NC_CLOBBER,MPI_INFO_NULL,&vnetcdf->ncid);
128: }
129: return(0);
130: }
132: EXTERN_C_END