1313Example commands for MPI run and outputs from running ncmpidump on the
1414netCDF file produced by this example program:
1515
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
1818 netcdf testfile {
1919 // file format: CDF-1
2020
3232import pnetcdf
3333
3434
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 ):
3736
38- # Run pnetcdf i/o
39-
40- # Create the file
37+ # Create a new file
4138 f = pnetcdf .File (filename = filename ,
4239 mode = 'w' ,
4340 format = file_format ,
@@ -53,49 +50,75 @@ def pnetcdf_io(filename, file_format):
5350 # Make sure the time string is consistent among all processes
5451 str_att = comm .bcast (str_att , root = 0 )
5552
56- # write a global attribute
53+ # write a global attribute of string data type
5754 f .history = str_att
5855
56+ # Equivalently, this can also be done by using a function call
57+ f .put_att ('history' ,str_att )
58+
5959 if rank == 0 and verbose :
6060 print (f'writing global attribute "history" of text { str_att } ' )
6161
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 )
6468
65- # add another global attribute named "digits": an array of short type
66- f .digits = digit
6769 if rank == 0 and verbose :
6870 print ("writing global attribute \" digits\" of 10 short integers" )
6971
70- # Equivalently, below uses function call
71- f .put_att ('digits' , digit )
72-
7372 # Close the file
7473 f .close ()
7574
76- # Read the file
77- f = pnetcdf .File (filename = filename , mode = 'r' )
7875
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 )
8186 if ngatts != 2 :
8287 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 )
8390
8491 # 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 ]} " )
8894
8995 # 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 )
91106
92107 # 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 ]} " )
96110
97111 # 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 )
99122
100123 # close the file
101124 f .close ()
@@ -145,7 +168,8 @@ def parse_help():
145168 print ("{}: example of put/get global attributes" .format (os .path .basename (__file__ )))
146169
147170 try :
148- pnetcdf_io (filename , file_format )
171+ write_attr (filename , file_format )
172+ read_attr (filename )
149173 except BaseException as err :
150174 print ("Error: type:" , type (err ), str (err ))
151175 raise
0 commit comments