Mac上如何使用Fortran来读写NetCDF数据文件

阅读建议:电脑端/iPad端阅读,按照文章左边栏三级标题来捋思路,安装过程输出可忽略。实际操作报错了,再按图索骥看细节!


本文简版同步发布于公众号『气象学家』传送门。从页面“Software for Manipulating or Displaying NetCDF Data”能找到几十种语言或者工具软件可以处理NetCDF(Network Common Data Form)的数据,有些软件是可以跨平台的,强烈推荐>>免费软件Panoply<<,对于NetCDF文件的可视化和简单的数据导出非常的优秀,Mac、Linux、Windows都能使用。当然,现在对于不同的处理要求和速度完全可以选择不同的工具方法处理NetCDF,Python/ncl对于NetCDF的读取和创建也已经非常得方便了。但是,目前就使用Fortran去调用NetCDF的Fortran API,处理速度上还是有很大的优势,所以,简单以MacOS为例,就Fortran处理NetCDF相关的库的安装和环境变量设置来做个简单的介绍,并用实例来加深理解,但是并不做深度解读,抛砖引玉而已。后面另开一篇文章就grib数据的处理做介绍(Python、NCL、wgrib2)。

配料:

1
2
3
4
5
6
7
1. NetCDF-C
2. NetCDF-Fortran
3. szip (Installing dependencies for netcdf: szip and hdf5)
4. hdf5 (Installing dependencies for netcdf: szip and hdf5)
5. gcc
6. gfortran
7. Homebrew

安装

系统版本:macOS Mojave version 10.14.5

1
2
3
4
5
6
7
对于macOS完全可以很大程度的依赖Homebrew来进行很多包的安装。
因为有人已经造好了轮子,依赖关系都已经完美处理好了,对于野生气象码农简直是太有好了。
当然,让我强行一波手动安装也是不在话下,对于初学者还是建议手动安装感受一下。
"熟练工"节省时间成本完全可以一键式骚操作!
本安装为了更省事可以说是一站式气象科学依赖库全家桶(安装cdo
/nco)都给安好了!
废话不多说,上干货。

安装Homebrew

homebrew can be downloaded and installed in one step:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

or

If this doesn’t work do

curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install > brew_install_script

ruby brew_install_script

rehash

在此,Homebrew安装结束!

编译工具安装 gcc/g++ 、 gfortran、make、cmake

首先在终端查看诸工具是否已经安装:比如gcc –version,如果没有安装或者不够新,下面是自动安装命令

gcc/g++: brew install gcc, brew install g++

gfortran: brew install gfortran

make: brew install make

cmake: brew install cmake

或者在cmake官网下载二进制安装文件(带GUI界面),双击安装即可!

安装 cdo、netcdf、nco

$brew tap homebrew/science

$brew install cdo

$brew install netcdf

$brew install nco

