Operating environment
Xeon E5-2620 v4 (8 cores) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 and its-devel
mpich.x86_64 3.1-5.el6 and its-devel
gcc version 4.4.7 (And gfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.Use 1.
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
Python 3.6.0 on virtualenv
Related http://qiita.com/7of9/items/6fd5d52439b0adc92d95
LN-test_nc: WRF related processing result file LN-latlon_nc: Latitude / longitude information file
The following was implemented to read two files and format them as (lat, lon, target value).
I'm not familiar with NCL and I'm not good at bash. I'm still learning Python. Continuing to use these three files is not easy to maintain.
I heard that it would be easy to use the netCDF4 package in Python and did so.
Reference http://qiita.com/okadate/items/954574a95545b06ca257
code
read_netcdf_170323.py
import netCDF4
import sys
# on Python 2.6.6
# codingrule:PEP8
# LN-test_nc: netCDF file including processed data
# LN-latlon_nc: netCDF file including (lat, lon)
#
# 1. read data
nc = netCDF4.Dataset('LN-test_nc', 'r')
cnc = nc.variables['CONC'][:]
nc.close()
# 2. read lat lon
nc = netCDF4.Dataset('LN-latlon_nc', 'r')
lat = nc.variables['XLAT'][:]
lon = nc.variables['XLONG'][:]
nc.close()
# debug
print(cnc.shape) # (24, 1, 1, 3, 65, 82)
print(lat.shape) # (65, 82)
print(lon.shape) # (65, 82)
print(cnc[0, 0, 0, 0, 0, 81]) # (24, 1, 1, 3, 65, 82)
# 3. extract map
timidx = 2 # 2: arbitrary
cmap = cnc[timidx, 0, 0, 0, :, :] # (65, 82)
print(cmap.shape)
# 4. output
for alat, alon, acnc in zip(lat, lon, cmap):
for idx in range(len(alat)):
print('%.5f, %.5f, %.5f' % (alat[idx], alon[idx], acnc[idx]))
# debug
if idx > 5:
sys.exit() # debug
result
$ python read_netcdf_170323.py
(24, 1, 1, 3, 65, 82)
(65, 82)
(65, 82)
0.0
(65, 82)
36.07000, 111.17000, 0.00000
36.07000, 111.29000, 0.00000
36.07000, 111.41000, 0.00000
36.07000, 111.53000, 0.00000
36.07000, 111.65000, 0.00000
36.07000, 111.77000, 0.00000
36.07000, 111.89000, 0.00000
Now you only have to maintain one file.
NCL (NCAR Command Language script) only needs to be able to read existing scripts. There is no need for learning costs to write.
The above code also works with virtualenv Python 3.6.0. (easy_install netCDF4)
@ shiracamus's Comment gave an example of using ʻenumerate ()and
zip (*)`. ..
Thank you for the information.
Recommended Posts