Esempio n. 1
0
 def message_set(self, item: str, value: T.Any) -> None:
     arr = isinstance(
         value, (np.ndarray, T.Sequence)) and not isinstance(value, str)
     if arr:
         eccodes.codes_set_array(self.codes_id, item, value)
     else:
         eccodes.codes_set(self.codes_id, item, value)
Esempio n. 2
0
 def message_set(self, item, value):
     # type: (str, T.Any) -> None
     set_array = isinstance(value, T.Sequence) and not isinstance(value, (str, bytes))
     if set_array:
         eccodes.codes_set_array(self.codes_id, item, value)
     else:
         eccodes.codes_set(self.codes_id, item, value)
Esempio n. 3
0
def test_bufr_encode(tmpdir):
    ibufr = eccodes.codes_bufr_new_from_samples("BUFR3_local_satellite")
    eccodes.codes_set_array(ibufr, "inputDelayedDescriptorReplicationFactor",
                            (4, ))
    eccodes.codes_set(ibufr, "masterTableNumber", 0)
    eccodes.codes_set(ibufr, "bufrHeaderSubCentre", 0)
    eccodes.codes_set(ibufr, "bufrHeaderCentre", 98)
    eccodes.codes_set(ibufr, "updateSequenceNumber", 0)
    eccodes.codes_set(ibufr, "dataCategory", 12)
    eccodes.codes_set(ibufr, "dataSubCategory", 139)
    eccodes.codes_set(ibufr, "masterTablesVersionNumber", 13)
    eccodes.codes_set(ibufr, "localTablesVersionNumber", 1)
    eccodes.codes_set(ibufr, "numberOfSubsets", 492)
    eccodes.codes_set(ibufr, "localNumberOfObservations", 492)
    eccodes.codes_set(ibufr, "satelliteID", 4)
    eccodes.codes_set(ibufr, "observedData", 1)
    eccodes.codes_set(ibufr, "compressedData", 1)
    eccodes.codes_set(ibufr, "unexpandedDescriptors", 312061)
    eccodes.codes_set(ibufr, "pixelSizeOnHorizontal1", 1.25e04)
    eccodes.codes_set(ibufr, "orbitNumber", 31330)
    eccodes.codes_set(ibufr, "#1#beamIdentifier", 1)
    eccodes.codes_set(ibufr, "#4#likelihoodComputedForSolution",
                      eccodes.CODES_MISSING_DOUBLE)
    eccodes.codes_set(ibufr, "pack", 1)
    output = tmpdir.join("test_bufr_encode.bufr")
    with open(str(output), "wb") as fout:
        eccodes.codes_write(ibufr, fout)
    eccodes.codes_release(ibufr)
Esempio n. 4
0
def test_grib_float_array():
    gid = eccodes.codes_grib_new_from_samples("regular_ll_sfc_grib2")
    for ftype in (float, np.float16, np.float32, np.float64):
        values = np.ones((100000, ), ftype)
        eccodes.codes_set_array(gid, "values", values)
        assert (eccodes.codes_get_values(gid) == 1.0).all()
        eccodes.codes_set_values(gid, values)
        assert (eccodes.codes_get_values(gid) == 1.0).all()
Esempio n. 5
0
def test_bufr_set_string_array():
    ibufr = eccodes.codes_bufr_new_from_samples("BUFR3_local_satellite")
    eccodes.codes_set(ibufr, "numberOfSubsets", 3)
    eccodes.codes_set(ibufr, "unexpandedDescriptors", 307022)
    inputVals = ("ARD2-LPTR", "EPFL-LPTR", "BOU2-LPTR")
    eccodes.codes_set_array(ibufr, "stationOrSiteName", inputVals)
    eccodes.codes_set(ibufr, "pack", 1)
    outputVals = eccodes.codes_get_string_array(ibufr, "stationOrSiteName")
    assert len(outputVals) == 3
    assert outputVals[0] == "ARD2-LPTR"
    assert outputVals[1] == "EPFL-LPTR"
    assert outputVals[2] == "BOU2-LPTR"
    eccodes.codes_release(ibufr)
Esempio n. 6
0
def save_test_data(path):
    """Save the test file to the indicated directory."""
    import eccodes as ec
    filepath = os.path.join(path, FILENAME)
    with open(filepath, "wb") as f:
        for m in [MSG]:
            buf = ec.codes_bufr_new_from_samples('BUFR4_local_satellite')
            for key in m:
                val = m[key]
                if np.isscalar(val):
                    ec.codes_set(buf, key, val)
                else:
                    ec.codes_set_array(buf, key, val)
            ec.codes_set(buf, 'pack', 1)
            ec.codes_write(buf, f)
            ec.codes_release(buf)
    return filepath
