diff --git a/hermes_eea/__init__.py b/hermes_eea/__init__.py index c09afe2..2a97891 100644 --- a/hermes_eea/__init__.py +++ b/hermes_eea/__init__.py @@ -1,6 +1,5 @@ # Licensed under Apache License v2 - see LICENSE.rst -import os.path -import sys +from pathlib import Path from hermes_core import log from hermes_eea.io.file_tools import read_file @@ -20,9 +19,9 @@ INST_TO_SHORTNAME = {INST_NAME: INST_SHORTNAME} INST_TO_TARGETNAME = {INST_NAME: INST_TARGETNAME} -_package_directory = os.path.dirname(os.path.abspath(__file__)) -_data_directory = os.path.abspath(os.path.join(_package_directory, "data")) -_calibration_directory = os.path.abspath(os.path.join(_data_directory, "calibration")) +_package_directory = Path(__file__).parent +_data_directory = _package_directory / "data" +_calibration_directory = _data_directory / "calibration" log.info(f"hermes_eea version: {__version__}") diff --git a/hermes_eea/calibration/calibration.py b/hermes_eea/calibration/calibration.py index 8a783bd..6093882 100644 --- a/hermes_eea/calibration/calibration.py +++ b/hermes_eea/calibration/calibration.py @@ -2,9 +2,6 @@ A module for all things calibration. """ -from datetime import datetime, timezone, timedelta -import random -import os.path from pathlib import Path import sys import ccsdspy @@ -36,33 +33,37 @@ def process_file(data_filename: Path) -> list: """ This is the entry point for the pipeline processing. - It runs all of the various processing steps required - to create a L1A Hermes CDF File + It runs all of the various processing steps required to create a L1A Hermes CDF File. calls: - calibrate_file() - parse_science - CCSDSPY (parse_L0_sci_packets()) - l0_sci_data_to_cdf() - SkymapFactory() - Use HermesData to populate CDF output file - Write the File - A Custom EEA SkymapFactory - HermesData + + .. code-block:: python + + calibrate_file(...) + parse_science_filename(...) + parse_l0_sci_packets(...) + l0_sci_data_to_cdf(...) + SkymapFactory(...) + # Use HermesData to populate CDF output file + Hermes_EEA_Data_Processor(...).build_HermesData() + # Write the File + hermes_eea_data.save(...) + # A Custom EEA SkymapFactory + # HermesData + Parameters ---------- - data_filename: str + data_filename: `pathlib.Path` Fully specificied filename of an input file. - The file contents: - Traditional binary packets in CCSDS format + The file contents: Traditional binary packets in CCSDS format Returns ------- - output_filenames: list - Fully specificied filenames for the output files. - The file contents: - CDF formatted file with n packets iincluding time and [41,4,32] skymap. + output_files: `list[pathlib.Path]` + Fully specificied filenames for the output CDF files. + The file contains CDF formatted file with n packets iincluding time and [41,4,32] skymap. + """ log.info(f"Processing file {data_filename}.") output_files = [] @@ -82,19 +83,21 @@ def process_file(data_filename: Path) -> list: return output_files -def calibrate_file(data_filename: Path, destination_dir) -> Path: +def calibrate_file(data_filename: Path, destination_dir: Path) -> Path: """ Given an input data file, raise it to the next level (e.g. level 0 to level 1, level 1 to quicklook) it and return a new file. Parameters ---------- - data_filename: Path + data_filename: `pathlib.Path` Fully specificied filename of the input data file. + destination_dir: `pathlib.Path` + Fully specificied directory where the output file will be written. Returns ------- - output_filename: Path + output_filename: `pathlib.Path` Fully specificied filename of the output file. """ @@ -156,7 +159,7 @@ def parse_l0_sci_packets(data_filename: Path) -> dict: Parameters ---------- - data_filename: str + data_filename: `pathlib.Path` Fully specificied filename Returns @@ -173,7 +176,7 @@ def parse_l0_sci_packets(data_filename: Path) -> dict: log.info(f"Parsing packets from file:{data_filename}.") pkt = ccsdspy.FixedLength.from_file( - os.path.join(hermes_eea._data_directory, "hermes_EEA_sci_packet_def.csv") + Path(hermes_eea._data_directory) / "hermes_EEA_sci_packet_def.csv" ) data = pkt.load(data_filename) return data @@ -189,12 +192,14 @@ def l0_sci_data_to_cdf( ---------- data: dict A dictionary of arrays which includes the ccsds header fields - original_filename: Path + original_filename: `pathlib.Path` The Path to the originating file. + destination_dir: `pathlib.Path` + The directory where the output file will be written. Returns ------- - output_filename: Path + output_filename: `pathlib.Path` Fully specificied filename of cdf file Examples @@ -243,7 +248,7 @@ def l0_sci_data_to_cdf( try: # this writes out the data to CDF file format cdf_path = hermes_eea_factory.hermes_eea_data.save( - str(destination_dir), True + output_path=destination_dir, overwrite=True ) except Exception as e: log.error(e) @@ -257,19 +262,19 @@ def get_calibration_file(data_filename: Path, time=None) -> Path: Given a time, return the appropriate calibration file. Parameters ---------- - data_filename: str + data_filename: `pathlib.Path` Fully specificied filename of the non-calibrated file (data level < 2) time: ~astropy.time.Time Returns ------- - calib_filename: str + calib_filename: `pathlib.Path` Fully specificied filename for the appropriate calibration file. Examples -------- """ - return os.path.join(hermes_eea._calibration_directory, data_filename) + return Path(hermes_eea._calibration_directory) / data_filename def read_calibration_file(calib_filename: Path): @@ -278,18 +283,18 @@ def read_calibration_file(calib_filename: Path): Parameters ---------- - calib_filename: str + calib_filename: `pathlib.Path` Fully specificied filename of the non-calibrated file (data level < 2) Returns ------- - output_filename: str + output_filename: `pathlib.Path` Fully specificied filename of the appropriate calibration file. Examples -------- """ - lines = read_file(os.path.join(calib_filename)) + lines = read_file(calib_filename) calib.energies = [] calib.deflections = [] for line in lines: diff --git a/hermes_eea/io/file_tools.py b/hermes_eea/io/file_tools.py index 2a881b1..7529048 100644 --- a/hermes_eea/io/file_tools.py +++ b/hermes_eea/io/file_tools.py @@ -1,6 +1,6 @@ +from pathlib import Path from ccsdspy import FixedLength import numpy as np -import os """ This module provides a generic file reader. @@ -9,13 +9,13 @@ __all__ = ["read_file", "read_ccsds"] -def read_file(data_filename): +def read_file(data_filename: Path): """ Read a file. Parameters ---------- - data_filename: str + data_filename: `pathlib.Path` A file to read. Returns @@ -29,16 +29,16 @@ def read_file(data_filename): with open(data_filename) as fh: return fh.readlines() except Exception: - raise Exception("Could not find: " + os.path.abspath(data_filename)) + raise Exception("Could not find: " + data_filename) -def read_ccsds(filename: str, pkt_def: FixedLength): +def read_ccsds(filename: Path, pkt_def: FixedLength): """ Read a ccsds packet file. Parameters ---------- - filename: str + filename: `pathlib.Path` A file to read. pkt_def: `ccsdspy.FixedLength` diff --git a/hermes_eea/tests/go_plotly.py b/hermes_eea/tests/go_plotly.py index 8d055a3..84b6859 100644 --- a/hermes_eea/tests/go_plotly.py +++ b/hermes_eea/tests/go_plotly.py @@ -1,5 +1,5 @@ -import os import sys +from pathlib import Path import numpy as np import math @@ -40,7 +40,7 @@ def read_data(file): print("file len:", length) return cdffile except Exception: - if not os.path.exists(file): + if not Path(file).exists(): print(file + " does not exist") return None diff --git a/hermes_eea/tests/test_calibration.py b/hermes_eea/tests/test_calibration.py index 453b184..b1b872c 100644 --- a/hermes_eea/tests/test_calibration.py +++ b/hermes_eea/tests/test_calibration.py @@ -1,5 +1,4 @@ import pytest -import os.path from pathlib import Path import shutil import tempfile @@ -16,7 +15,7 @@ @pytest.fixture(scope="session") # this is a pytest fixture def small_level0_file(tmp_path_factory): - fn = Path(os.path.join(_data_directory, "hermes_EEA_l0_2023042-000000_v0.bin")) + fn = Path(_data_directory) / "hermes_EEA_l0_2023042-000000_v0.bin" return fn @@ -32,7 +31,7 @@ def test_read_ccsdspy(small_level0_file): """ pkt = ccsdspy.FixedLength.from_file( - os.path.join(hermes_eea._data_directory, "hermes_EEA_sci_packet_def.csv") + Path(hermes_eea._data_directory) / "hermes_EEA_sci_packet_def.csv" ) result = read_ccsds(small_level0_file, pkt) assert len(result["ACCUM"]) == 3051 @@ -54,10 +53,12 @@ def test_process_file(small_level0_file): # Process the File output_files = calib.process_file(temp_test_file_path) - assert os.path.getsize(output_files[0]) > 0 + for f in output_files: + assert isinstance(f, Path) + assert output_files[0].stat().st_size > 0 # Ensure the file is closed before attempting to delete it - with pycdf.CDF(output_files[0]) as cdf: + with pycdf.CDF(str(output_files[0])) as cdf: assert len(cdf["Epoch"][:]) == 18 # Ensure the temporary directory is cleaned up even if an exception is raised (needed for Windows) diff --git a/hermes_eea/tests/test_file_tools.py b/hermes_eea/tests/test_file_tools.py index 37ad58e..41d2a67 100644 --- a/hermes_eea/tests/test_file_tools.py +++ b/hermes_eea/tests/test_file_tools.py @@ -1,5 +1,8 @@ +from pathlib import Path from hermes_eea.io.file_tools import read_file def test_read_file(): - assert read_file("./hermes_eea/data/calibration/flight_stepper.txt") is not None + assert ( + read_file(Path("./hermes_eea/data/calibration/flight_stepper.txt")) is not None + )