annotate Spectrum.py @ 1:593c4b45eda5 draft default tip

planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
author astroteam
date Thu, 18 Apr 2024 09:26:15 +0000
parents 02e4bb4fa10c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
1 #!/usr/bin/env python
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
2 # coding: utf-8
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
3
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
4 # flake8: noqa
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
5
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
6 import json
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
7 import os
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
8 import shutil
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
9
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
10 import matplotlib.pyplot as plt
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
11 import numpy as np
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
12 from astropy.coordinates import SkyCoord
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
13 from astropy.io import fits
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
14 from astropy.time import Time
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
15 from numpy import log10, sqrt
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
16 from oda_api.data_products import ODAAstropyTable, PictureProduct
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
17 from oda_api.json import CustomJSONEncoder
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
18
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
19 if os.path.exists("hess_dl3_dr1.tar.gz") == False:
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
20 get_ipython().system( # noqa: F821
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
21 "wget https://zenodo.org/record/1421099/files/hess_dl3_dr1.tar.gz"
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
22 )
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
23 get_ipython().system("tar -zxvf hess_dl3_dr1.tar.gz") # noqa: F821
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
24 from oda_api.api import ProgressReporter
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
25
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
26 pr = ProgressReporter()
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
27 pr.report_progress(stage="Progress", progress=0.0)
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
28
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
29 # src_name='Crab' #http://odahub.io/ontology#AstrophysicalObject
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
30 # RA = 83.628700 # http://odahub.io/ontology#PointOfInterestRA
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
31 # DEC = 22.014700 # http://odahub.io/ontology#PointOfInterestDEC
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
32 src_name = "PKS 2155-304" # http://odahub.io/ontology#AstrophysicalObject
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
33 RA = 329.716938 # http://odahub.io/ontology#PointOfInterestRA
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
34 DEC = -30.225588 # http://odahub.io/ontology#PointOfInterestDEC
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
35
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
36 T1 = "2000-10-09T13:16:00.0" # http://odahub.io/ontology#StartTime
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
37 T2 = "2022-10-10T13:16:00.0" # http://odahub.io/ontology#EndTime
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
38 Radius = 2.5 # http://odahub.io/ontology#AngleDegrees
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
39 R_s = 0.2 # http://odahub.io/ontology#AngleDegrees
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
40
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
41 Emin = 0.1 # http://odahub.io/ontology#Energy_TeV
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
42 Emax = 100.0 # http://odahub.io/ontology#Energy_TeV
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
43 NEbins = 30 # http://odahub.io/ontology#Integer
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
44
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
45 Efit_min = 0.2 # http://odahub.io/ontology#Energy_TeV
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
46 Efit_max = 10.0 # http://odahub.io/ontology#Energy_TeV
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
47
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
48 _galaxy_wd = os.getcwd()
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
49
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
50 with open("inputs.json", "r") as fd:
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
51 inp_dic = json.load(fd)
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
52 if "_data_product" in inp_dic.keys():
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
53 inp_pdic = inp_dic["_data_product"]
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
54 else:
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
55 inp_pdic = inp_dic
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
56
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
57 for vn, vv in inp_pdic.items():
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
58 if vn != "_selector":
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
59 globals()[vn] = type(globals()[vn])(vv)
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
60
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
61 E0 = 1.0
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
62
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
63 def model_dNdE(E, N, Gam):
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
64 return N * (E / E0) ** (Gam)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
65
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
66 def model_rate(E1, E2, N, Gam):
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
67 dEE = E2 - E1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
68 EE = sqrt(E1 * E2)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
69 return model_dNdE(EE, N, Gam) * dEE
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
70
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
71 Ebins = np.logspace(log10(Emin), log10(Emax), NEbins + 1)
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
72 Emins = Ebins[:-1]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
73 Emaxs = Ebins[1:]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
74 Emeans = sqrt(Emins * Emaxs)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
75 lgEmeans = log10(Emeans)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
76 dE = Ebins[1:] - Ebins[:-1]
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
77
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
78 T1 = Time(T1, format="isot", scale="utc").mjd
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
79 T2 = Time(T2, format="isot", scale="utc").mjd
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
80 message = ""
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
81 RA_pnts = []
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
82 DEC_pnts = []
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
83 DL3_files = []
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
84 OBSIDs = []
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
85 Tstart = []
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
86 Tstop = []
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
87 flist = os.listdir("data")
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
88 for f in flist:
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
89 if f[-7:] == "fits.gz":
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
90 DL3_files.append(f)
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
91 OBSIDs.append(int(f[20:26]))
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
92 hdul = fits.open("data/" + f)
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
93 RA_pnts.append(float(hdul[1].header["RA_PNT"]))
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
94 DEC_pnts.append(float(hdul[1].header["DEC_PNT"]))
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
95 Tstart.append(
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
96 Time(
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
97 hdul[1].header["DATE-OBS"] + "T" + hdul[1].header["TIME-OBS"],
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
98 format="isot",
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
99 scale="utc",
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
100 ).mjd
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
101 )
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
102 Tstop.append(
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
103 Time(
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
104 hdul[1].header["DATE-END"] + "T" + hdul[1].header["TIME-END"],
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
105 format="isot",
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
106 scale="utc",
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
107 ).mjd
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
108 )
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
109 hdul.close()
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
110
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
111 Coords_s = SkyCoord(RA, DEC, unit="degree")
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
112 COORDS_pnts = SkyCoord(RA_pnts, DEC_pnts, unit="degree")
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
113 seps = COORDS_pnts.separation(Coords_s).deg
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
114
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
115 mask = np.where((seps < Radius) & (Tstart > T1) & (Tstop < T2))[0]
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
116 OBSlist = []
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
117 for i in mask:
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
118 OBSlist.append(DL3_files[i])
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
119 if len(OBSlist) == 0:
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
120 message = "No data found"
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
121 raise RuntimeError("No data found")
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
122 offaxis = seps[mask]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
123 Tstart = np.array(Tstart)[mask]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
124 print("Found", len(Tstart), "pointings")
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
125
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
126 ind = 0
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
127 pointing = OBSlist[ind]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
128 hdul = fits.open("data/" + pointing)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
129 RMF = hdul["EDISP"].data
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
130 ENERG_LO = RMF["ENERG_LO"][0]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
131 ENERG_HI = RMF["ENERG_HI"][0]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
132 MIGRA_LO = RMF["MIGRA_LO"][0] # MIGRA_bins=np.linspace(0.2,5,161)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
133 MIGRA_HI = RMF["MIGRA_HI"][0]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
134 MIGRA = (MIGRA_LO + MIGRA_HI) / 2.0
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
135 ENERG = sqrt(ENERG_LO * ENERG_HI)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
136 dENERG = ENERG_HI - ENERG_LO
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
137
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
138 cts_s = []
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
139 cts_b = []
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
140 Eff_area = []
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
141 Eff_area_interp = []
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
142 Texp = []
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
143 RMFs = []
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
144 for ind in range(len(OBSlist)):
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
145 pointing = OBSlist[ind]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
146 hdul = fits.open("data/" + pointing)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
147
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
148 RA_pnt = hdul[1].header["RA_PNT"]
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
149 DEC_pnt = hdul[1].header["DEC_PNT"]
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
150 Texp.append(hdul[1].header["LIVETIME"])
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
151 dRA = RA - RA_pnt
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
152 dDEC = DEC - DEC_pnt
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
153 RA_b = RA_pnt - dRA
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
154 DEC_b = DEC_pnt - dDEC
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
155 Coords_b = SkyCoord(RA_b, DEC_b, unit="degree")
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
156 Coords_pnt = SkyCoord(RA_pnt, DEC_pnt, unit="degree")
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
157 dist = Coords_pnt.separation(Coords_s).deg
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
158
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
159 RMF = hdul["EDISP"].data
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
160 mask = RMF["THETA_LO"] < dist
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
161 ind_th = len(RMF["THETA_LO"][mask]) - 1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
162 RMF_th = RMF["MATRIX"][0][ind_th]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
163 RMF_interp = np.zeros((len(Emeans), len(ENERG)))
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
164 for k in range(len(ENERG)):
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
165 dp_dErec = RMF_th[:, k] / ENERG[k]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
166 Erec = MIGRA * ENERG[k]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
167 dp_dErec_interp = (
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
168 np.interp(Emeans, Erec, dp_dErec)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
169 * (Emeans > min(Erec))
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
170 * (Emeans < max(Erec))
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
171 )
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
172 RMF_interp[:, k] = dp_dErec_interp
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
173 RMFs.append(RMF_interp)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
174
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
175 AEFF = hdul["AEFF"].data
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
176 Eff_area.append(AEFF["EFFAREA"][0][ind_th])
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
177 Eff_area_interp.append(np.interp(Emeans, ENERG, Eff_area[-1]))
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
178
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
179 ev = hdul["EVENTS"].data
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
180 ev_ra = ev["RA"]
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
181 ev_dec = ev["DEC"]
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
182 ev_en = ev["ENERGY"]
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
183 ev_time = ev["TIME"]
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
184 ev_coords = SkyCoord(ev_ra, ev_dec, unit="degree")
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
185 sep_s = ev_coords.separation(Coords_s).deg
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
186 sep_b = ev_coords.separation(Coords_b).deg
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
187
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
188 mask = sep_s < R_s
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
189 cts_s.append(np.histogram(ev_en[mask], bins=Ebins)[0])
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
190 mask = sep_b < R_s
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
191 cts_b.append(np.histogram(ev_en[mask], bins=Ebins)[0])
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
192 hdul.close()
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
193
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
194 cts_s = np.array(cts_s)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
195 cts_b = np.array(cts_b)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
196 Eff_area = np.array(Eff_area)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
197 Eff_area_interp = np.array(Eff_area_interp)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
198 Texp = np.array(Texp)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
199
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
200 cts_s_tot = sum(cts_s)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
201 cts_b_tot = sum(cts_b)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
202 src_tot = cts_s_tot - cts_b_tot
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
203 src_tot_err = sqrt(cts_s_tot + cts_b_tot)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
204 Expos_tot_interp = sum(Eff_area_interp * np.outer(Texp, np.ones(NEbins))) * 1e4
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
205 flux_tot = src_tot / (Expos_tot_interp + 1) / (Emaxs - Emins) * Emaxs * Emins
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
206 flux_tot_err = (
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
207 src_tot_err / (Expos_tot_interp + 1) / (Emaxs - Emins) * Emaxs * Emins
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
208 )
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
209 print(
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
210 "Total source counts:",
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
211 sum(cts_s_tot),
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
212 "; background counts",
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
213 sum(cts_b_tot),
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
214 )
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
215
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
216 def model_cts_Erec(N, Gam):
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
217 model_ENERG = model_rate(ENERG_LO, ENERG_HI, N, Gam)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
218 res = np.zeros(NEbins)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
219 for ind in range(len(OBSlist)):
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
220 model_counts_ENERG = model_ENERG * Eff_area[ind] * 1e4 * Texp[ind]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
221 for k in range(len(ENERG)):
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
222 res += model_counts_ENERG[k] * RMFs[ind][:, k] * dE
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
223 return res
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
224
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
225 def chi2(p):
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
226 N, slope = p
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
227 counts = model_cts_Erec(N, slope)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
228 m = Emeans > Efit_min
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
229 m &= Emeans < Efit_max
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
230 m &= src_tot_err > 0.0
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
231 chi2 = (((counts[m] - src_tot[m]) / src_tot_err[m]) ** 2).sum()
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
232 dof = sum(m)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
233 # print(N,slope,chi2)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
234 return chi2, dof
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
235
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
236 plt.errorbar(
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
237 Emeans,
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
238 src_tot,
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
239 src_tot_err,
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
240 xerr=[Emeans - Emins, Emaxs - Emeans],
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
241 linestyle="none",
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
242 )
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
243
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
244 plt.axvline(Efit_min, color="black")
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
245 plt.axvline(Efit_max, color="black")
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
246 plt.xscale("log")
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
247 plt.yscale("log")
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
248 N = 4e-11
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
249 Gam = -2.7
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
250 plt.plot(Emeans, model_cts_Erec(N, Gam))
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
251 chi2([N, Gam])[0]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
252
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
253 # 1) find 90% confidence contour scanning over a wide parameter space
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
254 Norm_max = 1e-10
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
255 Norm_min = 1e-12
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
256 Norm_bins = 100
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
257 Gam_min = -1.0
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
258 Gam_max = -5.0
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
259 Gam_bins = 100
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
260 Ns = np.linspace(Norm_min, Norm_max, Norm_bins)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
261 Gams = np.linspace(Gam_min, Gam_max, Gam_bins)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
262 chi2_map = np.zeros((Norm_bins, Gam_bins))
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
263 Norm_best = Norm_min
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
264 Gam_best = Gam_min
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
265 chi2_best = 1e10
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
266 for i, N in enumerate(Ns):
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
267 pr.report_progress(stage="Progress", progress=5 + 45 * i / Norm_bins)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
268 for j, Gam in enumerate(Gams):
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
269 chi2_map[i, j] = chi2([N, Gam])[0]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
270 if chi2_map[i, j] < chi2_best:
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
271 Norm_best = N
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
272 Gam_best = Gam
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
273 chi2_best = chi2_map[i, j]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
274 print(Norm_best, Gam_best)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
275 # plt.imshow(chi2_map,vmax=np.amin(chi2_map)+4,origin='lower',extent=[Gams[0],Gams[-1],Ns[0],Ns[-1]],aspect=(Gams[-1]-Gams[0])/(Ns[-1]-Ns[0]))
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
276
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
277 # 68% contour from https://ui.adsabs.harvard.edu/abs/1976ApJ...208..177L/abstract for two-parameter fit
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
278 # 90% contour from https://ui.adsabs.harvard.edu/abs/1976ApJ...208..177L/abstract for two-parameter fit
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
279
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
280 cnt = plt.contour(
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
281 Gams, Ns, chi2_map, levels=[np.amin(chi2_map) + 4.61], colors="red"
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
282 )
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
283 plt.scatter([Gam_best], [Norm_best], marker="x", color="red")
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
284 # plt.colorbar()
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
285 print(np.amin(chi2_map))
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
286
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
287 cont = cnt.get_paths()[0].vertices
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
288 gammas = cont[:, 0]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
289 norms = cont[:, 1]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
290
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
291 # 2) refine with 68% contour calculation within the initial 90% countour
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
292 Norm_max = max(1.1 * norms)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
293 Norm_min = min(0.9 * norms)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
294 Norm_bins = 50
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
295 Gam_min = min(gammas - 0.2)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
296 Gam_max = max(gammas + 0.2)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
297 Gam_bins = 50
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
298 Ns = np.linspace(Norm_min, Norm_max, Norm_bins)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
299 Gams = np.linspace(Gam_min, Gam_max, Gam_bins)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
300 chi2_map = np.zeros((Norm_bins, Gam_bins))
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
301 Norm_best = Norm_min
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
302 Gam_best = Gam_min
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
303 chi2_best = 1e10
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
304 for i, N in enumerate(Ns):
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
305 pr.report_progress(stage="Progress", progress=50 + 50 * i / Norm_bins)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
306 for j, Gam in enumerate(Gams):
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
307 chi2_map[i, j] = chi2([N, Gam])[0]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
308 if chi2_map[i, j] < chi2_best:
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
309 Norm_best = N
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
310 Gam_best = Gam
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
311 chi2_best = chi2_map[i, j]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
312 print(Norm_best, Gam_best)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
313 # plt.imshow(chi2_map,vmax=np.amin(chi2_map)+4,origin='lower',extent=[Gams[0],Gams[-1],Ns[0],Ns[-1]],aspect=(Gams[-1]-Gams[0])/(Ns[-1]-Ns[0]))
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
314
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
315 # 68% contour from https://ui.adsabs.harvard.edu/abs/1976ApJ...208..177L/abstract for two-parameter fit
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
316 cnt = plt.contour(
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
317 Gams, Ns, chi2_map, levels=[np.amin(chi2_map) + 2.3], colors="black"
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
318 )
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
319 cont = cnt.get_paths()[0].vertices
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
320 gammas = cont[:, 0]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
321 norms = cont[:, 1]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
322
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
323 plt.scatter([Gam_best], [Norm_best], marker="x", color="black")
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
324 # plt.colorbar()
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
325 print(chi2([Norm_best, Gam_best]))
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
326 plt.xlabel("Slope")
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
327 plt.ylabel(r"Norm, 1/(TeV cm$^2$ s)")
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
328 plt.savefig("Contour.png", format="png", bbox_inches="tight")
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
329
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
330 plt.figure(figsize=(8, 6))
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
331 x = np.logspace(log10(Efit_min), log10(Efit_max), 10)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
332 ymax = np.zeros(10)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
333 ymin = np.ones(10)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
334 for i in range(len(gammas)):
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
335 y = model_dNdE(x, norms[i], gammas[i]) * x**2
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
336 ymax = np.maximum(y, ymax)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
337 ymin = np.minimum(y, ymin)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
338 # plt.plot(x,y)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
339 plt.fill_between(x, ymin, ymax, alpha=0.2)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
340 plt.plot(x, model_dNdE(x, Norm_best, Gam_best) * x**2)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
341
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
342 plt.errorbar(
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
343 Emeans,
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
344 flux_tot,
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
345 flux_tot_err,
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
346 xerr=[Emeans - Emins, Emaxs - Emeans],
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
347 linestyle="none",
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
348 color="black",
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
349 linewidth=2,
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
350 )
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
351
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
352 # plt.axvspan(0, Efit_min, alpha=0.2, color='black')
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
353 # plt.axvspan(Efit_max, 1000, alpha=0.2, color='black')
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
354
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
355 plt.xscale("log")
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
356 plt.yscale("log")
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
357 plt.xlim(0.1, 100)
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
358 plt.xlabel("$E$, TeV")
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
359 plt.ylabel("$E^2 dN/dE$, TeV/(cm$^2$ s)")
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
360 plt.savefig("Spectrum.png", format="png", bbox_inches="tight")
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
361
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
362 print("Here")
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
363 new_hdul = fits.HDUList()
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
364 form = str(len(ENERG)) + "E"
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
365
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
366 for i in range(len(OBSlist)):
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
367 col1 = fits.Column(name="COUNTS", format="E", array=cts_s[i])
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
368 col2 = fits.Column(name="BACKGROUND", format="E", array=cts_b[i])
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
369 hdu = fits.BinTableHDU.from_columns([col1, col2], name="COUNTS")
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
370 new_hdul.append(hdu)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
371 col = fits.Column(format=form, array=RMFs[i], name="RMF")
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
372 hdu = fits.BinTableHDU.from_columns([col], name="RMF")
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
373 new_hdul.append(hdu)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
374 col = fits.Column(format="E", array=Eff_area[i], name="ARF")
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
375 hdu = fits.BinTableHDU.from_columns([col], name="ARF")
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
376 new_hdul.append(hdu)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
377
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
378 col1 = fits.Column(name="E_MIN", format="E", unit="TeV", array=Emins)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
379 col2 = fits.Column(name="E_MAX", format="E", unit="TeV", array=Emaxs)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
380 cols = fits.ColDefs([col1, col2])
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
381 hdu = fits.BinTableHDU.from_columns(cols, name="Erec_BOUNDS")
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
382 new_hdul.append(hdu)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
383 col1 = fits.Column(name="ENERG_LO", format="E", unit="TeV", array=ENERG_LO)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
384 col2 = fits.Column(name="ENERG_HI", format="E", unit="TeV", array=ENERG_HI)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
385 cols = fits.ColDefs([col1, col2])
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
386 hdu = fits.BinTableHDU.from_columns(cols, name="Etrue_BOUNDS")
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
387 new_hdul.append(hdu)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
388 # new_hdul.writeto('Spectra.fits',overwrite=True)
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
389
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
390 print("and here")
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
391 bin_image = PictureProduct.from_file("Spectrum.png")
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
392 bin_image1 = PictureProduct.from_file("Contour.png")
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
393 from astropy.table import Table
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
394
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
395 data = [Emeans, Emins, Emaxs, flux_tot, flux_tot_err]
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
396 names = (
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
397 "Emean[TeV]",
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
398 "Emin[TeV]",
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
399 "Emax[TeV]",
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
400 "Flux[TeV/cm2s]",
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
401 "Flux_error[TeV/cm2s]",
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
402 )
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
403 spec = ODAAstropyTable(Table(data, names=names))
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
404
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
405 data = [
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
406 np.concatenate(([Gam_best], gammas)),
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
407 np.concatenate(([Norm_best], norms)),
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
408 ]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
409 names = ["Gamma", "Norm_1TeV[1/(TeV cm2 s)]"]
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
410 error_ellipse = ODAAstropyTable(Table(data, names=names))
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
411
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
412 png = bin_image # http://odahub.io/ontology#ODAPictureProduct
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
413 table_confidence_contour = (
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
414 error_ellipse # http://odahub.io/ontology#ODAAstropyTable
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
415 )
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
416 table_spectrum = spec # http://odahub.io/ontology#ODAAstropyTable
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
417
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
418 # output gathering
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
419 _galaxy_meta_data = {}
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
420 _oda_outs = []
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
421 _oda_outs.append(("out_Spectrum_png", "png_galaxy.output", png))
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
422 _oda_outs.append(
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
423 (
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
424 "out_Spectrum_table_confidence_contour",
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
425 "table_confidence_contour_galaxy.output",
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
426 table_confidence_contour,
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
427 )
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
428 )
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
429 _oda_outs.append(
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
430 (
1
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
431 "out_Spectrum_table_spectrum",
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
432 "table_spectrum_galaxy.output",
593c4b45eda5 planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2f23ec010eab8d33d2760f061286756e17015af7
astroteam
parents: 0
diff changeset
433 table_spectrum,
0
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
434 )
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
435 )
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
436
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
437 for _outn, _outfn, _outv in _oda_outs:
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
438 _galaxy_outfile_name = os.path.join(_galaxy_wd, _outfn)
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
439 if isinstance(_outv, str) and os.path.isfile(_outv):
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
440 shutil.move(_outv, _galaxy_outfile_name)
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
441 _galaxy_meta_data[_outn] = {"ext": "_sniff_"}
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
442 elif getattr(_outv, "write_fits_file", None):
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
443 _outv.write_fits_file(_galaxy_outfile_name)
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
444 _galaxy_meta_data[_outn] = {"ext": "fits"}
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
445 elif getattr(_outv, "write_file", None):
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
446 _outv.write_file(_galaxy_outfile_name)
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
447 _galaxy_meta_data[_outn] = {"ext": "_sniff_"}
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
448 else:
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
449 with open(_galaxy_outfile_name, "w") as fd:
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
450 json.dump(_outv, fd, cls=CustomJSONEncoder)
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
451 _galaxy_meta_data[_outn] = {"ext": "json"}
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
452
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
453 with open(os.path.join(_galaxy_wd, "galaxy.json"), "w") as fd:
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
454 json.dump(_galaxy_meta_data, fd)
02e4bb4fa10c planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit 2991f65b25d4e6d1b69458603fce917adff40f94
astroteam
parents:
diff changeset
455 print("*** Job finished successfully ***")