• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ghiggi / gpm_api / 9347528555

03 Jun 2024 09:16AM UTC coverage: 92.866% (+5.0%) from 87.87%
9347528555

push

github

ghiggi
Generalize checks defaults

10 of 10 new or added lines in 1 file covered. (100.0%)

266 existing lines in 25 files now uncovered.

13199 of 14213 relevant lines covered (92.87%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.0
/gpm/dataset/decoding/utils.py
1
# -----------------------------------------------------------------------------.
2
# MIT License
3

4
# Copyright (c) 2024 GPM-API developers
5
#
6
# This file is part of GPM-API.
7

8
# Permission is hereby granted, free of charge, to any person obtaining a copy
9
# of this software and associated documentation files (the "Software"), to deal
10
# in the Software without restriction, including without limitation the rights
11
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
# copies of the Software, and to permit persons to whom the Software is
13
# furnished to do so, subject to the following conditions:
14
#
15
# The above copyright notice and this permission notice shall be included in all
16
# copies or substantial portions of the Software.
17
#
18
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
# SOFTWARE.
25

26
# -----------------------------------------------------------------------------.
27
"""This module contains utilities for the decoding of GPM product variables."""
1✔
28
import dask.array
1✔
29
import numpy as np
1✔
30

31

32
def is_dataarray_decoded(da):
1✔
33
    """Check if a `xarray.DataArray` has been decoded by GPM-API."""
34
    return da.attrs.get("gpm_api_decoded", "no") == "yes"
1✔
35

36

37
def add_decoded_flag(ds, variables):
1✔
38
    """Add gpm_api_decoded flag to GPM-API decoded variables."""
39
    for var in variables:
1✔
40
        if var in ds:
1✔
41
            ds[var].attrs["gpm_api_decoded"] = "yes"
1✔
42
    return ds
1✔
43

44

45
# def _np_remap_numeric_array1(arr, remapping_dict, fill_value=np.nan):
46
#     # VERY SLOW ALTERNATIVE
47
#     isna = np.isnan(arr)
48
#     arr[isna] = -1 # dummy
49
#     unique_values = np.unique(arr[~np.isnan(arr)])
50
#     _ = [remapping_dict.setdefault(value, fill_value) for value in unique_values if value not in remapping_dict]
51
#     remapping_dict = {float(k): float(v) for k, v in remapping_dict.items()}
52
#     new_arr = np.vectorize(remapping_dict.__getitem__)(arr)
53
#     new_arr[isna] = np.nan
54
#     return new_arr
55

56

57
def _np_remap_numeric_array(arr, remapping_dict, fill_value=np.nan):
1✔
58
    # Define conditions
59
    conditions = [arr == i for i in remapping_dict]
1✔
60
    # Define choices corresponding to conditions
61
    choices = remapping_dict.values()
1✔
62
    # Apply np.select to transform the array
63
    return np.select(conditions, choices, default=fill_value)
1✔
64

65

66
def _dask_remap_numeric_array(arr, remapping_dict, fill_value=np.nan):
1✔
67
    return dask.array.map_blocks(_np_remap_numeric_array, arr, remapping_dict, fill_value, dtype=arr.dtype)
1✔
68

69

70
def remap_numeric_array(arr, remapping_dict, fill_value=np.nan):
1✔
71
    """Remap the values of a numeric array."""
72
    if hasattr(arr, "chunks"):
1✔
73
        return _dask_remap_numeric_array(arr, remapping_dict, fill_value=fill_value)
1✔
UNCOV
74
    return _np_remap_numeric_array(arr, remapping_dict, fill_value=fill_value)
×
75

76

77
def ceil_dataarray(da):
1✔
78
    """Ceil a `xarray.DataArray`."""
79
    data = da.data
1✔
80
    data = np.ceil(data) if hasattr(data, "chunks") else dask.array.ceil(data)
1✔
81
    da.data = data
1✔
82
    return da
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc