def upgrade_bufr_file(input_bufr_file, output_bufr_file):
    #  #[
    """
    an example routine to demonstrate how to read a BUFR message,
    upgrade it's edition number, and write it to an output BUFR file
    """
    # get 2 instances of the RawBUFRFile class
    rbf_in = RawBUFRFile()
    rbf_out = RawBUFRFile()

    # open the file for reading, count nr of BUFR messages in it
    # and store its content in memory, together with
    # an array of pointers to the start and end of each BUFR message
    rbf_in.open(input_bufr_file, 'rb')

    # open the file for writing
    rbf_out.open(output_bufr_file, 'wb')

    # extract the number of BUFR messages from the file
    num_msgs = rbf_in.get_num_bufr_msgs()

    for msg_nr in range(1, num_msgs+1):
        raw_msg, section_sizes, section_start_locations = \
                 rbf_in.get_raw_bufr_msg(msg_nr)
        bufr_obj = BUFRInterfaceECMWF(raw_msg, section_sizes,
                                      section_start_locations,
                                      verbose=True)

        bufr_obj.decode_sections_012()
        bufr_obj.setup_tables()
        bufr_obj.decode_data()

        nsub = bufr_obj.get_num_subsets()
        n_exp_descr = len(bufr_obj.values)/nsub
        bufr_obj.fill_descriptor_list(nr_of_expanded_descriptors=n_exp_descr)

        bufr_obj.ksec0[3-1] = 4 # set bufr edition to 4
        bufr_obj.ktdlst = bufr_obj.get_descriptor_list()

        # extract delayed replication factors
        delayed_repl_data = bufr_obj.derive_delayed_repl_factors()

        # fill the list of replication factors
        bufr_obj.fill_delayed_repl_data(delayed_repl_data)

        # activate this one if the encoding crashes without clear cause:
        # bufr_obj.estimated_num_bytes_for_encoding = 25000

        # encode the data
        bufr_obj.encode_data(bufr_obj.values, bufr_obj.cvals)
        print 'Encode BUFR msg %i' % msg_nr

        rbf_out.write_raw_bufr_msg(bufr_obj.encoded_message)

    # close the file
    rbf_in.close()
    rbf_out.close()
