Skip to content

Commit

Permalink
Updates to use Pathlib rather than str or os.path (#41)
Browse files Browse the repository at this point in the history
* Updates to use Pathlib rather than str or os.path

* formatting

* Updates to use Pathlib rather than str or os.path

* Update hermes_eea/calibration/calibration.py

Co-authored-by: Damian Barrous Dume <damianbarrous@gmail.com>

* More Updates for Pathlib

* Remove Unused Import

---------

Co-authored-by: Damian Barrous Dume <damianbarrous@gmail.com>
  • Loading branch information
Alrobbertz and dbarrous authored Jun 5, 2024
1 parent 20380d8 commit 0c49d5d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 54 deletions.
9 changes: 4 additions & 5 deletions hermes_eea/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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__}")
Expand Down
75 changes: 40 additions & 35 deletions hermes_eea/calibration/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = []
Expand All @@ -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.
"""
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand All @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions hermes_eea/io/file_tools.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand All @@ -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`
Expand Down
4 changes: 2 additions & 2 deletions hermes_eea/tests/go_plotly.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import sys
from pathlib import Path
import numpy as np
import math

Expand Down Expand Up @@ -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

Expand Down
11 changes: 6 additions & 5 deletions hermes_eea/tests/test_calibration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
import os.path
from pathlib import Path
import shutil
import tempfile
Expand All @@ -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


Expand All @@ -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
Expand All @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion hermes_eea/tests/test_file_tools.py
Original file line number Diff line number Diff line change
@@ -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
)

0 comments on commit 0c49d5d

Please sign in to comment.