Skip to content

Commit f442e20

Browse files
author
Kim Cunningham
committed
Added use of harmony filename generator for CASPER created files
1 parent 6c068f3 commit f442e20

2 files changed

Lines changed: 12 additions & 9 deletions

File tree

casper/convert_to_csv.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from logging import Logger
66

77
import xarray as xr
8+
from harmony_service_lib.util import generate_output_filename
89

910
from casper.file_ops import (
1011
valid_input_file,
@@ -131,7 +132,9 @@ def convert_to_csv(fname: str, zip_file: str, logger: Logger = default_logger) -
131132

132133
for idx in range(len(vals)):
133134
dims, vvs = vals[idx]
135+
# Use Harmony generated filename
134136
op_file = f"{input_filename}-{idx}.csv"
137+
op_file = generate_output_filename(op_file, ext="csv", is_reformatted=True)
135138
with zf.open(op_file, "w", force_zip64=True) as csv_file:
136139
ds = xr.combine_by_coords([data[vv].rename(vv) for vv in vvs])
137140
# Order columns: dimensions, non-dimensional coordinates, rest of variables

casper/harmony/service_adapter.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from uuid import uuid4
3333

3434
from harmony_service_lib.adapter import BaseHarmonyAdapter
35-
from harmony_service_lib.util import stage
35+
from harmony_service_lib.util import generate_output_filename, stage
3636
from pystac import Catalog, Item
3737
from pystac.item import Asset
3838

@@ -98,8 +98,8 @@ def process_file(self, catalog: Catalog) -> list[Catalog]:
9898
netcdf_url = _get_item_url(items[0])
9999
if netcdf_url is None:
100100
raise ValueError("No URL found for item")
101-
filename = Path(netcdf_url).stem
102101

102+
zip_file = ""
103103
with TemporaryDirectory() as temp_dir:
104104
# Download file
105105
input_file = download_file(
@@ -112,19 +112,20 @@ def process_file(self, catalog: Catalog) -> list[Catalog]:
112112
# Create the subdirectory
113113
self.logger.info("Running Casper.")
114114

115+
# Use Harmony generated filename
116+
zip_file = generate_output_filename(zip_file_name, ext="zip", is_reformatted=True)
117+
zip_file = f"{temp_dir}/{zip_file}"
118+
115119
# --- Run Casper ---
116-
zip_file = f"{temp_dir}/{zip_file_name}.zip"
117120
convert_to_csv(
118121
input_file,
119122
zip_file,
120123
logger=self.logger,
121124
)
122125

123-
self.logger.info(
124-
f"Casper conversion completed. Zip file created {zip_file_name}.zip"
125-
)
126-
staged_url = self._stage(zip_file, f"{zip_file_name}.zip", "application/zip")
126+
self.logger.info(f"Casper conversion completed. Zip file created {zip_file}")
127127

128+
staged_url = self._stage(zip_file, f"{zip_file.split('/')[-1]}", "application/zip")
128129
# -- Output to STAC catalog --
129130
result.clear_items()
130131
properties = {
@@ -138,11 +139,10 @@ def process_file(self, catalog: Catalog) -> list[Catalog]:
138139
None,
139140
properties,
140141
)
141-
filename = f"{zip_file_name}.zip"
142142

143143
asset = Asset(
144144
staged_url,
145-
title=filename,
145+
title=f"{zip_file.split('/')[-1]}",
146146
media_type="application/zip",
147147
roles=["data"],
148148
)

0 commit comments

Comments
 (0)