def encode(output_bufr_file, RadarData, WMOID):

    # Get the data for the wind profilers

    WindComponentData = RadarData.getWindComponents()

    GateSpaceWidths = RadarData.getGateSpace()

    if len(GateSpaceWidths) > 1:

        MaxWidth = 0
        for Width in GateSpaceWidths:

            if Width > MaxWidth:
                MaxWidth = Width
    else:

        MaxWidth = GateSpaces[0]

    print('Using Pulse Mode ' + str(MaxWidth))

    for DateTime in WindComponentData:

        print('Processing ' + str(DateTime))

        print(MaxWidth)
        print(WindComponentData[DateTime].keys())

        if MaxWidth in WindComponentData[DateTime].keys():
            print('Processing high mode only. Pulse ' + str(MaxWidth)
                  + 'ns')

            WindComponents = WindComponentData[DateTime][MaxWidth]

            # Create tge buffer for the hour block of data.

            bufr = BUFRInterfaceECMWF(verbose=True)

            # fill sections 0, 1, 2 and 3 in the BUFR table

            num_subsets = 1
            bufr.fill_sections_0123(  # ECMWF
                                      # wind profiler . Also know as Message Type (Table A)
                                      # Message sub-type
                                      # L2B processing facility
                bufr_code_centre=59,
                bufr_obstype=2,
                bufr_subtype=7,
                bufr_table_local_version=1,
                bufr_table_master=0,
                bufr_table_master_version=3,
                bufr_code_subcentre=0,
                num_subsets=num_subsets,
                bufr_compression_flag=0,
                )

            bufr.setup_tables()

        # define a descriptor list

            template = BufrTemplate()

            print('adding {0} descriptors'.format(10))

            template.add_descriptors(
                WMO_BLOCK_NUM,
                WMO_STATION_NUM,
                DD_LATITUDE_COARSE_ACCURACY,
                DD_LONGITUDE_COARSE_ACCURACY,
                STATION_HEIGHT,
                YEAR,
                MONTH,
                MDAY,
                HOUR,
                MIN,
                TIME_SIGNIFICANCE,
                TIME_PERIOD,
                DD_WIND_SPEED,
                DD_WIND_DIR,
                DD_PRESSURE,
                DD_TEMPERATURE,
                RAINFALL_SWE,
                DD_RELATIVE_HUMD,
                HEIGHT_INCREMENT,
                WIND_PROFILER_SUB_MODE_INFO,
                HEIGHT_INCREMENT,
                HEIGHT_INCREMENT,
                HEIGHT_INCREMENT,
                )

        # delay replication for the next 10 descriptors

            template.add_replicated_descriptors(
                len(WindComponents),
                WIND_PROFILER_MODE_INFO,
                WIND_PROFILER_QC_RESULTS,
                TOTAL_NUMBER,
                WIND_U_COMPONENT,
                WIND_V_COMPONENT,
                STD_DEV_HORIZONTAL_WIND_SPEED,
                TOTAL_NUMBER,
                RADAR_BACK_SCATTER,
                WIND_W_COMPONENT,
                STD_DEV_VERTICAL_SPEED,
                )

            bufr.register_and_expand_descriptors(template)

        # activate this one if the encoding crashes without clear cause:
        # bufr.estimated_num_bytes_for_encoding = 25000

        # retrieve the length of the expanded descriptor list

            exp_descr_list_length = bufr.ktdexl

        # fill the values array with some dummy varying data

            num_values = exp_descr_list_length

            values = np.zeros(num_values, dtype=np.float64)  # this is the default

        # note: these two must be identical for now, otherwise the
        # python to fortran interface breaks down. This also ofcourse is the
        # cause of the huge memory use of cvals in case num_values is large.

            num_cvalues = num_values
            cvals = np.zeros((num_cvalues, 80), dtype=np.character)

        # note that python starts counting with 0, unlike fortran,
            # so there is no need to take (subset-1)
        #     i = subset*exp_descr_list_length

            values[0] = WMOID[0:2]  # WMO Block Number
            values[1] = WMOID[2:5]  # WMO Station #

            values[2] = RadarData.getLatitude()  # Latitude
            values[3] = RadarData.getLongitude()
            values[4] = RadarData.getElev()  # Elevation of Station (meters)
            values[5] = DateTime.timetuple().tm_year  # year
            values[6] = DateTime.timetuple().tm_mon  # month
            values[7] = DateTime.timetuple().tm_mday  # day
            values[8] = DateTime.timetuple().tm_hour  # hour
            values[9] = 0  # minute
            values[10] = 2  # Time Significance
            values[11] = -60  # Time Period
            values[12] = 1  # Wind Speed
            values[13] = 1  # Wind Dir
            values[14] = 1  # Pressure
            values[15] = 1  # Temperature
            values[16] = .2  # Rainfall
            values[17] = 1  # Realative Humidty
            GateSpace = int(MaxWidth) * 1.e-9 * 3.e+8 / 2.
            values[18] = GateSpace  # Height Increment
            values[19] = 0  # Wind Profiler Sub Mode
            values[20] = GateSpace  # Height Increment
            values[21] = GateSpace  # Height Increment
            values[22] = GateSpace  # Height Increment

            print('Number of gates ' + str(len(WindComponents)))

            for i in range(0, len(WindComponents)):

                for t in range(0, 10):

                    # Calulcate the correct index in the BUFR

                    rec = i * 10 + 23 + t

                    if WindComponents[i][0] <= 1000 \
                        and WindComponents[i][1] <= 1000:
                        WindRadians = math.radians(WindComponents[i][1])
                        VelocityU = -WindComponents[i][0] \
                            * math.sin(WindRadians)
                        VelocityV = -WindComponents[i][0] \
                            * math.cos(WindRadians)
                        TotalNumber = WindComponents[i][2]
                        SnrDB = WindComponents[i][3]
                    else:

                        VelocityU = VelocityV = TotalNumbe = SnrDB = -99
                        values[rec] = float('NaN')  # level mode
                        continue

                    if rec % 10 == 3:
                        values[rec] = 1  # level mode

                    if rec % 10 == 4:
                        values[rec] = 0  # Quality Control test

                    if rec % 10 == 5:
                        values[rec] = TotalNumber  # Total Number (with respect to accumlation or average)

                    if rec % 10 == 6:
                        values[rec] = VelocityU  # U-Component

                    if rec % 10 == 7:
                        values[rec] = VelocityV  # V-Component

                    if rec % 10 == 8:
                        values[rec] = 0.000  # Std Deviation of horizontal wind speed

                    if rec % 10 == 9:
                        values[rec] = TotalNumber  # Total Number  (with respect to accumlation or average)

                    if rec % 10 == 0:
                        values[rec] = SnrDB / 100  # Radar Back Scatter (Peak Power)  x100

                    if rec % 10 == 1:
                        values[rec] = 0.000  # W-Component  x100

                    if rec % 10 == 2:
                        values[rec] = 0.000  # Std Deviation of vertical wind

            # do the encoding to binary format

            bufr.encode_data(values, cvals)

            HeaderString = '''

287

IUAK01 PANC %02d%02d00

''' \
                % (DateTime.timetuple().tm_mday,
                   DateTime.timetuple().tm_hour)

            if not os.path.exists(OutputPath):
                os.makedirs(OutputPath)

            OutputFile = \
                '%s/IUPTO2_%s_%02d%02d00_216234297.bufr.%04d%02d%02d%02d' \
                % (
                OutputPath,
                RadarData.getSiteID().upper(),
                DateTime.timetuple().tm_mon,
                DateTime.timetuple().tm_mday,
                DateTime.timetuple().tm_year,
                DateTime.timetuple().tm_mon,
                DateTime.timetuple().tm_mday,
                DateTime.timetuple().tm_hour,
                )

             # Remove file if exsists

            if os.path.exists(OutputFile):
                os.remove(OutputFile)

            bf1 = open(OutputFile, 'ab')
            bf1.write(HeaderString)
            bf1.close()

            # get an instance of the RawBUFRFile class

            bf1 = RawBUFRFile()

            # open the file for writing

            bf1.open(OutputFile, 'ab')

            # write the encoded BUFR message

            bf1.write_raw_bufr_msg(bufr.encoded_message)

            # close the file

            bf1.close()

            #  #]

            print('succesfully written BUFR encoded data to file: ',
                  OutputFile)
