Skip to content

Commit e5f3fdf

Browse files
committed
doc: add example code fragment
1 parent f290c51 commit e5f3fdf

File tree

6 files changed

+295
-77
lines changed

6 files changed

+295
-77
lines changed

examples/fill_mode.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,18 @@ def pnetcdf_io(filename):
8181
else:
8282
print("The old fill mode is NC_NOFILL\n")
8383

84-
# set the fill mode to back to NC_NOFILL for the entire file
84+
# set the fill mode back to NC_NOFILL for the entire file
8585
f.set_fill(pnetcdf.NC_NOFILL)
8686

87-
# set the variable's fill mode to NC_FILL with default fill value
87+
# set the variable's fill mode to NC_FILL with PnetCDF default fill value
8888
fix_var.def_fill(no_fill = 0)
8989

90-
# set a customized fill value -1
90+
# enable the variable's fill mode and use a customized value, -1
9191
fill_value = np.int32(-1)
92+
rec_var.def_fill(no_fill = 0, fill_value = fill_value)
93+
94+
# Equivalently this can be done by setting the PnetCDF pre-defined
95+
# attribute "_FillValue"
9296
rec_var._FillValue = fill_value
9397

9498
# exit define mode

examples/nonblocking/nonblocking_write.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def pnetcdf_io(filename, length):
112112
if pnetcdf.strerrno(req_errs[i]) != "NC_NOERR":
113113
print(f"Error on request {i}:", pnetcdf.strerror(req_errs[i]))
114114

115-
# detach the temporary buffer
115+
# detach the buffer
116116
f.detach_buff()
117117

118118
# Close the file

examples/put_var.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def pnetcdf_io(filename, file_format):
9292

9393
# Define dimensions
9494
dim_y = f.def_dim("Y", global_ny)
95-
dim_x = f.def_dim("X",global_nx)
95+
dim_x = f.def_dim("X", global_nx)
9696

9797
# Define a 2D variable of integer type
9898
var = f.def_var("var", pnetcdf.NC_INT, (dim_y, dim_x))