Esempio n. 7
0
    def __setitem__(self, key, value):
        """
        Set value associated with key.

        Iterables and scalars are handled intelligently.
        """
        if isinstance(key, str):
            if hasattr(value, "__iter__") and not isinstance(value, str):
                eccodes.codes_set_array(self.codes_id, key, value)
            else:
                eccodes.codes_set(self.codes_id, key, value)
        else:
            if len(key) != len(value):
                raise ValueError(
                    "Key array must have same size as value array")
            eccodes.codes_set_key_vals(
                self.codes_id,
                ",".join([
                    str(key[i]) + "=" + str(value[i]) for i in range(len(key))
                ]),
            )
Esempio n. 8
0
    def __init__(self, filename, with_adef=False, rect_lon='default'):
        """Initialize by mocking test data for testing the SEVIRI L2 BUFR reader."""
        import eccodes as ec

        from satpy.readers.seviri_l2_bufr import SeviriL2BufrFileHandler
        self.buf1 = ec.codes_bufr_new_from_samples('BUFR4_local_satellite')
        ec.codes_set(self.buf1, 'unpack', 1)
        # write the bufr test data twice as we want to read in and the concatenate the data in the reader
        # 55 id corresponds to METEOSAT 8`
        ec.codes_set(self.buf1, 'satelliteIdentifier', 55)
        ec.codes_set_array(self.buf1, 'latitude', LAT)
        ec.codes_set_array(self.buf1, 'latitude', LAT)
        ec.codes_set_array(self.buf1, 'longitude', LON)
        ec.codes_set_array(self.buf1, 'longitude', LON)
        ec.codes_set_array(self.buf1, '#1#brightnessTemperature', DATA)
        ec.codes_set_array(self.buf1, '#1#brightnessTemperature', DATA)

        self.m = mock.mock_open()
        # only our offline product contain MPEF product headers so we get the metadata from there
        if ('BUFRProd' in filename):
            with mock.patch(
                    'satpy.readers.seviri_l2_bufr.np.fromfile') as fromfile:
                fromfile.return_value = MPEF_PRODUCT_HEADER
                with mock.patch('satpy.readers.seviri_l2_bufr.recarray2dict'
                                ) as recarray2dict:
                    recarray2dict.side_effect = (lambda x: x)
                    self.fh = SeviriL2BufrFileHandler(
                        filename,
                        FILENAME_INFO2,
                        FILETYPE_INFO,
                        with_area_definition=with_adef,
                        rectification_longitude=rect_lon)
                    self.fh.mpef_header = MPEF_PRODUCT_HEADER

        else:
            # No Mpef Header  so we get the metadata from the BUFR messages
            with mock.patch('satpy.readers.seviri_l2_bufr.open',
                            self.m,
                            create=True):
                with mock.patch('eccodes.codes_bufr_new_from_file',
                                side_effect=[
                                    self.buf1, None, self.buf1, None,
                                    self.buf1, None
                                ]) as ec1:
                    ec1.return_value = ec1.side_effect
                    with mock.patch('eccodes.codes_set') as ec2:
                        ec2.return_value = 1
                        with mock.patch('eccodes.codes_release') as ec5:
                            ec5.return_value = 1
                            self.fh = SeviriL2BufrFileHandler(
                                filename,
                                FILENAME_INFO,
                                FILETYPE_INFO,
                                with_area_definition=with_adef,
                                rectification_longitude=rect_lon)
Esempio n. 9
0
    def seviri_l2_bufr_test(self, ):
        """Test the SEVIRI BUFR handler."""
        from satpy.readers.seviri_l2_bufr import SeviriL2BufrFileHandler
        import eccodes as ec
        buf1 = ec.codes_bufr_new_from_samples('BUFR4_local_satellite')
        ec.codes_set(buf1, 'unpac' 'k', 1)
        samp1 = np.random.uniform(low=250, high=350, size=(128, ))
        samp2 = np.random.uniform(low=-60, high=60, size=(128, ))
        samp3 = np.random.uniform(low=10, high=60, size=(128, ))
        # write the bufr test data twice as we want to read in and the concatenate the data in the reader
        ec.codes_set_array(buf1, '#1#brightnessTemperature', samp1)
        ec.codes_set_array(buf1, '#1#brightnessTemperature', samp1)
        ec.codes_set_array(buf1, 'latitude', samp2)
        ec.codes_set_array(buf1, 'latitude', samp2)
        ec.codes_set_array(buf1, 'longitude', samp3)
        ec.codes_set_array(buf1, 'longitude', samp3)

        m = mock.mock_open()
        with mock.patch(
                'satpy.readers.seviri_l2_bufr.np.fromfile') as fromfile:
            fromfile.return_value = MPEF_PRODUCT_HEADER
            with mock.patch('satpy.readers.seviri_l2_bufr.recarray2dict'
                            ) as recarray2dict:
                recarray2dict.side_effect = (lambda x: x)
                fh = SeviriL2BufrFileHandler(None, {}, FILETYPE_INFO)
                fh.mpef_header = MPEF_PRODUCT_HEADER
                with mock.patch('satpy.readers.seviri_l2_bufr.open',
                                m,
                                create=True):
                    with mock.patch('eccodes.codes_bufr_new_from_file',
                                    side_effect=[
                                        buf1, buf1, None, buf1, buf1, None,
                                        buf1, buf1, None
                                    ]) as ec1:
                        ec1.return_value = ec1.side_effect
                        with mock.patch('eccodes.codes_set') as ec2:
                            ec2.return_value = 1
                            with mock.patch('eccodes.codes_release') as ec5:
                                ec5.return_value = 1
                                z = fh.get_dataset(None, DATASET_INFO)
                                # concatenate the original test arrays as
                                # get dataset will have read and concatented the data
                                x1 = np.concatenate((samp1, samp1), axis=0)
                                x2 = np.concatenate((samp2, samp2), axis=0)
                                x3 = np.concatenate((samp3, samp3), axis=0)
                                np.testing.assert_array_equal(z.values, x1)
                                np.testing.assert_array_equal(
                                    z.coords['latitude'].values, x2)
                                np.testing.assert_array_equal(
                                    z.coords['longitude'].values, x3)
                                self.assertEqual(
                                    z.attrs['platform_name'],
                                    DATASET_ATTRS['platform_name'])
                                self.assertEqual(z.attrs['ssp_lon'],
                                                 DATASET_ATTRS['ssp_lon'])
                                self.assertEqual(z.attrs['seg_size'],
                                                 DATASET_ATTRS['seg_size'])
