Skip to content

Commit

Permalink
Merge pull request #637 from djhoese/feature-abi-rad-clip
Browse files Browse the repository at this point in the history
Add `--clip-negative-radiances` flag to ABI L1b reader
  • Loading branch information
djhoese authored Sep 11, 2023
2 parents 21a6ce9 + dda8c87 commit bf8ea3f
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 59 deletions.
54 changes: 0 additions & 54 deletions polar2grid/core/script_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@
import sys
from collections import defaultdict

try:
from argparse import BooleanOptionalAction
except ImportError:
BooleanOptionalAction = None

LOG = logging.getLogger(__name__)


Expand Down Expand Up @@ -311,52 +306,3 @@ def create_basic_parser(*args, **kwargs):
help="exit on first error including non-fatal errors",
)
return parser


# Backport of BooleanOptionalAction (added in Python 3.9)
class _BooleanOptionalAction(argparse.Action):
def __init__(
self,
option_strings,
dest,
const=None,
default=None,
type=None,
choices=None,
required=False,
help=None,
metavar=None,
):
_option_strings = []
for option_string in option_strings:
_option_strings.append(option_string)

if option_string.startswith("--"):
option_string = "--no-" + option_string[2:]
_option_strings.append(option_string)

if help is not None and default is not None:
help += f" (default: {default})"

super().__init__(
option_strings=_option_strings,
dest=dest,
nargs=0,
default=default,
type=type,
choices=choices,
required=required,
help=help,
metavar=metavar,
)

def __call__(self, parser, namespace, values, option_string=None):
if option_string in self.option_strings:
setattr(namespace, self.dest, not option_string.startswith("--no-"))

def format_usage(self):
return " | ".join(self.option_strings)


if BooleanOptionalAction is None:
BooleanOptionalAction = _BooleanOptionalAction
8 changes: 7 additions & 1 deletion polar2grid/readers/abi_l1b.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@

from __future__ import annotations

from argparse import ArgumentParser, _ArgumentGroup
from argparse import ArgumentParser, BooleanOptionalAction, _ArgumentGroup
from typing import Optional

from ._base import ReaderProxyBase
Expand Down Expand Up @@ -142,4 +142,10 @@ def add_reader_argument_groups(
"""
if group is None:
group = parser.add_argument_group(title="ABI L1b Reader")
group.add_argument(
"--clip-negative-radiances",
action=BooleanOptionalAction,
default=True,
help="Clip negative radiances for IR bands. Default is to perform the clipping.",
)
return group, None
3 changes: 1 addition & 2 deletions polar2grid/readers/ahi_hsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,9 @@

from __future__ import annotations

from argparse import ArgumentParser, _ArgumentGroup
from argparse import ArgumentParser, BooleanOptionalAction, _ArgumentGroup
from typing import Optional

from ..core.script_utils import BooleanOptionalAction
from ._base import ReaderProxyBase

PREFERRED_CHUNK_SIZE: int = 2200 # one segment
Expand Down
2 changes: 1 addition & 1 deletion polar2grid/writers/awips_tiled.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@
"""
import logging
from argparse import BooleanOptionalAction

from polar2grid.core.script_utils import BooleanOptionalAction
from polar2grid.utils.legacy_compat import convert_p2g_pattern_to_satpy

LOG = logging.getLogger(__name__)
Expand Down
3 changes: 2 additions & 1 deletion polar2grid/writers/geotiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@
import argparse
import logging
import os
from argparse import BooleanOptionalAction

from polar2grid.core.dtype import NUMPY_DTYPE_STRS, int_or_float, str_to_dtype
from polar2grid.core.script_utils import BooleanOptionalAction, NumpyDtypeList
from polar2grid.core.script_utils import NumpyDtypeList
from polar2grid.utils.legacy_compat import convert_p2g_pattern_to_satpy

LOG = logging.getLogger(__name__)
Expand Down

0 comments on commit bf8ea3f

Please sign in to comment.