nco安装时候,可能报错,需要安装[Java SE](https://github.com/frekele/oracle-java/releases). https://github.com/frekele/oracle-java/releases

安装结束!

具体的安装过程:

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
 ✘ ⚙  ~  brew tap homebrew/science
Error: homebrew/science was deprecated. This tap is now empty as all its formulae were migrated.
✘ ⚙  ~  brew install netcdf
==> Installing dependencies for netcdf: szip and hdf5
==> Installing netcdf dependency: szip
==> Downloading https://homebrew.bintray.com/bottles/szip-2.1.1_1.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring szip-2.1.1_1.mojave.bottle.tar.gz
🍺 /usr/local/Cellar/szip/2.1.1_1: 11 files, 109.4KB
==> Installing netcdf dependency: hdf5
==> Downloading https://homebrew.bintray.com/bottles/hdf5-1.10.5_1.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/28/28ee1944f9b17a50bddbfbc1730d06373efaf2f188930fa1624370bb895626df?__gda__=exp=1559187781~hmac=eda4d7981775af
######################################################################## 100.0%
==> Pouring hdf5-1.10.5_1.mojave.bottle.tar.gz
Warning: hdf5 dependency gcc was built with a different C++ standard
library (libstdc++ from clang). This may cause problems at runtime.
🍺 /usr/local/Cellar/hdf5/1.10.5_1: 256 files, 14.6MB
==> Installing netcdf
==> Downloading https://homebrew.bintray.com/bottles/netcdf-4.6.3_1.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/d7/d798cbc16c2e6c312d921abf4cef0d74f094c9f6496114c06219baabaa962974?__gda__=exp=1559187805~hmac=17d415c33a7f32
######################################################################## 100.0%
==> Pouring netcdf-4.6.3_1.mojave.bottle.tar.gz
Warning: netcdf dependency gcc was built with a different C++ standard
library (libstdc++ from clang). This may cause problems at runtime.
🍺 /usr/local/Cellar/netcdf/4.6.3_1: 85 files, 6.2MB
⚙  ~  ncdump -h
ncdump [-c|-h] [-v ...] [[-b|-f] [c|f]] [-l len] [-n name] [-p n[,n]] [-k] [-x] [-s] [-t|-i] [-g ...] [-w] [-Ln] file
[-c] Coordinate variable data and header information
[-h] Header information only, no data
[-v var1[,...]] Data for variable(s) <var1>,... only
[-b [c|f]] Brief annotations for C or Fortran indices in data
[-f [c|f]] Full annotations for C or Fortran indices in data
[-l len] Line length maximum in data section (default 80)
[-n name] Name for netCDF (default derived from file name)
[-p n[,n]] Display floating-point values with less precision
[-k] Output kind of netCDF file
[-s] Output special (virtual) attributes
[-t] Output time data as date-time strings
[-i] Output time data as date-time strings with ISO-8601 'T' separator
[-g grp1[,...]] Data and metadata for group(s) <grp1>,... only
[-w] With client-side caching of variables for DAP URLs
[-x] Output XML (NcML) instead of CDL
[-Xp] Unconditionally suppress output of the properties attribute
[-Ln] Set log level to n (>= 0); ignore if logging not enabled.
file Name of netCDF file (or URL if DAP access enabled)
netcdf library version 4.6.3 of May 8 2019 00:09:03 $
✘ ⚙  ~  brew install cdo
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
exploitdb

Error: No available formula with the name "cdo"
==> Searching for a previously deleted formula (in the last month)...
Warning: homebrew/core is shallow clone. To get complete history run:
git -C "$(brew --repo homebrew/core)" fetch --unshallow

Error: No previously deleted formula found.
==> Searching for similarly named formulae...
This similarly named formula was found:
cdogs-sdl
To install it, run:
brew install cdogs-sdl
==> Searching taps...
==> Searching taps on GitHub...
Error: No formulae found in taps.
✘ ⚙  ~  brew tap moffat/sciencebits
==> Tapping moffat/sciencebits
Cloning into '/usr/local/Homebrew/Library/Taps/moffat/homebrew-sciencebits'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 6 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
Tapped 1 formula (32 files, 28.0KB).
⚙  ~  brew install cdo

==> Installing cdo from moffat/sciencebits
==> Downloading https://code.mpimet.mpg.de/attachments/download/19299/cdo-1.9.6.tar.gz
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/cdo/1.9.6 LIBS=-lhdf5 --with-netcdf=/usr/local/opt/netcdf --with-hdf5=/usr/local/opt/hdf5 --with-szlib=/usr/local/o
==> make install
Warning: moffat/sciencebits/cdo dependency gcc was built with a different C++ standard
library (libstdc++ from clang). This may cause problems at runtime.
🍺 /usr/local/Cellar/cdo/1.9.6: 8 files, 4.8MB, built in 6 minutes 12 seconds
⚙  ~  cdo

No operator given!

usage : cdo [Options] Operator1 [-Operator2 [-OperatorN]]

Options:
-a Generate an absolute time axis
-b <nbits> Set the number of bits for the output precision
(I8/I16/I32/F32/F64 for nc1/nc2/nc4/nc4c/nc5; F32/F64 for grb2/srv/ext/ieg; P1 - P24 for grb1/grb2)
Add L or B to set the byteorder to Little or Big endian
--cmor CMOR conform NetCDF output
-C, --color Colorized output messages
--eccodes Use ecCodes to decode/encode GRIB1 messages
--enableexcept <except>
Set individual floating-point traps (DIVBYZERO, INEXACT, INVALID, OVERFLOW, UNDERFLOW, ALL_EXCEPT)
-f, --format <format>
Format of the output file. (grb1/grb2/nc1/nc2/nc4/nc4c/nc5/srv/ext/ieg)
-g <grid> Set default grid name or file. Available grids:
n<N>, t<RES>, tl<RES>, global_<DXY>, r<NX>x<NY>, g<NX>x<NY>, gme<NI>, lon=<LON>/lat=<LAT>
-h, --help Help information for the operators
--history Do not append to NetCDF "history" global attribute
--netcdf_hdr_pad, --hdr_pad, --header_pad <nbr>
Pad NetCDF output header with nbr bytes
-k <chunktype> NetCDF4 chunk type: auto, grid or lines
-L Lock IO (sequential access)
-M Switch to indicate that the I/O streams have missing values
-m <missval> Set the missing value of non NetCDF files (default: -9e+33)
--no_warnings Inhibit warning messages
-O Overwrite existing output file, if checked
--operators List of all operators
--percentile <method>
Percentile method: nrank, nist, numpy, numpy_lower, numpy_higher, numpy_nearest
--precision <float_digits[,double_digits]>
Precision to use in displaying floating-point data (default: 7,15)
--reduce_dim Reduce NetCDF dimensions
-R, --regular Convert GRIB1 data from global reduced to regular Gaussian grid (cgribex only)
-r Generate a relative time axis
-S Create an extra output stream for the module TIMSTAT. This stream
contains the number of non missing values for each output period.
-s, --silent Silent mode
--sortname Alphanumeric sorting of NetCDF parameter names
-t <codetab> Set GRIB1 default parameter code table name or file (cgribex only)
Predefined tables: echam4 echam5 echam6 mpiom1 ecmwf remo cosmo002 cosmo201 cosmo202 cosmo203 cosmo205 cosmo250
--timestat_date <srcdate>
Target timestamp (temporal statistics): first, middle, midhigh or last source timestep.
-V, --version Print the version number
-v, --verbose Print extra details for some operators
-W Print extra warning messages
-z szip SZIP compression of GRIB1 records
aec AEC compression of GRIB2 records
jpeg JPEG compression of GRIB2 records
zip[_1-9] Deflate compression of NetCDF4 variables

Operators:
Use option --operators for a list of all operators.

CDO version 1.9.6, Copyright (C) 2003-2019 Uwe Schulzweida
This is free software and comes with ABSOLUTELY NO WARRANTY
Report bugs to <http://mpimet.mpg.de/cdo>
✘ ⚙  ~  brew install nco
antlr@2: Java is required to install this formula.
Install AdoptOpenJDK with Homebrew Cask:
brew cask install adoptopenjdk
Error: An unsatisfied requirement failed this build.
✘ ⚙  ~  brew install https://raw.github.com/Homebrew/homebrew-science/master/nco.rb


curl: (22) The requested URL returned error: 404 Not Found
Error: Failure while executing; `/usr/bin/curl -q --show-error --user-agent Homebrew/2.1.4\ \(Macintosh\;\ Intel\ Mac\ OS\ X\ 10.14.5\)\ curl/7.54.0 --fail --progress-bar --location --remote-time --continue-at 0 --output /Users/zhpfu/Library/Caches/Homebrew/Formula/nco.rb https://raw.github.com/Homebrew/homebrew-science/master/nco.rb` exited with 22. Here's the output:

curl: (22) The requested URL returned error: 404 Not Found

✘ ⚙  ~  brew install homebrew/science/nco
Error: homebrew/science was deprecated. This tap is now empty as all its formulae were migrated.
✘ ⚙  ~  brew install nco
antlr@2: Java is required to install this formula.
Install AdoptOpenJDK with Homebrew Cask:
brew cask install adoptopenjdk
Error: An unsatisfied requirement failed this build.
✘ ⚙  ~  brew install nco
==> Installing dependencies for nco: antlr@2, gsl, texinfo and udunits
==> Installing nco dependency: antlr@2
==> Downloading https://homebrew.bintray.com/bottles/antlr@2-2.7.7_1.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/9b/9be9c82eba1b6b803c75114ed55947692693785566c59dca392b8bbae6b8aa19?__gda__=exp=1559191542~hmac=cc1f31c4e51967
######################################################################## 100.0%
==> Pouring antlr@2-2.7.7_1.mojave.bottle.tar.gz
==> Caveats
antlr@2 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have antlr@2 first in your PATH run:
echo 'export PATH="/usr/local/opt/antlr@2/bin:$PATH"' >> ~/.zshrc

For compilers to find antlr@2 you may need to set:
export LDFLAGS="-L/usr/local/opt/antlr@2/lib"
export CPPFLAGS="-I/usr/local/opt/antlr@2/include"

==> Summary
🍺 /usr/local/Cellar/antlr@2/2.7.7_1: 60 files, 1012.4KB
==> Installing nco dependency: gsl
==> Downloading https://homebrew.bintray.com/bottles/gsl-2.5.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/2b/2b76f0bb640a36340efb3bc44a9df6e8b1694cc251637f95eca02c541add53ff?__gda__=exp=1559191556~hmac=54a7b3a02d4374
######################################################################## 100.0%
==> Pouring gsl-2.5.mojave.bottle.tar.gz
🍺 /usr/local/Cellar/gsl/2.5: 271 files, 9.1MB
==> Installing nco dependency: texinfo
==> Downloading https://homebrew.bintray.com/bottles/texinfo-6.6.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/2e/2ea78114fc2f1bedb52a8cc4148c7ab48cbfe15bb2347783fb7f84998247ccc3?__gda__=exp=1559191575~hmac=f389f644770463
######################################################################## 100.0%
==> Pouring texinfo-6.6.mojave.bottle.tar.gz
==> Caveats
texinfo is keg-only, which means it was not symlinked into /usr/local,
because software that uses TeX, such as lilypond and octave, require a newer
version of these files.

If you need to have texinfo first in your PATH run:
echo 'export PATH="/usr/local/opt/texinfo/bin:$PATH"' >> ~/.zshrc

==> Summary
🍺 /usr/local/Cellar/texinfo/6.6: 404 files, 7.3MB
==> Installing nco dependency: udunits
==> Downloading https://homebrew.bintray.com/bottles/udunits-2.2.26.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring udunits-2.2.26.mojave.bottle.tar.gz
🍺 /usr/local/Cellar/udunits/2.2.26: 29 files, 538.7KB
==> Installing nco
==> Downloading https://homebrew.bintray.com/bottles/nco-4.8.0.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/8a/8a031331ccf4203570d7e87acc7da5434820dd872ce7c6690d02094b20cfa67f?__gda__=exp=1559191601~hmac=f3196d0b38d11d
######################################################################## 100.0%
==> Pouring nco-4.8.0.mojave.bottle.tar.gz
Warning: nco dependency gcc was built with a different C++ standard
library (libstdc++ from clang). This may cause problems at runtime.
🍺 /usr/local/Cellar/nco/4.8.0: 52 files, 10.9MB
==> Caveats
==> antlr@2
antlr@2 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have antlr@2 first in your PATH run:
echo 'export PATH="/usr/local/opt/antlr@2/bin:$PATH"' >> ~/.zshrc

For compilers to find antlr@2 you may need to set:
export LDFLAGS="-L/usr/local/opt/antlr@2/lib"
export CPPFLAGS="-I/usr/local/opt/antlr@2/include"

==> texinfo
texinfo is keg-only, which means it was not symlinked into /usr/local,
because software that uses TeX, such as lilypond and octave, require a newer
version of these files.

If you need to have texinfo first in your PATH run:
echo 'export PATH="/usr/local/opt/texinfo/bin:$PATH"' >> ~/.zshrc

⚙  ~  echo 'export PATH="/usr/local/opt/texinfo/bin:$PATH"' >> ~/.zshrc
⚙  ~  echo 'export PATH="/usr/local/opt/antlr@2/bin:$PATH"' >> ~/.zshrc

环境变量的设置

不难发现,ncdump的命令是有效的,可以用于NetCDF文件的信息查看。
而其安装的位置其实是软连接过来的。

1
2
3
4
5
6
7
~/Dropbox/Code_Fortress/02.My_Fortran_Library  which ncdump
/usr/local/bin/ncdump
~/Dropbox/Code_Fortress/02.My_Fortran_Library  ll /usr/local/bin/ncdump
lrwxr-xr-x 1 zhpfu admin 35 May 30 11:31 /usr/local/bin/ncdump -> ../Cellar/netcdf/4.6.3_1/bin/ncdump
~/Dropbox/Code_Fortress/02.My_Fortran_Library  ls /usr/local/Cellar/netcdf/4.6.3_1/bin/nc*
/usr/local/Cellar/netcdf/4.6.3_1/bin/nc-config /usr/local/Cellar/netcdf/4.6.3_1/bin/ncdump /usr/local/Cellar/netcdf/4.6.3_1/bin/ncgen3
/usr/local/Cellar/netcdf/4.6.3_1/bin/nccopy /usr/local/Cellar/netcdf/4.6.3_1/bin/ncgen

这对后续的调用netcdf的库,指定路径是很有帮助的。

NetCDF Programs实例

实例地址

选取f、f90结尾的文件即可!!!

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
Below we provide links to some sample netCDF programs.

Write a two-dimensional array of sample data that looks like simple_xy.cdl:
C: simple_xy_wr.c
Fortran 77: simple_xy_wr.f
Fortran 90: simple_xy_wr.f90
C++ (legacy): simple_xy_wr.cpp
C++ (netCDF-4): SimpleXyWr.cpp
Java: Simple_xy_wr.java
Contributed: Python: simple_xy_wr.py, MATLAB: simple_xy_wr.m, Perl: simple_xy_wr.pl, IDL: simple_xy_wr.pro
Command-line: ncgen -b simple_xy.cdl
Read data from the simple file written above:
C: simple_xy_rd.c
Fortran 77: simple_xy_rd.f
Fortran 90: simple_xy_rd.f90
C++ (legacy): simple_xy_rd.cpp
C++ (netCDF-4): SimpleXyRd.cpp
Java: Simple_xy_rd.java
Contributed: Python: simple_xy_rd.py, MATLAB: simple_xy_rd.m, Perl: simple_xy_rd.pl, IDL: simple_xy_rd.pro
Command-line: ncdump simple_xy.nc
Write some variables with units attributes and coordinate dimensions that will look like sfc_pres_temp.cdl:
C: sfc_pres_temp_wr.c
Fortran 77: sfc_pres_temp_wr.f
Fortran 90: sfc_pres_temp_wr.f90
C++ (legacy): sfc_pres_temp_wr.cpp
C++ (netCDF-4): SfcPresTempWr.cpp
Java: Sfc_pres_temp_wr.java
Contributed: Python: sfc_pres_temp_wr.py, MATLAB: sfc_pres_temp_wr.m, Perl: sfc_pres_temp_wr.pl, IDL: sfc_pres_temp_wr.pro
Command-line: ncgen -b sfc_pres_temp.cdl
Read data variables and attributes from the file written above:
C: sfc_pres_temp_rd.c
Fortran 77: sfc_pres_temp_rd.f
Fortran 90: sfc_pres_temp_rd.f90
C++ (legacy): sfc_pres_temp_rd.cpp
C++ (netCDF-4): SfcPresTempRd.cpp
Java: Sfc_pres_temp_rd.java
Contributed: Python: sfc_pres_temp_rd.py, MATLAB: sfc_pres_temp_rd.m, Perl: sfc_pres_temp_rd.pl, IDL: sfc_pres_temp_rd.pro
Command-line: ncdump sfc_pres_temp.nc
Write some four-dimensional variables using a record dimension in a file that will look like pres_temp_4D.cdl:
C: pres_temp_4D_wr.c
Fortran 77: pres_temp_4D_wr.f
Fortran 90: pres_temp_4D_wr.f90
C++ (legacy): pres_temp_4D_wr.cpp
C++ (netCDF-4): PresTemp4dWr.cpp
Java: Pres_temp_4D_wr.java
Contributed: Python: pres_temp_4D_wr.py, MATLAB: pres_temp_4D_wr.m, Perl: pres_temp_4D_wr.pl, IDL: pres_temp_4D_wr.pro
Command-line: ncgen -b pres_temp_4D.cdl
Read from the variables in the file written above:
C: pres_temp_4D_rd.c
Fortran 77: pres_temp_4D_rd.f
Fortran 90: pres_temp_4D_rd.f90
C++ (legacy): pres_temp_4D_rd.cpp
C++ (netCDF-4): PresTemp4dRd.cpp
Java: Pres_temp_4D_rd.java
Contributed: Python: pres_temp_4D_rd.py, MATLAB: pres_temp_4D_rd.m, Perl: pres_temp_4D_rd.pl, IDL: pres_temp_4D_rd.pro
Command-line: ncdump pres_temp_4D.nc
Test MATLAB native support for netCDF-4: nc4test.m
Copy any netCDF classic or 64-bit offset file, using only the netCDF-3 API:
C: nccopy3.c
Copy any netCDF file, using the netCDF-4 API:
C: nccopy4.c
Write profile data according to CF Conventions version 1.6, Appendix H:
Fortran 90: example_h3_cf1_6.f90

gfortran读写NetCDF测试

最最最重要的一点就是使用gfortran的时候,一定要指定具体依赖库的路径

在使用gfortran时候,命令如下:

> gfortran test.f90 -L/usr/local/Cellar/netcdf/4.6.3_1/lib -I/usr/local/Cellar/netcdf/4.6.3_1/include -lnetcdff -o testf

在使用intel fortran时候,命令形式如下:

> ifort -I/home/xxx/local/include read_netcdf.f90 -o a.exe -L/home/xxx/local/lib -lnetcdf

Linux中操作方法类似!

至于,用shell脚本或者Makefile来简化手动输入参数之苦,这也是更加实用的技能,再次不做介绍,感兴趣的可以自行Google百度/Bing!

Homebrew一键式安装netcdf的原理揭示

基于Ruby语言!!![参考链接](https://github.com/Homebrew/homebrew-core/blob/master/Formula/netcdf.rb)

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
class Netcdf < Formula
desc "Libraries and data formats for array-oriented scientific data"
homepage "https://www.unidata.ucar.edu/software/netcdf"
url "https://www.unidata.ucar.edu/downloads/netcdf/ftp/netcdf-c-4.6.3.tar.gz"
sha256 "335fdf16d7531f430ad75e732ed1a9a3fc83ad3ef91fb33a70119a555dd5415c"
revision 1

bottle do
sha256 "d798cbc16c2e6c312d921abf4cef0d74f094c9f6496114c06219baabaa962974" => :mojave
sha256 "0307831230fe41037beb09e9434d587497dc246ad4c0eb083e603efa4d5fbbec" => :high_sierra
sha256 "272ebe803e2c331d4839387d644261281315486413d581e460c1fef91455df6b" => :sierra
end

depends_on "cmake" => :build
depends_on "gcc" # for gfortran
depends_on "hdf5"

resource "cxx" do
url "https://github.com/Unidata/netcdf-cxx4/archive/v4.3.0.tar.gz"
sha256 "25da1c97d7a01bc4cee34121c32909872edd38404589c0427fefa1301743f18f"
end

resource "cxx-compat" do
url "https://www.unidata.ucar.edu/downloads/netcdf/ftp/netcdf-cxx-4.2.tar.gz"
mirror "https://www.gfd-dennou.org/arch/netcdf/unidata-mirror/netcdf-cxx-4.2.tar.gz"
sha256 "95ed6ab49a0ee001255eac4e44aacb5ca4ea96ba850c08337a3e4c9a0872ccd1"
end

resource "fortran" do
url "https://www.unidata.ucar.edu/downloads/netcdf/ftp/netcdf-fortran-4.4.4.tar.gz"
mirror "https://www.gfd-dennou.org/arch/netcdf/unidata-mirror/netcdf-fortran-4.4.4.tar.gz"
sha256 "b2d395175f8d283e68c8be516e231a96b191ade67ad0caafaf7fa01b1e6b5d75"
end

def install
ENV.deparallelize

common_args = std_cmake_args << "-DBUILD_TESTING=OFF"

mkdir "build" do
args = common_args.dup
args << "-DENABLE_TESTS=OFF"
args << "-DNC_EXTRA_DEPS=-lmpi" if Tab.for_name("hdf5").with? "mpi"
args << "-DENABLE_DAP_AUTH_TESTS=OFF" << "-DENABLE_NETCDF_4=ON" << "-DENABLE_DOXYGEN=OFF"

system "cmake", "..", "-DBUILD_SHARED_LIBS=ON", *args
system "make", "install"
system "make", "clean"
system "cmake", "..", "-DBUILD_SHARED_LIBS=OFF", *args
system "make"
lib.install "liblib/libnetcdf.a"
end

# Add newly created installation to paths so that binding libraries can
# find the core libs.
args = common_args.dup << "-DNETCDF_C_LIBRARY=#{lib}"

cxx_args = args.dup
cxx_args << "-DNCXX_ENABLE_TESTS=OFF"
resource("cxx").stage do
mkdir "build-cxx" do
system "cmake", "..", "-DBUILD_SHARED_LIBS=ON", *cxx_args
system "make", "install"
system "make", "clean"
system "cmake", "..", "-DBUILD_SHARED_LIBS=OFF", *cxx_args
system "make"
lib.install "cxx4/libnetcdf-cxx4.a"
end
end

fortran_args = args.dup
fortran_args << "-DENABLE_TESTS=OFF"
resource("fortran").stage do
mkdir "build-fortran" do
system "cmake", "..", "-DBUILD_SHARED_LIBS=ON", *fortran_args
system "make", "install"
system "make", "clean"
system "cmake", "..", "-DBUILD_SHARED_LIBS=OFF", *fortran_args
system "make"
lib.install "fortran/libnetcdff.a"
end
end

ENV.prepend "CPPFLAGS", "-I#{include}"
ENV.prepend "LDFLAGS", "-L#{lib}"
resource("cxx-compat").stage do
system "./configure", "--disable-dependency-tracking",
"--enable-shared",
"--enable-static",
"--prefix=#{prefix}"
system "make"
system "make", "install"
end

# SIP causes system Python not to play nicely with @rpath
libnetcdf = (lib/"libnetcdf.dylib").readlink
%w[libnetcdf-cxx4.dylib libnetcdf_c++.dylib].each do |f|
macho = MachO.open("#{lib}/#{f}")
macho.change_dylib("@rpath/#{libnetcdf}",
"#{lib}/#{libnetcdf}")
macho.write!
end
end

test do
(testpath/"test.c").write <<~EOS
#include <stdio.h>
#include "netcdf_meta.h"
int main()
{
printf(NC_VERSION);
return 0;
}
EOS
system ENV.cc, "test.c", "-L#{lib}", "-I#{include}", "-lnetcdf",
"-o", "test"
assert_equal `./test`, version.to_s

(testpath/"test.f90").write <<~EOS
program test
use netcdf
integer :: ncid, varid, dimids(2)
integer :: dat(2,2) = reshape([1, 2, 3, 4], [2, 2])
call check( nf90_create("test.nc", NF90_CLOBBER, ncid) )
call check( nf90_def_dim(ncid, "x", 2, dimids(2)) )
call check( nf90_def_dim(ncid, "y", 2, dimids(1)) )
call check( nf90_def_var(ncid, "data", NF90_INT, dimids, varid) )
call check( nf90_enddef(ncid) )
call check( nf90_put_var(ncid, varid, dat) )
call check( nf90_close(ncid) )
contains
subroutine check(status)
integer, intent(in) :: status
if (status /= nf90_noerr) call abort
end subroutine check
end program test
EOS
system "gfortran", "test.f90", "-L#{lib}", "-I#{include}", "-lnetcdff",
"-o", "testf"
system "./testf"
end
end

手动安装netcdf参考方法:

参考链接:install-netcdf-fortran/install_netcdf.sh

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/sh
#
# install_netcdf.sh
# Copyright (C) 2018 Daniel Santiago <dpelaez@cicese.edu.mx>
#
# Distributed under terms of the GNU/GPL license.
#
set -e


# ============================================================================
# Installation of NetCDF4 Fortran libraries
# ----------------------------------------------------------------------------
#
# Purpose:
# This script get the given versions of the NetCD4 libreries and its
# dependencies and install them in the MAINDIR=/usr/local/netcdf/ directory
#
# Usage:
# [sudo] CC=gcc FC=gfortran MAINDIR=/usr/local/netcdf ./install_netcdf.sh
#
# Autor:
# Daniel Santiago
# github/dspelaez
#
# ============================================================================

## define compilers
CC=${CC:-gcc}
FC=${FC:-gfortran}
F90=${FC}
F77=${FC}

# main directory
MAINDIR=${MAINDIR:-/usr/local/netcdf}

# version of libs
CLTAG="7.61.0"
ZLTAG="1.2.10"
H5TAG="1.10.1"
NCTAG="4.6.1"
NFTAG="4.4.4"

## donwload source code of depencies
wget -nc -nv https://curl.haxx.se/download/curl-$CLTAG.tar.gz
wget -nc -nv https://zlib.net/fossils/zlib-$ZLTAG.tar.gz
wget -nc -nv https://support.hdfgroup.org/ftp/HDF5/releases/hdf5-1.10/hdf5-$H5TAG/src/hdf5-$H5TAG.tar
wget -nc -nv ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-$NCTAG.tar.gz
wget -nc -nv ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-fortran-$NFTAG.tar.gz

## create config.log
touch config.log

## curl
tar -xf curl-$CLTAG.tar.gz
cd curl-$CLTAG/
CLDIR=$MAINDIR
echo " --->> Compiling curl-$CLTAG"
./configure --prefix=${CLDIR} > config.log 2>&1
make -j4 > config.log 2>&1
make install > config.log 2>&1
cd ..
rm -rf curl-$CLTAG


## zlib
tar -xf zlib-$ZLTAG.tar.gz
cd zlib-$ZLTAG/
ZDIR=$MAINDIR
echo " --->> Compiling zlib-$ZLTAG"
./configure --prefix=${ZDIR} > config.log 2>&1
make -j4 > config.log 2>&1
make install > config.log 2>&1
cd ..
rm -rf zlib-$ZLTAG

## hdf5
tar -xf hdf5-$H5TAG.tar
cd hdf5-$H5TAG/
H5DIR=$MAINDIR
echo " --->> Compiling hdf5-$H5TAG"
./configure --with-zlib=${ZDIR} --prefix=${H5DIR} > config.log 2>&1
make -j4 > config.log 2>&1
make install > config.log 2>&1
cd ..
rm -rf hdf5-$H5TAG

## netcdf4-c
tar -xf netcdf-$NCTAG.tar.gz
cd netcdf-$NCTAG/
NCDIR=$MAINDIR
echo " --->> Compiling netcdf-$NCTAG"
CPPFLAGS=-I${H5DIR}/include LDFLAGS=-L${H5DIR}/lib ./configure --prefix=${NCDIR} > config.log 2>&1
make -j4 > config.log 2>&1
make install > config.log 2>&1
cd ..
rm -rf netcdf-$NCTAG

## netcdf4-fortran
tar -xf netcdf-fortran-$NFTAG.tar.gz
cd netcdf-fortran-$NFTAG/
echo " --->> Compiling netcdf-fortran-$NFTAG"
CPPFLAGS=-I${NCDIR}/include LDFLAGS=-L${NCDIR}/lib ./configure --prefix=${NCDIR} > config.log 2>&1
make -j4 > config.log 2>&1
make install > config.log 2>&1
cd ..
rm -rf netcdf-fortran-$NFTAG

## show compilation options
$NCDIR/bin/nf-config --all

echo ""
echo ===============================================================================
echo "Finally, you must add this to the .profile (or .bashrc or .zshrc) file"
echo " Linux --\>" export LD_LIBRARY_PATH=$NCDIR/lib:'$LD_LIBRARY_PATH'
echo " OSX --\>" export DYLD_LIBRARY_PATH=$NCDIR/lib:'$DYLD_LIBRARY_PATH'
echo ===============================================================================
echo ""

参考:

链接.1
链接.2
链接.3
链接.4
链接.5
链接.6
链接.7
链接.8
链接.9
链接.10
链接.11

有任何问题都欢迎交流探讨,共同学习进步!


-------------本文结束 感谢阅读-------------
作者Gavin
有问题请在相应页面留言(评论系统DISQUS需要"翻墙"才可见)
或者私信给我 GMAIL: zhpfu.atm@gmail.com
满分是10分的话,这篇文章你给几分
--> -->