/
fv3grib2nc4.py
executable file
·119 lines (96 loc) · 3.88 KB
/
fv3grib2nc4.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
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
#!/usr/bin/env python
###############################################################
# < next few lines under version control, D O N O T E D I T >
# $Date: 2018-03-29 10:12:00 -0400 (Thu, 29 Mar 2018) $
# $Revision: 100014 $
# $Author: rahul.mahajan@noaa.gov $
# $Id: nemsio2nc4.py 100014 2018-03-29 14:12:00Z rahul.mahajan@noaa.gov $
###############################################################
__author__ = 'Barry Baker'
__email__ = 'barry.baker@noaa.gov'
__license__ = 'GPL'
'''
Simple utility to convert NCEP grib2 aerosol file into a netCDF4 file
Utilizes wgrib2 utility from NWPROD
'''
import os
from glob import glob
import sys
import subprocess
from distutils.spawn import find_executable
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
def execute_subprocess(cmd, verbose=False):
'''
call the subprocess command
:param cmd: command to execute
:param type: str
'''
if verbose:
print( 'Executing: %s' % cmd)
try:
out = subprocess.check_output(cmd,shell=True)
except subprocess.CalledProcessError as e:
raise subprocess.CalledProcessError('',cmd,output=e.output)
return
def get_exec_path(exec_name, verbose=False):
'''
get the full path to a given executable name
:param exec_name: executable to fine
:param type: str
'''
exec_path_def = '/nwprod2/grib_util.v1.0.0/exec/%s' % exec_name
# exec_path = find_executable(exec_name)
# if exec_path is None:
# exec_path = exec_path_def
# WCOSS
if os.path.exists('/nwprod2/grib_util.v1.0.0/exec/%s' % exec_name):
exec_path = '/nwprod2/grib_util.v1.0.0/exec/%s' % exec_name
#HERA
elif os.path.exists('/apps/wgrib2/2.0.8/intel/18.0.3.222/bin/%s' % exec_name):
exec_path = '/apps/wgrib2/2.0.8/intel/18.0.3.222/bin/%s' % exec_name
# ERROR
else:
print("ERROR.... WGRIB2 EXECUTABLE NOT FOUND")
if verbose:
print( '%s: %s' % (exec_name, exec_path))
return exec_path
def chdir(fname):
dir_path = os.path.dirname(os.path.realpath(fname))
os.chdir(dir_path)
return os.path.basename(fname)
def change_file(finput,verbose=False,outdir='./'):
# first change directory and get file name
fname = chdir(finput)
out = os.path.abspath(outdir)
# this will create 3 files and append to them
wgrib2 = get_exec_path('wgrib2', verbose=verbose)
# ENTIRE ATMOSPHERE GRIB LAYER
cmd = '%s %s -match "entire atmosphere:" -nc_nlev 1 -append -set_ext_name 1 -netcdf %s/%s.entire_atm.nc' % (wgrib2, fname, out, fname)
execute_subprocess(cmd, verbose=verbose)
# 1 hybrid level:
cmd = '%s %s -match "1 hybrid level:" -append -set_ext_name 1 -netcdf %s/%s.hybrid.nc' % (wgrib2, fname, out, fname)
execute_subprocess(cmd, verbose=verbose)
# surface:
cmd = '%s %s -match "surface:" -nc_nlev 1 -append -set_ext_name 1 -netcdf %s/%s.surface.nc' % (wgrib2, fname, out, fname)
execute_subprocess(cmd, verbose=verbose)
if __name__ == '__main__':
parser = ArgumentParser(description='convert nemsio file to netCDF4 file', formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('-f', '--files', help='input nemsio file name', type=str, required=True)
parser.add_argument('-o', '--output_dir', help='output directory path', default='./', required=False)
parser.add_argument('-v', '--verbose', help='print debugging information', action='store_true', required=False)
args = parser.parse_args()
finput = args.files
verbose = args.verbose
outdir = args.output_dir
files = sorted(glob(finput))
for i,j in enumerate(files):
files[i] = os.path.realpath(j)
if len(files) == 1:
finput = files[0]
change_file(finput,verbose=verbose,outdir=outdir)
else:
for i in files:
finput = i
print('FINPUT -> ',finput)
change_file(finput,verbose=verbose)
sys.exit(0)