# HG changeset patch # User astroteam # Date 1752847995 0 # Node ID 8f94a9d810c27989c932260666b0521c24973797 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 7be57c88cfb29c1bc1db2afbd42d3d80fcf42985 diff -r 000000000000 -r 8f94a9d810c2 fits2tiff.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/fits2tiff.py Fri Jul 18 14:13:15 2025 +0000 @@ -0,0 +1,84 @@ +#!/usr/bin/env python +# coding: utf-8 + +#!/usr/bin/env python + +# This script is generated with nb2galaxy + +# flake8: noqa + +import json +import os +import shutil + +import tifffile +from astropy.io import fits + +file_input = "./input.fits" # oda:POSIXPath; oda:label "Input file" +input_hdu_no = 0 # oda:Integer; oda:label "Input HDU number" + +_galaxy_wd = os.getcwd() + +with open("inputs.json", "r") as fd: + inp_dic = json.load(fd) +if "C_data_product_" in inp_dic.keys(): + inp_pdic = inp_dic["C_data_product_"] +else: + inp_pdic = inp_dic +file_input = str(inp_pdic["file_input"]) +input_hdu_no = int(inp_pdic["input_hdu_no"]) + +try: + hdul = fits.open(file_input) + data = hdul[input_hdu_no].data + header = hdul[input_hdu_no].header + data = data.astype(data.dtype.newbyteorder("=")) +except: + raise RuntimeError("The input file should have the FITS format.") + +image_out_path = "./output.tiff" +tifffile.imwrite(image_out_path, data) + +dict_old = dict(header) +dict_json = {} +for key in dict_old.keys(): + if key != "COMMENT": + dict_json[key] = dict_old[key] + +file_output = image_out_path +header_json = dict_json + +# output gathering +_galaxy_meta_data = {} +_simple_outs = [] +_simple_outs.append( + ("out_fits2tiff_file_output", "file_output_galaxy.output", file_output) +) +_simple_outs.append( + ("out_fits2tiff_header_json", "header_json_galaxy.output", header_json) +) + +try: + import numpy as np # noqa: E402 + + _numpy_available = True +except ImportError: + _numpy_available = False + +for _outn, _outfn, _outv in _simple_outs: + _galaxy_outfile_name = os.path.join(_galaxy_wd, _outfn) + if isinstance(_outv, str) and os.path.isfile(_outv): + shutil.move(_outv, _galaxy_outfile_name) + _galaxy_meta_data[_outn] = {"ext": "_sniff_"} + elif _numpy_available and isinstance(_outv, np.ndarray): + with open(_galaxy_outfile_name, "wb") as fd: + np.savez(fd, _outv) + _galaxy_meta_data[_outn] = {"ext": "npz"} + else: + with open(_galaxy_outfile_name, "w") as fd: + json.dump(_outv, fd) + _galaxy_meta_data[_outn] = {"ext": "expression.json"} + +with open(os.path.join(_galaxy_wd, "galaxy.json"), "w") as fd: + json.dump(_galaxy_meta_data, fd) +print("*** Job finished successfully ***") diff -r 000000000000 -r 8f94a9d810c2 fits2tiff_astro_tool.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/fits2tiff_astro_tool.xml Fri Jul 18 14:13:15 2025 +0000 @@ -0,0 +1,48 @@ + + + astropy + tifffile + ipython + + python '$__tool_directory__/fits2tiff.py' + + $__tool_directory__ + $__tool_directory__ + + + + + + + + + + + + + + + + + + + + + + This tool converts images stored in FITS files to TIFF format. If the +FITS file contains multiple HDUs, the desired HDU index can be +specified. + +**Important**: The image data itself is not altered during the +conversion. + +In addition to the TIFF file, the tool also outputs a JSON file +containing the metadata from the header of the selected FITS HDU. + + + 10.1051/0004-6361/201322068 + 10.3847/1538-3881/aabc4f + 10.3847/1538-4357/ac7c74 + 10.5281/zenodo.15645336 + + \ No newline at end of file