def encoding_example(output_bufr_file):
    #  #[
    """
    wrap the example in a function to circumvent the pylint
    convention of requiring capitals for constants in the global
    scope (since most of these variables are not constants at all))
    """

    bufr = BUFRInterfaceECMWF(verbose=True)

    # fill sections 0, 1, 2 and 3
    num_subsets = 4
    bufr.fill_sections_0123(bufr_code_centre=98, # ECMWF
                            bufr_obstype=3, # sounding
                            bufr_subtype=251, # L1B
                            bufr_table_local_version=1,
                            bufr_table_master=0,
                            bufr_table_master_version=15,
                            bufr_code_subcentre=0, # L2B processing facility
                            num_subsets=num_subsets,
                            bufr_compression_flag=0)
    # 64=compression/0=no compression

    # determine information from sections 0123 to construct the BUFR table
    # names expected by the ECMWF BUFR library and create symlinks to the
    # default tables if needed
    # NOTE: these custom BUFR tables have been created by the
    #       create_bufr_tables.py example script
    bufr.setup_tables(table_b_to_use='B_my_test_BUFR_table.txt',
                      table_c_to_use='C_my_test_BUFR_table.txt',
                      table_d_to_use='D_my_test_BUFR_table.txt')

    # define a descriptor list
    template = BufrTemplate(verbose=True)

    template.add_descriptors(DD_B_048001,        # 0
                             DD_B_048002,        # 1
                             DD_D_348001)        # 2

    bufr.register_and_expand_descriptors(template)

    # activate this one if the encoding crashes without clear cause:
    # bufr.estimated_num_bytes_for_encoding = 25000

    # retrieve the length of the expanded descriptor list
    exp_descr_list_length = bufr.ktdexl
    print("exp_descr_list_length = ", exp_descr_list_length)

    # fill the values array with some dummy varying data
    num_values = exp_descr_list_length*num_subsets
    values = np.zeros(num_values, dtype=np.float64) # this is the default

    # note: these two must be identical for now, otherwise the
    # python to fortran interface breaks down. This also ofcourse is the
    # cause of the huge memory use of cvals in case num_values is large.
    num_cvalues = num_values
    cvals = np.zeros((num_cvalues, 80), dtype=np.character)

    for subset in range(num_subsets):
        # note that python starts counting with 0, unlike fortran,
        # so there is no need to take (subset-1)
        i = subset*exp_descr_list_length

        # fill the message with some dummy data
        values[i] = 1.2515 + 0.0011*subset
        i = i+1
        values[i] = (3.4562 + 0.0012*subset)*1.e-9
        i = i+1
        values[i] = 1.2625 + 0.0003*subset
        i = i+1
        values[i] = (3.4561 + 0.0014*subset)*1.e-9

    # do the encoding to binary format
    bufr.encode_data(values, cvals)

    # get an instance of the RawBUFRFile class
    bf1 = RawBUFRFile()
    # open the file for writing
    bf1.open(output_bufr_file, 'wb')
    # write the encoded BUFR message
    bf1.write_raw_bufr_msg(bufr.encoded_message)
    # close the file
    bf1.close()
