def _do_read(b_table_file, d_table_file, bufr_file, read_func): b_file = codecs.open(b_table_file, 'rb', 'utf-8') with b_file: d_file = codecs.open(d_table_file, 'rb', 'utf-8') with d_file: table = libbufr.read_tables(b_file, d_file) f = open(bufr_file, 'rb') with f: return read_func(f, table)
from __future__ import print_function from __future__ import absolute_import import bufrpy from bufrpy.template import safnwc from bufrpy.table import libbufr import json import sys import time import codecs bufr_fname = None if len(sys.argv) == 4: # b-table and d-table table = libbufr.read_tables(codecs.open(sys.argv[1], 'rb', 'utf-8'), codecs.open(sys.argv[2], 'rb', 'utf-8')) bufr_fname = sys.argv[3] else: # either just b-table or a template file bufr_fname = sys.argv[2] try: # First try reading as safnwc template table = safnwc.read_template(codecs.open(sys.argv[1], 'rb', 'utf-8')) except Exception as e: # Try reading as libbufr table table = libbufr.read_tables(codecs.open(sys.argv[1], 'rb', 'utf-8')) msg = bufrpy.decode_file(open(bufr_fname, 'rb'), table) out = json.dumps(bufrpy.to_json(msg)) dec = bufrpy.from_json(json.loads(out)) print(out)
for time in subset.values[-1]: data.append(decode_time(time)) df = pd.DataFrame(data) df.loc[df['lon'] - min(df['lon']) > 90]['lon'] = df['lon'] - 360. df[INTERP_KEYS] = df[INTERP_KEYS].interpolate().where( df[INTERP_KEYS].bfill().notnull()) df.loc[(df['lon'] <= -180), 'lon'] = df['lon'] + 360. df.loc[(df['lon'] > 180), 'lon'] = df['lon'] - 360. alldata.append(df) return alldata #//TODO Deal with output file name if __name__ == '__main__': table = libbufr.read_tables(codecs.open(BUFR_TABLES[0], 'rb', 'utf-8'), codecs.open(BUFR_TABLES[1], 'rb', 'utf-8')) bufr_fname = '/ct12/abrammer/graphics/ecmf_tc_data/data/A_JSXX01ECEP141200_C_ECMP_20170714120000_tropical_cyclone_track_FERNANDA_-118p3degW_11degN_bufr4.bin' bufr_out = 'test.dat' #bufr_fname.replace('.bin', '.dat') bufr_fname = sys.argv[1] print(f"Converting {bufr_fname}") msg = bufrpy.decode_file(open(bufr_fname, 'rb'), table) alldata = bufr_to_data(msg) with open(bufr_out, 'a') as fout: for subset in alldata: basin = which_basin(subset) for index, datum in subset.iterrows(): if (pd.notnull(datum.get_value('lon'))): fout.write( atcf.line_out(basin, int(subset['STORM IDENTIFIER'][0][:2]),