Skip to content

Commit

Permalink
add --version option to the programs
Browse files Browse the repository at this point in the history
  • Loading branch information
carueda committed Mar 19, 2024
1 parent ce24f00 commit 5c344e5
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 54 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

2024-03

- added `--version` option to the programs
- minor code reorg
- enabled poetry
- now packaging and publishing to PyPI
- TODO ongoing: README/justfile adjustments, etc.
Expand Down
61 changes: 13 additions & 48 deletions pbp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,14 @@
import os

import xarray as xr

from pbp.logging_helper import PbpLogger


def save_dataset_to_netcdf(logger: PbpLogger, ds: xr.Dataset, filename: str) -> bool:
logger.info(f" - saving dataset to: {filename}")
try:
ds.to_netcdf(
filename,
engine="h5netcdf",
encoding={
"effort": {"_FillValue": None},
"frequency": {"_FillValue": None},
"sensitivity": {"_FillValue": None},
},
)
return True
except Exception as e: # pylint: disable=broad-exception-caught
error = f"Unable to save {filename}: {e}"
logger.error(error)
print(error)
return False


def save_dataset_to_csv(logger: PbpLogger, ds: xr.Dataset, filename: str):
logger.info(f" - saving dataset to: {filename}")
def get_pbp_version() -> str:
try:
ds.to_pandas().to_csv(filename, float_format="%.1f")
except Exception as e: # pylint: disable=broad-exception-caught
logger.error(f"Unable to save {filename}: {e}")


def get_cpus_to_use(num_cpus: int) -> int:
cpu_count: int = os.cpu_count() or 1
if num_cpus <= 0 or num_cpus > cpu_count:
num_cpus = cpu_count
return num_cpus


class PBPException(Exception):
"""
Placeholder for a more specific exception.
"""

def __init__(self, msg: str):
super().__init__(f"PBPException({msg})")
import importlib.metadata

return importlib.metadata.version("mbari-pbp")
except Exception: # pylint: disable=broad-exception-caught
try:
from poetry.core.factory import Factory

factory = Factory()
poetry = factory.create_poetry()
return poetry.package.version.text
except Exception: # pylint: disable=broad-exception-caught
return "version not found"
2 changes: 1 addition & 1 deletion pbp/json_generator/corrector.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import tempfile
import json

from pbp import PbpLogger
from pbp.logging_helper import PbpLogger


class MetadataCorrector:
Expand Down
2 changes: 1 addition & 1 deletion pbp/json_generator/gen_iclisten.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import pbp.json_generator.utils as utils
from pbp.json_generator.corrector import MetadataCorrector
from pbp.json_generator.metadata_extractor import IcListenWavFile
from pbp import PbpLogger
from pbp.logging_helper import PbpLogger
from pbp.json_generator.gen_abstract import MetadataGeneratorAbstract


Expand Down
2 changes: 1 addition & 1 deletion pbp/json_generator/gen_nrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from progressbar import progressbar
from pbp.json_generator.corrector import MetadataCorrector
from pbp.json_generator.metadata_extractor import FlacFile
from pbp import PbpLogger
from pbp.logging_helper import PbpLogger
from pbp.json_generator.gen_abstract import MetadataGeneratorAbstract
from pbp.json_generator.utils import parse_s3_or_gcp_url

Expand Down
2 changes: 1 addition & 1 deletion pbp/json_generator/gen_soundtrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pathlib import Path
from progressbar import progressbar

from pbp import PbpLogger
from pbp.logging_helper import PbpLogger
from pbp.json_generator.gen_abstract import MetadataGeneratorAbstract
from pbp.json_generator.metadata_extractor import SoundTrapWavFile
from pbp.json_generator.corrector import MetadataCorrector
Expand Down
9 changes: 9 additions & 0 deletions pbp/main_args.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from argparse import ArgumentParser, RawTextHelpFormatter


from pbp import get_pbp_version


def parse_arguments():
description = "PyPAM based processing of Pacific Sound data."
example = """
Expand All @@ -15,6 +18,12 @@ def parse_arguments():
description=description, epilog=example, formatter_class=RawTextHelpFormatter
)

parser.add_argument(
"--version",
action="version",
version=get_pbp_version(),
)

parser.add_argument(
"--json-base-dir",
type=str,
Expand Down
8 changes: 8 additions & 0 deletions pbp/main_json_generator_args.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from argparse import ArgumentParser, RawTextHelpFormatter

from pbp import get_pbp_version


def parse_arguments():
description = (
Expand All @@ -22,6 +24,12 @@ def parse_arguments():
description=description, epilog=example, formatter_class=RawTextHelpFormatter
)

parser.add_argument(
"--version",
action="version",
version=get_pbp_version(),
)

class InstrumentType:
NRS = "NRS"
ICLISTEN = "ICLISTEN"
Expand Down
7 changes: 7 additions & 0 deletions pbp/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
DEFAULT_YLIM,
DEFAULT_CMLIM,
)
from pbp import get_pbp_version


def parse_arguments():
description = "Generate summary plots for given netcdf files."

parser = ArgumentParser(description=description, formatter_class=RawTextHelpFormatter)

parser.add_argument(
"--version",
action="version",
version=get_pbp_version(),
)

parser.add_argument(
"--latlon",
type=float,
Expand Down
30 changes: 28 additions & 2 deletions pbp/process_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import numpy as np
import xarray as xr

from pbp import save_dataset_to_csv, save_dataset_to_netcdf

from pbp.file_helper import FileHelper
from pbp.logging_helper import PbpLogger
from pbp.metadata import MetadataHelper, parse_attributes, replace_snippets
Expand Down Expand Up @@ -312,3 +310,31 @@ def _get_global_attributes(self, year: int, month: int, day: int):
for k, v in global_attrs.items():
snippets["{{" + k + "}}"] = v
return replace_snippets(global_attrs, snippets)


def save_dataset_to_netcdf(logger: PbpLogger, ds: xr.Dataset, filename: str) -> bool:
logger.info(f" - saving dataset to: {filename}")
try:
ds.to_netcdf(
filename,
engine="h5netcdf",
encoding={
"effort": {"_FillValue": None},
"frequency": {"_FillValue": None},
"sensitivity": {"_FillValue": None},
},
)
return True
except Exception as e: # pylint: disable=broad-exception-caught
error = f"Unable to save {filename}: {e}"
logger.error(error)
print(error)
return False


def save_dataset_to_csv(logger: PbpLogger, ds: xr.Dataset, filename: str):
logger.info(f" - saving dataset to: {filename}")
try:
ds.to_pandas().to_csv(filename, float_format="%.1f")
except Exception as e: # pylint: disable=broad-exception-caught
logger.error(f"Unable to save {filename}: {e}")

0 comments on commit 5c344e5

Please sign in to comment.