def encoding_example(output_bufr_file):
    #  #[
    """
    wrap the example in a function to circumvent the pylint
    convention of requiring capitals for constants in the global
    scope (since most of these variables are not constants at all))
    """

    bufr = BUFRInterfaceECMWF(verbose=True)

    # fill sections 0, 1, 2 and 3
    num_subsets = 4
    bufr.fill_sections_0123(bufr_code_centre=98, # ECMWF
                            bufr_obstype=3, # sounding
                            bufr_subtype=251, # L1B
                            bufr_table_local_version=1,
                            bufr_table_master=0,
                            bufr_table_master_version=15,
                            bufr_code_subcentre=0, # L2B processing facility
                            num_subsets=num_subsets,
                            bufr_compression_flag=0)
    # 64=compression/0=no compression

    # determine information from sections 0123 to construct the BUFR table
    # names expected by the ECMWF BUFR library and create symlinks to the
    # default tables if needed
    bufr.setup_tables()

    # define a descriptor list
    template = BufrTemplate()

    template.add_descriptors(DD_D_DATE_YYYYMMDD, # 0
                             DD_D_TIME_HHMM)     # 1

    # delay replication for the next 2 descriptors
    # allow at most 2 delayed replications
    template.add_delayed_replic_descriptors(2,
                                            DD_PRESSURE,
                                            DD_TEMPERATURE)

    # replicate the next 2 descriptors 3 times
    template.add_replicated_descriptors(3,
                                        DD_LATITUDE_HIGH_ACCURACY,
                                        DD_LONGITUDE_HIGH_ACCURACY)

    bufr.register_and_expand_descriptors(template)

    # activate this one if the encoding crashes without clear cause:
    # bufr.estimated_num_bytes_for_encoding = 25000

    # retrieve the length of the expanded descriptor list
    exp_descr_list_length = bufr.ktdexl
    print "exp_descr_list_length = ", exp_descr_list_length

    # fill the values array with some dummy varying data
    num_values = exp_descr_list_length*num_subsets
    values = np.zeros(num_values, dtype=np.float64) # this is the default

    # note: these two must be identical for now, otherwise the
    # python to fortran interface breaks down. This also ofcourse is the
    # cause of the huge memory use of cvals in case num_values is large.
    num_cvalues = num_values
    cvals = np.zeros((num_cvalues, 80), dtype=np.character)

    for subset in range(num_subsets):
        # note that python starts counting with 0, unlike fortran,
        # so there is no need to take (subset-1)
        i = subset*exp_descr_list_length

        values[i] = 1999 # year
        i = i+1
        values[i] = 12 # month
        i = i+1
        values[i] = 31 # day
        i = i+1
        values[i] = 23 # hour
        i = i+1
        values[i] = 59 - subset # minute
        i = i+1
        values[i] = 2 # delayed replication factor
        # this delayed replication factor determines the actual number
        # of values to be stored for this particular subset
        # even if it is less then the number given in kdata above !
        for repl in range(2):
            i = i+1
            values[i] = 1013.e2 - 100.e2*subset+i+repl # pressure [pa]
            i = i+1
            values[i] = 273.15 - 10.*subset+i+repl # temperature [K]
        for repl in range(3):
            i = i+1
            values[i] = 51.82 + 0.05*subset+i+repl # latitude
            i = i+1
            values[i] = 5.25 + 0.1*subset+i+repl # longitude

    # do the encoding to binary format
    bufr.encode_data(values, cvals)

    # get an instance of the RawBUFRFile class
    bf1 = RawBUFRFile()
    # open the file for writing
    bf1.open(output_bufr_file, 'wb')
    # write the encoded BUFR message
    bf1.write_raw_bufr_msg(bufr.encoded_message)
    # close the file
    bf1.close()
    values[i] = num_repetitions[subset]
    i = i+1

    # in encoding, just one copy should be [rpvided of repeated sequences
    for k in range(1):

        # fill dummy var
        values[i] = 34567.
        i = i+1

# debug
print('values[:25]: ', values[:25].tolist()) #numpy.where(values != 0)]))
print('values[-25:]: ', values[-25:].tolist())

# do the encoding to binary format
bufr.encode_data(values, cvals)

# get an instance of the RawBUFRFile class
bf1 = RawBUFRFile()

output_bufr_file = 'dummy_bufr_file.bfr'
# open the file for writing
bf1.open(output_bufr_file, 'wb')
# write the encoded BUFR message
bf1.write_raw_bufr_msg(bufr.encoded_message)
# close the file
bf1.close()

##############################
# reopen the BUFR file as test
##############################
示例#6
0
        i = i + 1
        repl_counts.append(num_replications2[subset][j])

        for l in range(num_replications2[subset][j]):

            # fill dummy var
            values[i] = 45678.
            i = i + 1

# debug
print('values: ', list(values))  #numpy.where(values != 0)]))

# do the encoding to binary format
bufr.kdata = numpy.array(repl_counts)
print('bufr.kdata = ', bufr.kdata)
bufr.encode_data(values, cvals)

# get an instance of the RawBUFRFile class
bf1 = RawBUFRFile()

output_bufr_file = 'dummy_bufr_file.bfr'
# open the file for writing
bf1.open(output_bufr_file, 'wb')
# write the encoded BUFR message
bf1.write_raw_bufr_msg(bufr.encoded_message)
# close the file
bf1.close()

