Beispiel #1
0
def command_query(ns):
    """
    Command to query given BUFR files.
    """
    decoder = Decoder(definitions_dir=ns.definitions_directory,
                      tables_root_dir=ns.tables_root_directory,
                      compiled_template_cache_max=ns.compiled_template_cache_max)

    for filename in ns.filenames:
        with open(filename, 'rb') as ins:
            s = ins.read()

        if ns.query_string.strip()[0] == '%':
            bufr_message = decoder.process(s, file_path=filename, info_only=True)
            from pybufrkit.mdquery import MetadataExprParser, MetadataQuerent
            querent = MetadataQuerent(MetadataExprParser())
            value = querent.query(bufr_message, ns.query_string)
            print(filename)
            print(value)

        else:
            bufr_message = decoder.process(s, file_path=filename, wire_template_data=True,
                                           ignore_value_expectation=ns.ignore_value_expectation)
            from pybufrkit.dataquery import NodePathParser, DataQuerent
            querent = DataQuerent(NodePathParser())
            query_result = querent.query(bufr_message, ns.query_string)
            if ns.json:
                if ns.nested:
                    print(json.dumps(NestedJsonRenderer().render(query_result), **JSON_DUMPS_KWARGS))
                else:
                    print(json.dumps(FlatJsonRenderer().render(query_result), **JSON_DUMPS_KWARGS))
            else:
                print(filename)
                print(FlatTextRenderer().render(query_result))
Beispiel #2
0
 def show_message(m):
     if ns.attributed:
         m.wire()
         if ns.json:
             print(json.dumps(NestedJsonRenderer().render(m), **JSON_DUMPS_KWARGS))
         else:
             print(NestedTextRenderer().render(m))
     else:
         if ns.json:
             print(json.dumps(FlatJsonRenderer().render(m), **JSON_DUMPS_KWARGS))
         else:
             print(FlatTextRenderer().render(m))
Beispiel #3
0
    'profiler_european.bufr',
    'rado_250.bufr',
    'uegabe.bufr',
)


def read_bufr_file(file_name):
    with open(os.path.join(DATA_DIR, file_name), 'rb') as ins:
        s = ins.read()
    return s


decoder = Decoder()
flat_text_renderer = FlatTextRenderer()
nested_text_renderer = NestedTextRenderer()
flat_json_renderer = FlatJsonRenderer()
nested_json_renderer = NestedJsonRenderer()


def test_nested_json_to_flat_json():
    def func(filename):
        s = read_bufr_file(filename)
        bufr_message = decoder.process(s)
        nested = nested_json_renderer.render(bufr_message)
        flat = flat_json_renderer.render(bufr_message)
        assert flat == nested_json_to_flat_json(nested)

    for filename in FILES:
        func(filename)

Beispiel #4
0
name = 'FLORENCE'
latf = '-51p4degW'
lonf = '25degN'
s1 = 'A_JSXX01ECEP{}{}00'.format(dd, hh)
s2 = '_C_ECMP_20{}{}{}{}0000_tropical_cyclone_track_'.format(yy, mm, dd, hh)
s3 = '{}_{}_{}_bufr4.bin'.format(name, latf, lonf)
fn = s1 + s2 + s3

# read in file
print 'opening file...'
with open(fn, 'rb') as ins:
    bufr_message = dec.process(ins.read())

# convert to json
print 'conver to json...'
json_data = FlatJsonRenderer().render(bufr_message)
nj = len(json_data)
jd = [None] * nj
for i, j in enumerate(json_data):
    jd[i] = j

# put ensemble data into a dictionary
ens_data = jd[4][2]
nens = len(ens_data)
ens_tc_arr = [None] * nens

print 'making dictionary...'
for i in range(nens):
    ens_tc_arr[i] = ens_data[i]

ens_dict = ens_data_dict(ens_tc_arr)
            'id_no': forcast_df.id_no,
            'ensemble_number': forcast_df.ensemble_number,
            'is_ensemble':forcast_df.is_ensemble,
            'forecast_time': forcast_df.forecast_time,
            'basin': forcast_df.basin,
            'category': forcast_df.category,
        }
        )
track = track.set_coords(['lat', 'lon'])    
#%%
for line in StringIO(text_data):
    [line.split()[-1] for line in StringIO(text_data) if line[6:17]=="004001 YEAR" ]
    #print(line)
#%%
from pybufrkit.renderer import FlatJsonRenderer
json_data = FlatJsonRenderer().render(bufr)  


msg = {
    # subset forecast data
    'significance': data_query(bufr, '316082' + '> 008005')}


for index in msg['significance'].subset_indices():
    sig = np.array(msg['significance'].get_values(index), dtype='int')
    print(sig)

#%%
from pybufrkit.decoder import generate_bufr_message
i=1
with open(os.path.join(path_ecmwf,ecmwf_file), 'rb') as ins:
#** Open WMO bufr file 
decoder = Decoder()
with open(infilename,'rb') as ins:
  bufr_message = decoder.process(s=ins.read(),file_path=infilename)

#** open NCEP bufr file
if append:
  bufr = ncepbufr.open(outfilename,'a')
else:
  bufr = ncepbufr.open(outfilename,'w',table=bufrtable)

#---------------------------------------------------------------------------------#
# Read WMO Standard BUFR
#---------------------------------------------------------------------------------#
#** get whole data
data = FlatJsonRenderer().render(bufr_message)[3][2][0]

#** get header
hdr = bufr.missing_value*np.ones((44),np.float)
hdr[0:37] = np.asarray(data[0:37])
yyyymmddhh ='%04i%02i%02i%02i%02i' % tuple(hdr[6:11])

#** get number of replications of ROSEQ1
nreps_this_ROSEQ1 = np.asarray(data[37:38])

#** get data1b (roseq1)
data1b = bufr.missing_value*np.ones(([_nmaxseq,int(nreps_this_ROSEQ1)]),np.float)
nreps_this_ROSEQ2 = bufr.missing_value*np.ones(([int(nreps_this_ROSEQ1)]),np.float)

n = 38  # skip header (should be no change)
for k in range(int(nreps_this_ROSEQ1)):