Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MNT: Rename description to descriptor in filename variable #313

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions imap_processing/cdf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def calc_start_time(shcoarse_time: int):

def write_cdf(
data: xr.Dataset,
description: str = "",
descriptor: str,
directory: Optional[Path] = None,
):
"""Write the contents of "data" to a CDF file using cdflib.xarray_to_cdf.
Expand All @@ -57,10 +57,10 @@ def write_cdf(
----------
data : xarray.Dataset
The dataset object to convert to a CDF
description : str, optional
The description to insert into the file name after the
orbit, before the SPICE field. No underscores allowed.
directory : pathlib.Path
descriptor : str
The descriptor to insert into the file name after the
orbit, before the SPICE field. No underscores allowed.
directory : pathlib.Path, optional
The directory to write the file to. The default is obtained
from the global imap_processing.config["DATA_DIR"].

Expand All @@ -84,13 +84,6 @@ def write_cdf(

date_string = np.datetime_as_string(file_start_date, unit="D").replace("-", "")

# Determine the optional "description" field
description = (
description
if (description.startswith("_") or not description)
else f"_{description}"
)

# Determine the file name based on the attributes in the xarray
# Set file name based on this convention:
# imap_<instrument>_<datalevel>_<descriptor>_<startdate>_
Expand All @@ -99,11 +92,10 @@ def write_cdf(
# like this:
# imap_idex_l1
filename = (
data.attrs["Logical_source"]
+ description
+ "_"
+ date_string
+ f"_v{data.attrs['Data_version']}.cdf"
f"{data.attrs['Logical_source']}"
f"_{descriptor}"
f"_{date_string}"
f"_v{data.attrs['Data_version']}.cdf"
)

if directory is None:
Expand Down
2 changes: 1 addition & 1 deletion imap_processing/codice/codice_l1a.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def codice_l1a(packets: list[space_packet_parser.parser.Packet]) -> str:
# Write data to CDF
cdf_filename = write_cdf(
data,
description="hk",
descriptor="hk",
)

return cdf_filename
2 changes: 1 addition & 1 deletion imap_processing/swe/l1a/swe_l1a.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ def swe_l1a(packets):
mode = f"{data['APP_MODE'].data[0]}-" if apid == SWEAPID.SWE_APP_HK else ""
return write_cdf(
data,
description=f"{mode}{filename_descriptors.get(apid)}",
descriptor=f"{mode}{filename_descriptors.get(apid)}",
)
2 changes: 1 addition & 1 deletion imap_processing/swe/l1b/swe_l1b.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ def swe_l1b(l1a_dataset: xr.Dataset):
mode = f"{data['APP_MODE'].data[0]}-" if apid == SWEAPID.SWE_APP_HK else ""
return write_cdf(
data,
description=f"{mode}{filename_descriptors.get(apid)}",
descriptor=f"{mode}{filename_descriptors.get(apid)}",
)
8 changes: 4 additions & 4 deletions imap_processing/tests/cdf/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ def test_write_cdf(tmp_path):
)
dataset["Epoch"].attrs = ConstantCoordinates.EPOCH

fname = write_cdf(dataset, description="test-description")
fname = write_cdf(dataset, descriptor="test-descriptor")
assert fname.exists()
assert fname.name == "imap_test_l1_test-description_20100101_v01.cdf"
assert fname.name == "imap_test_l1_test-descriptor_20100101_v01.cdf"
# Created automatically for us
dir_structure = fname.parts[-5:-1]
# instrument, level, year, month
assert dir_structure == ("test", "l1", "2010", "01")

# Test an explicit directory doesn't create that structure
filename = write_cdf(dataset, description="test-description", directory=tmp_path)
filename = write_cdf(dataset, descriptor="test-descriptor", directory=tmp_path)
assert filename.exists()
assert filename.name == "imap_test_l1_test-description_20100101_v01.cdf"
assert filename.name == "imap_test_l1_test-descriptor_20100101_v01.cdf"
# Created automatically for us
dir_structure = filename.parts[-5:-1]
# It should be the same as the tmp_path structure (minus the file name)
Expand Down
12 changes: 6 additions & 6 deletions imap_processing/tests/idex/test_l1_cdfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ def decom_test_data():

def test_idex_cdf_file(decom_test_data):
# Verify that a CDF file can be created with no errors thrown by xarray_to_cdf
file_name = write_cdf(decom_test_data.data)
file_name = write_cdf(decom_test_data.data, descriptor="test")
date_to_test = "20250724"
assert file_name.name == (
f"{decom_test_data.data.attrs['Logical_source']}_"
f"{decom_test_data.data.attrs['Logical_source']}_test_"
f"{date_to_test}_v{idex.__version__}.cdf"
)
assert file_name.exists()
Expand All @@ -29,7 +29,7 @@ def test_bad_cdf_attributes(decom_test_data):
# Deliberately mess up the attributes to verify that an ISTPError is raised
del decom_test_data.data["TOF_High"].attrs["DEPEND_1"]
with pytest.raises(ISTPError):
write_cdf(decom_test_data.data)
write_cdf(decom_test_data.data, descriptor="test")


def test_bad_cdf_file_data(decom_test_data):
Expand Down Expand Up @@ -58,12 +58,12 @@ def test_bad_cdf_file_data(decom_test_data):
decom_test_data.data["Bad_data"] = bad_data_xr

with pytest.raises(ISTPError):
write_cdf(decom_test_data.data)
write_cdf(decom_test_data.data, descriptor="test")


def test_descriptor_in_file_name(decom_test_data):
# Deliberately mess up the data to verify no CDF is created
file_name = write_cdf(decom_test_data.data, description="impact-lab-test001")
file_name = write_cdf(decom_test_data.data, descriptor="impact-lab-test001")
date_to_test = "20250724"
assert file_name.name == (
f"{decom_test_data.data.attrs['Logical_source']}_"
Expand All @@ -78,7 +78,7 @@ def test_idex_tof_high_data_from_cdf(decom_test_data):
with open("imap_processing/tests/idex/impact_14_tof_high_data.txt") as f:
data = np.array([int(line.rstrip()) for line in f])

file_name = write_cdf(decom_test_data.data)
file_name = write_cdf(decom_test_data.data, descriptor="test")
l1_data = cdf_to_xarray(
file_name
) # Read in the data from the CDF file to an xarray object
Expand Down