Mercurial > repos > astroteam > plot_tools_astro_tool
comparison sky_plot.py @ 0:2b1759ccaa8b draft default tip
planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
author | astroteam |
---|---|
date | Fri, 25 Apr 2025 21:48:27 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:2b1759ccaa8b |
---|---|
1 #!/usr/bin/env python | |
2 # coding: utf-8 | |
3 | |
4 #!/usr/bin/env python | |
5 | |
6 # This script is generated with nb2galaxy | |
7 | |
8 # flake8: noqa | |
9 | |
10 import json | |
11 import os | |
12 import shutil | |
13 | |
14 from oda_api.json import CustomJSONEncoder | |
15 | |
16 fn = "data.tsv" # oda:POSIXPath | |
17 skiprows = 0 # http://odahub.io/ontology#Integer | |
18 sep = "whitespace" # http://odahub.io/ontology#String ; oda:allowed_value "auto", "comma", "tab", "whitespace", "semicolon" | |
19 | |
20 ra_col = "c3" # http://odahub.io/ontology#String | |
21 dec_col = "c4" # http://odahub.io/ontology#String | |
22 weight_col = "" # http://odahub.io/ontology#String | |
23 binsz = 0.02 # http://odahub.io/ontology#Float | |
24 window_size_RA = 2.0 # http://odahub.io/ontology#Degree | |
25 window_size_DEC = 2.0 # http://odahub.io/ontology#Degree | |
26 | |
27 _galaxy_wd = os.getcwd() | |
28 | |
29 with open("inputs.json", "r") as fd: | |
30 inp_dic = json.load(fd) | |
31 if "C_data_product_" in inp_dic.keys(): | |
32 inp_pdic = inp_dic["C_data_product_"] | |
33 else: | |
34 inp_pdic = inp_dic | |
35 fn = str(inp_pdic["fn"]) | |
36 skiprows = int(inp_pdic["skiprows"]) | |
37 sep = str(inp_pdic["sep"]) | |
38 ra_col = str(inp_pdic["ra_col"]) | |
39 dec_col = str(inp_pdic["dec_col"]) | |
40 weight_col = str(inp_pdic["weight_col"]) | |
41 binsz = float(inp_pdic["binsz"]) | |
42 window_size_RA = float(inp_pdic["window_size_RA"]) | |
43 window_size_DEC = float(inp_pdic["window_size_DEC"]) | |
44 | |
45 import astropy.units as u | |
46 import matplotlib.pyplot as plt | |
47 import numpy as np | |
48 import pandas as pd | |
49 from astropy.coordinates import SkyCoord | |
50 from gammapy.maps import Map | |
51 from oda_api.data_products import ImageDataProduct, PictureProduct | |
52 | |
53 separators = { | |
54 "tab": "\t", | |
55 "comma": ",", | |
56 "semicolon": ";", | |
57 "whitespace": "\s+", | |
58 "space": " ", | |
59 } | |
60 | |
61 df = None | |
62 | |
63 if sep == "auto": | |
64 for name, s in separators.items(): | |
65 try: | |
66 df = pd.read_csv(fn, sep=s, index_col=False, skiprows=skiprows) | |
67 if len(df.columns) > 2: | |
68 sep = s | |
69 print("Detected separator: ", name) | |
70 break | |
71 except Exception as e: | |
72 print("Separator ", s, " failed", e) | |
73 assert sep != "auto", "Failed to find valid separator" | |
74 | |
75 if df is None: | |
76 df = pd.read_csv(fn, sep=separators[sep], index_col=False) | |
77 | |
78 df.columns | |
79 | |
80 def read_data(df, colname, optional=False): | |
81 for i, c in enumerate(df.columns): | |
82 if colname == f"c{i+1}": | |
83 print(colname, c) | |
84 return df[c].values | |
85 elif colname == c: | |
86 print(colname, c) | |
87 return df[c].values | |
88 | |
89 assert optional, colname + " column not found" | |
90 return None | |
91 | |
92 ra = read_data(df, ra_col) | |
93 dec = read_data(df, dec_col) | |
94 w = read_data(df, weight_col, optional=True) | |
95 if w is None: | |
96 w = np.ones_like(ra) | |
97 | |
98 source = SkyCoord(ra=np.mean(ra) * u.deg, dec=np.mean(dec) * u.deg) | |
99 | |
100 map = Map.create( | |
101 binsz=binsz, | |
102 width=(window_size_RA * u.deg, window_size_DEC * u.deg), | |
103 frame="icrs", | |
104 axes=[], | |
105 skydir=SkyCoord(source), | |
106 ) | |
107 | |
108 map.fill_by_coord({"lat": dec * u.deg, "lon": ra * u.deg}, weights=w) | |
109 | |
110 map.plot() | |
111 plt.savefig("map.png") | |
112 | |
113 map.write("map.fits", overwrite=True) | |
114 fits_image = ImageDataProduct.from_fits_file("map.fits") | |
115 | |
116 plot = PictureProduct.from_file("map.png") | |
117 | |
118 plot = plot # http://odahub.io/ontology#ODAPictureProduct | |
119 fits_image = fits_image # http://odahub.io/ontology#Image | |
120 | |
121 # output gathering | |
122 _galaxy_meta_data = {} | |
123 _oda_outs = [] | |
124 _oda_outs.append(("out_sky_plot_plot", "plot_galaxy.output", plot)) | |
125 _oda_outs.append( | |
126 ("out_sky_plot_fits_image", "fits_image_galaxy.output", fits_image) | |
127 ) | |
128 | |
129 for _outn, _outfn, _outv in _oda_outs: | |
130 _galaxy_outfile_name = os.path.join(_galaxy_wd, _outfn) | |
131 if isinstance(_outv, str) and os.path.isfile(_outv): | |
132 shutil.move(_outv, _galaxy_outfile_name) | |
133 _galaxy_meta_data[_outn] = {"ext": "_sniff_"} | |
134 elif getattr(_outv, "write_fits_file", None): | |
135 _outv.write_fits_file(_galaxy_outfile_name) | |
136 _galaxy_meta_data[_outn] = {"ext": "fits"} | |
137 elif getattr(_outv, "write_file", None): | |
138 _outv.write_file(_galaxy_outfile_name) | |
139 _galaxy_meta_data[_outn] = {"ext": "_sniff_"} | |
140 else: | |
141 with open(_galaxy_outfile_name, "w") as fd: | |
142 json.dump(_outv, fd, cls=CustomJSONEncoder) | |
143 _galaxy_meta_data[_outn] = {"ext": "json"} | |
144 | |
145 with open(os.path.join(_galaxy_wd, "galaxy.json"), "w") as fd: | |
146 json.dump(_galaxy_meta_data, fd) | |
147 print("*** Job finished successfully ***") |