##############################
# reopen the BUFR file as test
##############################
def create_bufr_file(output_bufr_file, template):
    #  #[ create bufr file
    '''
    and use this BUFR template to create a test BUFR message
    '''
    from pybufr_ecmwf.bufr_interface_ecmwf import BUFRInterfaceECMWF
    bufr = BUFRInterfaceECMWF(verbose=True)

    # fill sections 0, 1, 2 and 3
    num_subsets = 2
    bufr.fill_sections_0123(bufr_code_centre=98, # ECMWF
                            bufr_obstype=3, # sounding
                            bufr_subtype=253, # L2B
                            bufr_table_local_version=1,
                            bufr_table_master=0,
                            bufr_table_master_version=15,
                            bufr_code_subcentre=0, # L2B processing facility
                            num_subsets=num_subsets,
                            bufr_compression_flag=0)
                            # 64=compression/0=no compression

    # determine information from sections 0123 to construct the BUFR table
    # names expected by the ECMWF BUFR library and create symlinks to the
    # default tables if needed
    bufr.setup_tables(table_b_to_use='B'+TABLE_NAME,
                      table_d_to_use='D'+TABLE_NAME)
    bufr.register_and_expand_descriptors(template)

    # activate this one if the encoding crashes without clear cause:
    # bufr.estimated_num_bytes_for_encoding = 25000

    # retrieve the length of the expanded descriptor list
    exp_descr_list_length = bufr.ktdexl
    print "exp_descr_list_length = ", exp_descr_list_length

    # fill the values array with some dummy varying data
    num_values = num_subsets*bufr.max_nr_expanded_descriptors
    values = numpy.zeros(num_values, dtype=numpy.float64) # this is the default

    # note: these two must be identical for now, otherwise the
    # python to fortran interface breaks down. This also ofcourse is the
    # cause of the huge memory use of cvals in case num_values is large.
    num_cvalues = num_values
    cvals = numpy.zeros((num_cvalues, 80), dtype=numpy.character)
    cvals_index = 0

    repl_counts = []

    for subset in range(num_subsets):
        # note that python starts counting with 0, unlike fortran,
        # so there is no need to take (subset-1)

        print 'subset,exp_descr_list_length = ', subset, exp_descr_list_length
        i = subset*exp_descr_list_length

        # fill the message with some dummy data

        # fill year, month, day
        for val in [2014, 3, 19]: # fill the header
            values[i] = val+subset
            i += 1

        # fill prod_name
        txt = 'filename{}.txt'.format(subset)
        cvals[cvals_index, :] = ' '
        for icval, cval in enumerate(txt):
            cvals[cvals_index, icval] = cval

        # values[i] = cvals_index * 1000 + 64 # len(txt)
        values[i] = (cvals_index+1) * 1000 + len(txt)
        i += 1
        cvals_index = cvals_index + 1

        for val in [5.1+0.1*subset, 55.2-0.01*subset, 23., 45., 73., 82.]:
            bufr.verify_in_range(i, val)
            values[i] = val
            i += 1

        if USE_DELAYED_REPLICATION:
            # set actual delayed replication repeats
            num_repl = 3 + 2*subset
            print 'num_repl = ', num_repl
            values[i] = num_repl
            i += 1
            repl_counts.append(num_repl)

            # fill the replicated variable
            for irepl in range(num_repl):
                val = 12.+subset*0.1 + irepl*0.01
                bufr.verify_in_range(i, val)
                values[i] = val
                i += 1

    # do the encoding to binary format
    bufr.kdata = numpy.array(repl_counts)
    print 'bufr.kdata = ', bufr.kdata
    bufr.encode_data(values, cvals)

    print 'DEBUG: values = ', values

    from pybufr_ecmwf.raw_bufr_file import RawBUFRFile
    # get an instance of the RawBUFRFile class
    bf1 = RawBUFRFile()

    # open the file for writing
    bf1.open(output_bufr_file, 'wb')
    # write the encoded BUFR message
    bf1.write_raw_bufr_msg(bufr.encoded_message)
    # close the file
    bf1.close()
