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

ssec / polar2grid / 14623022764

23 Apr 2025 04:16PM UTC coverage: 85.06% (+0.03%) from 85.035%
14623022764

Pull #747

github

web-flow
Merge dc592d111 into abb686751
Pull Request #747: Add Phase and PRES to 'abi_l2_nc' reader

11 of 13 new or added lines in 2 files covered. (84.62%)

4623 of 5435 relevant lines covered (85.06%)

0.85 hits per line

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

76.47
/polar2grid/resample/resample_decisions.py
1
#!/usr/bin/env python
2
# encoding: utf-8
3
# Copyright (C) 2021 Space Science and Engineering Center (SSEC),
4
#  University of Wisconsin-Madison.
5
#
6
#     This program is free software: you can redistribute it and/or modify
7
#     it under the terms of the GNU General Public License as published by
8
#     the Free Software Foundation, either version 3 of the License, or
9
#     (at your option) any later version.
10
#
11
#     This program is distributed in the hope that it will be useful,
12
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
13
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
#     GNU General Public License for more details.
15
#
16
#     You should have received a copy of the GNU General Public License
17
#     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
#
19
# This file is part of the polar2grid software package. Polar2grid takes
20
# satellite observation data, remaps it, and writes it to a file format for
21
# input into another program.
22
# Documentation: http://www.ssec.wisc.edu/software/polar2grid/
23
"""Decision utilties for resampling."""
1✔
24

25
import logging
1✔
26
import os
1✔
27

28
import yaml
1✔
29
from pyresample.geometry import SwathDefinition
1✔
30
from satpy._config import config_search_paths
1✔
31

32
try:
1✔
33
    from satpy.decision_tree import DecisionTree
1✔
34
except ImportError:
1✔
35
    from satpy.writers import DecisionTree
1✔
36

37
from polar2grid.utils.legacy_compat import get_sensor_alias
1✔
38
from polar2grid.utils.config import recursive_dict_update
1✔
39

40
logger = logging.getLogger(__name__)
1✔
41

42

43
class ResamplerDecisionTree(DecisionTree):
1✔
44
    """Helper class to determine resampler algorithm and other options."""
45

46
    def __init__(self, *decision_dicts, **kwargs):
1✔
47
        """Init the decision tree."""
48
        match_keys = kwargs.pop(
1✔
49
            "match_keys",
50
            (
51
                "name",
52
                "reader",
53
                "platform_name",
54
                "sensor",
55
                "area_type",
56
                "standard_name",
57
                "units",
58
            ),
59
        )
60
        self.prefix = kwargs.pop("config_section", "resampling")
1✔
61
        multival_keys = kwargs.pop("multival_keys", ["sensor"])
1✔
62
        super(ResamplerDecisionTree, self).__init__(decision_dicts, match_keys, multival_keys)
1✔
63

64
    @classmethod
1✔
65
    def from_configs(cls, config_filename="resampling.yaml"):
1✔
66
        config_paths = config_search_paths(config_filename)
1✔
67
        return cls(*config_paths)
1✔
68

69
    def add_config_to_tree(self, *config_files):
1✔
70
        """Add configuration to tree."""
71
        conf = {}
1✔
72
        for config_file in config_files:
1✔
73
            if os.path.isfile(config_file):
1✔
74
                with open(config_file) as fd:
1✔
75
                    resample_config = yaml.load(fd, Loader=yaml.UnsafeLoader)
1✔
76
                    if resample_config is None:
1✔
77
                        # empty file
78
                        continue
×
79
                    resampling_section = resample_config.get(self.prefix, {})
1✔
80
                    if not resampling_section:
1✔
81
                        logging.debug("Config '{}' has no '{}' section or it is empty".format(config_file, self.prefix))
×
82
                        continue
×
83
                    recursive_dict_update(conf, resampling_section)
1✔
84
            elif isinstance(config_file, dict):
×
NEW
85
                recursive_dict_update(conf, config_file)
×
86
            else:
87
                logger.debug("Loading resampling config string")
×
88
                d = yaml.load(config_file, Loader=yaml.UnsafeLoader)
×
89
                if not isinstance(d, dict):
×
90
                    raise ValueError("YAML file doesn't exist or string is not YAML dict: {}".format(config_file))
×
NEW
91
                recursive_dict_update(conf, d)
×
92
        self._build_tree(conf)
1✔
93

94
    def find_match(self, **query_dict):
1✔
95
        """Find a match."""
96
        query_dict["area_type"] = "swath" if isinstance(query_dict["area"], SwathDefinition) else "area"
1✔
97
        query_dict["sensor"] = get_sensor_alias(query_dict.get("sensor"))
1✔
98

99
        try:
1✔
100
            return super().find_match(**query_dict)
1✔
101
        except KeyError as err:
×
102
            # give a more understandable error message
103
            raise KeyError(
×
104
                f"No resampling configuration found for {query_dict['area_type']=} | {query_dict['name']=}"
105
            ) from err
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

© 2025 Coveralls, Inc