Skip to content

Commit

Permalink
MNT: Remove directory option from write_cdf (#379)
Browse files Browse the repository at this point in the history
We are now explicitly requiring a directory structure for the
science files to be written to. A user should update the
imap_data_access.config["DATA_DIR"] location if they want to write
to a different location because the ScienceFilePath uses that
field to construct the path.
  • Loading branch information
greglucas authored Apr 2, 2024
1 parent ff30e4c commit 021ea9a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 19 deletions.
15 changes: 5 additions & 10 deletions imap_processing/cdf/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""Various utility functions to support creation of CDF files."""

import logging
from pathlib import Path
from typing import Optional

import imap_data_access
import numpy as np
Expand Down Expand Up @@ -42,7 +40,7 @@ def calc_start_time(shcoarse_time: int):
return launch_time + time_delta


def write_cdf(dataset: xr.Dataset, directory: Optional[Path] = None):
def write_cdf(dataset: xr.Dataset):
"""Write the contents of "data" to a CDF file using cdflib.xarray_to_cdf.
This function determines the file name to use from the global attributes,
Expand All @@ -56,17 +54,12 @@ def write_cdf(dataset: xr.Dataset, directory: Optional[Path] = None):
----------
dataset : xarray.Dataset
The dataset object to convert to a CDF
filepath: Path
The output path, including filename, to write the CDF to.
Returns
-------
pathlib.Path
Path to the file created
"""
# Use the directory if provided, otherwise use the default
directory = directory or imap_data_access.config["DATA_DIR"]

# Create the filename from the global attributes
# Logical_source looks like "imap_swe_l2_counts-1min"
instrument, data_level, descriptor = dataset.attrs["Logical_source"].split("_")[1:]
Expand All @@ -83,9 +76,11 @@ def write_cdf(dataset: xr.Dataset, directory: Optional[Path] = None):
version=version,
repointing=repointing,
)
file_path = directory / science_file.construct_path()
file_path = science_file.construct_path()
if not file_path.parent.exists():
logger.info("The directory does not exist, creating directory %s", directory)
logger.info(
"The directory does not exist, creating directory %s", file_path.parent
)
file_path.parent.mkdir(parents=True)
# Insert the final attribute:
# The Logical_file_id is always the name of the file without the extension
Expand Down
11 changes: 2 additions & 9 deletions imap_processing/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,15 +364,8 @@ def process(self):
# read CDF file
l1a_dataset = cdf_to_xarray(dependencies[0])
processed_data = swe_l1b(l1a_dataset)
# TODO: Update this descriptor
descriptor = "test"
file = imap_data_access.ScienceFilePath.generate_from_inputs(
"swe", "l1b", descriptor, self.start_date, self.version
)

cdf_file_path = write_cdf(
data=processed_data, filepath=file.construct_path()
)
# TODO: Pass in the proper version and descriptor
cdf_file_path = write_cdf(data=processed_data)
print(f"processed file path: {cdf_file_path}")
if self.upload_to_sdc:
imap_data_access.upload(cdf_file_path)
Expand Down
2 changes: 2 additions & 0 deletions imap_processing/tests/cdf/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import imap_data_access
import numpy as np
import xarray as xr

Expand Down Expand Up @@ -34,3 +35,4 @@ def test_write_cdf():
file_path = write_cdf(dataset)
assert file_path.exists()
assert file_path.name == "imap_swe_l1_sci_20100101_v001.cdf"
assert file_path.relative_to(imap_data_access.config["DATA_DIR"])

0 comments on commit 021ea9a

Please sign in to comment.