Skip to content

Commit

Permalink
Remove unnecessary f strings and change comparison to None
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-hampton committed Nov 19, 2024
1 parent 785ed39 commit 611021e
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 82 deletions.
116 changes: 75 additions & 41 deletions src/ncas_amof_netcdf_template/create_netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@

def add_attributes(
ncfile: Dataset,
instrument_dict: Optional[dict[
str, dict[str, Union[str, list[str], dict[str, dict[str, Union[str, float]]]]]
]] = None,
instrument_dict: Optional[
dict[
str,
dict[str, Union[str, list[str], dict[str, dict[str, Union[str, float]]]]],
]
] = None,
product: Optional[str] = None,
created_time: Optional[str] = None,
location: Optional[str] = None,
Expand Down Expand Up @@ -81,7 +84,7 @@ def add_attributes(
instrument_dict["info"]["instrument_name"],
product,
loc,
tag
tag,
)
else:
warnings.warn(

Check warning on line 90 in src/ncas_amof_netcdf_template/create_netcdf.py

View check run for this annotation

Codecov / codecov/patch

src/ncas_amof_netcdf_template/create_netcdf.py#L90

Added line #L90 was not covered by tests
Expand Down Expand Up @@ -114,19 +117,25 @@ def add_attributes(
ncfile.setncattr(key, value["Fixed Value"])
elif key == "source":
if "Descriptor" in instrument_file_info.instrument_data.keys():
ncfile.setncattr(key, instrument_file_info.instrument_data["Descriptor"])
ncfile.setncattr(
key, instrument_file_info.instrument_data["Descriptor"]
)
else:
ncfile.setncattr(key, "n/a")
elif key == "institution":
ncfile.setncattr(key, "National Centre for Atmospheric Science (NCAS)")
elif key == "platform":
if "Mobile/Fixed (loc)" in instrument_file_info.instrument_data.keys():
ncfile.setncattr(key, instrument_file_info.instrument_data["Mobile/Fixed (loc)"])
ncfile.setncattr(
key, instrument_file_info.instrument_data["Mobile/Fixed (loc)"]
)
else:
ncfile.setncattr(key, "n/a")

Check warning on line 133 in src/ncas_amof_netcdf_template/create_netcdf.py

View check run for this annotation

Codecov / codecov/patch

src/ncas_amof_netcdf_template/create_netcdf.py#L133

Added line #L133 was not covered by tests
elif key == "instrument_manufacturer":
if "Manufacturer" in instrument_file_info.instrument_data.keys():
ncfile.setncattr(key, instrument_file_info.instrument_data["Manufacturer"])
ncfile.setncattr(
key, instrument_file_info.instrument_data["Manufacturer"]
)
else:
ncfile.setncattr(key, "n/a")
elif key == "instrument_model":
Expand All @@ -136,7 +145,9 @@ def add_attributes(
ncfile.setncattr(key, "n/a")
elif key == "instrument_serial_number":
if "Serial Number" in instrument_file_info.instrument_data.keys():
ncfile.setncattr(key, instrument_file_info.instrument_data["Serial Number"])
ncfile.setncattr(
key, instrument_file_info.instrument_data["Serial Number"]
)
else:
ncfile.setncattr(key, "n/a")
elif key == "amf_vocabularies_release":
Expand Down Expand Up @@ -169,9 +180,12 @@ def add_attributes(

def add_dimensions(
ncfile: Dataset,
instrument_dict: Optional[dict[
str, dict[str, Union[str, list[str], dict[str, dict[str, Union[str, float]]]]]
]] = None,
instrument_dict: Optional[
dict[
str,
dict[str, Union[str, list[str], dict[str, dict[str, Union[str, float]]]]],
]
] = None,
product: Optional[str] = None,
dimension_lengths: Optional[dict[str, int]] = None,
instrument_file_info: Optional[FileInfo] = None,
Expand Down Expand Up @@ -205,7 +219,7 @@ def add_dimensions(
)
if product is None or dimension_lengths is None:
msg = (

Check warning on line 221 in src/ncas_amof_netcdf_template/create_netcdf.py

View check run for this annotation

Codecov / codecov/patch

src/ncas_amof_netcdf_template/create_netcdf.py#L221

Added line #L221 was not covered by tests
"If instrument_dict is still being used, 'product' and"
"If instrument_dict is still being used, 'product' and"
" 'dimension_lengths' must be given. Preferred option is to switch"
" to using instrument_file_info instead."
)
Expand All @@ -221,22 +235,27 @@ def add_dimensions(

if instrument_file_info is not None:
for dim_name in instrument_file_info.dimensions.keys():
ncfile.createDimension(dim_name, instrument_file_info.dimensions[dim_name]["Length"])
ncfile.createDimension(
dim_name, instrument_file_info.dimensions[dim_name]["Length"]
)

elif dimension_lengths is not None:
for key, length in dimension_lengths.items():
if (
key in instrument_dict["common"]["dimensions"].keys()
or key in instrument_dict[product]["dimensions"].keys()
):
ncfile.createDimension(key, length)
ncfile.createDimension(key, length)


def add_variables(
ncfile: Dataset,
instrument_dict: Optional[dict[
str, dict[str, Union[str, list[str], dict[str, dict[str, Union[str, float]]]]]
]] = None,
instrument_dict: Optional[
dict[
str,
dict[str, Union[str, list[str], dict[str, dict[str, Union[str, float]]]]],
]
] = None,
product: Optional[str] = None,
instrument_file_info: Optional[FileInfo] = None,
verbose: int = 0,
Expand Down Expand Up @@ -390,11 +409,7 @@ def add_variables(
and ("EXAMPLE" in mdatvalue or mdatvalue == "")
):
# don't add empty attributes
if (
isinstance(mdatvalue, str)
and mdatvalue == ""
and verbose >= 1
):
if isinstance(mdatvalue, str) and mdatvalue == "" and verbose >= 1:
print(
f"WARN: No value for attribute {mdatkey} "
"for variable {key}, attribute not added"
Expand All @@ -407,9 +422,12 @@ def make_netcdf(
instrument: str,
product: str,
time: str,
instrument_dict: Optional[dict[
str, dict[str, Union[str, list[str], dict[str, dict[str, Union[str, float]]]]]
]] = None,
instrument_dict: Optional[
dict[
str,
dict[str, Union[str, list[str], dict[str, dict[str, Union[str, float]]]]],
]
] = None,
loc: str = "land",
dimension_lengths: dict[str, int] = {},
verbose: int = 0,
Expand All @@ -434,8 +452,8 @@ def make_netcdf(
time (str): time that the data represents, in YYYYmmdd-HHMMSS format or
as much of as required.
instrument_dict (dict or None): -DEPRECATED- information about the instrument
from tsv2dict.instrument_dict. Use
instrument_file_info argument instead. Will be
from tsv2dict.instrument_dict. Use
instrument_file_info argument instead. Will be
remved in version 2.7.0.
instrument_file_info (FileInfo or None): information about the instrument,
from file_info.FileInfo.
Expand Down Expand Up @@ -500,7 +518,7 @@ def make_netcdf(
instrument_dict["info"]["instrument_name"],
product,
loc,
tag
tag,
)
else:
warnings.warn(

Check warning on line 524 in src/ncas_amof_netcdf_template/create_netcdf.py

View check run for this annotation

Codecov / codecov/patch

src/ncas_amof_netcdf_template/create_netcdf.py#L524

Added line #L524 was not covered by tests
Expand Down Expand Up @@ -550,14 +568,22 @@ def make_netcdf(
var_dict[var]["shuffle"] = True

if (
instrument_file_info.instrument_data["Mobile/Fixed (loc)"].split("-")[0].strip().lower()
instrument_file_info.instrument_data["Mobile/Fixed (loc)"]
.split("-")[0]
.strip()
.lower()
== "fixed"
):
platform = (
instrument_file_info.instrument_data["Mobile/Fixed (loc)"].split("-")[-1].strip().lower()
instrument_file_info.instrument_data["Mobile/Fixed (loc)"]
.split("-")[-1]
.strip()
.lower()
)
else:
platform = instrument_file_info.instrument_data["Mobile/Fixed (loc)"].strip().lower()
platform = (
instrument_file_info.instrument_data["Mobile/Fixed (loc)"].strip().lower()
)

if options != "":
no_options = len(options.split("_"))
Expand Down Expand Up @@ -722,7 +748,9 @@ def make_product_netcdf(
if date is None:
date = dt.datetime.now(dt.timezone.utc).strftime("%Y%m%d")

product_file_info = FileInfo(instrument_name, product, deployment_mode=deployment_loc, tag=tag)
product_file_info = FileInfo(
instrument_name, product, deployment_mode=deployment_loc, tag=tag
)
product_file_info.get_common_info()
product_file_info.get_deployment_info()
product_file_info.get_product_info()
Expand Down Expand Up @@ -867,15 +895,15 @@ def main(

if isinstance(products, str):
products = [products]

Check warning on line 897 in src/ncas_amof_netcdf_template/create_netcdf.py

View check run for this annotation

Codecov / codecov/patch

src/ncas_amof_netcdf_template/create_netcdf.py#L897

Added line #L897 was not covered by tests
elif products == None:
elif products is None:
products = list_products(instrument=instrument, tag=tag)
warnings.warn(
"Passing 'None' as argument for 'products' is being deprecated. Use single"
" data product for this argument. Available data products for instrument"
f"{instrument} are {products}. The option to use 'None' will be removed"
" from version 2.7.0.",
DeprecationWarning,
stacklevel=2
stacklevel=2,
)
elif isinstance(products, list):
warnings.warn(

Check warning on line 909 in src/ncas_amof_netcdf_template/create_netcdf.py

View check run for this annotation

Codecov / codecov/patch

src/ncas_amof_netcdf_template/create_netcdf.py#L909

Added line #L909 was not covered by tests
Expand All @@ -886,19 +914,25 @@ def main(
stacklevel=2,
)


ncfiles = []

for product in products:
instrument_file_info = FileInfo(instrument, product, deployment_mode = loc, tag = tag)
instrument_file_info = FileInfo(
instrument, product, deployment_mode=loc, tag=tag
)
instrument_file_info.get_product_info()
instrument_file_info.get_deployment_info()
instrument_file_info.get_instrument_info()
instrument_file_info.get_common_info()

# check if platform needs changing
if platform is not None:
if "mobile" not in instrument_file_info.instrument_data["Mobile/Fixed (loc)"].lower():
if (
"mobile"
not in instrument_file_info.instrument_data[
"Mobile/Fixed (loc)"
].lower()
):
print(
"[WARNING]: Changing platform for an "
f"observatory instrument {instrument}."
Expand All @@ -921,7 +955,7 @@ def main(
instrument,
product,
date,
instrument_file_info = instrument_file_info,
instrument_file_info=instrument_file_info,
verbose=verbose,
options=options,
product_version=product_version,
Expand All @@ -934,13 +968,13 @@ def main(
complevel=complevel,
shuffle=shuffle,
)
)
)
else:
make_netcdf(
instrument,
product,
date,
instrument_file_info = instrument_file_info,
instrument_file_info=instrument_file_info,
verbose=verbose,
options=options,
product_version=product_version,
Expand Down
Loading

0 comments on commit 611021e

Please sign in to comment.