Skip to content

Commit

Permalink
add --set-global-attr key value option
Browse files Browse the repository at this point in the history
to facilitate dynamic replacement of `{{key}}` in the given file
  • Loading branch information
carueda committed Feb 22, 2024
1 parent 7edb634 commit 1a742be
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
2 changes: 1 addition & 1 deletion NRS11/globalAttributes_NRS11.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ time_coverage_end: AUTOPOPULATE
time_coverage_duration: P1D
time_coverage_resolution: P60S
platform: NOAA NRS11 mooring
instrument: AUH model ITC-1032, SN ####
instrument: "AUH model ITC-1032, SN {{serial_number}}"
cdm_data_type: TimeSeries
references: >-
Computation of single-sided mean-square sound pressure spectral density with 1 Hz resolution
Expand Down
2 changes: 2 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ main-nrs11 date='20200101' *more_args='':
--gs \
--json-base-dir=$WS/noaa-passive-bioacoustic_nrs_11_2019-2021 \
--global-attrs="$WS/globalAttributes_NRS11.yaml" \
--set-global-attr serial_number "000000" \
--variable-attrs="$WS/variableAttributes_NRS11.yaml" \
--voltage-multiplier=2.5 \
--sensitivity-uri="$WS/NRS11_H5R6_sensitivity_hms5kHz.nc" \
Expand All @@ -139,6 +140,7 @@ main-nrs11-multiple-days year month *days="":
--gs \
--json-base-dir=$WS/noaa-passive-bioacoustic_nrs_11_2019-2021 \
--global-attrs="$WS/globalAttributes_NRS11.yaml" \
--set-global-attr serial_number "000000" \
--variable-attrs="$WS/variableAttributes_NRS11.yaml" \
--voltage-multiplier=2.5 \
--sensitivity-uri="$WS/NRS11_H5R6_sensitivity_hms5kHz.nc" \
Expand Down
1 change: 1 addition & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def main(opts):
output_prefix=opts.output_prefix,
gen_csv=opts.gen_csv,
global_attrs_uri=opts.global_attrs,
set_global_attrs=opts.set_global_attrs,
variable_attrs_uri=opts.variable_attrs,
voltage_multiplier=opts.voltage_multiplier,
sensitivity_uri=opts.sensitivity_uri,
Expand Down
12 changes: 12 additions & 0 deletions src/main_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ def parse_arguments():
help="URI of JSON file with global attributes to be added to the NetCDF file.",
)

parser.add_argument(
"--set-global-attr",
type=str,
nargs=2,
default=None,
metavar=("key", "value"),
dest="set_global_attrs",
action="append",
help="Replace {{key}} with the given value for every occurrence of {{key}}"
" in the global attrs file.",
)

parser.add_argument(
"--variable-attrs",
type=str,
Expand Down
35 changes: 24 additions & 11 deletions src/process_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(
gen_netcdf: bool = True,
gen_csv: bool = False,
global_attrs_uri: Optional[str] = None,
set_global_attrs: Optional[list[list[str]]] = None,
variable_attrs_uri: Optional[str] = None,
voltage_multiplier: Optional[float] = None,
sensitivity_uri: Optional[str] = None,
Expand All @@ -61,6 +62,8 @@ def __init__(
True to also generate CSV version of the result.
:param global_attrs_uri:
URI of JSON file with global attributes to be added to the NetCDF file.
:param set_global_attrs:
List of [key, value] pairs to be considered for the global attributes.
:param variable_attrs_uri:
URI of JSON file with variable attributes to be added to the NetCDF file.
:param voltage_multiplier:
Expand All @@ -86,6 +89,7 @@ def __init__(
+ f"\n gen_netcdf: {gen_netcdf}"
+ f"\n gen_csv: {gen_csv}"
+ f"\n global_attrs_uri: {global_attrs_uri}"
+ f"\n set_global_attrs: {set_global_attrs}"
+ f"\n variable_attrs_uri: {variable_attrs_uri}"
+ f"\n voltage_multiplier: {voltage_multiplier}"
+ f"\n sensitivity_uri: {sensitivity_uri}"
Expand All @@ -106,7 +110,7 @@ def __init__(

self.metadata_helper = MetadataHelper(
self.logger,
self._load_attributes("global", global_attrs_uri),
self._load_attributes("global", global_attrs_uri, set_global_attrs),
self._load_attributes("variable", variable_attrs_uri),
)

Expand Down Expand Up @@ -140,14 +144,20 @@ def __init__(
pathlib.Path(output_dir).mkdir(exist_ok=True)

def _load_attributes(
self, what: str, attrs_uri: Optional[str]
self,
what: str,
attrs_uri: Optional[str],
set_attrs: Optional[list[list[str]]] = None,
) -> Optional[OrderedDict[str, Any]]:
if attrs_uri:
self.logger.info(f"Loading {what} attributes from {attrs_uri=}")
filename = self.file_helper.get_local_filename(attrs_uri)
if filename is not None:
with open(filename, "r", encoding="UTF-8") as f:
return parse_attributes(f.read(), pathlib.Path(filename).suffix)
res = parse_attributes(f.read(), pathlib.Path(filename).suffix)
for k, v in set_attrs or []:
res[k] = v
return res
else:
self.logger.error(f"Unable to resolve '{attrs_uri=}'. Ignoring it.")
else:
Expand Down Expand Up @@ -287,15 +297,18 @@ def process_segment_at_hour_minute(self, at_hour: int, at_minute: int):

def _get_global_attributes(self, year: int, month: int, day: int):
coverage_date = f"{year:04}-{month:02}-{day:02}"
global_attrs = {
"time_coverage_start": f"{coverage_date} 00:00:00Z",
"time_coverage_end": f"{coverage_date} 23:59:00Z",
"date_created": datetime.utcnow().strftime("%Y-%m-%d"),
}
md_helper = self.metadata_helper
md_helper.set_some_global_attributes(
{
"time_coverage_start": f"{coverage_date} 00:00:00Z",
"time_coverage_end": f"{coverage_date} 23:59:00Z",
"date_created": datetime.utcnow().strftime("%Y-%m-%d"),
}
)
md_helper.set_some_global_attributes(global_attrs)
# TODO get PyPAM version from somewhere
snippets = {"{{PyPAM_version}}": "0.2.0"}
snippets = {"{{PyPAM_version}}": "0.3.0"}
global_attrs = md_helper.get_global_attributes()
# for each, key, have the {{key}} snippet for replacement
# in case it is used in any values:
for k, v in global_attrs.items():
snippets["{{" + k + "}}"] = v
return replace_snippets(global_attrs, snippets)

0 comments on commit 1a742be

Please sign in to comment.