6
6
Running Python scripts with MPI
7
7
-------------------------------
8
8
9
- Python programs with PnetCDF-Python can be run with the command
10
- :program: `mpiexec `. In practice, running Python programs looks like:
9
+ Python programs using PnetCDF-Python can be run with the command
10
+ :program: `mpiexec `. In practice, running a Python program looks like:
11
11
12
- $ mpiexec -n 4 Python script.py
12
+ $ mpiexec -n 4 python script.py
13
13
14
- to run the program with 4 processors .
14
+ to run the program with 4 MPI processes .
15
15
16
16
Creating/Opening/Closing a netCDF file
17
17
--------------------------------------
@@ -27,24 +27,24 @@ Creating/Opening/Closing a netCDF file
27
27
Closing the netCDF file is accomplished via the :meth: `File.close ` method of
28
28
the ``File `` instance.
29
29
30
- Here's an example:
30
+ Here is an example of creating a new file :
31
31
32
32
.. code-block :: Python
33
33
34
- from pnetcdf import File
35
34
from mpi4py import MPI
36
- comm = MPI .COMM_WORLD
37
- f = File(filename = " testfile.nc" , mode = ' w' , comm = comm, info = None )
35
+ import pnetcdf
36
+
37
+ f = pnetcdf.File(filename = " testfile.nc" , mode = ' w' , comm = MPI .COMM_WORLD , info = None )
38
38
f.close()
39
39
40
- Equivalent example codes in ``netCDF4-python ``:
40
+ Equivalent example codes when using ``netCDF4-python ``:
41
41
42
42
.. code-block :: Python
43
43
44
44
from mpi4py import MPI
45
- from netCDF4 import Dataset
46
- comm = MPI . COMM_WORLD
47
- f = Dataset(filename = " testfile.nc" , mode = " w" , comm = comm , parallel = True )
45
+ import netCDF4
46
+
47
+ f = netCDF4. Dataset(filename = " testfile.nc" , mode = " w" , comm = MPI . COMM_WORLD , parallel = True )
48
48
f.close()
49
49
50
50
For the full example program, see ``examples/craete_open.py ``.
@@ -54,11 +54,11 @@ Dimensions
54
54
55
55
NetCDF variables are multi-dimensional arrays. Before creating any variables,
56
56
the dimensions they depend on must be established. To create a dimension, the
57
- :meth: `File.def_dim ` method is called on a File instance under define mode.
57
+ :meth: `File.def_dim ` method is called on a `` File `` instance under define mode.
58
58
The dimension's name is set using a Python string, while the size is defined
59
59
using an integer value. To create an unlimited dimension (a dimension that can
60
- be expanded), the size can be omitted or assigned as -1. A "Dimension" object
61
- will be returned as a handler for this dimension.
60
+ be expanded), the parameter size can be omitted or assigned as -1. A
61
+ `` Dimension `` instance will be returned as a handler for this dimension.
62
62
63
63
Here's an example (same if using netcdf4-python):
64
64
@@ -70,8 +70,8 @@ Dimensions
70
70
lat_dim = f.def_dim(LAT_NAME , LAT_LEN )
71
71
time_dim = f.def_dim(TIME_NAME , - 1 )
72
72
73
- All of the Dimension instances are stored in a dictionary as an Python
74
- attribute of File.
73
+ All of the `` Dimension `` instances are stored in a Python dictionary as an
74
+ attribute of `` File `` .
75
75
76
76
.. code-block :: Python
77
77
@@ -94,13 +94,13 @@ Variables
94
94
------------
95
95
96
96
NetCDF variables are similar to multidimensional array objects in Python
97
- provided by the numpy module. To define a netCDF variable, you can utilize the
98
- :meth: `File.def_var ` method within a File instance under define mode. The
99
- mandatory arguments for this methods include the variable name (a string in
100
- Python) and dimensions (either a tuple of dimension names or dimension
97
+ provided by the `` numpy `` module. To define a netCDF variable, you can utilize
98
+ the :meth: `File.def_var ` method within a `` File `` instance under define mode.
99
+ The mandatory arguments for this methods include the variable name (a string
100
+ in Python) and dimensions (either a tuple of dimension names or dimension
101
101
instances). In addition, the user need to specify the datatype of the variable
102
- using module-level NC constants (e.g. pnetcdf.NC_INT). The supported
103
- data types given each file format can be found :ref: `here<Datatype> `.
102
+ using module-level constants (e.g. `` pnetcdf.NC_INT `` ). The supported data
103
+ types given each file format can be found :ref: `here<Datatype> `.
104
104
105
105
Here's an example (same if using netcdf4-python):
106
106
@@ -132,7 +132,7 @@ Attributes
132
132
In a netCDF file, there are two types of attributes: global attributes and
133
133
variable attributes. Global attributes are usually related to the netCDF file
134
134
as a whole and may be used for purposes such as providing a title or
135
- processing history for a netCDF file. Variable's attributes are used to
135
+ processing history for a netCDF file. `` Variable `` 's attributes are used to
136
136
specify properties related to the variable, such as units, special values,
137
137
maximum and minimum valid values, and annotation.
138
138
@@ -144,11 +144,11 @@ Attributes
144
144
.. code-block :: Python
145
145
146
146
# set global attributes
147
- f.floatatt = math.pi # Option1 : Python attribute assignment
148
- f.put_att(" intatt" , np.int32(1 )) # Option2 : method put_att()
147
+ f.floatatt = math.pi # Option 1 : Python attribute assignment
148
+ f.put_att(" intatt" , np.int32(1 )) # Option 2 : method put_att()
149
149
f.seqatt = np.int32(np.arange(10 ))
150
150
151
- # set variable attributes
151
+ # write variable attributes
152
152
var = f.variables[' var' ]
153
153
var.floatatt = math.pi
154
154
var.put_att(" int_att" , np.int32(1 ))
@@ -159,8 +159,8 @@ Attributes
159
159
.. code-block :: Python
160
160
161
161
# set root group attributes
162
- f.floatatt = math.pi # Option1 : Python attribute assignment
163
- f.setncattr(" intatt" , np.int32(1 )) # Option2 : method setncattr()
162
+ f.floatatt = math.pi # Option 1 : Python attribute assignment
163
+ f.setncattr(" intatt" , np.int32(1 )) # Option 2 : method setncattr()
164
164
f.seqatt = np.int32(np.arange(10 ))
165
165
166
166
# set variable attributes
@@ -169,10 +169,10 @@ Attributes
169
169
var.setncattr(" int_att" , np.int32(1 ))
170
170
var.seqatt = np.int32(np.arange(10 ))
171
171
172
- The :meth: `File.ncattrs ` method of a File or Variable instance can be used to
173
- retrieve the names of all the netCDF attributes. And the __dict__ attribute of
174
- a File or Variable instance provides all the netCDF attribute name/value pairs
175
- in a python dictionary:
172
+ The :meth: `File.ncattrs ` method of a `` File `` or `` Variable `` instance can be
173
+ used to retrieve the names of all the netCDF attributes. And the __dict__
174
+ attribute of a `` File `` or `` Variable `` instance provides all the netCDF
175
+ attribute name/value pairs in a python dictionary:
176
176
177
177
.. code-block :: Python
178
178
@@ -184,14 +184,14 @@ Attributes
184
184
185
185
For the full example program, see ``examples/global_attributes.py ``.
186
186
187
- Writing to variable
187
+ Writing to a variable
188
188
--------------------
189
189
190
190
Once a netCDF variable instance is created, writing the variable must be done
191
191
while the file is in data mode. Then for writing, there are two options:
192
192
193
- Option1 Indexer (or slicing) syntax
194
- You can just treat the variable like an numpy array and assign data
193
+ Option 1 Indexer (or slicing) syntax
194
+ You can just treat the variable like an `` numpy `` array and assign data
195
195
to a slice. Slices are specified as a `start:stop:step ` triplet.
196
196
197
197
.. code-block :: Python
@@ -202,9 +202,9 @@ Option1 Indexer (or slicing) syntax
202
202
The indexer syntax is the same as in ``netcdf4-python `` library for writing to
203
203
netCDF variable.
204
204
205
- Option2 Method calls of put_var()/get_var()
206
- Alternatively you can also leverage Variable.put/get_var() method of a
207
- Variable instance to perform I/O according to specific access pattern needs.
205
+ Option 2 Method calls of put_var()/get_var()
206
+ Alternatively you can also leverage `` Variable.put/get_var() `` method of a
207
+ `` Variable `` instance to perform I/O according to specific access pattern needs.
208
208
209
209
Here is the example below to write an array to the netCDF variable. The part
210
210
of the netCDF variable to write is specified by giving a corner (`start `) and
@@ -218,7 +218,7 @@ Option2 Method calls of put_var()/get_var()
218
218
# The above line is equivalent to var[10:20, 0:50] = buff
219
219
220
220
221
- Reading from variable
221
+ Reading from a variable
222
222
----------------------
223
223
224
224
Symmetrically, users can use two options with different syntaxes to retrieve
@@ -228,10 +228,10 @@ Reading from variable
228
228
.. code-block :: Python
229
229
230
230
var = f.variables[' var' ]
231
- # Option1 Indexer: read the topleft 10*10 corner from variable var
231
+ # Option 1 Indexer: read the top-left 10*10 corner from variable var
232
232
buf = var[:10 , :10 ]
233
233
234
- # Option2 Method Call: equivalent to var[10:20, 0:50]
234
+ # Option 2 Method Call: equivalent to var[10:20, 0:50]
235
235
buf = var.get_var_all(start = [10 , 0 ], count = [10 , 50 ])
236
236
237
237
Similarly, :meth: `Variable.get_var ` takes the same set of optional arguments
0 commit comments