Skip to content
Snippets Groups Projects
Verified Commit 65ea217c authored by David Hoese's avatar David Hoese
Browse files

Fix a few style issues

parent 3983efcb
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python
"""Generate AOSS Tower NetCDF4 files from Level 00 ASCII files.""" """Generate AOSS Tower NetCDF4 files from Level 00 ASCII files."""
import logging import logging
import os import os
import platform import platform
import sys import sys
from datetime import datetime from datetime import datetime
from pathlib import Path
import numpy as np import numpy as np
import pandas as pd import pandas as pd
...@@ -37,7 +37,8 @@ def _get_data(input_files): ...@@ -37,7 +37,8 @@ def _get_data(input_files):
bad_files += 1 bad_files += 1
continue continue
if bad_files == len(input_files): if bad_files == len(input_files):
raise ValueError("No valid input data files found") msg = "No valid input data files found"
raise ValueError(msg)
def get_data(input_files): def get_data(input_files):
...@@ -75,14 +76,14 @@ def write_global_attributes(nc_file, input_sources, interval=None, datastream=No ...@@ -75,14 +76,14 @@ def write_global_attributes(nc_file, input_sources, interval=None, datastream=No
nc_file.command_line = " ".join(sys.argv) nc_file.command_line = " ".join(sys.argv)
# generate history # generate history
nc_file.history = " ".join(platform.uname()) + " " + os.path.basename(__file__) nc_file.history = " ".join(platform.uname()) + " " + Path(__file__).name
nc_file.input_source = input_sources[0] nc_file.input_source = input_sources[0]
nc_file.input_sources = ", ".join(input_sources) nc_file.input_sources = ", ".join(input_sources)
def create_giant_netcdf( def create_giant_netcdf(
input_files, input_files: list[Path],
output_fn, output_fn: Path,
zlib, zlib,
chunk_size, chunk_size,
start=None, start=None,
...@@ -94,13 +95,15 @@ def create_giant_netcdf( ...@@ -94,13 +95,15 @@ def create_giant_netcdf(
): ):
frame = get_data(input_files) frame = get_data(input_files)
if frame.empty: if frame.empty:
raise ValueError("No data found from input files: {}".format(", ".join(input_files))) in_files_str = ", ".join(str(in_file) for in_file in input_files)
msg = f"No data found from input files: {in_files_str}"
raise ValueError(msg)
# Add wind direction components so we can average wind direction properly # Add wind direction components so we can average wind direction properly
frame["wind_east"], frame["wind_north"], _ = calc.wind_vector_components(frame["wind_speed"], frame["wind_dir"]) frame["wind_east"], frame["wind_north"], _ = calc.wind_vector_components(frame["wind_speed"], frame["wind_dir"])
if "air_temp" in frame and "rh" in frame and ("dewpoint" in database or "dewpoint_mean" in database): if "air_temp" in frame and "rh" in frame and ("dewpoint" in database or "dewpoint_mean" in database):
LOG.info("'dewpoint' is missing from the input file, will calculate " "it from air temp and relative humidity") LOG.info("'dewpoint' is missing from the input file, will calculate it from air temp and relative humidity")
frame["dewpoint"] = calc.dewpoint(frame["air_temp"], frame["rh"]) frame["dewpoint"] = calc.dewpoint(frame["air_temp"], frame["rh"])
# round up each 1 minute group so data at time T is the average of data # round up each 1 minute group so data at time T is the average of data
...@@ -128,7 +131,7 @@ def create_giant_netcdf( ...@@ -128,7 +131,7 @@ def create_giant_netcdf(
if start and end: if start and end:
frame = frame[start.strftime("%Y-%m-%d %H:%M:%S") : end.strftime("%Y-%m-%d %H:%M:%S")] frame = frame[start.strftime("%Y-%m-%d %H:%M:%S") : end.strftime("%Y-%m-%d %H:%M:%S")]
chunk_sizes = [chunk_size] if chunk_size and not isinstance(chunk_size, (list, tuple)) else [frame.shape[0]] chunk_sizes = [chunk_size] if chunk_size and not isinstance(chunk_size, list | tuple) else [frame.shape[0]]
first_stamp = datetime.strptime(str(frame.index[0]), "%Y-%m-%d %H:%M:%S") first_stamp = datetime.strptime(str(frame.index[0]), "%Y-%m-%d %H:%M:%S")
# NETCDF4_CLASSIC was chosen so that MFDataset reading would work. See: # NETCDF4_CLASSIC was chosen so that MFDataset reading would work. See:
...@@ -147,7 +150,7 @@ def create_giant_netcdf( ...@@ -147,7 +150,7 @@ def create_giant_netcdf(
write_global_attributes( write_global_attributes(
nc_file, nc_file,
[os.path.basename(x) for x in input_files], [x.name for x in input_files],
interval=interval_width, interval=interval_width,
datastream=datastream, datastream=datastream,
) )
...@@ -195,13 +198,13 @@ def main(): ...@@ -195,13 +198,13 @@ def main():
"--start-time", "--start-time",
type=_dt_convert, type=_dt_convert,
help="Start time of massive netcdf file, if only -s is given, a netcdf file for only that day is given" help="Start time of massive netcdf file, if only -s is given, a netcdf file for only that day is given"
+ ". Formats allowed: 'YYYY-MM-DDTHH:MM:SS', 'YYYY-MM-DD'", ". Formats allowed: 'YYYY-MM-DDTHH:MM:SS', 'YYYY-MM-DD'",
) )
parser.add_argument( parser.add_argument(
"-e", "-e",
"--end-time", "--end-time",
type=_dt_convert, type=_dt_convert,
help="End time of massive netcdf file. Formats allowed:" + "'YYYY-MM-DDTHH:MM:SS', 'YYYY-MM-DD'", help="End time of massive netcdf file. Formats allowed: 'YYYY-MM-DDTHH:MM:SS', 'YYYY-MM-DD'",
) )
parser.add_argument( parser.add_argument(
"-n", "-n",
...@@ -257,7 +260,8 @@ each input file is mapped to the corresponding output file. ...@@ -257,7 +260,8 @@ each input file is mapped to the corresponding output file.
if args.start_time and not args.end_time: if args.start_time and not args.end_time:
args.end_time = args.start_time.replace(hour=23, minute=59, second=59) args.end_time = args.start_time.replace(hour=23, minute=59, second=59)
elif not args.start_time and args.end_time: elif not args.start_time and args.end_time:
raise ValueError("start time must be specified when end time is specified") msg = "start time must be specified when end time is specified"
raise ValueError(msg)
mini_database = {k: schema.database_dict[k] for k in args.fields} mini_database = {k: schema.database_dict[k] for k in args.fields}
if args.summary: if args.summary:
...@@ -266,18 +270,19 @@ each input file is mapped to the corresponding output file. ...@@ -266,18 +270,19 @@ each input file is mapped to the corresponding output file.
# Case 1: All inputs to 1 output file # Case 1: All inputs to 1 output file
# Case 2: Each input in to a separate output file # Case 2: Each input in to a separate output file
if args.output_files and len(args.output_files) not in [1, len(args.input_files)]: if args.output_files and len(args.output_files) not in [1, len(args.input_files)]:
raise ValueError("Output filenames must be 1 or the same length as input files") msg = "Output filenames must be 1 or the same length as input files"
elif args.output_files and len(args.output_files) == len(args.input_files): raise ValueError(msg)
if args.output_files and len(args.output_files) == len(args.input_files):
args.input_files = [[i] for i in args.input_files] args.input_files = [[i] for i in args.input_files]
else: else:
args.input_files = [args.input_files] args.input_files = [args.input_files]
success = False success = False
for in_files, out_fn in zip(args.input_files, args.output_files): for in_files, out_fn in zip(args.input_files, args.output_files, strict=True):
try: try:
create_giant_netcdf( create_giant_netcdf(
in_files, [Path(in_file) for in_file in in_files],
out_fn, Path(out_fn),
args.zlib, args.zlib,
args.chunk_size, args.chunk_size,
args.start_time, args.start_time,
...@@ -291,7 +296,8 @@ each input file is mapped to the corresponding output file. ...@@ -291,7 +296,8 @@ each input file is mapped to the corresponding output file.
except (ValueError, TypeError): except (ValueError, TypeError):
LOG.error(f"Could not generate NetCDF file for {in_files}", exc_info=True) LOG.error(f"Could not generate NetCDF file for {in_files}", exc_info=True)
if not success: if not success:
raise OSError("All ASCII files were empty or could not be read") msg = "All ASCII files were empty or could not be read"
raise OSError(msg)
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -39,6 +39,7 @@ ignore = ["D100", "D101", "D102", "D103", "D104", "D105", "D106", "D107", "D203" ...@@ -39,6 +39,7 @@ ignore = ["D100", "D101", "D102", "D103", "D104", "D105", "D106", "D107", "D203"
[tool.ruff.per-file-ignores] [tool.ruff.per-file-ignores]
"aosstower/tests/*" = ["S", "PLR2004"] "aosstower/tests/*" = ["S", "PLR2004"]
"aosstower/level_b1/quicklook.py" = ["PLR0913"] "aosstower/level_b1/quicklook.py" = ["PLR0913"]
"aosstower/level_b1/nc.py" = ["PLR0913"]
[tool.ruff.pydocstyle] [tool.ruff.pydocstyle]
convention = "google" convention = "google"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment