-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreate_open.py
85 lines (65 loc) · 2.23 KB
/
create_open.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#
# Copyright (C) 2024, Northwestern University and Argonne National Laboratory
# See COPYRIGHT notice in top-level directory.
#
"""
This example shows how to use `File` class constructor to create a netCDF file
and to open the file for read only.
Example commands for MPI run and outputs from running ncmpidump on the
netCDF file produced by this example program:
% mpiexec -n 4 python3 create_open.py /tmp/test1.nc
% ncmpidump /tmp/test1.nc
netcdf test1 {
// file format: CDF-1
}
"""
import sys, os, argparse
from mpi4py import MPI
import pnetcdf
def pnetcdf_io(filename):
# create a new file using file clobber mode, i.e. flag "-w"
f = pnetcdf.File(filename = filename,
mode = 'w',
comm = comm,
info = None)
# close the file
f.close()
# open the newly created file for read only
f = pnetcdf.File(filename=filename, mode = 'r', comm=comm, info=None)
# close the file
f.close()
def parse_help():
help_flag = "-h" in sys.argv or "--help" in sys.argv
if help_flag and rank == 0:
help_text = (
"Usage: {} [-h] | [-q] [file_name]\n"
" [-h] Print help\n"
" [-q] Quiet mode (reports when fail)\n"
" [filename] (Optional) output netCDF file name\n"
).format(sys.argv[0])
print(help_text)
return help_flag
if __name__ == "__main__":
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
nprocs = comm.Get_size()
if parse_help():
MPI.Finalize()
sys.exit(1)
# get command-line arguments
args = None
parser = argparse.ArgumentParser()
parser.add_argument("dir", nargs="?", type=str, help="(Optional) output netCDF file name",\
default = "testfile.nc")
parser.add_argument("-q", help="Quiet mode (reports when fail)", action="store_true")
args = parser.parse_args()
verbose = False if args.q else True
filename = args.dir
if verbose and rank == 0:
print("{}: example of file create and open".format(os.path.basename(__file__)))
try:
pnetcdf_io(filename)
except BaseException as err:
print("Error: type:", type(err), str(err))
raise
MPI.Finalize()