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: PetscViewerNetcdfType 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 = (PetscViewerNetcdfType) -1;
48: vnetcdf->filename = 0;
50: PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerSetFilename_C",
51: "PetscViewerSetFilename_Netcdf",
52: PetscViewerSetFilename_Netcdf);
53: PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerNetcdfSetType_C",
54: "PetscViewerNetcdfSetType_Netcdf",
55: PetscViewerNetcdfSetType_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 PetscViewerNetcdfSetType_Netcdf(PetscViewer viewer,PetscViewerNetcdfType type)
76: {
77: PetscViewer_Netcdf *vnetcdf = (PetscViewer_Netcdf*)viewer->data;
80: vnetcdf->nctype = type;
81: return(0);
82: }
83: EXTERN_C_END
85: int PetscViewerNetcdfSetType(PetscViewer viewer,PetscViewerNetcdfType type)
86: {
87: int ierr,(*f)(PetscViewer,PetscViewerNetcdfType);
91: PetscObjectQueryFunction((PetscObject)viewer,"PetscViewerNetcdfSetType_C",(void (**)(void))&f);
92: if (f) {
93: (*f)(viewer,type);
94: }
96: return(0);
97: }
102: int PetscViewerNetcdfOpen(MPI_Comm comm,const char name[],PetscViewerNetcdfType type,PetscViewer* viewer)
103: {
107: PetscViewerCreate(comm,viewer);
108: PetscViewerSetType(*viewer,PETSC_VIEWER_NETCDF);
109: PetscViewerNetcdfSetType(*viewer,type);
110: PetscViewerSetFilename(*viewer,name);
112: return(0);
113: }
115: EXTERN_C_BEGIN
118: int PetscViewerSetFilename_Netcdf(PetscViewer viewer,const char name[])
119: {
120: int rank,ierr;
121: PetscViewer_Netcdf *vnetcdf = (PetscViewer_Netcdf*)viewer->data;
122: PetscViewerNetcdfType type = vnetcdf->nctype;
123: MPI_Comm comm = viewer->comm;
124: PetscTruth flg;
125: char fname[PETSC_MAX_PATH_LEN],*gz;
126:
128: PetscOptionsGetString(PETSC_NULL,"-netcdf_viewer_name",fname,PETSC_MAX_PATH_LEN,&flg);
129: if (flg) {
130: PetscStrallocpy(fname,&vnetcdf->filename);
131: } else {
132: PetscStrallocpy(name,&vnetcdf->filename);
133: }
134: if (type == (PetscViewerNetcdfType) -1) {
135: SETERRQ(1,"Must call PetscViewerNetcdfSetType() before PetscViewerSetFilename()");
136: } else if (type == PETSC_NETCDF_RDONLY) {
137: ncmpi_open(comm,vnetcdf->filename,0,MPI_INFO_NULL,&vnetcdf->ncid);
138: } else if (type == PETSC_NETCDF_RDWR) {
139: ncmpi_open(comm,vnetcdf->filename,NC_WRITE,MPI_INFO_NULL,&vnetcdf->ncid);
140: } else if (type == PETSC_NETCDF_CREATE) {
141: ncmpi_create(comm,vnetcdf->filename,NC_CLOBBER,MPI_INFO_NULL,&vnetcdf->ncid);
142: }
143: return(0);
144: }
146: EXTERN_C_END