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."""
0 commit comments