示例#1
0
    def test_num_valids(self):
        self.maxDiff = None

        time_series_ds = xr.Dataset(data_vars=dict(
            time=xr.DataArray(pd.date_range(
                start='2010-04-05', periods=5, freq='1D'),
                              dims='time'),
            a=xr.DataArray([np.nan, 5, np.nan, 2, 3], dims='time'),
            b=xr.DataArray([np.nan, 33, np.nan, np.nan, 23], dims='time'),
            c=xr.DataArray([np.nan, 0.2, np.nan, 0.7, np.nan], dims='time')))

        result = _collect_timeseries_result(time_series_ds, {
            'a': 'a',
            'b': 'b',
            'c': 'c'
        },
                                            max_valids=-1)
        self.assertEqual([{
            'a': 5.0,
            'b': 33.0,
            'c': 0.2,
            'time': '2010-04-06T00:00:00Z'
        }, {
            'a': 2.0,
            'b': None,
            'c': 0.7,
            'time': '2010-04-08T00:00:00Z'
        }, {
            'a': 3.0,
            'b': 23.0,
            'c': None,
            'time': '2010-04-09T00:00:00Z'
        }], result)

        result = _collect_timeseries_result(time_series_ds, {
            'a': 'a',
            'b': 'b',
            'c': 'c'
        },
                                            max_valids=2)
        self.assertEqual([{
            'a': 2.0,
            'b': None,
            'c': 0.7,
            'time': '2010-04-08T00:00:00Z'
        }, {
            'a': 3.0,
            'b': 23.0,
            'c': None,
            'time': '2010-04-09T00:00:00Z'
        }], result)
示例#2
0
    def test_count(self):
        self.maxDiff = None

        time_series_ds = xr.Dataset(data_vars=dict(
            time=xr.DataArray(pd.date_range(start='2010-04-05',
                                            periods=3,
                                            freq='1D'),
                              dims='time'),
            c=xr.DataArray([23, 78, 74], dims='time')),
                                    attrs=dict(max_number_of_observations=82))

        result = _collect_timeseries_result(time_series_ds, {'count': 'c'},
                                            max_valids=-1)
        self.assertEqual([{
            'count': 23,
            'count_tot': 82,
            'time': '2010-04-05T00:00:00Z'
        }, {
            'count': 78,
            'count_tot': 82,
            'time': '2010-04-06T00:00:00Z'
        }, {
            'count': 74,
            'count_tot': 82,
            'time': '2010-04-07T00:00:00Z'
        }], result)
示例#3
0
    def test_all_types_converted_correctly(self):
        self.maxDiff = None

        time_series_ds = xr.Dataset(data_vars=dict(
            time=xr.DataArray(pd.date_range(
                start='2010-04-05', periods=4, freq='1D'),
                              dims='time'),
            a=xr.DataArray([True, False, False, True], dims='time'),
            b=xr.DataArray([32, 33, 35, 34], dims='time'),
            c=xr.DataArray([0.4, 0.2, np.nan, 0.7], dims='time')))
        result = _collect_timeseries_result(time_series_ds, {
            'a': 'a',
            'b': 'b',
            'c': 'c'
        },
                                            max_valids=None)
        self.assertEqual([{
            'a': True,
            'b': 32,
            'c': 0.4,
            'time': '2010-04-05T00:00:00Z'
        }, {
            'a': False,
            'b': 33,
            'c': 0.2,
            'time': '2010-04-06T00:00:00Z'
        }, {
            'a': False,
            'b': 35,
            'c': None,
            'time': '2010-04-07T00:00:00Z'
        }, {
            'a': True,
            'b': 34,
            'c': 0.7,
            'time': '2010-04-08T00:00:00Z'
        }], result)
        self.assertEqual(
            '[{"a": true, "b": 32, "c": 0.4, "time": "2010-04-05T00:00:00Z"},'
            ' {"a": false, "b": 33, "c": 0.2, "time": "2010-04-06T00:00:00Z"},'
            ' {"a": false, "b": 35, "c": null, "time": "2010-04-07T00:00:00Z"},'
            ' {"a": true, "b": 34, "c": 0.7, "time": "2010-04-08T00:00:00Z"}]',
            json.dumps(result))