示例#8
0
def encoding_example(output_bufr_file):
    #  #[
    """
    wrap the example in a function to circumvent the pylint
    convention of requiring capitals for constants in the global
    scope (since most of these variables are not constants at all))
    """

    bufr = BUFRInterfaceECMWF(verbose=True)

    # fill sections 0, 1, 2 and 3
    num_subsets = 4
    bufr.fill_sections_0123(
        bufr_code_centre=98,  # ECMWF
        bufr_obstype=3,  # sounding
        bufr_subtype=251,  # L1B
        bufr_table_local_version=1,
        bufr_table_master=0,
        bufr_table_master_version=15,
        bufr_code_subcentre=0,  # L2B processing facility
        num_subsets=num_subsets,
        bufr_compression_flag=0)
    # 64=compression/0=no compression

    # determine information from sections 0123 to construct the BUFR table
    # names expected by the ECMWF BUFR library and create symlinks to the
    # default tables if needed
    bufr.setup_tables()

    # define a descriptor list
    template = BufrTemplate(verbose=True)

    template.add_descriptors(
        DD_D_DATE_YYYYMMDD,  # 0
        DD_D_TIME_HHMM)  # 1

    # delay replication for the next 2 descriptors
    # allow at most 2 delayed replications
    template.add_delayed_replic_descriptors(2, DD_PRESSURE, DD_TEMPERATURE)

    # replicate the next 2 descriptors 3 times
    template.add_replicated_descriptors(3, DD_LATITUDE_HIGH_ACCURACY,
                                        DD_LONGITUDE_HIGH_ACCURACY)

    bufr.register_and_expand_descriptors(template)

    # activate this one if the encoding crashes without clear cause:
    # bufr.estimated_num_bytes_for_encoding = 25000

    # retrieve the length of the expanded descriptor list
    exp_descr_list_length = bufr.ktdexl
    print("exp_descr_list_length = ", exp_descr_list_length)

    # fill the values array with some dummy varying data
    num_values = exp_descr_list_length * num_subsets
    values = np.zeros(num_values, dtype=np.float64)  # this is the default

    # note: these two must be identical for now, otherwise the
    # python to fortran interface breaks down. This also ofcourse is the
    # cause of the huge memory use of cvals in case num_values is large.
    num_cvalues = num_values
    cvals = np.zeros((num_cvalues, 80), dtype=np.character)

    for subset in range(num_subsets):
        # note that python starts counting with 0, unlike fortran,
        # so there is no need to take (subset-1)
        i = subset * exp_descr_list_length

        values[i] = 1999  # year
        i = i + 1
        values[i] = 12  # month
        i = i + 1
        values[i] = 31  # day
        i = i + 1
        values[i] = 23  # hour
        i = i + 1
        values[i] = 59 - subset  # minute
        i = i + 1
        values[i] = 2  # delayed replication factor
        # this delayed replication factor determines the actual number
        # of values to be stored for this particular subset
        # even if it is less then the number given in kdata above !
        for repl in range(2):
            i = i + 1
            values[i] = 1013.e2 - 100.e2 * subset + i + repl  # pressure [pa]
            i = i + 1
            values[i] = 273.15 - 10. * subset + i + repl  # temperature [K]
        for repl in range(3):
            i = i + 1
            values[i] = 51.82 + 0.05 * subset + i + repl  # latitude
            i = i + 1
            values[i] = 5.25 + 0.1 * subset + i + repl  # longitude

    # do the encoding to binary format
    bufr.encode_data(values, cvals)

    # get an instance of the RawBUFRFile class
    bf1 = RawBUFRFile()
    # open the file for writing
    bf1.open(output_bufr_file, 'wb')
    # write the encoded BUFR message
    bf1.write_raw_bufr_msg(bufr.encoded_message)
    # close the file
    bf1.close()
示例#9
0
def create_bufr_file(output_bufr_file, template):
    #  #[ create bufr file
    '''
    and use this BUFR template to create a test BUFR message
    '''
    from pybufr_ecmwf.bufr_interface_ecmwf import BUFRInterfaceECMWF
    bufr = BUFRInterfaceECMWF(verbose=True)

    # fill sections 0, 1, 2 and 3
    num_subsets = 2
    bufr.fill_sections_0123(bufr_code_centre=98, # ECMWF
                            bufr_obstype=3, # sounding
                            bufr_subtype=253, # L2B
                            bufr_table_local_version=1,
                            bufr_table_master=0,
                            bufr_table_master_version=15,
                            bufr_code_subcentre=0, # L2B processing facility
                            num_subsets=num_subsets,
                            bufr_compression_flag=0)
                            # 64=compression/0=no compression

    # determine information from sections 0123 to construct the BUFR table
    # names expected by the ECMWF BUFR library and create symlinks to the
    # default tables if needed
    bufr.setup_tables(table_b_to_use='B'+TABLE_NAME,
                      table_d_to_use='D'+TABLE_NAME)
    bufr.register_and_expand_descriptors(template)

    # activate this one if the encoding crashes without clear cause:
    # bufr.estimated_num_bytes_for_encoding = 25000

    # retrieve the length of the expanded descriptor list
    exp_descr_list_length = bufr.ktdexl
    print "exp_descr_list_length = ", exp_descr_list_length

    # fill the values array with some dummy varying data
    num_values = num_subsets*bufr.max_nr_expanded_descriptors
    values = numpy.zeros(num_values, dtype=numpy.float64) # this is the default

    # note: these two must be identical for now, otherwise the
    # python to fortran interface breaks down. This also ofcourse is the
    # cause of the huge memory use of cvals in case num_values is large.
    num_cvalues = num_values
    cvals = numpy.zeros((num_cvalues, 80), dtype=numpy.character)
    cvals_index = 0

    repl_counts = []

    for subset in range(num_subsets):
        # note that python starts counting with 0, unlike fortran,
        # so there is no need to take (subset-1)

        print 'subset,exp_descr_list_length = ', subset, exp_descr_list_length
        i = subset*exp_descr_list_length

        # fill the message with some dummy data

        # fill year, month, day
        for val in [2014, 3, 19]: # fill the header
            values[i] = val+subset
            i += 1

        # fill prod_name
        # this is not python2.6 compatible
        #txt = 'filename{}.txt'.format(subset)
        txt = 'filename'+str(subset)+'.txt'
        cvals[cvals_index, :] = ' '
        for icval, cval in enumerate(txt):
            cvals[cvals_index, icval] = cval

        # values[i] = cvals_index * 1000 + 64 # len(txt)
        values[i] = (cvals_index+1) * 1000 + len(txt)
        i += 1
        cvals_index = cvals_index + 1

        for val in [5.1+0.1*subset, 55.2-0.01*subset, 23., 45., 73., 82.]:
            bufr.verify_in_range(i, val)
            values[i] = val
            i += 1

        if USE_DELAYED_REPLICATION:
            # set actual delayed replication repeats
            num_repl = 3 + 2*subset
            print 'num_repl = ', num_repl
            values[i] = num_repl
            i += 1
            repl_counts.append(num_repl)

            # fill the replicated variable
            for irepl in range(num_repl):
                val = 12.+subset*0.1 + irepl*0.01
                bufr.verify_in_range(i, val)
                values[i] = val
                i += 1

    # do the encoding to binary format
    bufr.kdata = numpy.array(repl_counts)
    print 'bufr.kdata = ', bufr.kdata
    bufr.encode_data(values, cvals)

    print 'DEBUG: values = ', values

    from pybufr_ecmwf.raw_bufr_file import RawBUFRFile
    # get an instance of the RawBUFRFile class
    bf1 = RawBUFRFile()

    # open the file for writing
    bf1.open(output_bufr_file, 'wb')
    # write the encoded BUFR message
    bf1.write_raw_bufr_msg(bufr.encoded_message)
    # close the file
    bf1.close()
