WRF / Python> Read two NetCDF files and display (latitude, longitude, target value)> enumerate (zip (* rows))

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).

  1. proc_vallatlon_170315_exec: bash script
  2. read_lat_lon.ncl : NCAR Command Language script
  3. combine_3files_170315.py: Python script

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)

Matters taught

@ shiracamus's Comment gave an example of using ʻenumerate ()andzip (*)`. ..

Thank you for the information.

Recommended Posts

WRF / Python> Read two NetCDF files and display (latitude, longitude, target value)> enumerate (zip (* rows))
Read and use Python files from Python
Read and write JSON files in Python
How to use Python zip and enumerate
Distance calculation between two latitude and longitude points with python (using spherical trigonometry)