Skip to content

Commit 4f1d279

Browse files
authored
Merge pull request #38 from wkliao/nc4_vs_pnetcdf
easier to tell programming differences between NetCDF4 and PnetCDF
2 parents 8a5d8aa + 9888707 commit 4f1d279

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

docs/nc4_vs_pnetcdf.md

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
## Programming difference between NetCDF4-python and PnetCDF-python
2-
* Table below shows examples of source code.
3-
+ Both example codes write to a variable named `WIND` in the collective mode.
4-
+ The differences are marked in colors, green for NetCDF and blue for PnetCDF.
1+
# Difference between NetCDF4-python and PnetCDF-python
2+
3+
---
4+
## Programming differences
5+
* Table below shows two python example codes.
6+
+ Both example codes create a new file, define dimensions, define a variable
7+
named `WIND` of type `NC_DOUBLE` and then write to it in the collective I/O
8+
mode.
9+
+ The differences are marked in colors, ${\textsf{\color{green}green}}$ for
10+
NetCDF4 and ${\textsf{\color{blue}blue}}$ for PnetCDF.
511

612
| NetCDF4 | PnetCDF |
713
|:-------|:--------|
8-
| # create a new file | |
9-
| ${\textsf{\color{green}f = netCDF4.Dataset}}$(filename="testfile.nc", mode="w", comm=comm, parallel=True) | ${\textsf{\color{blue}f = pnetcdf.File}}$(filename="testfile.nc", mode='w', comm=comm) |
10-
| # add a global attributes | |
11-
| ${\textsf{\color{green}f.history = }}$"Wed Mar 27 14:35:25 CDT 2024" | ${\textsf{\color{blue}f.history = }}$"Wed Mar 27 14:35:25 CDT 2024" |
12-
| # define dimensions | |
13-
| ${\textsf{\color{green}lat\\_dim = f.createDimension}}$("lat", 360) | ${\textsf{\color{blue}lat\\_dim = f.createDimension}}$("lat", 360) |
14-
| ${\textsf{\color{green}lon\\_dim = f.createDimension}}$("lon", 720) | ${\textsf{\color{blue}lon\\_dim = f.createDimension}}$("lon", 720) |
15-
| ${\textsf{\color{green}time\\_dim = f.createDimension}}$("time", None) | ${\textsf{\color{blue}time\\_dim = f.createDimension}}$("time", -1) |
16-
| # define a 3D variable of float type | |
17-
| ${\textsf{\color{green}var = f.createVariable}}$(varname="WIND", datatype="f8", dimensions = ("time", "lat", "lon")) | ${\textsf{\color{blue}var = f.createVariable}}$(varname="WIND", nc\_type=pnetcdf.NC\_FLOAT, dimensions = ("time", "lat", "lon")) |
18-
| # add attributes to the variable | |
19-
| ${\textsf{\color{green}var.long\\_name}}$="atmospheric wind velocity magnitude" |${\textsf{\color{blue}var.long\\_name}}$="atmospheric wind velocity magnitude" |
20-
| ${\textsf{\color{green}var.setncattr}}$("int_att", np.int32(1))|${\textsf{\color{blue}var.put\\_att}}$("int_att", np.int32(1)) |
21-
| # exit define mode | |
22-
| # No code needed. netCDF4-python automatically switches data and define mode | ${\textsf{\color{blue}f.enddef()}}$ | |
23-
| # collectively write to variable WIND | |
24-
| buff = np.zeros(shape = (5, 10), dtype = "f8") | |
25-
| ${\textsf{\color{green}var[0, 5:10, 0:10]}}$ = buff | ${\textsf{\color{blue}var[0, 5:10, 0:10]}}$ = buff |
26-
| | # alternatively <br> ${\textsf{\color{blue}var.put\\_var\\_all}}$(buff, start = [0, 5, 0], count = [1, 5, 10]) |
27-
| # close file | |
28-
| ${\textsf{\color{green}f.close()}}$ | ${\textsf{\color{blue}f.close()}}$ |
14+
| # import python module<br>import ${\textsf{\color{green}netCDF4}}$ | # import python module<br>import ${\textsf{\color{blue}pnetcdf}}$ |
15+
| ... ||
16+
| # create a new file<br>${\textsf{\color{green}f = netCDF4.Dataset}}$(filename="testfile.nc", mode="w", comm=comm, ${\textsf{\color{green}parallel=True}}$) | # create a new file<br>${\textsf{\color{blue}f = pnetcdf.File}}$(filename="testfile.nc", mode='w', comm=comm) |
17+
| # add a global attributes<br>f.history = "Wed Mar 27 14:35:25 CDT 2024" | ditto NetCDF4 |
18+
| # define dimensions<br>lat_dim = f.createDimension("lat", 360)<br>lon_dim = f.createDimension("lon", 720)<br>time_dim = f.createDimension("time", None) | ditto NetCDF4 |
19+
| # define a 3D variable of NC_DOUBLE type<br>var = f.createVariable(varname="WIND", datatype="f8", dimensions = ("time", "lat", "lon")) | ditto NetCDF4 |
20+
| # add attributes to the variable<br>var.long_name="atmospheric wind velocity magnitude"<br>var.units = "m/s" | ditto NetCDF4 |
21+
| ... ||
22+
| ${\textsf{\color{green}\\# NetCDF4-python requires no explicit define/data mode switching}}$ | ${\textsf{\color{blue}\\# exit define mode and enter data mode}}$<br>${\textsf{\color{blue}f.enddef()}}$ | |
23+
| # allocate and initialize the write buffer<br>buff = np.zeros(shape = (5, 10), dtype = "f8") | ditto NetCDF4 |
24+
| ... ||
25+
| ${\textsf{\color{green}\\# switch to collective I/O mode, default is independent in NetCDF4}}$<br>${\textsf{\color{green}var.set\\_collective(True)}}$ | ${\textsf{\color{blue}\\# collective I/O mode is default in PnetCDF}}$ |
26+
| # write to variable WIND in the file<br>var[0, 5:10, 0:10] = buff | ditto NetCDF4 |
27+
| ... ||
28+
| # close file<br>f.close() | ditto NetCDF4 |
29+

0 commit comments

Comments
 (0)