3
3
Basics
4
4
=========
5
5
6
- .. warning ::
7
-
8
- Under construction.
9
-
10
6
Running Python scripts with MPI
11
7
-------------------------------
12
8
13
- Python programs with PnetCDF-Python can be run with the command :program: ` mpiexec `. In
14
- practice, running Python programs looks like:
9
+ Python programs with PnetCDF-Python can be run with the command
10
+ :program: ` mpiexec `. In practice, running Python programs looks like:
15
11
16
12
$ mpiexec -n 4 Python script.py
17
13
@@ -20,14 +16,16 @@ Running Python scripts with MPI
20
16
Creating/Opening/Closing a netCDF file
21
17
--------------------------------------
22
18
23
- To create a netCDF file from Python, you simply call the ``File `` constructor. This is also
24
- the method used to open an existing netCDF file. If the file is open for write access
25
- (mode='w', 'r+' or 'a'), you may write any type of data including new dimensions, variables
26
- and attributes. Currently, netCDF files can be created in classic formats, specifically the
27
- formats of CDF-1, 2, and 5. When creating a new file, the format may be specified using the
28
- format keyword in the ``File `` constructor. The default format is CDF-1. To see how a given
29
- file is formatted, you can examine the ``file_format `` attribute. Closing the netCDF file is
30
- accomplished via the :meth: `File.close ` method of the ``File `` instance.
19
+ To create a netCDF file from Python, you simply call the ``File `` constructor.
20
+ This is also the method used to open an existing netCDF file. If the file is
21
+ open for write access (mode='w', 'r+' or 'a'), you may write any type of data
22
+ including new dimensions, variables and attributes. Currently, netCDF files
23
+ can be created in classic formats, specifically the formats of CDF-1, 2, and
24
+ 5. When creating a new file, the format may be specified using the format
25
+ keyword in the ``File `` constructor. The default format is CDF-1. To see how a
26
+ given file is formatted, you can examine the ``file_format `` attribute.
27
+ Closing the netCDF file is accomplished via the :meth: `File.close ` method of
28
+ the ``File `` instance.
31
29
32
30
Here's an example:
33
31
@@ -40,7 +38,7 @@ Creating/Opening/Closing a netCDF file
40
38
f.close()
41
39
42
40
Equivalent example codes in ``netCDF4-python ``:
43
-
41
+
44
42
.. code-block :: Python
45
43
46
44
from mpi4py import MPI
@@ -51,47 +49,38 @@ Creating/Opening/Closing a netCDF file
51
49
52
50
For the full example program, see ``examples/craete_open.py ``.
53
51
54
- Dimensions
52
+ Dimensions
55
53
-------------
56
54
57
- NetCDF specifies the sizes of variables based on dimensions. Therefore, before creating any variables,
58
- the dimensions they depend on must be established. To create a dimension, the :meth: `File.def_dim ` method is called
59
- on a File instance under define mode. The dimension's name is set using a Python string, while the size
60
- is defined using an integer value. To create an unlimited dimension (a dimension that can be expanded),
61
- the size can be omitted or assigned as -1. A "Dimension" object will be returned as a handler for this
62
- dimension.
55
+ NetCDF variables are multi-dimensional arrays. Before creating any variables,
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.
58
+ The dimension's name is set using a Python string, while the size is defined
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.
63
62
64
- Here's an example:
63
+ Here's an example (same if using netcdf4-python) :
65
64
66
65
.. code-block :: Python
67
66
68
- from pnetcdf import File
69
67
LAT_NAME = " lat"
70
68
LAT_LEN = 50
71
69
TIME_NAME = " time"
72
70
lat_dim = f.def_dim(LAT_NAME , LAT_LEN )
73
71
time_dim = f.def_dim(TIME_NAME , - 1 )
74
72
75
- Equivalent example codes in ``netCDF4-python ``:
76
-
77
- .. code-block :: Python
78
-
79
- LAT_NAME = " lat"
80
- LAT_LEN = 50
81
- TIME_NAME = " time"
82
- lat_dim = f.createDimension(LAT_NAME , LAT_LEN )
83
- time_dim = f.createDimension(TIME_NAME , None )
84
-
85
-
86
- All of the Dimension instances are stored in a dictionary as an Python attribute of File.
73
+ All of the Dimension instances are stored in a dictionary as an Python
74
+ attribute of File.
87
75
88
76
.. code-block :: Python
89
77
90
78
>> > print (f.dimensions)
91
79
{' lat' : < class ' pnetcdf._Dimension.Dimension' > : name = ' lat' , size = 50 , ' time' : < class ' pnetcdf._Dimension.Dimension' > (unlimited): name = ' time' , size = 0 }
92
80
93
- To retrieve the previous defined dimension instance from the file, you can directly index the dictionary using variable name as the key.
94
- The dimension information can be retrieved using following functions.
81
+ To retrieve the previous defined dimension instance from the file, you can
82
+ directly index the dictionary using variable name as the key. The dimension
83
+ information can be retrieved using following functions.
95
84
96
85
.. code-block :: Python
97
86
@@ -104,26 +93,25 @@ Dimensions
104
93
Variables
105
94
------------
106
95
107
- NetCDF variables are similar to multidimensional array objects in Python provided by the numpy module. To define a netCDF
108
- variable, you can utilize the :meth: `File.def_var ` method within a File instance under define mode. The mandatory arguments for
109
- this methods include the variable name (a string in Python) and dimensions (either a tuple of dimension names or dimension
110
- instances). In addition, the user need to specify the datatype of the variable using module-level NC constants (e.g. pnetcdf.NC_INT).
111
- The supported datatypes given each file format can be found :ref: `here<Datatype> `.
112
-
113
- Here's an example:
114
-
115
- .. code-block :: Python
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
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> `.
116
104
117
- var = f.def_var( varname = " var " , datatype = pnetcdf. NC_INT , dimensions = ( " time " , " lat " ))
105
+ Here's an example (same if using netcdf4-python):
118
106
119
- Equivalent example codes in ``netCDF4-python ``:
120
-
121
107
.. code-block :: Python
122
108
123
- var = f.createVariable(varname = " time " , datatype = " i4" , dimensions = (" time" , " lat" ))
109
+ var = f.createVariable(varname = " var " , datatype = " i4" , dimensions = (" time" , " lat" ))
124
110
125
- All of the variables in the file are stored in a Python dictionary, in the same way as the dimensions. To retrieve the previous defined
126
- netCDF variable instance from the file, you can directly index the dictionary using variable name as the key.
111
+ All of the variables in the file are stored in a Python dictionary, in the
112
+ same way as the dimensions. To retrieve the previous defined netCDF variable
113
+ instance from the file, you can directly index the dictionary using variable
114
+ name as the key.
127
115
128
116
.. code-block :: Python
129
117
@@ -134,56 +122,60 @@ Variables
134
122
unlimited dimensions: time
135
123
current shape = (0 , 50 )
136
124
filling off}
137
-
138
-
139
- Up to this point a netCDF variable is properly defined. To write data to or read from this variable, see later sections for more details.
140
125
141
- Attributes
126
+ Up to this point a netCDF variable is properly defined. To write data to or
127
+ read from this variable, see later sections for more details.
128
+
129
+ Attributes
142
130
------------
143
131
144
- In a netCDF file, there are two types of attributes: global attributes and variable attributes.
145
- Global attributes are usually related to the netCDF file as a whole and may be used for purposes
146
- such as providing a title or processing history for a netCDF file.Variable attributes are used to specify
147
- properties as units, special values, maximum and minimum valid values, scaling factors, and offsets.
132
+ In a netCDF file, there are two types of attributes: global attributes and
133
+ variable attributes. Global attributes are usually related to the netCDF file
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
136
+ specify properties related to the variable, such as units, special values,
137
+ maximum and minimum valid values, and annotation.
148
138
149
- Attributes for a netCDF file are defined when the file is first created, while the netCDF dataset is in
150
- define mode. Additional attributes may be added later by reentering define mode. Attributes can take
151
- the form of strings, numbers, or sequences. Returning to our example,
139
+ Attributes for a netCDF file are defined when the file is first created, while
140
+ the netCDF dataset is in define mode. Additional attributes may be added later
141
+ by reentering define mode. Attributes can take the form of strings, and
142
+ numerical values. Returning to our example,
152
143
153
144
.. code-block :: Python
154
145
155
146
# set global attributes
156
- f.floatatt = math.pi # Option1: Python attribute assignment
147
+ f.floatatt = math.pi # Option1: Python attribute assignment
157
148
f.put_att(" intatt" , np.int32(1 )) # Option2: method put_att()
158
149
f.seqatt = np.int32(np.arange(10 ))
159
150
160
151
# set variable attributes
161
- var = f.variables[' var' ]
162
- var.floatatt = math.pi
163
- var.put_att(" int_att" , np.int32(1 ))
152
+ var = f.variables[' var' ]
153
+ var.floatatt = math.pi
154
+ var.put_att(" int_att" , np.int32(1 ))
164
155
var.seqatt = np.int32(np.arange(10 ))
165
156
166
157
Equivalent example codes in ``netCDF4-python ``:
167
-
158
+
168
159
.. code-block :: Python
169
160
170
161
# set root group attributes
171
- f.floatatt = math.pi # Option1: Python attribute assignment
172
- f.setncattr(" intatt" , np.int32(1 )) # Option2: method put_att ()
162
+ f.floatatt = math.pi # Option1: Python attribute assignment
163
+ f.setncattr(" intatt" , np.int32(1 )) # Option2: method setncattr ()
173
164
f.seqatt = np.int32(np.arange(10 ))
174
165
175
166
# set variable attributes
176
- var = f.variables[' var' ]
177
- var.floatatt = math.pi
178
- var.setncattr(" int_att" , np.int32(1 ))
167
+ var = f.variables[' var' ]
168
+ var.floatatt = math.pi
169
+ var.setncattr(" int_att" , np.int32(1 ))
179
170
var.seqatt = np.int32(np.arange(10 ))
180
171
181
- The :meth: `File.ncattrs ` method of a File or Variable instance can be used to retrieve the names of all
182
- the netCDF attributes. And the __dict__ attribute of a File or Variable instance provides all the netCDF
183
- attribute name/value pairs in a python dictionary:
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:
184
176
185
177
.. code-block :: Python
186
-
178
+
187
179
>> > print (var.ncattrs())
188
180
[' floatatt' , ' intatt' , ' seqatt' , ' int_att' ]
189
181
>> > print (var.__dict__ )
@@ -195,10 +187,10 @@ Attributes
195
187
Writing to variable
196
188
--------------------
197
189
198
- Now that you have a netCDF Variable instance, how do you put data into it? Firstly make sure the file is in data mode.
199
- Then for writing, there are currently two options:
190
+ Once a netCDF variable instance is created, writing the variable must be done
191
+ while the file is in data mode. Then for writing, there are two options:
200
192
201
- Option1 Indexer (or slicing) syntax
193
+ Option1 Indexer (or slicing) syntax
202
194
You can just treat the variable like an numpy array and assign data
203
195
to a slice. Slices are specified as a `start:stop:step ` triplet.
204
196
@@ -207,34 +199,44 @@ Option1 Indexer (or slicing) syntax
207
199
buff = np.zeros(shape = (10 , 50 ), dtype = " i4" )
208
200
var[:] = buff # put values to the variable
209
201
210
- The indexer syntax is the same as in ``netcdf4-python `` library for writing to netCDF variable.
202
+ The indexer syntax is the same as in ``netcdf4-python `` library for writing to
203
+ netCDF variable.
211
204
212
- Option2 Method calls of put /get_var()
213
- Alternatively you can also leverage Variable.put/get_var() method of a Variable instance
214
- to perform i/o according to specfic access pattern needs.
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.
215
208
216
- Here is an example to write an array to the netCDF variable. The part of the netCDF variable to write is specified by giving a corner (`start `)
217
- and a vector of edge lengths (`count `) that refer to an array section of the netCDF variable.
209
+ Here is the example below to write an array to the netCDF variable. The part
210
+ of the netCDF variable to write is specified by giving a corner (`start `) and
211
+ a vector of edge lengths (`count `) that refer to an array section of the
212
+ netCDF variable.
218
213
219
214
.. code-block :: Python
220
215
221
216
buff = np.zeros(shape = (10 , 50 ), dtype = " i4" )
222
- var.put_var_all(buff, start = [10 , 0 ], count = [10 , 50 ]) # Equivalent to var[10:20, 0:50] = buff
217
+ var.put_var_all(buff, start = [10 , 0 ], count = [10 , 50 ])
218
+ # The above line is equivalent to var[10:20, 0:50] = buff
223
219
224
220
225
221
Reading from variable
226
222
----------------------
227
223
228
- Symmetrically, users can use two options with different syntaxes to retreive array values from the variable.
229
- The indexer syntax is the same as in ``netcdf4-python `` library for reading from netCDF variable.
224
+ Symmetrically, users can use two options with different syntaxes to retrieve
225
+ array values from the variable. The indexer syntax is the same as in
226
+ ``netcdf4-python `` library for reading from netCDF variable.
230
227
231
228
.. code-block :: Python
232
229
233
- var = f.variables[' var' ]
234
- print (var[:10 , :10 ]) # Option1 Indexer: read the topleft 10*10 corner from variable var
235
- print (var.get_var_all(start = [10 , 0 ], count = [10 , 50 ])) # Option2 Method Call: equivalent to var[10:20, 0:50]
236
-
237
- Similarly, :meth: `Variable.get_var ` takes the same set of optional arguments and behave differently depending on the pattern of provided
238
- optional arguments.
239
-
240
- To learn more about reading and writing, see the :ref: `here<Parallel Read and Write> ` page.
230
+ var = f.variables[' var' ]
231
+ # Option1 Indexer: read the topleft 10*10 corner from variable var
232
+ buf = var[:10 , :10 ]
233
+
234
+ # Option2 Method Call: equivalent to var[10:20, 0:50]
235
+ buf = var.get_var_all(start = [10 , 0 ], count = [10 , 50 ])
236
+
237
+ Similarly, :meth: `Variable.get_var ` takes the same set of optional arguments
238
+ and behave differently depending on the pattern of provided optional
239
+ arguments.
240
+
241
+ To learn more about reading and writing, see :ref: `here<Parallel Read and Write> `.
242
+
0 commit comments