Skip to content

Commit ba7ea7a

Browse files
TRT-635: Refine HarmonyAdapter.reformat method.
1 parent bf0e796 commit ba7ea7a

3 files changed

Lines changed: 49 additions & 35 deletions

File tree

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ HGA follows semantic versioning. All notable changes to this project will be
44
documented in this file. The format is based on [Keep a
55
Changelog](http://keepachangelog.com/en/1.0.0/).
66

7+
## [v3.0.3] - 2025-09-23
8+
9+
### Changed:
10+
11+
* This version of HGA tidies up the `HarmonyAdapter.reformat` method to ensure
12+
the following behaviour:
13+
* If a GeoTIFF is requested, no reformatting is required and a GeoTIFF is returned.
14+
* If no output format is requested, a GeoTIFF is returned.
15+
* If a netCDF4 is requested, the output is reformatted to netCDF4.
16+
* If any other format is requested, that reformatting is left to downstream
17+
steps in the Harmony service chain. Harmony should already have limited the
18+
requested output formats to those that can be handled by service chains
19+
including HGA, per the service chain configuration.
20+
721
## [v3.0.2] - 2025-09-22
822

923
### Changed:

gdal_subsetter/transform.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@
5050
get_version,
5151
has_world_file,
5252
is_geotiff,
53-
mime_to_extension,
54-
mime_to_gdal,
5553
OpenGDAL,
5654
process_flags,
5755
rename_file,
@@ -67,6 +65,8 @@ class HarmonyAdapter(BaseHarmonyAdapter):
6765
6866
"""
6967

68+
service_version = get_version()
69+
7070
def process_item(self, input_item: Item, source: HarmonySource) -> Item:
7171
"""Applies GDAL transformations to input GeoTIFFs, returns output STAC item
7272
@@ -150,7 +150,7 @@ def process_item(self, input_item: Item, source: HarmonySource) -> Item:
150150
if layernames and transformed_filename:
151151
transformed_bbox = self.get_bbox_lonlat(transformed_filename)
152152
# May need to convert transformed output to the MIME type
153-
# specified in the request (e.g., GeoTIFF to NetCDF-4).
153+
# specified in the request (e.g., netCDF-4).
154154
transformed_filename = self.reformat(transformed_filename, output_dir)
155155
return self.stage_and_get_output_stac(
156156
transformed_filename, transformed_bbox, input_item, output_basename
@@ -652,48 +652,48 @@ def stack_multi_file_with_metadata(
652652
dst_ds = None
653653
return outfile
654654

655-
def reformat(self, srcfile: str, dstdir: str) -> str:
656-
"""This method converts the transformed outputs produced by the
657-
service into a file with the requested MIME type. For example,
658-
NetCDF-4 requested outputs, will need to be converted and
659-
aggregated from transformed GeoTIFFs back to a NetCDF-4 file.
655+
def reformat(self, transformed_geotiff_file: str, dstdir: str) -> str:
656+
"""If necessary, convert transformed outputs to the requested MIME type.
657+
658+
* GeoTIFF or no specified output format, will return the GeoTIFF file
659+
with a metadata tag stating the service used to generate the file.
660+
* netCDF4 requested output will result in reformatting.
661+
* Other output formats are not supported within this service. But
662+
Harmony only allows requests with correct formats to be sent to
663+
service chains, so if the requested output format is not GeoTIFF or
664+
netCDF4, then the assumption is a downstream step in a service chain
665+
will handle that reformatting.
660666
661667
The return value of this method is the path to the output file with
662668
the requested MIME type.
663669
664670
"""
665-
gdal_subsetter_version = f"gdal_subsetter_version={get_version()}"
666671
output_mime = self.message.format.process("mime")
667-
if not output_mime == "image/png" or output_mime == "image/jpeg":
668-
command = ["gdal_edit.py", "-mo", gdal_subsetter_version, srcfile]
669-
self.execute_gdal_command(*command)
670-
if output_mime not in mime_to_gdal:
671-
raise Exception(f"Unrecognized output format: {output_mime}")
672-
if output_mime == "image/tiff":
673-
return srcfile
674-
675-
dstfile = os.path.join(dstdir, f"translated.{mime_to_extension[output_mime]}")
676672

677-
if output_mime in ("application/x-netcdf4", "application/x-zarr"):
678-
dstfile = os.path.join(dstdir, "translated.nc")
679-
dstfile = convert_geotiff_to_netcdf(srcfile, dstfile)
680-
return dstfile
673+
if output_mime == "application/x-netcdf4":
674+
formatted_file_path = os.path.join(dstdir, "translated.nc")
675+
formatted_file_path = convert_geotiff_to_netcdf(
676+
transformed_geotiff_file,
677+
formatted_file_path,
678+
)
681679
else:
682-
# png, jpeg, gif, etc.
683-
if os.path.exists(f"{os.path.splitext(srcfile)[0]}.wld"):
684-
# don't need to reformat files that have been processed earlier
685-
return srcfile
686-
680+
# Covers:
681+
# * GeoTIFF requested
682+
# * No output specified (GeoTIFF default),
683+
# * An alternative format that would be handled in downstream
684+
# services in the chain.
685+
gdal_subsetter_version = f"gdal_subsetter_version={self.service_version}"
686+
# TODO: Add to consolidated gdal_translate command.
687687
command = [
688-
"gdal_translate",
689-
"-of",
690-
mime_to_gdal[output_mime],
691-
"-scale",
692-
srcfile,
693-
dstfile,
688+
"gdal_edit.py",
689+
"-mo",
690+
gdal_subsetter_version,
691+
transformed_geotiff_file,
694692
]
695693
self.execute_gdal_command(*command)
696-
return dstfile
694+
formatted_file_path = transformed_geotiff_file
695+
696+
return formatted_file_path
697697

698698
def get_variables(self, filename: str):
699699
"""filename is either nc or tif."""

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0.2
1+
3.0.3

0 commit comments

Comments
 (0)