13
13
Example commands for MPI run and outputs from running ncmpidump on the
14
14
netCDF file produced by this example program:
15
15
16
- % mpiexec -n 4 python3 global_attribute.py ./tmp/test2 .nc
17
- % ncmpidump ./tmp/test2 .nc
16
+ % mpiexec -n 4 python3 global_attribute.py testfile .nc
17
+ % ncmpidump testfile .nc
18
18
netcdf testfile {
19
19
// file format: CDF-1
20
20
32
32
import pnetcdf
33
33
34
34
35
- def pnetcdf_io (filename , file_format ):
36
- digit = np .int16 ([0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ])
35
+ def write_attr (filename , file_format ):
37
36
38
- # Run pnetcdf i/o
39
-
40
- # Create the file
37
+ # Create a new file
41
38
f = pnetcdf .File (filename = filename ,
42
39
mode = 'w' ,
43
40
format = file_format ,
@@ -53,49 +50,75 @@ def pnetcdf_io(filename, file_format):
53
50
# Make sure the time string is consistent among all processes
54
51
str_att = comm .bcast (str_att , root = 0 )
55
52
56
- # write a global attribute
53
+ # write a global attribute of string data type
57
54
f .history = str_att
58
55
56
+ # Equivalently, this can also be done by using a function call
57
+ f .put_att ('history' ,str_att )
58
+
59
59
if rank == 0 and verbose :
60
60
print (f'writing global attribute "history" of text { str_att } ' )
61
61
62
- # Equivalently, below uses function call
63
- f .put_att ('history' ,str_att )
62
+ # add another global attribute named "digits": an array of type int16
63
+ digits = np .int16 ([0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ])
64
+ f .digits = digits
65
+
66
+ # Equivalently, this can also be done by using a function call
67
+ f .put_att ('digits' , digits )
64
68
65
- # add another global attribute named "digits": an array of short type
66
- f .digits = digit
67
69
if rank == 0 and verbose :
68
70
print ("writing global attribute \" digits\" of 10 short integers" )
69
71
70
- # Equivalently, below uses function call
71
- f .put_att ('digits' , digit )
72
-
73
72
# Close the file
74
73
f .close ()
75
74
76
- # Read the file
77
- f = pnetcdf .File (filename = filename , mode = 'r' )
78
75
79
- # get the number of attributes
80
- ngatts = len (f .ncattrs ())
76
+ def read_attr (filename ):
77
+
78
+ # Open the file for read
79
+ f = pnetcdf .File (filename = filename , mode = 'r' )
80
+
81
+ # obtain the name list of all global attributes
82
+ gatt_names = f .ncattrs ()
83
+
84
+ # the number of global attributes
85
+ ngatts = len (gatt_names )
81
86
if ngatts != 2 :
82
87
print (f"Error at line { sys ._getframe ().f_lineno } in { __file__ } : expected number of global attributes is 2, but got { ngatts } " )
88
+ elif verbose and rank == 0 :
89
+ print ("Number of global attributes = " , ngatts )
83
90
84
91
# Find the name of the first global attribute
85
- att_name = f .ncattrs ()[0 ]
86
- if att_name != "history" :
87
- print (f"Error: Expected attribute name 'history', but got { att_name } " )
92
+ if gatt_names [0 ] != "history" :
93
+ print (f"Error: Expected attribute name 'history', but got { gatt_names [0 ]} " )
88
94
89
95
# Read attribute value
90
- str_att = f .get_att (att_name )
96
+ str_att = f .history
97
+
98
+ if verbose and rank == 0 :
99
+ print ("Global attribute name=" , gatt_names [0 ]," value=" ,str_att )
100
+
101
+ # Equivalently, this can also be done by using a function call
102
+ str_att = f .get_att (gatt_names [0 ])
103
+
104
+ if verbose and rank == 0 :
105
+ print ("Global attribute name=" , gatt_names [0 ]," value=" ,str_att )
91
106
92
107
# Find the name of the second global attribute
93
- att_name = f .ncattrs ()[1 ]
94
- if att_name != "digits" :
95
- print (f"Error: Expected attribute name 'digits', but got { att_name } " )
108
+ if gatt_names [1 ] != "digits" :
109
+ print (f"Error: Expected attribute name 'digits', but got { gatt_names [1 ]} " )
96
110
97
111
# Read attribute value
98
- short_att = f .get_att (att_name )
112
+ short_att = f .digits
113
+
114
+ if verbose and rank == 0 :
115
+ print ("Global attribute name=" , gatt_names [1 ]," value=" ,short_att )
116
+
117
+ # Equivalently, this can also be done by using a function call
118
+ short_att = f .get_att (gatt_names [1 ])
119
+
120
+ if verbose and rank == 0 :
121
+ print ("Global attribute name=" , gatt_names [1 ]," value=" ,short_att )
99
122
100
123
# close the file
101
124
f .close ()
@@ -145,7 +168,8 @@ def parse_help():
145
168
print ("{}: example of put/get global attributes" .format (os .path .basename (__file__ )))
146
169
147
170
try :
148
- pnetcdf_io (filename , file_format )
171
+ write_attr (filename , file_format )
172
+ read_attr (filename )
149
173
except BaseException as err :
150
174
print ("Error: type:" , type (err ), str (err ))
151
175
raise
0 commit comments