2019-04-15 13:44:45 +00:00
|
|
|
# Copyright 2005-2019 ECMWF.
|
2015-11-11 10:24:37 +00:00
|
|
|
#
|
|
|
|
# This software is licensed under the terms of the Apache Licence Version 2.0
|
|
|
|
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
#
|
2015-11-11 17:11:55 +00:00
|
|
|
# In applying this licence, ECMWF does not waive the privileges and immunities
|
|
|
|
# granted to it by virtue of its status as an intergovernmental organisation
|
|
|
|
# nor does it submit to any jurisdiction.
|
2015-11-11 10:24:37 +00:00
|
|
|
#
|
|
|
|
|
2015-11-26 09:31:36 +00:00
|
|
|
# Description: how to encode flight dataset into BUFR
|
|
|
|
|
2017-08-09 12:52:53 +00:00
|
|
|
from __future__ import print_function
|
2015-11-12 13:42:31 +00:00
|
|
|
from datetime import datetime
|
2015-11-11 10:24:37 +00:00
|
|
|
import traceback
|
2015-11-12 13:42:31 +00:00
|
|
|
import numpy as np
|
2015-11-10 20:45:59 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
from eccodes import *
|
|
|
|
|
2015-11-16 10:54:07 +00:00
|
|
|
VERBOSE = 1 # verbose error reporting
|
2015-11-11 10:24:37 +00:00
|
|
|
|
2015-11-11 17:11:55 +00:00
|
|
|
|
2017-08-09 13:16:58 +00:00
|
|
|
def example(csvfile, input_filename, output_filename):
|
2015-11-12 15:15:50 +00:00
|
|
|
fbufrin = open(input_filename, 'rb')
|
2015-11-11 10:24:37 +00:00
|
|
|
fbufrout = open(output_filename, 'wb')
|
|
|
|
|
2017-08-09 12:52:53 +00:00
|
|
|
print('Using ecCodes version: ', codes_get_api_version())
|
2015-11-11 10:24:37 +00:00
|
|
|
|
|
|
|
# The first line in the CSV has the column names
|
2017-08-09 12:52:53 +00:00
|
|
|
print('Reading input CSV file: ', csvfile)
|
2018-11-23 18:04:03 +00:00
|
|
|
parse_date = lambda x: datetime.strptime(x.decode('ascii'), '%Y%m%d')
|
|
|
|
parse_time = lambda x: datetime.strptime(x.decode('ascii'), '%H:%M:%S')
|
2015-11-12 13:42:31 +00:00
|
|
|
data = np.genfromtxt(csvfile, delimiter=',', dtype=None, names=True,
|
|
|
|
converters={0: parse_date, 1: parse_time})
|
2015-11-11 10:24:37 +00:00
|
|
|
|
|
|
|
ymd_column = data['ymd']
|
2015-11-12 13:42:31 +00:00
|
|
|
years = np.array([x.year for x in ymd_column])
|
|
|
|
months = np.array([x.month for x in ymd_column])
|
|
|
|
days = np.array([x.day for x in ymd_column])
|
2015-11-11 10:24:37 +00:00
|
|
|
|
|
|
|
time_column = data['time']
|
2015-11-12 13:42:31 +00:00
|
|
|
hours = np.array([x.hour for x in time_column])
|
|
|
|
minutes = np.array([x.minute for x in time_column])
|
|
|
|
seconds = np.array([x.second for x in time_column])
|
2015-11-11 10:24:37 +00:00
|
|
|
|
2015-11-11 17:11:55 +00:00
|
|
|
latitudes = data['latitude']
|
|
|
|
longitudes = data['longitude']
|
|
|
|
altitudes = data['altitude']
|
|
|
|
pressures = data['pressure']
|
|
|
|
windSpeeds = data['windSpeed']
|
2015-11-11 10:24:37 +00:00
|
|
|
windDirections = data['windDirection']
|
2015-11-11 17:11:55 +00:00
|
|
|
temperatures = data['temperature']
|
2015-11-11 10:24:37 +00:00
|
|
|
|
2017-08-09 12:52:53 +00:00
|
|
|
print('Reading input BUFR file: ', input_filename)
|
2015-11-11 10:24:37 +00:00
|
|
|
bufr = codes_bufr_new_from_file(fbufrin)
|
|
|
|
|
2015-11-11 17:11:55 +00:00
|
|
|
codes_set(bufr, 'masterTablesVersionNumber', 24)
|
|
|
|
codes_set(bufr, 'localTablesVersionNumber', 0)
|
|
|
|
codes_set(bufr, 'compressedData', 1)
|
|
|
|
codes_set(bufr, 'numberOfSubsets', len(years))
|
2015-11-11 10:24:37 +00:00
|
|
|
|
2015-11-12 14:24:30 +00:00
|
|
|
# unexpandedDescriptors and BufrTemplate can be set alternatively
|
|
|
|
# to choose the template for the BUFR message
|
2015-11-11 10:24:37 +00:00
|
|
|
|
2017-08-09 13:16:58 +00:00
|
|
|
# unexpandedDescriptors = [301051,4006,7002,10004,12001,11001,11002,11031,11032,11033,20041]
|
|
|
|
# codes_set_array(bufr, 'unexpandedDescriptors', unexpandedDescriptors)
|
2015-11-12 14:24:30 +00:00
|
|
|
|
2017-08-09 13:16:58 +00:00
|
|
|
codes_set(bufr, 'BufrTemplate', 'aircraftReportWithSecondsAndPressure')
|
2015-11-11 10:24:37 +00:00
|
|
|
|
2016-12-19 16:01:13 +00:00
|
|
|
codes_set_array(bufr, 'year', years)
|
|
|
|
codes_set_array(bufr, 'month', months)
|
|
|
|
codes_set_array(bufr, 'day', days)
|
|
|
|
codes_set_array(bufr, 'hour', hours)
|
|
|
|
codes_set_array(bufr, 'minute', minutes)
|
|
|
|
codes_set_array(bufr, 'second', seconds)
|
|
|
|
codes_set_array(bufr, 'latitude', latitudes)
|
|
|
|
codes_set_array(bufr, 'longitude', longitudes)
|
|
|
|
codes_set_array(bufr, 'height', altitudes)
|
|
|
|
codes_set_array(bufr, 'nonCoordinatePressure', pressures)
|
|
|
|
codes_set_array(bufr, 'windSpeed', windSpeeds)
|
|
|
|
codes_set_array(bufr, 'windDirection', windDirections)
|
|
|
|
codes_set_array(bufr, 'airTemperature', temperatures)
|
2015-11-11 10:24:37 +00:00
|
|
|
|
2015-11-11 17:11:55 +00:00
|
|
|
codes_set(bufr, 'pack', 1)
|
2015-11-11 10:24:37 +00:00
|
|
|
|
2015-11-11 17:11:55 +00:00
|
|
|
codes_write(bufr, fbufrout)
|
2017-08-09 12:52:53 +00:00
|
|
|
print('Created output BUFR file: ', output_filename)
|
2015-11-11 10:24:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
if len(sys.argv) < 4:
|
2017-08-09 12:52:53 +00:00
|
|
|
print('Usage: ', sys.argv[0], ' csv bufr_in bufr_out', file=sys.stderr)
|
2015-11-11 10:24:37 +00:00
|
|
|
sys.exit(1)
|
2015-11-11 17:11:55 +00:00
|
|
|
|
|
|
|
csv_filename = sys.argv[1]
|
2015-11-11 10:24:37 +00:00
|
|
|
input_filename = sys.argv[2]
|
2015-11-11 17:11:55 +00:00
|
|
|
output_filename = sys.argv[3]
|
2015-11-11 10:24:37 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
example(csv_filename, input_filename, output_filename)
|
2016-06-01 15:15:50 +00:00
|
|
|
except CodesInternalError as err:
|
2015-11-11 10:24:37 +00:00
|
|
|
if VERBOSE:
|
|
|
|
traceback.print_exc(file=sys.stderr)
|
|
|
|
else:
|
2016-06-14 10:55:48 +00:00
|
|
|
sys.stderr.write(err.msg + '\n')
|
2015-11-11 10:24:37 +00:00
|
|
|
|
|
|
|
return 1
|
|
|
|
|
2017-08-09 13:16:58 +00:00
|
|
|
|
2015-11-11 10:24:37 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main())
|