3 from __future__
import print_function
8 from netCDF4
import Dataset
9 from pathlib
import Path
11 IODA_CONV_PATH = Path(__file__).parent/
"@SCRIPT_LIB_PATH@"
12 if not IODA_CONV_PATH.is_dir():
13 IODA_CONV_PATH = Path(__file__).parent/
'..'/
'lib-python'
14 sys.path.append(
str(IODA_CONV_PATH.resolve()))
16 import bufr2ncCommon
as cm
17 import bufr2ncObsTypes
as ot
28 bufr = ncepbufr.open(BufrFname)
33 Obs.start_msg_selector()
34 while (Obs.select_next_msg(bufr)):
35 NumObs += Obs.msg_obs_count(bufr)
39 return [NumObs, Obs.num_msg_selected, Obs.num_msg_mtype]
45 ScriptName = os.path.basename(sys.argv[0])
48 ap = argparse.ArgumentParser()
49 ap.add_argument(
"obs_type", help=
"observation type")
50 ap.add_argument(
"input_bufr", help=
"path to input BUFR file")
51 ap.add_argument(
"output_netcdf", help=
"path to output netCDF4 file")
52 ap.add_argument(
"-m",
"--maxmsgs", type=int, default=-1,
53 help=
"maximum number of messages to keep", metavar=
"<max_num_msgs>")
54 ap.add_argument(
"-t",
"--thin", type=int, default=1,
55 help=
"select every nth message (thinning)", metavar=
"<thin_interval>")
56 ap.add_argument(
"-c",
"--clobber", action=
"store_true",
57 help=
"allow overwrite of output netcdf file")
58 ap.add_argument(
"-p",
"--prepbufr", action=
"store_true",
59 help=
"input BUFR file is in prepBUFR format")
61 MyArgs = ap.parse_args()
63 ObsType = MyArgs.obs_type
64 BufrFname = MyArgs.input_bufr
65 NetcdfFname = MyArgs.output_netcdf
66 MaxNumMsg = MyArgs.maxmsgs
67 ThinInterval = MyArgs.thin
68 ClobberOfile = MyArgs.clobber
70 BfileType = cm.BFILE_PREPBUFR
72 BfileType = cm.BFILE_BUFR
76 if (
not os.path.isfile(BufrFname)):
77 print(
"ERROR: {0:s}: Specified input BUFR file does not exist: {1:s}".format(
78 ScriptName, BufrFname))
82 if (os.path.isfile(NetcdfFname)):
84 print(
"WARNING: {0:s}: Overwriting nc file: {1:s}".format(
85 ScriptName, NetcdfFname))
88 print(
"ERROR: {0:s}: Specified nc file already exists: {1:s}".format(
89 ScriptName, NetcdfFname))
90 print(
"ERROR: {0:s}: Use -c option to overwrite.".format(ScriptName))
95 if (ObsType ==
'Aircraft'):
96 Obs = ot.AircraftObsType(BfileType)
97 elif (ObsType ==
'Sondes'):
98 Obs = ot.SondesObsType(BfileType)
99 elif (ObsType ==
'Amsua'):
100 Obs = ot.AmsuaObsType(BfileType)
101 elif (ObsType ==
'Gpsro'):
102 Obs = ot.GpsroObsType(BfileType)
104 print(
"ERROR: {0:s}: Unknown observation type: {1:s}".format(
105 ScriptName, ObsType))
110 if (Obs.mtype_re ==
'UnDef'):
111 if (BfileType == cm.BFILE_BUFR):
112 print(
"ERROR: {0:s}: Observation type {1:s} for BUFR format is undefined".format(
113 ScriptName, ObsType))
114 elif (BfileType == cm.BFILE_PREPBUFR):
115 print(
"ERROR: {0:s}: Observation type {1:s} for prepBUFR format is undefined".format(
116 ScriptName, ObsType))
127 print(
"Converting BUFR to netCDF")
128 print(
" Observation Type: {0:s}".format(ObsType))
129 if (BfileType == cm.BFILE_BUFR):
130 print(
" Input BUFR file (BUFR format): {0:s}".format(BufrFname))
131 elif (BfileType == cm.BFILE_PREPBUFR):
132 print(
" Input BUFR file (prepBUFR format): {0:s}".format(BufrFname))
133 print(
" Output netCDF file: {0:s}".format(NetcdfFname))
136 " Limiting nubmer of messages to record to {0:d} messages".format(MaxNumMsg))
137 if (ThinInterval > 1):
138 print(
" Thining: selecting every {0:d}-th message".format(ThinInterval))
160 Obs.max_num_msg = MaxNumMsg
161 Obs.thin_interval = ThinInterval
164 print(
" Total number of messages that match obs type {0:s}: {1:d}".format(
166 print(
" Number of messages selected: {0:d}".format(NumMsgs))
167 print(
" Number of observations selected: {0:d}".format(NumObs))
173 Obs.set_nlocs(NumObs)
177 nc = Dataset(NetcdfFname,
'w', format=
'NETCDF4')
178 Obs.create_nc_datasets(nc)
186 bufr = ncepbufr.open(BufrFname)
189 Obs.convert(bufr, nc)
def BfilePreprocess(BufrFname, Obs)
SUBROUTINES.