Esempio n. 10
0
    def seviri_l2_bufr_test(self, filename):
        """Test the SEVIRI BUFR handler."""
        from satpy.readers.seviri_l2_bufr import SeviriL2BufrFileHandler
        import eccodes as ec
        buf1 = ec.codes_bufr_new_from_samples('BUFR4_local_satellite')
        ec.codes_set(buf1, 'unpack', 1)
        samp1 = np.random.uniform(low=250, high=350, size=(128, ))
        # write the bufr test data twice as we want to read in and the concatenate the data in the reader
        # 55 id corresponds to METEOSAT 8
        ec.codes_set(buf1, 'satelliteIdentifier', 55)
        ec.codes_set_array(buf1, '#1#brightnessTemperature', samp1)
        ec.codes_set_array(buf1, '#1#brightnessTemperature', samp1)

        m = mock.mock_open()
        # only our offline product contain MPEF product headers so we get the metadata from there
        if ('BUFRProd' in filename):
            with mock.patch(
                    'satpy.readers.seviri_l2_bufr.np.fromfile') as fromfile:
                fromfile.return_value = MPEF_PRODUCT_HEADER
                with mock.patch('satpy.readers.seviri_l2_bufr.recarray2dict'
                                ) as recarray2dict:
                    recarray2dict.side_effect = (lambda x: x)
                    fh = SeviriL2BufrFileHandler(filename, FILENAME_INFO2,
                                                 FILETYPE_INFO)
                    fh.mpef_header = MPEF_PRODUCT_HEADER

        else:
            # No Mpef Header  so we get the metadata from the BUFR messages
            with mock.patch('satpy.readers.seviri_l2_bufr.open',
                            m,
                            create=True):
                with mock.patch(
                        'eccodes.codes_bufr_new_from_file',
                        side_effect=[buf1, None, buf1, None, buf1,
                                     None]) as ec1:
                    ec1.return_value = ec1.side_effect
                    with mock.patch('eccodes.codes_set') as ec2:
                        ec2.return_value = 1
                        with mock.patch('eccodes.codes_release') as ec5:
                            ec5.return_value = 1
                            fh = SeviriL2BufrFileHandler(
                                filename, FILENAME_INFO, FILETYPE_INFO)

        with mock.patch('satpy.readers.seviri_l2_bufr.open', m, create=True):
            with mock.patch('eccodes.codes_bufr_new_from_file',
                            side_effect=[buf1, buf1, None]) as ec1:
                ec1.return_value = ec1.side_effect
                with mock.patch('eccodes.codes_set') as ec2:
                    ec2.return_value = 1
                    with mock.patch('eccodes.codes_release') as ec5:
                        ec5.return_value = 1
                        z = fh.get_dataset(None, DATASET_INFO)
                        # concatenate the original test arrays as
                        # get dataset will have read and concatented the data
                        x1 = np.concatenate((samp1, samp1), axis=0)
                        np.testing.assert_array_equal(z.values, x1)
                        self.assertEqual(z.attrs['platform_name'],
                                         DATASET_ATTRS['platform_name'])
                        self.assertEqual(z.attrs['ssp_lon'],
                                         DATASET_ATTRS['ssp_lon'])
                        self.assertEqual(z.attrs['seg_size'],
                                         DATASET_ATTRS['seg_size'])