src/pnetcdf/_Dimension.pyx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ cdef class Dimension:
3737
.. note:: ``Dimension`` instances should be created using the
3838
:meth:`File.def_dim` method of a ``File`` instance, not using
3939
:meth:`Dimension.__init__` directly.
40+
41+
:Example: A example is available in ``examples/put_var.py``
42+
43+
::
44+
# Define dimensions
45+
dim_t = f.def_dim('time', size = -1)
46+
dim_y = f.def_dim("Y", size = 100)
47+
dim_x = f.def_dim("X", size = 200)
48+
4049
"""
4150
cdef int ierr
4251
cdef char *dimname

src/pnetcdf/_File.pyx

Lines changed: 119 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ cdef class File:
6969
7070
:return: The created file instance.
7171
:rtype: :class:`pnetcdf.File`
72+
73+
:Example: A example is available in ``examples/create_open.py``
74+
75+
::
76+
# create a new file using file clobber mode, i.e. flag "-w"
77+
f = pnetcdf.File(filename = "foo.nc", mode = 'w', comm = MPI.COMM_WORLD, info = None)
78+
79+
# open an existing file for read only
80+
f = pnetcdf.File(filename = "foo.nc", mode = 'r', comm = MPI.COMM_WORLD, info = None)
81+
7282
"""
7383
cdef int ncid
7484
encoding = sys.getfilesystemencoding()
@@ -129,6 +139,14 @@ cdef class File:
129139
close(self)
130140
131141
Close the opened netCDF file
142+
143+
:Example: A example is available in ``examples/create_open.py``
144+
145+
::
146+
# create a new file using file clobber mode, i.e. flag "-w"
147+
f = pnetcdf.File(filename = "foo.nc", mode = 'w', comm = MPI.COMM_WORLD, info = None)
148+
f.close()
149+
132150
"""
133151
self._close(True)
134152

@@ -303,6 +321,16 @@ cdef class File:
303321
:param str dimname: Name of the new dimension.
304322
305323
:param int size: [Optional] Size of the new dimension.
324+
:Example: A example is available in ``examples/put_var.py``
325+
326+
::
327+
dim_t = f.def_dim('time', size = -1)
328+
dim_y = f.def_dim("Y", size = 100)
329+
dim_x = f.def_dim("X", size = 200)
330+
331+
# Define a 2D variable of integer type
332+
var = f.def_var("foo", pnetcdf.NC_INT, (dim_y, dim_x))
333+
306334
"""
307335
self.dimensions[dimname] = Dimension(self, dimname, size=size)
308336
return self.dimensions[dimname]
@@ -446,6 +474,16 @@ cdef class File:
446474
447475
:return: The created variable
448476
:rtype: :class:`pnetcdf.Variable`
477+
478+
:Example: A example is available in ``examples/put_var.py``
479+
480+
::
481+
dim_y = f.def_dim("Y", global_ny)
482+
dim_x = f.def_dim("X", global_nx)
483+
484+
# Define a 2D variable of integer type
485+
var = f.def_var("foo", pnetcdf.NC_INT, (dim_y, dim_x))
486+
449487
"""
450488

451489
# the following should be added to explanation of variable class.
@@ -521,6 +559,16 @@ cdef class File:
521559
522560
:Operational mode: This method must be called while the file is in
523561
define mode.
562+
563+
:Example: A example is available in ``examples/put_var.py``
564+
565+
::
566+
str_att = "example attribute of type text."
567+
var.foo_attr = str_att
568+
569+
# Equivalently, below uses function call
570+
var.put_att("foo_attr", str_att)
571+
524572
"""
525573
cdef nc_type xtype
526574
xtype=-99
@@ -545,6 +593,16 @@ cdef class File:
545593
546594
:Operational mode: This method can be called while the file is in
547595
either define or data mode (collective or independent).
596+
597+
:Example: A example is available in ``examples/get_var.py``
598+
599+
::
600+
# Get global attribute named "foo_attr"
601+
str_att = f.get_att("foo_attr")
602+
603+
# Get the variable's attribute named "foo_attr"
604+
str_att = v.foo_attr
605+
548606
"""
549607
return _get_att(self, NC_GLOBAL, name, encoding=encoding)
550608

@@ -681,13 +739,14 @@ cdef class File:
681739
_check_err(ierr)
682740
return None
683741

684-
def wait(self, num=None, requests=None, status=None):
742+
def wait_all(self, num=None, requests=None, status=None):
685743
"""
686-
wait(self, num=None, requests=None, status=None)
744+
wait_all(self, num=None, requests=None, status=None)
687745
688746
This method is a blocking call that wait for the completion of
689-
nonblocking I/O requests made by :meth:`Variable.iput_var`,
690-
:meth:`Variable.iget_var` and :meth:`Variable.bput_var`
747+
nonblocking I/O requests made by one of more method calls to
748+
:meth:`Variable.iput_var`, :meth:`Variable.iget_var` and
749+
:meth:`Variable.bput_var`
691750
692751
:param int num: [Optional]
693752
number of requests. It is also the array size of the next two
@@ -709,21 +768,35 @@ cdef class File:
709768
the error messages.
710769
:type status: list
711770
712-
:Operational mode: it is an independent subroutine and must be called
713-
while the file is in independent data mode.
771+
:Operational mode: it is an collective subroutine and must be called
772+
while the file is in collective data mode.
773+
774+
:Example: A example is available in ``examples/nonblocking/nonblocking_write.py``
775+
776+
::
777+
# Write one variable at a time, using iput APIs
778+
reqs = []
779+
for i in range(NUM_VARS):
780+
req_id = vars[i].iput_var(buf[i], start = start, count = count)
781+
reqs.append(req_id)
782+
783+
# commit posted nonblocking requests
784+
req_errs = [None] * NUM_VARS
785+
f.wait_all(NUM_VARS, reqs, req_errs)
786+
714787
"""
715-
return self._wait(num, requests, status, collective=False)
788+
return self._wait(num, requests, status, collective=True)
716789

717-
def wait_all(self, num=None, requests=None, status=None):
790+
def wait(self, num=None, requests=None, status=None):
718791
"""
719-
wait_all(self, num=None, requests=None, status=None)
792+
wait(self, num=None, requests=None, status=None)
720793
721-
Same as :meth:`File.wait` but in collective data mode
794+
Same as :meth:`File.wait_all` but called in independent data mode
722795
723-
:Operational mode: it is an collective subroutine and must be called
724-
while the file is in collective data mode.
796+
:Operational mode: it is an independent subroutine and must be called
797+
while the file is in independent data mode.
725798
"""
726-
return self._wait(num, requests, status, collective=True)
799+
return self._wait(num, requests, status, collective=False)
727800

728801
def cancel(self, num=None, requests=None, status=None):
729802
"""
@@ -814,6 +887,13 @@ cdef class File:
814887
``numpy.ndarray.nbytes``
815888
:type bufsize: int
816889
890+
:Example: A example is available in ``examples/nonblocking/nonblocking_write.py``
891+
892+
::
893+
# Before calling bput APIs, calculate allocate space needed
894+
bufsize = length * NUM_VARS * np.dtype(np.int32).itemsize
895+
f.attach_buff(bbufsize)
896+
817897
"""
818898
cdef MPI_Offset buffsize
819899
cdef int _file_id
@@ -829,6 +909,17 @@ cdef class File:
829909
830910
Detach the write buffer previously attached for buffered non-blocking
831911
write
912+
913+
:Example: A example is available in ``examples/nonblocking/nonblocking_write.py``
914+
915+
::
916+
# Before calling bput APIs, calculate allocate space needed
917+
bufsize = length * NUM_VARS * np.dtype(np.int32).itemsize
918+
f.attach_buff(bbufsize)
919+
920+
# detach the buffer
921+
f.detach_buff()
922+
832923
"""
833924
cdef int _file_id = self._ncid
834925
with nogil:
@@ -912,6 +1003,21 @@ cdef class File:
9121003
9131004
:Operational mode: This method is a collective subroutine and must be
9141005
called in define mode
1006+
1007+
:Example: A example is available in ``examples/fill_mode.py``
1008+
1009+
::
1010+
# set the fill mode to NC_FILL for the entire file
1011+
old_fillmode = f.set_fill(pnetcdf.NC_FILL)
1012+
if verbose:
1013+
if old_fillmode == pnetcdf.NC_FILL:
1014+
print("The old fill mode is NC_FILL\n")
1015+
else:
1016+
print("The old fill mode is NC_NOFILL\n")
1017+
1018+
# set the fill mode to back to NC_NOFILL for the entire file
1019+
f.set_fill(pnetcdf.NC_NOFILL)
1020+
9151021
"""
9161022
cdef int _file_id, _fillmode, _old_fillmode
9171023
_file_id = self._ncid

0 commit comments

Comments
 (0)