示例#10
0
def encode(output_bufr_file, RadarData, WMOID):

    # Get the data for the wind profilers

    WindComponentData = RadarData.getWindComponents()

    GateSpaceWidths = RadarData.getGateSpace()

    if len(GateSpaceWidths) > 1:

        MaxWidth = 0
        for Width in GateSpaceWidths:

            if Width > MaxWidth:
                MaxWidth = Width
    else:

        MaxWidth = GateSpaces[0]

    print('Using Pulse Mode ' + str(MaxWidth))

    for DateTime in WindComponentData:

        print('Processing ' + str(DateTime))

        print(MaxWidth)
        print(WindComponentData[DateTime].keys())

        if MaxWidth in WindComponentData[DateTime].keys():
            print('Processing high mode only. Pulse ' + str(MaxWidth) + 'ns')

            WindComponents = WindComponentData[DateTime][MaxWidth]

            # Create tge buffer for the hour block of data.

            bufr = BUFRInterfaceECMWF(verbose=True)

            # fill sections 0, 1, 2 and 3 in the BUFR table

            num_subsets = 1
            bufr.fill_sections_0123(  # ECMWF
                # wind profiler . Also know as Message Type (Table A)
                # Message sub-type
                # L2B processing facility
                bufr_code_centre=59,
                bufr_obstype=2,
                bufr_subtype=7,
                bufr_table_local_version=1,
                bufr_table_master=0,
                bufr_table_master_version=3,
                bufr_code_subcentre=0,
                num_subsets=num_subsets,
                bufr_compression_flag=0,
            )

            bufr.setup_tables()

            # define a descriptor list

            template = BufrTemplate()

            print('adding {0} descriptors'.format(10))

            template.add_descriptors(
                WMO_BLOCK_NUM,
                WMO_STATION_NUM,
                DD_LATITUDE_COARSE_ACCURACY,
                DD_LONGITUDE_COARSE_ACCURACY,
                STATION_HEIGHT,
                YEAR,
                MONTH,
                MDAY,
                HOUR,
                MIN,
                TIME_SIGNIFICANCE,
                TIME_PERIOD,
                DD_WIND_SPEED,
                DD_WIND_DIR,
                DD_PRESSURE,
                DD_TEMPERATURE,
                RAINFALL_SWE,
                DD_RELATIVE_HUMD,
                HEIGHT_INCREMENT,
                WIND_PROFILER_SUB_MODE_INFO,
                HEIGHT_INCREMENT,
                HEIGHT_INCREMENT,
                HEIGHT_INCREMENT,
            )

            # delay replication for the next 10 descriptors

            template.add_replicated_descriptors(
                len(WindComponents),
                WIND_PROFILER_MODE_INFO,
                WIND_PROFILER_QC_RESULTS,
                TOTAL_NUMBER,
                WIND_U_COMPONENT,
                WIND_V_COMPONENT,
                STD_DEV_HORIZONTAL_WIND_SPEED,
                TOTAL_NUMBER,
                RADAR_BACK_SCATTER,
                WIND_W_COMPONENT,
                STD_DEV_VERTICAL_SPEED,
            )

            bufr.register_and_expand_descriptors(template)

            # activate this one if the encoding crashes without clear cause:
            # bufr.estimated_num_bytes_for_encoding = 25000

            # retrieve the length of the expanded descriptor list

            exp_descr_list_length = bufr.ktdexl

            # fill the values array with some dummy varying data

            num_values = exp_descr_list_length

            values = np.zeros(num_values,
                              dtype=np.float64)  # this is the default

            # note: these two must be identical for now, otherwise the
            # python to fortran interface breaks down. This also ofcourse is the
            # cause of the huge memory use of cvals in case num_values is large.

            num_cvalues = num_values
            cvals = np.zeros((num_cvalues, 80), dtype=np.character)

            # note that python starts counting with 0, unlike fortran,
            # so there is no need to take (subset-1)
            #     i = subset*exp_descr_list_length

            values[0] = WMOID[0:2]  # WMO Block Number
            values[1] = WMOID[2:5]  # WMO Station #

            values[2] = RadarData.getLatitude()  # Latitude
            values[3] = RadarData.getLongitude()
            values[4] = RadarData.getElev()  # Elevation of Station (meters)
            values[5] = DateTime.timetuple().tm_year  # year
            values[6] = DateTime.timetuple().tm_mon  # month
            values[7] = DateTime.timetuple().tm_mday  # day
            values[8] = DateTime.timetuple().tm_hour  # hour
            values[9] = 0  # minute
            values[10] = 2  # Time Significance
            values[11] = -60  # Time Period
            values[12] = 1  # Wind Speed
            values[13] = 1  # Wind Dir
            values[14] = 1  # Pressure
            values[15] = 1  # Temperature
            values[16] = .2  # Rainfall
            values[17] = 1  # Realative Humidty
            GateSpace = int(MaxWidth) * 1.e-9 * 3.e+8 / 2.
            values[18] = GateSpace  # Height Increment
            values[19] = 0  # Wind Profiler Sub Mode
            values[20] = GateSpace  # Height Increment
            values[21] = GateSpace  # Height Increment
            values[22] = GateSpace  # Height Increment

            print('Number of gates ' + str(len(WindComponents)))

            for i in range(0, len(WindComponents)):

                for t in range(0, 10):

                    # Calulcate the correct index in the BUFR

                    rec = i * 10 + 23 + t

                    if WindComponents[i][0] <= 1000 \
                        and WindComponents[i][1] <= 1000:
                        WindRadians = math.radians(WindComponents[i][1])
                        VelocityU = -WindComponents[i][0] \
                            * math.sin(WindRadians)
                        VelocityV = -WindComponents[i][0] \
                            * math.cos(WindRadians)
                        TotalNumber = WindComponents[i][2]
                        SnrDB = WindComponents[i][3]
                    else:

                        VelocityU = VelocityV = TotalNumbe = SnrDB = -99
                        values[rec] = float('NaN')  # level mode
                        continue

                    if rec % 10 == 3:
                        values[rec] = 1  # level mode

                    if rec % 10 == 4:
                        values[rec] = 0  # Quality Control test

                    if rec % 10 == 5:
                        values[
                            rec] = TotalNumber  # Total Number (with respect to accumlation or average)

                    if rec % 10 == 6:
                        values[rec] = VelocityU  # U-Component

                    if rec % 10 == 7:
                        values[rec] = VelocityV  # V-Component

                    if rec % 10 == 8:
                        values[
                            rec] = 0.000  # Std Deviation of horizontal wind speed

                    if rec % 10 == 9:
                        values[
                            rec] = TotalNumber  # Total Number  (with respect to accumlation or average)

                    if rec % 10 == 0:
                        values[
                            rec] = SnrDB / 100  # Radar Back Scatter (Peak Power)  x100

                    if rec % 10 == 1:
                        values[rec] = 0.000  # W-Component  x100

                    if rec % 10 == 2:
                        values[rec] = 0.000  # Std Deviation of vertical wind

            # do the encoding to binary format

            bufr.encode_data(values, cvals)

            HeaderString = '''

287

IUAK01 PANC %02d%02d00

''' \
                % (DateTime.timetuple().tm_mday,
                   DateTime.timetuple().tm_hour)

            if not os.path.exists(OutputPath):
                os.makedirs(OutputPath)

            OutputFile = \
                '%s/IUPTO2_%s_%02d%02d00_216234297.bufr.%04d%02d%02d%02d' \
                % (
                OutputPath,
                RadarData.getSiteID().upper(),
                DateTime.timetuple().tm_mon,
                DateTime.timetuple().tm_mday,
                DateTime.timetuple().tm_year,
                DateTime.timetuple().tm_mon,
                DateTime.timetuple().tm_mday,
                DateTime.timetuple().tm_hour,
                )

            # Remove file if exsists

            if os.path.exists(OutputFile):
                os.remove(OutputFile)

            bf1 = open(OutputFile, 'ab')
            bf1.write(HeaderString)
            bf1.close()

            # get an instance of the RawBUFRFile class

            bf1 = RawBUFRFile()

            # open the file for writing

            bf1.open(OutputFile, 'ab')

            # write the encoded BUFR message

            bf1.write_raw_bufr_msg(bufr.encoded_message)

            # close the file

            bf1.close()

            #  #]

            print('succesfully written BUFR encoded data to file: ',
                  OutputFile)