Commit fb6981b726c5f9212847a66bfc991ba399d6f81f
1 parent
83ee3aec
Remove code duplication in csresmap and csresspec
Showing
3 changed files
with
62 additions
and
49 deletions
cscripts/csresmap.py
| @@ -2,7 +2,7 @@ | @@ -2,7 +2,7 @@ | ||
| 2 | # ========================================================================== | 2 | # ========================================================================== |
| 3 | # Generates a residual map. | 3 | # Generates a residual map. |
| 4 | # | 4 | # |
| 5 | -# Copyright (C) 2014-2018 Michael Mayer | 5 | +# Copyright (C) 2014-2019 Michael Mayer |
| 6 | # | 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify | 7 | # This program is free software: you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by | 8 | # it under the terms of the GNU General Public License as published by |
| @@ -165,29 +165,8 @@ class csresmap(ctools.csobservation): | @@ -165,29 +165,8 @@ class csresmap(ctools.csobservation): | ||
| 165 | # Write header | 165 | # Write header |
| 166 | self._log_header1(gammalib.TERSE, 'Generate binned map (ctbin)') | 166 | self._log_header1(gammalib.TERSE, 'Generate binned map (ctbin)') |
| 167 | 167 | ||
| 168 | - # Create countsmap | ||
| 169 | - binning = ctools.ctbin(self.obs()) | ||
| 170 | - binning['xref'] = self['xref'].real() | ||
| 171 | - binning['yref'] = self['yref'].real() | ||
| 172 | - binning['proj'] = self['proj'].string() | ||
| 173 | - binning['coordsys'] = self['coordsys'].string() | ||
| 174 | - binning['ebinalg'] = self['ebinalg'].string() | ||
| 175 | - binning['nxpix'] = self['nxpix'].integer() | ||
| 176 | - binning['nypix'] = self['nypix'].integer() | ||
| 177 | - binning['binsz'] = self['binsz'].real() | ||
| 178 | - if self['ebinalg'].string() == 'FILE': | ||
| 179 | - binning['ebinfile'] = self['ebinfile'].filename().file() | ||
| 180 | - else: | ||
| 181 | - binning['enumbins'] = self['enumbins'].integer() | ||
| 182 | - binning['emin'] = self['emin'].real() | ||
| 183 | - binning['emax'] = self['emax'].real() | ||
| 184 | - binning['chatter'] = self['chatter'].integer() | ||
| 185 | - binning['clobber'] = self['clobber'].boolean() | ||
| 186 | - binning['debug'] = self['debug'].boolean() | ||
| 187 | - binning.run() | ||
| 188 | - | ||
| 189 | - # Retrieve counts cube | ||
| 190 | - cta_counts_cube = binning.cube() | 168 | + # Create counts cube |
| 169 | + cta_counts_cube = obsutils.create_counts_cube(self, self.obs()) | ||
| 191 | 170 | ||
| 192 | # Assign GCTAEventCube to skymap | 171 | # Assign GCTAEventCube to skymap |
| 193 | countmap = cta_counts_cube.counts() | 172 | countmap = cta_counts_cube.counts() |
cscripts/csresspec.py
| @@ -18,10 +18,10 @@ | @@ -18,10 +18,10 @@ | ||
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | # | 19 | # |
| 20 | # ========================================================================== | 20 | # ========================================================================== |
| 21 | +import sys | ||
| 21 | import gammalib | 22 | import gammalib |
| 22 | import ctools | 23 | import ctools |
| 23 | from cscripts import obsutils | 24 | from cscripts import obsutils |
| 24 | -import sys | ||
| 25 | 25 | ||
| 26 | 26 | ||
| 27 | # =============== # | 27 | # =============== # |
| @@ -672,35 +672,18 @@ class csresspec(ctools.csobservation): | @@ -672,35 +672,18 @@ class csresspec(ctools.csobservation): | ||
| 672 | # Log processing header | 672 | # Log processing header |
| 673 | self._log_header1(gammalib.TERSE, 'Processing Observations') | 673 | self._log_header1(gammalib.TERSE, 'Processing Observations') |
| 674 | 674 | ||
| 675 | - #If observations are unbinned and we stack | 675 | + # If observations are unbinned and we stack |
| 676 | if self._stack and self.obs()[0].classname() == 'GCTAObservation': | 676 | if self._stack and self.obs()[0].classname() == 'GCTAObservation': |
| 677 | 677 | ||
| 678 | + # Log counts cube computation | ||
| 678 | msg = 'Computing count cube from multiple unbinned observations' | 679 | msg = 'Computing count cube from multiple unbinned observations' |
| 679 | self._log_string(gammalib.NORMAL, msg) | 680 | self._log_string(gammalib.NORMAL, msg) |
| 680 | 681 | ||
| 681 | - # Build count cube | ||
| 682 | - binning = ctools.ctbin(self.obs()) | ||
| 683 | - binning['xref'] = self['xref'].real() | ||
| 684 | - binning['yref'] = self['yref'].real() | ||
| 685 | - binning['proj'] = self['proj'].string() | ||
| 686 | - binning['coordsys'] = self['coordsys'].string() | ||
| 687 | - binning['ebinalg'] = self['ebinalg'].string() | ||
| 688 | - binning['nxpix'] = self['nxpix'].integer() | ||
| 689 | - binning['nypix'] = self['nypix'].integer() | ||
| 690 | - binning['binsz'] = self['binsz'].real() | ||
| 691 | - if self['ebinalg'].string() == 'FILE': | ||
| 692 | - binning['ebinfile'] = self['ebinfile'].filename().file() | ||
| 693 | - else: | ||
| 694 | - binning['enumbins'] = self['enumbins'].integer() | ||
| 695 | - binning['emin'] = self['emin'].real() | ||
| 696 | - binning['emax'] = self['emax'].real() | ||
| 697 | - binning['chatter'] = self['chatter'].integer() | ||
| 698 | - binning['clobber'] = self['clobber'].boolean() | ||
| 699 | - binning['debug'] = self['debug'].boolean() | ||
| 700 | - binning.run() | ||
| 701 | - | ||
| 702 | - #compute residuals using cube | ||
| 703 | - table = self._residuals_3D(self.obs(),'',binning.cube()) | 682 | + # Create counts cube |
| 683 | + cta_counts_cube = obsutils.create_counts_cube(self, self.obs()) | ||
| 684 | + | ||
| 685 | + # Compute residuals using cube | ||
| 686 | + table = self._residuals_3D(self.obs(), '', cta_counts_cube) | ||
| 704 | 687 | ||
| 705 | # Append results table to output file | 688 | # Append results table to output file |
| 706 | self._fits.append(table) | 689 | self._fits.append(table) |
cscripts/obsutils.py
| @@ -937,3 +937,54 @@ def residuals(cls, counts, model): | @@ -937,3 +937,54 @@ def residuals(cls, counts, model): | ||
| 937 | 937 | ||
| 938 | # Return | 938 | # Return |
| 939 | return residuals | 939 | return residuals |
| 940 | + | ||
| 941 | + | ||
| 942 | +# ================= # | ||
| 943 | +# Create counts map # | ||
| 944 | +# ================= # | ||
| 945 | +def create_counts_cube(cls, obs): | ||
| 946 | + """ | ||
| 947 | + Create counts cube from observations | ||
| 948 | + | ||
| 949 | + Parameters | ||
| 950 | + ---------- | ||
| 951 | + cls : `~ctools.cscript` | ||
| 952 | + cscript class | ||
| 953 | + obs : `~gammalib.GObservations` | ||
| 954 | + Observation container | ||
| 955 | + | ||
| 956 | + Returns | ||
| 957 | + ------- | ||
| 958 | + cntcube : `~gammalib.GCTAEventCube` | ||
| 959 | + Counts cube | ||
| 960 | + """ | ||
| 961 | + # Initialise ctbin | ||
| 962 | + ctbin = ctools.ctbin(obs) | ||
| 963 | + | ||
| 964 | + # Set parameters | ||
| 965 | + ctbin['xref'] = cls['xref'].real() | ||
| 966 | + ctbin['yref'] = cls['yref'].real() | ||
| 967 | + ctbin['proj'] = cls['proj'].string() | ||
| 968 | + ctbin['coordsys'] = cls['coordsys'].string() | ||
| 969 | + ctbin['ebinalg'] = cls['ebinalg'].string() | ||
| 970 | + ctbin['nxpix'] = cls['nxpix'].integer() | ||
| 971 | + ctbin['nypix'] = cls['nypix'].integer() | ||
| 972 | + ctbin['binsz'] = cls['binsz'].real() | ||
| 973 | + if cls['ebinalg'].string() == 'FILE': | ||
| 974 | + ctbin['ebinfile'] = cls['ebinfile'].filename().file() | ||
| 975 | + else: | ||
| 976 | + ctbin['enumbins'] = cls['enumbins'].integer() | ||
| 977 | + ctbin['emin'] = cls['emin'].real() | ||
| 978 | + ctbin['emax'] = cls['emax'].real() | ||
| 979 | + ctbin['chatter'] = cls['chatter'].integer() | ||
| 980 | + ctbin['clobber'] = cls['clobber'].boolean() | ||
| 981 | + ctbin['debug'] = cls['debug'].boolean() | ||
| 982 | + | ||
| 983 | + # Run ctbin | ||
| 984 | + ctbin.run() | ||
| 985 | + | ||
| 986 | + # Retrieve counts cube | ||
| 987 | + cntcube = ctbin.cube().copy() | ||
| 988 | + | ||
| 989 | + # Return counts cube | ||
| 990 | + return cntcube |